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