Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * CAN driver for esd electronics gmbh CAN-USB/2 and CAN-USB/Micro
0004  *
0005  * Copyright (C) 2010-2012 esd electronic system design gmbh, Matthias Fuchs <socketcan@esd.eu>
0006  * Copyright (C) 2022 esd electronics gmbh, Frank Jungclaus <frank.jungclaus@esd.eu>
0007  */
0008 #include <linux/ethtool.h>
0009 #include <linux/signal.h>
0010 #include <linux/slab.h>
0011 #include <linux/module.h>
0012 #include <linux/netdevice.h>
0013 #include <linux/usb.h>
0014 
0015 #include <linux/can.h>
0016 #include <linux/can/dev.h>
0017 #include <linux/can/error.h>
0018 
0019 MODULE_AUTHOR("Matthias Fuchs <socketcan@esd.eu>");
0020 MODULE_AUTHOR("Frank Jungclaus <frank.jungclaus@esd.eu>");
0021 MODULE_DESCRIPTION("CAN driver for esd electronics gmbh CAN-USB/2 and CAN-USB/Micro interfaces");
0022 MODULE_LICENSE("GPL v2");
0023 
0024 /* USB vendor and product ID */
0025 #define USB_ESDGMBH_VENDOR_ID   0x0ab4
0026 #define USB_CANUSB2_PRODUCT_ID  0x0010
0027 #define USB_CANUSBM_PRODUCT_ID  0x0011
0028 
0029 /* CAN controller clock frequencies */
0030 #define ESD_USB2_CAN_CLOCK  60000000
0031 #define ESD_USBM_CAN_CLOCK  36000000
0032 
0033 /* Maximum number of CAN nets */
0034 #define ESD_USB_MAX_NETS    2
0035 
0036 /* USB commands */
0037 #define CMD_VERSION     1 /* also used for VERSION_REPLY */
0038 #define CMD_CAN_RX      2 /* device to host only */
0039 #define CMD_CAN_TX      3 /* also used for TX_DONE */
0040 #define CMD_SETBAUD     4 /* also used for SETBAUD_REPLY */
0041 #define CMD_TS          5 /* also used for TS_REPLY */
0042 #define CMD_IDADD       6 /* also used for IDADD_REPLY */
0043 
0044 /* esd CAN message flags - dlc field */
0045 #define ESD_RTR         0x10
0046 
0047 /* esd CAN message flags - id field */
0048 #define ESD_EXTID       0x20000000
0049 #define ESD_EVENT       0x40000000
0050 #define ESD_IDMASK      0x1fffffff
0051 
0052 /* esd CAN event ids */
0053 #define ESD_EV_CAN_ERROR_EXT    2 /* CAN controller specific diagnostic data */
0054 
0055 /* baudrate message flags */
0056 #define ESD_USB_UBR     0x80000000
0057 #define ESD_USB_LOM     0x40000000
0058 #define ESD_USB_NO_BAUDRATE 0x7fffffff
0059 
0060 /* bit timing CAN-USB/2 */
0061 #define ESD_USB2_TSEG1_MIN  1
0062 #define ESD_USB2_TSEG1_MAX  16
0063 #define ESD_USB2_TSEG1_SHIFT    16
0064 #define ESD_USB2_TSEG2_MIN  1
0065 #define ESD_USB2_TSEG2_MAX  8
0066 #define ESD_USB2_TSEG2_SHIFT    20
0067 #define ESD_USB2_SJW_MAX    4
0068 #define ESD_USB2_SJW_SHIFT  14
0069 #define ESD_USBM_SJW_SHIFT  24
0070 #define ESD_USB2_BRP_MIN    1
0071 #define ESD_USB2_BRP_MAX    1024
0072 #define ESD_USB2_BRP_INC    1
0073 #define ESD_USB2_3_SAMPLES  0x00800000
0074 
0075 /* esd IDADD message */
0076 #define ESD_ID_ENABLE       0x80
0077 #define ESD_MAX_ID_SEGMENT  64
0078 
0079 /* SJA1000 ECC register (emulated by usb firmware) */
0080 #define SJA1000_ECC_SEG     0x1F
0081 #define SJA1000_ECC_DIR     0x20
0082 #define SJA1000_ECC_ERR     0x06
0083 #define SJA1000_ECC_BIT     0x00
0084 #define SJA1000_ECC_FORM    0x40
0085 #define SJA1000_ECC_STUFF   0x80
0086 #define SJA1000_ECC_MASK    0xc0
0087 
0088 /* esd bus state event codes */
0089 #define ESD_BUSSTATE_MASK   0xc0
0090 #define ESD_BUSSTATE_WARN   0x40
0091 #define ESD_BUSSTATE_ERRPASSIVE 0x80
0092 #define ESD_BUSSTATE_BUSOFF 0xc0
0093 
0094 #define RX_BUFFER_SIZE      1024
0095 #define MAX_RX_URBS     4
0096 #define MAX_TX_URBS     16 /* must be power of 2 */
0097 
0098 struct header_msg {
0099     u8 len; /* len is always the total message length in 32bit words */
0100     u8 cmd;
0101     u8 rsvd[2];
0102 };
0103 
0104 struct version_msg {
0105     u8 len;
0106     u8 cmd;
0107     u8 rsvd;
0108     u8 flags;
0109     __le32 drv_version;
0110 };
0111 
0112 struct version_reply_msg {
0113     u8 len;
0114     u8 cmd;
0115     u8 nets;
0116     u8 features;
0117     __le32 version;
0118     u8 name[16];
0119     __le32 rsvd;
0120     __le32 ts;
0121 };
0122 
0123 struct rx_msg {
0124     u8 len;
0125     u8 cmd;
0126     u8 net;
0127     u8 dlc;
0128     __le32 ts;
0129     __le32 id; /* upper 3 bits contain flags */
0130     u8 data[8];
0131 };
0132 
0133 struct tx_msg {
0134     u8 len;
0135     u8 cmd;
0136     u8 net;
0137     u8 dlc;
0138     u32 hnd;    /* opaque handle, not used by device */
0139     __le32 id; /* upper 3 bits contain flags */
0140     u8 data[8];
0141 };
0142 
0143 struct tx_done_msg {
0144     u8 len;
0145     u8 cmd;
0146     u8 net;
0147     u8 status;
0148     u32 hnd;    /* opaque handle, not used by device */
0149     __le32 ts;
0150 };
0151 
0152 struct id_filter_msg {
0153     u8 len;
0154     u8 cmd;
0155     u8 net;
0156     u8 option;
0157     __le32 mask[ESD_MAX_ID_SEGMENT + 1];
0158 };
0159 
0160 struct set_baudrate_msg {
0161     u8 len;
0162     u8 cmd;
0163     u8 net;
0164     u8 rsvd;
0165     __le32 baud;
0166 };
0167 
0168 /* Main message type used between library and application */
0169 struct __packed esd_usb_msg {
0170     union {
0171         struct header_msg hdr;
0172         struct version_msg version;
0173         struct version_reply_msg version_reply;
0174         struct rx_msg rx;
0175         struct tx_msg tx;
0176         struct tx_done_msg txdone;
0177         struct set_baudrate_msg setbaud;
0178         struct id_filter_msg filter;
0179     } msg;
0180 };
0181 
0182 static struct usb_device_id esd_usb_table[] = {
0183     {USB_DEVICE(USB_ESDGMBH_VENDOR_ID, USB_CANUSB2_PRODUCT_ID)},
0184     {USB_DEVICE(USB_ESDGMBH_VENDOR_ID, USB_CANUSBM_PRODUCT_ID)},
0185     {}
0186 };
0187 MODULE_DEVICE_TABLE(usb, esd_usb_table);
0188 
0189 struct esd_usb_net_priv;
0190 
0191 struct esd_tx_urb_context {
0192     struct esd_usb_net_priv *priv;
0193     u32 echo_index;
0194 };
0195 
0196 struct esd_usb {
0197     struct usb_device *udev;
0198     struct esd_usb_net_priv *nets[ESD_USB_MAX_NETS];
0199 
0200     struct usb_anchor rx_submitted;
0201 
0202     int net_count;
0203     u32 version;
0204     int rxinitdone;
0205     void *rxbuf[MAX_RX_URBS];
0206     dma_addr_t rxbuf_dma[MAX_RX_URBS];
0207 };
0208 
0209 struct esd_usb_net_priv {
0210     struct can_priv can; /* must be the first member */
0211 
0212     atomic_t active_tx_jobs;
0213     struct usb_anchor tx_submitted;
0214     struct esd_tx_urb_context tx_contexts[MAX_TX_URBS];
0215 
0216     struct esd_usb *usb;
0217     struct net_device *netdev;
0218     int index;
0219     u8 old_state;
0220     struct can_berr_counter bec;
0221 };
0222 
0223 static void esd_usb_rx_event(struct esd_usb_net_priv *priv,
0224                  struct esd_usb_msg *msg)
0225 {
0226     struct net_device_stats *stats = &priv->netdev->stats;
0227     struct can_frame *cf;
0228     struct sk_buff *skb;
0229     u32 id = le32_to_cpu(msg->msg.rx.id) & ESD_IDMASK;
0230 
0231     if (id == ESD_EV_CAN_ERROR_EXT) {
0232         u8 state = msg->msg.rx.data[0];
0233         u8 ecc = msg->msg.rx.data[1];
0234         u8 rxerr = msg->msg.rx.data[2];
0235         u8 txerr = msg->msg.rx.data[3];
0236 
0237         skb = alloc_can_err_skb(priv->netdev, &cf);
0238         if (skb == NULL) {
0239             stats->rx_dropped++;
0240             return;
0241         }
0242 
0243         if (state != priv->old_state) {
0244             priv->old_state = state;
0245 
0246             switch (state & ESD_BUSSTATE_MASK) {
0247             case ESD_BUSSTATE_BUSOFF:
0248                 priv->can.state = CAN_STATE_BUS_OFF;
0249                 cf->can_id |= CAN_ERR_BUSOFF;
0250                 priv->can.can_stats.bus_off++;
0251                 can_bus_off(priv->netdev);
0252                 break;
0253             case ESD_BUSSTATE_WARN:
0254                 priv->can.state = CAN_STATE_ERROR_WARNING;
0255                 priv->can.can_stats.error_warning++;
0256                 break;
0257             case ESD_BUSSTATE_ERRPASSIVE:
0258                 priv->can.state = CAN_STATE_ERROR_PASSIVE;
0259                 priv->can.can_stats.error_passive++;
0260                 break;
0261             default:
0262                 priv->can.state = CAN_STATE_ERROR_ACTIVE;
0263                 break;
0264             }
0265         } else {
0266             priv->can.can_stats.bus_error++;
0267             stats->rx_errors++;
0268 
0269             cf->can_id |= CAN_ERR_PROT | CAN_ERR_BUSERROR |
0270                       CAN_ERR_CNT;
0271 
0272             switch (ecc & SJA1000_ECC_MASK) {
0273             case SJA1000_ECC_BIT:
0274                 cf->data[2] |= CAN_ERR_PROT_BIT;
0275                 break;
0276             case SJA1000_ECC_FORM:
0277                 cf->data[2] |= CAN_ERR_PROT_FORM;
0278                 break;
0279             case SJA1000_ECC_STUFF:
0280                 cf->data[2] |= CAN_ERR_PROT_STUFF;
0281                 break;
0282             default:
0283                 cf->data[3] = ecc & SJA1000_ECC_SEG;
0284                 break;
0285             }
0286 
0287             /* Error occurred during transmission? */
0288             if (!(ecc & SJA1000_ECC_DIR))
0289                 cf->data[2] |= CAN_ERR_PROT_TX;
0290 
0291             if (priv->can.state == CAN_STATE_ERROR_WARNING ||
0292                 priv->can.state == CAN_STATE_ERROR_PASSIVE) {
0293                 cf->data[1] = (txerr > rxerr) ?
0294                     CAN_ERR_CRTL_TX_PASSIVE :
0295                     CAN_ERR_CRTL_RX_PASSIVE;
0296             }
0297             cf->data[6] = txerr;
0298             cf->data[7] = rxerr;
0299         }
0300 
0301         priv->bec.txerr = txerr;
0302         priv->bec.rxerr = rxerr;
0303 
0304         netif_rx(skb);
0305     }
0306 }
0307 
0308 static void esd_usb_rx_can_msg(struct esd_usb_net_priv *priv,
0309                    struct esd_usb_msg *msg)
0310 {
0311     struct net_device_stats *stats = &priv->netdev->stats;
0312     struct can_frame *cf;
0313     struct sk_buff *skb;
0314     int i;
0315     u32 id;
0316 
0317     if (!netif_device_present(priv->netdev))
0318         return;
0319 
0320     id = le32_to_cpu(msg->msg.rx.id);
0321 
0322     if (id & ESD_EVENT) {
0323         esd_usb_rx_event(priv, msg);
0324     } else {
0325         skb = alloc_can_skb(priv->netdev, &cf);
0326         if (skb == NULL) {
0327             stats->rx_dropped++;
0328             return;
0329         }
0330 
0331         cf->can_id = id & ESD_IDMASK;
0332         can_frame_set_cc_len(cf, msg->msg.rx.dlc & ~ESD_RTR,
0333                      priv->can.ctrlmode);
0334 
0335         if (id & ESD_EXTID)
0336             cf->can_id |= CAN_EFF_FLAG;
0337 
0338         if (msg->msg.rx.dlc & ESD_RTR) {
0339             cf->can_id |= CAN_RTR_FLAG;
0340         } else {
0341             for (i = 0; i < cf->len; i++)
0342                 cf->data[i] = msg->msg.rx.data[i];
0343 
0344             stats->rx_bytes += cf->len;
0345         }
0346         stats->rx_packets++;
0347 
0348         netif_rx(skb);
0349     }
0350 }
0351 
0352 static void esd_usb_tx_done_msg(struct esd_usb_net_priv *priv,
0353                 struct esd_usb_msg *msg)
0354 {
0355     struct net_device_stats *stats = &priv->netdev->stats;
0356     struct net_device *netdev = priv->netdev;
0357     struct esd_tx_urb_context *context;
0358 
0359     if (!netif_device_present(netdev))
0360         return;
0361 
0362     context = &priv->tx_contexts[msg->msg.txdone.hnd & (MAX_TX_URBS - 1)];
0363 
0364     if (!msg->msg.txdone.status) {
0365         stats->tx_packets++;
0366         stats->tx_bytes += can_get_echo_skb(netdev, context->echo_index,
0367                             NULL);
0368     } else {
0369         stats->tx_errors++;
0370         can_free_echo_skb(netdev, context->echo_index, NULL);
0371     }
0372 
0373     /* Release context */
0374     context->echo_index = MAX_TX_URBS;
0375     atomic_dec(&priv->active_tx_jobs);
0376 
0377     netif_wake_queue(netdev);
0378 }
0379 
0380 static void esd_usb_read_bulk_callback(struct urb *urb)
0381 {
0382     struct esd_usb *dev = urb->context;
0383     int retval;
0384     int pos = 0;
0385     int i;
0386 
0387     switch (urb->status) {
0388     case 0: /* success */
0389         break;
0390 
0391     case -ENOENT:
0392     case -EPIPE:
0393     case -EPROTO:
0394     case -ESHUTDOWN:
0395         return;
0396 
0397     default:
0398         dev_info(dev->udev->dev.parent,
0399              "Rx URB aborted (%d)\n", urb->status);
0400         goto resubmit_urb;
0401     }
0402 
0403     while (pos < urb->actual_length) {
0404         struct esd_usb_msg *msg;
0405 
0406         msg = (struct esd_usb_msg *)(urb->transfer_buffer + pos);
0407 
0408         switch (msg->msg.hdr.cmd) {
0409         case CMD_CAN_RX:
0410             if (msg->msg.rx.net >= dev->net_count) {
0411                 dev_err(dev->udev->dev.parent, "format error\n");
0412                 break;
0413             }
0414 
0415             esd_usb_rx_can_msg(dev->nets[msg->msg.rx.net], msg);
0416             break;
0417 
0418         case CMD_CAN_TX:
0419             if (msg->msg.txdone.net >= dev->net_count) {
0420                 dev_err(dev->udev->dev.parent, "format error\n");
0421                 break;
0422             }
0423 
0424             esd_usb_tx_done_msg(dev->nets[msg->msg.txdone.net],
0425                         msg);
0426             break;
0427         }
0428 
0429         pos += msg->msg.hdr.len << 2;
0430 
0431         if (pos > urb->actual_length) {
0432             dev_err(dev->udev->dev.parent, "format error\n");
0433             break;
0434         }
0435     }
0436 
0437 resubmit_urb:
0438     usb_fill_bulk_urb(urb, dev->udev, usb_rcvbulkpipe(dev->udev, 1),
0439               urb->transfer_buffer, RX_BUFFER_SIZE,
0440               esd_usb_read_bulk_callback, dev);
0441 
0442     retval = usb_submit_urb(urb, GFP_ATOMIC);
0443     if (retval == -ENODEV) {
0444         for (i = 0; i < dev->net_count; i++) {
0445             if (dev->nets[i])
0446                 netif_device_detach(dev->nets[i]->netdev);
0447         }
0448     } else if (retval) {
0449         dev_err(dev->udev->dev.parent,
0450             "failed resubmitting read bulk urb: %d\n", retval);
0451     }
0452 }
0453 
0454 /* callback for bulk IN urb */
0455 static void esd_usb_write_bulk_callback(struct urb *urb)
0456 {
0457     struct esd_tx_urb_context *context = urb->context;
0458     struct esd_usb_net_priv *priv;
0459     struct net_device *netdev;
0460     size_t size = sizeof(struct esd_usb_msg);
0461 
0462     WARN_ON(!context);
0463 
0464     priv = context->priv;
0465     netdev = priv->netdev;
0466 
0467     /* free up our allocated buffer */
0468     usb_free_coherent(urb->dev, size,
0469               urb->transfer_buffer, urb->transfer_dma);
0470 
0471     if (!netif_device_present(netdev))
0472         return;
0473 
0474     if (urb->status)
0475         netdev_info(netdev, "Tx URB aborted (%d)\n", urb->status);
0476 
0477     netif_trans_update(netdev);
0478 }
0479 
0480 static ssize_t firmware_show(struct device *d,
0481                  struct device_attribute *attr, char *buf)
0482 {
0483     struct usb_interface *intf = to_usb_interface(d);
0484     struct esd_usb *dev = usb_get_intfdata(intf);
0485 
0486     return sprintf(buf, "%d.%d.%d\n",
0487                (dev->version >> 12) & 0xf,
0488                (dev->version >> 8) & 0xf,
0489                dev->version & 0xff);
0490 }
0491 static DEVICE_ATTR_RO(firmware);
0492 
0493 static ssize_t hardware_show(struct device *d,
0494                  struct device_attribute *attr, char *buf)
0495 {
0496     struct usb_interface *intf = to_usb_interface(d);
0497     struct esd_usb *dev = usb_get_intfdata(intf);
0498 
0499     return sprintf(buf, "%d.%d.%d\n",
0500                (dev->version >> 28) & 0xf,
0501                (dev->version >> 24) & 0xf,
0502                (dev->version >> 16) & 0xff);
0503 }
0504 static DEVICE_ATTR_RO(hardware);
0505 
0506 static ssize_t nets_show(struct device *d,
0507              struct device_attribute *attr, char *buf)
0508 {
0509     struct usb_interface *intf = to_usb_interface(d);
0510     struct esd_usb *dev = usb_get_intfdata(intf);
0511 
0512     return sprintf(buf, "%d", dev->net_count);
0513 }
0514 static DEVICE_ATTR_RO(nets);
0515 
0516 static int esd_usb_send_msg(struct esd_usb *dev, struct esd_usb_msg *msg)
0517 {
0518     int actual_length;
0519 
0520     return usb_bulk_msg(dev->udev,
0521                 usb_sndbulkpipe(dev->udev, 2),
0522                 msg,
0523                 msg->msg.hdr.len << 2,
0524                 &actual_length,
0525                 1000);
0526 }
0527 
0528 static int esd_usb_wait_msg(struct esd_usb *dev,
0529                 struct esd_usb_msg *msg)
0530 {
0531     int actual_length;
0532 
0533     return usb_bulk_msg(dev->udev,
0534                 usb_rcvbulkpipe(dev->udev, 1),
0535                 msg,
0536                 sizeof(*msg),
0537                 &actual_length,
0538                 1000);
0539 }
0540 
0541 static int esd_usb_setup_rx_urbs(struct esd_usb *dev)
0542 {
0543     int i, err = 0;
0544 
0545     if (dev->rxinitdone)
0546         return 0;
0547 
0548     for (i = 0; i < MAX_RX_URBS; i++) {
0549         struct urb *urb = NULL;
0550         u8 *buf = NULL;
0551         dma_addr_t buf_dma;
0552 
0553         /* create a URB, and a buffer for it */
0554         urb = usb_alloc_urb(0, GFP_KERNEL);
0555         if (!urb) {
0556             err = -ENOMEM;
0557             break;
0558         }
0559 
0560         buf = usb_alloc_coherent(dev->udev, RX_BUFFER_SIZE, GFP_KERNEL,
0561                      &buf_dma);
0562         if (!buf) {
0563             dev_warn(dev->udev->dev.parent,
0564                  "No memory left for USB buffer\n");
0565             err = -ENOMEM;
0566             goto freeurb;
0567         }
0568 
0569         urb->transfer_dma = buf_dma;
0570 
0571         usb_fill_bulk_urb(urb, dev->udev,
0572                   usb_rcvbulkpipe(dev->udev, 1),
0573                   buf, RX_BUFFER_SIZE,
0574                   esd_usb_read_bulk_callback, dev);
0575         urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
0576         usb_anchor_urb(urb, &dev->rx_submitted);
0577 
0578         err = usb_submit_urb(urb, GFP_KERNEL);
0579         if (err) {
0580             usb_unanchor_urb(urb);
0581             usb_free_coherent(dev->udev, RX_BUFFER_SIZE, buf,
0582                       urb->transfer_dma);
0583             goto freeurb;
0584         }
0585 
0586         dev->rxbuf[i] = buf;
0587         dev->rxbuf_dma[i] = buf_dma;
0588 
0589 freeurb:
0590         /* Drop reference, USB core will take care of freeing it */
0591         usb_free_urb(urb);
0592         if (err)
0593             break;
0594     }
0595 
0596     /* Did we submit any URBs */
0597     if (i == 0) {
0598         dev_err(dev->udev->dev.parent, "couldn't setup read URBs\n");
0599         return err;
0600     }
0601 
0602     /* Warn if we've couldn't transmit all the URBs */
0603     if (i < MAX_RX_URBS) {
0604         dev_warn(dev->udev->dev.parent,
0605              "rx performance may be slow\n");
0606     }
0607 
0608     dev->rxinitdone = 1;
0609     return 0;
0610 }
0611 
0612 /* Start interface */
0613 static int esd_usb_start(struct esd_usb_net_priv *priv)
0614 {
0615     struct esd_usb *dev = priv->usb;
0616     struct net_device *netdev = priv->netdev;
0617     struct esd_usb_msg *msg;
0618     int err, i;
0619 
0620     msg = kmalloc(sizeof(*msg), GFP_KERNEL);
0621     if (!msg) {
0622         err = -ENOMEM;
0623         goto out;
0624     }
0625 
0626     /* Enable all IDs
0627      * The IDADD message takes up to 64 32 bit bitmasks (2048 bits).
0628      * Each bit represents one 11 bit CAN identifier. A set bit
0629      * enables reception of the corresponding CAN identifier. A cleared
0630      * bit disabled this identifier. An additional bitmask value
0631      * following the CAN 2.0A bits is used to enable reception of
0632      * extended CAN frames. Only the LSB of this final mask is checked
0633      * for the complete 29 bit ID range. The IDADD message also allows
0634      * filter configuration for an ID subset. In this case you can add
0635      * the number of the starting bitmask (0..64) to the filter.option
0636      * field followed by only some bitmasks.
0637      */
0638     msg->msg.hdr.cmd = CMD_IDADD;
0639     msg->msg.hdr.len = 2 + ESD_MAX_ID_SEGMENT;
0640     msg->msg.filter.net = priv->index;
0641     msg->msg.filter.option = ESD_ID_ENABLE; /* start with segment 0 */
0642     for (i = 0; i < ESD_MAX_ID_SEGMENT; i++)
0643         msg->msg.filter.mask[i] = cpu_to_le32(0xffffffff);
0644     /* enable 29bit extended IDs */
0645     msg->msg.filter.mask[ESD_MAX_ID_SEGMENT] = cpu_to_le32(0x00000001);
0646 
0647     err = esd_usb_send_msg(dev, msg);
0648     if (err)
0649         goto out;
0650 
0651     err = esd_usb_setup_rx_urbs(dev);
0652     if (err)
0653         goto out;
0654 
0655     priv->can.state = CAN_STATE_ERROR_ACTIVE;
0656 
0657 out:
0658     if (err == -ENODEV)
0659         netif_device_detach(netdev);
0660     if (err)
0661         netdev_err(netdev, "couldn't start device: %d\n", err);
0662 
0663     kfree(msg);
0664     return err;
0665 }
0666 
0667 static void unlink_all_urbs(struct esd_usb *dev)
0668 {
0669     struct esd_usb_net_priv *priv;
0670     int i, j;
0671 
0672     usb_kill_anchored_urbs(&dev->rx_submitted);
0673 
0674     for (i = 0; i < MAX_RX_URBS; ++i)
0675         usb_free_coherent(dev->udev, RX_BUFFER_SIZE,
0676                   dev->rxbuf[i], dev->rxbuf_dma[i]);
0677 
0678     for (i = 0; i < dev->net_count; i++) {
0679         priv = dev->nets[i];
0680         if (priv) {
0681             usb_kill_anchored_urbs(&priv->tx_submitted);
0682             atomic_set(&priv->active_tx_jobs, 0);
0683 
0684             for (j = 0; j < MAX_TX_URBS; j++)
0685                 priv->tx_contexts[j].echo_index = MAX_TX_URBS;
0686         }
0687     }
0688 }
0689 
0690 static int esd_usb_open(struct net_device *netdev)
0691 {
0692     struct esd_usb_net_priv *priv = netdev_priv(netdev);
0693     int err;
0694 
0695     /* common open */
0696     err = open_candev(netdev);
0697     if (err)
0698         return err;
0699 
0700     /* finally start device */
0701     err = esd_usb_start(priv);
0702     if (err) {
0703         netdev_warn(netdev, "couldn't start device: %d\n", err);
0704         close_candev(netdev);
0705         return err;
0706     }
0707 
0708     netif_start_queue(netdev);
0709 
0710     return 0;
0711 }
0712 
0713 static netdev_tx_t esd_usb_start_xmit(struct sk_buff *skb,
0714                       struct net_device *netdev)
0715 {
0716     struct esd_usb_net_priv *priv = netdev_priv(netdev);
0717     struct esd_usb *dev = priv->usb;
0718     struct esd_tx_urb_context *context = NULL;
0719     struct net_device_stats *stats = &netdev->stats;
0720     struct can_frame *cf = (struct can_frame *)skb->data;
0721     struct esd_usb_msg *msg;
0722     struct urb *urb;
0723     u8 *buf;
0724     int i, err;
0725     int ret = NETDEV_TX_OK;
0726     size_t size = sizeof(struct esd_usb_msg);
0727 
0728     if (can_dropped_invalid_skb(netdev, skb))
0729         return NETDEV_TX_OK;
0730 
0731     /* create a URB, and a buffer for it, and copy the data to the URB */
0732     urb = usb_alloc_urb(0, GFP_ATOMIC);
0733     if (!urb) {
0734         stats->tx_dropped++;
0735         dev_kfree_skb(skb);
0736         goto nourbmem;
0737     }
0738 
0739     buf = usb_alloc_coherent(dev->udev, size, GFP_ATOMIC,
0740                  &urb->transfer_dma);
0741     if (!buf) {
0742         netdev_err(netdev, "No memory left for USB buffer\n");
0743         stats->tx_dropped++;
0744         dev_kfree_skb(skb);
0745         goto nobufmem;
0746     }
0747 
0748     msg = (struct esd_usb_msg *)buf;
0749 
0750     msg->msg.hdr.len = 3; /* minimal length */
0751     msg->msg.hdr.cmd = CMD_CAN_TX;
0752     msg->msg.tx.net = priv->index;
0753     msg->msg.tx.dlc = can_get_cc_dlc(cf, priv->can.ctrlmode);
0754     msg->msg.tx.id = cpu_to_le32(cf->can_id & CAN_ERR_MASK);
0755 
0756     if (cf->can_id & CAN_RTR_FLAG)
0757         msg->msg.tx.dlc |= ESD_RTR;
0758 
0759     if (cf->can_id & CAN_EFF_FLAG)
0760         msg->msg.tx.id |= cpu_to_le32(ESD_EXTID);
0761 
0762     for (i = 0; i < cf->len; i++)
0763         msg->msg.tx.data[i] = cf->data[i];
0764 
0765     msg->msg.hdr.len += (cf->len + 3) >> 2;
0766 
0767     for (i = 0; i < MAX_TX_URBS; i++) {
0768         if (priv->tx_contexts[i].echo_index == MAX_TX_URBS) {
0769             context = &priv->tx_contexts[i];
0770             break;
0771         }
0772     }
0773 
0774     /* This may never happen */
0775     if (!context) {
0776         netdev_warn(netdev, "couldn't find free context\n");
0777         ret = NETDEV_TX_BUSY;
0778         goto releasebuf;
0779     }
0780 
0781     context->priv = priv;
0782     context->echo_index = i;
0783 
0784     /* hnd must not be 0 - MSB is stripped in txdone handling */
0785     msg->msg.tx.hnd = 0x80000000 | i; /* returned in TX done message */
0786 
0787     usb_fill_bulk_urb(urb, dev->udev, usb_sndbulkpipe(dev->udev, 2), buf,
0788               msg->msg.hdr.len << 2,
0789               esd_usb_write_bulk_callback, context);
0790 
0791     urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
0792 
0793     usb_anchor_urb(urb, &priv->tx_submitted);
0794 
0795     can_put_echo_skb(skb, netdev, context->echo_index, 0);
0796 
0797     atomic_inc(&priv->active_tx_jobs);
0798 
0799     /* Slow down tx path */
0800     if (atomic_read(&priv->active_tx_jobs) >= MAX_TX_URBS)
0801         netif_stop_queue(netdev);
0802 
0803     err = usb_submit_urb(urb, GFP_ATOMIC);
0804     if (err) {
0805         can_free_echo_skb(netdev, context->echo_index, NULL);
0806 
0807         atomic_dec(&priv->active_tx_jobs);
0808         usb_unanchor_urb(urb);
0809 
0810         stats->tx_dropped++;
0811 
0812         if (err == -ENODEV)
0813             netif_device_detach(netdev);
0814         else
0815             netdev_warn(netdev, "failed tx_urb %d\n", err);
0816 
0817         goto releasebuf;
0818     }
0819 
0820     netif_trans_update(netdev);
0821 
0822     /* Release our reference to this URB, the USB core will eventually free
0823      * it entirely.
0824      */
0825     usb_free_urb(urb);
0826 
0827     return NETDEV_TX_OK;
0828 
0829 releasebuf:
0830     usb_free_coherent(dev->udev, size, buf, urb->transfer_dma);
0831 
0832 nobufmem:
0833     usb_free_urb(urb);
0834 
0835 nourbmem:
0836     return ret;
0837 }
0838 
0839 static int esd_usb_close(struct net_device *netdev)
0840 {
0841     struct esd_usb_net_priv *priv = netdev_priv(netdev);
0842     struct esd_usb_msg *msg;
0843     int i;
0844 
0845     msg = kmalloc(sizeof(*msg), GFP_KERNEL);
0846     if (!msg)
0847         return -ENOMEM;
0848 
0849     /* Disable all IDs (see esd_usb_start()) */
0850     msg->msg.hdr.cmd = CMD_IDADD;
0851     msg->msg.hdr.len = 2 + ESD_MAX_ID_SEGMENT;
0852     msg->msg.filter.net = priv->index;
0853     msg->msg.filter.option = ESD_ID_ENABLE; /* start with segment 0 */
0854     for (i = 0; i <= ESD_MAX_ID_SEGMENT; i++)
0855         msg->msg.filter.mask[i] = 0;
0856     if (esd_usb_send_msg(priv->usb, msg) < 0)
0857         netdev_err(netdev, "sending idadd message failed\n");
0858 
0859     /* set CAN controller to reset mode */
0860     msg->msg.hdr.len = 2;
0861     msg->msg.hdr.cmd = CMD_SETBAUD;
0862     msg->msg.setbaud.net = priv->index;
0863     msg->msg.setbaud.rsvd = 0;
0864     msg->msg.setbaud.baud = cpu_to_le32(ESD_USB_NO_BAUDRATE);
0865     if (esd_usb_send_msg(priv->usb, msg) < 0)
0866         netdev_err(netdev, "sending setbaud message failed\n");
0867 
0868     priv->can.state = CAN_STATE_STOPPED;
0869 
0870     netif_stop_queue(netdev);
0871 
0872     close_candev(netdev);
0873 
0874     kfree(msg);
0875 
0876     return 0;
0877 }
0878 
0879 static const struct net_device_ops esd_usb_netdev_ops = {
0880     .ndo_open = esd_usb_open,
0881     .ndo_stop = esd_usb_close,
0882     .ndo_start_xmit = esd_usb_start_xmit,
0883     .ndo_change_mtu = can_change_mtu,
0884 };
0885 
0886 static const struct ethtool_ops esd_usb_ethtool_ops = {
0887     .get_ts_info = ethtool_op_get_ts_info,
0888 };
0889 
0890 static const struct can_bittiming_const esd_usb2_bittiming_const = {
0891     .name = "esd_usb2",
0892     .tseg1_min = ESD_USB2_TSEG1_MIN,
0893     .tseg1_max = ESD_USB2_TSEG1_MAX,
0894     .tseg2_min = ESD_USB2_TSEG2_MIN,
0895     .tseg2_max = ESD_USB2_TSEG2_MAX,
0896     .sjw_max = ESD_USB2_SJW_MAX,
0897     .brp_min = ESD_USB2_BRP_MIN,
0898     .brp_max = ESD_USB2_BRP_MAX,
0899     .brp_inc = ESD_USB2_BRP_INC,
0900 };
0901 
0902 static int esd_usb2_set_bittiming(struct net_device *netdev)
0903 {
0904     struct esd_usb_net_priv *priv = netdev_priv(netdev);
0905     struct can_bittiming *bt = &priv->can.bittiming;
0906     struct esd_usb_msg *msg;
0907     int err;
0908     u32 canbtr;
0909     int sjw_shift;
0910 
0911     canbtr = ESD_USB_UBR;
0912     if (priv->can.ctrlmode & CAN_CTRLMODE_LISTENONLY)
0913         canbtr |= ESD_USB_LOM;
0914 
0915     canbtr |= (bt->brp - 1) & (ESD_USB2_BRP_MAX - 1);
0916 
0917     if (le16_to_cpu(priv->usb->udev->descriptor.idProduct) ==
0918         USB_CANUSBM_PRODUCT_ID)
0919         sjw_shift = ESD_USBM_SJW_SHIFT;
0920     else
0921         sjw_shift = ESD_USB2_SJW_SHIFT;
0922 
0923     canbtr |= ((bt->sjw - 1) & (ESD_USB2_SJW_MAX - 1))
0924         << sjw_shift;
0925     canbtr |= ((bt->prop_seg + bt->phase_seg1 - 1)
0926            & (ESD_USB2_TSEG1_MAX - 1))
0927         << ESD_USB2_TSEG1_SHIFT;
0928     canbtr |= ((bt->phase_seg2 - 1) & (ESD_USB2_TSEG2_MAX - 1))
0929         << ESD_USB2_TSEG2_SHIFT;
0930     if (priv->can.ctrlmode & CAN_CTRLMODE_3_SAMPLES)
0931         canbtr |= ESD_USB2_3_SAMPLES;
0932 
0933     msg = kmalloc(sizeof(*msg), GFP_KERNEL);
0934     if (!msg)
0935         return -ENOMEM;
0936 
0937     msg->msg.hdr.len = 2;
0938     msg->msg.hdr.cmd = CMD_SETBAUD;
0939     msg->msg.setbaud.net = priv->index;
0940     msg->msg.setbaud.rsvd = 0;
0941     msg->msg.setbaud.baud = cpu_to_le32(canbtr);
0942 
0943     netdev_info(netdev, "setting BTR=%#x\n", canbtr);
0944 
0945     err = esd_usb_send_msg(priv->usb, msg);
0946 
0947     kfree(msg);
0948     return err;
0949 }
0950 
0951 static int esd_usb_get_berr_counter(const struct net_device *netdev,
0952                     struct can_berr_counter *bec)
0953 {
0954     struct esd_usb_net_priv *priv = netdev_priv(netdev);
0955 
0956     bec->txerr = priv->bec.txerr;
0957     bec->rxerr = priv->bec.rxerr;
0958 
0959     return 0;
0960 }
0961 
0962 static int esd_usb_set_mode(struct net_device *netdev, enum can_mode mode)
0963 {
0964     switch (mode) {
0965     case CAN_MODE_START:
0966         netif_wake_queue(netdev);
0967         break;
0968 
0969     default:
0970         return -EOPNOTSUPP;
0971     }
0972 
0973     return 0;
0974 }
0975 
0976 static int esd_usb_probe_one_net(struct usb_interface *intf, int index)
0977 {
0978     struct esd_usb *dev = usb_get_intfdata(intf);
0979     struct net_device *netdev;
0980     struct esd_usb_net_priv *priv;
0981     int err = 0;
0982     int i;
0983 
0984     netdev = alloc_candev(sizeof(*priv), MAX_TX_URBS);
0985     if (!netdev) {
0986         dev_err(&intf->dev, "couldn't alloc candev\n");
0987         err = -ENOMEM;
0988         goto done;
0989     }
0990 
0991     priv = netdev_priv(netdev);
0992 
0993     init_usb_anchor(&priv->tx_submitted);
0994     atomic_set(&priv->active_tx_jobs, 0);
0995 
0996     for (i = 0; i < MAX_TX_URBS; i++)
0997         priv->tx_contexts[i].echo_index = MAX_TX_URBS;
0998 
0999     priv->usb = dev;
1000     priv->netdev = netdev;
1001     priv->index = index;
1002 
1003     priv->can.state = CAN_STATE_STOPPED;
1004     priv->can.ctrlmode_supported = CAN_CTRLMODE_LISTENONLY |
1005         CAN_CTRLMODE_CC_LEN8_DLC;
1006 
1007     if (le16_to_cpu(dev->udev->descriptor.idProduct) ==
1008         USB_CANUSBM_PRODUCT_ID)
1009         priv->can.clock.freq = ESD_USBM_CAN_CLOCK;
1010     else {
1011         priv->can.clock.freq = ESD_USB2_CAN_CLOCK;
1012         priv->can.ctrlmode_supported |= CAN_CTRLMODE_3_SAMPLES;
1013     }
1014 
1015     priv->can.bittiming_const = &esd_usb2_bittiming_const;
1016     priv->can.do_set_bittiming = esd_usb2_set_bittiming;
1017     priv->can.do_set_mode = esd_usb_set_mode;
1018     priv->can.do_get_berr_counter = esd_usb_get_berr_counter;
1019 
1020     netdev->flags |= IFF_ECHO; /* we support local echo */
1021 
1022     netdev->netdev_ops = &esd_usb_netdev_ops;
1023     netdev->ethtool_ops = &esd_usb_ethtool_ops;
1024 
1025     SET_NETDEV_DEV(netdev, &intf->dev);
1026     netdev->dev_id = index;
1027 
1028     err = register_candev(netdev);
1029     if (err) {
1030         dev_err(&intf->dev, "couldn't register CAN device: %d\n", err);
1031         free_candev(netdev);
1032         err = -ENOMEM;
1033         goto done;
1034     }
1035 
1036     dev->nets[index] = priv;
1037     netdev_info(netdev, "device %s registered\n", netdev->name);
1038 
1039 done:
1040     return err;
1041 }
1042 
1043 /* probe function for new USB devices
1044  *
1045  * check version information and number of available
1046  * CAN interfaces
1047  */
1048 static int esd_usb_probe(struct usb_interface *intf,
1049              const struct usb_device_id *id)
1050 {
1051     struct esd_usb *dev;
1052     struct esd_usb_msg *msg;
1053     int i, err;
1054 
1055     dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1056     if (!dev) {
1057         err = -ENOMEM;
1058         goto done;
1059     }
1060 
1061     dev->udev = interface_to_usbdev(intf);
1062 
1063     init_usb_anchor(&dev->rx_submitted);
1064 
1065     usb_set_intfdata(intf, dev);
1066 
1067     msg = kmalloc(sizeof(*msg), GFP_KERNEL);
1068     if (!msg) {
1069         err = -ENOMEM;
1070         goto free_msg;
1071     }
1072 
1073     /* query number of CAN interfaces (nets) */
1074     msg->msg.hdr.cmd = CMD_VERSION;
1075     msg->msg.hdr.len = 2;
1076     msg->msg.version.rsvd = 0;
1077     msg->msg.version.flags = 0;
1078     msg->msg.version.drv_version = 0;
1079 
1080     err = esd_usb_send_msg(dev, msg);
1081     if (err < 0) {
1082         dev_err(&intf->dev, "sending version message failed\n");
1083         goto free_msg;
1084     }
1085 
1086     err = esd_usb_wait_msg(dev, msg);
1087     if (err < 0) {
1088         dev_err(&intf->dev, "no version message answer\n");
1089         goto free_msg;
1090     }
1091 
1092     dev->net_count = (int)msg->msg.version_reply.nets;
1093     dev->version = le32_to_cpu(msg->msg.version_reply.version);
1094 
1095     if (device_create_file(&intf->dev, &dev_attr_firmware))
1096         dev_err(&intf->dev,
1097             "Couldn't create device file for firmware\n");
1098 
1099     if (device_create_file(&intf->dev, &dev_attr_hardware))
1100         dev_err(&intf->dev,
1101             "Couldn't create device file for hardware\n");
1102 
1103     if (device_create_file(&intf->dev, &dev_attr_nets))
1104         dev_err(&intf->dev,
1105             "Couldn't create device file for nets\n");
1106 
1107     /* do per device probing */
1108     for (i = 0; i < dev->net_count; i++)
1109         esd_usb_probe_one_net(intf, i);
1110 
1111 free_msg:
1112     kfree(msg);
1113     if (err)
1114         kfree(dev);
1115 done:
1116     return err;
1117 }
1118 
1119 /* called by the usb core when the device is removed from the system */
1120 static void esd_usb_disconnect(struct usb_interface *intf)
1121 {
1122     struct esd_usb *dev = usb_get_intfdata(intf);
1123     struct net_device *netdev;
1124     int i;
1125 
1126     device_remove_file(&intf->dev, &dev_attr_firmware);
1127     device_remove_file(&intf->dev, &dev_attr_hardware);
1128     device_remove_file(&intf->dev, &dev_attr_nets);
1129 
1130     usb_set_intfdata(intf, NULL);
1131 
1132     if (dev) {
1133         for (i = 0; i < dev->net_count; i++) {
1134             if (dev->nets[i]) {
1135                 netdev = dev->nets[i]->netdev;
1136                 unregister_netdev(netdev);
1137                 free_candev(netdev);
1138             }
1139         }
1140         unlink_all_urbs(dev);
1141         kfree(dev);
1142     }
1143 }
1144 
1145 /* usb specific object needed to register this driver with the usb subsystem */
1146 static struct usb_driver esd_usb_driver = {
1147     .name = KBUILD_MODNAME,
1148     .probe = esd_usb_probe,
1149     .disconnect = esd_usb_disconnect,
1150     .id_table = esd_usb_table,
1151 };
1152 
1153 module_usb_driver(esd_usb_driver);