1 // SPDX-License-Identifier: GPL-2.0-only
3 * CAN driver for esd electronics gmbh CAN-USB/2 and CAN-USB/Micro
5 * Copyright (C) 2010-2012 esd electronic system design gmbh, Matthias Fuchs <
[email protected]>
8 #include <linux/ethtool.h>
9 #include <linux/signal.h>
10 #include <linux/slab.h>
11 #include <linux/module.h>
12 #include <linux/netdevice.h>
13 #include <linux/usb.h>
15 #include <linux/can.h>
16 #include <linux/can/dev.h>
17 #include <linux/can/error.h>
21 MODULE_DESCRIPTION("CAN driver for esd electronics gmbh CAN-USB/2 and CAN-USB/Micro interfaces");
22 MODULE_LICENSE("GPL v2");
24 /* USB vendor and product ID */
25 #define USB_ESDGMBH_VENDOR_ID 0x0ab4
26 #define USB_CANUSB2_PRODUCT_ID 0x0010
27 #define USB_CANUSBM_PRODUCT_ID 0x0011
29 /* CAN controller clock frequencies */
30 #define ESD_USB2_CAN_CLOCK 60000000
31 #define ESD_USBM_CAN_CLOCK 36000000
33 /* Maximum number of CAN nets */
34 #define ESD_USB_MAX_NETS 2
37 #define CMD_VERSION 1 /* also used for VERSION_REPLY */
38 #define CMD_CAN_RX 2 /* device to host only */
39 #define CMD_CAN_TX 3 /* also used for TX_DONE */
40 #define CMD_SETBAUD 4 /* also used for SETBAUD_REPLY */
41 #define CMD_TS 5 /* also used for TS_REPLY */
42 #define CMD_IDADD 6 /* also used for IDADD_REPLY */
44 /* esd CAN message flags - dlc field */
47 /* esd CAN message flags - id field */
48 #define ESD_EXTID 0x20000000
49 #define ESD_EVENT 0x40000000
50 #define ESD_IDMASK 0x1fffffff
52 /* esd CAN event ids */
53 #define ESD_EV_CAN_ERROR_EXT 2 /* CAN controller specific diagnostic data */
55 /* baudrate message flags */
56 #define ESD_USB_UBR 0x80000000
57 #define ESD_USB_LOM 0x40000000
58 #define ESD_USB_NO_BAUDRATE 0x7fffffff
60 /* bit timing CAN-USB/2 */
61 #define ESD_USB2_TSEG1_MIN 1
62 #define ESD_USB2_TSEG1_MAX 16
63 #define ESD_USB2_TSEG1_SHIFT 16
64 #define ESD_USB2_TSEG2_MIN 1
65 #define ESD_USB2_TSEG2_MAX 8
66 #define ESD_USB2_TSEG2_SHIFT 20
67 #define ESD_USB2_SJW_MAX 4
68 #define ESD_USB2_SJW_SHIFT 14
69 #define ESD_USBM_SJW_SHIFT 24
70 #define ESD_USB2_BRP_MIN 1
71 #define ESD_USB2_BRP_MAX 1024
72 #define ESD_USB2_BRP_INC 1
73 #define ESD_USB2_3_SAMPLES 0x00800000
75 /* esd IDADD message */
76 #define ESD_ID_ENABLE 0x80
77 #define ESD_MAX_ID_SEGMENT 64
79 /* SJA1000 ECC register (emulated by usb firmware) */
80 #define SJA1000_ECC_SEG 0x1F
81 #define SJA1000_ECC_DIR 0x20
82 #define SJA1000_ECC_ERR 0x06
83 #define SJA1000_ECC_BIT 0x00
84 #define SJA1000_ECC_FORM 0x40
85 #define SJA1000_ECC_STUFF 0x80
86 #define SJA1000_ECC_MASK 0xc0
88 /* esd bus state event codes */
89 #define ESD_BUSSTATE_MASK 0xc0
90 #define ESD_BUSSTATE_WARN 0x40
91 #define ESD_BUSSTATE_ERRPASSIVE 0x80
92 #define ESD_BUSSTATE_BUSOFF 0xc0
94 #define RX_BUFFER_SIZE 1024
96 #define MAX_TX_URBS 16 /* must be power of 2 */
99 u8 len; /* len is always the total message length in 32bit words */
112 struct version_reply_msg {
129 __le32 id; /* upper 3 bits contain flags */
138 u32 hnd; /* opaque handle, not used by device */
139 __le32 id; /* upper 3 bits contain flags */
148 u32 hnd; /* opaque handle, not used by device */
152 struct id_filter_msg {
157 __le32 mask[ESD_MAX_ID_SEGMENT + 1];
160 struct set_baudrate_msg {
168 /* Main message type used between library and application */
169 struct __packed esd_usb_msg {
171 struct header_msg hdr;
172 struct version_msg version;
173 struct version_reply_msg version_reply;
176 struct tx_done_msg txdone;
177 struct set_baudrate_msg setbaud;
178 struct id_filter_msg filter;
182 static struct usb_device_id esd_usb_table[] = {
183 {USB_DEVICE(USB_ESDGMBH_VENDOR_ID, USB_CANUSB2_PRODUCT_ID)},
184 {USB_DEVICE(USB_ESDGMBH_VENDOR_ID, USB_CANUSBM_PRODUCT_ID)},
187 MODULE_DEVICE_TABLE(usb, esd_usb_table);
189 struct esd_usb_net_priv;
191 struct esd_tx_urb_context {
192 struct esd_usb_net_priv *priv;
197 struct usb_device *udev;
198 struct esd_usb_net_priv *nets[ESD_USB_MAX_NETS];
200 struct usb_anchor rx_submitted;
205 void *rxbuf[MAX_RX_URBS];
206 dma_addr_t rxbuf_dma[MAX_RX_URBS];
209 struct esd_usb_net_priv {
210 struct can_priv can; /* must be the first member */
212 atomic_t active_tx_jobs;
213 struct usb_anchor tx_submitted;
214 struct esd_tx_urb_context tx_contexts[MAX_TX_URBS];
217 struct net_device *netdev;
220 struct can_berr_counter bec;
223 static void esd_usb_rx_event(struct esd_usb_net_priv *priv,
224 struct esd_usb_msg *msg)
226 struct net_device_stats *stats = &priv->netdev->stats;
227 struct can_frame *cf;
229 u32 id = le32_to_cpu(msg->msg.rx.id) & ESD_IDMASK;
231 if (id == ESD_EV_CAN_ERROR_EXT) {
232 u8 state = msg->msg.rx.data[0];
233 u8 ecc = msg->msg.rx.data[1];
234 u8 rxerr = msg->msg.rx.data[2];
235 u8 txerr = msg->msg.rx.data[3];
237 skb = alloc_can_err_skb(priv->netdev, &cf);
243 if (state != priv->old_state) {
244 priv->old_state = state;
246 switch (state & ESD_BUSSTATE_MASK) {
247 case ESD_BUSSTATE_BUSOFF:
248 priv->can.state = CAN_STATE_BUS_OFF;
249 cf->can_id |= CAN_ERR_BUSOFF;
250 priv->can.can_stats.bus_off++;
251 can_bus_off(priv->netdev);
253 case ESD_BUSSTATE_WARN:
254 priv->can.state = CAN_STATE_ERROR_WARNING;
255 priv->can.can_stats.error_warning++;
257 case ESD_BUSSTATE_ERRPASSIVE:
258 priv->can.state = CAN_STATE_ERROR_PASSIVE;
259 priv->can.can_stats.error_passive++;
262 priv->can.state = CAN_STATE_ERROR_ACTIVE;
266 priv->can.can_stats.bus_error++;
269 cf->can_id |= CAN_ERR_PROT | CAN_ERR_BUSERROR |
272 switch (ecc & SJA1000_ECC_MASK) {
273 case SJA1000_ECC_BIT:
274 cf->data[2] |= CAN_ERR_PROT_BIT;
276 case SJA1000_ECC_FORM:
277 cf->data[2] |= CAN_ERR_PROT_FORM;
279 case SJA1000_ECC_STUFF:
280 cf->data[2] |= CAN_ERR_PROT_STUFF;
283 cf->data[3] = ecc & SJA1000_ECC_SEG;
287 /* Error occurred during transmission? */
288 if (!(ecc & SJA1000_ECC_DIR))
289 cf->data[2] |= CAN_ERR_PROT_TX;
291 if (priv->can.state == CAN_STATE_ERROR_WARNING ||
292 priv->can.state == CAN_STATE_ERROR_PASSIVE) {
293 cf->data[1] = (txerr > rxerr) ?
294 CAN_ERR_CRTL_TX_PASSIVE :
295 CAN_ERR_CRTL_RX_PASSIVE;
301 priv->bec.txerr = txerr;
302 priv->bec.rxerr = rxerr;
308 static void esd_usb_rx_can_msg(struct esd_usb_net_priv *priv,
309 struct esd_usb_msg *msg)
311 struct net_device_stats *stats = &priv->netdev->stats;
312 struct can_frame *cf;
317 if (!netif_device_present(priv->netdev))
320 id = le32_to_cpu(msg->msg.rx.id);
322 if (id & ESD_EVENT) {
323 esd_usb_rx_event(priv, msg);
325 skb = alloc_can_skb(priv->netdev, &cf);
331 cf->can_id = id & ESD_IDMASK;
332 can_frame_set_cc_len(cf, msg->msg.rx.dlc & ~ESD_RTR,
336 cf->can_id |= CAN_EFF_FLAG;
338 if (msg->msg.rx.dlc & ESD_RTR) {
339 cf->can_id |= CAN_RTR_FLAG;
341 for (i = 0; i < cf->len; i++)
342 cf->data[i] = msg->msg.rx.data[i];
344 stats->rx_bytes += cf->len;
352 static void esd_usb_tx_done_msg(struct esd_usb_net_priv *priv,
353 struct esd_usb_msg *msg)
355 struct net_device_stats *stats = &priv->netdev->stats;
356 struct net_device *netdev = priv->netdev;
357 struct esd_tx_urb_context *context;
359 if (!netif_device_present(netdev))
362 context = &priv->tx_contexts[msg->msg.txdone.hnd & (MAX_TX_URBS - 1)];
364 if (!msg->msg.txdone.status) {
366 stats->tx_bytes += can_get_echo_skb(netdev, context->echo_index,
370 can_free_echo_skb(netdev, context->echo_index, NULL);
373 /* Release context */
374 context->echo_index = MAX_TX_URBS;
375 atomic_dec(&priv->active_tx_jobs);
377 netif_wake_queue(netdev);
380 static void esd_usb_read_bulk_callback(struct urb *urb)
382 struct esd_usb *dev = urb->context;
387 switch (urb->status) {
388 case 0: /* success */
398 dev_info(dev->udev->dev.parent,
399 "Rx URB aborted (%d)\n", urb->status);
403 while (pos < urb->actual_length) {
404 struct esd_usb_msg *msg;
406 msg = (struct esd_usb_msg *)(urb->transfer_buffer + pos);
408 switch (msg->msg.hdr.cmd) {
410 if (msg->msg.rx.net >= dev->net_count) {
411 dev_err(dev->udev->dev.parent, "format error\n");
415 esd_usb_rx_can_msg(dev->nets[msg->msg.rx.net], msg);
419 if (msg->msg.txdone.net >= dev->net_count) {
420 dev_err(dev->udev->dev.parent, "format error\n");
424 esd_usb_tx_done_msg(dev->nets[msg->msg.txdone.net],
429 pos += msg->msg.hdr.len << 2;
431 if (pos > urb->actual_length) {
432 dev_err(dev->udev->dev.parent, "format error\n");
438 usb_fill_bulk_urb(urb, dev->udev, usb_rcvbulkpipe(dev->udev, 1),
439 urb->transfer_buffer, RX_BUFFER_SIZE,
440 esd_usb_read_bulk_callback, dev);
442 retval = usb_submit_urb(urb, GFP_ATOMIC);
443 if (retval == -ENODEV) {
444 for (i = 0; i < dev->net_count; i++) {
446 netif_device_detach(dev->nets[i]->netdev);
449 dev_err(dev->udev->dev.parent,
450 "failed resubmitting read bulk urb: %d\n", retval);
454 /* callback for bulk IN urb */
455 static void esd_usb_write_bulk_callback(struct urb *urb)
457 struct esd_tx_urb_context *context = urb->context;
458 struct esd_usb_net_priv *priv;
459 struct net_device *netdev;
460 size_t size = sizeof(struct esd_usb_msg);
464 priv = context->priv;
465 netdev = priv->netdev;
467 /* free up our allocated buffer */
468 usb_free_coherent(urb->dev, size,
469 urb->transfer_buffer, urb->transfer_dma);
471 if (!netif_device_present(netdev))
475 netdev_info(netdev, "Tx URB aborted (%d)\n", urb->status);
477 netif_trans_update(netdev);
480 static ssize_t firmware_show(struct device *d,
481 struct device_attribute *attr, char *buf)
483 struct usb_interface *intf = to_usb_interface(d);
484 struct esd_usb *dev = usb_get_intfdata(intf);
486 return sprintf(buf, "%d.%d.%d\n",
487 (dev->version >> 12) & 0xf,
488 (dev->version >> 8) & 0xf,
489 dev->version & 0xff);
491 static DEVICE_ATTR_RO(firmware);
493 static ssize_t hardware_show(struct device *d,
494 struct device_attribute *attr, char *buf)
496 struct usb_interface *intf = to_usb_interface(d);
497 struct esd_usb *dev = usb_get_intfdata(intf);
499 return sprintf(buf, "%d.%d.%d\n",
500 (dev->version >> 28) & 0xf,
501 (dev->version >> 24) & 0xf,
502 (dev->version >> 16) & 0xff);
504 static DEVICE_ATTR_RO(hardware);
506 static ssize_t nets_show(struct device *d,
507 struct device_attribute *attr, char *buf)
509 struct usb_interface *intf = to_usb_interface(d);
510 struct esd_usb *dev = usb_get_intfdata(intf);
512 return sprintf(buf, "%d", dev->net_count);
514 static DEVICE_ATTR_RO(nets);
516 static int esd_usb_send_msg(struct esd_usb *dev, struct esd_usb_msg *msg)
520 return usb_bulk_msg(dev->udev,
521 usb_sndbulkpipe(dev->udev, 2),
523 msg->msg.hdr.len << 2,
528 static int esd_usb_wait_msg(struct esd_usb *dev,
529 struct esd_usb_msg *msg)
533 return usb_bulk_msg(dev->udev,
534 usb_rcvbulkpipe(dev->udev, 1),
541 static int esd_usb_setup_rx_urbs(struct esd_usb *dev)
548 for (i = 0; i < MAX_RX_URBS; i++) {
549 struct urb *urb = NULL;
553 /* create a URB, and a buffer for it */
554 urb = usb_alloc_urb(0, GFP_KERNEL);
560 buf = usb_alloc_coherent(dev->udev, RX_BUFFER_SIZE, GFP_KERNEL,
563 dev_warn(dev->udev->dev.parent,
564 "No memory left for USB buffer\n");
569 urb->transfer_dma = buf_dma;
571 usb_fill_bulk_urb(urb, dev->udev,
572 usb_rcvbulkpipe(dev->udev, 1),
574 esd_usb_read_bulk_callback, dev);
575 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
576 usb_anchor_urb(urb, &dev->rx_submitted);
578 err = usb_submit_urb(urb, GFP_KERNEL);
580 usb_unanchor_urb(urb);
581 usb_free_coherent(dev->udev, RX_BUFFER_SIZE, buf,
587 dev->rxbuf_dma[i] = buf_dma;
590 /* Drop reference, USB core will take care of freeing it */
596 /* Did we submit any URBs */
598 dev_err(dev->udev->dev.parent, "couldn't setup read URBs\n");
602 /* Warn if we've couldn't transmit all the URBs */
603 if (i < MAX_RX_URBS) {
604 dev_warn(dev->udev->dev.parent,
605 "rx performance may be slow\n");
612 /* Start interface */
613 static int esd_usb_start(struct esd_usb_net_priv *priv)
615 struct esd_usb *dev = priv->usb;
616 struct net_device *netdev = priv->netdev;
617 struct esd_usb_msg *msg;
620 msg = kmalloc(sizeof(*msg), GFP_KERNEL);
627 * The IDADD message takes up to 64 32 bit bitmasks (2048 bits).
628 * Each bit represents one 11 bit CAN identifier. A set bit
629 * enables reception of the corresponding CAN identifier. A cleared
630 * bit disabled this identifier. An additional bitmask value
631 * following the CAN 2.0A bits is used to enable reception of
632 * extended CAN frames. Only the LSB of this final mask is checked
633 * for the complete 29 bit ID range. The IDADD message also allows
634 * filter configuration for an ID subset. In this case you can add
635 * the number of the starting bitmask (0..64) to the filter.option
636 * field followed by only some bitmasks.
638 msg->msg.hdr.cmd = CMD_IDADD;
639 msg->msg.hdr.len = 2 + ESD_MAX_ID_SEGMENT;
640 msg->msg.filter.net = priv->index;
641 msg->msg.filter.option = ESD_ID_ENABLE; /* start with segment 0 */
642 for (i = 0; i < ESD_MAX_ID_SEGMENT; i++)
643 msg->msg.filter.mask[i] = cpu_to_le32(0xffffffff);
644 /* enable 29bit extended IDs */
645 msg->msg.filter.mask[ESD_MAX_ID_SEGMENT] = cpu_to_le32(0x00000001);
647 err = esd_usb_send_msg(dev, msg);
651 err = esd_usb_setup_rx_urbs(dev);
655 priv->can.state = CAN_STATE_ERROR_ACTIVE;
659 netif_device_detach(netdev);
661 netdev_err(netdev, "couldn't start device: %d\n", err);
667 static void unlink_all_urbs(struct esd_usb *dev)
669 struct esd_usb_net_priv *priv;
672 usb_kill_anchored_urbs(&dev->rx_submitted);
674 for (i = 0; i < MAX_RX_URBS; ++i)
675 usb_free_coherent(dev->udev, RX_BUFFER_SIZE,
676 dev->rxbuf[i], dev->rxbuf_dma[i]);
678 for (i = 0; i < dev->net_count; i++) {
681 usb_kill_anchored_urbs(&priv->tx_submitted);
682 atomic_set(&priv->active_tx_jobs, 0);
684 for (j = 0; j < MAX_TX_URBS; j++)
685 priv->tx_contexts[j].echo_index = MAX_TX_URBS;
690 static int esd_usb_open(struct net_device *netdev)
692 struct esd_usb_net_priv *priv = netdev_priv(netdev);
696 err = open_candev(netdev);
700 /* finally start device */
701 err = esd_usb_start(priv);
703 netdev_warn(netdev, "couldn't start device: %d\n", err);
704 close_candev(netdev);
708 netif_start_queue(netdev);
713 static netdev_tx_t esd_usb_start_xmit(struct sk_buff *skb,
714 struct net_device *netdev)
716 struct esd_usb_net_priv *priv = netdev_priv(netdev);
717 struct esd_usb *dev = priv->usb;
718 struct esd_tx_urb_context *context = NULL;
719 struct net_device_stats *stats = &netdev->stats;
720 struct can_frame *cf = (struct can_frame *)skb->data;
721 struct esd_usb_msg *msg;
725 int ret = NETDEV_TX_OK;
726 size_t size = sizeof(struct esd_usb_msg);
728 if (can_dropped_invalid_skb(netdev, skb))
731 /* create a URB, and a buffer for it, and copy the data to the URB */
732 urb = usb_alloc_urb(0, GFP_ATOMIC);
739 buf = usb_alloc_coherent(dev->udev, size, GFP_ATOMIC,
742 netdev_err(netdev, "No memory left for USB buffer\n");
748 msg = (struct esd_usb_msg *)buf;
750 msg->msg.hdr.len = 3; /* minimal length */
751 msg->msg.hdr.cmd = CMD_CAN_TX;
752 msg->msg.tx.net = priv->index;
753 msg->msg.tx.dlc = can_get_cc_dlc(cf, priv->can.ctrlmode);
754 msg->msg.tx.id = cpu_to_le32(cf->can_id & CAN_ERR_MASK);
756 if (cf->can_id & CAN_RTR_FLAG)
757 msg->msg.tx.dlc |= ESD_RTR;
759 if (cf->can_id & CAN_EFF_FLAG)
760 msg->msg.tx.id |= cpu_to_le32(ESD_EXTID);
762 for (i = 0; i < cf->len; i++)
763 msg->msg.tx.data[i] = cf->data[i];
765 msg->msg.hdr.len += (cf->len + 3) >> 2;
767 for (i = 0; i < MAX_TX_URBS; i++) {
768 if (priv->tx_contexts[i].echo_index == MAX_TX_URBS) {
769 context = &priv->tx_contexts[i];
774 /* This may never happen */
776 netdev_warn(netdev, "couldn't find free context\n");
777 ret = NETDEV_TX_BUSY;
781 context->priv = priv;
782 context->echo_index = i;
784 /* hnd must not be 0 - MSB is stripped in txdone handling */
785 msg->msg.tx.hnd = 0x80000000 | i; /* returned in TX done message */
787 usb_fill_bulk_urb(urb, dev->udev, usb_sndbulkpipe(dev->udev, 2), buf,
788 msg->msg.hdr.len << 2,
789 esd_usb_write_bulk_callback, context);
791 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
793 usb_anchor_urb(urb, &priv->tx_submitted);
795 can_put_echo_skb(skb, netdev, context->echo_index, 0);
797 atomic_inc(&priv->active_tx_jobs);
799 /* Slow down tx path */
800 if (atomic_read(&priv->active_tx_jobs) >= MAX_TX_URBS)
801 netif_stop_queue(netdev);
803 err = usb_submit_urb(urb, GFP_ATOMIC);
805 can_free_echo_skb(netdev, context->echo_index, NULL);
807 atomic_dec(&priv->active_tx_jobs);
808 usb_unanchor_urb(urb);
813 netif_device_detach(netdev);
815 netdev_warn(netdev, "failed tx_urb %d\n", err);
820 netif_trans_update(netdev);
822 /* Release our reference to this URB, the USB core will eventually free
830 usb_free_coherent(dev->udev, size, buf, urb->transfer_dma);
839 static int esd_usb_close(struct net_device *netdev)
841 struct esd_usb_net_priv *priv = netdev_priv(netdev);
842 struct esd_usb_msg *msg;
845 msg = kmalloc(sizeof(*msg), GFP_KERNEL);
849 /* Disable all IDs (see esd_usb_start()) */
850 msg->msg.hdr.cmd = CMD_IDADD;
851 msg->msg.hdr.len = 2 + ESD_MAX_ID_SEGMENT;
852 msg->msg.filter.net = priv->index;
853 msg->msg.filter.option = ESD_ID_ENABLE; /* start with segment 0 */
854 for (i = 0; i <= ESD_MAX_ID_SEGMENT; i++)
855 msg->msg.filter.mask[i] = 0;
856 if (esd_usb_send_msg(priv->usb, msg) < 0)
857 netdev_err(netdev, "sending idadd message failed\n");
859 /* set CAN controller to reset mode */
860 msg->msg.hdr.len = 2;
861 msg->msg.hdr.cmd = CMD_SETBAUD;
862 msg->msg.setbaud.net = priv->index;
863 msg->msg.setbaud.rsvd = 0;
864 msg->msg.setbaud.baud = cpu_to_le32(ESD_USB_NO_BAUDRATE);
865 if (esd_usb_send_msg(priv->usb, msg) < 0)
866 netdev_err(netdev, "sending setbaud message failed\n");
868 priv->can.state = CAN_STATE_STOPPED;
870 netif_stop_queue(netdev);
872 close_candev(netdev);
879 static const struct net_device_ops esd_usb_netdev_ops = {
880 .ndo_open = esd_usb_open,
881 .ndo_stop = esd_usb_close,
882 .ndo_start_xmit = esd_usb_start_xmit,
883 .ndo_change_mtu = can_change_mtu,
886 static const struct ethtool_ops esd_usb_ethtool_ops = {
887 .get_ts_info = ethtool_op_get_ts_info,
890 static const struct can_bittiming_const esd_usb2_bittiming_const = {
892 .tseg1_min = ESD_USB2_TSEG1_MIN,
893 .tseg1_max = ESD_USB2_TSEG1_MAX,
894 .tseg2_min = ESD_USB2_TSEG2_MIN,
895 .tseg2_max = ESD_USB2_TSEG2_MAX,
896 .sjw_max = ESD_USB2_SJW_MAX,
897 .brp_min = ESD_USB2_BRP_MIN,
898 .brp_max = ESD_USB2_BRP_MAX,
899 .brp_inc = ESD_USB2_BRP_INC,
902 static int esd_usb2_set_bittiming(struct net_device *netdev)
904 struct esd_usb_net_priv *priv = netdev_priv(netdev);
905 struct can_bittiming *bt = &priv->can.bittiming;
906 struct esd_usb_msg *msg;
911 canbtr = ESD_USB_UBR;
912 if (priv->can.ctrlmode & CAN_CTRLMODE_LISTENONLY)
913 canbtr |= ESD_USB_LOM;
915 canbtr |= (bt->brp - 1) & (ESD_USB2_BRP_MAX - 1);
917 if (le16_to_cpu(priv->usb->udev->descriptor.idProduct) ==
918 USB_CANUSBM_PRODUCT_ID)
919 sjw_shift = ESD_USBM_SJW_SHIFT;
921 sjw_shift = ESD_USB2_SJW_SHIFT;
923 canbtr |= ((bt->sjw - 1) & (ESD_USB2_SJW_MAX - 1))
925 canbtr |= ((bt->prop_seg + bt->phase_seg1 - 1)
926 & (ESD_USB2_TSEG1_MAX - 1))
927 << ESD_USB2_TSEG1_SHIFT;
928 canbtr |= ((bt->phase_seg2 - 1) & (ESD_USB2_TSEG2_MAX - 1))
929 << ESD_USB2_TSEG2_SHIFT;
930 if (priv->can.ctrlmode & CAN_CTRLMODE_3_SAMPLES)
931 canbtr |= ESD_USB2_3_SAMPLES;
933 msg = kmalloc(sizeof(*msg), GFP_KERNEL);
937 msg->msg.hdr.len = 2;
938 msg->msg.hdr.cmd = CMD_SETBAUD;
939 msg->msg.setbaud.net = priv->index;
940 msg->msg.setbaud.rsvd = 0;
941 msg->msg.setbaud.baud = cpu_to_le32(canbtr);
943 netdev_info(netdev, "setting BTR=%#x\n", canbtr);
945 err = esd_usb_send_msg(priv->usb, msg);
951 static int esd_usb_get_berr_counter(const struct net_device *netdev,
952 struct can_berr_counter *bec)
954 struct esd_usb_net_priv *priv = netdev_priv(netdev);
956 bec->txerr = priv->bec.txerr;
957 bec->rxerr = priv->bec.rxerr;
962 static int esd_usb_set_mode(struct net_device *netdev, enum can_mode mode)
966 netif_wake_queue(netdev);
976 static int esd_usb_probe_one_net(struct usb_interface *intf, int index)
978 struct esd_usb *dev = usb_get_intfdata(intf);
979 struct net_device *netdev;
980 struct esd_usb_net_priv *priv;
984 netdev = alloc_candev(sizeof(*priv), MAX_TX_URBS);
986 dev_err(&intf->dev, "couldn't alloc candev\n");
991 priv = netdev_priv(netdev);
993 init_usb_anchor(&priv->tx_submitted);
994 atomic_set(&priv->active_tx_jobs, 0);
996 for (i = 0; i < MAX_TX_URBS; i++)
997 priv->tx_contexts[i].echo_index = MAX_TX_URBS;
1000 priv->netdev = netdev;
1001 priv->index = index;
1003 priv->can.state = CAN_STATE_STOPPED;
1004 priv->can.ctrlmode_supported = CAN_CTRLMODE_LISTENONLY |
1005 CAN_CTRLMODE_CC_LEN8_DLC;
1007 if (le16_to_cpu(dev->udev->descriptor.idProduct) ==
1008 USB_CANUSBM_PRODUCT_ID)
1009 priv->can.clock.freq = ESD_USBM_CAN_CLOCK;
1011 priv->can.clock.freq = ESD_USB2_CAN_CLOCK;
1012 priv->can.ctrlmode_supported |= CAN_CTRLMODE_3_SAMPLES;
1015 priv->can.bittiming_const = &esd_usb2_bittiming_const;
1016 priv->can.do_set_bittiming = esd_usb2_set_bittiming;
1017 priv->can.do_set_mode = esd_usb_set_mode;
1018 priv->can.do_get_berr_counter = esd_usb_get_berr_counter;
1020 netdev->flags |= IFF_ECHO; /* we support local echo */
1022 netdev->netdev_ops = &esd_usb_netdev_ops;
1023 netdev->ethtool_ops = &esd_usb_ethtool_ops;
1025 SET_NETDEV_DEV(netdev, &intf->dev);
1026 netdev->dev_id = index;
1028 err = register_candev(netdev);
1030 dev_err(&intf->dev, "couldn't register CAN device: %d\n", err);
1031 free_candev(netdev);
1036 dev->nets[index] = priv;
1037 netdev_info(netdev, "device %s registered\n", netdev->name);
1043 /* probe function for new USB devices
1045 * check version information and number of available
1048 static int esd_usb_probe(struct usb_interface *intf,
1049 const struct usb_device_id *id)
1051 struct esd_usb *dev;
1052 struct esd_usb_msg *msg;
1055 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1061 dev->udev = interface_to_usbdev(intf);
1063 init_usb_anchor(&dev->rx_submitted);
1065 usb_set_intfdata(intf, dev);
1067 msg = kmalloc(sizeof(*msg), GFP_KERNEL);
1073 /* query number of CAN interfaces (nets) */
1074 msg->msg.hdr.cmd = CMD_VERSION;
1075 msg->msg.hdr.len = 2;
1076 msg->msg.version.rsvd = 0;
1077 msg->msg.version.flags = 0;
1078 msg->msg.version.drv_version = 0;
1080 err = esd_usb_send_msg(dev, msg);
1082 dev_err(&intf->dev, "sending version message failed\n");
1086 err = esd_usb_wait_msg(dev, msg);
1088 dev_err(&intf->dev, "no version message answer\n");
1092 dev->net_count = (int)msg->msg.version_reply.nets;
1093 dev->version = le32_to_cpu(msg->msg.version_reply.version);
1095 if (device_create_file(&intf->dev, &dev_attr_firmware))
1097 "Couldn't create device file for firmware\n");
1099 if (device_create_file(&intf->dev, &dev_attr_hardware))
1101 "Couldn't create device file for hardware\n");
1103 if (device_create_file(&intf->dev, &dev_attr_nets))
1105 "Couldn't create device file for nets\n");
1107 /* do per device probing */
1108 for (i = 0; i < dev->net_count; i++)
1109 esd_usb_probe_one_net(intf, i);
1119 /* called by the usb core when the device is removed from the system */
1120 static void esd_usb_disconnect(struct usb_interface *intf)
1122 struct esd_usb *dev = usb_get_intfdata(intf);
1123 struct net_device *netdev;
1126 device_remove_file(&intf->dev, &dev_attr_firmware);
1127 device_remove_file(&intf->dev, &dev_attr_hardware);
1128 device_remove_file(&intf->dev, &dev_attr_nets);
1130 usb_set_intfdata(intf, NULL);
1133 for (i = 0; i < dev->net_count; i++) {
1135 netdev = dev->nets[i]->netdev;
1136 unregister_netdev(netdev);
1137 free_candev(netdev);
1140 unlink_all_urbs(dev);
1145 /* usb specific object needed to register this driver with the usb subsystem */
1146 static struct usb_driver esd_usb_driver = {
1147 .name = KBUILD_MODNAME,
1148 .probe = esd_usb_probe,
1149 .disconnect = esd_usb_disconnect,
1150 .id_table = esd_usb_table,
1153 module_usb_driver(esd_usb_driver);