1 // SPDX-License-Identifier: GPL-2.0
2 /* Parts of this driver are based on the following:
3 * - Kvaser linux leaf driver (version 4.78)
4 * - CAN driver for esd CAN-USB/2
5 * - Kvaser linux usbcanII driver (version 5.3)
7 * Copyright (C) 2002-2018 KVASER AB, Sweden. All rights reserved.
8 * Copyright (C) 2010 Matthias Fuchs <matthias.fuchs@esd.eu>, esd gmbh
9 * Copyright (C) 2012 Olivier Sobrie <olivier@sobrie.be>
10 * Copyright (C) 2015 Valeo S.A.
13 #include <linux/completion.h>
14 #include <linux/device.h>
15 #include <linux/gfp.h>
16 #include <linux/jiffies.h>
17 #include <linux/kernel.h>
18 #include <linux/netdevice.h>
19 #include <linux/spinlock.h>
20 #include <linux/string.h>
21 #include <linux/types.h>
22 #include <linux/usb.h>
24 #include <linux/can.h>
25 #include <linux/can/dev.h>
26 #include <linux/can/error.h>
27 #include <linux/can/netlink.h>
29 #include "kvaser_usb.h"
31 /* Forward declaration */
32 static const struct kvaser_usb_dev_cfg kvaser_usb_leaf_dev_cfg
;
34 #define CAN_USB_CLOCK 8000000
35 #define MAX_USBCAN_NET_DEVICES 2
37 /* Command header size */
38 #define CMD_HEADER_LEN 2
40 /* Kvaser CAN message flags */
41 #define MSG_FLAG_ERROR_FRAME BIT(0)
42 #define MSG_FLAG_OVERRUN BIT(1)
43 #define MSG_FLAG_NERR BIT(2)
44 #define MSG_FLAG_WAKEUP BIT(3)
45 #define MSG_FLAG_REMOTE_FRAME BIT(4)
46 #define MSG_FLAG_RESERVED BIT(5)
47 #define MSG_FLAG_TX_ACK BIT(6)
48 #define MSG_FLAG_TX_REQUEST BIT(7)
50 /* CAN states (M16C CxSTRH register) */
51 #define M16C_STATE_BUS_RESET BIT(0)
52 #define M16C_STATE_BUS_ERROR BIT(4)
53 #define M16C_STATE_BUS_PASSIVE BIT(5)
54 #define M16C_STATE_BUS_OFF BIT(6)
56 /* Leaf/usbcan command ids */
57 #define CMD_RX_STD_MESSAGE 12
58 #define CMD_TX_STD_MESSAGE 13
59 #define CMD_RX_EXT_MESSAGE 14
60 #define CMD_TX_EXT_MESSAGE 15
61 #define CMD_SET_BUS_PARAMS 16
62 #define CMD_CHIP_STATE_EVENT 20
63 #define CMD_SET_CTRL_MODE 21
64 #define CMD_RESET_CHIP 24
65 #define CMD_START_CHIP 26
66 #define CMD_START_CHIP_REPLY 27
67 #define CMD_STOP_CHIP 28
68 #define CMD_STOP_CHIP_REPLY 29
70 #define CMD_USBCAN_CLOCK_OVERFLOW_EVENT 33
72 #define CMD_GET_CARD_INFO 34
73 #define CMD_GET_CARD_INFO_REPLY 35
74 #define CMD_GET_SOFTWARE_INFO 38
75 #define CMD_GET_SOFTWARE_INFO_REPLY 39
76 #define CMD_FLUSH_QUEUE 48
77 #define CMD_TX_ACKNOWLEDGE 50
78 #define CMD_CAN_ERROR_EVENT 51
79 #define CMD_FLUSH_QUEUE_REPLY 68
81 #define CMD_LEAF_LOG_MESSAGE 106
84 #define M16C_EF_ACKE BIT(0)
85 #define M16C_EF_CRCE BIT(1)
86 #define M16C_EF_FORME BIT(2)
87 #define M16C_EF_STFE BIT(3)
88 #define M16C_EF_BITE0 BIT(4)
89 #define M16C_EF_BITE1 BIT(5)
90 #define M16C_EF_RCVE BIT(6)
91 #define M16C_EF_TRE BIT(7)
93 /* Only Leaf-based devices can report M16C error factors,
94 * thus define our own error status flags for USBCANII
96 #define USBCAN_ERROR_STATE_NONE 0
97 #define USBCAN_ERROR_STATE_TX_ERROR BIT(0)
98 #define USBCAN_ERROR_STATE_RX_ERROR BIT(1)
99 #define USBCAN_ERROR_STATE_BUSERROR BIT(2)
101 /* bittiming parameters */
102 #define KVASER_USB_TSEG1_MIN 1
103 #define KVASER_USB_TSEG1_MAX 16
104 #define KVASER_USB_TSEG2_MIN 1
105 #define KVASER_USB_TSEG2_MAX 8
106 #define KVASER_USB_SJW_MAX 4
107 #define KVASER_USB_BRP_MIN 1
108 #define KVASER_USB_BRP_MAX 64
109 #define KVASER_USB_BRP_INC 1
112 #define KVASER_CTRL_MODE_NORMAL 1
113 #define KVASER_CTRL_MODE_SILENT 2
114 #define KVASER_CTRL_MODE_SELFRECEPTION 3
115 #define KVASER_CTRL_MODE_OFF 4
117 /* Extended CAN identifier flag */
118 #define KVASER_EXTENDED_FRAME BIT(31)
120 struct kvaser_cmd_simple
{
125 struct kvaser_cmd_cardinfo
{
128 __le32 serial_number
;
130 __le32 clock_resolution
;
145 struct leaf_cmd_softinfo
{
150 __le16 max_outstanding_tx
;
154 struct usbcan_cmd_softinfo
{
157 __le16 max_outstanding_tx
;
164 struct kvaser_cmd_busparams
{
174 struct kvaser_cmd_tx_can
{
190 struct kvaser_cmd_rx_can_header
{
195 struct leaf_cmd_rx_can
{
203 struct usbcan_cmd_rx_can
{
211 struct leaf_cmd_chip_state_event
{
223 struct usbcan_cmd_chip_state_event
{
235 struct kvaser_cmd_tx_acknowledge_header
{
240 struct leaf_cmd_error_event
{
252 struct usbcan_cmd_error_event
{
255 u8 tx_errors_count_ch0
;
256 u8 rx_errors_count_ch0
;
257 u8 tx_errors_count_ch1
;
258 u8 rx_errors_count_ch1
;
264 struct kvaser_cmd_ctrl_mode
{
271 struct kvaser_cmd_flush_queue
{
278 struct leaf_cmd_log_message
{
292 struct kvaser_cmd_simple simple
;
293 struct kvaser_cmd_cardinfo cardinfo
;
294 struct kvaser_cmd_busparams busparams
;
296 struct kvaser_cmd_rx_can_header rx_can_header
;
297 struct kvaser_cmd_tx_acknowledge_header tx_acknowledge_header
;
300 struct leaf_cmd_softinfo softinfo
;
301 struct leaf_cmd_rx_can rx_can
;
302 struct leaf_cmd_chip_state_event chip_state_event
;
303 struct leaf_cmd_error_event error_event
;
304 struct leaf_cmd_log_message log_message
;
308 struct usbcan_cmd_softinfo softinfo
;
309 struct usbcan_cmd_rx_can rx_can
;
310 struct usbcan_cmd_chip_state_event chip_state_event
;
311 struct usbcan_cmd_error_event error_event
;
314 struct kvaser_cmd_tx_can tx_can
;
315 struct kvaser_cmd_ctrl_mode ctrl_mode
;
316 struct kvaser_cmd_flush_queue flush_queue
;
320 /* Summary of a kvaser error event, for a unified Leaf/Usbcan error
321 * handling. Some discrepancies between the two families exist:
323 * - USBCAN firmware does not report M16C "error factors"
324 * - USBCAN controllers has difficulties reporting if the raised error
325 * event is for ch0 or ch1. They leave such arbitration to the OS
326 * driver by letting it compare error counters with previous values
327 * and decide the error event's channel. Thus for USBCAN, the channel
328 * field is only advisory.
330 struct kvaser_usb_err_summary
{
331 u8 channel
, status
, txerr
, rxerr
;
344 kvaser_usb_leaf_frame_to_cmd(const struct kvaser_usb_net_priv
*priv
,
345 const struct sk_buff
*skb
, int *frame_len
,
346 int *cmd_len
, u16 transid
)
348 struct kvaser_usb
*dev
= priv
->dev
;
349 struct kvaser_cmd
*cmd
;
350 u8
*cmd_tx_can_flags
= NULL
; /* GCC */
351 struct can_frame
*cf
= (struct can_frame
*)skb
->data
;
353 *frame_len
= cf
->can_dlc
;
355 cmd
= kmalloc(sizeof(*cmd
), GFP_ATOMIC
);
357 cmd
->u
.tx_can
.tid
= transid
& 0xff;
358 cmd
->len
= *cmd_len
= CMD_HEADER_LEN
+
359 sizeof(struct kvaser_cmd_tx_can
);
360 cmd
->u
.tx_can
.channel
= priv
->channel
;
362 switch (dev
->card_data
.leaf
.family
) {
364 cmd_tx_can_flags
= &cmd
->u
.tx_can
.leaf
.flags
;
367 cmd_tx_can_flags
= &cmd
->u
.tx_can
.usbcan
.flags
;
371 *cmd_tx_can_flags
= 0;
373 if (cf
->can_id
& CAN_EFF_FLAG
) {
374 cmd
->id
= CMD_TX_EXT_MESSAGE
;
375 cmd
->u
.tx_can
.data
[0] = (cf
->can_id
>> 24) & 0x1f;
376 cmd
->u
.tx_can
.data
[1] = (cf
->can_id
>> 18) & 0x3f;
377 cmd
->u
.tx_can
.data
[2] = (cf
->can_id
>> 14) & 0x0f;
378 cmd
->u
.tx_can
.data
[3] = (cf
->can_id
>> 6) & 0xff;
379 cmd
->u
.tx_can
.data
[4] = cf
->can_id
& 0x3f;
381 cmd
->id
= CMD_TX_STD_MESSAGE
;
382 cmd
->u
.tx_can
.data
[0] = (cf
->can_id
>> 6) & 0x1f;
383 cmd
->u
.tx_can
.data
[1] = cf
->can_id
& 0x3f;
386 cmd
->u
.tx_can
.data
[5] = cf
->can_dlc
;
387 memcpy(&cmd
->u
.tx_can
.data
[6], cf
->data
, cf
->can_dlc
);
389 if (cf
->can_id
& CAN_RTR_FLAG
)
390 *cmd_tx_can_flags
|= MSG_FLAG_REMOTE_FRAME
;
395 static int kvaser_usb_leaf_wait_cmd(const struct kvaser_usb
*dev
, u8 id
,
396 struct kvaser_cmd
*cmd
)
398 struct kvaser_cmd
*tmp
;
403 unsigned long to
= jiffies
+ msecs_to_jiffies(KVASER_USB_TIMEOUT
);
405 buf
= kzalloc(KVASER_USB_RX_BUFFER_SIZE
, GFP_KERNEL
);
410 err
= kvaser_usb_recv_cmd(dev
, buf
, KVASER_USB_RX_BUFFER_SIZE
,
416 while (pos
<= actual_len
- CMD_HEADER_LEN
) {
419 /* Handle commands crossing the USB endpoint max packet
420 * size boundary. Check kvaser_usb_read_bulk_callback()
421 * for further details.
426 (dev
->bulk_in
->wMaxPacketSize
));
430 if (pos
+ tmp
->len
> actual_len
) {
431 dev_err_ratelimited(&dev
->intf
->dev
,
437 memcpy(cmd
, tmp
, tmp
->len
);
443 } while (time_before(jiffies
, to
));
453 static int kvaser_usb_leaf_send_simple_cmd(const struct kvaser_usb
*dev
,
454 u8 cmd_id
, int channel
)
456 struct kvaser_cmd
*cmd
;
459 cmd
= kmalloc(sizeof(*cmd
), GFP_KERNEL
);
464 cmd
->len
= CMD_HEADER_LEN
+ sizeof(struct kvaser_cmd_simple
);
465 cmd
->u
.simple
.channel
= channel
;
466 cmd
->u
.simple
.tid
= 0xff;
468 rc
= kvaser_usb_send_cmd(dev
, cmd
, cmd
->len
);
474 static int kvaser_usb_leaf_get_software_info_inner(struct kvaser_usb
*dev
)
476 struct kvaser_cmd cmd
;
479 err
= kvaser_usb_leaf_send_simple_cmd(dev
, CMD_GET_SOFTWARE_INFO
, 0);
483 err
= kvaser_usb_leaf_wait_cmd(dev
, CMD_GET_SOFTWARE_INFO_REPLY
, &cmd
);
487 switch (dev
->card_data
.leaf
.family
) {
489 dev
->fw_version
= le32_to_cpu(cmd
.u
.leaf
.softinfo
.fw_version
);
491 le16_to_cpu(cmd
.u
.leaf
.softinfo
.max_outstanding_tx
);
494 dev
->fw_version
= le32_to_cpu(cmd
.u
.usbcan
.softinfo
.fw_version
);
496 le16_to_cpu(cmd
.u
.usbcan
.softinfo
.max_outstanding_tx
);
503 static int kvaser_usb_leaf_get_software_info(struct kvaser_usb
*dev
)
508 /* On some x86 laptops, plugging a Kvaser device again after
509 * an unplug makes the firmware always ignore the very first
510 * command. For such a case, provide some room for retries
511 * instead of completely exiting the driver.
514 err
= kvaser_usb_leaf_get_software_info_inner(dev
);
515 } while (--retry
&& err
== -ETIMEDOUT
);
520 static int kvaser_usb_leaf_get_card_info(struct kvaser_usb
*dev
)
522 struct kvaser_cmd cmd
;
525 err
= kvaser_usb_leaf_send_simple_cmd(dev
, CMD_GET_CARD_INFO
, 0);
529 err
= kvaser_usb_leaf_wait_cmd(dev
, CMD_GET_CARD_INFO_REPLY
, &cmd
);
533 dev
->nchannels
= cmd
.u
.cardinfo
.nchannels
;
534 if (dev
->nchannels
> KVASER_USB_MAX_NET_DEVICES
||
535 (dev
->card_data
.leaf
.family
== KVASER_USBCAN
&&
536 dev
->nchannels
> MAX_USBCAN_NET_DEVICES
))
542 static void kvaser_usb_leaf_tx_acknowledge(const struct kvaser_usb
*dev
,
543 const struct kvaser_cmd
*cmd
)
545 struct net_device_stats
*stats
;
546 struct kvaser_usb_tx_urb_context
*context
;
547 struct kvaser_usb_net_priv
*priv
;
551 channel
= cmd
->u
.tx_acknowledge_header
.channel
;
552 tid
= cmd
->u
.tx_acknowledge_header
.tid
;
554 if (channel
>= dev
->nchannels
) {
555 dev_err(&dev
->intf
->dev
,
556 "Invalid channel number (%d)\n", channel
);
560 priv
= dev
->nets
[channel
];
562 if (!netif_device_present(priv
->netdev
))
565 stats
= &priv
->netdev
->stats
;
567 context
= &priv
->tx_contexts
[tid
% dev
->max_tx_urbs
];
569 /* Sometimes the state change doesn't come after a bus-off event */
570 if (priv
->can
.restart_ms
&& priv
->can
.state
>= CAN_STATE_BUS_OFF
) {
572 struct can_frame
*cf
;
574 skb
= alloc_can_err_skb(priv
->netdev
, &cf
);
576 cf
->can_id
|= CAN_ERR_RESTARTED
;
579 stats
->rx_bytes
+= cf
->can_dlc
;
582 netdev_err(priv
->netdev
,
583 "No memory left for err_skb\n");
586 priv
->can
.can_stats
.restarts
++;
587 netif_carrier_on(priv
->netdev
);
589 priv
->can
.state
= CAN_STATE_ERROR_ACTIVE
;
593 stats
->tx_bytes
+= context
->dlc
;
595 spin_lock_irqsave(&priv
->tx_contexts_lock
, flags
);
597 can_get_echo_skb(priv
->netdev
, context
->echo_index
);
598 context
->echo_index
= dev
->max_tx_urbs
;
599 --priv
->active_tx_contexts
;
600 netif_wake_queue(priv
->netdev
);
602 spin_unlock_irqrestore(&priv
->tx_contexts_lock
, flags
);
605 static int kvaser_usb_leaf_simple_cmd_async(struct kvaser_usb_net_priv
*priv
,
608 struct kvaser_cmd
*cmd
;
611 cmd
= kzalloc(sizeof(*cmd
), GFP_ATOMIC
);
615 cmd
->len
= CMD_HEADER_LEN
+ sizeof(struct kvaser_cmd_simple
);
617 cmd
->u
.simple
.channel
= priv
->channel
;
619 err
= kvaser_usb_send_cmd_async(priv
, cmd
, cmd
->len
);
627 kvaser_usb_leaf_rx_error_update_can_state(struct kvaser_usb_net_priv
*priv
,
628 const struct kvaser_usb_err_summary
*es
,
629 struct can_frame
*cf
)
631 struct kvaser_usb
*dev
= priv
->dev
;
632 struct net_device_stats
*stats
= &priv
->netdev
->stats
;
633 enum can_state cur_state
, new_state
, tx_state
, rx_state
;
635 netdev_dbg(priv
->netdev
, "Error status: 0x%02x\n", es
->status
);
637 new_state
= priv
->can
.state
;
638 cur_state
= priv
->can
.state
;
640 if (es
->status
& (M16C_STATE_BUS_OFF
| M16C_STATE_BUS_RESET
)) {
641 new_state
= CAN_STATE_BUS_OFF
;
642 } else if (es
->status
& M16C_STATE_BUS_PASSIVE
) {
643 new_state
= CAN_STATE_ERROR_PASSIVE
;
644 } else if (es
->status
& M16C_STATE_BUS_ERROR
) {
645 /* Guard against spurious error events after a busoff */
646 if (cur_state
< CAN_STATE_BUS_OFF
) {
647 if (es
->txerr
>= 128 || es
->rxerr
>= 128)
648 new_state
= CAN_STATE_ERROR_PASSIVE
;
649 else if (es
->txerr
>= 96 || es
->rxerr
>= 96)
650 new_state
= CAN_STATE_ERROR_WARNING
;
651 else if (cur_state
> CAN_STATE_ERROR_ACTIVE
)
652 new_state
= CAN_STATE_ERROR_ACTIVE
;
657 new_state
= CAN_STATE_ERROR_ACTIVE
;
659 if (new_state
!= cur_state
) {
660 tx_state
= (es
->txerr
>= es
->rxerr
) ? new_state
: 0;
661 rx_state
= (es
->txerr
<= es
->rxerr
) ? new_state
: 0;
663 can_change_state(priv
->netdev
, cf
, tx_state
, rx_state
);
666 if (priv
->can
.restart_ms
&&
667 cur_state
>= CAN_STATE_BUS_OFF
&&
668 new_state
< CAN_STATE_BUS_OFF
)
669 priv
->can
.can_stats
.restarts
++;
671 switch (dev
->card_data
.leaf
.family
) {
673 if (es
->leaf
.error_factor
) {
674 priv
->can
.can_stats
.bus_error
++;
679 if (es
->usbcan
.error_state
& USBCAN_ERROR_STATE_TX_ERROR
)
681 if (es
->usbcan
.error_state
& USBCAN_ERROR_STATE_RX_ERROR
)
683 if (es
->usbcan
.error_state
& USBCAN_ERROR_STATE_BUSERROR
)
684 priv
->can
.can_stats
.bus_error
++;
688 priv
->bec
.txerr
= es
->txerr
;
689 priv
->bec
.rxerr
= es
->rxerr
;
692 static void kvaser_usb_leaf_rx_error(const struct kvaser_usb
*dev
,
693 const struct kvaser_usb_err_summary
*es
)
695 struct can_frame
*cf
;
696 struct can_frame tmp_cf
= { .can_id
= CAN_ERR_FLAG
,
697 .can_dlc
= CAN_ERR_DLC
};
699 struct net_device_stats
*stats
;
700 struct kvaser_usb_net_priv
*priv
;
701 enum can_state old_state
, new_state
;
703 if (es
->channel
>= dev
->nchannels
) {
704 dev_err(&dev
->intf
->dev
,
705 "Invalid channel number (%d)\n", es
->channel
);
709 priv
= dev
->nets
[es
->channel
];
710 stats
= &priv
->netdev
->stats
;
712 /* Update all of the CAN interface's state and error counters before
713 * trying any memory allocation that can actually fail with -ENOMEM.
715 * We send a temporary stack-allocated error CAN frame to
716 * can_change_state() for the very same reason.
718 * TODO: Split can_change_state() responsibility between updating the
719 * CAN interface's state and counters, and the setting up of CAN error
720 * frame ID and data to userspace. Remove stack allocation afterwards.
722 old_state
= priv
->can
.state
;
723 kvaser_usb_leaf_rx_error_update_can_state(priv
, es
, &tmp_cf
);
724 new_state
= priv
->can
.state
;
726 skb
= alloc_can_err_skb(priv
->netdev
, &cf
);
731 memcpy(cf
, &tmp_cf
, sizeof(*cf
));
733 if (new_state
!= old_state
) {
735 (M16C_STATE_BUS_OFF
| M16C_STATE_BUS_RESET
)) {
736 if (!priv
->can
.restart_ms
)
737 kvaser_usb_leaf_simple_cmd_async(priv
,
739 netif_carrier_off(priv
->netdev
);
742 if (priv
->can
.restart_ms
&&
743 old_state
>= CAN_STATE_BUS_OFF
&&
744 new_state
< CAN_STATE_BUS_OFF
) {
745 cf
->can_id
|= CAN_ERR_RESTARTED
;
746 netif_carrier_on(priv
->netdev
);
750 switch (dev
->card_data
.leaf
.family
) {
752 if (es
->leaf
.error_factor
) {
753 cf
->can_id
|= CAN_ERR_BUSERROR
| CAN_ERR_PROT
;
755 if (es
->leaf
.error_factor
& M16C_EF_ACKE
)
756 cf
->data
[3] = CAN_ERR_PROT_LOC_ACK
;
757 if (es
->leaf
.error_factor
& M16C_EF_CRCE
)
758 cf
->data
[3] = CAN_ERR_PROT_LOC_CRC_SEQ
;
759 if (es
->leaf
.error_factor
& M16C_EF_FORME
)
760 cf
->data
[2] |= CAN_ERR_PROT_FORM
;
761 if (es
->leaf
.error_factor
& M16C_EF_STFE
)
762 cf
->data
[2] |= CAN_ERR_PROT_STUFF
;
763 if (es
->leaf
.error_factor
& M16C_EF_BITE0
)
764 cf
->data
[2] |= CAN_ERR_PROT_BIT0
;
765 if (es
->leaf
.error_factor
& M16C_EF_BITE1
)
766 cf
->data
[2] |= CAN_ERR_PROT_BIT1
;
767 if (es
->leaf
.error_factor
& M16C_EF_TRE
)
768 cf
->data
[2] |= CAN_ERR_PROT_TX
;
772 if (es
->usbcan
.error_state
& USBCAN_ERROR_STATE_BUSERROR
)
773 cf
->can_id
|= CAN_ERR_BUSERROR
;
777 cf
->data
[6] = es
->txerr
;
778 cf
->data
[7] = es
->rxerr
;
781 stats
->rx_bytes
+= cf
->can_dlc
;
785 /* For USBCAN, report error to userspace if the channels's errors counter
786 * has changed, or we're the only channel seeing a bus error state.
789 kvaser_usb_leaf_usbcan_conditionally_rx_error(const struct kvaser_usb
*dev
,
790 struct kvaser_usb_err_summary
*es
)
792 struct kvaser_usb_net_priv
*priv
;
793 unsigned int channel
;
796 channel
= es
->channel
;
797 if (channel
>= dev
->nchannels
) {
798 dev_err(&dev
->intf
->dev
,
799 "Invalid channel number (%d)\n", channel
);
803 priv
= dev
->nets
[channel
];
804 report_error
= false;
806 if (es
->txerr
!= priv
->bec
.txerr
) {
807 es
->usbcan
.error_state
|= USBCAN_ERROR_STATE_TX_ERROR
;
810 if (es
->rxerr
!= priv
->bec
.rxerr
) {
811 es
->usbcan
.error_state
|= USBCAN_ERROR_STATE_RX_ERROR
;
814 if ((es
->status
& M16C_STATE_BUS_ERROR
) &&
815 !(es
->usbcan
.other_ch_status
& M16C_STATE_BUS_ERROR
)) {
816 es
->usbcan
.error_state
|= USBCAN_ERROR_STATE_BUSERROR
;
821 kvaser_usb_leaf_rx_error(dev
, es
);
824 static void kvaser_usb_leaf_usbcan_rx_error(const struct kvaser_usb
*dev
,
825 const struct kvaser_cmd
*cmd
)
827 struct kvaser_usb_err_summary es
= { };
830 /* Sometimes errors are sent as unsolicited chip state events */
831 case CMD_CHIP_STATE_EVENT
:
832 es
.channel
= cmd
->u
.usbcan
.chip_state_event
.channel
;
833 es
.status
= cmd
->u
.usbcan
.chip_state_event
.status
;
834 es
.txerr
= cmd
->u
.usbcan
.chip_state_event
.tx_errors_count
;
835 es
.rxerr
= cmd
->u
.usbcan
.chip_state_event
.rx_errors_count
;
836 kvaser_usb_leaf_usbcan_conditionally_rx_error(dev
, &es
);
839 case CMD_CAN_ERROR_EVENT
:
841 es
.status
= cmd
->u
.usbcan
.error_event
.status_ch0
;
842 es
.txerr
= cmd
->u
.usbcan
.error_event
.tx_errors_count_ch0
;
843 es
.rxerr
= cmd
->u
.usbcan
.error_event
.rx_errors_count_ch0
;
844 es
.usbcan
.other_ch_status
=
845 cmd
->u
.usbcan
.error_event
.status_ch1
;
846 kvaser_usb_leaf_usbcan_conditionally_rx_error(dev
, &es
);
848 /* The USBCAN firmware supports up to 2 channels.
849 * Now that ch0 was checked, check if ch1 has any errors.
851 if (dev
->nchannels
== MAX_USBCAN_NET_DEVICES
) {
853 es
.status
= cmd
->u
.usbcan
.error_event
.status_ch1
;
855 cmd
->u
.usbcan
.error_event
.tx_errors_count_ch1
;
857 cmd
->u
.usbcan
.error_event
.rx_errors_count_ch1
;
858 es
.usbcan
.other_ch_status
=
859 cmd
->u
.usbcan
.error_event
.status_ch0
;
860 kvaser_usb_leaf_usbcan_conditionally_rx_error(dev
, &es
);
865 dev_err(&dev
->intf
->dev
, "Invalid cmd id (%d)\n", cmd
->id
);
869 static void kvaser_usb_leaf_leaf_rx_error(const struct kvaser_usb
*dev
,
870 const struct kvaser_cmd
*cmd
)
872 struct kvaser_usb_err_summary es
= { };
875 case CMD_CAN_ERROR_EVENT
:
876 es
.channel
= cmd
->u
.leaf
.error_event
.channel
;
877 es
.status
= cmd
->u
.leaf
.error_event
.status
;
878 es
.txerr
= cmd
->u
.leaf
.error_event
.tx_errors_count
;
879 es
.rxerr
= cmd
->u
.leaf
.error_event
.rx_errors_count
;
880 es
.leaf
.error_factor
= cmd
->u
.leaf
.error_event
.error_factor
;
882 case CMD_LEAF_LOG_MESSAGE
:
883 es
.channel
= cmd
->u
.leaf
.log_message
.channel
;
884 es
.status
= cmd
->u
.leaf
.log_message
.data
[0];
885 es
.txerr
= cmd
->u
.leaf
.log_message
.data
[2];
886 es
.rxerr
= cmd
->u
.leaf
.log_message
.data
[3];
887 es
.leaf
.error_factor
= cmd
->u
.leaf
.log_message
.data
[1];
889 case CMD_CHIP_STATE_EVENT
:
890 es
.channel
= cmd
->u
.leaf
.chip_state_event
.channel
;
891 es
.status
= cmd
->u
.leaf
.chip_state_event
.status
;
892 es
.txerr
= cmd
->u
.leaf
.chip_state_event
.tx_errors_count
;
893 es
.rxerr
= cmd
->u
.leaf
.chip_state_event
.rx_errors_count
;
894 es
.leaf
.error_factor
= 0;
897 dev_err(&dev
->intf
->dev
, "Invalid cmd id (%d)\n", cmd
->id
);
901 kvaser_usb_leaf_rx_error(dev
, &es
);
904 static void kvaser_usb_leaf_rx_can_err(const struct kvaser_usb_net_priv
*priv
,
905 const struct kvaser_cmd
*cmd
)
907 if (cmd
->u
.rx_can_header
.flag
& (MSG_FLAG_ERROR_FRAME
|
909 struct net_device_stats
*stats
= &priv
->netdev
->stats
;
911 netdev_err(priv
->netdev
, "Unknown error (flags: 0x%02x)\n",
912 cmd
->u
.rx_can_header
.flag
);
918 if (cmd
->u
.rx_can_header
.flag
& MSG_FLAG_OVERRUN
)
919 kvaser_usb_can_rx_over_error(priv
->netdev
);
922 static void kvaser_usb_leaf_rx_can_msg(const struct kvaser_usb
*dev
,
923 const struct kvaser_cmd
*cmd
)
925 struct kvaser_usb_net_priv
*priv
;
926 struct can_frame
*cf
;
928 struct net_device_stats
*stats
;
929 u8 channel
= cmd
->u
.rx_can_header
.channel
;
930 const u8
*rx_data
= NULL
; /* GCC */
932 if (channel
>= dev
->nchannels
) {
933 dev_err(&dev
->intf
->dev
,
934 "Invalid channel number (%d)\n", channel
);
938 priv
= dev
->nets
[channel
];
939 stats
= &priv
->netdev
->stats
;
941 if ((cmd
->u
.rx_can_header
.flag
& MSG_FLAG_ERROR_FRAME
) &&
942 (dev
->card_data
.leaf
.family
== KVASER_LEAF
&&
943 cmd
->id
== CMD_LEAF_LOG_MESSAGE
)) {
944 kvaser_usb_leaf_leaf_rx_error(dev
, cmd
);
946 } else if (cmd
->u
.rx_can_header
.flag
& (MSG_FLAG_ERROR_FRAME
|
949 kvaser_usb_leaf_rx_can_err(priv
, cmd
);
951 } else if (cmd
->u
.rx_can_header
.flag
& ~MSG_FLAG_REMOTE_FRAME
) {
952 netdev_warn(priv
->netdev
,
953 "Unhandled frame (flags: 0x%02x)\n",
954 cmd
->u
.rx_can_header
.flag
);
958 switch (dev
->card_data
.leaf
.family
) {
960 rx_data
= cmd
->u
.leaf
.rx_can
.data
;
963 rx_data
= cmd
->u
.usbcan
.rx_can
.data
;
967 skb
= alloc_can_skb(priv
->netdev
, &cf
);
973 if (dev
->card_data
.leaf
.family
== KVASER_LEAF
&& cmd
->id
==
974 CMD_LEAF_LOG_MESSAGE
) {
975 cf
->can_id
= le32_to_cpu(cmd
->u
.leaf
.log_message
.id
);
976 if (cf
->can_id
& KVASER_EXTENDED_FRAME
)
977 cf
->can_id
&= CAN_EFF_MASK
| CAN_EFF_FLAG
;
979 cf
->can_id
&= CAN_SFF_MASK
;
981 cf
->can_dlc
= get_can_dlc(cmd
->u
.leaf
.log_message
.dlc
);
983 if (cmd
->u
.leaf
.log_message
.flags
& MSG_FLAG_REMOTE_FRAME
)
984 cf
->can_id
|= CAN_RTR_FLAG
;
986 memcpy(cf
->data
, &cmd
->u
.leaf
.log_message
.data
,
989 cf
->can_id
= ((rx_data
[0] & 0x1f) << 6) | (rx_data
[1] & 0x3f);
991 if (cmd
->id
== CMD_RX_EXT_MESSAGE
) {
993 cf
->can_id
|= ((rx_data
[2] & 0x0f) << 14) |
994 ((rx_data
[3] & 0xff) << 6) |
996 cf
->can_id
|= CAN_EFF_FLAG
;
999 cf
->can_dlc
= get_can_dlc(rx_data
[5]);
1001 if (cmd
->u
.rx_can_header
.flag
& MSG_FLAG_REMOTE_FRAME
)
1002 cf
->can_id
|= CAN_RTR_FLAG
;
1004 memcpy(cf
->data
, &rx_data
[6], cf
->can_dlc
);
1007 stats
->rx_packets
++;
1008 stats
->rx_bytes
+= cf
->can_dlc
;
1012 static void kvaser_usb_leaf_start_chip_reply(const struct kvaser_usb
*dev
,
1013 const struct kvaser_cmd
*cmd
)
1015 struct kvaser_usb_net_priv
*priv
;
1016 u8 channel
= cmd
->u
.simple
.channel
;
1018 if (channel
>= dev
->nchannels
) {
1019 dev_err(&dev
->intf
->dev
,
1020 "Invalid channel number (%d)\n", channel
);
1024 priv
= dev
->nets
[channel
];
1026 if (completion_done(&priv
->start_comp
) &&
1027 netif_queue_stopped(priv
->netdev
)) {
1028 netif_wake_queue(priv
->netdev
);
1030 netif_start_queue(priv
->netdev
);
1031 complete(&priv
->start_comp
);
1035 static void kvaser_usb_leaf_stop_chip_reply(const struct kvaser_usb
*dev
,
1036 const struct kvaser_cmd
*cmd
)
1038 struct kvaser_usb_net_priv
*priv
;
1039 u8 channel
= cmd
->u
.simple
.channel
;
1041 if (channel
>= dev
->nchannels
) {
1042 dev_err(&dev
->intf
->dev
,
1043 "Invalid channel number (%d)\n", channel
);
1047 priv
= dev
->nets
[channel
];
1049 complete(&priv
->stop_comp
);
1052 static void kvaser_usb_leaf_handle_command(const struct kvaser_usb
*dev
,
1053 const struct kvaser_cmd
*cmd
)
1056 case CMD_START_CHIP_REPLY
:
1057 kvaser_usb_leaf_start_chip_reply(dev
, cmd
);
1060 case CMD_STOP_CHIP_REPLY
:
1061 kvaser_usb_leaf_stop_chip_reply(dev
, cmd
);
1064 case CMD_RX_STD_MESSAGE
:
1065 case CMD_RX_EXT_MESSAGE
:
1066 kvaser_usb_leaf_rx_can_msg(dev
, cmd
);
1069 case CMD_LEAF_LOG_MESSAGE
:
1070 if (dev
->card_data
.leaf
.family
!= KVASER_LEAF
)
1072 kvaser_usb_leaf_rx_can_msg(dev
, cmd
);
1075 case CMD_CHIP_STATE_EVENT
:
1076 case CMD_CAN_ERROR_EVENT
:
1077 if (dev
->card_data
.leaf
.family
== KVASER_LEAF
)
1078 kvaser_usb_leaf_leaf_rx_error(dev
, cmd
);
1080 kvaser_usb_leaf_usbcan_rx_error(dev
, cmd
);
1083 case CMD_TX_ACKNOWLEDGE
:
1084 kvaser_usb_leaf_tx_acknowledge(dev
, cmd
);
1087 /* Ignored commands */
1088 case CMD_USBCAN_CLOCK_OVERFLOW_EVENT
:
1089 if (dev
->card_data
.leaf
.family
!= KVASER_USBCAN
)
1093 case CMD_FLUSH_QUEUE_REPLY
:
1094 if (dev
->card_data
.leaf
.family
!= KVASER_LEAF
)
1099 warn
: dev_warn(&dev
->intf
->dev
, "Unhandled command (%d)\n", cmd
->id
);
1104 static void kvaser_usb_leaf_read_bulk_callback(struct kvaser_usb
*dev
,
1107 struct kvaser_cmd
*cmd
;
1110 while (pos
<= len
- CMD_HEADER_LEN
) {
1113 /* The Kvaser firmware can only read and write commands that
1114 * does not cross the USB's endpoint wMaxPacketSize boundary.
1115 * If a follow-up command crosses such boundary, firmware puts
1116 * a placeholder zero-length command in its place then aligns
1117 * the real command to the next max packet size.
1119 * Handle such cases or we're going to miss a significant
1120 * number of events in case of a heavy rx load on the bus.
1122 if (cmd
->len
== 0) {
1123 pos
= round_up(pos
, le16_to_cpu
1124 (dev
->bulk_in
->wMaxPacketSize
));
1128 if (pos
+ cmd
->len
> len
) {
1129 dev_err_ratelimited(&dev
->intf
->dev
, "Format error\n");
1133 kvaser_usb_leaf_handle_command(dev
, cmd
);
1138 static int kvaser_usb_leaf_set_opt_mode(const struct kvaser_usb_net_priv
*priv
)
1140 struct kvaser_cmd
*cmd
;
1143 cmd
= kzalloc(sizeof(*cmd
), GFP_KERNEL
);
1147 cmd
->id
= CMD_SET_CTRL_MODE
;
1148 cmd
->len
= CMD_HEADER_LEN
+ sizeof(struct kvaser_cmd_ctrl_mode
);
1149 cmd
->u
.ctrl_mode
.tid
= 0xff;
1150 cmd
->u
.ctrl_mode
.channel
= priv
->channel
;
1152 if (priv
->can
.ctrlmode
& CAN_CTRLMODE_LISTENONLY
)
1153 cmd
->u
.ctrl_mode
.ctrl_mode
= KVASER_CTRL_MODE_SILENT
;
1155 cmd
->u
.ctrl_mode
.ctrl_mode
= KVASER_CTRL_MODE_NORMAL
;
1157 rc
= kvaser_usb_send_cmd(priv
->dev
, cmd
, cmd
->len
);
1163 static int kvaser_usb_leaf_start_chip(struct kvaser_usb_net_priv
*priv
)
1167 init_completion(&priv
->start_comp
);
1169 err
= kvaser_usb_leaf_send_simple_cmd(priv
->dev
, CMD_START_CHIP
,
1174 if (!wait_for_completion_timeout(&priv
->start_comp
,
1175 msecs_to_jiffies(KVASER_USB_TIMEOUT
)))
1181 static int kvaser_usb_leaf_stop_chip(struct kvaser_usb_net_priv
*priv
)
1185 init_completion(&priv
->stop_comp
);
1187 err
= kvaser_usb_leaf_send_simple_cmd(priv
->dev
, CMD_STOP_CHIP
,
1192 if (!wait_for_completion_timeout(&priv
->stop_comp
,
1193 msecs_to_jiffies(KVASER_USB_TIMEOUT
)))
1199 static int kvaser_usb_leaf_reset_chip(struct kvaser_usb
*dev
, int channel
)
1201 return kvaser_usb_leaf_send_simple_cmd(dev
, CMD_RESET_CHIP
, channel
);
1204 static int kvaser_usb_leaf_flush_queue(struct kvaser_usb_net_priv
*priv
)
1206 struct kvaser_cmd
*cmd
;
1209 cmd
= kzalloc(sizeof(*cmd
), GFP_KERNEL
);
1213 cmd
->id
= CMD_FLUSH_QUEUE
;
1214 cmd
->len
= CMD_HEADER_LEN
+ sizeof(struct kvaser_cmd_flush_queue
);
1215 cmd
->u
.flush_queue
.channel
= priv
->channel
;
1216 cmd
->u
.flush_queue
.flags
= 0x00;
1218 rc
= kvaser_usb_send_cmd(priv
->dev
, cmd
, cmd
->len
);
1224 static int kvaser_usb_leaf_init_card(struct kvaser_usb
*dev
)
1226 struct kvaser_usb_dev_card_data
*card_data
= &dev
->card_data
;
1228 dev
->cfg
= &kvaser_usb_leaf_dev_cfg
;
1229 card_data
->ctrlmode_supported
|= CAN_CTRLMODE_3_SAMPLES
;
1234 static const struct can_bittiming_const kvaser_usb_leaf_bittiming_const
= {
1235 .name
= "kvaser_usb",
1236 .tseg1_min
= KVASER_USB_TSEG1_MIN
,
1237 .tseg1_max
= KVASER_USB_TSEG1_MAX
,
1238 .tseg2_min
= KVASER_USB_TSEG2_MIN
,
1239 .tseg2_max
= KVASER_USB_TSEG2_MAX
,
1240 .sjw_max
= KVASER_USB_SJW_MAX
,
1241 .brp_min
= KVASER_USB_BRP_MIN
,
1242 .brp_max
= KVASER_USB_BRP_MAX
,
1243 .brp_inc
= KVASER_USB_BRP_INC
,
1246 static int kvaser_usb_leaf_set_bittiming(struct net_device
*netdev
)
1248 struct kvaser_usb_net_priv
*priv
= netdev_priv(netdev
);
1249 struct can_bittiming
*bt
= &priv
->can
.bittiming
;
1250 struct kvaser_usb
*dev
= priv
->dev
;
1251 struct kvaser_cmd
*cmd
;
1254 cmd
= kmalloc(sizeof(*cmd
), GFP_KERNEL
);
1258 cmd
->id
= CMD_SET_BUS_PARAMS
;
1259 cmd
->len
= CMD_HEADER_LEN
+ sizeof(struct kvaser_cmd_busparams
);
1260 cmd
->u
.busparams
.channel
= priv
->channel
;
1261 cmd
->u
.busparams
.tid
= 0xff;
1262 cmd
->u
.busparams
.bitrate
= cpu_to_le32(bt
->bitrate
);
1263 cmd
->u
.busparams
.sjw
= bt
->sjw
;
1264 cmd
->u
.busparams
.tseg1
= bt
->prop_seg
+ bt
->phase_seg1
;
1265 cmd
->u
.busparams
.tseg2
= bt
->phase_seg2
;
1267 if (priv
->can
.ctrlmode
& CAN_CTRLMODE_3_SAMPLES
)
1268 cmd
->u
.busparams
.no_samp
= 3;
1270 cmd
->u
.busparams
.no_samp
= 1;
1272 rc
= kvaser_usb_send_cmd(dev
, cmd
, cmd
->len
);
1278 static int kvaser_usb_leaf_set_mode(struct net_device
*netdev
,
1281 struct kvaser_usb_net_priv
*priv
= netdev_priv(netdev
);
1285 case CAN_MODE_START
:
1286 err
= kvaser_usb_leaf_simple_cmd_async(priv
, CMD_START_CHIP
);
1297 static int kvaser_usb_leaf_get_berr_counter(const struct net_device
*netdev
,
1298 struct can_berr_counter
*bec
)
1300 struct kvaser_usb_net_priv
*priv
= netdev_priv(netdev
);
1307 static int kvaser_usb_leaf_setup_endpoints(struct kvaser_usb
*dev
)
1309 const struct usb_host_interface
*iface_desc
;
1310 struct usb_endpoint_descriptor
*endpoint
;
1313 iface_desc
= dev
->intf
->cur_altsetting
;
1315 for (i
= 0; i
< iface_desc
->desc
.bNumEndpoints
; ++i
) {
1316 endpoint
= &iface_desc
->endpoint
[i
].desc
;
1318 if (!dev
->bulk_in
&& usb_endpoint_is_bulk_in(endpoint
))
1319 dev
->bulk_in
= endpoint
;
1321 if (!dev
->bulk_out
&& usb_endpoint_is_bulk_out(endpoint
))
1322 dev
->bulk_out
= endpoint
;
1324 /* use first bulk endpoint for in and out */
1325 if (dev
->bulk_in
&& dev
->bulk_out
)
1332 const struct kvaser_usb_dev_ops kvaser_usb_leaf_dev_ops
= {
1333 .dev_set_mode
= kvaser_usb_leaf_set_mode
,
1334 .dev_set_bittiming
= kvaser_usb_leaf_set_bittiming
,
1335 .dev_set_data_bittiming
= NULL
,
1336 .dev_get_berr_counter
= kvaser_usb_leaf_get_berr_counter
,
1337 .dev_setup_endpoints
= kvaser_usb_leaf_setup_endpoints
,
1338 .dev_init_card
= kvaser_usb_leaf_init_card
,
1339 .dev_get_software_info
= kvaser_usb_leaf_get_software_info
,
1340 .dev_get_software_details
= NULL
,
1341 .dev_get_card_info
= kvaser_usb_leaf_get_card_info
,
1342 .dev_get_capabilities
= NULL
,
1343 .dev_set_opt_mode
= kvaser_usb_leaf_set_opt_mode
,
1344 .dev_start_chip
= kvaser_usb_leaf_start_chip
,
1345 .dev_stop_chip
= kvaser_usb_leaf_stop_chip
,
1346 .dev_reset_chip
= kvaser_usb_leaf_reset_chip
,
1347 .dev_flush_queue
= kvaser_usb_leaf_flush_queue
,
1348 .dev_read_bulk_callback
= kvaser_usb_leaf_read_bulk_callback
,
1349 .dev_frame_to_cmd
= kvaser_usb_leaf_frame_to_cmd
,
1352 static const struct kvaser_usb_dev_cfg kvaser_usb_leaf_dev_cfg
= {
1354 .freq
= CAN_USB_CLOCK
,
1356 .timestamp_freq
= 1,
1357 .bittiming_const
= &kvaser_usb_leaf_bittiming_const
,