2 * CAN driver for PEAK System USB adapters
3 * Derived from the PCAN project file driver/src/pcan_usb_core.c
5 * Copyright (C) 2003-2010 PEAK System-Technik GmbH
6 * Copyright (C) 2010-2012 Stephane Grosjean <s.grosjean@peak-system.com>
8 * Many thanks to Klaus Hitschler <klaus.hitschler@gmx.de>
10 * This program is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU General Public License as published
12 * by the Free Software Foundation; version 2 of the License.
14 * This program is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
19 #include <linux/init.h>
20 #include <linux/signal.h>
21 #include <linux/slab.h>
22 #include <linux/module.h>
23 #include <linux/netdevice.h>
24 #include <linux/usb.h>
26 #include <linux/can.h>
27 #include <linux/can/dev.h>
28 #include <linux/can/error.h>
30 #include "pcan_usb_core.h"
32 MODULE_AUTHOR("Stephane Grosjean <s.grosjean@peak-system.com>");
33 MODULE_DESCRIPTION("CAN driver for PEAK-System USB adapters");
34 MODULE_LICENSE("GPL v2");
36 /* Table of devices that work with this driver */
37 static struct usb_device_id peak_usb_table[] = {
38 {USB_DEVICE(PCAN_USB_VENDOR_ID, PCAN_USB_PRODUCT_ID)},
39 {USB_DEVICE(PCAN_USB_VENDOR_ID, PCAN_USBPRO_PRODUCT_ID)},
40 {USB_DEVICE(PCAN_USB_VENDOR_ID, PCAN_USBFD_PRODUCT_ID)},
41 {USB_DEVICE(PCAN_USB_VENDOR_ID, PCAN_USBPROFD_PRODUCT_ID)},
42 {USB_DEVICE(PCAN_USB_VENDOR_ID, PCAN_USBCHIP_PRODUCT_ID)},
43 {USB_DEVICE(PCAN_USB_VENDOR_ID, PCAN_USBX6_PRODUCT_ID)},
44 {} /* Terminating entry */
47 MODULE_DEVICE_TABLE(usb, peak_usb_table);
49 /* List of supported PCAN-USB adapters (NULL terminated list) */
50 static const struct peak_usb_adapter *const peak_usb_adapters_list[] = {
63 void pcan_dump_mem(char *prompt, void *p, int l)
65 pr_info("%s dumping %s (%d bytes):\n",
66 PCAN_USB_DRIVER_NAME, prompt ? prompt : "memory", l);
67 print_hex_dump(KERN_INFO, PCAN_USB_DRIVER_NAME " ", DUMP_PREFIX_NONE,
68 DUMP_WIDTH, 1, p, l, false);
72 * initialize a time_ref object with usb adapter own settings
74 void peak_usb_init_time_ref(struct peak_time_ref *time_ref,
75 const struct peak_usb_adapter *adapter)
78 memset(time_ref, 0, sizeof(struct peak_time_ref));
79 time_ref->adapter = adapter;
84 * sometimes, another now may be more recent than current one...
86 void peak_usb_update_ts_now(struct peak_time_ref *time_ref, u32 ts_now)
88 time_ref->ts_dev_2 = ts_now;
90 /* should wait at least two passes before computing */
91 if (ktime_to_ns(time_ref->tv_host) > 0) {
92 u32 delta_ts = time_ref->ts_dev_2 - time_ref->ts_dev_1;
94 if (time_ref->ts_dev_2 < time_ref->ts_dev_1)
95 delta_ts &= (1 << time_ref->adapter->ts_used_bits) - 1;
97 time_ref->ts_total += delta_ts;
102 * register device timestamp as now
104 void peak_usb_set_ts_now(struct peak_time_ref *time_ref, u32 ts_now)
106 if (ktime_to_ns(time_ref->tv_host_0) == 0) {
107 /* use monotonic clock to correctly compute further deltas */
108 time_ref->tv_host_0 = ktime_get();
109 time_ref->tv_host = ktime_set(0, 0);
112 * delta_us should not be >= 2^32 => delta should be < 4294s
113 * handle 32-bits wrapping here: if count of s. reaches 4200,
114 * reset counters and change time base
116 if (ktime_to_ns(time_ref->tv_host)) {
117 ktime_t delta = ktime_sub(time_ref->tv_host,
118 time_ref->tv_host_0);
119 if (ktime_to_ns(delta) > (4200ull * NSEC_PER_SEC)) {
120 time_ref->tv_host_0 = time_ref->tv_host;
121 time_ref->ts_total = 0;
125 time_ref->tv_host = ktime_get();
126 time_ref->tick_count++;
129 time_ref->ts_dev_1 = time_ref->ts_dev_2;
130 peak_usb_update_ts_now(time_ref, ts_now);
134 * compute time according to current ts and time_ref data
136 void peak_usb_get_ts_time(struct peak_time_ref *time_ref, u32 ts, ktime_t *time)
138 /* protect from getting time before setting now */
139 if (ktime_to_ns(time_ref->tv_host)) {
143 /* General case: dev_ts_1 < dev_ts_2 < ts, with:
145 * - dev_ts_1 = previous sync timestamp
146 * - dev_ts_2 = last sync timestamp
147 * - ts = event timestamp
148 * - ts_period = known sync period (theoretical)
149 * ~ dev_ts2 - dev_ts1
152 * - time counters wrap (see adapter->ts_used_bits)
153 * - sometimes, dev_ts_1 < ts < dev_ts2
155 * "normal" case (sync time counters increase):
156 * must take into account case when ts wraps (tsw)
160 * ---+--------+----+-------0-+--+-->
161 * ts_dev_1 | ts_dev_2 |
164 if (time_ref->ts_dev_1 < time_ref->ts_dev_2) {
165 /* case when event time (tsw) wraps */
166 if (ts < time_ref->ts_dev_1)
167 delta_ts = BIT_ULL(time_ref->adapter->ts_used_bits);
169 /* Otherwise, sync time counter (ts_dev_2) has wrapped:
170 * handle case when event time (tsn) hasn't.
174 * ---+--------+--0-+---------+--+-->
175 * ts_dev_1 | ts_dev_2 |
178 } else if (time_ref->ts_dev_1 < ts) {
179 delta_ts = -BIT_ULL(time_ref->adapter->ts_used_bits);
182 /* add delay between last sync and event timestamps */
183 delta_ts += (signed int)(ts - time_ref->ts_dev_2);
185 /* add time from beginning to last sync */
186 delta_ts += time_ref->ts_total;
188 /* convert ticks number into microseconds */
189 delta_us = delta_ts * time_ref->adapter->us_per_ts_scale;
190 delta_us >>= time_ref->adapter->us_per_ts_shift;
192 *time = ktime_add_us(time_ref->tv_host_0, delta_us);
199 * post received skb after having set any hw timestamp
201 int peak_usb_netif_rx(struct sk_buff *skb,
202 struct peak_time_ref *time_ref, u32 ts_low)
204 struct skb_shared_hwtstamps *hwts = skb_hwtstamps(skb);
206 peak_usb_get_ts_time(time_ref, ts_low, &hwts->hwtstamp);
208 return netif_rx(skb);
212 * callback for bulk Rx urb
214 static void peak_usb_read_bulk_callback(struct urb *urb)
216 struct peak_usb_device *dev = urb->context;
217 struct net_device *netdev;
220 netdev = dev->netdev;
222 if (!netif_device_present(netdev))
225 /* check reception status */
226 switch (urb->status) {
240 "Rx urb aborted (%d)\n", urb->status);
244 /* protect from any incoming empty msgs */
245 if ((urb->actual_length > 0) && (dev->adapter->dev_decode_buf)) {
246 /* handle these kinds of msgs only if _start callback called */
247 if (dev->state & PCAN_USB_STATE_STARTED) {
248 err = dev->adapter->dev_decode_buf(dev, urb);
250 pcan_dump_mem("received usb message",
251 urb->transfer_buffer,
252 urb->transfer_buffer_length);
257 usb_fill_bulk_urb(urb, dev->udev,
258 usb_rcvbulkpipe(dev->udev, dev->ep_msg_in),
259 urb->transfer_buffer, dev->adapter->rx_buffer_size,
260 peak_usb_read_bulk_callback, dev);
262 usb_anchor_urb(urb, &dev->rx_submitted);
263 err = usb_submit_urb(urb, GFP_ATOMIC);
267 usb_unanchor_urb(urb);
270 netif_device_detach(netdev);
272 netdev_err(netdev, "failed resubmitting read bulk urb: %d\n",
277 * callback for bulk Tx urb
279 static void peak_usb_write_bulk_callback(struct urb *urb)
281 struct peak_tx_urb_context *context = urb->context;
282 struct peak_usb_device *dev;
283 struct net_device *netdev;
288 netdev = dev->netdev;
290 atomic_dec(&dev->active_tx_urbs);
292 if (!netif_device_present(netdev))
295 /* check tx status */
296 switch (urb->status) {
298 /* transmission complete */
299 netdev->stats.tx_packets++;
300 netdev->stats.tx_bytes += context->data_len;
302 /* prevent tx timeout */
303 netif_trans_update(netdev);
308 netdev_err(netdev, "Tx urb aborted (%d)\n",
318 /* should always release echo skb and corresponding context */
319 can_get_echo_skb(netdev, context->echo_index);
320 context->echo_index = PCAN_USB_MAX_TX_URBS;
322 /* do wakeup tx queue in case of success only */
324 netif_wake_queue(netdev);
328 * called by netdev to send one skb on the CAN interface.
330 static netdev_tx_t peak_usb_ndo_start_xmit(struct sk_buff *skb,
331 struct net_device *netdev)
333 struct peak_usb_device *dev = netdev_priv(netdev);
334 struct peak_tx_urb_context *context = NULL;
335 struct net_device_stats *stats = &netdev->stats;
336 struct canfd_frame *cfd = (struct canfd_frame *)skb->data;
340 size_t size = dev->adapter->tx_buffer_size;
342 if (can_dropped_invalid_skb(netdev, skb))
345 for (i = 0; i < PCAN_USB_MAX_TX_URBS; i++)
346 if (dev->tx_contexts[i].echo_index == PCAN_USB_MAX_TX_URBS) {
347 context = dev->tx_contexts + i;
352 /* should not occur except during restart */
353 return NETDEV_TX_BUSY;
357 obuf = urb->transfer_buffer;
359 err = dev->adapter->dev_encode_msg(dev, skb, obuf, &size);
362 netdev_err(netdev, "packet dropped\n");
368 context->echo_index = i;
370 /* Note: this works with CANFD frames too */
371 context->data_len = cfd->len;
373 usb_anchor_urb(urb, &dev->tx_submitted);
375 can_put_echo_skb(skb, netdev, context->echo_index);
377 atomic_inc(&dev->active_tx_urbs);
379 err = usb_submit_urb(urb, GFP_ATOMIC);
381 can_free_echo_skb(netdev, context->echo_index);
383 usb_unanchor_urb(urb);
385 /* this context is not used in fact */
386 context->echo_index = PCAN_USB_MAX_TX_URBS;
388 atomic_dec(&dev->active_tx_urbs);
392 netif_device_detach(netdev);
395 netdev_warn(netdev, "tx urb submitting failed err=%d\n",
399 /* cable unplugged */
403 netif_trans_update(netdev);
405 /* slow down tx path */
406 if (atomic_read(&dev->active_tx_urbs) >= PCAN_USB_MAX_TX_URBS)
407 netif_stop_queue(netdev);
414 * start the CAN interface.
415 * Rx and Tx urbs are allocated here. Rx urbs are submitted here.
417 static int peak_usb_start(struct peak_usb_device *dev)
419 struct net_device *netdev = dev->netdev;
422 for (i = 0; i < PCAN_USB_MAX_RX_URBS; i++) {
426 /* create a URB, and a buffer for it, to receive usb messages */
427 urb = usb_alloc_urb(0, GFP_KERNEL);
433 buf = kmalloc(dev->adapter->rx_buffer_size, GFP_KERNEL);
440 usb_fill_bulk_urb(urb, dev->udev,
441 usb_rcvbulkpipe(dev->udev, dev->ep_msg_in),
442 buf, dev->adapter->rx_buffer_size,
443 peak_usb_read_bulk_callback, dev);
445 /* ask last usb_free_urb() to also kfree() transfer_buffer */
446 urb->transfer_flags |= URB_FREE_BUFFER;
447 usb_anchor_urb(urb, &dev->rx_submitted);
449 err = usb_submit_urb(urb, GFP_KERNEL);
452 netif_device_detach(dev->netdev);
454 usb_unanchor_urb(urb);
460 /* drop reference, USB core will take care of freeing it */
464 /* did we submit any URBs? Warn if we was not able to submit all urbs */
465 if (i < PCAN_USB_MAX_RX_URBS) {
467 netdev_err(netdev, "couldn't setup any rx URB\n");
471 netdev_warn(netdev, "rx performance may be slow\n");
474 /* pre-alloc tx buffers and corresponding urbs */
475 for (i = 0; i < PCAN_USB_MAX_TX_URBS; i++) {
476 struct peak_tx_urb_context *context;
480 /* create a URB and a buffer for it, to transmit usb messages */
481 urb = usb_alloc_urb(0, GFP_KERNEL);
487 buf = kmalloc(dev->adapter->tx_buffer_size, GFP_KERNEL);
494 context = dev->tx_contexts + i;
498 usb_fill_bulk_urb(urb, dev->udev,
499 usb_sndbulkpipe(dev->udev, dev->ep_msg_out),
500 buf, dev->adapter->tx_buffer_size,
501 peak_usb_write_bulk_callback, context);
503 /* ask last usb_free_urb() to also kfree() transfer_buffer */
504 urb->transfer_flags |= URB_FREE_BUFFER;
507 /* warn if we were not able to allocate enough tx contexts */
508 if (i < PCAN_USB_MAX_TX_URBS) {
510 netdev_err(netdev, "couldn't setup any tx URB\n");
514 netdev_warn(netdev, "tx performance may be slow\n");
517 if (dev->adapter->dev_start) {
518 err = dev->adapter->dev_start(dev);
523 dev->state |= PCAN_USB_STATE_STARTED;
525 /* can set bus on now */
526 if (dev->adapter->dev_set_bus) {
527 err = dev->adapter->dev_set_bus(dev, 1);
532 dev->can.state = CAN_STATE_ERROR_ACTIVE;
538 netif_device_detach(dev->netdev);
540 netdev_warn(netdev, "couldn't submit control: %d\n", err);
542 for (i = 0; i < PCAN_USB_MAX_TX_URBS; i++) {
543 usb_free_urb(dev->tx_contexts[i].urb);
544 dev->tx_contexts[i].urb = NULL;
547 usb_kill_anchored_urbs(&dev->rx_submitted);
553 * called by netdev to open the corresponding CAN interface.
555 static int peak_usb_ndo_open(struct net_device *netdev)
557 struct peak_usb_device *dev = netdev_priv(netdev);
561 err = open_candev(netdev);
565 /* finally start device */
566 err = peak_usb_start(dev);
568 netdev_err(netdev, "couldn't start device: %d\n", err);
569 close_candev(netdev);
573 netif_start_queue(netdev);
579 * unlink in-flight Rx and Tx urbs and free their memory.
581 static void peak_usb_unlink_all_urbs(struct peak_usb_device *dev)
585 /* free all Rx (submitted) urbs */
586 usb_kill_anchored_urbs(&dev->rx_submitted);
588 /* free unsubmitted Tx urbs first */
589 for (i = 0; i < PCAN_USB_MAX_TX_URBS; i++) {
590 struct urb *urb = dev->tx_contexts[i].urb;
593 dev->tx_contexts[i].echo_index != PCAN_USB_MAX_TX_URBS) {
595 * this urb is already released or always submitted,
596 * let usb core free by itself
602 dev->tx_contexts[i].urb = NULL;
605 /* then free all submitted Tx urbs */
606 usb_kill_anchored_urbs(&dev->tx_submitted);
607 atomic_set(&dev->active_tx_urbs, 0);
611 * called by netdev to close the corresponding CAN interface.
613 static int peak_usb_ndo_stop(struct net_device *netdev)
615 struct peak_usb_device *dev = netdev_priv(netdev);
617 dev->state &= ~PCAN_USB_STATE_STARTED;
618 netif_stop_queue(netdev);
620 close_candev(netdev);
622 dev->can.state = CAN_STATE_STOPPED;
624 /* unlink all pending urbs and free used memory */
625 peak_usb_unlink_all_urbs(dev);
627 if (dev->adapter->dev_stop)
628 dev->adapter->dev_stop(dev);
630 /* can set bus off now */
631 if (dev->adapter->dev_set_bus) {
632 int err = dev->adapter->dev_set_bus(dev, 0);
641 * handle end of waiting for the device to reset
643 void peak_usb_restart_complete(struct peak_usb_device *dev)
645 /* finally MUST update can state */
646 dev->can.state = CAN_STATE_ERROR_ACTIVE;
648 /* netdev queue can be awaken now */
649 netif_wake_queue(dev->netdev);
652 void peak_usb_async_complete(struct urb *urb)
654 kfree(urb->transfer_buffer);
659 * device (auto-)restart mechanism runs in a timer context =>
660 * MUST handle restart with asynchronous usb transfers
662 static int peak_usb_restart(struct peak_usb_device *dev)
669 * if device doesn't define any asynchronous restart handler, simply
670 * wake the netdev queue up
672 if (!dev->adapter->dev_restart_async) {
673 peak_usb_restart_complete(dev);
677 /* first allocate a urb to handle the asynchronous steps */
678 urb = usb_alloc_urb(0, GFP_ATOMIC);
682 /* also allocate enough space for the commands to send */
683 buf = kmalloc(PCAN_USB_MAX_CMD_LEN, GFP_ATOMIC);
689 /* call the device specific handler for the restart */
690 err = dev->adapter->dev_restart_async(dev, urb, buf);
701 * candev callback used to change CAN mode.
702 * Warning: this is called from a timer context!
704 static int peak_usb_set_mode(struct net_device *netdev, enum can_mode mode)
706 struct peak_usb_device *dev = netdev_priv(netdev);
711 err = peak_usb_restart(dev);
713 netdev_err(netdev, "couldn't start device (err %d)\n",
725 * candev callback used to set device nominal/arbitration bitrate.
727 static int peak_usb_set_bittiming(struct net_device *netdev)
729 struct peak_usb_device *dev = netdev_priv(netdev);
730 const struct peak_usb_adapter *pa = dev->adapter;
732 if (pa->dev_set_bittiming) {
733 struct can_bittiming *bt = &dev->can.bittiming;
734 int err = pa->dev_set_bittiming(dev, bt);
737 netdev_info(netdev, "couldn't set bitrate (err %d)\n",
746 * candev callback used to set device data bitrate.
748 static int peak_usb_set_data_bittiming(struct net_device *netdev)
750 struct peak_usb_device *dev = netdev_priv(netdev);
751 const struct peak_usb_adapter *pa = dev->adapter;
753 if (pa->dev_set_data_bittiming) {
754 struct can_bittiming *bt = &dev->can.data_bittiming;
755 int err = pa->dev_set_data_bittiming(dev, bt);
759 "couldn't set data bitrate (err %d)\n",
768 static const struct net_device_ops peak_usb_netdev_ops = {
769 .ndo_open = peak_usb_ndo_open,
770 .ndo_stop = peak_usb_ndo_stop,
771 .ndo_start_xmit = peak_usb_ndo_start_xmit,
772 .ndo_change_mtu = can_change_mtu,
776 * create one device which is attached to CAN controller #ctrl_idx of the
779 static int peak_usb_create_dev(const struct peak_usb_adapter *peak_usb_adapter,
780 struct usb_interface *intf, int ctrl_idx)
782 struct usb_device *usb_dev = interface_to_usbdev(intf);
783 int sizeof_candev = peak_usb_adapter->sizeof_dev_private;
784 struct peak_usb_device *dev;
785 struct net_device *netdev;
789 if (sizeof_candev < sizeof(struct peak_usb_device))
790 sizeof_candev = sizeof(struct peak_usb_device);
792 netdev = alloc_candev(sizeof_candev, PCAN_USB_MAX_TX_URBS);
794 dev_err(&intf->dev, "%s: couldn't alloc candev\n",
795 PCAN_USB_DRIVER_NAME);
799 dev = netdev_priv(netdev);
801 /* allocate a buffer large enough to send commands */
802 dev->cmd_buf = kzalloc(PCAN_USB_MAX_CMD_LEN, GFP_KERNEL);
805 goto lbl_free_candev;
809 dev->netdev = netdev;
810 dev->adapter = peak_usb_adapter;
811 dev->ctrl_idx = ctrl_idx;
812 dev->state = PCAN_USB_STATE_CONNECTED;
814 dev->ep_msg_in = peak_usb_adapter->ep_msg_in;
815 dev->ep_msg_out = peak_usb_adapter->ep_msg_out[ctrl_idx];
817 dev->can.clock = peak_usb_adapter->clock;
818 dev->can.bittiming_const = peak_usb_adapter->bittiming_const;
819 dev->can.do_set_bittiming = peak_usb_set_bittiming;
820 dev->can.data_bittiming_const = peak_usb_adapter->data_bittiming_const;
821 dev->can.do_set_data_bittiming = peak_usb_set_data_bittiming;
822 dev->can.do_set_mode = peak_usb_set_mode;
823 dev->can.do_get_berr_counter = peak_usb_adapter->do_get_berr_counter;
824 dev->can.ctrlmode_supported = peak_usb_adapter->ctrlmode_supported;
826 netdev->netdev_ops = &peak_usb_netdev_ops;
828 netdev->flags |= IFF_ECHO; /* we support local echo */
830 init_usb_anchor(&dev->rx_submitted);
832 init_usb_anchor(&dev->tx_submitted);
833 atomic_set(&dev->active_tx_urbs, 0);
835 for (i = 0; i < PCAN_USB_MAX_TX_URBS; i++)
836 dev->tx_contexts[i].echo_index = PCAN_USB_MAX_TX_URBS;
838 dev->prev_siblings = usb_get_intfdata(intf);
839 usb_set_intfdata(intf, dev);
841 SET_NETDEV_DEV(netdev, &intf->dev);
842 netdev->dev_id = ctrl_idx;
844 err = register_candev(netdev);
846 dev_err(&intf->dev, "couldn't register CAN device: %d\n", err);
847 goto lbl_restore_intf_data;
850 if (dev->prev_siblings)
851 (dev->prev_siblings)->next_siblings = dev;
853 /* keep hw revision into the netdevice */
854 tmp16 = le16_to_cpu(usb_dev->descriptor.bcdDevice);
855 dev->device_rev = tmp16 >> 8;
857 if (dev->adapter->dev_init) {
858 err = dev->adapter->dev_init(dev);
860 goto lbl_unregister_candev;
864 if (dev->adapter->dev_set_bus) {
865 err = dev->adapter->dev_set_bus(dev, 0);
870 /* get device number early */
871 if (dev->adapter->dev_get_device_id)
872 dev->adapter->dev_get_device_id(dev, &dev->device_number);
874 netdev_info(netdev, "attached to %s channel %u (device %u)\n",
875 peak_usb_adapter->name, ctrl_idx, dev->device_number);
880 if (dev->adapter->dev_free)
881 dev->adapter->dev_free(dev);
883 lbl_unregister_candev:
884 unregister_candev(netdev);
886 lbl_restore_intf_data:
887 usb_set_intfdata(intf, dev->prev_siblings);
897 * called by the usb core when the device is unplugged from the system
899 static void peak_usb_disconnect(struct usb_interface *intf)
901 struct peak_usb_device *dev;
902 struct peak_usb_device *dev_prev_siblings;
904 /* unregister as many netdev devices as siblings */
905 for (dev = usb_get_intfdata(intf); dev; dev = dev_prev_siblings) {
906 struct net_device *netdev = dev->netdev;
909 dev_prev_siblings = dev->prev_siblings;
910 dev->state &= ~PCAN_USB_STATE_CONNECTED;
911 strlcpy(name, netdev->name, IFNAMSIZ);
913 unregister_netdev(netdev);
916 dev->next_siblings = NULL;
917 if (dev->adapter->dev_free)
918 dev->adapter->dev_free(dev);
921 dev_info(&intf->dev, "%s removed\n", name);
924 usb_set_intfdata(intf, NULL);
928 * probe function for new PEAK-System devices
930 static int peak_usb_probe(struct usb_interface *intf,
931 const struct usb_device_id *id)
933 struct usb_device *usb_dev = interface_to_usbdev(intf);
934 const u16 usb_id_product = le16_to_cpu(usb_dev->descriptor.idProduct);
935 const struct peak_usb_adapter *peak_usb_adapter = NULL;
936 int i, err = -ENOMEM;
938 /* get corresponding PCAN-USB adapter */
939 for (i = 0; i < ARRAY_SIZE(peak_usb_adapters_list); i++)
940 if (peak_usb_adapters_list[i]->device_id == usb_id_product) {
941 peak_usb_adapter = peak_usb_adapters_list[i];
945 if (!peak_usb_adapter) {
946 /* should never come except device_id bad usage in this file */
947 pr_err("%s: didn't find device id. 0x%x in devices list\n",
948 PCAN_USB_DRIVER_NAME, usb_id_product);
952 /* got corresponding adapter: check if it handles current interface */
953 if (peak_usb_adapter->intf_probe) {
954 err = peak_usb_adapter->intf_probe(intf);
959 for (i = 0; i < peak_usb_adapter->ctrl_count; i++) {
960 err = peak_usb_create_dev(peak_usb_adapter, intf, i);
962 /* deregister already created devices */
963 peak_usb_disconnect(intf);
971 /* usb specific object needed to register this driver with the usb subsystem */
972 static struct usb_driver peak_usb_driver = {
973 .name = PCAN_USB_DRIVER_NAME,
974 .disconnect = peak_usb_disconnect,
975 .probe = peak_usb_probe,
976 .id_table = peak_usb_table,
979 static int __init peak_usb_init(void)
983 /* register this driver with the USB subsystem */
984 err = usb_register(&peak_usb_driver);
986 pr_err("%s: usb_register failed (err %d)\n",
987 PCAN_USB_DRIVER_NAME, err);
992 static int peak_usb_do_device_exit(struct device *d, void *arg)
994 struct usb_interface *intf = to_usb_interface(d);
995 struct peak_usb_device *dev;
997 /* stop as many netdev devices as siblings */
998 for (dev = usb_get_intfdata(intf); dev; dev = dev->prev_siblings) {
999 struct net_device *netdev = dev->netdev;
1001 if (netif_device_present(netdev))
1002 if (dev->adapter->dev_exit)
1003 dev->adapter->dev_exit(dev);
1009 static void __exit peak_usb_exit(void)
1013 /* last chance do send any synchronous commands here */
1014 err = driver_for_each_device(&peak_usb_driver.drvwrap.driver, NULL,
1015 NULL, peak_usb_do_device_exit);
1017 pr_err("%s: failed to stop all can devices (err %d)\n",
1018 PCAN_USB_DRIVER_NAME, err);
1020 /* deregister this driver with the USB subsystem */
1021 usb_deregister(&peak_usb_driver);
1023 pr_info("%s: PCAN-USB interfaces driver unloaded\n",
1024 PCAN_USB_DRIVER_NAME);
1027 module_init(peak_usb_init);
1028 module_exit(peak_usb_exit);