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
9 * - Kvaser linux usbcanII driver (version 5.3)
11 * Copyright (C) 2002-2006 KVASER AB, Sweden. All rights reserved.
12 * Copyright (C) 2010 Matthias Fuchs <matthias.fuchs@esd.eu>, esd gmbh
13 * Copyright (C) 2012 Olivier Sobrie <olivier@sobrie.be>
14 * Copyright (C) 2015 Valeo S.A.
17 #include <linux/spinlock.h>
18 #include <linux/kernel.h>
19 #include <linux/completion.h>
20 #include <linux/module.h>
21 #include <linux/netdevice.h>
22 #include <linux/usb.h>
24 #include <linux/can.h>
25 #include <linux/can/dev.h>
26 #include <linux/can/error.h>
28 #define MAX_TX_URBS 16
30 #define START_TIMEOUT 1000 /* msecs */
31 #define STOP_TIMEOUT 1000 /* msecs */
32 #define USB_SEND_TIMEOUT 1000 /* msecs */
33 #define USB_RECV_TIMEOUT 1000 /* msecs */
34 #define RX_BUFFER_SIZE 3072
35 #define CAN_USB_CLOCK 8000000
36 #define MAX_NET_DEVICES 3
37 #define MAX_USBCAN_NET_DEVICES 2
39 /* Kvaser Leaf USB devices */
40 #define KVASER_VENDOR_ID 0x0bfd
41 #define USB_LEAF_DEVEL_PRODUCT_ID 10
42 #define USB_LEAF_LITE_PRODUCT_ID 11
43 #define USB_LEAF_PRO_PRODUCT_ID 12
44 #define USB_LEAF_SPRO_PRODUCT_ID 14
45 #define USB_LEAF_PRO_LS_PRODUCT_ID 15
46 #define USB_LEAF_PRO_SWC_PRODUCT_ID 16
47 #define USB_LEAF_PRO_LIN_PRODUCT_ID 17
48 #define USB_LEAF_SPRO_LS_PRODUCT_ID 18
49 #define USB_LEAF_SPRO_SWC_PRODUCT_ID 19
50 #define USB_MEMO2_DEVEL_PRODUCT_ID 22
51 #define USB_MEMO2_HSHS_PRODUCT_ID 23
52 #define USB_UPRO_HSHS_PRODUCT_ID 24
53 #define USB_LEAF_LITE_GI_PRODUCT_ID 25
54 #define USB_LEAF_PRO_OBDII_PRODUCT_ID 26
55 #define USB_MEMO2_HSLS_PRODUCT_ID 27
56 #define USB_LEAF_LITE_CH_PRODUCT_ID 28
57 #define USB_BLACKBIRD_SPRO_PRODUCT_ID 29
58 #define USB_OEM_MERCURY_PRODUCT_ID 34
59 #define USB_OEM_LEAF_PRODUCT_ID 35
60 #define USB_CAN_R_PRODUCT_ID 39
61 #define USB_LEAF_LITE_V2_PRODUCT_ID 288
62 #define USB_MINI_PCIE_HS_PRODUCT_ID 289
64 static inline bool kvaser_is_leaf(const struct usb_device_id
*id
)
66 return id
->idProduct
>= USB_LEAF_DEVEL_PRODUCT_ID
&&
67 id
->idProduct
<= USB_MINI_PCIE_HS_PRODUCT_ID
;
70 /* Kvaser USBCan-II devices */
71 #define USB_USBCAN_REVB_PRODUCT_ID 2
72 #define USB_VCI2_PRODUCT_ID 3
73 #define USB_USBCAN2_PRODUCT_ID 4
74 #define USB_MEMORATOR_PRODUCT_ID 5
76 static inline bool kvaser_is_usbcan(const struct usb_device_id
*id
)
78 return id
->idProduct
>= USB_USBCAN_REVB_PRODUCT_ID
&&
79 id
->idProduct
<= USB_MEMORATOR_PRODUCT_ID
;
82 /* USB devices features */
83 #define KVASER_HAS_SILENT_MODE BIT(0)
84 #define KVASER_HAS_TXRX_ERRORS BIT(1)
86 /* Message header size */
87 #define MSG_HEADER_LEN 2
89 /* Can message flags */
90 #define MSG_FLAG_ERROR_FRAME BIT(0)
91 #define MSG_FLAG_OVERRUN BIT(1)
92 #define MSG_FLAG_NERR BIT(2)
93 #define MSG_FLAG_WAKEUP BIT(3)
94 #define MSG_FLAG_REMOTE_FRAME BIT(4)
95 #define MSG_FLAG_RESERVED BIT(5)
96 #define MSG_FLAG_TX_ACK BIT(6)
97 #define MSG_FLAG_TX_REQUEST BIT(7)
99 /* Can states (M16C CxSTRH register) */
100 #define M16C_STATE_BUS_RESET BIT(0)
101 #define M16C_STATE_BUS_ERROR BIT(4)
102 #define M16C_STATE_BUS_PASSIVE BIT(5)
103 #define M16C_STATE_BUS_OFF BIT(6)
106 #define CMD_RX_STD_MESSAGE 12
107 #define CMD_TX_STD_MESSAGE 13
108 #define CMD_RX_EXT_MESSAGE 14
109 #define CMD_TX_EXT_MESSAGE 15
110 #define CMD_SET_BUS_PARAMS 16
111 #define CMD_GET_BUS_PARAMS 17
112 #define CMD_GET_BUS_PARAMS_REPLY 18
113 #define CMD_GET_CHIP_STATE 19
114 #define CMD_CHIP_STATE_EVENT 20
115 #define CMD_SET_CTRL_MODE 21
116 #define CMD_GET_CTRL_MODE 22
117 #define CMD_GET_CTRL_MODE_REPLY 23
118 #define CMD_RESET_CHIP 24
119 #define CMD_RESET_CARD 25
120 #define CMD_START_CHIP 26
121 #define CMD_START_CHIP_REPLY 27
122 #define CMD_STOP_CHIP 28
123 #define CMD_STOP_CHIP_REPLY 29
125 #define CMD_LEAF_GET_CARD_INFO2 32
126 #define CMD_USBCAN_RESET_CLOCK 32
127 #define CMD_USBCAN_CLOCK_OVERFLOW_EVENT 33
129 #define CMD_GET_CARD_INFO 34
130 #define CMD_GET_CARD_INFO_REPLY 35
131 #define CMD_GET_SOFTWARE_INFO 38
132 #define CMD_GET_SOFTWARE_INFO_REPLY 39
133 #define CMD_ERROR_EVENT 45
134 #define CMD_FLUSH_QUEUE 48
135 #define CMD_RESET_ERROR_COUNTER 49
136 #define CMD_TX_ACKNOWLEDGE 50
137 #define CMD_CAN_ERROR_EVENT 51
139 #define CMD_LEAF_USB_THROTTLE 77
140 #define CMD_LEAF_LOG_MESSAGE 106
143 #define M16C_EF_ACKE BIT(0)
144 #define M16C_EF_CRCE BIT(1)
145 #define M16C_EF_FORME BIT(2)
146 #define M16C_EF_STFE BIT(3)
147 #define M16C_EF_BITE0 BIT(4)
148 #define M16C_EF_BITE1 BIT(5)
149 #define M16C_EF_RCVE BIT(6)
150 #define M16C_EF_TRE BIT(7)
152 /* Only Leaf-based devices can report M16C error factors,
153 * thus define our own error status flags for USBCANII
155 #define USBCAN_ERROR_STATE_NONE 0
156 #define USBCAN_ERROR_STATE_TX_ERROR BIT(0)
157 #define USBCAN_ERROR_STATE_RX_ERROR BIT(1)
158 #define USBCAN_ERROR_STATE_BUSERROR BIT(2)
160 /* bittiming parameters */
161 #define KVASER_USB_TSEG1_MIN 1
162 #define KVASER_USB_TSEG1_MAX 16
163 #define KVASER_USB_TSEG2_MIN 1
164 #define KVASER_USB_TSEG2_MAX 8
165 #define KVASER_USB_SJW_MAX 4
166 #define KVASER_USB_BRP_MIN 1
167 #define KVASER_USB_BRP_MAX 64
168 #define KVASER_USB_BRP_INC 1
171 #define KVASER_CTRL_MODE_NORMAL 1
172 #define KVASER_CTRL_MODE_SILENT 2
173 #define KVASER_CTRL_MODE_SELFRECEPTION 3
174 #define KVASER_CTRL_MODE_OFF 4
176 /* Extended CAN identifier flag */
177 #define KVASER_EXTENDED_FRAME BIT(31)
179 /* Kvaser USB CAN dongles are divided into two major families:
180 * - Leaf: Based on Renesas M32C, running firmware labeled as 'filo'
181 * - UsbcanII: Based on Renesas M16C, running firmware labeled as 'helios'
183 enum kvaser_usb_family
{
188 struct kvaser_msg_simple
{
193 struct kvaser_msg_cardinfo
{
198 __le32 serial_number
;
202 __le32 serial_number_low
;
203 __le32 serial_number_high
;
206 __le32 clock_resolution
;
221 struct kvaser_msg_cardinfo2
{
225 __le32 oem_unlock_code
;
228 struct leaf_msg_softinfo
{
233 __le16 max_outstanding_tx
;
237 struct usbcan_msg_softinfo
{
240 __le16 max_outstanding_tx
;
247 struct kvaser_msg_busparams
{
257 struct kvaser_msg_tx_can
{
273 struct kvaser_msg_rx_can_header
{
278 struct leaf_msg_rx_can
{
286 struct usbcan_msg_rx_can
{
294 struct leaf_msg_chip_state_event
{
306 struct usbcan_msg_chip_state_event
{
318 struct kvaser_msg_tx_acknowledge_header
{
323 struct leaf_msg_tx_acknowledge
{
332 struct usbcan_msg_tx_acknowledge
{
340 struct leaf_msg_error_event
{
352 struct usbcan_msg_error_event
{
355 u8 tx_errors_count_ch0
;
356 u8 rx_errors_count_ch0
;
357 u8 tx_errors_count_ch1
;
358 u8 rx_errors_count_ch1
;
364 struct kvaser_msg_ctrl_mode
{
371 struct kvaser_msg_flush_queue
{
378 struct leaf_msg_log_message
{
392 struct kvaser_msg_simple simple
;
393 struct kvaser_msg_cardinfo cardinfo
;
394 struct kvaser_msg_cardinfo2 cardinfo2
;
395 struct kvaser_msg_busparams busparams
;
397 struct kvaser_msg_rx_can_header rx_can_header
;
398 struct kvaser_msg_tx_acknowledge_header tx_acknowledge_header
;
401 struct leaf_msg_softinfo softinfo
;
402 struct leaf_msg_rx_can rx_can
;
403 struct leaf_msg_chip_state_event chip_state_event
;
404 struct leaf_msg_tx_acknowledge tx_acknowledge
;
405 struct leaf_msg_error_event error_event
;
406 struct leaf_msg_log_message log_message
;
410 struct usbcan_msg_softinfo softinfo
;
411 struct usbcan_msg_rx_can rx_can
;
412 struct usbcan_msg_chip_state_event chip_state_event
;
413 struct usbcan_msg_tx_acknowledge tx_acknowledge
;
414 struct usbcan_msg_error_event error_event
;
417 struct kvaser_msg_tx_can tx_can
;
418 struct kvaser_msg_ctrl_mode ctrl_mode
;
419 struct kvaser_msg_flush_queue flush_queue
;
423 /* Summary of a kvaser error event, for a unified Leaf/Usbcan error
424 * handling. Some discrepancies between the two families exist:
426 * - USBCAN firmware does not report M16C "error factors"
427 * - USBCAN controllers has difficulties reporting if the raised error
428 * event is for ch0 or ch1. They leave such arbitration to the OS
429 * driver by letting it compare error counters with previous values
430 * and decide the error event's channel. Thus for USBCAN, the channel
431 * field is only advisory.
433 struct kvaser_usb_error_summary
{
434 u8 channel
, status
, txerr
, rxerr
;
446 struct kvaser_usb_tx_urb_context
{
447 struct kvaser_usb_net_priv
*priv
;
453 struct usb_device
*udev
;
454 struct kvaser_usb_net_priv
*nets
[MAX_NET_DEVICES
];
456 struct usb_endpoint_descriptor
*bulk_in
, *bulk_out
;
457 struct usb_anchor rx_submitted
;
460 unsigned int nchannels
;
461 enum kvaser_usb_family family
;
464 void *rxbuf
[MAX_RX_URBS
];
465 dma_addr_t rxbuf_dma
[MAX_RX_URBS
];
468 struct kvaser_usb_net_priv
{
471 spinlock_t tx_contexts_lock
;
472 int active_tx_contexts
;
473 struct kvaser_usb_tx_urb_context tx_contexts
[MAX_TX_URBS
];
475 struct usb_anchor tx_submitted
;
476 struct completion start_comp
, stop_comp
;
478 struct kvaser_usb
*dev
;
479 struct net_device
*netdev
;
482 struct can_berr_counter bec
;
485 static const struct usb_device_id kvaser_usb_table
[] = {
486 /* Leaf family IDs */
487 { USB_DEVICE(KVASER_VENDOR_ID
, USB_LEAF_DEVEL_PRODUCT_ID
) },
488 { USB_DEVICE(KVASER_VENDOR_ID
, USB_LEAF_LITE_PRODUCT_ID
) },
489 { USB_DEVICE(KVASER_VENDOR_ID
, USB_LEAF_PRO_PRODUCT_ID
),
490 .driver_info
= KVASER_HAS_TXRX_ERRORS
|
491 KVASER_HAS_SILENT_MODE
},
492 { USB_DEVICE(KVASER_VENDOR_ID
, USB_LEAF_SPRO_PRODUCT_ID
),
493 .driver_info
= KVASER_HAS_TXRX_ERRORS
|
494 KVASER_HAS_SILENT_MODE
},
495 { USB_DEVICE(KVASER_VENDOR_ID
, USB_LEAF_PRO_LS_PRODUCT_ID
),
496 .driver_info
= KVASER_HAS_TXRX_ERRORS
|
497 KVASER_HAS_SILENT_MODE
},
498 { USB_DEVICE(KVASER_VENDOR_ID
, USB_LEAF_PRO_SWC_PRODUCT_ID
),
499 .driver_info
= KVASER_HAS_TXRX_ERRORS
|
500 KVASER_HAS_SILENT_MODE
},
501 { USB_DEVICE(KVASER_VENDOR_ID
, USB_LEAF_PRO_LIN_PRODUCT_ID
),
502 .driver_info
= KVASER_HAS_TXRX_ERRORS
|
503 KVASER_HAS_SILENT_MODE
},
504 { USB_DEVICE(KVASER_VENDOR_ID
, USB_LEAF_SPRO_LS_PRODUCT_ID
),
505 .driver_info
= KVASER_HAS_TXRX_ERRORS
|
506 KVASER_HAS_SILENT_MODE
},
507 { USB_DEVICE(KVASER_VENDOR_ID
, USB_LEAF_SPRO_SWC_PRODUCT_ID
),
508 .driver_info
= KVASER_HAS_TXRX_ERRORS
|
509 KVASER_HAS_SILENT_MODE
},
510 { USB_DEVICE(KVASER_VENDOR_ID
, USB_MEMO2_DEVEL_PRODUCT_ID
),
511 .driver_info
= KVASER_HAS_TXRX_ERRORS
|
512 KVASER_HAS_SILENT_MODE
},
513 { USB_DEVICE(KVASER_VENDOR_ID
, USB_MEMO2_HSHS_PRODUCT_ID
),
514 .driver_info
= KVASER_HAS_TXRX_ERRORS
|
515 KVASER_HAS_SILENT_MODE
},
516 { USB_DEVICE(KVASER_VENDOR_ID
, USB_UPRO_HSHS_PRODUCT_ID
),
517 .driver_info
= KVASER_HAS_TXRX_ERRORS
},
518 { USB_DEVICE(KVASER_VENDOR_ID
, USB_LEAF_LITE_GI_PRODUCT_ID
) },
519 { USB_DEVICE(KVASER_VENDOR_ID
, USB_LEAF_PRO_OBDII_PRODUCT_ID
),
520 .driver_info
= KVASER_HAS_TXRX_ERRORS
|
521 KVASER_HAS_SILENT_MODE
},
522 { USB_DEVICE(KVASER_VENDOR_ID
, USB_MEMO2_HSLS_PRODUCT_ID
),
523 .driver_info
= KVASER_HAS_TXRX_ERRORS
},
524 { USB_DEVICE(KVASER_VENDOR_ID
, USB_LEAF_LITE_CH_PRODUCT_ID
),
525 .driver_info
= KVASER_HAS_TXRX_ERRORS
},
526 { USB_DEVICE(KVASER_VENDOR_ID
, USB_BLACKBIRD_SPRO_PRODUCT_ID
),
527 .driver_info
= KVASER_HAS_TXRX_ERRORS
},
528 { USB_DEVICE(KVASER_VENDOR_ID
, USB_OEM_MERCURY_PRODUCT_ID
),
529 .driver_info
= KVASER_HAS_TXRX_ERRORS
},
530 { USB_DEVICE(KVASER_VENDOR_ID
, USB_OEM_LEAF_PRODUCT_ID
),
531 .driver_info
= KVASER_HAS_TXRX_ERRORS
},
532 { USB_DEVICE(KVASER_VENDOR_ID
, USB_CAN_R_PRODUCT_ID
),
533 .driver_info
= KVASER_HAS_TXRX_ERRORS
},
534 { USB_DEVICE(KVASER_VENDOR_ID
, USB_LEAF_LITE_V2_PRODUCT_ID
) },
535 { USB_DEVICE(KVASER_VENDOR_ID
, USB_MINI_PCIE_HS_PRODUCT_ID
) },
537 /* USBCANII family IDs */
538 { USB_DEVICE(KVASER_VENDOR_ID
, USB_USBCAN2_PRODUCT_ID
),
539 .driver_info
= KVASER_HAS_TXRX_ERRORS
},
540 { USB_DEVICE(KVASER_VENDOR_ID
, USB_USBCAN_REVB_PRODUCT_ID
),
541 .driver_info
= KVASER_HAS_TXRX_ERRORS
},
542 { USB_DEVICE(KVASER_VENDOR_ID
, USB_MEMORATOR_PRODUCT_ID
),
543 .driver_info
= KVASER_HAS_TXRX_ERRORS
},
544 { USB_DEVICE(KVASER_VENDOR_ID
, USB_VCI2_PRODUCT_ID
),
545 .driver_info
= KVASER_HAS_TXRX_ERRORS
},
549 MODULE_DEVICE_TABLE(usb
, kvaser_usb_table
);
551 static inline int kvaser_usb_send_msg(const struct kvaser_usb
*dev
,
552 struct kvaser_msg
*msg
)
556 return usb_bulk_msg(dev
->udev
,
557 usb_sndbulkpipe(dev
->udev
,
558 dev
->bulk_out
->bEndpointAddress
),
559 msg
, msg
->len
, &actual_len
,
563 static int kvaser_usb_wait_msg(const struct kvaser_usb
*dev
, u8 id
,
564 struct kvaser_msg
*msg
)
566 struct kvaser_msg
*tmp
;
571 unsigned long to
= jiffies
+ msecs_to_jiffies(USB_RECV_TIMEOUT
);
573 buf
= kzalloc(RX_BUFFER_SIZE
, GFP_KERNEL
);
578 err
= usb_bulk_msg(dev
->udev
,
579 usb_rcvbulkpipe(dev
->udev
,
580 dev
->bulk_in
->bEndpointAddress
),
581 buf
, RX_BUFFER_SIZE
, &actual_len
,
587 while (pos
<= actual_len
- MSG_HEADER_LEN
) {
590 /* Handle messages crossing the USB endpoint max packet
591 * size boundary. Check kvaser_usb_read_bulk_callback()
592 * for further details.
596 dev
->bulk_in
->wMaxPacketSize
);
600 if (pos
+ tmp
->len
> actual_len
) {
601 dev_err(dev
->udev
->dev
.parent
,
607 memcpy(msg
, tmp
, tmp
->len
);
613 } while (time_before(jiffies
, to
));
623 static int kvaser_usb_send_simple_msg(const struct kvaser_usb
*dev
,
624 u8 msg_id
, int channel
)
626 struct kvaser_msg
*msg
;
629 msg
= kmalloc(sizeof(*msg
), GFP_KERNEL
);
634 msg
->len
= MSG_HEADER_LEN
+ sizeof(struct kvaser_msg_simple
);
635 msg
->u
.simple
.channel
= channel
;
636 msg
->u
.simple
.tid
= 0xff;
638 rc
= kvaser_usb_send_msg(dev
, msg
);
644 static int kvaser_usb_get_software_info(struct kvaser_usb
*dev
)
646 struct kvaser_msg msg
;
649 err
= kvaser_usb_send_simple_msg(dev
, CMD_GET_SOFTWARE_INFO
, 0);
653 err
= kvaser_usb_wait_msg(dev
, CMD_GET_SOFTWARE_INFO_REPLY
, &msg
);
657 switch (dev
->family
) {
659 dev
->fw_version
= le32_to_cpu(msg
.u
.leaf
.softinfo
.fw_version
);
662 dev
->fw_version
= le32_to_cpu(msg
.u
.usbcan
.softinfo
.fw_version
);
669 static int kvaser_usb_get_card_info(struct kvaser_usb
*dev
)
671 struct kvaser_msg msg
;
674 err
= kvaser_usb_send_simple_msg(dev
, CMD_GET_CARD_INFO
, 0);
678 err
= kvaser_usb_wait_msg(dev
, CMD_GET_CARD_INFO_REPLY
, &msg
);
682 dev
->nchannels
= msg
.u
.cardinfo
.nchannels
;
683 if ((dev
->nchannels
> MAX_NET_DEVICES
) ||
684 (dev
->family
== KVASER_USBCAN
&&
685 dev
->nchannels
> MAX_USBCAN_NET_DEVICES
))
691 static void kvaser_usb_tx_acknowledge(const struct kvaser_usb
*dev
,
692 const struct kvaser_msg
*msg
)
694 struct net_device_stats
*stats
;
695 struct kvaser_usb_tx_urb_context
*context
;
696 struct kvaser_usb_net_priv
*priv
;
698 struct can_frame
*cf
;
702 channel
= msg
->u
.tx_acknowledge_header
.channel
;
703 tid
= msg
->u
.tx_acknowledge_header
.tid
;
705 if (channel
>= dev
->nchannels
) {
706 dev_err(dev
->udev
->dev
.parent
,
707 "Invalid channel number (%d)\n", channel
);
711 priv
= dev
->nets
[channel
];
713 if (!netif_device_present(priv
->netdev
))
716 stats
= &priv
->netdev
->stats
;
718 context
= &priv
->tx_contexts
[tid
% MAX_TX_URBS
];
720 /* Sometimes the state change doesn't come after a bus-off event */
721 if (priv
->can
.restart_ms
&&
722 (priv
->can
.state
>= CAN_STATE_BUS_OFF
)) {
723 skb
= alloc_can_err_skb(priv
->netdev
, &cf
);
725 cf
->can_id
|= CAN_ERR_RESTARTED
;
728 stats
->rx_bytes
+= cf
->can_dlc
;
731 netdev_err(priv
->netdev
,
732 "No memory left for err_skb\n");
735 priv
->can
.can_stats
.restarts
++;
736 netif_carrier_on(priv
->netdev
);
738 priv
->can
.state
= CAN_STATE_ERROR_ACTIVE
;
742 stats
->tx_bytes
+= context
->dlc
;
744 spin_lock_irqsave(&priv
->tx_contexts_lock
, flags
);
746 can_get_echo_skb(priv
->netdev
, context
->echo_index
);
747 context
->echo_index
= MAX_TX_URBS
;
748 --priv
->active_tx_contexts
;
749 netif_wake_queue(priv
->netdev
);
751 spin_unlock_irqrestore(&priv
->tx_contexts_lock
, flags
);
754 static void kvaser_usb_simple_msg_callback(struct urb
*urb
)
756 struct net_device
*netdev
= urb
->context
;
758 kfree(urb
->transfer_buffer
);
761 netdev_warn(netdev
, "urb status received: %d\n",
765 static int kvaser_usb_simple_msg_async(struct kvaser_usb_net_priv
*priv
,
768 struct kvaser_usb
*dev
= priv
->dev
;
769 struct net_device
*netdev
= priv
->netdev
;
770 struct kvaser_msg
*msg
;
775 urb
= usb_alloc_urb(0, GFP_ATOMIC
);
777 netdev_err(netdev
, "No memory left for URBs\n");
781 buf
= kmalloc(sizeof(struct kvaser_msg
), GFP_ATOMIC
);
787 msg
= (struct kvaser_msg
*)buf
;
788 msg
->len
= MSG_HEADER_LEN
+ sizeof(struct kvaser_msg_simple
);
790 msg
->u
.simple
.channel
= priv
->channel
;
792 usb_fill_bulk_urb(urb
, dev
->udev
,
793 usb_sndbulkpipe(dev
->udev
,
794 dev
->bulk_out
->bEndpointAddress
),
796 kvaser_usb_simple_msg_callback
, netdev
);
797 usb_anchor_urb(urb
, &priv
->tx_submitted
);
799 err
= usb_submit_urb(urb
, GFP_ATOMIC
);
801 netdev_err(netdev
, "Error transmitting URB\n");
802 usb_unanchor_urb(urb
);
812 static void kvaser_usb_rx_error_update_can_state(struct kvaser_usb_net_priv
*priv
,
813 const struct kvaser_usb_error_summary
*es
,
814 struct can_frame
*cf
)
816 struct kvaser_usb
*dev
= priv
->dev
;
817 struct net_device_stats
*stats
= &priv
->netdev
->stats
;
818 enum can_state cur_state
, new_state
, tx_state
, rx_state
;
820 netdev_dbg(priv
->netdev
, "Error status: 0x%02x\n", es
->status
);
822 new_state
= cur_state
= priv
->can
.state
;
824 if (es
->status
& (M16C_STATE_BUS_OFF
| M16C_STATE_BUS_RESET
))
825 new_state
= CAN_STATE_BUS_OFF
;
826 else if (es
->status
& M16C_STATE_BUS_PASSIVE
)
827 new_state
= CAN_STATE_ERROR_PASSIVE
;
828 else if (es
->status
& M16C_STATE_BUS_ERROR
) {
829 /* Guard against spurious error events after a busoff */
830 if (cur_state
< CAN_STATE_BUS_OFF
) {
831 if ((es
->txerr
>= 128) || (es
->rxerr
>= 128))
832 new_state
= CAN_STATE_ERROR_PASSIVE
;
833 else if ((es
->txerr
>= 96) || (es
->rxerr
>= 96))
834 new_state
= CAN_STATE_ERROR_WARNING
;
835 else if (cur_state
> CAN_STATE_ERROR_ACTIVE
)
836 new_state
= CAN_STATE_ERROR_ACTIVE
;
841 new_state
= CAN_STATE_ERROR_ACTIVE
;
843 if (new_state
!= cur_state
) {
844 tx_state
= (es
->txerr
>= es
->rxerr
) ? new_state
: 0;
845 rx_state
= (es
->txerr
<= es
->rxerr
) ? new_state
: 0;
847 can_change_state(priv
->netdev
, cf
, tx_state
, rx_state
);
850 if (priv
->can
.restart_ms
&&
851 (cur_state
>= CAN_STATE_BUS_OFF
) &&
852 (new_state
< CAN_STATE_BUS_OFF
)) {
853 priv
->can
.can_stats
.restarts
++;
856 switch (dev
->family
) {
858 if (es
->leaf
.error_factor
) {
859 priv
->can
.can_stats
.bus_error
++;
864 if (es
->usbcan
.error_state
& USBCAN_ERROR_STATE_TX_ERROR
)
866 if (es
->usbcan
.error_state
& USBCAN_ERROR_STATE_RX_ERROR
)
868 if (es
->usbcan
.error_state
& USBCAN_ERROR_STATE_BUSERROR
) {
869 priv
->can
.can_stats
.bus_error
++;
874 priv
->bec
.txerr
= es
->txerr
;
875 priv
->bec
.rxerr
= es
->rxerr
;
878 static void kvaser_usb_rx_error(const struct kvaser_usb
*dev
,
879 const struct kvaser_usb_error_summary
*es
)
881 struct can_frame
*cf
, tmp_cf
= { .can_id
= CAN_ERR_FLAG
, .can_dlc
= CAN_ERR_DLC
};
883 struct net_device_stats
*stats
;
884 struct kvaser_usb_net_priv
*priv
;
885 enum can_state old_state
, new_state
;
887 if (es
->channel
>= dev
->nchannels
) {
888 dev_err(dev
->udev
->dev
.parent
,
889 "Invalid channel number (%d)\n", es
->channel
);
893 priv
= dev
->nets
[es
->channel
];
894 stats
= &priv
->netdev
->stats
;
896 /* Update all of the can interface's state and error counters before
897 * trying any memory allocation that can actually fail with -ENOMEM.
899 * We send a temporary stack-allocated error can frame to
900 * can_change_state() for the very same reason.
902 * TODO: Split can_change_state() responsibility between updating the
903 * can interface's state and counters, and the setting up of can error
904 * frame ID and data to userspace. Remove stack allocation afterwards.
906 old_state
= priv
->can
.state
;
907 kvaser_usb_rx_error_update_can_state(priv
, es
, &tmp_cf
);
908 new_state
= priv
->can
.state
;
910 skb
= alloc_can_err_skb(priv
->netdev
, &cf
);
915 memcpy(cf
, &tmp_cf
, sizeof(*cf
));
917 if (new_state
!= old_state
) {
919 (M16C_STATE_BUS_OFF
| M16C_STATE_BUS_RESET
)) {
920 if (!priv
->can
.restart_ms
)
921 kvaser_usb_simple_msg_async(priv
, CMD_STOP_CHIP
);
922 netif_carrier_off(priv
->netdev
);
925 if (priv
->can
.restart_ms
&&
926 (old_state
>= CAN_STATE_BUS_OFF
) &&
927 (new_state
< CAN_STATE_BUS_OFF
)) {
928 cf
->can_id
|= CAN_ERR_RESTARTED
;
929 netif_carrier_on(priv
->netdev
);
933 switch (dev
->family
) {
935 if (es
->leaf
.error_factor
) {
936 cf
->can_id
|= CAN_ERR_BUSERROR
| CAN_ERR_PROT
;
938 if (es
->leaf
.error_factor
& M16C_EF_ACKE
)
939 cf
->data
[3] |= (CAN_ERR_PROT_LOC_ACK
);
940 if (es
->leaf
.error_factor
& M16C_EF_CRCE
)
941 cf
->data
[3] |= (CAN_ERR_PROT_LOC_CRC_SEQ
|
942 CAN_ERR_PROT_LOC_CRC_DEL
);
943 if (es
->leaf
.error_factor
& M16C_EF_FORME
)
944 cf
->data
[2] |= CAN_ERR_PROT_FORM
;
945 if (es
->leaf
.error_factor
& M16C_EF_STFE
)
946 cf
->data
[2] |= CAN_ERR_PROT_STUFF
;
947 if (es
->leaf
.error_factor
& M16C_EF_BITE0
)
948 cf
->data
[2] |= CAN_ERR_PROT_BIT0
;
949 if (es
->leaf
.error_factor
& M16C_EF_BITE1
)
950 cf
->data
[2] |= CAN_ERR_PROT_BIT1
;
951 if (es
->leaf
.error_factor
& M16C_EF_TRE
)
952 cf
->data
[2] |= CAN_ERR_PROT_TX
;
956 if (es
->usbcan
.error_state
& USBCAN_ERROR_STATE_BUSERROR
) {
957 cf
->can_id
|= CAN_ERR_BUSERROR
;
962 cf
->data
[6] = es
->txerr
;
963 cf
->data
[7] = es
->rxerr
;
966 stats
->rx_bytes
+= cf
->can_dlc
;
970 /* For USBCAN, report error to userspace iff the channels's errors counter
971 * has changed, or we're the only channel seeing a bus error state.
973 static void kvaser_usbcan_conditionally_rx_error(const struct kvaser_usb
*dev
,
974 struct kvaser_usb_error_summary
*es
)
976 struct kvaser_usb_net_priv
*priv
;
980 channel
= es
->channel
;
981 if (channel
>= dev
->nchannels
) {
982 dev_err(dev
->udev
->dev
.parent
,
983 "Invalid channel number (%d)\n", channel
);
987 priv
= dev
->nets
[channel
];
988 report_error
= false;
990 if (es
->txerr
!= priv
->bec
.txerr
) {
991 es
->usbcan
.error_state
|= USBCAN_ERROR_STATE_TX_ERROR
;
994 if (es
->rxerr
!= priv
->bec
.rxerr
) {
995 es
->usbcan
.error_state
|= USBCAN_ERROR_STATE_RX_ERROR
;
998 if ((es
->status
& M16C_STATE_BUS_ERROR
) &&
999 !(es
->usbcan
.other_ch_status
& M16C_STATE_BUS_ERROR
)) {
1000 es
->usbcan
.error_state
|= USBCAN_ERROR_STATE_BUSERROR
;
1001 report_error
= true;
1005 kvaser_usb_rx_error(dev
, es
);
1008 static void kvaser_usbcan_rx_error(const struct kvaser_usb
*dev
,
1009 const struct kvaser_msg
*msg
)
1011 struct kvaser_usb_error_summary es
= { };
1014 /* Sometimes errors are sent as unsolicited chip state events */
1015 case CMD_CHIP_STATE_EVENT
:
1016 es
.channel
= msg
->u
.usbcan
.chip_state_event
.channel
;
1017 es
.status
= msg
->u
.usbcan
.chip_state_event
.status
;
1018 es
.txerr
= msg
->u
.usbcan
.chip_state_event
.tx_errors_count
;
1019 es
.rxerr
= msg
->u
.usbcan
.chip_state_event
.rx_errors_count
;
1020 kvaser_usbcan_conditionally_rx_error(dev
, &es
);
1023 case CMD_CAN_ERROR_EVENT
:
1025 es
.status
= msg
->u
.usbcan
.error_event
.status_ch0
;
1026 es
.txerr
= msg
->u
.usbcan
.error_event
.tx_errors_count_ch0
;
1027 es
.rxerr
= msg
->u
.usbcan
.error_event
.rx_errors_count_ch0
;
1028 es
.usbcan
.other_ch_status
=
1029 msg
->u
.usbcan
.error_event
.status_ch1
;
1030 kvaser_usbcan_conditionally_rx_error(dev
, &es
);
1032 /* The USBCAN firmware supports up to 2 channels.
1033 * Now that ch0 was checked, check if ch1 has any errors.
1035 if (dev
->nchannels
== MAX_USBCAN_NET_DEVICES
) {
1037 es
.status
= msg
->u
.usbcan
.error_event
.status_ch1
;
1038 es
.txerr
= msg
->u
.usbcan
.error_event
.tx_errors_count_ch1
;
1039 es
.rxerr
= msg
->u
.usbcan
.error_event
.rx_errors_count_ch1
;
1040 es
.usbcan
.other_ch_status
=
1041 msg
->u
.usbcan
.error_event
.status_ch0
;
1042 kvaser_usbcan_conditionally_rx_error(dev
, &es
);
1047 dev_err(dev
->udev
->dev
.parent
, "Invalid msg id (%d)\n",
1052 static void kvaser_leaf_rx_error(const struct kvaser_usb
*dev
,
1053 const struct kvaser_msg
*msg
)
1055 struct kvaser_usb_error_summary es
= { };
1058 case CMD_CAN_ERROR_EVENT
:
1059 es
.channel
= msg
->u
.leaf
.error_event
.channel
;
1060 es
.status
= msg
->u
.leaf
.error_event
.status
;
1061 es
.txerr
= msg
->u
.leaf
.error_event
.tx_errors_count
;
1062 es
.rxerr
= msg
->u
.leaf
.error_event
.rx_errors_count
;
1063 es
.leaf
.error_factor
= msg
->u
.leaf
.error_event
.error_factor
;
1065 case CMD_LEAF_LOG_MESSAGE
:
1066 es
.channel
= msg
->u
.leaf
.log_message
.channel
;
1067 es
.status
= msg
->u
.leaf
.log_message
.data
[0];
1068 es
.txerr
= msg
->u
.leaf
.log_message
.data
[2];
1069 es
.rxerr
= msg
->u
.leaf
.log_message
.data
[3];
1070 es
.leaf
.error_factor
= msg
->u
.leaf
.log_message
.data
[1];
1072 case CMD_CHIP_STATE_EVENT
:
1073 es
.channel
= msg
->u
.leaf
.chip_state_event
.channel
;
1074 es
.status
= msg
->u
.leaf
.chip_state_event
.status
;
1075 es
.txerr
= msg
->u
.leaf
.chip_state_event
.tx_errors_count
;
1076 es
.rxerr
= msg
->u
.leaf
.chip_state_event
.rx_errors_count
;
1077 es
.leaf
.error_factor
= 0;
1080 dev_err(dev
->udev
->dev
.parent
, "Invalid msg id (%d)\n",
1085 kvaser_usb_rx_error(dev
, &es
);
1088 static void kvaser_usb_rx_can_err(const struct kvaser_usb_net_priv
*priv
,
1089 const struct kvaser_msg
*msg
)
1091 struct can_frame
*cf
;
1092 struct sk_buff
*skb
;
1093 struct net_device_stats
*stats
= &priv
->netdev
->stats
;
1095 if (msg
->u
.rx_can_header
.flag
& (MSG_FLAG_ERROR_FRAME
|
1097 netdev_err(priv
->netdev
, "Unknow error (flags: 0x%02x)\n",
1098 msg
->u
.rx_can_header
.flag
);
1104 if (msg
->u
.rx_can_header
.flag
& MSG_FLAG_OVERRUN
) {
1105 stats
->rx_over_errors
++;
1108 skb
= alloc_can_err_skb(priv
->netdev
, &cf
);
1110 stats
->rx_dropped
++;
1114 cf
->can_id
|= CAN_ERR_CRTL
;
1115 cf
->data
[1] = CAN_ERR_CRTL_RX_OVERFLOW
;
1117 stats
->rx_packets
++;
1118 stats
->rx_bytes
+= cf
->can_dlc
;
1123 static void kvaser_usb_rx_can_msg(const struct kvaser_usb
*dev
,
1124 const struct kvaser_msg
*msg
)
1126 struct kvaser_usb_net_priv
*priv
;
1127 struct can_frame
*cf
;
1128 struct sk_buff
*skb
;
1129 struct net_device_stats
*stats
;
1130 u8 channel
= msg
->u
.rx_can_header
.channel
;
1131 const u8
*rx_msg
= NULL
; /* GCC */
1133 if (channel
>= dev
->nchannels
) {
1134 dev_err(dev
->udev
->dev
.parent
,
1135 "Invalid channel number (%d)\n", channel
);
1139 priv
= dev
->nets
[channel
];
1140 stats
= &priv
->netdev
->stats
;
1142 if ((msg
->u
.rx_can_header
.flag
& MSG_FLAG_ERROR_FRAME
) &&
1143 (dev
->family
== KVASER_LEAF
&& msg
->id
== CMD_LEAF_LOG_MESSAGE
)) {
1144 kvaser_leaf_rx_error(dev
, msg
);
1146 } else if (msg
->u
.rx_can_header
.flag
& (MSG_FLAG_ERROR_FRAME
|
1148 MSG_FLAG_OVERRUN
)) {
1149 kvaser_usb_rx_can_err(priv
, msg
);
1151 } else if (msg
->u
.rx_can_header
.flag
& ~MSG_FLAG_REMOTE_FRAME
) {
1152 netdev_warn(priv
->netdev
,
1153 "Unhandled frame (flags: 0x%02x)",
1154 msg
->u
.rx_can_header
.flag
);
1158 switch (dev
->family
) {
1160 rx_msg
= msg
->u
.leaf
.rx_can
.msg
;
1163 rx_msg
= msg
->u
.usbcan
.rx_can
.msg
;
1167 skb
= alloc_can_skb(priv
->netdev
, &cf
);
1169 stats
->tx_dropped
++;
1173 if (dev
->family
== KVASER_LEAF
&& msg
->id
== CMD_LEAF_LOG_MESSAGE
) {
1174 cf
->can_id
= le32_to_cpu(msg
->u
.leaf
.log_message
.id
);
1175 if (cf
->can_id
& KVASER_EXTENDED_FRAME
)
1176 cf
->can_id
&= CAN_EFF_MASK
| CAN_EFF_FLAG
;
1178 cf
->can_id
&= CAN_SFF_MASK
;
1180 cf
->can_dlc
= get_can_dlc(msg
->u
.leaf
.log_message
.dlc
);
1182 if (msg
->u
.leaf
.log_message
.flags
& MSG_FLAG_REMOTE_FRAME
)
1183 cf
->can_id
|= CAN_RTR_FLAG
;
1185 memcpy(cf
->data
, &msg
->u
.leaf
.log_message
.data
,
1188 cf
->can_id
= ((rx_msg
[0] & 0x1f) << 6) | (rx_msg
[1] & 0x3f);
1190 if (msg
->id
== CMD_RX_EXT_MESSAGE
) {
1192 cf
->can_id
|= ((rx_msg
[2] & 0x0f) << 14) |
1193 ((rx_msg
[3] & 0xff) << 6) |
1195 cf
->can_id
|= CAN_EFF_FLAG
;
1198 cf
->can_dlc
= get_can_dlc(rx_msg
[5]);
1200 if (msg
->u
.rx_can_header
.flag
& MSG_FLAG_REMOTE_FRAME
)
1201 cf
->can_id
|= CAN_RTR_FLAG
;
1203 memcpy(cf
->data
, &rx_msg
[6],
1207 stats
->rx_packets
++;
1208 stats
->rx_bytes
+= cf
->can_dlc
;
1212 static void kvaser_usb_start_chip_reply(const struct kvaser_usb
*dev
,
1213 const struct kvaser_msg
*msg
)
1215 struct kvaser_usb_net_priv
*priv
;
1216 u8 channel
= msg
->u
.simple
.channel
;
1218 if (channel
>= dev
->nchannels
) {
1219 dev_err(dev
->udev
->dev
.parent
,
1220 "Invalid channel number (%d)\n", channel
);
1224 priv
= dev
->nets
[channel
];
1226 if (completion_done(&priv
->start_comp
) &&
1227 netif_queue_stopped(priv
->netdev
)) {
1228 netif_wake_queue(priv
->netdev
);
1230 netif_start_queue(priv
->netdev
);
1231 complete(&priv
->start_comp
);
1235 static void kvaser_usb_stop_chip_reply(const struct kvaser_usb
*dev
,
1236 const struct kvaser_msg
*msg
)
1238 struct kvaser_usb_net_priv
*priv
;
1239 u8 channel
= msg
->u
.simple
.channel
;
1241 if (channel
>= dev
->nchannels
) {
1242 dev_err(dev
->udev
->dev
.parent
,
1243 "Invalid channel number (%d)\n", channel
);
1247 priv
= dev
->nets
[channel
];
1249 complete(&priv
->stop_comp
);
1252 static void kvaser_usb_handle_message(const struct kvaser_usb
*dev
,
1253 const struct kvaser_msg
*msg
)
1256 case CMD_START_CHIP_REPLY
:
1257 kvaser_usb_start_chip_reply(dev
, msg
);
1260 case CMD_STOP_CHIP_REPLY
:
1261 kvaser_usb_stop_chip_reply(dev
, msg
);
1264 case CMD_RX_STD_MESSAGE
:
1265 case CMD_RX_EXT_MESSAGE
:
1266 kvaser_usb_rx_can_msg(dev
, msg
);
1269 case CMD_LEAF_LOG_MESSAGE
:
1270 if (dev
->family
!= KVASER_LEAF
)
1272 kvaser_usb_rx_can_msg(dev
, msg
);
1275 case CMD_CHIP_STATE_EVENT
:
1276 case CMD_CAN_ERROR_EVENT
:
1277 if (dev
->family
== KVASER_LEAF
)
1278 kvaser_leaf_rx_error(dev
, msg
);
1280 kvaser_usbcan_rx_error(dev
, msg
);
1283 case CMD_TX_ACKNOWLEDGE
:
1284 kvaser_usb_tx_acknowledge(dev
, msg
);
1287 /* Ignored messages */
1288 case CMD_USBCAN_CLOCK_OVERFLOW_EVENT
:
1289 if (dev
->family
!= KVASER_USBCAN
)
1294 warn
: dev_warn(dev
->udev
->dev
.parent
,
1295 "Unhandled message (%d)\n", msg
->id
);
1300 static void kvaser_usb_read_bulk_callback(struct urb
*urb
)
1302 struct kvaser_usb
*dev
= urb
->context
;
1303 struct kvaser_msg
*msg
;
1307 switch (urb
->status
) {
1314 dev_info(dev
->udev
->dev
.parent
, "Rx URB aborted (%d)\n",
1319 while (pos
<= urb
->actual_length
- MSG_HEADER_LEN
) {
1320 msg
= urb
->transfer_buffer
+ pos
;
1322 /* The Kvaser firmware can only read and write messages that
1323 * does not cross the USB's endpoint wMaxPacketSize boundary.
1324 * If a follow-up command crosses such boundary, firmware puts
1325 * a placeholder zero-length command in its place then aligns
1326 * the real command to the next max packet size.
1328 * Handle such cases or we're going to miss a significant
1329 * number of events in case of a heavy rx load on the bus.
1331 if (msg
->len
== 0) {
1332 pos
= round_up(pos
, dev
->bulk_in
->wMaxPacketSize
);
1336 if (pos
+ msg
->len
> urb
->actual_length
) {
1337 dev_err(dev
->udev
->dev
.parent
, "Format error\n");
1341 kvaser_usb_handle_message(dev
, msg
);
1346 usb_fill_bulk_urb(urb
, dev
->udev
,
1347 usb_rcvbulkpipe(dev
->udev
,
1348 dev
->bulk_in
->bEndpointAddress
),
1349 urb
->transfer_buffer
, RX_BUFFER_SIZE
,
1350 kvaser_usb_read_bulk_callback
, dev
);
1352 err
= usb_submit_urb(urb
, GFP_ATOMIC
);
1353 if (err
== -ENODEV
) {
1354 for (i
= 0; i
< dev
->nchannels
; i
++) {
1358 netif_device_detach(dev
->nets
[i
]->netdev
);
1361 dev_err(dev
->udev
->dev
.parent
,
1362 "Failed resubmitting read bulk urb: %d\n", err
);
1368 static int kvaser_usb_setup_rx_urbs(struct kvaser_usb
*dev
)
1372 if (dev
->rxinitdone
)
1375 for (i
= 0; i
< MAX_RX_URBS
; i
++) {
1376 struct urb
*urb
= NULL
;
1380 urb
= usb_alloc_urb(0, GFP_KERNEL
);
1382 dev_warn(dev
->udev
->dev
.parent
,
1383 "No memory left for URBs\n");
1388 buf
= usb_alloc_coherent(dev
->udev
, RX_BUFFER_SIZE
,
1389 GFP_KERNEL
, &buf_dma
);
1391 dev_warn(dev
->udev
->dev
.parent
,
1392 "No memory left for USB buffer\n");
1398 usb_fill_bulk_urb(urb
, dev
->udev
,
1399 usb_rcvbulkpipe(dev
->udev
,
1400 dev
->bulk_in
->bEndpointAddress
),
1401 buf
, RX_BUFFER_SIZE
,
1402 kvaser_usb_read_bulk_callback
,
1404 urb
->transfer_dma
= buf_dma
;
1405 urb
->transfer_flags
|= URB_NO_TRANSFER_DMA_MAP
;
1406 usb_anchor_urb(urb
, &dev
->rx_submitted
);
1408 err
= usb_submit_urb(urb
, GFP_KERNEL
);
1410 usb_unanchor_urb(urb
);
1411 usb_free_coherent(dev
->udev
, RX_BUFFER_SIZE
, buf
,
1417 dev
->rxbuf
[i
] = buf
;
1418 dev
->rxbuf_dma
[i
] = buf_dma
;
1424 dev_warn(dev
->udev
->dev
.parent
,
1425 "Cannot setup read URBs, error %d\n", err
);
1427 } else if (i
< MAX_RX_URBS
) {
1428 dev_warn(dev
->udev
->dev
.parent
,
1429 "RX performances may be slow\n");
1432 dev
->rxinitdone
= true;
1437 static int kvaser_usb_set_opt_mode(const struct kvaser_usb_net_priv
*priv
)
1439 struct kvaser_msg
*msg
;
1442 msg
= kmalloc(sizeof(*msg
), GFP_KERNEL
);
1446 msg
->id
= CMD_SET_CTRL_MODE
;
1447 msg
->len
= MSG_HEADER_LEN
+ sizeof(struct kvaser_msg_ctrl_mode
);
1448 msg
->u
.ctrl_mode
.tid
= 0xff;
1449 msg
->u
.ctrl_mode
.channel
= priv
->channel
;
1451 if (priv
->can
.ctrlmode
& CAN_CTRLMODE_LISTENONLY
)
1452 msg
->u
.ctrl_mode
.ctrl_mode
= KVASER_CTRL_MODE_SILENT
;
1454 msg
->u
.ctrl_mode
.ctrl_mode
= KVASER_CTRL_MODE_NORMAL
;
1456 rc
= kvaser_usb_send_msg(priv
->dev
, msg
);
1462 static int kvaser_usb_start_chip(struct kvaser_usb_net_priv
*priv
)
1466 init_completion(&priv
->start_comp
);
1468 err
= kvaser_usb_send_simple_msg(priv
->dev
, CMD_START_CHIP
,
1473 if (!wait_for_completion_timeout(&priv
->start_comp
,
1474 msecs_to_jiffies(START_TIMEOUT
)))
1480 static int kvaser_usb_open(struct net_device
*netdev
)
1482 struct kvaser_usb_net_priv
*priv
= netdev_priv(netdev
);
1483 struct kvaser_usb
*dev
= priv
->dev
;
1486 err
= open_candev(netdev
);
1490 err
= kvaser_usb_setup_rx_urbs(dev
);
1494 err
= kvaser_usb_set_opt_mode(priv
);
1498 err
= kvaser_usb_start_chip(priv
);
1500 netdev_warn(netdev
, "Cannot start device, error %d\n", err
);
1504 priv
->can
.state
= CAN_STATE_ERROR_ACTIVE
;
1509 close_candev(netdev
);
1513 static void kvaser_usb_reset_tx_urb_contexts(struct kvaser_usb_net_priv
*priv
)
1517 priv
->active_tx_contexts
= 0;
1518 for (i
= 0; i
< MAX_TX_URBS
; i
++)
1519 priv
->tx_contexts
[i
].echo_index
= MAX_TX_URBS
;
1522 /* This method might sleep. Do not call it in the atomic context
1523 * of URB completions.
1525 static void kvaser_usb_unlink_tx_urbs(struct kvaser_usb_net_priv
*priv
)
1527 usb_kill_anchored_urbs(&priv
->tx_submitted
);
1528 kvaser_usb_reset_tx_urb_contexts(priv
);
1531 static void kvaser_usb_unlink_all_urbs(struct kvaser_usb
*dev
)
1535 usb_kill_anchored_urbs(&dev
->rx_submitted
);
1537 for (i
= 0; i
< MAX_RX_URBS
; i
++)
1538 usb_free_coherent(dev
->udev
, RX_BUFFER_SIZE
,
1542 for (i
= 0; i
< dev
->nchannels
; i
++) {
1543 struct kvaser_usb_net_priv
*priv
= dev
->nets
[i
];
1546 kvaser_usb_unlink_tx_urbs(priv
);
1550 static int kvaser_usb_stop_chip(struct kvaser_usb_net_priv
*priv
)
1554 init_completion(&priv
->stop_comp
);
1556 err
= kvaser_usb_send_simple_msg(priv
->dev
, CMD_STOP_CHIP
,
1561 if (!wait_for_completion_timeout(&priv
->stop_comp
,
1562 msecs_to_jiffies(STOP_TIMEOUT
)))
1568 static int kvaser_usb_flush_queue(struct kvaser_usb_net_priv
*priv
)
1570 struct kvaser_msg
*msg
;
1573 msg
= kmalloc(sizeof(*msg
), GFP_KERNEL
);
1577 msg
->id
= CMD_FLUSH_QUEUE
;
1578 msg
->len
= MSG_HEADER_LEN
+ sizeof(struct kvaser_msg_flush_queue
);
1579 msg
->u
.flush_queue
.channel
= priv
->channel
;
1580 msg
->u
.flush_queue
.flags
= 0x00;
1582 rc
= kvaser_usb_send_msg(priv
->dev
, msg
);
1588 static int kvaser_usb_close(struct net_device
*netdev
)
1590 struct kvaser_usb_net_priv
*priv
= netdev_priv(netdev
);
1591 struct kvaser_usb
*dev
= priv
->dev
;
1594 netif_stop_queue(netdev
);
1596 err
= kvaser_usb_flush_queue(priv
);
1598 netdev_warn(netdev
, "Cannot flush queue, error %d\n", err
);
1600 if (kvaser_usb_send_simple_msg(dev
, CMD_RESET_CHIP
, priv
->channel
))
1601 netdev_warn(netdev
, "Cannot reset card, error %d\n", err
);
1603 err
= kvaser_usb_stop_chip(priv
);
1605 netdev_warn(netdev
, "Cannot stop device, error %d\n", err
);
1607 /* reset tx contexts */
1608 kvaser_usb_unlink_tx_urbs(priv
);
1610 priv
->can
.state
= CAN_STATE_STOPPED
;
1611 close_candev(priv
->netdev
);
1616 static void kvaser_usb_write_bulk_callback(struct urb
*urb
)
1618 struct kvaser_usb_tx_urb_context
*context
= urb
->context
;
1619 struct kvaser_usb_net_priv
*priv
;
1620 struct net_device
*netdev
;
1622 if (WARN_ON(!context
))
1625 priv
= context
->priv
;
1626 netdev
= priv
->netdev
;
1628 kfree(urb
->transfer_buffer
);
1630 if (!netif_device_present(netdev
))
1634 netdev_info(netdev
, "Tx URB aborted (%d)\n", urb
->status
);
1637 static netdev_tx_t
kvaser_usb_start_xmit(struct sk_buff
*skb
,
1638 struct net_device
*netdev
)
1640 struct kvaser_usb_net_priv
*priv
= netdev_priv(netdev
);
1641 struct kvaser_usb
*dev
= priv
->dev
;
1642 struct net_device_stats
*stats
= &netdev
->stats
;
1643 struct can_frame
*cf
= (struct can_frame
*)skb
->data
;
1644 struct kvaser_usb_tx_urb_context
*context
= NULL
;
1647 struct kvaser_msg
*msg
;
1648 int i
, err
, ret
= NETDEV_TX_OK
;
1649 u8
*msg_tx_can_flags
= NULL
; /* GCC */
1650 unsigned long flags
;
1652 if (can_dropped_invalid_skb(netdev
, skb
))
1653 return NETDEV_TX_OK
;
1655 urb
= usb_alloc_urb(0, GFP_ATOMIC
);
1657 netdev_err(netdev
, "No memory left for URBs\n");
1658 stats
->tx_dropped
++;
1660 return NETDEV_TX_OK
;
1663 buf
= kmalloc(sizeof(struct kvaser_msg
), GFP_ATOMIC
);
1665 stats
->tx_dropped
++;
1671 msg
->len
= MSG_HEADER_LEN
+ sizeof(struct kvaser_msg_tx_can
);
1672 msg
->u
.tx_can
.channel
= priv
->channel
;
1674 switch (dev
->family
) {
1676 msg_tx_can_flags
= &msg
->u
.tx_can
.leaf
.flags
;
1679 msg_tx_can_flags
= &msg
->u
.tx_can
.usbcan
.flags
;
1683 *msg_tx_can_flags
= 0;
1685 if (cf
->can_id
& CAN_EFF_FLAG
) {
1686 msg
->id
= CMD_TX_EXT_MESSAGE
;
1687 msg
->u
.tx_can
.msg
[0] = (cf
->can_id
>> 24) & 0x1f;
1688 msg
->u
.tx_can
.msg
[1] = (cf
->can_id
>> 18) & 0x3f;
1689 msg
->u
.tx_can
.msg
[2] = (cf
->can_id
>> 14) & 0x0f;
1690 msg
->u
.tx_can
.msg
[3] = (cf
->can_id
>> 6) & 0xff;
1691 msg
->u
.tx_can
.msg
[4] = cf
->can_id
& 0x3f;
1693 msg
->id
= CMD_TX_STD_MESSAGE
;
1694 msg
->u
.tx_can
.msg
[0] = (cf
->can_id
>> 6) & 0x1f;
1695 msg
->u
.tx_can
.msg
[1] = cf
->can_id
& 0x3f;
1698 msg
->u
.tx_can
.msg
[5] = cf
->can_dlc
;
1699 memcpy(&msg
->u
.tx_can
.msg
[6], cf
->data
, cf
->can_dlc
);
1701 if (cf
->can_id
& CAN_RTR_FLAG
)
1702 *msg_tx_can_flags
|= MSG_FLAG_REMOTE_FRAME
;
1704 spin_lock_irqsave(&priv
->tx_contexts_lock
, flags
);
1705 for (i
= 0; i
< ARRAY_SIZE(priv
->tx_contexts
); i
++) {
1706 if (priv
->tx_contexts
[i
].echo_index
== MAX_TX_URBS
) {
1707 context
= &priv
->tx_contexts
[i
];
1709 context
->echo_index
= i
;
1710 can_put_echo_skb(skb
, netdev
, context
->echo_index
);
1711 ++priv
->active_tx_contexts
;
1712 if (priv
->active_tx_contexts
>= MAX_TX_URBS
)
1713 netif_stop_queue(netdev
);
1718 spin_unlock_irqrestore(&priv
->tx_contexts_lock
, flags
);
1720 /* This should never happen; it implies a flow control bug */
1722 netdev_warn(netdev
, "cannot find free context\n");
1725 ret
= NETDEV_TX_BUSY
;
1729 context
->priv
= priv
;
1730 context
->dlc
= cf
->can_dlc
;
1732 msg
->u
.tx_can
.tid
= context
->echo_index
;
1734 usb_fill_bulk_urb(urb
, dev
->udev
,
1735 usb_sndbulkpipe(dev
->udev
,
1736 dev
->bulk_out
->bEndpointAddress
),
1738 kvaser_usb_write_bulk_callback
, context
);
1739 usb_anchor_urb(urb
, &priv
->tx_submitted
);
1741 err
= usb_submit_urb(urb
, GFP_ATOMIC
);
1742 if (unlikely(err
)) {
1743 spin_lock_irqsave(&priv
->tx_contexts_lock
, flags
);
1745 can_free_echo_skb(netdev
, context
->echo_index
);
1746 context
->echo_index
= MAX_TX_URBS
;
1747 --priv
->active_tx_contexts
;
1748 netif_wake_queue(netdev
);
1750 spin_unlock_irqrestore(&priv
->tx_contexts_lock
, flags
);
1752 usb_unanchor_urb(urb
);
1754 stats
->tx_dropped
++;
1757 netif_device_detach(netdev
);
1759 netdev_warn(netdev
, "Failed tx_urb %d\n", err
);
1771 static const struct net_device_ops kvaser_usb_netdev_ops
= {
1772 .ndo_open
= kvaser_usb_open
,
1773 .ndo_stop
= kvaser_usb_close
,
1774 .ndo_start_xmit
= kvaser_usb_start_xmit
,
1775 .ndo_change_mtu
= can_change_mtu
,
1778 static const struct can_bittiming_const kvaser_usb_bittiming_const
= {
1779 .name
= "kvaser_usb",
1780 .tseg1_min
= KVASER_USB_TSEG1_MIN
,
1781 .tseg1_max
= KVASER_USB_TSEG1_MAX
,
1782 .tseg2_min
= KVASER_USB_TSEG2_MIN
,
1783 .tseg2_max
= KVASER_USB_TSEG2_MAX
,
1784 .sjw_max
= KVASER_USB_SJW_MAX
,
1785 .brp_min
= KVASER_USB_BRP_MIN
,
1786 .brp_max
= KVASER_USB_BRP_MAX
,
1787 .brp_inc
= KVASER_USB_BRP_INC
,
1790 static int kvaser_usb_set_bittiming(struct net_device
*netdev
)
1792 struct kvaser_usb_net_priv
*priv
= netdev_priv(netdev
);
1793 struct can_bittiming
*bt
= &priv
->can
.bittiming
;
1794 struct kvaser_usb
*dev
= priv
->dev
;
1795 struct kvaser_msg
*msg
;
1798 msg
= kmalloc(sizeof(*msg
), GFP_KERNEL
);
1802 msg
->id
= CMD_SET_BUS_PARAMS
;
1803 msg
->len
= MSG_HEADER_LEN
+ sizeof(struct kvaser_msg_busparams
);
1804 msg
->u
.busparams
.channel
= priv
->channel
;
1805 msg
->u
.busparams
.tid
= 0xff;
1806 msg
->u
.busparams
.bitrate
= cpu_to_le32(bt
->bitrate
);
1807 msg
->u
.busparams
.sjw
= bt
->sjw
;
1808 msg
->u
.busparams
.tseg1
= bt
->prop_seg
+ bt
->phase_seg1
;
1809 msg
->u
.busparams
.tseg2
= bt
->phase_seg2
;
1811 if (priv
->can
.ctrlmode
& CAN_CTRLMODE_3_SAMPLES
)
1812 msg
->u
.busparams
.no_samp
= 3;
1814 msg
->u
.busparams
.no_samp
= 1;
1816 rc
= kvaser_usb_send_msg(dev
, msg
);
1822 static int kvaser_usb_set_mode(struct net_device
*netdev
,
1825 struct kvaser_usb_net_priv
*priv
= netdev_priv(netdev
);
1829 case CAN_MODE_START
:
1830 err
= kvaser_usb_simple_msg_async(priv
, CMD_START_CHIP
);
1841 static int kvaser_usb_get_berr_counter(const struct net_device
*netdev
,
1842 struct can_berr_counter
*bec
)
1844 struct kvaser_usb_net_priv
*priv
= netdev_priv(netdev
);
1851 static void kvaser_usb_remove_interfaces(struct kvaser_usb
*dev
)
1855 for (i
= 0; i
< dev
->nchannels
; i
++) {
1859 unregister_netdev(dev
->nets
[i
]->netdev
);
1862 kvaser_usb_unlink_all_urbs(dev
);
1864 for (i
= 0; i
< dev
->nchannels
; i
++) {
1868 free_candev(dev
->nets
[i
]->netdev
);
1872 static int kvaser_usb_init_one(struct usb_interface
*intf
,
1873 const struct usb_device_id
*id
, int channel
)
1875 struct kvaser_usb
*dev
= usb_get_intfdata(intf
);
1876 struct net_device
*netdev
;
1877 struct kvaser_usb_net_priv
*priv
;
1880 err
= kvaser_usb_send_simple_msg(dev
, CMD_RESET_CHIP
, channel
);
1884 netdev
= alloc_candev(sizeof(*priv
), MAX_TX_URBS
);
1886 dev_err(&intf
->dev
, "Cannot alloc candev\n");
1890 priv
= netdev_priv(netdev
);
1892 init_usb_anchor(&priv
->tx_submitted
);
1893 init_completion(&priv
->start_comp
);
1894 init_completion(&priv
->stop_comp
);
1897 priv
->netdev
= netdev
;
1898 priv
->channel
= channel
;
1900 spin_lock_init(&priv
->tx_contexts_lock
);
1901 kvaser_usb_reset_tx_urb_contexts(priv
);
1903 priv
->can
.state
= CAN_STATE_STOPPED
;
1904 priv
->can
.clock
.freq
= CAN_USB_CLOCK
;
1905 priv
->can
.bittiming_const
= &kvaser_usb_bittiming_const
;
1906 priv
->can
.do_set_bittiming
= kvaser_usb_set_bittiming
;
1907 priv
->can
.do_set_mode
= kvaser_usb_set_mode
;
1908 if (id
->driver_info
& KVASER_HAS_TXRX_ERRORS
)
1909 priv
->can
.do_get_berr_counter
= kvaser_usb_get_berr_counter
;
1910 priv
->can
.ctrlmode_supported
= CAN_CTRLMODE_3_SAMPLES
;
1911 if (id
->driver_info
& KVASER_HAS_SILENT_MODE
)
1912 priv
->can
.ctrlmode_supported
|= CAN_CTRLMODE_LISTENONLY
;
1914 netdev
->flags
|= IFF_ECHO
;
1916 netdev
->netdev_ops
= &kvaser_usb_netdev_ops
;
1918 SET_NETDEV_DEV(netdev
, &intf
->dev
);
1919 netdev
->dev_id
= channel
;
1921 dev
->nets
[channel
] = priv
;
1923 err
= register_candev(netdev
);
1925 dev_err(&intf
->dev
, "Failed to register can device\n");
1926 free_candev(netdev
);
1927 dev
->nets
[channel
] = NULL
;
1931 netdev_dbg(netdev
, "device registered\n");
1936 static int kvaser_usb_get_endpoints(const struct usb_interface
*intf
,
1937 struct usb_endpoint_descriptor
**in
,
1938 struct usb_endpoint_descriptor
**out
)
1940 const struct usb_host_interface
*iface_desc
;
1941 struct usb_endpoint_descriptor
*endpoint
;
1944 iface_desc
= &intf
->altsetting
[0];
1946 for (i
= 0; i
< iface_desc
->desc
.bNumEndpoints
; ++i
) {
1947 endpoint
= &iface_desc
->endpoint
[i
].desc
;
1949 if (!*in
&& usb_endpoint_is_bulk_in(endpoint
))
1952 if (!*out
&& usb_endpoint_is_bulk_out(endpoint
))
1955 /* use first bulk endpoint for in and out */
1963 static int kvaser_usb_probe(struct usb_interface
*intf
,
1964 const struct usb_device_id
*id
)
1966 struct kvaser_usb
*dev
;
1970 dev
= devm_kzalloc(&intf
->dev
, sizeof(*dev
), GFP_KERNEL
);
1974 if (kvaser_is_leaf(id
)) {
1975 dev
->family
= KVASER_LEAF
;
1976 } else if (kvaser_is_usbcan(id
)) {
1977 dev
->family
= KVASER_USBCAN
;
1980 "Product ID (%d) does not belong to any known Kvaser USB family",
1985 err
= kvaser_usb_get_endpoints(intf
, &dev
->bulk_in
, &dev
->bulk_out
);
1987 dev_err(&intf
->dev
, "Cannot get usb endpoint(s)");
1991 dev
->udev
= interface_to_usbdev(intf
);
1993 init_usb_anchor(&dev
->rx_submitted
);
1995 usb_set_intfdata(intf
, dev
);
1997 /* On some x86 laptops, plugging a Kvaser device again after
1998 * an unplug makes the firmware always ignore the very first
1999 * command. For such a case, provide some room for retries
2000 * instead of completely exiting the driver.
2003 err
= kvaser_usb_get_software_info(dev
);
2004 } while (--retry
&& err
== -ETIMEDOUT
);
2008 "Cannot get software infos, error %d\n", err
);
2012 err
= kvaser_usb_get_card_info(dev
);
2015 "Cannot get card infos, error %d\n", err
);
2019 dev_dbg(&intf
->dev
, "Firmware version: %d.%d.%d\n",
2020 ((dev
->fw_version
>> 24) & 0xff),
2021 ((dev
->fw_version
>> 16) & 0xff),
2022 (dev
->fw_version
& 0xffff));
2024 for (i
= 0; i
< dev
->nchannels
; i
++) {
2025 err
= kvaser_usb_init_one(intf
, id
, i
);
2027 kvaser_usb_remove_interfaces(dev
);
2035 static void kvaser_usb_disconnect(struct usb_interface
*intf
)
2037 struct kvaser_usb
*dev
= usb_get_intfdata(intf
);
2039 usb_set_intfdata(intf
, NULL
);
2044 kvaser_usb_remove_interfaces(dev
);
2047 static struct usb_driver kvaser_usb_driver
= {
2048 .name
= "kvaser_usb",
2049 .probe
= kvaser_usb_probe
,
2050 .disconnect
= kvaser_usb_disconnect
,
2051 .id_table
= kvaser_usb_table
,
2054 module_usb_driver(kvaser_usb_driver
);
2056 MODULE_AUTHOR("Olivier Sobrie <olivier@sobrie.be>");
2057 MODULE_DESCRIPTION("CAN driver for Kvaser CAN/USB devices");
2058 MODULE_LICENSE("GPL v2");