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