1 // SPDX-License-Identifier: GPL-2.0-only
3 * CAN driver for PEAK System USB adapters
4 * Derived from the PCAN project file driver/src/pcan_usb_core.c
6 * Copyright (C) 2003-2010 PEAK System-Technik GmbH
7 * Copyright (C) 2010-2012 Stephane Grosjean <s.grosjean@peak-system.com>
9 * Many thanks to Klaus Hitschler <klaus.hitschler@gmx.de>
11 #include <linux/init.h>
12 #include <linux/signal.h>
13 #include <linux/slab.h>
14 #include <linux/module.h>
15 #include <linux/netdevice.h>
16 #include <linux/usb.h>
18 #include <linux/can.h>
19 #include <linux/can/dev.h>
20 #include <linux/can/error.h>
22 #include "pcan_usb_core.h"
24 MODULE_AUTHOR("Stephane Grosjean <s.grosjean@peak-system.com>");
25 MODULE_DESCRIPTION("CAN driver for PEAK-System USB adapters");
26 MODULE_LICENSE("GPL v2");
28 /* Table of devices that work with this driver */
29 static struct usb_device_id peak_usb_table[] = {
30 {USB_DEVICE(PCAN_USB_VENDOR_ID, PCAN_USB_PRODUCT_ID)},
31 {USB_DEVICE(PCAN_USB_VENDOR_ID, PCAN_USBPRO_PRODUCT_ID)},
32 {USB_DEVICE(PCAN_USB_VENDOR_ID, PCAN_USBFD_PRODUCT_ID)},
33 {USB_DEVICE(PCAN_USB_VENDOR_ID, PCAN_USBPROFD_PRODUCT_ID)},
34 {USB_DEVICE(PCAN_USB_VENDOR_ID, PCAN_USBCHIP_PRODUCT_ID)},
35 {USB_DEVICE(PCAN_USB_VENDOR_ID, PCAN_USBX6_PRODUCT_ID)},
36 {} /* Terminating entry */
39 MODULE_DEVICE_TABLE(usb, peak_usb_table);
41 /* List of supported PCAN-USB adapters (NULL terminated list) */
42 static const struct peak_usb_adapter *const peak_usb_adapters_list[] = {
55 void pcan_dump_mem(char *prompt, void *p, int l)
57 pr_info("%s dumping %s (%d bytes):\n",
58 PCAN_USB_DRIVER_NAME, prompt ? prompt : "memory", l);
59 print_hex_dump(KERN_INFO, PCAN_USB_DRIVER_NAME " ", DUMP_PREFIX_NONE,
60 DUMP_WIDTH, 1, p, l, false);
64 * initialize a time_ref object with usb adapter own settings
66 void peak_usb_init_time_ref(struct peak_time_ref *time_ref,
67 const struct peak_usb_adapter *adapter)
70 memset(time_ref, 0, sizeof(struct peak_time_ref));
71 time_ref->adapter = adapter;
76 * sometimes, another now may be more recent than current one...
78 void peak_usb_update_ts_now(struct peak_time_ref *time_ref, u32 ts_now)
80 time_ref->ts_dev_2 = ts_now;
82 /* should wait at least two passes before computing */
83 if (ktime_to_ns(time_ref->tv_host) > 0) {
84 u32 delta_ts = time_ref->ts_dev_2 - time_ref->ts_dev_1;
86 if (time_ref->ts_dev_2 < time_ref->ts_dev_1)
87 delta_ts &= (1 << time_ref->adapter->ts_used_bits) - 1;
89 time_ref->ts_total += delta_ts;
94 * register device timestamp as now
96 void peak_usb_set_ts_now(struct peak_time_ref *time_ref, u32 ts_now)
98 if (ktime_to_ns(time_ref->tv_host_0) == 0) {
99 /* use monotonic clock to correctly compute further deltas */
100 time_ref->tv_host_0 = ktime_get();
101 time_ref->tv_host = ktime_set(0, 0);
104 * delta_us should not be >= 2^32 => delta should be < 4294s
105 * handle 32-bits wrapping here: if count of s. reaches 4200,
106 * reset counters and change time base
108 if (ktime_to_ns(time_ref->tv_host)) {
109 ktime_t delta = ktime_sub(time_ref->tv_host,
110 time_ref->tv_host_0);
111 if (ktime_to_ns(delta) > (4200ull * NSEC_PER_SEC)) {
112 time_ref->tv_host_0 = time_ref->tv_host;
113 time_ref->ts_total = 0;
117 time_ref->tv_host = ktime_get();
118 time_ref->tick_count++;
121 time_ref->ts_dev_1 = time_ref->ts_dev_2;
122 peak_usb_update_ts_now(time_ref, ts_now);
126 * compute time according to current ts and time_ref data
128 void peak_usb_get_ts_time(struct peak_time_ref *time_ref, u32 ts, ktime_t *time)
130 /* protect from getting time before setting now */
131 if (ktime_to_ns(time_ref->tv_host)) {
135 /* General case: dev_ts_1 < dev_ts_2 < ts, with:
137 * - dev_ts_1 = previous sync timestamp
138 * - dev_ts_2 = last sync timestamp
139 * - ts = event timestamp
140 * - ts_period = known sync period (theoretical)
141 * ~ dev_ts2 - dev_ts1
144 * - time counters wrap (see adapter->ts_used_bits)
145 * - sometimes, dev_ts_1 < ts < dev_ts2
147 * "normal" case (sync time counters increase):
148 * must take into account case when ts wraps (tsw)
152 * ---+--------+----+-------0-+--+-->
153 * ts_dev_1 | ts_dev_2 |
156 if (time_ref->ts_dev_1 < time_ref->ts_dev_2) {
157 /* case when event time (tsw) wraps */
158 if (ts < time_ref->ts_dev_1)
159 delta_ts = 1 << time_ref->adapter->ts_used_bits;
161 /* Otherwise, sync time counter (ts_dev_2) has wrapped:
162 * handle case when event time (tsn) hasn't.
166 * ---+--------+--0-+---------+--+-->
167 * ts_dev_1 | ts_dev_2 |
170 } else if (time_ref->ts_dev_1 < ts) {
171 delta_ts = -(1 << time_ref->adapter->ts_used_bits);
174 /* add delay between last sync and event timestamps */
175 delta_ts += (signed int)(ts - time_ref->ts_dev_2);
177 /* add time from beginning to last sync */
178 delta_ts += time_ref->ts_total;
180 /* convert ticks number into microseconds */
181 delta_us = delta_ts * time_ref->adapter->us_per_ts_scale;
182 delta_us >>= time_ref->adapter->us_per_ts_shift;
184 *time = ktime_add_us(time_ref->tv_host_0, delta_us);
191 * post received skb after having set any hw timestamp
193 int peak_usb_netif_rx(struct sk_buff *skb,
194 struct peak_time_ref *time_ref, u32 ts_low)
196 struct skb_shared_hwtstamps *hwts = skb_hwtstamps(skb);
198 peak_usb_get_ts_time(time_ref, ts_low, &hwts->hwtstamp);
200 return netif_rx(skb);
204 * callback for bulk Rx urb
206 static void peak_usb_read_bulk_callback(struct urb *urb)
208 struct peak_usb_device *dev = urb->context;
209 struct net_device *netdev;
212 netdev = dev->netdev;
214 if (!netif_device_present(netdev))
217 /* check reception status */
218 switch (urb->status) {
232 "Rx urb aborted (%d)\n", urb->status);
236 /* protect from any incoming empty msgs */
237 if ((urb->actual_length > 0) && (dev->adapter->dev_decode_buf)) {
238 /* handle these kinds of msgs only if _start callback called */
239 if (dev->state & PCAN_USB_STATE_STARTED) {
240 err = dev->adapter->dev_decode_buf(dev, urb);
242 pcan_dump_mem("received usb message",
243 urb->transfer_buffer,
244 urb->transfer_buffer_length);
249 usb_fill_bulk_urb(urb, dev->udev,
250 usb_rcvbulkpipe(dev->udev, dev->ep_msg_in),
251 urb->transfer_buffer, dev->adapter->rx_buffer_size,
252 peak_usb_read_bulk_callback, dev);
254 usb_anchor_urb(urb, &dev->rx_submitted);
255 err = usb_submit_urb(urb, GFP_ATOMIC);
259 usb_unanchor_urb(urb);
262 netif_device_detach(netdev);
264 netdev_err(netdev, "failed resubmitting read bulk urb: %d\n",
269 * callback for bulk Tx urb
271 static void peak_usb_write_bulk_callback(struct urb *urb)
273 struct peak_tx_urb_context *context = urb->context;
274 struct peak_usb_device *dev;
275 struct net_device *netdev;
280 netdev = dev->netdev;
282 atomic_dec(&dev->active_tx_urbs);
284 if (!netif_device_present(netdev))
287 /* check tx status */
288 switch (urb->status) {
290 /* transmission complete */
291 netdev->stats.tx_packets++;
292 netdev->stats.tx_bytes += context->data_len;
294 /* prevent tx timeout */
295 netif_trans_update(netdev);
300 netdev_err(netdev, "Tx urb aborted (%d)\n",
310 /* should always release echo skb and corresponding context */
311 can_get_echo_skb(netdev, context->echo_index);
312 context->echo_index = PCAN_USB_MAX_TX_URBS;
314 /* do wakeup tx queue in case of success only */
316 netif_wake_queue(netdev);
320 * called by netdev to send one skb on the CAN interface.
322 static netdev_tx_t peak_usb_ndo_start_xmit(struct sk_buff *skb,
323 struct net_device *netdev)
325 struct peak_usb_device *dev = netdev_priv(netdev);
326 struct peak_tx_urb_context *context = NULL;
327 struct net_device_stats *stats = &netdev->stats;
328 struct canfd_frame *cfd = (struct canfd_frame *)skb->data;
332 size_t size = dev->adapter->tx_buffer_size;
334 if (can_dropped_invalid_skb(netdev, skb))
337 for (i = 0; i < PCAN_USB_MAX_TX_URBS; i++)
338 if (dev->tx_contexts[i].echo_index == PCAN_USB_MAX_TX_URBS) {
339 context = dev->tx_contexts + i;
344 /* should not occur except during restart */
345 return NETDEV_TX_BUSY;
349 obuf = urb->transfer_buffer;
351 err = dev->adapter->dev_encode_msg(dev, skb, obuf, &size);
354 netdev_err(netdev, "packet dropped\n");
360 context->echo_index = i;
362 /* Note: this works with CANFD frames too */
363 context->data_len = cfd->len;
365 usb_anchor_urb(urb, &dev->tx_submitted);
367 can_put_echo_skb(skb, netdev, context->echo_index);
369 atomic_inc(&dev->active_tx_urbs);
371 err = usb_submit_urb(urb, GFP_ATOMIC);
373 can_free_echo_skb(netdev, context->echo_index);
375 usb_unanchor_urb(urb);
377 /* this context is not used in fact */
378 context->echo_index = PCAN_USB_MAX_TX_URBS;
380 atomic_dec(&dev->active_tx_urbs);
384 netif_device_detach(netdev);
387 netdev_warn(netdev, "tx urb submitting failed err=%d\n",
391 /* cable unplugged */
395 netif_trans_update(netdev);
397 /* slow down tx path */
398 if (atomic_read(&dev->active_tx_urbs) >= PCAN_USB_MAX_TX_URBS)
399 netif_stop_queue(netdev);
406 * start the CAN interface.
407 * Rx and Tx urbs are allocated here. Rx urbs are submitted here.
409 static int peak_usb_start(struct peak_usb_device *dev)
411 struct net_device *netdev = dev->netdev;
414 for (i = 0; i < PCAN_USB_MAX_RX_URBS; i++) {
418 /* create a URB, and a buffer for it, to receive usb messages */
419 urb = usb_alloc_urb(0, GFP_KERNEL);
425 buf = kmalloc(dev->adapter->rx_buffer_size, GFP_KERNEL);
432 usb_fill_bulk_urb(urb, dev->udev,
433 usb_rcvbulkpipe(dev->udev, dev->ep_msg_in),
434 buf, dev->adapter->rx_buffer_size,
435 peak_usb_read_bulk_callback, dev);
437 /* ask last usb_free_urb() to also kfree() transfer_buffer */
438 urb->transfer_flags |= URB_FREE_BUFFER;
439 usb_anchor_urb(urb, &dev->rx_submitted);
441 err = usb_submit_urb(urb, GFP_KERNEL);
444 netif_device_detach(dev->netdev);
446 usb_unanchor_urb(urb);
452 /* drop reference, USB core will take care of freeing it */
456 /* did we submit any URBs? Warn if we was not able to submit all urbs */
457 if (i < PCAN_USB_MAX_RX_URBS) {
459 netdev_err(netdev, "couldn't setup any rx URB\n");
463 netdev_warn(netdev, "rx performance may be slow\n");
466 /* pre-alloc tx buffers and corresponding urbs */
467 for (i = 0; i < PCAN_USB_MAX_TX_URBS; i++) {
468 struct peak_tx_urb_context *context;
472 /* create a URB and a buffer for it, to transmit usb messages */
473 urb = usb_alloc_urb(0, GFP_KERNEL);
479 buf = kmalloc(dev->adapter->tx_buffer_size, GFP_KERNEL);
486 context = dev->tx_contexts + i;
490 usb_fill_bulk_urb(urb, dev->udev,
491 usb_sndbulkpipe(dev->udev, dev->ep_msg_out),
492 buf, dev->adapter->tx_buffer_size,
493 peak_usb_write_bulk_callback, context);
495 /* ask last usb_free_urb() to also kfree() transfer_buffer */
496 urb->transfer_flags |= URB_FREE_BUFFER;
499 /* warn if we were not able to allocate enough tx contexts */
500 if (i < PCAN_USB_MAX_TX_URBS) {
502 netdev_err(netdev, "couldn't setup any tx URB\n");
506 netdev_warn(netdev, "tx performance may be slow\n");
509 if (dev->adapter->dev_start) {
510 err = dev->adapter->dev_start(dev);
515 dev->state |= PCAN_USB_STATE_STARTED;
517 /* can set bus on now */
518 if (dev->adapter->dev_set_bus) {
519 err = dev->adapter->dev_set_bus(dev, 1);
524 dev->can.state = CAN_STATE_ERROR_ACTIVE;
530 netif_device_detach(dev->netdev);
532 netdev_warn(netdev, "couldn't submit control: %d\n", err);
534 for (i = 0; i < PCAN_USB_MAX_TX_URBS; i++) {
535 usb_free_urb(dev->tx_contexts[i].urb);
536 dev->tx_contexts[i].urb = NULL;
539 usb_kill_anchored_urbs(&dev->rx_submitted);
545 * called by netdev to open the corresponding CAN interface.
547 static int peak_usb_ndo_open(struct net_device *netdev)
549 struct peak_usb_device *dev = netdev_priv(netdev);
553 err = open_candev(netdev);
557 /* finally start device */
558 err = peak_usb_start(dev);
560 netdev_err(netdev, "couldn't start device: %d\n", err);
561 close_candev(netdev);
565 netif_start_queue(netdev);
571 * unlink in-flight Rx and Tx urbs and free their memory.
573 static void peak_usb_unlink_all_urbs(struct peak_usb_device *dev)
577 /* free all Rx (submitted) urbs */
578 usb_kill_anchored_urbs(&dev->rx_submitted);
580 /* free unsubmitted Tx urbs first */
581 for (i = 0; i < PCAN_USB_MAX_TX_URBS; i++) {
582 struct urb *urb = dev->tx_contexts[i].urb;
585 dev->tx_contexts[i].echo_index != PCAN_USB_MAX_TX_URBS) {
587 * this urb is already released or always submitted,
588 * let usb core free by itself
594 dev->tx_contexts[i].urb = NULL;
597 /* then free all submitted Tx urbs */
598 usb_kill_anchored_urbs(&dev->tx_submitted);
599 atomic_set(&dev->active_tx_urbs, 0);
603 * called by netdev to close the corresponding CAN interface.
605 static int peak_usb_ndo_stop(struct net_device *netdev)
607 struct peak_usb_device *dev = netdev_priv(netdev);
609 dev->state &= ~PCAN_USB_STATE_STARTED;
610 netif_stop_queue(netdev);
612 close_candev(netdev);
614 dev->can.state = CAN_STATE_STOPPED;
616 /* unlink all pending urbs and free used memory */
617 peak_usb_unlink_all_urbs(dev);
619 if (dev->adapter->dev_stop)
620 dev->adapter->dev_stop(dev);
622 /* can set bus off now */
623 if (dev->adapter->dev_set_bus) {
624 int err = dev->adapter->dev_set_bus(dev, 0);
633 * handle end of waiting for the device to reset
635 void peak_usb_restart_complete(struct peak_usb_device *dev)
637 /* finally MUST update can state */
638 dev->can.state = CAN_STATE_ERROR_ACTIVE;
640 /* netdev queue can be awaken now */
641 netif_wake_queue(dev->netdev);
644 void peak_usb_async_complete(struct urb *urb)
646 kfree(urb->transfer_buffer);
651 * device (auto-)restart mechanism runs in a timer context =>
652 * MUST handle restart with asynchronous usb transfers
654 static int peak_usb_restart(struct peak_usb_device *dev)
661 * if device doesn't define any asynchronous restart handler, simply
662 * wake the netdev queue up
664 if (!dev->adapter->dev_restart_async) {
665 peak_usb_restart_complete(dev);
669 /* first allocate a urb to handle the asynchronous steps */
670 urb = usb_alloc_urb(0, GFP_ATOMIC);
674 /* also allocate enough space for the commands to send */
675 buf = kmalloc(PCAN_USB_MAX_CMD_LEN, GFP_ATOMIC);
681 /* call the device specific handler for the restart */
682 err = dev->adapter->dev_restart_async(dev, urb, buf);
693 * candev callback used to change CAN mode.
694 * Warning: this is called from a timer context!
696 static int peak_usb_set_mode(struct net_device *netdev, enum can_mode mode)
698 struct peak_usb_device *dev = netdev_priv(netdev);
703 err = peak_usb_restart(dev);
705 netdev_err(netdev, "couldn't start device (err %d)\n",
717 * candev callback used to set device nominal/arbitration bitrate.
719 static int peak_usb_set_bittiming(struct net_device *netdev)
721 struct peak_usb_device *dev = netdev_priv(netdev);
722 const struct peak_usb_adapter *pa = dev->adapter;
724 if (pa->dev_set_bittiming) {
725 struct can_bittiming *bt = &dev->can.bittiming;
726 int err = pa->dev_set_bittiming(dev, bt);
729 netdev_info(netdev, "couldn't set bitrate (err %d)\n",
738 * candev callback used to set device data bitrate.
740 static int peak_usb_set_data_bittiming(struct net_device *netdev)
742 struct peak_usb_device *dev = netdev_priv(netdev);
743 const struct peak_usb_adapter *pa = dev->adapter;
745 if (pa->dev_set_data_bittiming) {
746 struct can_bittiming *bt = &dev->can.data_bittiming;
747 int err = pa->dev_set_data_bittiming(dev, bt);
751 "couldn't set data bitrate (err %d)\n",
760 static const struct net_device_ops peak_usb_netdev_ops = {
761 .ndo_open = peak_usb_ndo_open,
762 .ndo_stop = peak_usb_ndo_stop,
763 .ndo_start_xmit = peak_usb_ndo_start_xmit,
764 .ndo_change_mtu = can_change_mtu,
768 * create one device which is attached to CAN controller #ctrl_idx of the
771 static int peak_usb_create_dev(const struct peak_usb_adapter *peak_usb_adapter,
772 struct usb_interface *intf, int ctrl_idx)
774 struct usb_device *usb_dev = interface_to_usbdev(intf);
775 int sizeof_candev = peak_usb_adapter->sizeof_dev_private;
776 struct peak_usb_device *dev;
777 struct net_device *netdev;
781 if (sizeof_candev < sizeof(struct peak_usb_device))
782 sizeof_candev = sizeof(struct peak_usb_device);
784 netdev = alloc_candev(sizeof_candev, PCAN_USB_MAX_TX_URBS);
786 dev_err(&intf->dev, "%s: couldn't alloc candev\n",
787 PCAN_USB_DRIVER_NAME);
791 dev = netdev_priv(netdev);
793 /* allocate a buffer large enough to send commands */
794 dev->cmd_buf = kzalloc(PCAN_USB_MAX_CMD_LEN, GFP_KERNEL);
797 goto lbl_free_candev;
801 dev->netdev = netdev;
802 dev->adapter = peak_usb_adapter;
803 dev->ctrl_idx = ctrl_idx;
804 dev->state = PCAN_USB_STATE_CONNECTED;
806 dev->ep_msg_in = peak_usb_adapter->ep_msg_in;
807 dev->ep_msg_out = peak_usb_adapter->ep_msg_out[ctrl_idx];
809 dev->can.clock = peak_usb_adapter->clock;
810 dev->can.bittiming_const = peak_usb_adapter->bittiming_const;
811 dev->can.do_set_bittiming = peak_usb_set_bittiming;
812 dev->can.data_bittiming_const = peak_usb_adapter->data_bittiming_const;
813 dev->can.do_set_data_bittiming = peak_usb_set_data_bittiming;
814 dev->can.do_set_mode = peak_usb_set_mode;
815 dev->can.do_get_berr_counter = peak_usb_adapter->do_get_berr_counter;
816 dev->can.ctrlmode_supported = peak_usb_adapter->ctrlmode_supported;
818 netdev->netdev_ops = &peak_usb_netdev_ops;
820 netdev->flags |= IFF_ECHO; /* we support local echo */
822 init_usb_anchor(&dev->rx_submitted);
824 init_usb_anchor(&dev->tx_submitted);
825 atomic_set(&dev->active_tx_urbs, 0);
827 for (i = 0; i < PCAN_USB_MAX_TX_URBS; i++)
828 dev->tx_contexts[i].echo_index = PCAN_USB_MAX_TX_URBS;
830 dev->prev_siblings = usb_get_intfdata(intf);
831 usb_set_intfdata(intf, dev);
833 SET_NETDEV_DEV(netdev, &intf->dev);
834 netdev->dev_id = ctrl_idx;
836 err = register_candev(netdev);
838 dev_err(&intf->dev, "couldn't register CAN device: %d\n", err);
839 goto lbl_restore_intf_data;
842 if (dev->prev_siblings)
843 (dev->prev_siblings)->next_siblings = dev;
845 /* keep hw revision into the netdevice */
846 tmp16 = le16_to_cpu(usb_dev->descriptor.bcdDevice);
847 dev->device_rev = tmp16 >> 8;
849 if (dev->adapter->dev_init) {
850 err = dev->adapter->dev_init(dev);
852 goto lbl_unregister_candev;
856 if (dev->adapter->dev_set_bus) {
857 err = dev->adapter->dev_set_bus(dev, 0);
859 goto lbl_unregister_candev;
862 /* get device number early */
863 if (dev->adapter->dev_get_device_id)
864 dev->adapter->dev_get_device_id(dev, &dev->device_number);
866 netdev_info(netdev, "attached to %s channel %u (device %u)\n",
867 peak_usb_adapter->name, ctrl_idx, dev->device_number);
871 lbl_unregister_candev:
872 unregister_candev(netdev);
874 lbl_restore_intf_data:
875 usb_set_intfdata(intf, dev->prev_siblings);
885 * called by the usb core when the device is unplugged from the system
887 static void peak_usb_disconnect(struct usb_interface *intf)
889 struct peak_usb_device *dev;
890 struct peak_usb_device *dev_prev_siblings;
892 /* unregister as many netdev devices as siblings */
893 for (dev = usb_get_intfdata(intf); dev; dev = dev_prev_siblings) {
894 struct net_device *netdev = dev->netdev;
897 dev_prev_siblings = dev->prev_siblings;
898 dev->state &= ~PCAN_USB_STATE_CONNECTED;
899 strlcpy(name, netdev->name, IFNAMSIZ);
901 unregister_netdev(netdev);
904 dev->next_siblings = NULL;
905 if (dev->adapter->dev_free)
906 dev->adapter->dev_free(dev);
909 dev_info(&intf->dev, "%s removed\n", name);
912 usb_set_intfdata(intf, NULL);
916 * probe function for new PEAK-System devices
918 static int peak_usb_probe(struct usb_interface *intf,
919 const struct usb_device_id *id)
921 struct usb_device *usb_dev = interface_to_usbdev(intf);
922 const u16 usb_id_product = le16_to_cpu(usb_dev->descriptor.idProduct);
923 const struct peak_usb_adapter *peak_usb_adapter = NULL;
924 int i, err = -ENOMEM;
926 /* get corresponding PCAN-USB adapter */
927 for (i = 0; i < ARRAY_SIZE(peak_usb_adapters_list); i++)
928 if (peak_usb_adapters_list[i]->device_id == usb_id_product) {
929 peak_usb_adapter = peak_usb_adapters_list[i];
933 if (!peak_usb_adapter) {
934 /* should never come except device_id bad usage in this file */
935 pr_err("%s: didn't find device id. 0x%x in devices list\n",
936 PCAN_USB_DRIVER_NAME, usb_id_product);
940 /* got corresponding adapter: check if it handles current interface */
941 if (peak_usb_adapter->intf_probe) {
942 err = peak_usb_adapter->intf_probe(intf);
947 for (i = 0; i < peak_usb_adapter->ctrl_count; i++) {
948 err = peak_usb_create_dev(peak_usb_adapter, intf, i);
950 /* deregister already created devices */
951 peak_usb_disconnect(intf);
959 /* usb specific object needed to register this driver with the usb subsystem */
960 static struct usb_driver peak_usb_driver = {
961 .name = PCAN_USB_DRIVER_NAME,
962 .disconnect = peak_usb_disconnect,
963 .probe = peak_usb_probe,
964 .id_table = peak_usb_table,
967 static int __init peak_usb_init(void)
971 /* register this driver with the USB subsystem */
972 err = usb_register(&peak_usb_driver);
974 pr_err("%s: usb_register failed (err %d)\n",
975 PCAN_USB_DRIVER_NAME, err);
980 static int peak_usb_do_device_exit(struct device *d, void *arg)
982 struct usb_interface *intf = to_usb_interface(d);
983 struct peak_usb_device *dev;
985 /* stop as many netdev devices as siblings */
986 for (dev = usb_get_intfdata(intf); dev; dev = dev->prev_siblings) {
987 struct net_device *netdev = dev->netdev;
989 if (netif_device_present(netdev))
990 if (dev->adapter->dev_exit)
991 dev->adapter->dev_exit(dev);
997 static void __exit peak_usb_exit(void)
1001 /* last chance do send any synchronous commands here */
1002 err = driver_for_each_device(&peak_usb_driver.drvwrap.driver, NULL,
1003 NULL, peak_usb_do_device_exit);
1005 pr_err("%s: failed to stop all can devices (err %d)\n",
1006 PCAN_USB_DRIVER_NAME, err);
1008 /* deregister this driver with the USB subsystem */
1009 usb_deregister(&peak_usb_driver);
1011 pr_info("%s: PCAN-USB interfaces driver unloaded\n",
1012 PCAN_USB_DRIVER_NAME);
1015 module_init(peak_usb_init);
1016 module_exit(peak_usb_exit);