2 * This program is free software; you can redistribute it and/or
3 * modify it under the terms of the GNU General Public License as
4 * published by the Free Software Foundation version 2.
6 * Parts of this driver are based on the following:
7 * - Kvaser linux leaf driver (version 4.78)
8 * - CAN driver for esd CAN-USB/2
10 * Copyright (C) 2002-2006 KVASER AB, Sweden. All rights reserved.
11 * Copyright (C) 2010 Matthias Fuchs <matthias.fuchs@esd.eu>, esd gmbh
12 * Copyright (C) 2012 Olivier Sobrie <olivier@sobrie.be>
15 #include <linux/init.h>
16 #include <linux/completion.h>
17 #include <linux/module.h>
18 #include <linux/netdevice.h>
19 #include <linux/usb.h>
21 #include <linux/can.h>
22 #include <linux/can/dev.h>
23 #include <linux/can/error.h>
25 #define MAX_TX_URBS 16
27 #define START_TIMEOUT 1000 /* msecs */
28 #define STOP_TIMEOUT 1000 /* msecs */
29 #define USB_SEND_TIMEOUT 1000 /* msecs */
30 #define USB_RECV_TIMEOUT 1000 /* msecs */
31 #define RX_BUFFER_SIZE 3072
32 #define CAN_USB_CLOCK 8000000
33 #define MAX_NET_DEVICES 3
35 /* Kvaser USB devices */
36 #define KVASER_VENDOR_ID 0x0bfd
37 #define USB_LEAF_DEVEL_PRODUCT_ID 10
38 #define USB_LEAF_LITE_PRODUCT_ID 11
39 #define USB_LEAF_PRO_PRODUCT_ID 12
40 #define USB_LEAF_SPRO_PRODUCT_ID 14
41 #define USB_LEAF_PRO_LS_PRODUCT_ID 15
42 #define USB_LEAF_PRO_SWC_PRODUCT_ID 16
43 #define USB_LEAF_PRO_LIN_PRODUCT_ID 17
44 #define USB_LEAF_SPRO_LS_PRODUCT_ID 18
45 #define USB_LEAF_SPRO_SWC_PRODUCT_ID 19
46 #define USB_MEMO2_DEVEL_PRODUCT_ID 22
47 #define USB_MEMO2_HSHS_PRODUCT_ID 23
48 #define USB_UPRO_HSHS_PRODUCT_ID 24
49 #define USB_LEAF_LITE_GI_PRODUCT_ID 25
50 #define USB_LEAF_PRO_OBDII_PRODUCT_ID 26
51 #define USB_MEMO2_HSLS_PRODUCT_ID 27
52 #define USB_LEAF_LITE_CH_PRODUCT_ID 28
53 #define USB_BLACKBIRD_SPRO_PRODUCT_ID 29
54 #define USB_OEM_MERCURY_PRODUCT_ID 34
55 #define USB_OEM_LEAF_PRODUCT_ID 35
56 #define USB_CAN_R_PRODUCT_ID 39
58 /* USB devices features */
59 #define KVASER_HAS_SILENT_MODE BIT(0)
60 #define KVASER_HAS_TXRX_ERRORS BIT(1)
62 /* Message header size */
63 #define MSG_HEADER_LEN 2
65 /* Can message flags */
66 #define MSG_FLAG_ERROR_FRAME BIT(0)
67 #define MSG_FLAG_OVERRUN BIT(1)
68 #define MSG_FLAG_NERR BIT(2)
69 #define MSG_FLAG_WAKEUP BIT(3)
70 #define MSG_FLAG_REMOTE_FRAME BIT(4)
71 #define MSG_FLAG_RESERVED BIT(5)
72 #define MSG_FLAG_TX_ACK BIT(6)
73 #define MSG_FLAG_TX_REQUEST BIT(7)
76 #define M16C_STATE_BUS_RESET BIT(0)
77 #define M16C_STATE_BUS_ERROR BIT(4)
78 #define M16C_STATE_BUS_PASSIVE BIT(5)
79 #define M16C_STATE_BUS_OFF BIT(6)
82 #define CMD_RX_STD_MESSAGE 12
83 #define CMD_TX_STD_MESSAGE 13
84 #define CMD_RX_EXT_MESSAGE 14
85 #define CMD_TX_EXT_MESSAGE 15
86 #define CMD_SET_BUS_PARAMS 16
87 #define CMD_GET_BUS_PARAMS 17
88 #define CMD_GET_BUS_PARAMS_REPLY 18
89 #define CMD_GET_CHIP_STATE 19
90 #define CMD_CHIP_STATE_EVENT 20
91 #define CMD_SET_CTRL_MODE 21
92 #define CMD_GET_CTRL_MODE 22
93 #define CMD_GET_CTRL_MODE_REPLY 23
94 #define CMD_RESET_CHIP 24
95 #define CMD_RESET_CARD 25
96 #define CMD_START_CHIP 26
97 #define CMD_START_CHIP_REPLY 27
98 #define CMD_STOP_CHIP 28
99 #define CMD_STOP_CHIP_REPLY 29
100 #define CMD_GET_CARD_INFO2 32
101 #define CMD_GET_CARD_INFO 34
102 #define CMD_GET_CARD_INFO_REPLY 35
103 #define CMD_GET_SOFTWARE_INFO 38
104 #define CMD_GET_SOFTWARE_INFO_REPLY 39
105 #define CMD_ERROR_EVENT 45
106 #define CMD_FLUSH_QUEUE 48
107 #define CMD_RESET_ERROR_COUNTER 49
108 #define CMD_TX_ACKNOWLEDGE 50
109 #define CMD_CAN_ERROR_EVENT 51
110 #define CMD_USB_THROTTLE 77
111 #define CMD_LOG_MESSAGE 106
114 #define M16C_EF_ACKE BIT(0)
115 #define M16C_EF_CRCE BIT(1)
116 #define M16C_EF_FORME BIT(2)
117 #define M16C_EF_STFE BIT(3)
118 #define M16C_EF_BITE0 BIT(4)
119 #define M16C_EF_BITE1 BIT(5)
120 #define M16C_EF_RCVE BIT(6)
121 #define M16C_EF_TRE BIT(7)
123 /* bittiming parameters */
124 #define KVASER_USB_TSEG1_MIN 1
125 #define KVASER_USB_TSEG1_MAX 16
126 #define KVASER_USB_TSEG2_MIN 1
127 #define KVASER_USB_TSEG2_MAX 8
128 #define KVASER_USB_SJW_MAX 4
129 #define KVASER_USB_BRP_MIN 1
130 #define KVASER_USB_BRP_MAX 64
131 #define KVASER_USB_BRP_INC 1
134 #define KVASER_CTRL_MODE_NORMAL 1
135 #define KVASER_CTRL_MODE_SILENT 2
136 #define KVASER_CTRL_MODE_SELFRECEPTION 3
137 #define KVASER_CTRL_MODE_OFF 4
139 struct kvaser_msg_simple {
144 struct kvaser_msg_cardinfo {
147 __le32 serial_number;
149 __le32 clock_resolution;
157 struct kvaser_msg_cardinfo2 {
161 __le32 oem_unlock_code;
164 struct kvaser_msg_softinfo {
169 __le16 max_outstanding_tx;
173 struct kvaser_msg_busparams {
183 struct kvaser_msg_tx_can {
191 struct kvaser_msg_rx_can {
198 struct kvaser_msg_chip_state_event {
208 struct kvaser_msg_tx_acknowledge {
216 struct kvaser_msg_error_event {
228 struct kvaser_msg_ctrl_mode {
235 struct kvaser_msg_flush_queue {
242 struct kvaser_msg_log_message {
256 struct kvaser_msg_simple simple;
257 struct kvaser_msg_cardinfo cardinfo;
258 struct kvaser_msg_cardinfo2 cardinfo2;
259 struct kvaser_msg_softinfo softinfo;
260 struct kvaser_msg_busparams busparams;
261 struct kvaser_msg_tx_can tx_can;
262 struct kvaser_msg_rx_can rx_can;
263 struct kvaser_msg_chip_state_event chip_state_event;
264 struct kvaser_msg_tx_acknowledge tx_acknowledge;
265 struct kvaser_msg_error_event error_event;
266 struct kvaser_msg_ctrl_mode ctrl_mode;
267 struct kvaser_msg_flush_queue flush_queue;
268 struct kvaser_msg_log_message log_message;
272 struct kvaser_usb_tx_urb_context {
273 struct kvaser_usb_net_priv *priv;
279 struct usb_device *udev;
280 struct kvaser_usb_net_priv *nets[MAX_NET_DEVICES];
282 struct usb_endpoint_descriptor *bulk_in, *bulk_out;
283 struct usb_anchor rx_submitted;
286 unsigned int nchannels;
289 void *rxbuf[MAX_RX_URBS];
290 dma_addr_t rxbuf_dma[MAX_RX_URBS];
293 struct kvaser_usb_net_priv {
296 atomic_t active_tx_urbs;
297 struct usb_anchor tx_submitted;
298 struct kvaser_usb_tx_urb_context tx_contexts[MAX_TX_URBS];
300 struct completion start_comp, stop_comp;
302 struct kvaser_usb *dev;
303 struct net_device *netdev;
306 struct can_berr_counter bec;
309 static const struct usb_device_id kvaser_usb_table[] = {
310 { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_DEVEL_PRODUCT_ID) },
311 { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_LITE_PRODUCT_ID) },
312 { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_PRO_PRODUCT_ID),
313 .driver_info = KVASER_HAS_TXRX_ERRORS |
314 KVASER_HAS_SILENT_MODE },
315 { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_SPRO_PRODUCT_ID),
316 .driver_info = KVASER_HAS_TXRX_ERRORS |
317 KVASER_HAS_SILENT_MODE },
318 { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_PRO_LS_PRODUCT_ID),
319 .driver_info = KVASER_HAS_TXRX_ERRORS |
320 KVASER_HAS_SILENT_MODE },
321 { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_PRO_SWC_PRODUCT_ID),
322 .driver_info = KVASER_HAS_TXRX_ERRORS |
323 KVASER_HAS_SILENT_MODE },
324 { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_PRO_LIN_PRODUCT_ID),
325 .driver_info = KVASER_HAS_TXRX_ERRORS |
326 KVASER_HAS_SILENT_MODE },
327 { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_SPRO_LS_PRODUCT_ID),
328 .driver_info = KVASER_HAS_TXRX_ERRORS |
329 KVASER_HAS_SILENT_MODE },
330 { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_SPRO_SWC_PRODUCT_ID),
331 .driver_info = KVASER_HAS_TXRX_ERRORS |
332 KVASER_HAS_SILENT_MODE },
333 { USB_DEVICE(KVASER_VENDOR_ID, USB_MEMO2_DEVEL_PRODUCT_ID),
334 .driver_info = KVASER_HAS_TXRX_ERRORS |
335 KVASER_HAS_SILENT_MODE },
336 { USB_DEVICE(KVASER_VENDOR_ID, USB_MEMO2_HSHS_PRODUCT_ID),
337 .driver_info = KVASER_HAS_TXRX_ERRORS |
338 KVASER_HAS_SILENT_MODE },
339 { USB_DEVICE(KVASER_VENDOR_ID, USB_UPRO_HSHS_PRODUCT_ID),
340 .driver_info = KVASER_HAS_TXRX_ERRORS },
341 { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_LITE_GI_PRODUCT_ID) },
342 { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_PRO_OBDII_PRODUCT_ID),
343 .driver_info = KVASER_HAS_TXRX_ERRORS |
344 KVASER_HAS_SILENT_MODE },
345 { USB_DEVICE(KVASER_VENDOR_ID, USB_MEMO2_HSLS_PRODUCT_ID),
346 .driver_info = KVASER_HAS_TXRX_ERRORS },
347 { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_LITE_CH_PRODUCT_ID),
348 .driver_info = KVASER_HAS_TXRX_ERRORS },
349 { USB_DEVICE(KVASER_VENDOR_ID, USB_BLACKBIRD_SPRO_PRODUCT_ID),
350 .driver_info = KVASER_HAS_TXRX_ERRORS },
351 { USB_DEVICE(KVASER_VENDOR_ID, USB_OEM_MERCURY_PRODUCT_ID),
352 .driver_info = KVASER_HAS_TXRX_ERRORS },
353 { USB_DEVICE(KVASER_VENDOR_ID, USB_OEM_LEAF_PRODUCT_ID),
354 .driver_info = KVASER_HAS_TXRX_ERRORS },
355 { USB_DEVICE(KVASER_VENDOR_ID, USB_CAN_R_PRODUCT_ID),
356 .driver_info = KVASER_HAS_TXRX_ERRORS },
359 MODULE_DEVICE_TABLE(usb, kvaser_usb_table);
361 static inline int kvaser_usb_send_msg(const struct kvaser_usb *dev,
362 struct kvaser_msg *msg)
366 return usb_bulk_msg(dev->udev,
367 usb_sndbulkpipe(dev->udev,
368 dev->bulk_out->bEndpointAddress),
369 msg, msg->len, &actual_len,
373 static int kvaser_usb_wait_msg(const struct kvaser_usb *dev, u8 id,
374 struct kvaser_msg *msg)
376 struct kvaser_msg *tmp;
382 buf = kzalloc(RX_BUFFER_SIZE, GFP_KERNEL);
386 err = usb_bulk_msg(dev->udev,
387 usb_rcvbulkpipe(dev->udev,
388 dev->bulk_in->bEndpointAddress),
389 buf, RX_BUFFER_SIZE, &actual_len,
394 while (pos <= actual_len - MSG_HEADER_LEN) {
400 if (pos + tmp->len > actual_len) {
401 dev_err(dev->udev->dev.parent, "Format error\n");
406 memcpy(msg, tmp, tmp->len);
421 static int kvaser_usb_send_simple_msg(const struct kvaser_usb *dev,
422 u8 msg_id, int channel)
424 struct kvaser_msg *msg;
427 msg = kmalloc(sizeof(*msg), GFP_KERNEL);
432 msg->len = MSG_HEADER_LEN + sizeof(struct kvaser_msg_simple);
433 msg->u.simple.channel = channel;
434 msg->u.simple.tid = 0xff;
436 rc = kvaser_usb_send_msg(dev, msg);
442 static int kvaser_usb_get_software_info(struct kvaser_usb *dev)
444 struct kvaser_msg msg;
447 err = kvaser_usb_send_simple_msg(dev, CMD_GET_SOFTWARE_INFO, 0);
451 err = kvaser_usb_wait_msg(dev, CMD_GET_SOFTWARE_INFO_REPLY, &msg);
455 dev->fw_version = le32_to_cpu(msg.u.softinfo.fw_version);
460 static int kvaser_usb_get_card_info(struct kvaser_usb *dev)
462 struct kvaser_msg msg;
465 err = kvaser_usb_send_simple_msg(dev, CMD_GET_CARD_INFO, 0);
469 err = kvaser_usb_wait_msg(dev, CMD_GET_CARD_INFO_REPLY, &msg);
473 dev->nchannels = msg.u.cardinfo.nchannels;
478 static void kvaser_usb_tx_acknowledge(const struct kvaser_usb *dev,
479 const struct kvaser_msg *msg)
481 struct net_device_stats *stats;
482 struct kvaser_usb_tx_urb_context *context;
483 struct kvaser_usb_net_priv *priv;
485 struct can_frame *cf;
486 u8 channel = msg->u.tx_acknowledge.channel;
487 u8 tid = msg->u.tx_acknowledge.tid;
489 if (channel >= dev->nchannels) {
490 dev_err(dev->udev->dev.parent,
491 "Invalid channel number (%d)\n", channel);
495 priv = dev->nets[channel];
497 if (!netif_device_present(priv->netdev))
500 stats = &priv->netdev->stats;
502 context = &priv->tx_contexts[tid % MAX_TX_URBS];
504 /* Sometimes the state change doesn't come after a bus-off event */
505 if (priv->can.restart_ms &&
506 (priv->can.state >= CAN_STATE_BUS_OFF)) {
507 skb = alloc_can_err_skb(priv->netdev, &cf);
509 cf->can_id |= CAN_ERR_RESTARTED;
513 stats->rx_bytes += cf->can_dlc;
515 netdev_err(priv->netdev,
516 "No memory left for err_skb\n");
519 priv->can.can_stats.restarts++;
520 netif_carrier_on(priv->netdev);
522 priv->can.state = CAN_STATE_ERROR_ACTIVE;
526 stats->tx_bytes += context->dlc;
527 can_get_echo_skb(priv->netdev, context->echo_index);
529 context->echo_index = MAX_TX_URBS;
530 atomic_dec(&priv->active_tx_urbs);
532 netif_wake_queue(priv->netdev);
535 static void kvaser_usb_simple_msg_callback(struct urb *urb)
537 struct net_device *netdev = urb->context;
539 kfree(urb->transfer_buffer);
542 netdev_warn(netdev, "urb status received: %d\n",
546 static int kvaser_usb_simple_msg_async(struct kvaser_usb_net_priv *priv,
549 struct kvaser_usb *dev = priv->dev;
550 struct net_device *netdev = priv->netdev;
551 struct kvaser_msg *msg;
556 urb = usb_alloc_urb(0, GFP_ATOMIC);
558 netdev_err(netdev, "No memory left for URBs\n");
562 buf = kmalloc(sizeof(struct kvaser_msg), GFP_ATOMIC);
568 msg = (struct kvaser_msg *)buf;
569 msg->len = MSG_HEADER_LEN + sizeof(struct kvaser_msg_simple);
571 msg->u.simple.channel = priv->channel;
573 usb_fill_bulk_urb(urb, dev->udev,
574 usb_sndbulkpipe(dev->udev,
575 dev->bulk_out->bEndpointAddress),
577 kvaser_usb_simple_msg_callback, priv);
578 usb_anchor_urb(urb, &priv->tx_submitted);
580 err = usb_submit_urb(urb, GFP_ATOMIC);
582 netdev_err(netdev, "Error transmitting URB\n");
583 usb_unanchor_urb(urb);
594 static void kvaser_usb_unlink_tx_urbs(struct kvaser_usb_net_priv *priv)
598 usb_kill_anchored_urbs(&priv->tx_submitted);
599 atomic_set(&priv->active_tx_urbs, 0);
601 for (i = 0; i < MAX_TX_URBS; i++)
602 priv->tx_contexts[i].echo_index = MAX_TX_URBS;
605 static void kvaser_usb_rx_error(const struct kvaser_usb *dev,
606 const struct kvaser_msg *msg)
608 struct can_frame *cf;
610 struct net_device_stats *stats;
611 struct kvaser_usb_net_priv *priv;
612 unsigned int new_state;
613 u8 channel, status, txerr, rxerr, error_factor;
616 case CMD_CAN_ERROR_EVENT:
617 channel = msg->u.error_event.channel;
618 status = msg->u.error_event.status;
619 txerr = msg->u.error_event.tx_errors_count;
620 rxerr = msg->u.error_event.rx_errors_count;
621 error_factor = msg->u.error_event.error_factor;
623 case CMD_LOG_MESSAGE:
624 channel = msg->u.log_message.channel;
625 status = msg->u.log_message.data[0];
626 txerr = msg->u.log_message.data[2];
627 rxerr = msg->u.log_message.data[3];
628 error_factor = msg->u.log_message.data[1];
630 case CMD_CHIP_STATE_EVENT:
631 channel = msg->u.chip_state_event.channel;
632 status = msg->u.chip_state_event.status;
633 txerr = msg->u.chip_state_event.tx_errors_count;
634 rxerr = msg->u.chip_state_event.rx_errors_count;
638 dev_err(dev->udev->dev.parent, "Invalid msg id (%d)\n",
643 if (channel >= dev->nchannels) {
644 dev_err(dev->udev->dev.parent,
645 "Invalid channel number (%d)\n", channel);
649 priv = dev->nets[channel];
650 stats = &priv->netdev->stats;
652 if (status & M16C_STATE_BUS_RESET) {
653 kvaser_usb_unlink_tx_urbs(priv);
657 skb = alloc_can_err_skb(priv->netdev, &cf);
663 new_state = priv->can.state;
665 netdev_dbg(priv->netdev, "Error status: 0x%02x\n", status);
667 if (status & M16C_STATE_BUS_OFF) {
668 cf->can_id |= CAN_ERR_BUSOFF;
670 priv->can.can_stats.bus_off++;
671 if (!priv->can.restart_ms)
672 kvaser_usb_simple_msg_async(priv, CMD_STOP_CHIP);
674 netif_carrier_off(priv->netdev);
676 new_state = CAN_STATE_BUS_OFF;
677 } else if (status & M16C_STATE_BUS_PASSIVE) {
678 if (priv->can.state != CAN_STATE_ERROR_PASSIVE) {
679 cf->can_id |= CAN_ERR_CRTL;
682 cf->data[1] = (txerr > rxerr)
683 ? CAN_ERR_CRTL_TX_PASSIVE
684 : CAN_ERR_CRTL_RX_PASSIVE;
686 cf->data[1] = CAN_ERR_CRTL_TX_PASSIVE |
687 CAN_ERR_CRTL_RX_PASSIVE;
689 priv->can.can_stats.error_passive++;
692 new_state = CAN_STATE_ERROR_PASSIVE;
695 if (status == M16C_STATE_BUS_ERROR) {
696 if ((priv->can.state < CAN_STATE_ERROR_WARNING) &&
697 ((txerr >= 96) || (rxerr >= 96))) {
698 cf->can_id |= CAN_ERR_CRTL;
699 cf->data[1] = (txerr > rxerr)
700 ? CAN_ERR_CRTL_TX_WARNING
701 : CAN_ERR_CRTL_RX_WARNING;
703 priv->can.can_stats.error_warning++;
704 new_state = CAN_STATE_ERROR_WARNING;
705 } else if (priv->can.state > CAN_STATE_ERROR_ACTIVE) {
706 cf->can_id |= CAN_ERR_PROT;
707 cf->data[2] = CAN_ERR_PROT_ACTIVE;
709 new_state = CAN_STATE_ERROR_ACTIVE;
714 cf->can_id |= CAN_ERR_PROT;
715 cf->data[2] = CAN_ERR_PROT_ACTIVE;
717 new_state = CAN_STATE_ERROR_ACTIVE;
720 if (priv->can.restart_ms &&
721 (priv->can.state >= CAN_STATE_BUS_OFF) &&
722 (new_state < CAN_STATE_BUS_OFF)) {
723 cf->can_id |= CAN_ERR_RESTARTED;
724 netif_carrier_on(priv->netdev);
726 priv->can.can_stats.restarts++;
730 priv->can.can_stats.bus_error++;
733 cf->can_id |= CAN_ERR_BUSERROR | CAN_ERR_PROT;
735 if (error_factor & M16C_EF_ACKE)
736 cf->data[3] |= (CAN_ERR_PROT_LOC_ACK);
737 if (error_factor & M16C_EF_CRCE)
738 cf->data[3] |= (CAN_ERR_PROT_LOC_CRC_SEQ |
739 CAN_ERR_PROT_LOC_CRC_DEL);
740 if (error_factor & M16C_EF_FORME)
741 cf->data[2] |= CAN_ERR_PROT_FORM;
742 if (error_factor & M16C_EF_STFE)
743 cf->data[2] |= CAN_ERR_PROT_STUFF;
744 if (error_factor & M16C_EF_BITE0)
745 cf->data[2] |= CAN_ERR_PROT_BIT0;
746 if (error_factor & M16C_EF_BITE1)
747 cf->data[2] |= CAN_ERR_PROT_BIT1;
748 if (error_factor & M16C_EF_TRE)
749 cf->data[2] |= CAN_ERR_PROT_TX;
755 priv->bec.txerr = txerr;
756 priv->bec.rxerr = rxerr;
758 priv->can.state = new_state;
763 stats->rx_bytes += cf->can_dlc;
766 static void kvaser_usb_rx_can_err(const struct kvaser_usb_net_priv *priv,
767 const struct kvaser_msg *msg)
769 struct can_frame *cf;
771 struct net_device_stats *stats = &priv->netdev->stats;
773 if (msg->u.rx_can.flag & (MSG_FLAG_ERROR_FRAME |
775 netdev_err(priv->netdev, "Unknow error (flags: 0x%02x)\n",
782 if (msg->u.rx_can.flag & MSG_FLAG_OVERRUN) {
783 skb = alloc_can_err_skb(priv->netdev, &cf);
789 cf->can_id |= CAN_ERR_CRTL;
790 cf->data[1] = CAN_ERR_CRTL_RX_OVERFLOW;
792 stats->rx_over_errors++;
798 stats->rx_bytes += cf->can_dlc;
802 static void kvaser_usb_rx_can_msg(const struct kvaser_usb *dev,
803 const struct kvaser_msg *msg)
805 struct kvaser_usb_net_priv *priv;
806 struct can_frame *cf;
808 struct net_device_stats *stats;
809 u8 channel = msg->u.rx_can.channel;
811 if (channel >= dev->nchannels) {
812 dev_err(dev->udev->dev.parent,
813 "Invalid channel number (%d)\n", channel);
817 priv = dev->nets[channel];
818 stats = &priv->netdev->stats;
820 if (msg->u.rx_can.flag & (MSG_FLAG_ERROR_FRAME | MSG_FLAG_NERR |
822 kvaser_usb_rx_can_err(priv, msg);
824 } else if (msg->u.rx_can.flag & ~MSG_FLAG_REMOTE_FRAME) {
825 netdev_warn(priv->netdev,
826 "Unhandled frame (flags: 0x%02x)",
831 skb = alloc_can_skb(priv->netdev, &cf);
837 cf->can_id = ((msg->u.rx_can.msg[0] & 0x1f) << 6) |
838 (msg->u.rx_can.msg[1] & 0x3f);
839 cf->can_dlc = get_can_dlc(msg->u.rx_can.msg[5]);
841 if (msg->id == CMD_RX_EXT_MESSAGE) {
843 cf->can_id |= ((msg->u.rx_can.msg[2] & 0x0f) << 14) |
844 ((msg->u.rx_can.msg[3] & 0xff) << 6) |
845 (msg->u.rx_can.msg[4] & 0x3f);
846 cf->can_id |= CAN_EFF_FLAG;
849 if (msg->u.rx_can.flag & MSG_FLAG_REMOTE_FRAME)
850 cf->can_id |= CAN_RTR_FLAG;
852 memcpy(cf->data, &msg->u.rx_can.msg[6], cf->can_dlc);
857 stats->rx_bytes += cf->can_dlc;
860 static void kvaser_usb_start_chip_reply(const struct kvaser_usb *dev,
861 const struct kvaser_msg *msg)
863 struct kvaser_usb_net_priv *priv;
864 u8 channel = msg->u.simple.channel;
866 if (channel >= dev->nchannels) {
867 dev_err(dev->udev->dev.parent,
868 "Invalid channel number (%d)\n", channel);
872 priv = dev->nets[channel];
874 if (completion_done(&priv->start_comp) &&
875 netif_queue_stopped(priv->netdev)) {
876 netif_wake_queue(priv->netdev);
878 netif_start_queue(priv->netdev);
879 complete(&priv->start_comp);
883 static void kvaser_usb_stop_chip_reply(const struct kvaser_usb *dev,
884 const struct kvaser_msg *msg)
886 struct kvaser_usb_net_priv *priv;
887 u8 channel = msg->u.simple.channel;
889 if (channel >= dev->nchannels) {
890 dev_err(dev->udev->dev.parent,
891 "Invalid channel number (%d)\n", channel);
895 priv = dev->nets[channel];
897 complete(&priv->stop_comp);
900 static void kvaser_usb_handle_message(const struct kvaser_usb *dev,
901 const struct kvaser_msg *msg)
904 case CMD_START_CHIP_REPLY:
905 kvaser_usb_start_chip_reply(dev, msg);
908 case CMD_STOP_CHIP_REPLY:
909 kvaser_usb_stop_chip_reply(dev, msg);
912 case CMD_RX_STD_MESSAGE:
913 case CMD_RX_EXT_MESSAGE:
914 kvaser_usb_rx_can_msg(dev, msg);
917 case CMD_CHIP_STATE_EVENT:
918 case CMD_CAN_ERROR_EVENT:
919 kvaser_usb_rx_error(dev, msg);
922 case CMD_LOG_MESSAGE:
923 if (msg->u.log_message.flags & MSG_FLAG_ERROR_FRAME)
924 kvaser_usb_rx_error(dev, msg);
927 case CMD_TX_ACKNOWLEDGE:
928 kvaser_usb_tx_acknowledge(dev, msg);
932 dev_warn(dev->udev->dev.parent,
933 "Unhandled message (%d)\n", msg->id);
938 static void kvaser_usb_read_bulk_callback(struct urb *urb)
940 struct kvaser_usb *dev = urb->context;
941 struct kvaser_msg *msg;
945 switch (urb->status) {
952 dev_info(dev->udev->dev.parent, "Rx URB aborted (%d)\n",
957 while (pos <= urb->actual_length - MSG_HEADER_LEN) {
958 msg = urb->transfer_buffer + pos;
963 if (pos + msg->len > urb->actual_length) {
964 dev_err(dev->udev->dev.parent, "Format error\n");
968 kvaser_usb_handle_message(dev, msg);
974 usb_fill_bulk_urb(urb, dev->udev,
975 usb_rcvbulkpipe(dev->udev,
976 dev->bulk_in->bEndpointAddress),
977 urb->transfer_buffer, RX_BUFFER_SIZE,
978 kvaser_usb_read_bulk_callback, dev);
980 err = usb_submit_urb(urb, GFP_ATOMIC);
981 if (err == -ENODEV) {
982 for (i = 0; i < dev->nchannels; i++) {
986 netif_device_detach(dev->nets[i]->netdev);
989 dev_err(dev->udev->dev.parent,
990 "Failed resubmitting read bulk urb: %d\n", err);
996 static int kvaser_usb_setup_rx_urbs(struct kvaser_usb *dev)
1000 if (dev->rxinitdone)
1003 for (i = 0; i < MAX_RX_URBS; i++) {
1004 struct urb *urb = NULL;
1008 urb = usb_alloc_urb(0, GFP_KERNEL);
1010 dev_warn(dev->udev->dev.parent,
1011 "No memory left for URBs\n");
1016 buf = usb_alloc_coherent(dev->udev, RX_BUFFER_SIZE,
1017 GFP_KERNEL, &buf_dma);
1019 dev_warn(dev->udev->dev.parent,
1020 "No memory left for USB buffer\n");
1026 usb_fill_bulk_urb(urb, dev->udev,
1027 usb_rcvbulkpipe(dev->udev,
1028 dev->bulk_in->bEndpointAddress),
1029 buf, RX_BUFFER_SIZE,
1030 kvaser_usb_read_bulk_callback,
1032 urb->transfer_dma = buf_dma;
1033 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1034 usb_anchor_urb(urb, &dev->rx_submitted);
1036 err = usb_submit_urb(urb, GFP_KERNEL);
1038 usb_unanchor_urb(urb);
1039 usb_free_coherent(dev->udev, RX_BUFFER_SIZE, buf,
1045 dev->rxbuf[i] = buf;
1046 dev->rxbuf_dma[i] = buf_dma;
1052 dev_warn(dev->udev->dev.parent,
1053 "Cannot setup read URBs, error %d\n", err);
1055 } else if (i < MAX_RX_URBS) {
1056 dev_warn(dev->udev->dev.parent,
1057 "RX performances may be slow\n");
1060 dev->rxinitdone = true;
1065 static int kvaser_usb_set_opt_mode(const struct kvaser_usb_net_priv *priv)
1067 struct kvaser_msg *msg;
1070 msg = kmalloc(sizeof(*msg), GFP_KERNEL);
1074 msg->id = CMD_SET_CTRL_MODE;
1075 msg->len = MSG_HEADER_LEN + sizeof(struct kvaser_msg_ctrl_mode);
1076 msg->u.ctrl_mode.tid = 0xff;
1077 msg->u.ctrl_mode.channel = priv->channel;
1079 if (priv->can.ctrlmode & CAN_CTRLMODE_LISTENONLY)
1080 msg->u.ctrl_mode.ctrl_mode = KVASER_CTRL_MODE_SILENT;
1082 msg->u.ctrl_mode.ctrl_mode = KVASER_CTRL_MODE_NORMAL;
1084 rc = kvaser_usb_send_msg(priv->dev, msg);
1090 static int kvaser_usb_start_chip(struct kvaser_usb_net_priv *priv)
1094 init_completion(&priv->start_comp);
1096 err = kvaser_usb_send_simple_msg(priv->dev, CMD_START_CHIP,
1101 if (!wait_for_completion_timeout(&priv->start_comp,
1102 msecs_to_jiffies(START_TIMEOUT)))
1108 static int kvaser_usb_open(struct net_device *netdev)
1110 struct kvaser_usb_net_priv *priv = netdev_priv(netdev);
1111 struct kvaser_usb *dev = priv->dev;
1114 err = open_candev(netdev);
1118 err = kvaser_usb_setup_rx_urbs(dev);
1122 err = kvaser_usb_set_opt_mode(priv);
1126 err = kvaser_usb_start_chip(priv);
1128 netdev_warn(netdev, "Cannot start device, error %d\n", err);
1132 priv->can.state = CAN_STATE_ERROR_ACTIVE;
1137 close_candev(netdev);
1141 static void kvaser_usb_unlink_all_urbs(struct kvaser_usb *dev)
1145 usb_kill_anchored_urbs(&dev->rx_submitted);
1147 for (i = 0; i < MAX_RX_URBS; i++)
1148 usb_free_coherent(dev->udev, RX_BUFFER_SIZE,
1152 for (i = 0; i < MAX_NET_DEVICES; i++) {
1153 struct kvaser_usb_net_priv *priv = dev->nets[i];
1156 kvaser_usb_unlink_tx_urbs(priv);
1160 static int kvaser_usb_stop_chip(struct kvaser_usb_net_priv *priv)
1164 init_completion(&priv->stop_comp);
1166 err = kvaser_usb_send_simple_msg(priv->dev, CMD_STOP_CHIP,
1171 if (!wait_for_completion_timeout(&priv->stop_comp,
1172 msecs_to_jiffies(STOP_TIMEOUT)))
1178 static int kvaser_usb_flush_queue(struct kvaser_usb_net_priv *priv)
1180 struct kvaser_msg *msg;
1183 msg = kmalloc(sizeof(*msg), GFP_KERNEL);
1187 msg->id = CMD_FLUSH_QUEUE;
1188 msg->len = MSG_HEADER_LEN + sizeof(struct kvaser_msg_flush_queue);
1189 msg->u.flush_queue.channel = priv->channel;
1190 msg->u.flush_queue.flags = 0x00;
1192 rc = kvaser_usb_send_msg(priv->dev, msg);
1198 static int kvaser_usb_close(struct net_device *netdev)
1200 struct kvaser_usb_net_priv *priv = netdev_priv(netdev);
1201 struct kvaser_usb *dev = priv->dev;
1204 netif_stop_queue(netdev);
1206 err = kvaser_usb_flush_queue(priv);
1208 netdev_warn(netdev, "Cannot flush queue, error %d\n", err);
1210 if (kvaser_usb_send_simple_msg(dev, CMD_RESET_CHIP, priv->channel))
1211 netdev_warn(netdev, "Cannot reset card, error %d\n", err);
1213 err = kvaser_usb_stop_chip(priv);
1215 netdev_warn(netdev, "Cannot stop device, error %d\n", err);
1217 priv->can.state = CAN_STATE_STOPPED;
1218 close_candev(priv->netdev);
1223 static void kvaser_usb_write_bulk_callback(struct urb *urb)
1225 struct kvaser_usb_tx_urb_context *context = urb->context;
1226 struct kvaser_usb_net_priv *priv;
1227 struct net_device *netdev;
1229 if (WARN_ON(!context))
1232 priv = context->priv;
1233 netdev = priv->netdev;
1235 kfree(urb->transfer_buffer);
1237 if (!netif_device_present(netdev))
1241 netdev_info(netdev, "Tx URB aborted (%d)\n", urb->status);
1244 static netdev_tx_t kvaser_usb_start_xmit(struct sk_buff *skb,
1245 struct net_device *netdev)
1247 struct kvaser_usb_net_priv *priv = netdev_priv(netdev);
1248 struct kvaser_usb *dev = priv->dev;
1249 struct net_device_stats *stats = &netdev->stats;
1250 struct can_frame *cf = (struct can_frame *)skb->data;
1251 struct kvaser_usb_tx_urb_context *context = NULL;
1254 struct kvaser_msg *msg;
1256 int ret = NETDEV_TX_OK;
1258 if (can_dropped_invalid_skb(netdev, skb))
1259 return NETDEV_TX_OK;
1261 urb = usb_alloc_urb(0, GFP_ATOMIC);
1263 netdev_err(netdev, "No memory left for URBs\n");
1264 stats->tx_dropped++;
1268 buf = kmalloc(sizeof(struct kvaser_msg), GFP_ATOMIC);
1270 stats->tx_dropped++;
1275 msg->len = MSG_HEADER_LEN + sizeof(struct kvaser_msg_tx_can);
1276 msg->u.tx_can.flags = 0;
1277 msg->u.tx_can.channel = priv->channel;
1279 if (cf->can_id & CAN_EFF_FLAG) {
1280 msg->id = CMD_TX_EXT_MESSAGE;
1281 msg->u.tx_can.msg[0] = (cf->can_id >> 24) & 0x1f;
1282 msg->u.tx_can.msg[1] = (cf->can_id >> 18) & 0x3f;
1283 msg->u.tx_can.msg[2] = (cf->can_id >> 14) & 0x0f;
1284 msg->u.tx_can.msg[3] = (cf->can_id >> 6) & 0xff;
1285 msg->u.tx_can.msg[4] = cf->can_id & 0x3f;
1287 msg->id = CMD_TX_STD_MESSAGE;
1288 msg->u.tx_can.msg[0] = (cf->can_id >> 6) & 0x1f;
1289 msg->u.tx_can.msg[1] = cf->can_id & 0x3f;
1292 msg->u.tx_can.msg[5] = cf->can_dlc;
1293 memcpy(&msg->u.tx_can.msg[6], cf->data, cf->can_dlc);
1295 if (cf->can_id & CAN_RTR_FLAG)
1296 msg->u.tx_can.flags |= MSG_FLAG_REMOTE_FRAME;
1298 for (i = 0; i < ARRAY_SIZE(priv->tx_contexts); i++) {
1299 if (priv->tx_contexts[i].echo_index == MAX_TX_URBS) {
1300 context = &priv->tx_contexts[i];
1306 netdev_warn(netdev, "cannot find free context\n");
1307 ret = NETDEV_TX_BUSY;
1311 context->priv = priv;
1312 context->echo_index = i;
1313 context->dlc = cf->can_dlc;
1315 msg->u.tx_can.tid = context->echo_index;
1317 usb_fill_bulk_urb(urb, dev->udev,
1318 usb_sndbulkpipe(dev->udev,
1319 dev->bulk_out->bEndpointAddress),
1321 kvaser_usb_write_bulk_callback, context);
1322 usb_anchor_urb(urb, &priv->tx_submitted);
1324 can_put_echo_skb(skb, netdev, context->echo_index);
1326 atomic_inc(&priv->active_tx_urbs);
1328 if (atomic_read(&priv->active_tx_urbs) >= MAX_TX_URBS)
1329 netif_stop_queue(netdev);
1331 err = usb_submit_urb(urb, GFP_ATOMIC);
1332 if (unlikely(err)) {
1333 can_free_echo_skb(netdev, context->echo_index);
1335 skb = NULL; /* set to NULL to avoid double free in
1336 * dev_kfree_skb(skb) */
1338 atomic_dec(&priv->active_tx_urbs);
1339 usb_unanchor_urb(urb);
1341 stats->tx_dropped++;
1344 netif_device_detach(netdev);
1346 netdev_warn(netdev, "Failed tx_urb %d\n", err);
1353 return NETDEV_TX_OK;
1364 static const struct net_device_ops kvaser_usb_netdev_ops = {
1365 .ndo_open = kvaser_usb_open,
1366 .ndo_stop = kvaser_usb_close,
1367 .ndo_start_xmit = kvaser_usb_start_xmit,
1370 static const struct can_bittiming_const kvaser_usb_bittiming_const = {
1371 .name = "kvaser_usb",
1372 .tseg1_min = KVASER_USB_TSEG1_MIN,
1373 .tseg1_max = KVASER_USB_TSEG1_MAX,
1374 .tseg2_min = KVASER_USB_TSEG2_MIN,
1375 .tseg2_max = KVASER_USB_TSEG2_MAX,
1376 .sjw_max = KVASER_USB_SJW_MAX,
1377 .brp_min = KVASER_USB_BRP_MIN,
1378 .brp_max = KVASER_USB_BRP_MAX,
1379 .brp_inc = KVASER_USB_BRP_INC,
1382 static int kvaser_usb_set_bittiming(struct net_device *netdev)
1384 struct kvaser_usb_net_priv *priv = netdev_priv(netdev);
1385 struct can_bittiming *bt = &priv->can.bittiming;
1386 struct kvaser_usb *dev = priv->dev;
1387 struct kvaser_msg *msg;
1390 msg = kmalloc(sizeof(*msg), GFP_KERNEL);
1394 msg->id = CMD_SET_BUS_PARAMS;
1395 msg->len = MSG_HEADER_LEN + sizeof(struct kvaser_msg_busparams);
1396 msg->u.busparams.channel = priv->channel;
1397 msg->u.busparams.tid = 0xff;
1398 msg->u.busparams.bitrate = cpu_to_le32(bt->bitrate);
1399 msg->u.busparams.sjw = bt->sjw;
1400 msg->u.busparams.tseg1 = bt->prop_seg + bt->phase_seg1;
1401 msg->u.busparams.tseg2 = bt->phase_seg2;
1403 if (priv->can.ctrlmode & CAN_CTRLMODE_3_SAMPLES)
1404 msg->u.busparams.no_samp = 3;
1406 msg->u.busparams.no_samp = 1;
1408 rc = kvaser_usb_send_msg(dev, msg);
1414 static int kvaser_usb_set_mode(struct net_device *netdev,
1417 struct kvaser_usb_net_priv *priv = netdev_priv(netdev);
1421 case CAN_MODE_START:
1422 err = kvaser_usb_simple_msg_async(priv, CMD_START_CHIP);
1433 static int kvaser_usb_get_berr_counter(const struct net_device *netdev,
1434 struct can_berr_counter *bec)
1436 struct kvaser_usb_net_priv *priv = netdev_priv(netdev);
1443 static void kvaser_usb_remove_interfaces(struct kvaser_usb *dev)
1447 for (i = 0; i < dev->nchannels; i++) {
1451 unregister_netdev(dev->nets[i]->netdev);
1454 kvaser_usb_unlink_all_urbs(dev);
1456 for (i = 0; i < dev->nchannels; i++) {
1460 free_candev(dev->nets[i]->netdev);
1464 static int kvaser_usb_init_one(struct usb_interface *intf,
1465 const struct usb_device_id *id, int channel)
1467 struct kvaser_usb *dev = usb_get_intfdata(intf);
1468 struct net_device *netdev;
1469 struct kvaser_usb_net_priv *priv;
1472 netdev = alloc_candev(sizeof(*priv), MAX_TX_URBS);
1474 dev_err(&intf->dev, "Cannot alloc candev\n");
1478 priv = netdev_priv(netdev);
1480 init_completion(&priv->start_comp);
1481 init_completion(&priv->stop_comp);
1483 init_usb_anchor(&priv->tx_submitted);
1484 atomic_set(&priv->active_tx_urbs, 0);
1486 for (i = 0; i < ARRAY_SIZE(priv->tx_contexts); i++)
1487 priv->tx_contexts[i].echo_index = MAX_TX_URBS;
1490 priv->netdev = netdev;
1491 priv->channel = channel;
1493 priv->can.state = CAN_STATE_STOPPED;
1494 priv->can.clock.freq = CAN_USB_CLOCK;
1495 priv->can.bittiming_const = &kvaser_usb_bittiming_const;
1496 priv->can.do_set_bittiming = kvaser_usb_set_bittiming;
1497 priv->can.do_set_mode = kvaser_usb_set_mode;
1498 if (id->driver_info & KVASER_HAS_TXRX_ERRORS)
1499 priv->can.do_get_berr_counter = kvaser_usb_get_berr_counter;
1500 priv->can.ctrlmode_supported = CAN_CTRLMODE_3_SAMPLES;
1501 if (id->driver_info & KVASER_HAS_SILENT_MODE)
1502 priv->can.ctrlmode_supported |= CAN_CTRLMODE_LISTENONLY;
1504 netdev->flags |= IFF_ECHO;
1506 netdev->netdev_ops = &kvaser_usb_netdev_ops;
1508 SET_NETDEV_DEV(netdev, &intf->dev);
1510 dev->nets[channel] = priv;
1512 err = register_candev(netdev);
1514 dev_err(&intf->dev, "Failed to register can device\n");
1515 free_candev(netdev);
1516 dev->nets[channel] = NULL;
1520 netdev_dbg(netdev, "device registered\n");
1525 static void kvaser_usb_get_endpoints(const struct usb_interface *intf,
1526 struct usb_endpoint_descriptor **in,
1527 struct usb_endpoint_descriptor **out)
1529 const struct usb_host_interface *iface_desc;
1530 struct usb_endpoint_descriptor *endpoint;
1533 iface_desc = &intf->altsetting[0];
1535 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
1536 endpoint = &iface_desc->endpoint[i].desc;
1538 if (usb_endpoint_is_bulk_in(endpoint))
1541 if (usb_endpoint_is_bulk_out(endpoint))
1546 static int kvaser_usb_probe(struct usb_interface *intf,
1547 const struct usb_device_id *id)
1549 struct kvaser_usb *dev;
1553 dev = devm_kzalloc(&intf->dev, sizeof(*dev), GFP_KERNEL);
1557 kvaser_usb_get_endpoints(intf, &dev->bulk_in, &dev->bulk_out);
1558 if (!dev->bulk_in || !dev->bulk_out) {
1559 dev_err(&intf->dev, "Cannot get usb endpoint(s)");
1563 dev->udev = interface_to_usbdev(intf);
1565 init_usb_anchor(&dev->rx_submitted);
1567 usb_set_intfdata(intf, dev);
1569 for (i = 0; i < MAX_NET_DEVICES; i++)
1570 kvaser_usb_send_simple_msg(dev, CMD_RESET_CHIP, i);
1572 err = kvaser_usb_get_software_info(dev);
1575 "Cannot get software infos, error %d\n", err);
1579 err = kvaser_usb_get_card_info(dev);
1582 "Cannot get card infos, error %d\n", err);
1586 dev_dbg(&intf->dev, "Firmware version: %d.%d.%d\n",
1587 ((dev->fw_version >> 24) & 0xff),
1588 ((dev->fw_version >> 16) & 0xff),
1589 (dev->fw_version & 0xffff));
1591 for (i = 0; i < dev->nchannels; i++) {
1592 err = kvaser_usb_init_one(intf, id, i);
1594 kvaser_usb_remove_interfaces(dev);
1602 static void kvaser_usb_disconnect(struct usb_interface *intf)
1604 struct kvaser_usb *dev = usb_get_intfdata(intf);
1606 usb_set_intfdata(intf, NULL);
1611 kvaser_usb_remove_interfaces(dev);
1614 static struct usb_driver kvaser_usb_driver = {
1615 .name = "kvaser_usb",
1616 .probe = kvaser_usb_probe,
1617 .disconnect = kvaser_usb_disconnect,
1618 .id_table = kvaser_usb_table,
1621 module_usb_driver(kvaser_usb_driver);
1623 MODULE_AUTHOR("Olivier Sobrie <olivier@sobrie.be>");
1624 MODULE_DESCRIPTION("CAN driver for Kvaser CAN/USB devices");
1625 MODULE_LICENSE("GPL v2");