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