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
140 #define KVASER_EXTENDED_FRAME BIT(31)
142 struct kvaser_msg_simple {
147 struct kvaser_msg_cardinfo {
150 __le32 serial_number;
152 __le32 clock_resolution;
160 struct kvaser_msg_cardinfo2 {
164 __le32 oem_unlock_code;
167 struct kvaser_msg_softinfo {
172 __le16 max_outstanding_tx;
176 struct kvaser_msg_busparams {
186 struct kvaser_msg_tx_can {
194 struct kvaser_msg_rx_can {
201 struct kvaser_msg_chip_state_event {
211 struct kvaser_msg_tx_acknowledge {
219 struct kvaser_msg_error_event {
231 struct kvaser_msg_ctrl_mode {
238 struct kvaser_msg_flush_queue {
245 struct kvaser_msg_log_message {
259 struct kvaser_msg_simple simple;
260 struct kvaser_msg_cardinfo cardinfo;
261 struct kvaser_msg_cardinfo2 cardinfo2;
262 struct kvaser_msg_softinfo softinfo;
263 struct kvaser_msg_busparams busparams;
264 struct kvaser_msg_tx_can tx_can;
265 struct kvaser_msg_rx_can rx_can;
266 struct kvaser_msg_chip_state_event chip_state_event;
267 struct kvaser_msg_tx_acknowledge tx_acknowledge;
268 struct kvaser_msg_error_event error_event;
269 struct kvaser_msg_ctrl_mode ctrl_mode;
270 struct kvaser_msg_flush_queue flush_queue;
271 struct kvaser_msg_log_message log_message;
275 struct kvaser_usb_tx_urb_context {
276 struct kvaser_usb_net_priv *priv;
282 struct usb_device *udev;
283 struct kvaser_usb_net_priv *nets[MAX_NET_DEVICES];
285 struct usb_endpoint_descriptor *bulk_in, *bulk_out;
286 struct usb_anchor rx_submitted;
289 unsigned int nchannels;
292 void *rxbuf[MAX_RX_URBS];
293 dma_addr_t rxbuf_dma[MAX_RX_URBS];
296 struct kvaser_usb_net_priv {
299 atomic_t active_tx_urbs;
300 struct usb_anchor tx_submitted;
301 struct kvaser_usb_tx_urb_context tx_contexts[MAX_TX_URBS];
303 struct completion start_comp, stop_comp;
305 struct kvaser_usb *dev;
306 struct net_device *netdev;
309 struct can_berr_counter bec;
312 static const struct usb_device_id kvaser_usb_table[] = {
313 { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_DEVEL_PRODUCT_ID) },
314 { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_LITE_PRODUCT_ID) },
315 { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_PRO_PRODUCT_ID),
316 .driver_info = KVASER_HAS_TXRX_ERRORS |
317 KVASER_HAS_SILENT_MODE },
318 { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_SPRO_PRODUCT_ID),
319 .driver_info = KVASER_HAS_TXRX_ERRORS |
320 KVASER_HAS_SILENT_MODE },
321 { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_PRO_LS_PRODUCT_ID),
322 .driver_info = KVASER_HAS_TXRX_ERRORS |
323 KVASER_HAS_SILENT_MODE },
324 { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_PRO_SWC_PRODUCT_ID),
325 .driver_info = KVASER_HAS_TXRX_ERRORS |
326 KVASER_HAS_SILENT_MODE },
327 { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_PRO_LIN_PRODUCT_ID),
328 .driver_info = KVASER_HAS_TXRX_ERRORS |
329 KVASER_HAS_SILENT_MODE },
330 { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_SPRO_LS_PRODUCT_ID),
331 .driver_info = KVASER_HAS_TXRX_ERRORS |
332 KVASER_HAS_SILENT_MODE },
333 { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_SPRO_SWC_PRODUCT_ID),
334 .driver_info = KVASER_HAS_TXRX_ERRORS |
335 KVASER_HAS_SILENT_MODE },
336 { USB_DEVICE(KVASER_VENDOR_ID, USB_MEMO2_DEVEL_PRODUCT_ID),
337 .driver_info = KVASER_HAS_TXRX_ERRORS |
338 KVASER_HAS_SILENT_MODE },
339 { USB_DEVICE(KVASER_VENDOR_ID, USB_MEMO2_HSHS_PRODUCT_ID),
340 .driver_info = KVASER_HAS_TXRX_ERRORS |
341 KVASER_HAS_SILENT_MODE },
342 { USB_DEVICE(KVASER_VENDOR_ID, USB_UPRO_HSHS_PRODUCT_ID),
343 .driver_info = KVASER_HAS_TXRX_ERRORS },
344 { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_LITE_GI_PRODUCT_ID) },
345 { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_PRO_OBDII_PRODUCT_ID),
346 .driver_info = KVASER_HAS_TXRX_ERRORS |
347 KVASER_HAS_SILENT_MODE },
348 { USB_DEVICE(KVASER_VENDOR_ID, USB_MEMO2_HSLS_PRODUCT_ID),
349 .driver_info = KVASER_HAS_TXRX_ERRORS },
350 { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_LITE_CH_PRODUCT_ID),
351 .driver_info = KVASER_HAS_TXRX_ERRORS },
352 { USB_DEVICE(KVASER_VENDOR_ID, USB_BLACKBIRD_SPRO_PRODUCT_ID),
353 .driver_info = KVASER_HAS_TXRX_ERRORS },
354 { USB_DEVICE(KVASER_VENDOR_ID, USB_OEM_MERCURY_PRODUCT_ID),
355 .driver_info = KVASER_HAS_TXRX_ERRORS },
356 { USB_DEVICE(KVASER_VENDOR_ID, USB_OEM_LEAF_PRODUCT_ID),
357 .driver_info = KVASER_HAS_TXRX_ERRORS },
358 { USB_DEVICE(KVASER_VENDOR_ID, USB_CAN_R_PRODUCT_ID),
359 .driver_info = KVASER_HAS_TXRX_ERRORS },
362 MODULE_DEVICE_TABLE(usb, kvaser_usb_table);
364 static inline int kvaser_usb_send_msg(const struct kvaser_usb *dev,
365 struct kvaser_msg *msg)
369 return usb_bulk_msg(dev->udev,
370 usb_sndbulkpipe(dev->udev,
371 dev->bulk_out->bEndpointAddress),
372 msg, msg->len, &actual_len,
376 static int kvaser_usb_wait_msg(const struct kvaser_usb *dev, u8 id,
377 struct kvaser_msg *msg)
379 struct kvaser_msg *tmp;
385 buf = kzalloc(RX_BUFFER_SIZE, GFP_KERNEL);
389 err = usb_bulk_msg(dev->udev,
390 usb_rcvbulkpipe(dev->udev,
391 dev->bulk_in->bEndpointAddress),
392 buf, RX_BUFFER_SIZE, &actual_len,
397 while (pos <= actual_len - MSG_HEADER_LEN) {
403 if (pos + tmp->len > actual_len) {
404 dev_err(dev->udev->dev.parent, "Format error\n");
409 memcpy(msg, tmp, tmp->len);
424 static int kvaser_usb_send_simple_msg(const struct kvaser_usb *dev,
425 u8 msg_id, int channel)
427 struct kvaser_msg *msg;
430 msg = kmalloc(sizeof(*msg), GFP_KERNEL);
435 msg->len = MSG_HEADER_LEN + sizeof(struct kvaser_msg_simple);
436 msg->u.simple.channel = channel;
437 msg->u.simple.tid = 0xff;
439 rc = kvaser_usb_send_msg(dev, msg);
445 static int kvaser_usb_get_software_info(struct kvaser_usb *dev)
447 struct kvaser_msg msg;
450 err = kvaser_usb_send_simple_msg(dev, CMD_GET_SOFTWARE_INFO, 0);
454 err = kvaser_usb_wait_msg(dev, CMD_GET_SOFTWARE_INFO_REPLY, &msg);
458 dev->fw_version = le32_to_cpu(msg.u.softinfo.fw_version);
463 static int kvaser_usb_get_card_info(struct kvaser_usb *dev)
465 struct kvaser_msg msg;
468 err = kvaser_usb_send_simple_msg(dev, CMD_GET_CARD_INFO, 0);
472 err = kvaser_usb_wait_msg(dev, CMD_GET_CARD_INFO_REPLY, &msg);
476 dev->nchannels = msg.u.cardinfo.nchannels;
477 if (dev->nchannels > MAX_NET_DEVICES)
483 static void kvaser_usb_tx_acknowledge(const struct kvaser_usb *dev,
484 const struct kvaser_msg *msg)
486 struct net_device_stats *stats;
487 struct kvaser_usb_tx_urb_context *context;
488 struct kvaser_usb_net_priv *priv;
490 struct can_frame *cf;
491 u8 channel = msg->u.tx_acknowledge.channel;
492 u8 tid = msg->u.tx_acknowledge.tid;
494 if (channel >= dev->nchannels) {
495 dev_err(dev->udev->dev.parent,
496 "Invalid channel number (%d)\n", channel);
500 priv = dev->nets[channel];
502 if (!netif_device_present(priv->netdev))
505 stats = &priv->netdev->stats;
507 context = &priv->tx_contexts[tid % MAX_TX_URBS];
509 /* Sometimes the state change doesn't come after a bus-off event */
510 if (priv->can.restart_ms &&
511 (priv->can.state >= CAN_STATE_BUS_OFF)) {
512 skb = alloc_can_err_skb(priv->netdev, &cf);
514 cf->can_id |= CAN_ERR_RESTARTED;
518 stats->rx_bytes += cf->can_dlc;
520 netdev_err(priv->netdev,
521 "No memory left for err_skb\n");
524 priv->can.can_stats.restarts++;
525 netif_carrier_on(priv->netdev);
527 priv->can.state = CAN_STATE_ERROR_ACTIVE;
531 stats->tx_bytes += context->dlc;
532 can_get_echo_skb(priv->netdev, context->echo_index);
534 context->echo_index = MAX_TX_URBS;
535 atomic_dec(&priv->active_tx_urbs);
537 netif_wake_queue(priv->netdev);
540 static void kvaser_usb_simple_msg_callback(struct urb *urb)
542 struct net_device *netdev = urb->context;
544 kfree(urb->transfer_buffer);
547 netdev_warn(netdev, "urb status received: %d\n",
551 static int kvaser_usb_simple_msg_async(struct kvaser_usb_net_priv *priv,
554 struct kvaser_usb *dev = priv->dev;
555 struct net_device *netdev = priv->netdev;
556 struct kvaser_msg *msg;
561 urb = usb_alloc_urb(0, GFP_ATOMIC);
563 netdev_err(netdev, "No memory left for URBs\n");
567 buf = kmalloc(sizeof(struct kvaser_msg), GFP_ATOMIC);
573 msg = (struct kvaser_msg *)buf;
574 msg->len = MSG_HEADER_LEN + sizeof(struct kvaser_msg_simple);
576 msg->u.simple.channel = priv->channel;
578 usb_fill_bulk_urb(urb, dev->udev,
579 usb_sndbulkpipe(dev->udev,
580 dev->bulk_out->bEndpointAddress),
582 kvaser_usb_simple_msg_callback, priv);
583 usb_anchor_urb(urb, &priv->tx_submitted);
585 err = usb_submit_urb(urb, GFP_ATOMIC);
587 netdev_err(netdev, "Error transmitting URB\n");
588 usb_unanchor_urb(urb);
599 static void kvaser_usb_unlink_tx_urbs(struct kvaser_usb_net_priv *priv)
603 usb_kill_anchored_urbs(&priv->tx_submitted);
604 atomic_set(&priv->active_tx_urbs, 0);
606 for (i = 0; i < MAX_TX_URBS; i++)
607 priv->tx_contexts[i].echo_index = MAX_TX_URBS;
610 static void kvaser_usb_rx_error(const struct kvaser_usb *dev,
611 const struct kvaser_msg *msg)
613 struct can_frame *cf;
615 struct net_device_stats *stats;
616 struct kvaser_usb_net_priv *priv;
617 unsigned int new_state;
618 u8 channel, status, txerr, rxerr, error_factor;
621 case CMD_CAN_ERROR_EVENT:
622 channel = msg->u.error_event.channel;
623 status = msg->u.error_event.status;
624 txerr = msg->u.error_event.tx_errors_count;
625 rxerr = msg->u.error_event.rx_errors_count;
626 error_factor = msg->u.error_event.error_factor;
628 case CMD_LOG_MESSAGE:
629 channel = msg->u.log_message.channel;
630 status = msg->u.log_message.data[0];
631 txerr = msg->u.log_message.data[2];
632 rxerr = msg->u.log_message.data[3];
633 error_factor = msg->u.log_message.data[1];
635 case CMD_CHIP_STATE_EVENT:
636 channel = msg->u.chip_state_event.channel;
637 status = msg->u.chip_state_event.status;
638 txerr = msg->u.chip_state_event.tx_errors_count;
639 rxerr = msg->u.chip_state_event.rx_errors_count;
643 dev_err(dev->udev->dev.parent, "Invalid msg id (%d)\n",
648 if (channel >= dev->nchannels) {
649 dev_err(dev->udev->dev.parent,
650 "Invalid channel number (%d)\n", channel);
654 priv = dev->nets[channel];
655 stats = &priv->netdev->stats;
657 if (status & M16C_STATE_BUS_RESET) {
658 kvaser_usb_unlink_tx_urbs(priv);
662 skb = alloc_can_err_skb(priv->netdev, &cf);
668 new_state = priv->can.state;
670 netdev_dbg(priv->netdev, "Error status: 0x%02x\n", status);
672 if (status & M16C_STATE_BUS_OFF) {
673 cf->can_id |= CAN_ERR_BUSOFF;
675 priv->can.can_stats.bus_off++;
676 if (!priv->can.restart_ms)
677 kvaser_usb_simple_msg_async(priv, CMD_STOP_CHIP);
679 netif_carrier_off(priv->netdev);
681 new_state = CAN_STATE_BUS_OFF;
682 } else if (status & M16C_STATE_BUS_PASSIVE) {
683 if (priv->can.state != CAN_STATE_ERROR_PASSIVE) {
684 cf->can_id |= CAN_ERR_CRTL;
687 cf->data[1] = (txerr > rxerr)
688 ? CAN_ERR_CRTL_TX_PASSIVE
689 : CAN_ERR_CRTL_RX_PASSIVE;
691 cf->data[1] = CAN_ERR_CRTL_TX_PASSIVE |
692 CAN_ERR_CRTL_RX_PASSIVE;
694 priv->can.can_stats.error_passive++;
697 new_state = CAN_STATE_ERROR_PASSIVE;
700 if (status == M16C_STATE_BUS_ERROR) {
701 if ((priv->can.state < CAN_STATE_ERROR_WARNING) &&
702 ((txerr >= 96) || (rxerr >= 96))) {
703 cf->can_id |= CAN_ERR_CRTL;
704 cf->data[1] = (txerr > rxerr)
705 ? CAN_ERR_CRTL_TX_WARNING
706 : CAN_ERR_CRTL_RX_WARNING;
708 priv->can.can_stats.error_warning++;
709 new_state = CAN_STATE_ERROR_WARNING;
710 } else if (priv->can.state > CAN_STATE_ERROR_ACTIVE) {
711 cf->can_id |= CAN_ERR_PROT;
712 cf->data[2] = CAN_ERR_PROT_ACTIVE;
714 new_state = CAN_STATE_ERROR_ACTIVE;
719 cf->can_id |= CAN_ERR_PROT;
720 cf->data[2] = CAN_ERR_PROT_ACTIVE;
722 new_state = CAN_STATE_ERROR_ACTIVE;
725 if (priv->can.restart_ms &&
726 (priv->can.state >= CAN_STATE_BUS_OFF) &&
727 (new_state < CAN_STATE_BUS_OFF)) {
728 cf->can_id |= CAN_ERR_RESTARTED;
729 netif_carrier_on(priv->netdev);
731 priv->can.can_stats.restarts++;
735 priv->can.can_stats.bus_error++;
738 cf->can_id |= CAN_ERR_BUSERROR | CAN_ERR_PROT;
740 if (error_factor & M16C_EF_ACKE)
741 cf->data[3] |= (CAN_ERR_PROT_LOC_ACK);
742 if (error_factor & M16C_EF_CRCE)
743 cf->data[3] |= (CAN_ERR_PROT_LOC_CRC_SEQ |
744 CAN_ERR_PROT_LOC_CRC_DEL);
745 if (error_factor & M16C_EF_FORME)
746 cf->data[2] |= CAN_ERR_PROT_FORM;
747 if (error_factor & M16C_EF_STFE)
748 cf->data[2] |= CAN_ERR_PROT_STUFF;
749 if (error_factor & M16C_EF_BITE0)
750 cf->data[2] |= CAN_ERR_PROT_BIT0;
751 if (error_factor & M16C_EF_BITE1)
752 cf->data[2] |= CAN_ERR_PROT_BIT1;
753 if (error_factor & M16C_EF_TRE)
754 cf->data[2] |= CAN_ERR_PROT_TX;
760 priv->bec.txerr = txerr;
761 priv->bec.rxerr = rxerr;
763 priv->can.state = new_state;
768 stats->rx_bytes += cf->can_dlc;
771 static void kvaser_usb_rx_can_err(const struct kvaser_usb_net_priv *priv,
772 const struct kvaser_msg *msg)
774 struct can_frame *cf;
776 struct net_device_stats *stats = &priv->netdev->stats;
778 if (msg->u.rx_can.flag & (MSG_FLAG_ERROR_FRAME |
780 netdev_err(priv->netdev, "Unknow error (flags: 0x%02x)\n",
787 if (msg->u.rx_can.flag & MSG_FLAG_OVERRUN) {
788 skb = alloc_can_err_skb(priv->netdev, &cf);
794 cf->can_id |= CAN_ERR_CRTL;
795 cf->data[1] = CAN_ERR_CRTL_RX_OVERFLOW;
797 stats->rx_over_errors++;
803 stats->rx_bytes += cf->can_dlc;
807 static void kvaser_usb_rx_can_msg(const struct kvaser_usb *dev,
808 const struct kvaser_msg *msg)
810 struct kvaser_usb_net_priv *priv;
811 struct can_frame *cf;
813 struct net_device_stats *stats;
814 u8 channel = msg->u.rx_can.channel;
816 if (channel >= dev->nchannels) {
817 dev_err(dev->udev->dev.parent,
818 "Invalid channel number (%d)\n", channel);
822 priv = dev->nets[channel];
823 stats = &priv->netdev->stats;
825 if ((msg->u.rx_can.flag & MSG_FLAG_ERROR_FRAME) &&
826 (msg->id == CMD_LOG_MESSAGE)) {
827 kvaser_usb_rx_error(dev, msg);
829 } else if (msg->u.rx_can.flag & (MSG_FLAG_ERROR_FRAME |
832 kvaser_usb_rx_can_err(priv, msg);
834 } else if (msg->u.rx_can.flag & ~MSG_FLAG_REMOTE_FRAME) {
835 netdev_warn(priv->netdev,
836 "Unhandled frame (flags: 0x%02x)",
841 skb = alloc_can_skb(priv->netdev, &cf);
847 if (msg->id == CMD_LOG_MESSAGE) {
848 cf->can_id = le32_to_cpu(msg->u.log_message.id);
849 if (cf->can_id & KVASER_EXTENDED_FRAME)
850 cf->can_id &= CAN_EFF_MASK | CAN_EFF_FLAG;
852 cf->can_id &= CAN_SFF_MASK;
854 cf->can_dlc = get_can_dlc(msg->u.log_message.dlc);
856 if (msg->u.log_message.flags & MSG_FLAG_REMOTE_FRAME)
857 cf->can_id |= CAN_RTR_FLAG;
859 memcpy(cf->data, &msg->u.log_message.data,
862 cf->can_id = ((msg->u.rx_can.msg[0] & 0x1f) << 6) |
863 (msg->u.rx_can.msg[1] & 0x3f);
865 if (msg->id == CMD_RX_EXT_MESSAGE) {
867 cf->can_id |= ((msg->u.rx_can.msg[2] & 0x0f) << 14) |
868 ((msg->u.rx_can.msg[3] & 0xff) << 6) |
869 (msg->u.rx_can.msg[4] & 0x3f);
870 cf->can_id |= CAN_EFF_FLAG;
873 cf->can_dlc = get_can_dlc(msg->u.rx_can.msg[5]);
875 if (msg->u.rx_can.flag & MSG_FLAG_REMOTE_FRAME)
876 cf->can_id |= CAN_RTR_FLAG;
878 memcpy(cf->data, &msg->u.rx_can.msg[6],
885 stats->rx_bytes += cf->can_dlc;
888 static void kvaser_usb_start_chip_reply(const struct kvaser_usb *dev,
889 const struct kvaser_msg *msg)
891 struct kvaser_usb_net_priv *priv;
892 u8 channel = msg->u.simple.channel;
894 if (channel >= dev->nchannels) {
895 dev_err(dev->udev->dev.parent,
896 "Invalid channel number (%d)\n", channel);
900 priv = dev->nets[channel];
902 if (completion_done(&priv->start_comp) &&
903 netif_queue_stopped(priv->netdev)) {
904 netif_wake_queue(priv->netdev);
906 netif_start_queue(priv->netdev);
907 complete(&priv->start_comp);
911 static void kvaser_usb_stop_chip_reply(const struct kvaser_usb *dev,
912 const struct kvaser_msg *msg)
914 struct kvaser_usb_net_priv *priv;
915 u8 channel = msg->u.simple.channel;
917 if (channel >= dev->nchannels) {
918 dev_err(dev->udev->dev.parent,
919 "Invalid channel number (%d)\n", channel);
923 priv = dev->nets[channel];
925 complete(&priv->stop_comp);
928 static void kvaser_usb_handle_message(const struct kvaser_usb *dev,
929 const struct kvaser_msg *msg)
932 case CMD_START_CHIP_REPLY:
933 kvaser_usb_start_chip_reply(dev, msg);
936 case CMD_STOP_CHIP_REPLY:
937 kvaser_usb_stop_chip_reply(dev, msg);
940 case CMD_RX_STD_MESSAGE:
941 case CMD_RX_EXT_MESSAGE:
942 case CMD_LOG_MESSAGE:
943 kvaser_usb_rx_can_msg(dev, msg);
946 case CMD_CHIP_STATE_EVENT:
947 case CMD_CAN_ERROR_EVENT:
948 kvaser_usb_rx_error(dev, msg);
951 case CMD_TX_ACKNOWLEDGE:
952 kvaser_usb_tx_acknowledge(dev, msg);
956 dev_warn(dev->udev->dev.parent,
957 "Unhandled message (%d)\n", msg->id);
962 static void kvaser_usb_read_bulk_callback(struct urb *urb)
964 struct kvaser_usb *dev = urb->context;
965 struct kvaser_msg *msg;
969 switch (urb->status) {
976 dev_info(dev->udev->dev.parent, "Rx URB aborted (%d)\n",
981 while (pos <= urb->actual_length - MSG_HEADER_LEN) {
982 msg = urb->transfer_buffer + pos;
987 if (pos + msg->len > urb->actual_length) {
988 dev_err(dev->udev->dev.parent, "Format error\n");
992 kvaser_usb_handle_message(dev, msg);
998 usb_fill_bulk_urb(urb, dev->udev,
999 usb_rcvbulkpipe(dev->udev,
1000 dev->bulk_in->bEndpointAddress),
1001 urb->transfer_buffer, RX_BUFFER_SIZE,
1002 kvaser_usb_read_bulk_callback, dev);
1004 err = usb_submit_urb(urb, GFP_ATOMIC);
1005 if (err == -ENODEV) {
1006 for (i = 0; i < dev->nchannels; i++) {
1010 netif_device_detach(dev->nets[i]->netdev);
1013 dev_err(dev->udev->dev.parent,
1014 "Failed resubmitting read bulk urb: %d\n", err);
1020 static int kvaser_usb_setup_rx_urbs(struct kvaser_usb *dev)
1024 if (dev->rxinitdone)
1027 for (i = 0; i < MAX_RX_URBS; i++) {
1028 struct urb *urb = NULL;
1032 urb = usb_alloc_urb(0, GFP_KERNEL);
1034 dev_warn(dev->udev->dev.parent,
1035 "No memory left for URBs\n");
1040 buf = usb_alloc_coherent(dev->udev, RX_BUFFER_SIZE,
1041 GFP_KERNEL, &buf_dma);
1043 dev_warn(dev->udev->dev.parent,
1044 "No memory left for USB buffer\n");
1050 usb_fill_bulk_urb(urb, dev->udev,
1051 usb_rcvbulkpipe(dev->udev,
1052 dev->bulk_in->bEndpointAddress),
1053 buf, RX_BUFFER_SIZE,
1054 kvaser_usb_read_bulk_callback,
1056 urb->transfer_dma = buf_dma;
1057 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1058 usb_anchor_urb(urb, &dev->rx_submitted);
1060 err = usb_submit_urb(urb, GFP_KERNEL);
1062 usb_unanchor_urb(urb);
1063 usb_free_coherent(dev->udev, RX_BUFFER_SIZE, buf,
1069 dev->rxbuf[i] = buf;
1070 dev->rxbuf_dma[i] = buf_dma;
1076 dev_warn(dev->udev->dev.parent,
1077 "Cannot setup read URBs, error %d\n", err);
1079 } else if (i < MAX_RX_URBS) {
1080 dev_warn(dev->udev->dev.parent,
1081 "RX performances may be slow\n");
1084 dev->rxinitdone = true;
1089 static int kvaser_usb_set_opt_mode(const struct kvaser_usb_net_priv *priv)
1091 struct kvaser_msg *msg;
1094 msg = kmalloc(sizeof(*msg), GFP_KERNEL);
1098 msg->id = CMD_SET_CTRL_MODE;
1099 msg->len = MSG_HEADER_LEN + sizeof(struct kvaser_msg_ctrl_mode);
1100 msg->u.ctrl_mode.tid = 0xff;
1101 msg->u.ctrl_mode.channel = priv->channel;
1103 if (priv->can.ctrlmode & CAN_CTRLMODE_LISTENONLY)
1104 msg->u.ctrl_mode.ctrl_mode = KVASER_CTRL_MODE_SILENT;
1106 msg->u.ctrl_mode.ctrl_mode = KVASER_CTRL_MODE_NORMAL;
1108 rc = kvaser_usb_send_msg(priv->dev, msg);
1114 static int kvaser_usb_start_chip(struct kvaser_usb_net_priv *priv)
1118 init_completion(&priv->start_comp);
1120 err = kvaser_usb_send_simple_msg(priv->dev, CMD_START_CHIP,
1125 if (!wait_for_completion_timeout(&priv->start_comp,
1126 msecs_to_jiffies(START_TIMEOUT)))
1132 static int kvaser_usb_open(struct net_device *netdev)
1134 struct kvaser_usb_net_priv *priv = netdev_priv(netdev);
1135 struct kvaser_usb *dev = priv->dev;
1138 err = open_candev(netdev);
1142 err = kvaser_usb_setup_rx_urbs(dev);
1146 err = kvaser_usb_set_opt_mode(priv);
1150 err = kvaser_usb_start_chip(priv);
1152 netdev_warn(netdev, "Cannot start device, error %d\n", err);
1156 priv->can.state = CAN_STATE_ERROR_ACTIVE;
1161 close_candev(netdev);
1165 static void kvaser_usb_unlink_all_urbs(struct kvaser_usb *dev)
1169 usb_kill_anchored_urbs(&dev->rx_submitted);
1171 for (i = 0; i < MAX_RX_URBS; i++)
1172 usb_free_coherent(dev->udev, RX_BUFFER_SIZE,
1176 for (i = 0; i < MAX_NET_DEVICES; i++) {
1177 struct kvaser_usb_net_priv *priv = dev->nets[i];
1180 kvaser_usb_unlink_tx_urbs(priv);
1184 static int kvaser_usb_stop_chip(struct kvaser_usb_net_priv *priv)
1188 init_completion(&priv->stop_comp);
1190 err = kvaser_usb_send_simple_msg(priv->dev, CMD_STOP_CHIP,
1195 if (!wait_for_completion_timeout(&priv->stop_comp,
1196 msecs_to_jiffies(STOP_TIMEOUT)))
1202 static int kvaser_usb_flush_queue(struct kvaser_usb_net_priv *priv)
1204 struct kvaser_msg *msg;
1207 msg = kmalloc(sizeof(*msg), GFP_KERNEL);
1211 msg->id = CMD_FLUSH_QUEUE;
1212 msg->len = MSG_HEADER_LEN + sizeof(struct kvaser_msg_flush_queue);
1213 msg->u.flush_queue.channel = priv->channel;
1214 msg->u.flush_queue.flags = 0x00;
1216 rc = kvaser_usb_send_msg(priv->dev, msg);
1222 static int kvaser_usb_close(struct net_device *netdev)
1224 struct kvaser_usb_net_priv *priv = netdev_priv(netdev);
1225 struct kvaser_usb *dev = priv->dev;
1228 netif_stop_queue(netdev);
1230 err = kvaser_usb_flush_queue(priv);
1232 netdev_warn(netdev, "Cannot flush queue, error %d\n", err);
1234 if (kvaser_usb_send_simple_msg(dev, CMD_RESET_CHIP, priv->channel))
1235 netdev_warn(netdev, "Cannot reset card, error %d\n", err);
1237 err = kvaser_usb_stop_chip(priv);
1239 netdev_warn(netdev, "Cannot stop device, error %d\n", err);
1241 priv->can.state = CAN_STATE_STOPPED;
1242 close_candev(priv->netdev);
1247 static void kvaser_usb_write_bulk_callback(struct urb *urb)
1249 struct kvaser_usb_tx_urb_context *context = urb->context;
1250 struct kvaser_usb_net_priv *priv;
1251 struct net_device *netdev;
1253 if (WARN_ON(!context))
1256 priv = context->priv;
1257 netdev = priv->netdev;
1259 kfree(urb->transfer_buffer);
1261 if (!netif_device_present(netdev))
1265 netdev_info(netdev, "Tx URB aborted (%d)\n", urb->status);
1268 static netdev_tx_t kvaser_usb_start_xmit(struct sk_buff *skb,
1269 struct net_device *netdev)
1271 struct kvaser_usb_net_priv *priv = netdev_priv(netdev);
1272 struct kvaser_usb *dev = priv->dev;
1273 struct net_device_stats *stats = &netdev->stats;
1274 struct can_frame *cf = (struct can_frame *)skb->data;
1275 struct kvaser_usb_tx_urb_context *context = NULL;
1278 struct kvaser_msg *msg;
1280 int ret = NETDEV_TX_OK;
1282 if (can_dropped_invalid_skb(netdev, skb))
1283 return NETDEV_TX_OK;
1285 urb = usb_alloc_urb(0, GFP_ATOMIC);
1287 netdev_err(netdev, "No memory left for URBs\n");
1288 stats->tx_dropped++;
1290 return NETDEV_TX_OK;
1293 buf = kmalloc(sizeof(struct kvaser_msg), GFP_ATOMIC);
1295 stats->tx_dropped++;
1301 msg->len = MSG_HEADER_LEN + sizeof(struct kvaser_msg_tx_can);
1302 msg->u.tx_can.flags = 0;
1303 msg->u.tx_can.channel = priv->channel;
1305 if (cf->can_id & CAN_EFF_FLAG) {
1306 msg->id = CMD_TX_EXT_MESSAGE;
1307 msg->u.tx_can.msg[0] = (cf->can_id >> 24) & 0x1f;
1308 msg->u.tx_can.msg[1] = (cf->can_id >> 18) & 0x3f;
1309 msg->u.tx_can.msg[2] = (cf->can_id >> 14) & 0x0f;
1310 msg->u.tx_can.msg[3] = (cf->can_id >> 6) & 0xff;
1311 msg->u.tx_can.msg[4] = cf->can_id & 0x3f;
1313 msg->id = CMD_TX_STD_MESSAGE;
1314 msg->u.tx_can.msg[0] = (cf->can_id >> 6) & 0x1f;
1315 msg->u.tx_can.msg[1] = cf->can_id & 0x3f;
1318 msg->u.tx_can.msg[5] = cf->can_dlc;
1319 memcpy(&msg->u.tx_can.msg[6], cf->data, cf->can_dlc);
1321 if (cf->can_id & CAN_RTR_FLAG)
1322 msg->u.tx_can.flags |= MSG_FLAG_REMOTE_FRAME;
1324 for (i = 0; i < ARRAY_SIZE(priv->tx_contexts); i++) {
1325 if (priv->tx_contexts[i].echo_index == MAX_TX_URBS) {
1326 context = &priv->tx_contexts[i];
1331 /* This should never happen; it implies a flow control bug */
1333 netdev_warn(netdev, "cannot find free context\n");
1334 ret = NETDEV_TX_BUSY;
1338 context->priv = priv;
1339 context->echo_index = i;
1340 context->dlc = cf->can_dlc;
1342 msg->u.tx_can.tid = context->echo_index;
1344 usb_fill_bulk_urb(urb, dev->udev,
1345 usb_sndbulkpipe(dev->udev,
1346 dev->bulk_out->bEndpointAddress),
1348 kvaser_usb_write_bulk_callback, context);
1349 usb_anchor_urb(urb, &priv->tx_submitted);
1351 can_put_echo_skb(skb, netdev, context->echo_index);
1353 atomic_inc(&priv->active_tx_urbs);
1355 if (atomic_read(&priv->active_tx_urbs) >= MAX_TX_URBS)
1356 netif_stop_queue(netdev);
1358 err = usb_submit_urb(urb, GFP_ATOMIC);
1359 if (unlikely(err)) {
1360 can_free_echo_skb(netdev, context->echo_index);
1362 atomic_dec(&priv->active_tx_urbs);
1363 usb_unanchor_urb(urb);
1365 stats->tx_dropped++;
1368 netif_device_detach(netdev);
1370 netdev_warn(netdev, "Failed tx_urb %d\n", err);
1377 return NETDEV_TX_OK;
1386 static const struct net_device_ops kvaser_usb_netdev_ops = {
1387 .ndo_open = kvaser_usb_open,
1388 .ndo_stop = kvaser_usb_close,
1389 .ndo_start_xmit = kvaser_usb_start_xmit,
1392 static const struct can_bittiming_const kvaser_usb_bittiming_const = {
1393 .name = "kvaser_usb",
1394 .tseg1_min = KVASER_USB_TSEG1_MIN,
1395 .tseg1_max = KVASER_USB_TSEG1_MAX,
1396 .tseg2_min = KVASER_USB_TSEG2_MIN,
1397 .tseg2_max = KVASER_USB_TSEG2_MAX,
1398 .sjw_max = KVASER_USB_SJW_MAX,
1399 .brp_min = KVASER_USB_BRP_MIN,
1400 .brp_max = KVASER_USB_BRP_MAX,
1401 .brp_inc = KVASER_USB_BRP_INC,
1404 static int kvaser_usb_set_bittiming(struct net_device *netdev)
1406 struct kvaser_usb_net_priv *priv = netdev_priv(netdev);
1407 struct can_bittiming *bt = &priv->can.bittiming;
1408 struct kvaser_usb *dev = priv->dev;
1409 struct kvaser_msg *msg;
1412 msg = kmalloc(sizeof(*msg), GFP_KERNEL);
1416 msg->id = CMD_SET_BUS_PARAMS;
1417 msg->len = MSG_HEADER_LEN + sizeof(struct kvaser_msg_busparams);
1418 msg->u.busparams.channel = priv->channel;
1419 msg->u.busparams.tid = 0xff;
1420 msg->u.busparams.bitrate = cpu_to_le32(bt->bitrate);
1421 msg->u.busparams.sjw = bt->sjw;
1422 msg->u.busparams.tseg1 = bt->prop_seg + bt->phase_seg1;
1423 msg->u.busparams.tseg2 = bt->phase_seg2;
1425 if (priv->can.ctrlmode & CAN_CTRLMODE_3_SAMPLES)
1426 msg->u.busparams.no_samp = 3;
1428 msg->u.busparams.no_samp = 1;
1430 rc = kvaser_usb_send_msg(dev, msg);
1436 static int kvaser_usb_set_mode(struct net_device *netdev,
1439 struct kvaser_usb_net_priv *priv = netdev_priv(netdev);
1443 case CAN_MODE_START:
1444 err = kvaser_usb_simple_msg_async(priv, CMD_START_CHIP);
1455 static int kvaser_usb_get_berr_counter(const struct net_device *netdev,
1456 struct can_berr_counter *bec)
1458 struct kvaser_usb_net_priv *priv = netdev_priv(netdev);
1465 static void kvaser_usb_remove_interfaces(struct kvaser_usb *dev)
1469 for (i = 0; i < dev->nchannels; i++) {
1473 unregister_netdev(dev->nets[i]->netdev);
1476 kvaser_usb_unlink_all_urbs(dev);
1478 for (i = 0; i < dev->nchannels; i++) {
1482 free_candev(dev->nets[i]->netdev);
1486 static int kvaser_usb_init_one(struct usb_interface *intf,
1487 const struct usb_device_id *id, int channel)
1489 struct kvaser_usb *dev = usb_get_intfdata(intf);
1490 struct net_device *netdev;
1491 struct kvaser_usb_net_priv *priv;
1494 netdev = alloc_candev(sizeof(*priv), MAX_TX_URBS);
1496 dev_err(&intf->dev, "Cannot alloc candev\n");
1500 priv = netdev_priv(netdev);
1502 init_completion(&priv->start_comp);
1503 init_completion(&priv->stop_comp);
1505 init_usb_anchor(&priv->tx_submitted);
1506 atomic_set(&priv->active_tx_urbs, 0);
1508 for (i = 0; i < ARRAY_SIZE(priv->tx_contexts); i++)
1509 priv->tx_contexts[i].echo_index = MAX_TX_URBS;
1512 priv->netdev = netdev;
1513 priv->channel = channel;
1515 priv->can.state = CAN_STATE_STOPPED;
1516 priv->can.clock.freq = CAN_USB_CLOCK;
1517 priv->can.bittiming_const = &kvaser_usb_bittiming_const;
1518 priv->can.do_set_bittiming = kvaser_usb_set_bittiming;
1519 priv->can.do_set_mode = kvaser_usb_set_mode;
1520 if (id->driver_info & KVASER_HAS_TXRX_ERRORS)
1521 priv->can.do_get_berr_counter = kvaser_usb_get_berr_counter;
1522 priv->can.ctrlmode_supported = CAN_CTRLMODE_3_SAMPLES;
1523 if (id->driver_info & KVASER_HAS_SILENT_MODE)
1524 priv->can.ctrlmode_supported |= CAN_CTRLMODE_LISTENONLY;
1526 netdev->flags |= IFF_ECHO;
1528 netdev->netdev_ops = &kvaser_usb_netdev_ops;
1530 SET_NETDEV_DEV(netdev, &intf->dev);
1532 dev->nets[channel] = priv;
1534 err = register_candev(netdev);
1536 dev_err(&intf->dev, "Failed to register can device\n");
1537 free_candev(netdev);
1538 dev->nets[channel] = NULL;
1542 netdev_dbg(netdev, "device registered\n");
1547 static int kvaser_usb_get_endpoints(const struct usb_interface *intf,
1548 struct usb_endpoint_descriptor **in,
1549 struct usb_endpoint_descriptor **out)
1551 const struct usb_host_interface *iface_desc;
1552 struct usb_endpoint_descriptor *endpoint;
1555 iface_desc = &intf->altsetting[0];
1557 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
1558 endpoint = &iface_desc->endpoint[i].desc;
1560 if (!*in && usb_endpoint_is_bulk_in(endpoint))
1563 if (!*out && usb_endpoint_is_bulk_out(endpoint))
1566 /* use first bulk endpoint for in and out */
1574 static int kvaser_usb_probe(struct usb_interface *intf,
1575 const struct usb_device_id *id)
1577 struct kvaser_usb *dev;
1581 dev = devm_kzalloc(&intf->dev, sizeof(*dev), GFP_KERNEL);
1585 err = kvaser_usb_get_endpoints(intf, &dev->bulk_in, &dev->bulk_out);
1587 dev_err(&intf->dev, "Cannot get usb endpoint(s)");
1591 dev->udev = interface_to_usbdev(intf);
1593 init_usb_anchor(&dev->rx_submitted);
1595 usb_set_intfdata(intf, dev);
1597 for (i = 0; i < MAX_NET_DEVICES; i++)
1598 kvaser_usb_send_simple_msg(dev, CMD_RESET_CHIP, i);
1600 err = kvaser_usb_get_software_info(dev);
1603 "Cannot get software infos, error %d\n", err);
1607 err = kvaser_usb_get_card_info(dev);
1610 "Cannot get card infos, error %d\n", err);
1614 dev_dbg(&intf->dev, "Firmware version: %d.%d.%d\n",
1615 ((dev->fw_version >> 24) & 0xff),
1616 ((dev->fw_version >> 16) & 0xff),
1617 (dev->fw_version & 0xffff));
1619 for (i = 0; i < dev->nchannels; i++) {
1620 err = kvaser_usb_init_one(intf, id, i);
1622 kvaser_usb_remove_interfaces(dev);
1630 static void kvaser_usb_disconnect(struct usb_interface *intf)
1632 struct kvaser_usb *dev = usb_get_intfdata(intf);
1634 usb_set_intfdata(intf, NULL);
1639 kvaser_usb_remove_interfaces(dev);
1642 static struct usb_driver kvaser_usb_driver = {
1643 .name = "kvaser_usb",
1644 .probe = kvaser_usb_probe,
1645 .disconnect = kvaser_usb_disconnect,
1646 .id_table = kvaser_usb_table,
1649 module_usb_driver(kvaser_usb_driver);
1651 MODULE_AUTHOR("Olivier Sobrie <olivier@sobrie.be>");
1652 MODULE_DESCRIPTION("CAN driver for Kvaser CAN/USB devices");
1653 MODULE_LICENSE("GPL v2");