Commit | Line | Data |
---|---|---|
bb478555 SG |
1 | /* |
2 | * CAN driver for PEAK System USB adapters | |
3 | * Derived from the PCAN project file driver/src/pcan_usb_core.c | |
4 | * | |
5 | * Copyright (C) 2003-2010 PEAK System-Technik GmbH | |
6 | * Copyright (C) 2010-2012 Stephane Grosjean <s.grosjean@peak-system.com> | |
7 | * | |
8 | * Many thanks to Klaus Hitschler <klaus.hitschler@gmx.de> | |
9 | * | |
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. | |
13 | * | |
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. | |
18 | */ | |
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> | |
25 | ||
26 | #include <linux/can.h> | |
27 | #include <linux/can/dev.h> | |
28 | #include <linux/can/error.h> | |
29 | ||
30 | #include "pcan_usb_core.h" | |
31 | ||
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"); | |
35 | ||
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)}, | |
0a25e1f4 SG |
40 | {USB_DEVICE(PCAN_USB_VENDOR_ID, PCAN_USBFD_PRODUCT_ID)}, |
41 | {USB_DEVICE(PCAN_USB_VENDOR_ID, PCAN_USBPROFD_PRODUCT_ID)}, | |
ea8b65b5 | 42 | {USB_DEVICE(PCAN_USB_VENDOR_ID, PCAN_USBCHIP_PRODUCT_ID)}, |
f00b534d | 43 | {USB_DEVICE(PCAN_USB_VENDOR_ID, PCAN_USBX6_PRODUCT_ID)}, |
bb478555 SG |
44 | {} /* Terminating entry */ |
45 | }; | |
46 | ||
47 | MODULE_DEVICE_TABLE(usb, peak_usb_table); | |
48 | ||
49 | /* List of supported PCAN-USB adapters (NULL terminated list) */ | |
d6b5f30d | 50 | static const struct peak_usb_adapter *const peak_usb_adapters_list[] = { |
bb478555 SG |
51 | &pcan_usb, |
52 | &pcan_usb_pro, | |
0a25e1f4 SG |
53 | &pcan_usb_fd, |
54 | &pcan_usb_pro_fd, | |
ea8b65b5 | 55 | &pcan_usb_chip, |
f00b534d | 56 | &pcan_usb_x6, |
bb478555 SG |
57 | }; |
58 | ||
59 | /* | |
60 | * dump memory | |
61 | */ | |
62 | #define DUMP_WIDTH 16 | |
56b9f301 | 63 | void pcan_dump_mem(char *prompt, void *p, int l) |
bb478555 SG |
64 | { |
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); | |
69 | } | |
70 | ||
71 | /* | |
72 | * initialize a time_ref object with usb adapter own settings | |
73 | */ | |
74 | void peak_usb_init_time_ref(struct peak_time_ref *time_ref, | |
d6b5f30d | 75 | const struct peak_usb_adapter *adapter) |
bb478555 SG |
76 | { |
77 | if (time_ref) { | |
78 | memset(time_ref, 0, sizeof(struct peak_time_ref)); | |
79 | time_ref->adapter = adapter; | |
80 | } | |
81 | } | |
82 | ||
83 | static void peak_usb_add_us(struct timeval *tv, u32 delta_us) | |
84 | { | |
85 | /* number of s. to add to final time */ | |
86 | u32 delta_s = delta_us / 1000000; | |
87 | ||
88 | delta_us -= delta_s * 1000000; | |
89 | ||
90 | tv->tv_usec += delta_us; | |
91 | if (tv->tv_usec >= 1000000) { | |
92 | tv->tv_usec -= 1000000; | |
93 | delta_s++; | |
94 | } | |
95 | tv->tv_sec += delta_s; | |
96 | } | |
97 | ||
98 | /* | |
99 | * sometimes, another now may be more recent than current one... | |
100 | */ | |
101 | void peak_usb_update_ts_now(struct peak_time_ref *time_ref, u32 ts_now) | |
102 | { | |
103 | time_ref->ts_dev_2 = ts_now; | |
104 | ||
105 | /* should wait at least two passes before computing */ | |
106 | if (time_ref->tv_host.tv_sec > 0) { | |
107 | u32 delta_ts = time_ref->ts_dev_2 - time_ref->ts_dev_1; | |
108 | ||
109 | if (time_ref->ts_dev_2 < time_ref->ts_dev_1) | |
110 | delta_ts &= (1 << time_ref->adapter->ts_used_bits) - 1; | |
111 | ||
112 | time_ref->ts_total += delta_ts; | |
113 | } | |
114 | } | |
115 | ||
116 | /* | |
117 | * register device timestamp as now | |
118 | */ | |
119 | void peak_usb_set_ts_now(struct peak_time_ref *time_ref, u32 ts_now) | |
120 | { | |
121 | if (time_ref->tv_host_0.tv_sec == 0) { | |
122 | /* use monotonic clock to correctly compute further deltas */ | |
123 | time_ref->tv_host_0 = ktime_to_timeval(ktime_get()); | |
124 | time_ref->tv_host.tv_sec = 0; | |
125 | } else { | |
126 | /* | |
127 | * delta_us should not be >= 2^32 => delta_s should be < 4294 | |
128 | * handle 32-bits wrapping here: if count of s. reaches 4200, | |
129 | * reset counters and change time base | |
130 | */ | |
131 | if (time_ref->tv_host.tv_sec != 0) { | |
132 | u32 delta_s = time_ref->tv_host.tv_sec | |
133 | - time_ref->tv_host_0.tv_sec; | |
134 | if (delta_s > 4200) { | |
135 | time_ref->tv_host_0 = time_ref->tv_host; | |
136 | time_ref->ts_total = 0; | |
137 | } | |
138 | } | |
139 | ||
140 | time_ref->tv_host = ktime_to_timeval(ktime_get()); | |
141 | time_ref->tick_count++; | |
142 | } | |
143 | ||
144 | time_ref->ts_dev_1 = time_ref->ts_dev_2; | |
145 | peak_usb_update_ts_now(time_ref, ts_now); | |
146 | } | |
147 | ||
148 | /* | |
149 | * compute timeval according to current ts and time_ref data | |
150 | */ | |
151 | void peak_usb_get_ts_tv(struct peak_time_ref *time_ref, u32 ts, | |
152 | struct timeval *tv) | |
153 | { | |
154 | /* protect from getting timeval before setting now */ | |
155 | if (time_ref->tv_host.tv_sec > 0) { | |
156 | u64 delta_us; | |
157 | ||
158 | delta_us = ts - time_ref->ts_dev_2; | |
159 | if (ts < time_ref->ts_dev_2) | |
160 | delta_us &= (1 << time_ref->adapter->ts_used_bits) - 1; | |
161 | ||
162 | delta_us += time_ref->ts_total; | |
163 | ||
164 | delta_us *= time_ref->adapter->us_per_ts_scale; | |
165 | delta_us >>= time_ref->adapter->us_per_ts_shift; | |
166 | ||
167 | *tv = time_ref->tv_host_0; | |
168 | peak_usb_add_us(tv, (u32)delta_us); | |
169 | } else { | |
170 | *tv = ktime_to_timeval(ktime_get()); | |
171 | } | |
172 | } | |
173 | ||
faa233d9 SG |
174 | /* |
175 | * post received skb after having set any hw timestamp | |
176 | */ | |
177 | int peak_usb_netif_rx(struct sk_buff *skb, | |
178 | struct peak_time_ref *time_ref, u32 ts_low, u32 ts_high) | |
179 | { | |
180 | struct skb_shared_hwtstamps *hwts = skb_hwtstamps(skb); | |
181 | struct timeval tv; | |
182 | ||
183 | peak_usb_get_ts_tv(time_ref, ts_low, &tv); | |
184 | hwts->hwtstamp = timeval_to_ktime(tv); | |
185 | ||
186 | return netif_rx(skb); | |
187 | } | |
188 | ||
bb478555 SG |
189 | /* |
190 | * callback for bulk Rx urb | |
191 | */ | |
192 | static void peak_usb_read_bulk_callback(struct urb *urb) | |
193 | { | |
194 | struct peak_usb_device *dev = urb->context; | |
195 | struct net_device *netdev; | |
196 | int err; | |
197 | ||
198 | netdev = dev->netdev; | |
199 | ||
200 | if (!netif_device_present(netdev)) | |
201 | return; | |
202 | ||
203 | /* check reception status */ | |
204 | switch (urb->status) { | |
205 | case 0: | |
206 | /* success */ | |
207 | break; | |
208 | ||
209 | case -EILSEQ: | |
210 | case -ENOENT: | |
211 | case -ECONNRESET: | |
212 | case -ESHUTDOWN: | |
213 | return; | |
214 | ||
215 | default: | |
216 | if (net_ratelimit()) | |
217 | netdev_err(netdev, | |
218 | "Rx urb aborted (%d)\n", urb->status); | |
219 | goto resubmit_urb; | |
220 | } | |
221 | ||
222 | /* protect from any incoming empty msgs */ | |
223 | if ((urb->actual_length > 0) && (dev->adapter->dev_decode_buf)) { | |
224 | /* handle these kinds of msgs only if _start callback called */ | |
225 | if (dev->state & PCAN_USB_STATE_STARTED) { | |
226 | err = dev->adapter->dev_decode_buf(dev, urb); | |
227 | if (err) | |
56b9f301 RD |
228 | pcan_dump_mem("received usb message", |
229 | urb->transfer_buffer, | |
230 | urb->transfer_buffer_length); | |
bb478555 SG |
231 | } |
232 | } | |
233 | ||
234 | resubmit_urb: | |
235 | usb_fill_bulk_urb(urb, dev->udev, | |
236 | usb_rcvbulkpipe(dev->udev, dev->ep_msg_in), | |
237 | urb->transfer_buffer, dev->adapter->rx_buffer_size, | |
238 | peak_usb_read_bulk_callback, dev); | |
239 | ||
240 | usb_anchor_urb(urb, &dev->rx_submitted); | |
241 | err = usb_submit_urb(urb, GFP_ATOMIC); | |
242 | if (!err) | |
243 | return; | |
244 | ||
245 | usb_unanchor_urb(urb); | |
246 | ||
247 | if (err == -ENODEV) | |
248 | netif_device_detach(netdev); | |
249 | else | |
250 | netdev_err(netdev, "failed resubmitting read bulk urb: %d\n", | |
251 | err); | |
252 | } | |
253 | ||
254 | /* | |
255 | * callback for bulk Tx urb | |
256 | */ | |
257 | static void peak_usb_write_bulk_callback(struct urb *urb) | |
258 | { | |
259 | struct peak_tx_urb_context *context = urb->context; | |
260 | struct peak_usb_device *dev; | |
261 | struct net_device *netdev; | |
262 | ||
263 | BUG_ON(!context); | |
264 | ||
265 | dev = context->dev; | |
266 | netdev = dev->netdev; | |
267 | ||
268 | atomic_dec(&dev->active_tx_urbs); | |
269 | ||
270 | if (!netif_device_present(netdev)) | |
271 | return; | |
272 | ||
273 | /* check tx status */ | |
274 | switch (urb->status) { | |
275 | case 0: | |
276 | /* transmission complete */ | |
277 | netdev->stats.tx_packets++; | |
2b0861e8 | 278 | netdev->stats.tx_bytes += context->data_len; |
bb478555 SG |
279 | |
280 | /* prevent tx timeout */ | |
860e9538 | 281 | netif_trans_update(netdev); |
bb478555 SG |
282 | break; |
283 | ||
284 | default: | |
285 | if (net_ratelimit()) | |
286 | netdev_err(netdev, "Tx urb aborted (%d)\n", | |
287 | urb->status); | |
288 | case -EPROTO: | |
289 | case -ENOENT: | |
290 | case -ECONNRESET: | |
291 | case -ESHUTDOWN: | |
292 | ||
293 | break; | |
294 | } | |
295 | ||
296 | /* should always release echo skb and corresponding context */ | |
297 | can_get_echo_skb(netdev, context->echo_index); | |
298 | context->echo_index = PCAN_USB_MAX_TX_URBS; | |
299 | ||
300 | /* do wakeup tx queue in case of success only */ | |
301 | if (!urb->status) | |
302 | netif_wake_queue(netdev); | |
303 | } | |
304 | ||
305 | /* | |
306 | * called by netdev to send one skb on the CAN interface. | |
307 | */ | |
308 | static netdev_tx_t peak_usb_ndo_start_xmit(struct sk_buff *skb, | |
309 | struct net_device *netdev) | |
310 | { | |
311 | struct peak_usb_device *dev = netdev_priv(netdev); | |
312 | struct peak_tx_urb_context *context = NULL; | |
313 | struct net_device_stats *stats = &netdev->stats; | |
2b0861e8 | 314 | struct canfd_frame *cfd = (struct canfd_frame *)skb->data; |
bb478555 SG |
315 | struct urb *urb; |
316 | u8 *obuf; | |
317 | int i, err; | |
318 | size_t size = dev->adapter->tx_buffer_size; | |
319 | ||
320 | if (can_dropped_invalid_skb(netdev, skb)) | |
321 | return NETDEV_TX_OK; | |
322 | ||
323 | for (i = 0; i < PCAN_USB_MAX_TX_URBS; i++) | |
324 | if (dev->tx_contexts[i].echo_index == PCAN_USB_MAX_TX_URBS) { | |
325 | context = dev->tx_contexts + i; | |
326 | break; | |
327 | } | |
328 | ||
329 | if (!context) { | |
330 | /* should not occur except during restart */ | |
331 | return NETDEV_TX_BUSY; | |
332 | } | |
333 | ||
334 | urb = context->urb; | |
335 | obuf = urb->transfer_buffer; | |
336 | ||
337 | err = dev->adapter->dev_encode_msg(dev, skb, obuf, &size); | |
338 | if (err) { | |
339 | if (net_ratelimit()) | |
340 | netdev_err(netdev, "packet dropped\n"); | |
341 | dev_kfree_skb(skb); | |
342 | stats->tx_dropped++; | |
343 | return NETDEV_TX_OK; | |
344 | } | |
345 | ||
346 | context->echo_index = i; | |
2b0861e8 SG |
347 | |
348 | /* Note: this works with CANFD frames too */ | |
349 | context->data_len = cfd->len; | |
bb478555 SG |
350 | |
351 | usb_anchor_urb(urb, &dev->tx_submitted); | |
352 | ||
353 | can_put_echo_skb(skb, netdev, context->echo_index); | |
354 | ||
355 | atomic_inc(&dev->active_tx_urbs); | |
356 | ||
357 | err = usb_submit_urb(urb, GFP_ATOMIC); | |
358 | if (err) { | |
359 | can_free_echo_skb(netdev, context->echo_index); | |
360 | ||
361 | usb_unanchor_urb(urb); | |
362 | ||
363 | /* this context is not used in fact */ | |
364 | context->echo_index = PCAN_USB_MAX_TX_URBS; | |
365 | ||
366 | atomic_dec(&dev->active_tx_urbs); | |
367 | ||
368 | switch (err) { | |
369 | case -ENODEV: | |
370 | netif_device_detach(netdev); | |
371 | break; | |
372 | default: | |
373 | netdev_warn(netdev, "tx urb submitting failed err=%d\n", | |
374 | err); | |
375 | case -ENOENT: | |
376 | /* cable unplugged */ | |
377 | stats->tx_dropped++; | |
378 | } | |
379 | } else { | |
860e9538 | 380 | netif_trans_update(netdev); |
bb478555 SG |
381 | |
382 | /* slow down tx path */ | |
383 | if (atomic_read(&dev->active_tx_urbs) >= PCAN_USB_MAX_TX_URBS) | |
384 | netif_stop_queue(netdev); | |
385 | } | |
386 | ||
387 | return NETDEV_TX_OK; | |
388 | } | |
389 | ||
390 | /* | |
391 | * start the CAN interface. | |
392 | * Rx and Tx urbs are allocated here. Rx urbs are submitted here. | |
393 | */ | |
394 | static int peak_usb_start(struct peak_usb_device *dev) | |
395 | { | |
396 | struct net_device *netdev = dev->netdev; | |
397 | int err, i; | |
398 | ||
399 | for (i = 0; i < PCAN_USB_MAX_RX_URBS; i++) { | |
400 | struct urb *urb; | |
401 | u8 *buf; | |
402 | ||
403 | /* create a URB, and a buffer for it, to receive usb messages */ | |
404 | urb = usb_alloc_urb(0, GFP_KERNEL); | |
405 | if (!urb) { | |
bb478555 SG |
406 | err = -ENOMEM; |
407 | break; | |
408 | } | |
409 | ||
410 | buf = kmalloc(dev->adapter->rx_buffer_size, GFP_KERNEL); | |
411 | if (!buf) { | |
bb478555 SG |
412 | usb_free_urb(urb); |
413 | err = -ENOMEM; | |
414 | break; | |
415 | } | |
416 | ||
417 | usb_fill_bulk_urb(urb, dev->udev, | |
418 | usb_rcvbulkpipe(dev->udev, dev->ep_msg_in), | |
419 | buf, dev->adapter->rx_buffer_size, | |
420 | peak_usb_read_bulk_callback, dev); | |
421 | ||
422 | /* ask last usb_free_urb() to also kfree() transfer_buffer */ | |
423 | urb->transfer_flags |= URB_FREE_BUFFER; | |
424 | usb_anchor_urb(urb, &dev->rx_submitted); | |
425 | ||
426 | err = usb_submit_urb(urb, GFP_KERNEL); | |
427 | if (err) { | |
428 | if (err == -ENODEV) | |
429 | netif_device_detach(dev->netdev); | |
430 | ||
431 | usb_unanchor_urb(urb); | |
432 | kfree(buf); | |
433 | usb_free_urb(urb); | |
434 | break; | |
435 | } | |
436 | ||
437 | /* drop reference, USB core will take care of freeing it */ | |
438 | usb_free_urb(urb); | |
439 | } | |
440 | ||
441 | /* did we submit any URBs? Warn if we was not able to submit all urbs */ | |
442 | if (i < PCAN_USB_MAX_RX_URBS) { | |
443 | if (i == 0) { | |
444 | netdev_err(netdev, "couldn't setup any rx URB\n"); | |
445 | return err; | |
446 | } | |
447 | ||
448 | netdev_warn(netdev, "rx performance may be slow\n"); | |
449 | } | |
450 | ||
451 | /* pre-alloc tx buffers and corresponding urbs */ | |
452 | for (i = 0; i < PCAN_USB_MAX_TX_URBS; i++) { | |
453 | struct peak_tx_urb_context *context; | |
454 | struct urb *urb; | |
455 | u8 *buf; | |
456 | ||
457 | /* create a URB and a buffer for it, to transmit usb messages */ | |
458 | urb = usb_alloc_urb(0, GFP_KERNEL); | |
459 | if (!urb) { | |
bb478555 SG |
460 | err = -ENOMEM; |
461 | break; | |
462 | } | |
463 | ||
464 | buf = kmalloc(dev->adapter->tx_buffer_size, GFP_KERNEL); | |
465 | if (!buf) { | |
bb478555 SG |
466 | usb_free_urb(urb); |
467 | err = -ENOMEM; | |
468 | break; | |
469 | } | |
470 | ||
471 | context = dev->tx_contexts + i; | |
472 | context->dev = dev; | |
473 | context->urb = urb; | |
474 | ||
475 | usb_fill_bulk_urb(urb, dev->udev, | |
476 | usb_sndbulkpipe(dev->udev, dev->ep_msg_out), | |
477 | buf, dev->adapter->tx_buffer_size, | |
478 | peak_usb_write_bulk_callback, context); | |
479 | ||
480 | /* ask last usb_free_urb() to also kfree() transfer_buffer */ | |
481 | urb->transfer_flags |= URB_FREE_BUFFER; | |
482 | } | |
483 | ||
484 | /* warn if we were not able to allocate enough tx contexts */ | |
485 | if (i < PCAN_USB_MAX_TX_URBS) { | |
486 | if (i == 0) { | |
487 | netdev_err(netdev, "couldn't setup any tx URB\n"); | |
f37d6e70 | 488 | goto err_tx; |
bb478555 SG |
489 | } |
490 | ||
491 | netdev_warn(netdev, "tx performance may be slow\n"); | |
492 | } | |
493 | ||
494 | if (dev->adapter->dev_start) { | |
495 | err = dev->adapter->dev_start(dev); | |
496 | if (err) | |
f37d6e70 | 497 | goto err_adapter; |
bb478555 SG |
498 | } |
499 | ||
500 | dev->state |= PCAN_USB_STATE_STARTED; | |
501 | ||
502 | /* can set bus on now */ | |
503 | if (dev->adapter->dev_set_bus) { | |
504 | err = dev->adapter->dev_set_bus(dev, 1); | |
505 | if (err) | |
f37d6e70 | 506 | goto err_adapter; |
bb478555 SG |
507 | } |
508 | ||
509 | dev->can.state = CAN_STATE_ERROR_ACTIVE; | |
510 | ||
511 | return 0; | |
512 | ||
f37d6e70 | 513 | err_adapter: |
bb478555 SG |
514 | if (err == -ENODEV) |
515 | netif_device_detach(dev->netdev); | |
516 | ||
517 | netdev_warn(netdev, "couldn't submit control: %d\n", err); | |
518 | ||
f37d6e70 AK |
519 | for (i = 0; i < PCAN_USB_MAX_TX_URBS; i++) { |
520 | usb_free_urb(dev->tx_contexts[i].urb); | |
521 | dev->tx_contexts[i].urb = NULL; | |
522 | } | |
523 | err_tx: | |
524 | usb_kill_anchored_urbs(&dev->rx_submitted); | |
525 | ||
bb478555 SG |
526 | return err; |
527 | } | |
528 | ||
529 | /* | |
530 | * called by netdev to open the corresponding CAN interface. | |
531 | */ | |
532 | static int peak_usb_ndo_open(struct net_device *netdev) | |
533 | { | |
534 | struct peak_usb_device *dev = netdev_priv(netdev); | |
535 | int err; | |
536 | ||
537 | /* common open */ | |
538 | err = open_candev(netdev); | |
539 | if (err) | |
540 | return err; | |
541 | ||
542 | /* finally start device */ | |
543 | err = peak_usb_start(dev); | |
544 | if (err) { | |
545 | netdev_err(netdev, "couldn't start device: %d\n", err); | |
546 | close_candev(netdev); | |
547 | return err; | |
548 | } | |
549 | ||
bb478555 SG |
550 | netif_start_queue(netdev); |
551 | ||
552 | return 0; | |
553 | } | |
554 | ||
555 | /* | |
556 | * unlink in-flight Rx and Tx urbs and free their memory. | |
557 | */ | |
558 | static void peak_usb_unlink_all_urbs(struct peak_usb_device *dev) | |
559 | { | |
560 | int i; | |
561 | ||
562 | /* free all Rx (submitted) urbs */ | |
563 | usb_kill_anchored_urbs(&dev->rx_submitted); | |
564 | ||
565 | /* free unsubmitted Tx urbs first */ | |
566 | for (i = 0; i < PCAN_USB_MAX_TX_URBS; i++) { | |
567 | struct urb *urb = dev->tx_contexts[i].urb; | |
568 | ||
569 | if (!urb || | |
570 | dev->tx_contexts[i].echo_index != PCAN_USB_MAX_TX_URBS) { | |
571 | /* | |
572 | * this urb is already released or always submitted, | |
573 | * let usb core free by itself | |
574 | */ | |
575 | continue; | |
576 | } | |
577 | ||
578 | usb_free_urb(urb); | |
579 | dev->tx_contexts[i].urb = NULL; | |
580 | } | |
581 | ||
582 | /* then free all submitted Tx urbs */ | |
583 | usb_kill_anchored_urbs(&dev->tx_submitted); | |
584 | atomic_set(&dev->active_tx_urbs, 0); | |
585 | } | |
586 | ||
587 | /* | |
588 | * called by netdev to close the corresponding CAN interface. | |
589 | */ | |
590 | static int peak_usb_ndo_stop(struct net_device *netdev) | |
591 | { | |
592 | struct peak_usb_device *dev = netdev_priv(netdev); | |
593 | ||
594 | dev->state &= ~PCAN_USB_STATE_STARTED; | |
595 | netif_stop_queue(netdev); | |
596 | ||
597 | /* unlink all pending urbs and free used memory */ | |
598 | peak_usb_unlink_all_urbs(dev); | |
599 | ||
600 | if (dev->adapter->dev_stop) | |
601 | dev->adapter->dev_stop(dev); | |
602 | ||
603 | close_candev(netdev); | |
604 | ||
bb478555 SG |
605 | dev->can.state = CAN_STATE_STOPPED; |
606 | ||
607 | /* can set bus off now */ | |
608 | if (dev->adapter->dev_set_bus) { | |
609 | int err = dev->adapter->dev_set_bus(dev, 0); | |
610 | if (err) | |
611 | return err; | |
612 | } | |
613 | ||
614 | return 0; | |
615 | } | |
616 | ||
617 | /* | |
618 | * handle end of waiting for the device to reset | |
619 | */ | |
620 | void peak_usb_restart_complete(struct peak_usb_device *dev) | |
621 | { | |
622 | /* finally MUST update can state */ | |
623 | dev->can.state = CAN_STATE_ERROR_ACTIVE; | |
624 | ||
625 | /* netdev queue can be awaken now */ | |
626 | netif_wake_queue(dev->netdev); | |
627 | } | |
628 | ||
629 | void peak_usb_async_complete(struct urb *urb) | |
630 | { | |
631 | kfree(urb->transfer_buffer); | |
632 | usb_free_urb(urb); | |
633 | } | |
634 | ||
635 | /* | |
636 | * device (auto-)restart mechanism runs in a timer context => | |
637 | * MUST handle restart with asynchronous usb transfers | |
638 | */ | |
639 | static int peak_usb_restart(struct peak_usb_device *dev) | |
640 | { | |
641 | struct urb *urb; | |
642 | int err; | |
643 | u8 *buf; | |
644 | ||
645 | /* | |
646 | * if device doesn't define any asynchronous restart handler, simply | |
647 | * wake the netdev queue up | |
648 | */ | |
649 | if (!dev->adapter->dev_restart_async) { | |
650 | peak_usb_restart_complete(dev); | |
651 | return 0; | |
652 | } | |
653 | ||
654 | /* first allocate a urb to handle the asynchronous steps */ | |
655 | urb = usb_alloc_urb(0, GFP_ATOMIC); | |
2479204b | 656 | if (!urb) |
bb478555 | 657 | return -ENOMEM; |
bb478555 SG |
658 | |
659 | /* also allocate enough space for the commands to send */ | |
660 | buf = kmalloc(PCAN_USB_MAX_CMD_LEN, GFP_ATOMIC); | |
661 | if (!buf) { | |
bb478555 SG |
662 | usb_free_urb(urb); |
663 | return -ENOMEM; | |
664 | } | |
665 | ||
666 | /* call the device specific handler for the restart */ | |
667 | err = dev->adapter->dev_restart_async(dev, urb, buf); | |
668 | if (!err) | |
669 | return 0; | |
670 | ||
671 | kfree(buf); | |
672 | usb_free_urb(urb); | |
673 | ||
674 | return err; | |
675 | } | |
676 | ||
677 | /* | |
678 | * candev callback used to change CAN mode. | |
679 | * Warning: this is called from a timer context! | |
680 | */ | |
681 | static int peak_usb_set_mode(struct net_device *netdev, enum can_mode mode) | |
682 | { | |
683 | struct peak_usb_device *dev = netdev_priv(netdev); | |
684 | int err = 0; | |
685 | ||
bb478555 SG |
686 | switch (mode) { |
687 | case CAN_MODE_START: | |
688 | err = peak_usb_restart(dev); | |
689 | if (err) | |
690 | netdev_err(netdev, "couldn't start device (err %d)\n", | |
691 | err); | |
692 | break; | |
693 | ||
694 | default: | |
695 | return -EOPNOTSUPP; | |
696 | } | |
697 | ||
698 | return err; | |
699 | } | |
700 | ||
701 | /* | |
b9f2cc1b | 702 | * candev callback used to set device nominal/arbitration bitrate. |
bb478555 SG |
703 | */ |
704 | static int peak_usb_set_bittiming(struct net_device *netdev) | |
705 | { | |
706 | struct peak_usb_device *dev = netdev_priv(netdev); | |
b9f2cc1b | 707 | const struct peak_usb_adapter *pa = dev->adapter; |
bb478555 | 708 | |
b9f2cc1b SG |
709 | if (pa->dev_set_bittiming) { |
710 | struct can_bittiming *bt = &dev->can.bittiming; | |
711 | int err = pa->dev_set_bittiming(dev, bt); | |
bb478555 SG |
712 | |
713 | if (err) | |
714 | netdev_info(netdev, "couldn't set bitrate (err %d)\n", | |
b9f2cc1b SG |
715 | err); |
716 | return err; | |
717 | } | |
718 | ||
719 | return 0; | |
720 | } | |
721 | ||
722 | /* | |
723 | * candev callback used to set device data bitrate. | |
724 | */ | |
725 | static int peak_usb_set_data_bittiming(struct net_device *netdev) | |
726 | { | |
727 | struct peak_usb_device *dev = netdev_priv(netdev); | |
728 | const struct peak_usb_adapter *pa = dev->adapter; | |
729 | ||
730 | if (pa->dev_set_data_bittiming) { | |
731 | struct can_bittiming *bt = &dev->can.data_bittiming; | |
732 | int err = pa->dev_set_data_bittiming(dev, bt); | |
733 | ||
734 | if (err) | |
735 | netdev_info(netdev, | |
736 | "couldn't set data bitrate (err %d)\n", | |
737 | err); | |
738 | ||
bb478555 SG |
739 | return err; |
740 | } | |
741 | ||
742 | return 0; | |
743 | } | |
744 | ||
745 | static const struct net_device_ops peak_usb_netdev_ops = { | |
746 | .ndo_open = peak_usb_ndo_open, | |
747 | .ndo_stop = peak_usb_ndo_stop, | |
748 | .ndo_start_xmit = peak_usb_ndo_start_xmit, | |
c971fa2a | 749 | .ndo_change_mtu = can_change_mtu, |
bb478555 SG |
750 | }; |
751 | ||
752 | /* | |
753 | * create one device which is attached to CAN controller #ctrl_idx of the | |
754 | * usb adapter. | |
755 | */ | |
d6b5f30d | 756 | static int peak_usb_create_dev(const struct peak_usb_adapter *peak_usb_adapter, |
bb478555 SG |
757 | struct usb_interface *intf, int ctrl_idx) |
758 | { | |
759 | struct usb_device *usb_dev = interface_to_usbdev(intf); | |
760 | int sizeof_candev = peak_usb_adapter->sizeof_dev_private; | |
761 | struct peak_usb_device *dev; | |
762 | struct net_device *netdev; | |
763 | int i, err; | |
764 | u16 tmp16; | |
765 | ||
766 | if (sizeof_candev < sizeof(struct peak_usb_device)) | |
767 | sizeof_candev = sizeof(struct peak_usb_device); | |
768 | ||
769 | netdev = alloc_candev(sizeof_candev, PCAN_USB_MAX_TX_URBS); | |
770 | if (!netdev) { | |
771 | dev_err(&intf->dev, "%s: couldn't alloc candev\n", | |
772 | PCAN_USB_DRIVER_NAME); | |
773 | return -ENOMEM; | |
774 | } | |
775 | ||
776 | dev = netdev_priv(netdev); | |
777 | ||
778 | /* allocate a buffer large enough to send commands */ | |
779 | dev->cmd_buf = kmalloc(PCAN_USB_MAX_CMD_LEN, GFP_KERNEL); | |
780 | if (!dev->cmd_buf) { | |
bb478555 | 781 | err = -ENOMEM; |
af35d0f1 | 782 | goto lbl_free_candev; |
bb478555 SG |
783 | } |
784 | ||
785 | dev->udev = usb_dev; | |
786 | dev->netdev = netdev; | |
787 | dev->adapter = peak_usb_adapter; | |
788 | dev->ctrl_idx = ctrl_idx; | |
789 | dev->state = PCAN_USB_STATE_CONNECTED; | |
790 | ||
791 | dev->ep_msg_in = peak_usb_adapter->ep_msg_in; | |
792 | dev->ep_msg_out = peak_usb_adapter->ep_msg_out[ctrl_idx]; | |
793 | ||
794 | dev->can.clock = peak_usb_adapter->clock; | |
06b23f7f | 795 | dev->can.bittiming_const = peak_usb_adapter->bittiming_const; |
bb478555 | 796 | dev->can.do_set_bittiming = peak_usb_set_bittiming; |
06b23f7f | 797 | dev->can.data_bittiming_const = peak_usb_adapter->data_bittiming_const; |
b9f2cc1b | 798 | dev->can.do_set_data_bittiming = peak_usb_set_data_bittiming; |
bb478555 | 799 | dev->can.do_set_mode = peak_usb_set_mode; |
e3629f56 | 800 | dev->can.do_get_berr_counter = peak_usb_adapter->do_get_berr_counter; |
e3c5ea60 | 801 | dev->can.ctrlmode_supported = peak_usb_adapter->ctrlmode_supported; |
bb478555 SG |
802 | |
803 | netdev->netdev_ops = &peak_usb_netdev_ops; | |
804 | ||
805 | netdev->flags |= IFF_ECHO; /* we support local echo */ | |
806 | ||
807 | init_usb_anchor(&dev->rx_submitted); | |
808 | ||
809 | init_usb_anchor(&dev->tx_submitted); | |
810 | atomic_set(&dev->active_tx_urbs, 0); | |
811 | ||
812 | for (i = 0; i < PCAN_USB_MAX_TX_URBS; i++) | |
813 | dev->tx_contexts[i].echo_index = PCAN_USB_MAX_TX_URBS; | |
814 | ||
815 | dev->prev_siblings = usb_get_intfdata(intf); | |
816 | usb_set_intfdata(intf, dev); | |
817 | ||
818 | SET_NETDEV_DEV(netdev, &intf->dev); | |
3e66d013 | 819 | netdev->dev_id = ctrl_idx; |
bb478555 SG |
820 | |
821 | err = register_candev(netdev); | |
822 | if (err) { | |
823 | dev_err(&intf->dev, "couldn't register CAN device: %d\n", err); | |
af35d0f1 | 824 | goto lbl_restore_intf_data; |
bb478555 SG |
825 | } |
826 | ||
827 | if (dev->prev_siblings) | |
828 | (dev->prev_siblings)->next_siblings = dev; | |
829 | ||
830 | /* keep hw revision into the netdevice */ | |
831 | tmp16 = le16_to_cpu(usb_dev->descriptor.bcdDevice); | |
832 | dev->device_rev = tmp16 >> 8; | |
833 | ||
834 | if (dev->adapter->dev_init) { | |
835 | err = dev->adapter->dev_init(dev); | |
836 | if (err) | |
af35d0f1 | 837 | goto lbl_unregister_candev; |
bb478555 SG |
838 | } |
839 | ||
840 | /* set bus off */ | |
841 | if (dev->adapter->dev_set_bus) { | |
842 | err = dev->adapter->dev_set_bus(dev, 0); | |
843 | if (err) | |
af35d0f1 | 844 | goto lbl_unregister_candev; |
bb478555 SG |
845 | } |
846 | ||
847 | /* get device number early */ | |
848 | if (dev->adapter->dev_get_device_id) | |
849 | dev->adapter->dev_get_device_id(dev, &dev->device_number); | |
850 | ||
851 | netdev_info(netdev, "attached to %s channel %u (device %u)\n", | |
852 | peak_usb_adapter->name, ctrl_idx, dev->device_number); | |
853 | ||
854 | return 0; | |
855 | ||
af35d0f1 SG |
856 | lbl_unregister_candev: |
857 | unregister_candev(netdev); | |
bb478555 | 858 | |
af35d0f1 | 859 | lbl_restore_intf_data: |
bb478555 | 860 | usb_set_intfdata(intf, dev->prev_siblings); |
af35d0f1 SG |
861 | kfree(dev->cmd_buf); |
862 | ||
863 | lbl_free_candev: | |
bb478555 SG |
864 | free_candev(netdev); |
865 | ||
866 | return err; | |
867 | } | |
868 | ||
869 | /* | |
870 | * called by the usb core when the device is unplugged from the system | |
871 | */ | |
872 | static void peak_usb_disconnect(struct usb_interface *intf) | |
873 | { | |
874 | struct peak_usb_device *dev; | |
b67d0dd7 | 875 | struct peak_usb_device *dev_prev_siblings; |
bb478555 SG |
876 | |
877 | /* unregister as many netdev devices as siblings */ | |
b67d0dd7 | 878 | for (dev = usb_get_intfdata(intf); dev; dev = dev_prev_siblings) { |
bb478555 SG |
879 | struct net_device *netdev = dev->netdev; |
880 | char name[IFNAMSIZ]; | |
881 | ||
b67d0dd7 | 882 | dev_prev_siblings = dev->prev_siblings; |
bb478555 SG |
883 | dev->state &= ~PCAN_USB_STATE_CONNECTED; |
884 | strncpy(name, netdev->name, IFNAMSIZ); | |
885 | ||
886 | unregister_netdev(netdev); | |
bb478555 SG |
887 | |
888 | kfree(dev->cmd_buf); | |
889 | dev->next_siblings = NULL; | |
890 | if (dev->adapter->dev_free) | |
891 | dev->adapter->dev_free(dev); | |
892 | ||
b67d0dd7 | 893 | free_candev(netdev); |
bb478555 SG |
894 | dev_info(&intf->dev, "%s removed\n", name); |
895 | } | |
896 | ||
897 | usb_set_intfdata(intf, NULL); | |
898 | } | |
899 | ||
900 | /* | |
901 | * probe function for new PEAK-System devices | |
902 | */ | |
903 | static int peak_usb_probe(struct usb_interface *intf, | |
904 | const struct usb_device_id *id) | |
905 | { | |
906 | struct usb_device *usb_dev = interface_to_usbdev(intf); | |
62bc24f6 | 907 | const u16 usb_id_product = le16_to_cpu(usb_dev->descriptor.idProduct); |
d6b5f30d | 908 | const struct peak_usb_adapter *peak_usb_adapter = NULL; |
bb478555 SG |
909 | int i, err = -ENOMEM; |
910 | ||
bb478555 | 911 | /* get corresponding PCAN-USB adapter */ |
0adfd733 MKB |
912 | for (i = 0; i < ARRAY_SIZE(peak_usb_adapters_list); i++) |
913 | if (peak_usb_adapters_list[i]->device_id == usb_id_product) { | |
914 | peak_usb_adapter = peak_usb_adapters_list[i]; | |
bb478555 | 915 | break; |
0adfd733 | 916 | } |
bb478555 | 917 | |
bb478555 SG |
918 | if (!peak_usb_adapter) { |
919 | /* should never come except device_id bad usage in this file */ | |
920 | pr_err("%s: didn't find device id. 0x%x in devices list\n", | |
dadcd398 | 921 | PCAN_USB_DRIVER_NAME, usb_id_product); |
bb478555 SG |
922 | return -ENODEV; |
923 | } | |
924 | ||
925 | /* got corresponding adapter: check if it handles current interface */ | |
926 | if (peak_usb_adapter->intf_probe) { | |
927 | err = peak_usb_adapter->intf_probe(intf); | |
928 | if (err) | |
929 | return err; | |
930 | } | |
931 | ||
932 | for (i = 0; i < peak_usb_adapter->ctrl_count; i++) { | |
933 | err = peak_usb_create_dev(peak_usb_adapter, intf, i); | |
934 | if (err) { | |
935 | /* deregister already created devices */ | |
936 | peak_usb_disconnect(intf); | |
937 | break; | |
938 | } | |
939 | } | |
940 | ||
941 | return err; | |
942 | } | |
943 | ||
944 | /* usb specific object needed to register this driver with the usb subsystem */ | |
945 | static struct usb_driver peak_usb_driver = { | |
946 | .name = PCAN_USB_DRIVER_NAME, | |
947 | .disconnect = peak_usb_disconnect, | |
948 | .probe = peak_usb_probe, | |
949 | .id_table = peak_usb_table, | |
950 | }; | |
951 | ||
952 | static int __init peak_usb_init(void) | |
953 | { | |
954 | int err; | |
955 | ||
956 | /* register this driver with the USB subsystem */ | |
957 | err = usb_register(&peak_usb_driver); | |
958 | if (err) | |
959 | pr_err("%s: usb_register failed (err %d)\n", | |
960 | PCAN_USB_DRIVER_NAME, err); | |
961 | ||
962 | return err; | |
963 | } | |
964 | ||
965 | static int peak_usb_do_device_exit(struct device *d, void *arg) | |
966 | { | |
967 | struct usb_interface *intf = to_usb_interface(d); | |
968 | struct peak_usb_device *dev; | |
969 | ||
970 | /* stop as many netdev devices as siblings */ | |
971 | for (dev = usb_get_intfdata(intf); dev; dev = dev->prev_siblings) { | |
972 | struct net_device *netdev = dev->netdev; | |
973 | ||
974 | if (netif_device_present(netdev)) | |
975 | if (dev->adapter->dev_exit) | |
976 | dev->adapter->dev_exit(dev); | |
977 | } | |
978 | ||
979 | return 0; | |
980 | } | |
981 | ||
982 | static void __exit peak_usb_exit(void) | |
983 | { | |
984 | int err; | |
985 | ||
986 | /* last chance do send any synchronous commands here */ | |
987 | err = driver_for_each_device(&peak_usb_driver.drvwrap.driver, NULL, | |
988 | NULL, peak_usb_do_device_exit); | |
989 | if (err) | |
990 | pr_err("%s: failed to stop all can devices (err %d)\n", | |
991 | PCAN_USB_DRIVER_NAME, err); | |
992 | ||
993 | /* deregister this driver with the USB subsystem */ | |
994 | usb_deregister(&peak_usb_driver); | |
995 | ||
996 | pr_info("%s: PCAN-USB interfaces driver unloaded\n", | |
997 | PCAN_USB_DRIVER_NAME); | |
998 | } | |
999 | ||
1000 | module_init(peak_usb_init); | |
1001 | module_exit(peak_usb_exit); |