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/bitfield.h>
13 #include <linux/ethtool.h>
14 #include <linux/init.h>
15 #include <linux/module.h>
16 #include <linux/netdevice.h>
17 #include <linux/signal.h>
18 #include <linux/usb.h>
20 #include <linux/can.h>
21 #include <linux/can/dev.h>
22 #include <linux/can/error.h>
24 /* Device specific constants */
25 #define USB_GSUSB_1_VENDOR_ID 0x1d50
26 #define USB_GSUSB_1_PRODUCT_ID 0x606f
28 #define USB_CANDLELIGHT_VENDOR_ID 0x1209
29 #define USB_CANDLELIGHT_PRODUCT_ID 0x2323
31 #define USB_CES_CANEXT_FD_VENDOR_ID 0x1cd2
32 #define USB_CES_CANEXT_FD_PRODUCT_ID 0x606f
34 #define USB_ABE_CANDEBUGGER_FD_VENDOR_ID 0x16d0
35 #define USB_ABE_CANDEBUGGER_FD_PRODUCT_ID 0x10b8
37 #define GSUSB_ENDPOINT_IN 1
38 #define GSUSB_ENDPOINT_OUT 2
40 /* Device specific constants */
42 GS_USB_BREQ_HOST_FORMAT = 0,
43 GS_USB_BREQ_BITTIMING,
47 GS_USB_BREQ_DEVICE_CONFIG,
48 GS_USB_BREQ_TIMESTAMP,
50 GS_USB_BREQ_GET_USER_ID,
51 GS_USB_BREQ_QUIRK_CANTACT_PRO_DATA_BITTIMING = GS_USB_BREQ_GET_USER_ID,
52 GS_USB_BREQ_SET_USER_ID,
53 GS_USB_BREQ_DATA_BITTIMING,
54 GS_USB_BREQ_BT_CONST_EXT,
58 /* reset a channel. turns it off */
59 GS_CAN_MODE_RESET = 0,
60 /* starts a channel */
65 GS_CAN_STATE_ERROR_ACTIVE = 0,
66 GS_CAN_STATE_ERROR_WARNING,
67 GS_CAN_STATE_ERROR_PASSIVE,
73 enum gs_can_identify_mode {
74 GS_CAN_IDENTIFY_OFF = 0,
78 /* data types passed between host and device */
80 /* The firmware on the original USB2CAN by Geschwister Schneider
81 * Technologie Entwicklungs- und Vertriebs UG exchanges all data
82 * between the host and the device in host byte order. This is done
83 * with the struct gs_host_config::byte_order member, which is sent
84 * first to indicate the desired byte order.
86 * The widely used open source firmware candleLight doesn't support
87 * this feature and exchanges the data in little endian byte order.
89 struct gs_host_config {
93 struct gs_device_config {
102 #define GS_CAN_MODE_NORMAL 0
103 #define GS_CAN_MODE_LISTEN_ONLY BIT(0)
104 #define GS_CAN_MODE_LOOP_BACK BIT(1)
105 #define GS_CAN_MODE_TRIPLE_SAMPLE BIT(2)
106 #define GS_CAN_MODE_ONE_SHOT BIT(3)
107 #define GS_CAN_MODE_HW_TIMESTAMP BIT(4)
108 /* GS_CAN_FEATURE_IDENTIFY BIT(5) */
109 /* GS_CAN_FEATURE_USER_ID BIT(6) */
110 #define GS_CAN_MODE_PAD_PKTS_TO_MAX_PKT_SIZE BIT(7)
111 #define GS_CAN_MODE_FD BIT(8)
112 /* GS_CAN_FEATURE_REQ_USB_QUIRK_LPC546XX BIT(9) */
113 /* GS_CAN_FEATURE_BT_CONST_EXT BIT(10) */
115 struct gs_device_mode {
120 struct gs_device_state {
126 struct gs_device_bittiming {
134 struct gs_identify_mode {
138 #define GS_CAN_FEATURE_LISTEN_ONLY BIT(0)
139 #define GS_CAN_FEATURE_LOOP_BACK BIT(1)
140 #define GS_CAN_FEATURE_TRIPLE_SAMPLE BIT(2)
141 #define GS_CAN_FEATURE_ONE_SHOT BIT(3)
142 #define GS_CAN_FEATURE_HW_TIMESTAMP BIT(4)
143 #define GS_CAN_FEATURE_IDENTIFY BIT(5)
144 #define GS_CAN_FEATURE_USER_ID BIT(6)
145 #define GS_CAN_FEATURE_PAD_PKTS_TO_MAX_PKT_SIZE BIT(7)
146 #define GS_CAN_FEATURE_FD BIT(8)
147 #define GS_CAN_FEATURE_REQ_USB_QUIRK_LPC546XX BIT(9)
148 #define GS_CAN_FEATURE_BT_CONST_EXT BIT(10)
149 #define GS_CAN_FEATURE_MASK GENMASK(10, 0)
151 /* internal quirks - keep in GS_CAN_FEATURE space for now */
153 /* CANtact Pro original firmware:
154 * BREQ DATA_BITTIMING overlaps with GET_USER_ID
156 #define GS_CAN_FEATURE_QUIRK_BREQ_CANTACT_PRO BIT(31)
158 struct gs_device_bt_const {
171 struct gs_device_bt_const_extended {
193 #define GS_CAN_FLAG_OVERFLOW BIT(0)
194 #define GS_CAN_FLAG_FD BIT(1)
195 #define GS_CAN_FLAG_BRS BIT(2)
196 #define GS_CAN_FLAG_ESI BIT(3)
202 struct classic_can_quirk {
216 struct gs_host_frame {
226 DECLARE_FLEX_ARRAY(struct classic_can, classic_can);
227 DECLARE_FLEX_ARRAY(struct classic_can_quirk, classic_can_quirk);
228 DECLARE_FLEX_ARRAY(struct canfd, canfd);
229 DECLARE_FLEX_ARRAY(struct canfd_quirk, canfd_quirk);
232 /* The GS USB devices make use of the same flags and masks as in
233 * linux/can.h and linux/can/error.h, and no additional mapping is necessary.
236 /* Only send a max of GS_MAX_TX_URBS frames per channel at a time. */
237 #define GS_MAX_TX_URBS 10
238 /* Only launch a max of GS_MAX_RX_URBS usb requests at a time. */
239 #define GS_MAX_RX_URBS 30
240 /* Maximum number of interfaces the driver supports per device.
241 * Current hardware only supports 3 interfaces. The future may vary.
243 #define GS_MAX_INTF 3
245 struct gs_tx_context {
247 unsigned int echo_id;
251 struct can_priv can; /* must be the first member */
253 struct gs_usb *parent;
255 struct net_device *netdev;
256 struct usb_device *udev;
257 struct usb_interface *iface;
259 struct can_bittiming_const bt_const, data_bt_const;
260 unsigned int channel; /* channel number */
263 unsigned int hf_size_tx;
265 /* This lock prevents a race condition between xmit and receive. */
266 spinlock_t tx_ctx_lock;
267 struct gs_tx_context tx_context[GS_MAX_TX_URBS];
269 struct usb_anchor tx_submitted;
270 atomic_t active_tx_urbs;
273 /* usb interface struct */
275 struct gs_can *canch[GS_MAX_INTF];
276 struct usb_anchor rx_submitted;
277 struct usb_device *udev;
278 unsigned int hf_size_rx;
282 /* 'allocate' a tx context.
283 * returns a valid tx context or NULL if there is no space.
285 static struct gs_tx_context *gs_alloc_tx_context(struct gs_can *dev)
290 spin_lock_irqsave(&dev->tx_ctx_lock, flags);
292 for (; i < GS_MAX_TX_URBS; i++) {
293 if (dev->tx_context[i].echo_id == GS_MAX_TX_URBS) {
294 dev->tx_context[i].echo_id = i;
295 spin_unlock_irqrestore(&dev->tx_ctx_lock, flags);
296 return &dev->tx_context[i];
300 spin_unlock_irqrestore(&dev->tx_ctx_lock, flags);
304 /* releases a tx context
306 static void gs_free_tx_context(struct gs_tx_context *txc)
308 txc->echo_id = GS_MAX_TX_URBS;
311 /* Get a tx context by id.
313 static struct gs_tx_context *gs_get_tx_context(struct gs_can *dev,
318 if (id < GS_MAX_TX_URBS) {
319 spin_lock_irqsave(&dev->tx_ctx_lock, flags);
320 if (dev->tx_context[id].echo_id == id) {
321 spin_unlock_irqrestore(&dev->tx_ctx_lock, flags);
322 return &dev->tx_context[id];
324 spin_unlock_irqrestore(&dev->tx_ctx_lock, flags);
329 static int gs_cmd_reset(struct gs_can *gsdev)
331 struct gs_device_mode *dm;
332 struct usb_interface *intf = gsdev->iface;
335 dm = kzalloc(sizeof(*dm), GFP_KERNEL);
339 dm->mode = GS_CAN_MODE_RESET;
341 rc = usb_control_msg(interface_to_usbdev(intf),
342 usb_sndctrlpipe(interface_to_usbdev(intf), 0),
344 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
345 gsdev->channel, 0, dm, sizeof(*dm), 1000);
352 static void gs_update_state(struct gs_can *dev, struct can_frame *cf)
354 struct can_device_stats *can_stats = &dev->can.can_stats;
356 if (cf->can_id & CAN_ERR_RESTARTED) {
357 dev->can.state = CAN_STATE_ERROR_ACTIVE;
358 can_stats->restarts++;
359 } else if (cf->can_id & CAN_ERR_BUSOFF) {
360 dev->can.state = CAN_STATE_BUS_OFF;
361 can_stats->bus_off++;
362 } else if (cf->can_id & CAN_ERR_CRTL) {
363 if ((cf->data[1] & CAN_ERR_CRTL_TX_WARNING) ||
364 (cf->data[1] & CAN_ERR_CRTL_RX_WARNING)) {
365 dev->can.state = CAN_STATE_ERROR_WARNING;
366 can_stats->error_warning++;
367 } else if ((cf->data[1] & CAN_ERR_CRTL_TX_PASSIVE) ||
368 (cf->data[1] & CAN_ERR_CRTL_RX_PASSIVE)) {
369 dev->can.state = CAN_STATE_ERROR_PASSIVE;
370 can_stats->error_passive++;
372 dev->can.state = CAN_STATE_ERROR_ACTIVE;
377 static void gs_usb_receive_bulk_callback(struct urb *urb)
379 struct gs_usb *usbcan = urb->context;
381 struct net_device *netdev;
383 struct net_device_stats *stats;
384 struct gs_host_frame *hf = urb->transfer_buffer;
385 struct gs_tx_context *txc;
386 struct can_frame *cf;
387 struct canfd_frame *cfd;
392 switch (urb->status) {
393 case 0: /* success */
399 /* do not resubmit aborted urbs. eg: when device goes down */
403 /* device reports out of range channel id */
404 if (hf->channel >= GS_MAX_INTF)
407 dev = usbcan->canch[hf->channel];
409 netdev = dev->netdev;
410 stats = &netdev->stats;
412 if (!netif_device_present(netdev))
415 if (hf->echo_id == -1) { /* normal rx */
416 if (hf->flags & GS_CAN_FLAG_FD) {
417 skb = alloc_canfd_skb(dev->netdev, &cfd);
421 cfd->can_id = le32_to_cpu(hf->can_id);
422 cfd->len = can_fd_dlc2len(hf->can_dlc);
423 if (hf->flags & GS_CAN_FLAG_BRS)
424 cfd->flags |= CANFD_BRS;
425 if (hf->flags & GS_CAN_FLAG_ESI)
426 cfd->flags |= CANFD_ESI;
428 memcpy(cfd->data, hf->canfd->data, cfd->len);
430 skb = alloc_can_skb(dev->netdev, &cf);
434 cf->can_id = le32_to_cpu(hf->can_id);
435 can_frame_set_cc_len(cf, hf->can_dlc, dev->can.ctrlmode);
437 memcpy(cf->data, hf->classic_can->data, 8);
439 /* ERROR frames tell us information about the controller */
440 if (le32_to_cpu(hf->can_id) & CAN_ERR_FLAG)
441 gs_update_state(dev, cf);
444 netdev->stats.rx_packets++;
445 netdev->stats.rx_bytes += hf->can_dlc;
448 } else { /* echo_id == hf->echo_id */
449 if (hf->echo_id >= GS_MAX_TX_URBS) {
451 "Unexpected out of range echo id %u\n",
456 txc = gs_get_tx_context(dev, hf->echo_id);
458 /* bad devices send bad echo_ids. */
461 "Unexpected unused echo id %u\n",
466 netdev->stats.tx_packets++;
467 netdev->stats.tx_bytes += can_get_echo_skb(netdev, hf->echo_id,
470 gs_free_tx_context(txc);
472 atomic_dec(&dev->active_tx_urbs);
474 netif_wake_queue(netdev);
477 if (hf->flags & GS_CAN_FLAG_OVERFLOW) {
478 skb = alloc_can_err_skb(netdev, &cf);
482 cf->can_id |= CAN_ERR_CRTL;
483 cf->len = CAN_ERR_DLC;
484 cf->data[1] = CAN_ERR_CRTL_RX_OVERFLOW;
485 stats->rx_over_errors++;
491 usb_fill_bulk_urb(urb, usbcan->udev,
492 usb_rcvbulkpipe(usbcan->udev, GSUSB_ENDPOINT_IN),
493 hf, dev->parent->hf_size_rx,
494 gs_usb_receive_bulk_callback, usbcan);
496 rc = usb_submit_urb(urb, GFP_ATOMIC);
498 /* USB failure take down all interfaces */
501 for (rc = 0; rc < GS_MAX_INTF; rc++) {
502 if (usbcan->canch[rc])
503 netif_device_detach(usbcan->canch[rc]->netdev);
508 static int gs_usb_set_bittiming(struct net_device *netdev)
510 struct gs_can *dev = netdev_priv(netdev);
511 struct can_bittiming *bt = &dev->can.bittiming;
512 struct usb_interface *intf = dev->iface;
514 struct gs_device_bittiming *dbt;
516 dbt = kmalloc(sizeof(*dbt), GFP_KERNEL);
520 dbt->prop_seg = cpu_to_le32(bt->prop_seg);
521 dbt->phase_seg1 = cpu_to_le32(bt->phase_seg1);
522 dbt->phase_seg2 = cpu_to_le32(bt->phase_seg2);
523 dbt->sjw = cpu_to_le32(bt->sjw);
524 dbt->brp = cpu_to_le32(bt->brp);
526 /* request bit timings */
527 rc = usb_control_msg(interface_to_usbdev(intf),
528 usb_sndctrlpipe(interface_to_usbdev(intf), 0),
529 GS_USB_BREQ_BITTIMING,
530 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
531 dev->channel, 0, dbt, sizeof(*dbt), 1000);
536 dev_err(netdev->dev.parent, "Couldn't set bittimings (err=%d)",
539 return (rc > 0) ? 0 : rc;
542 static int gs_usb_set_data_bittiming(struct net_device *netdev)
544 struct gs_can *dev = netdev_priv(netdev);
545 struct can_bittiming *bt = &dev->can.data_bittiming;
546 struct usb_interface *intf = dev->iface;
547 struct gs_device_bittiming *dbt;
548 u8 request = GS_USB_BREQ_DATA_BITTIMING;
551 dbt = kmalloc(sizeof(*dbt), GFP_KERNEL);
555 dbt->prop_seg = cpu_to_le32(bt->prop_seg);
556 dbt->phase_seg1 = cpu_to_le32(bt->phase_seg1);
557 dbt->phase_seg2 = cpu_to_le32(bt->phase_seg2);
558 dbt->sjw = cpu_to_le32(bt->sjw);
559 dbt->brp = cpu_to_le32(bt->brp);
561 if (dev->feature & GS_CAN_FEATURE_QUIRK_BREQ_CANTACT_PRO)
562 request = GS_USB_BREQ_QUIRK_CANTACT_PRO_DATA_BITTIMING;
564 /* request bit timings */
565 rc = usb_control_msg(interface_to_usbdev(intf),
566 usb_sndctrlpipe(interface_to_usbdev(intf), 0),
568 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
569 dev->channel, 0, dbt, sizeof(*dbt), 1000);
574 dev_err(netdev->dev.parent,
575 "Couldn't set data bittimings (err=%d)", rc);
577 return (rc > 0) ? 0 : rc;
580 static void gs_usb_xmit_callback(struct urb *urb)
582 struct gs_tx_context *txc = urb->context;
583 struct gs_can *dev = txc->dev;
584 struct net_device *netdev = dev->netdev;
587 netdev_info(netdev, "usb xmit fail %u\n", txc->echo_id);
589 usb_free_coherent(urb->dev, urb->transfer_buffer_length,
590 urb->transfer_buffer, urb->transfer_dma);
593 static netdev_tx_t gs_can_start_xmit(struct sk_buff *skb,
594 struct net_device *netdev)
596 struct gs_can *dev = netdev_priv(netdev);
597 struct net_device_stats *stats = &dev->netdev->stats;
599 struct gs_host_frame *hf;
600 struct can_frame *cf;
601 struct canfd_frame *cfd;
604 struct gs_tx_context *txc;
606 if (can_dropped_invalid_skb(netdev, skb))
609 /* find an empty context to keep track of transmission */
610 txc = gs_alloc_tx_context(dev);
612 return NETDEV_TX_BUSY;
614 /* create a URB, and a buffer for it */
615 urb = usb_alloc_urb(0, GFP_ATOMIC);
619 hf = usb_alloc_coherent(dev->udev, dev->hf_size_tx, GFP_ATOMIC,
622 netdev_err(netdev, "No memory left for USB buffer\n");
628 if (idx >= GS_MAX_TX_URBS) {
629 netdev_err(netdev, "Invalid tx context %u\n", idx);
634 hf->channel = dev->channel;
638 if (can_is_canfd_skb(skb)) {
639 cfd = (struct canfd_frame *)skb->data;
641 hf->can_id = cpu_to_le32(cfd->can_id);
642 hf->can_dlc = can_fd_len2dlc(cfd->len);
643 hf->flags |= GS_CAN_FLAG_FD;
644 if (cfd->flags & CANFD_BRS)
645 hf->flags |= GS_CAN_FLAG_BRS;
646 if (cfd->flags & CANFD_ESI)
647 hf->flags |= GS_CAN_FLAG_ESI;
649 memcpy(hf->canfd->data, cfd->data, cfd->len);
651 cf = (struct can_frame *)skb->data;
653 hf->can_id = cpu_to_le32(cf->can_id);
654 hf->can_dlc = can_get_cc_dlc(cf, dev->can.ctrlmode);
656 memcpy(hf->classic_can->data, cf->data, cf->len);
659 usb_fill_bulk_urb(urb, dev->udev,
660 usb_sndbulkpipe(dev->udev, GSUSB_ENDPOINT_OUT),
662 gs_usb_xmit_callback, txc);
664 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
665 usb_anchor_urb(urb, &dev->tx_submitted);
667 can_put_echo_skb(skb, netdev, idx, 0);
669 atomic_inc(&dev->active_tx_urbs);
671 rc = usb_submit_urb(urb, GFP_ATOMIC);
672 if (unlikely(rc)) { /* usb send failed */
673 atomic_dec(&dev->active_tx_urbs);
675 can_free_echo_skb(netdev, idx, NULL);
676 gs_free_tx_context(txc);
678 usb_unanchor_urb(urb);
679 usb_free_coherent(dev->udev, urb->transfer_buffer_length,
680 urb->transfer_buffer, urb->transfer_dma);
683 netif_device_detach(netdev);
685 netdev_err(netdev, "usb_submit failed (err=%d)\n", rc);
689 /* Slow down tx path */
690 if (atomic_read(&dev->active_tx_urbs) >= GS_MAX_TX_URBS)
691 netif_stop_queue(netdev);
694 /* let usb core take care of this urb */
700 usb_free_coherent(dev->udev, urb->transfer_buffer_length,
701 urb->transfer_buffer, urb->transfer_dma);
706 gs_free_tx_context(txc);
712 static int gs_can_open(struct net_device *netdev)
714 struct gs_can *dev = netdev_priv(netdev);
715 struct gs_usb *parent = dev->parent;
717 struct gs_device_mode *dm;
718 struct gs_host_frame *hf;
722 rc = open_candev(netdev);
726 ctrlmode = dev->can.ctrlmode;
727 if (ctrlmode & CAN_CTRLMODE_FD) {
728 flags |= GS_CAN_MODE_FD;
730 if (dev->feature & GS_CAN_FEATURE_REQ_USB_QUIRK_LPC546XX)
731 dev->hf_size_tx = struct_size(hf, canfd_quirk, 1);
733 dev->hf_size_tx = struct_size(hf, canfd, 1);
735 if (dev->feature & GS_CAN_FEATURE_REQ_USB_QUIRK_LPC546XX)
736 dev->hf_size_tx = struct_size(hf, classic_can_quirk, 1);
738 dev->hf_size_tx = struct_size(hf, classic_can, 1);
741 if (!parent->active_channels) {
742 for (i = 0; i < GS_MAX_RX_URBS; i++) {
747 urb = usb_alloc_urb(0, GFP_KERNEL);
751 /* alloc rx buffer */
752 buf = usb_alloc_coherent(dev->udev,
753 dev->parent->hf_size_rx,
758 "No memory left for USB buffer\n");
763 /* fill, anchor, and submit rx urb */
764 usb_fill_bulk_urb(urb,
766 usb_rcvbulkpipe(dev->udev,
769 dev->parent->hf_size_rx,
770 gs_usb_receive_bulk_callback, parent);
771 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
773 usb_anchor_urb(urb, &parent->rx_submitted);
775 rc = usb_submit_urb(urb, GFP_KERNEL);
778 netif_device_detach(dev->netdev);
781 "usb_submit failed (err=%d)\n", rc);
783 usb_unanchor_urb(urb);
789 * USB core will take care of freeing it
795 dm = kmalloc(sizeof(*dm), GFP_KERNEL);
800 if (ctrlmode & CAN_CTRLMODE_LOOPBACK)
801 flags |= GS_CAN_MODE_LOOP_BACK;
802 else if (ctrlmode & CAN_CTRLMODE_LISTENONLY)
803 flags |= GS_CAN_MODE_LISTEN_ONLY;
805 /* Controller is not allowed to retry TX
806 * this mode is unavailable on atmels uc3c hardware
808 if (ctrlmode & CAN_CTRLMODE_ONE_SHOT)
809 flags |= GS_CAN_MODE_ONE_SHOT;
811 if (ctrlmode & CAN_CTRLMODE_3_SAMPLES)
812 flags |= GS_CAN_MODE_TRIPLE_SAMPLE;
814 /* finally start device */
815 dm->mode = cpu_to_le32(GS_CAN_MODE_START);
816 dm->flags = cpu_to_le32(flags);
817 rc = usb_control_msg(interface_to_usbdev(dev->iface),
818 usb_sndctrlpipe(interface_to_usbdev(dev->iface), 0),
820 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
821 dev->channel, 0, dm, sizeof(*dm), 1000);
824 netdev_err(netdev, "Couldn't start device (err=%d)\n", rc);
831 dev->can.state = CAN_STATE_ERROR_ACTIVE;
833 parent->active_channels++;
834 if (!(dev->can.ctrlmode & CAN_CTRLMODE_LISTENONLY))
835 netif_start_queue(netdev);
840 static int gs_can_close(struct net_device *netdev)
843 struct gs_can *dev = netdev_priv(netdev);
844 struct gs_usb *parent = dev->parent;
846 netif_stop_queue(netdev);
849 parent->active_channels--;
850 if (!parent->active_channels)
851 usb_kill_anchored_urbs(&parent->rx_submitted);
853 /* Stop sending URBs */
854 usb_kill_anchored_urbs(&dev->tx_submitted);
855 atomic_set(&dev->active_tx_urbs, 0);
857 /* reset the device */
858 rc = gs_cmd_reset(dev);
860 netdev_warn(netdev, "Couldn't shutdown device (err=%d)", rc);
862 /* reset tx contexts */
863 for (rc = 0; rc < GS_MAX_TX_URBS; rc++) {
864 dev->tx_context[rc].dev = dev;
865 dev->tx_context[rc].echo_id = GS_MAX_TX_URBS;
868 /* close the netdev */
869 close_candev(netdev);
874 static const struct net_device_ops gs_usb_netdev_ops = {
875 .ndo_open = gs_can_open,
876 .ndo_stop = gs_can_close,
877 .ndo_start_xmit = gs_can_start_xmit,
878 .ndo_change_mtu = can_change_mtu,
881 static int gs_usb_set_identify(struct net_device *netdev, bool do_identify)
883 struct gs_can *dev = netdev_priv(netdev);
884 struct gs_identify_mode *imode;
887 imode = kmalloc(sizeof(*imode), GFP_KERNEL);
893 imode->mode = cpu_to_le32(GS_CAN_IDENTIFY_ON);
895 imode->mode = cpu_to_le32(GS_CAN_IDENTIFY_OFF);
897 rc = usb_control_msg(interface_to_usbdev(dev->iface),
898 usb_sndctrlpipe(interface_to_usbdev(dev->iface), 0),
899 GS_USB_BREQ_IDENTIFY,
900 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
901 dev->channel, 0, imode, sizeof(*imode), 100);
905 return (rc > 0) ? 0 : rc;
908 /* blink LED's for finding the this interface */
909 static int gs_usb_set_phys_id(struct net_device *dev,
910 enum ethtool_phys_id_state state)
915 case ETHTOOL_ID_ACTIVE:
916 rc = gs_usb_set_identify(dev, GS_CAN_IDENTIFY_ON);
918 case ETHTOOL_ID_INACTIVE:
919 rc = gs_usb_set_identify(dev, GS_CAN_IDENTIFY_OFF);
928 static const struct ethtool_ops gs_usb_ethtool_ops = {
929 .set_phys_id = gs_usb_set_phys_id,
932 static struct gs_can *gs_make_candev(unsigned int channel,
933 struct usb_interface *intf,
934 struct gs_device_config *dconf)
937 struct net_device *netdev;
939 struct gs_device_bt_const *bt_const;
940 struct gs_device_bt_const_extended *bt_const_extended;
943 bt_const = kmalloc(sizeof(*bt_const), GFP_KERNEL);
945 return ERR_PTR(-ENOMEM);
947 /* fetch bit timing constants */
948 rc = usb_control_msg(interface_to_usbdev(intf),
949 usb_rcvctrlpipe(interface_to_usbdev(intf), 0),
950 GS_USB_BREQ_BT_CONST,
951 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
952 channel, 0, bt_const, sizeof(*bt_const), 1000);
956 "Couldn't get bit timing const for channel (err=%d)\n",
963 netdev = alloc_candev(sizeof(struct gs_can), GS_MAX_TX_URBS);
965 dev_err(&intf->dev, "Couldn't allocate candev\n");
967 return ERR_PTR(-ENOMEM);
970 dev = netdev_priv(netdev);
972 netdev->netdev_ops = &gs_usb_netdev_ops;
974 netdev->flags |= IFF_ECHO; /* we support full roundtrip echo */
977 strcpy(dev->bt_const.name, "gs_usb");
978 dev->bt_const.tseg1_min = le32_to_cpu(bt_const->tseg1_min);
979 dev->bt_const.tseg1_max = le32_to_cpu(bt_const->tseg1_max);
980 dev->bt_const.tseg2_min = le32_to_cpu(bt_const->tseg2_min);
981 dev->bt_const.tseg2_max = le32_to_cpu(bt_const->tseg2_max);
982 dev->bt_const.sjw_max = le32_to_cpu(bt_const->sjw_max);
983 dev->bt_const.brp_min = le32_to_cpu(bt_const->brp_min);
984 dev->bt_const.brp_max = le32_to_cpu(bt_const->brp_max);
985 dev->bt_const.brp_inc = le32_to_cpu(bt_const->brp_inc);
987 dev->udev = interface_to_usbdev(intf);
989 dev->netdev = netdev;
990 dev->channel = channel;
992 init_usb_anchor(&dev->tx_submitted);
993 atomic_set(&dev->active_tx_urbs, 0);
994 spin_lock_init(&dev->tx_ctx_lock);
995 for (rc = 0; rc < GS_MAX_TX_URBS; rc++) {
996 dev->tx_context[rc].dev = dev;
997 dev->tx_context[rc].echo_id = GS_MAX_TX_URBS;
1001 dev->can.state = CAN_STATE_STOPPED;
1002 dev->can.clock.freq = le32_to_cpu(bt_const->fclk_can);
1003 dev->can.bittiming_const = &dev->bt_const;
1004 dev->can.do_set_bittiming = gs_usb_set_bittiming;
1006 dev->can.ctrlmode_supported = CAN_CTRLMODE_CC_LEN8_DLC;
1008 feature = le32_to_cpu(bt_const->feature);
1009 dev->feature = FIELD_GET(GS_CAN_FEATURE_MASK, feature);
1010 if (feature & GS_CAN_FEATURE_LISTEN_ONLY)
1011 dev->can.ctrlmode_supported |= CAN_CTRLMODE_LISTENONLY;
1013 if (feature & GS_CAN_FEATURE_LOOP_BACK)
1014 dev->can.ctrlmode_supported |= CAN_CTRLMODE_LOOPBACK;
1016 if (feature & GS_CAN_FEATURE_TRIPLE_SAMPLE)
1017 dev->can.ctrlmode_supported |= CAN_CTRLMODE_3_SAMPLES;
1019 if (feature & GS_CAN_FEATURE_ONE_SHOT)
1020 dev->can.ctrlmode_supported |= CAN_CTRLMODE_ONE_SHOT;
1022 if (feature & GS_CAN_FEATURE_FD) {
1023 dev->can.ctrlmode_supported |= CAN_CTRLMODE_FD;
1024 /* The data bit timing will be overwritten, if
1025 * GS_CAN_FEATURE_BT_CONST_EXT is set.
1027 dev->can.data_bittiming_const = &dev->bt_const;
1028 dev->can.do_set_data_bittiming = gs_usb_set_data_bittiming;
1031 /* The CANtact Pro from LinkLayer Labs is based on the
1032 * LPC54616 µC, which is affected by the NXP LPC USB transfer
1033 * erratum. However, the current firmware (version 2) doesn't
1034 * set the GS_CAN_FEATURE_REQ_USB_QUIRK_LPC546XX bit. Set the
1035 * feature GS_CAN_FEATURE_REQ_USB_QUIRK_LPC546XX to workaround
1038 * For the GS_USB_BREQ_DATA_BITTIMING USB control message the
1039 * CANtact Pro firmware uses a request value, which is already
1040 * used by the candleLight firmware for a different purpose
1041 * (GS_USB_BREQ_GET_USER_ID). Set the feature
1042 * GS_CAN_FEATURE_QUIRK_BREQ_CANTACT_PRO to workaround this
1045 if (dev->udev->descriptor.idVendor == cpu_to_le16(USB_GSUSB_1_VENDOR_ID) &&
1046 dev->udev->descriptor.idProduct == cpu_to_le16(USB_GSUSB_1_PRODUCT_ID) &&
1047 dev->udev->manufacturer && dev->udev->product &&
1048 !strcmp(dev->udev->manufacturer, "LinkLayer Labs") &&
1049 !strcmp(dev->udev->product, "CANtact Pro") &&
1050 (le32_to_cpu(dconf->sw_version) <= 2))
1051 dev->feature |= GS_CAN_FEATURE_REQ_USB_QUIRK_LPC546XX |
1052 GS_CAN_FEATURE_QUIRK_BREQ_CANTACT_PRO;
1054 if (le32_to_cpu(dconf->sw_version) > 1)
1055 if (feature & GS_CAN_FEATURE_IDENTIFY)
1056 netdev->ethtool_ops = &gs_usb_ethtool_ops;
1060 /* fetch extended bit timing constants if device has feature
1061 * GS_CAN_FEATURE_FD and GS_CAN_FEATURE_BT_CONST_EXT
1063 if (feature & GS_CAN_FEATURE_FD &&
1064 feature & GS_CAN_FEATURE_BT_CONST_EXT) {
1065 bt_const_extended = kmalloc(sizeof(*bt_const_extended), GFP_KERNEL);
1066 if (!bt_const_extended)
1067 return ERR_PTR(-ENOMEM);
1069 rc = usb_control_msg(interface_to_usbdev(intf),
1070 usb_rcvctrlpipe(interface_to_usbdev(intf), 0),
1071 GS_USB_BREQ_BT_CONST_EXT,
1072 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
1073 channel, 0, bt_const_extended,
1074 sizeof(*bt_const_extended),
1078 "Couldn't get extended bit timing const for channel (err=%d)\n",
1080 kfree(bt_const_extended);
1084 strcpy(dev->data_bt_const.name, "gs_usb");
1085 dev->data_bt_const.tseg1_min = le32_to_cpu(bt_const_extended->dtseg1_min);
1086 dev->data_bt_const.tseg1_max = le32_to_cpu(bt_const_extended->dtseg1_max);
1087 dev->data_bt_const.tseg2_min = le32_to_cpu(bt_const_extended->dtseg2_min);
1088 dev->data_bt_const.tseg2_max = le32_to_cpu(bt_const_extended->dtseg2_max);
1089 dev->data_bt_const.sjw_max = le32_to_cpu(bt_const_extended->dsjw_max);
1090 dev->data_bt_const.brp_min = le32_to_cpu(bt_const_extended->dbrp_min);
1091 dev->data_bt_const.brp_max = le32_to_cpu(bt_const_extended->dbrp_max);
1092 dev->data_bt_const.brp_inc = le32_to_cpu(bt_const_extended->dbrp_inc);
1094 dev->can.data_bittiming_const = &dev->data_bt_const;
1096 kfree(bt_const_extended);
1099 SET_NETDEV_DEV(netdev, &intf->dev);
1101 rc = register_candev(dev->netdev);
1103 free_candev(dev->netdev);
1104 dev_err(&intf->dev, "Couldn't register candev (err=%d)\n", rc);
1111 static void gs_destroy_candev(struct gs_can *dev)
1113 unregister_candev(dev->netdev);
1114 usb_kill_anchored_urbs(&dev->tx_submitted);
1115 free_candev(dev->netdev);
1118 static int gs_usb_probe(struct usb_interface *intf,
1119 const struct usb_device_id *id)
1121 struct usb_device *udev = interface_to_usbdev(intf);
1122 struct gs_host_frame *hf;
1125 unsigned int icount, i;
1126 struct gs_host_config *hconf;
1127 struct gs_device_config *dconf;
1129 hconf = kmalloc(sizeof(*hconf), GFP_KERNEL);
1133 hconf->byte_order = cpu_to_le32(0x0000beef);
1135 /* send host config */
1136 rc = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
1137 GS_USB_BREQ_HOST_FORMAT,
1138 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
1139 1, intf->cur_altsetting->desc.bInterfaceNumber,
1140 hconf, sizeof(*hconf), 1000);
1145 dev_err(&intf->dev, "Couldn't send data format (err=%d)\n", rc);
1149 dconf = kmalloc(sizeof(*dconf), GFP_KERNEL);
1153 /* read device config */
1154 rc = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
1155 GS_USB_BREQ_DEVICE_CONFIG,
1156 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
1157 1, intf->cur_altsetting->desc.bInterfaceNumber,
1158 dconf, sizeof(*dconf), 1000);
1160 dev_err(&intf->dev, "Couldn't get device config: (err=%d)\n",
1166 icount = dconf->icount + 1;
1167 dev_info(&intf->dev, "Configuring for %u interfaces\n", icount);
1169 if (icount > GS_MAX_INTF) {
1171 "Driver cannot handle more that %u CAN interfaces\n",
1177 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1183 init_usb_anchor(&dev->rx_submitted);
1184 /* default to classic CAN, switch to CAN-FD if at least one of
1185 * our channels support CAN-FD.
1187 dev->hf_size_rx = struct_size(hf, classic_can, 1);
1189 usb_set_intfdata(intf, dev);
1192 for (i = 0; i < icount; i++) {
1193 dev->canch[i] = gs_make_candev(i, intf, dconf);
1194 if (IS_ERR_OR_NULL(dev->canch[i])) {
1195 /* save error code to return later */
1196 rc = PTR_ERR(dev->canch[i]);
1198 /* on failure destroy previously created candevs */
1200 for (i = 0; i < icount; i++)
1201 gs_destroy_candev(dev->canch[i]);
1203 usb_kill_anchored_urbs(&dev->rx_submitted);
1208 dev->canch[i]->parent = dev;
1210 if (dev->canch[i]->can.ctrlmode_supported & CAN_CTRLMODE_FD)
1211 dev->hf_size_rx = struct_size(hf, canfd, 1);
1219 static void gs_usb_disconnect(struct usb_interface *intf)
1221 struct gs_usb *dev = usb_get_intfdata(intf);
1224 usb_set_intfdata(intf, NULL);
1227 dev_err(&intf->dev, "Disconnect (nodata)\n");
1231 for (i = 0; i < GS_MAX_INTF; i++)
1233 gs_destroy_candev(dev->canch[i]);
1235 usb_kill_anchored_urbs(&dev->rx_submitted);
1239 static const struct usb_device_id gs_usb_table[] = {
1240 { USB_DEVICE_INTERFACE_NUMBER(USB_GSUSB_1_VENDOR_ID,
1241 USB_GSUSB_1_PRODUCT_ID, 0) },
1242 { USB_DEVICE_INTERFACE_NUMBER(USB_CANDLELIGHT_VENDOR_ID,
1243 USB_CANDLELIGHT_PRODUCT_ID, 0) },
1244 { USB_DEVICE_INTERFACE_NUMBER(USB_CES_CANEXT_FD_VENDOR_ID,
1245 USB_CES_CANEXT_FD_PRODUCT_ID, 0) },
1246 { USB_DEVICE_INTERFACE_NUMBER(USB_ABE_CANDEBUGGER_FD_VENDOR_ID,
1247 USB_ABE_CANDEBUGGER_FD_PRODUCT_ID, 0) },
1248 {} /* Terminating entry */
1251 MODULE_DEVICE_TABLE(usb, gs_usb_table);
1253 static struct usb_driver gs_usb_driver = {
1255 .probe = gs_usb_probe,
1256 .disconnect = gs_usb_disconnect,
1257 .id_table = gs_usb_table,
1260 module_usb_driver(gs_usb_driver);
1262 MODULE_AUTHOR("Maximilian Schneider <mws@schneidersoft.net>");
1264 "Socket CAN device driver for Geschwister Schneider Technologie-, "
1265 "Entwicklungs- und Vertriebs UG. USB2.0 to CAN interfaces\n"
1266 "and bytewerk.org candleLight USB CAN interfaces.");
1267 MODULE_LICENSE("GPL v2");