Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /* CAN driver for Geschwister Schneider USB/CAN devices
0003  * and bytewerk.org candleLight USB CAN interfaces.
0004  *
0005  * Copyright (C) 2013-2016 Geschwister Schneider Technologie-,
0006  * Entwicklungs- und Vertriebs UG (Haftungsbeschränkt).
0007  * Copyright (C) 2016 Hubert Denkmair
0008  *
0009  * Many thanks to all socketcan devs!
0010  */
0011 
0012 #include <linux/bitfield.h>
0013 #include <linux/ethtool.h>
0014 #include <linux/init.h>
0015 #include <linux/module.h>
0016 #include <linux/netdevice.h>
0017 #include <linux/signal.h>
0018 #include <linux/usb.h>
0019 
0020 #include <linux/can.h>
0021 #include <linux/can/dev.h>
0022 #include <linux/can/error.h>
0023 
0024 /* Device specific constants */
0025 #define USB_GSUSB_1_VENDOR_ID 0x1d50
0026 #define USB_GSUSB_1_PRODUCT_ID 0x606f
0027 
0028 #define USB_CANDLELIGHT_VENDOR_ID 0x1209
0029 #define USB_CANDLELIGHT_PRODUCT_ID 0x2323
0030 
0031 #define USB_CES_CANEXT_FD_VENDOR_ID 0x1cd2
0032 #define USB_CES_CANEXT_FD_PRODUCT_ID 0x606f
0033 
0034 #define USB_ABE_CANDEBUGGER_FD_VENDOR_ID 0x16d0
0035 #define USB_ABE_CANDEBUGGER_FD_PRODUCT_ID 0x10b8
0036 
0037 #define GSUSB_ENDPOINT_IN 1
0038 #define GSUSB_ENDPOINT_OUT 2
0039 
0040 /* Device specific constants */
0041 enum gs_usb_breq {
0042     GS_USB_BREQ_HOST_FORMAT = 0,
0043     GS_USB_BREQ_BITTIMING,
0044     GS_USB_BREQ_MODE,
0045     GS_USB_BREQ_BERR,
0046     GS_USB_BREQ_BT_CONST,
0047     GS_USB_BREQ_DEVICE_CONFIG,
0048     GS_USB_BREQ_TIMESTAMP,
0049     GS_USB_BREQ_IDENTIFY,
0050     GS_USB_BREQ_GET_USER_ID,
0051     GS_USB_BREQ_QUIRK_CANTACT_PRO_DATA_BITTIMING = GS_USB_BREQ_GET_USER_ID,
0052     GS_USB_BREQ_SET_USER_ID,
0053     GS_USB_BREQ_DATA_BITTIMING,
0054     GS_USB_BREQ_BT_CONST_EXT,
0055 };
0056 
0057 enum gs_can_mode {
0058     /* reset a channel. turns it off */
0059     GS_CAN_MODE_RESET = 0,
0060     /* starts a channel */
0061     GS_CAN_MODE_START
0062 };
0063 
0064 enum gs_can_state {
0065     GS_CAN_STATE_ERROR_ACTIVE = 0,
0066     GS_CAN_STATE_ERROR_WARNING,
0067     GS_CAN_STATE_ERROR_PASSIVE,
0068     GS_CAN_STATE_BUS_OFF,
0069     GS_CAN_STATE_STOPPED,
0070     GS_CAN_STATE_SLEEPING
0071 };
0072 
0073 enum gs_can_identify_mode {
0074     GS_CAN_IDENTIFY_OFF = 0,
0075     GS_CAN_IDENTIFY_ON
0076 };
0077 
0078 /* data types passed between host and device */
0079 
0080 /* The firmware on the original USB2CAN by Geschwister Schneider
0081  * Technologie Entwicklungs- und Vertriebs UG exchanges all data
0082  * between the host and the device in host byte order. This is done
0083  * with the struct gs_host_config::byte_order member, which is sent
0084  * first to indicate the desired byte order.
0085  *
0086  * The widely used open source firmware candleLight doesn't support
0087  * this feature and exchanges the data in little endian byte order.
0088  */
0089 struct gs_host_config {
0090     __le32 byte_order;
0091 } __packed;
0092 
0093 struct gs_device_config {
0094     u8 reserved1;
0095     u8 reserved2;
0096     u8 reserved3;
0097     u8 icount;
0098     __le32 sw_version;
0099     __le32 hw_version;
0100 } __packed;
0101 
0102 #define GS_CAN_MODE_NORMAL 0
0103 #define GS_CAN_MODE_LISTEN_ONLY BIT(0)
0104 #define GS_CAN_MODE_LOOP_BACK BIT(1)
0105 #define GS_CAN_MODE_TRIPLE_SAMPLE BIT(2)
0106 #define GS_CAN_MODE_ONE_SHOT BIT(3)
0107 #define GS_CAN_MODE_HW_TIMESTAMP BIT(4)
0108 /* GS_CAN_FEATURE_IDENTIFY BIT(5) */
0109 /* GS_CAN_FEATURE_USER_ID BIT(6) */
0110 #define GS_CAN_MODE_PAD_PKTS_TO_MAX_PKT_SIZE BIT(7)
0111 #define GS_CAN_MODE_FD BIT(8)
0112 /* GS_CAN_FEATURE_REQ_USB_QUIRK_LPC546XX BIT(9) */
0113 /* GS_CAN_FEATURE_BT_CONST_EXT BIT(10) */
0114 
0115 struct gs_device_mode {
0116     __le32 mode;
0117     __le32 flags;
0118 } __packed;
0119 
0120 struct gs_device_state {
0121     __le32 state;
0122     __le32 rxerr;
0123     __le32 txerr;
0124 } __packed;
0125 
0126 struct gs_device_bittiming {
0127     __le32 prop_seg;
0128     __le32 phase_seg1;
0129     __le32 phase_seg2;
0130     __le32 sjw;
0131     __le32 brp;
0132 } __packed;
0133 
0134 struct gs_identify_mode {
0135     __le32 mode;
0136 } __packed;
0137 
0138 #define GS_CAN_FEATURE_LISTEN_ONLY BIT(0)
0139 #define GS_CAN_FEATURE_LOOP_BACK BIT(1)
0140 #define GS_CAN_FEATURE_TRIPLE_SAMPLE BIT(2)
0141 #define GS_CAN_FEATURE_ONE_SHOT BIT(3)
0142 #define GS_CAN_FEATURE_HW_TIMESTAMP BIT(4)
0143 #define GS_CAN_FEATURE_IDENTIFY BIT(5)
0144 #define GS_CAN_FEATURE_USER_ID BIT(6)
0145 #define GS_CAN_FEATURE_PAD_PKTS_TO_MAX_PKT_SIZE BIT(7)
0146 #define GS_CAN_FEATURE_FD BIT(8)
0147 #define GS_CAN_FEATURE_REQ_USB_QUIRK_LPC546XX BIT(9)
0148 #define GS_CAN_FEATURE_BT_CONST_EXT BIT(10)
0149 #define GS_CAN_FEATURE_MASK GENMASK(10, 0)
0150 
0151 /* internal quirks - keep in GS_CAN_FEATURE space for now */
0152 
0153 /* CANtact Pro original firmware:
0154  * BREQ DATA_BITTIMING overlaps with GET_USER_ID
0155  */
0156 #define GS_CAN_FEATURE_QUIRK_BREQ_CANTACT_PRO BIT(31)
0157 
0158 struct gs_device_bt_const {
0159     __le32 feature;
0160     __le32 fclk_can;
0161     __le32 tseg1_min;
0162     __le32 tseg1_max;
0163     __le32 tseg2_min;
0164     __le32 tseg2_max;
0165     __le32 sjw_max;
0166     __le32 brp_min;
0167     __le32 brp_max;
0168     __le32 brp_inc;
0169 } __packed;
0170 
0171 struct gs_device_bt_const_extended {
0172     __le32 feature;
0173     __le32 fclk_can;
0174     __le32 tseg1_min;
0175     __le32 tseg1_max;
0176     __le32 tseg2_min;
0177     __le32 tseg2_max;
0178     __le32 sjw_max;
0179     __le32 brp_min;
0180     __le32 brp_max;
0181     __le32 brp_inc;
0182 
0183     __le32 dtseg1_min;
0184     __le32 dtseg1_max;
0185     __le32 dtseg2_min;
0186     __le32 dtseg2_max;
0187     __le32 dsjw_max;
0188     __le32 dbrp_min;
0189     __le32 dbrp_max;
0190     __le32 dbrp_inc;
0191 } __packed;
0192 
0193 #define GS_CAN_FLAG_OVERFLOW BIT(0)
0194 #define GS_CAN_FLAG_FD BIT(1)
0195 #define GS_CAN_FLAG_BRS BIT(2)
0196 #define GS_CAN_FLAG_ESI BIT(3)
0197 
0198 struct classic_can {
0199     u8 data[8];
0200 } __packed;
0201 
0202 struct classic_can_quirk {
0203     u8 data[8];
0204     u8 quirk;
0205 } __packed;
0206 
0207 struct canfd {
0208     u8 data[64];
0209 } __packed;
0210 
0211 struct canfd_quirk {
0212     u8 data[64];
0213     u8 quirk;
0214 } __packed;
0215 
0216 struct gs_host_frame {
0217     u32 echo_id;
0218     __le32 can_id;
0219 
0220     u8 can_dlc;
0221     u8 channel;
0222     u8 flags;
0223     u8 reserved;
0224 
0225     union {
0226         DECLARE_FLEX_ARRAY(struct classic_can, classic_can);
0227         DECLARE_FLEX_ARRAY(struct classic_can_quirk, classic_can_quirk);
0228         DECLARE_FLEX_ARRAY(struct canfd, canfd);
0229         DECLARE_FLEX_ARRAY(struct canfd_quirk, canfd_quirk);
0230     };
0231 } __packed;
0232 /* The GS USB devices make use of the same flags and masks as in
0233  * linux/can.h and linux/can/error.h, and no additional mapping is necessary.
0234  */
0235 
0236 /* Only send a max of GS_MAX_TX_URBS frames per channel at a time. */
0237 #define GS_MAX_TX_URBS 10
0238 /* Only launch a max of GS_MAX_RX_URBS usb requests at a time. */
0239 #define GS_MAX_RX_URBS 30
0240 /* Maximum number of interfaces the driver supports per device.
0241  * Current hardware only supports 3 interfaces. The future may vary.
0242  */
0243 #define GS_MAX_INTF 3
0244 
0245 struct gs_tx_context {
0246     struct gs_can *dev;
0247     unsigned int echo_id;
0248 };
0249 
0250 struct gs_can {
0251     struct can_priv can; /* must be the first member */
0252 
0253     struct gs_usb *parent;
0254 
0255     struct net_device *netdev;
0256     struct usb_device *udev;
0257     struct usb_interface *iface;
0258 
0259     struct can_bittiming_const bt_const, data_bt_const;
0260     unsigned int channel;   /* channel number */
0261 
0262     u32 feature;
0263     unsigned int hf_size_tx;
0264 
0265     /* This lock prevents a race condition between xmit and receive. */
0266     spinlock_t tx_ctx_lock;
0267     struct gs_tx_context tx_context[GS_MAX_TX_URBS];
0268 
0269     struct usb_anchor tx_submitted;
0270     atomic_t active_tx_urbs;
0271     void *rxbuf[GS_MAX_RX_URBS];
0272     dma_addr_t rxbuf_dma[GS_MAX_RX_URBS];
0273 };
0274 
0275 /* usb interface struct */
0276 struct gs_usb {
0277     struct gs_can *canch[GS_MAX_INTF];
0278     struct usb_anchor rx_submitted;
0279     struct usb_device *udev;
0280     unsigned int hf_size_rx;
0281     u8 active_channels;
0282 };
0283 
0284 /* 'allocate' a tx context.
0285  * returns a valid tx context or NULL if there is no space.
0286  */
0287 static struct gs_tx_context *gs_alloc_tx_context(struct gs_can *dev)
0288 {
0289     int i = 0;
0290     unsigned long flags;
0291 
0292     spin_lock_irqsave(&dev->tx_ctx_lock, flags);
0293 
0294     for (; i < GS_MAX_TX_URBS; i++) {
0295         if (dev->tx_context[i].echo_id == GS_MAX_TX_URBS) {
0296             dev->tx_context[i].echo_id = i;
0297             spin_unlock_irqrestore(&dev->tx_ctx_lock, flags);
0298             return &dev->tx_context[i];
0299         }
0300     }
0301 
0302     spin_unlock_irqrestore(&dev->tx_ctx_lock, flags);
0303     return NULL;
0304 }
0305 
0306 /* releases a tx context
0307  */
0308 static void gs_free_tx_context(struct gs_tx_context *txc)
0309 {
0310     txc->echo_id = GS_MAX_TX_URBS;
0311 }
0312 
0313 /* Get a tx context by id.
0314  */
0315 static struct gs_tx_context *gs_get_tx_context(struct gs_can *dev,
0316                            unsigned int id)
0317 {
0318     unsigned long flags;
0319 
0320     if (id < GS_MAX_TX_URBS) {
0321         spin_lock_irqsave(&dev->tx_ctx_lock, flags);
0322         if (dev->tx_context[id].echo_id == id) {
0323             spin_unlock_irqrestore(&dev->tx_ctx_lock, flags);
0324             return &dev->tx_context[id];
0325         }
0326         spin_unlock_irqrestore(&dev->tx_ctx_lock, flags);
0327     }
0328     return NULL;
0329 }
0330 
0331 static int gs_cmd_reset(struct gs_can *gsdev)
0332 {
0333     struct gs_device_mode *dm;
0334     struct usb_interface *intf = gsdev->iface;
0335     int rc;
0336 
0337     dm = kzalloc(sizeof(*dm), GFP_KERNEL);
0338     if (!dm)
0339         return -ENOMEM;
0340 
0341     dm->mode = GS_CAN_MODE_RESET;
0342 
0343     rc = usb_control_msg(interface_to_usbdev(intf),
0344                  usb_sndctrlpipe(interface_to_usbdev(intf), 0),
0345                  GS_USB_BREQ_MODE,
0346                  USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
0347                  gsdev->channel, 0, dm, sizeof(*dm), 1000);
0348 
0349     kfree(dm);
0350 
0351     return rc;
0352 }
0353 
0354 static void gs_update_state(struct gs_can *dev, struct can_frame *cf)
0355 {
0356     struct can_device_stats *can_stats = &dev->can.can_stats;
0357 
0358     if (cf->can_id & CAN_ERR_RESTARTED) {
0359         dev->can.state = CAN_STATE_ERROR_ACTIVE;
0360         can_stats->restarts++;
0361     } else if (cf->can_id & CAN_ERR_BUSOFF) {
0362         dev->can.state = CAN_STATE_BUS_OFF;
0363         can_stats->bus_off++;
0364     } else if (cf->can_id & CAN_ERR_CRTL) {
0365         if ((cf->data[1] & CAN_ERR_CRTL_TX_WARNING) ||
0366             (cf->data[1] & CAN_ERR_CRTL_RX_WARNING)) {
0367             dev->can.state = CAN_STATE_ERROR_WARNING;
0368             can_stats->error_warning++;
0369         } else if ((cf->data[1] & CAN_ERR_CRTL_TX_PASSIVE) ||
0370                (cf->data[1] & CAN_ERR_CRTL_RX_PASSIVE)) {
0371             dev->can.state = CAN_STATE_ERROR_PASSIVE;
0372             can_stats->error_passive++;
0373         } else {
0374             dev->can.state = CAN_STATE_ERROR_ACTIVE;
0375         }
0376     }
0377 }
0378 
0379 static void gs_usb_receive_bulk_callback(struct urb *urb)
0380 {
0381     struct gs_usb *usbcan = urb->context;
0382     struct gs_can *dev;
0383     struct net_device *netdev;
0384     int rc;
0385     struct net_device_stats *stats;
0386     struct gs_host_frame *hf = urb->transfer_buffer;
0387     struct gs_tx_context *txc;
0388     struct can_frame *cf;
0389     struct canfd_frame *cfd;
0390     struct sk_buff *skb;
0391 
0392     BUG_ON(!usbcan);
0393 
0394     switch (urb->status) {
0395     case 0: /* success */
0396         break;
0397     case -ENOENT:
0398     case -ESHUTDOWN:
0399         return;
0400     default:
0401         /* do not resubmit aborted urbs. eg: when device goes down */
0402         return;
0403     }
0404 
0405     /* device reports out of range channel id */
0406     if (hf->channel >= GS_MAX_INTF)
0407         goto device_detach;
0408 
0409     dev = usbcan->canch[hf->channel];
0410 
0411     netdev = dev->netdev;
0412     stats = &netdev->stats;
0413 
0414     if (!netif_device_present(netdev))
0415         return;
0416 
0417     if (hf->echo_id == -1) { /* normal rx */
0418         if (hf->flags & GS_CAN_FLAG_FD) {
0419             skb = alloc_canfd_skb(dev->netdev, &cfd);
0420             if (!skb)
0421                 return;
0422 
0423             cfd->can_id = le32_to_cpu(hf->can_id);
0424             cfd->len = can_fd_dlc2len(hf->can_dlc);
0425             if (hf->flags & GS_CAN_FLAG_BRS)
0426                 cfd->flags |= CANFD_BRS;
0427             if (hf->flags & GS_CAN_FLAG_ESI)
0428                 cfd->flags |= CANFD_ESI;
0429 
0430             memcpy(cfd->data, hf->canfd->data, cfd->len);
0431         } else {
0432             skb = alloc_can_skb(dev->netdev, &cf);
0433             if (!skb)
0434                 return;
0435 
0436             cf->can_id = le32_to_cpu(hf->can_id);
0437             can_frame_set_cc_len(cf, hf->can_dlc, dev->can.ctrlmode);
0438 
0439             memcpy(cf->data, hf->classic_can->data, 8);
0440 
0441             /* ERROR frames tell us information about the controller */
0442             if (le32_to_cpu(hf->can_id) & CAN_ERR_FLAG)
0443                 gs_update_state(dev, cf);
0444         }
0445 
0446         netdev->stats.rx_packets++;
0447         netdev->stats.rx_bytes += hf->can_dlc;
0448 
0449         netif_rx(skb);
0450     } else { /* echo_id == hf->echo_id */
0451         if (hf->echo_id >= GS_MAX_TX_URBS) {
0452             netdev_err(netdev,
0453                    "Unexpected out of range echo id %u\n",
0454                    hf->echo_id);
0455             goto resubmit_urb;
0456         }
0457 
0458         txc = gs_get_tx_context(dev, hf->echo_id);
0459 
0460         /* bad devices send bad echo_ids. */
0461         if (!txc) {
0462             netdev_err(netdev,
0463                    "Unexpected unused echo id %u\n",
0464                    hf->echo_id);
0465             goto resubmit_urb;
0466         }
0467 
0468         netdev->stats.tx_packets++;
0469         netdev->stats.tx_bytes += can_get_echo_skb(netdev, hf->echo_id,
0470                                NULL);
0471 
0472         gs_free_tx_context(txc);
0473 
0474         atomic_dec(&dev->active_tx_urbs);
0475 
0476         netif_wake_queue(netdev);
0477     }
0478 
0479     if (hf->flags & GS_CAN_FLAG_OVERFLOW) {
0480         skb = alloc_can_err_skb(netdev, &cf);
0481         if (!skb)
0482             goto resubmit_urb;
0483 
0484         cf->can_id |= CAN_ERR_CRTL;
0485         cf->len = CAN_ERR_DLC;
0486         cf->data[1] = CAN_ERR_CRTL_RX_OVERFLOW;
0487         stats->rx_over_errors++;
0488         stats->rx_errors++;
0489         netif_rx(skb);
0490     }
0491 
0492  resubmit_urb:
0493     usb_fill_bulk_urb(urb, usbcan->udev,
0494               usb_rcvbulkpipe(usbcan->udev, GSUSB_ENDPOINT_IN),
0495               hf, dev->parent->hf_size_rx,
0496               gs_usb_receive_bulk_callback, usbcan);
0497 
0498     rc = usb_submit_urb(urb, GFP_ATOMIC);
0499 
0500     /* USB failure take down all interfaces */
0501     if (rc == -ENODEV) {
0502  device_detach:
0503         for (rc = 0; rc < GS_MAX_INTF; rc++) {
0504             if (usbcan->canch[rc])
0505                 netif_device_detach(usbcan->canch[rc]->netdev);
0506         }
0507     }
0508 }
0509 
0510 static int gs_usb_set_bittiming(struct net_device *netdev)
0511 {
0512     struct gs_can *dev = netdev_priv(netdev);
0513     struct can_bittiming *bt = &dev->can.bittiming;
0514     struct usb_interface *intf = dev->iface;
0515     int rc;
0516     struct gs_device_bittiming *dbt;
0517 
0518     dbt = kmalloc(sizeof(*dbt), GFP_KERNEL);
0519     if (!dbt)
0520         return -ENOMEM;
0521 
0522     dbt->prop_seg = cpu_to_le32(bt->prop_seg);
0523     dbt->phase_seg1 = cpu_to_le32(bt->phase_seg1);
0524     dbt->phase_seg2 = cpu_to_le32(bt->phase_seg2);
0525     dbt->sjw = cpu_to_le32(bt->sjw);
0526     dbt->brp = cpu_to_le32(bt->brp);
0527 
0528     /* request bit timings */
0529     rc = usb_control_msg(interface_to_usbdev(intf),
0530                  usb_sndctrlpipe(interface_to_usbdev(intf), 0),
0531                  GS_USB_BREQ_BITTIMING,
0532                  USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
0533                  dev->channel, 0, dbt, sizeof(*dbt), 1000);
0534 
0535     kfree(dbt);
0536 
0537     if (rc < 0)
0538         dev_err(netdev->dev.parent, "Couldn't set bittimings (err=%d)",
0539             rc);
0540 
0541     return (rc > 0) ? 0 : rc;
0542 }
0543 
0544 static int gs_usb_set_data_bittiming(struct net_device *netdev)
0545 {
0546     struct gs_can *dev = netdev_priv(netdev);
0547     struct can_bittiming *bt = &dev->can.data_bittiming;
0548     struct usb_interface *intf = dev->iface;
0549     struct gs_device_bittiming *dbt;
0550     u8 request = GS_USB_BREQ_DATA_BITTIMING;
0551     int rc;
0552 
0553     dbt = kmalloc(sizeof(*dbt), GFP_KERNEL);
0554     if (!dbt)
0555         return -ENOMEM;
0556 
0557     dbt->prop_seg = cpu_to_le32(bt->prop_seg);
0558     dbt->phase_seg1 = cpu_to_le32(bt->phase_seg1);
0559     dbt->phase_seg2 = cpu_to_le32(bt->phase_seg2);
0560     dbt->sjw = cpu_to_le32(bt->sjw);
0561     dbt->brp = cpu_to_le32(bt->brp);
0562 
0563     if (dev->feature & GS_CAN_FEATURE_QUIRK_BREQ_CANTACT_PRO)
0564         request = GS_USB_BREQ_QUIRK_CANTACT_PRO_DATA_BITTIMING;
0565 
0566     /* request bit timings */
0567     rc = usb_control_msg(interface_to_usbdev(intf),
0568                  usb_sndctrlpipe(interface_to_usbdev(intf), 0),
0569                  request,
0570                  USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
0571                  dev->channel, 0, dbt, sizeof(*dbt), 1000);
0572 
0573     kfree(dbt);
0574 
0575     if (rc < 0)
0576         dev_err(netdev->dev.parent,
0577             "Couldn't set data bittimings (err=%d)", rc);
0578 
0579     return (rc > 0) ? 0 : rc;
0580 }
0581 
0582 static void gs_usb_xmit_callback(struct urb *urb)
0583 {
0584     struct gs_tx_context *txc = urb->context;
0585     struct gs_can *dev = txc->dev;
0586     struct net_device *netdev = dev->netdev;
0587 
0588     if (urb->status)
0589         netdev_info(netdev, "usb xmit fail %u\n", txc->echo_id);
0590 
0591     usb_free_coherent(urb->dev, urb->transfer_buffer_length,
0592               urb->transfer_buffer, urb->transfer_dma);
0593 }
0594 
0595 static netdev_tx_t gs_can_start_xmit(struct sk_buff *skb,
0596                      struct net_device *netdev)
0597 {
0598     struct gs_can *dev = netdev_priv(netdev);
0599     struct net_device_stats *stats = &dev->netdev->stats;
0600     struct urb *urb;
0601     struct gs_host_frame *hf;
0602     struct can_frame *cf;
0603     struct canfd_frame *cfd;
0604     int rc;
0605     unsigned int idx;
0606     struct gs_tx_context *txc;
0607 
0608     if (can_dropped_invalid_skb(netdev, skb))
0609         return NETDEV_TX_OK;
0610 
0611     /* find an empty context to keep track of transmission */
0612     txc = gs_alloc_tx_context(dev);
0613     if (!txc)
0614         return NETDEV_TX_BUSY;
0615 
0616     /* create a URB, and a buffer for it */
0617     urb = usb_alloc_urb(0, GFP_ATOMIC);
0618     if (!urb)
0619         goto nomem_urb;
0620 
0621     hf = usb_alloc_coherent(dev->udev, dev->hf_size_tx, GFP_ATOMIC,
0622                 &urb->transfer_dma);
0623     if (!hf) {
0624         netdev_err(netdev, "No memory left for USB buffer\n");
0625         goto nomem_hf;
0626     }
0627 
0628     idx = txc->echo_id;
0629 
0630     if (idx >= GS_MAX_TX_URBS) {
0631         netdev_err(netdev, "Invalid tx context %u\n", idx);
0632         goto badidx;
0633     }
0634 
0635     hf->echo_id = idx;
0636     hf->channel = dev->channel;
0637     hf->flags = 0;
0638     hf->reserved = 0;
0639 
0640     if (can_is_canfd_skb(skb)) {
0641         cfd = (struct canfd_frame *)skb->data;
0642 
0643         hf->can_id = cpu_to_le32(cfd->can_id);
0644         hf->can_dlc = can_fd_len2dlc(cfd->len);
0645         hf->flags |= GS_CAN_FLAG_FD;
0646         if (cfd->flags & CANFD_BRS)
0647             hf->flags |= GS_CAN_FLAG_BRS;
0648         if (cfd->flags & CANFD_ESI)
0649             hf->flags |= GS_CAN_FLAG_ESI;
0650 
0651         memcpy(hf->canfd->data, cfd->data, cfd->len);
0652     } else {
0653         cf = (struct can_frame *)skb->data;
0654 
0655         hf->can_id = cpu_to_le32(cf->can_id);
0656         hf->can_dlc = can_get_cc_dlc(cf, dev->can.ctrlmode);
0657 
0658         memcpy(hf->classic_can->data, cf->data, cf->len);
0659     }
0660 
0661     usb_fill_bulk_urb(urb, dev->udev,
0662               usb_sndbulkpipe(dev->udev, GSUSB_ENDPOINT_OUT),
0663               hf, dev->hf_size_tx,
0664               gs_usb_xmit_callback, txc);
0665 
0666     urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
0667     usb_anchor_urb(urb, &dev->tx_submitted);
0668 
0669     can_put_echo_skb(skb, netdev, idx, 0);
0670 
0671     atomic_inc(&dev->active_tx_urbs);
0672 
0673     rc = usb_submit_urb(urb, GFP_ATOMIC);
0674     if (unlikely(rc)) {         /* usb send failed */
0675         atomic_dec(&dev->active_tx_urbs);
0676 
0677         can_free_echo_skb(netdev, idx, NULL);
0678         gs_free_tx_context(txc);
0679 
0680         usb_unanchor_urb(urb);
0681         usb_free_coherent(dev->udev, urb->transfer_buffer_length,
0682                   urb->transfer_buffer, urb->transfer_dma);
0683 
0684         if (rc == -ENODEV) {
0685             netif_device_detach(netdev);
0686         } else {
0687             netdev_err(netdev, "usb_submit failed (err=%d)\n", rc);
0688             stats->tx_dropped++;
0689         }
0690     } else {
0691         /* Slow down tx path */
0692         if (atomic_read(&dev->active_tx_urbs) >= GS_MAX_TX_URBS)
0693             netif_stop_queue(netdev);
0694     }
0695 
0696     /* let usb core take care of this urb */
0697     usb_free_urb(urb);
0698 
0699     return NETDEV_TX_OK;
0700 
0701  badidx:
0702     usb_free_coherent(dev->udev, urb->transfer_buffer_length,
0703               urb->transfer_buffer, urb->transfer_dma);
0704  nomem_hf:
0705     usb_free_urb(urb);
0706 
0707  nomem_urb:
0708     gs_free_tx_context(txc);
0709     dev_kfree_skb(skb);
0710     stats->tx_dropped++;
0711     return NETDEV_TX_OK;
0712 }
0713 
0714 static int gs_can_open(struct net_device *netdev)
0715 {
0716     struct gs_can *dev = netdev_priv(netdev);
0717     struct gs_usb *parent = dev->parent;
0718     int rc, i;
0719     struct gs_device_mode *dm;
0720     struct gs_host_frame *hf;
0721     u32 ctrlmode;
0722     u32 flags = 0;
0723 
0724     rc = open_candev(netdev);
0725     if (rc)
0726         return rc;
0727 
0728     ctrlmode = dev->can.ctrlmode;
0729     if (ctrlmode & CAN_CTRLMODE_FD) {
0730         flags |= GS_CAN_MODE_FD;
0731 
0732         if (dev->feature & GS_CAN_FEATURE_REQ_USB_QUIRK_LPC546XX)
0733             dev->hf_size_tx = struct_size(hf, canfd_quirk, 1);
0734         else
0735             dev->hf_size_tx = struct_size(hf, canfd, 1);
0736     } else {
0737         if (dev->feature & GS_CAN_FEATURE_REQ_USB_QUIRK_LPC546XX)
0738             dev->hf_size_tx = struct_size(hf, classic_can_quirk, 1);
0739         else
0740             dev->hf_size_tx = struct_size(hf, classic_can, 1);
0741     }
0742 
0743     if (!parent->active_channels) {
0744         for (i = 0; i < GS_MAX_RX_URBS; i++) {
0745             struct urb *urb;
0746             u8 *buf;
0747             dma_addr_t buf_dma;
0748 
0749             /* alloc rx urb */
0750             urb = usb_alloc_urb(0, GFP_KERNEL);
0751             if (!urb)
0752                 return -ENOMEM;
0753 
0754             /* alloc rx buffer */
0755             buf = usb_alloc_coherent(dev->udev,
0756                          dev->parent->hf_size_rx,
0757                          GFP_KERNEL,
0758                          &buf_dma);
0759             if (!buf) {
0760                 netdev_err(netdev,
0761                        "No memory left for USB buffer\n");
0762                 usb_free_urb(urb);
0763                 return -ENOMEM;
0764             }
0765 
0766             urb->transfer_dma = buf_dma;
0767 
0768             /* fill, anchor, and submit rx urb */
0769             usb_fill_bulk_urb(urb,
0770                       dev->udev,
0771                       usb_rcvbulkpipe(dev->udev,
0772                               GSUSB_ENDPOINT_IN),
0773                       buf,
0774                       dev->parent->hf_size_rx,
0775                       gs_usb_receive_bulk_callback, parent);
0776             urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
0777 
0778             usb_anchor_urb(urb, &parent->rx_submitted);
0779 
0780             rc = usb_submit_urb(urb, GFP_KERNEL);
0781             if (rc) {
0782                 if (rc == -ENODEV)
0783                     netif_device_detach(dev->netdev);
0784 
0785                 netdev_err(netdev,
0786                        "usb_submit failed (err=%d)\n", rc);
0787 
0788                 usb_unanchor_urb(urb);
0789                 usb_free_coherent(dev->udev,
0790                           sizeof(struct gs_host_frame),
0791                           buf,
0792                           buf_dma);
0793                 usb_free_urb(urb);
0794                 break;
0795             }
0796 
0797             dev->rxbuf[i] = buf;
0798             dev->rxbuf_dma[i] = buf_dma;
0799 
0800             /* Drop reference,
0801              * USB core will take care of freeing it
0802              */
0803             usb_free_urb(urb);
0804         }
0805     }
0806 
0807     dm = kmalloc(sizeof(*dm), GFP_KERNEL);
0808     if (!dm)
0809         return -ENOMEM;
0810 
0811     /* flags */
0812     if (ctrlmode & CAN_CTRLMODE_LOOPBACK)
0813         flags |= GS_CAN_MODE_LOOP_BACK;
0814     else if (ctrlmode & CAN_CTRLMODE_LISTENONLY)
0815         flags |= GS_CAN_MODE_LISTEN_ONLY;
0816 
0817     /* Controller is not allowed to retry TX
0818      * this mode is unavailable on atmels uc3c hardware
0819      */
0820     if (ctrlmode & CAN_CTRLMODE_ONE_SHOT)
0821         flags |= GS_CAN_MODE_ONE_SHOT;
0822 
0823     if (ctrlmode & CAN_CTRLMODE_3_SAMPLES)
0824         flags |= GS_CAN_MODE_TRIPLE_SAMPLE;
0825 
0826     /* finally start device */
0827     dev->can.state = CAN_STATE_ERROR_ACTIVE;
0828     dm->mode = cpu_to_le32(GS_CAN_MODE_START);
0829     dm->flags = cpu_to_le32(flags);
0830     rc = usb_control_msg(interface_to_usbdev(dev->iface),
0831                  usb_sndctrlpipe(interface_to_usbdev(dev->iface), 0),
0832                  GS_USB_BREQ_MODE,
0833                  USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
0834                  dev->channel, 0, dm, sizeof(*dm), 1000);
0835 
0836     if (rc < 0) {
0837         netdev_err(netdev, "Couldn't start device (err=%d)\n", rc);
0838         kfree(dm);
0839         dev->can.state = CAN_STATE_STOPPED;
0840         return rc;
0841     }
0842 
0843     kfree(dm);
0844 
0845     parent->active_channels++;
0846     if (!(dev->can.ctrlmode & CAN_CTRLMODE_LISTENONLY))
0847         netif_start_queue(netdev);
0848 
0849     return 0;
0850 }
0851 
0852 static int gs_can_close(struct net_device *netdev)
0853 {
0854     int rc;
0855     struct gs_can *dev = netdev_priv(netdev);
0856     struct gs_usb *parent = dev->parent;
0857     unsigned int i;
0858 
0859     netif_stop_queue(netdev);
0860 
0861     /* Stop polling */
0862     parent->active_channels--;
0863     if (!parent->active_channels) {
0864         usb_kill_anchored_urbs(&parent->rx_submitted);
0865         for (i = 0; i < GS_MAX_RX_URBS; i++)
0866             usb_free_coherent(dev->udev,
0867                       sizeof(struct gs_host_frame),
0868                       dev->rxbuf[i],
0869                       dev->rxbuf_dma[i]);
0870     }
0871 
0872     /* Stop sending URBs */
0873     usb_kill_anchored_urbs(&dev->tx_submitted);
0874     atomic_set(&dev->active_tx_urbs, 0);
0875 
0876     /* reset the device */
0877     rc = gs_cmd_reset(dev);
0878     if (rc < 0)
0879         netdev_warn(netdev, "Couldn't shutdown device (err=%d)", rc);
0880 
0881     /* reset tx contexts */
0882     for (rc = 0; rc < GS_MAX_TX_URBS; rc++) {
0883         dev->tx_context[rc].dev = dev;
0884         dev->tx_context[rc].echo_id = GS_MAX_TX_URBS;
0885     }
0886 
0887     /* close the netdev */
0888     close_candev(netdev);
0889 
0890     return 0;
0891 }
0892 
0893 static const struct net_device_ops gs_usb_netdev_ops = {
0894     .ndo_open = gs_can_open,
0895     .ndo_stop = gs_can_close,
0896     .ndo_start_xmit = gs_can_start_xmit,
0897     .ndo_change_mtu = can_change_mtu,
0898 };
0899 
0900 static int gs_usb_set_identify(struct net_device *netdev, bool do_identify)
0901 {
0902     struct gs_can *dev = netdev_priv(netdev);
0903     struct gs_identify_mode *imode;
0904     int rc;
0905 
0906     imode = kmalloc(sizeof(*imode), GFP_KERNEL);
0907 
0908     if (!imode)
0909         return -ENOMEM;
0910 
0911     if (do_identify)
0912         imode->mode = cpu_to_le32(GS_CAN_IDENTIFY_ON);
0913     else
0914         imode->mode = cpu_to_le32(GS_CAN_IDENTIFY_OFF);
0915 
0916     rc = usb_control_msg(interface_to_usbdev(dev->iface),
0917                  usb_sndctrlpipe(interface_to_usbdev(dev->iface), 0),
0918                  GS_USB_BREQ_IDENTIFY,
0919                  USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
0920                  dev->channel, 0, imode, sizeof(*imode), 100);
0921 
0922     kfree(imode);
0923 
0924     return (rc > 0) ? 0 : rc;
0925 }
0926 
0927 /* blink LED's for finding the this interface */
0928 static int gs_usb_set_phys_id(struct net_device *netdev,
0929                   enum ethtool_phys_id_state state)
0930 {
0931     const struct gs_can *dev = netdev_priv(netdev);
0932     int rc = 0;
0933 
0934     if (!(dev->feature & GS_CAN_FEATURE_IDENTIFY))
0935         return -EOPNOTSUPP;
0936 
0937     switch (state) {
0938     case ETHTOOL_ID_ACTIVE:
0939         rc = gs_usb_set_identify(netdev, GS_CAN_IDENTIFY_ON);
0940         break;
0941     case ETHTOOL_ID_INACTIVE:
0942         rc = gs_usb_set_identify(netdev, GS_CAN_IDENTIFY_OFF);
0943         break;
0944     default:
0945         break;
0946     }
0947 
0948     return rc;
0949 }
0950 
0951 static const struct ethtool_ops gs_usb_ethtool_ops = {
0952     .set_phys_id = gs_usb_set_phys_id,
0953     .get_ts_info = ethtool_op_get_ts_info,
0954 };
0955 
0956 static struct gs_can *gs_make_candev(unsigned int channel,
0957                      struct usb_interface *intf,
0958                      struct gs_device_config *dconf)
0959 {
0960     struct gs_can *dev;
0961     struct net_device *netdev;
0962     int rc;
0963     struct gs_device_bt_const *bt_const;
0964     struct gs_device_bt_const_extended *bt_const_extended;
0965     u32 feature;
0966 
0967     bt_const = kmalloc(sizeof(*bt_const), GFP_KERNEL);
0968     if (!bt_const)
0969         return ERR_PTR(-ENOMEM);
0970 
0971     /* fetch bit timing constants */
0972     rc = usb_control_msg(interface_to_usbdev(intf),
0973                  usb_rcvctrlpipe(interface_to_usbdev(intf), 0),
0974                  GS_USB_BREQ_BT_CONST,
0975                  USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
0976                  channel, 0, bt_const, sizeof(*bt_const), 1000);
0977 
0978     if (rc < 0) {
0979         dev_err(&intf->dev,
0980             "Couldn't get bit timing const for channel (err=%d)\n",
0981             rc);
0982         kfree(bt_const);
0983         return ERR_PTR(rc);
0984     }
0985 
0986     /* create netdev */
0987     netdev = alloc_candev(sizeof(struct gs_can), GS_MAX_TX_URBS);
0988     if (!netdev) {
0989         dev_err(&intf->dev, "Couldn't allocate candev\n");
0990         kfree(bt_const);
0991         return ERR_PTR(-ENOMEM);
0992     }
0993 
0994     dev = netdev_priv(netdev);
0995 
0996     netdev->netdev_ops = &gs_usb_netdev_ops;
0997     netdev->ethtool_ops = &gs_usb_ethtool_ops;
0998 
0999     netdev->flags |= IFF_ECHO; /* we support full roundtrip echo */
1000 
1001     /* dev setup */
1002     strcpy(dev->bt_const.name, KBUILD_MODNAME);
1003     dev->bt_const.tseg1_min = le32_to_cpu(bt_const->tseg1_min);
1004     dev->bt_const.tseg1_max = le32_to_cpu(bt_const->tseg1_max);
1005     dev->bt_const.tseg2_min = le32_to_cpu(bt_const->tseg2_min);
1006     dev->bt_const.tseg2_max = le32_to_cpu(bt_const->tseg2_max);
1007     dev->bt_const.sjw_max = le32_to_cpu(bt_const->sjw_max);
1008     dev->bt_const.brp_min = le32_to_cpu(bt_const->brp_min);
1009     dev->bt_const.brp_max = le32_to_cpu(bt_const->brp_max);
1010     dev->bt_const.brp_inc = le32_to_cpu(bt_const->brp_inc);
1011 
1012     dev->udev = interface_to_usbdev(intf);
1013     dev->iface = intf;
1014     dev->netdev = netdev;
1015     dev->channel = channel;
1016 
1017     init_usb_anchor(&dev->tx_submitted);
1018     atomic_set(&dev->active_tx_urbs, 0);
1019     spin_lock_init(&dev->tx_ctx_lock);
1020     for (rc = 0; rc < GS_MAX_TX_URBS; rc++) {
1021         dev->tx_context[rc].dev = dev;
1022         dev->tx_context[rc].echo_id = GS_MAX_TX_URBS;
1023     }
1024 
1025     /* can setup */
1026     dev->can.state = CAN_STATE_STOPPED;
1027     dev->can.clock.freq = le32_to_cpu(bt_const->fclk_can);
1028     dev->can.bittiming_const = &dev->bt_const;
1029     dev->can.do_set_bittiming = gs_usb_set_bittiming;
1030 
1031     dev->can.ctrlmode_supported = CAN_CTRLMODE_CC_LEN8_DLC;
1032 
1033     feature = le32_to_cpu(bt_const->feature);
1034     dev->feature = FIELD_GET(GS_CAN_FEATURE_MASK, feature);
1035     if (feature & GS_CAN_FEATURE_LISTEN_ONLY)
1036         dev->can.ctrlmode_supported |= CAN_CTRLMODE_LISTENONLY;
1037 
1038     if (feature & GS_CAN_FEATURE_LOOP_BACK)
1039         dev->can.ctrlmode_supported |= CAN_CTRLMODE_LOOPBACK;
1040 
1041     if (feature & GS_CAN_FEATURE_TRIPLE_SAMPLE)
1042         dev->can.ctrlmode_supported |= CAN_CTRLMODE_3_SAMPLES;
1043 
1044     if (feature & GS_CAN_FEATURE_ONE_SHOT)
1045         dev->can.ctrlmode_supported |= CAN_CTRLMODE_ONE_SHOT;
1046 
1047     if (feature & GS_CAN_FEATURE_FD) {
1048         dev->can.ctrlmode_supported |= CAN_CTRLMODE_FD;
1049         /* The data bit timing will be overwritten, if
1050          * GS_CAN_FEATURE_BT_CONST_EXT is set.
1051          */
1052         dev->can.data_bittiming_const = &dev->bt_const;
1053         dev->can.do_set_data_bittiming = gs_usb_set_data_bittiming;
1054     }
1055 
1056     /* The CANtact Pro from LinkLayer Labs is based on the
1057      * LPC54616 µC, which is affected by the NXP LPC USB transfer
1058      * erratum. However, the current firmware (version 2) doesn't
1059      * set the GS_CAN_FEATURE_REQ_USB_QUIRK_LPC546XX bit. Set the
1060      * feature GS_CAN_FEATURE_REQ_USB_QUIRK_LPC546XX to workaround
1061      * this issue.
1062      *
1063      * For the GS_USB_BREQ_DATA_BITTIMING USB control message the
1064      * CANtact Pro firmware uses a request value, which is already
1065      * used by the candleLight firmware for a different purpose
1066      * (GS_USB_BREQ_GET_USER_ID). Set the feature
1067      * GS_CAN_FEATURE_QUIRK_BREQ_CANTACT_PRO to workaround this
1068      * issue.
1069      */
1070     if (dev->udev->descriptor.idVendor == cpu_to_le16(USB_GSUSB_1_VENDOR_ID) &&
1071         dev->udev->descriptor.idProduct == cpu_to_le16(USB_GSUSB_1_PRODUCT_ID) &&
1072         dev->udev->manufacturer && dev->udev->product &&
1073         !strcmp(dev->udev->manufacturer, "LinkLayer Labs") &&
1074         !strcmp(dev->udev->product, "CANtact Pro") &&
1075         (le32_to_cpu(dconf->sw_version) <= 2))
1076         dev->feature |= GS_CAN_FEATURE_REQ_USB_QUIRK_LPC546XX |
1077             GS_CAN_FEATURE_QUIRK_BREQ_CANTACT_PRO;
1078 
1079     /* GS_CAN_FEATURE_IDENTIFY is only supported for sw_version > 1 */
1080     if (!(le32_to_cpu(dconf->sw_version) > 1 &&
1081           feature & GS_CAN_FEATURE_IDENTIFY))
1082         dev->feature &= ~GS_CAN_FEATURE_IDENTIFY;
1083 
1084     kfree(bt_const);
1085 
1086     /* fetch extended bit timing constants if device has feature
1087      * GS_CAN_FEATURE_FD and GS_CAN_FEATURE_BT_CONST_EXT
1088      */
1089     if (feature & GS_CAN_FEATURE_FD &&
1090         feature & GS_CAN_FEATURE_BT_CONST_EXT) {
1091         bt_const_extended = kmalloc(sizeof(*bt_const_extended), GFP_KERNEL);
1092         if (!bt_const_extended)
1093             return ERR_PTR(-ENOMEM);
1094 
1095         rc = usb_control_msg(interface_to_usbdev(intf),
1096                      usb_rcvctrlpipe(interface_to_usbdev(intf), 0),
1097                      GS_USB_BREQ_BT_CONST_EXT,
1098                      USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
1099                      channel, 0, bt_const_extended,
1100                      sizeof(*bt_const_extended),
1101                      1000);
1102         if (rc < 0) {
1103             dev_err(&intf->dev,
1104                 "Couldn't get extended bit timing const for channel (err=%d)\n",
1105                 rc);
1106             kfree(bt_const_extended);
1107             return ERR_PTR(rc);
1108         }
1109 
1110         strcpy(dev->data_bt_const.name, KBUILD_MODNAME);
1111         dev->data_bt_const.tseg1_min = le32_to_cpu(bt_const_extended->dtseg1_min);
1112         dev->data_bt_const.tseg1_max = le32_to_cpu(bt_const_extended->dtseg1_max);
1113         dev->data_bt_const.tseg2_min = le32_to_cpu(bt_const_extended->dtseg2_min);
1114         dev->data_bt_const.tseg2_max = le32_to_cpu(bt_const_extended->dtseg2_max);
1115         dev->data_bt_const.sjw_max = le32_to_cpu(bt_const_extended->dsjw_max);
1116         dev->data_bt_const.brp_min = le32_to_cpu(bt_const_extended->dbrp_min);
1117         dev->data_bt_const.brp_max = le32_to_cpu(bt_const_extended->dbrp_max);
1118         dev->data_bt_const.brp_inc = le32_to_cpu(bt_const_extended->dbrp_inc);
1119 
1120         dev->can.data_bittiming_const = &dev->data_bt_const;
1121 
1122         kfree(bt_const_extended);
1123     }
1124 
1125     SET_NETDEV_DEV(netdev, &intf->dev);
1126 
1127     rc = register_candev(dev->netdev);
1128     if (rc) {
1129         free_candev(dev->netdev);
1130         dev_err(&intf->dev, "Couldn't register candev (err=%d)\n", rc);
1131         return ERR_PTR(rc);
1132     }
1133 
1134     return dev;
1135 }
1136 
1137 static void gs_destroy_candev(struct gs_can *dev)
1138 {
1139     unregister_candev(dev->netdev);
1140     usb_kill_anchored_urbs(&dev->tx_submitted);
1141     free_candev(dev->netdev);
1142 }
1143 
1144 static int gs_usb_probe(struct usb_interface *intf,
1145             const struct usb_device_id *id)
1146 {
1147     struct usb_device *udev = interface_to_usbdev(intf);
1148     struct gs_host_frame *hf;
1149     struct gs_usb *dev;
1150     int rc = -ENOMEM;
1151     unsigned int icount, i;
1152     struct gs_host_config *hconf;
1153     struct gs_device_config *dconf;
1154 
1155     hconf = kmalloc(sizeof(*hconf), GFP_KERNEL);
1156     if (!hconf)
1157         return -ENOMEM;
1158 
1159     hconf->byte_order = cpu_to_le32(0x0000beef);
1160 
1161     /* send host config */
1162     rc = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
1163                  GS_USB_BREQ_HOST_FORMAT,
1164                  USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
1165                  1, intf->cur_altsetting->desc.bInterfaceNumber,
1166                  hconf, sizeof(*hconf), 1000);
1167 
1168     kfree(hconf);
1169 
1170     if (rc < 0) {
1171         dev_err(&intf->dev, "Couldn't send data format (err=%d)\n", rc);
1172         return rc;
1173     }
1174 
1175     dconf = kmalloc(sizeof(*dconf), GFP_KERNEL);
1176     if (!dconf)
1177         return -ENOMEM;
1178 
1179     /* read device config */
1180     rc = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
1181                  GS_USB_BREQ_DEVICE_CONFIG,
1182                  USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
1183                  1, intf->cur_altsetting->desc.bInterfaceNumber,
1184                  dconf, sizeof(*dconf), 1000);
1185     if (rc < 0) {
1186         dev_err(&intf->dev, "Couldn't get device config: (err=%d)\n",
1187             rc);
1188         kfree(dconf);
1189         return rc;
1190     }
1191 
1192     icount = dconf->icount + 1;
1193     dev_info(&intf->dev, "Configuring for %u interfaces\n", icount);
1194 
1195     if (icount > GS_MAX_INTF) {
1196         dev_err(&intf->dev,
1197             "Driver cannot handle more that %u CAN interfaces\n",
1198             GS_MAX_INTF);
1199         kfree(dconf);
1200         return -EINVAL;
1201     }
1202 
1203     dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1204     if (!dev) {
1205         kfree(dconf);
1206         return -ENOMEM;
1207     }
1208 
1209     init_usb_anchor(&dev->rx_submitted);
1210     /* default to classic CAN, switch to CAN-FD if at least one of
1211      * our channels support CAN-FD.
1212      */
1213     dev->hf_size_rx = struct_size(hf, classic_can, 1);
1214 
1215     usb_set_intfdata(intf, dev);
1216     dev->udev = udev;
1217 
1218     for (i = 0; i < icount; i++) {
1219         dev->canch[i] = gs_make_candev(i, intf, dconf);
1220         if (IS_ERR_OR_NULL(dev->canch[i])) {
1221             /* save error code to return later */
1222             rc = PTR_ERR(dev->canch[i]);
1223 
1224             /* on failure destroy previously created candevs */
1225             icount = i;
1226             for (i = 0; i < icount; i++)
1227                 gs_destroy_candev(dev->canch[i]);
1228 
1229             usb_kill_anchored_urbs(&dev->rx_submitted);
1230             kfree(dconf);
1231             kfree(dev);
1232             return rc;
1233         }
1234         dev->canch[i]->parent = dev;
1235 
1236         if (dev->canch[i]->can.ctrlmode_supported & CAN_CTRLMODE_FD)
1237             dev->hf_size_rx = struct_size(hf, canfd, 1);
1238     }
1239 
1240     kfree(dconf);
1241 
1242     return 0;
1243 }
1244 
1245 static void gs_usb_disconnect(struct usb_interface *intf)
1246 {
1247     struct gs_usb *dev = usb_get_intfdata(intf);
1248     unsigned int i;
1249 
1250     usb_set_intfdata(intf, NULL);
1251 
1252     if (!dev) {
1253         dev_err(&intf->dev, "Disconnect (nodata)\n");
1254         return;
1255     }
1256 
1257     for (i = 0; i < GS_MAX_INTF; i++)
1258         if (dev->canch[i])
1259             gs_destroy_candev(dev->canch[i]);
1260 
1261     usb_kill_anchored_urbs(&dev->rx_submitted);
1262     kfree(dev);
1263 }
1264 
1265 static const struct usb_device_id gs_usb_table[] = {
1266     { USB_DEVICE_INTERFACE_NUMBER(USB_GSUSB_1_VENDOR_ID,
1267                       USB_GSUSB_1_PRODUCT_ID, 0) },
1268     { USB_DEVICE_INTERFACE_NUMBER(USB_CANDLELIGHT_VENDOR_ID,
1269                       USB_CANDLELIGHT_PRODUCT_ID, 0) },
1270     { USB_DEVICE_INTERFACE_NUMBER(USB_CES_CANEXT_FD_VENDOR_ID,
1271                       USB_CES_CANEXT_FD_PRODUCT_ID, 0) },
1272     { USB_DEVICE_INTERFACE_NUMBER(USB_ABE_CANDEBUGGER_FD_VENDOR_ID,
1273                       USB_ABE_CANDEBUGGER_FD_PRODUCT_ID, 0) },
1274     {} /* Terminating entry */
1275 };
1276 
1277 MODULE_DEVICE_TABLE(usb, gs_usb_table);
1278 
1279 static struct usb_driver gs_usb_driver = {
1280     .name = KBUILD_MODNAME,
1281     .probe = gs_usb_probe,
1282     .disconnect = gs_usb_disconnect,
1283     .id_table = gs_usb_table,
1284 };
1285 
1286 module_usb_driver(gs_usb_driver);
1287 
1288 MODULE_AUTHOR("Maximilian Schneider <mws@schneidersoft.net>");
1289 MODULE_DESCRIPTION(
1290 "Socket CAN device driver for Geschwister Schneider Technologie-, "
1291 "Entwicklungs- und Vertriebs UG. USB2.0 to CAN interfaces\n"
1292 "and bytewerk.org candleLight USB CAN interfaces.");
1293 MODULE_LICENSE("GPL v2");