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