Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * CAN driver for EMS Dr. Thomas Wuensche CPC-USB/ARM7
0004  *
0005  * Copyright (C) 2004-2009 EMS Dr. Thomas Wuensche
0006  */
0007 #include <linux/ethtool.h>
0008 #include <linux/signal.h>
0009 #include <linux/slab.h>
0010 #include <linux/module.h>
0011 #include <linux/netdevice.h>
0012 #include <linux/usb.h>
0013 
0014 #include <linux/can.h>
0015 #include <linux/can/dev.h>
0016 #include <linux/can/error.h>
0017 
0018 MODULE_AUTHOR("Sebastian Haas <haas@ems-wuensche.com>");
0019 MODULE_DESCRIPTION("CAN driver for EMS Dr. Thomas Wuensche CAN/USB interfaces");
0020 MODULE_LICENSE("GPL v2");
0021 
0022 /* Control-Values for CPC_Control() Command Subject Selection */
0023 #define CONTR_CAN_MESSAGE 0x04
0024 #define CONTR_CAN_STATE   0x0C
0025 #define CONTR_BUS_ERROR   0x1C
0026 
0027 /* Control Command Actions */
0028 #define CONTR_CONT_OFF 0
0029 #define CONTR_CONT_ON  1
0030 #define CONTR_ONCE     2
0031 
0032 /* Messages from CPC to PC */
0033 #define CPC_MSG_TYPE_CAN_FRAME       1  /* CAN data frame */
0034 #define CPC_MSG_TYPE_RTR_FRAME       8  /* CAN remote frame */
0035 #define CPC_MSG_TYPE_CAN_PARAMS      12 /* Actual CAN parameters */
0036 #define CPC_MSG_TYPE_CAN_STATE       14 /* CAN state message */
0037 #define CPC_MSG_TYPE_EXT_CAN_FRAME   16 /* Extended CAN data frame */
0038 #define CPC_MSG_TYPE_EXT_RTR_FRAME   17 /* Extended remote frame */
0039 #define CPC_MSG_TYPE_CONTROL         19 /* change interface behavior */
0040 #define CPC_MSG_TYPE_CONFIRM         20 /* command processed confirmation */
0041 #define CPC_MSG_TYPE_OVERRUN         21 /* overrun events */
0042 #define CPC_MSG_TYPE_CAN_FRAME_ERROR 23 /* detected bus errors */
0043 #define CPC_MSG_TYPE_ERR_COUNTER     25 /* RX/TX error counter */
0044 
0045 /* Messages from the PC to the CPC interface  */
0046 #define CPC_CMD_TYPE_CAN_FRAME     1   /* CAN data frame */
0047 #define CPC_CMD_TYPE_CONTROL       3   /* control of interface behavior */
0048 #define CPC_CMD_TYPE_CAN_PARAMS    6   /* set CAN parameters */
0049 #define CPC_CMD_TYPE_RTR_FRAME     13  /* CAN remote frame */
0050 #define CPC_CMD_TYPE_CAN_STATE     14  /* CAN state message */
0051 #define CPC_CMD_TYPE_EXT_CAN_FRAME 15  /* Extended CAN data frame */
0052 #define CPC_CMD_TYPE_EXT_RTR_FRAME 16  /* Extended CAN remote frame */
0053 #define CPC_CMD_TYPE_CAN_EXIT      200 /* exit the CAN */
0054 
0055 #define CPC_CMD_TYPE_INQ_ERR_COUNTER 25 /* request the CAN error counters */
0056 #define CPC_CMD_TYPE_CLEAR_MSG_QUEUE 8  /* clear CPC_MSG queue */
0057 #define CPC_CMD_TYPE_CLEAR_CMD_QUEUE 28 /* clear CPC_CMD queue */
0058 
0059 #define CPC_CC_TYPE_SJA1000 2 /* Philips basic CAN controller */
0060 
0061 #define CPC_CAN_ECODE_ERRFRAME 0x01 /* Ecode type */
0062 
0063 /* Overrun types */
0064 #define CPC_OVR_EVENT_CAN       0x01
0065 #define CPC_OVR_EVENT_CANSTATE  0x02
0066 #define CPC_OVR_EVENT_BUSERROR  0x04
0067 
0068 /*
0069  * If the CAN controller lost a message we indicate it with the highest bit
0070  * set in the count field.
0071  */
0072 #define CPC_OVR_HW 0x80
0073 
0074 /* Size of the "struct ems_cpc_msg" without the union */
0075 #define CPC_MSG_HEADER_LEN   11
0076 #define CPC_CAN_MSG_MIN_SIZE 5
0077 
0078 /* Define these values to match your devices */
0079 #define USB_CPCUSB_VENDOR_ID 0x12D6
0080 
0081 #define USB_CPCUSB_ARM7_PRODUCT_ID 0x0444
0082 
0083 /* Mode register NXP LPC2119/SJA1000 CAN Controller */
0084 #define SJA1000_MOD_NORMAL 0x00
0085 #define SJA1000_MOD_RM     0x01
0086 
0087 /* ECC register NXP LPC2119/SJA1000 CAN Controller */
0088 #define SJA1000_ECC_SEG   0x1F
0089 #define SJA1000_ECC_DIR   0x20
0090 #define SJA1000_ECC_ERR   0x06
0091 #define SJA1000_ECC_BIT   0x00
0092 #define SJA1000_ECC_FORM  0x40
0093 #define SJA1000_ECC_STUFF 0x80
0094 #define SJA1000_ECC_MASK  0xc0
0095 
0096 /* Status register content */
0097 #define SJA1000_SR_BS 0x80
0098 #define SJA1000_SR_ES 0x40
0099 
0100 #define SJA1000_DEFAULT_OUTPUT_CONTROL 0xDA
0101 
0102 /*
0103  * The device actually uses a 16MHz clock to generate the CAN clock
0104  * but it expects SJA1000 bit settings based on 8MHz (is internally
0105  * converted).
0106  */
0107 #define EMS_USB_ARM7_CLOCK 8000000
0108 
0109 #define CPC_TX_QUEUE_TRIGGER_LOW    25
0110 #define CPC_TX_QUEUE_TRIGGER_HIGH   35
0111 
0112 /*
0113  * CAN-Message representation in a CPC_MSG. Message object type is
0114  * CPC_MSG_TYPE_CAN_FRAME or CPC_MSG_TYPE_RTR_FRAME or
0115  * CPC_MSG_TYPE_EXT_CAN_FRAME or CPC_MSG_TYPE_EXT_RTR_FRAME.
0116  */
0117 struct cpc_can_msg {
0118     __le32 id;
0119     u8 length;
0120     u8 msg[8];
0121 };
0122 
0123 /* Representation of the CAN parameters for the SJA1000 controller */
0124 struct cpc_sja1000_params {
0125     u8 mode;
0126     u8 acc_code0;
0127     u8 acc_code1;
0128     u8 acc_code2;
0129     u8 acc_code3;
0130     u8 acc_mask0;
0131     u8 acc_mask1;
0132     u8 acc_mask2;
0133     u8 acc_mask3;
0134     u8 btr0;
0135     u8 btr1;
0136     u8 outp_contr;
0137 };
0138 
0139 /* CAN params message representation */
0140 struct cpc_can_params {
0141     u8 cc_type;
0142 
0143     /* Will support M16C CAN controller in the future */
0144     union {
0145         struct cpc_sja1000_params sja1000;
0146     } cc_params;
0147 };
0148 
0149 /* Structure for confirmed message handling */
0150 struct cpc_confirm {
0151     u8 error; /* error code */
0152 };
0153 
0154 /* Structure for overrun conditions */
0155 struct cpc_overrun {
0156     u8 event;
0157     u8 count;
0158 };
0159 
0160 /* SJA1000 CAN errors (compatible to NXP LPC2119) */
0161 struct cpc_sja1000_can_error {
0162     u8 ecc;
0163     u8 rxerr;
0164     u8 txerr;
0165 };
0166 
0167 /* structure for CAN error conditions */
0168 struct cpc_can_error {
0169     u8 ecode;
0170 
0171     struct {
0172         u8 cc_type;
0173 
0174         /* Other controllers may also provide error code capture regs */
0175         union {
0176             struct cpc_sja1000_can_error sja1000;
0177         } regs;
0178     } cc;
0179 };
0180 
0181 /*
0182  * Structure containing RX/TX error counter. This structure is used to request
0183  * the values of the CAN controllers TX and RX error counter.
0184  */
0185 struct cpc_can_err_counter {
0186     u8 rx;
0187     u8 tx;
0188 };
0189 
0190 /* Main message type used between library and application */
0191 struct __packed ems_cpc_msg {
0192     u8 type;    /* type of message */
0193     u8 length;  /* length of data within union 'msg' */
0194     u8 msgid;   /* confirmation handle */
0195     __le32 ts_sec;  /* timestamp in seconds */
0196     __le32 ts_nsec; /* timestamp in nano seconds */
0197 
0198     union __packed {
0199         u8 generic[64];
0200         struct cpc_can_msg can_msg;
0201         struct cpc_can_params can_params;
0202         struct cpc_confirm confirmation;
0203         struct cpc_overrun overrun;
0204         struct cpc_can_error error;
0205         struct cpc_can_err_counter err_counter;
0206         u8 can_state;
0207     } msg;
0208 };
0209 
0210 /*
0211  * Table of devices that work with this driver
0212  * NOTE: This driver supports only CPC-USB/ARM7 (LPC2119) yet.
0213  */
0214 static struct usb_device_id ems_usb_table[] = {
0215     {USB_DEVICE(USB_CPCUSB_VENDOR_ID, USB_CPCUSB_ARM7_PRODUCT_ID)},
0216     {} /* Terminating entry */
0217 };
0218 
0219 MODULE_DEVICE_TABLE(usb, ems_usb_table);
0220 
0221 #define RX_BUFFER_SIZE      64
0222 #define CPC_HEADER_SIZE     4
0223 #define INTR_IN_BUFFER_SIZE 4
0224 
0225 #define MAX_RX_URBS 10
0226 #define MAX_TX_URBS 10
0227 
0228 struct ems_usb;
0229 
0230 struct ems_tx_urb_context {
0231     struct ems_usb *dev;
0232 
0233     u32 echo_index;
0234 };
0235 
0236 struct ems_usb {
0237     struct can_priv can; /* must be the first member */
0238 
0239     struct sk_buff *echo_skb[MAX_TX_URBS];
0240 
0241     struct usb_device *udev;
0242     struct net_device *netdev;
0243 
0244     atomic_t active_tx_urbs;
0245     struct usb_anchor tx_submitted;
0246     struct ems_tx_urb_context tx_contexts[MAX_TX_URBS];
0247 
0248     struct usb_anchor rx_submitted;
0249 
0250     struct urb *intr_urb;
0251 
0252     u8 *tx_msg_buffer;
0253 
0254     u8 *intr_in_buffer;
0255     unsigned int free_slots; /* remember number of available slots */
0256 
0257     struct ems_cpc_msg active_params; /* active controller parameters */
0258     void *rxbuf[MAX_RX_URBS];
0259     dma_addr_t rxbuf_dma[MAX_RX_URBS];
0260 };
0261 
0262 static void ems_usb_read_interrupt_callback(struct urb *urb)
0263 {
0264     struct ems_usb *dev = urb->context;
0265     struct net_device *netdev = dev->netdev;
0266     int err;
0267 
0268     if (!netif_device_present(netdev))
0269         return;
0270 
0271     switch (urb->status) {
0272     case 0:
0273         dev->free_slots = dev->intr_in_buffer[1];
0274         if (dev->free_slots > CPC_TX_QUEUE_TRIGGER_HIGH &&
0275             netif_queue_stopped(netdev))
0276             netif_wake_queue(netdev);
0277         break;
0278 
0279     case -ECONNRESET: /* unlink */
0280     case -ENOENT:
0281     case -EPIPE:
0282     case -EPROTO:
0283     case -ESHUTDOWN:
0284         return;
0285 
0286     default:
0287         netdev_info(netdev, "Rx interrupt aborted %d\n", urb->status);
0288         break;
0289     }
0290 
0291     err = usb_submit_urb(urb, GFP_ATOMIC);
0292 
0293     if (err == -ENODEV)
0294         netif_device_detach(netdev);
0295     else if (err)
0296         netdev_err(netdev, "failed resubmitting intr urb: %d\n", err);
0297 }
0298 
0299 static void ems_usb_rx_can_msg(struct ems_usb *dev, struct ems_cpc_msg *msg)
0300 {
0301     struct can_frame *cf;
0302     struct sk_buff *skb;
0303     int i;
0304     struct net_device_stats *stats = &dev->netdev->stats;
0305 
0306     skb = alloc_can_skb(dev->netdev, &cf);
0307     if (skb == NULL)
0308         return;
0309 
0310     cf->can_id = le32_to_cpu(msg->msg.can_msg.id);
0311     cf->len = can_cc_dlc2len(msg->msg.can_msg.length & 0xF);
0312 
0313     if (msg->type == CPC_MSG_TYPE_EXT_CAN_FRAME ||
0314         msg->type == CPC_MSG_TYPE_EXT_RTR_FRAME)
0315         cf->can_id |= CAN_EFF_FLAG;
0316 
0317     if (msg->type == CPC_MSG_TYPE_RTR_FRAME ||
0318         msg->type == CPC_MSG_TYPE_EXT_RTR_FRAME) {
0319         cf->can_id |= CAN_RTR_FLAG;
0320     } else {
0321         for (i = 0; i < cf->len; i++)
0322             cf->data[i] = msg->msg.can_msg.msg[i];
0323 
0324         stats->rx_bytes += cf->len;
0325     }
0326     stats->rx_packets++;
0327 
0328     netif_rx(skb);
0329 }
0330 
0331 static void ems_usb_rx_err(struct ems_usb *dev, struct ems_cpc_msg *msg)
0332 {
0333     struct can_frame *cf;
0334     struct sk_buff *skb;
0335     struct net_device_stats *stats = &dev->netdev->stats;
0336 
0337     skb = alloc_can_err_skb(dev->netdev, &cf);
0338     if (skb == NULL)
0339         return;
0340 
0341     if (msg->type == CPC_MSG_TYPE_CAN_STATE) {
0342         u8 state = msg->msg.can_state;
0343 
0344         if (state & SJA1000_SR_BS) {
0345             dev->can.state = CAN_STATE_BUS_OFF;
0346             cf->can_id |= CAN_ERR_BUSOFF;
0347 
0348             dev->can.can_stats.bus_off++;
0349             can_bus_off(dev->netdev);
0350         } else if (state & SJA1000_SR_ES) {
0351             dev->can.state = CAN_STATE_ERROR_WARNING;
0352             dev->can.can_stats.error_warning++;
0353         } else {
0354             dev->can.state = CAN_STATE_ERROR_ACTIVE;
0355             dev->can.can_stats.error_passive++;
0356         }
0357     } else if (msg->type == CPC_MSG_TYPE_CAN_FRAME_ERROR) {
0358         u8 ecc = msg->msg.error.cc.regs.sja1000.ecc;
0359         u8 txerr = msg->msg.error.cc.regs.sja1000.txerr;
0360         u8 rxerr = msg->msg.error.cc.regs.sja1000.rxerr;
0361 
0362         /* bus error interrupt */
0363         dev->can.can_stats.bus_error++;
0364         stats->rx_errors++;
0365 
0366         cf->can_id |= CAN_ERR_PROT | CAN_ERR_BUSERROR;
0367 
0368         switch (ecc & SJA1000_ECC_MASK) {
0369         case SJA1000_ECC_BIT:
0370             cf->data[2] |= CAN_ERR_PROT_BIT;
0371             break;
0372         case SJA1000_ECC_FORM:
0373             cf->data[2] |= CAN_ERR_PROT_FORM;
0374             break;
0375         case SJA1000_ECC_STUFF:
0376             cf->data[2] |= CAN_ERR_PROT_STUFF;
0377             break;
0378         default:
0379             cf->data[3] = ecc & SJA1000_ECC_SEG;
0380             break;
0381         }
0382 
0383         /* Error occurred during transmission? */
0384         if ((ecc & SJA1000_ECC_DIR) == 0)
0385             cf->data[2] |= CAN_ERR_PROT_TX;
0386 
0387         if (dev->can.state == CAN_STATE_ERROR_WARNING ||
0388             dev->can.state == CAN_STATE_ERROR_PASSIVE) {
0389             cf->can_id |= CAN_ERR_CRTL;
0390             cf->data[1] = (txerr > rxerr) ?
0391                 CAN_ERR_CRTL_TX_PASSIVE : CAN_ERR_CRTL_RX_PASSIVE;
0392         }
0393     } else if (msg->type == CPC_MSG_TYPE_OVERRUN) {
0394         cf->can_id |= CAN_ERR_CRTL;
0395         cf->data[1] = CAN_ERR_CRTL_RX_OVERFLOW;
0396 
0397         stats->rx_over_errors++;
0398         stats->rx_errors++;
0399     }
0400 
0401     netif_rx(skb);
0402 }
0403 
0404 /*
0405  * callback for bulk IN urb
0406  */
0407 static void ems_usb_read_bulk_callback(struct urb *urb)
0408 {
0409     struct ems_usb *dev = urb->context;
0410     struct net_device *netdev;
0411     int retval;
0412 
0413     netdev = dev->netdev;
0414 
0415     if (!netif_device_present(netdev))
0416         return;
0417 
0418     switch (urb->status) {
0419     case 0: /* success */
0420         break;
0421 
0422     case -ENOENT:
0423         return;
0424 
0425     default:
0426         netdev_info(netdev, "Rx URB aborted (%d)\n", urb->status);
0427         goto resubmit_urb;
0428     }
0429 
0430     if (urb->actual_length > CPC_HEADER_SIZE) {
0431         struct ems_cpc_msg *msg;
0432         u8 *ibuf = urb->transfer_buffer;
0433         u8 msg_count, start;
0434 
0435         msg_count = ibuf[0] & ~0x80;
0436 
0437         start = CPC_HEADER_SIZE;
0438 
0439         while (msg_count) {
0440             msg = (struct ems_cpc_msg *)&ibuf[start];
0441 
0442             switch (msg->type) {
0443             case CPC_MSG_TYPE_CAN_STATE:
0444                 /* Process CAN state changes */
0445                 ems_usb_rx_err(dev, msg);
0446                 break;
0447 
0448             case CPC_MSG_TYPE_CAN_FRAME:
0449             case CPC_MSG_TYPE_EXT_CAN_FRAME:
0450             case CPC_MSG_TYPE_RTR_FRAME:
0451             case CPC_MSG_TYPE_EXT_RTR_FRAME:
0452                 ems_usb_rx_can_msg(dev, msg);
0453                 break;
0454 
0455             case CPC_MSG_TYPE_CAN_FRAME_ERROR:
0456                 /* Process errorframe */
0457                 ems_usb_rx_err(dev, msg);
0458                 break;
0459 
0460             case CPC_MSG_TYPE_OVERRUN:
0461                 /* Message lost while receiving */
0462                 ems_usb_rx_err(dev, msg);
0463                 break;
0464             }
0465 
0466             start += CPC_MSG_HEADER_LEN + msg->length;
0467             msg_count--;
0468 
0469             if (start > urb->transfer_buffer_length) {
0470                 netdev_err(netdev, "format error\n");
0471                 break;
0472             }
0473         }
0474     }
0475 
0476 resubmit_urb:
0477     usb_fill_bulk_urb(urb, dev->udev, usb_rcvbulkpipe(dev->udev, 2),
0478               urb->transfer_buffer, RX_BUFFER_SIZE,
0479               ems_usb_read_bulk_callback, dev);
0480 
0481     retval = usb_submit_urb(urb, GFP_ATOMIC);
0482 
0483     if (retval == -ENODEV)
0484         netif_device_detach(netdev);
0485     else if (retval)
0486         netdev_err(netdev,
0487                "failed resubmitting read bulk urb: %d\n", retval);
0488 }
0489 
0490 /*
0491  * callback for bulk IN urb
0492  */
0493 static void ems_usb_write_bulk_callback(struct urb *urb)
0494 {
0495     struct ems_tx_urb_context *context = urb->context;
0496     struct ems_usb *dev;
0497     struct net_device *netdev;
0498 
0499     BUG_ON(!context);
0500 
0501     dev = context->dev;
0502     netdev = dev->netdev;
0503 
0504     /* free up our allocated buffer */
0505     usb_free_coherent(urb->dev, urb->transfer_buffer_length,
0506               urb->transfer_buffer, urb->transfer_dma);
0507 
0508     atomic_dec(&dev->active_tx_urbs);
0509 
0510     if (!netif_device_present(netdev))
0511         return;
0512 
0513     if (urb->status)
0514         netdev_info(netdev, "Tx URB aborted (%d)\n", urb->status);
0515 
0516     netif_trans_update(netdev);
0517 
0518     /* transmission complete interrupt */
0519     netdev->stats.tx_packets++;
0520     netdev->stats.tx_bytes += can_get_echo_skb(netdev, context->echo_index,
0521                            NULL);
0522 
0523     /* Release context */
0524     context->echo_index = MAX_TX_URBS;
0525 
0526 }
0527 
0528 /*
0529  * Send the given CPC command synchronously
0530  */
0531 static int ems_usb_command_msg(struct ems_usb *dev, struct ems_cpc_msg *msg)
0532 {
0533     int actual_length;
0534 
0535     /* Copy payload */
0536     memcpy(&dev->tx_msg_buffer[CPC_HEADER_SIZE], msg,
0537            msg->length + CPC_MSG_HEADER_LEN);
0538 
0539     /* Clear header */
0540     memset(&dev->tx_msg_buffer[0], 0, CPC_HEADER_SIZE);
0541 
0542     return usb_bulk_msg(dev->udev, usb_sndbulkpipe(dev->udev, 2),
0543                 &dev->tx_msg_buffer[0],
0544                 msg->length + CPC_MSG_HEADER_LEN + CPC_HEADER_SIZE,
0545                 &actual_length, 1000);
0546 }
0547 
0548 /*
0549  * Change CAN controllers' mode register
0550  */
0551 static int ems_usb_write_mode(struct ems_usb *dev, u8 mode)
0552 {
0553     dev->active_params.msg.can_params.cc_params.sja1000.mode = mode;
0554 
0555     return ems_usb_command_msg(dev, &dev->active_params);
0556 }
0557 
0558 /*
0559  * Send a CPC_Control command to change behaviour when interface receives a CAN
0560  * message, bus error or CAN state changed notifications.
0561  */
0562 static int ems_usb_control_cmd(struct ems_usb *dev, u8 val)
0563 {
0564     struct ems_cpc_msg cmd;
0565 
0566     cmd.type = CPC_CMD_TYPE_CONTROL;
0567     cmd.length = CPC_MSG_HEADER_LEN + 1;
0568 
0569     cmd.msgid = 0;
0570 
0571     cmd.msg.generic[0] = val;
0572 
0573     return ems_usb_command_msg(dev, &cmd);
0574 }
0575 
0576 /*
0577  * Start interface
0578  */
0579 static int ems_usb_start(struct ems_usb *dev)
0580 {
0581     struct net_device *netdev = dev->netdev;
0582     int err, i;
0583 
0584     dev->intr_in_buffer[0] = 0;
0585     dev->free_slots = 50; /* initial size */
0586 
0587     for (i = 0; i < MAX_RX_URBS; i++) {
0588         struct urb *urb = NULL;
0589         u8 *buf = NULL;
0590         dma_addr_t buf_dma;
0591 
0592         /* create a URB, and a buffer for it */
0593         urb = usb_alloc_urb(0, GFP_KERNEL);
0594         if (!urb) {
0595             err = -ENOMEM;
0596             break;
0597         }
0598 
0599         buf = usb_alloc_coherent(dev->udev, RX_BUFFER_SIZE, GFP_KERNEL,
0600                      &buf_dma);
0601         if (!buf) {
0602             netdev_err(netdev, "No memory left for USB buffer\n");
0603             usb_free_urb(urb);
0604             err = -ENOMEM;
0605             break;
0606         }
0607 
0608         urb->transfer_dma = buf_dma;
0609 
0610         usb_fill_bulk_urb(urb, dev->udev, usb_rcvbulkpipe(dev->udev, 2),
0611                   buf, RX_BUFFER_SIZE,
0612                   ems_usb_read_bulk_callback, dev);
0613         urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
0614         usb_anchor_urb(urb, &dev->rx_submitted);
0615 
0616         err = usb_submit_urb(urb, GFP_KERNEL);
0617         if (err) {
0618             usb_unanchor_urb(urb);
0619             usb_free_coherent(dev->udev, RX_BUFFER_SIZE, buf,
0620                       urb->transfer_dma);
0621             usb_free_urb(urb);
0622             break;
0623         }
0624 
0625         dev->rxbuf[i] = buf;
0626         dev->rxbuf_dma[i] = buf_dma;
0627 
0628         /* Drop reference, USB core will take care of freeing it */
0629         usb_free_urb(urb);
0630     }
0631 
0632     /* Did we submit any URBs */
0633     if (i == 0) {
0634         netdev_warn(netdev, "couldn't setup read URBs\n");
0635         return err;
0636     }
0637 
0638     /* Warn if we've couldn't transmit all the URBs */
0639     if (i < MAX_RX_URBS)
0640         netdev_warn(netdev, "rx performance may be slow\n");
0641 
0642     /* Setup and start interrupt URB */
0643     usb_fill_int_urb(dev->intr_urb, dev->udev,
0644              usb_rcvintpipe(dev->udev, 1),
0645              dev->intr_in_buffer,
0646              INTR_IN_BUFFER_SIZE,
0647              ems_usb_read_interrupt_callback, dev, 1);
0648 
0649     err = usb_submit_urb(dev->intr_urb, GFP_KERNEL);
0650     if (err) {
0651         netdev_warn(netdev, "intr URB submit failed: %d\n", err);
0652 
0653         return err;
0654     }
0655 
0656     /* CPC-USB will transfer received message to host */
0657     err = ems_usb_control_cmd(dev, CONTR_CAN_MESSAGE | CONTR_CONT_ON);
0658     if (err)
0659         goto failed;
0660 
0661     /* CPC-USB will transfer CAN state changes to host */
0662     err = ems_usb_control_cmd(dev, CONTR_CAN_STATE | CONTR_CONT_ON);
0663     if (err)
0664         goto failed;
0665 
0666     /* CPC-USB will transfer bus errors to host */
0667     err = ems_usb_control_cmd(dev, CONTR_BUS_ERROR | CONTR_CONT_ON);
0668     if (err)
0669         goto failed;
0670 
0671     err = ems_usb_write_mode(dev, SJA1000_MOD_NORMAL);
0672     if (err)
0673         goto failed;
0674 
0675     dev->can.state = CAN_STATE_ERROR_ACTIVE;
0676 
0677     return 0;
0678 
0679 failed:
0680     netdev_warn(netdev, "couldn't submit control: %d\n", err);
0681 
0682     return err;
0683 }
0684 
0685 static void unlink_all_urbs(struct ems_usb *dev)
0686 {
0687     int i;
0688 
0689     usb_unlink_urb(dev->intr_urb);
0690 
0691     usb_kill_anchored_urbs(&dev->rx_submitted);
0692 
0693     for (i = 0; i < MAX_RX_URBS; ++i)
0694         usb_free_coherent(dev->udev, RX_BUFFER_SIZE,
0695                   dev->rxbuf[i], dev->rxbuf_dma[i]);
0696 
0697     usb_kill_anchored_urbs(&dev->tx_submitted);
0698     atomic_set(&dev->active_tx_urbs, 0);
0699 
0700     for (i = 0; i < MAX_TX_URBS; i++)
0701         dev->tx_contexts[i].echo_index = MAX_TX_URBS;
0702 }
0703 
0704 static int ems_usb_open(struct net_device *netdev)
0705 {
0706     struct ems_usb *dev = netdev_priv(netdev);
0707     int err;
0708 
0709     err = ems_usb_write_mode(dev, SJA1000_MOD_RM);
0710     if (err)
0711         return err;
0712 
0713     /* common open */
0714     err = open_candev(netdev);
0715     if (err)
0716         return err;
0717 
0718     /* finally start device */
0719     err = ems_usb_start(dev);
0720     if (err) {
0721         if (err == -ENODEV)
0722             netif_device_detach(dev->netdev);
0723 
0724         netdev_warn(netdev, "couldn't start device: %d\n", err);
0725 
0726         close_candev(netdev);
0727 
0728         return err;
0729     }
0730 
0731 
0732     netif_start_queue(netdev);
0733 
0734     return 0;
0735 }
0736 
0737 static netdev_tx_t ems_usb_start_xmit(struct sk_buff *skb, struct net_device *netdev)
0738 {
0739     struct ems_usb *dev = netdev_priv(netdev);
0740     struct ems_tx_urb_context *context = NULL;
0741     struct net_device_stats *stats = &netdev->stats;
0742     struct can_frame *cf = (struct can_frame *)skb->data;
0743     struct ems_cpc_msg *msg;
0744     struct urb *urb;
0745     u8 *buf;
0746     int i, err;
0747     size_t size = CPC_HEADER_SIZE + CPC_MSG_HEADER_LEN
0748             + sizeof(struct cpc_can_msg);
0749 
0750     if (can_dropped_invalid_skb(netdev, skb))
0751         return NETDEV_TX_OK;
0752 
0753     /* create a URB, and a buffer for it, and copy the data to the URB */
0754     urb = usb_alloc_urb(0, GFP_ATOMIC);
0755     if (!urb)
0756         goto nomem;
0757 
0758     buf = usb_alloc_coherent(dev->udev, size, GFP_ATOMIC, &urb->transfer_dma);
0759     if (!buf) {
0760         netdev_err(netdev, "No memory left for USB buffer\n");
0761         usb_free_urb(urb);
0762         goto nomem;
0763     }
0764 
0765     msg = (struct ems_cpc_msg *)&buf[CPC_HEADER_SIZE];
0766 
0767     msg->msg.can_msg.id = cpu_to_le32(cf->can_id & CAN_ERR_MASK);
0768     msg->msg.can_msg.length = cf->len;
0769 
0770     if (cf->can_id & CAN_RTR_FLAG) {
0771         msg->type = cf->can_id & CAN_EFF_FLAG ?
0772             CPC_CMD_TYPE_EXT_RTR_FRAME : CPC_CMD_TYPE_RTR_FRAME;
0773 
0774         msg->length = CPC_CAN_MSG_MIN_SIZE;
0775     } else {
0776         msg->type = cf->can_id & CAN_EFF_FLAG ?
0777             CPC_CMD_TYPE_EXT_CAN_FRAME : CPC_CMD_TYPE_CAN_FRAME;
0778 
0779         for (i = 0; i < cf->len; i++)
0780             msg->msg.can_msg.msg[i] = cf->data[i];
0781 
0782         msg->length = CPC_CAN_MSG_MIN_SIZE + cf->len;
0783     }
0784 
0785     for (i = 0; i < MAX_TX_URBS; i++) {
0786         if (dev->tx_contexts[i].echo_index == MAX_TX_URBS) {
0787             context = &dev->tx_contexts[i];
0788             break;
0789         }
0790     }
0791 
0792     /*
0793      * May never happen! When this happens we'd more URBs in flight as
0794      * allowed (MAX_TX_URBS).
0795      */
0796     if (!context) {
0797         usb_free_coherent(dev->udev, size, buf, urb->transfer_dma);
0798         usb_free_urb(urb);
0799 
0800         netdev_warn(netdev, "couldn't find free context\n");
0801 
0802         return NETDEV_TX_BUSY;
0803     }
0804 
0805     context->dev = dev;
0806     context->echo_index = i;
0807 
0808     usb_fill_bulk_urb(urb, dev->udev, usb_sndbulkpipe(dev->udev, 2), buf,
0809               size, ems_usb_write_bulk_callback, context);
0810     urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
0811     usb_anchor_urb(urb, &dev->tx_submitted);
0812 
0813     can_put_echo_skb(skb, netdev, context->echo_index, 0);
0814 
0815     atomic_inc(&dev->active_tx_urbs);
0816 
0817     err = usb_submit_urb(urb, GFP_ATOMIC);
0818     if (unlikely(err)) {
0819         can_free_echo_skb(netdev, context->echo_index, NULL);
0820 
0821         usb_unanchor_urb(urb);
0822         usb_free_coherent(dev->udev, size, buf, urb->transfer_dma);
0823 
0824         atomic_dec(&dev->active_tx_urbs);
0825 
0826         if (err == -ENODEV) {
0827             netif_device_detach(netdev);
0828         } else {
0829             netdev_warn(netdev, "failed tx_urb %d\n", err);
0830 
0831             stats->tx_dropped++;
0832         }
0833     } else {
0834         netif_trans_update(netdev);
0835 
0836         /* Slow down tx path */
0837         if (atomic_read(&dev->active_tx_urbs) >= MAX_TX_URBS ||
0838             dev->free_slots < CPC_TX_QUEUE_TRIGGER_LOW) {
0839             netif_stop_queue(netdev);
0840         }
0841     }
0842 
0843     /*
0844      * Release our reference to this URB, the USB core will eventually free
0845      * it entirely.
0846      */
0847     usb_free_urb(urb);
0848 
0849     return NETDEV_TX_OK;
0850 
0851 nomem:
0852     dev_kfree_skb(skb);
0853     stats->tx_dropped++;
0854 
0855     return NETDEV_TX_OK;
0856 }
0857 
0858 static int ems_usb_close(struct net_device *netdev)
0859 {
0860     struct ems_usb *dev = netdev_priv(netdev);
0861 
0862     /* Stop polling */
0863     unlink_all_urbs(dev);
0864 
0865     netif_stop_queue(netdev);
0866 
0867     /* Set CAN controller to reset mode */
0868     if (ems_usb_write_mode(dev, SJA1000_MOD_RM))
0869         netdev_warn(netdev, "couldn't stop device");
0870 
0871     close_candev(netdev);
0872 
0873     return 0;
0874 }
0875 
0876 static const struct net_device_ops ems_usb_netdev_ops = {
0877     .ndo_open = ems_usb_open,
0878     .ndo_stop = ems_usb_close,
0879     .ndo_start_xmit = ems_usb_start_xmit,
0880     .ndo_change_mtu = can_change_mtu,
0881 };
0882 
0883 static const struct ethtool_ops ems_usb_ethtool_ops = {
0884     .get_ts_info = ethtool_op_get_ts_info,
0885 };
0886 
0887 static const struct can_bittiming_const ems_usb_bittiming_const = {
0888     .name = KBUILD_MODNAME,
0889     .tseg1_min = 1,
0890     .tseg1_max = 16,
0891     .tseg2_min = 1,
0892     .tseg2_max = 8,
0893     .sjw_max = 4,
0894     .brp_min = 1,
0895     .brp_max = 64,
0896     .brp_inc = 1,
0897 };
0898 
0899 static int ems_usb_set_mode(struct net_device *netdev, enum can_mode mode)
0900 {
0901     struct ems_usb *dev = netdev_priv(netdev);
0902 
0903     switch (mode) {
0904     case CAN_MODE_START:
0905         if (ems_usb_write_mode(dev, SJA1000_MOD_NORMAL))
0906             netdev_warn(netdev, "couldn't start device");
0907 
0908         if (netif_queue_stopped(netdev))
0909             netif_wake_queue(netdev);
0910         break;
0911 
0912     default:
0913         return -EOPNOTSUPP;
0914     }
0915 
0916     return 0;
0917 }
0918 
0919 static int ems_usb_set_bittiming(struct net_device *netdev)
0920 {
0921     struct ems_usb *dev = netdev_priv(netdev);
0922     struct can_bittiming *bt = &dev->can.bittiming;
0923     u8 btr0, btr1;
0924 
0925     btr0 = ((bt->brp - 1) & 0x3f) | (((bt->sjw - 1) & 0x3) << 6);
0926     btr1 = ((bt->prop_seg + bt->phase_seg1 - 1) & 0xf) |
0927         (((bt->phase_seg2 - 1) & 0x7) << 4);
0928     if (dev->can.ctrlmode & CAN_CTRLMODE_3_SAMPLES)
0929         btr1 |= 0x80;
0930 
0931     netdev_info(netdev, "setting BTR0=0x%02x BTR1=0x%02x\n", btr0, btr1);
0932 
0933     dev->active_params.msg.can_params.cc_params.sja1000.btr0 = btr0;
0934     dev->active_params.msg.can_params.cc_params.sja1000.btr1 = btr1;
0935 
0936     return ems_usb_command_msg(dev, &dev->active_params);
0937 }
0938 
0939 static void init_params_sja1000(struct ems_cpc_msg *msg)
0940 {
0941     struct cpc_sja1000_params *sja1000 =
0942         &msg->msg.can_params.cc_params.sja1000;
0943 
0944     msg->type = CPC_CMD_TYPE_CAN_PARAMS;
0945     msg->length = sizeof(struct cpc_can_params);
0946     msg->msgid = 0;
0947 
0948     msg->msg.can_params.cc_type = CPC_CC_TYPE_SJA1000;
0949 
0950     /* Acceptance filter open */
0951     sja1000->acc_code0 = 0x00;
0952     sja1000->acc_code1 = 0x00;
0953     sja1000->acc_code2 = 0x00;
0954     sja1000->acc_code3 = 0x00;
0955 
0956     /* Acceptance filter open */
0957     sja1000->acc_mask0 = 0xFF;
0958     sja1000->acc_mask1 = 0xFF;
0959     sja1000->acc_mask2 = 0xFF;
0960     sja1000->acc_mask3 = 0xFF;
0961 
0962     sja1000->btr0 = 0;
0963     sja1000->btr1 = 0;
0964 
0965     sja1000->outp_contr = SJA1000_DEFAULT_OUTPUT_CONTROL;
0966     sja1000->mode = SJA1000_MOD_RM;
0967 }
0968 
0969 /*
0970  * probe function for new CPC-USB devices
0971  */
0972 static int ems_usb_probe(struct usb_interface *intf,
0973              const struct usb_device_id *id)
0974 {
0975     struct net_device *netdev;
0976     struct ems_usb *dev;
0977     int i, err = -ENOMEM;
0978 
0979     netdev = alloc_candev(sizeof(struct ems_usb), MAX_TX_URBS);
0980     if (!netdev) {
0981         dev_err(&intf->dev, "ems_usb: Couldn't alloc candev\n");
0982         return -ENOMEM;
0983     }
0984 
0985     dev = netdev_priv(netdev);
0986 
0987     dev->udev = interface_to_usbdev(intf);
0988     dev->netdev = netdev;
0989 
0990     dev->can.state = CAN_STATE_STOPPED;
0991     dev->can.clock.freq = EMS_USB_ARM7_CLOCK;
0992     dev->can.bittiming_const = &ems_usb_bittiming_const;
0993     dev->can.do_set_bittiming = ems_usb_set_bittiming;
0994     dev->can.do_set_mode = ems_usb_set_mode;
0995     dev->can.ctrlmode_supported = CAN_CTRLMODE_3_SAMPLES;
0996 
0997     netdev->netdev_ops = &ems_usb_netdev_ops;
0998     netdev->ethtool_ops = &ems_usb_ethtool_ops;
0999 
1000     netdev->flags |= IFF_ECHO; /* we support local echo */
1001 
1002     init_usb_anchor(&dev->rx_submitted);
1003 
1004     init_usb_anchor(&dev->tx_submitted);
1005     atomic_set(&dev->active_tx_urbs, 0);
1006 
1007     for (i = 0; i < MAX_TX_URBS; i++)
1008         dev->tx_contexts[i].echo_index = MAX_TX_URBS;
1009 
1010     dev->intr_urb = usb_alloc_urb(0, GFP_KERNEL);
1011     if (!dev->intr_urb)
1012         goto cleanup_candev;
1013 
1014     dev->intr_in_buffer = kzalloc(INTR_IN_BUFFER_SIZE, GFP_KERNEL);
1015     if (!dev->intr_in_buffer)
1016         goto cleanup_intr_urb;
1017 
1018     dev->tx_msg_buffer = kzalloc(CPC_HEADER_SIZE +
1019                      sizeof(struct ems_cpc_msg), GFP_KERNEL);
1020     if (!dev->tx_msg_buffer)
1021         goto cleanup_intr_in_buffer;
1022 
1023     usb_set_intfdata(intf, dev);
1024 
1025     SET_NETDEV_DEV(netdev, &intf->dev);
1026 
1027     init_params_sja1000(&dev->active_params);
1028 
1029     err = ems_usb_command_msg(dev, &dev->active_params);
1030     if (err) {
1031         netdev_err(netdev, "couldn't initialize controller: %d\n", err);
1032         goto cleanup_tx_msg_buffer;
1033     }
1034 
1035     err = register_candev(netdev);
1036     if (err) {
1037         netdev_err(netdev, "couldn't register CAN device: %d\n", err);
1038         goto cleanup_tx_msg_buffer;
1039     }
1040 
1041     return 0;
1042 
1043 cleanup_tx_msg_buffer:
1044     kfree(dev->tx_msg_buffer);
1045 
1046 cleanup_intr_in_buffer:
1047     kfree(dev->intr_in_buffer);
1048 
1049 cleanup_intr_urb:
1050     usb_free_urb(dev->intr_urb);
1051 
1052 cleanup_candev:
1053     free_candev(netdev);
1054 
1055     return err;
1056 }
1057 
1058 /*
1059  * called by the usb core when the device is removed from the system
1060  */
1061 static void ems_usb_disconnect(struct usb_interface *intf)
1062 {
1063     struct ems_usb *dev = usb_get_intfdata(intf);
1064 
1065     usb_set_intfdata(intf, NULL);
1066 
1067     if (dev) {
1068         unregister_netdev(dev->netdev);
1069 
1070         unlink_all_urbs(dev);
1071 
1072         usb_free_urb(dev->intr_urb);
1073 
1074         kfree(dev->intr_in_buffer);
1075         kfree(dev->tx_msg_buffer);
1076 
1077         free_candev(dev->netdev);
1078     }
1079 }
1080 
1081 /* usb specific object needed to register this driver with the usb subsystem */
1082 static struct usb_driver ems_usb_driver = {
1083     .name = KBUILD_MODNAME,
1084     .probe = ems_usb_probe,
1085     .disconnect = ems_usb_disconnect,
1086     .id_table = ems_usb_table,
1087 };
1088 
1089 module_usb_driver(ems_usb_driver);