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;
67 if (can_dropped_invalid_skb(dev, skb))
72 spin_lock(&card->spin);
75 spin_lock_bh(&card->spin);
80 if (card->tx.pending >= TXMAX)
82 if (priv->tx.pending >= TX_ECHO_SKB_MAX)
84 fifo_wr = card->dpram.tx->wr;
85 if (fifo_wr == card->dpram.tx->rd)
88 memcpy(&msg, skb->data, sizeof(msg));
89 ptr = &card->dpram.tx->fifo[fifo_wr][0];
91 if (msg.can_id & CAN_RTR_FLAG)
93 if (msg.can_id & CAN_EFF_FLAG)
99 *ptr++ = (msg.can_id >> 0);
100 *ptr++ = (msg.can_id >> 8);
101 if (msg.can_id & CAN_EFF_FLAG) {
102 *ptr++ = (msg.can_id >> 16);
103 *ptr++ = (msg.can_id >> 24);
105 /*increment 1, not 2 as you might think */
108 if (!(msg.can_id & CAN_RTR_FLAG))
109 memcpy_toio(ptr, &msg.data[0], msg.can_dlc);
111 sizeof(card->dpram.tx->fifo) /
112 sizeof(card->dpram.tx->fifo[0]))
114 card->dpram.tx->wr = fifo_wr;
115 card->tx.last_bus = priv->index;
118 can_put_echo_skb(skb, dev, priv->tx.echo_put);
120 if (priv->tx.echo_put >= TX_ECHO_SKB_MAX)
121 priv->tx.echo_put = 0;
122 /* can_put_echo_skb() saves the skb, safe to return TX_OK */
126 spin_unlock_bh(&card->spin);
128 spin_unlock(&card->spin);
129 if (card->tx.pending >= TXMAX) {
130 struct softing_priv *bus;
132 for (j = 0; j < card->nbus; ++j) {
136 netif_stop_queue(bus->netdev);
139 if (ret != NETDEV_TX_OK)
140 netif_stop_queue(dev);
145 int softing_rx(struct net_device *netdev, const struct can_frame *msg,
149 struct can_frame *cf;
151 struct net_device_stats *stats;
153 skb = alloc_can_skb(netdev, &cf);
156 memcpy(cf, msg, sizeof(*msg));
159 if (ret == NET_RX_DROP) {
160 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
161 stats = can_get_stats(netdev);
163 stats = &netdev->stats;
170 static int softing_dev_svc_once(struct softing *card)
173 struct softing_priv *bus;
175 struct can_frame msg;
177 unsigned int fifo_rd;
178 unsigned int cnt = 0;
179 struct net_device_stats *stats;
184 memset(&msg, 0, sizeof(msg));
185 if (card->dpram.rx->lost_msg) {
187 card->dpram.rx->lost_msg = 0;
189 msg.can_id = CAN_ERR_FLAG | CAN_ERR_CRTL;
190 msg.can_dlc = CAN_ERR_DLC;
191 msg.data[1] = CAN_ERR_CRTL_RX_OVERFLOW;
193 * service to all busses, we don't know which it was applicable
194 * but only service busses that are online
196 for (j = 0; j < card->nbus; ++j) {
200 if (!canif_is_active(bus->netdev))
201 /* a dead bus has no overflows */
203 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
204 stats = can_get_stats(bus->netdev);
206 stats = &bus->netdev->stats;
208 ++stats->rx_over_errors;
209 softing_rx(bus->netdev, &msg, ktime_set(0, 0));
211 /* prepare for other use */
212 memset(&msg, 0, sizeof(msg));
216 fifo_rd = card->dpram.rx->rd;
217 if (++fifo_rd >= ARRAY_SIZE(card->dpram.rx->fifo))
220 if (card->dpram.rx->wr == fifo_rd)
223 ptr = &card->dpram.rx->fifo[fifo_rd][0];
227 /*not quite usefull, probably the card has got out */
228 dev_alert(card->dev, "got cmd 0x%02x,"
229 " I suspect the card is lost\n", cmd);
231 /*mod_trace("0x%02x", cmd);*/
236 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
237 stats = can_get_stats(bus->netdev);
239 stats = &bus->netdev->stats;
246 msg.can_id = CAN_ERR_FLAG;
247 msg.can_dlc = CAN_ERR_DLC;
250 can_state = CAN_STATE_BUS_OFF;
251 msg.can_id |= CAN_ERR_BUSOFF;
253 } else if (state & 0x60) {
254 can_state = CAN_STATE_ERROR_PASSIVE;
255 msg.can_id |= CAN_ERR_BUSERROR;
256 msg.data[1] = CAN_ERR_CRTL_TX_PASSIVE;
259 can_state = CAN_STATE_ERROR_ACTIVE;
261 msg.can_id |= CAN_ERR_BUSERROR;
265 card->dpram.info->bus_state = state;
267 card->dpram.info->bus_state2 = state;
269 tmp = (ptr[0] << 0) | (ptr[1] << 8)
270 | (ptr[2] << 16) | (ptr[3] << 24);
272 ktime = softing_raw2ktime(card, tmp);
273 /*trigger dual port RAM */
275 card->dpram.rx->rd = fifo_rd;
277 ++bus->can.can_stats.bus_error;
279 /*update internal status */
280 if (can_state != bus->can.state) {
281 bus->can.state = can_state;
282 if (can_state == CAN_STATE_ERROR_PASSIVE)
283 ++bus->can.can_stats.error_passive;
284 if (can_state == CAN_STATE_BUS_OFF) {
285 /* this calls can_close_cleanup() */
286 can_bus_off(bus->netdev);
287 netif_stop_queue(bus->netdev);
289 /*trigger socketcan */
290 softing_rx(bus->netdev, &msg, ktime);
295 msg.can_id |= CAN_RTR_FLAG;
296 /* acknowledge, was tx msg
297 * no real tx flag to set
301 msg.can_dlc = get_can_dlc(*ptr++);
303 msg.can_id |= CAN_EFF_FLAG;
304 msg.can_id |= (ptr[0] << 0) | (ptr[1] << 8)
305 | (ptr[2] << 16) | (ptr[3] << 24);
308 msg.can_id |= (ptr[0] << 0) | (ptr[1] << 8);
311 tmp = (ptr[0] << 0) | (ptr[1] << 8)
312 | (ptr[2] << 16) | (ptr[3] << 24);
314 ktime = softing_raw2ktime(card, tmp);
315 memcpy_fromio(&msg.data[0], ptr, 8);
317 /*trigger dual port RAM */
319 card->dpram.rx->rd = fifo_rd;
323 skb = bus->can.echo_skb[bus->tx.echo_get];
326 can_get_echo_skb(bus->netdev, bus->tx.echo_get);
328 if (bus->tx.echo_get >= TX_ECHO_SKB_MAX)
329 bus->tx.echo_get = 0;
332 if (card->tx.pending)
335 stats->tx_bytes += msg.can_dlc;
338 stats->rx_bytes += msg.can_dlc;
339 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,32)
340 bus->netdev->last_rx = jiffies;
342 softing_rx(bus->netdev, &msg, ktime);
349 static void softing_dev_svc(unsigned long param)
351 struct softing *card = (struct softing *)param;
352 struct softing_priv *bus;
356 spin_lock(&card->spin);
357 while (softing_dev_svc_once(card) > 0)
358 ++card->irq.svc_count;
359 spin_unlock(&card->spin);
360 /*resume tx queue's */
361 offset = card->tx.last_bus;
362 for (j = 0; j < card->nbus; ++j) {
363 if (card->tx.pending >= TXMAX)
365 bus = card->bus[(j + offset + 1) % card->nbus];
368 if (!canif_is_active(bus->netdev))
369 /* it makes no sense to wake dead busses */
371 if (bus->tx.pending >= TX_ECHO_SKB_MAX)
373 netif_wake_queue(bus->netdev);
378 irqreturn_t dev_interrupt_shared(int irq, void *dev_id)
380 struct softing *card = (struct softing *)dev_id;
382 ir = card->dpram.virt[0xe02];
383 card->dpram.virt[0xe02] = 0;
384 if (card->dpram.rx->rd == 0xffff) {
385 dev_alert(card->dev, "I think the card is gone\n");
389 tasklet_schedule(&card->irq.bh);
391 } else if (ir == 0x10) {
399 irqreturn_t dev_interrupt_nshared(int irq, void *dev_id)
401 struct softing *card = (struct softing *)dev_id;
402 unsigned char irq_host;
403 irq_host = card->dpram.irq->to_host;
404 /* make sure we have a copy, before clearing the variable in DPRAM */
406 card->dpram.irq->to_host = 0;
407 /* make sure we cleared it */
409 if (card->dpram.rx->rd == 0xffff) {
410 dev_alert(card->dev, "I think the card is gone\n");
413 tasklet_schedule(&card->irq.bh);
417 static int netdev_open(struct net_device *ndev)
419 struct softing_priv *priv = netdev_priv(ndev);
420 struct softing *card = priv->card;
423 /* check or determine and set bittime */
424 ret = open_candev(ndev);
427 ret = softing_cycle(card, priv, 1);
430 netif_start_queue(ndev);
436 static int netdev_stop(struct net_device *ndev)
438 struct softing_priv *priv = netdev_priv(ndev);
439 struct softing *card = priv->card;
442 netif_stop_queue(ndev);
444 /* softing cycle does close_candev() */
445 ret = softing_cycle(card, priv, 0);
449 static int candev_set_mode(struct net_device *ndev, enum can_mode mode)
451 struct softing_priv *priv = netdev_priv(ndev);
452 struct softing *card = priv->card;
457 /* softing cycle does close_candev() */
458 ret = softing_cycle(card, priv, 1);
467 /*assume the card->lock is held*/
469 int softing_card_irq(struct softing *card, int enable)
473 if (card->irq.requested && card->irq.nr) {
474 free_irq(card->irq.nr, card);
475 card->irq.requested = 0;
479 if (!card->irq.requested && (card->irq.nr)) {
480 irqreturn_t(*fn) (int, void *);
482 flags = IRQF_DISABLED | IRQF_SHARED;/*| IRQF_TRIGGER_LOW; */
483 fn = dev_interrupt_nshared;
484 if (card->desc->generation >= 2)
485 fn = dev_interrupt_shared;
486 ret = request_irq(card->irq.nr, fn, flags, card->id.name, card);
488 dev_alert(card->dev, "%s, request_irq(%u) failed\n",
489 card->id.name, card->irq.nr);
492 card->irq.requested = 1;
497 static void shutdown_card(struct softing *card)
500 dev_dbg(card->dev, "%s()\n", __func__);
501 if (mutex_lock_interruptible(&card->fw.lock))
502 /* return -ERESTARTSYS*/;
506 if (card->irq.requested && card->irq.nr) {
507 free_irq(card->irq.nr, card);
508 card->irq.requested = 0;
511 if (card->fn.enable_irq)
512 card->fn.enable_irq(card, 0);
514 card->fn.reset(card, 1);
516 mutex_unlock(&card->fw.lock);
517 tasklet_kill(&card->irq.bh);
520 static int boot_card(struct softing *card)
523 static const unsigned char stream[] =
524 { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, };
525 unsigned char back[sizeof(stream)];
526 dev_dbg(card->dev, "%s()\n", __func__);
528 if (mutex_lock_interruptible(&card->fw.lock))
531 mutex_unlock(&card->fw.lock);
536 if (card->fn.enable_irq)
537 card->fn.enable_irq(card, 1);
540 card->fn.reset(card, 1);
542 if (!card->dpram.virt)
544 for (lp = card->dpram.virt; &lp[sizeof(stream)] <= card->dpram.end;
545 lp += sizeof(stream)) {
547 memcpy_toio(lp, stream, sizeof(stream));
550 memcpy_fromio(back, lp, sizeof(stream));
552 if (!memcmp(back, stream, sizeof(stream)))
554 /* memory is not equal */
555 dev_alert(card->dev, "write to dpram failed at 0x%04lx\n",
556 (unsigned long)(lp - card->dpram.virt));
560 /*load boot firmware */
561 if (softing_load_fw(card->desc->boot.fw, card, card->dpram.virt,
563 card->desc->boot.offs -
564 card->desc->boot.addr))
566 /*load load firmware */
567 if (softing_load_fw(card->desc->load.fw, card, card->dpram.virt,
569 card->desc->load.offs -
570 card->desc->load.addr))
574 card->fn.reset(card, 0);
575 if (softing_bootloader_command(card, 0, "card boot"))
577 if (softing_load_app_fw(card->desc->app.fw, card))
580 card->dpram.info->reset_rcv_fifo = 0;
581 card->dpram.info->reset = 1;
583 if (softing_fct_cmd(card, 99, 0x55, "sync-a"))
585 if (softing_fct_cmd(card, 99, 0xaa, "sync-a"))
588 if (softing_fct_cmd(card, 0, 0, "reset_chip"))
591 if (softing_fct_cmd(card, 43, 0, "get_serial_number"))
594 (u16) card->dpram.fct->param[1] +
595 (((u16) card->dpram.fct->param[2]) << 16);
597 if (softing_fct_cmd(card, 12, 0, "get_version"))
599 card->id.fw = (u16) card->dpram.fct->param[1];
600 card->id.hw = (u16) card->dpram.fct->param[2];
601 card->id.lic = (u16) card->dpram.fct->param[3];
602 card->id.chip[0] = (u16) card->dpram.fct->param[4];
603 card->id.chip[1] = (u16) card->dpram.fct->param[5];
605 dev_info(card->dev, "card booted, type %s, "
606 "serial %u, fw %u, hw %u, lic %u, chip (%u,%u)\n",
607 card->id.name, card->id.serial, card->id.fw, card->id.hw,
608 card->id.lic, card->id.chip[0], card->id.chip[1]);
611 mutex_unlock(&card->fw.lock);
615 if (card->fn.enable_irq)
616 card->fn.enable_irq(card, 0);
618 card->fn.reset(card, 1);
619 mutex_unlock(&card->fw.lock);
623 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,28)
624 static const struct net_device_ops softing_netdev_ops = {
625 .ndo_open = netdev_open,
626 .ndo_stop = netdev_stop,
627 .ndo_start_xmit = netdev_start_xmit,
631 static const struct can_bittiming_const softing_btr_const = {
636 .sjw_max = 4, /* overruled */
638 .brp_max = 32, /* overruled */
643 static struct softing_priv *mk_netdev(struct softing *card, u16 chip_id)
645 struct net_device *ndev;
646 struct softing_priv *priv;
648 ndev = alloc_candev(sizeof(*priv), TX_ECHO_SKB_MAX);
650 dev_alert(card->dev, "alloc_candev failed\n");
653 priv = netdev_priv(ndev);
656 memcpy(&priv->btr_const, &softing_btr_const, sizeof(priv->btr_const));
657 priv->btr_const.brp_max = card->desc->max_brp;
658 priv->btr_const.sjw_max = card->desc->max_sjw;
659 priv->can.bittiming_const = &priv->btr_const;
660 priv->can.clock.freq = 8000000;
661 priv->chip = chip_id;
662 priv->output = softing_default_output(card, priv);
663 SET_NETDEV_DEV(ndev, card->dev);
665 ndev->flags |= IFF_ECHO;
666 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,28)
667 ndev->netdev_ops = &softing_netdev_ops;
669 ndev->open = netdev_open;
670 ndev->stop = netdev_stop;
671 ndev->hard_start_xmit = netdev_start_xmit;
673 priv->can.do_set_mode = candev_set_mode;
674 priv->can.ctrlmode_supported = CAN_CTRLMODE_3_SAMPLES;
679 static int reg_netdev(struct softing_priv *priv)
682 ret = register_candev(priv->netdev);
684 dev_alert(priv->card->dev, "%s, register failed\n",
685 priv->card->id.name);
688 ret = softing_bus_sysfs_create(priv);
690 dev_alert(priv->card->dev, "%s, sysfs failed\n",
691 priv->card->id.name);
696 unregister_candev(priv->netdev);
701 void rm_softing(struct softing *card)
705 /*first, disable card*/
708 for (j = 0; j < card->nbus; ++j) {
711 softing_bus_sysfs_remove(card->bus[j]);
712 unregister_candev(card->bus[j]->netdev);
713 free_candev(card->bus[j]->netdev);
717 softing_card_sysfs_remove(card);
719 iounmap(card->dpram.virt);
721 EXPORT_SYMBOL(rm_softing);
723 int mk_softing(struct softing *card)
727 /* try_module_get(THIS_MODULE); */
728 mutex_init(&card->fw.lock);
729 spin_lock_init(&card->spin);
730 tasklet_init(&card->irq.bh, softing_dev_svc, (unsigned long)card);
733 dev_alert(card->dev, "no card description\n");
736 card->id.name = card->desc->name;
738 card->dpram.virt = ioremap(card->dpram.phys, card->dpram.size);
739 if (!card->dpram.virt) {
740 dev_alert(card->dev, "dpram ioremap failed\n");
744 card->dpram.size = card->desc->dpram_size;
745 card->dpram.end = &card->dpram.virt[card->dpram.size];
746 /*initialize_board */
747 card->dpram.rx = (struct softing_rx *)&card->dpram.virt[0x0000];
748 card->dpram.tx = (struct softing_tx *)&card->dpram.virt[0x0400];
749 card->dpram.fct = (struct softing_fct *)&card->dpram.virt[0x0300];
750 card->dpram.info = (struct softing_info *)&card->dpram.virt[0x0330];
751 card->dpram.command = (unsigned short *)&card->dpram.virt[0x07e0];
752 card->dpram.receipt = (unsigned short *)&card->dpram.virt[0x07f0];
753 card->dpram.irq = (struct softing_irq *)&card->dpram.virt[0x07fe];
757 card->fn.reset(card, 1);
758 if (boot_card(card)) {
759 dev_alert(card->dev, "failed to boot\n");
763 /*only now, the chip's are known */
764 card->id.freq = card->desc->freq * 1000000UL;
766 if (softing_card_sysfs_create(card)) {
767 dev_alert(card->dev, "sysfs failed\n");
771 if (card->nbus > (sizeof(card->bus) / sizeof(card->bus[0]))) {
772 card->nbus = sizeof(card->bus) / sizeof(card->bus[0]);
773 dev_alert(card->dev, "have %u busses\n", card->nbus);
776 for (j = 0; j < card->nbus; ++j) {
777 card->bus[j] = mk_netdev(card, card->id.chip[j]);
779 dev_alert(card->dev, "failed to make can[%i]", j);
782 card->bus[j]->index = j;
784 for (j = 0; j < card->nbus; ++j) {
785 if (reg_netdev(card->bus[j])) {
787 "failed to register can[%i]\n", j);
791 dev_info(card->dev, "card initialised\n");
795 for (j = 0; j < card->nbus; ++j) {
798 softing_bus_sysfs_remove(card->bus[j]);
799 unregister_candev(card->bus[j]->netdev);
802 for (j = 0; j < card->nbus; ++j) {
805 free_candev(card->bus[j]->netdev);
808 softing_card_sysfs_remove(card);
812 iounmap(card->dpram.virt);
813 card->dpram.virt = 0;
817 tasklet_kill(&card->irq.bh);
820 EXPORT_SYMBOL(mk_softing);
822 static int __init mod_start(void)
824 printk(KERN_INFO "[%s] start\n", THIS_MODULE->name);
828 static void __exit mod_stop(void)
830 printk(KERN_INFO "[%s] stop\n", THIS_MODULE->name);
833 module_init(mod_start);
834 module_exit(mod_stop);
836 MODULE_DESCRIPTION("socketcan softing driver");
837 MODULE_AUTHOR("Kurt Van Dijck <kurt.van.dijck@eia.be>");
838 MODULE_LICENSE("GPL");
840 int softing_debug = 1;
841 EXPORT_SYMBOL(softing_debug);
842 module_param(softing_debug, int , S_IRUGO | S_IWUSR | S_IWGRP);
843 MODULE_PARM_DESC(softing_debug, "trace softing functions");