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;
271 void *rxbuf[GS_MAX_RX_URBS];
272 dma_addr_t rxbuf_dma[GS_MAX_RX_URBS];
275 /* usb interface struct */
277 struct gs_can *canch[GS_MAX_INTF];
278 struct usb_anchor rx_submitted;
279 struct usb_device *udev;
280 unsigned int hf_size_rx;
284 /* 'allocate' a tx context.
285 * returns a valid tx context or NULL if there is no space.
287 static struct gs_tx_context *gs_alloc_tx_context(struct gs_can *dev)
292 spin_lock_irqsave(&dev->tx_ctx_lock, flags);
294 for (; i < GS_MAX_TX_URBS; i++) {
295 if (dev->tx_context[i].echo_id == GS_MAX_TX_URBS) {
296 dev->tx_context[i].echo_id = i;
297 spin_unlock_irqrestore(&dev->tx_ctx_lock, flags);
298 return &dev->tx_context[i];
302 spin_unlock_irqrestore(&dev->tx_ctx_lock, flags);
306 /* releases a tx context
308 static void gs_free_tx_context(struct gs_tx_context *txc)
310 txc->echo_id = GS_MAX_TX_URBS;
313 /* Get a tx context by id.
315 static struct gs_tx_context *gs_get_tx_context(struct gs_can *dev,
320 if (id < GS_MAX_TX_URBS) {
321 spin_lock_irqsave(&dev->tx_ctx_lock, flags);
322 if (dev->tx_context[id].echo_id == id) {
323 spin_unlock_irqrestore(&dev->tx_ctx_lock, flags);
324 return &dev->tx_context[id];
326 spin_unlock_irqrestore(&dev->tx_ctx_lock, flags);
331 static int gs_cmd_reset(struct gs_can *gsdev)
333 struct gs_device_mode *dm;
334 struct usb_interface *intf = gsdev->iface;
337 dm = kzalloc(sizeof(*dm), GFP_KERNEL);
341 dm->mode = GS_CAN_MODE_RESET;
343 rc = usb_control_msg(interface_to_usbdev(intf),
344 usb_sndctrlpipe(interface_to_usbdev(intf), 0),
346 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
347 gsdev->channel, 0, dm, sizeof(*dm), 1000);
354 static void gs_update_state(struct gs_can *dev, struct can_frame *cf)
356 struct can_device_stats *can_stats = &dev->can.can_stats;
358 if (cf->can_id & CAN_ERR_RESTARTED) {
359 dev->can.state = CAN_STATE_ERROR_ACTIVE;
360 can_stats->restarts++;
361 } else if (cf->can_id & CAN_ERR_BUSOFF) {
362 dev->can.state = CAN_STATE_BUS_OFF;
363 can_stats->bus_off++;
364 } else if (cf->can_id & CAN_ERR_CRTL) {
365 if ((cf->data[1] & CAN_ERR_CRTL_TX_WARNING) ||
366 (cf->data[1] & CAN_ERR_CRTL_RX_WARNING)) {
367 dev->can.state = CAN_STATE_ERROR_WARNING;
368 can_stats->error_warning++;
369 } else if ((cf->data[1] & CAN_ERR_CRTL_TX_PASSIVE) ||
370 (cf->data[1] & CAN_ERR_CRTL_RX_PASSIVE)) {
371 dev->can.state = CAN_STATE_ERROR_PASSIVE;
372 can_stats->error_passive++;
374 dev->can.state = CAN_STATE_ERROR_ACTIVE;
379 static void gs_usb_receive_bulk_callback(struct urb *urb)
381 struct gs_usb *usbcan = urb->context;
383 struct net_device *netdev;
385 struct net_device_stats *stats;
386 struct gs_host_frame *hf = urb->transfer_buffer;
387 struct gs_tx_context *txc;
388 struct can_frame *cf;
389 struct canfd_frame *cfd;
394 switch (urb->status) {
395 case 0: /* success */
401 /* do not resubmit aborted urbs. eg: when device goes down */
405 /* device reports out of range channel id */
406 if (hf->channel >= GS_MAX_INTF)
409 dev = usbcan->canch[hf->channel];
411 netdev = dev->netdev;
412 stats = &netdev->stats;
414 if (!netif_device_present(netdev))
417 if (hf->echo_id == -1) { /* normal rx */
418 if (hf->flags & GS_CAN_FLAG_FD) {
419 skb = alloc_canfd_skb(dev->netdev, &cfd);
423 cfd->can_id = le32_to_cpu(hf->can_id);
424 cfd->len = can_fd_dlc2len(hf->can_dlc);
425 if (hf->flags & GS_CAN_FLAG_BRS)
426 cfd->flags |= CANFD_BRS;
427 if (hf->flags & GS_CAN_FLAG_ESI)
428 cfd->flags |= CANFD_ESI;
430 memcpy(cfd->data, hf->canfd->data, cfd->len);
432 skb = alloc_can_skb(dev->netdev, &cf);
436 cf->can_id = le32_to_cpu(hf->can_id);
437 can_frame_set_cc_len(cf, hf->can_dlc, dev->can.ctrlmode);
439 memcpy(cf->data, hf->classic_can->data, 8);
441 /* ERROR frames tell us information about the controller */
442 if (le32_to_cpu(hf->can_id) & CAN_ERR_FLAG)
443 gs_update_state(dev, cf);
446 netdev->stats.rx_packets++;
447 netdev->stats.rx_bytes += hf->can_dlc;
450 } else { /* echo_id == hf->echo_id */
451 if (hf->echo_id >= GS_MAX_TX_URBS) {
453 "Unexpected out of range echo id %u\n",
458 txc = gs_get_tx_context(dev, hf->echo_id);
460 /* bad devices send bad echo_ids. */
463 "Unexpected unused echo id %u\n",
468 netdev->stats.tx_packets++;
469 netdev->stats.tx_bytes += can_get_echo_skb(netdev, hf->echo_id,
472 gs_free_tx_context(txc);
474 atomic_dec(&dev->active_tx_urbs);
476 netif_wake_queue(netdev);
479 if (hf->flags & GS_CAN_FLAG_OVERFLOW) {
480 skb = alloc_can_err_skb(netdev, &cf);
484 cf->can_id |= CAN_ERR_CRTL;
485 cf->len = CAN_ERR_DLC;
486 cf->data[1] = CAN_ERR_CRTL_RX_OVERFLOW;
487 stats->rx_over_errors++;
493 usb_fill_bulk_urb(urb, usbcan->udev,
494 usb_rcvbulkpipe(usbcan->udev, GSUSB_ENDPOINT_IN),
495 hf, dev->parent->hf_size_rx,
496 gs_usb_receive_bulk_callback, usbcan);
498 rc = usb_submit_urb(urb, GFP_ATOMIC);
500 /* USB failure take down all interfaces */
503 for (rc = 0; rc < GS_MAX_INTF; rc++) {
504 if (usbcan->canch[rc])
505 netif_device_detach(usbcan->canch[rc]->netdev);
510 static int gs_usb_set_bittiming(struct net_device *netdev)
512 struct gs_can *dev = netdev_priv(netdev);
513 struct can_bittiming *bt = &dev->can.bittiming;
514 struct usb_interface *intf = dev->iface;
516 struct gs_device_bittiming *dbt;
518 dbt = kmalloc(sizeof(*dbt), GFP_KERNEL);
522 dbt->prop_seg = cpu_to_le32(bt->prop_seg);
523 dbt->phase_seg1 = cpu_to_le32(bt->phase_seg1);
524 dbt->phase_seg2 = cpu_to_le32(bt->phase_seg2);
525 dbt->sjw = cpu_to_le32(bt->sjw);
526 dbt->brp = cpu_to_le32(bt->brp);
528 /* request bit timings */
529 rc = usb_control_msg(interface_to_usbdev(intf),
530 usb_sndctrlpipe(interface_to_usbdev(intf), 0),
531 GS_USB_BREQ_BITTIMING,
532 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
533 dev->channel, 0, dbt, sizeof(*dbt), 1000);
538 dev_err(netdev->dev.parent, "Couldn't set bittimings (err=%d)",
541 return (rc > 0) ? 0 : rc;
544 static int gs_usb_set_data_bittiming(struct net_device *netdev)
546 struct gs_can *dev = netdev_priv(netdev);
547 struct can_bittiming *bt = &dev->can.data_bittiming;
548 struct usb_interface *intf = dev->iface;
549 struct gs_device_bittiming *dbt;
550 u8 request = GS_USB_BREQ_DATA_BITTIMING;
553 dbt = kmalloc(sizeof(*dbt), GFP_KERNEL);
557 dbt->prop_seg = cpu_to_le32(bt->prop_seg);
558 dbt->phase_seg1 = cpu_to_le32(bt->phase_seg1);
559 dbt->phase_seg2 = cpu_to_le32(bt->phase_seg2);
560 dbt->sjw = cpu_to_le32(bt->sjw);
561 dbt->brp = cpu_to_le32(bt->brp);
563 if (dev->feature & GS_CAN_FEATURE_QUIRK_BREQ_CANTACT_PRO)
564 request = GS_USB_BREQ_QUIRK_CANTACT_PRO_DATA_BITTIMING;
566 /* request bit timings */
567 rc = usb_control_msg(interface_to_usbdev(intf),
568 usb_sndctrlpipe(interface_to_usbdev(intf), 0),
570 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
571 dev->channel, 0, dbt, sizeof(*dbt), 1000);
576 dev_err(netdev->dev.parent,
577 "Couldn't set data bittimings (err=%d)", rc);
579 return (rc > 0) ? 0 : rc;
582 static void gs_usb_xmit_callback(struct urb *urb)
584 struct gs_tx_context *txc = urb->context;
585 struct gs_can *dev = txc->dev;
586 struct net_device *netdev = dev->netdev;
589 netdev_info(netdev, "usb xmit fail %u\n", txc->echo_id);
591 usb_free_coherent(urb->dev, urb->transfer_buffer_length,
592 urb->transfer_buffer, urb->transfer_dma);
595 static netdev_tx_t gs_can_start_xmit(struct sk_buff *skb,
596 struct net_device *netdev)
598 struct gs_can *dev = netdev_priv(netdev);
599 struct net_device_stats *stats = &dev->netdev->stats;
601 struct gs_host_frame *hf;
602 struct can_frame *cf;
603 struct canfd_frame *cfd;
606 struct gs_tx_context *txc;
608 if (can_dropped_invalid_skb(netdev, skb))
611 /* find an empty context to keep track of transmission */
612 txc = gs_alloc_tx_context(dev);
614 return NETDEV_TX_BUSY;
616 /* create a URB, and a buffer for it */
617 urb = usb_alloc_urb(0, GFP_ATOMIC);
621 hf = usb_alloc_coherent(dev->udev, dev->hf_size_tx, GFP_ATOMIC,
624 netdev_err(netdev, "No memory left for USB buffer\n");
630 if (idx >= GS_MAX_TX_URBS) {
631 netdev_err(netdev, "Invalid tx context %u\n", idx);
636 hf->channel = dev->channel;
640 if (can_is_canfd_skb(skb)) {
641 cfd = (struct canfd_frame *)skb->data;
643 hf->can_id = cpu_to_le32(cfd->can_id);
644 hf->can_dlc = can_fd_len2dlc(cfd->len);
645 hf->flags |= GS_CAN_FLAG_FD;
646 if (cfd->flags & CANFD_BRS)
647 hf->flags |= GS_CAN_FLAG_BRS;
648 if (cfd->flags & CANFD_ESI)
649 hf->flags |= GS_CAN_FLAG_ESI;
651 memcpy(hf->canfd->data, cfd->data, cfd->len);
653 cf = (struct can_frame *)skb->data;
655 hf->can_id = cpu_to_le32(cf->can_id);
656 hf->can_dlc = can_get_cc_dlc(cf, dev->can.ctrlmode);
658 memcpy(hf->classic_can->data, cf->data, cf->len);
661 usb_fill_bulk_urb(urb, dev->udev,
662 usb_sndbulkpipe(dev->udev, GSUSB_ENDPOINT_OUT),
664 gs_usb_xmit_callback, txc);
666 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
667 usb_anchor_urb(urb, &dev->tx_submitted);
669 can_put_echo_skb(skb, netdev, idx, 0);
671 atomic_inc(&dev->active_tx_urbs);
673 rc = usb_submit_urb(urb, GFP_ATOMIC);
674 if (unlikely(rc)) { /* usb send failed */
675 atomic_dec(&dev->active_tx_urbs);
677 can_free_echo_skb(netdev, idx, NULL);
678 gs_free_tx_context(txc);
680 usb_unanchor_urb(urb);
681 usb_free_coherent(dev->udev, urb->transfer_buffer_length,
682 urb->transfer_buffer, urb->transfer_dma);
685 netif_device_detach(netdev);
687 netdev_err(netdev, "usb_submit failed (err=%d)\n", rc);
691 /* Slow down tx path */
692 if (atomic_read(&dev->active_tx_urbs) >= GS_MAX_TX_URBS)
693 netif_stop_queue(netdev);
696 /* let usb core take care of this urb */
702 usb_free_coherent(dev->udev, urb->transfer_buffer_length,
703 urb->transfer_buffer, urb->transfer_dma);
708 gs_free_tx_context(txc);
714 static int gs_can_open(struct net_device *netdev)
716 struct gs_can *dev = netdev_priv(netdev);
717 struct gs_usb *parent = dev->parent;
719 struct gs_device_mode *dm;
720 struct gs_host_frame *hf;
724 rc = open_candev(netdev);
728 ctrlmode = dev->can.ctrlmode;
729 if (ctrlmode & CAN_CTRLMODE_FD) {
730 flags |= GS_CAN_MODE_FD;
732 if (dev->feature & GS_CAN_FEATURE_REQ_USB_QUIRK_LPC546XX)
733 dev->hf_size_tx = struct_size(hf, canfd_quirk, 1);
735 dev->hf_size_tx = struct_size(hf, canfd, 1);
737 if (dev->feature & GS_CAN_FEATURE_REQ_USB_QUIRK_LPC546XX)
738 dev->hf_size_tx = struct_size(hf, classic_can_quirk, 1);
740 dev->hf_size_tx = struct_size(hf, classic_can, 1);
743 if (!parent->active_channels) {
744 for (i = 0; i < GS_MAX_RX_URBS; i++) {
750 urb = usb_alloc_urb(0, GFP_KERNEL);
754 /* alloc rx buffer */
755 buf = usb_alloc_coherent(dev->udev,
756 dev->parent->hf_size_rx,
761 "No memory left for USB buffer\n");
766 urb->transfer_dma = buf_dma;
768 /* fill, anchor, and submit rx urb */
769 usb_fill_bulk_urb(urb,
771 usb_rcvbulkpipe(dev->udev,
774 dev->parent->hf_size_rx,
775 gs_usb_receive_bulk_callback, parent);
776 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
778 usb_anchor_urb(urb, &parent->rx_submitted);
780 rc = usb_submit_urb(urb, GFP_KERNEL);
783 netif_device_detach(dev->netdev);
786 "usb_submit failed (err=%d)\n", rc);
788 usb_unanchor_urb(urb);
789 usb_free_coherent(dev->udev,
790 sizeof(struct gs_host_frame),
798 dev->rxbuf_dma[i] = buf_dma;
801 * USB core will take care of freeing it
807 dm = kmalloc(sizeof(*dm), GFP_KERNEL);
812 if (ctrlmode & CAN_CTRLMODE_LOOPBACK)
813 flags |= GS_CAN_MODE_LOOP_BACK;
814 else if (ctrlmode & CAN_CTRLMODE_LISTENONLY)
815 flags |= GS_CAN_MODE_LISTEN_ONLY;
817 /* Controller is not allowed to retry TX
818 * this mode is unavailable on atmels uc3c hardware
820 if (ctrlmode & CAN_CTRLMODE_ONE_SHOT)
821 flags |= GS_CAN_MODE_ONE_SHOT;
823 if (ctrlmode & CAN_CTRLMODE_3_SAMPLES)
824 flags |= GS_CAN_MODE_TRIPLE_SAMPLE;
826 /* finally start device */
827 dm->mode = cpu_to_le32(GS_CAN_MODE_START);
828 dm->flags = cpu_to_le32(flags);
829 rc = usb_control_msg(interface_to_usbdev(dev->iface),
830 usb_sndctrlpipe(interface_to_usbdev(dev->iface), 0),
832 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
833 dev->channel, 0, dm, sizeof(*dm), 1000);
836 netdev_err(netdev, "Couldn't start device (err=%d)\n", rc);
843 dev->can.state = CAN_STATE_ERROR_ACTIVE;
845 parent->active_channels++;
846 if (!(dev->can.ctrlmode & CAN_CTRLMODE_LISTENONLY))
847 netif_start_queue(netdev);
852 static int gs_can_close(struct net_device *netdev)
855 struct gs_can *dev = netdev_priv(netdev);
856 struct gs_usb *parent = dev->parent;
859 netif_stop_queue(netdev);
862 parent->active_channels--;
863 if (!parent->active_channels) {
864 usb_kill_anchored_urbs(&parent->rx_submitted);
865 for (i = 0; i < GS_MAX_RX_URBS; i++)
866 usb_free_coherent(dev->udev,
867 sizeof(struct gs_host_frame),
872 /* Stop sending URBs */
873 usb_kill_anchored_urbs(&dev->tx_submitted);
874 atomic_set(&dev->active_tx_urbs, 0);
876 /* reset the device */
877 rc = gs_cmd_reset(dev);
879 netdev_warn(netdev, "Couldn't shutdown device (err=%d)", rc);
881 /* reset tx contexts */
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;
887 /* close the netdev */
888 close_candev(netdev);
893 static const struct net_device_ops gs_usb_netdev_ops = {
894 .ndo_open = gs_can_open,
895 .ndo_stop = gs_can_close,
896 .ndo_start_xmit = gs_can_start_xmit,
897 .ndo_change_mtu = can_change_mtu,
900 static int gs_usb_set_identify(struct net_device *netdev, bool do_identify)
902 struct gs_can *dev = netdev_priv(netdev);
903 struct gs_identify_mode *imode;
906 imode = kmalloc(sizeof(*imode), GFP_KERNEL);
912 imode->mode = cpu_to_le32(GS_CAN_IDENTIFY_ON);
914 imode->mode = cpu_to_le32(GS_CAN_IDENTIFY_OFF);
916 rc = usb_control_msg(interface_to_usbdev(dev->iface),
917 usb_sndctrlpipe(interface_to_usbdev(dev->iface), 0),
918 GS_USB_BREQ_IDENTIFY,
919 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
920 dev->channel, 0, imode, sizeof(*imode), 100);
924 return (rc > 0) ? 0 : rc;
927 /* blink LED's for finding the this interface */
928 static int gs_usb_set_phys_id(struct net_device *dev,
929 enum ethtool_phys_id_state state)
934 case ETHTOOL_ID_ACTIVE:
935 rc = gs_usb_set_identify(dev, GS_CAN_IDENTIFY_ON);
937 case ETHTOOL_ID_INACTIVE:
938 rc = gs_usb_set_identify(dev, GS_CAN_IDENTIFY_OFF);
947 static const struct ethtool_ops gs_usb_ethtool_ops = {
948 .set_phys_id = gs_usb_set_phys_id,
951 static struct gs_can *gs_make_candev(unsigned int channel,
952 struct usb_interface *intf,
953 struct gs_device_config *dconf)
956 struct net_device *netdev;
958 struct gs_device_bt_const *bt_const;
959 struct gs_device_bt_const_extended *bt_const_extended;
962 bt_const = kmalloc(sizeof(*bt_const), GFP_KERNEL);
964 return ERR_PTR(-ENOMEM);
966 /* fetch bit timing constants */
967 rc = usb_control_msg(interface_to_usbdev(intf),
968 usb_rcvctrlpipe(interface_to_usbdev(intf), 0),
969 GS_USB_BREQ_BT_CONST,
970 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
971 channel, 0, bt_const, sizeof(*bt_const), 1000);
975 "Couldn't get bit timing const for channel (err=%d)\n",
982 netdev = alloc_candev(sizeof(struct gs_can), GS_MAX_TX_URBS);
984 dev_err(&intf->dev, "Couldn't allocate candev\n");
986 return ERR_PTR(-ENOMEM);
989 dev = netdev_priv(netdev);
991 netdev->netdev_ops = &gs_usb_netdev_ops;
993 netdev->flags |= IFF_ECHO; /* we support full roundtrip echo */
996 strcpy(dev->bt_const.name, "gs_usb");
997 dev->bt_const.tseg1_min = le32_to_cpu(bt_const->tseg1_min);
998 dev->bt_const.tseg1_max = le32_to_cpu(bt_const->tseg1_max);
999 dev->bt_const.tseg2_min = le32_to_cpu(bt_const->tseg2_min);
1000 dev->bt_const.tseg2_max = le32_to_cpu(bt_const->tseg2_max);
1001 dev->bt_const.sjw_max = le32_to_cpu(bt_const->sjw_max);
1002 dev->bt_const.brp_min = le32_to_cpu(bt_const->brp_min);
1003 dev->bt_const.brp_max = le32_to_cpu(bt_const->brp_max);
1004 dev->bt_const.brp_inc = le32_to_cpu(bt_const->brp_inc);
1006 dev->udev = interface_to_usbdev(intf);
1008 dev->netdev = netdev;
1009 dev->channel = channel;
1011 init_usb_anchor(&dev->tx_submitted);
1012 atomic_set(&dev->active_tx_urbs, 0);
1013 spin_lock_init(&dev->tx_ctx_lock);
1014 for (rc = 0; rc < GS_MAX_TX_URBS; rc++) {
1015 dev->tx_context[rc].dev = dev;
1016 dev->tx_context[rc].echo_id = GS_MAX_TX_URBS;
1020 dev->can.state = CAN_STATE_STOPPED;
1021 dev->can.clock.freq = le32_to_cpu(bt_const->fclk_can);
1022 dev->can.bittiming_const = &dev->bt_const;
1023 dev->can.do_set_bittiming = gs_usb_set_bittiming;
1025 dev->can.ctrlmode_supported = CAN_CTRLMODE_CC_LEN8_DLC;
1027 feature = le32_to_cpu(bt_const->feature);
1028 dev->feature = FIELD_GET(GS_CAN_FEATURE_MASK, feature);
1029 if (feature & GS_CAN_FEATURE_LISTEN_ONLY)
1030 dev->can.ctrlmode_supported |= CAN_CTRLMODE_LISTENONLY;
1032 if (feature & GS_CAN_FEATURE_LOOP_BACK)
1033 dev->can.ctrlmode_supported |= CAN_CTRLMODE_LOOPBACK;
1035 if (feature & GS_CAN_FEATURE_TRIPLE_SAMPLE)
1036 dev->can.ctrlmode_supported |= CAN_CTRLMODE_3_SAMPLES;
1038 if (feature & GS_CAN_FEATURE_ONE_SHOT)
1039 dev->can.ctrlmode_supported |= CAN_CTRLMODE_ONE_SHOT;
1041 if (feature & GS_CAN_FEATURE_FD) {
1042 dev->can.ctrlmode_supported |= CAN_CTRLMODE_FD;
1043 /* The data bit timing will be overwritten, if
1044 * GS_CAN_FEATURE_BT_CONST_EXT is set.
1046 dev->can.data_bittiming_const = &dev->bt_const;
1047 dev->can.do_set_data_bittiming = gs_usb_set_data_bittiming;
1050 /* The CANtact Pro from LinkLayer Labs is based on the
1051 * LPC54616 µC, which is affected by the NXP LPC USB transfer
1052 * erratum. However, the current firmware (version 2) doesn't
1053 * set the GS_CAN_FEATURE_REQ_USB_QUIRK_LPC546XX bit. Set the
1054 * feature GS_CAN_FEATURE_REQ_USB_QUIRK_LPC546XX to workaround
1057 * For the GS_USB_BREQ_DATA_BITTIMING USB control message the
1058 * CANtact Pro firmware uses a request value, which is already
1059 * used by the candleLight firmware for a different purpose
1060 * (GS_USB_BREQ_GET_USER_ID). Set the feature
1061 * GS_CAN_FEATURE_QUIRK_BREQ_CANTACT_PRO to workaround this
1064 if (dev->udev->descriptor.idVendor == cpu_to_le16(USB_GSUSB_1_VENDOR_ID) &&
1065 dev->udev->descriptor.idProduct == cpu_to_le16(USB_GSUSB_1_PRODUCT_ID) &&
1066 dev->udev->manufacturer && dev->udev->product &&
1067 !strcmp(dev->udev->manufacturer, "LinkLayer Labs") &&
1068 !strcmp(dev->udev->product, "CANtact Pro") &&
1069 (le32_to_cpu(dconf->sw_version) <= 2))
1070 dev->feature |= GS_CAN_FEATURE_REQ_USB_QUIRK_LPC546XX |
1071 GS_CAN_FEATURE_QUIRK_BREQ_CANTACT_PRO;
1073 if (le32_to_cpu(dconf->sw_version) > 1)
1074 if (feature & GS_CAN_FEATURE_IDENTIFY)
1075 netdev->ethtool_ops = &gs_usb_ethtool_ops;
1079 /* fetch extended bit timing constants if device has feature
1080 * GS_CAN_FEATURE_FD and GS_CAN_FEATURE_BT_CONST_EXT
1082 if (feature & GS_CAN_FEATURE_FD &&
1083 feature & GS_CAN_FEATURE_BT_CONST_EXT) {
1084 bt_const_extended = kmalloc(sizeof(*bt_const_extended), GFP_KERNEL);
1085 if (!bt_const_extended)
1086 return ERR_PTR(-ENOMEM);
1088 rc = usb_control_msg(interface_to_usbdev(intf),
1089 usb_rcvctrlpipe(interface_to_usbdev(intf), 0),
1090 GS_USB_BREQ_BT_CONST_EXT,
1091 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
1092 channel, 0, bt_const_extended,
1093 sizeof(*bt_const_extended),
1097 "Couldn't get extended bit timing const for channel (err=%d)\n",
1099 kfree(bt_const_extended);
1103 strcpy(dev->data_bt_const.name, "gs_usb");
1104 dev->data_bt_const.tseg1_min = le32_to_cpu(bt_const_extended->dtseg1_min);
1105 dev->data_bt_const.tseg1_max = le32_to_cpu(bt_const_extended->dtseg1_max);
1106 dev->data_bt_const.tseg2_min = le32_to_cpu(bt_const_extended->dtseg2_min);
1107 dev->data_bt_const.tseg2_max = le32_to_cpu(bt_const_extended->dtseg2_max);
1108 dev->data_bt_const.sjw_max = le32_to_cpu(bt_const_extended->dsjw_max);
1109 dev->data_bt_const.brp_min = le32_to_cpu(bt_const_extended->dbrp_min);
1110 dev->data_bt_const.brp_max = le32_to_cpu(bt_const_extended->dbrp_max);
1111 dev->data_bt_const.brp_inc = le32_to_cpu(bt_const_extended->dbrp_inc);
1113 dev->can.data_bittiming_const = &dev->data_bt_const;
1115 kfree(bt_const_extended);
1118 SET_NETDEV_DEV(netdev, &intf->dev);
1120 rc = register_candev(dev->netdev);
1122 free_candev(dev->netdev);
1123 dev_err(&intf->dev, "Couldn't register candev (err=%d)\n", rc);
1130 static void gs_destroy_candev(struct gs_can *dev)
1132 unregister_candev(dev->netdev);
1133 usb_kill_anchored_urbs(&dev->tx_submitted);
1134 free_candev(dev->netdev);
1137 static int gs_usb_probe(struct usb_interface *intf,
1138 const struct usb_device_id *id)
1140 struct usb_device *udev = interface_to_usbdev(intf);
1141 struct gs_host_frame *hf;
1144 unsigned int icount, i;
1145 struct gs_host_config *hconf;
1146 struct gs_device_config *dconf;
1148 hconf = kmalloc(sizeof(*hconf), GFP_KERNEL);
1152 hconf->byte_order = cpu_to_le32(0x0000beef);
1154 /* send host config */
1155 rc = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
1156 GS_USB_BREQ_HOST_FORMAT,
1157 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
1158 1, intf->cur_altsetting->desc.bInterfaceNumber,
1159 hconf, sizeof(*hconf), 1000);
1164 dev_err(&intf->dev, "Couldn't send data format (err=%d)\n", rc);
1168 dconf = kmalloc(sizeof(*dconf), GFP_KERNEL);
1172 /* read device config */
1173 rc = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
1174 GS_USB_BREQ_DEVICE_CONFIG,
1175 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
1176 1, intf->cur_altsetting->desc.bInterfaceNumber,
1177 dconf, sizeof(*dconf), 1000);
1179 dev_err(&intf->dev, "Couldn't get device config: (err=%d)\n",
1185 icount = dconf->icount + 1;
1186 dev_info(&intf->dev, "Configuring for %u interfaces\n", icount);
1188 if (icount > GS_MAX_INTF) {
1190 "Driver cannot handle more that %u CAN interfaces\n",
1196 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1202 init_usb_anchor(&dev->rx_submitted);
1203 /* default to classic CAN, switch to CAN-FD if at least one of
1204 * our channels support CAN-FD.
1206 dev->hf_size_rx = struct_size(hf, classic_can, 1);
1208 usb_set_intfdata(intf, dev);
1211 for (i = 0; i < icount; i++) {
1212 dev->canch[i] = gs_make_candev(i, intf, dconf);
1213 if (IS_ERR_OR_NULL(dev->canch[i])) {
1214 /* save error code to return later */
1215 rc = PTR_ERR(dev->canch[i]);
1217 /* on failure destroy previously created candevs */
1219 for (i = 0; i < icount; i++)
1220 gs_destroy_candev(dev->canch[i]);
1222 usb_kill_anchored_urbs(&dev->rx_submitted);
1227 dev->canch[i]->parent = dev;
1229 if (dev->canch[i]->can.ctrlmode_supported & CAN_CTRLMODE_FD)
1230 dev->hf_size_rx = struct_size(hf, canfd, 1);
1238 static void gs_usb_disconnect(struct usb_interface *intf)
1240 struct gs_usb *dev = usb_get_intfdata(intf);
1243 usb_set_intfdata(intf, NULL);
1246 dev_err(&intf->dev, "Disconnect (nodata)\n");
1250 for (i = 0; i < GS_MAX_INTF; i++)
1252 gs_destroy_candev(dev->canch[i]);
1254 usb_kill_anchored_urbs(&dev->rx_submitted);
1258 static const struct usb_device_id gs_usb_table[] = {
1259 { USB_DEVICE_INTERFACE_NUMBER(USB_GSUSB_1_VENDOR_ID,
1260 USB_GSUSB_1_PRODUCT_ID, 0) },
1261 { USB_DEVICE_INTERFACE_NUMBER(USB_CANDLELIGHT_VENDOR_ID,
1262 USB_CANDLELIGHT_PRODUCT_ID, 0) },
1263 { USB_DEVICE_INTERFACE_NUMBER(USB_CES_CANEXT_FD_VENDOR_ID,
1264 USB_CES_CANEXT_FD_PRODUCT_ID, 0) },
1265 { USB_DEVICE_INTERFACE_NUMBER(USB_ABE_CANDEBUGGER_FD_VENDOR_ID,
1266 USB_ABE_CANDEBUGGER_FD_PRODUCT_ID, 0) },
1267 {} /* Terminating entry */
1270 MODULE_DEVICE_TABLE(usb, gs_usb_table);
1272 static struct usb_driver gs_usb_driver = {
1274 .probe = gs_usb_probe,
1275 .disconnect = gs_usb_disconnect,
1276 .id_table = gs_usb_table,
1279 module_usb_driver(gs_usb_driver);
1281 MODULE_AUTHOR("Maximilian Schneider <mws@schneidersoft.net>");
1283 "Socket CAN device driver for Geschwister Schneider Technologie-, "
1284 "Entwicklungs- und Vertriebs UG. USB2.0 to CAN interfaces\n"
1285 "and bytewerk.org candleLight USB CAN interfaces.");
1286 MODULE_LICENSE("GPL v2");