2 * drivers/net/can/softing/softing_main.c
6 * - Kurt Van Dijck, EIA Electronics
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the version 2 of the GNU General Public License
10 * as published by the Free Software Foundation
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include <linux/version.h>
23 #include <linux/module.h>
24 #include <linux/init.h>
25 #include <linux/device.h>
26 #include <linux/interrupt.h>
27 #include <linux/mutex.h>
32 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
33 #error This driver does not support Kernel versions < 2.6.23
36 #define TX_ECHO_SKB_MAX 4
39 * test is a specific CAN netdev
40 * is online (ie. up 'n running, not sleeping, not busoff
42 static inline int canif_is_active(struct net_device *netdev)
44 struct can_priv *can = netdev_priv(netdev);
45 if (!netif_running(netdev))
47 return (can->state <= CAN_STATE_ERROR_PASSIVE);
50 /* trigger the tx queue-ing */
51 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,31)
52 static int netdev_start_xmit(struct sk_buff *skb, struct net_device *dev)
54 static netdev_tx_t netdev_start_xmit(struct sk_buff *skb,
55 struct net_device *dev)
58 struct softing_priv *priv = netdev_priv(dev);
59 struct softing *card = priv->card;
69 spin_lock(&card->spin);
72 spin_lock_bh(&card->spin);
77 if (card->tx.pending >= TXMAX)
79 if (priv->tx.pending >= TX_ECHO_SKB_MAX)
81 fifo_wr = card->dpram.tx->wr;
82 if (fifo_wr == card->dpram.tx->rd)
85 memcpy(&msg, skb->data, sizeof(msg));
86 ptr = &card->dpram.tx->fifo[fifo_wr][0];
88 if (msg.can_id & CAN_RTR_FLAG)
90 if (msg.can_id & CAN_EFF_FLAG)
96 *ptr++ = (msg.can_id >> 0);
97 *ptr++ = (msg.can_id >> 8);
98 if (msg.can_id & CAN_EFF_FLAG) {
99 *ptr++ = (msg.can_id >> 16);
100 *ptr++ = (msg.can_id >> 24);
102 /*increment 1, not 2 as you might think */
105 if (!(msg.can_id & CAN_RTR_FLAG))
106 memcpy_toio(ptr, &msg.data[0], msg.can_dlc);
108 sizeof(card->dpram.tx->fifo) /
109 sizeof(card->dpram.tx->fifo[0]))
111 card->dpram.tx->wr = fifo_wr;
112 card->tx.last_bus = priv->index;
115 can_put_echo_skb(skb, dev, priv->tx.echo_put);
117 if (priv->tx.echo_put >= TX_ECHO_SKB_MAX)
118 priv->tx.echo_put = 0;
119 /* can_put_echo_skb() saves the skb, safe to return TX_OK */
123 spin_unlock_bh(&card->spin);
125 spin_unlock(&card->spin);
126 if (card->tx.pending >= TXMAX) {
127 struct softing_priv *bus;
129 for (j = 0; j < card->nbus; ++j) {
133 netif_stop_queue(bus->netdev);
136 if (ret != NETDEV_TX_OK)
137 netif_stop_queue(dev);
142 int softing_rx(struct net_device *netdev, const struct can_frame *msg,
147 struct net_device_stats *stats;
149 skb = dev_alloc_skb(sizeof(msg));
153 skb->protocol = htons(ETH_P_CAN);
154 skb->ip_summed = CHECKSUM_UNNECESSARY;
155 memcpy(skb_put(skb, sizeof(*msg)), msg, sizeof(*msg));
158 if (ret == NET_RX_DROP) {
159 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
160 stats = can_get_stats(netdev);
162 stats = &netdev->stats;
169 static int softing_dev_svc_once(struct softing *card)
172 struct softing_priv *bus;
174 struct can_frame msg;
176 unsigned int fifo_rd;
177 unsigned int cnt = 0;
178 struct net_device_stats *stats;
183 memset(&msg, 0, sizeof(msg));
184 if (card->dpram.rx->lost_msg) {
186 card->dpram.rx->lost_msg = 0;
188 msg.can_id = CAN_ERR_FLAG | CAN_ERR_CRTL;
189 msg.can_dlc = CAN_ERR_DLC;
190 msg.data[1] = CAN_ERR_CRTL_RX_OVERFLOW;
192 * service to all busses, we don't know which it was applicable
193 * but only service busses that are online
195 for (j = 0; j < card->nbus; ++j) {
199 if (!canif_is_active(bus->netdev))
200 /* a dead bus has no overflows */
202 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
203 stats = can_get_stats(bus->netdev);
205 stats = &bus->netdev->stats;
207 ++stats->rx_over_errors;
208 softing_rx(bus->netdev, &msg, ktime_set(0, 0));
210 /* prepare for other use */
211 memset(&msg, 0, sizeof(msg));
215 fifo_rd = card->dpram.rx->rd;
216 if (++fifo_rd >= ARRAY_SIZE(card->dpram.rx->fifo))
219 if (card->dpram.rx->wr == fifo_rd)
222 ptr = &card->dpram.rx->fifo[fifo_rd][0];
226 /*not quite usefull, probably the card has got out */
227 dev_alert(card->dev, "got cmd 0x%02x,"
228 " I suspect the card is lost\n", cmd);
230 /*mod_trace("0x%02x", cmd);*/
235 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
236 stats = can_get_stats(bus->netdev);
238 stats = &bus->netdev->stats;
245 msg.can_id = CAN_ERR_FLAG;
246 msg.can_dlc = CAN_ERR_DLC;
249 can_state = CAN_STATE_BUS_OFF;
250 msg.can_id |= CAN_ERR_BUSOFF;
252 } else if (state & 0x60) {
253 can_state = CAN_STATE_ERROR_PASSIVE;
254 msg.can_id |= CAN_ERR_BUSERROR;
255 msg.data[1] = CAN_ERR_CRTL_TX_PASSIVE;
258 can_state = CAN_STATE_ERROR_ACTIVE;
260 msg.can_id |= CAN_ERR_BUSERROR;
264 card->dpram.info->bus_state = state;
266 card->dpram.info->bus_state2 = state;
268 tmp = (ptr[0] << 0) | (ptr[1] << 8)
269 | (ptr[2] << 16) | (ptr[3] << 24);
271 ktime = softing_raw2ktime(card, tmp);
272 /*trigger dual port RAM */
274 card->dpram.rx->rd = fifo_rd;
276 ++bus->can.can_stats.bus_error;
278 /*update internal status */
279 if (can_state != bus->can.state) {
280 bus->can.state = can_state;
281 if (can_state == CAN_STATE_ERROR_PASSIVE)
282 ++bus->can.can_stats.error_passive;
283 if (can_state == CAN_STATE_BUS_OFF) {
284 /* this calls can_close_cleanup() */
285 can_bus_off(bus->netdev);
286 netif_stop_queue(bus->netdev);
288 /*trigger socketcan */
289 softing_rx(bus->netdev, &msg, ktime);
294 msg.can_id |= CAN_RTR_FLAG;
295 /* acknowledge, was tx msg
296 * no real tx flag to set
300 msg.can_dlc = *ptr++;
304 msg.can_id |= CAN_EFF_FLAG;
305 msg.can_id |= (ptr[0] << 0) | (ptr[1] << 8)
306 | (ptr[2] << 16) | (ptr[3] << 24);
309 msg.can_id |= (ptr[0] << 0) | (ptr[1] << 8);
312 tmp = (ptr[0] << 0) | (ptr[1] << 8)
313 | (ptr[2] << 16) | (ptr[3] << 24);
315 ktime = softing_raw2ktime(card, tmp);
316 memcpy_fromio(&msg.data[0], ptr, 8);
318 /*trigger dual port RAM */
320 card->dpram.rx->rd = fifo_rd;
324 skb = bus->can.echo_skb[bus->tx.echo_get];
327 can_get_echo_skb(bus->netdev, bus->tx.echo_get);
329 if (bus->tx.echo_get >= TX_ECHO_SKB_MAX)
330 bus->tx.echo_get = 0;
333 if (card->tx.pending)
336 stats->tx_bytes += msg.can_dlc;
339 stats->rx_bytes += msg.can_dlc;
340 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,31)
341 bus->netdev->last_rx = jiffies;
343 softing_rx(bus->netdev, &msg, ktime);
350 static void softing_dev_svc(unsigned long param)
352 struct softing *card = (struct softing *)param;
353 struct softing_priv *bus;
357 spin_lock(&card->spin);
358 while (softing_dev_svc_once(card) > 0)
359 ++card->irq.svc_count;
360 spin_unlock(&card->spin);
361 /*resume tx queue's */
362 offset = card->tx.last_bus;
363 for (j = 0; j < card->nbus; ++j) {
364 if (card->tx.pending >= TXMAX)
366 bus = card->bus[(j + offset + 1) % card->nbus];
369 if (!canif_is_active(bus->netdev))
370 /* it makes no sense to wake dead busses */
372 if (bus->tx.pending >= TX_ECHO_SKB_MAX)
374 netif_wake_queue(bus->netdev);
379 irqreturn_t dev_interrupt_shared(int irq, void *dev_id)
381 struct softing *card = (struct softing *)dev_id;
383 ir = card->dpram.virt[0xe02];
384 card->dpram.virt[0xe02] = 0;
385 if (card->dpram.rx->rd == 0xffff) {
386 dev_alert(card->dev, "I think the card is gone\n");
390 tasklet_schedule(&card->irq.bh);
392 } else if (ir == 0x10) {
400 irqreturn_t dev_interrupt_nshared(int irq, void *dev_id)
402 struct softing *card = (struct softing *)dev_id;
403 unsigned char irq_host;
404 irq_host = card->dpram.irq->to_host;
405 /* make sure we have a copy, before clearing the variable in DPRAM */
407 card->dpram.irq->to_host = 0;
408 /* make sure we cleared it */
410 if (card->dpram.rx->rd == 0xffff) {
411 dev_alert(card->dev, "I think the card is gone\n");
414 tasklet_schedule(&card->irq.bh);
418 static int netdev_open(struct net_device *ndev)
420 struct softing_priv *priv = netdev_priv(ndev);
421 struct softing *card = priv->card;
424 /* check or determine and set bittime */
425 ret = open_candev(ndev);
428 ret = softing_cycle(card, priv, 1);
431 netif_start_queue(ndev);
437 static int netdev_stop(struct net_device *ndev)
439 struct softing_priv *priv = netdev_priv(ndev);
440 struct softing *card = priv->card;
443 netif_stop_queue(ndev);
445 /* softing cycle does close_candev() */
446 ret = softing_cycle(card, priv, 0);
450 static int candev_set_mode(struct net_device *ndev, enum can_mode mode)
452 struct softing_priv *priv = netdev_priv(ndev);
453 struct softing *card = priv->card;
458 /* softing cycle does close_candev() */
459 ret = softing_cycle(card, priv, 1);
468 /*assume the card->lock is held*/
470 int softing_card_irq(struct softing *card, int enable)
474 if (card->irq.requested && card->irq.nr) {
475 free_irq(card->irq.nr, card);
476 card->irq.requested = 0;
480 if (!card->irq.requested && (card->irq.nr)) {
481 irqreturn_t(*fn) (int, void *);
483 flags = IRQF_DISABLED | IRQF_SHARED;/*| IRQF_TRIGGER_LOW; */
484 fn = dev_interrupt_nshared;
485 if (card->desc->generation >= 2)
486 fn = dev_interrupt_shared;
487 ret = request_irq(card->irq.nr, fn, flags, card->id.name, card);
489 dev_alert(card->dev, "%s, request_irq(%u) failed\n",
490 card->id.name, card->irq.nr);
493 card->irq.requested = 1;
498 static void shutdown_card(struct softing *card)
501 dev_dbg(card->dev, "%s()\n", __func__);
502 if (mutex_lock_interruptible(&card->fw.lock))
503 /* return -ERESTARTSYS*/;
507 if (card->irq.requested && card->irq.nr) {
508 free_irq(card->irq.nr, card);
509 card->irq.requested = 0;
512 if (card->fn.enable_irq)
513 card->fn.enable_irq(card, 0);
515 card->fn.reset(card, 1);
517 mutex_unlock(&card->fw.lock);
518 tasklet_kill(&card->irq.bh);
521 static int boot_card(struct softing *card)
524 static const unsigned char stream[] =
525 { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, };
526 unsigned char back[sizeof(stream)];
527 dev_dbg(card->dev, "%s()\n", __func__);
529 if (mutex_lock_interruptible(&card->fw.lock))
532 mutex_unlock(&card->fw.lock);
537 if (card->fn.enable_irq)
538 card->fn.enable_irq(card, 1);
541 card->fn.reset(card, 1);
543 if (!card->dpram.virt)
545 for (lp = card->dpram.virt; &lp[sizeof(stream)] <= card->dpram.end;
546 lp += sizeof(stream)) {
548 memcpy_toio(lp, stream, sizeof(stream));
551 memcpy_fromio(back, lp, sizeof(stream));
553 if (!memcmp(back, stream, sizeof(stream)))
555 /* memory is not equal */
556 dev_alert(card->dev, "write to dpram failed at 0x%04lx\n",
557 (unsigned long)(lp - card->dpram.virt));
561 /*load boot firmware */
562 if (softing_load_fw(card->desc->boot.fw, card, card->dpram.virt,
564 card->desc->boot.offs -
565 card->desc->boot.addr))
567 /*load load firmware */
568 if (softing_load_fw(card->desc->load.fw, card, card->dpram.virt,
570 card->desc->load.offs -
571 card->desc->load.addr))
575 card->fn.reset(card, 0);
576 if (softing_bootloader_command(card, 0, "card boot"))
578 if (softing_load_app_fw(card->desc->app.fw, card))
581 card->dpram.info->reset_rcv_fifo = 0;
582 card->dpram.info->reset = 1;
584 if (softing_fct_cmd(card, 99, 0x55, "sync-a"))
586 if (softing_fct_cmd(card, 99, 0xaa, "sync-a"))
589 if (softing_fct_cmd(card, 0, 0, "reset_chip"))
592 if (softing_fct_cmd(card, 43, 0, "get_serial_number"))
595 (u16) card->dpram.fct->param[1] +
596 (((u16) card->dpram.fct->param[2]) << 16);
598 if (softing_fct_cmd(card, 12, 0, "get_version"))
600 card->id.fw = (u16) card->dpram.fct->param[1];
601 card->id.hw = (u16) card->dpram.fct->param[2];
602 card->id.lic = (u16) card->dpram.fct->param[3];
603 card->id.chip[0] = (u16) card->dpram.fct->param[4];
604 card->id.chip[1] = (u16) card->dpram.fct->param[5];
606 dev_info(card->dev, "card booted, type %s, "
607 "serial %u, fw %u, hw %u, lic %u, chip (%u,%u)\n",
608 card->id.name, card->id.serial, card->id.fw, card->id.hw,
609 card->id.lic, card->id.chip[0], card->id.chip[1]);
612 mutex_unlock(&card->fw.lock);
616 if (card->fn.enable_irq)
617 card->fn.enable_irq(card, 0);
619 card->fn.reset(card, 1);
620 mutex_unlock(&card->fw.lock);
624 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,28)
625 static const struct net_device_ops softing_netdev_ops = {
626 .ndo_open = netdev_open,
627 .ndo_stop = netdev_stop,
628 .ndo_start_xmit = netdev_start_xmit,
632 static struct softing_priv *mk_netdev(struct softing *card, u16 chip_id)
634 struct net_device *ndev;
635 struct softing_priv *priv;
637 ndev = alloc_candev(sizeof(*priv), TX_ECHO_SKB_MAX);
639 dev_alert(card->dev, "alloc_candev failed\n");
642 priv = netdev_priv(ndev);
645 memcpy(&priv->btr_const, &softing_btr_const, sizeof(priv->btr_const));
646 priv->btr_const.brp_max = card->desc->max_brp;
647 priv->btr_const.sjw_max = card->desc->max_sjw;
648 priv->can.bittiming_const = &priv->btr_const;
649 priv->can.clock.freq = 8000000;
650 priv->chip = chip_id;
651 priv->output = softing_default_output(card, priv);
652 SET_NETDEV_DEV(ndev, card->dev);
654 ndev->flags |= IFF_ECHO;
655 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,28)
656 ndev->netdev_ops = &softing_netdev_ops;
658 ndev->open = netdev_open;
659 ndev->stop = netdev_stop;
660 ndev->hard_start_xmit = netdev_start_xmit;
662 priv->can.do_set_mode = candev_set_mode;
667 static int reg_netdev(struct softing_priv *priv)
670 ret = register_candev(priv->netdev);
672 dev_alert(priv->card->dev, "%s, register failed\n",
673 priv->card->id.name);
676 ret = softing_bus_sysfs_create(priv);
678 dev_alert(priv->card->dev, "%s, sysfs failed\n",
679 priv->card->id.name);
684 unregister_candev(priv->netdev);
689 void rm_softing(struct softing *card)
693 /*first, disable card*/
696 for (j = 0; j < card->nbus; ++j) {
699 softing_bus_sysfs_remove(card->bus[j]);
700 unregister_candev(card->bus[j]->netdev);
701 free_candev(card->bus[j]->netdev);
705 softing_card_sysfs_remove(card);
707 iounmap(card->dpram.virt);
709 EXPORT_SYMBOL(rm_softing);
711 int mk_softing(struct softing *card)
715 /* try_module_get(THIS_MODULE); */
716 mutex_init(&card->fw.lock);
717 spin_lock_init(&card->spin);
718 tasklet_init(&card->irq.bh, softing_dev_svc, (unsigned long)card);
720 card->desc = softing_lookup_desc(card->id.manf, card->id.prod);
722 dev_alert(card->dev, "0x%04x:0x%04x not supported\n",
723 card->id.manf, card->id.prod);
726 card->id.name = card->desc->name;
728 card->dpram.virt = ioremap(card->dpram.phys, card->dpram.size);
729 if (!card->dpram.virt) {
730 dev_alert(card->dev, "dpram ioremap failed\n");
734 card->dpram.size = card->desc->dpram_size;
735 card->dpram.end = &card->dpram.virt[card->dpram.size];
736 /*initialize_board */
737 card->dpram.rx = (struct softing_rx *)&card->dpram.virt[0x0000];
738 card->dpram.tx = (struct softing_tx *)&card->dpram.virt[0x0400];
739 card->dpram.fct = (struct softing_fct *)&card->dpram.virt[0x0300];
740 card->dpram.info = (struct softing_info *)&card->dpram.virt[0x0330];
741 card->dpram.command = (unsigned short *)&card->dpram.virt[0x07e0];
742 card->dpram.receipt = (unsigned short *)&card->dpram.virt[0x07f0];
743 card->dpram.irq = (struct softing_irq *)&card->dpram.virt[0x07fe];
747 card->fn.reset(card, 1);
748 if (boot_card(card)) {
749 dev_alert(card->dev, "failed to boot\n");
753 /*only now, the chip's are known */
754 card->id.freq = card->desc->freq * 1000000UL;
756 if (softing_card_sysfs_create(card)) {
757 dev_alert(card->dev, "sysfs failed\n");
761 if (card->nbus > (sizeof(card->bus) / sizeof(card->bus[0]))) {
762 card->nbus = sizeof(card->bus) / sizeof(card->bus[0]);
763 dev_alert(card->dev, "have %u busses\n", card->nbus);
766 for (j = 0; j < card->nbus; ++j) {
767 card->bus[j] = mk_netdev(card, card->id.chip[j]);
769 dev_alert(card->dev, "failed to make can[%i]", j);
772 card->bus[j]->index = j;
774 for (j = 0; j < card->nbus; ++j) {
775 if (reg_netdev(card->bus[j])) {
777 "failed to register can[%i]\n", j);
781 dev_info(card->dev, "card initialised\n");
785 for (j = 0; j < card->nbus; ++j) {
788 softing_bus_sysfs_remove(card->bus[j]);
789 unregister_candev(card->bus[j]->netdev);
792 for (j = 0; j < card->nbus; ++j) {
795 free_candev(card->bus[j]->netdev);
798 softing_card_sysfs_remove(card);
802 iounmap(card->dpram.virt);
803 card->dpram.virt = 0;
807 tasklet_kill(&card->irq.bh);
810 EXPORT_SYMBOL(mk_softing);
812 static int __init mod_start(void)
814 printk(KERN_INFO "[%s] start\n", THIS_MODULE->name);
818 static void __exit mod_stop(void)
820 printk(KERN_INFO "[%s] stop\n", THIS_MODULE->name);
823 module_init(mod_start);
824 module_exit(mod_stop);
826 MODULE_DESCRIPTION("socketcan softing driver");
827 MODULE_AUTHOR("Kurt Van Dijck <kurt.van.dijck@eia.be>");
828 MODULE_LICENSE("GPL");
830 int softing_debug = 1;
831 EXPORT_SYMBOL(softing_debug);
832 module_param(softing_debug, int , S_IRUGO | S_IWUSR | S_IWGRP);
833 MODULE_PARM_DESC(softing_debug, "trace softing functions");