Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /* SocketCAN driver for Microchip CAN BUS Analyzer Tool
0003  *
0004  * Copyright (C) 2017 Mobica Limited
0005  *
0006  * This driver is inspired by the 4.6.2 version of net/can/usb/usb_8dev.c
0007  */
0008 
0009 #include <asm/unaligned.h>
0010 #include <linux/can.h>
0011 #include <linux/can/dev.h>
0012 #include <linux/can/error.h>
0013 #include <linux/ethtool.h>
0014 #include <linux/module.h>
0015 #include <linux/netdevice.h>
0016 #include <linux/signal.h>
0017 #include <linux/slab.h>
0018 #include <linux/usb.h>
0019 
0020 /* vendor and product id */
0021 #define MCBA_MODULE_NAME "mcba_usb"
0022 #define MCBA_VENDOR_ID 0x04d8
0023 #define MCBA_PRODUCT_ID 0x0a30
0024 
0025 /* driver constants */
0026 #define MCBA_MAX_RX_URBS 20
0027 #define MCBA_MAX_TX_URBS 20
0028 #define MCBA_CTX_FREE MCBA_MAX_TX_URBS
0029 
0030 /* RX buffer must be bigger than msg size since at the
0031  * beginning USB messages are stacked.
0032  */
0033 #define MCBA_USB_RX_BUFF_SIZE 64
0034 #define MCBA_USB_TX_BUFF_SIZE (sizeof(struct mcba_usb_msg))
0035 
0036 /* Microchip command id */
0037 #define MBCA_CMD_RECEIVE_MESSAGE 0xE3
0038 #define MBCA_CMD_I_AM_ALIVE_FROM_CAN 0xF5
0039 #define MBCA_CMD_I_AM_ALIVE_FROM_USB 0xF7
0040 #define MBCA_CMD_CHANGE_BIT_RATE 0xA1
0041 #define MBCA_CMD_TRANSMIT_MESSAGE_EV 0xA3
0042 #define MBCA_CMD_SETUP_TERMINATION_RESISTANCE 0xA8
0043 #define MBCA_CMD_READ_FW_VERSION 0xA9
0044 #define MBCA_CMD_NOTHING_TO_SEND 0xFF
0045 #define MBCA_CMD_TRANSMIT_MESSAGE_RSP 0xE2
0046 
0047 #define MCBA_VER_REQ_USB 1
0048 #define MCBA_VER_REQ_CAN 2
0049 
0050 #define MCBA_SIDL_EXID_MASK 0x8
0051 #define MCBA_DLC_MASK 0xf
0052 #define MCBA_DLC_RTR_MASK 0x40
0053 
0054 #define MCBA_CAN_STATE_WRN_TH 95
0055 #define MCBA_CAN_STATE_ERR_PSV_TH 127
0056 
0057 #define MCBA_TERMINATION_DISABLED CAN_TERMINATION_DISABLED
0058 #define MCBA_TERMINATION_ENABLED 120
0059 
0060 struct mcba_usb_ctx {
0061     struct mcba_priv *priv;
0062     u32 ndx;
0063     bool can;
0064 };
0065 
0066 /* Structure to hold all of our device specific stuff */
0067 struct mcba_priv {
0068     struct can_priv can; /* must be the first member */
0069     struct sk_buff *echo_skb[MCBA_MAX_TX_URBS];
0070     struct mcba_usb_ctx tx_context[MCBA_MAX_TX_URBS];
0071     struct usb_device *udev;
0072     struct net_device *netdev;
0073     struct usb_anchor tx_submitted;
0074     struct usb_anchor rx_submitted;
0075     struct can_berr_counter bec;
0076     bool usb_ka_first_pass;
0077     bool can_ka_first_pass;
0078     bool can_speed_check;
0079     atomic_t free_ctx_cnt;
0080     void *rxbuf[MCBA_MAX_RX_URBS];
0081     dma_addr_t rxbuf_dma[MCBA_MAX_RX_URBS];
0082     int rx_pipe;
0083     int tx_pipe;
0084 };
0085 
0086 /* CAN frame */
0087 struct __packed mcba_usb_msg_can {
0088     u8 cmd_id;
0089     __be16 eid;
0090     __be16 sid;
0091     u8 dlc;
0092     u8 data[8];
0093     u8 timestamp[4];
0094     u8 checksum;
0095 };
0096 
0097 /* command frame */
0098 struct __packed mcba_usb_msg {
0099     u8 cmd_id;
0100     u8 unused[18];
0101 };
0102 
0103 struct __packed mcba_usb_msg_ka_usb {
0104     u8 cmd_id;
0105     u8 termination_state;
0106     u8 soft_ver_major;
0107     u8 soft_ver_minor;
0108     u8 unused[15];
0109 };
0110 
0111 struct __packed mcba_usb_msg_ka_can {
0112     u8 cmd_id;
0113     u8 tx_err_cnt;
0114     u8 rx_err_cnt;
0115     u8 rx_buff_ovfl;
0116     u8 tx_bus_off;
0117     __be16 can_bitrate;
0118     __le16 rx_lost;
0119     u8 can_stat;
0120     u8 soft_ver_major;
0121     u8 soft_ver_minor;
0122     u8 debug_mode;
0123     u8 test_complete;
0124     u8 test_result;
0125     u8 unused[4];
0126 };
0127 
0128 struct __packed mcba_usb_msg_change_bitrate {
0129     u8 cmd_id;
0130     __be16 bitrate;
0131     u8 unused[16];
0132 };
0133 
0134 struct __packed mcba_usb_msg_termination {
0135     u8 cmd_id;
0136     u8 termination;
0137     u8 unused[17];
0138 };
0139 
0140 struct __packed mcba_usb_msg_fw_ver {
0141     u8 cmd_id;
0142     u8 pic;
0143     u8 unused[17];
0144 };
0145 
0146 static const struct usb_device_id mcba_usb_table[] = {
0147     { USB_DEVICE(MCBA_VENDOR_ID, MCBA_PRODUCT_ID) },
0148     {} /* Terminating entry */
0149 };
0150 
0151 MODULE_DEVICE_TABLE(usb, mcba_usb_table);
0152 
0153 static const u16 mcba_termination[] = { MCBA_TERMINATION_DISABLED,
0154                     MCBA_TERMINATION_ENABLED };
0155 
0156 static const u32 mcba_bitrate[] = { 20000,  33333,  50000,  80000,  83333,
0157                     100000, 125000, 150000, 175000, 200000,
0158                     225000, 250000, 275000, 300000, 500000,
0159                     625000, 800000, 1000000 };
0160 
0161 static inline void mcba_init_ctx(struct mcba_priv *priv)
0162 {
0163     int i = 0;
0164 
0165     for (i = 0; i < MCBA_MAX_TX_URBS; i++) {
0166         priv->tx_context[i].ndx = MCBA_CTX_FREE;
0167         priv->tx_context[i].priv = priv;
0168     }
0169 
0170     atomic_set(&priv->free_ctx_cnt, ARRAY_SIZE(priv->tx_context));
0171 }
0172 
0173 static inline struct mcba_usb_ctx *mcba_usb_get_free_ctx(struct mcba_priv *priv,
0174                              struct can_frame *cf)
0175 {
0176     int i = 0;
0177     struct mcba_usb_ctx *ctx = NULL;
0178 
0179     for (i = 0; i < MCBA_MAX_TX_URBS; i++) {
0180         if (priv->tx_context[i].ndx == MCBA_CTX_FREE) {
0181             ctx = &priv->tx_context[i];
0182             ctx->ndx = i;
0183 
0184             if (cf)
0185                 ctx->can = true;
0186             else
0187                 ctx->can = false;
0188 
0189             atomic_dec(&priv->free_ctx_cnt);
0190             break;
0191         }
0192     }
0193 
0194     if (!atomic_read(&priv->free_ctx_cnt))
0195         /* That was the last free ctx. Slow down tx path */
0196         netif_stop_queue(priv->netdev);
0197 
0198     return ctx;
0199 }
0200 
0201 /* mcba_usb_free_ctx and mcba_usb_get_free_ctx are executed by different
0202  * threads. The order of execution in below function is important.
0203  */
0204 static inline void mcba_usb_free_ctx(struct mcba_usb_ctx *ctx)
0205 {
0206     /* Increase number of free ctxs before freeing ctx */
0207     atomic_inc(&ctx->priv->free_ctx_cnt);
0208 
0209     ctx->ndx = MCBA_CTX_FREE;
0210 
0211     /* Wake up the queue once ctx is marked free */
0212     netif_wake_queue(ctx->priv->netdev);
0213 }
0214 
0215 static void mcba_usb_write_bulk_callback(struct urb *urb)
0216 {
0217     struct mcba_usb_ctx *ctx = urb->context;
0218     struct net_device *netdev;
0219 
0220     WARN_ON(!ctx);
0221 
0222     netdev = ctx->priv->netdev;
0223 
0224     /* free up our allocated buffer */
0225     usb_free_coherent(urb->dev, urb->transfer_buffer_length,
0226               urb->transfer_buffer, urb->transfer_dma);
0227 
0228     if (ctx->can) {
0229         if (!netif_device_present(netdev))
0230             return;
0231 
0232         netdev->stats.tx_packets++;
0233         netdev->stats.tx_bytes += can_get_echo_skb(netdev, ctx->ndx,
0234                                NULL);
0235     }
0236 
0237     if (urb->status)
0238         netdev_info(netdev, "Tx URB aborted (%d)\n", urb->status);
0239 
0240     /* Release the context */
0241     mcba_usb_free_ctx(ctx);
0242 }
0243 
0244 /* Send data to device */
0245 static netdev_tx_t mcba_usb_xmit(struct mcba_priv *priv,
0246                  struct mcba_usb_msg *usb_msg,
0247                  struct mcba_usb_ctx *ctx)
0248 {
0249     struct urb *urb;
0250     u8 *buf;
0251     int err;
0252 
0253     /* create a URB, and a buffer for it, and copy the data to the URB */
0254     urb = usb_alloc_urb(0, GFP_ATOMIC);
0255     if (!urb)
0256         return -ENOMEM;
0257 
0258     buf = usb_alloc_coherent(priv->udev, MCBA_USB_TX_BUFF_SIZE, GFP_ATOMIC,
0259                  &urb->transfer_dma);
0260     if (!buf) {
0261         err = -ENOMEM;
0262         goto nomembuf;
0263     }
0264 
0265     memcpy(buf, usb_msg, MCBA_USB_TX_BUFF_SIZE);
0266 
0267     usb_fill_bulk_urb(urb, priv->udev, priv->tx_pipe, buf, MCBA_USB_TX_BUFF_SIZE,
0268               mcba_usb_write_bulk_callback, ctx);
0269 
0270     urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
0271     usb_anchor_urb(urb, &priv->tx_submitted);
0272 
0273     err = usb_submit_urb(urb, GFP_ATOMIC);
0274     if (unlikely(err))
0275         goto failed;
0276 
0277     /* Release our reference to this URB, the USB core will eventually free
0278      * it entirely.
0279      */
0280     usb_free_urb(urb);
0281 
0282     return 0;
0283 
0284 failed:
0285     usb_unanchor_urb(urb);
0286     usb_free_coherent(priv->udev, MCBA_USB_TX_BUFF_SIZE, buf,
0287               urb->transfer_dma);
0288 
0289     if (err == -ENODEV)
0290         netif_device_detach(priv->netdev);
0291     else
0292         netdev_warn(priv->netdev, "failed tx_urb %d\n", err);
0293 
0294 nomembuf:
0295     usb_free_urb(urb);
0296 
0297     return err;
0298 }
0299 
0300 /* Send data to device */
0301 static netdev_tx_t mcba_usb_start_xmit(struct sk_buff *skb,
0302                        struct net_device *netdev)
0303 {
0304     struct mcba_priv *priv = netdev_priv(netdev);
0305     struct can_frame *cf = (struct can_frame *)skb->data;
0306     struct mcba_usb_ctx *ctx = NULL;
0307     struct net_device_stats *stats = &priv->netdev->stats;
0308     u16 sid;
0309     int err;
0310     struct mcba_usb_msg_can usb_msg = {
0311         .cmd_id = MBCA_CMD_TRANSMIT_MESSAGE_EV
0312     };
0313 
0314     if (can_dropped_invalid_skb(netdev, skb))
0315         return NETDEV_TX_OK;
0316 
0317     ctx = mcba_usb_get_free_ctx(priv, cf);
0318     if (!ctx)
0319         return NETDEV_TX_BUSY;
0320 
0321     if (cf->can_id & CAN_EFF_FLAG) {
0322         /* SIDH    | SIDL                 | EIDH   | EIDL
0323          * 28 - 21 | 20 19 18 x x x 17 16 | 15 - 8 | 7 - 0
0324          */
0325         sid = MCBA_SIDL_EXID_MASK;
0326         /* store 28-18 bits */
0327         sid |= (cf->can_id & 0x1ffc0000) >> 13;
0328         /* store 17-16 bits */
0329         sid |= (cf->can_id & 0x30000) >> 16;
0330         put_unaligned_be16(sid, &usb_msg.sid);
0331 
0332         /* store 15-0 bits */
0333         put_unaligned_be16(cf->can_id & 0xffff, &usb_msg.eid);
0334     } else {
0335         /* SIDH   | SIDL
0336          * 10 - 3 | 2 1 0 x x x x x
0337          */
0338         put_unaligned_be16((cf->can_id & CAN_SFF_MASK) << 5,
0339                    &usb_msg.sid);
0340         usb_msg.eid = 0;
0341     }
0342 
0343     usb_msg.dlc = cf->len;
0344 
0345     memcpy(usb_msg.data, cf->data, usb_msg.dlc);
0346 
0347     if (cf->can_id & CAN_RTR_FLAG)
0348         usb_msg.dlc |= MCBA_DLC_RTR_MASK;
0349 
0350     can_put_echo_skb(skb, priv->netdev, ctx->ndx, 0);
0351 
0352     err = mcba_usb_xmit(priv, (struct mcba_usb_msg *)&usb_msg, ctx);
0353     if (err)
0354         goto xmit_failed;
0355 
0356     return NETDEV_TX_OK;
0357 
0358 xmit_failed:
0359     can_free_echo_skb(priv->netdev, ctx->ndx, NULL);
0360     mcba_usb_free_ctx(ctx);
0361     stats->tx_dropped++;
0362 
0363     return NETDEV_TX_OK;
0364 }
0365 
0366 /* Send cmd to device */
0367 static void mcba_usb_xmit_cmd(struct mcba_priv *priv,
0368                   struct mcba_usb_msg *usb_msg)
0369 {
0370     struct mcba_usb_ctx *ctx = NULL;
0371     int err;
0372 
0373     ctx = mcba_usb_get_free_ctx(priv, NULL);
0374     if (!ctx) {
0375         netdev_err(priv->netdev,
0376                "Lack of free ctx. Sending (%d) cmd aborted",
0377                usb_msg->cmd_id);
0378 
0379         return;
0380     }
0381 
0382     err = mcba_usb_xmit(priv, usb_msg, ctx);
0383     if (err)
0384         netdev_err(priv->netdev, "Failed to send cmd (%d)",
0385                usb_msg->cmd_id);
0386 }
0387 
0388 static void mcba_usb_xmit_change_bitrate(struct mcba_priv *priv, u16 bitrate)
0389 {
0390     struct mcba_usb_msg_change_bitrate usb_msg = {
0391         .cmd_id = MBCA_CMD_CHANGE_BIT_RATE
0392     };
0393 
0394     put_unaligned_be16(bitrate, &usb_msg.bitrate);
0395 
0396     mcba_usb_xmit_cmd(priv, (struct mcba_usb_msg *)&usb_msg);
0397 }
0398 
0399 static void mcba_usb_xmit_read_fw_ver(struct mcba_priv *priv, u8 pic)
0400 {
0401     struct mcba_usb_msg_fw_ver usb_msg = {
0402         .cmd_id = MBCA_CMD_READ_FW_VERSION,
0403         .pic = pic
0404     };
0405 
0406     mcba_usb_xmit_cmd(priv, (struct mcba_usb_msg *)&usb_msg);
0407 }
0408 
0409 static void mcba_usb_process_can(struct mcba_priv *priv,
0410                  struct mcba_usb_msg_can *msg)
0411 {
0412     struct can_frame *cf;
0413     struct sk_buff *skb;
0414     struct net_device_stats *stats = &priv->netdev->stats;
0415     u16 sid;
0416 
0417     skb = alloc_can_skb(priv->netdev, &cf);
0418     if (!skb)
0419         return;
0420 
0421     sid = get_unaligned_be16(&msg->sid);
0422 
0423     if (sid & MCBA_SIDL_EXID_MASK) {
0424         /* SIDH    | SIDL                 | EIDH   | EIDL
0425          * 28 - 21 | 20 19 18 x x x 17 16 | 15 - 8 | 7 - 0
0426          */
0427         cf->can_id = CAN_EFF_FLAG;
0428 
0429         /* store 28-18 bits */
0430         cf->can_id |= (sid & 0xffe0) << 13;
0431         /* store 17-16 bits */
0432         cf->can_id |= (sid & 3) << 16;
0433         /* store 15-0 bits */
0434         cf->can_id |= get_unaligned_be16(&msg->eid);
0435     } else {
0436         /* SIDH   | SIDL
0437          * 10 - 3 | 2 1 0 x x x x x
0438          */
0439         cf->can_id = (sid & 0xffe0) >> 5;
0440     }
0441 
0442     cf->len = can_cc_dlc2len(msg->dlc & MCBA_DLC_MASK);
0443 
0444     if (msg->dlc & MCBA_DLC_RTR_MASK) {
0445         cf->can_id |= CAN_RTR_FLAG;
0446     } else {
0447         memcpy(cf->data, msg->data, cf->len);
0448 
0449         stats->rx_bytes += cf->len;
0450     }
0451     stats->rx_packets++;
0452 
0453     netif_rx(skb);
0454 }
0455 
0456 static void mcba_usb_process_ka_usb(struct mcba_priv *priv,
0457                     struct mcba_usb_msg_ka_usb *msg)
0458 {
0459     if (unlikely(priv->usb_ka_first_pass)) {
0460         netdev_info(priv->netdev, "PIC USB version %u.%u\n",
0461                 msg->soft_ver_major, msg->soft_ver_minor);
0462 
0463         priv->usb_ka_first_pass = false;
0464     }
0465 
0466     if (msg->termination_state)
0467         priv->can.termination = MCBA_TERMINATION_ENABLED;
0468     else
0469         priv->can.termination = MCBA_TERMINATION_DISABLED;
0470 }
0471 
0472 static u32 convert_can2host_bitrate(struct mcba_usb_msg_ka_can *msg)
0473 {
0474     const u32 bitrate = get_unaligned_be16(&msg->can_bitrate);
0475 
0476     if ((bitrate == 33) || (bitrate == 83))
0477         return bitrate * 1000 + 333;
0478     else
0479         return bitrate * 1000;
0480 }
0481 
0482 static void mcba_usb_process_ka_can(struct mcba_priv *priv,
0483                     struct mcba_usb_msg_ka_can *msg)
0484 {
0485     if (unlikely(priv->can_ka_first_pass)) {
0486         netdev_info(priv->netdev, "PIC CAN version %u.%u\n",
0487                 msg->soft_ver_major, msg->soft_ver_minor);
0488 
0489         priv->can_ka_first_pass = false;
0490     }
0491 
0492     if (unlikely(priv->can_speed_check)) {
0493         const u32 bitrate = convert_can2host_bitrate(msg);
0494 
0495         priv->can_speed_check = false;
0496 
0497         if (bitrate != priv->can.bittiming.bitrate)
0498             netdev_err(
0499                 priv->netdev,
0500                 "Wrong bitrate reported by the device (%u). Expected %u",
0501                 bitrate, priv->can.bittiming.bitrate);
0502     }
0503 
0504     priv->bec.txerr = msg->tx_err_cnt;
0505     priv->bec.rxerr = msg->rx_err_cnt;
0506 
0507     if (msg->tx_bus_off)
0508         priv->can.state = CAN_STATE_BUS_OFF;
0509 
0510     else if ((priv->bec.txerr > MCBA_CAN_STATE_ERR_PSV_TH) ||
0511          (priv->bec.rxerr > MCBA_CAN_STATE_ERR_PSV_TH))
0512         priv->can.state = CAN_STATE_ERROR_PASSIVE;
0513 
0514     else if ((priv->bec.txerr > MCBA_CAN_STATE_WRN_TH) ||
0515          (priv->bec.rxerr > MCBA_CAN_STATE_WRN_TH))
0516         priv->can.state = CAN_STATE_ERROR_WARNING;
0517 }
0518 
0519 static void mcba_usb_process_rx(struct mcba_priv *priv,
0520                 struct mcba_usb_msg *msg)
0521 {
0522     switch (msg->cmd_id) {
0523     case MBCA_CMD_I_AM_ALIVE_FROM_CAN:
0524         mcba_usb_process_ka_can(priv,
0525                     (struct mcba_usb_msg_ka_can *)msg);
0526         break;
0527 
0528     case MBCA_CMD_I_AM_ALIVE_FROM_USB:
0529         mcba_usb_process_ka_usb(priv,
0530                     (struct mcba_usb_msg_ka_usb *)msg);
0531         break;
0532 
0533     case MBCA_CMD_RECEIVE_MESSAGE:
0534         mcba_usb_process_can(priv, (struct mcba_usb_msg_can *)msg);
0535         break;
0536 
0537     case MBCA_CMD_NOTHING_TO_SEND:
0538         /* Side effect of communication between PIC_USB and PIC_CAN.
0539          * PIC_CAN is telling us that it has nothing to send
0540          */
0541         break;
0542 
0543     case MBCA_CMD_TRANSMIT_MESSAGE_RSP:
0544         /* Transmission response from the device containing timestamp */
0545         break;
0546 
0547     default:
0548         netdev_warn(priv->netdev, "Unsupported msg (0x%X)",
0549                 msg->cmd_id);
0550         break;
0551     }
0552 }
0553 
0554 /* Callback for reading data from device
0555  *
0556  * Check urb status, call read function and resubmit urb read operation.
0557  */
0558 static void mcba_usb_read_bulk_callback(struct urb *urb)
0559 {
0560     struct mcba_priv *priv = urb->context;
0561     struct net_device *netdev;
0562     int retval;
0563     int pos = 0;
0564 
0565     netdev = priv->netdev;
0566 
0567     if (!netif_device_present(netdev))
0568         return;
0569 
0570     switch (urb->status) {
0571     case 0: /* success */
0572         break;
0573 
0574     case -ENOENT:
0575     case -EPIPE:
0576     case -EPROTO:
0577     case -ESHUTDOWN:
0578         return;
0579 
0580     default:
0581         netdev_info(netdev, "Rx URB aborted (%d)\n", urb->status);
0582 
0583         goto resubmit_urb;
0584     }
0585 
0586     while (pos < urb->actual_length) {
0587         struct mcba_usb_msg *msg;
0588 
0589         if (pos + sizeof(struct mcba_usb_msg) > urb->actual_length) {
0590             netdev_err(priv->netdev, "format error\n");
0591             break;
0592         }
0593 
0594         msg = (struct mcba_usb_msg *)(urb->transfer_buffer + pos);
0595         mcba_usb_process_rx(priv, msg);
0596 
0597         pos += sizeof(struct mcba_usb_msg);
0598     }
0599 
0600 resubmit_urb:
0601 
0602     usb_fill_bulk_urb(urb, priv->udev,
0603               priv->rx_pipe,
0604               urb->transfer_buffer, MCBA_USB_RX_BUFF_SIZE,
0605               mcba_usb_read_bulk_callback, priv);
0606 
0607     retval = usb_submit_urb(urb, GFP_ATOMIC);
0608 
0609     if (retval == -ENODEV)
0610         netif_device_detach(netdev);
0611     else if (retval)
0612         netdev_err(netdev, "failed resubmitting read bulk urb: %d\n",
0613                retval);
0614 }
0615 
0616 /* Start USB device */
0617 static int mcba_usb_start(struct mcba_priv *priv)
0618 {
0619     struct net_device *netdev = priv->netdev;
0620     int err, i;
0621 
0622     mcba_init_ctx(priv);
0623 
0624     for (i = 0; i < MCBA_MAX_RX_URBS; i++) {
0625         struct urb *urb = NULL;
0626         u8 *buf;
0627         dma_addr_t buf_dma;
0628 
0629         /* create a URB, and a buffer for it */
0630         urb = usb_alloc_urb(0, GFP_KERNEL);
0631         if (!urb) {
0632             err = -ENOMEM;
0633             break;
0634         }
0635 
0636         buf = usb_alloc_coherent(priv->udev, MCBA_USB_RX_BUFF_SIZE,
0637                      GFP_KERNEL, &buf_dma);
0638         if (!buf) {
0639             netdev_err(netdev, "No memory left for USB buffer\n");
0640             usb_free_urb(urb);
0641             err = -ENOMEM;
0642             break;
0643         }
0644 
0645         urb->transfer_dma = buf_dma;
0646 
0647         usb_fill_bulk_urb(urb, priv->udev,
0648                   priv->rx_pipe,
0649                   buf, MCBA_USB_RX_BUFF_SIZE,
0650                   mcba_usb_read_bulk_callback, priv);
0651         urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
0652         usb_anchor_urb(urb, &priv->rx_submitted);
0653 
0654         err = usb_submit_urb(urb, GFP_KERNEL);
0655         if (err) {
0656             usb_unanchor_urb(urb);
0657             usb_free_coherent(priv->udev, MCBA_USB_RX_BUFF_SIZE,
0658                       buf, buf_dma);
0659             usb_free_urb(urb);
0660             break;
0661         }
0662 
0663         priv->rxbuf[i] = buf;
0664         priv->rxbuf_dma[i] = buf_dma;
0665 
0666         /* Drop reference, USB core will take care of freeing it */
0667         usb_free_urb(urb);
0668     }
0669 
0670     /* Did we submit any URBs */
0671     if (i == 0) {
0672         netdev_warn(netdev, "couldn't setup read URBs\n");
0673         return err;
0674     }
0675 
0676     /* Warn if we've couldn't transmit all the URBs */
0677     if (i < MCBA_MAX_RX_URBS)
0678         netdev_warn(netdev, "rx performance may be slow\n");
0679 
0680     mcba_usb_xmit_read_fw_ver(priv, MCBA_VER_REQ_USB);
0681     mcba_usb_xmit_read_fw_ver(priv, MCBA_VER_REQ_CAN);
0682 
0683     return err;
0684 }
0685 
0686 /* Open USB device */
0687 static int mcba_usb_open(struct net_device *netdev)
0688 {
0689     struct mcba_priv *priv = netdev_priv(netdev);
0690     int err;
0691 
0692     /* common open */
0693     err = open_candev(netdev);
0694     if (err)
0695         return err;
0696 
0697     priv->can_speed_check = true;
0698     priv->can.state = CAN_STATE_ERROR_ACTIVE;
0699 
0700     netif_start_queue(netdev);
0701 
0702     return 0;
0703 }
0704 
0705 static void mcba_urb_unlink(struct mcba_priv *priv)
0706 {
0707     int i;
0708 
0709     usb_kill_anchored_urbs(&priv->rx_submitted);
0710 
0711     for (i = 0; i < MCBA_MAX_RX_URBS; ++i)
0712         usb_free_coherent(priv->udev, MCBA_USB_RX_BUFF_SIZE,
0713                   priv->rxbuf[i], priv->rxbuf_dma[i]);
0714 
0715     usb_kill_anchored_urbs(&priv->tx_submitted);
0716 }
0717 
0718 /* Close USB device */
0719 static int mcba_usb_close(struct net_device *netdev)
0720 {
0721     struct mcba_priv *priv = netdev_priv(netdev);
0722 
0723     priv->can.state = CAN_STATE_STOPPED;
0724 
0725     netif_stop_queue(netdev);
0726 
0727     /* Stop polling */
0728     mcba_urb_unlink(priv);
0729 
0730     close_candev(netdev);
0731 
0732     return 0;
0733 }
0734 
0735 /* Set network device mode
0736  *
0737  * Maybe we should leave this function empty, because the device
0738  * set mode variable with open command.
0739  */
0740 static int mcba_net_set_mode(struct net_device *netdev, enum can_mode mode)
0741 {
0742     return 0;
0743 }
0744 
0745 static int mcba_net_get_berr_counter(const struct net_device *netdev,
0746                      struct can_berr_counter *bec)
0747 {
0748     struct mcba_priv *priv = netdev_priv(netdev);
0749 
0750     bec->txerr = priv->bec.txerr;
0751     bec->rxerr = priv->bec.rxerr;
0752 
0753     return 0;
0754 }
0755 
0756 static const struct net_device_ops mcba_netdev_ops = {
0757     .ndo_open = mcba_usb_open,
0758     .ndo_stop = mcba_usb_close,
0759     .ndo_start_xmit = mcba_usb_start_xmit,
0760 };
0761 
0762 static const struct ethtool_ops mcba_ethtool_ops = {
0763     .get_ts_info = ethtool_op_get_ts_info,
0764 };
0765 
0766 /* Microchip CANBUS has hardcoded bittiming values by default.
0767  * This function sends request via USB to change the speed and align bittiming
0768  * values for presentation purposes only
0769  */
0770 static int mcba_net_set_bittiming(struct net_device *netdev)
0771 {
0772     struct mcba_priv *priv = netdev_priv(netdev);
0773     const u16 bitrate_kbps = priv->can.bittiming.bitrate / 1000;
0774 
0775     mcba_usb_xmit_change_bitrate(priv, bitrate_kbps);
0776 
0777     return 0;
0778 }
0779 
0780 static int mcba_set_termination(struct net_device *netdev, u16 term)
0781 {
0782     struct mcba_priv *priv = netdev_priv(netdev);
0783     struct mcba_usb_msg_termination usb_msg = {
0784         .cmd_id = MBCA_CMD_SETUP_TERMINATION_RESISTANCE
0785     };
0786 
0787     if (term == MCBA_TERMINATION_ENABLED)
0788         usb_msg.termination = 1;
0789     else
0790         usb_msg.termination = 0;
0791 
0792     mcba_usb_xmit_cmd(priv, (struct mcba_usb_msg *)&usb_msg);
0793 
0794     return 0;
0795 }
0796 
0797 static int mcba_usb_probe(struct usb_interface *intf,
0798               const struct usb_device_id *id)
0799 {
0800     struct net_device *netdev;
0801     struct mcba_priv *priv;
0802     int err;
0803     struct usb_device *usbdev = interface_to_usbdev(intf);
0804     struct usb_endpoint_descriptor *in, *out;
0805 
0806     err = usb_find_common_endpoints(intf->cur_altsetting, &in, &out, NULL, NULL);
0807     if (err) {
0808         dev_err(&intf->dev, "Can't find endpoints\n");
0809         return err;
0810     }
0811 
0812     netdev = alloc_candev(sizeof(struct mcba_priv), MCBA_MAX_TX_URBS);
0813     if (!netdev) {
0814         dev_err(&intf->dev, "Couldn't alloc candev\n");
0815         return -ENOMEM;
0816     }
0817 
0818     priv = netdev_priv(netdev);
0819 
0820     priv->udev = usbdev;
0821     priv->netdev = netdev;
0822     priv->usb_ka_first_pass = true;
0823     priv->can_ka_first_pass = true;
0824     priv->can_speed_check = false;
0825 
0826     init_usb_anchor(&priv->rx_submitted);
0827     init_usb_anchor(&priv->tx_submitted);
0828 
0829     usb_set_intfdata(intf, priv);
0830 
0831     /* Init CAN device */
0832     priv->can.state = CAN_STATE_STOPPED;
0833     priv->can.termination_const = mcba_termination;
0834     priv->can.termination_const_cnt = ARRAY_SIZE(mcba_termination);
0835     priv->can.bitrate_const = mcba_bitrate;
0836     priv->can.bitrate_const_cnt = ARRAY_SIZE(mcba_bitrate);
0837 
0838     priv->can.do_set_termination = mcba_set_termination;
0839     priv->can.do_set_mode = mcba_net_set_mode;
0840     priv->can.do_get_berr_counter = mcba_net_get_berr_counter;
0841     priv->can.do_set_bittiming = mcba_net_set_bittiming;
0842 
0843     netdev->netdev_ops = &mcba_netdev_ops;
0844     netdev->ethtool_ops = &mcba_ethtool_ops;
0845 
0846     netdev->flags |= IFF_ECHO; /* we support local echo */
0847 
0848     SET_NETDEV_DEV(netdev, &intf->dev);
0849 
0850     err = register_candev(netdev);
0851     if (err) {
0852         netdev_err(netdev, "couldn't register CAN device: %d\n", err);
0853 
0854         goto cleanup_free_candev;
0855     }
0856 
0857     priv->rx_pipe = usb_rcvbulkpipe(priv->udev, in->bEndpointAddress);
0858     priv->tx_pipe = usb_sndbulkpipe(priv->udev, out->bEndpointAddress);
0859 
0860     /* Start USB dev only if we have successfully registered CAN device */
0861     err = mcba_usb_start(priv);
0862     if (err) {
0863         if (err == -ENODEV)
0864             netif_device_detach(priv->netdev);
0865 
0866         netdev_warn(netdev, "couldn't start device: %d\n", err);
0867 
0868         goto cleanup_unregister_candev;
0869     }
0870 
0871     dev_info(&intf->dev, "Microchip CAN BUS Analyzer connected\n");
0872 
0873     return 0;
0874 
0875 cleanup_unregister_candev:
0876     unregister_candev(priv->netdev);
0877 
0878 cleanup_free_candev:
0879     free_candev(netdev);
0880 
0881     return err;
0882 }
0883 
0884 /* Called by the usb core when driver is unloaded or device is removed */
0885 static void mcba_usb_disconnect(struct usb_interface *intf)
0886 {
0887     struct mcba_priv *priv = usb_get_intfdata(intf);
0888 
0889     usb_set_intfdata(intf, NULL);
0890 
0891     netdev_info(priv->netdev, "device disconnected\n");
0892 
0893     unregister_candev(priv->netdev);
0894     mcba_urb_unlink(priv);
0895     free_candev(priv->netdev);
0896 }
0897 
0898 static struct usb_driver mcba_usb_driver = {
0899     .name = MCBA_MODULE_NAME,
0900     .probe = mcba_usb_probe,
0901     .disconnect = mcba_usb_disconnect,
0902     .id_table = mcba_usb_table,
0903 };
0904 
0905 module_usb_driver(mcba_usb_driver);
0906 
0907 MODULE_AUTHOR("Remigiusz Kołłątaj <remigiusz.kollataj@mobica.com>");
0908 MODULE_DESCRIPTION("SocketCAN driver for Microchip CAN BUS Analyzer Tool");
0909 MODULE_LICENSE("GPL v2");