usb: renesas_usbhs: disable TX IRQ before starting TX DMAC transfer
[linux/fpc-iii.git] / drivers / net / can / usb / gs_usb.c
blob047b63caa3b7c2fa67b7751b0ba2bcff5435c4c1
1 /* CAN driver for Geschwister Schneider USB/CAN devices.
3 * Copyright (C) 2013 Geschwister Schneider Technologie-,
4 * Entwicklungs- und Vertriebs UG (Haftungsbeschränkt).
6 * Many thanks to all socketcan devs!
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.
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.
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>
24 #include <linux/can.h>
25 #include <linux/can/dev.h>
26 #include <linux/can/error.h>
28 /* Device specific constants */
29 #define USB_GSUSB_1_VENDOR_ID 0x1d50
30 #define USB_GSUSB_1_PRODUCT_ID 0x606f
32 #define GSUSB_ENDPOINT_IN 1
33 #define GSUSB_ENDPOINT_OUT 2
35 /* Device specific constants */
36 enum 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
45 enum 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
52 enum 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
61 /* data types passed between host and device */
62 struct 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.
70 struct 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;
79 #define GS_CAN_MODE_NORMAL 0
80 #define GS_CAN_MODE_LISTEN_ONLY (1<<0)
81 #define GS_CAN_MODE_LOOP_BACK (1<<1)
82 #define GS_CAN_MODE_TRIPLE_SAMPLE (1<<2)
83 #define GS_CAN_MODE_ONE_SHOT (1<<3)
85 struct gs_device_mode {
86 u32 mode;
87 u32 flags;
88 } __packed;
90 struct gs_device_state {
91 u32 state;
92 u32 rxerr;
93 u32 txerr;
94 } __packed;
96 struct gs_device_bittiming {
97 u32 prop_seg;
98 u32 phase_seg1;
99 u32 phase_seg2;
100 u32 sjw;
101 u32 brp;
102 } __packed;
104 #define GS_CAN_FEATURE_LISTEN_ONLY (1<<0)
105 #define GS_CAN_FEATURE_LOOP_BACK (1<<1)
106 #define GS_CAN_FEATURE_TRIPLE_SAMPLE (1<<2)
107 #define GS_CAN_FEATURE_ONE_SHOT (1<<3)
109 struct 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;
122 #define GS_CAN_FLAG_OVERFLOW 1
124 struct gs_host_frame {
125 u32 echo_id;
126 u32 can_id;
128 u8 can_dlc;
129 u8 channel;
130 u8 flags;
131 u8 reserved;
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.
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.
146 #define GS_MAX_INTF 2
148 struct gs_tx_context {
149 struct gs_can *dev;
150 unsigned int echo_id;
153 struct gs_can {
154 struct can_priv can; /* must be the first member */
156 struct gs_usb *parent;
158 struct net_device *netdev;
159 struct usb_device *udev;
160 struct usb_interface *iface;
162 struct can_bittiming_const bt_const;
163 unsigned int channel; /* channel number */
165 /* This lock prevents a race condition between xmit and recieve. */
166 spinlock_t tx_ctx_lock;
167 struct gs_tx_context tx_context[GS_MAX_TX_URBS];
169 struct usb_anchor tx_submitted;
170 atomic_t active_tx_urbs;
173 /* usb interface struct */
174 struct 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;
181 /* 'allocate' a tx context.
182 * returns a valid tx context or NULL if there is no space.
184 static struct gs_tx_context *gs_alloc_tx_context(struct gs_can *dev)
186 int i = 0;
187 unsigned long flags;
189 spin_lock_irqsave(&dev->tx_ctx_lock, flags);
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];
199 spin_unlock_irqrestore(&dev->tx_ctx_lock, flags);
200 return NULL;
203 /* releases a tx context
205 static void gs_free_tx_context(struct gs_tx_context *txc)
207 txc->echo_id = GS_MAX_TX_URBS;
210 /* Get a tx context by id.
212 static struct gs_tx_context *gs_get_tx_context(struct gs_can *dev, unsigned int id)
214 unsigned long flags;
216 if (id < GS_MAX_TX_URBS) {
217 spin_lock_irqsave(&dev->tx_ctx_lock, flags);
218 if (dev->tx_context[id].echo_id == id) {
219 spin_unlock_irqrestore(&dev->tx_ctx_lock, flags);
220 return &dev->tx_context[id];
222 spin_unlock_irqrestore(&dev->tx_ctx_lock, flags);
224 return NULL;
227 static int gs_cmd_reset(struct gs_usb *gsusb, struct gs_can *gsdev)
229 struct gs_device_mode *dm;
230 struct usb_interface *intf = gsdev->iface;
231 int rc;
233 dm = kzalloc(sizeof(*dm), GFP_KERNEL);
234 if (!dm)
235 return -ENOMEM;
237 dm->mode = GS_CAN_MODE_RESET;
239 rc = usb_control_msg(interface_to_usbdev(intf),
240 usb_sndctrlpipe(interface_to_usbdev(intf), 0),
241 GS_USB_BREQ_MODE,
242 USB_DIR_OUT|USB_TYPE_VENDOR|USB_RECIP_INTERFACE,
243 gsdev->channel,
246 sizeof(*dm),
247 1000);
249 return rc;
252 static void gs_update_state(struct gs_can *dev, struct can_frame *cf)
254 struct can_device_stats *can_stats = &dev->can.can_stats;
256 if (cf->can_id & CAN_ERR_RESTARTED) {
257 dev->can.state = CAN_STATE_ERROR_ACTIVE;
258 can_stats->restarts++;
259 } else if (cf->can_id & CAN_ERR_BUSOFF) {
260 dev->can.state = CAN_STATE_BUS_OFF;
261 can_stats->bus_off++;
262 } else if (cf->can_id & CAN_ERR_CRTL) {
263 if ((cf->data[1] & CAN_ERR_CRTL_TX_WARNING) ||
264 (cf->data[1] & CAN_ERR_CRTL_RX_WARNING)) {
265 dev->can.state = CAN_STATE_ERROR_WARNING;
266 can_stats->error_warning++;
267 } else if ((cf->data[1] & CAN_ERR_CRTL_TX_PASSIVE) ||
268 (cf->data[1] & CAN_ERR_CRTL_RX_PASSIVE)) {
269 dev->can.state = CAN_STATE_ERROR_PASSIVE;
270 can_stats->error_passive++;
271 } else {
272 dev->can.state = CAN_STATE_ERROR_ACTIVE;
277 static void gs_usb_recieve_bulk_callback(struct urb *urb)
279 struct gs_usb *usbcan = urb->context;
280 struct gs_can *dev;
281 struct net_device *netdev;
282 int rc;
283 struct net_device_stats *stats;
284 struct gs_host_frame *hf = urb->transfer_buffer;
285 struct gs_tx_context *txc;
286 struct can_frame *cf;
287 struct sk_buff *skb;
289 BUG_ON(!usbcan);
291 switch (urb->status) {
292 case 0: /* success */
293 break;
294 case -ENOENT:
295 case -ESHUTDOWN:
296 return;
297 default:
298 /* do not resubmit aborted urbs. eg: when device goes down */
299 return;
302 /* device reports out of range channel id */
303 if (hf->channel >= GS_MAX_INTF)
304 goto resubmit_urb;
306 dev = usbcan->canch[hf->channel];
308 netdev = dev->netdev;
309 stats = &netdev->stats;
311 if (!netif_device_present(netdev))
312 return;
314 if (hf->echo_id == -1) { /* normal rx */
315 skb = alloc_can_skb(dev->netdev, &cf);
316 if (!skb)
317 return;
319 cf->can_id = hf->can_id;
321 cf->can_dlc = get_can_dlc(hf->can_dlc);
322 memcpy(cf->data, hf->data, 8);
324 /* ERROR frames tell us information about the controller */
325 if (hf->can_id & CAN_ERR_FLAG)
326 gs_update_state(dev, cf);
328 netdev->stats.rx_packets++;
329 netdev->stats.rx_bytes += hf->can_dlc;
331 netif_rx(skb);
332 } else { /* echo_id == hf->echo_id */
333 if (hf->echo_id >= GS_MAX_TX_URBS) {
334 netdev_err(netdev,
335 "Unexpected out of range echo id %d\n",
336 hf->echo_id);
337 goto resubmit_urb;
340 netdev->stats.tx_packets++;
341 netdev->stats.tx_bytes += hf->can_dlc;
343 txc = gs_get_tx_context(dev, hf->echo_id);
345 /* bad devices send bad echo_ids. */
346 if (!txc) {
347 netdev_err(netdev,
348 "Unexpected unused echo id %d\n",
349 hf->echo_id);
350 goto resubmit_urb;
353 can_get_echo_skb(netdev, hf->echo_id);
355 gs_free_tx_context(txc);
357 netif_wake_queue(netdev);
360 if (hf->flags & GS_CAN_FLAG_OVERFLOW) {
361 skb = alloc_can_err_skb(netdev, &cf);
362 if (!skb)
363 goto resubmit_urb;
365 cf->can_id |= CAN_ERR_CRTL;
366 cf->can_dlc = CAN_ERR_DLC;
367 cf->data[1] = CAN_ERR_CRTL_RX_OVERFLOW;
368 stats->rx_over_errors++;
369 stats->rx_errors++;
370 netif_rx(skb);
373 resubmit_urb:
374 usb_fill_bulk_urb(urb,
375 usbcan->udev,
376 usb_rcvbulkpipe(usbcan->udev, GSUSB_ENDPOINT_IN),
378 sizeof(struct gs_host_frame),
379 gs_usb_recieve_bulk_callback,
380 usbcan
383 rc = usb_submit_urb(urb, GFP_ATOMIC);
385 /* USB failure take down all interfaces */
386 if (rc == -ENODEV) {
387 for (rc = 0; rc < GS_MAX_INTF; rc++) {
388 if (usbcan->canch[rc])
389 netif_device_detach(usbcan->canch[rc]->netdev);
394 static int gs_usb_set_bittiming(struct net_device *netdev)
396 struct gs_can *dev = netdev_priv(netdev);
397 struct can_bittiming *bt = &dev->can.bittiming;
398 struct usb_interface *intf = dev->iface;
399 int rc;
400 struct gs_device_bittiming *dbt;
402 dbt = kmalloc(sizeof(*dbt), GFP_KERNEL);
403 if (!dbt)
404 return -ENOMEM;
406 dbt->prop_seg = bt->prop_seg;
407 dbt->phase_seg1 = bt->phase_seg1;
408 dbt->phase_seg2 = bt->phase_seg2;
409 dbt->sjw = bt->sjw;
410 dbt->brp = bt->brp;
412 /* request bit timings */
413 rc = usb_control_msg(interface_to_usbdev(intf),
414 usb_sndctrlpipe(interface_to_usbdev(intf), 0),
415 GS_USB_BREQ_BITTIMING,
416 USB_DIR_OUT|USB_TYPE_VENDOR|USB_RECIP_INTERFACE,
417 dev->channel,
419 dbt,
420 sizeof(*dbt),
421 1000);
423 kfree(dbt);
425 if (rc < 0)
426 dev_err(netdev->dev.parent, "Couldn't set bittimings (err=%d)",
427 rc);
429 return rc;
432 static void gs_usb_xmit_callback(struct urb *urb)
434 struct gs_tx_context *txc = urb->context;
435 struct gs_can *dev = txc->dev;
436 struct net_device *netdev = dev->netdev;
438 if (urb->status)
439 netdev_info(netdev, "usb xmit fail %d\n", txc->echo_id);
441 usb_free_coherent(urb->dev,
442 urb->transfer_buffer_length,
443 urb->transfer_buffer,
444 urb->transfer_dma);
446 atomic_dec(&dev->active_tx_urbs);
448 if (!netif_device_present(netdev))
449 return;
451 if (netif_queue_stopped(netdev))
452 netif_wake_queue(netdev);
455 static netdev_tx_t gs_can_start_xmit(struct sk_buff *skb, struct net_device *netdev)
457 struct gs_can *dev = netdev_priv(netdev);
458 struct net_device_stats *stats = &dev->netdev->stats;
459 struct urb *urb;
460 struct gs_host_frame *hf;
461 struct can_frame *cf;
462 int rc;
463 unsigned int idx;
464 struct gs_tx_context *txc;
466 if (can_dropped_invalid_skb(netdev, skb))
467 return NETDEV_TX_OK;
469 /* find an empty context to keep track of transmission */
470 txc = gs_alloc_tx_context(dev);
471 if (!txc)
472 return NETDEV_TX_BUSY;
474 /* create a URB, and a buffer for it */
475 urb = usb_alloc_urb(0, GFP_ATOMIC);
476 if (!urb) {
477 netdev_err(netdev, "No memory left for URB\n");
478 goto nomem_urb;
481 hf = usb_alloc_coherent(dev->udev, sizeof(*hf), GFP_ATOMIC,
482 &urb->transfer_dma);
483 if (!hf) {
484 netdev_err(netdev, "No memory left for USB buffer\n");
485 goto nomem_hf;
488 idx = txc->echo_id;
490 if (idx >= GS_MAX_TX_URBS) {
491 netdev_err(netdev, "Invalid tx context %d\n", idx);
492 goto badidx;
495 hf->echo_id = idx;
496 hf->channel = dev->channel;
498 cf = (struct can_frame *)skb->data;
500 hf->can_id = cf->can_id;
501 hf->can_dlc = cf->can_dlc;
502 memcpy(hf->data, cf->data, cf->can_dlc);
504 usb_fill_bulk_urb(urb, dev->udev,
505 usb_sndbulkpipe(dev->udev, GSUSB_ENDPOINT_OUT),
507 sizeof(*hf),
508 gs_usb_xmit_callback,
509 txc);
511 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
512 usb_anchor_urb(urb, &dev->tx_submitted);
514 can_put_echo_skb(skb, netdev, idx);
516 atomic_inc(&dev->active_tx_urbs);
518 rc = usb_submit_urb(urb, GFP_ATOMIC);
519 if (unlikely(rc)) { /* usb send failed */
520 atomic_dec(&dev->active_tx_urbs);
522 can_free_echo_skb(netdev, idx);
523 gs_free_tx_context(txc);
525 usb_unanchor_urb(urb);
526 usb_free_coherent(dev->udev,
527 sizeof(*hf),
529 urb->transfer_dma);
532 if (rc == -ENODEV) {
533 netif_device_detach(netdev);
534 } else {
535 netdev_err(netdev, "usb_submit failed (err=%d)\n", rc);
536 stats->tx_dropped++;
538 } else {
539 /* Slow down tx path */
540 if (atomic_read(&dev->active_tx_urbs) >= GS_MAX_TX_URBS)
541 netif_stop_queue(netdev);
544 /* let usb core take care of this urb */
545 usb_free_urb(urb);
547 return NETDEV_TX_OK;
549 badidx:
550 usb_free_coherent(dev->udev,
551 sizeof(*hf),
553 urb->transfer_dma);
554 nomem_hf:
555 usb_free_urb(urb);
557 nomem_urb:
558 gs_free_tx_context(txc);
559 dev_kfree_skb(skb);
560 stats->tx_dropped++;
561 return NETDEV_TX_OK;
564 static int gs_can_open(struct net_device *netdev)
566 struct gs_can *dev = netdev_priv(netdev);
567 struct gs_usb *parent = dev->parent;
568 int rc, i;
569 struct gs_device_mode *dm;
570 u32 ctrlmode;
572 rc = open_candev(netdev);
573 if (rc)
574 return rc;
576 if (atomic_add_return(1, &parent->active_channels) == 1) {
577 for (i = 0; i < GS_MAX_RX_URBS; i++) {
578 struct urb *urb;
579 u8 *buf;
581 /* alloc rx urb */
582 urb = usb_alloc_urb(0, GFP_KERNEL);
583 if (!urb) {
584 netdev_err(netdev,
585 "No memory left for URB\n");
586 return -ENOMEM;
589 /* alloc rx buffer */
590 buf = usb_alloc_coherent(dev->udev,
591 sizeof(struct gs_host_frame),
592 GFP_KERNEL,
593 &urb->transfer_dma);
594 if (!buf) {
595 netdev_err(netdev,
596 "No memory left for USB buffer\n");
597 usb_free_urb(urb);
598 return -ENOMEM;
601 /* fill, anchor, and submit rx urb */
602 usb_fill_bulk_urb(urb,
603 dev->udev,
604 usb_rcvbulkpipe(dev->udev,
605 GSUSB_ENDPOINT_IN),
606 buf,
607 sizeof(struct gs_host_frame),
608 gs_usb_recieve_bulk_callback,
609 parent);
610 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
612 usb_anchor_urb(urb, &parent->rx_submitted);
614 rc = usb_submit_urb(urb, GFP_KERNEL);
615 if (rc) {
616 if (rc == -ENODEV)
617 netif_device_detach(dev->netdev);
619 netdev_err(netdev,
620 "usb_submit failed (err=%d)\n",
621 rc);
623 usb_unanchor_urb(urb);
624 break;
627 /* Drop reference,
628 * USB core will take care of freeing it
630 usb_free_urb(urb);
634 dm = kmalloc(sizeof(*dm), GFP_KERNEL);
635 if (!dm)
636 return -ENOMEM;
638 /* flags */
639 ctrlmode = dev->can.ctrlmode;
640 dm->flags = 0;
642 if (ctrlmode & CAN_CTRLMODE_LOOPBACK)
643 dm->flags |= GS_CAN_MODE_LOOP_BACK;
644 else if (ctrlmode & CAN_CTRLMODE_LISTENONLY)
645 dm->flags |= GS_CAN_MODE_LISTEN_ONLY;
647 /* Controller is not allowed to retry TX
648 * this mode is unavailable on atmels uc3c hardware
650 if (ctrlmode & CAN_CTRLMODE_ONE_SHOT)
651 dm->flags |= GS_CAN_MODE_ONE_SHOT;
653 if (ctrlmode & CAN_CTRLMODE_3_SAMPLES)
654 dm->flags |= GS_CAN_MODE_TRIPLE_SAMPLE;
656 /* finally start device */
657 dm->mode = GS_CAN_MODE_START;
658 rc = usb_control_msg(interface_to_usbdev(dev->iface),
659 usb_sndctrlpipe(interface_to_usbdev(dev->iface), 0),
660 GS_USB_BREQ_MODE,
661 USB_DIR_OUT|USB_TYPE_VENDOR|USB_RECIP_INTERFACE,
662 dev->channel,
665 sizeof(*dm),
666 1000);
668 if (rc < 0) {
669 netdev_err(netdev, "Couldn't start device (err=%d)\n", rc);
670 kfree(dm);
671 return rc;
674 kfree(dm);
676 dev->can.state = CAN_STATE_ERROR_ACTIVE;
678 if (!(dev->can.ctrlmode & CAN_CTRLMODE_LISTENONLY))
679 netif_start_queue(netdev);
681 return 0;
684 static int gs_can_close(struct net_device *netdev)
686 int rc;
687 struct gs_can *dev = netdev_priv(netdev);
688 struct gs_usb *parent = dev->parent;
690 netif_stop_queue(netdev);
692 /* Stop polling */
693 if (atomic_dec_and_test(&parent->active_channels))
694 usb_kill_anchored_urbs(&parent->rx_submitted);
696 /* Stop sending URBs */
697 usb_kill_anchored_urbs(&dev->tx_submitted);
698 atomic_set(&dev->active_tx_urbs, 0);
700 /* reset the device */
701 rc = gs_cmd_reset(parent, dev);
702 if (rc < 0)
703 netdev_warn(netdev, "Couldn't shutdown device (err=%d)", rc);
705 /* reset tx contexts */
706 for (rc = 0; rc < GS_MAX_TX_URBS; rc++) {
707 dev->tx_context[rc].dev = dev;
708 dev->tx_context[rc].echo_id = GS_MAX_TX_URBS;
711 /* close the netdev */
712 close_candev(netdev);
714 return 0;
717 static const struct net_device_ops gs_usb_netdev_ops = {
718 .ndo_open = gs_can_open,
719 .ndo_stop = gs_can_close,
720 .ndo_start_xmit = gs_can_start_xmit,
723 static struct gs_can *gs_make_candev(unsigned int channel, struct usb_interface *intf)
725 struct gs_can *dev;
726 struct net_device *netdev;
727 int rc;
728 struct gs_device_bt_const *bt_const;
730 bt_const = kmalloc(sizeof(*bt_const), GFP_KERNEL);
731 if (!bt_const)
732 return ERR_PTR(-ENOMEM);
734 /* fetch bit timing constants */
735 rc = usb_control_msg(interface_to_usbdev(intf),
736 usb_rcvctrlpipe(interface_to_usbdev(intf), 0),
737 GS_USB_BREQ_BT_CONST,
738 USB_DIR_IN|USB_TYPE_VENDOR|USB_RECIP_INTERFACE,
739 channel,
741 bt_const,
742 sizeof(*bt_const),
743 1000);
745 if (rc < 0) {
746 dev_err(&intf->dev,
747 "Couldn't get bit timing const for channel (err=%d)\n",
748 rc);
749 kfree(bt_const);
750 return ERR_PTR(rc);
753 /* create netdev */
754 netdev = alloc_candev(sizeof(struct gs_can), GS_MAX_TX_URBS);
755 if (!netdev) {
756 dev_err(&intf->dev, "Couldn't allocate candev\n");
757 kfree(bt_const);
758 return ERR_PTR(-ENOMEM);
761 dev = netdev_priv(netdev);
763 netdev->netdev_ops = &gs_usb_netdev_ops;
765 netdev->flags |= IFF_ECHO; /* we support full roundtrip echo */
767 /* dev settup */
768 strcpy(dev->bt_const.name, "gs_usb");
769 dev->bt_const.tseg1_min = bt_const->tseg1_min;
770 dev->bt_const.tseg1_max = bt_const->tseg1_max;
771 dev->bt_const.tseg2_min = bt_const->tseg2_min;
772 dev->bt_const.tseg2_max = bt_const->tseg2_max;
773 dev->bt_const.sjw_max = bt_const->sjw_max;
774 dev->bt_const.brp_min = bt_const->brp_min;
775 dev->bt_const.brp_max = bt_const->brp_max;
776 dev->bt_const.brp_inc = bt_const->brp_inc;
778 dev->udev = interface_to_usbdev(intf);
779 dev->iface = intf;
780 dev->netdev = netdev;
781 dev->channel = channel;
783 init_usb_anchor(&dev->tx_submitted);
784 atomic_set(&dev->active_tx_urbs, 0);
785 spin_lock_init(&dev->tx_ctx_lock);
786 for (rc = 0; rc < GS_MAX_TX_URBS; rc++) {
787 dev->tx_context[rc].dev = dev;
788 dev->tx_context[rc].echo_id = GS_MAX_TX_URBS;
791 /* can settup */
792 dev->can.state = CAN_STATE_STOPPED;
793 dev->can.clock.freq = bt_const->fclk_can;
794 dev->can.bittiming_const = &dev->bt_const;
795 dev->can.do_set_bittiming = gs_usb_set_bittiming;
797 dev->can.ctrlmode_supported = 0;
799 if (bt_const->feature & GS_CAN_FEATURE_LISTEN_ONLY)
800 dev->can.ctrlmode_supported |= CAN_CTRLMODE_LISTENONLY;
802 if (bt_const->feature & GS_CAN_FEATURE_LOOP_BACK)
803 dev->can.ctrlmode_supported |= CAN_CTRLMODE_LOOPBACK;
805 if (bt_const->feature & GS_CAN_FEATURE_TRIPLE_SAMPLE)
806 dev->can.ctrlmode_supported |= CAN_CTRLMODE_3_SAMPLES;
808 if (bt_const->feature & GS_CAN_FEATURE_ONE_SHOT)
809 dev->can.ctrlmode_supported |= CAN_CTRLMODE_ONE_SHOT;
811 kfree(bt_const);
813 SET_NETDEV_DEV(netdev, &intf->dev);
815 rc = register_candev(dev->netdev);
816 if (rc) {
817 free_candev(dev->netdev);
818 dev_err(&intf->dev, "Couldn't register candev (err=%d)\n", rc);
819 return ERR_PTR(rc);
822 return dev;
825 static void gs_destroy_candev(struct gs_can *dev)
827 unregister_candev(dev->netdev);
828 usb_kill_anchored_urbs(&dev->tx_submitted);
829 free_candev(dev->netdev);
832 static int gs_usb_probe(struct usb_interface *intf, const struct usb_device_id *id)
834 struct gs_usb *dev;
835 int rc = -ENOMEM;
836 unsigned int icount, i;
837 struct gs_host_config *hconf;
838 struct gs_device_config *dconf;
840 hconf = kmalloc(sizeof(*hconf), GFP_KERNEL);
841 if (!hconf)
842 return -ENOMEM;
844 hconf->byte_order = 0x0000beef;
846 /* send host config */
847 rc = usb_control_msg(interface_to_usbdev(intf),
848 usb_sndctrlpipe(interface_to_usbdev(intf), 0),
849 GS_USB_BREQ_HOST_FORMAT,
850 USB_DIR_OUT|USB_TYPE_VENDOR|USB_RECIP_INTERFACE,
852 intf->altsetting[0].desc.bInterfaceNumber,
853 hconf,
854 sizeof(*hconf),
855 1000);
857 kfree(hconf);
859 if (rc < 0) {
860 dev_err(&intf->dev, "Couldn't send data format (err=%d)\n",
861 rc);
862 return rc;
865 dconf = kmalloc(sizeof(*dconf), GFP_KERNEL);
866 if (!dconf)
867 return -ENOMEM;
869 /* read device config */
870 rc = usb_control_msg(interface_to_usbdev(intf),
871 usb_rcvctrlpipe(interface_to_usbdev(intf), 0),
872 GS_USB_BREQ_DEVICE_CONFIG,
873 USB_DIR_IN|USB_TYPE_VENDOR|USB_RECIP_INTERFACE,
875 intf->altsetting[0].desc.bInterfaceNumber,
876 dconf,
877 sizeof(*dconf),
878 1000);
879 if (rc < 0) {
880 dev_err(&intf->dev, "Couldn't get device config: (err=%d)\n",
881 rc);
883 kfree(dconf);
885 return rc;
888 icount = dconf->icount+1;
890 kfree(dconf);
892 dev_info(&intf->dev, "Configuring for %d interfaces\n", icount);
894 if (icount > GS_MAX_INTF) {
895 dev_err(&intf->dev,
896 "Driver cannot handle more that %d CAN interfaces\n",
897 GS_MAX_INTF);
898 return -EINVAL;
901 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
902 init_usb_anchor(&dev->rx_submitted);
904 atomic_set(&dev->active_channels, 0);
906 usb_set_intfdata(intf, dev);
907 dev->udev = interface_to_usbdev(intf);
909 for (i = 0; i < icount; i++) {
910 dev->canch[i] = gs_make_candev(i, intf);
911 if (IS_ERR_OR_NULL(dev->canch[i])) {
912 /* save error code to return later */
913 rc = PTR_ERR(dev->canch[i]);
915 /* on failure destroy previously created candevs */
916 icount = i;
917 for (i = 0; i < icount; i++)
918 gs_destroy_candev(dev->canch[i]);
920 usb_kill_anchored_urbs(&dev->rx_submitted);
921 kfree(dev);
922 return rc;
924 dev->canch[i]->parent = dev;
927 return 0;
930 static void gs_usb_disconnect(struct usb_interface *intf)
932 unsigned i;
933 struct gs_usb *dev = usb_get_intfdata(intf);
934 usb_set_intfdata(intf, NULL);
936 if (!dev) {
937 dev_err(&intf->dev, "Disconnect (nodata)\n");
938 return;
941 for (i = 0; i < GS_MAX_INTF; i++)
942 if (dev->canch[i])
943 gs_destroy_candev(dev->canch[i]);
945 usb_kill_anchored_urbs(&dev->rx_submitted);
946 kfree(dev);
949 static const struct usb_device_id gs_usb_table[] = {
950 {USB_DEVICE(USB_GSUSB_1_VENDOR_ID, USB_GSUSB_1_PRODUCT_ID)},
951 {} /* Terminating entry */
954 MODULE_DEVICE_TABLE(usb, gs_usb_table);
956 static struct usb_driver gs_usb_driver = {
957 .name = "gs_usb",
958 .probe = gs_usb_probe,
959 .disconnect = gs_usb_disconnect,
960 .id_table = gs_usb_table,
963 module_usb_driver(gs_usb_driver);
965 MODULE_AUTHOR("Maximilian Schneider <mws@schneidersoft.net>");
966 MODULE_DESCRIPTION(
967 "Socket CAN device driver for Geschwister Schneider Technologie-, "
968 "Entwicklungs- und Vertriebs UG. USB2.0 to CAN interfaces.");
969 MODULE_LICENSE("GPL v2");