1 // SPDX-License-Identifier: GPL-2.0-only
2 /* CAN driver for Geschwister Schneider USB/CAN devices
3 * and bytewerk.org candleLight USB CAN interfaces.
4 *
5 * Copyright (C) 2013-2016 Geschwister Schneider Technologie-,
6 * Entwicklungs- und Vertriebs UG (Haftungsbeschränkt).
7 * Copyright (C) 2016 Hubert Denkmair
8 * Copyright (c) 2023 Pengutronix, Marc Kleine-Budde <kernel@pengutronix.de>
9 *
10 * Many thanks to all socketcan devs!
11 */
12
13 #include <linux/bitfield.h>
14 #include <linux/clocksource.h>
15 #include <linux/ethtool.h>
16 #include <linux/init.h>
17 #include <linux/module.h>
18 #include <linux/netdevice.h>
19 #include <linux/signal.h>
20 #include <linux/timecounter.h>
21 #include <linux/units.h>
22 #include <linux/usb.h>
23 #include <linux/workqueue.h>
24
25 #include <linux/can.h>
26 #include <linux/can/dev.h>
27 #include <linux/can/error.h>
28 #include <linux/can/rx-offload.h>
29
30 /* Device specific constants */
31 #define USB_GS_USB_1_VENDOR_ID 0x1d50
32 #define USB_GS_USB_1_PRODUCT_ID 0x606f
33
34 #define USB_CANDLELIGHT_VENDOR_ID 0x1209
35 #define USB_CANDLELIGHT_PRODUCT_ID 0x2323
36
37 #define USB_CES_CANEXT_FD_VENDOR_ID 0x1cd2
38 #define USB_CES_CANEXT_FD_PRODUCT_ID 0x606f
39
40 #define USB_ABE_CANDEBUGGER_FD_VENDOR_ID 0x16d0
41 #define USB_ABE_CANDEBUGGER_FD_PRODUCT_ID 0x10b8
42
43 #define USB_XYLANTA_SAINT3_VENDOR_ID 0x16d0
44 #define USB_XYLANTA_SAINT3_PRODUCT_ID 0x0f30
45
46 #define USB_CANNECTIVITY_VENDOR_ID 0x1209
47 #define USB_CANNECTIVITY_PRODUCT_ID 0xca01
48
49 /* Timestamp 32 bit timer runs at 1 MHz (1 µs tick). Worker accounts
50 * for timer overflow (will be after ~71 minutes)
51 */
52 #define GS_USB_TIMESTAMP_TIMER_HZ (1 * HZ_PER_MHZ)
53 #define GS_USB_TIMESTAMP_WORK_DELAY_SEC 1800
54 static_assert(GS_USB_TIMESTAMP_WORK_DELAY_SEC <
55 CYCLECOUNTER_MASK(32) / GS_USB_TIMESTAMP_TIMER_HZ / 2);
56
57 /* Device specific constants */
58 enum gs_usb_breq {
59 GS_USB_BREQ_HOST_FORMAT = 0,
60 GS_USB_BREQ_BITTIMING,
61 GS_USB_BREQ_MODE,
62 GS_USB_BREQ_BERR,
63 GS_USB_BREQ_BT_CONST,
64 GS_USB_BREQ_DEVICE_CONFIG,
65 GS_USB_BREQ_TIMESTAMP,
66 GS_USB_BREQ_IDENTIFY,
67 GS_USB_BREQ_GET_USER_ID,
68 GS_USB_BREQ_QUIRK_CANTACT_PRO_DATA_BITTIMING = GS_USB_BREQ_GET_USER_ID,
69 GS_USB_BREQ_SET_USER_ID,
70 GS_USB_BREQ_DATA_BITTIMING,
71 GS_USB_BREQ_BT_CONST_EXT,
72 GS_USB_BREQ_SET_TERMINATION,
73 GS_USB_BREQ_GET_TERMINATION,
74 GS_USB_BREQ_GET_STATE,
75 };
76
77 enum gs_can_mode {
78 /* reset a channel. turns it off */
79 GS_CAN_MODE_RESET = 0,
80 /* starts a channel */
81 GS_CAN_MODE_START
82 };
83
84 enum gs_can_state {
85 GS_CAN_STATE_ERROR_ACTIVE = 0,
86 GS_CAN_STATE_ERROR_WARNING,
87 GS_CAN_STATE_ERROR_PASSIVE,
88 GS_CAN_STATE_BUS_OFF,
89 GS_CAN_STATE_STOPPED,
90 GS_CAN_STATE_SLEEPING
91 };
92
93 enum gs_can_identify_mode {
94 GS_CAN_IDENTIFY_OFF = 0,
95 GS_CAN_IDENTIFY_ON
96 };
97
98 enum gs_can_termination_state {
99 GS_CAN_TERMINATION_STATE_OFF = 0,
100 GS_CAN_TERMINATION_STATE_ON
101 };
102
103 #define GS_USB_TERMINATION_DISABLED CAN_TERMINATION_DISABLED
104 #define GS_USB_TERMINATION_ENABLED 120
105
106 /* data types passed between host and device */
107
108 /* The firmware on the original USB2CAN by Geschwister Schneider
109 * Technologie Entwicklungs- und Vertriebs UG exchanges all data
110 * between the host and the device in host byte order. This is done
111 * with the struct gs_host_config::byte_order member, which is sent
112 * first to indicate the desired byte order.
113 *
114 * The widely used open source firmware candleLight doesn't support
115 * this feature and exchanges the data in little endian byte order.
116 */
117 struct gs_host_config {
118 __le32 byte_order;
119 } __packed;
120
121 struct gs_device_config {
122 u8 reserved1;
123 u8 reserved2;
124 u8 reserved3;
125 u8 icount;
126 __le32 sw_version;
127 __le32 hw_version;
128 } __packed;
129
130 #define GS_CAN_MODE_NORMAL 0
131 #define GS_CAN_MODE_LISTEN_ONLY BIT(0)
132 #define GS_CAN_MODE_LOOP_BACK BIT(1)
133 #define GS_CAN_MODE_TRIPLE_SAMPLE BIT(2)
134 #define GS_CAN_MODE_ONE_SHOT BIT(3)
135 #define GS_CAN_MODE_HW_TIMESTAMP BIT(4)
136 /* GS_CAN_FEATURE_IDENTIFY BIT(5) */
137 /* GS_CAN_FEATURE_USER_ID BIT(6) */
138 #define GS_CAN_MODE_PAD_PKTS_TO_MAX_PKT_SIZE BIT(7)
139 #define GS_CAN_MODE_FD BIT(8)
140 /* GS_CAN_FEATURE_REQ_USB_QUIRK_LPC546XX BIT(9) */
141 /* GS_CAN_FEATURE_BT_CONST_EXT BIT(10) */
142 /* GS_CAN_FEATURE_TERMINATION BIT(11) */
143 #define GS_CAN_MODE_BERR_REPORTING BIT(12)
144 /* GS_CAN_FEATURE_GET_STATE BIT(13) */
145
146 struct gs_device_mode {
147 __le32 mode;
148 __le32 flags;
149 } __packed;
150
151 struct gs_device_state {
152 __le32 state;
153 __le32 rxerr;
154 __le32 txerr;
155 } __packed;
156
157 struct gs_device_bittiming {
158 __le32 prop_seg;
159 __le32 phase_seg1;
160 __le32 phase_seg2;
161 __le32 sjw;
162 __le32 brp;
163 } __packed;
164
165 struct gs_identify_mode {
166 __le32 mode;
167 } __packed;
168
169 struct gs_device_termination_state {
170 __le32 state;
171 } __packed;
172
173 #define GS_CAN_FEATURE_LISTEN_ONLY BIT(0)
174 #define GS_CAN_FEATURE_LOOP_BACK BIT(1)
175 #define GS_CAN_FEATURE_TRIPLE_SAMPLE BIT(2)
176 #define GS_CAN_FEATURE_ONE_SHOT BIT(3)
177 #define GS_CAN_FEATURE_HW_TIMESTAMP BIT(4)
178 #define GS_CAN_FEATURE_IDENTIFY BIT(5)
179 #define GS_CAN_FEATURE_USER_ID BIT(6)
180 #define GS_CAN_FEATURE_PAD_PKTS_TO_MAX_PKT_SIZE BIT(7)
181 #define GS_CAN_FEATURE_FD BIT(8)
182 #define GS_CAN_FEATURE_REQ_USB_QUIRK_LPC546XX BIT(9)
183 #define GS_CAN_FEATURE_BT_CONST_EXT BIT(10)
184 #define GS_CAN_FEATURE_TERMINATION BIT(11)
185 #define GS_CAN_FEATURE_BERR_REPORTING BIT(12)
186 #define GS_CAN_FEATURE_GET_STATE BIT(13)
187 #define GS_CAN_FEATURE_MASK GENMASK(13, 0)
188
189 /* internal quirks - keep in GS_CAN_FEATURE space for now */
190
191 /* CANtact Pro original firmware:
192 * BREQ DATA_BITTIMING overlaps with GET_USER_ID
193 */
194 #define GS_CAN_FEATURE_QUIRK_BREQ_CANTACT_PRO BIT(31)
195
196 struct gs_device_bt_const {
197 __le32 feature;
198 __le32 fclk_can;
199 __le32 tseg1_min;
200 __le32 tseg1_max;
201 __le32 tseg2_min;
202 __le32 tseg2_max;
203 __le32 sjw_max;
204 __le32 brp_min;
205 __le32 brp_max;
206 __le32 brp_inc;
207 } __packed;
208
209 struct gs_device_bt_const_extended {
210 __le32 feature;
211 __le32 fclk_can;
212 __le32 tseg1_min;
213 __le32 tseg1_max;
214 __le32 tseg2_min;
215 __le32 tseg2_max;
216 __le32 sjw_max;
217 __le32 brp_min;
218 __le32 brp_max;
219 __le32 brp_inc;
220
221 __le32 dtseg1_min;
222 __le32 dtseg1_max;
223 __le32 dtseg2_min;
224 __le32 dtseg2_max;
225 __le32 dsjw_max;
226 __le32 dbrp_min;
227 __le32 dbrp_max;
228 __le32 dbrp_inc;
229 } __packed;
230
231 #define GS_CAN_FLAG_OVERFLOW BIT(0)
232 #define GS_CAN_FLAG_FD BIT(1)
233 #define GS_CAN_FLAG_BRS BIT(2)
234 #define GS_CAN_FLAG_ESI BIT(3)
235
236 struct classic_can {
237 u8 data[8];
238 } __packed;
239
240 struct classic_can_ts {
241 u8 data[8];
242 __le32 timestamp_us;
243 } __packed;
244
245 struct classic_can_quirk {
246 u8 data[8];
247 u8 quirk;
248 } __packed;
249
250 struct canfd {
251 u8 data[64];
252 } __packed;
253
254 struct canfd_ts {
255 u8 data[64];
256 __le32 timestamp_us;
257 } __packed;
258
259 struct canfd_quirk {
260 u8 data[64];
261 u8 quirk;
262 } __packed;
263
264 /* struct gs_host_frame::echo_id == GS_HOST_FRAME_ECHO_ID_RX indicates
265 * a regular RX'ed CAN frame
266 */
267 #define GS_HOST_FRAME_ECHO_ID_RX 0xffffffff
268
269 struct gs_host_frame {
270 struct_group(header,
271 u32 echo_id;
272 __le32 can_id;
273
274 u8 can_dlc;
275 u8 channel;
276 u8 flags;
277 u8 reserved;
278 );
279
280 union {
281 DECLARE_FLEX_ARRAY(struct classic_can, classic_can);
282 DECLARE_FLEX_ARRAY(struct classic_can_ts, classic_can_ts);
283 DECLARE_FLEX_ARRAY(struct classic_can_quirk, classic_can_quirk);
284 DECLARE_FLEX_ARRAY(struct canfd, canfd);
285 DECLARE_FLEX_ARRAY(struct canfd_ts, canfd_ts);
286 DECLARE_FLEX_ARRAY(struct canfd_quirk, canfd_quirk);
287 };
288 } __packed;
289 /* The GS USB devices make use of the same flags and masks as in
290 * linux/can.h and linux/can/error.h, and no additional mapping is necessary.
291 */
292
293 /* Only send a max of GS_MAX_TX_URBS frames per channel at a time. */
294 #define GS_MAX_TX_URBS 10
295 /* Only launch a max of GS_MAX_RX_URBS usb requests at a time. */
296 #define GS_MAX_RX_URBS 30
297 #define GS_NAPI_WEIGHT 32
298
299 struct gs_tx_context {
300 struct gs_can *dev;
301 unsigned int echo_id;
302 };
303
304 struct gs_can {
305 struct can_priv can; /* must be the first member */
306
307 struct can_rx_offload offload;
308 struct gs_usb *parent;
309
310 struct net_device *netdev;
311 struct usb_device *udev;
312
313 struct can_bittiming_const bt_const, data_bt_const;
314 unsigned int channel; /* channel number */
315
316 u32 feature;
317 unsigned int hf_size_tx;
318
319 /* This lock prevents a race condition between xmit and receive. */
320 spinlock_t tx_ctx_lock;
321 struct gs_tx_context tx_context[GS_MAX_TX_URBS];
322
323 struct usb_anchor tx_submitted;
324 atomic_t active_tx_urbs;
325 };
326
327 /* usb interface struct */
328 struct gs_usb {
329 struct usb_anchor rx_submitted;
330 struct usb_device *udev;
331
332 /* time counter for hardware timestamps */
333 struct cyclecounter cc;
334 struct timecounter tc;
335 spinlock_t tc_lock; /* spinlock to guard access tc->cycle_last */
336 struct delayed_work timestamp;
337
338 unsigned int hf_size_rx;
339 u8 active_channels;
340 u8 channel_cnt;
341
342 unsigned int pipe_in;
343 unsigned int pipe_out;
344 struct gs_can *canch[] __counted_by(channel_cnt);
345 };
346
347 /* 'allocate' a tx context.
348 * returns a valid tx context or NULL if there is no space.
349 */
gs_alloc_tx_context(struct gs_can * dev)350 static struct gs_tx_context *gs_alloc_tx_context(struct gs_can *dev)
351 {
352 int i = 0;
353 unsigned long flags;
354
355 spin_lock_irqsave(&dev->tx_ctx_lock, flags);
356
357 for (; i < GS_MAX_TX_URBS; i++) {
358 if (dev->tx_context[i].echo_id == GS_MAX_TX_URBS) {
359 dev->tx_context[i].echo_id = i;
360 spin_unlock_irqrestore(&dev->tx_ctx_lock, flags);
361 return &dev->tx_context[i];
362 }
363 }
364
365 spin_unlock_irqrestore(&dev->tx_ctx_lock, flags);
366 return NULL;
367 }
368
369 /* releases a tx context
370 */
gs_free_tx_context(struct gs_tx_context * txc)371 static void gs_free_tx_context(struct gs_tx_context *txc)
372 {
373 txc->echo_id = GS_MAX_TX_URBS;
374 }
375
376 /* Get a tx context by id.
377 */
gs_get_tx_context(struct gs_can * dev,unsigned int id)378 static struct gs_tx_context *gs_get_tx_context(struct gs_can *dev,
379 unsigned int id)
380 {
381 unsigned long flags;
382
383 if (id < GS_MAX_TX_URBS) {
384 spin_lock_irqsave(&dev->tx_ctx_lock, flags);
385 if (dev->tx_context[id].echo_id == id) {
386 spin_unlock_irqrestore(&dev->tx_ctx_lock, flags);
387 return &dev->tx_context[id];
388 }
389 spin_unlock_irqrestore(&dev->tx_ctx_lock, flags);
390 }
391 return NULL;
392 }
393
gs_cmd_reset(struct gs_can * dev)394 static int gs_cmd_reset(struct gs_can *dev)
395 {
396 struct gs_device_mode dm = {
397 .mode = cpu_to_le32(GS_CAN_MODE_RESET),
398 };
399
400 return usb_control_msg_send(dev->udev, 0, GS_USB_BREQ_MODE,
401 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
402 dev->channel, 0, &dm, sizeof(dm), 1000,
403 GFP_KERNEL);
404 }
405
gs_usb_get_timestamp(const struct gs_usb * parent,u32 * timestamp_p)406 static inline int gs_usb_get_timestamp(const struct gs_usb *parent,
407 u32 *timestamp_p)
408 {
409 __le32 timestamp;
410 int rc;
411
412 rc = usb_control_msg_recv(parent->udev, 0, GS_USB_BREQ_TIMESTAMP,
413 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
414 0, 0,
415 ×tamp, sizeof(timestamp),
416 USB_CTRL_GET_TIMEOUT,
417 GFP_KERNEL);
418 if (rc)
419 return rc;
420
421 *timestamp_p = le32_to_cpu(timestamp);
422
423 return 0;
424 }
425
gs_usb_timestamp_read(struct cyclecounter * cc)426 static u64 gs_usb_timestamp_read(struct cyclecounter *cc) __must_hold(&dev->tc_lock)
427 {
428 struct gs_usb *parent = container_of(cc, struct gs_usb, cc);
429 u32 timestamp = 0;
430 int err;
431
432 lockdep_assert_held(&parent->tc_lock);
433
434 /* drop lock for synchronous USB transfer */
435 spin_unlock_bh(&parent->tc_lock);
436 err = gs_usb_get_timestamp(parent, ×tamp);
437 spin_lock_bh(&parent->tc_lock);
438 if (err)
439 dev_err(&parent->udev->dev,
440 "Error %d while reading timestamp. HW timestamps may be inaccurate.",
441 err);
442
443 return timestamp;
444 }
445
gs_usb_timestamp_work(struct work_struct * work)446 static void gs_usb_timestamp_work(struct work_struct *work)
447 {
448 struct delayed_work *delayed_work = to_delayed_work(work);
449 struct gs_usb *parent;
450
451 parent = container_of(delayed_work, struct gs_usb, timestamp);
452 spin_lock_bh(&parent->tc_lock);
453 timecounter_read(&parent->tc);
454 spin_unlock_bh(&parent->tc_lock);
455
456 schedule_delayed_work(&parent->timestamp,
457 GS_USB_TIMESTAMP_WORK_DELAY_SEC * HZ);
458 }
459
gs_usb_skb_set_timestamp(struct gs_can * dev,struct sk_buff * skb,u32 timestamp)460 static void gs_usb_skb_set_timestamp(struct gs_can *dev,
461 struct sk_buff *skb, u32 timestamp)
462 {
463 struct skb_shared_hwtstamps *hwtstamps = skb_hwtstamps(skb);
464 struct gs_usb *parent = dev->parent;
465 u64 ns;
466
467 spin_lock_bh(&parent->tc_lock);
468 ns = timecounter_cyc2time(&parent->tc, timestamp);
469 spin_unlock_bh(&parent->tc_lock);
470
471 hwtstamps->hwtstamp = ns_to_ktime(ns);
472 }
473
gs_usb_timestamp_init(struct gs_usb * parent)474 static void gs_usb_timestamp_init(struct gs_usb *parent)
475 {
476 struct cyclecounter *cc = &parent->cc;
477
478 cc->read = gs_usb_timestamp_read;
479 cc->mask = CYCLECOUNTER_MASK(32);
480 cc->shift = 32 - bits_per(NSEC_PER_SEC / GS_USB_TIMESTAMP_TIMER_HZ);
481 cc->mult = clocksource_hz2mult(GS_USB_TIMESTAMP_TIMER_HZ, cc->shift);
482
483 spin_lock_init(&parent->tc_lock);
484 spin_lock_bh(&parent->tc_lock);
485 timecounter_init(&parent->tc, &parent->cc, ktime_get_real_ns());
486 spin_unlock_bh(&parent->tc_lock);
487
488 INIT_DELAYED_WORK(&parent->timestamp, gs_usb_timestamp_work);
489 schedule_delayed_work(&parent->timestamp,
490 GS_USB_TIMESTAMP_WORK_DELAY_SEC * HZ);
491 }
492
gs_usb_timestamp_stop(struct gs_usb * parent)493 static void gs_usb_timestamp_stop(struct gs_usb *parent)
494 {
495 cancel_delayed_work_sync(&parent->timestamp);
496 }
497
gs_update_state(struct gs_can * dev,struct can_frame * cf)498 static void gs_update_state(struct gs_can *dev, struct can_frame *cf)
499 {
500 struct can_device_stats *can_stats = &dev->can.can_stats;
501
502 if (cf->can_id & CAN_ERR_RESTARTED) {
503 dev->can.state = CAN_STATE_ERROR_ACTIVE;
504 can_stats->restarts++;
505 } else if (cf->can_id & CAN_ERR_BUSOFF) {
506 dev->can.state = CAN_STATE_BUS_OFF;
507 can_stats->bus_off++;
508 } else if (cf->can_id & CAN_ERR_CRTL) {
509 if ((cf->data[1] & CAN_ERR_CRTL_TX_WARNING) ||
510 (cf->data[1] & CAN_ERR_CRTL_RX_WARNING)) {
511 dev->can.state = CAN_STATE_ERROR_WARNING;
512 can_stats->error_warning++;
513 } else if ((cf->data[1] & CAN_ERR_CRTL_TX_PASSIVE) ||
514 (cf->data[1] & CAN_ERR_CRTL_RX_PASSIVE)) {
515 dev->can.state = CAN_STATE_ERROR_PASSIVE;
516 can_stats->error_passive++;
517 } else {
518 dev->can.state = CAN_STATE_ERROR_ACTIVE;
519 }
520 }
521 }
522
gs_usb_set_timestamp(struct gs_can * dev,struct sk_buff * skb,const struct gs_host_frame * hf)523 static u32 gs_usb_set_timestamp(struct gs_can *dev, struct sk_buff *skb,
524 const struct gs_host_frame *hf)
525 {
526 u32 timestamp;
527
528 if (hf->flags & GS_CAN_FLAG_FD)
529 timestamp = le32_to_cpu(hf->canfd_ts->timestamp_us);
530 else
531 timestamp = le32_to_cpu(hf->classic_can_ts->timestamp_us);
532
533 if (skb)
534 gs_usb_skb_set_timestamp(dev, skb, timestamp);
535
536 return timestamp;
537 }
538
gs_usb_rx_offload(struct gs_can * dev,struct sk_buff * skb,const struct gs_host_frame * hf)539 static void gs_usb_rx_offload(struct gs_can *dev, struct sk_buff *skb,
540 const struct gs_host_frame *hf)
541 {
542 struct can_rx_offload *offload = &dev->offload;
543 int rc;
544
545 if (dev->feature & GS_CAN_FEATURE_HW_TIMESTAMP) {
546 const u32 ts = gs_usb_set_timestamp(dev, skb, hf);
547
548 rc = can_rx_offload_queue_timestamp(offload, skb, ts);
549 } else {
550 rc = can_rx_offload_queue_tail(offload, skb);
551 }
552
553 if (rc)
554 dev->netdev->stats.rx_fifo_errors++;
555 }
556
557 static unsigned int
gs_usb_get_echo_skb(struct gs_can * dev,struct sk_buff * skb,const struct gs_host_frame * hf)558 gs_usb_get_echo_skb(struct gs_can *dev, struct sk_buff *skb,
559 const struct gs_host_frame *hf)
560 {
561 struct can_rx_offload *offload = &dev->offload;
562 const u32 echo_id = hf->echo_id;
563 unsigned int len;
564
565 if (dev->feature & GS_CAN_FEATURE_HW_TIMESTAMP) {
566 const u32 ts = gs_usb_set_timestamp(dev, skb, hf);
567
568 len = can_rx_offload_get_echo_skb_queue_timestamp(offload, echo_id,
569 ts, NULL);
570 } else {
571 len = can_rx_offload_get_echo_skb_queue_tail(offload, echo_id,
572 NULL);
573 }
574
575 return len;
576 }
577
578 static unsigned int
gs_usb_get_minimum_rx_length(const struct gs_can * dev,const struct gs_host_frame * hf,unsigned int * data_length_p)579 gs_usb_get_minimum_rx_length(const struct gs_can *dev, const struct gs_host_frame *hf,
580 unsigned int *data_length_p)
581 {
582 unsigned int minimum_length, data_length = 0;
583
584 if (hf->flags & GS_CAN_FLAG_FD) {
585 if (hf->echo_id == GS_HOST_FRAME_ECHO_ID_RX)
586 data_length = can_fd_dlc2len(hf->can_dlc);
587
588 if (dev->feature & GS_CAN_FEATURE_HW_TIMESTAMP)
589 /* timestamp follows data field of max size */
590 minimum_length = struct_size(hf, canfd_ts, 1);
591 else
592 minimum_length = sizeof(hf->header) + data_length;
593 } else {
594 if (hf->echo_id == GS_HOST_FRAME_ECHO_ID_RX &&
595 !(hf->can_id & cpu_to_le32(CAN_RTR_FLAG)))
596 data_length = can_cc_dlc2len(hf->can_dlc);
597
598 if (dev->feature & GS_CAN_FEATURE_HW_TIMESTAMP)
599 /* timestamp follows data field of max size */
600 minimum_length = struct_size(hf, classic_can_ts, 1);
601 else
602 minimum_length = sizeof(hf->header) + data_length;
603 }
604
605 *data_length_p = data_length;
606 return minimum_length;
607 }
608
gs_usb_receive_bulk_callback(struct urb * urb)609 static void gs_usb_receive_bulk_callback(struct urb *urb)
610 {
611 struct gs_usb *parent = urb->context;
612 struct gs_can *dev;
613 struct net_device *netdev;
614 int rc;
615 struct net_device_stats *stats;
616 struct gs_host_frame *hf = urb->transfer_buffer;
617 unsigned int minimum_length, data_length;
618 struct gs_tx_context *txc;
619 struct can_frame *cf;
620 struct canfd_frame *cfd;
621 struct sk_buff *skb;
622
623 BUG_ON(!parent);
624
625 switch (urb->status) {
626 case 0: /* success */
627 break;
628 case -ENOENT:
629 case -ESHUTDOWN:
630 return;
631 default:
632 /* do not resubmit aborted urbs. eg: when device goes down */
633 return;
634 }
635
636 minimum_length = sizeof(hf->header);
637 if (urb->actual_length < minimum_length) {
638 dev_err_ratelimited(&parent->udev->dev,
639 "short read (actual_length=%u, minimum_length=%u)\n",
640 urb->actual_length, minimum_length);
641
642 goto resubmit_urb;
643 }
644
645 /* device reports out of range channel id */
646 if (hf->channel >= parent->channel_cnt)
647 goto device_detach;
648
649 dev = parent->canch[hf->channel];
650
651 netdev = dev->netdev;
652 stats = &netdev->stats;
653
654 if (!netif_device_present(netdev))
655 return;
656
657 if (!netif_running(netdev))
658 goto resubmit_urb;
659
660 minimum_length = gs_usb_get_minimum_rx_length(dev, hf, &data_length);
661 if (urb->actual_length < minimum_length) {
662 stats->rx_errors++;
663 stats->rx_length_errors++;
664
665 if (net_ratelimit())
666 netdev_err(netdev,
667 "short read (actual_length=%u, minimum_length=%u)\n",
668 urb->actual_length, minimum_length);
669
670 goto resubmit_urb;
671 }
672
673 if (hf->echo_id == GS_HOST_FRAME_ECHO_ID_RX) { /* normal rx */
674 if (hf->flags & GS_CAN_FLAG_FD) {
675 skb = alloc_canfd_skb(netdev, &cfd);
676 if (!skb)
677 return;
678
679 cfd->can_id = le32_to_cpu(hf->can_id);
680 cfd->len = data_length;
681 if (hf->flags & GS_CAN_FLAG_BRS)
682 cfd->flags |= CANFD_BRS;
683 if (hf->flags & GS_CAN_FLAG_ESI)
684 cfd->flags |= CANFD_ESI;
685
686 memcpy(cfd->data, hf->canfd->data, data_length);
687 } else {
688 skb = alloc_can_skb(netdev, &cf);
689 if (!skb)
690 return;
691
692 cf->can_id = le32_to_cpu(hf->can_id);
693 can_frame_set_cc_len(cf, hf->can_dlc, dev->can.ctrlmode);
694
695 memcpy(cf->data, hf->classic_can->data, data_length);
696
697 /* ERROR frames tell us information about the controller */
698 if (le32_to_cpu(hf->can_id) & CAN_ERR_FLAG)
699 gs_update_state(dev, cf);
700 }
701
702 gs_usb_rx_offload(dev, skb, hf);
703 } else { /* echo_id == hf->echo_id */
704 if (hf->echo_id >= GS_MAX_TX_URBS) {
705 netdev_err(netdev,
706 "Unexpected out of range echo id %u\n",
707 hf->echo_id);
708 goto resubmit_urb;
709 }
710
711 txc = gs_get_tx_context(dev, hf->echo_id);
712
713 /* bad devices send bad echo_ids. */
714 if (!txc) {
715 netdev_err(netdev,
716 "Unexpected unused echo id %u\n",
717 hf->echo_id);
718 goto resubmit_urb;
719 }
720
721 skb = dev->can.echo_skb[hf->echo_id];
722 stats->tx_packets++;
723 stats->tx_bytes += gs_usb_get_echo_skb(dev, skb, hf);
724 gs_free_tx_context(txc);
725
726 atomic_dec(&dev->active_tx_urbs);
727
728 netif_wake_queue(netdev);
729 }
730
731 if (hf->flags & GS_CAN_FLAG_OVERFLOW) {
732 stats->rx_over_errors++;
733 stats->rx_errors++;
734
735 skb = alloc_can_err_skb(netdev, &cf);
736 if (!skb)
737 goto resubmit_urb;
738
739 cf->can_id |= CAN_ERR_CRTL;
740 cf->len = CAN_ERR_DLC;
741 cf->data[1] = CAN_ERR_CRTL_RX_OVERFLOW;
742
743 gs_usb_rx_offload(dev, skb, hf);
744 }
745
746 can_rx_offload_irq_finish(&dev->offload);
747
748 resubmit_urb:
749 usb_fill_bulk_urb(urb, parent->udev,
750 parent->pipe_in,
751 hf, parent->hf_size_rx,
752 gs_usb_receive_bulk_callback, parent);
753
754 usb_anchor_urb(urb, &parent->rx_submitted);
755
756 rc = usb_submit_urb(urb, GFP_ATOMIC);
757 if (!rc)
758 return;
759
760 usb_unanchor_urb(urb);
761
762 /* USB failure take down all interfaces */
763 if (rc == -ENODEV) {
764 device_detach:
765 for (rc = 0; rc < parent->channel_cnt; rc++) {
766 if (parent->canch[rc])
767 netif_device_detach(parent->canch[rc]->netdev);
768 }
769 } else if (rc != -ESHUTDOWN && net_ratelimit()) {
770 netdev_info(netdev, "failed to re-submit IN URB: %pe\n",
771 ERR_PTR(urb->status));
772 }
773 }
774
gs_usb_set_bittiming(struct net_device * netdev)775 static int gs_usb_set_bittiming(struct net_device *netdev)
776 {
777 struct gs_can *dev = netdev_priv(netdev);
778 struct can_bittiming *bt = &dev->can.bittiming;
779 struct gs_device_bittiming dbt = {
780 .prop_seg = cpu_to_le32(bt->prop_seg),
781 .phase_seg1 = cpu_to_le32(bt->phase_seg1),
782 .phase_seg2 = cpu_to_le32(bt->phase_seg2),
783 .sjw = cpu_to_le32(bt->sjw),
784 .brp = cpu_to_le32(bt->brp),
785 };
786
787 /* request bit timings */
788 return usb_control_msg_send(dev->udev, 0, GS_USB_BREQ_BITTIMING,
789 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
790 dev->channel, 0, &dbt, sizeof(dbt), 1000,
791 GFP_KERNEL);
792 }
793
gs_usb_set_data_bittiming(struct net_device * netdev)794 static int gs_usb_set_data_bittiming(struct net_device *netdev)
795 {
796 struct gs_can *dev = netdev_priv(netdev);
797 struct can_bittiming *bt = &dev->can.fd.data_bittiming;
798 struct gs_device_bittiming dbt = {
799 .prop_seg = cpu_to_le32(bt->prop_seg),
800 .phase_seg1 = cpu_to_le32(bt->phase_seg1),
801 .phase_seg2 = cpu_to_le32(bt->phase_seg2),
802 .sjw = cpu_to_le32(bt->sjw),
803 .brp = cpu_to_le32(bt->brp),
804 };
805 u8 request = GS_USB_BREQ_DATA_BITTIMING;
806
807 if (dev->feature & GS_CAN_FEATURE_QUIRK_BREQ_CANTACT_PRO)
808 request = GS_USB_BREQ_QUIRK_CANTACT_PRO_DATA_BITTIMING;
809
810 /* request data bit timings */
811 return usb_control_msg_send(dev->udev, 0, request,
812 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
813 dev->channel, 0, &dbt, sizeof(dbt), 1000,
814 GFP_KERNEL);
815 }
816
gs_usb_xmit_callback(struct urb * urb)817 static void gs_usb_xmit_callback(struct urb *urb)
818 {
819 struct gs_tx_context *txc = urb->context;
820 struct gs_can *dev = txc->dev;
821 struct net_device *netdev = dev->netdev;
822
823 if (!urb->status)
824 return;
825
826 if (urb->status != -ESHUTDOWN && net_ratelimit())
827 netdev_info(netdev, "failed to xmit URB %u: %pe\n",
828 txc->echo_id, ERR_PTR(urb->status));
829
830 netdev->stats.tx_dropped++;
831 netdev->stats.tx_errors++;
832
833 can_free_echo_skb(netdev, txc->echo_id, NULL);
834 gs_free_tx_context(txc);
835 atomic_dec(&dev->active_tx_urbs);
836
837 netif_wake_queue(netdev);
838 }
839
gs_can_start_xmit(struct sk_buff * skb,struct net_device * netdev)840 static netdev_tx_t gs_can_start_xmit(struct sk_buff *skb,
841 struct net_device *netdev)
842 {
843 struct gs_can *dev = netdev_priv(netdev);
844 struct net_device_stats *stats = &dev->netdev->stats;
845 struct urb *urb;
846 struct gs_host_frame *hf;
847 struct can_frame *cf;
848 struct canfd_frame *cfd;
849 int rc;
850 unsigned int idx;
851 struct gs_tx_context *txc;
852
853 if (can_dev_dropped_skb(netdev, skb))
854 return NETDEV_TX_OK;
855
856 /* find an empty context to keep track of transmission */
857 txc = gs_alloc_tx_context(dev);
858 if (!txc)
859 return NETDEV_TX_BUSY;
860
861 /* create a URB, and a buffer for it */
862 urb = usb_alloc_urb(0, GFP_ATOMIC);
863 if (!urb)
864 goto nomem_urb;
865
866 hf = kmalloc(dev->hf_size_tx, GFP_ATOMIC);
867 if (!hf)
868 goto nomem_hf;
869
870 idx = txc->echo_id;
871
872 if (idx >= GS_MAX_TX_URBS) {
873 netdev_err(netdev, "Invalid tx context %u\n", idx);
874 goto badidx;
875 }
876
877 hf->echo_id = idx;
878 hf->channel = dev->channel;
879 hf->flags = 0;
880 hf->reserved = 0;
881
882 if (can_is_canfd_skb(skb)) {
883 cfd = (struct canfd_frame *)skb->data;
884
885 hf->can_id = cpu_to_le32(cfd->can_id);
886 hf->can_dlc = can_fd_len2dlc(cfd->len);
887 hf->flags |= GS_CAN_FLAG_FD;
888 if (cfd->flags & CANFD_BRS)
889 hf->flags |= GS_CAN_FLAG_BRS;
890 if (cfd->flags & CANFD_ESI)
891 hf->flags |= GS_CAN_FLAG_ESI;
892
893 memcpy(hf->canfd->data, cfd->data, cfd->len);
894 } else {
895 cf = (struct can_frame *)skb->data;
896
897 hf->can_id = cpu_to_le32(cf->can_id);
898 hf->can_dlc = can_get_cc_dlc(cf, dev->can.ctrlmode);
899
900 memcpy(hf->classic_can->data, cf->data, cf->len);
901 }
902
903 usb_fill_bulk_urb(urb, dev->udev,
904 dev->parent->pipe_out,
905 hf, dev->hf_size_tx,
906 gs_usb_xmit_callback, txc);
907
908 urb->transfer_flags |= URB_FREE_BUFFER;
909 usb_anchor_urb(urb, &dev->tx_submitted);
910
911 can_put_echo_skb(skb, netdev, idx, 0);
912
913 atomic_inc(&dev->active_tx_urbs);
914
915 rc = usb_submit_urb(urb, GFP_ATOMIC);
916 if (unlikely(rc)) { /* usb send failed */
917 atomic_dec(&dev->active_tx_urbs);
918
919 can_free_echo_skb(netdev, idx, NULL);
920 gs_free_tx_context(txc);
921
922 usb_unanchor_urb(urb);
923
924 if (rc == -ENODEV) {
925 netif_device_detach(netdev);
926 } else {
927 netdev_err(netdev, "usb_submit failed (err=%d)\n", rc);
928 stats->tx_dropped++;
929 }
930 } else {
931 /* Slow down tx path */
932 if (atomic_read(&dev->active_tx_urbs) >= GS_MAX_TX_URBS)
933 netif_stop_queue(netdev);
934 }
935
936 /* let usb core take care of this urb */
937 usb_free_urb(urb);
938
939 return NETDEV_TX_OK;
940
941 badidx:
942 kfree(hf);
943 nomem_hf:
944 usb_free_urb(urb);
945
946 nomem_urb:
947 gs_free_tx_context(txc);
948 dev_kfree_skb(skb);
949 stats->tx_dropped++;
950 return NETDEV_TX_OK;
951 }
952
gs_can_open(struct net_device * netdev)953 static int gs_can_open(struct net_device *netdev)
954 {
955 struct gs_can *dev = netdev_priv(netdev);
956 struct gs_usb *parent = dev->parent;
957 struct gs_device_mode dm = {
958 .mode = cpu_to_le32(GS_CAN_MODE_START),
959 };
960 struct gs_host_frame *hf;
961 struct urb *urb = NULL;
962 u32 ctrlmode;
963 u32 flags = 0;
964 int rc, i;
965
966 rc = open_candev(netdev);
967 if (rc)
968 return rc;
969
970 ctrlmode = dev->can.ctrlmode;
971 if (ctrlmode & CAN_CTRLMODE_FD) {
972 if (dev->feature & GS_CAN_FEATURE_REQ_USB_QUIRK_LPC546XX)
973 dev->hf_size_tx = struct_size(hf, canfd_quirk, 1);
974 else
975 dev->hf_size_tx = struct_size(hf, canfd, 1);
976 } else {
977 if (dev->feature & GS_CAN_FEATURE_REQ_USB_QUIRK_LPC546XX)
978 dev->hf_size_tx = struct_size(hf, classic_can_quirk, 1);
979 else
980 dev->hf_size_tx = struct_size(hf, classic_can, 1);
981 }
982
983 can_rx_offload_enable(&dev->offload);
984
985 if (!parent->active_channels) {
986 if (dev->feature & GS_CAN_FEATURE_HW_TIMESTAMP)
987 gs_usb_timestamp_init(parent);
988
989 for (i = 0; i < GS_MAX_RX_URBS; i++) {
990 u8 *buf;
991
992 /* alloc rx urb */
993 urb = usb_alloc_urb(0, GFP_KERNEL);
994 if (!urb) {
995 rc = -ENOMEM;
996 goto out_usb_kill_anchored_urbs;
997 }
998
999 /* alloc rx buffer */
1000 buf = kmalloc(dev->parent->hf_size_rx,
1001 GFP_KERNEL);
1002 if (!buf) {
1003 rc = -ENOMEM;
1004 goto out_usb_free_urb;
1005 }
1006
1007 /* fill, anchor, and submit rx urb */
1008 usb_fill_bulk_urb(urb,
1009 dev->udev,
1010 dev->parent->pipe_in,
1011 buf,
1012 dev->parent->hf_size_rx,
1013 gs_usb_receive_bulk_callback, parent);
1014 urb->transfer_flags |= URB_FREE_BUFFER;
1015
1016 usb_anchor_urb(urb, &parent->rx_submitted);
1017
1018 rc = usb_submit_urb(urb, GFP_KERNEL);
1019 if (rc) {
1020 if (rc == -ENODEV)
1021 netif_device_detach(dev->netdev);
1022
1023 netdev_err(netdev,
1024 "usb_submit_urb() failed, error %pe\n",
1025 ERR_PTR(rc));
1026
1027 goto out_usb_unanchor_urb;
1028 }
1029
1030 /* Drop reference,
1031 * USB core will take care of freeing it
1032 */
1033 usb_free_urb(urb);
1034 }
1035 }
1036
1037 /* flags */
1038 if (ctrlmode & CAN_CTRLMODE_LOOPBACK)
1039 flags |= GS_CAN_MODE_LOOP_BACK;
1040
1041 if (ctrlmode & CAN_CTRLMODE_LISTENONLY)
1042 flags |= GS_CAN_MODE_LISTEN_ONLY;
1043
1044 if (ctrlmode & CAN_CTRLMODE_3_SAMPLES)
1045 flags |= GS_CAN_MODE_TRIPLE_SAMPLE;
1046
1047 if (ctrlmode & CAN_CTRLMODE_ONE_SHOT)
1048 flags |= GS_CAN_MODE_ONE_SHOT;
1049
1050 if (ctrlmode & CAN_CTRLMODE_BERR_REPORTING)
1051 flags |= GS_CAN_MODE_BERR_REPORTING;
1052
1053 if (ctrlmode & CAN_CTRLMODE_FD)
1054 flags |= GS_CAN_MODE_FD;
1055
1056 /* if hardware supports timestamps, enable it */
1057 if (dev->feature & GS_CAN_FEATURE_HW_TIMESTAMP)
1058 flags |= GS_CAN_MODE_HW_TIMESTAMP;
1059
1060 /* finally start device */
1061 dev->can.state = CAN_STATE_ERROR_ACTIVE;
1062 dm.flags = cpu_to_le32(flags);
1063 rc = usb_control_msg_send(dev->udev, 0, GS_USB_BREQ_MODE,
1064 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
1065 dev->channel, 0, &dm, sizeof(dm), 1000,
1066 GFP_KERNEL);
1067 if (rc) {
1068 netdev_err(netdev, "Couldn't start device (err=%d)\n", rc);
1069 dev->can.state = CAN_STATE_STOPPED;
1070
1071 goto out_usb_kill_anchored_urbs;
1072 }
1073
1074 parent->active_channels++;
1075 if (!(dev->can.ctrlmode & CAN_CTRLMODE_LISTENONLY))
1076 netif_start_queue(netdev);
1077
1078 return 0;
1079
1080 out_usb_unanchor_urb:
1081 usb_unanchor_urb(urb);
1082 out_usb_free_urb:
1083 usb_free_urb(urb);
1084 out_usb_kill_anchored_urbs:
1085 if (!parent->active_channels) {
1086 usb_kill_anchored_urbs(&parent->rx_submitted);
1087
1088 if (dev->feature & GS_CAN_FEATURE_HW_TIMESTAMP)
1089 gs_usb_timestamp_stop(parent);
1090 }
1091
1092 can_rx_offload_disable(&dev->offload);
1093 close_candev(netdev);
1094
1095 return rc;
1096 }
1097
gs_usb_get_state(const struct net_device * netdev,struct can_berr_counter * bec,enum can_state * state)1098 static int gs_usb_get_state(const struct net_device *netdev,
1099 struct can_berr_counter *bec,
1100 enum can_state *state)
1101 {
1102 struct gs_can *dev = netdev_priv(netdev);
1103 struct gs_device_state ds;
1104 int rc;
1105
1106 rc = usb_control_msg_recv(dev->udev, 0, GS_USB_BREQ_GET_STATE,
1107 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
1108 dev->channel, 0,
1109 &ds, sizeof(ds),
1110 USB_CTRL_GET_TIMEOUT,
1111 GFP_KERNEL);
1112 if (rc)
1113 return rc;
1114
1115 if (le32_to_cpu(ds.state) >= CAN_STATE_MAX)
1116 return -EOPNOTSUPP;
1117
1118 *state = le32_to_cpu(ds.state);
1119 bec->txerr = le32_to_cpu(ds.txerr);
1120 bec->rxerr = le32_to_cpu(ds.rxerr);
1121
1122 return 0;
1123 }
1124
gs_usb_can_get_berr_counter(const struct net_device * netdev,struct can_berr_counter * bec)1125 static int gs_usb_can_get_berr_counter(const struct net_device *netdev,
1126 struct can_berr_counter *bec)
1127 {
1128 enum can_state state;
1129
1130 return gs_usb_get_state(netdev, bec, &state);
1131 }
1132
gs_can_close(struct net_device * netdev)1133 static int gs_can_close(struct net_device *netdev)
1134 {
1135 int rc;
1136 struct gs_can *dev = netdev_priv(netdev);
1137 struct gs_usb *parent = dev->parent;
1138
1139 netif_stop_queue(netdev);
1140
1141 /* Stop polling */
1142 parent->active_channels--;
1143 if (!parent->active_channels) {
1144 usb_kill_anchored_urbs(&parent->rx_submitted);
1145
1146 if (dev->feature & GS_CAN_FEATURE_HW_TIMESTAMP)
1147 gs_usb_timestamp_stop(parent);
1148 }
1149
1150 /* Stop sending URBs */
1151 usb_kill_anchored_urbs(&dev->tx_submitted);
1152 atomic_set(&dev->active_tx_urbs, 0);
1153
1154 dev->can.state = CAN_STATE_STOPPED;
1155
1156 /* reset the device */
1157 gs_cmd_reset(dev);
1158
1159 /* reset tx contexts */
1160 for (rc = 0; rc < GS_MAX_TX_URBS; rc++) {
1161 dev->tx_context[rc].dev = dev;
1162 dev->tx_context[rc].echo_id = GS_MAX_TX_URBS;
1163 }
1164
1165 can_rx_offload_disable(&dev->offload);
1166
1167 /* close the netdev */
1168 close_candev(netdev);
1169
1170 return 0;
1171 }
1172
gs_can_hwtstamp_get(struct net_device * netdev,struct kernel_hwtstamp_config * cfg)1173 static int gs_can_hwtstamp_get(struct net_device *netdev,
1174 struct kernel_hwtstamp_config *cfg)
1175 {
1176 const struct gs_can *dev = netdev_priv(netdev);
1177
1178 if (dev->feature & GS_CAN_FEATURE_HW_TIMESTAMP)
1179 return can_hwtstamp_get(netdev, cfg);
1180
1181 return -EOPNOTSUPP;
1182 }
1183
gs_can_hwtstamp_set(struct net_device * netdev,struct kernel_hwtstamp_config * cfg,struct netlink_ext_ack * extack)1184 static int gs_can_hwtstamp_set(struct net_device *netdev,
1185 struct kernel_hwtstamp_config *cfg,
1186 struct netlink_ext_ack *extack)
1187 {
1188 const struct gs_can *dev = netdev_priv(netdev);
1189
1190 if (dev->feature & GS_CAN_FEATURE_HW_TIMESTAMP)
1191 return can_hwtstamp_set(netdev, cfg, extack);
1192
1193 return -EOPNOTSUPP;
1194 }
1195
1196 static const struct net_device_ops gs_usb_netdev_ops = {
1197 .ndo_open = gs_can_open,
1198 .ndo_stop = gs_can_close,
1199 .ndo_start_xmit = gs_can_start_xmit,
1200 .ndo_hwtstamp_get = gs_can_hwtstamp_get,
1201 .ndo_hwtstamp_set = gs_can_hwtstamp_set,
1202 };
1203
gs_usb_set_identify(struct net_device * netdev,bool do_identify)1204 static int gs_usb_set_identify(struct net_device *netdev, bool do_identify)
1205 {
1206 struct gs_can *dev = netdev_priv(netdev);
1207 struct gs_identify_mode imode;
1208
1209 if (do_identify)
1210 imode.mode = cpu_to_le32(GS_CAN_IDENTIFY_ON);
1211 else
1212 imode.mode = cpu_to_le32(GS_CAN_IDENTIFY_OFF);
1213
1214 return usb_control_msg_send(dev->udev, 0, GS_USB_BREQ_IDENTIFY,
1215 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
1216 dev->channel, 0, &imode, sizeof(imode), 100,
1217 GFP_KERNEL);
1218 }
1219
1220 /* blink LED's for finding the this interface */
gs_usb_set_phys_id(struct net_device * netdev,enum ethtool_phys_id_state state)1221 static int gs_usb_set_phys_id(struct net_device *netdev,
1222 enum ethtool_phys_id_state state)
1223 {
1224 const struct gs_can *dev = netdev_priv(netdev);
1225 int rc = 0;
1226
1227 if (!(dev->feature & GS_CAN_FEATURE_IDENTIFY))
1228 return -EOPNOTSUPP;
1229
1230 switch (state) {
1231 case ETHTOOL_ID_ACTIVE:
1232 rc = gs_usb_set_identify(netdev, GS_CAN_IDENTIFY_ON);
1233 break;
1234 case ETHTOOL_ID_INACTIVE:
1235 rc = gs_usb_set_identify(netdev, GS_CAN_IDENTIFY_OFF);
1236 break;
1237 default:
1238 break;
1239 }
1240
1241 return rc;
1242 }
1243
gs_usb_get_ts_info(struct net_device * netdev,struct kernel_ethtool_ts_info * info)1244 static int gs_usb_get_ts_info(struct net_device *netdev,
1245 struct kernel_ethtool_ts_info *info)
1246 {
1247 struct gs_can *dev = netdev_priv(netdev);
1248
1249 /* report if device supports HW timestamps */
1250 if (dev->feature & GS_CAN_FEATURE_HW_TIMESTAMP)
1251 return can_ethtool_op_get_ts_info_hwts(netdev, info);
1252
1253 return ethtool_op_get_ts_info(netdev, info);
1254 }
1255
1256 static const struct ethtool_ops gs_usb_ethtool_ops = {
1257 .set_phys_id = gs_usb_set_phys_id,
1258 .get_ts_info = gs_usb_get_ts_info,
1259 };
1260
gs_usb_get_termination(struct net_device * netdev,u16 * term)1261 static int gs_usb_get_termination(struct net_device *netdev, u16 *term)
1262 {
1263 struct gs_can *dev = netdev_priv(netdev);
1264 struct gs_device_termination_state term_state;
1265 int rc;
1266
1267 rc = usb_control_msg_recv(dev->udev, 0, GS_USB_BREQ_GET_TERMINATION,
1268 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
1269 dev->channel, 0,
1270 &term_state, sizeof(term_state), 1000,
1271 GFP_KERNEL);
1272 if (rc)
1273 return rc;
1274
1275 if (term_state.state == cpu_to_le32(GS_CAN_TERMINATION_STATE_ON))
1276 *term = GS_USB_TERMINATION_ENABLED;
1277 else
1278 *term = GS_USB_TERMINATION_DISABLED;
1279
1280 return 0;
1281 }
1282
gs_usb_set_termination(struct net_device * netdev,u16 term)1283 static int gs_usb_set_termination(struct net_device *netdev, u16 term)
1284 {
1285 struct gs_can *dev = netdev_priv(netdev);
1286 struct gs_device_termination_state term_state;
1287
1288 if (term == GS_USB_TERMINATION_ENABLED)
1289 term_state.state = cpu_to_le32(GS_CAN_TERMINATION_STATE_ON);
1290 else
1291 term_state.state = cpu_to_le32(GS_CAN_TERMINATION_STATE_OFF);
1292
1293 return usb_control_msg_send(dev->udev, 0, GS_USB_BREQ_SET_TERMINATION,
1294 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
1295 dev->channel, 0,
1296 &term_state, sizeof(term_state), 1000,
1297 GFP_KERNEL);
1298 }
1299
1300 static const u16 gs_usb_termination_const[] = {
1301 GS_USB_TERMINATION_DISABLED,
1302 GS_USB_TERMINATION_ENABLED
1303 };
1304
gs_make_candev(unsigned int channel,struct usb_interface * intf,struct gs_device_config * dconf)1305 static struct gs_can *gs_make_candev(unsigned int channel,
1306 struct usb_interface *intf,
1307 struct gs_device_config *dconf)
1308 {
1309 struct gs_can *dev;
1310 struct net_device *netdev;
1311 int rc;
1312 struct gs_device_bt_const_extended bt_const_extended;
1313 struct gs_device_bt_const bt_const;
1314 u32 feature;
1315
1316 /* fetch bit timing constants */
1317 rc = usb_control_msg_recv(interface_to_usbdev(intf), 0,
1318 GS_USB_BREQ_BT_CONST,
1319 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
1320 channel, 0, &bt_const, sizeof(bt_const), 1000,
1321 GFP_KERNEL);
1322
1323 if (rc) {
1324 dev_err(&intf->dev,
1325 "Couldn't get bit timing const for channel %d (%pe)\n",
1326 channel, ERR_PTR(rc));
1327 return ERR_PTR(rc);
1328 }
1329
1330 /* create netdev */
1331 netdev = alloc_candev(sizeof(struct gs_can), GS_MAX_TX_URBS);
1332 if (!netdev) {
1333 dev_err(&intf->dev, "Couldn't allocate candev\n");
1334 return ERR_PTR(-ENOMEM);
1335 }
1336
1337 dev = netdev_priv(netdev);
1338
1339 netdev->netdev_ops = &gs_usb_netdev_ops;
1340 netdev->ethtool_ops = &gs_usb_ethtool_ops;
1341
1342 netdev->flags |= IFF_ECHO; /* we support full roundtrip echo */
1343 netdev->dev_id = channel;
1344 netdev->dev_port = channel;
1345
1346 /* dev setup */
1347 strcpy(dev->bt_const.name, KBUILD_MODNAME);
1348 dev->bt_const.tseg1_min = le32_to_cpu(bt_const.tseg1_min);
1349 dev->bt_const.tseg1_max = le32_to_cpu(bt_const.tseg1_max);
1350 dev->bt_const.tseg2_min = le32_to_cpu(bt_const.tseg2_min);
1351 dev->bt_const.tseg2_max = le32_to_cpu(bt_const.tseg2_max);
1352 dev->bt_const.sjw_max = le32_to_cpu(bt_const.sjw_max);
1353 dev->bt_const.brp_min = le32_to_cpu(bt_const.brp_min);
1354 dev->bt_const.brp_max = le32_to_cpu(bt_const.brp_max);
1355 dev->bt_const.brp_inc = le32_to_cpu(bt_const.brp_inc);
1356
1357 dev->udev = interface_to_usbdev(intf);
1358 dev->netdev = netdev;
1359 dev->channel = channel;
1360
1361 init_usb_anchor(&dev->tx_submitted);
1362 atomic_set(&dev->active_tx_urbs, 0);
1363 spin_lock_init(&dev->tx_ctx_lock);
1364 for (rc = 0; rc < GS_MAX_TX_URBS; rc++) {
1365 dev->tx_context[rc].dev = dev;
1366 dev->tx_context[rc].echo_id = GS_MAX_TX_URBS;
1367 }
1368
1369 /* can setup */
1370 dev->can.state = CAN_STATE_STOPPED;
1371 dev->can.clock.freq = le32_to_cpu(bt_const.fclk_can);
1372 dev->can.bittiming_const = &dev->bt_const;
1373 dev->can.do_set_bittiming = gs_usb_set_bittiming;
1374
1375 dev->can.ctrlmode_supported = CAN_CTRLMODE_CC_LEN8_DLC;
1376
1377 feature = le32_to_cpu(bt_const.feature);
1378 dev->feature = FIELD_GET(GS_CAN_FEATURE_MASK, feature);
1379 if (feature & GS_CAN_FEATURE_LISTEN_ONLY)
1380 dev->can.ctrlmode_supported |= CAN_CTRLMODE_LISTENONLY;
1381
1382 if (feature & GS_CAN_FEATURE_LOOP_BACK)
1383 dev->can.ctrlmode_supported |= CAN_CTRLMODE_LOOPBACK;
1384
1385 if (feature & GS_CAN_FEATURE_TRIPLE_SAMPLE)
1386 dev->can.ctrlmode_supported |= CAN_CTRLMODE_3_SAMPLES;
1387
1388 if (feature & GS_CAN_FEATURE_ONE_SHOT)
1389 dev->can.ctrlmode_supported |= CAN_CTRLMODE_ONE_SHOT;
1390
1391 if (feature & GS_CAN_FEATURE_FD) {
1392 dev->can.ctrlmode_supported |= CAN_CTRLMODE_FD;
1393 /* The data bit timing will be overwritten, if
1394 * GS_CAN_FEATURE_BT_CONST_EXT is set.
1395 */
1396 dev->can.fd.data_bittiming_const = &dev->bt_const;
1397 dev->can.fd.do_set_data_bittiming = gs_usb_set_data_bittiming;
1398 }
1399
1400 if (feature & GS_CAN_FEATURE_TERMINATION) {
1401 rc = gs_usb_get_termination(netdev, &dev->can.termination);
1402 if (rc) {
1403 dev->feature &= ~GS_CAN_FEATURE_TERMINATION;
1404
1405 dev_info(&intf->dev,
1406 "Disabling termination support for channel %d (%pe)\n",
1407 channel, ERR_PTR(rc));
1408 } else {
1409 dev->can.termination_const = gs_usb_termination_const;
1410 dev->can.termination_const_cnt = ARRAY_SIZE(gs_usb_termination_const);
1411 dev->can.do_set_termination = gs_usb_set_termination;
1412 }
1413 }
1414
1415 if (feature & GS_CAN_FEATURE_BERR_REPORTING)
1416 dev->can.ctrlmode_supported |= CAN_CTRLMODE_BERR_REPORTING;
1417
1418 if (feature & GS_CAN_FEATURE_GET_STATE)
1419 dev->can.do_get_berr_counter = gs_usb_can_get_berr_counter;
1420
1421 /* The CANtact Pro from LinkLayer Labs is based on the
1422 * LPC54616 µC, which is affected by the NXP LPC USB transfer
1423 * erratum. However, the current firmware (version 2) doesn't
1424 * set the GS_CAN_FEATURE_REQ_USB_QUIRK_LPC546XX bit. Set the
1425 * feature GS_CAN_FEATURE_REQ_USB_QUIRK_LPC546XX to workaround
1426 * this issue.
1427 *
1428 * For the GS_USB_BREQ_DATA_BITTIMING USB control message the
1429 * CANtact Pro firmware uses a request value, which is already
1430 * used by the candleLight firmware for a different purpose
1431 * (GS_USB_BREQ_GET_USER_ID). Set the feature
1432 * GS_CAN_FEATURE_QUIRK_BREQ_CANTACT_PRO to workaround this
1433 * issue.
1434 */
1435 if (dev->udev->descriptor.idVendor == cpu_to_le16(USB_GS_USB_1_VENDOR_ID) &&
1436 dev->udev->descriptor.idProduct == cpu_to_le16(USB_GS_USB_1_PRODUCT_ID) &&
1437 dev->udev->manufacturer && dev->udev->product &&
1438 !strcmp(dev->udev->manufacturer, "LinkLayer Labs") &&
1439 !strcmp(dev->udev->product, "CANtact Pro") &&
1440 (le32_to_cpu(dconf->sw_version) <= 2))
1441 dev->feature |= GS_CAN_FEATURE_REQ_USB_QUIRK_LPC546XX |
1442 GS_CAN_FEATURE_QUIRK_BREQ_CANTACT_PRO;
1443
1444 /* GS_CAN_FEATURE_IDENTIFY is only supported for sw_version > 1 */
1445 if (!(le32_to_cpu(dconf->sw_version) > 1 &&
1446 feature & GS_CAN_FEATURE_IDENTIFY))
1447 dev->feature &= ~GS_CAN_FEATURE_IDENTIFY;
1448
1449 /* fetch extended bit timing constants if device has feature
1450 * GS_CAN_FEATURE_FD and GS_CAN_FEATURE_BT_CONST_EXT
1451 */
1452 if (feature & GS_CAN_FEATURE_FD &&
1453 feature & GS_CAN_FEATURE_BT_CONST_EXT) {
1454 rc = usb_control_msg_recv(interface_to_usbdev(intf), 0,
1455 GS_USB_BREQ_BT_CONST_EXT,
1456 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
1457 channel, 0, &bt_const_extended,
1458 sizeof(bt_const_extended),
1459 1000, GFP_KERNEL);
1460 if (rc) {
1461 dev_err(&intf->dev,
1462 "Couldn't get extended bit timing const for channel %d (%pe)\n",
1463 channel, ERR_PTR(rc));
1464 goto out_free_candev;
1465 }
1466
1467 strcpy(dev->data_bt_const.name, KBUILD_MODNAME);
1468 dev->data_bt_const.tseg1_min = le32_to_cpu(bt_const_extended.dtseg1_min);
1469 dev->data_bt_const.tseg1_max = le32_to_cpu(bt_const_extended.dtseg1_max);
1470 dev->data_bt_const.tseg2_min = le32_to_cpu(bt_const_extended.dtseg2_min);
1471 dev->data_bt_const.tseg2_max = le32_to_cpu(bt_const_extended.dtseg2_max);
1472 dev->data_bt_const.sjw_max = le32_to_cpu(bt_const_extended.dsjw_max);
1473 dev->data_bt_const.brp_min = le32_to_cpu(bt_const_extended.dbrp_min);
1474 dev->data_bt_const.brp_max = le32_to_cpu(bt_const_extended.dbrp_max);
1475 dev->data_bt_const.brp_inc = le32_to_cpu(bt_const_extended.dbrp_inc);
1476
1477 dev->can.fd.data_bittiming_const = &dev->data_bt_const;
1478 }
1479
1480 can_rx_offload_add_manual(netdev, &dev->offload, GS_NAPI_WEIGHT);
1481 SET_NETDEV_DEV(netdev, &intf->dev);
1482
1483 rc = register_candev(dev->netdev);
1484 if (rc) {
1485 dev_err(&intf->dev,
1486 "Couldn't register candev for channel %d (%pe)\n",
1487 channel, ERR_PTR(rc));
1488 goto out_can_rx_offload_del;
1489 }
1490
1491 return dev;
1492
1493 out_can_rx_offload_del:
1494 can_rx_offload_del(&dev->offload);
1495 out_free_candev:
1496 free_candev(dev->netdev);
1497 return ERR_PTR(rc);
1498 }
1499
gs_destroy_candev(struct gs_can * dev)1500 static void gs_destroy_candev(struct gs_can *dev)
1501 {
1502 unregister_candev(dev->netdev);
1503 can_rx_offload_del(&dev->offload);
1504 free_candev(dev->netdev);
1505 }
1506
gs_usb_probe(struct usb_interface * intf,const struct usb_device_id * id)1507 static int gs_usb_probe(struct usb_interface *intf,
1508 const struct usb_device_id *id)
1509 {
1510 struct usb_device *udev = interface_to_usbdev(intf);
1511 struct usb_endpoint_descriptor *ep_in, *ep_out;
1512 struct gs_host_frame *hf;
1513 struct gs_usb *parent;
1514 struct gs_host_config hconf = {
1515 .byte_order = cpu_to_le32(0x0000beef),
1516 };
1517 struct gs_device_config dconf;
1518 unsigned int icount, i;
1519 int rc;
1520
1521 rc = usb_find_common_endpoints(intf->cur_altsetting,
1522 &ep_in, &ep_out, NULL, NULL);
1523 if (rc) {
1524 dev_err(&intf->dev, "Required endpoints not found\n");
1525 return rc;
1526 }
1527
1528 /* send host config */
1529 rc = usb_control_msg_send(udev, 0,
1530 GS_USB_BREQ_HOST_FORMAT,
1531 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
1532 1, intf->cur_altsetting->desc.bInterfaceNumber,
1533 &hconf, sizeof(hconf), 1000,
1534 GFP_KERNEL);
1535 if (rc) {
1536 dev_err(&intf->dev, "Couldn't send data format (err=%d)\n", rc);
1537 return rc;
1538 }
1539
1540 /* read device config */
1541 rc = usb_control_msg_recv(udev, 0,
1542 GS_USB_BREQ_DEVICE_CONFIG,
1543 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
1544 1, intf->cur_altsetting->desc.bInterfaceNumber,
1545 &dconf, sizeof(dconf), 1000,
1546 GFP_KERNEL);
1547 if (rc) {
1548 dev_err(&intf->dev, "Couldn't get device config: (err=%d)\n",
1549 rc);
1550 return rc;
1551 }
1552
1553 icount = dconf.icount + 1;
1554 dev_info(&intf->dev, "Configuring for %u interfaces\n", icount);
1555
1556 if (icount > type_max(parent->channel_cnt)) {
1557 dev_err(&intf->dev,
1558 "Driver cannot handle more that %u CAN interfaces\n",
1559 type_max(parent->channel_cnt));
1560 return -EINVAL;
1561 }
1562
1563 parent = kzalloc(struct_size(parent, canch, icount), GFP_KERNEL);
1564 if (!parent)
1565 return -ENOMEM;
1566
1567 parent->channel_cnt = icount;
1568
1569 init_usb_anchor(&parent->rx_submitted);
1570
1571 usb_set_intfdata(intf, parent);
1572 parent->udev = udev;
1573
1574 /* store the detected endpoints */
1575 parent->pipe_in = usb_rcvbulkpipe(parent->udev, ep_in->bEndpointAddress);
1576 parent->pipe_out = usb_sndbulkpipe(parent->udev, ep_out->bEndpointAddress);
1577
1578 for (i = 0; i < icount; i++) {
1579 unsigned int hf_size_rx = 0;
1580
1581 parent->canch[i] = gs_make_candev(i, intf, &dconf);
1582 if (IS_ERR_OR_NULL(parent->canch[i])) {
1583 /* save error code to return later */
1584 rc = PTR_ERR(parent->canch[i]);
1585
1586 /* on failure destroy previously created candevs */
1587 icount = i;
1588 for (i = 0; i < icount; i++)
1589 gs_destroy_candev(parent->canch[i]);
1590
1591 usb_kill_anchored_urbs(&parent->rx_submitted);
1592 kfree(parent);
1593 return rc;
1594 }
1595 parent->canch[i]->parent = parent;
1596
1597 /* set RX packet size based on FD and if hardware
1598 * timestamps are supported.
1599 */
1600 if (parent->canch[i]->can.ctrlmode_supported & CAN_CTRLMODE_FD) {
1601 if (parent->canch[i]->feature & GS_CAN_FEATURE_HW_TIMESTAMP)
1602 hf_size_rx = struct_size(hf, canfd_ts, 1);
1603 else
1604 hf_size_rx = struct_size(hf, canfd, 1);
1605 } else {
1606 if (parent->canch[i]->feature & GS_CAN_FEATURE_HW_TIMESTAMP)
1607 hf_size_rx = struct_size(hf, classic_can_ts, 1);
1608 else
1609 hf_size_rx = struct_size(hf, classic_can, 1);
1610 }
1611 parent->hf_size_rx = max(parent->hf_size_rx, hf_size_rx);
1612 }
1613
1614 return 0;
1615 }
1616
gs_usb_disconnect(struct usb_interface * intf)1617 static void gs_usb_disconnect(struct usb_interface *intf)
1618 {
1619 struct gs_usb *parent = usb_get_intfdata(intf);
1620 unsigned int i;
1621
1622 usb_set_intfdata(intf, NULL);
1623
1624 if (!parent) {
1625 dev_err(&intf->dev, "Disconnect (nodata)\n");
1626 return;
1627 }
1628
1629 for (i = 0; i < parent->channel_cnt; i++)
1630 if (parent->canch[i])
1631 gs_destroy_candev(parent->canch[i]);
1632
1633 kfree(parent);
1634 }
1635
1636 static const struct usb_device_id gs_usb_table[] = {
1637 { USB_DEVICE_INTERFACE_NUMBER(USB_GS_USB_1_VENDOR_ID,
1638 USB_GS_USB_1_PRODUCT_ID, 0) },
1639 { USB_DEVICE_INTERFACE_NUMBER(USB_CANDLELIGHT_VENDOR_ID,
1640 USB_CANDLELIGHT_PRODUCT_ID, 0) },
1641 { USB_DEVICE_INTERFACE_NUMBER(USB_CES_CANEXT_FD_VENDOR_ID,
1642 USB_CES_CANEXT_FD_PRODUCT_ID, 0) },
1643 { USB_DEVICE_INTERFACE_NUMBER(USB_ABE_CANDEBUGGER_FD_VENDOR_ID,
1644 USB_ABE_CANDEBUGGER_FD_PRODUCT_ID, 0) },
1645 { USB_DEVICE_INTERFACE_NUMBER(USB_XYLANTA_SAINT3_VENDOR_ID,
1646 USB_XYLANTA_SAINT3_PRODUCT_ID, 0) },
1647 { USB_DEVICE_INTERFACE_NUMBER(USB_CANNECTIVITY_VENDOR_ID,
1648 USB_CANNECTIVITY_PRODUCT_ID, 0) },
1649 {} /* Terminating entry */
1650 };
1651
1652 MODULE_DEVICE_TABLE(usb, gs_usb_table);
1653
1654 static struct usb_driver gs_usb_driver = {
1655 .name = KBUILD_MODNAME,
1656 .probe = gs_usb_probe,
1657 .disconnect = gs_usb_disconnect,
1658 .id_table = gs_usb_table,
1659 };
1660
1661 module_usb_driver(gs_usb_driver);
1662
1663 MODULE_AUTHOR("Maximilian Schneider <mws@schneidersoft.net>");
1664 MODULE_DESCRIPTION(
1665 "Socket CAN device driver for Geschwister Schneider Technologie-, "
1666 "Entwicklungs- und Vertriebs UG. USB2.0 to CAN interfaces\n"
1667 "and bytewerk.org candleLight USB CAN interfaces.");
1668 MODULE_LICENSE("GPL v2");
1669