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>
29 #define START_TIMEOUT 1000 /* msecs */
30 #define STOP_TIMEOUT 1000 /* msecs */
31 #define USB_SEND_TIMEOUT 1000 /* msecs */
32 #define USB_RECV_TIMEOUT 1000 /* msecs */
33 #define RX_BUFFER_SIZE 3072
34 #define CAN_USB_CLOCK 8000000
35 #define MAX_NET_DEVICES 3
36 #define MAX_USBCAN_NET_DEVICES 2
38 /* Kvaser Leaf USB devices */
39 #define KVASER_VENDOR_ID 0x0bfd
40 #define USB_LEAF_DEVEL_PRODUCT_ID 10
41 #define USB_LEAF_LITE_PRODUCT_ID 11
42 #define USB_LEAF_PRO_PRODUCT_ID 12
43 #define USB_LEAF_SPRO_PRODUCT_ID 14
44 #define USB_LEAF_PRO_LS_PRODUCT_ID 15
45 #define USB_LEAF_PRO_SWC_PRODUCT_ID 16
46 #define USB_LEAF_PRO_LIN_PRODUCT_ID 17
47 #define USB_LEAF_SPRO_LS_PRODUCT_ID 18
48 #define USB_LEAF_SPRO_SWC_PRODUCT_ID 19
49 #define USB_MEMO2_DEVEL_PRODUCT_ID 22
50 #define USB_MEMO2_HSHS_PRODUCT_ID 23
51 #define USB_UPRO_HSHS_PRODUCT_ID 24
52 #define USB_LEAF_LITE_GI_PRODUCT_ID 25
53 #define USB_LEAF_PRO_OBDII_PRODUCT_ID 26
54 #define USB_MEMO2_HSLS_PRODUCT_ID 27
55 #define USB_LEAF_LITE_CH_PRODUCT_ID 28
56 #define USB_BLACKBIRD_SPRO_PRODUCT_ID 29
57 #define USB_OEM_MERCURY_PRODUCT_ID 34
58 #define USB_OEM_LEAF_PRODUCT_ID 35
59 #define USB_CAN_R_PRODUCT_ID 39
60 #define USB_LEAF_LITE_V2_PRODUCT_ID 288
61 #define USB_MINI_PCIE_HS_PRODUCT_ID 289
62 #define USB_LEAF_LIGHT_HS_V2_OEM_PRODUCT_ID 290
63 #define USB_USBCAN_LIGHT_2HS_PRODUCT_ID 291
64 #define USB_MINI_PCIE_2HS_PRODUCT_ID 292
66 static inline bool kvaser_is_leaf(const struct usb_device_id
*id
)
68 return id
->idProduct
>= USB_LEAF_DEVEL_PRODUCT_ID
&&
69 id
->idProduct
<= USB_MINI_PCIE_2HS_PRODUCT_ID
;
72 /* Kvaser USBCan-II devices */
73 #define USB_USBCAN_REVB_PRODUCT_ID 2
74 #define USB_VCI2_PRODUCT_ID 3
75 #define USB_USBCAN2_PRODUCT_ID 4
76 #define USB_MEMORATOR_PRODUCT_ID 5
78 static inline bool kvaser_is_usbcan(const struct usb_device_id
*id
)
80 return id
->idProduct
>= USB_USBCAN_REVB_PRODUCT_ID
&&
81 id
->idProduct
<= USB_MEMORATOR_PRODUCT_ID
;
84 /* USB devices features */
85 #define KVASER_HAS_SILENT_MODE BIT(0)
86 #define KVASER_HAS_TXRX_ERRORS BIT(1)
88 /* Message header size */
89 #define MSG_HEADER_LEN 2
91 /* Can message flags */
92 #define MSG_FLAG_ERROR_FRAME BIT(0)
93 #define MSG_FLAG_OVERRUN BIT(1)
94 #define MSG_FLAG_NERR BIT(2)
95 #define MSG_FLAG_WAKEUP BIT(3)
96 #define MSG_FLAG_REMOTE_FRAME BIT(4)
97 #define MSG_FLAG_RESERVED BIT(5)
98 #define MSG_FLAG_TX_ACK BIT(6)
99 #define MSG_FLAG_TX_REQUEST BIT(7)
101 /* Can states (M16C CxSTRH register) */
102 #define M16C_STATE_BUS_RESET BIT(0)
103 #define M16C_STATE_BUS_ERROR BIT(4)
104 #define M16C_STATE_BUS_PASSIVE BIT(5)
105 #define M16C_STATE_BUS_OFF BIT(6)
108 #define CMD_RX_STD_MESSAGE 12
109 #define CMD_TX_STD_MESSAGE 13
110 #define CMD_RX_EXT_MESSAGE 14
111 #define CMD_TX_EXT_MESSAGE 15
112 #define CMD_SET_BUS_PARAMS 16
113 #define CMD_GET_BUS_PARAMS 17
114 #define CMD_GET_BUS_PARAMS_REPLY 18
115 #define CMD_GET_CHIP_STATE 19
116 #define CMD_CHIP_STATE_EVENT 20
117 #define CMD_SET_CTRL_MODE 21
118 #define CMD_GET_CTRL_MODE 22
119 #define CMD_GET_CTRL_MODE_REPLY 23
120 #define CMD_RESET_CHIP 24
121 #define CMD_RESET_CARD 25
122 #define CMD_START_CHIP 26
123 #define CMD_START_CHIP_REPLY 27
124 #define CMD_STOP_CHIP 28
125 #define CMD_STOP_CHIP_REPLY 29
127 #define CMD_LEAF_GET_CARD_INFO2 32
128 #define CMD_USBCAN_RESET_CLOCK 32
129 #define CMD_USBCAN_CLOCK_OVERFLOW_EVENT 33
131 #define CMD_GET_CARD_INFO 34
132 #define CMD_GET_CARD_INFO_REPLY 35
133 #define CMD_GET_SOFTWARE_INFO 38
134 #define CMD_GET_SOFTWARE_INFO_REPLY 39
135 #define CMD_ERROR_EVENT 45
136 #define CMD_FLUSH_QUEUE 48
137 #define CMD_RESET_ERROR_COUNTER 49
138 #define CMD_TX_ACKNOWLEDGE 50
139 #define CMD_CAN_ERROR_EVENT 51
141 #define CMD_LEAF_USB_THROTTLE 77
142 #define CMD_LEAF_LOG_MESSAGE 106
145 #define M16C_EF_ACKE BIT(0)
146 #define M16C_EF_CRCE BIT(1)
147 #define M16C_EF_FORME BIT(2)
148 #define M16C_EF_STFE BIT(3)
149 #define M16C_EF_BITE0 BIT(4)
150 #define M16C_EF_BITE1 BIT(5)
151 #define M16C_EF_RCVE BIT(6)
152 #define M16C_EF_TRE BIT(7)
154 /* Only Leaf-based devices can report M16C error factors,
155 * thus define our own error status flags for USBCANII
157 #define USBCAN_ERROR_STATE_NONE 0
158 #define USBCAN_ERROR_STATE_TX_ERROR BIT(0)
159 #define USBCAN_ERROR_STATE_RX_ERROR BIT(1)
160 #define USBCAN_ERROR_STATE_BUSERROR BIT(2)
162 /* bittiming parameters */
163 #define KVASER_USB_TSEG1_MIN 1
164 #define KVASER_USB_TSEG1_MAX 16
165 #define KVASER_USB_TSEG2_MIN 1
166 #define KVASER_USB_TSEG2_MAX 8
167 #define KVASER_USB_SJW_MAX 4
168 #define KVASER_USB_BRP_MIN 1
169 #define KVASER_USB_BRP_MAX 64
170 #define KVASER_USB_BRP_INC 1
173 #define KVASER_CTRL_MODE_NORMAL 1
174 #define KVASER_CTRL_MODE_SILENT 2
175 #define KVASER_CTRL_MODE_SELFRECEPTION 3
176 #define KVASER_CTRL_MODE_OFF 4
178 /* Extended CAN identifier flag */
179 #define KVASER_EXTENDED_FRAME BIT(31)
181 /* Kvaser USB CAN dongles are divided into two major families:
182 * - Leaf: Based on Renesas M32C, running firmware labeled as 'filo'
183 * - UsbcanII: Based on Renesas M16C, running firmware labeled as 'helios'
185 enum kvaser_usb_family
{
190 struct kvaser_msg_simple
{
195 struct kvaser_msg_cardinfo
{
200 __le32 serial_number
;
204 __le32 serial_number_low
;
205 __le32 serial_number_high
;
208 __le32 clock_resolution
;
223 struct kvaser_msg_cardinfo2
{
227 __le32 oem_unlock_code
;
230 struct leaf_msg_softinfo
{
235 __le16 max_outstanding_tx
;
239 struct usbcan_msg_softinfo
{
242 __le16 max_outstanding_tx
;
249 struct kvaser_msg_busparams
{
259 struct kvaser_msg_tx_can
{
275 struct kvaser_msg_rx_can_header
{
280 struct leaf_msg_rx_can
{
288 struct usbcan_msg_rx_can
{
296 struct leaf_msg_chip_state_event
{
308 struct usbcan_msg_chip_state_event
{
320 struct kvaser_msg_tx_acknowledge_header
{
325 struct leaf_msg_tx_acknowledge
{
334 struct usbcan_msg_tx_acknowledge
{
342 struct leaf_msg_error_event
{
354 struct usbcan_msg_error_event
{
357 u8 tx_errors_count_ch0
;
358 u8 rx_errors_count_ch0
;
359 u8 tx_errors_count_ch1
;
360 u8 rx_errors_count_ch1
;
366 struct kvaser_msg_ctrl_mode
{
373 struct kvaser_msg_flush_queue
{
380 struct leaf_msg_log_message
{
394 struct kvaser_msg_simple simple
;
395 struct kvaser_msg_cardinfo cardinfo
;
396 struct kvaser_msg_cardinfo2 cardinfo2
;
397 struct kvaser_msg_busparams busparams
;
399 struct kvaser_msg_rx_can_header rx_can_header
;
400 struct kvaser_msg_tx_acknowledge_header tx_acknowledge_header
;
403 struct leaf_msg_softinfo softinfo
;
404 struct leaf_msg_rx_can rx_can
;
405 struct leaf_msg_chip_state_event chip_state_event
;
406 struct leaf_msg_tx_acknowledge tx_acknowledge
;
407 struct leaf_msg_error_event error_event
;
408 struct leaf_msg_log_message log_message
;
412 struct usbcan_msg_softinfo softinfo
;
413 struct usbcan_msg_rx_can rx_can
;
414 struct usbcan_msg_chip_state_event chip_state_event
;
415 struct usbcan_msg_tx_acknowledge tx_acknowledge
;
416 struct usbcan_msg_error_event error_event
;
419 struct kvaser_msg_tx_can tx_can
;
420 struct kvaser_msg_ctrl_mode ctrl_mode
;
421 struct kvaser_msg_flush_queue flush_queue
;
425 /* Summary of a kvaser error event, for a unified Leaf/Usbcan error
426 * handling. Some discrepancies between the two families exist:
428 * - USBCAN firmware does not report M16C "error factors"
429 * - USBCAN controllers has difficulties reporting if the raised error
430 * event is for ch0 or ch1. They leave such arbitration to the OS
431 * driver by letting it compare error counters with previous values
432 * and decide the error event's channel. Thus for USBCAN, the channel
433 * field is only advisory.
435 struct kvaser_usb_error_summary
{
436 u8 channel
, status
, txerr
, rxerr
;
448 /* Context for an outstanding, not yet ACKed, transmission */
449 struct kvaser_usb_tx_urb_context
{
450 struct kvaser_usb_net_priv
*priv
;
456 struct usb_device
*udev
;
457 struct kvaser_usb_net_priv
*nets
[MAX_NET_DEVICES
];
459 struct usb_endpoint_descriptor
*bulk_in
, *bulk_out
;
460 struct usb_anchor rx_submitted
;
462 /* @max_tx_urbs: Firmware-reported maximum number of oustanding,
463 * not yet ACKed, transmissions on this device. This value is
464 * also used as a sentinel for marking free tx contexts.
467 unsigned int nchannels
;
468 unsigned int max_tx_urbs
;
469 enum kvaser_usb_family family
;
472 void *rxbuf
[MAX_RX_URBS
];
473 dma_addr_t rxbuf_dma
[MAX_RX_URBS
];
476 struct kvaser_usb_net_priv
{
478 struct can_berr_counter bec
;
480 struct kvaser_usb
*dev
;
481 struct net_device
*netdev
;
484 struct completion start_comp
, stop_comp
;
485 struct usb_anchor tx_submitted
;
487 spinlock_t tx_contexts_lock
;
488 int active_tx_contexts
;
489 struct kvaser_usb_tx_urb_context tx_contexts
[];
492 static const struct usb_device_id kvaser_usb_table
[] = {
493 /* Leaf family IDs */
494 { USB_DEVICE(KVASER_VENDOR_ID
, USB_LEAF_DEVEL_PRODUCT_ID
) },
495 { USB_DEVICE(KVASER_VENDOR_ID
, USB_LEAF_LITE_PRODUCT_ID
) },
496 { USB_DEVICE(KVASER_VENDOR_ID
, USB_LEAF_PRO_PRODUCT_ID
),
497 .driver_info
= KVASER_HAS_TXRX_ERRORS
|
498 KVASER_HAS_SILENT_MODE
},
499 { USB_DEVICE(KVASER_VENDOR_ID
, USB_LEAF_SPRO_PRODUCT_ID
),
500 .driver_info
= KVASER_HAS_TXRX_ERRORS
|
501 KVASER_HAS_SILENT_MODE
},
502 { USB_DEVICE(KVASER_VENDOR_ID
, USB_LEAF_PRO_LS_PRODUCT_ID
),
503 .driver_info
= KVASER_HAS_TXRX_ERRORS
|
504 KVASER_HAS_SILENT_MODE
},
505 { USB_DEVICE(KVASER_VENDOR_ID
, USB_LEAF_PRO_SWC_PRODUCT_ID
),
506 .driver_info
= KVASER_HAS_TXRX_ERRORS
|
507 KVASER_HAS_SILENT_MODE
},
508 { USB_DEVICE(KVASER_VENDOR_ID
, USB_LEAF_PRO_LIN_PRODUCT_ID
),
509 .driver_info
= KVASER_HAS_TXRX_ERRORS
|
510 KVASER_HAS_SILENT_MODE
},
511 { USB_DEVICE(KVASER_VENDOR_ID
, USB_LEAF_SPRO_LS_PRODUCT_ID
),
512 .driver_info
= KVASER_HAS_TXRX_ERRORS
|
513 KVASER_HAS_SILENT_MODE
},
514 { USB_DEVICE(KVASER_VENDOR_ID
, USB_LEAF_SPRO_SWC_PRODUCT_ID
),
515 .driver_info
= KVASER_HAS_TXRX_ERRORS
|
516 KVASER_HAS_SILENT_MODE
},
517 { USB_DEVICE(KVASER_VENDOR_ID
, USB_MEMO2_DEVEL_PRODUCT_ID
),
518 .driver_info
= KVASER_HAS_TXRX_ERRORS
|
519 KVASER_HAS_SILENT_MODE
},
520 { USB_DEVICE(KVASER_VENDOR_ID
, USB_MEMO2_HSHS_PRODUCT_ID
),
521 .driver_info
= KVASER_HAS_TXRX_ERRORS
|
522 KVASER_HAS_SILENT_MODE
},
523 { USB_DEVICE(KVASER_VENDOR_ID
, USB_UPRO_HSHS_PRODUCT_ID
),
524 .driver_info
= KVASER_HAS_TXRX_ERRORS
},
525 { USB_DEVICE(KVASER_VENDOR_ID
, USB_LEAF_LITE_GI_PRODUCT_ID
) },
526 { USB_DEVICE(KVASER_VENDOR_ID
, USB_LEAF_PRO_OBDII_PRODUCT_ID
),
527 .driver_info
= KVASER_HAS_TXRX_ERRORS
|
528 KVASER_HAS_SILENT_MODE
},
529 { USB_DEVICE(KVASER_VENDOR_ID
, USB_MEMO2_HSLS_PRODUCT_ID
),
530 .driver_info
= KVASER_HAS_TXRX_ERRORS
},
531 { USB_DEVICE(KVASER_VENDOR_ID
, USB_LEAF_LITE_CH_PRODUCT_ID
),
532 .driver_info
= KVASER_HAS_TXRX_ERRORS
},
533 { USB_DEVICE(KVASER_VENDOR_ID
, USB_BLACKBIRD_SPRO_PRODUCT_ID
),
534 .driver_info
= KVASER_HAS_TXRX_ERRORS
},
535 { USB_DEVICE(KVASER_VENDOR_ID
, USB_OEM_MERCURY_PRODUCT_ID
),
536 .driver_info
= KVASER_HAS_TXRX_ERRORS
},
537 { USB_DEVICE(KVASER_VENDOR_ID
, USB_OEM_LEAF_PRODUCT_ID
),
538 .driver_info
= KVASER_HAS_TXRX_ERRORS
},
539 { USB_DEVICE(KVASER_VENDOR_ID
, USB_CAN_R_PRODUCT_ID
),
540 .driver_info
= KVASER_HAS_TXRX_ERRORS
},
541 { USB_DEVICE(KVASER_VENDOR_ID
, USB_LEAF_LITE_V2_PRODUCT_ID
) },
542 { USB_DEVICE(KVASER_VENDOR_ID
, USB_MINI_PCIE_HS_PRODUCT_ID
) },
543 { USB_DEVICE(KVASER_VENDOR_ID
, USB_LEAF_LIGHT_HS_V2_OEM_PRODUCT_ID
) },
544 { USB_DEVICE(KVASER_VENDOR_ID
, USB_USBCAN_LIGHT_2HS_PRODUCT_ID
) },
545 { USB_DEVICE(KVASER_VENDOR_ID
, USB_MINI_PCIE_2HS_PRODUCT_ID
) },
547 /* USBCANII family IDs */
548 { USB_DEVICE(KVASER_VENDOR_ID
, USB_USBCAN2_PRODUCT_ID
),
549 .driver_info
= KVASER_HAS_TXRX_ERRORS
},
550 { USB_DEVICE(KVASER_VENDOR_ID
, USB_USBCAN_REVB_PRODUCT_ID
),
551 .driver_info
= KVASER_HAS_TXRX_ERRORS
},
552 { USB_DEVICE(KVASER_VENDOR_ID
, USB_MEMORATOR_PRODUCT_ID
),
553 .driver_info
= KVASER_HAS_TXRX_ERRORS
},
554 { USB_DEVICE(KVASER_VENDOR_ID
, USB_VCI2_PRODUCT_ID
),
555 .driver_info
= KVASER_HAS_TXRX_ERRORS
},
559 MODULE_DEVICE_TABLE(usb
, kvaser_usb_table
);
561 static inline int kvaser_usb_send_msg(const struct kvaser_usb
*dev
,
562 struct kvaser_msg
*msg
)
566 return usb_bulk_msg(dev
->udev
,
567 usb_sndbulkpipe(dev
->udev
,
568 dev
->bulk_out
->bEndpointAddress
),
569 msg
, msg
->len
, &actual_len
,
573 static int kvaser_usb_wait_msg(const struct kvaser_usb
*dev
, u8 id
,
574 struct kvaser_msg
*msg
)
576 struct kvaser_msg
*tmp
;
581 unsigned long to
= jiffies
+ msecs_to_jiffies(USB_RECV_TIMEOUT
);
583 buf
= kzalloc(RX_BUFFER_SIZE
, GFP_KERNEL
);
588 err
= usb_bulk_msg(dev
->udev
,
589 usb_rcvbulkpipe(dev
->udev
,
590 dev
->bulk_in
->bEndpointAddress
),
591 buf
, RX_BUFFER_SIZE
, &actual_len
,
597 while (pos
<= actual_len
- MSG_HEADER_LEN
) {
600 /* Handle messages crossing the USB endpoint max packet
601 * size boundary. Check kvaser_usb_read_bulk_callback()
602 * for further details.
605 pos
= round_up(pos
, le16_to_cpu(dev
->bulk_in
->
610 if (pos
+ tmp
->len
> actual_len
) {
611 dev_err(dev
->udev
->dev
.parent
,
617 memcpy(msg
, tmp
, tmp
->len
);
623 } while (time_before(jiffies
, to
));
633 static int kvaser_usb_send_simple_msg(const struct kvaser_usb
*dev
,
634 u8 msg_id
, int channel
)
636 struct kvaser_msg
*msg
;
639 msg
= kmalloc(sizeof(*msg
), GFP_KERNEL
);
644 msg
->len
= MSG_HEADER_LEN
+ sizeof(struct kvaser_msg_simple
);
645 msg
->u
.simple
.channel
= channel
;
646 msg
->u
.simple
.tid
= 0xff;
648 rc
= kvaser_usb_send_msg(dev
, msg
);
654 static int kvaser_usb_get_software_info(struct kvaser_usb
*dev
)
656 struct kvaser_msg msg
;
659 err
= kvaser_usb_send_simple_msg(dev
, CMD_GET_SOFTWARE_INFO
, 0);
663 err
= kvaser_usb_wait_msg(dev
, CMD_GET_SOFTWARE_INFO_REPLY
, &msg
);
667 switch (dev
->family
) {
669 dev
->fw_version
= le32_to_cpu(msg
.u
.leaf
.softinfo
.fw_version
);
671 le16_to_cpu(msg
.u
.leaf
.softinfo
.max_outstanding_tx
);
674 dev
->fw_version
= le32_to_cpu(msg
.u
.usbcan
.softinfo
.fw_version
);
676 le16_to_cpu(msg
.u
.usbcan
.softinfo
.max_outstanding_tx
);
683 static int kvaser_usb_get_card_info(struct kvaser_usb
*dev
)
685 struct kvaser_msg msg
;
688 err
= kvaser_usb_send_simple_msg(dev
, CMD_GET_CARD_INFO
, 0);
692 err
= kvaser_usb_wait_msg(dev
, CMD_GET_CARD_INFO_REPLY
, &msg
);
696 dev
->nchannels
= msg
.u
.cardinfo
.nchannels
;
697 if ((dev
->nchannels
> MAX_NET_DEVICES
) ||
698 (dev
->family
== KVASER_USBCAN
&&
699 dev
->nchannels
> MAX_USBCAN_NET_DEVICES
))
705 static void kvaser_usb_tx_acknowledge(const struct kvaser_usb
*dev
,
706 const struct kvaser_msg
*msg
)
708 struct net_device_stats
*stats
;
709 struct kvaser_usb_tx_urb_context
*context
;
710 struct kvaser_usb_net_priv
*priv
;
712 struct can_frame
*cf
;
716 channel
= msg
->u
.tx_acknowledge_header
.channel
;
717 tid
= msg
->u
.tx_acknowledge_header
.tid
;
719 if (channel
>= dev
->nchannels
) {
720 dev_err(dev
->udev
->dev
.parent
,
721 "Invalid channel number (%d)\n", channel
);
725 priv
= dev
->nets
[channel
];
727 if (!netif_device_present(priv
->netdev
))
730 stats
= &priv
->netdev
->stats
;
732 context
= &priv
->tx_contexts
[tid
% dev
->max_tx_urbs
];
734 /* Sometimes the state change doesn't come after a bus-off event */
735 if (priv
->can
.restart_ms
&&
736 (priv
->can
.state
>= CAN_STATE_BUS_OFF
)) {
737 skb
= alloc_can_err_skb(priv
->netdev
, &cf
);
739 cf
->can_id
|= CAN_ERR_RESTARTED
;
742 stats
->rx_bytes
+= cf
->can_dlc
;
745 netdev_err(priv
->netdev
,
746 "No memory left for err_skb\n");
749 priv
->can
.can_stats
.restarts
++;
750 netif_carrier_on(priv
->netdev
);
752 priv
->can
.state
= CAN_STATE_ERROR_ACTIVE
;
756 stats
->tx_bytes
+= context
->dlc
;
758 spin_lock_irqsave(&priv
->tx_contexts_lock
, flags
);
760 can_get_echo_skb(priv
->netdev
, context
->echo_index
);
761 context
->echo_index
= dev
->max_tx_urbs
;
762 --priv
->active_tx_contexts
;
763 netif_wake_queue(priv
->netdev
);
765 spin_unlock_irqrestore(&priv
->tx_contexts_lock
, flags
);
768 static void kvaser_usb_simple_msg_callback(struct urb
*urb
)
770 struct net_device
*netdev
= urb
->context
;
772 kfree(urb
->transfer_buffer
);
775 netdev_warn(netdev
, "urb status received: %d\n",
779 static int kvaser_usb_simple_msg_async(struct kvaser_usb_net_priv
*priv
,
782 struct kvaser_usb
*dev
= priv
->dev
;
783 struct net_device
*netdev
= priv
->netdev
;
784 struct kvaser_msg
*msg
;
789 urb
= usb_alloc_urb(0, GFP_ATOMIC
);
791 netdev_err(netdev
, "No memory left for URBs\n");
795 buf
= kmalloc(sizeof(struct kvaser_msg
), GFP_ATOMIC
);
801 msg
= (struct kvaser_msg
*)buf
;
802 msg
->len
= MSG_HEADER_LEN
+ sizeof(struct kvaser_msg_simple
);
804 msg
->u
.simple
.channel
= priv
->channel
;
806 usb_fill_bulk_urb(urb
, dev
->udev
,
807 usb_sndbulkpipe(dev
->udev
,
808 dev
->bulk_out
->bEndpointAddress
),
810 kvaser_usb_simple_msg_callback
, netdev
);
811 usb_anchor_urb(urb
, &priv
->tx_submitted
);
813 err
= usb_submit_urb(urb
, GFP_ATOMIC
);
815 netdev_err(netdev
, "Error transmitting URB\n");
816 usb_unanchor_urb(urb
);
826 static void kvaser_usb_rx_error_update_can_state(struct kvaser_usb_net_priv
*priv
,
827 const struct kvaser_usb_error_summary
*es
,
828 struct can_frame
*cf
)
830 struct kvaser_usb
*dev
= priv
->dev
;
831 struct net_device_stats
*stats
= &priv
->netdev
->stats
;
832 enum can_state cur_state
, new_state
, tx_state
, rx_state
;
834 netdev_dbg(priv
->netdev
, "Error status: 0x%02x\n", es
->status
);
836 new_state
= cur_state
= priv
->can
.state
;
838 if (es
->status
& (M16C_STATE_BUS_OFF
| M16C_STATE_BUS_RESET
))
839 new_state
= CAN_STATE_BUS_OFF
;
840 else if (es
->status
& M16C_STATE_BUS_PASSIVE
)
841 new_state
= CAN_STATE_ERROR_PASSIVE
;
842 else if (es
->status
& M16C_STATE_BUS_ERROR
) {
843 /* Guard against spurious error events after a busoff */
844 if (cur_state
< CAN_STATE_BUS_OFF
) {
845 if ((es
->txerr
>= 128) || (es
->rxerr
>= 128))
846 new_state
= CAN_STATE_ERROR_PASSIVE
;
847 else if ((es
->txerr
>= 96) || (es
->rxerr
>= 96))
848 new_state
= CAN_STATE_ERROR_WARNING
;
849 else if (cur_state
> CAN_STATE_ERROR_ACTIVE
)
850 new_state
= CAN_STATE_ERROR_ACTIVE
;
855 new_state
= CAN_STATE_ERROR_ACTIVE
;
857 if (new_state
!= cur_state
) {
858 tx_state
= (es
->txerr
>= es
->rxerr
) ? new_state
: 0;
859 rx_state
= (es
->txerr
<= es
->rxerr
) ? new_state
: 0;
861 can_change_state(priv
->netdev
, cf
, tx_state
, rx_state
);
864 if (priv
->can
.restart_ms
&&
865 (cur_state
>= CAN_STATE_BUS_OFF
) &&
866 (new_state
< CAN_STATE_BUS_OFF
)) {
867 priv
->can
.can_stats
.restarts
++;
870 switch (dev
->family
) {
872 if (es
->leaf
.error_factor
) {
873 priv
->can
.can_stats
.bus_error
++;
878 if (es
->usbcan
.error_state
& USBCAN_ERROR_STATE_TX_ERROR
)
880 if (es
->usbcan
.error_state
& USBCAN_ERROR_STATE_RX_ERROR
)
882 if (es
->usbcan
.error_state
& USBCAN_ERROR_STATE_BUSERROR
) {
883 priv
->can
.can_stats
.bus_error
++;
888 priv
->bec
.txerr
= es
->txerr
;
889 priv
->bec
.rxerr
= es
->rxerr
;
892 static void kvaser_usb_rx_error(const struct kvaser_usb
*dev
,
893 const struct kvaser_usb_error_summary
*es
)
895 struct can_frame
*cf
, tmp_cf
= { .can_id
= CAN_ERR_FLAG
, .can_dlc
= CAN_ERR_DLC
};
897 struct net_device_stats
*stats
;
898 struct kvaser_usb_net_priv
*priv
;
899 enum can_state old_state
, new_state
;
901 if (es
->channel
>= dev
->nchannels
) {
902 dev_err(dev
->udev
->dev
.parent
,
903 "Invalid channel number (%d)\n", es
->channel
);
907 priv
= dev
->nets
[es
->channel
];
908 stats
= &priv
->netdev
->stats
;
910 /* Update all of the can interface's state and error counters before
911 * trying any memory allocation that can actually fail with -ENOMEM.
913 * We send a temporary stack-allocated error can frame to
914 * can_change_state() for the very same reason.
916 * TODO: Split can_change_state() responsibility between updating the
917 * can interface's state and counters, and the setting up of can error
918 * frame ID and data to userspace. Remove stack allocation afterwards.
920 old_state
= priv
->can
.state
;
921 kvaser_usb_rx_error_update_can_state(priv
, es
, &tmp_cf
);
922 new_state
= priv
->can
.state
;
924 skb
= alloc_can_err_skb(priv
->netdev
, &cf
);
929 memcpy(cf
, &tmp_cf
, sizeof(*cf
));
931 if (new_state
!= old_state
) {
933 (M16C_STATE_BUS_OFF
| M16C_STATE_BUS_RESET
)) {
934 if (!priv
->can
.restart_ms
)
935 kvaser_usb_simple_msg_async(priv
, CMD_STOP_CHIP
);
936 netif_carrier_off(priv
->netdev
);
939 if (priv
->can
.restart_ms
&&
940 (old_state
>= CAN_STATE_BUS_OFF
) &&
941 (new_state
< CAN_STATE_BUS_OFF
)) {
942 cf
->can_id
|= CAN_ERR_RESTARTED
;
943 netif_carrier_on(priv
->netdev
);
947 switch (dev
->family
) {
949 if (es
->leaf
.error_factor
) {
950 cf
->can_id
|= CAN_ERR_BUSERROR
| CAN_ERR_PROT
;
952 if (es
->leaf
.error_factor
& M16C_EF_ACKE
)
953 cf
->data
[3] = CAN_ERR_PROT_LOC_ACK
;
954 if (es
->leaf
.error_factor
& M16C_EF_CRCE
)
955 cf
->data
[3] = CAN_ERR_PROT_LOC_CRC_SEQ
;
956 if (es
->leaf
.error_factor
& M16C_EF_FORME
)
957 cf
->data
[2] |= CAN_ERR_PROT_FORM
;
958 if (es
->leaf
.error_factor
& M16C_EF_STFE
)
959 cf
->data
[2] |= CAN_ERR_PROT_STUFF
;
960 if (es
->leaf
.error_factor
& M16C_EF_BITE0
)
961 cf
->data
[2] |= CAN_ERR_PROT_BIT0
;
962 if (es
->leaf
.error_factor
& M16C_EF_BITE1
)
963 cf
->data
[2] |= CAN_ERR_PROT_BIT1
;
964 if (es
->leaf
.error_factor
& M16C_EF_TRE
)
965 cf
->data
[2] |= CAN_ERR_PROT_TX
;
969 if (es
->usbcan
.error_state
& USBCAN_ERROR_STATE_BUSERROR
) {
970 cf
->can_id
|= CAN_ERR_BUSERROR
;
975 cf
->data
[6] = es
->txerr
;
976 cf
->data
[7] = es
->rxerr
;
979 stats
->rx_bytes
+= cf
->can_dlc
;
983 /* For USBCAN, report error to userspace iff the channels's errors counter
984 * has changed, or we're the only channel seeing a bus error state.
986 static void kvaser_usbcan_conditionally_rx_error(const struct kvaser_usb
*dev
,
987 struct kvaser_usb_error_summary
*es
)
989 struct kvaser_usb_net_priv
*priv
;
993 channel
= es
->channel
;
994 if (channel
>= dev
->nchannels
) {
995 dev_err(dev
->udev
->dev
.parent
,
996 "Invalid channel number (%d)\n", channel
);
1000 priv
= dev
->nets
[channel
];
1001 report_error
= false;
1003 if (es
->txerr
!= priv
->bec
.txerr
) {
1004 es
->usbcan
.error_state
|= USBCAN_ERROR_STATE_TX_ERROR
;
1005 report_error
= true;
1007 if (es
->rxerr
!= priv
->bec
.rxerr
) {
1008 es
->usbcan
.error_state
|= USBCAN_ERROR_STATE_RX_ERROR
;
1009 report_error
= true;
1011 if ((es
->status
& M16C_STATE_BUS_ERROR
) &&
1012 !(es
->usbcan
.other_ch_status
& M16C_STATE_BUS_ERROR
)) {
1013 es
->usbcan
.error_state
|= USBCAN_ERROR_STATE_BUSERROR
;
1014 report_error
= true;
1018 kvaser_usb_rx_error(dev
, es
);
1021 static void kvaser_usbcan_rx_error(const struct kvaser_usb
*dev
,
1022 const struct kvaser_msg
*msg
)
1024 struct kvaser_usb_error_summary es
= { };
1027 /* Sometimes errors are sent as unsolicited chip state events */
1028 case CMD_CHIP_STATE_EVENT
:
1029 es
.channel
= msg
->u
.usbcan
.chip_state_event
.channel
;
1030 es
.status
= msg
->u
.usbcan
.chip_state_event
.status
;
1031 es
.txerr
= msg
->u
.usbcan
.chip_state_event
.tx_errors_count
;
1032 es
.rxerr
= msg
->u
.usbcan
.chip_state_event
.rx_errors_count
;
1033 kvaser_usbcan_conditionally_rx_error(dev
, &es
);
1036 case CMD_CAN_ERROR_EVENT
:
1038 es
.status
= msg
->u
.usbcan
.error_event
.status_ch0
;
1039 es
.txerr
= msg
->u
.usbcan
.error_event
.tx_errors_count_ch0
;
1040 es
.rxerr
= msg
->u
.usbcan
.error_event
.rx_errors_count_ch0
;
1041 es
.usbcan
.other_ch_status
=
1042 msg
->u
.usbcan
.error_event
.status_ch1
;
1043 kvaser_usbcan_conditionally_rx_error(dev
, &es
);
1045 /* The USBCAN firmware supports up to 2 channels.
1046 * Now that ch0 was checked, check if ch1 has any errors.
1048 if (dev
->nchannels
== MAX_USBCAN_NET_DEVICES
) {
1050 es
.status
= msg
->u
.usbcan
.error_event
.status_ch1
;
1051 es
.txerr
= msg
->u
.usbcan
.error_event
.tx_errors_count_ch1
;
1052 es
.rxerr
= msg
->u
.usbcan
.error_event
.rx_errors_count_ch1
;
1053 es
.usbcan
.other_ch_status
=
1054 msg
->u
.usbcan
.error_event
.status_ch0
;
1055 kvaser_usbcan_conditionally_rx_error(dev
, &es
);
1060 dev_err(dev
->udev
->dev
.parent
, "Invalid msg id (%d)\n",
1065 static void kvaser_leaf_rx_error(const struct kvaser_usb
*dev
,
1066 const struct kvaser_msg
*msg
)
1068 struct kvaser_usb_error_summary es
= { };
1071 case CMD_CAN_ERROR_EVENT
:
1072 es
.channel
= msg
->u
.leaf
.error_event
.channel
;
1073 es
.status
= msg
->u
.leaf
.error_event
.status
;
1074 es
.txerr
= msg
->u
.leaf
.error_event
.tx_errors_count
;
1075 es
.rxerr
= msg
->u
.leaf
.error_event
.rx_errors_count
;
1076 es
.leaf
.error_factor
= msg
->u
.leaf
.error_event
.error_factor
;
1078 case CMD_LEAF_LOG_MESSAGE
:
1079 es
.channel
= msg
->u
.leaf
.log_message
.channel
;
1080 es
.status
= msg
->u
.leaf
.log_message
.data
[0];
1081 es
.txerr
= msg
->u
.leaf
.log_message
.data
[2];
1082 es
.rxerr
= msg
->u
.leaf
.log_message
.data
[3];
1083 es
.leaf
.error_factor
= msg
->u
.leaf
.log_message
.data
[1];
1085 case CMD_CHIP_STATE_EVENT
:
1086 es
.channel
= msg
->u
.leaf
.chip_state_event
.channel
;
1087 es
.status
= msg
->u
.leaf
.chip_state_event
.status
;
1088 es
.txerr
= msg
->u
.leaf
.chip_state_event
.tx_errors_count
;
1089 es
.rxerr
= msg
->u
.leaf
.chip_state_event
.rx_errors_count
;
1090 es
.leaf
.error_factor
= 0;
1093 dev_err(dev
->udev
->dev
.parent
, "Invalid msg id (%d)\n",
1098 kvaser_usb_rx_error(dev
, &es
);
1101 static void kvaser_usb_rx_can_err(const struct kvaser_usb_net_priv
*priv
,
1102 const struct kvaser_msg
*msg
)
1104 struct can_frame
*cf
;
1105 struct sk_buff
*skb
;
1106 struct net_device_stats
*stats
= &priv
->netdev
->stats
;
1108 if (msg
->u
.rx_can_header
.flag
& (MSG_FLAG_ERROR_FRAME
|
1110 netdev_err(priv
->netdev
, "Unknown error (flags: 0x%02x)\n",
1111 msg
->u
.rx_can_header
.flag
);
1117 if (msg
->u
.rx_can_header
.flag
& MSG_FLAG_OVERRUN
) {
1118 stats
->rx_over_errors
++;
1121 skb
= alloc_can_err_skb(priv
->netdev
, &cf
);
1123 stats
->rx_dropped
++;
1127 cf
->can_id
|= CAN_ERR_CRTL
;
1128 cf
->data
[1] = CAN_ERR_CRTL_RX_OVERFLOW
;
1130 stats
->rx_packets
++;
1131 stats
->rx_bytes
+= cf
->can_dlc
;
1136 static void kvaser_usb_rx_can_msg(const struct kvaser_usb
*dev
,
1137 const struct kvaser_msg
*msg
)
1139 struct kvaser_usb_net_priv
*priv
;
1140 struct can_frame
*cf
;
1141 struct sk_buff
*skb
;
1142 struct net_device_stats
*stats
;
1143 u8 channel
= msg
->u
.rx_can_header
.channel
;
1144 const u8
*rx_msg
= NULL
; /* GCC */
1146 if (channel
>= dev
->nchannels
) {
1147 dev_err(dev
->udev
->dev
.parent
,
1148 "Invalid channel number (%d)\n", channel
);
1152 priv
= dev
->nets
[channel
];
1153 stats
= &priv
->netdev
->stats
;
1155 if ((msg
->u
.rx_can_header
.flag
& MSG_FLAG_ERROR_FRAME
) &&
1156 (dev
->family
== KVASER_LEAF
&& msg
->id
== CMD_LEAF_LOG_MESSAGE
)) {
1157 kvaser_leaf_rx_error(dev
, msg
);
1159 } else if (msg
->u
.rx_can_header
.flag
& (MSG_FLAG_ERROR_FRAME
|
1161 MSG_FLAG_OVERRUN
)) {
1162 kvaser_usb_rx_can_err(priv
, msg
);
1164 } else if (msg
->u
.rx_can_header
.flag
& ~MSG_FLAG_REMOTE_FRAME
) {
1165 netdev_warn(priv
->netdev
,
1166 "Unhandled frame (flags: 0x%02x)",
1167 msg
->u
.rx_can_header
.flag
);
1171 switch (dev
->family
) {
1173 rx_msg
= msg
->u
.leaf
.rx_can
.msg
;
1176 rx_msg
= msg
->u
.usbcan
.rx_can
.msg
;
1180 skb
= alloc_can_skb(priv
->netdev
, &cf
);
1182 stats
->tx_dropped
++;
1186 if (dev
->family
== KVASER_LEAF
&& msg
->id
== CMD_LEAF_LOG_MESSAGE
) {
1187 cf
->can_id
= le32_to_cpu(msg
->u
.leaf
.log_message
.id
);
1188 if (cf
->can_id
& KVASER_EXTENDED_FRAME
)
1189 cf
->can_id
&= CAN_EFF_MASK
| CAN_EFF_FLAG
;
1191 cf
->can_id
&= CAN_SFF_MASK
;
1193 cf
->can_dlc
= get_can_dlc(msg
->u
.leaf
.log_message
.dlc
);
1195 if (msg
->u
.leaf
.log_message
.flags
& MSG_FLAG_REMOTE_FRAME
)
1196 cf
->can_id
|= CAN_RTR_FLAG
;
1198 memcpy(cf
->data
, &msg
->u
.leaf
.log_message
.data
,
1201 cf
->can_id
= ((rx_msg
[0] & 0x1f) << 6) | (rx_msg
[1] & 0x3f);
1203 if (msg
->id
== CMD_RX_EXT_MESSAGE
) {
1205 cf
->can_id
|= ((rx_msg
[2] & 0x0f) << 14) |
1206 ((rx_msg
[3] & 0xff) << 6) |
1208 cf
->can_id
|= CAN_EFF_FLAG
;
1211 cf
->can_dlc
= get_can_dlc(rx_msg
[5]);
1213 if (msg
->u
.rx_can_header
.flag
& MSG_FLAG_REMOTE_FRAME
)
1214 cf
->can_id
|= CAN_RTR_FLAG
;
1216 memcpy(cf
->data
, &rx_msg
[6],
1220 stats
->rx_packets
++;
1221 stats
->rx_bytes
+= cf
->can_dlc
;
1225 static void kvaser_usb_start_chip_reply(const struct kvaser_usb
*dev
,
1226 const struct kvaser_msg
*msg
)
1228 struct kvaser_usb_net_priv
*priv
;
1229 u8 channel
= msg
->u
.simple
.channel
;
1231 if (channel
>= dev
->nchannels
) {
1232 dev_err(dev
->udev
->dev
.parent
,
1233 "Invalid channel number (%d)\n", channel
);
1237 priv
= dev
->nets
[channel
];
1239 if (completion_done(&priv
->start_comp
) &&
1240 netif_queue_stopped(priv
->netdev
)) {
1241 netif_wake_queue(priv
->netdev
);
1243 netif_start_queue(priv
->netdev
);
1244 complete(&priv
->start_comp
);
1248 static void kvaser_usb_stop_chip_reply(const struct kvaser_usb
*dev
,
1249 const struct kvaser_msg
*msg
)
1251 struct kvaser_usb_net_priv
*priv
;
1252 u8 channel
= msg
->u
.simple
.channel
;
1254 if (channel
>= dev
->nchannels
) {
1255 dev_err(dev
->udev
->dev
.parent
,
1256 "Invalid channel number (%d)\n", channel
);
1260 priv
= dev
->nets
[channel
];
1262 complete(&priv
->stop_comp
);
1265 static void kvaser_usb_handle_message(const struct kvaser_usb
*dev
,
1266 const struct kvaser_msg
*msg
)
1269 case CMD_START_CHIP_REPLY
:
1270 kvaser_usb_start_chip_reply(dev
, msg
);
1273 case CMD_STOP_CHIP_REPLY
:
1274 kvaser_usb_stop_chip_reply(dev
, msg
);
1277 case CMD_RX_STD_MESSAGE
:
1278 case CMD_RX_EXT_MESSAGE
:
1279 kvaser_usb_rx_can_msg(dev
, msg
);
1282 case CMD_LEAF_LOG_MESSAGE
:
1283 if (dev
->family
!= KVASER_LEAF
)
1285 kvaser_usb_rx_can_msg(dev
, msg
);
1288 case CMD_CHIP_STATE_EVENT
:
1289 case CMD_CAN_ERROR_EVENT
:
1290 if (dev
->family
== KVASER_LEAF
)
1291 kvaser_leaf_rx_error(dev
, msg
);
1293 kvaser_usbcan_rx_error(dev
, msg
);
1296 case CMD_TX_ACKNOWLEDGE
:
1297 kvaser_usb_tx_acknowledge(dev
, msg
);
1300 /* Ignored messages */
1301 case CMD_USBCAN_CLOCK_OVERFLOW_EVENT
:
1302 if (dev
->family
!= KVASER_USBCAN
)
1307 warn
: dev_warn(dev
->udev
->dev
.parent
,
1308 "Unhandled message (%d)\n", msg
->id
);
1313 static void kvaser_usb_read_bulk_callback(struct urb
*urb
)
1315 struct kvaser_usb
*dev
= urb
->context
;
1316 struct kvaser_msg
*msg
;
1320 switch (urb
->status
) {
1327 dev_info(dev
->udev
->dev
.parent
, "Rx URB aborted (%d)\n",
1332 while (pos
<= urb
->actual_length
- MSG_HEADER_LEN
) {
1333 msg
= urb
->transfer_buffer
+ pos
;
1335 /* The Kvaser firmware can only read and write messages that
1336 * does not cross the USB's endpoint wMaxPacketSize boundary.
1337 * If a follow-up command crosses such boundary, firmware puts
1338 * a placeholder zero-length command in its place then aligns
1339 * the real command to the next max packet size.
1341 * Handle such cases or we're going to miss a significant
1342 * number of events in case of a heavy rx load on the bus.
1344 if (msg
->len
== 0) {
1345 pos
= round_up(pos
, le16_to_cpu(dev
->bulk_in
->
1350 if (pos
+ msg
->len
> urb
->actual_length
) {
1351 dev_err(dev
->udev
->dev
.parent
, "Format error\n");
1355 kvaser_usb_handle_message(dev
, msg
);
1360 usb_fill_bulk_urb(urb
, dev
->udev
,
1361 usb_rcvbulkpipe(dev
->udev
,
1362 dev
->bulk_in
->bEndpointAddress
),
1363 urb
->transfer_buffer
, RX_BUFFER_SIZE
,
1364 kvaser_usb_read_bulk_callback
, dev
);
1366 err
= usb_submit_urb(urb
, GFP_ATOMIC
);
1367 if (err
== -ENODEV
) {
1368 for (i
= 0; i
< dev
->nchannels
; i
++) {
1372 netif_device_detach(dev
->nets
[i
]->netdev
);
1375 dev_err(dev
->udev
->dev
.parent
,
1376 "Failed resubmitting read bulk urb: %d\n", err
);
1382 static int kvaser_usb_setup_rx_urbs(struct kvaser_usb
*dev
)
1386 if (dev
->rxinitdone
)
1389 for (i
= 0; i
< MAX_RX_URBS
; i
++) {
1390 struct urb
*urb
= NULL
;
1394 urb
= usb_alloc_urb(0, GFP_KERNEL
);
1396 dev_warn(dev
->udev
->dev
.parent
,
1397 "No memory left for URBs\n");
1402 buf
= usb_alloc_coherent(dev
->udev
, RX_BUFFER_SIZE
,
1403 GFP_KERNEL
, &buf_dma
);
1405 dev_warn(dev
->udev
->dev
.parent
,
1406 "No memory left for USB buffer\n");
1412 usb_fill_bulk_urb(urb
, dev
->udev
,
1413 usb_rcvbulkpipe(dev
->udev
,
1414 dev
->bulk_in
->bEndpointAddress
),
1415 buf
, RX_BUFFER_SIZE
,
1416 kvaser_usb_read_bulk_callback
,
1418 urb
->transfer_dma
= buf_dma
;
1419 urb
->transfer_flags
|= URB_NO_TRANSFER_DMA_MAP
;
1420 usb_anchor_urb(urb
, &dev
->rx_submitted
);
1422 err
= usb_submit_urb(urb
, GFP_KERNEL
);
1424 usb_unanchor_urb(urb
);
1425 usb_free_coherent(dev
->udev
, RX_BUFFER_SIZE
, buf
,
1431 dev
->rxbuf
[i
] = buf
;
1432 dev
->rxbuf_dma
[i
] = buf_dma
;
1438 dev_warn(dev
->udev
->dev
.parent
,
1439 "Cannot setup read URBs, error %d\n", err
);
1441 } else if (i
< MAX_RX_URBS
) {
1442 dev_warn(dev
->udev
->dev
.parent
,
1443 "RX performances may be slow\n");
1446 dev
->rxinitdone
= true;
1451 static int kvaser_usb_set_opt_mode(const struct kvaser_usb_net_priv
*priv
)
1453 struct kvaser_msg
*msg
;
1456 msg
= kmalloc(sizeof(*msg
), GFP_KERNEL
);
1460 msg
->id
= CMD_SET_CTRL_MODE
;
1461 msg
->len
= MSG_HEADER_LEN
+ sizeof(struct kvaser_msg_ctrl_mode
);
1462 msg
->u
.ctrl_mode
.tid
= 0xff;
1463 msg
->u
.ctrl_mode
.channel
= priv
->channel
;
1465 if (priv
->can
.ctrlmode
& CAN_CTRLMODE_LISTENONLY
)
1466 msg
->u
.ctrl_mode
.ctrl_mode
= KVASER_CTRL_MODE_SILENT
;
1468 msg
->u
.ctrl_mode
.ctrl_mode
= KVASER_CTRL_MODE_NORMAL
;
1470 rc
= kvaser_usb_send_msg(priv
->dev
, msg
);
1476 static int kvaser_usb_start_chip(struct kvaser_usb_net_priv
*priv
)
1480 init_completion(&priv
->start_comp
);
1482 err
= kvaser_usb_send_simple_msg(priv
->dev
, CMD_START_CHIP
,
1487 if (!wait_for_completion_timeout(&priv
->start_comp
,
1488 msecs_to_jiffies(START_TIMEOUT
)))
1494 static int kvaser_usb_open(struct net_device
*netdev
)
1496 struct kvaser_usb_net_priv
*priv
= netdev_priv(netdev
);
1497 struct kvaser_usb
*dev
= priv
->dev
;
1500 err
= open_candev(netdev
);
1504 err
= kvaser_usb_setup_rx_urbs(dev
);
1508 err
= kvaser_usb_set_opt_mode(priv
);
1512 err
= kvaser_usb_start_chip(priv
);
1514 netdev_warn(netdev
, "Cannot start device, error %d\n", err
);
1518 priv
->can
.state
= CAN_STATE_ERROR_ACTIVE
;
1523 close_candev(netdev
);
1527 static void kvaser_usb_reset_tx_urb_contexts(struct kvaser_usb_net_priv
*priv
)
1531 max_tx_urbs
= priv
->dev
->max_tx_urbs
;
1533 priv
->active_tx_contexts
= 0;
1534 for (i
= 0; i
< max_tx_urbs
; i
++)
1535 priv
->tx_contexts
[i
].echo_index
= max_tx_urbs
;
1538 /* This method might sleep. Do not call it in the atomic context
1539 * of URB completions.
1541 static void kvaser_usb_unlink_tx_urbs(struct kvaser_usb_net_priv
*priv
)
1543 usb_kill_anchored_urbs(&priv
->tx_submitted
);
1544 kvaser_usb_reset_tx_urb_contexts(priv
);
1547 static void kvaser_usb_unlink_all_urbs(struct kvaser_usb
*dev
)
1551 usb_kill_anchored_urbs(&dev
->rx_submitted
);
1553 for (i
= 0; i
< MAX_RX_URBS
; i
++)
1554 usb_free_coherent(dev
->udev
, RX_BUFFER_SIZE
,
1558 for (i
= 0; i
< dev
->nchannels
; i
++) {
1559 struct kvaser_usb_net_priv
*priv
= dev
->nets
[i
];
1562 kvaser_usb_unlink_tx_urbs(priv
);
1566 static int kvaser_usb_stop_chip(struct kvaser_usb_net_priv
*priv
)
1570 init_completion(&priv
->stop_comp
);
1572 err
= kvaser_usb_send_simple_msg(priv
->dev
, CMD_STOP_CHIP
,
1577 if (!wait_for_completion_timeout(&priv
->stop_comp
,
1578 msecs_to_jiffies(STOP_TIMEOUT
)))
1584 static int kvaser_usb_flush_queue(struct kvaser_usb_net_priv
*priv
)
1586 struct kvaser_msg
*msg
;
1589 msg
= kmalloc(sizeof(*msg
), GFP_KERNEL
);
1593 msg
->id
= CMD_FLUSH_QUEUE
;
1594 msg
->len
= MSG_HEADER_LEN
+ sizeof(struct kvaser_msg_flush_queue
);
1595 msg
->u
.flush_queue
.channel
= priv
->channel
;
1596 msg
->u
.flush_queue
.flags
= 0x00;
1598 rc
= kvaser_usb_send_msg(priv
->dev
, msg
);
1604 static int kvaser_usb_close(struct net_device
*netdev
)
1606 struct kvaser_usb_net_priv
*priv
= netdev_priv(netdev
);
1607 struct kvaser_usb
*dev
= priv
->dev
;
1610 netif_stop_queue(netdev
);
1612 err
= kvaser_usb_flush_queue(priv
);
1614 netdev_warn(netdev
, "Cannot flush queue, error %d\n", err
);
1616 if (kvaser_usb_send_simple_msg(dev
, CMD_RESET_CHIP
, priv
->channel
))
1617 netdev_warn(netdev
, "Cannot reset card, error %d\n", err
);
1619 err
= kvaser_usb_stop_chip(priv
);
1621 netdev_warn(netdev
, "Cannot stop device, error %d\n", err
);
1623 /* reset tx contexts */
1624 kvaser_usb_unlink_tx_urbs(priv
);
1626 priv
->can
.state
= CAN_STATE_STOPPED
;
1627 close_candev(priv
->netdev
);
1632 static void kvaser_usb_write_bulk_callback(struct urb
*urb
)
1634 struct kvaser_usb_tx_urb_context
*context
= urb
->context
;
1635 struct kvaser_usb_net_priv
*priv
;
1636 struct net_device
*netdev
;
1638 if (WARN_ON(!context
))
1641 priv
= context
->priv
;
1642 netdev
= priv
->netdev
;
1644 kfree(urb
->transfer_buffer
);
1646 if (!netif_device_present(netdev
))
1650 netdev_info(netdev
, "Tx URB aborted (%d)\n", urb
->status
);
1653 static netdev_tx_t
kvaser_usb_start_xmit(struct sk_buff
*skb
,
1654 struct net_device
*netdev
)
1656 struct kvaser_usb_net_priv
*priv
= netdev_priv(netdev
);
1657 struct kvaser_usb
*dev
= priv
->dev
;
1658 struct net_device_stats
*stats
= &netdev
->stats
;
1659 struct can_frame
*cf
= (struct can_frame
*)skb
->data
;
1660 struct kvaser_usb_tx_urb_context
*context
= NULL
;
1663 struct kvaser_msg
*msg
;
1664 int i
, err
, ret
= NETDEV_TX_OK
;
1665 u8
*msg_tx_can_flags
= NULL
; /* GCC */
1666 unsigned long flags
;
1668 if (can_dropped_invalid_skb(netdev
, skb
))
1669 return NETDEV_TX_OK
;
1671 urb
= usb_alloc_urb(0, GFP_ATOMIC
);
1673 netdev_err(netdev
, "No memory left for URBs\n");
1674 stats
->tx_dropped
++;
1676 return NETDEV_TX_OK
;
1679 buf
= kmalloc(sizeof(struct kvaser_msg
), GFP_ATOMIC
);
1681 stats
->tx_dropped
++;
1687 msg
->len
= MSG_HEADER_LEN
+ sizeof(struct kvaser_msg_tx_can
);
1688 msg
->u
.tx_can
.channel
= priv
->channel
;
1690 switch (dev
->family
) {
1692 msg_tx_can_flags
= &msg
->u
.tx_can
.leaf
.flags
;
1695 msg_tx_can_flags
= &msg
->u
.tx_can
.usbcan
.flags
;
1699 *msg_tx_can_flags
= 0;
1701 if (cf
->can_id
& CAN_EFF_FLAG
) {
1702 msg
->id
= CMD_TX_EXT_MESSAGE
;
1703 msg
->u
.tx_can
.msg
[0] = (cf
->can_id
>> 24) & 0x1f;
1704 msg
->u
.tx_can
.msg
[1] = (cf
->can_id
>> 18) & 0x3f;
1705 msg
->u
.tx_can
.msg
[2] = (cf
->can_id
>> 14) & 0x0f;
1706 msg
->u
.tx_can
.msg
[3] = (cf
->can_id
>> 6) & 0xff;
1707 msg
->u
.tx_can
.msg
[4] = cf
->can_id
& 0x3f;
1709 msg
->id
= CMD_TX_STD_MESSAGE
;
1710 msg
->u
.tx_can
.msg
[0] = (cf
->can_id
>> 6) & 0x1f;
1711 msg
->u
.tx_can
.msg
[1] = cf
->can_id
& 0x3f;
1714 msg
->u
.tx_can
.msg
[5] = cf
->can_dlc
;
1715 memcpy(&msg
->u
.tx_can
.msg
[6], cf
->data
, cf
->can_dlc
);
1717 if (cf
->can_id
& CAN_RTR_FLAG
)
1718 *msg_tx_can_flags
|= MSG_FLAG_REMOTE_FRAME
;
1720 spin_lock_irqsave(&priv
->tx_contexts_lock
, flags
);
1721 for (i
= 0; i
< dev
->max_tx_urbs
; i
++) {
1722 if (priv
->tx_contexts
[i
].echo_index
== dev
->max_tx_urbs
) {
1723 context
= &priv
->tx_contexts
[i
];
1725 context
->echo_index
= i
;
1726 can_put_echo_skb(skb
, netdev
, context
->echo_index
);
1727 ++priv
->active_tx_contexts
;
1728 if (priv
->active_tx_contexts
>= dev
->max_tx_urbs
)
1729 netif_stop_queue(netdev
);
1734 spin_unlock_irqrestore(&priv
->tx_contexts_lock
, flags
);
1736 /* This should never happen; it implies a flow control bug */
1738 netdev_warn(netdev
, "cannot find free context\n");
1741 ret
= NETDEV_TX_BUSY
;
1745 context
->priv
= priv
;
1746 context
->dlc
= cf
->can_dlc
;
1748 msg
->u
.tx_can
.tid
= context
->echo_index
;
1750 usb_fill_bulk_urb(urb
, dev
->udev
,
1751 usb_sndbulkpipe(dev
->udev
,
1752 dev
->bulk_out
->bEndpointAddress
),
1754 kvaser_usb_write_bulk_callback
, context
);
1755 usb_anchor_urb(urb
, &priv
->tx_submitted
);
1757 err
= usb_submit_urb(urb
, GFP_ATOMIC
);
1758 if (unlikely(err
)) {
1759 spin_lock_irqsave(&priv
->tx_contexts_lock
, flags
);
1761 can_free_echo_skb(netdev
, context
->echo_index
);
1762 context
->echo_index
= dev
->max_tx_urbs
;
1763 --priv
->active_tx_contexts
;
1764 netif_wake_queue(netdev
);
1766 spin_unlock_irqrestore(&priv
->tx_contexts_lock
, flags
);
1768 usb_unanchor_urb(urb
);
1770 stats
->tx_dropped
++;
1773 netif_device_detach(netdev
);
1775 netdev_warn(netdev
, "Failed tx_urb %d\n", err
);
1787 static const struct net_device_ops kvaser_usb_netdev_ops
= {
1788 .ndo_open
= kvaser_usb_open
,
1789 .ndo_stop
= kvaser_usb_close
,
1790 .ndo_start_xmit
= kvaser_usb_start_xmit
,
1791 .ndo_change_mtu
= can_change_mtu
,
1794 static const struct can_bittiming_const kvaser_usb_bittiming_const
= {
1795 .name
= "kvaser_usb",
1796 .tseg1_min
= KVASER_USB_TSEG1_MIN
,
1797 .tseg1_max
= KVASER_USB_TSEG1_MAX
,
1798 .tseg2_min
= KVASER_USB_TSEG2_MIN
,
1799 .tseg2_max
= KVASER_USB_TSEG2_MAX
,
1800 .sjw_max
= KVASER_USB_SJW_MAX
,
1801 .brp_min
= KVASER_USB_BRP_MIN
,
1802 .brp_max
= KVASER_USB_BRP_MAX
,
1803 .brp_inc
= KVASER_USB_BRP_INC
,
1806 static int kvaser_usb_set_bittiming(struct net_device
*netdev
)
1808 struct kvaser_usb_net_priv
*priv
= netdev_priv(netdev
);
1809 struct can_bittiming
*bt
= &priv
->can
.bittiming
;
1810 struct kvaser_usb
*dev
= priv
->dev
;
1811 struct kvaser_msg
*msg
;
1814 msg
= kmalloc(sizeof(*msg
), GFP_KERNEL
);
1818 msg
->id
= CMD_SET_BUS_PARAMS
;
1819 msg
->len
= MSG_HEADER_LEN
+ sizeof(struct kvaser_msg_busparams
);
1820 msg
->u
.busparams
.channel
= priv
->channel
;
1821 msg
->u
.busparams
.tid
= 0xff;
1822 msg
->u
.busparams
.bitrate
= cpu_to_le32(bt
->bitrate
);
1823 msg
->u
.busparams
.sjw
= bt
->sjw
;
1824 msg
->u
.busparams
.tseg1
= bt
->prop_seg
+ bt
->phase_seg1
;
1825 msg
->u
.busparams
.tseg2
= bt
->phase_seg2
;
1827 if (priv
->can
.ctrlmode
& CAN_CTRLMODE_3_SAMPLES
)
1828 msg
->u
.busparams
.no_samp
= 3;
1830 msg
->u
.busparams
.no_samp
= 1;
1832 rc
= kvaser_usb_send_msg(dev
, msg
);
1838 static int kvaser_usb_set_mode(struct net_device
*netdev
,
1841 struct kvaser_usb_net_priv
*priv
= netdev_priv(netdev
);
1845 case CAN_MODE_START
:
1846 err
= kvaser_usb_simple_msg_async(priv
, CMD_START_CHIP
);
1857 static int kvaser_usb_get_berr_counter(const struct net_device
*netdev
,
1858 struct can_berr_counter
*bec
)
1860 struct kvaser_usb_net_priv
*priv
= netdev_priv(netdev
);
1867 static void kvaser_usb_remove_interfaces(struct kvaser_usb
*dev
)
1871 for (i
= 0; i
< dev
->nchannels
; i
++) {
1875 unregister_candev(dev
->nets
[i
]->netdev
);
1878 kvaser_usb_unlink_all_urbs(dev
);
1880 for (i
= 0; i
< dev
->nchannels
; i
++) {
1884 free_candev(dev
->nets
[i
]->netdev
);
1888 static int kvaser_usb_init_one(struct usb_interface
*intf
,
1889 const struct usb_device_id
*id
, int channel
)
1891 struct kvaser_usb
*dev
= usb_get_intfdata(intf
);
1892 struct net_device
*netdev
;
1893 struct kvaser_usb_net_priv
*priv
;
1896 err
= kvaser_usb_send_simple_msg(dev
, CMD_RESET_CHIP
, channel
);
1900 netdev
= alloc_candev(sizeof(*priv
) +
1901 dev
->max_tx_urbs
* sizeof(*priv
->tx_contexts
),
1904 dev_err(&intf
->dev
, "Cannot alloc candev\n");
1908 priv
= netdev_priv(netdev
);
1910 init_usb_anchor(&priv
->tx_submitted
);
1911 init_completion(&priv
->start_comp
);
1912 init_completion(&priv
->stop_comp
);
1915 priv
->netdev
= netdev
;
1916 priv
->channel
= channel
;
1918 spin_lock_init(&priv
->tx_contexts_lock
);
1919 kvaser_usb_reset_tx_urb_contexts(priv
);
1921 priv
->can
.state
= CAN_STATE_STOPPED
;
1922 priv
->can
.clock
.freq
= CAN_USB_CLOCK
;
1923 priv
->can
.bittiming_const
= &kvaser_usb_bittiming_const
;
1924 priv
->can
.do_set_bittiming
= kvaser_usb_set_bittiming
;
1925 priv
->can
.do_set_mode
= kvaser_usb_set_mode
;
1926 if (id
->driver_info
& KVASER_HAS_TXRX_ERRORS
)
1927 priv
->can
.do_get_berr_counter
= kvaser_usb_get_berr_counter
;
1928 priv
->can
.ctrlmode_supported
= CAN_CTRLMODE_3_SAMPLES
;
1929 if (id
->driver_info
& KVASER_HAS_SILENT_MODE
)
1930 priv
->can
.ctrlmode_supported
|= CAN_CTRLMODE_LISTENONLY
;
1932 netdev
->flags
|= IFF_ECHO
;
1934 netdev
->netdev_ops
= &kvaser_usb_netdev_ops
;
1936 SET_NETDEV_DEV(netdev
, &intf
->dev
);
1937 netdev
->dev_id
= channel
;
1939 dev
->nets
[channel
] = priv
;
1941 err
= register_candev(netdev
);
1943 dev_err(&intf
->dev
, "Failed to register can device\n");
1944 free_candev(netdev
);
1945 dev
->nets
[channel
] = NULL
;
1949 netdev_dbg(netdev
, "device registered\n");
1954 static int kvaser_usb_get_endpoints(const struct usb_interface
*intf
,
1955 struct usb_endpoint_descriptor
**in
,
1956 struct usb_endpoint_descriptor
**out
)
1958 const struct usb_host_interface
*iface_desc
;
1959 struct usb_endpoint_descriptor
*endpoint
;
1962 iface_desc
= &intf
->altsetting
[0];
1964 for (i
= 0; i
< iface_desc
->desc
.bNumEndpoints
; ++i
) {
1965 endpoint
= &iface_desc
->endpoint
[i
].desc
;
1967 if (!*in
&& usb_endpoint_is_bulk_in(endpoint
))
1970 if (!*out
&& usb_endpoint_is_bulk_out(endpoint
))
1973 /* use first bulk endpoint for in and out */
1981 static int kvaser_usb_probe(struct usb_interface
*intf
,
1982 const struct usb_device_id
*id
)
1984 struct kvaser_usb
*dev
;
1988 dev
= devm_kzalloc(&intf
->dev
, sizeof(*dev
), GFP_KERNEL
);
1992 if (kvaser_is_leaf(id
)) {
1993 dev
->family
= KVASER_LEAF
;
1994 } else if (kvaser_is_usbcan(id
)) {
1995 dev
->family
= KVASER_USBCAN
;
1998 "Product ID (%d) does not belong to any known Kvaser USB family",
2003 err
= kvaser_usb_get_endpoints(intf
, &dev
->bulk_in
, &dev
->bulk_out
);
2005 dev_err(&intf
->dev
, "Cannot get usb endpoint(s)");
2009 dev
->udev
= interface_to_usbdev(intf
);
2011 init_usb_anchor(&dev
->rx_submitted
);
2013 usb_set_intfdata(intf
, dev
);
2015 /* On some x86 laptops, plugging a Kvaser device again after
2016 * an unplug makes the firmware always ignore the very first
2017 * command. For such a case, provide some room for retries
2018 * instead of completely exiting the driver.
2021 err
= kvaser_usb_get_software_info(dev
);
2022 } while (--retry
&& err
== -ETIMEDOUT
);
2026 "Cannot get software infos, error %d\n", err
);
2030 dev_dbg(&intf
->dev
, "Firmware version: %d.%d.%d\n",
2031 ((dev
->fw_version
>> 24) & 0xff),
2032 ((dev
->fw_version
>> 16) & 0xff),
2033 (dev
->fw_version
& 0xffff));
2035 dev_dbg(&intf
->dev
, "Max oustanding tx = %d URBs\n", dev
->max_tx_urbs
);
2037 err
= kvaser_usb_get_card_info(dev
);
2040 "Cannot get card infos, error %d\n", err
);
2044 for (i
= 0; i
< dev
->nchannels
; i
++) {
2045 err
= kvaser_usb_init_one(intf
, id
, i
);
2047 kvaser_usb_remove_interfaces(dev
);
2055 static void kvaser_usb_disconnect(struct usb_interface
*intf
)
2057 struct kvaser_usb
*dev
= usb_get_intfdata(intf
);
2059 usb_set_intfdata(intf
, NULL
);
2064 kvaser_usb_remove_interfaces(dev
);
2067 static struct usb_driver kvaser_usb_driver
= {
2068 .name
= "kvaser_usb",
2069 .probe
= kvaser_usb_probe
,
2070 .disconnect
= kvaser_usb_disconnect
,
2071 .id_table
= kvaser_usb_table
,
2074 module_usb_driver(kvaser_usb_driver
);
2076 MODULE_AUTHOR("Olivier Sobrie <olivier@sobrie.be>");
2077 MODULE_DESCRIPTION("CAN driver for Kvaser CAN/USB devices");
2078 MODULE_LICENSE("GPL v2");