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.
5 * Copyright (C) 2013-2016 Geschwister Schneider Technologie-,
6 * Entwicklungs- und Vertriebs UG (Haftungsbeschränkt).
7 * Copyright (C) 2016 Hubert Denkmair
9 * Many thanks to all socketcan devs!
12 #include <linux/ethtool.h>
13 #include <linux/init.h>
14 #include <linux/signal.h>
15 #include <linux/module.h>
16 #include <linux/netdevice.h>
17 #include <linux/usb.h>
19 #include <linux/can.h>
20 #include <linux/can/dev.h>
21 #include <linux/can/error.h>
23 /* Device specific constants */
24 #define USB_GSUSB_1_VENDOR_ID 0x1d50
25 #define USB_GSUSB_1_PRODUCT_ID 0x606f
27 #define USB_CANDLELIGHT_VENDOR_ID 0x1209
28 #define USB_CANDLELIGHT_PRODUCT_ID 0x2323
30 #define GSUSB_ENDPOINT_IN 1
31 #define GSUSB_ENDPOINT_OUT 2
33 /* Device specific constants */
35 GS_USB_BREQ_HOST_FORMAT = 0,
36 GS_USB_BREQ_BITTIMING,
40 GS_USB_BREQ_DEVICE_CONFIG,
41 GS_USB_BREQ_TIMESTAMP,
46 /* reset a channel. turns it off */
47 GS_CAN_MODE_RESET = 0,
48 /* starts a channel */
53 GS_CAN_STATE_ERROR_ACTIVE = 0,
54 GS_CAN_STATE_ERROR_WARNING,
55 GS_CAN_STATE_ERROR_PASSIVE,
61 enum gs_can_identify_mode {
62 GS_CAN_IDENTIFY_OFF = 0,
66 /* data types passed between host and device */
67 struct gs_host_config {
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.
75 struct gs_device_config {
84 #define GS_CAN_MODE_NORMAL 0
85 #define GS_CAN_MODE_LISTEN_ONLY BIT(0)
86 #define GS_CAN_MODE_LOOP_BACK BIT(1)
87 #define GS_CAN_MODE_TRIPLE_SAMPLE BIT(2)
88 #define GS_CAN_MODE_ONE_SHOT BIT(3)
90 struct gs_device_mode {
95 struct gs_device_state {
101 struct gs_device_bittiming {
109 struct gs_identify_mode {
113 #define GS_CAN_FEATURE_LISTEN_ONLY BIT(0)
114 #define GS_CAN_FEATURE_LOOP_BACK BIT(1)
115 #define GS_CAN_FEATURE_TRIPLE_SAMPLE BIT(2)
116 #define GS_CAN_FEATURE_ONE_SHOT BIT(3)
117 #define GS_CAN_FEATURE_HW_TIMESTAMP BIT(4)
118 #define GS_CAN_FEATURE_IDENTIFY BIT(5)
120 struct gs_device_bt_const {
133 #define GS_CAN_FLAG_OVERFLOW 1
135 struct gs_host_frame {
146 /* The GS USB devices make use of the same flags and masks as in
147 * linux/can.h and linux/can/error.h, and no additional mapping is necessary.
150 /* Only send a max of GS_MAX_TX_URBS frames per channel at a time. */
151 #define GS_MAX_TX_URBS 10
152 /* Only launch a max of GS_MAX_RX_URBS usb requests at a time. */
153 #define GS_MAX_RX_URBS 30
154 /* Maximum number of interfaces the driver supports per device.
155 * Current hardware only supports 2 interfaces. The future may vary.
157 #define GS_MAX_INTF 2
159 struct gs_tx_context {
161 unsigned int echo_id;
165 struct can_priv can; /* must be the first member */
167 struct gs_usb *parent;
169 struct net_device *netdev;
170 struct usb_device *udev;
171 struct usb_interface *iface;
173 struct can_bittiming_const bt_const;
174 unsigned int channel; /* channel number */
176 /* This lock prevents a race condition between xmit and receive. */
177 spinlock_t tx_ctx_lock;
178 struct gs_tx_context tx_context[GS_MAX_TX_URBS];
180 struct usb_anchor tx_submitted;
181 atomic_t active_tx_urbs;
184 /* usb interface struct */
186 struct gs_can *canch[GS_MAX_INTF];
187 struct usb_anchor rx_submitted;
188 atomic_t active_channels;
189 struct usb_device *udev;
192 /* 'allocate' a tx context.
193 * returns a valid tx context or NULL if there is no space.
195 static struct gs_tx_context *gs_alloc_tx_context(struct gs_can *dev)
200 spin_lock_irqsave(&dev->tx_ctx_lock, flags);
202 for (; i < GS_MAX_TX_URBS; i++) {
203 if (dev->tx_context[i].echo_id == GS_MAX_TX_URBS) {
204 dev->tx_context[i].echo_id = i;
205 spin_unlock_irqrestore(&dev->tx_ctx_lock, flags);
206 return &dev->tx_context[i];
210 spin_unlock_irqrestore(&dev->tx_ctx_lock, flags);
214 /* releases a tx context
216 static void gs_free_tx_context(struct gs_tx_context *txc)
218 txc->echo_id = GS_MAX_TX_URBS;
221 /* Get a tx context by id.
223 static struct gs_tx_context *gs_get_tx_context(struct gs_can *dev,
228 if (id < GS_MAX_TX_URBS) {
229 spin_lock_irqsave(&dev->tx_ctx_lock, flags);
230 if (dev->tx_context[id].echo_id == id) {
231 spin_unlock_irqrestore(&dev->tx_ctx_lock, flags);
232 return &dev->tx_context[id];
234 spin_unlock_irqrestore(&dev->tx_ctx_lock, flags);
239 static int gs_cmd_reset(struct gs_can *gsdev)
241 struct gs_device_mode *dm;
242 struct usb_interface *intf = gsdev->iface;
245 dm = kzalloc(sizeof(*dm), GFP_KERNEL);
249 dm->mode = GS_CAN_MODE_RESET;
251 rc = usb_control_msg(interface_to_usbdev(intf),
252 usb_sndctrlpipe(interface_to_usbdev(intf), 0),
254 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
266 static void gs_update_state(struct gs_can *dev, struct can_frame *cf)
268 struct can_device_stats *can_stats = &dev->can.can_stats;
270 if (cf->can_id & CAN_ERR_RESTARTED) {
271 dev->can.state = CAN_STATE_ERROR_ACTIVE;
272 can_stats->restarts++;
273 } else if (cf->can_id & CAN_ERR_BUSOFF) {
274 dev->can.state = CAN_STATE_BUS_OFF;
275 can_stats->bus_off++;
276 } else if (cf->can_id & CAN_ERR_CRTL) {
277 if ((cf->data[1] & CAN_ERR_CRTL_TX_WARNING) ||
278 (cf->data[1] & CAN_ERR_CRTL_RX_WARNING)) {
279 dev->can.state = CAN_STATE_ERROR_WARNING;
280 can_stats->error_warning++;
281 } else if ((cf->data[1] & CAN_ERR_CRTL_TX_PASSIVE) ||
282 (cf->data[1] & CAN_ERR_CRTL_RX_PASSIVE)) {
283 dev->can.state = CAN_STATE_ERROR_PASSIVE;
284 can_stats->error_passive++;
286 dev->can.state = CAN_STATE_ERROR_ACTIVE;
291 static void gs_usb_receive_bulk_callback(struct urb *urb)
293 struct gs_usb *usbcan = urb->context;
295 struct net_device *netdev;
297 struct net_device_stats *stats;
298 struct gs_host_frame *hf = urb->transfer_buffer;
299 struct gs_tx_context *txc;
300 struct can_frame *cf;
305 switch (urb->status) {
306 case 0: /* success */
312 /* do not resubmit aborted urbs. eg: when device goes down */
316 /* device reports out of range channel id */
317 if (hf->channel >= GS_MAX_INTF)
320 dev = usbcan->canch[hf->channel];
322 netdev = dev->netdev;
323 stats = &netdev->stats;
325 if (!netif_device_present(netdev))
328 if (hf->echo_id == -1) { /* normal rx */
329 skb = alloc_can_skb(dev->netdev, &cf);
333 cf->can_id = hf->can_id;
335 can_frame_set_cc_len(cf, hf->can_dlc, dev->can.ctrlmode);
336 memcpy(cf->data, hf->data, 8);
338 /* ERROR frames tell us information about the controller */
339 if (hf->can_id & CAN_ERR_FLAG)
340 gs_update_state(dev, cf);
342 netdev->stats.rx_packets++;
343 netdev->stats.rx_bytes += hf->can_dlc;
346 } else { /* echo_id == hf->echo_id */
347 if (hf->echo_id >= GS_MAX_TX_URBS) {
349 "Unexpected out of range echo id %d\n",
354 netdev->stats.tx_packets++;
355 netdev->stats.tx_bytes += hf->can_dlc;
357 txc = gs_get_tx_context(dev, hf->echo_id);
359 /* bad devices send bad echo_ids. */
362 "Unexpected unused echo id %d\n",
367 can_get_echo_skb(netdev, hf->echo_id);
369 gs_free_tx_context(txc);
371 atomic_dec(&dev->active_tx_urbs);
373 netif_wake_queue(netdev);
376 if (hf->flags & GS_CAN_FLAG_OVERFLOW) {
377 skb = alloc_can_err_skb(netdev, &cf);
381 cf->can_id |= CAN_ERR_CRTL;
382 cf->len = CAN_ERR_DLC;
383 cf->data[1] = CAN_ERR_CRTL_RX_OVERFLOW;
384 stats->rx_over_errors++;
390 usb_fill_bulk_urb(urb,
392 usb_rcvbulkpipe(usbcan->udev, GSUSB_ENDPOINT_IN),
394 sizeof(struct gs_host_frame),
395 gs_usb_receive_bulk_callback,
399 rc = usb_submit_urb(urb, GFP_ATOMIC);
401 /* USB failure take down all interfaces */
403 for (rc = 0; rc < GS_MAX_INTF; rc++) {
404 if (usbcan->canch[rc])
405 netif_device_detach(usbcan->canch[rc]->netdev);
410 static int gs_usb_set_bittiming(struct net_device *netdev)
412 struct gs_can *dev = netdev_priv(netdev);
413 struct can_bittiming *bt = &dev->can.bittiming;
414 struct usb_interface *intf = dev->iface;
416 struct gs_device_bittiming *dbt;
418 dbt = kmalloc(sizeof(*dbt), GFP_KERNEL);
422 dbt->prop_seg = bt->prop_seg;
423 dbt->phase_seg1 = bt->phase_seg1;
424 dbt->phase_seg2 = bt->phase_seg2;
428 /* request bit timings */
429 rc = usb_control_msg(interface_to_usbdev(intf),
430 usb_sndctrlpipe(interface_to_usbdev(intf), 0),
431 GS_USB_BREQ_BITTIMING,
432 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
442 dev_err(netdev->dev.parent, "Couldn't set bittimings (err=%d)",
445 return (rc > 0) ? 0 : rc;
448 static void gs_usb_xmit_callback(struct urb *urb)
450 struct gs_tx_context *txc = urb->context;
451 struct gs_can *dev = txc->dev;
452 struct net_device *netdev = dev->netdev;
455 netdev_info(netdev, "usb xmit fail %d\n", txc->echo_id);
457 usb_free_coherent(urb->dev,
458 urb->transfer_buffer_length,
459 urb->transfer_buffer,
463 static netdev_tx_t gs_can_start_xmit(struct sk_buff *skb,
464 struct net_device *netdev)
466 struct gs_can *dev = netdev_priv(netdev);
467 struct net_device_stats *stats = &dev->netdev->stats;
469 struct gs_host_frame *hf;
470 struct can_frame *cf;
473 struct gs_tx_context *txc;
475 if (can_dropped_invalid_skb(netdev, skb))
478 /* find an empty context to keep track of transmission */
479 txc = gs_alloc_tx_context(dev);
481 return NETDEV_TX_BUSY;
483 /* create a URB, and a buffer for it */
484 urb = usb_alloc_urb(0, GFP_ATOMIC);
488 hf = usb_alloc_coherent(dev->udev, sizeof(*hf), GFP_ATOMIC,
491 netdev_err(netdev, "No memory left for USB buffer\n");
497 if (idx >= GS_MAX_TX_URBS) {
498 netdev_err(netdev, "Invalid tx context %d\n", idx);
503 hf->channel = dev->channel;
505 cf = (struct can_frame *)skb->data;
507 hf->can_id = cf->can_id;
508 hf->can_dlc = can_get_cc_dlc(cf, dev->can.ctrlmode);
510 memcpy(hf->data, cf->data, cf->len);
512 usb_fill_bulk_urb(urb, dev->udev,
513 usb_sndbulkpipe(dev->udev, GSUSB_ENDPOINT_OUT),
516 gs_usb_xmit_callback,
519 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
520 usb_anchor_urb(urb, &dev->tx_submitted);
522 can_put_echo_skb(skb, netdev, idx);
524 atomic_inc(&dev->active_tx_urbs);
526 rc = usb_submit_urb(urb, GFP_ATOMIC);
527 if (unlikely(rc)) { /* usb send failed */
528 atomic_dec(&dev->active_tx_urbs);
530 can_free_echo_skb(netdev, idx);
531 gs_free_tx_context(txc);
533 usb_unanchor_urb(urb);
534 usb_free_coherent(dev->udev,
540 netif_device_detach(netdev);
542 netdev_err(netdev, "usb_submit failed (err=%d)\n", rc);
546 /* Slow down tx path */
547 if (atomic_read(&dev->active_tx_urbs) >= GS_MAX_TX_URBS)
548 netif_stop_queue(netdev);
551 /* let usb core take care of this urb */
557 usb_free_coherent(dev->udev,
565 gs_free_tx_context(txc);
571 static int gs_can_open(struct net_device *netdev)
573 struct gs_can *dev = netdev_priv(netdev);
574 struct gs_usb *parent = dev->parent;
576 struct gs_device_mode *dm;
579 rc = open_candev(netdev);
583 if (atomic_add_return(1, &parent->active_channels) == 1) {
584 for (i = 0; i < GS_MAX_RX_URBS; i++) {
589 urb = usb_alloc_urb(0, GFP_KERNEL);
593 /* alloc rx buffer */
594 buf = usb_alloc_coherent(dev->udev,
595 sizeof(struct gs_host_frame),
600 "No memory left for USB buffer\n");
605 /* fill, anchor, and submit rx urb */
606 usb_fill_bulk_urb(urb,
608 usb_rcvbulkpipe(dev->udev,
611 sizeof(struct gs_host_frame),
612 gs_usb_receive_bulk_callback,
614 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
616 usb_anchor_urb(urb, &parent->rx_submitted);
618 rc = usb_submit_urb(urb, GFP_KERNEL);
621 netif_device_detach(dev->netdev);
624 "usb_submit failed (err=%d)\n",
627 usb_unanchor_urb(urb);
633 * USB core will take care of freeing it
639 dm = kmalloc(sizeof(*dm), GFP_KERNEL);
644 ctrlmode = dev->can.ctrlmode;
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;
652 /* Controller is not allowed to retry TX
653 * this mode is unavailable on atmels uc3c hardware
655 if (ctrlmode & CAN_CTRLMODE_ONE_SHOT)
656 dm->flags |= GS_CAN_MODE_ONE_SHOT;
658 if (ctrlmode & CAN_CTRLMODE_3_SAMPLES)
659 dm->flags |= GS_CAN_MODE_TRIPLE_SAMPLE;
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),
666 USB_DIR_OUT | USB_TYPE_VENDOR |
675 netdev_err(netdev, "Couldn't start device (err=%d)\n", rc);
682 dev->can.state = CAN_STATE_ERROR_ACTIVE;
684 if (!(dev->can.ctrlmode & CAN_CTRLMODE_LISTENONLY))
685 netif_start_queue(netdev);
690 static int gs_can_close(struct net_device *netdev)
693 struct gs_can *dev = netdev_priv(netdev);
694 struct gs_usb *parent = dev->parent;
696 netif_stop_queue(netdev);
699 if (atomic_dec_and_test(&parent->active_channels))
700 usb_kill_anchored_urbs(&parent->rx_submitted);
702 /* Stop sending URBs */
703 usb_kill_anchored_urbs(&dev->tx_submitted);
704 atomic_set(&dev->active_tx_urbs, 0);
706 /* reset the device */
707 rc = gs_cmd_reset(dev);
709 netdev_warn(netdev, "Couldn't shutdown device (err=%d)", rc);
711 /* reset tx contexts */
712 for (rc = 0; rc < GS_MAX_TX_URBS; rc++) {
713 dev->tx_context[rc].dev = dev;
714 dev->tx_context[rc].echo_id = GS_MAX_TX_URBS;
717 /* close the netdev */
718 close_candev(netdev);
723 static const struct net_device_ops gs_usb_netdev_ops = {
724 .ndo_open = gs_can_open,
725 .ndo_stop = gs_can_close,
726 .ndo_start_xmit = gs_can_start_xmit,
727 .ndo_change_mtu = can_change_mtu,
730 static int gs_usb_set_identify(struct net_device *netdev, bool do_identify)
732 struct gs_can *dev = netdev_priv(netdev);
733 struct gs_identify_mode *imode;
736 imode = kmalloc(sizeof(*imode), GFP_KERNEL);
742 imode->mode = GS_CAN_IDENTIFY_ON;
744 imode->mode = GS_CAN_IDENTIFY_OFF;
746 rc = usb_control_msg(interface_to_usbdev(dev->iface),
747 usb_sndctrlpipe(interface_to_usbdev(dev->iface),
749 GS_USB_BREQ_IDENTIFY,
750 USB_DIR_OUT | USB_TYPE_VENDOR |
760 return (rc > 0) ? 0 : rc;
763 /* blink LED's for finding the this interface */
764 static int gs_usb_set_phys_id(struct net_device *dev,
765 enum ethtool_phys_id_state state)
770 case ETHTOOL_ID_ACTIVE:
771 rc = gs_usb_set_identify(dev, GS_CAN_IDENTIFY_ON);
773 case ETHTOOL_ID_INACTIVE:
774 rc = gs_usb_set_identify(dev, GS_CAN_IDENTIFY_OFF);
783 static const struct ethtool_ops gs_usb_ethtool_ops = {
784 .set_phys_id = gs_usb_set_phys_id,
787 static struct gs_can *gs_make_candev(unsigned int channel,
788 struct usb_interface *intf,
789 struct gs_device_config *dconf)
792 struct net_device *netdev;
794 struct gs_device_bt_const *bt_const;
796 bt_const = kmalloc(sizeof(*bt_const), GFP_KERNEL);
798 return ERR_PTR(-ENOMEM);
800 /* fetch bit timing constants */
801 rc = usb_control_msg(interface_to_usbdev(intf),
802 usb_rcvctrlpipe(interface_to_usbdev(intf), 0),
803 GS_USB_BREQ_BT_CONST,
804 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
813 "Couldn't get bit timing const for channel (err=%d)\n",
820 netdev = alloc_candev(sizeof(struct gs_can), GS_MAX_TX_URBS);
822 dev_err(&intf->dev, "Couldn't allocate candev\n");
824 return ERR_PTR(-ENOMEM);
827 dev = netdev_priv(netdev);
829 netdev->netdev_ops = &gs_usb_netdev_ops;
831 netdev->flags |= IFF_ECHO; /* we support full roundtrip echo */
834 strcpy(dev->bt_const.name, "gs_usb");
835 dev->bt_const.tseg1_min = bt_const->tseg1_min;
836 dev->bt_const.tseg1_max = bt_const->tseg1_max;
837 dev->bt_const.tseg2_min = bt_const->tseg2_min;
838 dev->bt_const.tseg2_max = bt_const->tseg2_max;
839 dev->bt_const.sjw_max = bt_const->sjw_max;
840 dev->bt_const.brp_min = bt_const->brp_min;
841 dev->bt_const.brp_max = bt_const->brp_max;
842 dev->bt_const.brp_inc = bt_const->brp_inc;
844 dev->udev = interface_to_usbdev(intf);
846 dev->netdev = netdev;
847 dev->channel = channel;
849 init_usb_anchor(&dev->tx_submitted);
850 atomic_set(&dev->active_tx_urbs, 0);
851 spin_lock_init(&dev->tx_ctx_lock);
852 for (rc = 0; rc < GS_MAX_TX_URBS; rc++) {
853 dev->tx_context[rc].dev = dev;
854 dev->tx_context[rc].echo_id = GS_MAX_TX_URBS;
858 dev->can.state = CAN_STATE_STOPPED;
859 dev->can.clock.freq = bt_const->fclk_can;
860 dev->can.bittiming_const = &dev->bt_const;
861 dev->can.do_set_bittiming = gs_usb_set_bittiming;
863 dev->can.ctrlmode_supported = CAN_CTRLMODE_CC_LEN8_DLC;
865 if (bt_const->feature & GS_CAN_FEATURE_LISTEN_ONLY)
866 dev->can.ctrlmode_supported |= CAN_CTRLMODE_LISTENONLY;
868 if (bt_const->feature & GS_CAN_FEATURE_LOOP_BACK)
869 dev->can.ctrlmode_supported |= CAN_CTRLMODE_LOOPBACK;
871 if (bt_const->feature & GS_CAN_FEATURE_TRIPLE_SAMPLE)
872 dev->can.ctrlmode_supported |= CAN_CTRLMODE_3_SAMPLES;
874 if (bt_const->feature & GS_CAN_FEATURE_ONE_SHOT)
875 dev->can.ctrlmode_supported |= CAN_CTRLMODE_ONE_SHOT;
877 SET_NETDEV_DEV(netdev, &intf->dev);
879 if (dconf->sw_version > 1)
880 if (bt_const->feature & GS_CAN_FEATURE_IDENTIFY)
881 netdev->ethtool_ops = &gs_usb_ethtool_ops;
885 rc = register_candev(dev->netdev);
887 free_candev(dev->netdev);
888 dev_err(&intf->dev, "Couldn't register candev (err=%d)\n", rc);
895 static void gs_destroy_candev(struct gs_can *dev)
897 unregister_candev(dev->netdev);
898 usb_kill_anchored_urbs(&dev->tx_submitted);
899 free_candev(dev->netdev);
902 static int gs_usb_probe(struct usb_interface *intf,
903 const struct usb_device_id *id)
907 unsigned int icount, i;
908 struct gs_host_config *hconf;
909 struct gs_device_config *dconf;
911 hconf = kmalloc(sizeof(*hconf), GFP_KERNEL);
915 hconf->byte_order = 0x0000beef;
917 /* send host config */
918 rc = usb_control_msg(interface_to_usbdev(intf),
919 usb_sndctrlpipe(interface_to_usbdev(intf), 0),
920 GS_USB_BREQ_HOST_FORMAT,
921 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
923 intf->cur_altsetting->desc.bInterfaceNumber,
931 dev_err(&intf->dev, "Couldn't send data format (err=%d)\n",
936 dconf = kmalloc(sizeof(*dconf), GFP_KERNEL);
940 /* read device config */
941 rc = usb_control_msg(interface_to_usbdev(intf),
942 usb_rcvctrlpipe(interface_to_usbdev(intf), 0),
943 GS_USB_BREQ_DEVICE_CONFIG,
944 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
946 intf->cur_altsetting->desc.bInterfaceNumber,
951 dev_err(&intf->dev, "Couldn't get device config: (err=%d)\n",
957 icount = dconf->icount + 1;
958 dev_info(&intf->dev, "Configuring for %d interfaces\n", icount);
960 if (icount > GS_MAX_INTF) {
962 "Driver cannot handle more that %d CAN interfaces\n",
968 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
974 init_usb_anchor(&dev->rx_submitted);
976 atomic_set(&dev->active_channels, 0);
978 usb_set_intfdata(intf, dev);
979 dev->udev = interface_to_usbdev(intf);
981 for (i = 0; i < icount; i++) {
982 dev->canch[i] = gs_make_candev(i, intf, dconf);
983 if (IS_ERR_OR_NULL(dev->canch[i])) {
984 /* save error code to return later */
985 rc = PTR_ERR(dev->canch[i]);
987 /* on failure destroy previously created candevs */
989 for (i = 0; i < icount; i++)
990 gs_destroy_candev(dev->canch[i]);
992 usb_kill_anchored_urbs(&dev->rx_submitted);
997 dev->canch[i]->parent = dev;
1005 static void gs_usb_disconnect(struct usb_interface *intf)
1008 struct gs_usb *dev = usb_get_intfdata(intf);
1009 usb_set_intfdata(intf, NULL);
1012 dev_err(&intf->dev, "Disconnect (nodata)\n");
1016 for (i = 0; i < GS_MAX_INTF; i++)
1018 gs_destroy_candev(dev->canch[i]);
1020 usb_kill_anchored_urbs(&dev->rx_submitted);
1024 static const struct usb_device_id gs_usb_table[] = {
1025 { USB_DEVICE_INTERFACE_NUMBER(USB_GSUSB_1_VENDOR_ID,
1026 USB_GSUSB_1_PRODUCT_ID, 0) },
1027 { USB_DEVICE_INTERFACE_NUMBER(USB_CANDLELIGHT_VENDOR_ID,
1028 USB_CANDLELIGHT_PRODUCT_ID, 0) },
1029 {} /* Terminating entry */
1032 MODULE_DEVICE_TABLE(usb, gs_usb_table);
1034 static struct usb_driver gs_usb_driver = {
1036 .probe = gs_usb_probe,
1037 .disconnect = gs_usb_disconnect,
1038 .id_table = gs_usb_table,
1041 module_usb_driver(gs_usb_driver);
1043 MODULE_AUTHOR("Maximilian Schneider <mws@schneidersoft.net>");
1045 "Socket CAN device driver for Geschwister Schneider Technologie-, "
1046 "Entwicklungs- und Vertriebs UG. USB2.0 to CAN interfaces\n"
1047 "and bytewerk.org candleLight USB CAN interfaces.");
1048 MODULE_LICENSE("GPL v2");