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 {} /* Terminating entry */
43 MODULE_DEVICE_TABLE(usb
, peak_usb_table
);
45 /* List of supported PCAN-USB adapters (NULL terminated list) */
46 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
;
75 static void peak_usb_add_us(struct timeval
*tv
, u32 delta_us
)
77 /* number of s. to add to final time */
78 u32 delta_s
= delta_us
/ 1000000;
80 delta_us
-= delta_s
* 1000000;
82 tv
->tv_usec
+= delta_us
;
83 if (tv
->tv_usec
>= 1000000) {
84 tv
->tv_usec
-= 1000000;
87 tv
->tv_sec
+= delta_s
;
91 * sometimes, another now may be more recent than current one...
93 void peak_usb_update_ts_now(struct peak_time_ref
*time_ref
, u32 ts_now
)
95 time_ref
->ts_dev_2
= ts_now
;
97 /* should wait at least two passes before computing */
98 if (time_ref
->tv_host
.tv_sec
> 0) {
99 u32 delta_ts
= time_ref
->ts_dev_2
- time_ref
->ts_dev_1
;
101 if (time_ref
->ts_dev_2
< time_ref
->ts_dev_1
)
102 delta_ts
&= (1 << time_ref
->adapter
->ts_used_bits
) - 1;
104 time_ref
->ts_total
+= delta_ts
;
109 * register device timestamp as now
111 void peak_usb_set_ts_now(struct peak_time_ref
*time_ref
, u32 ts_now
)
113 if (time_ref
->tv_host_0
.tv_sec
== 0) {
114 /* use monotonic clock to correctly compute further deltas */
115 time_ref
->tv_host_0
= ktime_to_timeval(ktime_get());
116 time_ref
->tv_host
.tv_sec
= 0;
119 * delta_us should not be >= 2^32 => delta_s should be < 4294
120 * handle 32-bits wrapping here: if count of s. reaches 4200,
121 * reset counters and change time base
123 if (time_ref
->tv_host
.tv_sec
!= 0) {
124 u32 delta_s
= time_ref
->tv_host
.tv_sec
125 - time_ref
->tv_host_0
.tv_sec
;
126 if (delta_s
> 4200) {
127 time_ref
->tv_host_0
= time_ref
->tv_host
;
128 time_ref
->ts_total
= 0;
132 time_ref
->tv_host
= ktime_to_timeval(ktime_get());
133 time_ref
->tick_count
++;
136 time_ref
->ts_dev_1
= time_ref
->ts_dev_2
;
137 peak_usb_update_ts_now(time_ref
, ts_now
);
141 * compute timeval according to current ts and time_ref data
143 void peak_usb_get_ts_tv(struct peak_time_ref
*time_ref
, u32 ts
,
146 /* protect from getting timeval before setting now */
147 if (time_ref
->tv_host
.tv_sec
> 0) {
150 delta_us
= ts
- time_ref
->ts_dev_2
;
151 if (ts
< time_ref
->ts_dev_2
)
152 delta_us
&= (1 << time_ref
->adapter
->ts_used_bits
) - 1;
154 delta_us
+= time_ref
->ts_total
;
156 delta_us
*= time_ref
->adapter
->us_per_ts_scale
;
157 delta_us
>>= time_ref
->adapter
->us_per_ts_shift
;
159 *tv
= time_ref
->tv_host_0
;
160 peak_usb_add_us(tv
, (u32
)delta_us
);
162 *tv
= ktime_to_timeval(ktime_get());
167 * callback for bulk Rx urb
169 static void peak_usb_read_bulk_callback(struct urb
*urb
)
171 struct peak_usb_device
*dev
= urb
->context
;
172 struct net_device
*netdev
;
175 netdev
= dev
->netdev
;
177 if (!netif_device_present(netdev
))
180 /* check reception status */
181 switch (urb
->status
) {
195 "Rx urb aborted (%d)\n", urb
->status
);
199 /* protect from any incoming empty msgs */
200 if ((urb
->actual_length
> 0) && (dev
->adapter
->dev_decode_buf
)) {
201 /* handle these kinds of msgs only if _start callback called */
202 if (dev
->state
& PCAN_USB_STATE_STARTED
) {
203 err
= dev
->adapter
->dev_decode_buf(dev
, urb
);
205 pcan_dump_mem("received usb message",
206 urb
->transfer_buffer
,
207 urb
->transfer_buffer_length
);
212 usb_fill_bulk_urb(urb
, dev
->udev
,
213 usb_rcvbulkpipe(dev
->udev
, dev
->ep_msg_in
),
214 urb
->transfer_buffer
, dev
->adapter
->rx_buffer_size
,
215 peak_usb_read_bulk_callback
, dev
);
217 usb_anchor_urb(urb
, &dev
->rx_submitted
);
218 err
= usb_submit_urb(urb
, GFP_ATOMIC
);
222 usb_unanchor_urb(urb
);
225 netif_device_detach(netdev
);
227 netdev_err(netdev
, "failed resubmitting read bulk urb: %d\n",
232 * callback for bulk Tx urb
234 static void peak_usb_write_bulk_callback(struct urb
*urb
)
236 struct peak_tx_urb_context
*context
= urb
->context
;
237 struct peak_usb_device
*dev
;
238 struct net_device
*netdev
;
243 netdev
= dev
->netdev
;
245 atomic_dec(&dev
->active_tx_urbs
);
247 if (!netif_device_present(netdev
))
250 /* check tx status */
251 switch (urb
->status
) {
253 /* transmission complete */
254 netdev
->stats
.tx_packets
++;
255 netdev
->stats
.tx_bytes
+= context
->dlc
;
257 /* prevent tx timeout */
258 netdev
->trans_start
= jiffies
;
263 netdev_err(netdev
, "Tx urb aborted (%d)\n",
273 /* should always release echo skb and corresponding context */
274 can_get_echo_skb(netdev
, context
->echo_index
);
275 context
->echo_index
= PCAN_USB_MAX_TX_URBS
;
277 /* do wakeup tx queue in case of success only */
279 netif_wake_queue(netdev
);
283 * called by netdev to send one skb on the CAN interface.
285 static netdev_tx_t
peak_usb_ndo_start_xmit(struct sk_buff
*skb
,
286 struct net_device
*netdev
)
288 struct peak_usb_device
*dev
= netdev_priv(netdev
);
289 struct peak_tx_urb_context
*context
= NULL
;
290 struct net_device_stats
*stats
= &netdev
->stats
;
291 struct can_frame
*cf
= (struct can_frame
*)skb
->data
;
295 size_t size
= dev
->adapter
->tx_buffer_size
;
297 if (can_dropped_invalid_skb(netdev
, skb
))
300 for (i
= 0; i
< PCAN_USB_MAX_TX_URBS
; i
++)
301 if (dev
->tx_contexts
[i
].echo_index
== PCAN_USB_MAX_TX_URBS
) {
302 context
= dev
->tx_contexts
+ i
;
307 /* should not occur except during restart */
308 return NETDEV_TX_BUSY
;
312 obuf
= urb
->transfer_buffer
;
314 err
= dev
->adapter
->dev_encode_msg(dev
, skb
, obuf
, &size
);
317 netdev_err(netdev
, "packet dropped\n");
323 context
->echo_index
= i
;
324 context
->dlc
= cf
->can_dlc
;
326 usb_anchor_urb(urb
, &dev
->tx_submitted
);
328 can_put_echo_skb(skb
, netdev
, context
->echo_index
);
330 atomic_inc(&dev
->active_tx_urbs
);
332 err
= usb_submit_urb(urb
, GFP_ATOMIC
);
334 can_free_echo_skb(netdev
, context
->echo_index
);
336 usb_unanchor_urb(urb
);
338 /* this context is not used in fact */
339 context
->echo_index
= PCAN_USB_MAX_TX_URBS
;
341 atomic_dec(&dev
->active_tx_urbs
);
345 netif_device_detach(netdev
);
348 netdev_warn(netdev
, "tx urb submitting failed err=%d\n",
351 /* cable unplugged */
355 netdev
->trans_start
= jiffies
;
357 /* slow down tx path */
358 if (atomic_read(&dev
->active_tx_urbs
) >= PCAN_USB_MAX_TX_URBS
)
359 netif_stop_queue(netdev
);
366 * start the CAN interface.
367 * Rx and Tx urbs are allocated here. Rx urbs are submitted here.
369 static int peak_usb_start(struct peak_usb_device
*dev
)
371 struct net_device
*netdev
= dev
->netdev
;
374 for (i
= 0; i
< PCAN_USB_MAX_RX_URBS
; i
++) {
378 /* create a URB, and a buffer for it, to receive usb messages */
379 urb
= usb_alloc_urb(0, GFP_KERNEL
);
381 netdev_err(netdev
, "No memory left for URBs\n");
386 buf
= kmalloc(dev
->adapter
->rx_buffer_size
, GFP_KERNEL
);
393 usb_fill_bulk_urb(urb
, dev
->udev
,
394 usb_rcvbulkpipe(dev
->udev
, dev
->ep_msg_in
),
395 buf
, dev
->adapter
->rx_buffer_size
,
396 peak_usb_read_bulk_callback
, dev
);
398 /* ask last usb_free_urb() to also kfree() transfer_buffer */
399 urb
->transfer_flags
|= URB_FREE_BUFFER
;
400 usb_anchor_urb(urb
, &dev
->rx_submitted
);
402 err
= usb_submit_urb(urb
, GFP_KERNEL
);
405 netif_device_detach(dev
->netdev
);
407 usb_unanchor_urb(urb
);
413 /* drop reference, USB core will take care of freeing it */
417 /* did we submit any URBs? Warn if we was not able to submit all urbs */
418 if (i
< PCAN_USB_MAX_RX_URBS
) {
420 netdev_err(netdev
, "couldn't setup any rx URB\n");
424 netdev_warn(netdev
, "rx performance may be slow\n");
427 /* pre-alloc tx buffers and corresponding urbs */
428 for (i
= 0; i
< PCAN_USB_MAX_TX_URBS
; i
++) {
429 struct peak_tx_urb_context
*context
;
433 /* create a URB and a buffer for it, to transmit usb messages */
434 urb
= usb_alloc_urb(0, GFP_KERNEL
);
436 netdev_err(netdev
, "No memory left for URBs\n");
441 buf
= kmalloc(dev
->adapter
->tx_buffer_size
, GFP_KERNEL
);
448 context
= dev
->tx_contexts
+ i
;
452 usb_fill_bulk_urb(urb
, dev
->udev
,
453 usb_sndbulkpipe(dev
->udev
, dev
->ep_msg_out
),
454 buf
, dev
->adapter
->tx_buffer_size
,
455 peak_usb_write_bulk_callback
, context
);
457 /* ask last usb_free_urb() to also kfree() transfer_buffer */
458 urb
->transfer_flags
|= URB_FREE_BUFFER
;
461 /* warn if we were not able to allocate enough tx contexts */
462 if (i
< PCAN_USB_MAX_TX_URBS
) {
464 netdev_err(netdev
, "couldn't setup any tx URB\n");
468 netdev_warn(netdev
, "tx performance may be slow\n");
471 if (dev
->adapter
->dev_start
) {
472 err
= dev
->adapter
->dev_start(dev
);
477 dev
->state
|= PCAN_USB_STATE_STARTED
;
479 /* can set bus on now */
480 if (dev
->adapter
->dev_set_bus
) {
481 err
= dev
->adapter
->dev_set_bus(dev
, 1);
486 dev
->can
.state
= CAN_STATE_ERROR_ACTIVE
;
492 netif_device_detach(dev
->netdev
);
494 netdev_warn(netdev
, "couldn't submit control: %d\n", err
);
496 for (i
= 0; i
< PCAN_USB_MAX_TX_URBS
; i
++) {
497 usb_free_urb(dev
->tx_contexts
[i
].urb
);
498 dev
->tx_contexts
[i
].urb
= NULL
;
501 usb_kill_anchored_urbs(&dev
->rx_submitted
);
507 * called by netdev to open the corresponding CAN interface.
509 static int peak_usb_ndo_open(struct net_device
*netdev
)
511 struct peak_usb_device
*dev
= netdev_priv(netdev
);
515 err
= open_candev(netdev
);
519 /* finally start device */
520 err
= peak_usb_start(dev
);
522 netdev_err(netdev
, "couldn't start device: %d\n", err
);
523 close_candev(netdev
);
527 netif_start_queue(netdev
);
533 * unlink in-flight Rx and Tx urbs and free their memory.
535 static void peak_usb_unlink_all_urbs(struct peak_usb_device
*dev
)
539 /* free all Rx (submitted) urbs */
540 usb_kill_anchored_urbs(&dev
->rx_submitted
);
542 /* free unsubmitted Tx urbs first */
543 for (i
= 0; i
< PCAN_USB_MAX_TX_URBS
; i
++) {
544 struct urb
*urb
= dev
->tx_contexts
[i
].urb
;
547 dev
->tx_contexts
[i
].echo_index
!= PCAN_USB_MAX_TX_URBS
) {
549 * this urb is already released or always submitted,
550 * let usb core free by itself
556 dev
->tx_contexts
[i
].urb
= NULL
;
559 /* then free all submitted Tx urbs */
560 usb_kill_anchored_urbs(&dev
->tx_submitted
);
561 atomic_set(&dev
->active_tx_urbs
, 0);
565 * called by netdev to close the corresponding CAN interface.
567 static int peak_usb_ndo_stop(struct net_device
*netdev
)
569 struct peak_usb_device
*dev
= netdev_priv(netdev
);
571 dev
->state
&= ~PCAN_USB_STATE_STARTED
;
572 netif_stop_queue(netdev
);
574 /* unlink all pending urbs and free used memory */
575 peak_usb_unlink_all_urbs(dev
);
577 if (dev
->adapter
->dev_stop
)
578 dev
->adapter
->dev_stop(dev
);
580 close_candev(netdev
);
582 dev
->can
.state
= CAN_STATE_STOPPED
;
584 /* can set bus off now */
585 if (dev
->adapter
->dev_set_bus
) {
586 int err
= dev
->adapter
->dev_set_bus(dev
, 0);
595 * handle end of waiting for the device to reset
597 void peak_usb_restart_complete(struct peak_usb_device
*dev
)
599 /* finally MUST update can state */
600 dev
->can
.state
= CAN_STATE_ERROR_ACTIVE
;
602 /* netdev queue can be awaken now */
603 netif_wake_queue(dev
->netdev
);
606 void peak_usb_async_complete(struct urb
*urb
)
608 kfree(urb
->transfer_buffer
);
613 * device (auto-)restart mechanism runs in a timer context =>
614 * MUST handle restart with asynchronous usb transfers
616 static int peak_usb_restart(struct peak_usb_device
*dev
)
623 * if device doesn't define any asynchronous restart handler, simply
624 * wake the netdev queue up
626 if (!dev
->adapter
->dev_restart_async
) {
627 peak_usb_restart_complete(dev
);
631 /* first allocate a urb to handle the asynchronous steps */
632 urb
= usb_alloc_urb(0, GFP_ATOMIC
);
634 netdev_err(dev
->netdev
, "no memory left for urb\n");
638 /* also allocate enough space for the commands to send */
639 buf
= kmalloc(PCAN_USB_MAX_CMD_LEN
, GFP_ATOMIC
);
645 /* call the device specific handler for the restart */
646 err
= dev
->adapter
->dev_restart_async(dev
, urb
, buf
);
657 * candev callback used to change CAN mode.
658 * Warning: this is called from a timer context!
660 static int peak_usb_set_mode(struct net_device
*netdev
, enum can_mode mode
)
662 struct peak_usb_device
*dev
= netdev_priv(netdev
);
667 err
= peak_usb_restart(dev
);
669 netdev_err(netdev
, "couldn't start device (err %d)\n",
681 * candev callback used to set device bitrate.
683 static int peak_usb_set_bittiming(struct net_device
*netdev
)
685 struct peak_usb_device
*dev
= netdev_priv(netdev
);
686 struct can_bittiming
*bt
= &dev
->can
.bittiming
;
688 if (dev
->adapter
->dev_set_bittiming
) {
689 int err
= dev
->adapter
->dev_set_bittiming(dev
, bt
);
692 netdev_info(netdev
, "couldn't set bitrate (err %d)\n",
700 static const struct net_device_ops peak_usb_netdev_ops
= {
701 .ndo_open
= peak_usb_ndo_open
,
702 .ndo_stop
= peak_usb_ndo_stop
,
703 .ndo_start_xmit
= peak_usb_ndo_start_xmit
,
704 .ndo_change_mtu
= can_change_mtu
,
708 * create one device which is attached to CAN controller #ctrl_idx of the
711 static int peak_usb_create_dev(const struct peak_usb_adapter
*peak_usb_adapter
,
712 struct usb_interface
*intf
, int ctrl_idx
)
714 struct usb_device
*usb_dev
= interface_to_usbdev(intf
);
715 int sizeof_candev
= peak_usb_adapter
->sizeof_dev_private
;
716 struct peak_usb_device
*dev
;
717 struct net_device
*netdev
;
721 if (sizeof_candev
< sizeof(struct peak_usb_device
))
722 sizeof_candev
= sizeof(struct peak_usb_device
);
724 netdev
= alloc_candev(sizeof_candev
, PCAN_USB_MAX_TX_URBS
);
726 dev_err(&intf
->dev
, "%s: couldn't alloc candev\n",
727 PCAN_USB_DRIVER_NAME
);
731 dev
= netdev_priv(netdev
);
733 /* allocate a buffer large enough to send commands */
734 dev
->cmd_buf
= kmalloc(PCAN_USB_MAX_CMD_LEN
, GFP_KERNEL
);
737 goto lbl_free_candev
;
741 dev
->netdev
= netdev
;
742 dev
->adapter
= peak_usb_adapter
;
743 dev
->ctrl_idx
= ctrl_idx
;
744 dev
->state
= PCAN_USB_STATE_CONNECTED
;
746 dev
->ep_msg_in
= peak_usb_adapter
->ep_msg_in
;
747 dev
->ep_msg_out
= peak_usb_adapter
->ep_msg_out
[ctrl_idx
];
749 dev
->can
.clock
= peak_usb_adapter
->clock
;
750 dev
->can
.bittiming_const
= &peak_usb_adapter
->bittiming_const
;
751 dev
->can
.do_set_bittiming
= peak_usb_set_bittiming
;
752 dev
->can
.do_set_mode
= peak_usb_set_mode
;
753 dev
->can
.do_get_berr_counter
= peak_usb_adapter
->do_get_berr_counter
;
754 dev
->can
.ctrlmode_supported
= peak_usb_adapter
->ctrlmode_supported
;
756 netdev
->netdev_ops
= &peak_usb_netdev_ops
;
758 netdev
->flags
|= IFF_ECHO
; /* we support local echo */
760 init_usb_anchor(&dev
->rx_submitted
);
762 init_usb_anchor(&dev
->tx_submitted
);
763 atomic_set(&dev
->active_tx_urbs
, 0);
765 for (i
= 0; i
< PCAN_USB_MAX_TX_URBS
; i
++)
766 dev
->tx_contexts
[i
].echo_index
= PCAN_USB_MAX_TX_URBS
;
768 dev
->prev_siblings
= usb_get_intfdata(intf
);
769 usb_set_intfdata(intf
, dev
);
771 SET_NETDEV_DEV(netdev
, &intf
->dev
);
772 netdev
->dev_id
= ctrl_idx
;
774 err
= register_candev(netdev
);
776 dev_err(&intf
->dev
, "couldn't register CAN device: %d\n", err
);
777 goto lbl_restore_intf_data
;
780 if (dev
->prev_siblings
)
781 (dev
->prev_siblings
)->next_siblings
= dev
;
783 /* keep hw revision into the netdevice */
784 tmp16
= le16_to_cpu(usb_dev
->descriptor
.bcdDevice
);
785 dev
->device_rev
= tmp16
>> 8;
787 if (dev
->adapter
->dev_init
) {
788 err
= dev
->adapter
->dev_init(dev
);
790 goto lbl_unregister_candev
;
794 if (dev
->adapter
->dev_set_bus
) {
795 err
= dev
->adapter
->dev_set_bus(dev
, 0);
797 goto lbl_unregister_candev
;
800 /* get device number early */
801 if (dev
->adapter
->dev_get_device_id
)
802 dev
->adapter
->dev_get_device_id(dev
, &dev
->device_number
);
804 netdev_info(netdev
, "attached to %s channel %u (device %u)\n",
805 peak_usb_adapter
->name
, ctrl_idx
, dev
->device_number
);
809 lbl_unregister_candev
:
810 unregister_candev(netdev
);
812 lbl_restore_intf_data
:
813 usb_set_intfdata(intf
, dev
->prev_siblings
);
823 * called by the usb core when the device is unplugged from the system
825 static void peak_usb_disconnect(struct usb_interface
*intf
)
827 struct peak_usb_device
*dev
;
829 /* unregister as many netdev devices as siblings */
830 for (dev
= usb_get_intfdata(intf
); dev
; dev
= dev
->prev_siblings
) {
831 struct net_device
*netdev
= dev
->netdev
;
834 dev
->state
&= ~PCAN_USB_STATE_CONNECTED
;
835 strncpy(name
, netdev
->name
, IFNAMSIZ
);
837 unregister_netdev(netdev
);
841 dev
->next_siblings
= NULL
;
842 if (dev
->adapter
->dev_free
)
843 dev
->adapter
->dev_free(dev
);
845 dev_info(&intf
->dev
, "%s removed\n", name
);
848 usb_set_intfdata(intf
, NULL
);
852 * probe function for new PEAK-System devices
854 static int peak_usb_probe(struct usb_interface
*intf
,
855 const struct usb_device_id
*id
)
857 struct usb_device
*usb_dev
= interface_to_usbdev(intf
);
858 const u16 usb_id_product
= le16_to_cpu(usb_dev
->descriptor
.idProduct
);
859 const struct peak_usb_adapter
*peak_usb_adapter
= NULL
;
860 int i
, err
= -ENOMEM
;
862 usb_dev
= interface_to_usbdev(intf
);
864 /* get corresponding PCAN-USB adapter */
865 for (i
= 0; i
< ARRAY_SIZE(peak_usb_adapters_list
); i
++)
866 if (peak_usb_adapters_list
[i
]->device_id
== usb_id_product
) {
867 peak_usb_adapter
= peak_usb_adapters_list
[i
];
871 if (!peak_usb_adapter
) {
872 /* should never come except device_id bad usage in this file */
873 pr_err("%s: didn't find device id. 0x%x in devices list\n",
874 PCAN_USB_DRIVER_NAME
, usb_dev
->descriptor
.idProduct
);
878 /* got corresponding adapter: check if it handles current interface */
879 if (peak_usb_adapter
->intf_probe
) {
880 err
= peak_usb_adapter
->intf_probe(intf
);
885 for (i
= 0; i
< peak_usb_adapter
->ctrl_count
; i
++) {
886 err
= peak_usb_create_dev(peak_usb_adapter
, intf
, i
);
888 /* deregister already created devices */
889 peak_usb_disconnect(intf
);
897 /* usb specific object needed to register this driver with the usb subsystem */
898 static struct usb_driver peak_usb_driver
= {
899 .name
= PCAN_USB_DRIVER_NAME
,
900 .disconnect
= peak_usb_disconnect
,
901 .probe
= peak_usb_probe
,
902 .id_table
= peak_usb_table
,
905 static int __init
peak_usb_init(void)
909 /* register this driver with the USB subsystem */
910 err
= usb_register(&peak_usb_driver
);
912 pr_err("%s: usb_register failed (err %d)\n",
913 PCAN_USB_DRIVER_NAME
, err
);
918 static int peak_usb_do_device_exit(struct device
*d
, void *arg
)
920 struct usb_interface
*intf
= to_usb_interface(d
);
921 struct peak_usb_device
*dev
;
923 /* stop as many netdev devices as siblings */
924 for (dev
= usb_get_intfdata(intf
); dev
; dev
= dev
->prev_siblings
) {
925 struct net_device
*netdev
= dev
->netdev
;
927 if (netif_device_present(netdev
))
928 if (dev
->adapter
->dev_exit
)
929 dev
->adapter
->dev_exit(dev
);
935 static void __exit
peak_usb_exit(void)
939 /* last chance do send any synchronous commands here */
940 err
= driver_for_each_device(&peak_usb_driver
.drvwrap
.driver
, NULL
,
941 NULL
, peak_usb_do_device_exit
);
943 pr_err("%s: failed to stop all can devices (err %d)\n",
944 PCAN_USB_DRIVER_NAME
, err
);
946 /* deregister this driver with the USB subsystem */
947 usb_deregister(&peak_usb_driver
);
949 pr_info("%s: PCAN-USB interfaces driver unloaded\n",
950 PCAN_USB_DRIVER_NAME
);
953 module_init(peak_usb_init
);
954 module_exit(peak_usb_exit
);