]> git.itanic.dy.fi Git - linux-stable/blob - drivers/net/can/usb/gs_usb.c
d3a658b444b5f645fb7a579190fe7379bae1279c
[linux-stable] / drivers / net / can / usb / gs_usb.c
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         dm->mode = cpu_to_le32(GS_CAN_MODE_START);
828         dm->flags = cpu_to_le32(flags);
829         rc = usb_control_msg(interface_to_usbdev(dev->iface),
830                              usb_sndctrlpipe(interface_to_usbdev(dev->iface), 0),
831                              GS_USB_BREQ_MODE,
832                              USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
833                              dev->channel, 0, dm, sizeof(*dm), 1000);
834
835         if (rc < 0) {
836                 netdev_err(netdev, "Couldn't start device (err=%d)\n", rc);
837                 kfree(dm);
838                 return rc;
839         }
840
841         kfree(dm);
842
843         dev->can.state = CAN_STATE_ERROR_ACTIVE;
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 *dev,
929                               enum ethtool_phys_id_state state)
930 {
931         int rc = 0;
932
933         switch (state) {
934         case ETHTOOL_ID_ACTIVE:
935                 rc = gs_usb_set_identify(dev, GS_CAN_IDENTIFY_ON);
936                 break;
937         case ETHTOOL_ID_INACTIVE:
938                 rc = gs_usb_set_identify(dev, GS_CAN_IDENTIFY_OFF);
939                 break;
940         default:
941                 break;
942         }
943
944         return rc;
945 }
946
947 static const struct ethtool_ops gs_usb_ethtool_ops = {
948         .set_phys_id = gs_usb_set_phys_id,
949 };
950
951 static struct gs_can *gs_make_candev(unsigned int channel,
952                                      struct usb_interface *intf,
953                                      struct gs_device_config *dconf)
954 {
955         struct gs_can *dev;
956         struct net_device *netdev;
957         int rc;
958         struct gs_device_bt_const *bt_const;
959         struct gs_device_bt_const_extended *bt_const_extended;
960         u32 feature;
961
962         bt_const = kmalloc(sizeof(*bt_const), GFP_KERNEL);
963         if (!bt_const)
964                 return ERR_PTR(-ENOMEM);
965
966         /* fetch bit timing constants */
967         rc = usb_control_msg(interface_to_usbdev(intf),
968                              usb_rcvctrlpipe(interface_to_usbdev(intf), 0),
969                              GS_USB_BREQ_BT_CONST,
970                              USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
971                              channel, 0, bt_const, sizeof(*bt_const), 1000);
972
973         if (rc < 0) {
974                 dev_err(&intf->dev,
975                         "Couldn't get bit timing const for channel (err=%d)\n",
976                         rc);
977                 kfree(bt_const);
978                 return ERR_PTR(rc);
979         }
980
981         /* create netdev */
982         netdev = alloc_candev(sizeof(struct gs_can), GS_MAX_TX_URBS);
983         if (!netdev) {
984                 dev_err(&intf->dev, "Couldn't allocate candev\n");
985                 kfree(bt_const);
986                 return ERR_PTR(-ENOMEM);
987         }
988
989         dev = netdev_priv(netdev);
990
991         netdev->netdev_ops = &gs_usb_netdev_ops;
992
993         netdev->flags |= IFF_ECHO; /* we support full roundtrip echo */
994
995         /* dev setup */
996         strcpy(dev->bt_const.name, "gs_usb");
997         dev->bt_const.tseg1_min = le32_to_cpu(bt_const->tseg1_min);
998         dev->bt_const.tseg1_max = le32_to_cpu(bt_const->tseg1_max);
999         dev->bt_const.tseg2_min = le32_to_cpu(bt_const->tseg2_min);
1000         dev->bt_const.tseg2_max = le32_to_cpu(bt_const->tseg2_max);
1001         dev->bt_const.sjw_max = le32_to_cpu(bt_const->sjw_max);
1002         dev->bt_const.brp_min = le32_to_cpu(bt_const->brp_min);
1003         dev->bt_const.brp_max = le32_to_cpu(bt_const->brp_max);
1004         dev->bt_const.brp_inc = le32_to_cpu(bt_const->brp_inc);
1005
1006         dev->udev = interface_to_usbdev(intf);
1007         dev->iface = intf;
1008         dev->netdev = netdev;
1009         dev->channel = channel;
1010
1011         init_usb_anchor(&dev->tx_submitted);
1012         atomic_set(&dev->active_tx_urbs, 0);
1013         spin_lock_init(&dev->tx_ctx_lock);
1014         for (rc = 0; rc < GS_MAX_TX_URBS; rc++) {
1015                 dev->tx_context[rc].dev = dev;
1016                 dev->tx_context[rc].echo_id = GS_MAX_TX_URBS;
1017         }
1018
1019         /* can setup */
1020         dev->can.state = CAN_STATE_STOPPED;
1021         dev->can.clock.freq = le32_to_cpu(bt_const->fclk_can);
1022         dev->can.bittiming_const = &dev->bt_const;
1023         dev->can.do_set_bittiming = gs_usb_set_bittiming;
1024
1025         dev->can.ctrlmode_supported = CAN_CTRLMODE_CC_LEN8_DLC;
1026
1027         feature = le32_to_cpu(bt_const->feature);
1028         dev->feature = FIELD_GET(GS_CAN_FEATURE_MASK, feature);
1029         if (feature & GS_CAN_FEATURE_LISTEN_ONLY)
1030                 dev->can.ctrlmode_supported |= CAN_CTRLMODE_LISTENONLY;
1031
1032         if (feature & GS_CAN_FEATURE_LOOP_BACK)
1033                 dev->can.ctrlmode_supported |= CAN_CTRLMODE_LOOPBACK;
1034
1035         if (feature & GS_CAN_FEATURE_TRIPLE_SAMPLE)
1036                 dev->can.ctrlmode_supported |= CAN_CTRLMODE_3_SAMPLES;
1037
1038         if (feature & GS_CAN_FEATURE_ONE_SHOT)
1039                 dev->can.ctrlmode_supported |= CAN_CTRLMODE_ONE_SHOT;
1040
1041         if (feature & GS_CAN_FEATURE_FD) {
1042                 dev->can.ctrlmode_supported |= CAN_CTRLMODE_FD;
1043                 /* The data bit timing will be overwritten, if
1044                  * GS_CAN_FEATURE_BT_CONST_EXT is set.
1045                  */
1046                 dev->can.data_bittiming_const = &dev->bt_const;
1047                 dev->can.do_set_data_bittiming = gs_usb_set_data_bittiming;
1048         }
1049
1050         /* The CANtact Pro from LinkLayer Labs is based on the
1051          * LPC54616 µC, which is affected by the NXP LPC USB transfer
1052          * erratum. However, the current firmware (version 2) doesn't
1053          * set the GS_CAN_FEATURE_REQ_USB_QUIRK_LPC546XX bit. Set the
1054          * feature GS_CAN_FEATURE_REQ_USB_QUIRK_LPC546XX to workaround
1055          * this issue.
1056          *
1057          * For the GS_USB_BREQ_DATA_BITTIMING USB control message the
1058          * CANtact Pro firmware uses a request value, which is already
1059          * used by the candleLight firmware for a different purpose
1060          * (GS_USB_BREQ_GET_USER_ID). Set the feature
1061          * GS_CAN_FEATURE_QUIRK_BREQ_CANTACT_PRO to workaround this
1062          * issue.
1063          */
1064         if (dev->udev->descriptor.idVendor == cpu_to_le16(USB_GSUSB_1_VENDOR_ID) &&
1065             dev->udev->descriptor.idProduct == cpu_to_le16(USB_GSUSB_1_PRODUCT_ID) &&
1066             dev->udev->manufacturer && dev->udev->product &&
1067             !strcmp(dev->udev->manufacturer, "LinkLayer Labs") &&
1068             !strcmp(dev->udev->product, "CANtact Pro") &&
1069             (le32_to_cpu(dconf->sw_version) <= 2))
1070                 dev->feature |= GS_CAN_FEATURE_REQ_USB_QUIRK_LPC546XX |
1071                         GS_CAN_FEATURE_QUIRK_BREQ_CANTACT_PRO;
1072
1073         if (le32_to_cpu(dconf->sw_version) > 1)
1074                 if (feature & GS_CAN_FEATURE_IDENTIFY)
1075                         netdev->ethtool_ops = &gs_usb_ethtool_ops;
1076
1077         kfree(bt_const);
1078
1079         /* fetch extended bit timing constants if device has feature
1080          * GS_CAN_FEATURE_FD and GS_CAN_FEATURE_BT_CONST_EXT
1081          */
1082         if (feature & GS_CAN_FEATURE_FD &&
1083             feature & GS_CAN_FEATURE_BT_CONST_EXT) {
1084                 bt_const_extended = kmalloc(sizeof(*bt_const_extended), GFP_KERNEL);
1085                 if (!bt_const_extended)
1086                         return ERR_PTR(-ENOMEM);
1087
1088                 rc = usb_control_msg(interface_to_usbdev(intf),
1089                                      usb_rcvctrlpipe(interface_to_usbdev(intf), 0),
1090                                      GS_USB_BREQ_BT_CONST_EXT,
1091                                      USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
1092                                      channel, 0, bt_const_extended,
1093                                      sizeof(*bt_const_extended),
1094                                      1000);
1095                 if (rc < 0) {
1096                         dev_err(&intf->dev,
1097                                 "Couldn't get extended bit timing const for channel (err=%d)\n",
1098                                 rc);
1099                         kfree(bt_const_extended);
1100                         return ERR_PTR(rc);
1101                 }
1102
1103                 strcpy(dev->data_bt_const.name, "gs_usb");
1104                 dev->data_bt_const.tseg1_min = le32_to_cpu(bt_const_extended->dtseg1_min);
1105                 dev->data_bt_const.tseg1_max = le32_to_cpu(bt_const_extended->dtseg1_max);
1106                 dev->data_bt_const.tseg2_min = le32_to_cpu(bt_const_extended->dtseg2_min);
1107                 dev->data_bt_const.tseg2_max = le32_to_cpu(bt_const_extended->dtseg2_max);
1108                 dev->data_bt_const.sjw_max = le32_to_cpu(bt_const_extended->dsjw_max);
1109                 dev->data_bt_const.brp_min = le32_to_cpu(bt_const_extended->dbrp_min);
1110                 dev->data_bt_const.brp_max = le32_to_cpu(bt_const_extended->dbrp_max);
1111                 dev->data_bt_const.brp_inc = le32_to_cpu(bt_const_extended->dbrp_inc);
1112
1113                 dev->can.data_bittiming_const = &dev->data_bt_const;
1114
1115                 kfree(bt_const_extended);
1116         }
1117
1118         SET_NETDEV_DEV(netdev, &intf->dev);
1119
1120         rc = register_candev(dev->netdev);
1121         if (rc) {
1122                 free_candev(dev->netdev);
1123                 dev_err(&intf->dev, "Couldn't register candev (err=%d)\n", rc);
1124                 return ERR_PTR(rc);
1125         }
1126
1127         return dev;
1128 }
1129
1130 static void gs_destroy_candev(struct gs_can *dev)
1131 {
1132         unregister_candev(dev->netdev);
1133         usb_kill_anchored_urbs(&dev->tx_submitted);
1134         free_candev(dev->netdev);
1135 }
1136
1137 static int gs_usb_probe(struct usb_interface *intf,
1138                         const struct usb_device_id *id)
1139 {
1140         struct usb_device *udev = interface_to_usbdev(intf);
1141         struct gs_host_frame *hf;
1142         struct gs_usb *dev;
1143         int rc = -ENOMEM;
1144         unsigned int icount, i;
1145         struct gs_host_config *hconf;
1146         struct gs_device_config *dconf;
1147
1148         hconf = kmalloc(sizeof(*hconf), GFP_KERNEL);
1149         if (!hconf)
1150                 return -ENOMEM;
1151
1152         hconf->byte_order = cpu_to_le32(0x0000beef);
1153
1154         /* send host config */
1155         rc = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
1156                              GS_USB_BREQ_HOST_FORMAT,
1157                              USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
1158                              1, intf->cur_altsetting->desc.bInterfaceNumber,
1159                              hconf, sizeof(*hconf), 1000);
1160
1161         kfree(hconf);
1162
1163         if (rc < 0) {
1164                 dev_err(&intf->dev, "Couldn't send data format (err=%d)\n", rc);
1165                 return rc;
1166         }
1167
1168         dconf = kmalloc(sizeof(*dconf), GFP_KERNEL);
1169         if (!dconf)
1170                 return -ENOMEM;
1171
1172         /* read device config */
1173         rc = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
1174                              GS_USB_BREQ_DEVICE_CONFIG,
1175                              USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
1176                              1, intf->cur_altsetting->desc.bInterfaceNumber,
1177                              dconf, sizeof(*dconf), 1000);
1178         if (rc < 0) {
1179                 dev_err(&intf->dev, "Couldn't get device config: (err=%d)\n",
1180                         rc);
1181                 kfree(dconf);
1182                 return rc;
1183         }
1184
1185         icount = dconf->icount + 1;
1186         dev_info(&intf->dev, "Configuring for %u interfaces\n", icount);
1187
1188         if (icount > GS_MAX_INTF) {
1189                 dev_err(&intf->dev,
1190                         "Driver cannot handle more that %u CAN interfaces\n",
1191                         GS_MAX_INTF);
1192                 kfree(dconf);
1193                 return -EINVAL;
1194         }
1195
1196         dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1197         if (!dev) {
1198                 kfree(dconf);
1199                 return -ENOMEM;
1200         }
1201
1202         init_usb_anchor(&dev->rx_submitted);
1203         /* default to classic CAN, switch to CAN-FD if at least one of
1204          * our channels support CAN-FD.
1205          */
1206         dev->hf_size_rx = struct_size(hf, classic_can, 1);
1207
1208         usb_set_intfdata(intf, dev);
1209         dev->udev = udev;
1210
1211         for (i = 0; i < icount; i++) {
1212                 dev->canch[i] = gs_make_candev(i, intf, dconf);
1213                 if (IS_ERR_OR_NULL(dev->canch[i])) {
1214                         /* save error code to return later */
1215                         rc = PTR_ERR(dev->canch[i]);
1216
1217                         /* on failure destroy previously created candevs */
1218                         icount = i;
1219                         for (i = 0; i < icount; i++)
1220                                 gs_destroy_candev(dev->canch[i]);
1221
1222                         usb_kill_anchored_urbs(&dev->rx_submitted);
1223                         kfree(dconf);
1224                         kfree(dev);
1225                         return rc;
1226                 }
1227                 dev->canch[i]->parent = dev;
1228
1229                 if (dev->canch[i]->can.ctrlmode_supported & CAN_CTRLMODE_FD)
1230                         dev->hf_size_rx = struct_size(hf, canfd, 1);
1231         }
1232
1233         kfree(dconf);
1234
1235         return 0;
1236 }
1237
1238 static void gs_usb_disconnect(struct usb_interface *intf)
1239 {
1240         struct gs_usb *dev = usb_get_intfdata(intf);
1241         unsigned int i;
1242
1243         usb_set_intfdata(intf, NULL);
1244
1245         if (!dev) {
1246                 dev_err(&intf->dev, "Disconnect (nodata)\n");
1247                 return;
1248         }
1249
1250         for (i = 0; i < GS_MAX_INTF; i++)
1251                 if (dev->canch[i])
1252                         gs_destroy_candev(dev->canch[i]);
1253
1254         usb_kill_anchored_urbs(&dev->rx_submitted);
1255         kfree(dev);
1256 }
1257
1258 static const struct usb_device_id gs_usb_table[] = {
1259         { USB_DEVICE_INTERFACE_NUMBER(USB_GSUSB_1_VENDOR_ID,
1260                                       USB_GSUSB_1_PRODUCT_ID, 0) },
1261         { USB_DEVICE_INTERFACE_NUMBER(USB_CANDLELIGHT_VENDOR_ID,
1262                                       USB_CANDLELIGHT_PRODUCT_ID, 0) },
1263         { USB_DEVICE_INTERFACE_NUMBER(USB_CES_CANEXT_FD_VENDOR_ID,
1264                                       USB_CES_CANEXT_FD_PRODUCT_ID, 0) },
1265         { USB_DEVICE_INTERFACE_NUMBER(USB_ABE_CANDEBUGGER_FD_VENDOR_ID,
1266                                       USB_ABE_CANDEBUGGER_FD_PRODUCT_ID, 0) },
1267         {} /* Terminating entry */
1268 };
1269
1270 MODULE_DEVICE_TABLE(usb, gs_usb_table);
1271
1272 static struct usb_driver gs_usb_driver = {
1273         .name = "gs_usb",
1274         .probe = gs_usb_probe,
1275         .disconnect = gs_usb_disconnect,
1276         .id_table = gs_usb_table,
1277 };
1278
1279 module_usb_driver(gs_usb_driver);
1280
1281 MODULE_AUTHOR("Maximilian Schneider <mws@schneidersoft.net>");
1282 MODULE_DESCRIPTION(
1283 "Socket CAN device driver for Geschwister Schneider Technologie-, "
1284 "Entwicklungs- und Vertriebs UG. USB2.0 to CAN interfaces\n"
1285 "and bytewerk.org candleLight USB CAN interfaces.");
1286 MODULE_LICENSE("GPL v2");