17 #include <linux/netdevice.h>
19 #include <linux/module.h>
31 #define PCAN_USBPRO_EP_CMDOUT 1
32 #define PCAN_USBPRO_EP_CMDIN (PCAN_USBPRO_EP_CMDOUT | USB_DIR_IN)
33 #define PCAN_USBPRO_EP_MSGOUT_0 2
34 #define PCAN_USBPRO_EP_MSGIN (PCAN_USBPRO_EP_MSGOUT_0 | USB_DIR_IN)
35 #define PCAN_USBPRO_EP_MSGOUT_1 3
36 #define PCAN_USBPRO_EP_UNUSED (PCAN_USBPRO_EP_MSGOUT_1 | USB_DIR_IN)
38 #define PCAN_USBPRO_CHANNEL_COUNT 2
41 #define PCAN_USBPRO_CRYSTAL_HZ 56000000
44 #define PCAN_USBPRO_COMMAND_TIMEOUT 1000
47 #define PCAN_USBPRO_RX_BUFFER_SIZE 1024
48 #define PCAN_USBPRO_TX_BUFFER_SIZE 64
50 #define PCAN_USBPRO_MSG_HEADER_LEN 4
53 #define PCAN_USBPRO_RSP_SUBMIT_MAX 2
55 #define PCAN_USBPRO_RTR 0x01
56 #define PCAN_USBPRO_EXT 0x02
58 #define PCAN_USBPRO_CMD_BUFFER_SIZE 512
88 static u16 pcan_usb_pro_sizeof_rec[256] = {
124 void *buffer_addr,
int buffer_size)
126 u8 *
pr = pcan_msg_init(pm, buffer_addr, buffer_size);
150 case PCAN_USBPRO_TXMSG8:
152 case PCAN_USBPRO_TXMSG4:
154 case PCAN_USBPRO_TXMSG0:
165 case PCAN_USBPRO_GETDEVID:
172 case PCAN_USBPRO_SETFILTR:
173 case PCAN_USBPRO_SETBUSACT:
174 case PCAN_USBPRO_SETSILENT:
180 case PCAN_USBPRO_SETLED:
188 case PCAN_USBPRO_SETTS:
195 pr_err(
"%s: %s(): unknown data type %02Xh (%d)\n",
233 netdev_err(dev->
netdev,
"sending command failure: %d\n", err);
244 u8 req_data_type, req_channel;
268 netdev_err(dev->
netdev,
"waiting rsp error %d\n", err);
272 if (actual_length == 0)
278 "got abnormal too small rsp (len=%d)\n",
289 for (r = 0; r < rec_cnt; r++) {
291 rec_len = pcan_usb_pro_sizeof_rec[pr->
data_type];
294 "got unprocessed record in msg\n");
303 "got unwanted rsp %xh: ignored\n",
307 else if ((req_channel != 0xff) && \
308 (pr->
bus_act.channel != req_channel))
310 "got rsp %xh but on chan%u: ignored\n",
311 req_data_type, pr->
bus_act.channel);
322 return (i >= PCAN_USBPRO_RSP_SUBMIT_MAX) ? -
ERANGE :
err;
326 int req_value,
void *req_addr,
int req_size)
336 memset(req_addr,
'\0', req_size);
342 p = usb_sndctrlpipe(dev->
udev, 0);
346 p = usb_rcvctrlpipe(dev->
udev, 0);
352 req_addr, req_size, 2 * USB_CTRL_GET_TIMEOUT);
355 "unable to request usb[type=%d value=%d] err=%d\n",
356 req_id, req_value, err);
368 pcan_msg_add_rec(&um, PCAN_USBPRO_SETTS, onoff);
370 return pcan_usb_pro_send_cmd(dev, &um);
385 return pcan_usb_pro_send_cmd(dev, &um);
401 pcan_msg_add_rec(&um, PCAN_USBPRO_SETBUSACT, dev->
ctrl_idx, onoff);
403 return pcan_usb_pro_send_cmd(dev, &um);
411 pcan_msg_add_rec(&um, PCAN_USBPRO_SETSILENT, dev->
ctrl_idx, onoff);
413 return pcan_usb_pro_send_cmd(dev, &um);
421 pcan_msg_add_rec(&um, PCAN_USBPRO_SETFILTR, dev->
ctrl_idx, filter_mode);
423 return pcan_usb_pro_send_cmd(dev, &um);
432 pcan_msg_add_rec(&um, PCAN_USBPRO_SETLED, dev->
ctrl_idx, mode, timeout);
434 return pcan_usb_pro_send_cmd(dev, &um);
446 pcan_msg_add_rec(&um, PCAN_USBPRO_GETDEVID, dev->
ctrl_idx);
448 err = pcan_usb_pro_send_cmd(dev, &um);
452 err = pcan_usb_pro_wait_rsp(dev, &um);
469 ccbt |= (bt->
sjw - 1) << 24;
474 netdev_info(dev->
netdev,
"setting ccbt=0x%08x\n", ccbt);
476 return pcan_usb_pro_set_bitrate(dev, ccbt);
479 static void pcan_usb_pro_restart_complete(
struct urb *
urb)
497 pcan_msg_add_rec(&um, PCAN_USBPRO_SETBUSACT, dev->
ctrl_idx, 1);
499 usb_fill_bulk_urb(urb, dev->
udev,
502 pcan_usb_pro_restart_complete, dev);
507 static void pcan_usb_pro_drv_loaded(
struct peak_usb_device *dev,
int loaded)
512 buffer[1] = !!loaded;
529 const unsigned int ctrl_idx = (rx->
len >> 4) & 0x0f;
553 hwts = skb_hwtstamps(skb);
554 hwts->
hwtstamp = timeval_to_ktime(tv);
557 netdev->
stats.rx_packets++;
567 const unsigned int ctrl_idx = (er->
channel >> 4) & 0x0f;
570 struct can_frame *can_frame;
599 if (rx_err_cnt > 127)
601 else if (rx_err_cnt > 96)
604 if (tx_err_cnt > 127)
606 else if (tx_err_cnt > 96)
618 if (dev->
can.state == new_state)
634 can_frame->data[1] |= err_mask;
635 dev->
can.can_stats.error_passive++;
640 can_frame->data[1] |= err_mask;
641 dev->
can.can_stats.error_warning++;
652 netdev->
stats.rx_over_errors++;
653 netdev->
stats.rx_errors++;
659 netdev->
stats.rx_over_errors++;
660 netdev->
stats.rx_errors++;
670 hwts = skb_hwtstamps(skb);
671 hwts->
hwtstamp = timeval_to_ktime(tv);
673 netdev->
stats.rx_packets++;
693 static int pcan_usb_pro_decode_buf(
struct peak_usb_device *dev,
struct urb *urb)
702 rec_ptr = pcan_msg_init(&usb_msg, urb->transfer_buffer,
705 netdev_err(netdev,
"bad msg hdr len %d\n", urb->actual_length);
710 msg_end = urb->transfer_buffer + urb->actual_length;
712 for (; rec_cnt > 0; rec_cnt--) {
714 u16 sizeof_rec = pcan_usb_pro_sizeof_rec[pr->
data_type];
718 "got unsupported rec in usb msg:\n");
724 if (rec_ptr + sizeof_rec > msg_end) {
726 "got frag rec: should inc usb rx buf size\n");
732 case PCAN_USBPRO_RXMSG8:
733 case PCAN_USBPRO_RXMSG4:
734 case PCAN_USBPRO_RXMSG0:
735 case PCAN_USBPRO_RXRTR:
736 err = pcan_usb_pro_handle_canmsg(usb_if, &pr->
rx_msg);
741 case PCAN_USBPRO_RXSTATUS:
742 err = pcan_usb_pro_handle_error(usb_if, &pr->
rx_status);
747 case PCAN_USBPRO_RXTS:
748 pcan_usb_pro_handle_ts(usb_if, &pr->
rx_ts);
753 "unhandled rec type 0x%02x (%d): ignored\n",
758 rec_ptr += sizeof_rec;
764 urb->transfer_buffer, urb->actual_length);
772 struct can_frame *
cf = (
struct can_frame *)skb->
data;
776 pcan_msg_init_empty(&usb_msg, obuf, *size);
793 pcan_msg_add_rec(&usb_msg, data_type, 0,
flags, len, cf->
can_id,
796 *size = usb_msg.rec_buffer_len;
807 err = pcan_usb_pro_set_silent(dev,
813 err = pcan_usb_pro_set_filter(dev, 1);
818 if (pdev->
usb_if->dev_opened_count == 0) {
823 err = pcan_usb_pro_set_ts(dev, 1);
826 pdev->
usb_if->dev_opened_count++;
841 if (pdev->
usb_if->dev_opened_count == 1)
842 pcan_usb_pro_set_ts(dev, 0);
844 pdev->
usb_if->dev_opened_count--;
884 "unable to read %s firmware info (err %d)\n",
895 "unable to read %s bootloader info (err %d)\n",
901 "PEAK-System %s hwrev %u serial %08X.%08X (%u channels)\n",
903 bi.hw_rev,
bi.serial_num_hi,
bi.serial_num_lo,
907 pcan_usb_pro_drv_loaded(dev, 1);
916 pcan_usb_pro_set_led(dev, 0, 1);
932 pcan_usb_pro_set_bus(dev, 0);
938 if (pdev->
usb_if->dev_opened_count > 0)
939 pcan_usb_pro_set_ts(dev, 0);
942 pcan_usb_pro_drv_loaded(dev, 0);
953 kfree(pcan_usb_pro_dev_if(dev));
961 struct usb_host_interface *if_desc;
964 if_desc = intf->altsetting;
967 for (i = 0; i < if_desc->desc.bNumEndpoints; i++) {
994 .name =
"PCAN-USB Pro",
1000 .bittiming_const = {
1001 .name =
"pcan_usb_pro",
1017 .ts_period = 1000000,
1018 .us_per_ts_scale = 1,
1019 .us_per_ts_shift = 0,
1030 .intf_probe = pcan_usb_pro_probe,
1031 .dev_init = pcan_usb_pro_init,
1032 .dev_exit = pcan_usb_pro_exit,
1033 .dev_free = pcan_usb_pro_free,
1034 .dev_set_bus = pcan_usb_pro_set_bus,
1035 .dev_set_bittiming = pcan_usb_pro_set_bittiming,
1036 .dev_get_device_id = pcan_usb_pro_get_device_id,
1037 .dev_decode_buf = pcan_usb_pro_decode_buf,
1038 .dev_encode_msg = pcan_usb_pro_encode_msg,
1039 .dev_start = pcan_usb_pro_start,
1040 .dev_stop = pcan_usb_pro_stop,
1041 .dev_restart_async = pcan_usb_pro_restart_async,