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,32)
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,
146 struct can_frame *cf;
148 struct net_device_stats *stats;
150 skb = alloc_can_skb(netdev, &cf);
153 memcpy(cf, msg, sizeof(*msg));
156 if (ret == NET_RX_DROP) {
157 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
158 stats = can_get_stats(netdev);
160 stats = &netdev->stats;
167 static int softing_dev_svc_once(struct softing *card)
170 struct softing_priv *bus;
172 struct can_frame msg;
174 unsigned int fifo_rd;
175 unsigned int cnt = 0;
176 struct net_device_stats *stats;
181 memset(&msg, 0, sizeof(msg));
182 if (card->dpram.rx->lost_msg) {
184 card->dpram.rx->lost_msg = 0;
186 msg.can_id = CAN_ERR_FLAG | CAN_ERR_CRTL;
187 msg.can_dlc = CAN_ERR_DLC;
188 msg.data[1] = CAN_ERR_CRTL_RX_OVERFLOW;
190 * service to all busses, we don't know which it was applicable
191 * but only service busses that are online
193 for (j = 0; j < card->nbus; ++j) {
197 if (!canif_is_active(bus->netdev))
198 /* a dead bus has no overflows */
200 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
201 stats = can_get_stats(bus->netdev);
203 stats = &bus->netdev->stats;
205 ++stats->rx_over_errors;
206 softing_rx(bus->netdev, &msg, ktime_set(0, 0));
208 /* prepare for other use */
209 memset(&msg, 0, sizeof(msg));
213 fifo_rd = card->dpram.rx->rd;
214 if (++fifo_rd >= ARRAY_SIZE(card->dpram.rx->fifo))
217 if (card->dpram.rx->wr == fifo_rd)
220 ptr = &card->dpram.rx->fifo[fifo_rd][0];
224 /*not quite usefull, probably the card has got out */
225 dev_alert(card->dev, "got cmd 0x%02x,"
226 " I suspect the card is lost\n", cmd);
228 /*mod_trace("0x%02x", cmd);*/
233 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
234 stats = can_get_stats(bus->netdev);
236 stats = &bus->netdev->stats;
243 msg.can_id = CAN_ERR_FLAG;
244 msg.can_dlc = CAN_ERR_DLC;
247 can_state = CAN_STATE_BUS_OFF;
248 msg.can_id |= CAN_ERR_BUSOFF;
250 } else if (state & 0x60) {
251 can_state = CAN_STATE_ERROR_PASSIVE;
252 msg.can_id |= CAN_ERR_BUSERROR;
253 msg.data[1] = CAN_ERR_CRTL_TX_PASSIVE;
256 can_state = CAN_STATE_ERROR_ACTIVE;
258 msg.can_id |= CAN_ERR_BUSERROR;
262 card->dpram.info->bus_state = state;
264 card->dpram.info->bus_state2 = state;
266 tmp = (ptr[0] << 0) | (ptr[1] << 8)
267 | (ptr[2] << 16) | (ptr[3] << 24);
269 ktime = softing_raw2ktime(card, tmp);
270 /*trigger dual port RAM */
272 card->dpram.rx->rd = fifo_rd;
274 ++bus->can.can_stats.bus_error;
276 /*update internal status */
277 if (can_state != bus->can.state) {
278 bus->can.state = can_state;
279 if (can_state == CAN_STATE_ERROR_PASSIVE)
280 ++bus->can.can_stats.error_passive;
281 if (can_state == CAN_STATE_BUS_OFF) {
282 /* this calls can_close_cleanup() */
283 can_bus_off(bus->netdev);
284 netif_stop_queue(bus->netdev);
286 /*trigger socketcan */
287 softing_rx(bus->netdev, &msg, ktime);
292 msg.can_id |= CAN_RTR_FLAG;
293 /* acknowledge, was tx msg
294 * no real tx flag to set
298 msg.can_dlc = get_can_dlc(*ptr++);
300 msg.can_id |= CAN_EFF_FLAG;
301 msg.can_id |= (ptr[0] << 0) | (ptr[1] << 8)
302 | (ptr[2] << 16) | (ptr[3] << 24);
305 msg.can_id |= (ptr[0] << 0) | (ptr[1] << 8);
308 tmp = (ptr[0] << 0) | (ptr[1] << 8)
309 | (ptr[2] << 16) | (ptr[3] << 24);
311 ktime = softing_raw2ktime(card, tmp);
312 memcpy_fromio(&msg.data[0], ptr, 8);
314 /*trigger dual port RAM */
316 card->dpram.rx->rd = fifo_rd;
320 skb = bus->can.echo_skb[bus->tx.echo_get];
323 can_get_echo_skb(bus->netdev, bus->tx.echo_get);
325 if (bus->tx.echo_get >= TX_ECHO_SKB_MAX)
326 bus->tx.echo_get = 0;
329 if (card->tx.pending)
332 stats->tx_bytes += msg.can_dlc;
335 stats->rx_bytes += msg.can_dlc;
336 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,32)
337 bus->netdev->last_rx = jiffies;
339 softing_rx(bus->netdev, &msg, ktime);
346 static void softing_dev_svc(unsigned long param)
348 struct softing *card = (struct softing *)param;
349 struct softing_priv *bus;
353 spin_lock(&card->spin);
354 while (softing_dev_svc_once(card) > 0)
355 ++card->irq.svc_count;
356 spin_unlock(&card->spin);
357 /*resume tx queue's */
358 offset = card->tx.last_bus;
359 for (j = 0; j < card->nbus; ++j) {
360 if (card->tx.pending >= TXMAX)
362 bus = card->bus[(j + offset + 1) % card->nbus];
365 if (!canif_is_active(bus->netdev))
366 /* it makes no sense to wake dead busses */
368 if (bus->tx.pending >= TX_ECHO_SKB_MAX)
370 netif_wake_queue(bus->netdev);
375 irqreturn_t dev_interrupt_shared(int irq, void *dev_id)
377 struct softing *card = (struct softing *)dev_id;
379 ir = card->dpram.virt[0xe02];
380 card->dpram.virt[0xe02] = 0;
381 if (card->dpram.rx->rd == 0xffff) {
382 dev_alert(card->dev, "I think the card is gone\n");
386 tasklet_schedule(&card->irq.bh);
388 } else if (ir == 0x10) {
396 irqreturn_t dev_interrupt_nshared(int irq, void *dev_id)
398 struct softing *card = (struct softing *)dev_id;
399 unsigned char irq_host;
400 irq_host = card->dpram.irq->to_host;
401 /* make sure we have a copy, before clearing the variable in DPRAM */
403 card->dpram.irq->to_host = 0;
404 /* make sure we cleared it */
406 if (card->dpram.rx->rd == 0xffff) {
407 dev_alert(card->dev, "I think the card is gone\n");
410 tasklet_schedule(&card->irq.bh);
414 static int netdev_open(struct net_device *ndev)
416 struct softing_priv *priv = netdev_priv(ndev);
417 struct softing *card = priv->card;
420 /* check or determine and set bittime */
421 ret = open_candev(ndev);
424 ret = softing_cycle(card, priv, 1);
427 netif_start_queue(ndev);
433 static int netdev_stop(struct net_device *ndev)
435 struct softing_priv *priv = netdev_priv(ndev);
436 struct softing *card = priv->card;
439 netif_stop_queue(ndev);
441 /* softing cycle does close_candev() */
442 ret = softing_cycle(card, priv, 0);
446 static int candev_set_mode(struct net_device *ndev, enum can_mode mode)
448 struct softing_priv *priv = netdev_priv(ndev);
449 struct softing *card = priv->card;
454 /* softing cycle does close_candev() */
455 ret = softing_cycle(card, priv, 1);
464 /*assume the card->lock is held*/
466 int softing_card_irq(struct softing *card, int enable)
470 if (card->irq.requested && card->irq.nr) {
471 free_irq(card->irq.nr, card);
472 card->irq.requested = 0;
476 if (!card->irq.requested && (card->irq.nr)) {
477 irqreturn_t(*fn) (int, void *);
479 flags = IRQF_DISABLED | IRQF_SHARED;/*| IRQF_TRIGGER_LOW; */
480 fn = dev_interrupt_nshared;
481 if (card->desc->generation >= 2)
482 fn = dev_interrupt_shared;
483 ret = request_irq(card->irq.nr, fn, flags, card->id.name, card);
485 dev_alert(card->dev, "%s, request_irq(%u) failed\n",
486 card->id.name, card->irq.nr);
489 card->irq.requested = 1;
494 static void shutdown_card(struct softing *card)
497 dev_dbg(card->dev, "%s()\n", __func__);
498 if (mutex_lock_interruptible(&card->fw.lock))
499 /* return -ERESTARTSYS*/;
503 if (card->irq.requested && card->irq.nr) {
504 free_irq(card->irq.nr, card);
505 card->irq.requested = 0;
508 if (card->fn.enable_irq)
509 card->fn.enable_irq(card, 0);
511 card->fn.reset(card, 1);
513 mutex_unlock(&card->fw.lock);
514 tasklet_kill(&card->irq.bh);
517 static int boot_card(struct softing *card)
520 static const unsigned char stream[] =
521 { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, };
522 unsigned char back[sizeof(stream)];
523 dev_dbg(card->dev, "%s()\n", __func__);
525 if (mutex_lock_interruptible(&card->fw.lock))
528 mutex_unlock(&card->fw.lock);
533 if (card->fn.enable_irq)
534 card->fn.enable_irq(card, 1);
537 card->fn.reset(card, 1);
539 if (!card->dpram.virt)
541 for (lp = card->dpram.virt; &lp[sizeof(stream)] <= card->dpram.end;
542 lp += sizeof(stream)) {
544 memcpy_toio(lp, stream, sizeof(stream));
547 memcpy_fromio(back, lp, sizeof(stream));
549 if (!memcmp(back, stream, sizeof(stream)))
551 /* memory is not equal */
552 dev_alert(card->dev, "write to dpram failed at 0x%04lx\n",
553 (unsigned long)(lp - card->dpram.virt));
557 /*load boot firmware */
558 if (softing_load_fw(card->desc->boot.fw, card, card->dpram.virt,
560 card->desc->boot.offs -
561 card->desc->boot.addr))
563 /*load load firmware */
564 if (softing_load_fw(card->desc->load.fw, card, card->dpram.virt,
566 card->desc->load.offs -
567 card->desc->load.addr))
571 card->fn.reset(card, 0);
572 if (softing_bootloader_command(card, 0, "card boot"))
574 if (softing_load_app_fw(card->desc->app.fw, card))
577 card->dpram.info->reset_rcv_fifo = 0;
578 card->dpram.info->reset = 1;
580 if (softing_fct_cmd(card, 99, 0x55, "sync-a"))
582 if (softing_fct_cmd(card, 99, 0xaa, "sync-a"))
585 if (softing_fct_cmd(card, 0, 0, "reset_chip"))
588 if (softing_fct_cmd(card, 43, 0, "get_serial_number"))
591 (u16) card->dpram.fct->param[1] +
592 (((u16) card->dpram.fct->param[2]) << 16);
594 if (softing_fct_cmd(card, 12, 0, "get_version"))
596 card->id.fw = (u16) card->dpram.fct->param[1];
597 card->id.hw = (u16) card->dpram.fct->param[2];
598 card->id.lic = (u16) card->dpram.fct->param[3];
599 card->id.chip[0] = (u16) card->dpram.fct->param[4];
600 card->id.chip[1] = (u16) card->dpram.fct->param[5];
602 dev_info(card->dev, "card booted, type %s, "
603 "serial %u, fw %u, hw %u, lic %u, chip (%u,%u)\n",
604 card->id.name, card->id.serial, card->id.fw, card->id.hw,
605 card->id.lic, card->id.chip[0], card->id.chip[1]);
608 mutex_unlock(&card->fw.lock);
612 if (card->fn.enable_irq)
613 card->fn.enable_irq(card, 0);
615 card->fn.reset(card, 1);
616 mutex_unlock(&card->fw.lock);
620 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,28)
621 static const struct net_device_ops softing_netdev_ops = {
622 .ndo_open = netdev_open,
623 .ndo_stop = netdev_stop,
624 .ndo_start_xmit = netdev_start_xmit,
628 static const struct can_bittiming_const softing_btr_const = {
633 .sjw_max = 4, /* overruled */
635 .brp_max = 32, /* overruled */
640 static struct softing_priv *mk_netdev(struct softing *card, u16 chip_id)
642 struct net_device *ndev;
643 struct softing_priv *priv;
645 ndev = alloc_candev(sizeof(*priv), TX_ECHO_SKB_MAX);
647 dev_alert(card->dev, "alloc_candev failed\n");
650 priv = netdev_priv(ndev);
653 memcpy(&priv->btr_const, &softing_btr_const, sizeof(priv->btr_const));
654 priv->btr_const.brp_max = card->desc->max_brp;
655 priv->btr_const.sjw_max = card->desc->max_sjw;
656 priv->can.bittiming_const = &priv->btr_const;
657 priv->can.clock.freq = 8000000;
658 priv->chip = chip_id;
659 priv->output = softing_default_output(card, priv);
660 SET_NETDEV_DEV(ndev, card->dev);
662 ndev->flags |= IFF_ECHO;
663 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,28)
664 ndev->netdev_ops = &softing_netdev_ops;
666 ndev->open = netdev_open;
667 ndev->stop = netdev_stop;
668 ndev->hard_start_xmit = netdev_start_xmit;
670 priv->can.do_set_mode = candev_set_mode;
675 static int reg_netdev(struct softing_priv *priv)
678 ret = register_candev(priv->netdev);
680 dev_alert(priv->card->dev, "%s, register failed\n",
681 priv->card->id.name);
684 ret = softing_bus_sysfs_create(priv);
686 dev_alert(priv->card->dev, "%s, sysfs failed\n",
687 priv->card->id.name);
692 unregister_candev(priv->netdev);
697 void rm_softing(struct softing *card)
701 /*first, disable card*/
704 for (j = 0; j < card->nbus; ++j) {
707 softing_bus_sysfs_remove(card->bus[j]);
708 unregister_candev(card->bus[j]->netdev);
709 free_candev(card->bus[j]->netdev);
713 softing_card_sysfs_remove(card);
715 iounmap(card->dpram.virt);
717 EXPORT_SYMBOL(rm_softing);
719 int mk_softing(struct softing *card)
723 /* try_module_get(THIS_MODULE); */
724 mutex_init(&card->fw.lock);
725 spin_lock_init(&card->spin);
726 tasklet_init(&card->irq.bh, softing_dev_svc, (unsigned long)card);
729 dev_alert(card->dev, "no card description\n");
732 card->id.name = card->desc->name;
734 card->dpram.virt = ioremap(card->dpram.phys, card->dpram.size);
735 if (!card->dpram.virt) {
736 dev_alert(card->dev, "dpram ioremap failed\n");
740 card->dpram.size = card->desc->dpram_size;
741 card->dpram.end = &card->dpram.virt[card->dpram.size];
742 /*initialize_board */
743 card->dpram.rx = (struct softing_rx *)&card->dpram.virt[0x0000];
744 card->dpram.tx = (struct softing_tx *)&card->dpram.virt[0x0400];
745 card->dpram.fct = (struct softing_fct *)&card->dpram.virt[0x0300];
746 card->dpram.info = (struct softing_info *)&card->dpram.virt[0x0330];
747 card->dpram.command = (unsigned short *)&card->dpram.virt[0x07e0];
748 card->dpram.receipt = (unsigned short *)&card->dpram.virt[0x07f0];
749 card->dpram.irq = (struct softing_irq *)&card->dpram.virt[0x07fe];
753 card->fn.reset(card, 1);
754 if (boot_card(card)) {
755 dev_alert(card->dev, "failed to boot\n");
759 /*only now, the chip's are known */
760 card->id.freq = card->desc->freq * 1000000UL;
762 if (softing_card_sysfs_create(card)) {
763 dev_alert(card->dev, "sysfs failed\n");
767 if (card->nbus > (sizeof(card->bus) / sizeof(card->bus[0]))) {
768 card->nbus = sizeof(card->bus) / sizeof(card->bus[0]);
769 dev_alert(card->dev, "have %u busses\n", card->nbus);
772 for (j = 0; j < card->nbus; ++j) {
773 card->bus[j] = mk_netdev(card, card->id.chip[j]);
775 dev_alert(card->dev, "failed to make can[%i]", j);
778 card->bus[j]->index = j;
780 for (j = 0; j < card->nbus; ++j) {
781 if (reg_netdev(card->bus[j])) {
783 "failed to register can[%i]\n", j);
787 dev_info(card->dev, "card initialised\n");
791 for (j = 0; j < card->nbus; ++j) {
794 softing_bus_sysfs_remove(card->bus[j]);
795 unregister_candev(card->bus[j]->netdev);
798 for (j = 0; j < card->nbus; ++j) {
801 free_candev(card->bus[j]->netdev);
804 softing_card_sysfs_remove(card);
808 iounmap(card->dpram.virt);
809 card->dpram.virt = 0;
813 tasklet_kill(&card->irq.bh);
816 EXPORT_SYMBOL(mk_softing);
818 static int __init mod_start(void)
820 printk(KERN_INFO "[%s] start\n", THIS_MODULE->name);
824 static void __exit mod_stop(void)
826 printk(KERN_INFO "[%s] stop\n", THIS_MODULE->name);
829 module_init(mod_start);
830 module_exit(mod_stop);
832 MODULE_DESCRIPTION("socketcan softing driver");
833 MODULE_AUTHOR("Kurt Van Dijck <kurt.van.dijck@eia.be>");
834 MODULE_LICENSE("GPL");
836 int softing_debug = 1;
837 EXPORT_SYMBOL(softing_debug);
838 module_param(softing_debug, int , S_IRUGO | S_IWUSR | S_IWGRP);
839 MODULE_PARM_DESC(softing_debug, "trace softing functions");