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) { | |
bb478555 SG |
389 | usb_free_urb(urb); |
390 | err = -ENOMEM; | |
391 | break; | |
392 | } | |
393 | ||
394 | usb_fill_bulk_urb(urb, dev->udev, | |
395 | usb_rcvbulkpipe(dev->udev, dev->ep_msg_in), | |
396 | buf, dev->adapter->rx_buffer_size, | |
397 | peak_usb_read_bulk_callback, dev); | |
398 | ||
399 | /* ask last usb_free_urb() to also kfree() transfer_buffer */ | |
400 | urb->transfer_flags |= URB_FREE_BUFFER; | |
401 | usb_anchor_urb(urb, &dev->rx_submitted); | |
402 | ||
403 | err = usb_submit_urb(urb, GFP_KERNEL); | |
404 | if (err) { | |
405 | if (err == -ENODEV) | |
406 | netif_device_detach(dev->netdev); | |
407 | ||
408 | usb_unanchor_urb(urb); | |
409 | kfree(buf); | |
410 | usb_free_urb(urb); | |
411 | break; | |
412 | } | |
413 | ||
414 | /* drop reference, USB core will take care of freeing it */ | |
415 | usb_free_urb(urb); | |
416 | } | |
417 | ||
418 | /* did we submit any URBs? Warn if we was not able to submit all urbs */ | |
419 | if (i < PCAN_USB_MAX_RX_URBS) { | |
420 | if (i == 0) { | |
421 | netdev_err(netdev, "couldn't setup any rx URB\n"); | |
422 | return err; | |
423 | } | |
424 | ||
425 | netdev_warn(netdev, "rx performance may be slow\n"); | |
426 | } | |
427 | ||
428 | /* pre-alloc tx buffers and corresponding urbs */ | |
429 | for (i = 0; i < PCAN_USB_MAX_TX_URBS; i++) { | |
430 | struct peak_tx_urb_context *context; | |
431 | struct urb *urb; | |
432 | u8 *buf; | |
433 | ||
434 | /* create a URB and a buffer for it, to transmit usb messages */ | |
435 | urb = usb_alloc_urb(0, GFP_KERNEL); | |
436 | if (!urb) { | |
437 | netdev_err(netdev, "No memory left for URBs\n"); | |
438 | err = -ENOMEM; | |
439 | break; | |
440 | } | |
441 | ||
442 | buf = kmalloc(dev->adapter->tx_buffer_size, GFP_KERNEL); | |
443 | if (!buf) { | |
bb478555 SG |
444 | usb_free_urb(urb); |
445 | err = -ENOMEM; | |
446 | break; | |
447 | } | |
448 | ||
449 | context = dev->tx_contexts + i; | |
450 | context->dev = dev; | |
451 | context->urb = urb; | |
452 | ||
453 | usb_fill_bulk_urb(urb, dev->udev, | |
454 | usb_sndbulkpipe(dev->udev, dev->ep_msg_out), | |
455 | buf, dev->adapter->tx_buffer_size, | |
456 | peak_usb_write_bulk_callback, context); | |
457 | ||
458 | /* ask last usb_free_urb() to also kfree() transfer_buffer */ | |
459 | urb->transfer_flags |= URB_FREE_BUFFER; | |
460 | } | |
461 | ||
462 | /* warn if we were not able to allocate enough tx contexts */ | |
463 | if (i < PCAN_USB_MAX_TX_URBS) { | |
464 | if (i == 0) { | |
465 | netdev_err(netdev, "couldn't setup any tx URB\n"); | |
f37d6e70 | 466 | goto err_tx; |
bb478555 SG |
467 | } |
468 | ||
469 | netdev_warn(netdev, "tx performance may be slow\n"); | |
470 | } | |
471 | ||
472 | if (dev->adapter->dev_start) { | |
473 | err = dev->adapter->dev_start(dev); | |
474 | if (err) | |
f37d6e70 | 475 | goto err_adapter; |
bb478555 SG |
476 | } |
477 | ||
478 | dev->state |= PCAN_USB_STATE_STARTED; | |
479 | ||
480 | /* can set bus on now */ | |
481 | if (dev->adapter->dev_set_bus) { | |
482 | err = dev->adapter->dev_set_bus(dev, 1); | |
483 | if (err) | |
f37d6e70 | 484 | goto err_adapter; |
bb478555 SG |
485 | } |
486 | ||
487 | dev->can.state = CAN_STATE_ERROR_ACTIVE; | |
488 | ||
489 | return 0; | |
490 | ||
f37d6e70 | 491 | err_adapter: |
bb478555 SG |
492 | if (err == -ENODEV) |
493 | netif_device_detach(dev->netdev); | |
494 | ||
495 | netdev_warn(netdev, "couldn't submit control: %d\n", err); | |
496 | ||
f37d6e70 AK |
497 | for (i = 0; i < PCAN_USB_MAX_TX_URBS; i++) { |
498 | usb_free_urb(dev->tx_contexts[i].urb); | |
499 | dev->tx_contexts[i].urb = NULL; | |
500 | } | |
501 | err_tx: | |
502 | usb_kill_anchored_urbs(&dev->rx_submitted); | |
503 | ||
bb478555 SG |
504 | return err; |
505 | } | |
506 | ||
507 | /* | |
508 | * called by netdev to open the corresponding CAN interface. | |
509 | */ | |
510 | static int peak_usb_ndo_open(struct net_device *netdev) | |
511 | { | |
512 | struct peak_usb_device *dev = netdev_priv(netdev); | |
513 | int err; | |
514 | ||
515 | /* common open */ | |
516 | err = open_candev(netdev); | |
517 | if (err) | |
518 | return err; | |
519 | ||
520 | /* finally start device */ | |
521 | err = peak_usb_start(dev); | |
522 | if (err) { | |
523 | netdev_err(netdev, "couldn't start device: %d\n", err); | |
524 | close_candev(netdev); | |
525 | return err; | |
526 | } | |
527 | ||
bb478555 SG |
528 | netif_start_queue(netdev); |
529 | ||
530 | return 0; | |
531 | } | |
532 | ||
533 | /* | |
534 | * unlink in-flight Rx and Tx urbs and free their memory. | |
535 | */ | |
536 | static void peak_usb_unlink_all_urbs(struct peak_usb_device *dev) | |
537 | { | |
538 | int i; | |
539 | ||
540 | /* free all Rx (submitted) urbs */ | |
541 | usb_kill_anchored_urbs(&dev->rx_submitted); | |
542 | ||
543 | /* free unsubmitted Tx urbs first */ | |
544 | for (i = 0; i < PCAN_USB_MAX_TX_URBS; i++) { | |
545 | struct urb *urb = dev->tx_contexts[i].urb; | |
546 | ||
547 | if (!urb || | |
548 | dev->tx_contexts[i].echo_index != PCAN_USB_MAX_TX_URBS) { | |
549 | /* | |
550 | * this urb is already released or always submitted, | |
551 | * let usb core free by itself | |
552 | */ | |
553 | continue; | |
554 | } | |
555 | ||
556 | usb_free_urb(urb); | |
557 | dev->tx_contexts[i].urb = NULL; | |
558 | } | |
559 | ||
560 | /* then free all submitted Tx urbs */ | |
561 | usb_kill_anchored_urbs(&dev->tx_submitted); | |
562 | atomic_set(&dev->active_tx_urbs, 0); | |
563 | } | |
564 | ||
565 | /* | |
566 | * called by netdev to close the corresponding CAN interface. | |
567 | */ | |
568 | static int peak_usb_ndo_stop(struct net_device *netdev) | |
569 | { | |
570 | struct peak_usb_device *dev = netdev_priv(netdev); | |
571 | ||
572 | dev->state &= ~PCAN_USB_STATE_STARTED; | |
573 | netif_stop_queue(netdev); | |
574 | ||
575 | /* unlink all pending urbs and free used memory */ | |
576 | peak_usb_unlink_all_urbs(dev); | |
577 | ||
578 | if (dev->adapter->dev_stop) | |
579 | dev->adapter->dev_stop(dev); | |
580 | ||
581 | close_candev(netdev); | |
582 | ||
bb478555 SG |
583 | dev->can.state = CAN_STATE_STOPPED; |
584 | ||
585 | /* can set bus off now */ | |
586 | if (dev->adapter->dev_set_bus) { | |
587 | int err = dev->adapter->dev_set_bus(dev, 0); | |
588 | if (err) | |
589 | return err; | |
590 | } | |
591 | ||
592 | return 0; | |
593 | } | |
594 | ||
595 | /* | |
596 | * handle end of waiting for the device to reset | |
597 | */ | |
598 | void peak_usb_restart_complete(struct peak_usb_device *dev) | |
599 | { | |
600 | /* finally MUST update can state */ | |
601 | dev->can.state = CAN_STATE_ERROR_ACTIVE; | |
602 | ||
603 | /* netdev queue can be awaken now */ | |
604 | netif_wake_queue(dev->netdev); | |
605 | } | |
606 | ||
607 | void peak_usb_async_complete(struct urb *urb) | |
608 | { | |
609 | kfree(urb->transfer_buffer); | |
610 | usb_free_urb(urb); | |
611 | } | |
612 | ||
613 | /* | |
614 | * device (auto-)restart mechanism runs in a timer context => | |
615 | * MUST handle restart with asynchronous usb transfers | |
616 | */ | |
617 | static int peak_usb_restart(struct peak_usb_device *dev) | |
618 | { | |
619 | struct urb *urb; | |
620 | int err; | |
621 | u8 *buf; | |
622 | ||
623 | /* | |
624 | * if device doesn't define any asynchronous restart handler, simply | |
625 | * wake the netdev queue up | |
626 | */ | |
627 | if (!dev->adapter->dev_restart_async) { | |
628 | peak_usb_restart_complete(dev); | |
629 | return 0; | |
630 | } | |
631 | ||
632 | /* first allocate a urb to handle the asynchronous steps */ | |
633 | urb = usb_alloc_urb(0, GFP_ATOMIC); | |
634 | if (!urb) { | |
635 | netdev_err(dev->netdev, "no memory left for urb\n"); | |
636 | return -ENOMEM; | |
637 | } | |
638 | ||
639 | /* also allocate enough space for the commands to send */ | |
640 | buf = kmalloc(PCAN_USB_MAX_CMD_LEN, GFP_ATOMIC); | |
641 | if (!buf) { | |
bb478555 SG |
642 | usb_free_urb(urb); |
643 | return -ENOMEM; | |
644 | } | |
645 | ||
646 | /* call the device specific handler for the restart */ | |
647 | err = dev->adapter->dev_restart_async(dev, urb, buf); | |
648 | if (!err) | |
649 | return 0; | |
650 | ||
651 | kfree(buf); | |
652 | usb_free_urb(urb); | |
653 | ||
654 | return err; | |
655 | } | |
656 | ||
657 | /* | |
658 | * candev callback used to change CAN mode. | |
659 | * Warning: this is called from a timer context! | |
660 | */ | |
661 | static int peak_usb_set_mode(struct net_device *netdev, enum can_mode mode) | |
662 | { | |
663 | struct peak_usb_device *dev = netdev_priv(netdev); | |
664 | int err = 0; | |
665 | ||
bb478555 SG |
666 | switch (mode) { |
667 | case CAN_MODE_START: | |
668 | err = peak_usb_restart(dev); | |
669 | if (err) | |
670 | netdev_err(netdev, "couldn't start device (err %d)\n", | |
671 | err); | |
672 | break; | |
673 | ||
674 | default: | |
675 | return -EOPNOTSUPP; | |
676 | } | |
677 | ||
678 | return err; | |
679 | } | |
680 | ||
681 | /* | |
682 | * candev callback used to set device bitrate. | |
683 | */ | |
684 | static int peak_usb_set_bittiming(struct net_device *netdev) | |
685 | { | |
686 | struct peak_usb_device *dev = netdev_priv(netdev); | |
687 | struct can_bittiming *bt = &dev->can.bittiming; | |
688 | ||
689 | if (dev->adapter->dev_set_bittiming) { | |
690 | int err = dev->adapter->dev_set_bittiming(dev, bt); | |
691 | ||
692 | if (err) | |
693 | netdev_info(netdev, "couldn't set bitrate (err %d)\n", | |
694 | err); | |
695 | return err; | |
696 | } | |
697 | ||
698 | return 0; | |
699 | } | |
700 | ||
701 | static const struct net_device_ops peak_usb_netdev_ops = { | |
702 | .ndo_open = peak_usb_ndo_open, | |
703 | .ndo_stop = peak_usb_ndo_stop, | |
704 | .ndo_start_xmit = peak_usb_ndo_start_xmit, | |
705 | }; | |
706 | ||
707 | /* | |
708 | * create one device which is attached to CAN controller #ctrl_idx of the | |
709 | * usb adapter. | |
710 | */ | |
711 | static int peak_usb_create_dev(struct peak_usb_adapter *peak_usb_adapter, | |
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 SG |
736 | err = -ENOMEM; |
737 | goto lbl_set_intf_data; | |
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; | |
753 | dev->can.ctrlmode_supported = CAN_CTRLMODE_3_SAMPLES | | |
754 | CAN_CTRLMODE_LISTENONLY; | |
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); | |
772 | ||
773 | err = register_candev(netdev); | |
774 | if (err) { | |
775 | dev_err(&intf->dev, "couldn't register CAN device: %d\n", err); | |
776 | goto lbl_free_cmd_buf; | |
777 | } | |
778 | ||
779 | if (dev->prev_siblings) | |
780 | (dev->prev_siblings)->next_siblings = dev; | |
781 | ||
782 | /* keep hw revision into the netdevice */ | |
783 | tmp16 = le16_to_cpu(usb_dev->descriptor.bcdDevice); | |
784 | dev->device_rev = tmp16 >> 8; | |
785 | ||
786 | if (dev->adapter->dev_init) { | |
787 | err = dev->adapter->dev_init(dev); | |
788 | if (err) | |
789 | goto lbl_free_cmd_buf; | |
790 | } | |
791 | ||
792 | /* set bus off */ | |
793 | if (dev->adapter->dev_set_bus) { | |
794 | err = dev->adapter->dev_set_bus(dev, 0); | |
795 | if (err) | |
796 | goto lbl_free_cmd_buf; | |
797 | } | |
798 | ||
799 | /* get device number early */ | |
800 | if (dev->adapter->dev_get_device_id) | |
801 | dev->adapter->dev_get_device_id(dev, &dev->device_number); | |
802 | ||
803 | netdev_info(netdev, "attached to %s channel %u (device %u)\n", | |
804 | peak_usb_adapter->name, ctrl_idx, dev->device_number); | |
805 | ||
806 | return 0; | |
807 | ||
808 | lbl_free_cmd_buf: | |
809 | kfree(dev->cmd_buf); | |
810 | ||
811 | lbl_set_intf_data: | |
812 | usb_set_intfdata(intf, dev->prev_siblings); | |
813 | free_candev(netdev); | |
814 | ||
815 | return err; | |
816 | } | |
817 | ||
818 | /* | |
819 | * called by the usb core when the device is unplugged from the system | |
820 | */ | |
821 | static void peak_usb_disconnect(struct usb_interface *intf) | |
822 | { | |
823 | struct peak_usb_device *dev; | |
824 | ||
825 | /* unregister as many netdev devices as siblings */ | |
826 | for (dev = usb_get_intfdata(intf); dev; dev = dev->prev_siblings) { | |
827 | struct net_device *netdev = dev->netdev; | |
828 | char name[IFNAMSIZ]; | |
829 | ||
830 | dev->state &= ~PCAN_USB_STATE_CONNECTED; | |
831 | strncpy(name, netdev->name, IFNAMSIZ); | |
832 | ||
833 | unregister_netdev(netdev); | |
834 | free_candev(netdev); | |
835 | ||
836 | kfree(dev->cmd_buf); | |
837 | dev->next_siblings = NULL; | |
838 | if (dev->adapter->dev_free) | |
839 | dev->adapter->dev_free(dev); | |
840 | ||
841 | dev_info(&intf->dev, "%s removed\n", name); | |
842 | } | |
843 | ||
844 | usb_set_intfdata(intf, NULL); | |
845 | } | |
846 | ||
847 | /* | |
848 | * probe function for new PEAK-System devices | |
849 | */ | |
850 | static int peak_usb_probe(struct usb_interface *intf, | |
851 | const struct usb_device_id *id) | |
852 | { | |
853 | struct usb_device *usb_dev = interface_to_usbdev(intf); | |
854 | struct peak_usb_adapter *peak_usb_adapter, **pp; | |
855 | int i, err = -ENOMEM; | |
856 | ||
857 | usb_dev = interface_to_usbdev(intf); | |
858 | ||
859 | /* get corresponding PCAN-USB adapter */ | |
860 | for (pp = peak_usb_adapters_list; *pp; pp++) | |
861 | if ((*pp)->device_id == usb_dev->descriptor.idProduct) | |
862 | break; | |
863 | ||
864 | peak_usb_adapter = *pp; | |
865 | if (!peak_usb_adapter) { | |
866 | /* should never come except device_id bad usage in this file */ | |
867 | pr_err("%s: didn't find device id. 0x%x in devices list\n", | |
868 | PCAN_USB_DRIVER_NAME, usb_dev->descriptor.idProduct); | |
869 | return -ENODEV; | |
870 | } | |
871 | ||
872 | /* got corresponding adapter: check if it handles current interface */ | |
873 | if (peak_usb_adapter->intf_probe) { | |
874 | err = peak_usb_adapter->intf_probe(intf); | |
875 | if (err) | |
876 | return err; | |
877 | } | |
878 | ||
879 | for (i = 0; i < peak_usb_adapter->ctrl_count; i++) { | |
880 | err = peak_usb_create_dev(peak_usb_adapter, intf, i); | |
881 | if (err) { | |
882 | /* deregister already created devices */ | |
883 | peak_usb_disconnect(intf); | |
884 | break; | |
885 | } | |
886 | } | |
887 | ||
888 | return err; | |
889 | } | |
890 | ||
891 | /* usb specific object needed to register this driver with the usb subsystem */ | |
892 | static struct usb_driver peak_usb_driver = { | |
893 | .name = PCAN_USB_DRIVER_NAME, | |
894 | .disconnect = peak_usb_disconnect, | |
895 | .probe = peak_usb_probe, | |
896 | .id_table = peak_usb_table, | |
897 | }; | |
898 | ||
899 | static int __init peak_usb_init(void) | |
900 | { | |
901 | int err; | |
902 | ||
903 | /* register this driver with the USB subsystem */ | |
904 | err = usb_register(&peak_usb_driver); | |
905 | if (err) | |
906 | pr_err("%s: usb_register failed (err %d)\n", | |
907 | PCAN_USB_DRIVER_NAME, err); | |
908 | ||
909 | return err; | |
910 | } | |
911 | ||
912 | static int peak_usb_do_device_exit(struct device *d, void *arg) | |
913 | { | |
914 | struct usb_interface *intf = to_usb_interface(d); | |
915 | struct peak_usb_device *dev; | |
916 | ||
917 | /* stop as many netdev devices as siblings */ | |
918 | for (dev = usb_get_intfdata(intf); dev; dev = dev->prev_siblings) { | |
919 | struct net_device *netdev = dev->netdev; | |
920 | ||
921 | if (netif_device_present(netdev)) | |
922 | if (dev->adapter->dev_exit) | |
923 | dev->adapter->dev_exit(dev); | |
924 | } | |
925 | ||
926 | return 0; | |
927 | } | |
928 | ||
929 | static void __exit peak_usb_exit(void) | |
930 | { | |
931 | int err; | |
932 | ||
933 | /* last chance do send any synchronous commands here */ | |
934 | err = driver_for_each_device(&peak_usb_driver.drvwrap.driver, NULL, | |
935 | NULL, peak_usb_do_device_exit); | |
936 | if (err) | |
937 | pr_err("%s: failed to stop all can devices (err %d)\n", | |
938 | PCAN_USB_DRIVER_NAME, err); | |
939 | ||
940 | /* deregister this driver with the USB subsystem */ | |
941 | usb_deregister(&peak_usb_driver); | |
942 | ||
943 | pr_info("%s: PCAN-USB interfaces driver unloaded\n", | |
944 | PCAN_USB_DRIVER_NAME); | |
945 | } | |
946 | ||
947 | module_init(peak_usb_init); | |
948 | module_exit(peak_usb_exit); |