xref: /linux/drivers/net/can/usb/gs_usb.c (revision 058443934524590d5537a80f490267cc95a61c05)
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 	void *rxbuf[GS_MAX_RX_URBS];
272 	dma_addr_t rxbuf_dma[GS_MAX_RX_URBS];
273 };
274 
275 /* usb interface struct */
276 struct gs_usb {
277 	struct gs_can *canch[GS_MAX_INTF];
278 	struct usb_anchor rx_submitted;
279 	struct usb_device *udev;
280 	unsigned int hf_size_rx;
281 	u8 active_channels;
282 };
283 
284 /* 'allocate' a tx context.
285  * returns a valid tx context or NULL if there is no space.
286  */
287 static struct gs_tx_context *gs_alloc_tx_context(struct gs_can *dev)
288 {
289 	int i = 0;
290 	unsigned long flags;
291 
292 	spin_lock_irqsave(&dev->tx_ctx_lock, flags);
293 
294 	for (; i < GS_MAX_TX_URBS; i++) {
295 		if (dev->tx_context[i].echo_id == GS_MAX_TX_URBS) {
296 			dev->tx_context[i].echo_id = i;
297 			spin_unlock_irqrestore(&dev->tx_ctx_lock, flags);
298 			return &dev->tx_context[i];
299 		}
300 	}
301 
302 	spin_unlock_irqrestore(&dev->tx_ctx_lock, flags);
303 	return NULL;
304 }
305 
306 /* releases a tx context
307  */
308 static void gs_free_tx_context(struct gs_tx_context *txc)
309 {
310 	txc->echo_id = GS_MAX_TX_URBS;
311 }
312 
313 /* Get a tx context by id.
314  */
315 static struct gs_tx_context *gs_get_tx_context(struct gs_can *dev,
316 					       unsigned int id)
317 {
318 	unsigned long flags;
319 
320 	if (id < GS_MAX_TX_URBS) {
321 		spin_lock_irqsave(&dev->tx_ctx_lock, flags);
322 		if (dev->tx_context[id].echo_id == id) {
323 			spin_unlock_irqrestore(&dev->tx_ctx_lock, flags);
324 			return &dev->tx_context[id];
325 		}
326 		spin_unlock_irqrestore(&dev->tx_ctx_lock, flags);
327 	}
328 	return NULL;
329 }
330 
331 static int gs_cmd_reset(struct gs_can *gsdev)
332 {
333 	struct gs_device_mode *dm;
334 	struct usb_interface *intf = gsdev->iface;
335 	int rc;
336 
337 	dm = kzalloc(sizeof(*dm), GFP_KERNEL);
338 	if (!dm)
339 		return -ENOMEM;
340 
341 	dm->mode = GS_CAN_MODE_RESET;
342 
343 	rc = usb_control_msg(interface_to_usbdev(intf),
344 			     usb_sndctrlpipe(interface_to_usbdev(intf), 0),
345 			     GS_USB_BREQ_MODE,
346 			     USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
347 			     gsdev->channel, 0, dm, sizeof(*dm), 1000);
348 
349 	kfree(dm);
350 
351 	return rc;
352 }
353 
354 static void gs_update_state(struct gs_can *dev, struct can_frame *cf)
355 {
356 	struct can_device_stats *can_stats = &dev->can.can_stats;
357 
358 	if (cf->can_id & CAN_ERR_RESTARTED) {
359 		dev->can.state = CAN_STATE_ERROR_ACTIVE;
360 		can_stats->restarts++;
361 	} else if (cf->can_id & CAN_ERR_BUSOFF) {
362 		dev->can.state = CAN_STATE_BUS_OFF;
363 		can_stats->bus_off++;
364 	} else if (cf->can_id & CAN_ERR_CRTL) {
365 		if ((cf->data[1] & CAN_ERR_CRTL_TX_WARNING) ||
366 		    (cf->data[1] & CAN_ERR_CRTL_RX_WARNING)) {
367 			dev->can.state = CAN_STATE_ERROR_WARNING;
368 			can_stats->error_warning++;
369 		} else if ((cf->data[1] & CAN_ERR_CRTL_TX_PASSIVE) ||
370 			   (cf->data[1] & CAN_ERR_CRTL_RX_PASSIVE)) {
371 			dev->can.state = CAN_STATE_ERROR_PASSIVE;
372 			can_stats->error_passive++;
373 		} else {
374 			dev->can.state = CAN_STATE_ERROR_ACTIVE;
375 		}
376 	}
377 }
378 
379 static void gs_usb_receive_bulk_callback(struct urb *urb)
380 {
381 	struct gs_usb *usbcan = urb->context;
382 	struct gs_can *dev;
383 	struct net_device *netdev;
384 	int rc;
385 	struct net_device_stats *stats;
386 	struct gs_host_frame *hf = urb->transfer_buffer;
387 	struct gs_tx_context *txc;
388 	struct can_frame *cf;
389 	struct canfd_frame *cfd;
390 	struct sk_buff *skb;
391 
392 	BUG_ON(!usbcan);
393 
394 	switch (urb->status) {
395 	case 0: /* success */
396 		break;
397 	case -ENOENT:
398 	case -ESHUTDOWN:
399 		return;
400 	default:
401 		/* do not resubmit aborted urbs. eg: when device goes down */
402 		return;
403 	}
404 
405 	/* device reports out of range channel id */
406 	if (hf->channel >= GS_MAX_INTF)
407 		goto device_detach;
408 
409 	dev = usbcan->canch[hf->channel];
410 
411 	netdev = dev->netdev;
412 	stats = &netdev->stats;
413 
414 	if (!netif_device_present(netdev))
415 		return;
416 
417 	if (hf->echo_id == -1) { /* normal rx */
418 		if (hf->flags & GS_CAN_FLAG_FD) {
419 			skb = alloc_canfd_skb(dev->netdev, &cfd);
420 			if (!skb)
421 				return;
422 
423 			cfd->can_id = le32_to_cpu(hf->can_id);
424 			cfd->len = can_fd_dlc2len(hf->can_dlc);
425 			if (hf->flags & GS_CAN_FLAG_BRS)
426 				cfd->flags |= CANFD_BRS;
427 			if (hf->flags & GS_CAN_FLAG_ESI)
428 				cfd->flags |= CANFD_ESI;
429 
430 			memcpy(cfd->data, hf->canfd->data, cfd->len);
431 		} else {
432 			skb = alloc_can_skb(dev->netdev, &cf);
433 			if (!skb)
434 				return;
435 
436 			cf->can_id = le32_to_cpu(hf->can_id);
437 			can_frame_set_cc_len(cf, hf->can_dlc, dev->can.ctrlmode);
438 
439 			memcpy(cf->data, hf->classic_can->data, 8);
440 
441 			/* ERROR frames tell us information about the controller */
442 			if (le32_to_cpu(hf->can_id) & CAN_ERR_FLAG)
443 				gs_update_state(dev, cf);
444 		}
445 
446 		netdev->stats.rx_packets++;
447 		netdev->stats.rx_bytes += hf->can_dlc;
448 
449 		netif_rx(skb);
450 	} else { /* echo_id == hf->echo_id */
451 		if (hf->echo_id >= GS_MAX_TX_URBS) {
452 			netdev_err(netdev,
453 				   "Unexpected out of range echo id %u\n",
454 				   hf->echo_id);
455 			goto resubmit_urb;
456 		}
457 
458 		txc = gs_get_tx_context(dev, hf->echo_id);
459 
460 		/* bad devices send bad echo_ids. */
461 		if (!txc) {
462 			netdev_err(netdev,
463 				   "Unexpected unused echo id %u\n",
464 				   hf->echo_id);
465 			goto resubmit_urb;
466 		}
467 
468 		netdev->stats.tx_packets++;
469 		netdev->stats.tx_bytes += can_get_echo_skb(netdev, hf->echo_id,
470 							   NULL);
471 
472 		gs_free_tx_context(txc);
473 
474 		atomic_dec(&dev->active_tx_urbs);
475 
476 		netif_wake_queue(netdev);
477 	}
478 
479 	if (hf->flags & GS_CAN_FLAG_OVERFLOW) {
480 		skb = alloc_can_err_skb(netdev, &cf);
481 		if (!skb)
482 			goto resubmit_urb;
483 
484 		cf->can_id |= CAN_ERR_CRTL;
485 		cf->len = CAN_ERR_DLC;
486 		cf->data[1] = CAN_ERR_CRTL_RX_OVERFLOW;
487 		stats->rx_over_errors++;
488 		stats->rx_errors++;
489 		netif_rx(skb);
490 	}
491 
492  resubmit_urb:
493 	usb_fill_bulk_urb(urb, usbcan->udev,
494 			  usb_rcvbulkpipe(usbcan->udev, GSUSB_ENDPOINT_IN),
495 			  hf, dev->parent->hf_size_rx,
496 			  gs_usb_receive_bulk_callback, usbcan);
497 
498 	rc = usb_submit_urb(urb, GFP_ATOMIC);
499 
500 	/* USB failure take down all interfaces */
501 	if (rc == -ENODEV) {
502  device_detach:
503 		for (rc = 0; rc < GS_MAX_INTF; rc++) {
504 			if (usbcan->canch[rc])
505 				netif_device_detach(usbcan->canch[rc]->netdev);
506 		}
507 	}
508 }
509 
510 static int gs_usb_set_bittiming(struct net_device *netdev)
511 {
512 	struct gs_can *dev = netdev_priv(netdev);
513 	struct can_bittiming *bt = &dev->can.bittiming;
514 	struct usb_interface *intf = dev->iface;
515 	int rc;
516 	struct gs_device_bittiming *dbt;
517 
518 	dbt = kmalloc(sizeof(*dbt), GFP_KERNEL);
519 	if (!dbt)
520 		return -ENOMEM;
521 
522 	dbt->prop_seg = cpu_to_le32(bt->prop_seg);
523 	dbt->phase_seg1 = cpu_to_le32(bt->phase_seg1);
524 	dbt->phase_seg2 = cpu_to_le32(bt->phase_seg2);
525 	dbt->sjw = cpu_to_le32(bt->sjw);
526 	dbt->brp = cpu_to_le32(bt->brp);
527 
528 	/* request bit timings */
529 	rc = usb_control_msg(interface_to_usbdev(intf),
530 			     usb_sndctrlpipe(interface_to_usbdev(intf), 0),
531 			     GS_USB_BREQ_BITTIMING,
532 			     USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
533 			     dev->channel, 0, dbt, sizeof(*dbt), 1000);
534 
535 	kfree(dbt);
536 
537 	if (rc < 0)
538 		dev_err(netdev->dev.parent, "Couldn't set bittimings (err=%d)",
539 			rc);
540 
541 	return (rc > 0) ? 0 : rc;
542 }
543 
544 static int gs_usb_set_data_bittiming(struct net_device *netdev)
545 {
546 	struct gs_can *dev = netdev_priv(netdev);
547 	struct can_bittiming *bt = &dev->can.data_bittiming;
548 	struct usb_interface *intf = dev->iface;
549 	struct gs_device_bittiming *dbt;
550 	u8 request = GS_USB_BREQ_DATA_BITTIMING;
551 	int rc;
552 
553 	dbt = kmalloc(sizeof(*dbt), GFP_KERNEL);
554 	if (!dbt)
555 		return -ENOMEM;
556 
557 	dbt->prop_seg = cpu_to_le32(bt->prop_seg);
558 	dbt->phase_seg1 = cpu_to_le32(bt->phase_seg1);
559 	dbt->phase_seg2 = cpu_to_le32(bt->phase_seg2);
560 	dbt->sjw = cpu_to_le32(bt->sjw);
561 	dbt->brp = cpu_to_le32(bt->brp);
562 
563 	if (dev->feature & GS_CAN_FEATURE_QUIRK_BREQ_CANTACT_PRO)
564 		request = GS_USB_BREQ_QUIRK_CANTACT_PRO_DATA_BITTIMING;
565 
566 	/* request bit timings */
567 	rc = usb_control_msg(interface_to_usbdev(intf),
568 			     usb_sndctrlpipe(interface_to_usbdev(intf), 0),
569 			     request,
570 			     USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
571 			     dev->channel, 0, dbt, sizeof(*dbt), 1000);
572 
573 	kfree(dbt);
574 
575 	if (rc < 0)
576 		dev_err(netdev->dev.parent,
577 			"Couldn't set data bittimings (err=%d)", rc);
578 
579 	return (rc > 0) ? 0 : rc;
580 }
581 
582 static void gs_usb_xmit_callback(struct urb *urb)
583 {
584 	struct gs_tx_context *txc = urb->context;
585 	struct gs_can *dev = txc->dev;
586 	struct net_device *netdev = dev->netdev;
587 
588 	if (urb->status)
589 		netdev_info(netdev, "usb xmit fail %u\n", txc->echo_id);
590 
591 	usb_free_coherent(urb->dev, urb->transfer_buffer_length,
592 			  urb->transfer_buffer, urb->transfer_dma);
593 }
594 
595 static netdev_tx_t gs_can_start_xmit(struct sk_buff *skb,
596 				     struct net_device *netdev)
597 {
598 	struct gs_can *dev = netdev_priv(netdev);
599 	struct net_device_stats *stats = &dev->netdev->stats;
600 	struct urb *urb;
601 	struct gs_host_frame *hf;
602 	struct can_frame *cf;
603 	struct canfd_frame *cfd;
604 	int rc;
605 	unsigned int idx;
606 	struct gs_tx_context *txc;
607 
608 	if (can_dropped_invalid_skb(netdev, skb))
609 		return NETDEV_TX_OK;
610 
611 	/* find an empty context to keep track of transmission */
612 	txc = gs_alloc_tx_context(dev);
613 	if (!txc)
614 		return NETDEV_TX_BUSY;
615 
616 	/* create a URB, and a buffer for it */
617 	urb = usb_alloc_urb(0, GFP_ATOMIC);
618 	if (!urb)
619 		goto nomem_urb;
620 
621 	hf = usb_alloc_coherent(dev->udev, dev->hf_size_tx, GFP_ATOMIC,
622 				&urb->transfer_dma);
623 	if (!hf) {
624 		netdev_err(netdev, "No memory left for USB buffer\n");
625 		goto nomem_hf;
626 	}
627 
628 	idx = txc->echo_id;
629 
630 	if (idx >= GS_MAX_TX_URBS) {
631 		netdev_err(netdev, "Invalid tx context %u\n", idx);
632 		goto badidx;
633 	}
634 
635 	hf->echo_id = idx;
636 	hf->channel = dev->channel;
637 	hf->flags = 0;
638 	hf->reserved = 0;
639 
640 	if (can_is_canfd_skb(skb)) {
641 		cfd = (struct canfd_frame *)skb->data;
642 
643 		hf->can_id = cpu_to_le32(cfd->can_id);
644 		hf->can_dlc = can_fd_len2dlc(cfd->len);
645 		hf->flags |= GS_CAN_FLAG_FD;
646 		if (cfd->flags & CANFD_BRS)
647 			hf->flags |= GS_CAN_FLAG_BRS;
648 		if (cfd->flags & CANFD_ESI)
649 			hf->flags |= GS_CAN_FLAG_ESI;
650 
651 		memcpy(hf->canfd->data, cfd->data, cfd->len);
652 	} else {
653 		cf = (struct can_frame *)skb->data;
654 
655 		hf->can_id = cpu_to_le32(cf->can_id);
656 		hf->can_dlc = can_get_cc_dlc(cf, dev->can.ctrlmode);
657 
658 		memcpy(hf->classic_can->data, cf->data, cf->len);
659 	}
660 
661 	usb_fill_bulk_urb(urb, dev->udev,
662 			  usb_sndbulkpipe(dev->udev, GSUSB_ENDPOINT_OUT),
663 			  hf, dev->hf_size_tx,
664 			  gs_usb_xmit_callback, txc);
665 
666 	urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
667 	usb_anchor_urb(urb, &dev->tx_submitted);
668 
669 	can_put_echo_skb(skb, netdev, idx, 0);
670 
671 	atomic_inc(&dev->active_tx_urbs);
672 
673 	rc = usb_submit_urb(urb, GFP_ATOMIC);
674 	if (unlikely(rc)) {			/* usb send failed */
675 		atomic_dec(&dev->active_tx_urbs);
676 
677 		can_free_echo_skb(netdev, idx, NULL);
678 		gs_free_tx_context(txc);
679 
680 		usb_unanchor_urb(urb);
681 		usb_free_coherent(dev->udev, urb->transfer_buffer_length,
682 				  urb->transfer_buffer, urb->transfer_dma);
683 
684 		if (rc == -ENODEV) {
685 			netif_device_detach(netdev);
686 		} else {
687 			netdev_err(netdev, "usb_submit failed (err=%d)\n", rc);
688 			stats->tx_dropped++;
689 		}
690 	} else {
691 		/* Slow down tx path */
692 		if (atomic_read(&dev->active_tx_urbs) >= GS_MAX_TX_URBS)
693 			netif_stop_queue(netdev);
694 	}
695 
696 	/* let usb core take care of this urb */
697 	usb_free_urb(urb);
698 
699 	return NETDEV_TX_OK;
700 
701  badidx:
702 	usb_free_coherent(dev->udev, urb->transfer_buffer_length,
703 			  urb->transfer_buffer, urb->transfer_dma);
704  nomem_hf:
705 	usb_free_urb(urb);
706 
707  nomem_urb:
708 	gs_free_tx_context(txc);
709 	dev_kfree_skb(skb);
710 	stats->tx_dropped++;
711 	return NETDEV_TX_OK;
712 }
713 
714 static int gs_can_open(struct net_device *netdev)
715 {
716 	struct gs_can *dev = netdev_priv(netdev);
717 	struct gs_usb *parent = dev->parent;
718 	int rc, i;
719 	struct gs_device_mode *dm;
720 	struct gs_host_frame *hf;
721 	u32 ctrlmode;
722 	u32 flags = 0;
723 
724 	rc = open_candev(netdev);
725 	if (rc)
726 		return rc;
727 
728 	ctrlmode = dev->can.ctrlmode;
729 	if (ctrlmode & CAN_CTRLMODE_FD) {
730 		flags |= GS_CAN_MODE_FD;
731 
732 		if (dev->feature & GS_CAN_FEATURE_REQ_USB_QUIRK_LPC546XX)
733 			dev->hf_size_tx = struct_size(hf, canfd_quirk, 1);
734 		else
735 			dev->hf_size_tx = struct_size(hf, canfd, 1);
736 	} else {
737 		if (dev->feature & GS_CAN_FEATURE_REQ_USB_QUIRK_LPC546XX)
738 			dev->hf_size_tx = struct_size(hf, classic_can_quirk, 1);
739 		else
740 			dev->hf_size_tx = struct_size(hf, classic_can, 1);
741 	}
742 
743 	if (!parent->active_channels) {
744 		for (i = 0; i < GS_MAX_RX_URBS; i++) {
745 			struct urb *urb;
746 			u8 *buf;
747 			dma_addr_t buf_dma;
748 
749 			/* alloc rx urb */
750 			urb = usb_alloc_urb(0, GFP_KERNEL);
751 			if (!urb)
752 				return -ENOMEM;
753 
754 			/* alloc rx buffer */
755 			buf = usb_alloc_coherent(dev->udev,
756 						 dev->parent->hf_size_rx,
757 						 GFP_KERNEL,
758 						 &buf_dma);
759 			if (!buf) {
760 				netdev_err(netdev,
761 					   "No memory left for USB buffer\n");
762 				usb_free_urb(urb);
763 				return -ENOMEM;
764 			}
765 
766 			urb->transfer_dma = buf_dma;
767 
768 			/* fill, anchor, and submit rx urb */
769 			usb_fill_bulk_urb(urb,
770 					  dev->udev,
771 					  usb_rcvbulkpipe(dev->udev,
772 							  GSUSB_ENDPOINT_IN),
773 					  buf,
774 					  dev->parent->hf_size_rx,
775 					  gs_usb_receive_bulk_callback, parent);
776 			urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
777 
778 			usb_anchor_urb(urb, &parent->rx_submitted);
779 
780 			rc = usb_submit_urb(urb, GFP_KERNEL);
781 			if (rc) {
782 				if (rc == -ENODEV)
783 					netif_device_detach(dev->netdev);
784 
785 				netdev_err(netdev,
786 					   "usb_submit failed (err=%d)\n", rc);
787 
788 				usb_unanchor_urb(urb);
789 				usb_free_coherent(dev->udev,
790 						  sizeof(struct gs_host_frame),
791 						  buf,
792 						  buf_dma);
793 				usb_free_urb(urb);
794 				break;
795 			}
796 
797 			dev->rxbuf[i] = buf;
798 			dev->rxbuf_dma[i] = buf_dma;
799 
800 			/* Drop reference,
801 			 * USB core will take care of freeing it
802 			 */
803 			usb_free_urb(urb);
804 		}
805 	}
806 
807 	dm = kmalloc(sizeof(*dm), GFP_KERNEL);
808 	if (!dm)
809 		return -ENOMEM;
810 
811 	/* flags */
812 	if (ctrlmode & CAN_CTRLMODE_LOOPBACK)
813 		flags |= GS_CAN_MODE_LOOP_BACK;
814 	else if (ctrlmode & CAN_CTRLMODE_LISTENONLY)
815 		flags |= GS_CAN_MODE_LISTEN_ONLY;
816 
817 	/* Controller is not allowed to retry TX
818 	 * this mode is unavailable on atmels uc3c hardware
819 	 */
820 	if (ctrlmode & CAN_CTRLMODE_ONE_SHOT)
821 		flags |= GS_CAN_MODE_ONE_SHOT;
822 
823 	if (ctrlmode & CAN_CTRLMODE_3_SAMPLES)
824 		flags |= GS_CAN_MODE_TRIPLE_SAMPLE;
825 
826 	/* finally start device */
827 	dev->can.state = CAN_STATE_ERROR_ACTIVE;
828 	dm->mode = cpu_to_le32(GS_CAN_MODE_START);
829 	dm->flags = cpu_to_le32(flags);
830 	rc = usb_control_msg(interface_to_usbdev(dev->iface),
831 			     usb_sndctrlpipe(interface_to_usbdev(dev->iface), 0),
832 			     GS_USB_BREQ_MODE,
833 			     USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
834 			     dev->channel, 0, dm, sizeof(*dm), 1000);
835 
836 	if (rc < 0) {
837 		netdev_err(netdev, "Couldn't start device (err=%d)\n", rc);
838 		kfree(dm);
839 		dev->can.state = CAN_STATE_STOPPED;
840 		return rc;
841 	}
842 
843 	kfree(dm);
844 
845 	parent->active_channels++;
846 	if (!(dev->can.ctrlmode & CAN_CTRLMODE_LISTENONLY))
847 		netif_start_queue(netdev);
848 
849 	return 0;
850 }
851 
852 static int gs_can_close(struct net_device *netdev)
853 {
854 	int rc;
855 	struct gs_can *dev = netdev_priv(netdev);
856 	struct gs_usb *parent = dev->parent;
857 	unsigned int i;
858 
859 	netif_stop_queue(netdev);
860 
861 	/* Stop polling */
862 	parent->active_channels--;
863 	if (!parent->active_channels) {
864 		usb_kill_anchored_urbs(&parent->rx_submitted);
865 		for (i = 0; i < GS_MAX_RX_URBS; i++)
866 			usb_free_coherent(dev->udev,
867 					  sizeof(struct gs_host_frame),
868 					  dev->rxbuf[i],
869 					  dev->rxbuf_dma[i]);
870 	}
871 
872 	/* Stop sending URBs */
873 	usb_kill_anchored_urbs(&dev->tx_submitted);
874 	atomic_set(&dev->active_tx_urbs, 0);
875 
876 	/* reset the device */
877 	rc = gs_cmd_reset(dev);
878 	if (rc < 0)
879 		netdev_warn(netdev, "Couldn't shutdown device (err=%d)", rc);
880 
881 	/* reset tx contexts */
882 	for (rc = 0; rc < GS_MAX_TX_URBS; rc++) {
883 		dev->tx_context[rc].dev = dev;
884 		dev->tx_context[rc].echo_id = GS_MAX_TX_URBS;
885 	}
886 
887 	/* close the netdev */
888 	close_candev(netdev);
889 
890 	return 0;
891 }
892 
893 static const struct net_device_ops gs_usb_netdev_ops = {
894 	.ndo_open = gs_can_open,
895 	.ndo_stop = gs_can_close,
896 	.ndo_start_xmit = gs_can_start_xmit,
897 	.ndo_change_mtu = can_change_mtu,
898 };
899 
900 static int gs_usb_set_identify(struct net_device *netdev, bool do_identify)
901 {
902 	struct gs_can *dev = netdev_priv(netdev);
903 	struct gs_identify_mode *imode;
904 	int rc;
905 
906 	imode = kmalloc(sizeof(*imode), GFP_KERNEL);
907 
908 	if (!imode)
909 		return -ENOMEM;
910 
911 	if (do_identify)
912 		imode->mode = cpu_to_le32(GS_CAN_IDENTIFY_ON);
913 	else
914 		imode->mode = cpu_to_le32(GS_CAN_IDENTIFY_OFF);
915 
916 	rc = usb_control_msg(interface_to_usbdev(dev->iface),
917 			     usb_sndctrlpipe(interface_to_usbdev(dev->iface), 0),
918 			     GS_USB_BREQ_IDENTIFY,
919 			     USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
920 			     dev->channel, 0, imode, sizeof(*imode), 100);
921 
922 	kfree(imode);
923 
924 	return (rc > 0) ? 0 : rc;
925 }
926 
927 /* blink LED's for finding the this interface */
928 static int gs_usb_set_phys_id(struct net_device *netdev,
929 			      enum ethtool_phys_id_state state)
930 {
931 	const struct gs_can *dev = netdev_priv(netdev);
932 	int rc = 0;
933 
934 	if (!(dev->feature & GS_CAN_FEATURE_IDENTIFY))
935 		return -EOPNOTSUPP;
936 
937 	switch (state) {
938 	case ETHTOOL_ID_ACTIVE:
939 		rc = gs_usb_set_identify(netdev, GS_CAN_IDENTIFY_ON);
940 		break;
941 	case ETHTOOL_ID_INACTIVE:
942 		rc = gs_usb_set_identify(netdev, GS_CAN_IDENTIFY_OFF);
943 		break;
944 	default:
945 		break;
946 	}
947 
948 	return rc;
949 }
950 
951 static const struct ethtool_ops gs_usb_ethtool_ops = {
952 	.set_phys_id = gs_usb_set_phys_id,
953 	.get_ts_info = ethtool_op_get_ts_info,
954 };
955 
956 static struct gs_can *gs_make_candev(unsigned int channel,
957 				     struct usb_interface *intf,
958 				     struct gs_device_config *dconf)
959 {
960 	struct gs_can *dev;
961 	struct net_device *netdev;
962 	int rc;
963 	struct gs_device_bt_const *bt_const;
964 	struct gs_device_bt_const_extended *bt_const_extended;
965 	u32 feature;
966 
967 	bt_const = kmalloc(sizeof(*bt_const), GFP_KERNEL);
968 	if (!bt_const)
969 		return ERR_PTR(-ENOMEM);
970 
971 	/* fetch bit timing constants */
972 	rc = usb_control_msg(interface_to_usbdev(intf),
973 			     usb_rcvctrlpipe(interface_to_usbdev(intf), 0),
974 			     GS_USB_BREQ_BT_CONST,
975 			     USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
976 			     channel, 0, bt_const, sizeof(*bt_const), 1000);
977 
978 	if (rc < 0) {
979 		dev_err(&intf->dev,
980 			"Couldn't get bit timing const for channel (err=%d)\n",
981 			rc);
982 		kfree(bt_const);
983 		return ERR_PTR(rc);
984 	}
985 
986 	/* create netdev */
987 	netdev = alloc_candev(sizeof(struct gs_can), GS_MAX_TX_URBS);
988 	if (!netdev) {
989 		dev_err(&intf->dev, "Couldn't allocate candev\n");
990 		kfree(bt_const);
991 		return ERR_PTR(-ENOMEM);
992 	}
993 
994 	dev = netdev_priv(netdev);
995 
996 	netdev->netdev_ops = &gs_usb_netdev_ops;
997 	netdev->ethtool_ops = &gs_usb_ethtool_ops;
998 
999 	netdev->flags |= IFF_ECHO; /* we support full roundtrip echo */
1000 
1001 	/* dev setup */
1002 	strcpy(dev->bt_const.name, KBUILD_MODNAME);
1003 	dev->bt_const.tseg1_min = le32_to_cpu(bt_const->tseg1_min);
1004 	dev->bt_const.tseg1_max = le32_to_cpu(bt_const->tseg1_max);
1005 	dev->bt_const.tseg2_min = le32_to_cpu(bt_const->tseg2_min);
1006 	dev->bt_const.tseg2_max = le32_to_cpu(bt_const->tseg2_max);
1007 	dev->bt_const.sjw_max = le32_to_cpu(bt_const->sjw_max);
1008 	dev->bt_const.brp_min = le32_to_cpu(bt_const->brp_min);
1009 	dev->bt_const.brp_max = le32_to_cpu(bt_const->brp_max);
1010 	dev->bt_const.brp_inc = le32_to_cpu(bt_const->brp_inc);
1011 
1012 	dev->udev = interface_to_usbdev(intf);
1013 	dev->iface = intf;
1014 	dev->netdev = netdev;
1015 	dev->channel = channel;
1016 
1017 	init_usb_anchor(&dev->tx_submitted);
1018 	atomic_set(&dev->active_tx_urbs, 0);
1019 	spin_lock_init(&dev->tx_ctx_lock);
1020 	for (rc = 0; rc < GS_MAX_TX_URBS; rc++) {
1021 		dev->tx_context[rc].dev = dev;
1022 		dev->tx_context[rc].echo_id = GS_MAX_TX_URBS;
1023 	}
1024 
1025 	/* can setup */
1026 	dev->can.state = CAN_STATE_STOPPED;
1027 	dev->can.clock.freq = le32_to_cpu(bt_const->fclk_can);
1028 	dev->can.bittiming_const = &dev->bt_const;
1029 	dev->can.do_set_bittiming = gs_usb_set_bittiming;
1030 
1031 	dev->can.ctrlmode_supported = CAN_CTRLMODE_CC_LEN8_DLC;
1032 
1033 	feature = le32_to_cpu(bt_const->feature);
1034 	dev->feature = FIELD_GET(GS_CAN_FEATURE_MASK, feature);
1035 	if (feature & GS_CAN_FEATURE_LISTEN_ONLY)
1036 		dev->can.ctrlmode_supported |= CAN_CTRLMODE_LISTENONLY;
1037 
1038 	if (feature & GS_CAN_FEATURE_LOOP_BACK)
1039 		dev->can.ctrlmode_supported |= CAN_CTRLMODE_LOOPBACK;
1040 
1041 	if (feature & GS_CAN_FEATURE_TRIPLE_SAMPLE)
1042 		dev->can.ctrlmode_supported |= CAN_CTRLMODE_3_SAMPLES;
1043 
1044 	if (feature & GS_CAN_FEATURE_ONE_SHOT)
1045 		dev->can.ctrlmode_supported |= CAN_CTRLMODE_ONE_SHOT;
1046 
1047 	if (feature & GS_CAN_FEATURE_FD) {
1048 		dev->can.ctrlmode_supported |= CAN_CTRLMODE_FD;
1049 		/* The data bit timing will be overwritten, if
1050 		 * GS_CAN_FEATURE_BT_CONST_EXT is set.
1051 		 */
1052 		dev->can.data_bittiming_const = &dev->bt_const;
1053 		dev->can.do_set_data_bittiming = gs_usb_set_data_bittiming;
1054 	}
1055 
1056 	/* The CANtact Pro from LinkLayer Labs is based on the
1057 	 * LPC54616 µC, which is affected by the NXP LPC USB transfer
1058 	 * erratum. However, the current firmware (version 2) doesn't
1059 	 * set the GS_CAN_FEATURE_REQ_USB_QUIRK_LPC546XX bit. Set the
1060 	 * feature GS_CAN_FEATURE_REQ_USB_QUIRK_LPC546XX to workaround
1061 	 * this issue.
1062 	 *
1063 	 * For the GS_USB_BREQ_DATA_BITTIMING USB control message the
1064 	 * CANtact Pro firmware uses a request value, which is already
1065 	 * used by the candleLight firmware for a different purpose
1066 	 * (GS_USB_BREQ_GET_USER_ID). Set the feature
1067 	 * GS_CAN_FEATURE_QUIRK_BREQ_CANTACT_PRO to workaround this
1068 	 * issue.
1069 	 */
1070 	if (dev->udev->descriptor.idVendor == cpu_to_le16(USB_GSUSB_1_VENDOR_ID) &&
1071 	    dev->udev->descriptor.idProduct == cpu_to_le16(USB_GSUSB_1_PRODUCT_ID) &&
1072 	    dev->udev->manufacturer && dev->udev->product &&
1073 	    !strcmp(dev->udev->manufacturer, "LinkLayer Labs") &&
1074 	    !strcmp(dev->udev->product, "CANtact Pro") &&
1075 	    (le32_to_cpu(dconf->sw_version) <= 2))
1076 		dev->feature |= GS_CAN_FEATURE_REQ_USB_QUIRK_LPC546XX |
1077 			GS_CAN_FEATURE_QUIRK_BREQ_CANTACT_PRO;
1078 
1079 	/* GS_CAN_FEATURE_IDENTIFY is only supported for sw_version > 1 */
1080 	if (!(le32_to_cpu(dconf->sw_version) > 1 &&
1081 	      feature & GS_CAN_FEATURE_IDENTIFY))
1082 		dev->feature &= ~GS_CAN_FEATURE_IDENTIFY;
1083 
1084 	kfree(bt_const);
1085 
1086 	/* fetch extended bit timing constants if device has feature
1087 	 * GS_CAN_FEATURE_FD and GS_CAN_FEATURE_BT_CONST_EXT
1088 	 */
1089 	if (feature & GS_CAN_FEATURE_FD &&
1090 	    feature & GS_CAN_FEATURE_BT_CONST_EXT) {
1091 		bt_const_extended = kmalloc(sizeof(*bt_const_extended), GFP_KERNEL);
1092 		if (!bt_const_extended)
1093 			return ERR_PTR(-ENOMEM);
1094 
1095 		rc = usb_control_msg(interface_to_usbdev(intf),
1096 				     usb_rcvctrlpipe(interface_to_usbdev(intf), 0),
1097 				     GS_USB_BREQ_BT_CONST_EXT,
1098 				     USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
1099 				     channel, 0, bt_const_extended,
1100 				     sizeof(*bt_const_extended),
1101 				     1000);
1102 		if (rc < 0) {
1103 			dev_err(&intf->dev,
1104 				"Couldn't get extended bit timing const for channel (err=%d)\n",
1105 				rc);
1106 			kfree(bt_const_extended);
1107 			return ERR_PTR(rc);
1108 		}
1109 
1110 		strcpy(dev->data_bt_const.name, KBUILD_MODNAME);
1111 		dev->data_bt_const.tseg1_min = le32_to_cpu(bt_const_extended->dtseg1_min);
1112 		dev->data_bt_const.tseg1_max = le32_to_cpu(bt_const_extended->dtseg1_max);
1113 		dev->data_bt_const.tseg2_min = le32_to_cpu(bt_const_extended->dtseg2_min);
1114 		dev->data_bt_const.tseg2_max = le32_to_cpu(bt_const_extended->dtseg2_max);
1115 		dev->data_bt_const.sjw_max = le32_to_cpu(bt_const_extended->dsjw_max);
1116 		dev->data_bt_const.brp_min = le32_to_cpu(bt_const_extended->dbrp_min);
1117 		dev->data_bt_const.brp_max = le32_to_cpu(bt_const_extended->dbrp_max);
1118 		dev->data_bt_const.brp_inc = le32_to_cpu(bt_const_extended->dbrp_inc);
1119 
1120 		dev->can.data_bittiming_const = &dev->data_bt_const;
1121 
1122 		kfree(bt_const_extended);
1123 	}
1124 
1125 	SET_NETDEV_DEV(netdev, &intf->dev);
1126 
1127 	rc = register_candev(dev->netdev);
1128 	if (rc) {
1129 		free_candev(dev->netdev);
1130 		dev_err(&intf->dev, "Couldn't register candev (err=%d)\n", rc);
1131 		return ERR_PTR(rc);
1132 	}
1133 
1134 	return dev;
1135 }
1136 
1137 static void gs_destroy_candev(struct gs_can *dev)
1138 {
1139 	unregister_candev(dev->netdev);
1140 	usb_kill_anchored_urbs(&dev->tx_submitted);
1141 	free_candev(dev->netdev);
1142 }
1143 
1144 static int gs_usb_probe(struct usb_interface *intf,
1145 			const struct usb_device_id *id)
1146 {
1147 	struct usb_device *udev = interface_to_usbdev(intf);
1148 	struct gs_host_frame *hf;
1149 	struct gs_usb *dev;
1150 	int rc = -ENOMEM;
1151 	unsigned int icount, i;
1152 	struct gs_host_config *hconf;
1153 	struct gs_device_config *dconf;
1154 
1155 	hconf = kmalloc(sizeof(*hconf), GFP_KERNEL);
1156 	if (!hconf)
1157 		return -ENOMEM;
1158 
1159 	hconf->byte_order = cpu_to_le32(0x0000beef);
1160 
1161 	/* send host config */
1162 	rc = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
1163 			     GS_USB_BREQ_HOST_FORMAT,
1164 			     USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
1165 			     1, intf->cur_altsetting->desc.bInterfaceNumber,
1166 			     hconf, sizeof(*hconf), 1000);
1167 
1168 	kfree(hconf);
1169 
1170 	if (rc < 0) {
1171 		dev_err(&intf->dev, "Couldn't send data format (err=%d)\n", rc);
1172 		return rc;
1173 	}
1174 
1175 	dconf = kmalloc(sizeof(*dconf), GFP_KERNEL);
1176 	if (!dconf)
1177 		return -ENOMEM;
1178 
1179 	/* read device config */
1180 	rc = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
1181 			     GS_USB_BREQ_DEVICE_CONFIG,
1182 			     USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
1183 			     1, intf->cur_altsetting->desc.bInterfaceNumber,
1184 			     dconf, sizeof(*dconf), 1000);
1185 	if (rc < 0) {
1186 		dev_err(&intf->dev, "Couldn't get device config: (err=%d)\n",
1187 			rc);
1188 		kfree(dconf);
1189 		return rc;
1190 	}
1191 
1192 	icount = dconf->icount + 1;
1193 	dev_info(&intf->dev, "Configuring for %u interfaces\n", icount);
1194 
1195 	if (icount > GS_MAX_INTF) {
1196 		dev_err(&intf->dev,
1197 			"Driver cannot handle more that %u CAN interfaces\n",
1198 			GS_MAX_INTF);
1199 		kfree(dconf);
1200 		return -EINVAL;
1201 	}
1202 
1203 	dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1204 	if (!dev) {
1205 		kfree(dconf);
1206 		return -ENOMEM;
1207 	}
1208 
1209 	init_usb_anchor(&dev->rx_submitted);
1210 	/* default to classic CAN, switch to CAN-FD if at least one of
1211 	 * our channels support CAN-FD.
1212 	 */
1213 	dev->hf_size_rx = struct_size(hf, classic_can, 1);
1214 
1215 	usb_set_intfdata(intf, dev);
1216 	dev->udev = udev;
1217 
1218 	for (i = 0; i < icount; i++) {
1219 		dev->canch[i] = gs_make_candev(i, intf, dconf);
1220 		if (IS_ERR_OR_NULL(dev->canch[i])) {
1221 			/* save error code to return later */
1222 			rc = PTR_ERR(dev->canch[i]);
1223 
1224 			/* on failure destroy previously created candevs */
1225 			icount = i;
1226 			for (i = 0; i < icount; i++)
1227 				gs_destroy_candev(dev->canch[i]);
1228 
1229 			usb_kill_anchored_urbs(&dev->rx_submitted);
1230 			kfree(dconf);
1231 			kfree(dev);
1232 			return rc;
1233 		}
1234 		dev->canch[i]->parent = dev;
1235 
1236 		if (dev->canch[i]->can.ctrlmode_supported & CAN_CTRLMODE_FD)
1237 			dev->hf_size_rx = struct_size(hf, canfd, 1);
1238 	}
1239 
1240 	kfree(dconf);
1241 
1242 	return 0;
1243 }
1244 
1245 static void gs_usb_disconnect(struct usb_interface *intf)
1246 {
1247 	struct gs_usb *dev = usb_get_intfdata(intf);
1248 	unsigned int i;
1249 
1250 	usb_set_intfdata(intf, NULL);
1251 
1252 	if (!dev) {
1253 		dev_err(&intf->dev, "Disconnect (nodata)\n");
1254 		return;
1255 	}
1256 
1257 	for (i = 0; i < GS_MAX_INTF; i++)
1258 		if (dev->canch[i])
1259 			gs_destroy_candev(dev->canch[i]);
1260 
1261 	usb_kill_anchored_urbs(&dev->rx_submitted);
1262 	kfree(dev);
1263 }
1264 
1265 static const struct usb_device_id gs_usb_table[] = {
1266 	{ USB_DEVICE_INTERFACE_NUMBER(USB_GSUSB_1_VENDOR_ID,
1267 				      USB_GSUSB_1_PRODUCT_ID, 0) },
1268 	{ USB_DEVICE_INTERFACE_NUMBER(USB_CANDLELIGHT_VENDOR_ID,
1269 				      USB_CANDLELIGHT_PRODUCT_ID, 0) },
1270 	{ USB_DEVICE_INTERFACE_NUMBER(USB_CES_CANEXT_FD_VENDOR_ID,
1271 				      USB_CES_CANEXT_FD_PRODUCT_ID, 0) },
1272 	{ USB_DEVICE_INTERFACE_NUMBER(USB_ABE_CANDEBUGGER_FD_VENDOR_ID,
1273 				      USB_ABE_CANDEBUGGER_FD_PRODUCT_ID, 0) },
1274 	{} /* Terminating entry */
1275 };
1276 
1277 MODULE_DEVICE_TABLE(usb, gs_usb_table);
1278 
1279 static struct usb_driver gs_usb_driver = {
1280 	.name = KBUILD_MODNAME,
1281 	.probe = gs_usb_probe,
1282 	.disconnect = gs_usb_disconnect,
1283 	.id_table = gs_usb_table,
1284 };
1285 
1286 module_usb_driver(gs_usb_driver);
1287 
1288 MODULE_AUTHOR("Maximilian Schneider <mws@schneidersoft.net>");
1289 MODULE_DESCRIPTION(
1290 "Socket CAN device driver for Geschwister Schneider Technologie-, "
1291 "Entwicklungs- und Vertriebs UG. USB2.0 to CAN interfaces\n"
1292 "and bytewerk.org candleLight USB CAN interfaces.");
1293 MODULE_LICENSE("GPL v2");
1294