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 | ||
166 | /* | |
167 | * callback for bulk Rx urb | |
168 | */ | |
169 | static void peak_usb_read_bulk_callback(struct urb *urb) | |
170 | { | |
171 | struct peak_usb_device *dev = urb->context; | |
172 | struct net_device *netdev; | |
173 | int err; | |
174 | ||
175 | netdev = dev->netdev; | |
176 | ||
177 | if (!netif_device_present(netdev)) | |
178 | return; | |
179 | ||
180 | /* check reception status */ | |
181 | switch (urb->status) { | |
182 | case 0: | |
183 | /* success */ | |
184 | break; | |
185 | ||
186 | case -EILSEQ: | |
187 | case -ENOENT: | |
188 | case -ECONNRESET: | |
189 | case -ESHUTDOWN: | |
190 | return; | |
191 | ||
192 | default: | |
193 | if (net_ratelimit()) | |
194 | netdev_err(netdev, | |
195 | "Rx urb aborted (%d)\n", urb->status); | |
196 | goto resubmit_urb; | |
197 | } | |
198 | ||
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); | |
204 | if (err) | |
56b9f301 RD |
205 | pcan_dump_mem("received usb message", |
206 | urb->transfer_buffer, | |
207 | urb->transfer_buffer_length); | |
bb478555 SG |
208 | } |
209 | } | |
210 | ||
211 | resubmit_urb: | |
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); | |
216 | ||
217 | usb_anchor_urb(urb, &dev->rx_submitted); | |
218 | err = usb_submit_urb(urb, GFP_ATOMIC); | |
219 | if (!err) | |
220 | return; | |
221 | ||
222 | usb_unanchor_urb(urb); | |
223 | ||
224 | if (err == -ENODEV) | |
225 | netif_device_detach(netdev); | |
226 | else | |
227 | netdev_err(netdev, "failed resubmitting read bulk urb: %d\n", | |
228 | err); | |
229 | } | |
230 | ||
231 | /* | |
232 | * callback for bulk Tx urb | |
233 | */ | |
234 | static void peak_usb_write_bulk_callback(struct urb *urb) | |
235 | { | |
236 | struct peak_tx_urb_context *context = urb->context; | |
237 | struct peak_usb_device *dev; | |
238 | struct net_device *netdev; | |
239 | ||
240 | BUG_ON(!context); | |
241 | ||
242 | dev = context->dev; | |
243 | netdev = dev->netdev; | |
244 | ||
245 | atomic_dec(&dev->active_tx_urbs); | |
246 | ||
247 | if (!netif_device_present(netdev)) | |
248 | return; | |
249 | ||
250 | /* check tx status */ | |
251 | switch (urb->status) { | |
252 | case 0: | |
253 | /* transmission complete */ | |
254 | netdev->stats.tx_packets++; | |
255 | netdev->stats.tx_bytes += context->dlc; | |
256 | ||
257 | /* prevent tx timeout */ | |
258 | netdev->trans_start = jiffies; | |
259 | break; | |
260 | ||
261 | default: | |
262 | if (net_ratelimit()) | |
263 | netdev_err(netdev, "Tx urb aborted (%d)\n", | |
264 | urb->status); | |
265 | case -EPROTO: | |
266 | case -ENOENT: | |
267 | case -ECONNRESET: | |
268 | case -ESHUTDOWN: | |
269 | ||
270 | break; | |
271 | } | |
272 | ||
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; | |
276 | ||
277 | /* do wakeup tx queue in case of success only */ | |
278 | if (!urb->status) | |
279 | netif_wake_queue(netdev); | |
280 | } | |
281 | ||
282 | /* | |
283 | * called by netdev to send one skb on the CAN interface. | |
284 | */ | |
285 | static netdev_tx_t peak_usb_ndo_start_xmit(struct sk_buff *skb, | |
286 | struct net_device *netdev) | |
287 | { | |
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; | |
292 | struct urb *urb; | |
293 | u8 *obuf; | |
294 | int i, err; | |
295 | size_t size = dev->adapter->tx_buffer_size; | |
296 | ||
297 | if (can_dropped_invalid_skb(netdev, skb)) | |
298 | return NETDEV_TX_OK; | |
299 | ||
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; | |
303 | break; | |
304 | } | |
305 | ||
306 | if (!context) { | |
307 | /* should not occur except during restart */ | |
308 | return NETDEV_TX_BUSY; | |
309 | } | |
310 | ||
311 | urb = context->urb; | |
312 | obuf = urb->transfer_buffer; | |
313 | ||
314 | err = dev->adapter->dev_encode_msg(dev, skb, obuf, &size); | |
315 | if (err) { | |
316 | if (net_ratelimit()) | |
317 | netdev_err(netdev, "packet dropped\n"); | |
318 | dev_kfree_skb(skb); | |
319 | stats->tx_dropped++; | |
320 | return NETDEV_TX_OK; | |
321 | } | |
322 | ||
323 | context->echo_index = i; | |
324 | context->dlc = cf->can_dlc; | |
325 | ||
326 | usb_anchor_urb(urb, &dev->tx_submitted); | |
327 | ||
328 | can_put_echo_skb(skb, netdev, context->echo_index); | |
329 | ||
330 | atomic_inc(&dev->active_tx_urbs); | |
331 | ||
332 | err = usb_submit_urb(urb, GFP_ATOMIC); | |
333 | if (err) { | |
334 | can_free_echo_skb(netdev, context->echo_index); | |
335 | ||
336 | usb_unanchor_urb(urb); | |
337 | ||
338 | /* this context is not used in fact */ | |
339 | context->echo_index = PCAN_USB_MAX_TX_URBS; | |
340 | ||
341 | atomic_dec(&dev->active_tx_urbs); | |
342 | ||
343 | switch (err) { | |
344 | case -ENODEV: | |
345 | netif_device_detach(netdev); | |
346 | break; | |
347 | default: | |
348 | netdev_warn(netdev, "tx urb submitting failed err=%d\n", | |
349 | err); | |
350 | case -ENOENT: | |
351 | /* cable unplugged */ | |
352 | stats->tx_dropped++; | |
353 | } | |
354 | } else { | |
355 | netdev->trans_start = jiffies; | |
356 | ||
357 | /* slow down tx path */ | |
358 | if (atomic_read(&dev->active_tx_urbs) >= PCAN_USB_MAX_TX_URBS) | |
359 | netif_stop_queue(netdev); | |
360 | } | |
361 | ||
362 | return NETDEV_TX_OK; | |
363 | } | |
364 | ||
365 | /* | |
366 | * start the CAN interface. | |
367 | * Rx and Tx urbs are allocated here. Rx urbs are submitted here. | |
368 | */ | |
369 | static int peak_usb_start(struct peak_usb_device *dev) | |
370 | { | |
371 | struct net_device *netdev = dev->netdev; | |
372 | int err, i; | |
373 | ||
374 | for (i = 0; i < PCAN_USB_MAX_RX_URBS; i++) { | |
375 | struct urb *urb; | |
376 | u8 *buf; | |
377 | ||
378 | /* create a URB, and a buffer for it, to receive usb messages */ | |
379 | urb = usb_alloc_urb(0, GFP_KERNEL); | |
380 | if (!urb) { | |
381 | netdev_err(netdev, "No memory left for URBs\n"); | |
382 | err = -ENOMEM; | |
383 | break; | |
384 | } | |
385 | ||
386 | buf = kmalloc(dev->adapter->rx_buffer_size, GFP_KERNEL); | |
387 | if (!buf) { | |
bb478555 SG |
388 | usb_free_urb(urb); |
389 | err = -ENOMEM; | |
390 | break; | |
391 | } | |
392 | ||
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); | |
397 | ||
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); | |
401 | ||
402 | err = usb_submit_urb(urb, GFP_KERNEL); | |
403 | if (err) { | |
404 | if (err == -ENODEV) | |
405 | netif_device_detach(dev->netdev); | |
406 | ||
407 | usb_unanchor_urb(urb); | |
408 | kfree(buf); | |
409 | usb_free_urb(urb); | |
410 | break; | |
411 | } | |
412 | ||
413 | /* drop reference, USB core will take care of freeing it */ | |
414 | usb_free_urb(urb); | |
415 | } | |
416 | ||
417 | /* did we submit any URBs? Warn if we was not able to submit all urbs */ | |
418 | if (i < PCAN_USB_MAX_RX_URBS) { | |
419 | if (i == 0) { | |
420 | netdev_err(netdev, "couldn't setup any rx URB\n"); | |
421 | return err; | |
422 | } | |
423 | ||
424 | netdev_warn(netdev, "rx performance may be slow\n"); | |
425 | } | |
426 | ||
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; | |
430 | struct urb *urb; | |
431 | u8 *buf; | |
432 | ||
433 | /* create a URB and a buffer for it, to transmit usb messages */ | |
434 | urb = usb_alloc_urb(0, GFP_KERNEL); | |
435 | if (!urb) { | |
436 | netdev_err(netdev, "No memory left for URBs\n"); | |
437 | err = -ENOMEM; | |
438 | break; | |
439 | } | |
440 | ||
441 | buf = kmalloc(dev->adapter->tx_buffer_size, GFP_KERNEL); | |
442 | if (!buf) { | |
bb478555 SG |
443 | usb_free_urb(urb); |
444 | err = -ENOMEM; | |
445 | break; | |
446 | } | |
447 | ||
448 | context = dev->tx_contexts + i; | |
449 | context->dev = dev; | |
450 | context->urb = urb; | |
451 | ||
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); | |
456 | ||
457 | /* ask last usb_free_urb() to also kfree() transfer_buffer */ | |
458 | urb->transfer_flags |= URB_FREE_BUFFER; | |
459 | } | |
460 | ||
461 | /* warn if we were not able to allocate enough tx contexts */ | |
462 | if (i < PCAN_USB_MAX_TX_URBS) { | |
463 | if (i == 0) { | |
464 | netdev_err(netdev, "couldn't setup any tx URB\n"); | |
f37d6e70 | 465 | goto err_tx; |
bb478555 SG |
466 | } |
467 | ||
468 | netdev_warn(netdev, "tx performance may be slow\n"); | |
469 | } | |
470 | ||
471 | if (dev->adapter->dev_start) { | |
472 | err = dev->adapter->dev_start(dev); | |
473 | if (err) | |
f37d6e70 | 474 | goto err_adapter; |
bb478555 SG |
475 | } |
476 | ||
477 | dev->state |= PCAN_USB_STATE_STARTED; | |
478 | ||
479 | /* can set bus on now */ | |
480 | if (dev->adapter->dev_set_bus) { | |
481 | err = dev->adapter->dev_set_bus(dev, 1); | |
482 | if (err) | |
f37d6e70 | 483 | goto err_adapter; |
bb478555 SG |
484 | } |
485 | ||
486 | dev->can.state = CAN_STATE_ERROR_ACTIVE; | |
487 | ||
488 | return 0; | |
489 | ||
f37d6e70 | 490 | err_adapter: |
bb478555 SG |
491 | if (err == -ENODEV) |
492 | netif_device_detach(dev->netdev); | |
493 | ||
494 | netdev_warn(netdev, "couldn't submit control: %d\n", err); | |
495 | ||
f37d6e70 AK |
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; | |
499 | } | |
500 | err_tx: | |
501 | usb_kill_anchored_urbs(&dev->rx_submitted); | |
502 | ||
bb478555 SG |
503 | return err; |
504 | } | |
505 | ||
506 | /* | |
507 | * called by netdev to open the corresponding CAN interface. | |
508 | */ | |
509 | static int peak_usb_ndo_open(struct net_device *netdev) | |
510 | { | |
511 | struct peak_usb_device *dev = netdev_priv(netdev); | |
512 | int err; | |
513 | ||
514 | /* common open */ | |
515 | err = open_candev(netdev); | |
516 | if (err) | |
517 | return err; | |
518 | ||
519 | /* finally start device */ | |
520 | err = peak_usb_start(dev); | |
521 | if (err) { | |
522 | netdev_err(netdev, "couldn't start device: %d\n", err); | |
523 | close_candev(netdev); | |
524 | return err; | |
525 | } | |
526 | ||
bb478555 SG |
527 | netif_start_queue(netdev); |
528 | ||
529 | return 0; | |
530 | } | |
531 | ||
532 | /* | |
533 | * unlink in-flight Rx and Tx urbs and free their memory. | |
534 | */ | |
535 | static void peak_usb_unlink_all_urbs(struct peak_usb_device *dev) | |
536 | { | |
537 | int i; | |
538 | ||
539 | /* free all Rx (submitted) urbs */ | |
540 | usb_kill_anchored_urbs(&dev->rx_submitted); | |
541 | ||
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; | |
545 | ||
546 | if (!urb || | |
547 | dev->tx_contexts[i].echo_index != PCAN_USB_MAX_TX_URBS) { | |
548 | /* | |
549 | * this urb is already released or always submitted, | |
550 | * let usb core free by itself | |
551 | */ | |
552 | continue; | |
553 | } | |
554 | ||
555 | usb_free_urb(urb); | |
556 | dev->tx_contexts[i].urb = NULL; | |
557 | } | |
558 | ||
559 | /* then free all submitted Tx urbs */ | |
560 | usb_kill_anchored_urbs(&dev->tx_submitted); | |
561 | atomic_set(&dev->active_tx_urbs, 0); | |
562 | } | |
563 | ||
564 | /* | |
565 | * called by netdev to close the corresponding CAN interface. | |
566 | */ | |
567 | static int peak_usb_ndo_stop(struct net_device *netdev) | |
568 | { | |
569 | struct peak_usb_device *dev = netdev_priv(netdev); | |
570 | ||
571 | dev->state &= ~PCAN_USB_STATE_STARTED; | |
572 | netif_stop_queue(netdev); | |
573 | ||
574 | /* unlink all pending urbs and free used memory */ | |
575 | peak_usb_unlink_all_urbs(dev); | |
576 | ||
577 | if (dev->adapter->dev_stop) | |
578 | dev->adapter->dev_stop(dev); | |
579 | ||
580 | close_candev(netdev); | |
581 | ||
bb478555 SG |
582 | dev->can.state = CAN_STATE_STOPPED; |
583 | ||
584 | /* can set bus off now */ | |
585 | if (dev->adapter->dev_set_bus) { | |
586 | int err = dev->adapter->dev_set_bus(dev, 0); | |
587 | if (err) | |
588 | return err; | |
589 | } | |
590 | ||
591 | return 0; | |
592 | } | |
593 | ||
594 | /* | |
595 | * handle end of waiting for the device to reset | |
596 | */ | |
597 | void peak_usb_restart_complete(struct peak_usb_device *dev) | |
598 | { | |
599 | /* finally MUST update can state */ | |
600 | dev->can.state = CAN_STATE_ERROR_ACTIVE; | |
601 | ||
602 | /* netdev queue can be awaken now */ | |
603 | netif_wake_queue(dev->netdev); | |
604 | } | |
605 | ||
606 | void peak_usb_async_complete(struct urb *urb) | |
607 | { | |
608 | kfree(urb->transfer_buffer); | |
609 | usb_free_urb(urb); | |
610 | } | |
611 | ||
612 | /* | |
613 | * device (auto-)restart mechanism runs in a timer context => | |
614 | * MUST handle restart with asynchronous usb transfers | |
615 | */ | |
616 | static int peak_usb_restart(struct peak_usb_device *dev) | |
617 | { | |
618 | struct urb *urb; | |
619 | int err; | |
620 | u8 *buf; | |
621 | ||
622 | /* | |
623 | * if device doesn't define any asynchronous restart handler, simply | |
624 | * wake the netdev queue up | |
625 | */ | |
626 | if (!dev->adapter->dev_restart_async) { | |
627 | peak_usb_restart_complete(dev); | |
628 | return 0; | |
629 | } | |
630 | ||
631 | /* first allocate a urb to handle the asynchronous steps */ | |
632 | urb = usb_alloc_urb(0, GFP_ATOMIC); | |
633 | if (!urb) { | |
634 | netdev_err(dev->netdev, "no memory left for urb\n"); | |
635 | return -ENOMEM; | |
636 | } | |
637 | ||
638 | /* also allocate enough space for the commands to send */ | |
639 | buf = kmalloc(PCAN_USB_MAX_CMD_LEN, GFP_ATOMIC); | |
640 | if (!buf) { | |
bb478555 SG |
641 | usb_free_urb(urb); |
642 | return -ENOMEM; | |
643 | } | |
644 | ||
645 | /* call the device specific handler for the restart */ | |
646 | err = dev->adapter->dev_restart_async(dev, urb, buf); | |
647 | if (!err) | |
648 | return 0; | |
649 | ||
650 | kfree(buf); | |
651 | usb_free_urb(urb); | |
652 | ||
653 | return err; | |
654 | } | |
655 | ||
656 | /* | |
657 | * candev callback used to change CAN mode. | |
658 | * Warning: this is called from a timer context! | |
659 | */ | |
660 | static int peak_usb_set_mode(struct net_device *netdev, enum can_mode mode) | |
661 | { | |
662 | struct peak_usb_device *dev = netdev_priv(netdev); | |
663 | int err = 0; | |
664 | ||
bb478555 SG |
665 | switch (mode) { |
666 | case CAN_MODE_START: | |
667 | err = peak_usb_restart(dev); | |
668 | if (err) | |
669 | netdev_err(netdev, "couldn't start device (err %d)\n", | |
670 | err); | |
671 | break; | |
672 | ||
673 | default: | |
674 | return -EOPNOTSUPP; | |
675 | } | |
676 | ||
677 | return err; | |
678 | } | |
679 | ||
680 | /* | |
681 | * candev callback used to set device bitrate. | |
682 | */ | |
683 | static int peak_usb_set_bittiming(struct net_device *netdev) | |
684 | { | |
685 | struct peak_usb_device *dev = netdev_priv(netdev); | |
686 | struct can_bittiming *bt = &dev->can.bittiming; | |
687 | ||
688 | if (dev->adapter->dev_set_bittiming) { | |
689 | int err = dev->adapter->dev_set_bittiming(dev, bt); | |
690 | ||
691 | if (err) | |
692 | netdev_info(netdev, "couldn't set bitrate (err %d)\n", | |
693 | err); | |
694 | return err; | |
695 | } | |
696 | ||
697 | return 0; | |
698 | } | |
699 | ||
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, | |
c971fa2a | 704 | .ndo_change_mtu = can_change_mtu, |
bb478555 SG |
705 | }; |
706 | ||
707 | /* | |
708 | * create one device which is attached to CAN controller #ctrl_idx of the | |
709 | * usb adapter. | |
710 | */ | |
d6b5f30d | 711 | static int peak_usb_create_dev(const struct peak_usb_adapter *peak_usb_adapter, |
bb478555 SG |
712 | struct usb_interface *intf, int ctrl_idx) |
713 | { | |
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; | |
718 | int i, err; | |
719 | u16 tmp16; | |
720 | ||
721 | if (sizeof_candev < sizeof(struct peak_usb_device)) | |
722 | sizeof_candev = sizeof(struct peak_usb_device); | |
723 | ||
724 | netdev = alloc_candev(sizeof_candev, PCAN_USB_MAX_TX_URBS); | |
725 | if (!netdev) { | |
726 | dev_err(&intf->dev, "%s: couldn't alloc candev\n", | |
727 | PCAN_USB_DRIVER_NAME); | |
728 | return -ENOMEM; | |
729 | } | |
730 | ||
731 | dev = netdev_priv(netdev); | |
732 | ||
733 | /* allocate a buffer large enough to send commands */ | |
734 | dev->cmd_buf = kmalloc(PCAN_USB_MAX_CMD_LEN, GFP_KERNEL); | |
735 | if (!dev->cmd_buf) { | |
bb478555 | 736 | err = -ENOMEM; |
af35d0f1 | 737 | goto lbl_free_candev; |
bb478555 SG |
738 | } |
739 | ||
740 | dev->udev = usb_dev; | |
741 | dev->netdev = netdev; | |
742 | dev->adapter = peak_usb_adapter; | |
743 | dev->ctrl_idx = ctrl_idx; | |
744 | dev->state = PCAN_USB_STATE_CONNECTED; | |
745 | ||
746 | dev->ep_msg_in = peak_usb_adapter->ep_msg_in; | |
747 | dev->ep_msg_out = peak_usb_adapter->ep_msg_out[ctrl_idx]; | |
748 | ||
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; | |
e3629f56 | 753 | dev->can.do_get_berr_counter = peak_usb_adapter->do_get_berr_counter; |
e3c5ea60 | 754 | dev->can.ctrlmode_supported = peak_usb_adapter->ctrlmode_supported; |
bb478555 SG |
755 | |
756 | netdev->netdev_ops = &peak_usb_netdev_ops; | |
757 | ||
758 | netdev->flags |= IFF_ECHO; /* we support local echo */ | |
759 | ||
760 | init_usb_anchor(&dev->rx_submitted); | |
761 | ||
762 | init_usb_anchor(&dev->tx_submitted); | |
763 | atomic_set(&dev->active_tx_urbs, 0); | |
764 | ||
765 | for (i = 0; i < PCAN_USB_MAX_TX_URBS; i++) | |
766 | dev->tx_contexts[i].echo_index = PCAN_USB_MAX_TX_URBS; | |
767 | ||
768 | dev->prev_siblings = usb_get_intfdata(intf); | |
769 | usb_set_intfdata(intf, dev); | |
770 | ||
771 | SET_NETDEV_DEV(netdev, &intf->dev); | |
3e66d013 | 772 | netdev->dev_id = ctrl_idx; |
bb478555 SG |
773 | |
774 | err = register_candev(netdev); | |
775 | if (err) { | |
776 | dev_err(&intf->dev, "couldn't register CAN device: %d\n", err); | |
af35d0f1 | 777 | goto lbl_restore_intf_data; |
bb478555 SG |
778 | } |
779 | ||
780 | if (dev->prev_siblings) | |
781 | (dev->prev_siblings)->next_siblings = dev; | |
782 | ||
783 | /* keep hw revision into the netdevice */ | |
784 | tmp16 = le16_to_cpu(usb_dev->descriptor.bcdDevice); | |
785 | dev->device_rev = tmp16 >> 8; | |
786 | ||
787 | if (dev->adapter->dev_init) { | |
788 | err = dev->adapter->dev_init(dev); | |
789 | if (err) | |
af35d0f1 | 790 | goto lbl_unregister_candev; |
bb478555 SG |
791 | } |
792 | ||
793 | /* set bus off */ | |
794 | if (dev->adapter->dev_set_bus) { | |
795 | err = dev->adapter->dev_set_bus(dev, 0); | |
796 | if (err) | |
af35d0f1 | 797 | goto lbl_unregister_candev; |
bb478555 SG |
798 | } |
799 | ||
800 | /* get device number early */ | |
801 | if (dev->adapter->dev_get_device_id) | |
802 | dev->adapter->dev_get_device_id(dev, &dev->device_number); | |
803 | ||
804 | netdev_info(netdev, "attached to %s channel %u (device %u)\n", | |
805 | peak_usb_adapter->name, ctrl_idx, dev->device_number); | |
806 | ||
807 | return 0; | |
808 | ||
af35d0f1 SG |
809 | lbl_unregister_candev: |
810 | unregister_candev(netdev); | |
bb478555 | 811 | |
af35d0f1 | 812 | lbl_restore_intf_data: |
bb478555 | 813 | usb_set_intfdata(intf, dev->prev_siblings); |
af35d0f1 SG |
814 | kfree(dev->cmd_buf); |
815 | ||
816 | lbl_free_candev: | |
bb478555 SG |
817 | free_candev(netdev); |
818 | ||
819 | return err; | |
820 | } | |
821 | ||
822 | /* | |
823 | * called by the usb core when the device is unplugged from the system | |
824 | */ | |
825 | static void peak_usb_disconnect(struct usb_interface *intf) | |
826 | { | |
827 | struct peak_usb_device *dev; | |
828 | ||
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; | |
832 | char name[IFNAMSIZ]; | |
833 | ||
834 | dev->state &= ~PCAN_USB_STATE_CONNECTED; | |
835 | strncpy(name, netdev->name, IFNAMSIZ); | |
836 | ||
837 | unregister_netdev(netdev); | |
838 | free_candev(netdev); | |
839 | ||
840 | kfree(dev->cmd_buf); | |
841 | dev->next_siblings = NULL; | |
842 | if (dev->adapter->dev_free) | |
843 | dev->adapter->dev_free(dev); | |
844 | ||
845 | dev_info(&intf->dev, "%s removed\n", name); | |
846 | } | |
847 | ||
848 | usb_set_intfdata(intf, NULL); | |
849 | } | |
850 | ||
851 | /* | |
852 | * probe function for new PEAK-System devices | |
853 | */ | |
854 | static int peak_usb_probe(struct usb_interface *intf, | |
855 | const struct usb_device_id *id) | |
856 | { | |
857 | struct usb_device *usb_dev = interface_to_usbdev(intf); | |
62bc24f6 | 858 | const u16 usb_id_product = le16_to_cpu(usb_dev->descriptor.idProduct); |
d6b5f30d | 859 | const struct peak_usb_adapter *peak_usb_adapter = NULL; |
bb478555 SG |
860 | int i, err = -ENOMEM; |
861 | ||
862 | usb_dev = interface_to_usbdev(intf); | |
863 | ||
864 | /* get corresponding PCAN-USB adapter */ | |
0adfd733 MKB |
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]; | |
bb478555 | 868 | break; |
0adfd733 | 869 | } |
bb478555 | 870 | |
bb478555 SG |
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); | |
875 | return -ENODEV; | |
876 | } | |
877 | ||
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); | |
881 | if (err) | |
882 | return err; | |
883 | } | |
884 | ||
885 | for (i = 0; i < peak_usb_adapter->ctrl_count; i++) { | |
886 | err = peak_usb_create_dev(peak_usb_adapter, intf, i); | |
887 | if (err) { | |
888 | /* deregister already created devices */ | |
889 | peak_usb_disconnect(intf); | |
890 | break; | |
891 | } | |
892 | } | |
893 | ||
894 | return err; | |
895 | } | |
896 | ||
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, | |
903 | }; | |
904 | ||
905 | static int __init peak_usb_init(void) | |
906 | { | |
907 | int err; | |
908 | ||
909 | /* register this driver with the USB subsystem */ | |
910 | err = usb_register(&peak_usb_driver); | |
911 | if (err) | |
912 | pr_err("%s: usb_register failed (err %d)\n", | |
913 | PCAN_USB_DRIVER_NAME, err); | |
914 | ||
915 | return err; | |
916 | } | |
917 | ||
918 | static int peak_usb_do_device_exit(struct device *d, void *arg) | |
919 | { | |
920 | struct usb_interface *intf = to_usb_interface(d); | |
921 | struct peak_usb_device *dev; | |
922 | ||
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; | |
926 | ||
927 | if (netif_device_present(netdev)) | |
928 | if (dev->adapter->dev_exit) | |
929 | dev->adapter->dev_exit(dev); | |
930 | } | |
931 | ||
932 | return 0; | |
933 | } | |
934 | ||
935 | static void __exit peak_usb_exit(void) | |
936 | { | |
937 | int err; | |
938 | ||
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); | |
942 | if (err) | |
943 | pr_err("%s: failed to stop all can devices (err %d)\n", | |
944 | PCAN_USB_DRIVER_NAME, err); | |
945 | ||
946 | /* deregister this driver with the USB subsystem */ | |
947 | usb_deregister(&peak_usb_driver); | |
948 | ||
949 | pr_info("%s: PCAN-USB interfaces driver unloaded\n", | |
950 | PCAN_USB_DRIVER_NAME); | |
951 | } | |
952 | ||
953 | module_init(peak_usb_init); | |
954 | module_exit(peak_usb_exit); |