1 // SPDX-License-Identifier: GPL-2.0-only
3 * CAN driver for esd electronics gmbh CAN-USB/2, CAN-USB/3 and CAN-USB/Micro
5 * Copyright (C) 2010-2012 esd electronic system design gmbh, Matthias Fuchs <
[email protected]>
10 #include <linux/can/dev.h>
11 #include <linux/can/error.h>
12 #include <linux/ethtool.h>
13 #include <linux/module.h>
14 #include <linux/netdevice.h>
15 #include <linux/signal.h>
16 #include <linux/slab.h>
17 #include <linux/units.h>
18 #include <linux/usb.h>
22 MODULE_DESCRIPTION("CAN driver for esd electronics gmbh CAN-USB/2, CAN-USB/3 and CAN-USB/Micro interfaces");
23 MODULE_LICENSE("GPL v2");
25 /* USB vendor and product ID */
26 #define ESD_USB_ESDGMBH_VENDOR_ID 0x0ab4
27 #define ESD_USB_CANUSB2_PRODUCT_ID 0x0010
28 #define ESD_USB_CANUSBM_PRODUCT_ID 0x0011
29 #define ESD_USB_CANUSB3_PRODUCT_ID 0x0014
31 /* CAN controller clock frequencies */
32 #define ESD_USB_2_CAN_CLOCK (60 * MEGA) /* Hz */
33 #define ESD_USB_M_CAN_CLOCK (36 * MEGA) /* Hz */
34 #define ESD_USB_3_CAN_CLOCK (80 * MEGA) /* Hz */
36 /* Maximum number of CAN nets */
37 #define ESD_USB_MAX_NETS 2
40 #define ESD_USB_CMD_VERSION 1 /* also used for VERSION_REPLY */
41 #define ESD_USB_CMD_CAN_RX 2 /* device to host only */
42 #define ESD_USB_CMD_CAN_TX 3 /* also used for TX_DONE */
43 #define ESD_USB_CMD_SETBAUD 4 /* also used for SETBAUD_REPLY */
44 #define ESD_USB_CMD_TS 5 /* also used for TS_REPLY */
45 #define ESD_USB_CMD_IDADD 6 /* also used for IDADD_REPLY */
47 /* esd CAN message flags - dlc field */
48 #define ESD_USB_RTR BIT(4)
49 #define ESD_USB_NO_BRS BIT(4)
50 #define ESD_USB_ESI BIT(5)
51 #define ESD_USB_FD BIT(7)
53 /* esd CAN message flags - id field */
54 #define ESD_USB_EXTID BIT(29)
55 #define ESD_USB_EVENT BIT(30)
56 #define ESD_USB_IDMASK GENMASK(28, 0)
58 /* esd CAN event ids */
59 #define ESD_USB_EV_CAN_ERROR_EXT 2 /* CAN controller specific diagnostic data */
61 /* baudrate message flags */
62 #define ESD_USB_LOM BIT(30) /* Listen Only Mode */
63 #define ESD_USB_UBR BIT(31) /* User Bit Rate (controller BTR) in bits 0..27 */
64 #define ESD_USB_NO_BAUDRATE GENMASK(30, 0) /* bit rate unconfigured */
66 /* bit timing esd CAN-USB */
67 #define ESD_USB_2_TSEG1_SHIFT 16
68 #define ESD_USB_2_TSEG2_SHIFT 20
69 #define ESD_USB_2_SJW_SHIFT 14
70 #define ESD_USB_M_SJW_SHIFT 24
71 #define ESD_USB_TRIPLE_SAMPLES BIT(23)
73 /* Transmitter Delay Compensation */
74 #define ESD_USB_3_TDC_MODE_AUTO 0
76 /* esd IDADD message */
77 #define ESD_USB_ID_ENABLE BIT(7)
78 #define ESD_USB_MAX_ID_SEGMENT 64
80 /* SJA1000 ECC register (emulated by usb firmware) */
81 #define ESD_USB_SJA1000_ECC_SEG GENMASK(4, 0)
82 #define ESD_USB_SJA1000_ECC_DIR BIT(5)
83 #define ESD_USB_SJA1000_ECC_ERR BIT(2, 1)
84 #define ESD_USB_SJA1000_ECC_BIT 0x00
85 #define ESD_USB_SJA1000_ECC_FORM BIT(6)
86 #define ESD_USB_SJA1000_ECC_STUFF BIT(7)
87 #define ESD_USB_SJA1000_ECC_MASK GENMASK(7, 6)
89 /* esd bus state event codes */
90 #define ESD_USB_BUSSTATE_MASK GENMASK(7, 6)
91 #define ESD_USB_BUSSTATE_WARN BIT(6)
92 #define ESD_USB_BUSSTATE_ERRPASSIVE BIT(7)
93 #define ESD_USB_BUSSTATE_BUSOFF GENMASK(7, 6)
95 #define ESD_USB_RX_BUFFER_SIZE 1024
96 #define ESD_USB_MAX_RX_URBS 4
97 #define ESD_USB_MAX_TX_URBS 16 /* must be power of 2 */
99 /* Modes for CAN-USB/3, to be used for esd_usb_3_set_baudrate_msg_x.mode */
100 #define ESD_USB_3_BAUDRATE_MODE_DISABLE 0 /* remove from bus */
101 #define ESD_USB_3_BAUDRATE_MODE_INDEX 1 /* ESD (CiA) bit rate idx */
102 #define ESD_USB_3_BAUDRATE_MODE_BTR_CTRL 2 /* BTR values (controller)*/
103 #define ESD_USB_3_BAUDRATE_MODE_BTR_CANONICAL 3 /* BTR values (canonical) */
104 #define ESD_USB_3_BAUDRATE_MODE_NUM 4 /* numerical bit rate */
105 #define ESD_USB_3_BAUDRATE_MODE_AUTOBAUD 5 /* autobaud */
107 /* Flags for CAN-USB/3, to be used for esd_usb_3_set_baudrate_msg_x.flags */
108 #define ESD_USB_3_BAUDRATE_FLAG_FD BIT(0) /* enable CAN FD mode */
109 #define ESD_USB_3_BAUDRATE_FLAG_LOM BIT(1) /* enable listen only mode */
110 #define ESD_USB_3_BAUDRATE_FLAG_STM BIT(2) /* enable self test mode */
111 #define ESD_USB_3_BAUDRATE_FLAG_TRS BIT(3) /* enable triple sampling */
112 #define ESD_USB_3_BAUDRATE_FLAG_TXP BIT(4) /* enable transmit pause */
114 struct esd_usb_header_msg {
115 u8 len; /* total message length in 32bit words */
120 struct esd_usb_version_msg {
121 u8 len; /* total message length in 32bit words */
128 struct esd_usb_version_reply_msg {
129 u8 len; /* total message length in 32bit words */
139 struct esd_usb_rx_msg {
140 u8 len; /* total message length in 32bit words */
145 __le32 id; /* upper 3 bits contain flags */
147 u8 data[CAN_MAX_DLEN];
148 u8 data_fd[CANFD_MAX_DLEN];
150 u8 status; /* CAN Controller Status */
151 u8 ecc; /* Error Capture Register */
152 u8 rec; /* RX Error Counter */
153 u8 tec; /* TX Error Counter */
154 } ev_can_err_ext; /* For ESD_EV_CAN_ERROR_EXT */
158 struct esd_usb_tx_msg {
159 u8 len; /* total message length in 32bit words */
163 u32 hnd; /* opaque handle, not used by device */
164 __le32 id; /* upper 3 bits contain flags */
166 u8 data[CAN_MAX_DLEN];
167 u8 data_fd[CANFD_MAX_DLEN];
171 struct esd_usb_tx_done_msg {
172 u8 len; /* total message length in 32bit words */
176 u32 hnd; /* opaque handle, not used by device */
180 struct esd_usb_id_filter_msg {
181 u8 len; /* total message length in 32bit words */
185 __le32 mask[ESD_USB_MAX_ID_SEGMENT + 1]; /* +1 for 29bit extended IDs */
188 struct esd_usb_set_baudrate_msg {
189 u8 len; /* total message length in 32bit words */
196 /* CAN-USB/3 baudrate configuration, used for nominal as well as for data bit rate */
197 struct esd_usb_3_baudrate_cfg {
198 __le16 brp; /* bit rate pre-scaler */
199 __le16 tseg1; /* time segment before sample point */
200 __le16 tseg2; /* time segment after sample point */
201 __le16 sjw; /* synchronization jump Width */
204 /* In principle, the esd CAN-USB/3 supports Transmitter Delay Compensation (TDC),
205 * but currently only the automatic TDC mode is supported by this driver.
206 * An implementation for manual TDC configuration will follow.
208 * For information about struct esd_usb_3_tdc_cfg, see
209 * NTCAN Application Developers Manual, 6.2.25 NTCAN_TDC_CFG + related chapters
210 * https://esd.eu/fileadmin/esd/docs/manuals/NTCAN_Part1_Function_API_Manual_en_56.pdf
212 struct esd_usb_3_tdc_cfg {
213 u8 tdc_mode; /* transmitter delay compensation mode */
214 u8 ssp_offset; /* secondary sample point offset in mtq */
215 s8 ssp_shift; /* secondary sample point shift in mtq */
216 u8 tdc_filter; /* TDC filter in mtq */
219 /* Extended version of the above set_baudrate_msg for a CAN-USB/3
220 * to define the CAN bit timing configuration of the CAN controller in
221 * CAN FD mode as well as in Classical CAN mode.
223 * The payload of this command is a NTCAN_BAUDRATE_X structure according to
224 * esd electronics gmbh, NTCAN Application Developers Manual, 6.2.15 NTCAN_BAUDRATE_X
225 * https://esd.eu/fileadmin/esd/docs/manuals/NTCAN_Part1_Function_API_Manual_en_56.pdf
227 struct esd_usb_3_set_baudrate_msg_x {
228 u8 len; /* total message length in 32bit words */
231 u8 rsvd; /*reserved */
233 __le16 mode; /* mode word, see ESD_USB_3_BAUDRATE_MODE_xxx */
234 __le16 flags; /* control flags, see ESD_USB_3_BAUDRATE_FLAG_xxx */
235 struct esd_usb_3_tdc_cfg tdc; /* TDC configuration */
236 struct esd_usb_3_baudrate_cfg nom; /* nominal bit rate */
237 struct esd_usb_3_baudrate_cfg data; /* data bit rate */
240 /* Main message type used between library and application */
241 union __packed esd_usb_msg {
242 struct esd_usb_header_msg hdr;
243 struct esd_usb_version_msg version;
244 struct esd_usb_version_reply_msg version_reply;
245 struct esd_usb_rx_msg rx;
246 struct esd_usb_tx_msg tx;
247 struct esd_usb_tx_done_msg txdone;
248 struct esd_usb_set_baudrate_msg setbaud;
249 struct esd_usb_3_set_baudrate_msg_x setbaud_x;
250 struct esd_usb_id_filter_msg filter;
253 static struct usb_device_id esd_usb_table[] = {
254 {USB_DEVICE(ESD_USB_ESDGMBH_VENDOR_ID, ESD_USB_CANUSB2_PRODUCT_ID)},
255 {USB_DEVICE(ESD_USB_ESDGMBH_VENDOR_ID, ESD_USB_CANUSBM_PRODUCT_ID)},
256 {USB_DEVICE(ESD_USB_ESDGMBH_VENDOR_ID, ESD_USB_CANUSB3_PRODUCT_ID)},
259 MODULE_DEVICE_TABLE(usb, esd_usb_table);
261 struct esd_usb_net_priv;
263 struct esd_tx_urb_context {
264 struct esd_usb_net_priv *priv;
269 struct usb_device *udev;
270 struct esd_usb_net_priv *nets[ESD_USB_MAX_NETS];
272 struct usb_anchor rx_submitted;
277 void *rxbuf[ESD_USB_MAX_RX_URBS];
278 dma_addr_t rxbuf_dma[ESD_USB_MAX_RX_URBS];
281 struct esd_usb_net_priv {
282 struct can_priv can; /* must be the first member */
284 atomic_t active_tx_jobs;
285 struct usb_anchor tx_submitted;
286 struct esd_tx_urb_context tx_contexts[ESD_USB_MAX_TX_URBS];
289 struct net_device *netdev;
292 struct can_berr_counter bec;
295 static void esd_usb_rx_event(struct esd_usb_net_priv *priv,
296 union esd_usb_msg *msg)
298 struct net_device_stats *stats = &priv->netdev->stats;
299 struct can_frame *cf;
301 u32 id = le32_to_cpu(msg->rx.id) & ESD_USB_IDMASK;
303 if (id == ESD_USB_EV_CAN_ERROR_EXT) {
304 u8 state = msg->rx.ev_can_err_ext.status;
305 u8 ecc = msg->rx.ev_can_err_ext.ecc;
307 priv->bec.rxerr = msg->rx.ev_can_err_ext.rec;
308 priv->bec.txerr = msg->rx.ev_can_err_ext.tec;
310 netdev_dbg(priv->netdev,
311 "CAN_ERR_EV_EXT: dlc=%#02x state=%02x ecc=%02x rec=%02x tec=%02x\n",
312 msg->rx.dlc, state, ecc,
313 priv->bec.rxerr, priv->bec.txerr);
315 /* if berr-reporting is off, only pass through on state change ... */
316 if (!(priv->can.ctrlmode & CAN_CTRLMODE_BERR_REPORTING) &&
317 state == priv->old_state)
320 skb = alloc_can_err_skb(priv->netdev, &cf);
324 if (state != priv->old_state) {
325 enum can_state tx_state, rx_state;
326 enum can_state new_state = CAN_STATE_ERROR_ACTIVE;
328 priv->old_state = state;
330 switch (state & ESD_USB_BUSSTATE_MASK) {
331 case ESD_USB_BUSSTATE_BUSOFF:
332 new_state = CAN_STATE_BUS_OFF;
333 can_bus_off(priv->netdev);
335 case ESD_USB_BUSSTATE_WARN:
336 new_state = CAN_STATE_ERROR_WARNING;
338 case ESD_USB_BUSSTATE_ERRPASSIVE:
339 new_state = CAN_STATE_ERROR_PASSIVE;
342 new_state = CAN_STATE_ERROR_ACTIVE;
348 if (new_state != priv->can.state) {
349 tx_state = (priv->bec.txerr >= priv->bec.rxerr) ? new_state : 0;
350 rx_state = (priv->bec.txerr <= priv->bec.rxerr) ? new_state : 0;
351 can_change_state(priv->netdev, cf,
355 priv->can.can_stats.bus_error++;
358 cf->can_id |= CAN_ERR_PROT | CAN_ERR_BUSERROR;
360 switch (ecc & ESD_USB_SJA1000_ECC_MASK) {
361 case ESD_USB_SJA1000_ECC_BIT:
362 cf->data[2] |= CAN_ERR_PROT_BIT;
364 case ESD_USB_SJA1000_ECC_FORM:
365 cf->data[2] |= CAN_ERR_PROT_FORM;
367 case ESD_USB_SJA1000_ECC_STUFF:
368 cf->data[2] |= CAN_ERR_PROT_STUFF;
374 /* Error occurred during transmission? */
375 if (!(ecc & ESD_USB_SJA1000_ECC_DIR))
376 cf->data[2] |= CAN_ERR_PROT_TX;
378 /* Bit stream position in CAN frame as the error was detected */
379 cf->data[3] = ecc & ESD_USB_SJA1000_ECC_SEG;
383 cf->can_id |= CAN_ERR_CNT;
384 cf->data[6] = priv->bec.txerr;
385 cf->data[7] = priv->bec.rxerr;
392 static void esd_usb_rx_can_msg(struct esd_usb_net_priv *priv,
393 union esd_usb_msg *msg)
395 struct net_device_stats *stats = &priv->netdev->stats;
396 struct can_frame *cf;
397 struct canfd_frame *cfd;
402 if (!netif_device_present(priv->netdev))
405 id = le32_to_cpu(msg->rx.id);
407 if (id & ESD_USB_EVENT) {
408 esd_usb_rx_event(priv, msg);
410 if (msg->rx.dlc & ESD_USB_FD) {
411 skb = alloc_canfd_skb(priv->netdev, &cfd);
413 skb = alloc_can_skb(priv->netdev, &cf);
414 cfd = (struct canfd_frame *)cf;
422 cfd->can_id = id & ESD_USB_IDMASK;
424 if (msg->rx.dlc & ESD_USB_FD) {
425 /* masking by 0x0F is already done within can_fd_dlc2len() */
426 cfd->len = can_fd_dlc2len(msg->rx.dlc);
428 if ((msg->rx.dlc & ESD_USB_NO_BRS) == 0)
429 cfd->flags |= CANFD_BRS;
430 if (msg->rx.dlc & ESD_USB_ESI)
431 cfd->flags |= CANFD_ESI;
433 can_frame_set_cc_len(cf, msg->rx.dlc & ~ESD_USB_RTR, priv->can.ctrlmode);
435 if (msg->rx.dlc & ESD_USB_RTR) {
436 cf->can_id |= CAN_RTR_FLAG;
441 if (id & ESD_USB_EXTID)
442 cfd->can_id |= CAN_EFF_FLAG;
444 memcpy(cfd->data, msg->rx.data_fd, len);
445 stats->rx_bytes += len;
452 static void esd_usb_tx_done_msg(struct esd_usb_net_priv *priv,
453 union esd_usb_msg *msg)
455 struct net_device_stats *stats = &priv->netdev->stats;
456 struct net_device *netdev = priv->netdev;
457 struct esd_tx_urb_context *context;
459 if (!netif_device_present(netdev))
462 context = &priv->tx_contexts[msg->txdone.hnd & (ESD_USB_MAX_TX_URBS - 1)];
464 if (!msg->txdone.status) {
466 stats->tx_bytes += can_get_echo_skb(netdev, context->echo_index,
470 can_free_echo_skb(netdev, context->echo_index, NULL);
473 /* Release context */
474 context->echo_index = ESD_USB_MAX_TX_URBS;
475 atomic_dec(&priv->active_tx_jobs);
477 netif_wake_queue(netdev);
480 static void esd_usb_read_bulk_callback(struct urb *urb)
482 struct esd_usb *dev = urb->context;
487 switch (urb->status) {
488 case 0: /* success */
498 dev_info(dev->udev->dev.parent,
499 "Rx URB aborted (%d)\n", urb->status);
503 while (pos < urb->actual_length) {
504 union esd_usb_msg *msg;
506 msg = (union esd_usb_msg *)(urb->transfer_buffer + pos);
508 switch (msg->hdr.cmd) {
509 case ESD_USB_CMD_CAN_RX:
510 if (msg->rx.net >= dev->net_count) {
511 dev_err(dev->udev->dev.parent, "format error\n");
515 esd_usb_rx_can_msg(dev->nets[msg->rx.net], msg);
518 case ESD_USB_CMD_CAN_TX:
519 if (msg->txdone.net >= dev->net_count) {
520 dev_err(dev->udev->dev.parent, "format error\n");
524 esd_usb_tx_done_msg(dev->nets[msg->txdone.net],
529 pos += msg->hdr.len * sizeof(u32); /* convert to # of bytes */
531 if (pos > urb->actual_length) {
532 dev_err(dev->udev->dev.parent, "format error\n");
538 usb_fill_bulk_urb(urb, dev->udev, usb_rcvbulkpipe(dev->udev, 1),
539 urb->transfer_buffer, ESD_USB_RX_BUFFER_SIZE,
540 esd_usb_read_bulk_callback, dev);
542 retval = usb_submit_urb(urb, GFP_ATOMIC);
543 if (retval == -ENODEV) {
544 for (i = 0; i < dev->net_count; i++) {
546 netif_device_detach(dev->nets[i]->netdev);
549 dev_err(dev->udev->dev.parent,
550 "failed resubmitting read bulk urb: %d\n", retval);
554 /* callback for bulk IN urb */
555 static void esd_usb_write_bulk_callback(struct urb *urb)
557 struct esd_tx_urb_context *context = urb->context;
558 struct esd_usb_net_priv *priv;
559 struct net_device *netdev;
560 size_t size = sizeof(union esd_usb_msg);
564 priv = context->priv;
565 netdev = priv->netdev;
567 /* free up our allocated buffer */
568 usb_free_coherent(urb->dev, size,
569 urb->transfer_buffer, urb->transfer_dma);
571 if (!netif_device_present(netdev))
575 netdev_info(netdev, "Tx URB aborted (%d)\n", urb->status);
577 netif_trans_update(netdev);
580 static ssize_t firmware_show(struct device *d,
581 struct device_attribute *attr, char *buf)
583 struct usb_interface *intf = to_usb_interface(d);
584 struct esd_usb *dev = usb_get_intfdata(intf);
586 return sprintf(buf, "%d.%d.%d\n",
587 (dev->version >> 12) & 0xf,
588 (dev->version >> 8) & 0xf,
589 dev->version & 0xff);
591 static DEVICE_ATTR_RO(firmware);
593 static ssize_t hardware_show(struct device *d,
594 struct device_attribute *attr, char *buf)
596 struct usb_interface *intf = to_usb_interface(d);
597 struct esd_usb *dev = usb_get_intfdata(intf);
599 return sprintf(buf, "%d.%d.%d\n",
600 (dev->version >> 28) & 0xf,
601 (dev->version >> 24) & 0xf,
602 (dev->version >> 16) & 0xff);
604 static DEVICE_ATTR_RO(hardware);
606 static ssize_t nets_show(struct device *d,
607 struct device_attribute *attr, char *buf)
609 struct usb_interface *intf = to_usb_interface(d);
610 struct esd_usb *dev = usb_get_intfdata(intf);
612 return sprintf(buf, "%d", dev->net_count);
614 static DEVICE_ATTR_RO(nets);
616 static int esd_usb_send_msg(struct esd_usb *dev, union esd_usb_msg *msg)
620 return usb_bulk_msg(dev->udev,
621 usb_sndbulkpipe(dev->udev, 2),
623 msg->hdr.len * sizeof(u32), /* convert to # of bytes */
628 static int esd_usb_wait_msg(struct esd_usb *dev,
629 union esd_usb_msg *msg)
633 return usb_bulk_msg(dev->udev,
634 usb_rcvbulkpipe(dev->udev, 1),
641 static int esd_usb_setup_rx_urbs(struct esd_usb *dev)
648 for (i = 0; i < ESD_USB_MAX_RX_URBS; i++) {
649 struct urb *urb = NULL;
653 /* create a URB, and a buffer for it */
654 urb = usb_alloc_urb(0, GFP_KERNEL);
660 buf = usb_alloc_coherent(dev->udev, ESD_USB_RX_BUFFER_SIZE, GFP_KERNEL,
663 dev_warn(dev->udev->dev.parent,
664 "No memory left for USB buffer\n");
669 urb->transfer_dma = buf_dma;
671 usb_fill_bulk_urb(urb, dev->udev,
672 usb_rcvbulkpipe(dev->udev, 1),
673 buf, ESD_USB_RX_BUFFER_SIZE,
674 esd_usb_read_bulk_callback, dev);
675 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
676 usb_anchor_urb(urb, &dev->rx_submitted);
678 err = usb_submit_urb(urb, GFP_KERNEL);
680 usb_unanchor_urb(urb);
681 usb_free_coherent(dev->udev, ESD_USB_RX_BUFFER_SIZE, buf,
687 dev->rxbuf_dma[i] = buf_dma;
690 /* Drop reference, USB core will take care of freeing it */
696 /* Did we submit any URBs */
698 dev_err(dev->udev->dev.parent, "couldn't setup read URBs\n");
702 /* Warn if we've couldn't transmit all the URBs */
703 if (i < ESD_USB_MAX_RX_URBS) {
704 dev_warn(dev->udev->dev.parent,
705 "rx performance may be slow\n");
712 /* Start interface */
713 static int esd_usb_start(struct esd_usb_net_priv *priv)
715 struct esd_usb *dev = priv->usb;
716 struct net_device *netdev = priv->netdev;
717 union esd_usb_msg *msg;
720 msg = kmalloc(sizeof(*msg), GFP_KERNEL);
727 * The IDADD message takes up to 64 32 bit bitmasks (2048 bits).
728 * Each bit represents one 11 bit CAN identifier. A set bit
729 * enables reception of the corresponding CAN identifier. A cleared
730 * bit disabled this identifier. An additional bitmask value
731 * following the CAN 2.0A bits is used to enable reception of
732 * extended CAN frames. Only the LSB of this final mask is checked
733 * for the complete 29 bit ID range. The IDADD message also allows
734 * filter configuration for an ID subset. In this case you can add
735 * the number of the starting bitmask (0..64) to the filter.option
736 * field followed by only some bitmasks.
738 msg->hdr.cmd = ESD_USB_CMD_IDADD;
739 msg->hdr.len = sizeof(struct esd_usb_id_filter_msg) / sizeof(u32); /* # of 32bit words */
740 msg->filter.net = priv->index;
741 msg->filter.option = ESD_USB_ID_ENABLE; /* start with segment 0 */
742 for (i = 0; i < ESD_USB_MAX_ID_SEGMENT; i++)
743 msg->filter.mask[i] = cpu_to_le32(GENMASK(31, 0));
744 /* enable 29bit extended IDs */
745 msg->filter.mask[ESD_USB_MAX_ID_SEGMENT] = cpu_to_le32(BIT(0));
747 err = esd_usb_send_msg(dev, msg);
751 err = esd_usb_setup_rx_urbs(dev);
755 priv->can.state = CAN_STATE_ERROR_ACTIVE;
759 netif_device_detach(netdev);
761 netdev_err(netdev, "couldn't start device: %d\n", err);
767 static void unlink_all_urbs(struct esd_usb *dev)
769 struct esd_usb_net_priv *priv;
772 usb_kill_anchored_urbs(&dev->rx_submitted);
774 for (i = 0; i < ESD_USB_MAX_RX_URBS; ++i)
775 usb_free_coherent(dev->udev, ESD_USB_RX_BUFFER_SIZE,
776 dev->rxbuf[i], dev->rxbuf_dma[i]);
778 for (i = 0; i < dev->net_count; i++) {
781 usb_kill_anchored_urbs(&priv->tx_submitted);
782 atomic_set(&priv->active_tx_jobs, 0);
784 for (j = 0; j < ESD_USB_MAX_TX_URBS; j++)
785 priv->tx_contexts[j].echo_index = ESD_USB_MAX_TX_URBS;
790 static int esd_usb_open(struct net_device *netdev)
792 struct esd_usb_net_priv *priv = netdev_priv(netdev);
796 err = open_candev(netdev);
800 /* finally start device */
801 err = esd_usb_start(priv);
803 netdev_warn(netdev, "couldn't start device: %d\n", err);
804 close_candev(netdev);
808 netif_start_queue(netdev);
813 static netdev_tx_t esd_usb_start_xmit(struct sk_buff *skb,
814 struct net_device *netdev)
816 struct esd_usb_net_priv *priv = netdev_priv(netdev);
817 struct esd_usb *dev = priv->usb;
818 struct esd_tx_urb_context *context = NULL;
819 struct net_device_stats *stats = &netdev->stats;
820 struct canfd_frame *cfd = (struct canfd_frame *)skb->data;
821 union esd_usb_msg *msg;
825 int ret = NETDEV_TX_OK;
826 size_t size = sizeof(union esd_usb_msg);
828 if (can_dev_dropped_skb(netdev, skb))
831 /* create a URB, and a buffer for it, and copy the data to the URB */
832 urb = usb_alloc_urb(0, GFP_ATOMIC);
839 buf = usb_alloc_coherent(dev->udev, size, GFP_ATOMIC,
842 netdev_err(netdev, "No memory left for USB buffer\n");
848 msg = (union esd_usb_msg *)buf;
850 /* minimal length as # of 32bit words */
851 msg->hdr.len = offsetof(struct esd_usb_tx_msg, data) / sizeof(u32);
852 msg->hdr.cmd = ESD_USB_CMD_CAN_TX;
853 msg->tx.net = priv->index;
855 if (can_is_canfd_skb(skb)) {
856 msg->tx.dlc = can_fd_len2dlc(cfd->len);
857 msg->tx.dlc |= ESD_USB_FD;
859 if ((cfd->flags & CANFD_BRS) == 0)
860 msg->tx.dlc |= ESD_USB_NO_BRS;
862 msg->tx.dlc = can_get_cc_dlc((struct can_frame *)cfd, priv->can.ctrlmode);
864 if (cfd->can_id & CAN_RTR_FLAG)
865 msg->tx.dlc |= ESD_USB_RTR;
868 msg->tx.id = cpu_to_le32(cfd->can_id & CAN_ERR_MASK);
870 if (cfd->can_id & CAN_EFF_FLAG)
871 msg->tx.id |= cpu_to_le32(ESD_USB_EXTID);
873 memcpy(msg->tx.data_fd, cfd->data, cfd->len);
875 /* round up, then divide by 4 to add the payload length as # of 32bit words */
876 msg->hdr.len += DIV_ROUND_UP(cfd->len, sizeof(u32));
878 for (i = 0; i < ESD_USB_MAX_TX_URBS; i++) {
879 if (priv->tx_contexts[i].echo_index == ESD_USB_MAX_TX_URBS) {
880 context = &priv->tx_contexts[i];
885 /* This may never happen */
887 netdev_warn(netdev, "couldn't find free context\n");
888 ret = NETDEV_TX_BUSY;
892 context->priv = priv;
893 context->echo_index = i;
895 /* hnd must not be 0 - MSB is stripped in txdone handling */
896 msg->tx.hnd = BIT(31) | i; /* returned in TX done message */
898 usb_fill_bulk_urb(urb, dev->udev, usb_sndbulkpipe(dev->udev, 2), buf,
899 msg->hdr.len * sizeof(u32), /* convert to # of bytes */
900 esd_usb_write_bulk_callback, context);
902 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
904 usb_anchor_urb(urb, &priv->tx_submitted);
906 can_put_echo_skb(skb, netdev, context->echo_index, 0);
908 atomic_inc(&priv->active_tx_jobs);
910 /* Slow down tx path */
911 if (atomic_read(&priv->active_tx_jobs) >= ESD_USB_MAX_TX_URBS)
912 netif_stop_queue(netdev);
914 err = usb_submit_urb(urb, GFP_ATOMIC);
916 can_free_echo_skb(netdev, context->echo_index, NULL);
918 atomic_dec(&priv->active_tx_jobs);
919 usb_unanchor_urb(urb);
924 netif_device_detach(netdev);
926 netdev_warn(netdev, "failed tx_urb %d\n", err);
931 netif_trans_update(netdev);
933 /* Release our reference to this URB, the USB core will eventually free
941 usb_free_coherent(dev->udev, size, buf, urb->transfer_dma);
950 static int esd_usb_close(struct net_device *netdev)
952 struct esd_usb_net_priv *priv = netdev_priv(netdev);
953 union esd_usb_msg *msg;
956 msg = kmalloc(sizeof(*msg), GFP_KERNEL);
960 /* Disable all IDs (see esd_usb_start()) */
961 msg->hdr.cmd = ESD_USB_CMD_IDADD;
962 msg->hdr.len = sizeof(struct esd_usb_id_filter_msg) / sizeof(u32);/* # of 32bit words */
963 msg->filter.net = priv->index;
964 msg->filter.option = ESD_USB_ID_ENABLE; /* start with segment 0 */
965 for (i = 0; i <= ESD_USB_MAX_ID_SEGMENT; i++)
966 msg->filter.mask[i] = 0;
967 if (esd_usb_send_msg(priv->usb, msg) < 0)
968 netdev_err(netdev, "sending idadd message failed\n");
970 /* set CAN controller to reset mode */
971 msg->hdr.len = sizeof(struct esd_usb_set_baudrate_msg) / sizeof(u32); /* # of 32bit words */
972 msg->hdr.cmd = ESD_USB_CMD_SETBAUD;
973 msg->setbaud.net = priv->index;
974 msg->setbaud.rsvd = 0;
975 msg->setbaud.baud = cpu_to_le32(ESD_USB_NO_BAUDRATE);
976 if (esd_usb_send_msg(priv->usb, msg) < 0)
977 netdev_err(netdev, "sending setbaud message failed\n");
979 priv->can.state = CAN_STATE_STOPPED;
981 netif_stop_queue(netdev);
983 close_candev(netdev);
990 static const struct net_device_ops esd_usb_netdev_ops = {
991 .ndo_open = esd_usb_open,
992 .ndo_stop = esd_usb_close,
993 .ndo_start_xmit = esd_usb_start_xmit,
994 .ndo_change_mtu = can_change_mtu,
997 static const struct ethtool_ops esd_usb_ethtool_ops = {
998 .get_ts_info = ethtool_op_get_ts_info,
1001 static const struct can_bittiming_const esd_usb_2_bittiming_const = {
1002 .name = "esd_usb_2",
1013 static int esd_usb_2_set_bittiming(struct net_device *netdev)
1015 const struct can_bittiming_const *btc = &esd_usb_2_bittiming_const;
1016 struct esd_usb_net_priv *priv = netdev_priv(netdev);
1017 struct can_bittiming *bt = &priv->can.bittiming;
1018 union esd_usb_msg *msg;
1023 canbtr = ESD_USB_UBR;
1024 if (priv->can.ctrlmode & CAN_CTRLMODE_LISTENONLY)
1025 canbtr |= ESD_USB_LOM;
1027 canbtr |= (bt->brp - 1) & (btc->brp_max - 1);
1029 if (le16_to_cpu(priv->usb->udev->descriptor.idProduct) ==
1030 ESD_USB_CANUSBM_PRODUCT_ID)
1031 sjw_shift = ESD_USB_M_SJW_SHIFT;
1033 sjw_shift = ESD_USB_2_SJW_SHIFT;
1035 canbtr |= ((bt->sjw - 1) & (btc->sjw_max - 1))
1037 canbtr |= ((bt->prop_seg + bt->phase_seg1 - 1)
1038 & (btc->tseg1_max - 1))
1039 << ESD_USB_2_TSEG1_SHIFT;
1040 canbtr |= ((bt->phase_seg2 - 1) & (btc->tseg2_max - 1))
1041 << ESD_USB_2_TSEG2_SHIFT;
1042 if (priv->can.ctrlmode & CAN_CTRLMODE_3_SAMPLES)
1043 canbtr |= ESD_USB_TRIPLE_SAMPLES;
1045 msg = kmalloc(sizeof(*msg), GFP_KERNEL);
1049 msg->hdr.len = sizeof(struct esd_usb_set_baudrate_msg) / sizeof(u32); /* # of 32bit words */
1050 msg->hdr.cmd = ESD_USB_CMD_SETBAUD;
1051 msg->setbaud.net = priv->index;
1052 msg->setbaud.rsvd = 0;
1053 msg->setbaud.baud = cpu_to_le32(canbtr);
1055 netdev_dbg(netdev, "setting BTR=%#x\n", canbtr);
1057 err = esd_usb_send_msg(priv->usb, msg);
1063 /* Nominal bittiming constants, see
1064 * Microchip SAM E70/S70/V70/V71, Data Sheet, Rev. G - 07/2022
1065 * 48.6.8 MCAN Nominal Bit Timing and Prescaler Register
1067 static const struct can_bittiming_const esd_usb_3_nom_bittiming_const = {
1068 .name = "esd_usb_3",
1079 /* Data bittiming constants, see
1080 * Microchip SAM E70/S70/V70/V71, Data Sheet, Rev. G - 07/2022
1081 * 48.6.4 MCAN Data Bit Timing and Prescaler Register
1083 static const struct can_bittiming_const esd_usb_3_data_bittiming_const = {
1084 .name = "esd_usb_3",
1095 static int esd_usb_3_set_bittiming(struct net_device *netdev)
1097 const struct can_bittiming_const *nom_btc = &esd_usb_3_nom_bittiming_const;
1098 const struct can_bittiming_const *data_btc = &esd_usb_3_data_bittiming_const;
1099 struct esd_usb_net_priv *priv = netdev_priv(netdev);
1100 struct can_bittiming *nom_bt = &priv->can.bittiming;
1101 struct can_bittiming *data_bt = &priv->can.data_bittiming;
1102 struct esd_usb_3_set_baudrate_msg_x *baud_x;
1103 union esd_usb_msg *msg;
1107 msg = kmalloc(sizeof(*msg), GFP_KERNEL);
1111 baud_x = &msg->setbaud_x;
1113 /* Canonical is the most reasonable mode for SocketCAN on CAN-USB/3 ... */
1114 baud_x->mode = cpu_to_le16(ESD_USB_3_BAUDRATE_MODE_BTR_CANONICAL);
1116 if (priv->can.ctrlmode & CAN_CTRLMODE_LISTENONLY)
1117 flags |= ESD_USB_3_BAUDRATE_FLAG_LOM;
1119 if (priv->can.ctrlmode & CAN_CTRLMODE_3_SAMPLES)
1120 flags |= ESD_USB_3_BAUDRATE_FLAG_TRS;
1122 baud_x->nom.brp = cpu_to_le16(nom_bt->brp & (nom_btc->brp_max - 1));
1123 baud_x->nom.sjw = cpu_to_le16(nom_bt->sjw & (nom_btc->sjw_max - 1));
1124 baud_x->nom.tseg1 = cpu_to_le16((nom_bt->prop_seg + nom_bt->phase_seg1)
1125 & (nom_btc->tseg1_max - 1));
1126 baud_x->nom.tseg2 = cpu_to_le16(nom_bt->phase_seg2 & (nom_btc->tseg2_max - 1));
1128 if (priv->can.ctrlmode & CAN_CTRLMODE_FD) {
1129 baud_x->data.brp = cpu_to_le16(data_bt->brp & (data_btc->brp_max - 1));
1130 baud_x->data.sjw = cpu_to_le16(data_bt->sjw & (data_btc->sjw_max - 1));
1131 baud_x->data.tseg1 = cpu_to_le16((data_bt->prop_seg + data_bt->phase_seg1)
1132 & (data_btc->tseg1_max - 1));
1133 baud_x->data.tseg2 = cpu_to_le16(data_bt->phase_seg2 & (data_btc->tseg2_max - 1));
1134 flags |= ESD_USB_3_BAUDRATE_FLAG_FD;
1137 /* Currently this driver only supports the automatic TDC mode */
1138 baud_x->tdc.tdc_mode = ESD_USB_3_TDC_MODE_AUTO;
1139 baud_x->tdc.ssp_offset = 0;
1140 baud_x->tdc.ssp_shift = 0;
1141 baud_x->tdc.tdc_filter = 0;
1143 baud_x->flags = cpu_to_le16(flags);
1144 baud_x->net = priv->index;
1147 /* set len as # of 32bit words */
1148 msg->hdr.len = sizeof(struct esd_usb_3_set_baudrate_msg_x) / sizeof(u32);
1149 msg->hdr.cmd = ESD_USB_CMD_SETBAUD;
1152 "ctrlmode=%#x/%#x, esd-net=%u, esd-mode=%#x, esd-flags=%#x\n",
1153 priv->can.ctrlmode, priv->can.ctrlmode_supported,
1154 priv->index, le16_to_cpu(baud_x->mode), flags);
1156 err = esd_usb_send_msg(priv->usb, msg);
1162 static int esd_usb_get_berr_counter(const struct net_device *netdev,
1163 struct can_berr_counter *bec)
1165 struct esd_usb_net_priv *priv = netdev_priv(netdev);
1167 bec->txerr = priv->bec.txerr;
1168 bec->rxerr = priv->bec.rxerr;
1173 static int esd_usb_set_mode(struct net_device *netdev, enum can_mode mode)
1176 case CAN_MODE_START:
1177 netif_wake_queue(netdev);
1187 static int esd_usb_probe_one_net(struct usb_interface *intf, int index)
1189 struct esd_usb *dev = usb_get_intfdata(intf);
1190 struct net_device *netdev;
1191 struct esd_usb_net_priv *priv;
1195 netdev = alloc_candev(sizeof(*priv), ESD_USB_MAX_TX_URBS);
1197 dev_err(&intf->dev, "couldn't alloc candev\n");
1202 priv = netdev_priv(netdev);
1204 init_usb_anchor(&priv->tx_submitted);
1205 atomic_set(&priv->active_tx_jobs, 0);
1207 for (i = 0; i < ESD_USB_MAX_TX_URBS; i++)
1208 priv->tx_contexts[i].echo_index = ESD_USB_MAX_TX_URBS;
1211 priv->netdev = netdev;
1212 priv->index = index;
1214 priv->can.state = CAN_STATE_STOPPED;
1215 priv->can.ctrlmode_supported = CAN_CTRLMODE_LISTENONLY |
1216 CAN_CTRLMODE_CC_LEN8_DLC |
1217 CAN_CTRLMODE_BERR_REPORTING;
1219 switch (le16_to_cpu(dev->udev->descriptor.idProduct)) {
1220 case ESD_USB_CANUSB3_PRODUCT_ID:
1221 priv->can.clock.freq = ESD_USB_3_CAN_CLOCK;
1222 priv->can.ctrlmode_supported |= CAN_CTRLMODE_3_SAMPLES;
1223 priv->can.ctrlmode_supported |= CAN_CTRLMODE_FD;
1224 priv->can.bittiming_const = &esd_usb_3_nom_bittiming_const;
1225 priv->can.data_bittiming_const = &esd_usb_3_data_bittiming_const;
1226 priv->can.do_set_bittiming = esd_usb_3_set_bittiming;
1227 priv->can.do_set_data_bittiming = esd_usb_3_set_bittiming;
1230 case ESD_USB_CANUSBM_PRODUCT_ID:
1231 priv->can.clock.freq = ESD_USB_M_CAN_CLOCK;
1232 priv->can.bittiming_const = &esd_usb_2_bittiming_const;
1233 priv->can.do_set_bittiming = esd_usb_2_set_bittiming;
1236 case ESD_USB_CANUSB2_PRODUCT_ID:
1238 priv->can.clock.freq = ESD_USB_2_CAN_CLOCK;
1239 priv->can.ctrlmode_supported |= CAN_CTRLMODE_3_SAMPLES;
1240 priv->can.bittiming_const = &esd_usb_2_bittiming_const;
1241 priv->can.do_set_bittiming = esd_usb_2_set_bittiming;
1245 priv->can.do_set_mode = esd_usb_set_mode;
1246 priv->can.do_get_berr_counter = esd_usb_get_berr_counter;
1248 netdev->flags |= IFF_ECHO; /* we support local echo */
1250 netdev->netdev_ops = &esd_usb_netdev_ops;
1251 netdev->ethtool_ops = &esd_usb_ethtool_ops;
1253 SET_NETDEV_DEV(netdev, &intf->dev);
1254 netdev->dev_id = index;
1256 err = register_candev(netdev);
1258 dev_err(&intf->dev, "couldn't register CAN device: %d\n", err);
1259 free_candev(netdev);
1264 dev->nets[index] = priv;
1265 netdev_info(netdev, "device %s registered\n", netdev->name);
1271 /* probe function for new USB devices
1273 * check version information and number of available
1276 static int esd_usb_probe(struct usb_interface *intf,
1277 const struct usb_device_id *id)
1279 struct esd_usb *dev;
1280 union esd_usb_msg *msg;
1283 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1289 dev->udev = interface_to_usbdev(intf);
1291 init_usb_anchor(&dev->rx_submitted);
1293 usb_set_intfdata(intf, dev);
1295 msg = kmalloc(sizeof(*msg), GFP_KERNEL);
1301 /* query number of CAN interfaces (nets) */
1302 msg->hdr.cmd = ESD_USB_CMD_VERSION;
1303 msg->hdr.len = sizeof(struct esd_usb_version_msg) / sizeof(u32); /* # of 32bit words */
1304 msg->version.rsvd = 0;
1305 msg->version.flags = 0;
1306 msg->version.drv_version = 0;
1308 err = esd_usb_send_msg(dev, msg);
1310 dev_err(&intf->dev, "sending version message failed\n");
1314 err = esd_usb_wait_msg(dev, msg);
1316 dev_err(&intf->dev, "no version message answer\n");
1320 dev->net_count = (int)msg->version_reply.nets;
1321 dev->version = le32_to_cpu(msg->version_reply.version);
1323 if (device_create_file(&intf->dev, &dev_attr_firmware))
1325 "Couldn't create device file for firmware\n");
1327 if (device_create_file(&intf->dev, &dev_attr_hardware))
1329 "Couldn't create device file for hardware\n");
1331 if (device_create_file(&intf->dev, &dev_attr_nets))
1333 "Couldn't create device file for nets\n");
1335 /* do per device probing */
1336 for (i = 0; i < dev->net_count; i++)
1337 esd_usb_probe_one_net(intf, i);
1347 /* called by the usb core when the device is removed from the system */
1348 static void esd_usb_disconnect(struct usb_interface *intf)
1350 struct esd_usb *dev = usb_get_intfdata(intf);
1351 struct net_device *netdev;
1354 device_remove_file(&intf->dev, &dev_attr_firmware);
1355 device_remove_file(&intf->dev, &dev_attr_hardware);
1356 device_remove_file(&intf->dev, &dev_attr_nets);
1358 usb_set_intfdata(intf, NULL);
1361 for (i = 0; i < dev->net_count; i++) {
1363 netdev = dev->nets[i]->netdev;
1364 unregister_netdev(netdev);
1365 free_candev(netdev);
1368 unlink_all_urbs(dev);
1373 /* usb specific object needed to register this driver with the usb subsystem */
1374 static struct usb_driver esd_usb_driver = {
1375 .name = KBUILD_MODNAME,
1376 .probe = esd_usb_probe,
1377 .disconnect = esd_usb_disconnect,
1378 .id_table = esd_usb_table,
1381 module_usb_driver(esd_usb_driver);