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