20 #include <linux/signal.h>
21 #include <linux/slab.h>
22 #include <linux/module.h>
23 #include <linux/netdevice.h>
35 #define CONTR_CAN_MESSAGE 0x04
36 #define CONTR_CAN_STATE 0x0C
37 #define CONTR_BUS_ERROR 0x1C
40 #define CONTR_CONT_OFF 0
41 #define CONTR_CONT_ON 1
45 #define CPC_MSG_TYPE_CAN_FRAME 1
46 #define CPC_MSG_TYPE_RTR_FRAME 8
47 #define CPC_MSG_TYPE_CAN_PARAMS 12
48 #define CPC_MSG_TYPE_CAN_STATE 14
49 #define CPC_MSG_TYPE_EXT_CAN_FRAME 16
50 #define CPC_MSG_TYPE_EXT_RTR_FRAME 17
51 #define CPC_MSG_TYPE_CONTROL 19
52 #define CPC_MSG_TYPE_CONFIRM 20
53 #define CPC_MSG_TYPE_OVERRUN 21
54 #define CPC_MSG_TYPE_CAN_FRAME_ERROR 23
55 #define CPC_MSG_TYPE_ERR_COUNTER 25
58 #define CPC_CMD_TYPE_CAN_FRAME 1
59 #define CPC_CMD_TYPE_CONTROL 3
60 #define CPC_CMD_TYPE_CAN_PARAMS 6
61 #define CPC_CMD_TYPE_RTR_FRAME 13
62 #define CPC_CMD_TYPE_CAN_STATE 14
63 #define CPC_CMD_TYPE_EXT_CAN_FRAME 15
64 #define CPC_CMD_TYPE_EXT_RTR_FRAME 16
65 #define CPC_CMD_TYPE_CAN_EXIT 200
67 #define CPC_CMD_TYPE_INQ_ERR_COUNTER 25
68 #define CPC_CMD_TYPE_CLEAR_MSG_QUEUE 8
69 #define CPC_CMD_TYPE_CLEAR_CMD_QUEUE 28
71 #define CPC_CC_TYPE_SJA1000 2
73 #define CPC_CAN_ECODE_ERRFRAME 0x01
76 #define CPC_OVR_EVENT_CAN 0x01
77 #define CPC_OVR_EVENT_CANSTATE 0x02
78 #define CPC_OVR_EVENT_BUSERROR 0x04
84 #define CPC_OVR_HW 0x80
87 #define CPC_MSG_HEADER_LEN 11
88 #define CPC_CAN_MSG_MIN_SIZE 5
91 #define USB_CPCUSB_VENDOR_ID 0x12D6
93 #define USB_CPCUSB_ARM7_PRODUCT_ID 0x0444
96 #define SJA1000_MOD_NORMAL 0x00
97 #define SJA1000_MOD_RM 0x01
100 #define SJA1000_ECC_SEG 0x1F
101 #define SJA1000_ECC_DIR 0x20
102 #define SJA1000_ECC_ERR 0x06
103 #define SJA1000_ECC_BIT 0x00
104 #define SJA1000_ECC_FORM 0x40
105 #define SJA1000_ECC_STUFF 0x80
106 #define SJA1000_ECC_MASK 0xc0
109 #define SJA1000_SR_BS 0x80
110 #define SJA1000_SR_ES 0x40
112 #define SJA1000_DEFAULT_OUTPUT_CONTROL 0xDA
119 #define EMS_USB_ARM7_CLOCK 8000000
230 #define RX_BUFFER_SIZE 64
231 #define CPC_HEADER_SIZE 4
232 #define INTR_IN_BUFFER_SIZE 4
234 #define MAX_RX_URBS 10
235 #define MAX_TX_URBS 10
271 static void ems_usb_read_interrupt_callback(
struct urb *
urb)
277 if (!netif_device_present(netdev))
280 switch (urb->status) {
291 netdev_info(netdev,
"Rx interrupt aborted %d\n", urb->status);
300 netdev_err(netdev,
"failed resubmitting intr urb: %d\n", err);
325 for (i = 0; i < cf->
can_dlc; i++)
355 dev->
can.can_stats.error_warning++;
358 dev->
can.can_stats.error_passive++;
362 u8 txerr = msg->
msg.
error.cc.regs.sja1000.txerr;
363 u8 rxerr = msg->
msg.
error.cc.regs.sja1000.rxerr;
366 dev->
can.can_stats.bus_error++;
393 cf->data[1] = (txerr > rxerr) ?
413 static void ems_usb_read_bulk_callback(
struct urb *urb)
415 struct ems_usb *dev = urb->context;
421 if (!netif_device_present(netdev))
424 switch (urb->status) {
432 netdev_info(netdev,
"Rx URB aborted (%d)\n", urb->status);
438 u8 *ibuf = urb->transfer_buffer;
441 msg_count = ibuf[0] & ~0x80;
442 again = ibuf[0] & 0x80;
452 ems_usb_rx_err(dev, msg);
459 ems_usb_rx_can_msg(dev, msg);
464 ems_usb_rx_err(dev, msg);
469 ems_usb_rx_err(dev, msg);
476 if (start > urb->transfer_buffer_length) {
477 netdev_err(netdev,
"format error\n");
484 usb_fill_bulk_urb(urb, dev->
udev, usb_rcvbulkpipe(dev->
udev, 2),
486 ems_usb_read_bulk_callback, dev);
494 "failed resubmitting read bulk urb: %d\n", retval);
500 static void ems_usb_write_bulk_callback(
struct urb *urb)
513 urb->transfer_buffer, urb->transfer_dma);
517 if (!netif_device_present(netdev))
521 netdev_info(netdev,
"Tx URB aborted (%d)\n", urb->status);
526 netdev->
stats.tx_packets++;
527 netdev->
stats.tx_bytes += context->
dlc;
534 if (netif_queue_stopped(netdev))
535 netif_wake_queue(netdev);
555 &actual_length, 1000);
572 static int ems_usb_control_cmd(
struct ems_usb *dev,
u8 val)
583 return ems_usb_command_msg(dev, &
cmd);
589 static int ems_usb_start(
struct ems_usb *dev)
598 struct urb *urb =
NULL;
604 netdev_err(netdev,
"No memory left for URBs\n");
612 netdev_err(netdev,
"No memory left for USB buffer\n");
618 usb_fill_bulk_urb(urb, dev->
udev, usb_rcvbulkpipe(dev->
udev, 2),
620 ems_usb_read_bulk_callback, dev);
621 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
638 netdev_warn(netdev,
"couldn't setup read URBs\n");
644 netdev_warn(netdev,
"rx performance may be slow\n");
648 usb_rcvintpipe(dev->
udev, 1),
651 ems_usb_read_interrupt_callback, dev, 1);
655 netdev_warn(netdev,
"intr URB submit failed: %d\n", err);
684 netdev_warn(netdev,
"couldn't submit control: %d\n", err);
689 static void unlink_all_urbs(
struct ems_usb *dev)
704 static int ems_usb_open(
struct net_device *netdev)
706 struct ems_usb *dev = netdev_priv(netdev);
719 err = ems_usb_start(dev);
724 netdev_warn(netdev,
"couldn't start device: %d\n", err);
733 netif_start_queue(netdev);
740 struct ems_usb *dev = netdev_priv(netdev);
751 if (can_dropped_invalid_skb(netdev, skb))
757 netdev_err(netdev,
"No memory left for URBs\n");
763 netdev_err(netdev,
"No memory left for USB buffer\n");
782 for (i = 0; i < cf->
can_dlc; i++)
792 if (dev->
tx_contexts[i].echo_index == MAX_TX_URBS) {
806 netdev_warn(netdev,
"couldn't find free context\n");
815 usb_fill_bulk_urb(urb, dev->
udev, usb_sndbulkpipe(dev->
udev, 2), buf,
816 size, ems_usb_write_bulk_callback, context);
817 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
837 netdev_warn(netdev,
"failed tx_urb %d\n", err);
847 netif_stop_queue(netdev);
866 static int ems_usb_close(
struct net_device *netdev)
868 struct ems_usb *dev = netdev_priv(netdev);
871 unlink_all_urbs(dev);
873 netif_stop_queue(netdev);
877 netdev_warn(netdev,
"couldn't stop device");
887 .ndo_open = ems_usb_open,
888 .ndo_stop = ems_usb_close,
889 .ndo_start_xmit = ems_usb_start_xmit,
906 struct ems_usb *dev = netdev_priv(netdev);
914 netdev_warn(netdev,
"couldn't start device");
916 if (netif_queue_stopped(netdev))
917 netif_wake_queue(netdev);
927 static int ems_usb_set_bittiming(
struct net_device *netdev)
929 struct ems_usb *dev = netdev_priv(netdev);
933 btr0 = ((bt->
brp - 1) & 0x3f) | (((bt->
sjw - 1) & 0x3) << 6);
939 netdev_info(netdev,
"setting BTR0=0x%02x BTR1=0x%02x\n", btr0, btr1);
941 dev->
active_params.msg.can_params.cc_params.sja1000.btr0 = btr0;
942 dev->
active_params.msg.can_params.cc_params.sja1000.btr1 = btr1;
947 static void init_params_sja1000(
struct ems_cpc_msg *msg)
989 dev_err(&intf->dev,
"ems_usb: Couldn't alloc candev\n");
993 dev = netdev_priv(netdev);
995 dev->
udev = interface_to_usbdev(intf);
1000 dev->
can.bittiming_const = &ems_usb_bittiming_const;
1001 dev->
can.do_set_bittiming = ems_usb_set_bittiming;
1002 dev->
can.do_set_mode = ems_usb_set_mode;
1019 dev_err(&intf->dev,
"Couldn't alloc intr URB\n");
1020 goto cleanup_candev;
1025 dev_err(&intf->dev,
"Couldn't alloc Intr buffer\n");
1026 goto cleanup_intr_urb;
1032 dev_err(&intf->dev,
"Couldn't alloc Tx buffer\n");
1033 goto cleanup_intr_in_buffer;
1036 usb_set_intfdata(intf, dev);
1044 netdev_err(netdev,
"couldn't initialize controller: %d\n", err);
1045 goto cleanup_tx_msg_buffer;
1050 netdev_err(netdev,
"couldn't register CAN device: %d\n", err);
1051 goto cleanup_tx_msg_buffer;
1056 cleanup_tx_msg_buffer:
1059 cleanup_intr_in_buffer:
1076 struct ems_usb *dev = usb_get_intfdata(intf);
1078 usb_set_intfdata(intf,
NULL);
1084 unlink_all_urbs(dev);
1093 static struct usb_driver ems_usb_driver = {
1095 .probe = ems_usb_probe,
1096 .disconnect = ems_usb_disconnect,
1097 .id_table = ems_usb_table,