20 #include <linux/signal.h>
21 #include <linux/slab.h>
22 #include <linux/module.h>
23 #include <linux/netdevice.h>
35 #define USB_ESDGMBH_VENDOR_ID 0x0ab4
36 #define USB_CANUSB2_PRODUCT_ID 0x0010
38 #define ESD_USB2_CAN_CLOCK 60000000
39 #define ESD_USB2_MAX_NETS 2
53 #define ESD_EXTID 0x20000000
54 #define ESD_EVENT 0x40000000
55 #define ESD_IDMASK 0x1fffffff
58 #define ESD_EV_CAN_ERROR_EXT 2
61 #define ESD_USB2_UBR 0x80000000
62 #define ESD_USB2_LOM 0x40000000
63 #define ESD_USB2_NO_BAUDRATE 0x7fffffff
64 #define ESD_USB2_TSEG1_MIN 1
65 #define ESD_USB2_TSEG1_MAX 16
66 #define ESD_USB2_TSEG1_SHIFT 16
67 #define ESD_USB2_TSEG2_MIN 1
68 #define ESD_USB2_TSEG2_MAX 8
69 #define ESD_USB2_TSEG2_SHIFT 20
70 #define ESD_USB2_SJW_MAX 4
71 #define ESD_USB2_SJW_SHIFT 14
72 #define ESD_USB2_BRP_MIN 1
73 #define ESD_USB2_BRP_MAX 1024
74 #define ESD_USB2_BRP_INC 1
75 #define ESD_USB2_3_SAMPLES 0x00800000
78 #define ESD_ID_ENABLE 0x80
79 #define ESD_MAX_ID_SEGMENT 64
82 #define SJA1000_ECC_SEG 0x1F
83 #define SJA1000_ECC_DIR 0x20
84 #define SJA1000_ECC_ERR 0x06
85 #define SJA1000_ECC_BIT 0x00
86 #define SJA1000_ECC_FORM 0x40
87 #define SJA1000_ECC_STUFF 0x80
88 #define SJA1000_ECC_MASK 0xc0
91 #define ESD_BUSSTATE_MASK 0xc0
92 #define ESD_BUSSTATE_WARN 0x40
93 #define ESD_BUSSTATE_ERRPASSIVE 0x80
94 #define ESD_BUSSTATE_BUSOFF 0xc0
96 #define RX_BUFFER_SIZE 1024
98 #define MAX_TX_URBS 16
225 struct esd_usb2_msg *
msg)
233 u8 state = msg->msg.rx.data[0];
234 u8 ecc = msg->msg.rx.data[1];
235 u8 txerr = msg->msg.rx.data[2];
236 u8 rxerr = msg->msg.rx.data[3];
255 priv->
can.can_stats.error_warning++;
259 priv->
can.can_stats.error_passive++;
266 priv->
can.can_stats.bus_error++;
293 cf->data[1] = (txerr > rxerr) ?
303 priv->
bec.txerr = txerr;
304 priv->
bec.rxerr = rxerr;
312 struct esd_usb2_msg *msg)
320 if (!netif_device_present(priv->
netdev))
326 esd_usb2_rx_event(priv, msg);
340 if (msg->msg.rx.dlc &
ESD_RTR) {
343 for (i = 0; i < cf->
can_dlc; i++)
344 cf->data[i] = msg->msg.rx.data[i];
357 struct esd_usb2_msg *msg)
363 if (!netif_device_present(netdev))
368 if (!msg->msg.txdone.status) {
381 netif_wake_queue(netdev);
384 static void esd_usb2_read_bulk_callback(
struct urb *
urb)
391 switch (urb->status) {
401 "Rx URB aborted (%d)\n", urb->status);
405 while (pos < urb->actual_length) {
406 struct esd_usb2_msg *
msg;
408 msg = (
struct esd_usb2_msg *)(urb->transfer_buffer + pos);
410 switch (msg->msg.hdr.cmd) {
412 esd_usb2_rx_can_msg(dev->
nets[msg->msg.rx.net], msg);
416 esd_usb2_tx_done_msg(dev->
nets[msg->msg.txdone.net],
421 pos += msg->msg.hdr.len << 2;
423 if (pos > urb->actual_length) {
430 usb_fill_bulk_urb(urb, dev->
udev, usb_rcvbulkpipe(dev->
udev, 1),
432 esd_usb2_read_bulk_callback, dev);
442 "failed resubmitting read bulk urb: %d\n", retval);
451 static void esd_usb2_write_bulk_callback(
struct urb *urb)
457 size_t size =
sizeof(
struct esd_usb2_msg);
461 priv = context->
priv;
467 urb->transfer_buffer, urb->transfer_dma);
469 if (!netif_device_present(netdev))
473 netdev_info(netdev,
"Tx URB aborted (%d)\n", urb->status);
482 struct esd_usb2 *dev = usb_get_intfdata(intf);
484 return sprintf(buf,
"%d.%d.%d\n",
495 struct esd_usb2 *dev = usb_get_intfdata(intf);
497 return sprintf(buf,
"%d.%d.%d\n",
508 struct esd_usb2 *dev = usb_get_intfdata(intf);
514 static int esd_usb2_send_msg(
struct esd_usb2 *dev,
struct esd_usb2_msg *msg)
519 usb_sndbulkpipe(dev->
udev, 2),
521 msg->msg.hdr.len << 2,
526 static int esd_usb2_wait_msg(
struct esd_usb2 *dev,
527 struct esd_usb2_msg *msg)
532 usb_rcvbulkpipe(dev->
udev, 1),
539 static int esd_usb2_setup_rx_urbs(
struct esd_usb2 *dev)
547 struct urb *urb =
NULL;
554 "No memory left for URBs\n");
563 "No memory left for USB buffer\n");
568 usb_fill_bulk_urb(urb, dev->
udev,
569 usb_rcvbulkpipe(dev->
udev, 1),
571 esd_usb2_read_bulk_callback, dev);
572 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
591 dev_err(dev->
udev->dev.parent,
"couldn't setup read URBs\n");
596 if (i < MAX_RX_URBS) {
598 "rx performance may be slow\n");
612 struct esd_usb2_msg msg;
630 msg.msg.filter.net = priv->
index;
637 err = esd_usb2_send_msg(dev, &msg);
641 err = esd_usb2_setup_rx_urbs(dev);
653 netdev_err(netdev,
"couldn't start device: %d\n", err);
658 static void unlink_all_urbs(
struct esd_usb2 *dev)
676 static int esd_usb2_open(
struct net_device *netdev)
687 err = esd_usb2_start(priv);
689 netdev_warn(netdev,
"couldn't start device: %d\n", err);
696 netif_start_queue(netdev);
709 struct esd_usb2_msg *msg;
714 size_t size =
sizeof(
struct esd_usb2_msg);
716 if (can_dropped_invalid_skb(netdev, skb))
722 netdev_err(netdev,
"No memory left for URBs\n");
731 netdev_err(netdev,
"No memory left for USB buffer\n");
737 msg = (
struct esd_usb2_msg *)buf;
739 msg->msg.hdr.len = 3;
741 msg->msg.tx.net = priv->
index;
751 for (i = 0; i < cf->
can_dlc; i++)
752 msg->msg.tx.data[i] = cf->data[i];
754 msg->msg.hdr.len += (cf->
can_dlc + 3) >> 2;
757 if (priv->
tx_contexts[i].echo_index == MAX_TX_URBS) {
767 netdev_warn(netdev,
"couldn't find free context\n");
777 msg->msg.tx.hnd = 0x80000000 |
i;
779 usb_fill_bulk_urb(urb, dev->
udev, usb_sndbulkpipe(dev->
udev, 2), buf,
780 msg->msg.hdr.len << 2,
781 esd_usb2_write_bulk_callback, context);
783 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
793 netif_stop_queue(netdev);
807 netdev_warn(netdev,
"failed tx_urb %d\n", err);
832 static int esd_usb2_close(
struct net_device *netdev)
835 struct esd_usb2_msg msg;
841 msg.msg.filter.net = priv->
index;
844 msg.msg.filter.mask[i] = 0;
845 if (esd_usb2_send_msg(priv->
usb2, &msg) < 0)
846 netdev_err(netdev,
"sending idadd message failed\n");
851 msg.msg.setbaud.net = priv->
index;
852 msg.msg.setbaud.rsvd = 0;
854 if (esd_usb2_send_msg(priv->
usb2, &msg) < 0)
855 netdev_err(netdev,
"sending setbaud message failed\n");
859 netif_stop_queue(netdev);
869 .ndo_open = esd_usb2_open,
870 .ndo_stop = esd_usb2_close,
871 .ndo_start_xmit = esd_usb2_start_xmit,
886 static int esd_usb2_set_bittiming(
struct net_device *netdev)
890 struct esd_usb2_msg msg;
907 msg.msg.setbaud.net = priv->
index;
908 msg.msg.setbaud.rsvd = 0;
911 netdev_info(netdev,
"setting BTR=%#x\n", canbtr);
913 return esd_usb2_send_msg(priv->
usb2, &msg);
916 static int esd_usb2_get_berr_counter(
const struct net_device *netdev,
936 netif_wake_queue(netdev);
948 struct esd_usb2 *dev = usb_get_intfdata(intf);
956 dev_err(&intf->dev,
"couldn't alloc candev\n");
961 priv = netdev_priv(netdev);
975 priv->
can.bittiming_const = &esd_usb2_bittiming_const;
976 priv->
can.do_set_bittiming = esd_usb2_set_bittiming;
977 priv->
can.do_set_mode = esd_usb2_set_mode;
978 priv->
can.do_get_berr_counter = esd_usb2_get_berr_counter;
989 dev_err(&intf->dev,
"couldn't register CAN device: %d\n", err);
996 netdev_info(netdev,
"device %s registered\n", netdev->
name);
1012 struct esd_usb2_msg msg;
1021 dev->
udev = interface_to_usbdev(intf);
1025 usb_set_intfdata(intf, dev);
1029 msg.msg.hdr.len = 2;
1030 msg.msg.version.rsvd = 0;
1031 msg.msg.version.flags = 0;
1032 msg.msg.version.drv_version = 0;
1034 err = esd_usb2_send_msg(dev, &msg);
1036 dev_err(&intf->dev,
"sending version message failed\n");
1040 err = esd_usb2_wait_msg(dev, &msg);
1042 dev_err(&intf->dev,
"no version message answer\n");
1051 "Couldn't create device file for firmware\n");
1055 "Couldn't create device file for hardware\n");
1059 "Couldn't create device file for nets\n");
1063 esd_usb2_probe_one_net(intf, i);
1078 struct esd_usb2 *dev = usb_get_intfdata(intf);
1086 usb_set_intfdata(intf,
NULL);
1091 netdev = dev->
nets[
i]->netdev;
1096 unlink_all_urbs(dev);
1101 static struct usb_driver esd_usb2_driver = {
1103 .probe = esd_usb2_probe,
1104 .disconnect = esd_usb2_disconnect,
1105 .id_table = esd_usb2_table,