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