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) {
154 delta_us
= ts
- time_ref
->ts_dev_2
;
155 if (ts
< time_ref
->ts_dev_2
)
156 delta_us
&= (1 << time_ref
->adapter
->ts_used_bits
) - 1;
158 delta_us
+= time_ref
->ts_total
;
160 delta_us
*= time_ref
->adapter
->us_per_ts_scale
;
161 delta_us
>>= time_ref
->adapter
->us_per_ts_shift
;
163 *tv
= time_ref
->tv_host_0
;
164 peak_usb_add_us(tv
, (u32
)delta_us
);
166 *tv
= ktime_to_timeval(ktime_get());
171 * post received skb after having set any hw timestamp
173 int peak_usb_netif_rx(struct sk_buff
*skb
,
174 struct peak_time_ref
*time_ref
, u32 ts_low
, u32 ts_high
)
176 struct skb_shared_hwtstamps
*hwts
= skb_hwtstamps(skb
);
179 peak_usb_get_ts_tv(time_ref
, ts_low
, &tv
);
180 hwts
->hwtstamp
= timeval_to_ktime(tv
);
182 return netif_rx(skb
);
186 * callback for bulk Rx urb
188 static void peak_usb_read_bulk_callback(struct urb
*urb
)
190 struct peak_usb_device
*dev
= urb
->context
;
191 struct net_device
*netdev
;
194 netdev
= dev
->netdev
;
196 if (!netif_device_present(netdev
))
199 /* check reception status */
200 switch (urb
->status
) {
214 "Rx urb aborted (%d)\n", urb
->status
);
218 /* protect from any incoming empty msgs */
219 if ((urb
->actual_length
> 0) && (dev
->adapter
->dev_decode_buf
)) {
220 /* handle these kinds of msgs only if _start callback called */
221 if (dev
->state
& PCAN_USB_STATE_STARTED
) {
222 err
= dev
->adapter
->dev_decode_buf(dev
, urb
);
224 pcan_dump_mem("received usb message",
225 urb
->transfer_buffer
,
226 urb
->transfer_buffer_length
);
231 usb_fill_bulk_urb(urb
, dev
->udev
,
232 usb_rcvbulkpipe(dev
->udev
, dev
->ep_msg_in
),
233 urb
->transfer_buffer
, dev
->adapter
->rx_buffer_size
,
234 peak_usb_read_bulk_callback
, dev
);
236 usb_anchor_urb(urb
, &dev
->rx_submitted
);
237 err
= usb_submit_urb(urb
, GFP_ATOMIC
);
241 usb_unanchor_urb(urb
);
244 netif_device_detach(netdev
);
246 netdev_err(netdev
, "failed resubmitting read bulk urb: %d\n",
251 * callback for bulk Tx urb
253 static void peak_usb_write_bulk_callback(struct urb
*urb
)
255 struct peak_tx_urb_context
*context
= urb
->context
;
256 struct peak_usb_device
*dev
;
257 struct net_device
*netdev
;
262 netdev
= dev
->netdev
;
264 atomic_dec(&dev
->active_tx_urbs
);
266 if (!netif_device_present(netdev
))
269 /* check tx status */
270 switch (urb
->status
) {
272 /* transmission complete */
273 netdev
->stats
.tx_packets
++;
274 netdev
->stats
.tx_bytes
+= context
->data_len
;
276 /* prevent tx timeout */
277 netif_trans_update(netdev
);
282 netdev_err(netdev
, "Tx urb aborted (%d)\n",
292 /* should always release echo skb and corresponding context */
293 can_get_echo_skb(netdev
, context
->echo_index
);
294 context
->echo_index
= PCAN_USB_MAX_TX_URBS
;
296 /* do wakeup tx queue in case of success only */
298 netif_wake_queue(netdev
);
302 * called by netdev to send one skb on the CAN interface.
304 static netdev_tx_t
peak_usb_ndo_start_xmit(struct sk_buff
*skb
,
305 struct net_device
*netdev
)
307 struct peak_usb_device
*dev
= netdev_priv(netdev
);
308 struct peak_tx_urb_context
*context
= NULL
;
309 struct net_device_stats
*stats
= &netdev
->stats
;
310 struct canfd_frame
*cfd
= (struct canfd_frame
*)skb
->data
;
314 size_t size
= dev
->adapter
->tx_buffer_size
;
316 if (can_dropped_invalid_skb(netdev
, skb
))
319 for (i
= 0; i
< PCAN_USB_MAX_TX_URBS
; i
++)
320 if (dev
->tx_contexts
[i
].echo_index
== PCAN_USB_MAX_TX_URBS
) {
321 context
= dev
->tx_contexts
+ i
;
326 /* should not occur except during restart */
327 return NETDEV_TX_BUSY
;
331 obuf
= urb
->transfer_buffer
;
333 err
= dev
->adapter
->dev_encode_msg(dev
, skb
, obuf
, &size
);
336 netdev_err(netdev
, "packet dropped\n");
342 context
->echo_index
= i
;
344 /* Note: this works with CANFD frames too */
345 context
->data_len
= cfd
->len
;
347 usb_anchor_urb(urb
, &dev
->tx_submitted
);
349 can_put_echo_skb(skb
, netdev
, context
->echo_index
);
351 atomic_inc(&dev
->active_tx_urbs
);
353 err
= usb_submit_urb(urb
, GFP_ATOMIC
);
355 can_free_echo_skb(netdev
, context
->echo_index
);
357 usb_unanchor_urb(urb
);
359 /* this context is not used in fact */
360 context
->echo_index
= PCAN_USB_MAX_TX_URBS
;
362 atomic_dec(&dev
->active_tx_urbs
);
366 netif_device_detach(netdev
);
369 netdev_warn(netdev
, "tx urb submitting failed err=%d\n",
372 /* cable unplugged */
376 netif_trans_update(netdev
);
378 /* slow down tx path */
379 if (atomic_read(&dev
->active_tx_urbs
) >= PCAN_USB_MAX_TX_URBS
)
380 netif_stop_queue(netdev
);
387 * start the CAN interface.
388 * Rx and Tx urbs are allocated here. Rx urbs are submitted here.
390 static int peak_usb_start(struct peak_usb_device
*dev
)
392 struct net_device
*netdev
= dev
->netdev
;
395 for (i
= 0; i
< PCAN_USB_MAX_RX_URBS
; i
++) {
399 /* create a URB, and a buffer for it, to receive usb messages */
400 urb
= usb_alloc_urb(0, GFP_KERNEL
);
406 buf
= kmalloc(dev
->adapter
->rx_buffer_size
, GFP_KERNEL
);
413 usb_fill_bulk_urb(urb
, dev
->udev
,
414 usb_rcvbulkpipe(dev
->udev
, dev
->ep_msg_in
),
415 buf
, dev
->adapter
->rx_buffer_size
,
416 peak_usb_read_bulk_callback
, dev
);
418 /* ask last usb_free_urb() to also kfree() transfer_buffer */
419 urb
->transfer_flags
|= URB_FREE_BUFFER
;
420 usb_anchor_urb(urb
, &dev
->rx_submitted
);
422 err
= usb_submit_urb(urb
, GFP_KERNEL
);
425 netif_device_detach(dev
->netdev
);
427 usb_unanchor_urb(urb
);
433 /* drop reference, USB core will take care of freeing it */
437 /* did we submit any URBs? Warn if we was not able to submit all urbs */
438 if (i
< PCAN_USB_MAX_RX_URBS
) {
440 netdev_err(netdev
, "couldn't setup any rx URB\n");
444 netdev_warn(netdev
, "rx performance may be slow\n");
447 /* pre-alloc tx buffers and corresponding urbs */
448 for (i
= 0; i
< PCAN_USB_MAX_TX_URBS
; i
++) {
449 struct peak_tx_urb_context
*context
;
453 /* create a URB and a buffer for it, to transmit usb messages */
454 urb
= usb_alloc_urb(0, GFP_KERNEL
);
460 buf
= kmalloc(dev
->adapter
->tx_buffer_size
, GFP_KERNEL
);
467 context
= dev
->tx_contexts
+ i
;
471 usb_fill_bulk_urb(urb
, dev
->udev
,
472 usb_sndbulkpipe(dev
->udev
, dev
->ep_msg_out
),
473 buf
, dev
->adapter
->tx_buffer_size
,
474 peak_usb_write_bulk_callback
, context
);
476 /* ask last usb_free_urb() to also kfree() transfer_buffer */
477 urb
->transfer_flags
|= URB_FREE_BUFFER
;
480 /* warn if we were not able to allocate enough tx contexts */
481 if (i
< PCAN_USB_MAX_TX_URBS
) {
483 netdev_err(netdev
, "couldn't setup any tx URB\n");
487 netdev_warn(netdev
, "tx performance may be slow\n");
490 if (dev
->adapter
->dev_start
) {
491 err
= dev
->adapter
->dev_start(dev
);
496 dev
->state
|= PCAN_USB_STATE_STARTED
;
498 /* can set bus on now */
499 if (dev
->adapter
->dev_set_bus
) {
500 err
= dev
->adapter
->dev_set_bus(dev
, 1);
505 dev
->can
.state
= CAN_STATE_ERROR_ACTIVE
;
511 netif_device_detach(dev
->netdev
);
513 netdev_warn(netdev
, "couldn't submit control: %d\n", err
);
515 for (i
= 0; i
< PCAN_USB_MAX_TX_URBS
; i
++) {
516 usb_free_urb(dev
->tx_contexts
[i
].urb
);
517 dev
->tx_contexts
[i
].urb
= NULL
;
520 usb_kill_anchored_urbs(&dev
->rx_submitted
);
526 * called by netdev to open the corresponding CAN interface.
528 static int peak_usb_ndo_open(struct net_device
*netdev
)
530 struct peak_usb_device
*dev
= netdev_priv(netdev
);
534 err
= open_candev(netdev
);
538 /* finally start device */
539 err
= peak_usb_start(dev
);
541 netdev_err(netdev
, "couldn't start device: %d\n", err
);
542 close_candev(netdev
);
546 netif_start_queue(netdev
);
552 * unlink in-flight Rx and Tx urbs and free their memory.
554 static void peak_usb_unlink_all_urbs(struct peak_usb_device
*dev
)
558 /* free all Rx (submitted) urbs */
559 usb_kill_anchored_urbs(&dev
->rx_submitted
);
561 /* free unsubmitted Tx urbs first */
562 for (i
= 0; i
< PCAN_USB_MAX_TX_URBS
; i
++) {
563 struct urb
*urb
= dev
->tx_contexts
[i
].urb
;
566 dev
->tx_contexts
[i
].echo_index
!= PCAN_USB_MAX_TX_URBS
) {
568 * this urb is already released or always submitted,
569 * let usb core free by itself
575 dev
->tx_contexts
[i
].urb
= NULL
;
578 /* then free all submitted Tx urbs */
579 usb_kill_anchored_urbs(&dev
->tx_submitted
);
580 atomic_set(&dev
->active_tx_urbs
, 0);
584 * called by netdev to close the corresponding CAN interface.
586 static int peak_usb_ndo_stop(struct net_device
*netdev
)
588 struct peak_usb_device
*dev
= netdev_priv(netdev
);
590 dev
->state
&= ~PCAN_USB_STATE_STARTED
;
591 netif_stop_queue(netdev
);
593 /* unlink all pending urbs and free used memory */
594 peak_usb_unlink_all_urbs(dev
);
596 if (dev
->adapter
->dev_stop
)
597 dev
->adapter
->dev_stop(dev
);
599 close_candev(netdev
);
601 dev
->can
.state
= CAN_STATE_STOPPED
;
603 /* can set bus off now */
604 if (dev
->adapter
->dev_set_bus
) {
605 int err
= dev
->adapter
->dev_set_bus(dev
, 0);
614 * handle end of waiting for the device to reset
616 void peak_usb_restart_complete(struct peak_usb_device
*dev
)
618 /* finally MUST update can state */
619 dev
->can
.state
= CAN_STATE_ERROR_ACTIVE
;
621 /* netdev queue can be awaken now */
622 netif_wake_queue(dev
->netdev
);
625 void peak_usb_async_complete(struct urb
*urb
)
627 kfree(urb
->transfer_buffer
);
632 * device (auto-)restart mechanism runs in a timer context =>
633 * MUST handle restart with asynchronous usb transfers
635 static int peak_usb_restart(struct peak_usb_device
*dev
)
642 * if device doesn't define any asynchronous restart handler, simply
643 * wake the netdev queue up
645 if (!dev
->adapter
->dev_restart_async
) {
646 peak_usb_restart_complete(dev
);
650 /* first allocate a urb to handle the asynchronous steps */
651 urb
= usb_alloc_urb(0, GFP_ATOMIC
);
655 /* also allocate enough space for the commands to send */
656 buf
= kmalloc(PCAN_USB_MAX_CMD_LEN
, GFP_ATOMIC
);
662 /* call the device specific handler for the restart */
663 err
= dev
->adapter
->dev_restart_async(dev
, urb
, buf
);
674 * candev callback used to change CAN mode.
675 * Warning: this is called from a timer context!
677 static int peak_usb_set_mode(struct net_device
*netdev
, enum can_mode mode
)
679 struct peak_usb_device
*dev
= netdev_priv(netdev
);
684 err
= peak_usb_restart(dev
);
686 netdev_err(netdev
, "couldn't start device (err %d)\n",
698 * candev callback used to set device nominal/arbitration bitrate.
700 static int peak_usb_set_bittiming(struct net_device
*netdev
)
702 struct peak_usb_device
*dev
= netdev_priv(netdev
);
703 const struct peak_usb_adapter
*pa
= dev
->adapter
;
705 if (pa
->dev_set_bittiming
) {
706 struct can_bittiming
*bt
= &dev
->can
.bittiming
;
707 int err
= pa
->dev_set_bittiming(dev
, bt
);
710 netdev_info(netdev
, "couldn't set bitrate (err %d)\n",
719 * candev callback used to set device data bitrate.
721 static int peak_usb_set_data_bittiming(struct net_device
*netdev
)
723 struct peak_usb_device
*dev
= netdev_priv(netdev
);
724 const struct peak_usb_adapter
*pa
= dev
->adapter
;
726 if (pa
->dev_set_data_bittiming
) {
727 struct can_bittiming
*bt
= &dev
->can
.data_bittiming
;
728 int err
= pa
->dev_set_data_bittiming(dev
, bt
);
732 "couldn't set data bitrate (err %d)\n",
741 static const struct net_device_ops peak_usb_netdev_ops
= {
742 .ndo_open
= peak_usb_ndo_open
,
743 .ndo_stop
= peak_usb_ndo_stop
,
744 .ndo_start_xmit
= peak_usb_ndo_start_xmit
,
745 .ndo_change_mtu
= can_change_mtu
,
749 * create one device which is attached to CAN controller #ctrl_idx of the
752 static int peak_usb_create_dev(const struct peak_usb_adapter
*peak_usb_adapter
,
753 struct usb_interface
*intf
, int ctrl_idx
)
755 struct usb_device
*usb_dev
= interface_to_usbdev(intf
);
756 int sizeof_candev
= peak_usb_adapter
->sizeof_dev_private
;
757 struct peak_usb_device
*dev
;
758 struct net_device
*netdev
;
762 if (sizeof_candev
< sizeof(struct peak_usb_device
))
763 sizeof_candev
= sizeof(struct peak_usb_device
);
765 netdev
= alloc_candev(sizeof_candev
, PCAN_USB_MAX_TX_URBS
);
767 dev_err(&intf
->dev
, "%s: couldn't alloc candev\n",
768 PCAN_USB_DRIVER_NAME
);
772 dev
= netdev_priv(netdev
);
774 /* allocate a buffer large enough to send commands */
775 dev
->cmd_buf
= kmalloc(PCAN_USB_MAX_CMD_LEN
, GFP_KERNEL
);
778 goto lbl_free_candev
;
782 dev
->netdev
= netdev
;
783 dev
->adapter
= peak_usb_adapter
;
784 dev
->ctrl_idx
= ctrl_idx
;
785 dev
->state
= PCAN_USB_STATE_CONNECTED
;
787 dev
->ep_msg_in
= peak_usb_adapter
->ep_msg_in
;
788 dev
->ep_msg_out
= peak_usb_adapter
->ep_msg_out
[ctrl_idx
];
790 dev
->can
.clock
= peak_usb_adapter
->clock
;
791 dev
->can
.bittiming_const
= peak_usb_adapter
->bittiming_const
;
792 dev
->can
.do_set_bittiming
= peak_usb_set_bittiming
;
793 dev
->can
.data_bittiming_const
= peak_usb_adapter
->data_bittiming_const
;
794 dev
->can
.do_set_data_bittiming
= peak_usb_set_data_bittiming
;
795 dev
->can
.do_set_mode
= peak_usb_set_mode
;
796 dev
->can
.do_get_berr_counter
= peak_usb_adapter
->do_get_berr_counter
;
797 dev
->can
.ctrlmode_supported
= peak_usb_adapter
->ctrlmode_supported
;
799 netdev
->netdev_ops
= &peak_usb_netdev_ops
;
801 netdev
->flags
|= IFF_ECHO
; /* we support local echo */
803 init_usb_anchor(&dev
->rx_submitted
);
805 init_usb_anchor(&dev
->tx_submitted
);
806 atomic_set(&dev
->active_tx_urbs
, 0);
808 for (i
= 0; i
< PCAN_USB_MAX_TX_URBS
; i
++)
809 dev
->tx_contexts
[i
].echo_index
= PCAN_USB_MAX_TX_URBS
;
811 dev
->prev_siblings
= usb_get_intfdata(intf
);
812 usb_set_intfdata(intf
, dev
);
814 SET_NETDEV_DEV(netdev
, &intf
->dev
);
815 netdev
->dev_id
= ctrl_idx
;
817 err
= register_candev(netdev
);
819 dev_err(&intf
->dev
, "couldn't register CAN device: %d\n", err
);
820 goto lbl_restore_intf_data
;
823 if (dev
->prev_siblings
)
824 (dev
->prev_siblings
)->next_siblings
= dev
;
826 /* keep hw revision into the netdevice */
827 tmp16
= le16_to_cpu(usb_dev
->descriptor
.bcdDevice
);
828 dev
->device_rev
= tmp16
>> 8;
830 if (dev
->adapter
->dev_init
) {
831 err
= dev
->adapter
->dev_init(dev
);
833 goto lbl_unregister_candev
;
837 if (dev
->adapter
->dev_set_bus
) {
838 err
= dev
->adapter
->dev_set_bus(dev
, 0);
840 goto lbl_unregister_candev
;
843 /* get device number early */
844 if (dev
->adapter
->dev_get_device_id
)
845 dev
->adapter
->dev_get_device_id(dev
, &dev
->device_number
);
847 netdev_info(netdev
, "attached to %s channel %u (device %u)\n",
848 peak_usb_adapter
->name
, ctrl_idx
, dev
->device_number
);
852 lbl_unregister_candev
:
853 unregister_candev(netdev
);
855 lbl_restore_intf_data
:
856 usb_set_intfdata(intf
, dev
->prev_siblings
);
866 * called by the usb core when the device is unplugged from the system
868 static void peak_usb_disconnect(struct usb_interface
*intf
)
870 struct peak_usb_device
*dev
;
872 /* unregister as many netdev devices as siblings */
873 for (dev
= usb_get_intfdata(intf
); dev
; dev
= dev
->prev_siblings
) {
874 struct net_device
*netdev
= dev
->netdev
;
877 dev
->state
&= ~PCAN_USB_STATE_CONNECTED
;
878 strncpy(name
, netdev
->name
, IFNAMSIZ
);
880 unregister_netdev(netdev
);
884 dev
->next_siblings
= NULL
;
885 if (dev
->adapter
->dev_free
)
886 dev
->adapter
->dev_free(dev
);
888 dev_info(&intf
->dev
, "%s removed\n", name
);
891 usb_set_intfdata(intf
, NULL
);
895 * probe function for new PEAK-System devices
897 static int peak_usb_probe(struct usb_interface
*intf
,
898 const struct usb_device_id
*id
)
900 struct usb_device
*usb_dev
= interface_to_usbdev(intf
);
901 const u16 usb_id_product
= le16_to_cpu(usb_dev
->descriptor
.idProduct
);
902 const struct peak_usb_adapter
*peak_usb_adapter
= NULL
;
903 int i
, err
= -ENOMEM
;
905 usb_dev
= interface_to_usbdev(intf
);
907 /* get corresponding PCAN-USB adapter */
908 for (i
= 0; i
< ARRAY_SIZE(peak_usb_adapters_list
); i
++)
909 if (peak_usb_adapters_list
[i
]->device_id
== usb_id_product
) {
910 peak_usb_adapter
= peak_usb_adapters_list
[i
];
914 if (!peak_usb_adapter
) {
915 /* should never come except device_id bad usage in this file */
916 pr_err("%s: didn't find device id. 0x%x in devices list\n",
917 PCAN_USB_DRIVER_NAME
, usb_dev
->descriptor
.idProduct
);
921 /* got corresponding adapter: check if it handles current interface */
922 if (peak_usb_adapter
->intf_probe
) {
923 err
= peak_usb_adapter
->intf_probe(intf
);
928 for (i
= 0; i
< peak_usb_adapter
->ctrl_count
; i
++) {
929 err
= peak_usb_create_dev(peak_usb_adapter
, intf
, i
);
931 /* deregister already created devices */
932 peak_usb_disconnect(intf
);
940 /* usb specific object needed to register this driver with the usb subsystem */
941 static struct usb_driver peak_usb_driver
= {
942 .name
= PCAN_USB_DRIVER_NAME
,
943 .disconnect
= peak_usb_disconnect
,
944 .probe
= peak_usb_probe
,
945 .id_table
= peak_usb_table
,
948 static int __init
peak_usb_init(void)
952 /* register this driver with the USB subsystem */
953 err
= usb_register(&peak_usb_driver
);
955 pr_err("%s: usb_register failed (err %d)\n",
956 PCAN_USB_DRIVER_NAME
, err
);
961 static int peak_usb_do_device_exit(struct device
*d
, void *arg
)
963 struct usb_interface
*intf
= to_usb_interface(d
);
964 struct peak_usb_device
*dev
;
966 /* stop as many netdev devices as siblings */
967 for (dev
= usb_get_intfdata(intf
); dev
; dev
= dev
->prev_siblings
) {
968 struct net_device
*netdev
= dev
->netdev
;
970 if (netif_device_present(netdev
))
971 if (dev
->adapter
->dev_exit
)
972 dev
->adapter
->dev_exit(dev
);
978 static void __exit
peak_usb_exit(void)
982 /* last chance do send any synchronous commands here */
983 err
= driver_for_each_device(&peak_usb_driver
.drvwrap
.driver
, NULL
,
984 NULL
, peak_usb_do_device_exit
);
986 pr_err("%s: failed to stop all can devices (err %d)\n",
987 PCAN_USB_DRIVER_NAME
, err
);
989 /* deregister this driver with the USB subsystem */
990 usb_deregister(&peak_usb_driver
);
992 pr_info("%s: PCAN-USB interfaces driver unloaded\n",
993 PCAN_USB_DRIVER_NAME
);
996 module_init(peak_usb_init
);
997 module_exit(peak_usb_exit
);