xref: /openbmc/linux/drivers/net/can/usb/gs_usb.c (revision 12e24d8a)
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 /* Timestamp 32 bit timer runs at 1 MHz (1 µs tick). Worker accounts
47  * for timer overflow (will be after ~71 minutes)
48  */
49 #define GS_USB_TIMESTAMP_TIMER_HZ (1 * HZ_PER_MHZ)
50 #define GS_USB_TIMESTAMP_WORK_DELAY_SEC 1800
51 static_assert(GS_USB_TIMESTAMP_WORK_DELAY_SEC <
52 	      CYCLECOUNTER_MASK(32) / GS_USB_TIMESTAMP_TIMER_HZ / 2);
53 
54 /* Device specific constants */
55 enum gs_usb_breq {
56 	GS_USB_BREQ_HOST_FORMAT = 0,
57 	GS_USB_BREQ_BITTIMING,
58 	GS_USB_BREQ_MODE,
59 	GS_USB_BREQ_BERR,
60 	GS_USB_BREQ_BT_CONST,
61 	GS_USB_BREQ_DEVICE_CONFIG,
62 	GS_USB_BREQ_TIMESTAMP,
63 	GS_USB_BREQ_IDENTIFY,
64 	GS_USB_BREQ_GET_USER_ID,
65 	GS_USB_BREQ_QUIRK_CANTACT_PRO_DATA_BITTIMING = GS_USB_BREQ_GET_USER_ID,
66 	GS_USB_BREQ_SET_USER_ID,
67 	GS_USB_BREQ_DATA_BITTIMING,
68 	GS_USB_BREQ_BT_CONST_EXT,
69 	GS_USB_BREQ_SET_TERMINATION,
70 	GS_USB_BREQ_GET_TERMINATION,
71 	GS_USB_BREQ_GET_STATE,
72 };
73 
74 enum gs_can_mode {
75 	/* reset a channel. turns it off */
76 	GS_CAN_MODE_RESET = 0,
77 	/* starts a channel */
78 	GS_CAN_MODE_START
79 };
80 
81 enum gs_can_state {
82 	GS_CAN_STATE_ERROR_ACTIVE = 0,
83 	GS_CAN_STATE_ERROR_WARNING,
84 	GS_CAN_STATE_ERROR_PASSIVE,
85 	GS_CAN_STATE_BUS_OFF,
86 	GS_CAN_STATE_STOPPED,
87 	GS_CAN_STATE_SLEEPING
88 };
89 
90 enum gs_can_identify_mode {
91 	GS_CAN_IDENTIFY_OFF = 0,
92 	GS_CAN_IDENTIFY_ON
93 };
94 
95 enum gs_can_termination_state {
96 	GS_CAN_TERMINATION_STATE_OFF = 0,
97 	GS_CAN_TERMINATION_STATE_ON
98 };
99 
100 #define GS_USB_TERMINATION_DISABLED CAN_TERMINATION_DISABLED
101 #define GS_USB_TERMINATION_ENABLED 120
102 
103 /* data types passed between host and device */
104 
105 /* The firmware on the original USB2CAN by Geschwister Schneider
106  * Technologie Entwicklungs- und Vertriebs UG exchanges all data
107  * between the host and the device in host byte order. This is done
108  * with the struct gs_host_config::byte_order member, which is sent
109  * first to indicate the desired byte order.
110  *
111  * The widely used open source firmware candleLight doesn't support
112  * this feature and exchanges the data in little endian byte order.
113  */
114 struct gs_host_config {
115 	__le32 byte_order;
116 } __packed;
117 
118 struct gs_device_config {
119 	u8 reserved1;
120 	u8 reserved2;
121 	u8 reserved3;
122 	u8 icount;
123 	__le32 sw_version;
124 	__le32 hw_version;
125 } __packed;
126 
127 #define GS_CAN_MODE_NORMAL 0
128 #define GS_CAN_MODE_LISTEN_ONLY BIT(0)
129 #define GS_CAN_MODE_LOOP_BACK BIT(1)
130 #define GS_CAN_MODE_TRIPLE_SAMPLE BIT(2)
131 #define GS_CAN_MODE_ONE_SHOT BIT(3)
132 #define GS_CAN_MODE_HW_TIMESTAMP BIT(4)
133 /* GS_CAN_FEATURE_IDENTIFY BIT(5) */
134 /* GS_CAN_FEATURE_USER_ID BIT(6) */
135 #define GS_CAN_MODE_PAD_PKTS_TO_MAX_PKT_SIZE BIT(7)
136 #define GS_CAN_MODE_FD BIT(8)
137 /* GS_CAN_FEATURE_REQ_USB_QUIRK_LPC546XX BIT(9) */
138 /* GS_CAN_FEATURE_BT_CONST_EXT BIT(10) */
139 /* GS_CAN_FEATURE_TERMINATION BIT(11) */
140 #define GS_CAN_MODE_BERR_REPORTING BIT(12)
141 /* GS_CAN_FEATURE_GET_STATE BIT(13) */
142 
143 struct gs_device_mode {
144 	__le32 mode;
145 	__le32 flags;
146 } __packed;
147 
148 struct gs_device_state {
149 	__le32 state;
150 	__le32 rxerr;
151 	__le32 txerr;
152 } __packed;
153 
154 struct gs_device_bittiming {
155 	__le32 prop_seg;
156 	__le32 phase_seg1;
157 	__le32 phase_seg2;
158 	__le32 sjw;
159 	__le32 brp;
160 } __packed;
161 
162 struct gs_identify_mode {
163 	__le32 mode;
164 } __packed;
165 
166 struct gs_device_termination_state {
167 	__le32 state;
168 } __packed;
169 
170 #define GS_CAN_FEATURE_LISTEN_ONLY BIT(0)
171 #define GS_CAN_FEATURE_LOOP_BACK BIT(1)
172 #define GS_CAN_FEATURE_TRIPLE_SAMPLE BIT(2)
173 #define GS_CAN_FEATURE_ONE_SHOT BIT(3)
174 #define GS_CAN_FEATURE_HW_TIMESTAMP BIT(4)
175 #define GS_CAN_FEATURE_IDENTIFY BIT(5)
176 #define GS_CAN_FEATURE_USER_ID BIT(6)
177 #define GS_CAN_FEATURE_PAD_PKTS_TO_MAX_PKT_SIZE BIT(7)
178 #define GS_CAN_FEATURE_FD BIT(8)
179 #define GS_CAN_FEATURE_REQ_USB_QUIRK_LPC546XX BIT(9)
180 #define GS_CAN_FEATURE_BT_CONST_EXT BIT(10)
181 #define GS_CAN_FEATURE_TERMINATION BIT(11)
182 #define GS_CAN_FEATURE_BERR_REPORTING BIT(12)
183 #define GS_CAN_FEATURE_GET_STATE BIT(13)
184 #define GS_CAN_FEATURE_MASK GENMASK(13, 0)
185 
186 /* internal quirks - keep in GS_CAN_FEATURE space for now */
187 
188 /* CANtact Pro original firmware:
189  * BREQ DATA_BITTIMING overlaps with GET_USER_ID
190  */
191 #define GS_CAN_FEATURE_QUIRK_BREQ_CANTACT_PRO BIT(31)
192 
193 struct gs_device_bt_const {
194 	__le32 feature;
195 	__le32 fclk_can;
196 	__le32 tseg1_min;
197 	__le32 tseg1_max;
198 	__le32 tseg2_min;
199 	__le32 tseg2_max;
200 	__le32 sjw_max;
201 	__le32 brp_min;
202 	__le32 brp_max;
203 	__le32 brp_inc;
204 } __packed;
205 
206 struct gs_device_bt_const_extended {
207 	__le32 feature;
208 	__le32 fclk_can;
209 	__le32 tseg1_min;
210 	__le32 tseg1_max;
211 	__le32 tseg2_min;
212 	__le32 tseg2_max;
213 	__le32 sjw_max;
214 	__le32 brp_min;
215 	__le32 brp_max;
216 	__le32 brp_inc;
217 
218 	__le32 dtseg1_min;
219 	__le32 dtseg1_max;
220 	__le32 dtseg2_min;
221 	__le32 dtseg2_max;
222 	__le32 dsjw_max;
223 	__le32 dbrp_min;
224 	__le32 dbrp_max;
225 	__le32 dbrp_inc;
226 } __packed;
227 
228 #define GS_CAN_FLAG_OVERFLOW BIT(0)
229 #define GS_CAN_FLAG_FD BIT(1)
230 #define GS_CAN_FLAG_BRS BIT(2)
231 #define GS_CAN_FLAG_ESI BIT(3)
232 
233 struct classic_can {
234 	u8 data[8];
235 } __packed;
236 
237 struct classic_can_ts {
238 	u8 data[8];
239 	__le32 timestamp_us;
240 } __packed;
241 
242 struct classic_can_quirk {
243 	u8 data[8];
244 	u8 quirk;
245 } __packed;
246 
247 struct canfd {
248 	u8 data[64];
249 } __packed;
250 
251 struct canfd_ts {
252 	u8 data[64];
253 	__le32 timestamp_us;
254 } __packed;
255 
256 struct canfd_quirk {
257 	u8 data[64];
258 	u8 quirk;
259 } __packed;
260 
261 struct gs_host_frame {
262 	u32 echo_id;
263 	__le32 can_id;
264 
265 	u8 can_dlc;
266 	u8 channel;
267 	u8 flags;
268 	u8 reserved;
269 
270 	union {
271 		DECLARE_FLEX_ARRAY(struct classic_can, classic_can);
272 		DECLARE_FLEX_ARRAY(struct classic_can_ts, classic_can_ts);
273 		DECLARE_FLEX_ARRAY(struct classic_can_quirk, classic_can_quirk);
274 		DECLARE_FLEX_ARRAY(struct canfd, canfd);
275 		DECLARE_FLEX_ARRAY(struct canfd_ts, canfd_ts);
276 		DECLARE_FLEX_ARRAY(struct canfd_quirk, canfd_quirk);
277 	};
278 } __packed;
279 /* The GS USB devices make use of the same flags and masks as in
280  * linux/can.h and linux/can/error.h, and no additional mapping is necessary.
281  */
282 
283 /* Only send a max of GS_MAX_TX_URBS frames per channel at a time. */
284 #define GS_MAX_TX_URBS 10
285 /* Only launch a max of GS_MAX_RX_URBS usb requests at a time. */
286 #define GS_MAX_RX_URBS 30
287 #define GS_NAPI_WEIGHT 32
288 
289 /* Maximum number of interfaces the driver supports per device.
290  * Current hardware only supports 3 interfaces. The future may vary.
291  */
292 #define GS_MAX_INTF 3
293 
294 struct gs_tx_context {
295 	struct gs_can *dev;
296 	unsigned int echo_id;
297 };
298 
299 struct gs_can {
300 	struct can_priv can; /* must be the first member */
301 
302 	struct can_rx_offload offload;
303 	struct gs_usb *parent;
304 
305 	struct net_device *netdev;
306 	struct usb_device *udev;
307 
308 	struct can_bittiming_const bt_const, data_bt_const;
309 	unsigned int channel;	/* channel number */
310 
311 	u32 feature;
312 	unsigned int hf_size_tx;
313 
314 	/* This lock prevents a race condition between xmit and receive. */
315 	spinlock_t tx_ctx_lock;
316 	struct gs_tx_context tx_context[GS_MAX_TX_URBS];
317 
318 	struct usb_anchor tx_submitted;
319 	atomic_t active_tx_urbs;
320 };
321 
322 /* usb interface struct */
323 struct gs_usb {
324 	struct gs_can *canch[GS_MAX_INTF];
325 	struct usb_anchor rx_submitted;
326 	struct usb_device *udev;
327 
328 	/* time counter for hardware timestamps */
329 	struct cyclecounter cc;
330 	struct timecounter tc;
331 	spinlock_t tc_lock; /* spinlock to guard access tc->cycle_last */
332 	struct delayed_work timestamp;
333 
334 	unsigned int hf_size_rx;
335 	u8 active_channels;
336 
337 	unsigned int pipe_in;
338 	unsigned int pipe_out;
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 = 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 			  parent->pipe_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 			  dev->parent->pipe_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 					  dev->parent->pipe_in,
929 					  buf,
930 					  dev->parent->hf_size_rx,
931 					  gs_usb_receive_bulk_callback, parent);
932 			urb->transfer_flags |= URB_FREE_BUFFER;
933 
934 			usb_anchor_urb(urb, &parent->rx_submitted);
935 
936 			rc = usb_submit_urb(urb, GFP_KERNEL);
937 			if (rc) {
938 				if (rc == -ENODEV)
939 					netif_device_detach(dev->netdev);
940 
941 				netdev_err(netdev,
942 					   "usb_submit_urb() failed, error %pe\n",
943 					   ERR_PTR(rc));
944 
945 				goto out_usb_unanchor_urb;
946 			}
947 
948 			/* Drop reference,
949 			 * USB core will take care of freeing it
950 			 */
951 			usb_free_urb(urb);
952 		}
953 	}
954 
955 	/* flags */
956 	if (ctrlmode & CAN_CTRLMODE_LOOPBACK)
957 		flags |= GS_CAN_MODE_LOOP_BACK;
958 
959 	if (ctrlmode & CAN_CTRLMODE_LISTENONLY)
960 		flags |= GS_CAN_MODE_LISTEN_ONLY;
961 
962 	if (ctrlmode & CAN_CTRLMODE_3_SAMPLES)
963 		flags |= GS_CAN_MODE_TRIPLE_SAMPLE;
964 
965 	if (ctrlmode & CAN_CTRLMODE_ONE_SHOT)
966 		flags |= GS_CAN_MODE_ONE_SHOT;
967 
968 	if (ctrlmode & CAN_CTRLMODE_BERR_REPORTING)
969 		flags |= GS_CAN_MODE_BERR_REPORTING;
970 
971 	if (ctrlmode & CAN_CTRLMODE_FD)
972 		flags |= GS_CAN_MODE_FD;
973 
974 	/* if hardware supports timestamps, enable it */
975 	if (dev->feature & GS_CAN_FEATURE_HW_TIMESTAMP)
976 		flags |= GS_CAN_MODE_HW_TIMESTAMP;
977 
978 	/* finally start device */
979 	dev->can.state = CAN_STATE_ERROR_ACTIVE;
980 	dm.flags = cpu_to_le32(flags);
981 	rc = usb_control_msg_send(dev->udev, 0, GS_USB_BREQ_MODE,
982 				  USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
983 				  dev->channel, 0, &dm, sizeof(dm), 1000,
984 				  GFP_KERNEL);
985 	if (rc) {
986 		netdev_err(netdev, "Couldn't start device (err=%d)\n", rc);
987 		dev->can.state = CAN_STATE_STOPPED;
988 
989 		goto out_usb_kill_anchored_urbs;
990 	}
991 
992 	parent->active_channels++;
993 	if (!(dev->can.ctrlmode & CAN_CTRLMODE_LISTENONLY))
994 		netif_start_queue(netdev);
995 
996 	return 0;
997 
998 out_usb_unanchor_urb:
999 	usb_unanchor_urb(urb);
1000 out_usb_free_urb:
1001 	usb_free_urb(urb);
1002 out_usb_kill_anchored_urbs:
1003 	if (!parent->active_channels) {
1004 		usb_kill_anchored_urbs(&dev->tx_submitted);
1005 
1006 		if (dev->feature & GS_CAN_FEATURE_HW_TIMESTAMP)
1007 			gs_usb_timestamp_stop(parent);
1008 	}
1009 
1010 	can_rx_offload_disable(&dev->offload);
1011 	close_candev(netdev);
1012 
1013 	return rc;
1014 }
1015 
1016 static int gs_usb_get_state(const struct net_device *netdev,
1017 			    struct can_berr_counter *bec,
1018 			    enum can_state *state)
1019 {
1020 	struct gs_can *dev = netdev_priv(netdev);
1021 	struct gs_device_state ds;
1022 	int rc;
1023 
1024 	rc = usb_control_msg_recv(dev->udev, 0, GS_USB_BREQ_GET_STATE,
1025 				  USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
1026 				  dev->channel, 0,
1027 				  &ds, sizeof(ds),
1028 				  USB_CTRL_GET_TIMEOUT,
1029 				  GFP_KERNEL);
1030 	if (rc)
1031 		return rc;
1032 
1033 	if (le32_to_cpu(ds.state) >= CAN_STATE_MAX)
1034 		return -EOPNOTSUPP;
1035 
1036 	*state = le32_to_cpu(ds.state);
1037 	bec->txerr = le32_to_cpu(ds.txerr);
1038 	bec->rxerr = le32_to_cpu(ds.rxerr);
1039 
1040 	return 0;
1041 }
1042 
1043 static int gs_usb_can_get_berr_counter(const struct net_device *netdev,
1044 				       struct can_berr_counter *bec)
1045 {
1046 	enum can_state state;
1047 
1048 	return gs_usb_get_state(netdev, bec, &state);
1049 }
1050 
1051 static int gs_can_close(struct net_device *netdev)
1052 {
1053 	int rc;
1054 	struct gs_can *dev = netdev_priv(netdev);
1055 	struct gs_usb *parent = dev->parent;
1056 
1057 	netif_stop_queue(netdev);
1058 
1059 	/* Stop polling */
1060 	parent->active_channels--;
1061 	if (!parent->active_channels) {
1062 		usb_kill_anchored_urbs(&parent->rx_submitted);
1063 
1064 		if (dev->feature & GS_CAN_FEATURE_HW_TIMESTAMP)
1065 			gs_usb_timestamp_stop(parent);
1066 	}
1067 
1068 	/* Stop sending URBs */
1069 	usb_kill_anchored_urbs(&dev->tx_submitted);
1070 	atomic_set(&dev->active_tx_urbs, 0);
1071 
1072 	dev->can.state = CAN_STATE_STOPPED;
1073 
1074 	/* reset the device */
1075 	gs_cmd_reset(dev);
1076 
1077 	/* reset tx contexts */
1078 	for (rc = 0; rc < GS_MAX_TX_URBS; rc++) {
1079 		dev->tx_context[rc].dev = dev;
1080 		dev->tx_context[rc].echo_id = GS_MAX_TX_URBS;
1081 	}
1082 
1083 	can_rx_offload_disable(&dev->offload);
1084 
1085 	/* close the netdev */
1086 	close_candev(netdev);
1087 
1088 	return 0;
1089 }
1090 
1091 static int gs_can_eth_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
1092 {
1093 	const struct gs_can *dev = netdev_priv(netdev);
1094 
1095 	if (dev->feature & GS_CAN_FEATURE_HW_TIMESTAMP)
1096 		return can_eth_ioctl_hwts(netdev, ifr, cmd);
1097 
1098 	return -EOPNOTSUPP;
1099 }
1100 
1101 static const struct net_device_ops gs_usb_netdev_ops = {
1102 	.ndo_open = gs_can_open,
1103 	.ndo_stop = gs_can_close,
1104 	.ndo_start_xmit = gs_can_start_xmit,
1105 	.ndo_change_mtu = can_change_mtu,
1106 	.ndo_eth_ioctl = gs_can_eth_ioctl,
1107 };
1108 
1109 static int gs_usb_set_identify(struct net_device *netdev, bool do_identify)
1110 {
1111 	struct gs_can *dev = netdev_priv(netdev);
1112 	struct gs_identify_mode imode;
1113 
1114 	if (do_identify)
1115 		imode.mode = cpu_to_le32(GS_CAN_IDENTIFY_ON);
1116 	else
1117 		imode.mode = cpu_to_le32(GS_CAN_IDENTIFY_OFF);
1118 
1119 	return usb_control_msg_send(dev->udev, 0, GS_USB_BREQ_IDENTIFY,
1120 				    USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
1121 				    dev->channel, 0, &imode, sizeof(imode), 100,
1122 				    GFP_KERNEL);
1123 }
1124 
1125 /* blink LED's for finding the this interface */
1126 static int gs_usb_set_phys_id(struct net_device *netdev,
1127 			      enum ethtool_phys_id_state state)
1128 {
1129 	const struct gs_can *dev = netdev_priv(netdev);
1130 	int rc = 0;
1131 
1132 	if (!(dev->feature & GS_CAN_FEATURE_IDENTIFY))
1133 		return -EOPNOTSUPP;
1134 
1135 	switch (state) {
1136 	case ETHTOOL_ID_ACTIVE:
1137 		rc = gs_usb_set_identify(netdev, GS_CAN_IDENTIFY_ON);
1138 		break;
1139 	case ETHTOOL_ID_INACTIVE:
1140 		rc = gs_usb_set_identify(netdev, GS_CAN_IDENTIFY_OFF);
1141 		break;
1142 	default:
1143 		break;
1144 	}
1145 
1146 	return rc;
1147 }
1148 
1149 static int gs_usb_get_ts_info(struct net_device *netdev,
1150 			      struct ethtool_ts_info *info)
1151 {
1152 	struct gs_can *dev = netdev_priv(netdev);
1153 
1154 	/* report if device supports HW timestamps */
1155 	if (dev->feature & GS_CAN_FEATURE_HW_TIMESTAMP)
1156 		return can_ethtool_op_get_ts_info_hwts(netdev, info);
1157 
1158 	return ethtool_op_get_ts_info(netdev, info);
1159 }
1160 
1161 static const struct ethtool_ops gs_usb_ethtool_ops = {
1162 	.set_phys_id = gs_usb_set_phys_id,
1163 	.get_ts_info = gs_usb_get_ts_info,
1164 };
1165 
1166 static int gs_usb_get_termination(struct net_device *netdev, u16 *term)
1167 {
1168 	struct gs_can *dev = netdev_priv(netdev);
1169 	struct gs_device_termination_state term_state;
1170 	int rc;
1171 
1172 	rc = usb_control_msg_recv(dev->udev, 0, GS_USB_BREQ_GET_TERMINATION,
1173 				  USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
1174 				  dev->channel, 0,
1175 				  &term_state, sizeof(term_state), 1000,
1176 				  GFP_KERNEL);
1177 	if (rc)
1178 		return rc;
1179 
1180 	if (term_state.state == cpu_to_le32(GS_CAN_TERMINATION_STATE_ON))
1181 		*term = GS_USB_TERMINATION_ENABLED;
1182 	else
1183 		*term = GS_USB_TERMINATION_DISABLED;
1184 
1185 	return 0;
1186 }
1187 
1188 static int gs_usb_set_termination(struct net_device *netdev, u16 term)
1189 {
1190 	struct gs_can *dev = netdev_priv(netdev);
1191 	struct gs_device_termination_state term_state;
1192 
1193 	if (term == GS_USB_TERMINATION_ENABLED)
1194 		term_state.state = cpu_to_le32(GS_CAN_TERMINATION_STATE_ON);
1195 	else
1196 		term_state.state = cpu_to_le32(GS_CAN_TERMINATION_STATE_OFF);
1197 
1198 	return usb_control_msg_send(dev->udev, 0, GS_USB_BREQ_SET_TERMINATION,
1199 				    USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
1200 				    dev->channel, 0,
1201 				    &term_state, sizeof(term_state), 1000,
1202 				    GFP_KERNEL);
1203 }
1204 
1205 static const u16 gs_usb_termination_const[] = {
1206 	GS_USB_TERMINATION_DISABLED,
1207 	GS_USB_TERMINATION_ENABLED
1208 };
1209 
1210 static struct gs_can *gs_make_candev(unsigned int channel,
1211 				     struct usb_interface *intf,
1212 				     struct gs_device_config *dconf)
1213 {
1214 	struct gs_can *dev;
1215 	struct net_device *netdev;
1216 	int rc;
1217 	struct gs_device_bt_const_extended bt_const_extended;
1218 	struct gs_device_bt_const bt_const;
1219 	u32 feature;
1220 
1221 	/* fetch bit timing constants */
1222 	rc = usb_control_msg_recv(interface_to_usbdev(intf), 0,
1223 				  GS_USB_BREQ_BT_CONST,
1224 				  USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
1225 				  channel, 0, &bt_const, sizeof(bt_const), 1000,
1226 				  GFP_KERNEL);
1227 
1228 	if (rc) {
1229 		dev_err(&intf->dev,
1230 			"Couldn't get bit timing const for channel %d (%pe)\n",
1231 			channel, ERR_PTR(rc));
1232 		return ERR_PTR(rc);
1233 	}
1234 
1235 	/* create netdev */
1236 	netdev = alloc_candev(sizeof(struct gs_can), GS_MAX_TX_URBS);
1237 	if (!netdev) {
1238 		dev_err(&intf->dev, "Couldn't allocate candev\n");
1239 		return ERR_PTR(-ENOMEM);
1240 	}
1241 
1242 	dev = netdev_priv(netdev);
1243 
1244 	netdev->netdev_ops = &gs_usb_netdev_ops;
1245 	netdev->ethtool_ops = &gs_usb_ethtool_ops;
1246 
1247 	netdev->flags |= IFF_ECHO; /* we support full roundtrip echo */
1248 	netdev->dev_id = 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.data_bittiming_const = &dev->bt_const;
1301 		dev->can.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.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 > GS_MAX_INTF) {
1461 		dev_err(&intf->dev,
1462 			"Driver cannot handle more that %u CAN interfaces\n",
1463 			GS_MAX_INTF);
1464 		return -EINVAL;
1465 	}
1466 
1467 	parent = kzalloc(sizeof(*parent), GFP_KERNEL);
1468 	if (!parent)
1469 		return -ENOMEM;
1470 
1471 	init_usb_anchor(&parent->rx_submitted);
1472 
1473 	usb_set_intfdata(intf, parent);
1474 	parent->udev = udev;
1475 
1476 	/* store the detected endpoints */
1477 	parent->pipe_in = usb_rcvbulkpipe(parent->udev, ep_in->bEndpointAddress);
1478 	parent->pipe_out = usb_sndbulkpipe(parent->udev, ep_out->bEndpointAddress);
1479 
1480 	for (i = 0; i < icount; i++) {
1481 		unsigned int hf_size_rx = 0;
1482 
1483 		parent->canch[i] = gs_make_candev(i, intf, &dconf);
1484 		if (IS_ERR_OR_NULL(parent->canch[i])) {
1485 			/* save error code to return later */
1486 			rc = PTR_ERR(parent->canch[i]);
1487 
1488 			/* on failure destroy previously created candevs */
1489 			icount = i;
1490 			for (i = 0; i < icount; i++)
1491 				gs_destroy_candev(parent->canch[i]);
1492 
1493 			usb_kill_anchored_urbs(&parent->rx_submitted);
1494 			kfree(parent);
1495 			return rc;
1496 		}
1497 		parent->canch[i]->parent = parent;
1498 
1499 		/* set RX packet size based on FD and if hardware
1500 		 * timestamps are supported.
1501 		 */
1502 		if (parent->canch[i]->can.ctrlmode_supported & CAN_CTRLMODE_FD) {
1503 			if (parent->canch[i]->feature & GS_CAN_FEATURE_HW_TIMESTAMP)
1504 				hf_size_rx = struct_size(hf, canfd_ts, 1);
1505 			else
1506 				hf_size_rx = struct_size(hf, canfd, 1);
1507 		} else {
1508 			if (parent->canch[i]->feature & GS_CAN_FEATURE_HW_TIMESTAMP)
1509 				hf_size_rx = struct_size(hf, classic_can_ts, 1);
1510 			else
1511 				hf_size_rx = struct_size(hf, classic_can, 1);
1512 		}
1513 		parent->hf_size_rx = max(parent->hf_size_rx, hf_size_rx);
1514 	}
1515 
1516 	return 0;
1517 }
1518 
1519 static void gs_usb_disconnect(struct usb_interface *intf)
1520 {
1521 	struct gs_usb *parent = usb_get_intfdata(intf);
1522 	unsigned int i;
1523 
1524 	usb_set_intfdata(intf, NULL);
1525 
1526 	if (!parent) {
1527 		dev_err(&intf->dev, "Disconnect (nodata)\n");
1528 		return;
1529 	}
1530 
1531 	for (i = 0; i < GS_MAX_INTF; i++)
1532 		if (parent->canch[i])
1533 			gs_destroy_candev(parent->canch[i]);
1534 
1535 	kfree(parent);
1536 }
1537 
1538 static const struct usb_device_id gs_usb_table[] = {
1539 	{ USB_DEVICE_INTERFACE_NUMBER(USB_GS_USB_1_VENDOR_ID,
1540 				      USB_GS_USB_1_PRODUCT_ID, 0) },
1541 	{ USB_DEVICE_INTERFACE_NUMBER(USB_CANDLELIGHT_VENDOR_ID,
1542 				      USB_CANDLELIGHT_PRODUCT_ID, 0) },
1543 	{ USB_DEVICE_INTERFACE_NUMBER(USB_CES_CANEXT_FD_VENDOR_ID,
1544 				      USB_CES_CANEXT_FD_PRODUCT_ID, 0) },
1545 	{ USB_DEVICE_INTERFACE_NUMBER(USB_ABE_CANDEBUGGER_FD_VENDOR_ID,
1546 				      USB_ABE_CANDEBUGGER_FD_PRODUCT_ID, 0) },
1547 	{ USB_DEVICE_INTERFACE_NUMBER(USB_XYLANTA_SAINT3_VENDOR_ID,
1548 				      USB_XYLANTA_SAINT3_PRODUCT_ID, 0) },
1549 	{} /* Terminating entry */
1550 };
1551 
1552 MODULE_DEVICE_TABLE(usb, gs_usb_table);
1553 
1554 static struct usb_driver gs_usb_driver = {
1555 	.name = KBUILD_MODNAME,
1556 	.probe = gs_usb_probe,
1557 	.disconnect = gs_usb_disconnect,
1558 	.id_table = gs_usb_table,
1559 };
1560 
1561 module_usb_driver(gs_usb_driver);
1562 
1563 MODULE_AUTHOR("Maximilian Schneider <mws@schneidersoft.net>");
1564 MODULE_DESCRIPTION(
1565 "Socket CAN device driver for Geschwister Schneider Technologie-, "
1566 "Entwicklungs- und Vertriebs UG. USB2.0 to CAN interfaces\n"
1567 "and bytewerk.org candleLight USB CAN interfaces.");
1568 MODULE_LICENSE("GPL v2");
1569