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/init.h>
13 #include <linux/signal.h>
14 #include <linux/module.h>
15 #include <linux/netdevice.h>
16 #include <linux/usb.h>
18 #include <linux/can.h>
19 #include <linux/can/dev.h>
20 #include <linux/can/error.h>
22 /* Device specific constants */
23 #define USB_GSUSB_1_VENDOR_ID 0x1d50
24 #define USB_GSUSB_1_PRODUCT_ID 0x606f
26 #define USB_CANDLELIGHT_VENDOR_ID 0x1209
27 #define USB_CANDLELIGHT_PRODUCT_ID 0x2323
29 #define GSUSB_ENDPOINT_IN 1
30 #define GSUSB_ENDPOINT_OUT 2
32 /* Device specific constants */
34 GS_USB_BREQ_HOST_FORMAT = 0,
35 GS_USB_BREQ_BITTIMING,
39 GS_USB_BREQ_DEVICE_CONFIG,
40 GS_USB_BREQ_TIMESTAMP,
45 /* reset a channel. turns it off */
46 GS_CAN_MODE_RESET = 0,
47 /* starts a channel */
52 GS_CAN_STATE_ERROR_ACTIVE = 0,
53 GS_CAN_STATE_ERROR_WARNING,
54 GS_CAN_STATE_ERROR_PASSIVE,
60 enum gs_can_identify_mode {
61 GS_CAN_IDENTIFY_OFF = 0,
65 /* data types passed between host and device */
67 /* The firmware on the original USB2CAN by Geschwister Schneider
68 * Technologie Entwicklungs- und Vertriebs UG exchanges all data
69 * between the host and the device in host byte order. This is done
70 * with the struct gs_host_config::byte_order member, which is sent
71 * first to indicate the desired byte order.
73 * The widely used open source firmware candleLight doesn't support
74 * this feature and exchanges the data in little endian byte order.
76 struct gs_host_config {
80 struct gs_device_config {
89 #define GS_CAN_MODE_NORMAL 0
90 #define GS_CAN_MODE_LISTEN_ONLY BIT(0)
91 #define GS_CAN_MODE_LOOP_BACK BIT(1)
92 #define GS_CAN_MODE_TRIPLE_SAMPLE BIT(2)
93 #define GS_CAN_MODE_ONE_SHOT BIT(3)
95 struct gs_device_mode {
100 struct gs_device_state {
106 struct gs_device_bittiming {
114 struct gs_identify_mode {
118 #define GS_CAN_FEATURE_LISTEN_ONLY BIT(0)
119 #define GS_CAN_FEATURE_LOOP_BACK BIT(1)
120 #define GS_CAN_FEATURE_TRIPLE_SAMPLE BIT(2)
121 #define GS_CAN_FEATURE_ONE_SHOT BIT(3)
122 #define GS_CAN_FEATURE_HW_TIMESTAMP BIT(4)
123 #define GS_CAN_FEATURE_IDENTIFY BIT(5)
125 struct gs_device_bt_const {
138 #define GS_CAN_FLAG_OVERFLOW 1
140 struct gs_host_frame {
151 /* The GS USB devices make use of the same flags and masks as in
152 * linux/can.h and linux/can/error.h, and no additional mapping is necessary.
155 /* Only send a max of GS_MAX_TX_URBS frames per channel at a time. */
156 #define GS_MAX_TX_URBS 10
157 /* Only launch a max of GS_MAX_RX_URBS usb requests at a time. */
158 #define GS_MAX_RX_URBS 30
159 /* Maximum number of interfaces the driver supports per device.
160 * Current hardware only supports 2 interfaces. The future may vary.
162 #define GS_MAX_INTF 2
164 struct gs_tx_context {
166 unsigned int echo_id;
170 struct can_priv can; /* must be the first member */
172 struct gs_usb *parent;
174 struct net_device *netdev;
175 struct usb_device *udev;
176 struct usb_interface *iface;
178 struct can_bittiming_const bt_const;
179 unsigned int channel; /* channel number */
181 /* This lock prevents a race condition between xmit and receive. */
182 spinlock_t tx_ctx_lock;
183 struct gs_tx_context tx_context[GS_MAX_TX_URBS];
185 struct usb_anchor tx_submitted;
186 atomic_t active_tx_urbs;
187 void *rxbuf[GS_MAX_RX_URBS];
188 dma_addr_t rxbuf_dma[GS_MAX_RX_URBS];
191 /* usb interface struct */
193 struct gs_can *canch[GS_MAX_INTF];
194 struct usb_anchor rx_submitted;
195 struct usb_device *udev;
199 /* 'allocate' a tx context.
200 * returns a valid tx context or NULL if there is no space.
202 static struct gs_tx_context *gs_alloc_tx_context(struct gs_can *dev)
207 spin_lock_irqsave(&dev->tx_ctx_lock, flags);
209 for (; i < GS_MAX_TX_URBS; i++) {
210 if (dev->tx_context[i].echo_id == GS_MAX_TX_URBS) {
211 dev->tx_context[i].echo_id = i;
212 spin_unlock_irqrestore(&dev->tx_ctx_lock, flags);
213 return &dev->tx_context[i];
217 spin_unlock_irqrestore(&dev->tx_ctx_lock, flags);
221 /* releases a tx context
223 static void gs_free_tx_context(struct gs_tx_context *txc)
225 txc->echo_id = GS_MAX_TX_URBS;
228 /* Get a tx context by id.
230 static struct gs_tx_context *gs_get_tx_context(struct gs_can *dev,
235 if (id < GS_MAX_TX_URBS) {
236 spin_lock_irqsave(&dev->tx_ctx_lock, flags);
237 if (dev->tx_context[id].echo_id == id) {
238 spin_unlock_irqrestore(&dev->tx_ctx_lock, flags);
239 return &dev->tx_context[id];
241 spin_unlock_irqrestore(&dev->tx_ctx_lock, flags);
246 static int gs_cmd_reset(struct gs_can *gsdev)
248 struct gs_device_mode *dm;
249 struct usb_interface *intf = gsdev->iface;
252 dm = kzalloc(sizeof(*dm), GFP_KERNEL);
256 dm->mode = GS_CAN_MODE_RESET;
258 rc = usb_control_msg(interface_to_usbdev(intf),
259 usb_sndctrlpipe(interface_to_usbdev(intf), 0),
261 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
273 static void gs_update_state(struct gs_can *dev, struct can_frame *cf)
275 struct can_device_stats *can_stats = &dev->can.can_stats;
277 if (cf->can_id & CAN_ERR_RESTARTED) {
278 dev->can.state = CAN_STATE_ERROR_ACTIVE;
279 can_stats->restarts++;
280 } else if (cf->can_id & CAN_ERR_BUSOFF) {
281 dev->can.state = CAN_STATE_BUS_OFF;
282 can_stats->bus_off++;
283 } else if (cf->can_id & CAN_ERR_CRTL) {
284 if ((cf->data[1] & CAN_ERR_CRTL_TX_WARNING) ||
285 (cf->data[1] & CAN_ERR_CRTL_RX_WARNING)) {
286 dev->can.state = CAN_STATE_ERROR_WARNING;
287 can_stats->error_warning++;
288 } else if ((cf->data[1] & CAN_ERR_CRTL_TX_PASSIVE) ||
289 (cf->data[1] & CAN_ERR_CRTL_RX_PASSIVE)) {
290 dev->can.state = CAN_STATE_ERROR_PASSIVE;
291 can_stats->error_passive++;
293 dev->can.state = CAN_STATE_ERROR_ACTIVE;
298 static void gs_usb_receive_bulk_callback(struct urb *urb)
300 struct gs_usb *usbcan = urb->context;
302 struct net_device *netdev;
304 struct net_device_stats *stats;
305 struct gs_host_frame *hf = urb->transfer_buffer;
306 struct gs_tx_context *txc;
307 struct can_frame *cf;
312 switch (urb->status) {
313 case 0: /* success */
319 /* do not resubmit aborted urbs. eg: when device goes down */
323 /* device reports out of range channel id */
324 if (hf->channel >= GS_MAX_INTF)
327 dev = usbcan->canch[hf->channel];
329 netdev = dev->netdev;
330 stats = &netdev->stats;
332 if (!netif_device_present(netdev))
335 if (hf->echo_id == -1) { /* normal rx */
336 skb = alloc_can_skb(dev->netdev, &cf);
340 cf->can_id = le32_to_cpu(hf->can_id);
342 cf->can_dlc = get_can_dlc(hf->can_dlc);
343 memcpy(cf->data, hf->data, 8);
345 /* ERROR frames tell us information about the controller */
346 if (le32_to_cpu(hf->can_id) & CAN_ERR_FLAG)
347 gs_update_state(dev, cf);
349 netdev->stats.rx_packets++;
350 netdev->stats.rx_bytes += hf->can_dlc;
353 } else { /* echo_id == hf->echo_id */
354 if (hf->echo_id >= GS_MAX_TX_URBS) {
356 "Unexpected out of range echo id %d\n",
361 netdev->stats.tx_packets++;
362 netdev->stats.tx_bytes += hf->can_dlc;
364 txc = gs_get_tx_context(dev, hf->echo_id);
366 /* bad devices send bad echo_ids. */
369 "Unexpected unused echo id %d\n",
374 can_get_echo_skb(netdev, hf->echo_id);
376 gs_free_tx_context(txc);
378 atomic_dec(&dev->active_tx_urbs);
380 netif_wake_queue(netdev);
383 if (hf->flags & GS_CAN_FLAG_OVERFLOW) {
384 skb = alloc_can_err_skb(netdev, &cf);
388 cf->can_id |= CAN_ERR_CRTL;
389 cf->can_dlc = CAN_ERR_DLC;
390 cf->data[1] = CAN_ERR_CRTL_RX_OVERFLOW;
391 stats->rx_over_errors++;
397 usb_fill_bulk_urb(urb,
399 usb_rcvbulkpipe(usbcan->udev, GSUSB_ENDPOINT_IN),
401 sizeof(struct gs_host_frame),
402 gs_usb_receive_bulk_callback,
406 rc = usb_submit_urb(urb, GFP_ATOMIC);
408 /* USB failure take down all interfaces */
411 for (rc = 0; rc < GS_MAX_INTF; rc++) {
412 if (usbcan->canch[rc])
413 netif_device_detach(usbcan->canch[rc]->netdev);
418 static int gs_usb_set_bittiming(struct net_device *netdev)
420 struct gs_can *dev = netdev_priv(netdev);
421 struct can_bittiming *bt = &dev->can.bittiming;
422 struct usb_interface *intf = dev->iface;
424 struct gs_device_bittiming *dbt;
426 dbt = kmalloc(sizeof(*dbt), GFP_KERNEL);
430 dbt->prop_seg = cpu_to_le32(bt->prop_seg);
431 dbt->phase_seg1 = cpu_to_le32(bt->phase_seg1);
432 dbt->phase_seg2 = cpu_to_le32(bt->phase_seg2);
433 dbt->sjw = cpu_to_le32(bt->sjw);
434 dbt->brp = cpu_to_le32(bt->brp);
436 /* request bit timings */
437 rc = usb_control_msg(interface_to_usbdev(intf),
438 usb_sndctrlpipe(interface_to_usbdev(intf), 0),
439 GS_USB_BREQ_BITTIMING,
440 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
450 dev_err(netdev->dev.parent, "Couldn't set bittimings (err=%d)",
453 return (rc > 0) ? 0 : rc;
456 static void gs_usb_xmit_callback(struct urb *urb)
458 struct gs_tx_context *txc = urb->context;
459 struct gs_can *dev = txc->dev;
460 struct net_device *netdev = dev->netdev;
463 netdev_info(netdev, "usb xmit fail %d\n", txc->echo_id);
465 usb_free_coherent(urb->dev,
466 urb->transfer_buffer_length,
467 urb->transfer_buffer,
471 static netdev_tx_t gs_can_start_xmit(struct sk_buff *skb,
472 struct net_device *netdev)
474 struct gs_can *dev = netdev_priv(netdev);
475 struct net_device_stats *stats = &dev->netdev->stats;
477 struct gs_host_frame *hf;
478 struct can_frame *cf;
481 struct gs_tx_context *txc;
483 if (can_dropped_invalid_skb(netdev, skb))
486 /* find an empty context to keep track of transmission */
487 txc = gs_alloc_tx_context(dev);
489 return NETDEV_TX_BUSY;
491 /* create a URB, and a buffer for it */
492 urb = usb_alloc_urb(0, GFP_ATOMIC);
496 hf = usb_alloc_coherent(dev->udev, sizeof(*hf), GFP_ATOMIC,
499 netdev_err(netdev, "No memory left for USB buffer\n");
505 if (idx >= GS_MAX_TX_URBS) {
506 netdev_err(netdev, "Invalid tx context %d\n", idx);
511 hf->channel = dev->channel;
515 cf = (struct can_frame *)skb->data;
517 hf->can_id = cpu_to_le32(cf->can_id);
518 hf->can_dlc = cf->can_dlc;
519 memcpy(hf->data, cf->data, cf->can_dlc);
521 usb_fill_bulk_urb(urb, dev->udev,
522 usb_sndbulkpipe(dev->udev, GSUSB_ENDPOINT_OUT),
525 gs_usb_xmit_callback,
528 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
529 usb_anchor_urb(urb, &dev->tx_submitted);
531 can_put_echo_skb(skb, netdev, idx);
533 atomic_inc(&dev->active_tx_urbs);
535 rc = usb_submit_urb(urb, GFP_ATOMIC);
536 if (unlikely(rc)) { /* usb send failed */
537 atomic_dec(&dev->active_tx_urbs);
539 can_free_echo_skb(netdev, idx);
540 gs_free_tx_context(txc);
542 usb_unanchor_urb(urb);
543 usb_free_coherent(dev->udev,
549 netif_device_detach(netdev);
551 netdev_err(netdev, "usb_submit failed (err=%d)\n", rc);
555 /* Slow down tx path */
556 if (atomic_read(&dev->active_tx_urbs) >= GS_MAX_TX_URBS)
557 netif_stop_queue(netdev);
560 /* let usb core take care of this urb */
566 usb_free_coherent(dev->udev,
574 gs_free_tx_context(txc);
580 static int gs_can_open(struct net_device *netdev)
582 struct gs_can *dev = netdev_priv(netdev);
583 struct gs_usb *parent = dev->parent;
585 struct gs_device_mode *dm;
589 rc = open_candev(netdev);
593 if (!parent->active_channels) {
594 for (i = 0; i < GS_MAX_RX_URBS; i++) {
600 urb = usb_alloc_urb(0, GFP_KERNEL);
604 /* alloc rx buffer */
605 buf = usb_alloc_coherent(dev->udev,
606 sizeof(struct gs_host_frame),
611 "No memory left for USB buffer\n");
616 urb->transfer_dma = buf_dma;
618 /* fill, anchor, and submit rx urb */
619 usb_fill_bulk_urb(urb,
621 usb_rcvbulkpipe(dev->udev,
624 sizeof(struct gs_host_frame),
625 gs_usb_receive_bulk_callback,
627 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
629 usb_anchor_urb(urb, &parent->rx_submitted);
631 rc = usb_submit_urb(urb, GFP_KERNEL);
634 netif_device_detach(dev->netdev);
637 "usb_submit failed (err=%d)\n",
640 usb_unanchor_urb(urb);
641 usb_free_coherent(dev->udev,
642 sizeof(struct gs_host_frame),
650 dev->rxbuf_dma[i] = buf_dma;
653 * USB core will take care of freeing it
659 dm = kmalloc(sizeof(*dm), GFP_KERNEL);
664 ctrlmode = dev->can.ctrlmode;
666 if (ctrlmode & CAN_CTRLMODE_LOOPBACK)
667 flags |= GS_CAN_MODE_LOOP_BACK;
668 else if (ctrlmode & CAN_CTRLMODE_LISTENONLY)
669 flags |= GS_CAN_MODE_LISTEN_ONLY;
671 /* Controller is not allowed to retry TX
672 * this mode is unavailable on atmels uc3c hardware
674 if (ctrlmode & CAN_CTRLMODE_ONE_SHOT)
675 flags |= GS_CAN_MODE_ONE_SHOT;
677 if (ctrlmode & CAN_CTRLMODE_3_SAMPLES)
678 flags |= GS_CAN_MODE_TRIPLE_SAMPLE;
680 /* finally start device */
681 dev->can.state = CAN_STATE_ERROR_ACTIVE;
682 dm->mode = cpu_to_le32(GS_CAN_MODE_START);
683 dm->flags = cpu_to_le32(flags);
684 rc = usb_control_msg(interface_to_usbdev(dev->iface),
685 usb_sndctrlpipe(interface_to_usbdev(dev->iface), 0),
687 USB_DIR_OUT | USB_TYPE_VENDOR |
696 netdev_err(netdev, "Couldn't start device (err=%d)\n", rc);
698 dev->can.state = CAN_STATE_STOPPED;
704 parent->active_channels++;
705 if (!(dev->can.ctrlmode & CAN_CTRLMODE_LISTENONLY))
706 netif_start_queue(netdev);
711 static int gs_can_close(struct net_device *netdev)
714 struct gs_can *dev = netdev_priv(netdev);
715 struct gs_usb *parent = dev->parent;
718 netif_stop_queue(netdev);
721 parent->active_channels--;
722 if (!parent->active_channels) {
723 usb_kill_anchored_urbs(&parent->rx_submitted);
724 for (i = 0; i < GS_MAX_RX_URBS; i++)
725 usb_free_coherent(dev->udev,
726 sizeof(struct gs_host_frame),
731 /* Stop sending URBs */
732 usb_kill_anchored_urbs(&dev->tx_submitted);
733 atomic_set(&dev->active_tx_urbs, 0);
735 /* reset the device */
736 rc = gs_cmd_reset(dev);
738 netdev_warn(netdev, "Couldn't shutdown device (err=%d)", rc);
740 /* reset tx contexts */
741 for (rc = 0; rc < GS_MAX_TX_URBS; rc++) {
742 dev->tx_context[rc].dev = dev;
743 dev->tx_context[rc].echo_id = GS_MAX_TX_URBS;
746 /* close the netdev */
747 close_candev(netdev);
752 static const struct net_device_ops gs_usb_netdev_ops = {
753 .ndo_open = gs_can_open,
754 .ndo_stop = gs_can_close,
755 .ndo_start_xmit = gs_can_start_xmit,
756 .ndo_change_mtu = can_change_mtu,
759 static int gs_usb_set_identify(struct net_device *netdev, bool do_identify)
761 struct gs_can *dev = netdev_priv(netdev);
762 struct gs_identify_mode *imode;
765 imode = kmalloc(sizeof(*imode), GFP_KERNEL);
771 imode->mode = cpu_to_le32(GS_CAN_IDENTIFY_ON);
773 imode->mode = cpu_to_le32(GS_CAN_IDENTIFY_OFF);
775 rc = usb_control_msg(interface_to_usbdev(dev->iface),
776 usb_sndctrlpipe(interface_to_usbdev(dev->iface),
778 GS_USB_BREQ_IDENTIFY,
779 USB_DIR_OUT | USB_TYPE_VENDOR |
789 return (rc > 0) ? 0 : rc;
792 /* blink LED's for finding the this interface */
793 static int gs_usb_set_phys_id(struct net_device *dev,
794 enum ethtool_phys_id_state state)
799 case ETHTOOL_ID_ACTIVE:
800 rc = gs_usb_set_identify(dev, GS_CAN_IDENTIFY_ON);
802 case ETHTOOL_ID_INACTIVE:
803 rc = gs_usb_set_identify(dev, GS_CAN_IDENTIFY_OFF);
812 static const struct ethtool_ops gs_usb_ethtool_ops = {
813 .set_phys_id = gs_usb_set_phys_id,
816 static struct gs_can *gs_make_candev(unsigned int channel,
817 struct usb_interface *intf,
818 struct gs_device_config *dconf)
821 struct net_device *netdev;
823 struct gs_device_bt_const *bt_const;
826 bt_const = kmalloc(sizeof(*bt_const), GFP_KERNEL);
828 return ERR_PTR(-ENOMEM);
830 /* fetch bit timing constants */
831 rc = usb_control_msg(interface_to_usbdev(intf),
832 usb_rcvctrlpipe(interface_to_usbdev(intf), 0),
833 GS_USB_BREQ_BT_CONST,
834 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
843 "Couldn't get bit timing const for channel (err=%d)\n",
850 netdev = alloc_candev(sizeof(struct gs_can), GS_MAX_TX_URBS);
852 dev_err(&intf->dev, "Couldn't allocate candev\n");
854 return ERR_PTR(-ENOMEM);
857 dev = netdev_priv(netdev);
859 netdev->netdev_ops = &gs_usb_netdev_ops;
861 netdev->flags |= IFF_ECHO; /* we support full roundtrip echo */
864 strcpy(dev->bt_const.name, "gs_usb");
865 dev->bt_const.tseg1_min = le32_to_cpu(bt_const->tseg1_min);
866 dev->bt_const.tseg1_max = le32_to_cpu(bt_const->tseg1_max);
867 dev->bt_const.tseg2_min = le32_to_cpu(bt_const->tseg2_min);
868 dev->bt_const.tseg2_max = le32_to_cpu(bt_const->tseg2_max);
869 dev->bt_const.sjw_max = le32_to_cpu(bt_const->sjw_max);
870 dev->bt_const.brp_min = le32_to_cpu(bt_const->brp_min);
871 dev->bt_const.brp_max = le32_to_cpu(bt_const->brp_max);
872 dev->bt_const.brp_inc = le32_to_cpu(bt_const->brp_inc);
874 dev->udev = interface_to_usbdev(intf);
876 dev->netdev = netdev;
877 dev->channel = channel;
879 init_usb_anchor(&dev->tx_submitted);
880 atomic_set(&dev->active_tx_urbs, 0);
881 spin_lock_init(&dev->tx_ctx_lock);
882 for (rc = 0; rc < GS_MAX_TX_URBS; rc++) {
883 dev->tx_context[rc].dev = dev;
884 dev->tx_context[rc].echo_id = GS_MAX_TX_URBS;
888 dev->can.state = CAN_STATE_STOPPED;
889 dev->can.clock.freq = le32_to_cpu(bt_const->fclk_can);
890 dev->can.bittiming_const = &dev->bt_const;
891 dev->can.do_set_bittiming = gs_usb_set_bittiming;
893 dev->can.ctrlmode_supported = 0;
895 feature = le32_to_cpu(bt_const->feature);
896 if (feature & GS_CAN_FEATURE_LISTEN_ONLY)
897 dev->can.ctrlmode_supported |= CAN_CTRLMODE_LISTENONLY;
899 if (feature & GS_CAN_FEATURE_LOOP_BACK)
900 dev->can.ctrlmode_supported |= CAN_CTRLMODE_LOOPBACK;
902 if (feature & GS_CAN_FEATURE_TRIPLE_SAMPLE)
903 dev->can.ctrlmode_supported |= CAN_CTRLMODE_3_SAMPLES;
905 if (feature & GS_CAN_FEATURE_ONE_SHOT)
906 dev->can.ctrlmode_supported |= CAN_CTRLMODE_ONE_SHOT;
908 SET_NETDEV_DEV(netdev, &intf->dev);
910 if (le32_to_cpu(dconf->sw_version) > 1)
911 if (feature & GS_CAN_FEATURE_IDENTIFY)
912 netdev->ethtool_ops = &gs_usb_ethtool_ops;
916 rc = register_candev(dev->netdev);
918 free_candev(dev->netdev);
919 dev_err(&intf->dev, "Couldn't register candev (err=%d)\n", rc);
926 static void gs_destroy_candev(struct gs_can *dev)
928 unregister_candev(dev->netdev);
929 usb_kill_anchored_urbs(&dev->tx_submitted);
930 free_candev(dev->netdev);
933 static int gs_usb_probe(struct usb_interface *intf,
934 const struct usb_device_id *id)
938 unsigned int icount, i;
939 struct gs_host_config *hconf;
940 struct gs_device_config *dconf;
942 hconf = kmalloc(sizeof(*hconf), GFP_KERNEL);
946 hconf->byte_order = cpu_to_le32(0x0000beef);
948 /* send host config */
949 rc = usb_control_msg(interface_to_usbdev(intf),
950 usb_sndctrlpipe(interface_to_usbdev(intf), 0),
951 GS_USB_BREQ_HOST_FORMAT,
952 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
954 intf->cur_altsetting->desc.bInterfaceNumber,
962 dev_err(&intf->dev, "Couldn't send data format (err=%d)\n",
967 dconf = kmalloc(sizeof(*dconf), GFP_KERNEL);
971 /* read device config */
972 rc = usb_control_msg(interface_to_usbdev(intf),
973 usb_rcvctrlpipe(interface_to_usbdev(intf), 0),
974 GS_USB_BREQ_DEVICE_CONFIG,
975 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
977 intf->cur_altsetting->desc.bInterfaceNumber,
982 dev_err(&intf->dev, "Couldn't get device config: (err=%d)\n",
988 icount = dconf->icount + 1;
989 dev_info(&intf->dev, "Configuring for %d interfaces\n", icount);
991 if (icount > GS_MAX_INTF) {
993 "Driver cannot handle more that %d CAN interfaces\n",
999 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1005 init_usb_anchor(&dev->rx_submitted);
1007 usb_set_intfdata(intf, dev);
1008 dev->udev = interface_to_usbdev(intf);
1010 for (i = 0; i < icount; i++) {
1011 dev->canch[i] = gs_make_candev(i, intf, dconf);
1012 if (IS_ERR_OR_NULL(dev->canch[i])) {
1013 /* save error code to return later */
1014 rc = PTR_ERR(dev->canch[i]);
1016 /* on failure destroy previously created candevs */
1018 for (i = 0; i < icount; i++)
1019 gs_destroy_candev(dev->canch[i]);
1021 usb_kill_anchored_urbs(&dev->rx_submitted);
1026 dev->canch[i]->parent = dev;
1034 static void gs_usb_disconnect(struct usb_interface *intf)
1037 struct gs_usb *dev = usb_get_intfdata(intf);
1038 usb_set_intfdata(intf, NULL);
1041 dev_err(&intf->dev, "Disconnect (nodata)\n");
1045 for (i = 0; i < GS_MAX_INTF; i++)
1047 gs_destroy_candev(dev->canch[i]);
1049 usb_kill_anchored_urbs(&dev->rx_submitted);
1053 static const struct usb_device_id gs_usb_table[] = {
1054 { USB_DEVICE_INTERFACE_NUMBER(USB_GSUSB_1_VENDOR_ID,
1055 USB_GSUSB_1_PRODUCT_ID, 0) },
1056 { USB_DEVICE_INTERFACE_NUMBER(USB_CANDLELIGHT_VENDOR_ID,
1057 USB_CANDLELIGHT_PRODUCT_ID, 0) },
1058 {} /* Terminating entry */
1061 MODULE_DEVICE_TABLE(usb, gs_usb_table);
1063 static struct usb_driver gs_usb_driver = {
1065 .probe = gs_usb_probe,
1066 .disconnect = gs_usb_disconnect,
1067 .id_table = gs_usb_table,
1070 module_usb_driver(gs_usb_driver);
1072 MODULE_AUTHOR("Maximilian Schneider <mws@schneidersoft.net>");
1074 "Socket CAN device driver for Geschwister Schneider Technologie-, "
1075 "Entwicklungs- und Vertriebs UG. USB2.0 to CAN interfaces\n"
1076 "and bytewerk.org candleLight USB CAN interfaces.");
1077 MODULE_LICENSE("GPL v2");