19 #include <linux/netdevice.h>
21 #include <linux/module.h>
32 #define PCAN_USB_EP_CMDOUT 1
33 #define PCAN_USB_EP_CMDIN (PCAN_USB_EP_CMDOUT | USB_DIR_IN)
34 #define PCAN_USB_EP_MSGOUT 2
35 #define PCAN_USB_EP_MSGIN (PCAN_USB_EP_MSGOUT | USB_DIR_IN)
38 #define PCAN_USB_CMD_FUNC 0
39 #define PCAN_USB_CMD_NUM 1
40 #define PCAN_USB_CMD_ARGS 2
41 #define PCAN_USB_CMD_ARGS_LEN 14
42 #define PCAN_USB_CMD_LEN (PCAN_USB_CMD_ARGS + \
43 PCAN_USB_CMD_ARGS_LEN)
46 #define PCAN_USB_COMMAND_TIMEOUT 1000
49 #define PCAN_USB_STARTUP_TIMEOUT 10
52 #define PCAN_USB_RX_BUFFER_SIZE 64
53 #define PCAN_USB_TX_BUFFER_SIZE 64
55 #define PCAN_USB_MSG_HEADER_LEN 2
58 #define PCAN_USB_CRYSTAL_HZ 16000000
61 #define PCAN_USB_STATUSLEN_TIMESTAMP (1 << 7)
62 #define PCAN_USB_STATUSLEN_INTERNAL (1 << 6)
63 #define PCAN_USB_STATUSLEN_EXT_ID (1 << 5)
64 #define PCAN_USB_STATUSLEN_RTR (1 << 4)
65 #define PCAN_USB_STATUSLEN_DLC (0xf)
68 #define PCAN_USB_ERROR_TXFULL 0x01
69 #define PCAN_USB_ERROR_RXQOVR 0x02
70 #define PCAN_USB_ERROR_BUS_LIGHT 0x04
71 #define PCAN_USB_ERROR_BUS_HEAVY 0x08
72 #define PCAN_USB_ERROR_BUS_OFF 0x10
73 #define PCAN_USB_ERROR_RXQEMPTY 0x20
74 #define PCAN_USB_ERROR_QOVR 0x40
75 #define PCAN_USB_ERROR_TXQFULL 0x80
78 #define SJA1000_MODE_NORMAL 0x00
79 #define SJA1000_MODE_INIT 0x01
86 #define PCAN_USB_TS_DIV_SHIFTER 20
87 #define PCAN_USB_TS_US_PER_TICK 44739243
90 #define PCAN_USB_REC_ERROR 1
91 #define PCAN_USB_REC_ANALOG 2
92 #define PCAN_USB_REC_BUSLOAD 3
93 #define PCAN_USB_REC_TS 4
94 #define PCAN_USB_REC_BUSEVT 5
141 "sending cmd f=0x%x n=0x%x failure: %d\n",
159 err = pcan_usb_send_cmd(dev, f, n,
NULL);
169 "waiting rsp f=0x%x n=0x%x failure: %d\n", f, n, err);
183 return pcan_usb_send_cmd(dev, 9, 2, args);
192 return pcan_usb_send_cmd(dev, 3, 2, args);
201 return pcan_usb_send_cmd(dev, 3, 3, args);
210 return pcan_usb_send_cmd(dev, 10, 2, args);
222 btr0 = ((bt->
brp - 1) & 0x3f) | (((bt->
sjw - 1) & 0x3) << 6);
228 netdev_info(dev->
netdev,
"setting BTR0=0x%02x BTR1=0x%02x\n",
234 return pcan_usb_send_cmd(dev, 1, 2, args);
244 err = pcan_usb_set_bus(dev, onoff);
262 static void pcan_usb_restart(
unsigned long arg)
271 static void pcan_usb_restart_pending(
struct urb *
urb)
286 static int pcan_usb_restart_async(
struct peak_usb_device *dev,
struct urb *urb,
299 usb_fill_bulk_urb(urb, dev->
udev,
302 pcan_usb_restart_pending, pdev);
315 err = pcan_usb_wait_rsp(dev, 6, 1, args);
317 netdev_err(dev->
netdev,
"getting serial failure: %d\n", err);
318 }
else if (serial_number) {
336 err = pcan_usb_wait_rsp(dev, 4, 1, args);
338 netdev_err(dev->
netdev,
"getting device id failure: %d\n", err);
340 *device_id = args[0];
352 if ((mc->
ptr+2) > mc->
end)
376 if ((mc->
ptr + 2) > mc->
end)
387 if ((mc->
ptr + 1) > mc->
end)
392 if (ts8 < mc->prev_ts8)
413 if (!mc->
pdev->time_ref.tick_count)
416 new_state = mc->
pdev->dev.can.state;
418 switch (mc->
pdev->dev.can.state) {
442 if ((n & PCAN_USB_ERROR_BUS_LIGHT) == 0) {
450 if (n & PCAN_USB_ERROR_BUS_OFF) {
454 if (n & PCAN_USB_ERROR_BUS_LIGHT) {
467 if ((n & PCAN_USB_ERROR_BUS_HEAVY) == 0) {
480 if (mc->
pdev->dev.can.state == new_state)
498 mc->
pdev->dev.can.can_stats.error_passive++;
505 mc->
pdev->dev.can.can_stats.error_warning++;
512 mc->
netdev->stats.rx_over_errors++;
513 mc->
netdev->stats.rx_errors++;
515 new_state = mc->
pdev->dev.can.state;
525 hwts->
hwtstamp = timeval_to_ktime(tv);
529 mc->
netdev->stats.rx_packets++;
546 if ((mc->
ptr + 2) > mc->
end)
554 int err = pcan_usb_decode_ts(mc, !mc->
rec_idx);
562 err = pcan_usb_decode_error(mc, n, status_len);
579 if (pcan_usb_update_ts(mc))
589 netdev_err(mc->
netdev,
"unexpected function %u\n", f);
593 if ((mc->
ptr + rec_len) > mc->
end)
619 if ((mc->
ptr + 4) > mc->
end)
629 if ((mc->
ptr + 2) > mc->
end)
645 memset(cf->data, 0x0,
sizeof(cf->data));
649 if ((mc->
ptr + rec_len) > mc->
end)
658 hwts = skb_hwtstamps(skb);
659 hwts->
hwtstamp = timeval_to_ktime(tv);
665 mc->
netdev->stats.rx_packets++;
694 err = pcan_usb_decode_status(&mc, sl);
697 err = pcan_usb_decode_data(&mc, sl);
708 static int pcan_usb_decode_buf(
struct peak_usb_device *dev,
struct urb *urb)
713 err = pcan_usb_decode_msg(dev, urb->transfer_buffer,
716 }
else if (urb->actual_length > 0) {
717 netdev_err(dev->
netdev,
"usb message length error (%u)\n",
785 err = pcan_usb_set_silent(dev,
791 return pcan_usb_set_ext_vcc(dev, 0);
810 err = pcan_usb_get_serial(dev, &serial_number);
813 "unable to read %s serial number (err %d)\n",
819 "PEAK-System %s adapter hwrev %u serial %08X (%u channel)\n",
831 struct usb_host_interface *if_desc;
834 if_desc = intf->altsetting;
837 for (i = 0; i < if_desc->desc.bNumEndpoints; i++) {
877 .sizeof_dev_private =
sizeof(
struct pcan_usb),
894 .intf_probe = pcan_usb_probe,
895 .dev_init = pcan_usb_init,
896 .dev_set_bus = pcan_usb_write_mode,
897 .dev_set_bittiming = pcan_usb_set_bittiming,
898 .dev_get_device_id = pcan_usb_get_device_id,
899 .dev_decode_buf = pcan_usb_decode_buf,
900 .dev_encode_msg = pcan_usb_encode_msg,
901 .dev_start = pcan_usb_start,
902 .dev_restart_async = pcan_usb_restart_async,