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