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