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/module.h>
23 #include <linux/init.h>
24 #include <linux/device.h>
25 #include <linux/firmware.h>
26 #include <linux/interrupt.h>
27 #include <linux/mutex.h>
32 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 25)
33 /* clear statistics */
34 #define strict_strtoul simple_strtoul
36 /* this is the worst thing on the softing API
37 * 2 busses are driven together, I don't know how
38 * to recover a single of them.
39 * Therefore, when one bus is modified, the other
42 void softing_flush_echo_skb(struct softing_priv *priv)
44 can_close_cleanup(priv->netdev);
46 priv->tx.echo_put = 0;
47 priv->tx.echo_get = 0;
50 /*softing_unlocked_tx_run:*/
51 /*trigger the tx queue-ing*/
52 /*no locks are grabbed, so be sure to have the spin spinlock*/
53 static int netdev_start_xmit(struct sk_buff *skb, struct net_device *dev)
55 struct softing_priv *priv = (struct softing_priv *)dev->priv;
56 struct softing *card = priv->card;
67 spin_lock(&card->spin);
70 spin_lock_bh(&card->spin);
76 if (netif_carrier_ok(priv->netdev) <= 0) {
80 if (card->tx.pending >= TXMAX) {
84 if (priv->tx.pending >= CAN_ECHO_SKB_MAX) {
88 fifo_wr = card->dpram.tx->wr;
89 if (fifo_wr == card->dpram.tx->rd) {
94 memcpy(&msg, skb->data, sizeof(msg));
95 ptr = &card->dpram.tx->fifo[fifo_wr][0];
97 if (msg.can_id & CAN_RTR_FLAG)
99 if (msg.can_id & CAN_EFF_FLAG)
104 *ptr++ = msg.can_dlc;
105 *ptr++ = (msg.can_id >> 0);
106 *ptr++ = (msg.can_id >> 8);
107 if (msg.can_id & CAN_EFF_FLAG) {
108 *ptr++ = (msg.can_id >> 16);
109 *ptr++ = (msg.can_id >> 24);
111 /*increment 1, not 2 as you might think */
114 if (!(msg.can_id & CAN_RTR_FLAG))
115 memcpy_toio(ptr, &msg.data[0], msg.can_dlc);
117 sizeof(card->dpram.tx->fifo) /
118 sizeof(card->dpram.tx->fifo[0]))
120 card->dpram.tx->wr = fifo_wr;
124 can_put_echo_skb(skb, dev, priv->tx.echo_put);
126 if (priv->tx.echo_put >= CAN_ECHO_SKB_MAX)
127 priv->tx.echo_put = 0;
128 /* clear pointer, so don't erase later */
132 spin_unlock_bh(&card->spin);
134 spin_unlock(&card->spin);
135 if (card->tx.pending >= TXMAX) {
136 struct softing_priv *bus;
138 for (j = 0; j < card->nbus; ++j) {
142 netif_stop_queue(bus->netdev);
146 /* free skb, if not handled by the driver */
152 static int softing_dev_svc_once(struct softing *card)
155 struct softing_priv *bus;
157 struct can_frame msg;
159 unsigned int fifo_rd;
160 unsigned int cnt = 0;
161 struct net_device_stats *stats;
163 memset(&msg, 0, sizeof(msg));
164 if (card->dpram.rx->lost_msg) {
166 card->dpram.rx->lost_msg = 0;
168 msg.can_id = CAN_ERR_FLAG | CAN_ERR_CRTL;
169 msg.can_dlc = CAN_ERR_DLC;
170 msg.data[1] = CAN_ERR_CRTL_RX_OVERFLOW;
171 /*service to both busses, we don't know which one generated */
172 for (j = 0; j < card->nbus; ++j) {
176 if (!netif_carrier_ok(bus->netdev))
178 ++bus->can.can_stats.data_overrun;
179 skb = dev_alloc_skb(sizeof(msg));
182 skb->dev = bus->netdev;
183 skb->protocol = htons(ETH_P_CAN);
184 skb->ip_summed = CHECKSUM_UNNECESSARY;
185 memcpy(skb_put(skb, sizeof(msg)), &msg, sizeof(msg));
187 dev_kfree_skb_irq(skb);
189 memset(&msg, 0, sizeof(msg));
193 fifo_rd = card->dpram.rx->rd;
195 sizeof(card->dpram.rx->fifo) / sizeof(card->dpram.rx->fifo[0]))
197 if (card->dpram.rx->wr != fifo_rd) {
203 ptr = &card->dpram.rx->fifo[fifo_rd][0];
207 /*not quite usefull, probably the card has got out */
208 mod_alert("got cmd 0x%02x, I suspect the card is lost"
211 /*mod_trace("0x%02x", cmd);*/
216 stats = bus->netdev->get_stats(bus->netdev);
222 msg.can_id = CAN_ERR_FLAG;
223 msg.can_dlc = CAN_ERR_DLC;
226 can_state = CAN_STATE_BUS_OFF;
227 msg.can_id |= CAN_ERR_BUSOFF;
229 } else if (state & 0x60) {
230 can_state = CAN_STATE_BUS_PASSIVE;
231 msg.can_id |= CAN_ERR_BUSERROR;
234 can_state = CAN_STATE_ACTIVE;
240 card->dpram.info->bus_state = state;
242 card->dpram.info->bus_state2 = state;
249 /*msg.time = */ softing_time2usec(card, tmp);
250 /*trigger dual port RAM */
252 card->dpram.rx->rd = fifo_rd;
253 /*update internal status */
254 if (can_state != bus->can.state) {
255 bus->can.state = can_state;
257 bus->can.can_stats.error_passive += 1;
259 bus->can.can_stats.bus_error += 1;
261 /*trigger socketcan */
263 /* this calls can_close_cleanup() */
264 softing_flush_echo_skb(bus);
265 can_bus_off(bus->netdev);
266 netif_stop_queue(bus->netdev);
268 if ((state == CAN_STATE_BUS_OFF)
269 || (state == CAN_STATE_BUS_PASSIVE)) {
270 skb = dev_alloc_skb(sizeof(msg));
273 skb->dev = bus->netdev;
274 skb->protocol = htons(ETH_P_CAN);
275 skb->ip_summed = CHECKSUM_UNNECESSARY;
276 memcpy(skb_put(skb, sizeof(msg)), &msg,
279 dev_kfree_skb_irq(skb);
283 msg.can_id |= CAN_RTR_FLAG;
284 /* acknowledge, was tx msg
285 * no real tx flag to set
289 msg.can_dlc = *ptr++;
293 msg.can_id |= CAN_EFF_FLAG;
301 msg.can_id |= (ptr[0] << 0) | (ptr[1] << 8);
309 /*msg.time = */ softing_time2usec(card, tmp);
310 memcpy_fromio(&msg.data[0], ptr, 8);
312 /*trigger dual port RAM */
314 card->dpram.rx->rd = fifo_rd;
317 can_get_echo_skb(bus->netdev, bus->tx.echo_get);
319 if (bus->tx.echo_get >= CAN_ECHO_SKB_MAX)
320 bus->tx.echo_get = 0;
323 if (card->tx.pending)
325 stats->tx_packets += 1;
326 stats->tx_bytes += msg.can_dlc;
328 stats->rx_packets += 1;
329 stats->rx_bytes += msg.can_dlc;
330 bus->netdev->last_rx = jiffies;
331 skb = dev_alloc_skb(sizeof(msg));
333 skb->dev = bus->netdev;
334 skb->protocol = htons(ETH_P_CAN);
335 skb->ip_summed = CHECKSUM_UNNECESSARY;
336 memcpy(skb_put(skb, sizeof(msg)), &msg,
339 dev_kfree_skb_irq(skb);
348 static void softing_dev_svc(unsigned long param)
350 struct softing *card = (struct softing *)param;
351 struct softing_priv *bus;
355 spin_lock(&card->spin);
356 while (softing_dev_svc_once(card) > 0)
357 ++card->irq.svc_count;
358 /*resume tx queue's */
359 offset = card->tx.last_bus;
360 for (j = 0; j < card->nbus; ++j) {
361 if (card->tx.pending >= TXMAX)
363 bus = card->bus[(j + offset) % card->nbus];
364 if (netif_carrier_ok(bus->netdev))
365 netif_wake_queue(bus->netdev);
367 spin_unlock(&card->spin);
370 static void card_seems_down(struct softing *card)
372 /* free interrupt, but probably
373 * in wrong (interrupt) context
374 if (card->irq.requested) {
375 free_irq(card->irq.nr, card);
376 card->irq.requested = 0;
380 mod_alert("I think the card is vanished");
384 irqreturn_t dev_interrupt_shared(int irq, void *dev_id)
386 struct softing *card = (struct softing *)dev_id;
388 ir = card->dpram.virt[0xe02];
389 card->dpram.virt[0xe02] = 0;
390 if (card->dpram.rx->rd == 0xffff) {
391 card_seems_down(card);
395 tasklet_schedule(&card->irq.bh);
397 } else if (ir == 0x10) {
405 irqreturn_t dev_interrupt_nshared(int irq, void *dev_id)
407 struct softing *card = (struct softing *)dev_id;
408 unsigned char irq_host;
409 irq_host = card->dpram.irq->to_host;
410 /* make sure we have a copy, before clearing the variable in DPRAM */
412 card->dpram.irq->to_host = 0;
413 /* make sure we cleared it */
415 mod_trace("0x%02x", irq_host);
416 if (card->dpram.rx->rd == 0xffff) {
417 card_seems_down(card);
420 tasklet_schedule(&card->irq.bh);
424 static int netdev_open(struct net_device *ndev)
426 struct softing_priv *priv = netdev_priv(ndev);
427 struct softing *card = priv->card;
429 mod_trace("%s", ndev->name);
430 if (mutex_lock_interruptible(&card->fw.lock))
435 , (card->bus[0] == priv) ? 1 : -1
436 , (card->bus[1] == priv) ? 1 : -1);
437 mutex_unlock(&card->fw.lock);
440 netif_start_queue(ndev);
444 static int netdev_stop(struct net_device *ndev)
446 struct softing_priv *priv = netdev_priv(ndev);
447 struct softing *card = priv->card;
450 mod_trace("%s", ndev->name);
451 netif_stop_queue(ndev);
452 netif_carrier_off(ndev);
453 softing_flush_echo_skb(priv);
454 can_close_cleanup(ndev);
455 if (mutex_lock_interruptible(&card->fw.lock))
460 , (card->bus[0] == priv) ? 0 : -1
461 , (card->bus[1] == priv) ? 0 : -1);
462 mutex_unlock(&card->fw.lock);
468 #define out_of_range(x, min, max) (((x) < min) || ((x) > max))
470 static int candev_set_bittiming(struct net_device *ndev)
472 struct softing_priv *priv = netdev_priv(ndev);
473 struct can_bittiming *bt = &priv->can.bittiming;
475 /*SJW is test via max_sjw */
476 if (out_of_range(bt->sjw, 1, 4))
478 if (out_of_range(bt->phase_seg1 + bt->prop_seg, 1, 16))
480 if (out_of_range(bt->phase_seg2, 1, 8))
482 /*BRP is test via max_brp */
483 if (ndev->flags & IFF_UP)
489 static int candev_get_state(struct net_device *ndev, enum can_state *state)
491 struct softing_priv *priv = netdev_priv(ndev);
492 mod_trace("%s", ndev->name);
493 if (priv->netdev->flags & IFF_UP)
494 *state = CAN_STATE_STOPPED;
495 else if (priv->can.state == CAN_STATE_STOPPED)
496 *state = CAN_STATE_STOPPED;
498 *state = CAN_STATE_ACTIVE;
502 static int candev_set_mode(struct net_device *ndev, enum can_mode mode)
504 struct softing_priv *priv = netdev_priv(ndev);
505 struct softing *card = priv->card;
506 mod_trace("%s %u", ndev->name, mode);
509 /*recovery from busoff? */
510 if (mutex_lock_interruptible(&card->fw.lock))
512 softing_reinit(card, -1, -1);
513 mutex_unlock(&card->fw.lock);
522 /*assume the card->lock is held*/
524 int softing_card_irq(struct softing *card, int enable)
528 if (card->irq.requested && card->irq.nr) {
529 free_irq(card->irq.nr, card);
530 card->irq.requested = 0;
534 if (!card->irq.requested && (card->irq.nr)) {
535 irqreturn_t(*fn) (int, void *);
537 flags = IRQF_DISABLED; /*| IRQF_TRIGGER_LOW; */
538 fn = dev_interrupt_nshared;
539 if (card->irq.shared) {
540 flags |= IRQF_SHARED;
541 fn = dev_interrupt_shared;
543 ret = request_irq(card->irq.nr, fn, flags, card->id.name, card);
545 mod_alert("%s, request_irq(%u) failed, %s shared"
546 , card->id.name, card->irq.nr
547 , (card->irq.shared ? "" : "not")
551 card->irq.requested = 1;
556 static void shutdown_card(struct softing *card)
559 mod_trace("%s", card->id.name);
560 if (mutex_lock_interruptible(&card->fw.lock))
561 /* return -ERESTARTSYS*/;
565 if (card->irq.requested && card->irq.nr) {
566 free_irq(card->irq.nr, card);
567 card->irq.requested = 0;
570 if (card->fn.enable_irq)
571 card->fn.enable_irq(card, 0);
573 card->fn.reset(card, 1);
575 mutex_unlock(&card->fw.lock);
576 tasklet_kill(&card->irq.bh);
579 static int boot_card(struct softing *card)
581 mod_trace("%s", card->id.name);
582 if (mutex_lock_interruptible(&card->fw.lock))
585 mutex_unlock(&card->fw.lock);
590 if (card->fn.enable_irq)
591 card->fn.enable_irq(card, 1);
594 card->fn.reset(card, 1);
596 if (card->dpram.virt) {
598 static const unsigned char stream[]
599 = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, };
600 unsigned char back[sizeof(stream)];
601 for (lp = card->dpram.virt;
602 &lp[sizeof(stream)] <= card->dpram.end;
603 lp += sizeof(stream)) {
604 memcpy_toio(lp, stream, sizeof(stream));
607 memcpy_fromio(back, lp, sizeof(stream));
609 if (memcmp(back, stream, sizeof(stream))) {
610 char line[3 * sizeof(stream)
611 / sizeof(stream[0]) + 1];
613 unsigned char *addr = lp;
614 for (lp = back; lp < &back[sizeof(stream)
615 / sizeof(stream[0])]; ++lp)
616 pline += sprintf(pline, " %02x", *lp);
618 mod_alert("write to dpram failed at 0x%p, %s"
623 /*fill dpram with 0x55 */
624 /*for (lp = card->dpram.virt; lp <= card->dpram.end; ++lp) {
631 /*load boot firmware */
632 if (softing_load_fw(card->desc->boot.fw, card, card->dpram.virt,
634 card->desc->boot.offs -
635 card->desc->boot.addr))
637 /*load load firmware */
638 if (softing_load_fw(card->desc->load.fw, card, card->dpram.virt,
640 card->desc->load.offs -
641 card->desc->load.addr))
645 card->fn.reset(card, 0);
646 if (softing_bootloader_command(card, 0, "card boot"))
648 if (softing_load_app_fw(card->desc->app.fw, card))
651 card->dpram.info->reset_rcv_fifo = 0;
652 card->dpram.info->reset = 1;
654 if (softing_fct_cmd(card, 99, 0x55, "sync-a"))
656 if (softing_fct_cmd(card, 99, 0xaa, "sync-a"))
659 if (softing_fct_cmd(card, 0, 0, "reset_chip"))
662 if (softing_fct_cmd(card, 43, 0, "get_serial_number"))
665 (u16) card->dpram.fct->param[1] +
666 (((u16) card->dpram.fct->param[2]) << 16);
668 if (softing_fct_cmd(card, 12, 0, "get_version"))
670 card->id.fw = (u16) card->dpram.fct->param[1];
671 card->id.hw = (u16) card->dpram.fct->param[2];
672 card->id.lic = (u16) card->dpram.fct->param[3];
673 card->id.chip[0] = (u16) card->dpram.fct->param[4];
674 card->id.chip[1] = (u16) card->dpram.fct->param[5];
676 mod_info("%s, card booted, "
677 "serial %u, fw %u, hw %u, lic %u, chip (%u,%u)",
678 card->id.name, card->id.serial, card->id.fw, card->id.hw,
679 card->id.lic, card->id.chip[0], card->id.chip[1]);
682 mutex_unlock(&card->fw.lock);
686 if (card->fn.enable_irq)
687 card->fn.enable_irq(card, 0);
689 card->fn.reset(card, 1);
690 mutex_unlock(&card->fw.lock);
696 /* Because the struct softing may be used by pcmcia devices
697 * as well as pci devices, * we have no clue how to get
698 * from a struct device * towards the struct softing *.
699 * It may go over a pci_device->priv or over a pcmcia_device->priv.
700 * Therefore, provide the struct softing pointer within the attribute.
701 * Then we don't need driver/bus specific things in these attributes
703 struct softing_attribute {
704 struct device_attribute dev;
705 ssize_t (*show) (struct softing *card, char *buf);
706 ssize_t (*store)(struct softing *card, const char *buf, size_t count);
707 struct softing *card;
710 static ssize_t rd_card_attr(struct device *dev, struct device_attribute *attr
712 struct softing_attribute *cattr
713 = container_of(attr, struct softing_attribute, dev);
714 return cattr->show ? cattr->show(cattr->card, buf) : 0;
716 static ssize_t wr_card_attr(struct device *dev, struct device_attribute *attr
717 , const char *buf, size_t count) {
718 struct softing_attribute *cattr
719 = container_of(attr, struct softing_attribute, dev);
720 return cattr->store ? cattr->store(cattr->card, buf, count) : 0;
723 #define declare_attr(_name, _mode, _show, _store) { \
726 .name = __stringify(_name), \
729 .show = rd_card_attr, \
730 .store = wr_card_attr, \
736 #define CARD_SHOW(name, member) \
737 static ssize_t show_##name(struct softing *card, char *buf) { \
738 return sprintf(buf, "%u\n", card->member); \
740 CARD_SHOW(serial , id.serial);
741 CARD_SHOW(firmware , id.fw);
742 CARD_SHOW(hardware , id.hw);
743 CARD_SHOW(license , id.lic);
744 CARD_SHOW(freq , id.freq);
745 CARD_SHOW(txpending , tx.pending);
747 static const struct softing_attribute card_attr_proto [] = {
748 declare_attr(serial , 0644, show_serial , 0),
749 declare_attr(firmware , 0644, show_firmware , 0),
750 declare_attr(hardware , 0644, show_hardware , 0),
751 declare_attr(license , 0644, show_license , 0),
752 declare_attr(freq , 0644, show_freq , 0),
753 declare_attr(txpending , 0644, show_txpending , 0),
756 static int mk_card_sysfs(struct softing *card)
761 size = sizeof(card_attr_proto)/sizeof(card_attr_proto[0]);
762 card->attr = kmalloc((size+1)*sizeof(card->attr[0]), GFP_KERNEL);
764 goto attr_mem_failed;
765 memcpy(card->attr, card_attr_proto, size * sizeof(card->attr[0]));
766 memset(&card->attr[size], 0, sizeof(card->attr[0]));
768 card->grp = kmalloc((size+1)*sizeof(card->grp [0]), GFP_KERNEL);
772 for (j = 0; j < size; ++j) {
773 card->attr[j].card = card;
774 card->grp[j] = &card->attr[j].dev.attr;
775 if (!card->attr[j].show)
776 card->attr[j].dev.attr.mode &= ~(S_IRUGO);
777 if (!card->attr[j].store)
778 card->attr[j].dev.attr.mode &= ~(S_IWUGO);
781 card->sysfs.name = "softing";
782 card->sysfs.attrs = card->grp;
783 if (sysfs_create_group(&card->dev->kobj, &card->sysfs) < 0)
795 static void rm_card_sysfs(struct softing *card)
797 sysfs_remove_group(&card->dev->kobj, &card->sysfs);
802 static ssize_t show_chip(struct device *dev
803 , struct device_attribute *attr, char *buf)
805 struct net_device *ndev = to_net_dev(dev);
806 struct softing_priv *priv = netdev2softing(ndev);
807 return sprintf(buf, "%i\n", priv->chip);
810 static ssize_t show_output(struct device *dev
811 , struct device_attribute *attr, char *buf)
813 struct net_device *ndev = to_net_dev(dev);
814 struct softing_priv *priv = netdev2softing(ndev);
815 return sprintf(buf, "0x%02x\n", priv->output);
818 static ssize_t store_output(struct device *dev
819 , struct device_attribute *attr
820 , const char *buf, size_t count)
822 struct net_device *ndev = to_net_dev(dev);
823 struct softing_priv *priv = netdev2softing(ndev);
824 struct softing *card = priv->card;
826 int v = strict_strtol(buf, 0, 0);
828 if (mutex_lock_interruptible(&card->fw.lock))
830 if (ndev->flags & IFF_UP) {
832 /* we will need a restart */
833 for (j = 0; j < card->nbus; ++j) {
834 if (j == priv->index)
837 if (card->bus[j]->netdev->flags & IFF_UP) {
838 mutex_unlock(&card->fw.lock);
843 softing_reinit(card, -1, -1);
847 mutex_unlock(&card->fw.lock);
851 * the latest softing cards support sleep mode too
854 static const DEVICE_ATTR(chip, S_IRUGO, show_chip, 0);
855 static const DEVICE_ATTR(output, S_IRUGO | S_IWUSR, show_output, store_output);
857 static const struct attribute *const netdev_sysfs_entries [] = {
858 &dev_attr_chip .attr,
859 &dev_attr_output .attr,
862 static const struct attribute_group netdev_sysfs = {
864 .attrs = (struct attribute **)netdev_sysfs_entries,
867 static int mk_netdev_sysfs(struct softing_priv *priv)
869 if (!priv->netdev->dev.kobj.sd) {
870 mod_alert("sysfs_create_group would fail");
873 return sysfs_create_group(&priv->netdev->dev.kobj, &netdev_sysfs);
875 static void rm_netdev_sysfs(struct softing_priv *priv)
877 sysfs_remove_group(&priv->netdev->dev.kobj, &netdev_sysfs);
880 static struct softing_priv *mk_netdev(struct softing *card, u16 chip_id)
882 struct net_device *ndev;
883 struct softing_priv *priv;
885 ndev = alloc_candev(sizeof(*priv));
887 mod_alert("alloc_candev failed");
890 priv = netdev_priv(ndev);
894 priv->can.bittiming.bitrate = 250000UL; /* default bitrate */
895 priv->can.bittiming.clock = 8000000UL;
896 // priv->can.bittiming.clock = card->desc->freq * 1000000 / 2;
898 /* point to private card description dependent bittiming */
899 priv->can.bittiming_const = &priv->softing_bittiming;
901 /* fill card description dependent bittiming */
902 priv->softing_bittiming.tseg1_min = 1;
903 priv->softing_bittiming.tseg1_max = 16;
904 priv->softing_bittiming.tseg2_min = 1;
905 priv->softing_bittiming.tseg2_max = 8;
906 priv->softing_bittiming.sjw_max = card->desc->max_sjw;
907 priv->softing_bittiming.brp_min = 1;
908 priv->softing_bittiming.brp_max = card->desc->max_brp;
909 priv->softing_bittiming.brp_inc = 1;
912 priv->chip = chip_id;
913 priv->output = softing_default_output(card, priv);
914 SET_NETDEV_DEV(ndev, card->dev);
916 ndev->flags |= IFF_ECHO;
917 ndev->open = netdev_open;
918 ndev->stop = netdev_stop;
919 ndev->hard_start_xmit = netdev_start_xmit;
920 priv->can.do_set_bittiming = candev_set_bittiming;
921 priv->can.do_get_state = candev_get_state;
922 priv->can.do_set_mode = candev_set_mode;
924 if (can_set_bittiming(ndev))
925 mod_alert("bitrate failed");
930 static void rm_netdev(struct softing_priv *priv)
932 free_candev(priv->netdev);
935 static int reg_netdev(struct softing_priv *priv)
938 netif_carrier_off(priv->netdev);
939 ret = register_netdev(priv->netdev);
941 mod_alert("%s, register failed", priv->card->id.name);
944 ret = mk_netdev_sysfs(priv);
946 mod_alert("%s, sysfs failed", priv->card->id.name);
951 unregister_netdev(priv->netdev);
956 static void unreg_netdev(struct softing_priv *priv)
958 rm_netdev_sysfs(priv);
959 unregister_netdev(priv->netdev);
962 void rm_softing(struct softing *card)
966 /*first, disable card*/
969 for (j = 0; j < card->nbus; ++j) {
970 unreg_netdev(card->bus[j]);
971 rm_netdev(card->bus[j]);
976 iounmap(card->dpram.virt);
978 EXPORT_SYMBOL(rm_softing);
980 int mk_softing(struct softing *card)
984 /* try_module_get(THIS_MODULE); */
985 mutex_init(&card->fw.lock);
986 spin_lock_init(&card->spin);
987 tasklet_init(&card->irq.bh, softing_dev_svc, (unsigned long)card);
989 card->desc = softing_lookup_desc(card->id.manf, card->id.prod);
991 mod_alert("0x%04x:0x%04x not supported\n", card->id.manf,
995 card->id.name = card->desc->name;
996 mod_trace("can (%s)", card->id.name);
998 card->dpram.virt = ioremap(card->dpram.phys, card->dpram.size);
999 if (!card->dpram.virt) {
1000 mod_alert("dpram ioremap failed\n");
1001 goto ioremap_failed;
1004 card->dpram.size = card->desc->dpram_size;
1005 card->dpram.end = &card->dpram.virt[card->dpram.size];
1006 /*initialize_board */
1007 card->dpram.rx = (struct softing_rx *)&card->dpram.virt[0x0000];
1008 card->dpram.tx = (struct softing_tx *)&card->dpram.virt[0x0400];
1009 card->dpram.fct = (struct softing_fct *)&card->dpram.virt[0x0300];
1010 card->dpram.info = (struct softing_info *)&card->dpram.virt[0x0330];
1011 card->dpram.command = (unsigned short *)&card->dpram.virt[0x07e0];
1012 card->dpram.receipt = (unsigned short *)&card->dpram.virt[0x07f0];
1013 card->dpram.irq = (struct softing_irq *)&card->dpram.virt[0x07fe];
1017 card->fn.reset(card, 1);
1018 if (boot_card(card)) {
1019 mod_alert("%s, failed to boot", card->id.name);
1023 /*only now, the chip's are known */
1024 card->id.freq = card->desc->freq * 1000000UL;
1026 if (mk_card_sysfs(card)) {
1027 mod_alert("%s, sysfs failed", card->id.name);
1031 if (card->nbus > (sizeof(card->bus) / sizeof(card->bus[0]))) {
1032 card->nbus = sizeof(card->bus) / sizeof(card->bus[0]);
1033 mod_alert("%s, going for %u busses", card->id.name, card->nbus);
1036 for (j = 0; j < card->nbus; ++j) {
1037 card->bus[j] = mk_netdev(card, card->id.chip[j]);
1038 if (!card->bus[j]) {
1039 mod_alert("%s: failed to make can[%i]", card->id.name,
1043 card->bus[j]->index = j;
1045 for (j = 0; j < card->nbus; ++j) {
1046 if (reg_netdev(card->bus[j])) {
1047 mod_alert("%s: failed to register can[%i]",
1052 mod_trace("card initialised");
1056 for (j = 0; j < card->nbus; ++j)
1057 unreg_netdev(card->bus[j]);
1059 for (j = 0; j < card->nbus; ++j) {
1061 rm_netdev(card->bus[j]);
1063 rm_card_sysfs(card);
1065 shutdown_card(card);
1067 iounmap(card->dpram.virt);
1068 card->dpram.virt = 0;
1069 card->dpram.end = 0;
1072 tasklet_kill(&card->irq.bh);
1075 EXPORT_SYMBOL(mk_softing);
1077 static int __init mod_start(void)
1083 static void __exit mod_stop(void)
1088 module_init(mod_start);
1089 module_exit(mod_stop);
1091 MODULE_DESCRIPTION("socketcan softing driver");
1092 MODULE_AUTHOR("Kurt Van Dijck <kurt.van.dijck@eia.be>");
1093 MODULE_LICENSE("GPL");
1095 int softing_debug = 1;
1096 EXPORT_SYMBOL(softing_debug);
1097 module_param(softing_debug, int , S_IRUGO | S_IWUSR | S_IWGRP);
1098 MODULE_PARM_DESC(softing_debug, "trace softing functions");