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