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