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 {} /* Terminating entry */
45 MODULE_DEVICE_TABLE(usb, peak_usb_table);
47 /* List of supported PCAN-USB adapters (NULL terminated list) */
48 static const struct peak_usb_adapter *const peak_usb_adapters_list[] = {
59 void pcan_dump_mem(char *prompt, void *p, int l)
61 pr_info("%s dumping %s (%d bytes):\n",
62 PCAN_USB_DRIVER_NAME, prompt ? prompt : "memory", l);
63 print_hex_dump(KERN_INFO, PCAN_USB_DRIVER_NAME " ", DUMP_PREFIX_NONE,
64 DUMP_WIDTH, 1, p, l, false);
68 * initialize a time_ref object with usb adapter own settings
70 void peak_usb_init_time_ref(struct peak_time_ref *time_ref,
71 const struct peak_usb_adapter *adapter)
74 memset(time_ref, 0, sizeof(struct peak_time_ref));
75 time_ref->adapter = adapter;
79 static void peak_usb_add_us(struct timeval *tv, u32 delta_us)
81 /* number of s. to add to final time */
82 u32 delta_s = delta_us / 1000000;
84 delta_us -= delta_s * 1000000;
86 tv->tv_usec += delta_us;
87 if (tv->tv_usec >= 1000000) {
88 tv->tv_usec -= 1000000;
91 tv->tv_sec += delta_s;
95 * sometimes, another now may be more recent than current one...
97 void peak_usb_update_ts_now(struct peak_time_ref *time_ref, u32 ts_now)
99 time_ref->ts_dev_2 = ts_now;
101 /* should wait at least two passes before computing */
102 if (time_ref->tv_host.tv_sec > 0) {
103 u32 delta_ts = time_ref->ts_dev_2 - time_ref->ts_dev_1;
105 if (time_ref->ts_dev_2 < time_ref->ts_dev_1)
106 delta_ts &= (1 << time_ref->adapter->ts_used_bits) - 1;
108 time_ref->ts_total += delta_ts;
113 * register device timestamp as now
115 void peak_usb_set_ts_now(struct peak_time_ref *time_ref, u32 ts_now)
117 if (time_ref->tv_host_0.tv_sec == 0) {
118 /* use monotonic clock to correctly compute further deltas */
119 time_ref->tv_host_0 = ktime_to_timeval(ktime_get());
120 time_ref->tv_host.tv_sec = 0;
123 * delta_us should not be >= 2^32 => delta_s should be < 4294
124 * handle 32-bits wrapping here: if count of s. reaches 4200,
125 * reset counters and change time base
127 if (time_ref->tv_host.tv_sec != 0) {
128 u32 delta_s = time_ref->tv_host.tv_sec
129 - time_ref->tv_host_0.tv_sec;
130 if (delta_s > 4200) {
131 time_ref->tv_host_0 = time_ref->tv_host;
132 time_ref->ts_total = 0;
136 time_ref->tv_host = ktime_to_timeval(ktime_get());
137 time_ref->tick_count++;
140 time_ref->ts_dev_1 = time_ref->ts_dev_2;
141 peak_usb_update_ts_now(time_ref, ts_now);
145 * compute timeval according to current ts and time_ref data
147 void peak_usb_get_ts_tv(struct peak_time_ref *time_ref, u32 ts,
150 /* protect from getting timeval before setting now */
151 if (time_ref->tv_host.tv_sec > 0) {
155 /* General case: dev_ts_1 < dev_ts_2 < ts, with:
157 * - dev_ts_1 = previous sync timestamp
158 * - dev_ts_2 = last sync timestamp
159 * - ts = event timestamp
160 * - ts_period = known sync period (theoretical)
161 * ~ dev_ts2 - dev_ts1
164 * - time counters wrap (see adapter->ts_used_bits)
165 * - sometimes, dev_ts_1 < ts < dev_ts2
167 * "normal" case (sync time counters increase):
168 * must take into account case when ts wraps (tsw)
172 * ---+--------+----+-------0-+--+-->
173 * ts_dev_1 | ts_dev_2 |
176 if (time_ref->ts_dev_1 < time_ref->ts_dev_2) {
177 /* case when event time (tsw) wraps */
178 if (ts < time_ref->ts_dev_1)
179 delta_ts = BIT_ULL(time_ref->adapter->ts_used_bits);
181 /* Otherwise, sync time counter (ts_dev_2) has wrapped:
182 * handle case when event time (tsn) hasn't.
186 * ---+--------+--0-+---------+--+-->
187 * ts_dev_1 | ts_dev_2 |
190 } else if (time_ref->ts_dev_1 < ts) {
191 delta_ts = -BIT_ULL(time_ref->adapter->ts_used_bits);
194 /* add delay between last sync and event timestamps */
195 delta_ts += (signed int)(ts - time_ref->ts_dev_2);
197 /* add time from beginning to last sync */
198 delta_ts += time_ref->ts_total;
200 /* convert ticks number into microseconds */
201 delta_us = delta_ts * time_ref->adapter->us_per_ts_scale;
202 delta_us >>= time_ref->adapter->us_per_ts_shift;
204 *tv = time_ref->tv_host_0;
205 peak_usb_add_us(tv, (u32)delta_us);
207 *tv = ktime_to_timeval(ktime_get());
212 * post received skb after having set any hw timestamp
214 int peak_usb_netif_rx(struct sk_buff *skb,
215 struct peak_time_ref *time_ref, u32 ts_low, u32 ts_high)
217 struct skb_shared_hwtstamps *hwts = skb_hwtstamps(skb);
220 peak_usb_get_ts_tv(time_ref, ts_low, &tv);
221 hwts->hwtstamp = timeval_to_ktime(tv);
223 return netif_rx(skb);
227 * callback for bulk Rx urb
229 static void peak_usb_read_bulk_callback(struct urb *urb)
231 struct peak_usb_device *dev = urb->context;
232 struct net_device *netdev;
235 netdev = dev->netdev;
237 if (!netif_device_present(netdev))
240 /* check reception status */
241 switch (urb->status) {
255 "Rx urb aborted (%d)\n", urb->status);
259 /* protect from any incoming empty msgs */
260 if ((urb->actual_length > 0) && (dev->adapter->dev_decode_buf)) {
261 /* handle these kinds of msgs only if _start callback called */
262 if (dev->state & PCAN_USB_STATE_STARTED) {
263 err = dev->adapter->dev_decode_buf(dev, urb);
265 pcan_dump_mem("received usb message",
266 urb->transfer_buffer,
267 urb->transfer_buffer_length);
272 usb_fill_bulk_urb(urb, dev->udev,
273 usb_rcvbulkpipe(dev->udev, dev->ep_msg_in),
274 urb->transfer_buffer, dev->adapter->rx_buffer_size,
275 peak_usb_read_bulk_callback, dev);
277 usb_anchor_urb(urb, &dev->rx_submitted);
278 err = usb_submit_urb(urb, GFP_ATOMIC);
282 usb_unanchor_urb(urb);
285 netif_device_detach(netdev);
287 netdev_err(netdev, "failed resubmitting read bulk urb: %d\n",
292 * callback for bulk Tx urb
294 static void peak_usb_write_bulk_callback(struct urb *urb)
296 struct peak_tx_urb_context *context = urb->context;
297 struct peak_usb_device *dev;
298 struct net_device *netdev;
303 netdev = dev->netdev;
305 atomic_dec(&dev->active_tx_urbs);
307 if (!netif_device_present(netdev))
310 /* check tx status */
311 switch (urb->status) {
313 /* transmission complete */
314 netdev->stats.tx_packets++;
315 netdev->stats.tx_bytes += context->data_len;
317 /* prevent tx timeout */
318 netdev->trans_start = jiffies;
323 netdev_err(netdev, "Tx urb aborted (%d)\n",
333 /* should always release echo skb and corresponding context */
334 can_get_echo_skb(netdev, context->echo_index);
335 context->echo_index = PCAN_USB_MAX_TX_URBS;
337 /* do wakeup tx queue in case of success only */
339 netif_wake_queue(netdev);
343 * called by netdev to send one skb on the CAN interface.
345 static netdev_tx_t peak_usb_ndo_start_xmit(struct sk_buff *skb,
346 struct net_device *netdev)
348 struct peak_usb_device *dev = netdev_priv(netdev);
349 struct peak_tx_urb_context *context = NULL;
350 struct net_device_stats *stats = &netdev->stats;
351 struct canfd_frame *cfd = (struct canfd_frame *)skb->data;
355 size_t size = dev->adapter->tx_buffer_size;
357 if (can_dropped_invalid_skb(netdev, skb))
360 for (i = 0; i < PCAN_USB_MAX_TX_URBS; i++)
361 if (dev->tx_contexts[i].echo_index == PCAN_USB_MAX_TX_URBS) {
362 context = dev->tx_contexts + i;
367 /* should not occur except during restart */
368 return NETDEV_TX_BUSY;
372 obuf = urb->transfer_buffer;
374 err = dev->adapter->dev_encode_msg(dev, skb, obuf, &size);
377 netdev_err(netdev, "packet dropped\n");
383 context->echo_index = i;
385 /* Note: this works with CANFD frames too */
386 context->data_len = cfd->len;
388 usb_anchor_urb(urb, &dev->tx_submitted);
390 can_put_echo_skb(skb, netdev, context->echo_index);
392 atomic_inc(&dev->active_tx_urbs);
394 err = usb_submit_urb(urb, GFP_ATOMIC);
396 can_free_echo_skb(netdev, context->echo_index);
398 usb_unanchor_urb(urb);
400 /* this context is not used in fact */
401 context->echo_index = PCAN_USB_MAX_TX_URBS;
403 atomic_dec(&dev->active_tx_urbs);
407 netif_device_detach(netdev);
410 netdev_warn(netdev, "tx urb submitting failed err=%d\n",
413 /* cable unplugged */
417 netdev->trans_start = jiffies;
419 /* slow down tx path */
420 if (atomic_read(&dev->active_tx_urbs) >= PCAN_USB_MAX_TX_URBS)
421 netif_stop_queue(netdev);
428 * start the CAN interface.
429 * Rx and Tx urbs are allocated here. Rx urbs are submitted here.
431 static int peak_usb_start(struct peak_usb_device *dev)
433 struct net_device *netdev = dev->netdev;
436 for (i = 0; i < PCAN_USB_MAX_RX_URBS; i++) {
440 /* create a URB, and a buffer for it, to receive usb messages */
441 urb = usb_alloc_urb(0, GFP_KERNEL);
443 netdev_err(netdev, "No memory left for URBs\n");
448 buf = kmalloc(dev->adapter->rx_buffer_size, GFP_KERNEL);
455 usb_fill_bulk_urb(urb, dev->udev,
456 usb_rcvbulkpipe(dev->udev, dev->ep_msg_in),
457 buf, dev->adapter->rx_buffer_size,
458 peak_usb_read_bulk_callback, dev);
460 /* ask last usb_free_urb() to also kfree() transfer_buffer */
461 urb->transfer_flags |= URB_FREE_BUFFER;
462 usb_anchor_urb(urb, &dev->rx_submitted);
464 err = usb_submit_urb(urb, GFP_KERNEL);
467 netif_device_detach(dev->netdev);
469 usb_unanchor_urb(urb);
475 /* drop reference, USB core will take care of freeing it */
479 /* did we submit any URBs? Warn if we was not able to submit all urbs */
480 if (i < PCAN_USB_MAX_RX_URBS) {
482 netdev_err(netdev, "couldn't setup any rx URB\n");
486 netdev_warn(netdev, "rx performance may be slow\n");
489 /* pre-alloc tx buffers and corresponding urbs */
490 for (i = 0; i < PCAN_USB_MAX_TX_URBS; i++) {
491 struct peak_tx_urb_context *context;
495 /* create a URB and a buffer for it, to transmit usb messages */
496 urb = usb_alloc_urb(0, GFP_KERNEL);
498 netdev_err(netdev, "No memory left for URBs\n");
503 buf = kmalloc(dev->adapter->tx_buffer_size, GFP_KERNEL);
510 context = dev->tx_contexts + i;
514 usb_fill_bulk_urb(urb, dev->udev,
515 usb_sndbulkpipe(dev->udev, dev->ep_msg_out),
516 buf, dev->adapter->tx_buffer_size,
517 peak_usb_write_bulk_callback, context);
519 /* ask last usb_free_urb() to also kfree() transfer_buffer */
520 urb->transfer_flags |= URB_FREE_BUFFER;
523 /* warn if we were not able to allocate enough tx contexts */
524 if (i < PCAN_USB_MAX_TX_URBS) {
526 netdev_err(netdev, "couldn't setup any tx URB\n");
530 netdev_warn(netdev, "tx performance may be slow\n");
533 if (dev->adapter->dev_start) {
534 err = dev->adapter->dev_start(dev);
539 dev->state |= PCAN_USB_STATE_STARTED;
541 /* can set bus on now */
542 if (dev->adapter->dev_set_bus) {
543 err = dev->adapter->dev_set_bus(dev, 1);
548 dev->can.state = CAN_STATE_ERROR_ACTIVE;
554 netif_device_detach(dev->netdev);
556 netdev_warn(netdev, "couldn't submit control: %d\n", err);
558 for (i = 0; i < PCAN_USB_MAX_TX_URBS; i++) {
559 usb_free_urb(dev->tx_contexts[i].urb);
560 dev->tx_contexts[i].urb = NULL;
563 usb_kill_anchored_urbs(&dev->rx_submitted);
569 * called by netdev to open the corresponding CAN interface.
571 static int peak_usb_ndo_open(struct net_device *netdev)
573 struct peak_usb_device *dev = netdev_priv(netdev);
577 err = open_candev(netdev);
581 /* finally start device */
582 err = peak_usb_start(dev);
584 netdev_err(netdev, "couldn't start device: %d\n", err);
585 close_candev(netdev);
589 netif_start_queue(netdev);
595 * unlink in-flight Rx and Tx urbs and free their memory.
597 static void peak_usb_unlink_all_urbs(struct peak_usb_device *dev)
601 /* free all Rx (submitted) urbs */
602 usb_kill_anchored_urbs(&dev->rx_submitted);
604 /* free unsubmitted Tx urbs first */
605 for (i = 0; i < PCAN_USB_MAX_TX_URBS; i++) {
606 struct urb *urb = dev->tx_contexts[i].urb;
609 dev->tx_contexts[i].echo_index != PCAN_USB_MAX_TX_URBS) {
611 * this urb is already released or always submitted,
612 * let usb core free by itself
618 dev->tx_contexts[i].urb = NULL;
621 /* then free all submitted Tx urbs */
622 usb_kill_anchored_urbs(&dev->tx_submitted);
623 atomic_set(&dev->active_tx_urbs, 0);
627 * called by netdev to close the corresponding CAN interface.
629 static int peak_usb_ndo_stop(struct net_device *netdev)
631 struct peak_usb_device *dev = netdev_priv(netdev);
633 dev->state &= ~PCAN_USB_STATE_STARTED;
634 netif_stop_queue(netdev);
636 close_candev(netdev);
638 dev->can.state = CAN_STATE_STOPPED;
640 /* unlink all pending urbs and free used memory */
641 peak_usb_unlink_all_urbs(dev);
643 if (dev->adapter->dev_stop)
644 dev->adapter->dev_stop(dev);
646 /* can set bus off now */
647 if (dev->adapter->dev_set_bus) {
648 int err = dev->adapter->dev_set_bus(dev, 0);
657 * handle end of waiting for the device to reset
659 void peak_usb_restart_complete(struct peak_usb_device *dev)
661 /* finally MUST update can state */
662 dev->can.state = CAN_STATE_ERROR_ACTIVE;
664 /* netdev queue can be awaken now */
665 netif_wake_queue(dev->netdev);
668 void peak_usb_async_complete(struct urb *urb)
670 kfree(urb->transfer_buffer);
675 * device (auto-)restart mechanism runs in a timer context =>
676 * MUST handle restart with asynchronous usb transfers
678 static int peak_usb_restart(struct peak_usb_device *dev)
685 * if device doesn't define any asynchronous restart handler, simply
686 * wake the netdev queue up
688 if (!dev->adapter->dev_restart_async) {
689 peak_usb_restart_complete(dev);
693 /* first allocate a urb to handle the asynchronous steps */
694 urb = usb_alloc_urb(0, GFP_ATOMIC);
696 netdev_err(dev->netdev, "no memory left for urb\n");
700 /* also allocate enough space for the commands to send */
701 buf = kmalloc(PCAN_USB_MAX_CMD_LEN, GFP_ATOMIC);
707 /* call the device specific handler for the restart */
708 err = dev->adapter->dev_restart_async(dev, urb, buf);
719 * candev callback used to change CAN mode.
720 * Warning: this is called from a timer context!
722 static int peak_usb_set_mode(struct net_device *netdev, enum can_mode mode)
724 struct peak_usb_device *dev = netdev_priv(netdev);
729 err = peak_usb_restart(dev);
731 netdev_err(netdev, "couldn't start device (err %d)\n",
743 * candev callback used to set device nominal/arbitration bitrate.
745 static int peak_usb_set_bittiming(struct net_device *netdev)
747 struct peak_usb_device *dev = netdev_priv(netdev);
748 const struct peak_usb_adapter *pa = dev->adapter;
750 if (pa->dev_set_bittiming) {
751 struct can_bittiming *bt = &dev->can.bittiming;
752 int err = pa->dev_set_bittiming(dev, bt);
755 netdev_info(netdev, "couldn't set bitrate (err %d)\n",
764 * candev callback used to set device data bitrate.
766 static int peak_usb_set_data_bittiming(struct net_device *netdev)
768 struct peak_usb_device *dev = netdev_priv(netdev);
769 const struct peak_usb_adapter *pa = dev->adapter;
771 if (pa->dev_set_data_bittiming) {
772 struct can_bittiming *bt = &dev->can.data_bittiming;
773 int err = pa->dev_set_data_bittiming(dev, bt);
777 "couldn't set data bitrate (err %d)\n",
786 static const struct net_device_ops peak_usb_netdev_ops = {
787 .ndo_open = peak_usb_ndo_open,
788 .ndo_stop = peak_usb_ndo_stop,
789 .ndo_start_xmit = peak_usb_ndo_start_xmit,
790 .ndo_change_mtu = can_change_mtu,
794 * create one device which is attached to CAN controller #ctrl_idx of the
797 static int peak_usb_create_dev(const struct peak_usb_adapter *peak_usb_adapter,
798 struct usb_interface *intf, int ctrl_idx)
800 struct usb_device *usb_dev = interface_to_usbdev(intf);
801 int sizeof_candev = peak_usb_adapter->sizeof_dev_private;
802 struct peak_usb_device *dev;
803 struct net_device *netdev;
807 if (sizeof_candev < sizeof(struct peak_usb_device))
808 sizeof_candev = sizeof(struct peak_usb_device);
810 netdev = alloc_candev(sizeof_candev, PCAN_USB_MAX_TX_URBS);
812 dev_err(&intf->dev, "%s: couldn't alloc candev\n",
813 PCAN_USB_DRIVER_NAME);
817 dev = netdev_priv(netdev);
819 /* allocate a buffer large enough to send commands */
820 dev->cmd_buf = kzalloc(PCAN_USB_MAX_CMD_LEN, GFP_KERNEL);
823 goto lbl_free_candev;
827 dev->netdev = netdev;
828 dev->adapter = peak_usb_adapter;
829 dev->ctrl_idx = ctrl_idx;
830 dev->state = PCAN_USB_STATE_CONNECTED;
832 dev->ep_msg_in = peak_usb_adapter->ep_msg_in;
833 dev->ep_msg_out = peak_usb_adapter->ep_msg_out[ctrl_idx];
835 dev->can.clock = peak_usb_adapter->clock;
836 dev->can.bittiming_const = peak_usb_adapter->bittiming_const;
837 dev->can.do_set_bittiming = peak_usb_set_bittiming;
838 dev->can.data_bittiming_const = peak_usb_adapter->data_bittiming_const;
839 dev->can.do_set_data_bittiming = peak_usb_set_data_bittiming;
840 dev->can.do_set_mode = peak_usb_set_mode;
841 dev->can.do_get_berr_counter = peak_usb_adapter->do_get_berr_counter;
842 dev->can.ctrlmode_supported = peak_usb_adapter->ctrlmode_supported;
844 netdev->netdev_ops = &peak_usb_netdev_ops;
846 netdev->flags |= IFF_ECHO; /* we support local echo */
848 init_usb_anchor(&dev->rx_submitted);
850 init_usb_anchor(&dev->tx_submitted);
851 atomic_set(&dev->active_tx_urbs, 0);
853 for (i = 0; i < PCAN_USB_MAX_TX_URBS; i++)
854 dev->tx_contexts[i].echo_index = PCAN_USB_MAX_TX_URBS;
856 dev->prev_siblings = usb_get_intfdata(intf);
857 usb_set_intfdata(intf, dev);
859 SET_NETDEV_DEV(netdev, &intf->dev);
860 netdev->dev_id = ctrl_idx;
862 err = register_candev(netdev);
864 dev_err(&intf->dev, "couldn't register CAN device: %d\n", err);
865 goto lbl_restore_intf_data;
868 if (dev->prev_siblings)
869 (dev->prev_siblings)->next_siblings = dev;
871 /* keep hw revision into the netdevice */
872 tmp16 = le16_to_cpu(usb_dev->descriptor.bcdDevice);
873 dev->device_rev = tmp16 >> 8;
875 if (dev->adapter->dev_init) {
876 err = dev->adapter->dev_init(dev);
878 goto lbl_unregister_candev;
882 if (dev->adapter->dev_set_bus) {
883 err = dev->adapter->dev_set_bus(dev, 0);
888 /* get device number early */
889 if (dev->adapter->dev_get_device_id)
890 dev->adapter->dev_get_device_id(dev, &dev->device_number);
892 netdev_info(netdev, "attached to %s channel %u (device %u)\n",
893 peak_usb_adapter->name, ctrl_idx, dev->device_number);
898 if (dev->adapter->dev_free)
899 dev->adapter->dev_free(dev);
901 lbl_unregister_candev:
902 unregister_candev(netdev);
904 lbl_restore_intf_data:
905 usb_set_intfdata(intf, dev->prev_siblings);
915 * called by the usb core when the device is unplugged from the system
917 static void peak_usb_disconnect(struct usb_interface *intf)
919 struct peak_usb_device *dev;
920 struct peak_usb_device *dev_prev_siblings;
922 /* unregister as many netdev devices as siblings */
923 for (dev = usb_get_intfdata(intf); dev; dev = dev_prev_siblings) {
924 struct net_device *netdev = dev->netdev;
927 dev_prev_siblings = dev->prev_siblings;
928 dev->state &= ~PCAN_USB_STATE_CONNECTED;
929 strlcpy(name, netdev->name, IFNAMSIZ);
931 unregister_netdev(netdev);
934 dev->next_siblings = NULL;
935 if (dev->adapter->dev_free)
936 dev->adapter->dev_free(dev);
939 dev_info(&intf->dev, "%s removed\n", name);
942 usb_set_intfdata(intf, NULL);
946 * probe function for new PEAK-System devices
948 static int peak_usb_probe(struct usb_interface *intf,
949 const struct usb_device_id *id)
951 struct usb_device *usb_dev = interface_to_usbdev(intf);
952 const u16 usb_id_product = le16_to_cpu(usb_dev->descriptor.idProduct);
953 const struct peak_usb_adapter *peak_usb_adapter = NULL;
954 int i, err = -ENOMEM;
956 usb_dev = interface_to_usbdev(intf);
958 /* get corresponding PCAN-USB adapter */
959 for (i = 0; i < ARRAY_SIZE(peak_usb_adapters_list); i++)
960 if (peak_usb_adapters_list[i]->device_id == usb_id_product) {
961 peak_usb_adapter = peak_usb_adapters_list[i];
965 if (!peak_usb_adapter) {
966 /* should never come except device_id bad usage in this file */
967 pr_err("%s: didn't find device id. 0x%x in devices list\n",
968 PCAN_USB_DRIVER_NAME, usb_dev->descriptor.idProduct);
972 /* got corresponding adapter: check if it handles current interface */
973 if (peak_usb_adapter->intf_probe) {
974 err = peak_usb_adapter->intf_probe(intf);
979 for (i = 0; i < peak_usb_adapter->ctrl_count; i++) {
980 err = peak_usb_create_dev(peak_usb_adapter, intf, i);
982 /* deregister already created devices */
983 peak_usb_disconnect(intf);
991 /* usb specific object needed to register this driver with the usb subsystem */
992 static struct usb_driver peak_usb_driver = {
993 .name = PCAN_USB_DRIVER_NAME,
994 .disconnect = peak_usb_disconnect,
995 .probe = peak_usb_probe,
996 .id_table = peak_usb_table,
999 static int __init peak_usb_init(void)
1003 /* register this driver with the USB subsystem */
1004 err = usb_register(&peak_usb_driver);
1006 pr_err("%s: usb_register failed (err %d)\n",
1007 PCAN_USB_DRIVER_NAME, err);
1012 static int peak_usb_do_device_exit(struct device *d, void *arg)
1014 struct usb_interface *intf = to_usb_interface(d);
1015 struct peak_usb_device *dev;
1017 /* stop as many netdev devices as siblings */
1018 for (dev = usb_get_intfdata(intf); dev; dev = dev->prev_siblings) {
1019 struct net_device *netdev = dev->netdev;
1021 if (netif_device_present(netdev))
1022 if (dev->adapter->dev_exit)
1023 dev->adapter->dev_exit(dev);
1029 static void __exit peak_usb_exit(void)
1033 /* last chance do send any synchronous commands here */
1034 err = driver_for_each_device(&peak_usb_driver.drvwrap.driver, NULL,
1035 NULL, peak_usb_do_device_exit);
1037 pr_err("%s: failed to stop all can devices (err %d)\n",
1038 PCAN_USB_DRIVER_NAME, err);
1040 /* deregister this driver with the USB subsystem */
1041 usb_deregister(&peak_usb_driver);
1043 pr_info("%s: PCAN-USB interfaces driver unloaded\n",
1044 PCAN_USB_DRIVER_NAME);
1047 module_init(peak_usb_init);
1048 module_exit(peak_usb_exit);