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);
435 static int netdev_stop(struct net_device *ndev)
437 struct softing_priv *priv = netdev_priv(ndev);
438 struct softing *card = priv->card;
441 netif_stop_queue(ndev);
443 /* softing cycle does close_candev() */
444 ret = softing_cycle(card, priv, 0);
448 static int candev_set_mode(struct net_device *ndev, enum can_mode mode)
450 struct softing_priv *priv = netdev_priv(ndev);
451 struct softing *card = priv->card;
456 /* softing cycle does close_candev() */
457 ret = softing_cycle(card, priv, 1);
466 /*assume the card->lock is held*/
468 int softing_card_irq(struct softing *card, int enable)
472 if (card->irq.requested && card->irq.nr) {
473 free_irq(card->irq.nr, card);
474 card->irq.requested = 0;
478 if (!card->irq.requested && (card->irq.nr)) {
479 irqreturn_t(*fn) (int, void *);
480 fn = dev_interrupt_nshared;
481 if (card->desc->generation >= 2)
482 fn = dev_interrupt_shared;
483 ret = request_irq(card->irq.nr, fn, IRQF_SHARED,
484 card->id.name, card);
486 dev_alert(card->dev, "%s, request_irq(%u) failed\n",
487 card->id.name, card->irq.nr);
490 card->irq.requested = 1;
495 static void shutdown_card(struct softing *card)
498 dev_dbg(card->dev, "%s()\n", __func__);
499 if (mutex_lock_interruptible(&card->fw.lock))
500 /* return -ERESTARTSYS*/;
504 if (card->irq.requested && card->irq.nr) {
505 free_irq(card->irq.nr, card);
506 card->irq.requested = 0;
509 if (card->fn.enable_irq)
510 card->fn.enable_irq(card, 0);
512 card->fn.reset(card, 1);
514 mutex_unlock(&card->fw.lock);
515 tasklet_kill(&card->irq.bh);
518 static int boot_card(struct softing *card)
521 static const unsigned char stream[] =
522 { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, };
523 unsigned char back[sizeof(stream)];
524 dev_dbg(card->dev, "%s()\n", __func__);
526 if (mutex_lock_interruptible(&card->fw.lock))
529 mutex_unlock(&card->fw.lock);
534 if (card->fn.enable_irq)
535 card->fn.enable_irq(card, 1);
538 card->fn.reset(card, 1);
540 if (!card->dpram.virt)
542 for (lp = card->dpram.virt; &lp[sizeof(stream)] <= card->dpram.end;
543 lp += sizeof(stream)) {
545 memcpy_toio(lp, stream, sizeof(stream));
548 memcpy_fromio(back, lp, sizeof(stream));
550 if (!memcmp(back, stream, sizeof(stream)))
552 /* memory is not equal */
553 dev_alert(card->dev, "write to dpram failed at 0x%04lx\n",
554 (unsigned long)(lp - card->dpram.virt));
558 /*load boot firmware */
559 if (softing_load_fw(card->desc->boot.fw, card, card->dpram.virt,
561 card->desc->boot.offs -
562 card->desc->boot.addr))
564 /*load load firmware */
565 if (softing_load_fw(card->desc->load.fw, card, card->dpram.virt,
567 card->desc->load.offs -
568 card->desc->load.addr))
572 card->fn.reset(card, 0);
573 if (softing_bootloader_command(card, 0, "card boot"))
575 if (softing_load_app_fw(card->desc->app.fw, card))
578 card->dpram.info->reset_rcv_fifo = 0;
579 card->dpram.info->reset = 1;
581 if (softing_fct_cmd(card, 99, 0x55, "sync-a"))
583 if (softing_fct_cmd(card, 99, 0xaa, "sync-a"))
586 if (softing_fct_cmd(card, 0, 0, "reset_chip"))
589 if (softing_fct_cmd(card, 43, 0, "get_serial_number"))
592 (u16) card->dpram.fct->param[1] +
593 (((u16) card->dpram.fct->param[2]) << 16);
595 if (softing_fct_cmd(card, 12, 0, "get_version"))
597 card->id.fw = (u16) card->dpram.fct->param[1];
598 card->id.hw = (u16) card->dpram.fct->param[2];
599 card->id.lic = (u16) card->dpram.fct->param[3];
600 card->id.chip[0] = (u16) card->dpram.fct->param[4];
601 card->id.chip[1] = (u16) card->dpram.fct->param[5];
603 dev_info(card->dev, "card booted, type %s, "
604 "serial %u, fw %u, hw %u, lic %u, chip (%u,%u)\n",
605 card->id.name, card->id.serial, card->id.fw, card->id.hw,
606 card->id.lic, card->id.chip[0], card->id.chip[1]);
609 mutex_unlock(&card->fw.lock);
613 if (card->fn.enable_irq)
614 card->fn.enable_irq(card, 0);
616 card->fn.reset(card, 1);
617 mutex_unlock(&card->fw.lock);
621 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,28)
622 static const struct net_device_ops softing_netdev_ops = {
623 .ndo_open = netdev_open,
624 .ndo_stop = netdev_stop,
625 .ndo_start_xmit = netdev_start_xmit,
629 static const struct can_bittiming_const softing_btr_const = {
634 .sjw_max = 4, /* overruled */
636 .brp_max = 32, /* overruled */
641 static struct softing_priv *mk_netdev(struct softing *card, u16 chip_id)
643 struct net_device *ndev;
644 struct softing_priv *priv;
646 ndev = alloc_candev(sizeof(*priv), TX_ECHO_SKB_MAX);
648 dev_alert(card->dev, "alloc_candev failed\n");
651 priv = netdev_priv(ndev);
654 memcpy(&priv->btr_const, &softing_btr_const, sizeof(priv->btr_const));
655 priv->btr_const.brp_max = card->desc->max_brp;
656 priv->btr_const.sjw_max = card->desc->max_sjw;
657 priv->can.bittiming_const = &priv->btr_const;
658 priv->can.clock.freq = 8000000;
659 priv->chip = chip_id;
660 priv->output = softing_default_output(card, priv);
661 SET_NETDEV_DEV(ndev, card->dev);
663 ndev->flags |= IFF_ECHO;
664 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,28)
665 ndev->netdev_ops = &softing_netdev_ops;
667 ndev->open = netdev_open;
668 ndev->stop = netdev_stop;
669 ndev->hard_start_xmit = netdev_start_xmit;
671 priv->can.do_set_mode = candev_set_mode;
672 priv->can.ctrlmode_supported = CAN_CTRLMODE_3_SAMPLES;
677 static int reg_netdev(struct softing_priv *priv)
680 ret = register_candev(priv->netdev);
682 dev_alert(priv->card->dev, "%s, register failed\n",
683 priv->card->id.name);
686 ret = softing_bus_sysfs_create(priv);
688 dev_alert(priv->card->dev, "%s, sysfs failed\n",
689 priv->card->id.name);
694 unregister_candev(priv->netdev);
699 void rm_softing(struct softing *card)
703 /*first, disable card*/
706 for (j = 0; j < card->nbus; ++j) {
709 softing_bus_sysfs_remove(card->bus[j]);
710 unregister_candev(card->bus[j]->netdev);
711 free_candev(card->bus[j]->netdev);
715 softing_card_sysfs_remove(card);
717 iounmap(card->dpram.virt);
719 EXPORT_SYMBOL(rm_softing);
721 int mk_softing(struct softing *card)
725 /* try_module_get(THIS_MODULE); */
726 mutex_init(&card->fw.lock);
727 spin_lock_init(&card->spin);
728 tasklet_init(&card->irq.bh, softing_dev_svc, (unsigned long)card);
731 dev_alert(card->dev, "no card description\n");
734 card->id.name = card->desc->name;
736 card->dpram.virt = ioremap(card->dpram.phys, card->dpram.size);
737 if (!card->dpram.virt) {
738 dev_alert(card->dev, "dpram ioremap failed\n");
742 card->dpram.size = card->desc->dpram_size;
743 card->dpram.end = &card->dpram.virt[card->dpram.size];
744 /*initialize_board */
745 card->dpram.rx = (struct softing_rx *)&card->dpram.virt[0x0000];
746 card->dpram.tx = (struct softing_tx *)&card->dpram.virt[0x0400];
747 card->dpram.fct = (struct softing_fct *)&card->dpram.virt[0x0300];
748 card->dpram.info = (struct softing_info *)&card->dpram.virt[0x0330];
749 card->dpram.command = (unsigned short *)&card->dpram.virt[0x07e0];
750 card->dpram.receipt = (unsigned short *)&card->dpram.virt[0x07f0];
751 card->dpram.irq = (struct softing_irq *)&card->dpram.virt[0x07fe];
755 card->fn.reset(card, 1);
756 if (boot_card(card)) {
757 dev_alert(card->dev, "failed to boot\n");
761 /*only now, the chip's are known */
762 card->id.freq = card->desc->freq * 1000000UL;
764 if (softing_card_sysfs_create(card)) {
765 dev_alert(card->dev, "sysfs failed\n");
769 if (card->nbus > (sizeof(card->bus) / sizeof(card->bus[0]))) {
770 card->nbus = sizeof(card->bus) / sizeof(card->bus[0]);
771 dev_alert(card->dev, "have %u busses\n", card->nbus);
774 for (j = 0; j < card->nbus; ++j) {
775 card->bus[j] = mk_netdev(card, card->id.chip[j]);
777 dev_alert(card->dev, "failed to make can[%i]", j);
780 card->bus[j]->index = j;
782 for (j = 0; j < card->nbus; ++j) {
783 if (reg_netdev(card->bus[j])) {
785 "failed to register can[%i]\n", j);
789 dev_info(card->dev, "card initialised\n");
793 for (j = 0; j < card->nbus; ++j) {
796 softing_bus_sysfs_remove(card->bus[j]);
797 unregister_candev(card->bus[j]->netdev);
800 for (j = 0; j < card->nbus; ++j) {
803 free_candev(card->bus[j]->netdev);
806 softing_card_sysfs_remove(card);
810 iounmap(card->dpram.virt);
811 card->dpram.virt = 0;
815 tasklet_kill(&card->irq.bh);
818 EXPORT_SYMBOL(mk_softing);
820 static int __init mod_start(void)
822 printk(KERN_INFO "[%s] start\n", THIS_MODULE->name);
826 static void __exit mod_stop(void)
828 printk(KERN_INFO "[%s] stop\n", THIS_MODULE->name);
831 module_init(mod_start);
832 module_exit(mod_stop);
834 MODULE_DESCRIPTION("socketcan softing driver");
835 MODULE_AUTHOR("Kurt Van Dijck <kurt.van.dijck@eia.be>");
836 MODULE_LICENSE("GPL");
838 int softing_debug = 1;
839 EXPORT_SYMBOL(softing_debug);
840 module_param(softing_debug, int , S_IRUGO | S_IWUSR | S_IWGRP);
841 MODULE_PARM_DESC(softing_debug, "trace softing functions");