xref: /linux/drivers/net/can/usb/gs_usb.c (revision 55d0969c451159cff86949b38c39171cab962069)
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 GS_USB_ENDPOINT_IN 1
47 #define GS_USB_ENDPOINT_OUT 2
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 {
265 	u32 echo_id;
266 	__le32 can_id;
267 
268 	u8 can_dlc;
269 	u8 channel;
270 	u8 flags;
271 	u8 reserved;
272 
273 	union {
274 		DECLARE_FLEX_ARRAY(struct classic_can, classic_can);
275 		DECLARE_FLEX_ARRAY(struct classic_can_ts, classic_can_ts);
276 		DECLARE_FLEX_ARRAY(struct classic_can_quirk, classic_can_quirk);
277 		DECLARE_FLEX_ARRAY(struct canfd, canfd);
278 		DECLARE_FLEX_ARRAY(struct canfd_ts, canfd_ts);
279 		DECLARE_FLEX_ARRAY(struct canfd_quirk, canfd_quirk);
280 	};
281 } __packed;
282 /* The GS USB devices make use of the same flags and masks as in
283  * linux/can.h and linux/can/error.h, and no additional mapping is necessary.
284  */
285 
286 /* Only send a max of GS_MAX_TX_URBS frames per channel at a time. */
287 #define GS_MAX_TX_URBS 10
288 /* Only launch a max of GS_MAX_RX_URBS usb requests at a time. */
289 #define GS_MAX_RX_URBS 30
290 #define GS_NAPI_WEIGHT 32
291 
292 /* Maximum number of interfaces the driver supports per device.
293  * Current hardware only supports 3 interfaces. The future may vary.
294  */
295 #define GS_MAX_INTF 3
296 
297 struct gs_tx_context {
298 	struct gs_can *dev;
299 	unsigned int echo_id;
300 };
301 
302 struct gs_can {
303 	struct can_priv can; /* must be the first member */
304 
305 	struct can_rx_offload offload;
306 	struct gs_usb *parent;
307 
308 	struct net_device *netdev;
309 	struct usb_device *udev;
310 
311 	struct can_bittiming_const bt_const, data_bt_const;
312 	unsigned int channel;	/* channel number */
313 
314 	u32 feature;
315 	unsigned int hf_size_tx;
316 
317 	/* This lock prevents a race condition between xmit and receive. */
318 	spinlock_t tx_ctx_lock;
319 	struct gs_tx_context tx_context[GS_MAX_TX_URBS];
320 
321 	struct usb_anchor tx_submitted;
322 	atomic_t active_tx_urbs;
323 };
324 
325 /* usb interface struct */
326 struct gs_usb {
327 	struct gs_can *canch[GS_MAX_INTF];
328 	struct usb_anchor rx_submitted;
329 	struct usb_device *udev;
330 
331 	/* time counter for hardware timestamps */
332 	struct cyclecounter cc;
333 	struct timecounter tc;
334 	spinlock_t tc_lock; /* spinlock to guard access tc->cycle_last */
335 	struct delayed_work timestamp;
336 
337 	unsigned int hf_size_rx;
338 	u8 active_channels;
339 };
340 
341 /* 'allocate' a tx context.
342  * returns a valid tx context or NULL if there is no space.
343  */
344 static struct gs_tx_context *gs_alloc_tx_context(struct gs_can *dev)
345 {
346 	int i = 0;
347 	unsigned long flags;
348 
349 	spin_lock_irqsave(&dev->tx_ctx_lock, flags);
350 
351 	for (; i < GS_MAX_TX_URBS; i++) {
352 		if (dev->tx_context[i].echo_id == GS_MAX_TX_URBS) {
353 			dev->tx_context[i].echo_id = i;
354 			spin_unlock_irqrestore(&dev->tx_ctx_lock, flags);
355 			return &dev->tx_context[i];
356 		}
357 	}
358 
359 	spin_unlock_irqrestore(&dev->tx_ctx_lock, flags);
360 	return NULL;
361 }
362 
363 /* releases a tx context
364  */
365 static void gs_free_tx_context(struct gs_tx_context *txc)
366 {
367 	txc->echo_id = GS_MAX_TX_URBS;
368 }
369 
370 /* Get a tx context by id.
371  */
372 static struct gs_tx_context *gs_get_tx_context(struct gs_can *dev,
373 					       unsigned int id)
374 {
375 	unsigned long flags;
376 
377 	if (id < GS_MAX_TX_URBS) {
378 		spin_lock_irqsave(&dev->tx_ctx_lock, flags);
379 		if (dev->tx_context[id].echo_id == id) {
380 			spin_unlock_irqrestore(&dev->tx_ctx_lock, flags);
381 			return &dev->tx_context[id];
382 		}
383 		spin_unlock_irqrestore(&dev->tx_ctx_lock, flags);
384 	}
385 	return NULL;
386 }
387 
388 static int gs_cmd_reset(struct gs_can *dev)
389 {
390 	struct gs_device_mode dm = {
391 		.mode = cpu_to_le32(GS_CAN_MODE_RESET),
392 	};
393 
394 	return usb_control_msg_send(dev->udev, 0, GS_USB_BREQ_MODE,
395 				    USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
396 				    dev->channel, 0, &dm, sizeof(dm), 1000,
397 				    GFP_KERNEL);
398 }
399 
400 static inline int gs_usb_get_timestamp(const struct gs_usb *parent,
401 				       u32 *timestamp_p)
402 {
403 	__le32 timestamp;
404 	int rc;
405 
406 	rc = usb_control_msg_recv(parent->udev, 0, GS_USB_BREQ_TIMESTAMP,
407 				  USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
408 				  0, 0,
409 				  &timestamp, sizeof(timestamp),
410 				  USB_CTRL_GET_TIMEOUT,
411 				  GFP_KERNEL);
412 	if (rc)
413 		return rc;
414 
415 	*timestamp_p = le32_to_cpu(timestamp);
416 
417 	return 0;
418 }
419 
420 static u64 gs_usb_timestamp_read(const struct cyclecounter *cc) __must_hold(&dev->tc_lock)
421 {
422 	struct gs_usb *parent = container_of(cc, struct gs_usb, cc);
423 	u32 timestamp = 0;
424 	int err;
425 
426 	lockdep_assert_held(&parent->tc_lock);
427 
428 	/* drop lock for synchronous USB transfer */
429 	spin_unlock_bh(&parent->tc_lock);
430 	err = gs_usb_get_timestamp(parent, &timestamp);
431 	spin_lock_bh(&parent->tc_lock);
432 	if (err)
433 		dev_err(&parent->udev->dev,
434 			"Error %d while reading timestamp. HW timestamps may be inaccurate.",
435 			err);
436 
437 	return timestamp;
438 }
439 
440 static void gs_usb_timestamp_work(struct work_struct *work)
441 {
442 	struct delayed_work *delayed_work = to_delayed_work(work);
443 	struct gs_usb *parent;
444 
445 	parent = container_of(delayed_work, struct gs_usb, timestamp);
446 	spin_lock_bh(&parent->tc_lock);
447 	timecounter_read(&parent->tc);
448 	spin_unlock_bh(&parent->tc_lock);
449 
450 	schedule_delayed_work(&parent->timestamp,
451 			      GS_USB_TIMESTAMP_WORK_DELAY_SEC * HZ);
452 }
453 
454 static void gs_usb_skb_set_timestamp(struct gs_can *dev,
455 				     struct sk_buff *skb, u32 timestamp)
456 {
457 	struct skb_shared_hwtstamps *hwtstamps = skb_hwtstamps(skb);
458 	struct gs_usb *parent = dev->parent;
459 	u64 ns;
460 
461 	spin_lock_bh(&parent->tc_lock);
462 	ns = timecounter_cyc2time(&parent->tc, timestamp);
463 	spin_unlock_bh(&parent->tc_lock);
464 
465 	hwtstamps->hwtstamp = ns_to_ktime(ns);
466 }
467 
468 static void gs_usb_timestamp_init(struct gs_usb *parent)
469 {
470 	struct cyclecounter *cc = &parent->cc;
471 
472 	cc->read = gs_usb_timestamp_read;
473 	cc->mask = CYCLECOUNTER_MASK(32);
474 	cc->shift = 32 - bits_per(NSEC_PER_SEC / GS_USB_TIMESTAMP_TIMER_HZ);
475 	cc->mult = clocksource_hz2mult(GS_USB_TIMESTAMP_TIMER_HZ, cc->shift);
476 
477 	spin_lock_init(&parent->tc_lock);
478 	spin_lock_bh(&parent->tc_lock);
479 	timecounter_init(&parent->tc, &parent->cc, ktime_get_real_ns());
480 	spin_unlock_bh(&parent->tc_lock);
481 
482 	INIT_DELAYED_WORK(&parent->timestamp, gs_usb_timestamp_work);
483 	schedule_delayed_work(&parent->timestamp,
484 			      GS_USB_TIMESTAMP_WORK_DELAY_SEC * HZ);
485 }
486 
487 static void gs_usb_timestamp_stop(struct gs_usb *parent)
488 {
489 	cancel_delayed_work_sync(&parent->timestamp);
490 }
491 
492 static void gs_update_state(struct gs_can *dev, struct can_frame *cf)
493 {
494 	struct can_device_stats *can_stats = &dev->can.can_stats;
495 
496 	if (cf->can_id & CAN_ERR_RESTARTED) {
497 		dev->can.state = CAN_STATE_ERROR_ACTIVE;
498 		can_stats->restarts++;
499 	} else if (cf->can_id & CAN_ERR_BUSOFF) {
500 		dev->can.state = CAN_STATE_BUS_OFF;
501 		can_stats->bus_off++;
502 	} else if (cf->can_id & CAN_ERR_CRTL) {
503 		if ((cf->data[1] & CAN_ERR_CRTL_TX_WARNING) ||
504 		    (cf->data[1] & CAN_ERR_CRTL_RX_WARNING)) {
505 			dev->can.state = CAN_STATE_ERROR_WARNING;
506 			can_stats->error_warning++;
507 		} else if ((cf->data[1] & CAN_ERR_CRTL_TX_PASSIVE) ||
508 			   (cf->data[1] & CAN_ERR_CRTL_RX_PASSIVE)) {
509 			dev->can.state = CAN_STATE_ERROR_PASSIVE;
510 			can_stats->error_passive++;
511 		} else {
512 			dev->can.state = CAN_STATE_ERROR_ACTIVE;
513 		}
514 	}
515 }
516 
517 static u32 gs_usb_set_timestamp(struct gs_can *dev, struct sk_buff *skb,
518 				const struct gs_host_frame *hf)
519 {
520 	u32 timestamp;
521 
522 	if (hf->flags & GS_CAN_FLAG_FD)
523 		timestamp = le32_to_cpu(hf->canfd_ts->timestamp_us);
524 	else
525 		timestamp = le32_to_cpu(hf->classic_can_ts->timestamp_us);
526 
527 	if (skb)
528 		gs_usb_skb_set_timestamp(dev, skb, timestamp);
529 
530 	return timestamp;
531 }
532 
533 static void gs_usb_rx_offload(struct gs_can *dev, struct sk_buff *skb,
534 			      const struct gs_host_frame *hf)
535 {
536 	struct can_rx_offload *offload = &dev->offload;
537 	int rc;
538 
539 	if (dev->feature & GS_CAN_FEATURE_HW_TIMESTAMP) {
540 		const u32 ts = gs_usb_set_timestamp(dev, skb, hf);
541 
542 		rc = can_rx_offload_queue_timestamp(offload, skb, ts);
543 	} else {
544 		rc = can_rx_offload_queue_tail(offload, skb);
545 	}
546 
547 	if (rc)
548 		dev->netdev->stats.rx_fifo_errors++;
549 }
550 
551 static unsigned int
552 gs_usb_get_echo_skb(struct gs_can *dev, struct sk_buff *skb,
553 		    const struct gs_host_frame *hf)
554 {
555 	struct can_rx_offload *offload = &dev->offload;
556 	const u32 echo_id = hf->echo_id;
557 	unsigned int len;
558 
559 	if (dev->feature & GS_CAN_FEATURE_HW_TIMESTAMP) {
560 		const u32 ts = gs_usb_set_timestamp(dev, skb, hf);
561 
562 		len = can_rx_offload_get_echo_skb_queue_timestamp(offload, echo_id,
563 								  ts, NULL);
564 	} else {
565 		len = can_rx_offload_get_echo_skb_queue_tail(offload, echo_id,
566 							     NULL);
567 	}
568 
569 	return len;
570 }
571 
572 static void gs_usb_receive_bulk_callback(struct urb *urb)
573 {
574 	struct gs_usb *parent = urb->context;
575 	struct gs_can *dev;
576 	struct net_device *netdev;
577 	int rc;
578 	struct net_device_stats *stats;
579 	struct gs_host_frame *hf = urb->transfer_buffer;
580 	struct gs_tx_context *txc;
581 	struct can_frame *cf;
582 	struct canfd_frame *cfd;
583 	struct sk_buff *skb;
584 
585 	BUG_ON(!parent);
586 
587 	switch (urb->status) {
588 	case 0: /* success */
589 		break;
590 	case -ENOENT:
591 	case -ESHUTDOWN:
592 		return;
593 	default:
594 		/* do not resubmit aborted urbs. eg: when device goes down */
595 		return;
596 	}
597 
598 	/* device reports out of range channel id */
599 	if (hf->channel >= GS_MAX_INTF)
600 		goto device_detach;
601 
602 	dev = parent->canch[hf->channel];
603 
604 	netdev = dev->netdev;
605 	stats = &netdev->stats;
606 
607 	if (!netif_device_present(netdev))
608 		return;
609 
610 	if (!netif_running(netdev))
611 		goto resubmit_urb;
612 
613 	if (hf->echo_id == -1) { /* normal rx */
614 		if (hf->flags & GS_CAN_FLAG_FD) {
615 			skb = alloc_canfd_skb(netdev, &cfd);
616 			if (!skb)
617 				return;
618 
619 			cfd->can_id = le32_to_cpu(hf->can_id);
620 			cfd->len = can_fd_dlc2len(hf->can_dlc);
621 			if (hf->flags & GS_CAN_FLAG_BRS)
622 				cfd->flags |= CANFD_BRS;
623 			if (hf->flags & GS_CAN_FLAG_ESI)
624 				cfd->flags |= CANFD_ESI;
625 
626 			memcpy(cfd->data, hf->canfd->data, cfd->len);
627 		} else {
628 			skb = alloc_can_skb(netdev, &cf);
629 			if (!skb)
630 				return;
631 
632 			cf->can_id = le32_to_cpu(hf->can_id);
633 			can_frame_set_cc_len(cf, hf->can_dlc, dev->can.ctrlmode);
634 
635 			memcpy(cf->data, hf->classic_can->data, 8);
636 
637 			/* ERROR frames tell us information about the controller */
638 			if (le32_to_cpu(hf->can_id) & CAN_ERR_FLAG)
639 				gs_update_state(dev, cf);
640 		}
641 
642 		gs_usb_rx_offload(dev, skb, hf);
643 	} else { /* echo_id == hf->echo_id */
644 		if (hf->echo_id >= GS_MAX_TX_URBS) {
645 			netdev_err(netdev,
646 				   "Unexpected out of range echo id %u\n",
647 				   hf->echo_id);
648 			goto resubmit_urb;
649 		}
650 
651 		txc = gs_get_tx_context(dev, hf->echo_id);
652 
653 		/* bad devices send bad echo_ids. */
654 		if (!txc) {
655 			netdev_err(netdev,
656 				   "Unexpected unused echo id %u\n",
657 				   hf->echo_id);
658 			goto resubmit_urb;
659 		}
660 
661 		skb = dev->can.echo_skb[hf->echo_id];
662 		stats->tx_packets++;
663 		stats->tx_bytes += gs_usb_get_echo_skb(dev, skb, hf);
664 		gs_free_tx_context(txc);
665 
666 		atomic_dec(&dev->active_tx_urbs);
667 
668 		netif_wake_queue(netdev);
669 	}
670 
671 	if (hf->flags & GS_CAN_FLAG_OVERFLOW) {
672 		stats->rx_over_errors++;
673 		stats->rx_errors++;
674 
675 		skb = alloc_can_err_skb(netdev, &cf);
676 		if (!skb)
677 			goto resubmit_urb;
678 
679 		cf->can_id |= CAN_ERR_CRTL;
680 		cf->len = CAN_ERR_DLC;
681 		cf->data[1] = CAN_ERR_CRTL_RX_OVERFLOW;
682 
683 		gs_usb_rx_offload(dev, skb, hf);
684 	}
685 
686 	can_rx_offload_irq_finish(&dev->offload);
687 
688 resubmit_urb:
689 	usb_fill_bulk_urb(urb, parent->udev,
690 			  usb_rcvbulkpipe(parent->udev, GS_USB_ENDPOINT_IN),
691 			  hf, dev->parent->hf_size_rx,
692 			  gs_usb_receive_bulk_callback, parent);
693 
694 	rc = usb_submit_urb(urb, GFP_ATOMIC);
695 
696 	/* USB failure take down all interfaces */
697 	if (rc == -ENODEV) {
698 device_detach:
699 		for (rc = 0; rc < GS_MAX_INTF; rc++) {
700 			if (parent->canch[rc])
701 				netif_device_detach(parent->canch[rc]->netdev);
702 		}
703 	}
704 }
705 
706 static int gs_usb_set_bittiming(struct net_device *netdev)
707 {
708 	struct gs_can *dev = netdev_priv(netdev);
709 	struct can_bittiming *bt = &dev->can.bittiming;
710 	struct gs_device_bittiming dbt = {
711 		.prop_seg = cpu_to_le32(bt->prop_seg),
712 		.phase_seg1 = cpu_to_le32(bt->phase_seg1),
713 		.phase_seg2 = cpu_to_le32(bt->phase_seg2),
714 		.sjw = cpu_to_le32(bt->sjw),
715 		.brp = cpu_to_le32(bt->brp),
716 	};
717 
718 	/* request bit timings */
719 	return usb_control_msg_send(dev->udev, 0, GS_USB_BREQ_BITTIMING,
720 				    USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
721 				    dev->channel, 0, &dbt, sizeof(dbt), 1000,
722 				    GFP_KERNEL);
723 }
724 
725 static int gs_usb_set_data_bittiming(struct net_device *netdev)
726 {
727 	struct gs_can *dev = netdev_priv(netdev);
728 	struct can_bittiming *bt = &dev->can.data_bittiming;
729 	struct gs_device_bittiming dbt = {
730 		.prop_seg = cpu_to_le32(bt->prop_seg),
731 		.phase_seg1 = cpu_to_le32(bt->phase_seg1),
732 		.phase_seg2 = cpu_to_le32(bt->phase_seg2),
733 		.sjw = cpu_to_le32(bt->sjw),
734 		.brp = cpu_to_le32(bt->brp),
735 	};
736 	u8 request = GS_USB_BREQ_DATA_BITTIMING;
737 
738 	if (dev->feature & GS_CAN_FEATURE_QUIRK_BREQ_CANTACT_PRO)
739 		request = GS_USB_BREQ_QUIRK_CANTACT_PRO_DATA_BITTIMING;
740 
741 	/* request data bit timings */
742 	return usb_control_msg_send(dev->udev, 0, request,
743 				    USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
744 				    dev->channel, 0, &dbt, sizeof(dbt), 1000,
745 				    GFP_KERNEL);
746 }
747 
748 static void gs_usb_xmit_callback(struct urb *urb)
749 {
750 	struct gs_tx_context *txc = urb->context;
751 	struct gs_can *dev = txc->dev;
752 	struct net_device *netdev = dev->netdev;
753 
754 	if (urb->status)
755 		netdev_info(netdev, "usb xmit fail %u\n", txc->echo_id);
756 }
757 
758 static netdev_tx_t gs_can_start_xmit(struct sk_buff *skb,
759 				     struct net_device *netdev)
760 {
761 	struct gs_can *dev = netdev_priv(netdev);
762 	struct net_device_stats *stats = &dev->netdev->stats;
763 	struct urb *urb;
764 	struct gs_host_frame *hf;
765 	struct can_frame *cf;
766 	struct canfd_frame *cfd;
767 	int rc;
768 	unsigned int idx;
769 	struct gs_tx_context *txc;
770 
771 	if (can_dev_dropped_skb(netdev, skb))
772 		return NETDEV_TX_OK;
773 
774 	/* find an empty context to keep track of transmission */
775 	txc = gs_alloc_tx_context(dev);
776 	if (!txc)
777 		return NETDEV_TX_BUSY;
778 
779 	/* create a URB, and a buffer for it */
780 	urb = usb_alloc_urb(0, GFP_ATOMIC);
781 	if (!urb)
782 		goto nomem_urb;
783 
784 	hf = kmalloc(dev->hf_size_tx, GFP_ATOMIC);
785 	if (!hf)
786 		goto nomem_hf;
787 
788 	idx = txc->echo_id;
789 
790 	if (idx >= GS_MAX_TX_URBS) {
791 		netdev_err(netdev, "Invalid tx context %u\n", idx);
792 		goto badidx;
793 	}
794 
795 	hf->echo_id = idx;
796 	hf->channel = dev->channel;
797 	hf->flags = 0;
798 	hf->reserved = 0;
799 
800 	if (can_is_canfd_skb(skb)) {
801 		cfd = (struct canfd_frame *)skb->data;
802 
803 		hf->can_id = cpu_to_le32(cfd->can_id);
804 		hf->can_dlc = can_fd_len2dlc(cfd->len);
805 		hf->flags |= GS_CAN_FLAG_FD;
806 		if (cfd->flags & CANFD_BRS)
807 			hf->flags |= GS_CAN_FLAG_BRS;
808 		if (cfd->flags & CANFD_ESI)
809 			hf->flags |= GS_CAN_FLAG_ESI;
810 
811 		memcpy(hf->canfd->data, cfd->data, cfd->len);
812 	} else {
813 		cf = (struct can_frame *)skb->data;
814 
815 		hf->can_id = cpu_to_le32(cf->can_id);
816 		hf->can_dlc = can_get_cc_dlc(cf, dev->can.ctrlmode);
817 
818 		memcpy(hf->classic_can->data, cf->data, cf->len);
819 	}
820 
821 	usb_fill_bulk_urb(urb, dev->udev,
822 			  usb_sndbulkpipe(dev->udev, GS_USB_ENDPOINT_OUT),
823 			  hf, dev->hf_size_tx,
824 			  gs_usb_xmit_callback, txc);
825 
826 	urb->transfer_flags |= URB_FREE_BUFFER;
827 	usb_anchor_urb(urb, &dev->tx_submitted);
828 
829 	can_put_echo_skb(skb, netdev, idx, 0);
830 
831 	atomic_inc(&dev->active_tx_urbs);
832 
833 	rc = usb_submit_urb(urb, GFP_ATOMIC);
834 	if (unlikely(rc)) {			/* usb send failed */
835 		atomic_dec(&dev->active_tx_urbs);
836 
837 		can_free_echo_skb(netdev, idx, NULL);
838 		gs_free_tx_context(txc);
839 
840 		usb_unanchor_urb(urb);
841 
842 		if (rc == -ENODEV) {
843 			netif_device_detach(netdev);
844 		} else {
845 			netdev_err(netdev, "usb_submit failed (err=%d)\n", rc);
846 			stats->tx_dropped++;
847 		}
848 	} else {
849 		/* Slow down tx path */
850 		if (atomic_read(&dev->active_tx_urbs) >= GS_MAX_TX_URBS)
851 			netif_stop_queue(netdev);
852 	}
853 
854 	/* let usb core take care of this urb */
855 	usb_free_urb(urb);
856 
857 	return NETDEV_TX_OK;
858 
859 badidx:
860 	kfree(hf);
861 nomem_hf:
862 	usb_free_urb(urb);
863 
864 nomem_urb:
865 	gs_free_tx_context(txc);
866 	dev_kfree_skb(skb);
867 	stats->tx_dropped++;
868 	return NETDEV_TX_OK;
869 }
870 
871 static int gs_can_open(struct net_device *netdev)
872 {
873 	struct gs_can *dev = netdev_priv(netdev);
874 	struct gs_usb *parent = dev->parent;
875 	struct gs_device_mode dm = {
876 		.mode = cpu_to_le32(GS_CAN_MODE_START),
877 	};
878 	struct gs_host_frame *hf;
879 	struct urb *urb = NULL;
880 	u32 ctrlmode;
881 	u32 flags = 0;
882 	int rc, i;
883 
884 	rc = open_candev(netdev);
885 	if (rc)
886 		return rc;
887 
888 	ctrlmode = dev->can.ctrlmode;
889 	if (ctrlmode & CAN_CTRLMODE_FD) {
890 		if (dev->feature & GS_CAN_FEATURE_REQ_USB_QUIRK_LPC546XX)
891 			dev->hf_size_tx = struct_size(hf, canfd_quirk, 1);
892 		else
893 			dev->hf_size_tx = struct_size(hf, canfd, 1);
894 	} else {
895 		if (dev->feature & GS_CAN_FEATURE_REQ_USB_QUIRK_LPC546XX)
896 			dev->hf_size_tx = struct_size(hf, classic_can_quirk, 1);
897 		else
898 			dev->hf_size_tx = struct_size(hf, classic_can, 1);
899 	}
900 
901 	can_rx_offload_enable(&dev->offload);
902 
903 	if (!parent->active_channels) {
904 		if (dev->feature & GS_CAN_FEATURE_HW_TIMESTAMP)
905 			gs_usb_timestamp_init(parent);
906 
907 		for (i = 0; i < GS_MAX_RX_URBS; i++) {
908 			u8 *buf;
909 
910 			/* alloc rx urb */
911 			urb = usb_alloc_urb(0, GFP_KERNEL);
912 			if (!urb) {
913 				rc = -ENOMEM;
914 				goto out_usb_kill_anchored_urbs;
915 			}
916 
917 			/* alloc rx buffer */
918 			buf = kmalloc(dev->parent->hf_size_rx,
919 				      GFP_KERNEL);
920 			if (!buf) {
921 				rc = -ENOMEM;
922 				goto out_usb_free_urb;
923 			}
924 
925 			/* fill, anchor, and submit rx urb */
926 			usb_fill_bulk_urb(urb,
927 					  dev->udev,
928 					  usb_rcvbulkpipe(dev->udev,
929 							  GS_USB_ENDPOINT_IN),
930 					  buf,
931 					  dev->parent->hf_size_rx,
932 					  gs_usb_receive_bulk_callback, parent);
933 			urb->transfer_flags |= URB_FREE_BUFFER;
934 
935 			usb_anchor_urb(urb, &parent->rx_submitted);
936 
937 			rc = usb_submit_urb(urb, GFP_KERNEL);
938 			if (rc) {
939 				if (rc == -ENODEV)
940 					netif_device_detach(dev->netdev);
941 
942 				netdev_err(netdev,
943 					   "usb_submit_urb() failed, error %pe\n",
944 					   ERR_PTR(rc));
945 
946 				goto out_usb_unanchor_urb;
947 			}
948 
949 			/* Drop reference,
950 			 * USB core will take care of freeing it
951 			 */
952 			usb_free_urb(urb);
953 		}
954 	}
955 
956 	/* flags */
957 	if (ctrlmode & CAN_CTRLMODE_LOOPBACK)
958 		flags |= GS_CAN_MODE_LOOP_BACK;
959 
960 	if (ctrlmode & CAN_CTRLMODE_LISTENONLY)
961 		flags |= GS_CAN_MODE_LISTEN_ONLY;
962 
963 	if (ctrlmode & CAN_CTRLMODE_3_SAMPLES)
964 		flags |= GS_CAN_MODE_TRIPLE_SAMPLE;
965 
966 	if (ctrlmode & CAN_CTRLMODE_ONE_SHOT)
967 		flags |= GS_CAN_MODE_ONE_SHOT;
968 
969 	if (ctrlmode & CAN_CTRLMODE_BERR_REPORTING)
970 		flags |= GS_CAN_MODE_BERR_REPORTING;
971 
972 	if (ctrlmode & CAN_CTRLMODE_FD)
973 		flags |= GS_CAN_MODE_FD;
974 
975 	/* if hardware supports timestamps, enable it */
976 	if (dev->feature & GS_CAN_FEATURE_HW_TIMESTAMP)
977 		flags |= GS_CAN_MODE_HW_TIMESTAMP;
978 
979 	/* finally start device */
980 	dev->can.state = CAN_STATE_ERROR_ACTIVE;
981 	dm.flags = cpu_to_le32(flags);
982 	rc = usb_control_msg_send(dev->udev, 0, GS_USB_BREQ_MODE,
983 				  USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
984 				  dev->channel, 0, &dm, sizeof(dm), 1000,
985 				  GFP_KERNEL);
986 	if (rc) {
987 		netdev_err(netdev, "Couldn't start device (err=%d)\n", rc);
988 		dev->can.state = CAN_STATE_STOPPED;
989 
990 		goto out_usb_kill_anchored_urbs;
991 	}
992 
993 	parent->active_channels++;
994 	if (!(dev->can.ctrlmode & CAN_CTRLMODE_LISTENONLY))
995 		netif_start_queue(netdev);
996 
997 	return 0;
998 
999 out_usb_unanchor_urb:
1000 	usb_unanchor_urb(urb);
1001 out_usb_free_urb:
1002 	usb_free_urb(urb);
1003 out_usb_kill_anchored_urbs:
1004 	if (!parent->active_channels) {
1005 		usb_kill_anchored_urbs(&dev->tx_submitted);
1006 
1007 		if (dev->feature & GS_CAN_FEATURE_HW_TIMESTAMP)
1008 			gs_usb_timestamp_stop(parent);
1009 	}
1010 
1011 	can_rx_offload_disable(&dev->offload);
1012 	close_candev(netdev);
1013 
1014 	return rc;
1015 }
1016 
1017 static int gs_usb_get_state(const struct net_device *netdev,
1018 			    struct can_berr_counter *bec,
1019 			    enum can_state *state)
1020 {
1021 	struct gs_can *dev = netdev_priv(netdev);
1022 	struct gs_device_state ds;
1023 	int rc;
1024 
1025 	rc = usb_control_msg_recv(dev->udev, 0, GS_USB_BREQ_GET_STATE,
1026 				  USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
1027 				  dev->channel, 0,
1028 				  &ds, sizeof(ds),
1029 				  USB_CTRL_GET_TIMEOUT,
1030 				  GFP_KERNEL);
1031 	if (rc)
1032 		return rc;
1033 
1034 	if (le32_to_cpu(ds.state) >= CAN_STATE_MAX)
1035 		return -EOPNOTSUPP;
1036 
1037 	*state = le32_to_cpu(ds.state);
1038 	bec->txerr = le32_to_cpu(ds.txerr);
1039 	bec->rxerr = le32_to_cpu(ds.rxerr);
1040 
1041 	return 0;
1042 }
1043 
1044 static int gs_usb_can_get_berr_counter(const struct net_device *netdev,
1045 				       struct can_berr_counter *bec)
1046 {
1047 	enum can_state state;
1048 
1049 	return gs_usb_get_state(netdev, bec, &state);
1050 }
1051 
1052 static int gs_can_close(struct net_device *netdev)
1053 {
1054 	int rc;
1055 	struct gs_can *dev = netdev_priv(netdev);
1056 	struct gs_usb *parent = dev->parent;
1057 
1058 	netif_stop_queue(netdev);
1059 
1060 	/* Stop polling */
1061 	parent->active_channels--;
1062 	if (!parent->active_channels) {
1063 		usb_kill_anchored_urbs(&parent->rx_submitted);
1064 
1065 		if (dev->feature & GS_CAN_FEATURE_HW_TIMESTAMP)
1066 			gs_usb_timestamp_stop(parent);
1067 	}
1068 
1069 	/* Stop sending URBs */
1070 	usb_kill_anchored_urbs(&dev->tx_submitted);
1071 	atomic_set(&dev->active_tx_urbs, 0);
1072 
1073 	dev->can.state = CAN_STATE_STOPPED;
1074 
1075 	/* reset the device */
1076 	gs_cmd_reset(dev);
1077 
1078 	/* reset tx contexts */
1079 	for (rc = 0; rc < GS_MAX_TX_URBS; rc++) {
1080 		dev->tx_context[rc].dev = dev;
1081 		dev->tx_context[rc].echo_id = GS_MAX_TX_URBS;
1082 	}
1083 
1084 	can_rx_offload_disable(&dev->offload);
1085 
1086 	/* close the netdev */
1087 	close_candev(netdev);
1088 
1089 	return 0;
1090 }
1091 
1092 static int gs_can_eth_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
1093 {
1094 	const struct gs_can *dev = netdev_priv(netdev);
1095 
1096 	if (dev->feature & GS_CAN_FEATURE_HW_TIMESTAMP)
1097 		return can_eth_ioctl_hwts(netdev, ifr, cmd);
1098 
1099 	return -EOPNOTSUPP;
1100 }
1101 
1102 static const struct net_device_ops gs_usb_netdev_ops = {
1103 	.ndo_open = gs_can_open,
1104 	.ndo_stop = gs_can_close,
1105 	.ndo_start_xmit = gs_can_start_xmit,
1106 	.ndo_change_mtu = can_change_mtu,
1107 	.ndo_eth_ioctl = gs_can_eth_ioctl,
1108 };
1109 
1110 static int gs_usb_set_identify(struct net_device *netdev, bool do_identify)
1111 {
1112 	struct gs_can *dev = netdev_priv(netdev);
1113 	struct gs_identify_mode imode;
1114 
1115 	if (do_identify)
1116 		imode.mode = cpu_to_le32(GS_CAN_IDENTIFY_ON);
1117 	else
1118 		imode.mode = cpu_to_le32(GS_CAN_IDENTIFY_OFF);
1119 
1120 	return usb_control_msg_send(dev->udev, 0, GS_USB_BREQ_IDENTIFY,
1121 				    USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
1122 				    dev->channel, 0, &imode, sizeof(imode), 100,
1123 				    GFP_KERNEL);
1124 }
1125 
1126 /* blink LED's for finding the this interface */
1127 static int gs_usb_set_phys_id(struct net_device *netdev,
1128 			      enum ethtool_phys_id_state state)
1129 {
1130 	const struct gs_can *dev = netdev_priv(netdev);
1131 	int rc = 0;
1132 
1133 	if (!(dev->feature & GS_CAN_FEATURE_IDENTIFY))
1134 		return -EOPNOTSUPP;
1135 
1136 	switch (state) {
1137 	case ETHTOOL_ID_ACTIVE:
1138 		rc = gs_usb_set_identify(netdev, GS_CAN_IDENTIFY_ON);
1139 		break;
1140 	case ETHTOOL_ID_INACTIVE:
1141 		rc = gs_usb_set_identify(netdev, GS_CAN_IDENTIFY_OFF);
1142 		break;
1143 	default:
1144 		break;
1145 	}
1146 
1147 	return rc;
1148 }
1149 
1150 static int gs_usb_get_ts_info(struct net_device *netdev,
1151 			      struct kernel_ethtool_ts_info *info)
1152 {
1153 	struct gs_can *dev = netdev_priv(netdev);
1154 
1155 	/* report if device supports HW timestamps */
1156 	if (dev->feature & GS_CAN_FEATURE_HW_TIMESTAMP)
1157 		return can_ethtool_op_get_ts_info_hwts(netdev, info);
1158 
1159 	return ethtool_op_get_ts_info(netdev, info);
1160 }
1161 
1162 static const struct ethtool_ops gs_usb_ethtool_ops = {
1163 	.set_phys_id = gs_usb_set_phys_id,
1164 	.get_ts_info = gs_usb_get_ts_info,
1165 };
1166 
1167 static int gs_usb_get_termination(struct net_device *netdev, u16 *term)
1168 {
1169 	struct gs_can *dev = netdev_priv(netdev);
1170 	struct gs_device_termination_state term_state;
1171 	int rc;
1172 
1173 	rc = usb_control_msg_recv(dev->udev, 0, GS_USB_BREQ_GET_TERMINATION,
1174 				  USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
1175 				  dev->channel, 0,
1176 				  &term_state, sizeof(term_state), 1000,
1177 				  GFP_KERNEL);
1178 	if (rc)
1179 		return rc;
1180 
1181 	if (term_state.state == cpu_to_le32(GS_CAN_TERMINATION_STATE_ON))
1182 		*term = GS_USB_TERMINATION_ENABLED;
1183 	else
1184 		*term = GS_USB_TERMINATION_DISABLED;
1185 
1186 	return 0;
1187 }
1188 
1189 static int gs_usb_set_termination(struct net_device *netdev, u16 term)
1190 {
1191 	struct gs_can *dev = netdev_priv(netdev);
1192 	struct gs_device_termination_state term_state;
1193 
1194 	if (term == GS_USB_TERMINATION_ENABLED)
1195 		term_state.state = cpu_to_le32(GS_CAN_TERMINATION_STATE_ON);
1196 	else
1197 		term_state.state = cpu_to_le32(GS_CAN_TERMINATION_STATE_OFF);
1198 
1199 	return usb_control_msg_send(dev->udev, 0, GS_USB_BREQ_SET_TERMINATION,
1200 				    USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
1201 				    dev->channel, 0,
1202 				    &term_state, sizeof(term_state), 1000,
1203 				    GFP_KERNEL);
1204 }
1205 
1206 static const u16 gs_usb_termination_const[] = {
1207 	GS_USB_TERMINATION_DISABLED,
1208 	GS_USB_TERMINATION_ENABLED
1209 };
1210 
1211 static struct gs_can *gs_make_candev(unsigned int channel,
1212 				     struct usb_interface *intf,
1213 				     struct gs_device_config *dconf)
1214 {
1215 	struct gs_can *dev;
1216 	struct net_device *netdev;
1217 	int rc;
1218 	struct gs_device_bt_const_extended bt_const_extended;
1219 	struct gs_device_bt_const bt_const;
1220 	u32 feature;
1221 
1222 	/* fetch bit timing constants */
1223 	rc = usb_control_msg_recv(interface_to_usbdev(intf), 0,
1224 				  GS_USB_BREQ_BT_CONST,
1225 				  USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
1226 				  channel, 0, &bt_const, sizeof(bt_const), 1000,
1227 				  GFP_KERNEL);
1228 
1229 	if (rc) {
1230 		dev_err(&intf->dev,
1231 			"Couldn't get bit timing const for channel %d (%pe)\n",
1232 			channel, ERR_PTR(rc));
1233 		return ERR_PTR(rc);
1234 	}
1235 
1236 	/* create netdev */
1237 	netdev = alloc_candev(sizeof(struct gs_can), GS_MAX_TX_URBS);
1238 	if (!netdev) {
1239 		dev_err(&intf->dev, "Couldn't allocate candev\n");
1240 		return ERR_PTR(-ENOMEM);
1241 	}
1242 
1243 	dev = netdev_priv(netdev);
1244 
1245 	netdev->netdev_ops = &gs_usb_netdev_ops;
1246 	netdev->ethtool_ops = &gs_usb_ethtool_ops;
1247 
1248 	netdev->flags |= IFF_ECHO; /* we support full roundtrip echo */
1249 	netdev->dev_id = channel;
1250 
1251 	/* dev setup */
1252 	strcpy(dev->bt_const.name, KBUILD_MODNAME);
1253 	dev->bt_const.tseg1_min = le32_to_cpu(bt_const.tseg1_min);
1254 	dev->bt_const.tseg1_max = le32_to_cpu(bt_const.tseg1_max);
1255 	dev->bt_const.tseg2_min = le32_to_cpu(bt_const.tseg2_min);
1256 	dev->bt_const.tseg2_max = le32_to_cpu(bt_const.tseg2_max);
1257 	dev->bt_const.sjw_max = le32_to_cpu(bt_const.sjw_max);
1258 	dev->bt_const.brp_min = le32_to_cpu(bt_const.brp_min);
1259 	dev->bt_const.brp_max = le32_to_cpu(bt_const.brp_max);
1260 	dev->bt_const.brp_inc = le32_to_cpu(bt_const.brp_inc);
1261 
1262 	dev->udev = interface_to_usbdev(intf);
1263 	dev->netdev = netdev;
1264 	dev->channel = channel;
1265 
1266 	init_usb_anchor(&dev->tx_submitted);
1267 	atomic_set(&dev->active_tx_urbs, 0);
1268 	spin_lock_init(&dev->tx_ctx_lock);
1269 	for (rc = 0; rc < GS_MAX_TX_URBS; rc++) {
1270 		dev->tx_context[rc].dev = dev;
1271 		dev->tx_context[rc].echo_id = GS_MAX_TX_URBS;
1272 	}
1273 
1274 	/* can setup */
1275 	dev->can.state = CAN_STATE_STOPPED;
1276 	dev->can.clock.freq = le32_to_cpu(bt_const.fclk_can);
1277 	dev->can.bittiming_const = &dev->bt_const;
1278 	dev->can.do_set_bittiming = gs_usb_set_bittiming;
1279 
1280 	dev->can.ctrlmode_supported = CAN_CTRLMODE_CC_LEN8_DLC;
1281 
1282 	feature = le32_to_cpu(bt_const.feature);
1283 	dev->feature = FIELD_GET(GS_CAN_FEATURE_MASK, feature);
1284 	if (feature & GS_CAN_FEATURE_LISTEN_ONLY)
1285 		dev->can.ctrlmode_supported |= CAN_CTRLMODE_LISTENONLY;
1286 
1287 	if (feature & GS_CAN_FEATURE_LOOP_BACK)
1288 		dev->can.ctrlmode_supported |= CAN_CTRLMODE_LOOPBACK;
1289 
1290 	if (feature & GS_CAN_FEATURE_TRIPLE_SAMPLE)
1291 		dev->can.ctrlmode_supported |= CAN_CTRLMODE_3_SAMPLES;
1292 
1293 	if (feature & GS_CAN_FEATURE_ONE_SHOT)
1294 		dev->can.ctrlmode_supported |= CAN_CTRLMODE_ONE_SHOT;
1295 
1296 	if (feature & GS_CAN_FEATURE_FD) {
1297 		dev->can.ctrlmode_supported |= CAN_CTRLMODE_FD;
1298 		/* The data bit timing will be overwritten, if
1299 		 * GS_CAN_FEATURE_BT_CONST_EXT is set.
1300 		 */
1301 		dev->can.data_bittiming_const = &dev->bt_const;
1302 		dev->can.do_set_data_bittiming = gs_usb_set_data_bittiming;
1303 	}
1304 
1305 	if (feature & GS_CAN_FEATURE_TERMINATION) {
1306 		rc = gs_usb_get_termination(netdev, &dev->can.termination);
1307 		if (rc) {
1308 			dev->feature &= ~GS_CAN_FEATURE_TERMINATION;
1309 
1310 			dev_info(&intf->dev,
1311 				 "Disabling termination support for channel %d (%pe)\n",
1312 				 channel, ERR_PTR(rc));
1313 		} else {
1314 			dev->can.termination_const = gs_usb_termination_const;
1315 			dev->can.termination_const_cnt = ARRAY_SIZE(gs_usb_termination_const);
1316 			dev->can.do_set_termination = gs_usb_set_termination;
1317 		}
1318 	}
1319 
1320 	if (feature & GS_CAN_FEATURE_BERR_REPORTING)
1321 		dev->can.ctrlmode_supported |= CAN_CTRLMODE_BERR_REPORTING;
1322 
1323 	if (feature & GS_CAN_FEATURE_GET_STATE)
1324 		dev->can.do_get_berr_counter = gs_usb_can_get_berr_counter;
1325 
1326 	/* The CANtact Pro from LinkLayer Labs is based on the
1327 	 * LPC54616 µC, which is affected by the NXP LPC USB transfer
1328 	 * erratum. However, the current firmware (version 2) doesn't
1329 	 * set the GS_CAN_FEATURE_REQ_USB_QUIRK_LPC546XX bit. Set the
1330 	 * feature GS_CAN_FEATURE_REQ_USB_QUIRK_LPC546XX to workaround
1331 	 * this issue.
1332 	 *
1333 	 * For the GS_USB_BREQ_DATA_BITTIMING USB control message the
1334 	 * CANtact Pro firmware uses a request value, which is already
1335 	 * used by the candleLight firmware for a different purpose
1336 	 * (GS_USB_BREQ_GET_USER_ID). Set the feature
1337 	 * GS_CAN_FEATURE_QUIRK_BREQ_CANTACT_PRO to workaround this
1338 	 * issue.
1339 	 */
1340 	if (dev->udev->descriptor.idVendor == cpu_to_le16(USB_GS_USB_1_VENDOR_ID) &&
1341 	    dev->udev->descriptor.idProduct == cpu_to_le16(USB_GS_USB_1_PRODUCT_ID) &&
1342 	    dev->udev->manufacturer && dev->udev->product &&
1343 	    !strcmp(dev->udev->manufacturer, "LinkLayer Labs") &&
1344 	    !strcmp(dev->udev->product, "CANtact Pro") &&
1345 	    (le32_to_cpu(dconf->sw_version) <= 2))
1346 		dev->feature |= GS_CAN_FEATURE_REQ_USB_QUIRK_LPC546XX |
1347 			GS_CAN_FEATURE_QUIRK_BREQ_CANTACT_PRO;
1348 
1349 	/* GS_CAN_FEATURE_IDENTIFY is only supported for sw_version > 1 */
1350 	if (!(le32_to_cpu(dconf->sw_version) > 1 &&
1351 	      feature & GS_CAN_FEATURE_IDENTIFY))
1352 		dev->feature &= ~GS_CAN_FEATURE_IDENTIFY;
1353 
1354 	/* fetch extended bit timing constants if device has feature
1355 	 * GS_CAN_FEATURE_FD and GS_CAN_FEATURE_BT_CONST_EXT
1356 	 */
1357 	if (feature & GS_CAN_FEATURE_FD &&
1358 	    feature & GS_CAN_FEATURE_BT_CONST_EXT) {
1359 		rc = usb_control_msg_recv(interface_to_usbdev(intf), 0,
1360 					  GS_USB_BREQ_BT_CONST_EXT,
1361 					  USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
1362 					  channel, 0, &bt_const_extended,
1363 					  sizeof(bt_const_extended),
1364 					  1000, GFP_KERNEL);
1365 		if (rc) {
1366 			dev_err(&intf->dev,
1367 				"Couldn't get extended bit timing const for channel %d (%pe)\n",
1368 				channel, ERR_PTR(rc));
1369 			goto out_free_candev;
1370 		}
1371 
1372 		strcpy(dev->data_bt_const.name, KBUILD_MODNAME);
1373 		dev->data_bt_const.tseg1_min = le32_to_cpu(bt_const_extended.dtseg1_min);
1374 		dev->data_bt_const.tseg1_max = le32_to_cpu(bt_const_extended.dtseg1_max);
1375 		dev->data_bt_const.tseg2_min = le32_to_cpu(bt_const_extended.dtseg2_min);
1376 		dev->data_bt_const.tseg2_max = le32_to_cpu(bt_const_extended.dtseg2_max);
1377 		dev->data_bt_const.sjw_max = le32_to_cpu(bt_const_extended.dsjw_max);
1378 		dev->data_bt_const.brp_min = le32_to_cpu(bt_const_extended.dbrp_min);
1379 		dev->data_bt_const.brp_max = le32_to_cpu(bt_const_extended.dbrp_max);
1380 		dev->data_bt_const.brp_inc = le32_to_cpu(bt_const_extended.dbrp_inc);
1381 
1382 		dev->can.data_bittiming_const = &dev->data_bt_const;
1383 	}
1384 
1385 	can_rx_offload_add_manual(netdev, &dev->offload, GS_NAPI_WEIGHT);
1386 	SET_NETDEV_DEV(netdev, &intf->dev);
1387 
1388 	rc = register_candev(dev->netdev);
1389 	if (rc) {
1390 		dev_err(&intf->dev,
1391 			"Couldn't register candev for channel %d (%pe)\n",
1392 			channel, ERR_PTR(rc));
1393 		goto out_can_rx_offload_del;
1394 	}
1395 
1396 	return dev;
1397 
1398 out_can_rx_offload_del:
1399 	can_rx_offload_del(&dev->offload);
1400 out_free_candev:
1401 	free_candev(dev->netdev);
1402 	return ERR_PTR(rc);
1403 }
1404 
1405 static void gs_destroy_candev(struct gs_can *dev)
1406 {
1407 	unregister_candev(dev->netdev);
1408 	can_rx_offload_del(&dev->offload);
1409 	free_candev(dev->netdev);
1410 }
1411 
1412 static int gs_usb_probe(struct usb_interface *intf,
1413 			const struct usb_device_id *id)
1414 {
1415 	struct usb_device *udev = interface_to_usbdev(intf);
1416 	struct gs_host_frame *hf;
1417 	struct gs_usb *parent;
1418 	struct gs_host_config hconf = {
1419 		.byte_order = cpu_to_le32(0x0000beef),
1420 	};
1421 	struct gs_device_config dconf;
1422 	unsigned int icount, i;
1423 	int rc;
1424 
1425 	/* send host config */
1426 	rc = usb_control_msg_send(udev, 0,
1427 				  GS_USB_BREQ_HOST_FORMAT,
1428 				  USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
1429 				  1, intf->cur_altsetting->desc.bInterfaceNumber,
1430 				  &hconf, sizeof(hconf), 1000,
1431 				  GFP_KERNEL);
1432 	if (rc) {
1433 		dev_err(&intf->dev, "Couldn't send data format (err=%d)\n", rc);
1434 		return rc;
1435 	}
1436 
1437 	/* read device config */
1438 	rc = usb_control_msg_recv(udev, 0,
1439 				  GS_USB_BREQ_DEVICE_CONFIG,
1440 				  USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
1441 				  1, intf->cur_altsetting->desc.bInterfaceNumber,
1442 				  &dconf, sizeof(dconf), 1000,
1443 				  GFP_KERNEL);
1444 	if (rc) {
1445 		dev_err(&intf->dev, "Couldn't get device config: (err=%d)\n",
1446 			rc);
1447 		return rc;
1448 	}
1449 
1450 	icount = dconf.icount + 1;
1451 	dev_info(&intf->dev, "Configuring for %u interfaces\n", icount);
1452 
1453 	if (icount > GS_MAX_INTF) {
1454 		dev_err(&intf->dev,
1455 			"Driver cannot handle more that %u CAN interfaces\n",
1456 			GS_MAX_INTF);
1457 		return -EINVAL;
1458 	}
1459 
1460 	parent = kzalloc(sizeof(*parent), GFP_KERNEL);
1461 	if (!parent)
1462 		return -ENOMEM;
1463 
1464 	init_usb_anchor(&parent->rx_submitted);
1465 
1466 	usb_set_intfdata(intf, parent);
1467 	parent->udev = udev;
1468 
1469 	for (i = 0; i < icount; i++) {
1470 		unsigned int hf_size_rx = 0;
1471 
1472 		parent->canch[i] = gs_make_candev(i, intf, &dconf);
1473 		if (IS_ERR_OR_NULL(parent->canch[i])) {
1474 			/* save error code to return later */
1475 			rc = PTR_ERR(parent->canch[i]);
1476 
1477 			/* on failure destroy previously created candevs */
1478 			icount = i;
1479 			for (i = 0; i < icount; i++)
1480 				gs_destroy_candev(parent->canch[i]);
1481 
1482 			usb_kill_anchored_urbs(&parent->rx_submitted);
1483 			kfree(parent);
1484 			return rc;
1485 		}
1486 		parent->canch[i]->parent = parent;
1487 
1488 		/* set RX packet size based on FD and if hardware
1489 		 * timestamps are supported.
1490 		 */
1491 		if (parent->canch[i]->can.ctrlmode_supported & CAN_CTRLMODE_FD) {
1492 			if (parent->canch[i]->feature & GS_CAN_FEATURE_HW_TIMESTAMP)
1493 				hf_size_rx = struct_size(hf, canfd_ts, 1);
1494 			else
1495 				hf_size_rx = struct_size(hf, canfd, 1);
1496 		} else {
1497 			if (parent->canch[i]->feature & GS_CAN_FEATURE_HW_TIMESTAMP)
1498 				hf_size_rx = struct_size(hf, classic_can_ts, 1);
1499 			else
1500 				hf_size_rx = struct_size(hf, classic_can, 1);
1501 		}
1502 		parent->hf_size_rx = max(parent->hf_size_rx, hf_size_rx);
1503 	}
1504 
1505 	return 0;
1506 }
1507 
1508 static void gs_usb_disconnect(struct usb_interface *intf)
1509 {
1510 	struct gs_usb *parent = usb_get_intfdata(intf);
1511 	unsigned int i;
1512 
1513 	usb_set_intfdata(intf, NULL);
1514 
1515 	if (!parent) {
1516 		dev_err(&intf->dev, "Disconnect (nodata)\n");
1517 		return;
1518 	}
1519 
1520 	for (i = 0; i < GS_MAX_INTF; i++)
1521 		if (parent->canch[i])
1522 			gs_destroy_candev(parent->canch[i]);
1523 
1524 	kfree(parent);
1525 }
1526 
1527 static const struct usb_device_id gs_usb_table[] = {
1528 	{ USB_DEVICE_INTERFACE_NUMBER(USB_GS_USB_1_VENDOR_ID,
1529 				      USB_GS_USB_1_PRODUCT_ID, 0) },
1530 	{ USB_DEVICE_INTERFACE_NUMBER(USB_CANDLELIGHT_VENDOR_ID,
1531 				      USB_CANDLELIGHT_PRODUCT_ID, 0) },
1532 	{ USB_DEVICE_INTERFACE_NUMBER(USB_CES_CANEXT_FD_VENDOR_ID,
1533 				      USB_CES_CANEXT_FD_PRODUCT_ID, 0) },
1534 	{ USB_DEVICE_INTERFACE_NUMBER(USB_ABE_CANDEBUGGER_FD_VENDOR_ID,
1535 				      USB_ABE_CANDEBUGGER_FD_PRODUCT_ID, 0) },
1536 	{ USB_DEVICE_INTERFACE_NUMBER(USB_XYLANTA_SAINT3_VENDOR_ID,
1537 				      USB_XYLANTA_SAINT3_PRODUCT_ID, 0) },
1538 	{} /* Terminating entry */
1539 };
1540 
1541 MODULE_DEVICE_TABLE(usb, gs_usb_table);
1542 
1543 static struct usb_driver gs_usb_driver = {
1544 	.name = KBUILD_MODNAME,
1545 	.probe = gs_usb_probe,
1546 	.disconnect = gs_usb_disconnect,
1547 	.id_table = gs_usb_table,
1548 };
1549 
1550 module_usb_driver(gs_usb_driver);
1551 
1552 MODULE_AUTHOR("Maximilian Schneider <mws@schneidersoft.net>");
1553 MODULE_DESCRIPTION(
1554 "Socket CAN device driver for Geschwister Schneider Technologie-, "
1555 "Entwicklungs- und Vertriebs UG. USB2.0 to CAN interfaces\n"
1556 "and bytewerk.org candleLight USB CAN interfaces.");
1557 MODULE_LICENSE("GPL v2");
1558