1 // SPDX-License-Identifier: GPL-2.0-only
3 * CAN driver for esd CAN-USB/2 and CAN-USB/Micro
5 * Copyright (C) 2010-2012 Matthias Fuchs <matthias.fuchs@esd.eu>, esd gmbh
7 #include <linux/signal.h>
8 #include <linux/slab.h>
9 #include <linux/module.h>
10 #include <linux/netdevice.h>
11 #include <linux/usb.h>
13 #include <linux/can.h>
14 #include <linux/can/dev.h>
15 #include <linux/can/error.h>
17 MODULE_AUTHOR("Matthias Fuchs <matthias.fuchs@esd.eu>");
18 MODULE_DESCRIPTION("CAN driver for esd CAN-USB/2 and CAN-USB/Micro interfaces");
19 MODULE_LICENSE("GPL v2");
21 /* Define these values to match your devices */
22 #define USB_ESDGMBH_VENDOR_ID 0x0ab4
23 #define USB_CANUSB2_PRODUCT_ID 0x0010
24 #define USB_CANUSBM_PRODUCT_ID 0x0011
26 #define ESD_USB2_CAN_CLOCK 60000000
27 #define ESD_USBM_CAN_CLOCK 36000000
28 #define ESD_USB2_MAX_NETS 2
31 #define CMD_VERSION 1 /* also used for VERSION_REPLY */
32 #define CMD_CAN_RX 2 /* device to host only */
33 #define CMD_CAN_TX 3 /* also used for TX_DONE */
34 #define CMD_SETBAUD 4 /* also used for SETBAUD_REPLY */
35 #define CMD_TS 5 /* also used for TS_REPLY */
36 #define CMD_IDADD 6 /* also used for IDADD_REPLY */
38 /* esd CAN message flags - dlc field */
41 /* esd CAN message flags - id field */
42 #define ESD_EXTID 0x20000000
43 #define ESD_EVENT 0x40000000
44 #define ESD_IDMASK 0x1fffffff
46 /* esd CAN event ids used by this driver */
47 #define ESD_EV_CAN_ERROR_EXT 2
49 /* baudrate message flags */
50 #define ESD_USB2_UBR 0x80000000
51 #define ESD_USB2_LOM 0x40000000
52 #define ESD_USB2_NO_BAUDRATE 0x7fffffff
53 #define ESD_USB2_TSEG1_MIN 1
54 #define ESD_USB2_TSEG1_MAX 16
55 #define ESD_USB2_TSEG1_SHIFT 16
56 #define ESD_USB2_TSEG2_MIN 1
57 #define ESD_USB2_TSEG2_MAX 8
58 #define ESD_USB2_TSEG2_SHIFT 20
59 #define ESD_USB2_SJW_MAX 4
60 #define ESD_USB2_SJW_SHIFT 14
61 #define ESD_USBM_SJW_SHIFT 24
62 #define ESD_USB2_BRP_MIN 1
63 #define ESD_USB2_BRP_MAX 1024
64 #define ESD_USB2_BRP_INC 1
65 #define ESD_USB2_3_SAMPLES 0x00800000
67 /* esd IDADD message */
68 #define ESD_ID_ENABLE 0x80
69 #define ESD_MAX_ID_SEGMENT 64
71 /* SJA1000 ECC register (emulated by usb2 firmware) */
72 #define SJA1000_ECC_SEG 0x1F
73 #define SJA1000_ECC_DIR 0x20
74 #define SJA1000_ECC_ERR 0x06
75 #define SJA1000_ECC_BIT 0x00
76 #define SJA1000_ECC_FORM 0x40
77 #define SJA1000_ECC_STUFF 0x80
78 #define SJA1000_ECC_MASK 0xc0
80 /* esd bus state event codes */
81 #define ESD_BUSSTATE_MASK 0xc0
82 #define ESD_BUSSTATE_WARN 0x40
83 #define ESD_BUSSTATE_ERRPASSIVE 0x80
84 #define ESD_BUSSTATE_BUSOFF 0xc0
86 #define RX_BUFFER_SIZE 1024
88 #define MAX_TX_URBS 16 /* must be power of 2 */
91 u8 len; /* len is always the total message length in 32bit words */
104 struct version_reply_msg {
121 __le32 id; /* upper 3 bits contain flags */
130 u32 hnd; /* opaque handle, not used by device */
131 __le32 id; /* upper 3 bits contain flags */
140 u32 hnd; /* opaque handle, not used by device */
144 struct id_filter_msg {
149 __le32 mask[ESD_MAX_ID_SEGMENT + 1];
152 struct set_baudrate_msg {
160 /* Main message type used between library and application */
161 struct __attribute__ ((packed)) esd_usb2_msg {
163 struct header_msg hdr;
164 struct version_msg version;
165 struct version_reply_msg version_reply;
168 struct tx_done_msg txdone;
169 struct set_baudrate_msg setbaud;
170 struct id_filter_msg filter;
174 static struct usb_device_id esd_usb2_table[] = {
175 {USB_DEVICE(USB_ESDGMBH_VENDOR_ID, USB_CANUSB2_PRODUCT_ID)},
176 {USB_DEVICE(USB_ESDGMBH_VENDOR_ID, USB_CANUSBM_PRODUCT_ID)},
179 MODULE_DEVICE_TABLE(usb, esd_usb2_table);
181 struct esd_usb2_net_priv;
183 struct esd_tx_urb_context {
184 struct esd_usb2_net_priv *priv;
186 int len; /* CAN payload length */
190 struct usb_device *udev;
191 struct esd_usb2_net_priv *nets[ESD_USB2_MAX_NETS];
193 struct usb_anchor rx_submitted;
200 struct esd_usb2_net_priv {
201 struct can_priv can; /* must be the first member */
203 atomic_t active_tx_jobs;
204 struct usb_anchor tx_submitted;
205 struct esd_tx_urb_context tx_contexts[MAX_TX_URBS];
207 struct esd_usb2 *usb2;
208 struct net_device *netdev;
211 struct can_berr_counter bec;
214 static void esd_usb2_rx_event(struct esd_usb2_net_priv *priv,
215 struct esd_usb2_msg *msg)
217 struct net_device_stats *stats = &priv->netdev->stats;
218 struct can_frame *cf;
220 u32 id = le32_to_cpu(msg->msg.rx.id) & ESD_IDMASK;
222 if (id == ESD_EV_CAN_ERROR_EXT) {
223 u8 state = msg->msg.rx.data[0];
224 u8 ecc = msg->msg.rx.data[1];
225 u8 txerr = msg->msg.rx.data[2];
226 u8 rxerr = msg->msg.rx.data[3];
228 skb = alloc_can_err_skb(priv->netdev, &cf);
234 if (state != priv->old_state) {
235 priv->old_state = state;
237 switch (state & ESD_BUSSTATE_MASK) {
238 case ESD_BUSSTATE_BUSOFF:
239 priv->can.state = CAN_STATE_BUS_OFF;
240 cf->can_id |= CAN_ERR_BUSOFF;
241 priv->can.can_stats.bus_off++;
242 can_bus_off(priv->netdev);
244 case ESD_BUSSTATE_WARN:
245 priv->can.state = CAN_STATE_ERROR_WARNING;
246 priv->can.can_stats.error_warning++;
248 case ESD_BUSSTATE_ERRPASSIVE:
249 priv->can.state = CAN_STATE_ERROR_PASSIVE;
250 priv->can.can_stats.error_passive++;
253 priv->can.state = CAN_STATE_ERROR_ACTIVE;
257 priv->can.can_stats.bus_error++;
260 cf->can_id |= CAN_ERR_PROT | CAN_ERR_BUSERROR;
262 switch (ecc & SJA1000_ECC_MASK) {
263 case SJA1000_ECC_BIT:
264 cf->data[2] |= CAN_ERR_PROT_BIT;
266 case SJA1000_ECC_FORM:
267 cf->data[2] |= CAN_ERR_PROT_FORM;
269 case SJA1000_ECC_STUFF:
270 cf->data[2] |= CAN_ERR_PROT_STUFF;
273 cf->data[3] = ecc & SJA1000_ECC_SEG;
277 /* Error occurred during transmission? */
278 if (!(ecc & SJA1000_ECC_DIR))
279 cf->data[2] |= CAN_ERR_PROT_TX;
281 if (priv->can.state == CAN_STATE_ERROR_WARNING ||
282 priv->can.state == CAN_STATE_ERROR_PASSIVE) {
283 cf->data[1] = (txerr > rxerr) ?
284 CAN_ERR_CRTL_TX_PASSIVE :
285 CAN_ERR_CRTL_RX_PASSIVE;
291 priv->bec.txerr = txerr;
292 priv->bec.rxerr = rxerr;
295 stats->rx_bytes += cf->len;
300 static void esd_usb2_rx_can_msg(struct esd_usb2_net_priv *priv,
301 struct esd_usb2_msg *msg)
303 struct net_device_stats *stats = &priv->netdev->stats;
304 struct can_frame *cf;
309 if (!netif_device_present(priv->netdev))
312 id = le32_to_cpu(msg->msg.rx.id);
314 if (id & ESD_EVENT) {
315 esd_usb2_rx_event(priv, msg);
317 skb = alloc_can_skb(priv->netdev, &cf);
323 cf->can_id = id & ESD_IDMASK;
324 can_frame_set_cc_len(cf, msg->msg.rx.dlc & ~ESD_RTR,
328 cf->can_id |= CAN_EFF_FLAG;
330 if (msg->msg.rx.dlc & ESD_RTR) {
331 cf->can_id |= CAN_RTR_FLAG;
333 for (i = 0; i < cf->len; i++)
334 cf->data[i] = msg->msg.rx.data[i];
338 stats->rx_bytes += cf->len;
345 static void esd_usb2_tx_done_msg(struct esd_usb2_net_priv *priv,
346 struct esd_usb2_msg *msg)
348 struct net_device_stats *stats = &priv->netdev->stats;
349 struct net_device *netdev = priv->netdev;
350 struct esd_tx_urb_context *context;
352 if (!netif_device_present(netdev))
355 context = &priv->tx_contexts[msg->msg.txdone.hnd & (MAX_TX_URBS - 1)];
357 if (!msg->msg.txdone.status) {
359 stats->tx_bytes += context->len;
360 can_get_echo_skb(netdev, context->echo_index);
363 can_free_echo_skb(netdev, context->echo_index);
366 /* Release context */
367 context->echo_index = MAX_TX_URBS;
368 atomic_dec(&priv->active_tx_jobs);
370 netif_wake_queue(netdev);
373 static void esd_usb2_read_bulk_callback(struct urb *urb)
375 struct esd_usb2 *dev = urb->context;
380 switch (urb->status) {
381 case 0: /* success */
391 dev_info(dev->udev->dev.parent,
392 "Rx URB aborted (%d)\n", urb->status);
396 while (pos < urb->actual_length) {
397 struct esd_usb2_msg *msg;
399 msg = (struct esd_usb2_msg *)(urb->transfer_buffer + pos);
401 switch (msg->msg.hdr.cmd) {
403 if (msg->msg.rx.net >= dev->net_count) {
404 dev_err(dev->udev->dev.parent, "format error\n");
408 esd_usb2_rx_can_msg(dev->nets[msg->msg.rx.net], msg);
412 if (msg->msg.txdone.net >= dev->net_count) {
413 dev_err(dev->udev->dev.parent, "format error\n");
417 esd_usb2_tx_done_msg(dev->nets[msg->msg.txdone.net],
422 pos += msg->msg.hdr.len << 2;
424 if (pos > urb->actual_length) {
425 dev_err(dev->udev->dev.parent, "format error\n");
431 usb_fill_bulk_urb(urb, dev->udev, usb_rcvbulkpipe(dev->udev, 1),
432 urb->transfer_buffer, RX_BUFFER_SIZE,
433 esd_usb2_read_bulk_callback, dev);
435 retval = usb_submit_urb(urb, GFP_ATOMIC);
436 if (retval == -ENODEV) {
437 for (i = 0; i < dev->net_count; i++) {
439 netif_device_detach(dev->nets[i]->netdev);
442 dev_err(dev->udev->dev.parent,
443 "failed resubmitting read bulk urb: %d\n", retval);
450 * callback for bulk IN urb
452 static void esd_usb2_write_bulk_callback(struct urb *urb)
454 struct esd_tx_urb_context *context = urb->context;
455 struct esd_usb2_net_priv *priv;
456 struct net_device *netdev;
457 size_t size = sizeof(struct esd_usb2_msg);
461 priv = context->priv;
462 netdev = priv->netdev;
464 /* free up our allocated buffer */
465 usb_free_coherent(urb->dev, size,
466 urb->transfer_buffer, urb->transfer_dma);
468 if (!netif_device_present(netdev))
472 netdev_info(netdev, "Tx URB aborted (%d)\n", urb->status);
474 netif_trans_update(netdev);
477 static ssize_t show_firmware(struct device *d,
478 struct device_attribute *attr, char *buf)
480 struct usb_interface *intf = to_usb_interface(d);
481 struct esd_usb2 *dev = usb_get_intfdata(intf);
483 return sprintf(buf, "%d.%d.%d\n",
484 (dev->version >> 12) & 0xf,
485 (dev->version >> 8) & 0xf,
486 dev->version & 0xff);
488 static DEVICE_ATTR(firmware, 0444, show_firmware, NULL);
490 static ssize_t show_hardware(struct device *d,
491 struct device_attribute *attr, char *buf)
493 struct usb_interface *intf = to_usb_interface(d);
494 struct esd_usb2 *dev = usb_get_intfdata(intf);
496 return sprintf(buf, "%d.%d.%d\n",
497 (dev->version >> 28) & 0xf,
498 (dev->version >> 24) & 0xf,
499 (dev->version >> 16) & 0xff);
501 static DEVICE_ATTR(hardware, 0444, show_hardware, NULL);
503 static ssize_t show_nets(struct device *d,
504 struct device_attribute *attr, char *buf)
506 struct usb_interface *intf = to_usb_interface(d);
507 struct esd_usb2 *dev = usb_get_intfdata(intf);
509 return sprintf(buf, "%d", dev->net_count);
511 static DEVICE_ATTR(nets, 0444, show_nets, NULL);
513 static int esd_usb2_send_msg(struct esd_usb2 *dev, struct esd_usb2_msg *msg)
517 return usb_bulk_msg(dev->udev,
518 usb_sndbulkpipe(dev->udev, 2),
520 msg->msg.hdr.len << 2,
525 static int esd_usb2_wait_msg(struct esd_usb2 *dev,
526 struct esd_usb2_msg *msg)
530 return usb_bulk_msg(dev->udev,
531 usb_rcvbulkpipe(dev->udev, 1),
538 static int esd_usb2_setup_rx_urbs(struct esd_usb2 *dev)
545 for (i = 0; i < MAX_RX_URBS; i++) {
546 struct urb *urb = NULL;
549 /* create a URB, and a buffer for it */
550 urb = usb_alloc_urb(0, GFP_KERNEL);
556 buf = usb_alloc_coherent(dev->udev, RX_BUFFER_SIZE, GFP_KERNEL,
559 dev_warn(dev->udev->dev.parent,
560 "No memory left for USB buffer\n");
565 usb_fill_bulk_urb(urb, dev->udev,
566 usb_rcvbulkpipe(dev->udev, 1),
568 esd_usb2_read_bulk_callback, dev);
569 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
570 usb_anchor_urb(urb, &dev->rx_submitted);
572 err = usb_submit_urb(urb, GFP_KERNEL);
574 usb_unanchor_urb(urb);
575 usb_free_coherent(dev->udev, RX_BUFFER_SIZE, buf,
580 /* Drop reference, USB core will take care of freeing it */
586 /* Did we submit any URBs */
588 dev_err(dev->udev->dev.parent, "couldn't setup read URBs\n");
592 /* Warn if we've couldn't transmit all the URBs */
593 if (i < MAX_RX_URBS) {
594 dev_warn(dev->udev->dev.parent,
595 "rx performance may be slow\n");
605 static int esd_usb2_start(struct esd_usb2_net_priv *priv)
607 struct esd_usb2 *dev = priv->usb2;
608 struct net_device *netdev = priv->netdev;
609 struct esd_usb2_msg *msg;
612 msg = kmalloc(sizeof(*msg), GFP_KERNEL);
620 * The IDADD message takes up to 64 32 bit bitmasks (2048 bits).
621 * Each bit represents one 11 bit CAN identifier. A set bit
622 * enables reception of the corresponding CAN identifier. A cleared
623 * bit disabled this identifier. An additional bitmask value
624 * following the CAN 2.0A bits is used to enable reception of
625 * extended CAN frames. Only the LSB of this final mask is checked
626 * for the complete 29 bit ID range. The IDADD message also allows
627 * filter configuration for an ID subset. In this case you can add
628 * the number of the starting bitmask (0..64) to the filter.option
629 * field followed by only some bitmasks.
631 msg->msg.hdr.cmd = CMD_IDADD;
632 msg->msg.hdr.len = 2 + ESD_MAX_ID_SEGMENT;
633 msg->msg.filter.net = priv->index;
634 msg->msg.filter.option = ESD_ID_ENABLE; /* start with segment 0 */
635 for (i = 0; i < ESD_MAX_ID_SEGMENT; i++)
636 msg->msg.filter.mask[i] = cpu_to_le32(0xffffffff);
637 /* enable 29bit extended IDs */
638 msg->msg.filter.mask[ESD_MAX_ID_SEGMENT] = cpu_to_le32(0x00000001);
640 err = esd_usb2_send_msg(dev, msg);
644 err = esd_usb2_setup_rx_urbs(dev);
648 priv->can.state = CAN_STATE_ERROR_ACTIVE;
652 netif_device_detach(netdev);
654 netdev_err(netdev, "couldn't start device: %d\n", err);
660 static void unlink_all_urbs(struct esd_usb2 *dev)
662 struct esd_usb2_net_priv *priv;
665 usb_kill_anchored_urbs(&dev->rx_submitted);
666 for (i = 0; i < dev->net_count; i++) {
669 usb_kill_anchored_urbs(&priv->tx_submitted);
670 atomic_set(&priv->active_tx_jobs, 0);
672 for (j = 0; j < MAX_TX_URBS; j++)
673 priv->tx_contexts[j].echo_index = MAX_TX_URBS;
678 static int esd_usb2_open(struct net_device *netdev)
680 struct esd_usb2_net_priv *priv = netdev_priv(netdev);
684 err = open_candev(netdev);
688 /* finally start device */
689 err = esd_usb2_start(priv);
691 netdev_warn(netdev, "couldn't start device: %d\n", err);
692 close_candev(netdev);
696 netif_start_queue(netdev);
701 static netdev_tx_t esd_usb2_start_xmit(struct sk_buff *skb,
702 struct net_device *netdev)
704 struct esd_usb2_net_priv *priv = netdev_priv(netdev);
705 struct esd_usb2 *dev = priv->usb2;
706 struct esd_tx_urb_context *context = NULL;
707 struct net_device_stats *stats = &netdev->stats;
708 struct can_frame *cf = (struct can_frame *)skb->data;
709 struct esd_usb2_msg *msg;
713 int ret = NETDEV_TX_OK;
714 size_t size = sizeof(struct esd_usb2_msg);
716 if (can_dropped_invalid_skb(netdev, skb))
719 /* create a URB, and a buffer for it, and copy the data to the URB */
720 urb = usb_alloc_urb(0, GFP_ATOMIC);
727 buf = usb_alloc_coherent(dev->udev, size, GFP_ATOMIC,
730 netdev_err(netdev, "No memory left for USB buffer\n");
736 msg = (struct esd_usb2_msg *)buf;
738 msg->msg.hdr.len = 3; /* minimal length */
739 msg->msg.hdr.cmd = CMD_CAN_TX;
740 msg->msg.tx.net = priv->index;
741 msg->msg.tx.dlc = can_get_cc_dlc(cf, priv->can.ctrlmode);
742 msg->msg.tx.id = cpu_to_le32(cf->can_id & CAN_ERR_MASK);
744 if (cf->can_id & CAN_RTR_FLAG)
745 msg->msg.tx.dlc |= ESD_RTR;
747 if (cf->can_id & CAN_EFF_FLAG)
748 msg->msg.tx.id |= cpu_to_le32(ESD_EXTID);
750 for (i = 0; i < cf->len; i++)
751 msg->msg.tx.data[i] = cf->data[i];
753 msg->msg.hdr.len += (cf->len + 3) >> 2;
755 for (i = 0; i < MAX_TX_URBS; i++) {
756 if (priv->tx_contexts[i].echo_index == MAX_TX_URBS) {
757 context = &priv->tx_contexts[i];
763 * This may never happen.
766 netdev_warn(netdev, "couldn't find free context\n");
767 ret = NETDEV_TX_BUSY;
771 context->priv = priv;
772 context->echo_index = i;
773 context->len = cf->len;
775 /* hnd must not be 0 - MSB is stripped in txdone handling */
776 msg->msg.tx.hnd = 0x80000000 | i; /* returned in TX done message */
778 usb_fill_bulk_urb(urb, dev->udev, usb_sndbulkpipe(dev->udev, 2), buf,
779 msg->msg.hdr.len << 2,
780 esd_usb2_write_bulk_callback, context);
782 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
784 usb_anchor_urb(urb, &priv->tx_submitted);
786 can_put_echo_skb(skb, netdev, context->echo_index, 0);
788 atomic_inc(&priv->active_tx_jobs);
790 /* Slow down tx path */
791 if (atomic_read(&priv->active_tx_jobs) >= MAX_TX_URBS)
792 netif_stop_queue(netdev);
794 err = usb_submit_urb(urb, GFP_ATOMIC);
796 can_free_echo_skb(netdev, context->echo_index);
798 atomic_dec(&priv->active_tx_jobs);
799 usb_unanchor_urb(urb);
804 netif_device_detach(netdev);
806 netdev_warn(netdev, "failed tx_urb %d\n", err);
811 netif_trans_update(netdev);
814 * Release our reference to this URB, the USB core will eventually free
822 usb_free_coherent(dev->udev, size, buf, urb->transfer_dma);
831 static int esd_usb2_close(struct net_device *netdev)
833 struct esd_usb2_net_priv *priv = netdev_priv(netdev);
834 struct esd_usb2_msg *msg;
837 msg = kmalloc(sizeof(*msg), GFP_KERNEL);
841 /* Disable all IDs (see esd_usb2_start()) */
842 msg->msg.hdr.cmd = CMD_IDADD;
843 msg->msg.hdr.len = 2 + ESD_MAX_ID_SEGMENT;
844 msg->msg.filter.net = priv->index;
845 msg->msg.filter.option = ESD_ID_ENABLE; /* start with segment 0 */
846 for (i = 0; i <= ESD_MAX_ID_SEGMENT; i++)
847 msg->msg.filter.mask[i] = 0;
848 if (esd_usb2_send_msg(priv->usb2, msg) < 0)
849 netdev_err(netdev, "sending idadd message failed\n");
851 /* set CAN controller to reset mode */
852 msg->msg.hdr.len = 2;
853 msg->msg.hdr.cmd = CMD_SETBAUD;
854 msg->msg.setbaud.net = priv->index;
855 msg->msg.setbaud.rsvd = 0;
856 msg->msg.setbaud.baud = cpu_to_le32(ESD_USB2_NO_BAUDRATE);
857 if (esd_usb2_send_msg(priv->usb2, msg) < 0)
858 netdev_err(netdev, "sending setbaud message failed\n");
860 priv->can.state = CAN_STATE_STOPPED;
862 netif_stop_queue(netdev);
864 close_candev(netdev);
871 static const struct net_device_ops esd_usb2_netdev_ops = {
872 .ndo_open = esd_usb2_open,
873 .ndo_stop = esd_usb2_close,
874 .ndo_start_xmit = esd_usb2_start_xmit,
875 .ndo_change_mtu = can_change_mtu,
878 static const struct can_bittiming_const esd_usb2_bittiming_const = {
880 .tseg1_min = ESD_USB2_TSEG1_MIN,
881 .tseg1_max = ESD_USB2_TSEG1_MAX,
882 .tseg2_min = ESD_USB2_TSEG2_MIN,
883 .tseg2_max = ESD_USB2_TSEG2_MAX,
884 .sjw_max = ESD_USB2_SJW_MAX,
885 .brp_min = ESD_USB2_BRP_MIN,
886 .brp_max = ESD_USB2_BRP_MAX,
887 .brp_inc = ESD_USB2_BRP_INC,
890 static int esd_usb2_set_bittiming(struct net_device *netdev)
892 struct esd_usb2_net_priv *priv = netdev_priv(netdev);
893 struct can_bittiming *bt = &priv->can.bittiming;
894 struct esd_usb2_msg *msg;
899 canbtr = ESD_USB2_UBR;
900 if (priv->can.ctrlmode & CAN_CTRLMODE_LISTENONLY)
901 canbtr |= ESD_USB2_LOM;
903 canbtr |= (bt->brp - 1) & (ESD_USB2_BRP_MAX - 1);
905 if (le16_to_cpu(priv->usb2->udev->descriptor.idProduct) ==
906 USB_CANUSBM_PRODUCT_ID)
907 sjw_shift = ESD_USBM_SJW_SHIFT;
909 sjw_shift = ESD_USB2_SJW_SHIFT;
911 canbtr |= ((bt->sjw - 1) & (ESD_USB2_SJW_MAX - 1))
913 canbtr |= ((bt->prop_seg + bt->phase_seg1 - 1)
914 & (ESD_USB2_TSEG1_MAX - 1))
915 << ESD_USB2_TSEG1_SHIFT;
916 canbtr |= ((bt->phase_seg2 - 1) & (ESD_USB2_TSEG2_MAX - 1))
917 << ESD_USB2_TSEG2_SHIFT;
918 if (priv->can.ctrlmode & CAN_CTRLMODE_3_SAMPLES)
919 canbtr |= ESD_USB2_3_SAMPLES;
921 msg = kmalloc(sizeof(*msg), GFP_KERNEL);
925 msg->msg.hdr.len = 2;
926 msg->msg.hdr.cmd = CMD_SETBAUD;
927 msg->msg.setbaud.net = priv->index;
928 msg->msg.setbaud.rsvd = 0;
929 msg->msg.setbaud.baud = cpu_to_le32(canbtr);
931 netdev_info(netdev, "setting BTR=%#x\n", canbtr);
933 err = esd_usb2_send_msg(priv->usb2, msg);
939 static int esd_usb2_get_berr_counter(const struct net_device *netdev,
940 struct can_berr_counter *bec)
942 struct esd_usb2_net_priv *priv = netdev_priv(netdev);
944 bec->txerr = priv->bec.txerr;
945 bec->rxerr = priv->bec.rxerr;
950 static int esd_usb2_set_mode(struct net_device *netdev, enum can_mode mode)
954 netif_wake_queue(netdev);
964 static int esd_usb2_probe_one_net(struct usb_interface *intf, int index)
966 struct esd_usb2 *dev = usb_get_intfdata(intf);
967 struct net_device *netdev;
968 struct esd_usb2_net_priv *priv;
972 netdev = alloc_candev(sizeof(*priv), MAX_TX_URBS);
974 dev_err(&intf->dev, "couldn't alloc candev\n");
979 priv = netdev_priv(netdev);
981 init_usb_anchor(&priv->tx_submitted);
982 atomic_set(&priv->active_tx_jobs, 0);
984 for (i = 0; i < MAX_TX_URBS; i++)
985 priv->tx_contexts[i].echo_index = MAX_TX_URBS;
988 priv->netdev = netdev;
991 priv->can.state = CAN_STATE_STOPPED;
992 priv->can.ctrlmode_supported = CAN_CTRLMODE_LISTENONLY |
993 CAN_CTRLMODE_CC_LEN8_DLC;
995 if (le16_to_cpu(dev->udev->descriptor.idProduct) ==
996 USB_CANUSBM_PRODUCT_ID)
997 priv->can.clock.freq = ESD_USBM_CAN_CLOCK;
999 priv->can.clock.freq = ESD_USB2_CAN_CLOCK;
1000 priv->can.ctrlmode_supported |= CAN_CTRLMODE_3_SAMPLES;
1003 priv->can.bittiming_const = &esd_usb2_bittiming_const;
1004 priv->can.do_set_bittiming = esd_usb2_set_bittiming;
1005 priv->can.do_set_mode = esd_usb2_set_mode;
1006 priv->can.do_get_berr_counter = esd_usb2_get_berr_counter;
1008 netdev->flags |= IFF_ECHO; /* we support local echo */
1010 netdev->netdev_ops = &esd_usb2_netdev_ops;
1012 SET_NETDEV_DEV(netdev, &intf->dev);
1013 netdev->dev_id = index;
1015 err = register_candev(netdev);
1017 dev_err(&intf->dev, "couldn't register CAN device: %d\n", err);
1018 free_candev(netdev);
1023 dev->nets[index] = priv;
1024 netdev_info(netdev, "device %s registered\n", netdev->name);
1031 * probe function for new USB2 devices
1033 * check version information and number of available
1036 static int esd_usb2_probe(struct usb_interface *intf,
1037 const struct usb_device_id *id)
1039 struct esd_usb2 *dev;
1040 struct esd_usb2_msg *msg;
1043 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1049 dev->udev = interface_to_usbdev(intf);
1051 init_usb_anchor(&dev->rx_submitted);
1053 usb_set_intfdata(intf, dev);
1055 msg = kmalloc(sizeof(*msg), GFP_KERNEL);
1061 /* query number of CAN interfaces (nets) */
1062 msg->msg.hdr.cmd = CMD_VERSION;
1063 msg->msg.hdr.len = 2;
1064 msg->msg.version.rsvd = 0;
1065 msg->msg.version.flags = 0;
1066 msg->msg.version.drv_version = 0;
1068 err = esd_usb2_send_msg(dev, msg);
1070 dev_err(&intf->dev, "sending version message failed\n");
1074 err = esd_usb2_wait_msg(dev, msg);
1076 dev_err(&intf->dev, "no version message answer\n");
1080 dev->net_count = (int)msg->msg.version_reply.nets;
1081 dev->version = le32_to_cpu(msg->msg.version_reply.version);
1083 if (device_create_file(&intf->dev, &dev_attr_firmware))
1085 "Couldn't create device file for firmware\n");
1087 if (device_create_file(&intf->dev, &dev_attr_hardware))
1089 "Couldn't create device file for hardware\n");
1091 if (device_create_file(&intf->dev, &dev_attr_nets))
1093 "Couldn't create device file for nets\n");
1095 /* do per device probing */
1096 for (i = 0; i < dev->net_count; i++)
1097 esd_usb2_probe_one_net(intf, i);
1108 * called by the usb core when the device is removed from the system
1110 static void esd_usb2_disconnect(struct usb_interface *intf)
1112 struct esd_usb2 *dev = usb_get_intfdata(intf);
1113 struct net_device *netdev;
1116 device_remove_file(&intf->dev, &dev_attr_firmware);
1117 device_remove_file(&intf->dev, &dev_attr_hardware);
1118 device_remove_file(&intf->dev, &dev_attr_nets);
1120 usb_set_intfdata(intf, NULL);
1123 for (i = 0; i < dev->net_count; i++) {
1125 netdev = dev->nets[i]->netdev;
1126 unregister_netdev(netdev);
1127 free_candev(netdev);
1130 unlink_all_urbs(dev);
1135 /* usb specific object needed to register this driver with the usb subsystem */
1136 static struct usb_driver esd_usb2_driver = {
1138 .probe = esd_usb2_probe,
1139 .disconnect = esd_usb2_disconnect,
1140 .id_table = esd_usb2_table,
1143 module_usb_driver(esd_usb2_driver);