xref: /openbmc/linux/drivers/net/can/usb/gs_usb.c (revision 55eb9a6c)
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  *
9  * Many thanks to all socketcan devs!
10  */
11 
12 #include <linux/bitfield.h>
13 #include <linux/ethtool.h>
14 #include <linux/init.h>
15 #include <linux/module.h>
16 #include <linux/netdevice.h>
17 #include <linux/signal.h>
18 #include <linux/usb.h>
19 
20 #include <linux/can.h>
21 #include <linux/can/dev.h>
22 #include <linux/can/error.h>
23 
24 /* Device specific constants */
25 #define USB_GSUSB_1_VENDOR_ID 0x1d50
26 #define USB_GSUSB_1_PRODUCT_ID 0x606f
27 
28 #define USB_CANDLELIGHT_VENDOR_ID 0x1209
29 #define USB_CANDLELIGHT_PRODUCT_ID 0x2323
30 
31 #define USB_CES_CANEXT_FD_VENDOR_ID 0x1cd2
32 #define USB_CES_CANEXT_FD_PRODUCT_ID 0x606f
33 
34 #define USB_ABE_CANDEBUGGER_FD_VENDOR_ID 0x16d0
35 #define USB_ABE_CANDEBUGGER_FD_PRODUCT_ID 0x10b8
36 
37 #define GSUSB_ENDPOINT_IN 1
38 #define GSUSB_ENDPOINT_OUT 2
39 
40 /* Device specific constants */
41 enum gs_usb_breq {
42 	GS_USB_BREQ_HOST_FORMAT = 0,
43 	GS_USB_BREQ_BITTIMING,
44 	GS_USB_BREQ_MODE,
45 	GS_USB_BREQ_BERR,
46 	GS_USB_BREQ_BT_CONST,
47 	GS_USB_BREQ_DEVICE_CONFIG,
48 	GS_USB_BREQ_TIMESTAMP,
49 	GS_USB_BREQ_IDENTIFY,
50 	GS_USB_BREQ_GET_USER_ID,
51 	GS_USB_BREQ_QUIRK_CANTACT_PRO_DATA_BITTIMING = GS_USB_BREQ_GET_USER_ID,
52 	GS_USB_BREQ_SET_USER_ID,
53 	GS_USB_BREQ_DATA_BITTIMING,
54 	GS_USB_BREQ_BT_CONST_EXT,
55 };
56 
57 enum gs_can_mode {
58 	/* reset a channel. turns it off */
59 	GS_CAN_MODE_RESET = 0,
60 	/* starts a channel */
61 	GS_CAN_MODE_START
62 };
63 
64 enum gs_can_state {
65 	GS_CAN_STATE_ERROR_ACTIVE = 0,
66 	GS_CAN_STATE_ERROR_WARNING,
67 	GS_CAN_STATE_ERROR_PASSIVE,
68 	GS_CAN_STATE_BUS_OFF,
69 	GS_CAN_STATE_STOPPED,
70 	GS_CAN_STATE_SLEEPING
71 };
72 
73 enum gs_can_identify_mode {
74 	GS_CAN_IDENTIFY_OFF = 0,
75 	GS_CAN_IDENTIFY_ON
76 };
77 
78 /* data types passed between host and device */
79 
80 /* The firmware on the original USB2CAN by Geschwister Schneider
81  * Technologie Entwicklungs- und Vertriebs UG exchanges all data
82  * between the host and the device in host byte order. This is done
83  * with the struct gs_host_config::byte_order member, which is sent
84  * first to indicate the desired byte order.
85  *
86  * The widely used open source firmware candleLight doesn't support
87  * this feature and exchanges the data in little endian byte order.
88  */
89 struct gs_host_config {
90 	__le32 byte_order;
91 } __packed;
92 
93 struct gs_device_config {
94 	u8 reserved1;
95 	u8 reserved2;
96 	u8 reserved3;
97 	u8 icount;
98 	__le32 sw_version;
99 	__le32 hw_version;
100 } __packed;
101 
102 #define GS_CAN_MODE_NORMAL 0
103 #define GS_CAN_MODE_LISTEN_ONLY BIT(0)
104 #define GS_CAN_MODE_LOOP_BACK BIT(1)
105 #define GS_CAN_MODE_TRIPLE_SAMPLE BIT(2)
106 #define GS_CAN_MODE_ONE_SHOT BIT(3)
107 #define GS_CAN_MODE_HW_TIMESTAMP BIT(4)
108 /* GS_CAN_FEATURE_IDENTIFY BIT(5) */
109 /* GS_CAN_FEATURE_USER_ID BIT(6) */
110 #define GS_CAN_MODE_PAD_PKTS_TO_MAX_PKT_SIZE BIT(7)
111 #define GS_CAN_MODE_FD BIT(8)
112 /* GS_CAN_FEATURE_REQ_USB_QUIRK_LPC546XX BIT(9) */
113 /* GS_CAN_FEATURE_BT_CONST_EXT BIT(10) */
114 
115 struct gs_device_mode {
116 	__le32 mode;
117 	__le32 flags;
118 } __packed;
119 
120 struct gs_device_state {
121 	__le32 state;
122 	__le32 rxerr;
123 	__le32 txerr;
124 } __packed;
125 
126 struct gs_device_bittiming {
127 	__le32 prop_seg;
128 	__le32 phase_seg1;
129 	__le32 phase_seg2;
130 	__le32 sjw;
131 	__le32 brp;
132 } __packed;
133 
134 struct gs_identify_mode {
135 	__le32 mode;
136 } __packed;
137 
138 #define GS_CAN_FEATURE_LISTEN_ONLY BIT(0)
139 #define GS_CAN_FEATURE_LOOP_BACK BIT(1)
140 #define GS_CAN_FEATURE_TRIPLE_SAMPLE BIT(2)
141 #define GS_CAN_FEATURE_ONE_SHOT BIT(3)
142 #define GS_CAN_FEATURE_HW_TIMESTAMP BIT(4)
143 #define GS_CAN_FEATURE_IDENTIFY BIT(5)
144 #define GS_CAN_FEATURE_USER_ID BIT(6)
145 #define GS_CAN_FEATURE_PAD_PKTS_TO_MAX_PKT_SIZE BIT(7)
146 #define GS_CAN_FEATURE_FD BIT(8)
147 #define GS_CAN_FEATURE_REQ_USB_QUIRK_LPC546XX BIT(9)
148 #define GS_CAN_FEATURE_BT_CONST_EXT BIT(10)
149 #define GS_CAN_FEATURE_MASK GENMASK(10, 0)
150 
151 /* internal quirks - keep in GS_CAN_FEATURE space for now */
152 
153 /* CANtact Pro original firmware:
154  * BREQ DATA_BITTIMING overlaps with GET_USER_ID
155  */
156 #define GS_CAN_FEATURE_QUIRK_BREQ_CANTACT_PRO BIT(31)
157 
158 struct gs_device_bt_const {
159 	__le32 feature;
160 	__le32 fclk_can;
161 	__le32 tseg1_min;
162 	__le32 tseg1_max;
163 	__le32 tseg2_min;
164 	__le32 tseg2_max;
165 	__le32 sjw_max;
166 	__le32 brp_min;
167 	__le32 brp_max;
168 	__le32 brp_inc;
169 } __packed;
170 
171 struct gs_device_bt_const_extended {
172 	__le32 feature;
173 	__le32 fclk_can;
174 	__le32 tseg1_min;
175 	__le32 tseg1_max;
176 	__le32 tseg2_min;
177 	__le32 tseg2_max;
178 	__le32 sjw_max;
179 	__le32 brp_min;
180 	__le32 brp_max;
181 	__le32 brp_inc;
182 
183 	__le32 dtseg1_min;
184 	__le32 dtseg1_max;
185 	__le32 dtseg2_min;
186 	__le32 dtseg2_max;
187 	__le32 dsjw_max;
188 	__le32 dbrp_min;
189 	__le32 dbrp_max;
190 	__le32 dbrp_inc;
191 } __packed;
192 
193 #define GS_CAN_FLAG_OVERFLOW BIT(0)
194 #define GS_CAN_FLAG_FD BIT(1)
195 #define GS_CAN_FLAG_BRS BIT(2)
196 #define GS_CAN_FLAG_ESI BIT(3)
197 
198 struct classic_can {
199 	u8 data[8];
200 } __packed;
201 
202 struct classic_can_quirk {
203 	u8 data[8];
204 	u8 quirk;
205 } __packed;
206 
207 struct canfd {
208 	u8 data[64];
209 } __packed;
210 
211 struct canfd_quirk {
212 	u8 data[64];
213 	u8 quirk;
214 } __packed;
215 
216 struct gs_host_frame {
217 	u32 echo_id;
218 	__le32 can_id;
219 
220 	u8 can_dlc;
221 	u8 channel;
222 	u8 flags;
223 	u8 reserved;
224 
225 	union {
226 		DECLARE_FLEX_ARRAY(struct classic_can, classic_can);
227 		DECLARE_FLEX_ARRAY(struct classic_can_quirk, classic_can_quirk);
228 		DECLARE_FLEX_ARRAY(struct canfd, canfd);
229 		DECLARE_FLEX_ARRAY(struct canfd_quirk, canfd_quirk);
230 	};
231 } __packed;
232 /* The GS USB devices make use of the same flags and masks as in
233  * linux/can.h and linux/can/error.h, and no additional mapping is necessary.
234  */
235 
236 /* Only send a max of GS_MAX_TX_URBS frames per channel at a time. */
237 #define GS_MAX_TX_URBS 10
238 /* Only launch a max of GS_MAX_RX_URBS usb requests at a time. */
239 #define GS_MAX_RX_URBS 30
240 /* Maximum number of interfaces the driver supports per device.
241  * Current hardware only supports 3 interfaces. The future may vary.
242  */
243 #define GS_MAX_INTF 3
244 
245 struct gs_tx_context {
246 	struct gs_can *dev;
247 	unsigned int echo_id;
248 };
249 
250 struct gs_can {
251 	struct can_priv can; /* must be the first member */
252 
253 	struct gs_usb *parent;
254 
255 	struct net_device *netdev;
256 	struct usb_device *udev;
257 	struct usb_interface *iface;
258 
259 	struct can_bittiming_const bt_const, data_bt_const;
260 	unsigned int channel;	/* channel number */
261 
262 	u32 feature;
263 	unsigned int hf_size_tx;
264 
265 	/* This lock prevents a race condition between xmit and receive. */
266 	spinlock_t tx_ctx_lock;
267 	struct gs_tx_context tx_context[GS_MAX_TX_URBS];
268 
269 	struct usb_anchor tx_submitted;
270 	atomic_t active_tx_urbs;
271 };
272 
273 /* usb interface struct */
274 struct gs_usb {
275 	struct gs_can *canch[GS_MAX_INTF];
276 	struct usb_anchor rx_submitted;
277 	struct usb_device *udev;
278 	unsigned int hf_size_rx;
279 	u8 active_channels;
280 };
281 
282 /* 'allocate' a tx context.
283  * returns a valid tx context or NULL if there is no space.
284  */
285 static struct gs_tx_context *gs_alloc_tx_context(struct gs_can *dev)
286 {
287 	int i = 0;
288 	unsigned long flags;
289 
290 	spin_lock_irqsave(&dev->tx_ctx_lock, flags);
291 
292 	for (; i < GS_MAX_TX_URBS; i++) {
293 		if (dev->tx_context[i].echo_id == GS_MAX_TX_URBS) {
294 			dev->tx_context[i].echo_id = i;
295 			spin_unlock_irqrestore(&dev->tx_ctx_lock, flags);
296 			return &dev->tx_context[i];
297 		}
298 	}
299 
300 	spin_unlock_irqrestore(&dev->tx_ctx_lock, flags);
301 	return NULL;
302 }
303 
304 /* releases a tx context
305  */
306 static void gs_free_tx_context(struct gs_tx_context *txc)
307 {
308 	txc->echo_id = GS_MAX_TX_URBS;
309 }
310 
311 /* Get a tx context by id.
312  */
313 static struct gs_tx_context *gs_get_tx_context(struct gs_can *dev,
314 					       unsigned int id)
315 {
316 	unsigned long flags;
317 
318 	if (id < GS_MAX_TX_URBS) {
319 		spin_lock_irqsave(&dev->tx_ctx_lock, flags);
320 		if (dev->tx_context[id].echo_id == id) {
321 			spin_unlock_irqrestore(&dev->tx_ctx_lock, flags);
322 			return &dev->tx_context[id];
323 		}
324 		spin_unlock_irqrestore(&dev->tx_ctx_lock, flags);
325 	}
326 	return NULL;
327 }
328 
329 static int gs_cmd_reset(struct gs_can *gsdev)
330 {
331 	struct gs_device_mode *dm;
332 	struct usb_interface *intf = gsdev->iface;
333 	int rc;
334 
335 	dm = kzalloc(sizeof(*dm), GFP_KERNEL);
336 	if (!dm)
337 		return -ENOMEM;
338 
339 	dm->mode = GS_CAN_MODE_RESET;
340 
341 	rc = usb_control_msg(interface_to_usbdev(intf),
342 			     usb_sndctrlpipe(interface_to_usbdev(intf), 0),
343 			     GS_USB_BREQ_MODE,
344 			     USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
345 			     gsdev->channel, 0, dm, sizeof(*dm), 1000);
346 
347 	kfree(dm);
348 
349 	return rc;
350 }
351 
352 static void gs_update_state(struct gs_can *dev, struct can_frame *cf)
353 {
354 	struct can_device_stats *can_stats = &dev->can.can_stats;
355 
356 	if (cf->can_id & CAN_ERR_RESTARTED) {
357 		dev->can.state = CAN_STATE_ERROR_ACTIVE;
358 		can_stats->restarts++;
359 	} else if (cf->can_id & CAN_ERR_BUSOFF) {
360 		dev->can.state = CAN_STATE_BUS_OFF;
361 		can_stats->bus_off++;
362 	} else if (cf->can_id & CAN_ERR_CRTL) {
363 		if ((cf->data[1] & CAN_ERR_CRTL_TX_WARNING) ||
364 		    (cf->data[1] & CAN_ERR_CRTL_RX_WARNING)) {
365 			dev->can.state = CAN_STATE_ERROR_WARNING;
366 			can_stats->error_warning++;
367 		} else if ((cf->data[1] & CAN_ERR_CRTL_TX_PASSIVE) ||
368 			   (cf->data[1] & CAN_ERR_CRTL_RX_PASSIVE)) {
369 			dev->can.state = CAN_STATE_ERROR_PASSIVE;
370 			can_stats->error_passive++;
371 		} else {
372 			dev->can.state = CAN_STATE_ERROR_ACTIVE;
373 		}
374 	}
375 }
376 
377 static void gs_usb_receive_bulk_callback(struct urb *urb)
378 {
379 	struct gs_usb *usbcan = urb->context;
380 	struct gs_can *dev;
381 	struct net_device *netdev;
382 	int rc;
383 	struct net_device_stats *stats;
384 	struct gs_host_frame *hf = urb->transfer_buffer;
385 	struct gs_tx_context *txc;
386 	struct can_frame *cf;
387 	struct canfd_frame *cfd;
388 	struct sk_buff *skb;
389 
390 	BUG_ON(!usbcan);
391 
392 	switch (urb->status) {
393 	case 0: /* success */
394 		break;
395 	case -ENOENT:
396 	case -ESHUTDOWN:
397 		return;
398 	default:
399 		/* do not resubmit aborted urbs. eg: when device goes down */
400 		return;
401 	}
402 
403 	/* device reports out of range channel id */
404 	if (hf->channel >= GS_MAX_INTF)
405 		goto device_detach;
406 
407 	dev = usbcan->canch[hf->channel];
408 
409 	netdev = dev->netdev;
410 	stats = &netdev->stats;
411 
412 	if (!netif_device_present(netdev))
413 		return;
414 
415 	if (hf->echo_id == -1) { /* normal rx */
416 		if (hf->flags & GS_CAN_FLAG_FD) {
417 			skb = alloc_canfd_skb(dev->netdev, &cfd);
418 			if (!skb)
419 				return;
420 
421 			cfd->can_id = le32_to_cpu(hf->can_id);
422 			cfd->len = can_fd_dlc2len(hf->can_dlc);
423 			if (hf->flags & GS_CAN_FLAG_BRS)
424 				cfd->flags |= CANFD_BRS;
425 			if (hf->flags & GS_CAN_FLAG_ESI)
426 				cfd->flags |= CANFD_ESI;
427 
428 			memcpy(cfd->data, hf->canfd->data, cfd->len);
429 		} else {
430 			skb = alloc_can_skb(dev->netdev, &cf);
431 			if (!skb)
432 				return;
433 
434 			cf->can_id = le32_to_cpu(hf->can_id);
435 			can_frame_set_cc_len(cf, hf->can_dlc, dev->can.ctrlmode);
436 
437 			memcpy(cf->data, hf->classic_can->data, 8);
438 
439 			/* ERROR frames tell us information about the controller */
440 			if (le32_to_cpu(hf->can_id) & CAN_ERR_FLAG)
441 				gs_update_state(dev, cf);
442 		}
443 
444 		netdev->stats.rx_packets++;
445 		netdev->stats.rx_bytes += hf->can_dlc;
446 
447 		netif_rx(skb);
448 	} else { /* echo_id == hf->echo_id */
449 		if (hf->echo_id >= GS_MAX_TX_URBS) {
450 			netdev_err(netdev,
451 				   "Unexpected out of range echo id %u\n",
452 				   hf->echo_id);
453 			goto resubmit_urb;
454 		}
455 
456 		txc = gs_get_tx_context(dev, hf->echo_id);
457 
458 		/* bad devices send bad echo_ids. */
459 		if (!txc) {
460 			netdev_err(netdev,
461 				   "Unexpected unused echo id %u\n",
462 				   hf->echo_id);
463 			goto resubmit_urb;
464 		}
465 
466 		netdev->stats.tx_packets++;
467 		netdev->stats.tx_bytes += can_get_echo_skb(netdev, hf->echo_id,
468 							   NULL);
469 
470 		gs_free_tx_context(txc);
471 
472 		atomic_dec(&dev->active_tx_urbs);
473 
474 		netif_wake_queue(netdev);
475 	}
476 
477 	if (hf->flags & GS_CAN_FLAG_OVERFLOW) {
478 		skb = alloc_can_err_skb(netdev, &cf);
479 		if (!skb)
480 			goto resubmit_urb;
481 
482 		cf->can_id |= CAN_ERR_CRTL;
483 		cf->len = CAN_ERR_DLC;
484 		cf->data[1] = CAN_ERR_CRTL_RX_OVERFLOW;
485 		stats->rx_over_errors++;
486 		stats->rx_errors++;
487 		netif_rx(skb);
488 	}
489 
490  resubmit_urb:
491 	usb_fill_bulk_urb(urb, usbcan->udev,
492 			  usb_rcvbulkpipe(usbcan->udev, GSUSB_ENDPOINT_IN),
493 			  hf, dev->parent->hf_size_rx,
494 			  gs_usb_receive_bulk_callback, usbcan);
495 
496 	rc = usb_submit_urb(urb, GFP_ATOMIC);
497 
498 	/* USB failure take down all interfaces */
499 	if (rc == -ENODEV) {
500  device_detach:
501 		for (rc = 0; rc < GS_MAX_INTF; rc++) {
502 			if (usbcan->canch[rc])
503 				netif_device_detach(usbcan->canch[rc]->netdev);
504 		}
505 	}
506 }
507 
508 static int gs_usb_set_bittiming(struct net_device *netdev)
509 {
510 	struct gs_can *dev = netdev_priv(netdev);
511 	struct can_bittiming *bt = &dev->can.bittiming;
512 	struct usb_interface *intf = dev->iface;
513 	int rc;
514 	struct gs_device_bittiming *dbt;
515 
516 	dbt = kmalloc(sizeof(*dbt), GFP_KERNEL);
517 	if (!dbt)
518 		return -ENOMEM;
519 
520 	dbt->prop_seg = cpu_to_le32(bt->prop_seg);
521 	dbt->phase_seg1 = cpu_to_le32(bt->phase_seg1);
522 	dbt->phase_seg2 = cpu_to_le32(bt->phase_seg2);
523 	dbt->sjw = cpu_to_le32(bt->sjw);
524 	dbt->brp = cpu_to_le32(bt->brp);
525 
526 	/* request bit timings */
527 	rc = usb_control_msg(interface_to_usbdev(intf),
528 			     usb_sndctrlpipe(interface_to_usbdev(intf), 0),
529 			     GS_USB_BREQ_BITTIMING,
530 			     USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
531 			     dev->channel, 0, dbt, sizeof(*dbt), 1000);
532 
533 	kfree(dbt);
534 
535 	if (rc < 0)
536 		dev_err(netdev->dev.parent, "Couldn't set bittimings (err=%d)",
537 			rc);
538 
539 	return (rc > 0) ? 0 : rc;
540 }
541 
542 static int gs_usb_set_data_bittiming(struct net_device *netdev)
543 {
544 	struct gs_can *dev = netdev_priv(netdev);
545 	struct can_bittiming *bt = &dev->can.data_bittiming;
546 	struct usb_interface *intf = dev->iface;
547 	struct gs_device_bittiming *dbt;
548 	u8 request = GS_USB_BREQ_DATA_BITTIMING;
549 	int rc;
550 
551 	dbt = kmalloc(sizeof(*dbt), GFP_KERNEL);
552 	if (!dbt)
553 		return -ENOMEM;
554 
555 	dbt->prop_seg = cpu_to_le32(bt->prop_seg);
556 	dbt->phase_seg1 = cpu_to_le32(bt->phase_seg1);
557 	dbt->phase_seg2 = cpu_to_le32(bt->phase_seg2);
558 	dbt->sjw = cpu_to_le32(bt->sjw);
559 	dbt->brp = cpu_to_le32(bt->brp);
560 
561 	if (dev->feature & GS_CAN_FEATURE_QUIRK_BREQ_CANTACT_PRO)
562 		request = GS_USB_BREQ_QUIRK_CANTACT_PRO_DATA_BITTIMING;
563 
564 	/* request bit timings */
565 	rc = usb_control_msg(interface_to_usbdev(intf),
566 			     usb_sndctrlpipe(interface_to_usbdev(intf), 0),
567 			     request,
568 			     USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
569 			     dev->channel, 0, dbt, sizeof(*dbt), 1000);
570 
571 	kfree(dbt);
572 
573 	if (rc < 0)
574 		dev_err(netdev->dev.parent,
575 			"Couldn't set data bittimings (err=%d)", rc);
576 
577 	return (rc > 0) ? 0 : rc;
578 }
579 
580 static void gs_usb_xmit_callback(struct urb *urb)
581 {
582 	struct gs_tx_context *txc = urb->context;
583 	struct gs_can *dev = txc->dev;
584 	struct net_device *netdev = dev->netdev;
585 
586 	if (urb->status)
587 		netdev_info(netdev, "usb xmit fail %u\n", txc->echo_id);
588 
589 	usb_free_coherent(urb->dev, urb->transfer_buffer_length,
590 			  urb->transfer_buffer, urb->transfer_dma);
591 }
592 
593 static netdev_tx_t gs_can_start_xmit(struct sk_buff *skb,
594 				     struct net_device *netdev)
595 {
596 	struct gs_can *dev = netdev_priv(netdev);
597 	struct net_device_stats *stats = &dev->netdev->stats;
598 	struct urb *urb;
599 	struct gs_host_frame *hf;
600 	struct can_frame *cf;
601 	struct canfd_frame *cfd;
602 	int rc;
603 	unsigned int idx;
604 	struct gs_tx_context *txc;
605 
606 	if (can_dropped_invalid_skb(netdev, skb))
607 		return NETDEV_TX_OK;
608 
609 	/* find an empty context to keep track of transmission */
610 	txc = gs_alloc_tx_context(dev);
611 	if (!txc)
612 		return NETDEV_TX_BUSY;
613 
614 	/* create a URB, and a buffer for it */
615 	urb = usb_alloc_urb(0, GFP_ATOMIC);
616 	if (!urb)
617 		goto nomem_urb;
618 
619 	hf = usb_alloc_coherent(dev->udev, dev->hf_size_tx, GFP_ATOMIC,
620 				&urb->transfer_dma);
621 	if (!hf) {
622 		netdev_err(netdev, "No memory left for USB buffer\n");
623 		goto nomem_hf;
624 	}
625 
626 	idx = txc->echo_id;
627 
628 	if (idx >= GS_MAX_TX_URBS) {
629 		netdev_err(netdev, "Invalid tx context %u\n", idx);
630 		goto badidx;
631 	}
632 
633 	hf->echo_id = idx;
634 	hf->channel = dev->channel;
635 	hf->flags = 0;
636 	hf->reserved = 0;
637 
638 	if (can_is_canfd_skb(skb)) {
639 		cfd = (struct canfd_frame *)skb->data;
640 
641 		hf->can_id = cpu_to_le32(cfd->can_id);
642 		hf->can_dlc = can_fd_len2dlc(cfd->len);
643 		hf->flags |= GS_CAN_FLAG_FD;
644 		if (cfd->flags & CANFD_BRS)
645 			hf->flags |= GS_CAN_FLAG_BRS;
646 		if (cfd->flags & CANFD_ESI)
647 			hf->flags |= GS_CAN_FLAG_ESI;
648 
649 		memcpy(hf->canfd->data, cfd->data, cfd->len);
650 	} else {
651 		cf = (struct can_frame *)skb->data;
652 
653 		hf->can_id = cpu_to_le32(cf->can_id);
654 		hf->can_dlc = can_get_cc_dlc(cf, dev->can.ctrlmode);
655 
656 		memcpy(hf->classic_can->data, cf->data, cf->len);
657 	}
658 
659 	usb_fill_bulk_urb(urb, dev->udev,
660 			  usb_sndbulkpipe(dev->udev, GSUSB_ENDPOINT_OUT),
661 			  hf, dev->hf_size_tx,
662 			  gs_usb_xmit_callback, txc);
663 
664 	urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
665 	usb_anchor_urb(urb, &dev->tx_submitted);
666 
667 	can_put_echo_skb(skb, netdev, idx, 0);
668 
669 	atomic_inc(&dev->active_tx_urbs);
670 
671 	rc = usb_submit_urb(urb, GFP_ATOMIC);
672 	if (unlikely(rc)) {			/* usb send failed */
673 		atomic_dec(&dev->active_tx_urbs);
674 
675 		can_free_echo_skb(netdev, idx, NULL);
676 		gs_free_tx_context(txc);
677 
678 		usb_unanchor_urb(urb);
679 		usb_free_coherent(dev->udev, urb->transfer_buffer_length,
680 				  urb->transfer_buffer, urb->transfer_dma);
681 
682 		if (rc == -ENODEV) {
683 			netif_device_detach(netdev);
684 		} else {
685 			netdev_err(netdev, "usb_submit failed (err=%d)\n", rc);
686 			stats->tx_dropped++;
687 		}
688 	} else {
689 		/* Slow down tx path */
690 		if (atomic_read(&dev->active_tx_urbs) >= GS_MAX_TX_URBS)
691 			netif_stop_queue(netdev);
692 	}
693 
694 	/* let usb core take care of this urb */
695 	usb_free_urb(urb);
696 
697 	return NETDEV_TX_OK;
698 
699  badidx:
700 	usb_free_coherent(dev->udev, urb->transfer_buffer_length,
701 			  urb->transfer_buffer, urb->transfer_dma);
702  nomem_hf:
703 	usb_free_urb(urb);
704 
705  nomem_urb:
706 	gs_free_tx_context(txc);
707 	dev_kfree_skb(skb);
708 	stats->tx_dropped++;
709 	return NETDEV_TX_OK;
710 }
711 
712 static int gs_can_open(struct net_device *netdev)
713 {
714 	struct gs_can *dev = netdev_priv(netdev);
715 	struct gs_usb *parent = dev->parent;
716 	int rc, i;
717 	struct gs_device_mode *dm;
718 	struct gs_host_frame *hf;
719 	u32 ctrlmode;
720 	u32 flags = 0;
721 
722 	rc = open_candev(netdev);
723 	if (rc)
724 		return rc;
725 
726 	ctrlmode = dev->can.ctrlmode;
727 	if (ctrlmode & CAN_CTRLMODE_FD) {
728 		flags |= GS_CAN_MODE_FD;
729 
730 		if (dev->feature & GS_CAN_FEATURE_REQ_USB_QUIRK_LPC546XX)
731 			dev->hf_size_tx = struct_size(hf, canfd_quirk, 1);
732 		else
733 			dev->hf_size_tx = struct_size(hf, canfd, 1);
734 	} else {
735 		if (dev->feature & GS_CAN_FEATURE_REQ_USB_QUIRK_LPC546XX)
736 			dev->hf_size_tx = struct_size(hf, classic_can_quirk, 1);
737 		else
738 			dev->hf_size_tx = struct_size(hf, classic_can, 1);
739 	}
740 
741 	if (!parent->active_channels) {
742 		for (i = 0; i < GS_MAX_RX_URBS; i++) {
743 			struct urb *urb;
744 			u8 *buf;
745 
746 			/* alloc rx urb */
747 			urb = usb_alloc_urb(0, GFP_KERNEL);
748 			if (!urb)
749 				return -ENOMEM;
750 
751 			/* alloc rx buffer */
752 			buf = usb_alloc_coherent(dev->udev,
753 						 dev->parent->hf_size_rx,
754 						 GFP_KERNEL,
755 						 &urb->transfer_dma);
756 			if (!buf) {
757 				netdev_err(netdev,
758 					   "No memory left for USB buffer\n");
759 				usb_free_urb(urb);
760 				return -ENOMEM;
761 			}
762 
763 			/* fill, anchor, and submit rx urb */
764 			usb_fill_bulk_urb(urb,
765 					  dev->udev,
766 					  usb_rcvbulkpipe(dev->udev,
767 							  GSUSB_ENDPOINT_IN),
768 					  buf,
769 					  dev->parent->hf_size_rx,
770 					  gs_usb_receive_bulk_callback, parent);
771 			urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
772 
773 			usb_anchor_urb(urb, &parent->rx_submitted);
774 
775 			rc = usb_submit_urb(urb, GFP_KERNEL);
776 			if (rc) {
777 				if (rc == -ENODEV)
778 					netif_device_detach(dev->netdev);
779 
780 				netdev_err(netdev,
781 					   "usb_submit failed (err=%d)\n", rc);
782 
783 				usb_unanchor_urb(urb);
784 				usb_free_urb(urb);
785 				break;
786 			}
787 
788 			/* Drop reference,
789 			 * USB core will take care of freeing it
790 			 */
791 			usb_free_urb(urb);
792 		}
793 	}
794 
795 	dm = kmalloc(sizeof(*dm), GFP_KERNEL);
796 	if (!dm)
797 		return -ENOMEM;
798 
799 	/* flags */
800 	if (ctrlmode & CAN_CTRLMODE_LOOPBACK)
801 		flags |= GS_CAN_MODE_LOOP_BACK;
802 	else if (ctrlmode & CAN_CTRLMODE_LISTENONLY)
803 		flags |= GS_CAN_MODE_LISTEN_ONLY;
804 
805 	/* Controller is not allowed to retry TX
806 	 * this mode is unavailable on atmels uc3c hardware
807 	 */
808 	if (ctrlmode & CAN_CTRLMODE_ONE_SHOT)
809 		flags |= GS_CAN_MODE_ONE_SHOT;
810 
811 	if (ctrlmode & CAN_CTRLMODE_3_SAMPLES)
812 		flags |= GS_CAN_MODE_TRIPLE_SAMPLE;
813 
814 	/* finally start device */
815 	dm->mode = cpu_to_le32(GS_CAN_MODE_START);
816 	dm->flags = cpu_to_le32(flags);
817 	rc = usb_control_msg(interface_to_usbdev(dev->iface),
818 			     usb_sndctrlpipe(interface_to_usbdev(dev->iface), 0),
819 			     GS_USB_BREQ_MODE,
820 			     USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
821 			     dev->channel, 0, dm, sizeof(*dm), 1000);
822 
823 	if (rc < 0) {
824 		netdev_err(netdev, "Couldn't start device (err=%d)\n", rc);
825 		kfree(dm);
826 		return rc;
827 	}
828 
829 	kfree(dm);
830 
831 	dev->can.state = CAN_STATE_ERROR_ACTIVE;
832 
833 	parent->active_channels++;
834 	if (!(dev->can.ctrlmode & CAN_CTRLMODE_LISTENONLY))
835 		netif_start_queue(netdev);
836 
837 	return 0;
838 }
839 
840 static int gs_can_close(struct net_device *netdev)
841 {
842 	int rc;
843 	struct gs_can *dev = netdev_priv(netdev);
844 	struct gs_usb *parent = dev->parent;
845 
846 	netif_stop_queue(netdev);
847 
848 	/* Stop polling */
849 	parent->active_channels--;
850 	if (!parent->active_channels)
851 		usb_kill_anchored_urbs(&parent->rx_submitted);
852 
853 	/* Stop sending URBs */
854 	usb_kill_anchored_urbs(&dev->tx_submitted);
855 	atomic_set(&dev->active_tx_urbs, 0);
856 
857 	/* reset the device */
858 	rc = gs_cmd_reset(dev);
859 	if (rc < 0)
860 		netdev_warn(netdev, "Couldn't shutdown device (err=%d)", rc);
861 
862 	/* reset tx contexts */
863 	for (rc = 0; rc < GS_MAX_TX_URBS; rc++) {
864 		dev->tx_context[rc].dev = dev;
865 		dev->tx_context[rc].echo_id = GS_MAX_TX_URBS;
866 	}
867 
868 	/* close the netdev */
869 	close_candev(netdev);
870 
871 	return 0;
872 }
873 
874 static const struct net_device_ops gs_usb_netdev_ops = {
875 	.ndo_open = gs_can_open,
876 	.ndo_stop = gs_can_close,
877 	.ndo_start_xmit = gs_can_start_xmit,
878 	.ndo_change_mtu = can_change_mtu,
879 };
880 
881 static int gs_usb_set_identify(struct net_device *netdev, bool do_identify)
882 {
883 	struct gs_can *dev = netdev_priv(netdev);
884 	struct gs_identify_mode *imode;
885 	int rc;
886 
887 	imode = kmalloc(sizeof(*imode), GFP_KERNEL);
888 
889 	if (!imode)
890 		return -ENOMEM;
891 
892 	if (do_identify)
893 		imode->mode = cpu_to_le32(GS_CAN_IDENTIFY_ON);
894 	else
895 		imode->mode = cpu_to_le32(GS_CAN_IDENTIFY_OFF);
896 
897 	rc = usb_control_msg(interface_to_usbdev(dev->iface),
898 			     usb_sndctrlpipe(interface_to_usbdev(dev->iface), 0),
899 			     GS_USB_BREQ_IDENTIFY,
900 			     USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
901 			     dev->channel, 0, imode, sizeof(*imode), 100);
902 
903 	kfree(imode);
904 
905 	return (rc > 0) ? 0 : rc;
906 }
907 
908 /* blink LED's for finding the this interface */
909 static int gs_usb_set_phys_id(struct net_device *dev,
910 			      enum ethtool_phys_id_state state)
911 {
912 	int rc = 0;
913 
914 	switch (state) {
915 	case ETHTOOL_ID_ACTIVE:
916 		rc = gs_usb_set_identify(dev, GS_CAN_IDENTIFY_ON);
917 		break;
918 	case ETHTOOL_ID_INACTIVE:
919 		rc = gs_usb_set_identify(dev, GS_CAN_IDENTIFY_OFF);
920 		break;
921 	default:
922 		break;
923 	}
924 
925 	return rc;
926 }
927 
928 static const struct ethtool_ops gs_usb_ethtool_ops = {
929 	.set_phys_id = gs_usb_set_phys_id,
930 };
931 
932 static struct gs_can *gs_make_candev(unsigned int channel,
933 				     struct usb_interface *intf,
934 				     struct gs_device_config *dconf)
935 {
936 	struct gs_can *dev;
937 	struct net_device *netdev;
938 	int rc;
939 	struct gs_device_bt_const *bt_const;
940 	struct gs_device_bt_const_extended *bt_const_extended;
941 	u32 feature;
942 
943 	bt_const = kmalloc(sizeof(*bt_const), GFP_KERNEL);
944 	if (!bt_const)
945 		return ERR_PTR(-ENOMEM);
946 
947 	/* fetch bit timing constants */
948 	rc = usb_control_msg(interface_to_usbdev(intf),
949 			     usb_rcvctrlpipe(interface_to_usbdev(intf), 0),
950 			     GS_USB_BREQ_BT_CONST,
951 			     USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
952 			     channel, 0, bt_const, sizeof(*bt_const), 1000);
953 
954 	if (rc < 0) {
955 		dev_err(&intf->dev,
956 			"Couldn't get bit timing const for channel (err=%d)\n",
957 			rc);
958 		kfree(bt_const);
959 		return ERR_PTR(rc);
960 	}
961 
962 	/* create netdev */
963 	netdev = alloc_candev(sizeof(struct gs_can), GS_MAX_TX_URBS);
964 	if (!netdev) {
965 		dev_err(&intf->dev, "Couldn't allocate candev\n");
966 		kfree(bt_const);
967 		return ERR_PTR(-ENOMEM);
968 	}
969 
970 	dev = netdev_priv(netdev);
971 
972 	netdev->netdev_ops = &gs_usb_netdev_ops;
973 
974 	netdev->flags |= IFF_ECHO; /* we support full roundtrip echo */
975 
976 	/* dev setup */
977 	strcpy(dev->bt_const.name, "gs_usb");
978 	dev->bt_const.tseg1_min = le32_to_cpu(bt_const->tseg1_min);
979 	dev->bt_const.tseg1_max = le32_to_cpu(bt_const->tseg1_max);
980 	dev->bt_const.tseg2_min = le32_to_cpu(bt_const->tseg2_min);
981 	dev->bt_const.tseg2_max = le32_to_cpu(bt_const->tseg2_max);
982 	dev->bt_const.sjw_max = le32_to_cpu(bt_const->sjw_max);
983 	dev->bt_const.brp_min = le32_to_cpu(bt_const->brp_min);
984 	dev->bt_const.brp_max = le32_to_cpu(bt_const->brp_max);
985 	dev->bt_const.brp_inc = le32_to_cpu(bt_const->brp_inc);
986 
987 	dev->udev = interface_to_usbdev(intf);
988 	dev->iface = intf;
989 	dev->netdev = netdev;
990 	dev->channel = channel;
991 
992 	init_usb_anchor(&dev->tx_submitted);
993 	atomic_set(&dev->active_tx_urbs, 0);
994 	spin_lock_init(&dev->tx_ctx_lock);
995 	for (rc = 0; rc < GS_MAX_TX_URBS; rc++) {
996 		dev->tx_context[rc].dev = dev;
997 		dev->tx_context[rc].echo_id = GS_MAX_TX_URBS;
998 	}
999 
1000 	/* can setup */
1001 	dev->can.state = CAN_STATE_STOPPED;
1002 	dev->can.clock.freq = le32_to_cpu(bt_const->fclk_can);
1003 	dev->can.bittiming_const = &dev->bt_const;
1004 	dev->can.do_set_bittiming = gs_usb_set_bittiming;
1005 
1006 	dev->can.ctrlmode_supported = CAN_CTRLMODE_CC_LEN8_DLC;
1007 
1008 	feature = le32_to_cpu(bt_const->feature);
1009 	dev->feature = FIELD_GET(GS_CAN_FEATURE_MASK, feature);
1010 	if (feature & GS_CAN_FEATURE_LISTEN_ONLY)
1011 		dev->can.ctrlmode_supported |= CAN_CTRLMODE_LISTENONLY;
1012 
1013 	if (feature & GS_CAN_FEATURE_LOOP_BACK)
1014 		dev->can.ctrlmode_supported |= CAN_CTRLMODE_LOOPBACK;
1015 
1016 	if (feature & GS_CAN_FEATURE_TRIPLE_SAMPLE)
1017 		dev->can.ctrlmode_supported |= CAN_CTRLMODE_3_SAMPLES;
1018 
1019 	if (feature & GS_CAN_FEATURE_ONE_SHOT)
1020 		dev->can.ctrlmode_supported |= CAN_CTRLMODE_ONE_SHOT;
1021 
1022 	if (feature & GS_CAN_FEATURE_FD) {
1023 		dev->can.ctrlmode_supported |= CAN_CTRLMODE_FD;
1024 		/* The data bit timing will be overwritten, if
1025 		 * GS_CAN_FEATURE_BT_CONST_EXT is set.
1026 		 */
1027 		dev->can.data_bittiming_const = &dev->bt_const;
1028 		dev->can.do_set_data_bittiming = gs_usb_set_data_bittiming;
1029 	}
1030 
1031 	/* The CANtact Pro from LinkLayer Labs is based on the
1032 	 * LPC54616 µC, which is affected by the NXP LPC USB transfer
1033 	 * erratum. However, the current firmware (version 2) doesn't
1034 	 * set the GS_CAN_FEATURE_REQ_USB_QUIRK_LPC546XX bit. Set the
1035 	 * feature GS_CAN_FEATURE_REQ_USB_QUIRK_LPC546XX to workaround
1036 	 * this issue.
1037 	 *
1038 	 * For the GS_USB_BREQ_DATA_BITTIMING USB control message the
1039 	 * CANtact Pro firmware uses a request value, which is already
1040 	 * used by the candleLight firmware for a different purpose
1041 	 * (GS_USB_BREQ_GET_USER_ID). Set the feature
1042 	 * GS_CAN_FEATURE_QUIRK_BREQ_CANTACT_PRO to workaround this
1043 	 * issue.
1044 	 */
1045 	if (dev->udev->descriptor.idVendor == cpu_to_le16(USB_GSUSB_1_VENDOR_ID) &&
1046 	    dev->udev->descriptor.idProduct == cpu_to_le16(USB_GSUSB_1_PRODUCT_ID) &&
1047 	    dev->udev->manufacturer && dev->udev->product &&
1048 	    !strcmp(dev->udev->manufacturer, "LinkLayer Labs") &&
1049 	    !strcmp(dev->udev->product, "CANtact Pro") &&
1050 	    (le32_to_cpu(dconf->sw_version) <= 2))
1051 		dev->feature |= GS_CAN_FEATURE_REQ_USB_QUIRK_LPC546XX |
1052 			GS_CAN_FEATURE_QUIRK_BREQ_CANTACT_PRO;
1053 
1054 	if (le32_to_cpu(dconf->sw_version) > 1)
1055 		if (feature & GS_CAN_FEATURE_IDENTIFY)
1056 			netdev->ethtool_ops = &gs_usb_ethtool_ops;
1057 
1058 	kfree(bt_const);
1059 
1060 	/* fetch extended bit timing constants if device has feature
1061 	 * GS_CAN_FEATURE_FD and GS_CAN_FEATURE_BT_CONST_EXT
1062 	 */
1063 	if (feature & GS_CAN_FEATURE_FD &&
1064 	    feature & GS_CAN_FEATURE_BT_CONST_EXT) {
1065 		bt_const_extended = kmalloc(sizeof(*bt_const_extended), GFP_KERNEL);
1066 		if (!bt_const_extended)
1067 			return ERR_PTR(-ENOMEM);
1068 
1069 		rc = usb_control_msg(interface_to_usbdev(intf),
1070 				     usb_rcvctrlpipe(interface_to_usbdev(intf), 0),
1071 				     GS_USB_BREQ_BT_CONST_EXT,
1072 				     USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
1073 				     channel, 0, bt_const_extended,
1074 				     sizeof(*bt_const_extended),
1075 				     1000);
1076 		if (rc < 0) {
1077 			dev_err(&intf->dev,
1078 				"Couldn't get extended bit timing const for channel (err=%d)\n",
1079 				rc);
1080 			kfree(bt_const_extended);
1081 			return ERR_PTR(rc);
1082 		}
1083 
1084 		strcpy(dev->data_bt_const.name, "gs_usb");
1085 		dev->data_bt_const.tseg1_min = le32_to_cpu(bt_const_extended->dtseg1_min);
1086 		dev->data_bt_const.tseg1_max = le32_to_cpu(bt_const_extended->dtseg1_max);
1087 		dev->data_bt_const.tseg2_min = le32_to_cpu(bt_const_extended->dtseg2_min);
1088 		dev->data_bt_const.tseg2_max = le32_to_cpu(bt_const_extended->dtseg2_max);
1089 		dev->data_bt_const.sjw_max = le32_to_cpu(bt_const_extended->dsjw_max);
1090 		dev->data_bt_const.brp_min = le32_to_cpu(bt_const_extended->dbrp_min);
1091 		dev->data_bt_const.brp_max = le32_to_cpu(bt_const_extended->dbrp_max);
1092 		dev->data_bt_const.brp_inc = le32_to_cpu(bt_const_extended->dbrp_inc);
1093 
1094 		dev->can.data_bittiming_const = &dev->data_bt_const;
1095 
1096 		kfree(bt_const_extended);
1097 	}
1098 
1099 	SET_NETDEV_DEV(netdev, &intf->dev);
1100 
1101 	rc = register_candev(dev->netdev);
1102 	if (rc) {
1103 		free_candev(dev->netdev);
1104 		dev_err(&intf->dev, "Couldn't register candev (err=%d)\n", rc);
1105 		return ERR_PTR(rc);
1106 	}
1107 
1108 	return dev;
1109 }
1110 
1111 static void gs_destroy_candev(struct gs_can *dev)
1112 {
1113 	unregister_candev(dev->netdev);
1114 	usb_kill_anchored_urbs(&dev->tx_submitted);
1115 	free_candev(dev->netdev);
1116 }
1117 
1118 static int gs_usb_probe(struct usb_interface *intf,
1119 			const struct usb_device_id *id)
1120 {
1121 	struct usb_device *udev = interface_to_usbdev(intf);
1122 	struct gs_host_frame *hf;
1123 	struct gs_usb *dev;
1124 	int rc = -ENOMEM;
1125 	unsigned int icount, i;
1126 	struct gs_host_config *hconf;
1127 	struct gs_device_config *dconf;
1128 
1129 	hconf = kmalloc(sizeof(*hconf), GFP_KERNEL);
1130 	if (!hconf)
1131 		return -ENOMEM;
1132 
1133 	hconf->byte_order = cpu_to_le32(0x0000beef);
1134 
1135 	/* send host config */
1136 	rc = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
1137 			     GS_USB_BREQ_HOST_FORMAT,
1138 			     USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
1139 			     1, intf->cur_altsetting->desc.bInterfaceNumber,
1140 			     hconf, sizeof(*hconf), 1000);
1141 
1142 	kfree(hconf);
1143 
1144 	if (rc < 0) {
1145 		dev_err(&intf->dev, "Couldn't send data format (err=%d)\n", rc);
1146 		return rc;
1147 	}
1148 
1149 	dconf = kmalloc(sizeof(*dconf), GFP_KERNEL);
1150 	if (!dconf)
1151 		return -ENOMEM;
1152 
1153 	/* read device config */
1154 	rc = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
1155 			     GS_USB_BREQ_DEVICE_CONFIG,
1156 			     USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
1157 			     1, intf->cur_altsetting->desc.bInterfaceNumber,
1158 			     dconf, sizeof(*dconf), 1000);
1159 	if (rc < 0) {
1160 		dev_err(&intf->dev, "Couldn't get device config: (err=%d)\n",
1161 			rc);
1162 		kfree(dconf);
1163 		return rc;
1164 	}
1165 
1166 	icount = dconf->icount + 1;
1167 	dev_info(&intf->dev, "Configuring for %u interfaces\n", icount);
1168 
1169 	if (icount > GS_MAX_INTF) {
1170 		dev_err(&intf->dev,
1171 			"Driver cannot handle more that %u CAN interfaces\n",
1172 			GS_MAX_INTF);
1173 		kfree(dconf);
1174 		return -EINVAL;
1175 	}
1176 
1177 	dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1178 	if (!dev) {
1179 		kfree(dconf);
1180 		return -ENOMEM;
1181 	}
1182 
1183 	init_usb_anchor(&dev->rx_submitted);
1184 	/* default to classic CAN, switch to CAN-FD if at least one of
1185 	 * our channels support CAN-FD.
1186 	 */
1187 	dev->hf_size_rx = struct_size(hf, classic_can, 1);
1188 
1189 	usb_set_intfdata(intf, dev);
1190 	dev->udev = udev;
1191 
1192 	for (i = 0; i < icount; i++) {
1193 		dev->canch[i] = gs_make_candev(i, intf, dconf);
1194 		if (IS_ERR_OR_NULL(dev->canch[i])) {
1195 			/* save error code to return later */
1196 			rc = PTR_ERR(dev->canch[i]);
1197 
1198 			/* on failure destroy previously created candevs */
1199 			icount = i;
1200 			for (i = 0; i < icount; i++)
1201 				gs_destroy_candev(dev->canch[i]);
1202 
1203 			usb_kill_anchored_urbs(&dev->rx_submitted);
1204 			kfree(dconf);
1205 			kfree(dev);
1206 			return rc;
1207 		}
1208 		dev->canch[i]->parent = dev;
1209 
1210 		if (dev->canch[i]->can.ctrlmode_supported & CAN_CTRLMODE_FD)
1211 			dev->hf_size_rx = struct_size(hf, canfd, 1);
1212 	}
1213 
1214 	kfree(dconf);
1215 
1216 	return 0;
1217 }
1218 
1219 static void gs_usb_disconnect(struct usb_interface *intf)
1220 {
1221 	struct gs_usb *dev = usb_get_intfdata(intf);
1222 	unsigned int i;
1223 
1224 	usb_set_intfdata(intf, NULL);
1225 
1226 	if (!dev) {
1227 		dev_err(&intf->dev, "Disconnect (nodata)\n");
1228 		return;
1229 	}
1230 
1231 	for (i = 0; i < GS_MAX_INTF; i++)
1232 		if (dev->canch[i])
1233 			gs_destroy_candev(dev->canch[i]);
1234 
1235 	usb_kill_anchored_urbs(&dev->rx_submitted);
1236 	kfree(dev);
1237 }
1238 
1239 static const struct usb_device_id gs_usb_table[] = {
1240 	{ USB_DEVICE_INTERFACE_NUMBER(USB_GSUSB_1_VENDOR_ID,
1241 				      USB_GSUSB_1_PRODUCT_ID, 0) },
1242 	{ USB_DEVICE_INTERFACE_NUMBER(USB_CANDLELIGHT_VENDOR_ID,
1243 				      USB_CANDLELIGHT_PRODUCT_ID, 0) },
1244 	{ USB_DEVICE_INTERFACE_NUMBER(USB_CES_CANEXT_FD_VENDOR_ID,
1245 				      USB_CES_CANEXT_FD_PRODUCT_ID, 0) },
1246 	{ USB_DEVICE_INTERFACE_NUMBER(USB_ABE_CANDEBUGGER_FD_VENDOR_ID,
1247 				      USB_ABE_CANDEBUGGER_FD_PRODUCT_ID, 0) },
1248 	{} /* Terminating entry */
1249 };
1250 
1251 MODULE_DEVICE_TABLE(usb, gs_usb_table);
1252 
1253 static struct usb_driver gs_usb_driver = {
1254 	.name = "gs_usb",
1255 	.probe = gs_usb_probe,
1256 	.disconnect = gs_usb_disconnect,
1257 	.id_table = gs_usb_table,
1258 };
1259 
1260 module_usb_driver(gs_usb_driver);
1261 
1262 MODULE_AUTHOR("Maximilian Schneider <mws@schneidersoft.net>");
1263 MODULE_DESCRIPTION(
1264 "Socket CAN device driver for Geschwister Schneider Technologie-, "
1265 "Entwicklungs- und Vertriebs UG. USB2.0 to CAN interfaces\n"
1266 "and bytewerk.org candleLight USB CAN interfaces.");
1267 MODULE_LICENSE("GPL v2");
1268