1 // SPDX-License-Identifier: GPL-2.0
2 /* Parts of this driver are based on the following:
3 * - Kvaser linux mhydra driver (version 5.24)
4 * - CAN driver for esd CAN-USB/2
6 * Copyright (C) 2018 KVASER AB, Sweden. All rights reserved.
7 * Copyright (C) 2010 Matthias Fuchs <matthias.fuchs@esd.eu>, esd gmbh
10 * - Transition from CAN_STATE_ERROR_WARNING to CAN_STATE_ERROR_ACTIVE is only
11 * reported after a call to do_get_berr_counter(), since firmware does not
12 * distinguish between ERROR_WARNING and ERROR_ACTIVE.
13 * - Hardware timestamps are not set for CAN Tx frames.
16 #include <linux/completion.h>
17 #include <linux/device.h>
18 #include <linux/gfp.h>
19 #include <linux/jiffies.h>
20 #include <linux/kernel.h>
21 #include <linux/netdevice.h>
22 #include <linux/spinlock.h>
23 #include <linux/string.h>
24 #include <linux/types.h>
25 #include <linux/usb.h>
27 #include <linux/can.h>
28 #include <linux/can/dev.h>
29 #include <linux/can/error.h>
30 #include <linux/can/netlink.h>
32 #include "kvaser_usb.h"
34 /* Forward declarations */
35 static const struct kvaser_usb_dev_cfg kvaser_usb_hydra_dev_cfg_kcan
;
36 static const struct kvaser_usb_dev_cfg kvaser_usb_hydra_dev_cfg_flexc
;
38 #define KVASER_USB_HYDRA_BULK_EP_IN_ADDR 0x82
39 #define KVASER_USB_HYDRA_BULK_EP_OUT_ADDR 0x02
41 #define KVASER_USB_HYDRA_MAX_TRANSID 0xff
42 #define KVASER_USB_HYDRA_MIN_TRANSID 0x01
44 /* Minihydra command IDs */
45 #define CMD_SET_BUSPARAMS_REQ 16
46 #define CMD_GET_CHIP_STATE_REQ 19
47 #define CMD_CHIP_STATE_EVENT 20
48 #define CMD_SET_DRIVERMODE_REQ 21
49 #define CMD_START_CHIP_REQ 26
50 #define CMD_START_CHIP_RESP 27
51 #define CMD_STOP_CHIP_REQ 28
52 #define CMD_STOP_CHIP_RESP 29
53 #define CMD_TX_CAN_MESSAGE 33
54 #define CMD_GET_CARD_INFO_REQ 34
55 #define CMD_GET_CARD_INFO_RESP 35
56 #define CMD_GET_SOFTWARE_INFO_REQ 38
57 #define CMD_GET_SOFTWARE_INFO_RESP 39
58 #define CMD_ERROR_EVENT 45
59 #define CMD_FLUSH_QUEUE 48
60 #define CMD_TX_ACKNOWLEDGE 50
61 #define CMD_FLUSH_QUEUE_RESP 66
62 #define CMD_SET_BUSPARAMS_FD_REQ 69
63 #define CMD_SET_BUSPARAMS_FD_RESP 70
64 #define CMD_SET_BUSPARAMS_RESP 85
65 #define CMD_GET_CAPABILITIES_REQ 95
66 #define CMD_GET_CAPABILITIES_RESP 96
67 #define CMD_RX_MESSAGE 106
68 #define CMD_MAP_CHANNEL_REQ 200
69 #define CMD_MAP_CHANNEL_RESP 201
70 #define CMD_GET_SOFTWARE_DETAILS_REQ 202
71 #define CMD_GET_SOFTWARE_DETAILS_RESP 203
72 #define CMD_EXTENDED 255
74 /* Minihydra extended command IDs */
75 #define CMD_TX_CAN_MESSAGE_FD 224
76 #define CMD_TX_ACKNOWLEDGE_FD 225
77 #define CMD_RX_MESSAGE_FD 226
79 /* Hydra commands are handled by different threads in firmware.
80 * The threads are denoted hydra entity (HE). Each HE got a unique 6-bit
81 * address. The address is used in hydra commands to get/set source and
82 * destination HE. There are two predefined HE addresses, the remaining
83 * addresses are different between devices and firmware versions. Hence, we need
84 * to enumerate the addresses (see kvaser_usb_hydra_map_channel()).
87 /* Well-known HE addresses */
88 #define KVASER_USB_HYDRA_HE_ADDRESS_ROUTER 0x00
89 #define KVASER_USB_HYDRA_HE_ADDRESS_ILLEGAL 0x3e
91 #define KVASER_USB_HYDRA_TRANSID_CANHE 0x40
92 #define KVASER_USB_HYDRA_TRANSID_SYSDBG 0x61
94 struct kvaser_cmd_map_ch_req
{
100 struct kvaser_cmd_map_ch_res
{
106 struct kvaser_cmd_card_info
{
107 __le32 serial_number
;
119 struct kvaser_cmd_sw_info
{
121 __le16 max_outstanding_tx
;
125 struct kvaser_cmd_sw_detail_req
{
130 /* Software detail flags */
131 #define KVASER_USB_HYDRA_SW_FLAG_FW_BETA BIT(2)
132 #define KVASER_USB_HYDRA_SW_FLAG_FW_BAD BIT(4)
133 #define KVASER_USB_HYDRA_SW_FLAG_FREQ_80M BIT(5)
134 #define KVASER_USB_HYDRA_SW_FLAG_EXT_CMD BIT(9)
135 #define KVASER_USB_HYDRA_SW_FLAG_CANFD BIT(10)
136 #define KVASER_USB_HYDRA_SW_FLAG_NONISO BIT(11)
137 #define KVASER_USB_HYDRA_SW_FLAG_EXT_CAP BIT(12)
138 struct kvaser_cmd_sw_detail_res
{
147 /* Sub commands for cap_req and cap_res */
148 #define KVASER_USB_HYDRA_CAP_CMD_LISTEN_MODE 0x02
149 #define KVASER_USB_HYDRA_CAP_CMD_ERR_REPORT 0x05
150 #define KVASER_USB_HYDRA_CAP_CMD_ONE_SHOT 0x06
151 struct kvaser_cmd_cap_req
{
156 /* Status codes for cap_res */
157 #define KVASER_USB_HYDRA_CAP_STAT_OK 0x00
158 #define KVASER_USB_HYDRA_CAP_STAT_NOT_IMPL 0x01
159 #define KVASER_USB_HYDRA_CAP_STAT_UNAVAIL 0x02
160 struct kvaser_cmd_cap_res
{
168 /* CMD_ERROR_EVENT error codes */
169 #define KVASER_USB_HYDRA_ERROR_EVENT_CAN 0x01
170 #define KVASER_USB_HYDRA_ERROR_EVENT_PARAM 0x09
171 struct kvaser_cmd_error_event
{
179 /* Chip state status flags. Used for chip_state_event and err_frame_data. */
180 #define KVASER_USB_HYDRA_BUS_ERR_ACT 0x00
181 #define KVASER_USB_HYDRA_BUS_ERR_PASS BIT(5)
182 #define KVASER_USB_HYDRA_BUS_BUS_OFF BIT(6)
183 struct kvaser_cmd_chip_state_event
{
192 #define KVASER_USB_HYDRA_BUS_MODE_CAN 0x00
193 #define KVASER_USB_HYDRA_BUS_MODE_CANFD_ISO 0x01
194 #define KVASER_USB_HYDRA_BUS_MODE_NONISO 0x02
195 struct kvaser_cmd_set_busparams
{
212 #define KVASER_USB_HYDRA_CTRLMODE_NORMAL 0x01
213 #define KVASER_USB_HYDRA_CTRLMODE_LISTEN 0x02
214 struct kvaser_cmd_set_ctrlmode
{
219 struct kvaser_err_frame_data
{
227 struct kvaser_cmd_rx_can
{
238 struct kvaser_err_frame_data err_frame_data
;
242 /* Extended CAN ID flag. Used in rx_can and tx_can */
243 #define KVASER_USB_HYDRA_EXTENDED_FRAME_ID BIT(31)
244 struct kvaser_cmd_tx_can
{
254 struct kvaser_cmd_header
{
256 /* The destination HE address is stored in 0..5 of he_addr.
257 * The upper part of source HE address is stored in 6..7 of he_addr, and
258 * the lower part is stored in 12..15 of transid.
265 struct kvaser_cmd_header header
;
267 struct kvaser_cmd_map_ch_req map_ch_req
;
268 struct kvaser_cmd_map_ch_res map_ch_res
;
270 struct kvaser_cmd_card_info card_info
;
271 struct kvaser_cmd_sw_info sw_info
;
272 struct kvaser_cmd_sw_detail_req sw_detail_req
;
273 struct kvaser_cmd_sw_detail_res sw_detail_res
;
275 struct kvaser_cmd_cap_req cap_req
;
276 struct kvaser_cmd_cap_res cap_res
;
278 struct kvaser_cmd_error_event error_event
;
280 struct kvaser_cmd_set_busparams set_busparams_req
;
282 struct kvaser_cmd_chip_state_event chip_state_event
;
284 struct kvaser_cmd_set_ctrlmode set_ctrlmode
;
286 struct kvaser_cmd_rx_can rx_can
;
287 struct kvaser_cmd_tx_can tx_can
;
291 /* CAN frame flags. Used in rx_can, ext_rx_can, tx_can and ext_tx_can */
292 #define KVASER_USB_HYDRA_CF_FLAG_ERROR_FRAME BIT(0)
293 #define KVASER_USB_HYDRA_CF_FLAG_OVERRUN BIT(1)
294 #define KVASER_USB_HYDRA_CF_FLAG_REMOTE_FRAME BIT(4)
295 #define KVASER_USB_HYDRA_CF_FLAG_EXTENDED_ID BIT(5)
296 /* CAN frame flags. Used in ext_rx_can and ext_tx_can */
297 #define KVASER_USB_HYDRA_CF_FLAG_OSM_NACK BIT(12)
298 #define KVASER_USB_HYDRA_CF_FLAG_ABL BIT(13)
299 #define KVASER_USB_HYDRA_CF_FLAG_FDF BIT(16)
300 #define KVASER_USB_HYDRA_CF_FLAG_BRS BIT(17)
301 #define KVASER_USB_HYDRA_CF_FLAG_ESI BIT(18)
303 /* KCAN packet header macros. Used in ext_rx_can and ext_tx_can */
304 #define KVASER_USB_KCAN_DATA_DLC_BITS 4
305 #define KVASER_USB_KCAN_DATA_DLC_SHIFT 8
306 #define KVASER_USB_KCAN_DATA_DLC_MASK \
307 GENMASK(KVASER_USB_KCAN_DATA_DLC_BITS - 1 + \
308 KVASER_USB_KCAN_DATA_DLC_SHIFT, \
309 KVASER_USB_KCAN_DATA_DLC_SHIFT)
311 #define KVASER_USB_KCAN_DATA_BRS BIT(14)
312 #define KVASER_USB_KCAN_DATA_FDF BIT(15)
313 #define KVASER_USB_KCAN_DATA_OSM BIT(16)
314 #define KVASER_USB_KCAN_DATA_AREQ BIT(31)
315 #define KVASER_USB_KCAN_DATA_SRR BIT(31)
316 #define KVASER_USB_KCAN_DATA_RTR BIT(29)
317 #define KVASER_USB_KCAN_DATA_IDE BIT(30)
318 struct kvaser_cmd_ext_rx_can
{
326 struct kvaser_err_frame_data err_frame_data
;
330 struct kvaser_cmd_ext_tx_can
{
341 struct kvaser_cmd_ext_tx_ack
{
348 /* struct for extended commands (CMD_EXTENDED) */
349 struct kvaser_cmd_ext
{
350 struct kvaser_cmd_header header
;
356 struct kvaser_cmd_ext_rx_can rx_can
;
357 struct kvaser_cmd_ext_tx_can tx_can
;
358 struct kvaser_cmd_ext_tx_ack tx_ack
;
362 static const struct can_bittiming_const kvaser_usb_hydra_kcan_bittiming_c
= {
363 .name
= "kvaser_usb_kcan",
374 static const struct can_bittiming_const kvaser_usb_hydra_flexc_bittiming_c
= {
375 .name
= "kvaser_usb_flex",
386 #define KVASER_USB_HYDRA_TRANSID_BITS 12
387 #define KVASER_USB_HYDRA_TRANSID_MASK \
388 GENMASK(KVASER_USB_HYDRA_TRANSID_BITS - 1, 0)
389 #define KVASER_USB_HYDRA_HE_ADDR_SRC_MASK GENMASK(7, 6)
390 #define KVASER_USB_HYDRA_HE_ADDR_DEST_MASK GENMASK(5, 0)
391 #define KVASER_USB_HYDRA_HE_ADDR_SRC_BITS 2
392 static inline u16
kvaser_usb_hydra_get_cmd_transid(const struct kvaser_cmd
*cmd
)
394 return le16_to_cpu(cmd
->header
.transid
) & KVASER_USB_HYDRA_TRANSID_MASK
;
397 static inline void kvaser_usb_hydra_set_cmd_transid(struct kvaser_cmd
*cmd
,
400 cmd
->header
.transid
=
401 cpu_to_le16(transid
& KVASER_USB_HYDRA_TRANSID_MASK
);
404 static inline u8
kvaser_usb_hydra_get_cmd_src_he(const struct kvaser_cmd
*cmd
)
406 return (cmd
->header
.he_addr
& KVASER_USB_HYDRA_HE_ADDR_SRC_MASK
) >>
407 KVASER_USB_HYDRA_HE_ADDR_SRC_BITS
|
408 le16_to_cpu(cmd
->header
.transid
) >>
409 KVASER_USB_HYDRA_TRANSID_BITS
;
412 static inline void kvaser_usb_hydra_set_cmd_dest_he(struct kvaser_cmd
*cmd
,
415 cmd
->header
.he_addr
=
416 (cmd
->header
.he_addr
& KVASER_USB_HYDRA_HE_ADDR_SRC_MASK
) |
417 (dest_he
& KVASER_USB_HYDRA_HE_ADDR_DEST_MASK
);
420 static u8
kvaser_usb_hydra_channel_from_cmd(const struct kvaser_usb
*dev
,
421 const struct kvaser_cmd
*cmd
)
425 u8 src_he
= kvaser_usb_hydra_get_cmd_src_he(cmd
);
427 for (i
= 0; i
< KVASER_USB_MAX_NET_DEVICES
; i
++) {
428 if (dev
->card_data
.hydra
.channel_to_he
[i
] == src_he
) {
437 static u16
kvaser_usb_hydra_get_next_transid(struct kvaser_usb
*dev
)
441 struct kvaser_usb_dev_card_data_hydra
*card_data
=
442 &dev
->card_data
.hydra
;
444 spin_lock_irqsave(&card_data
->transid_lock
, flags
);
445 transid
= card_data
->transid
;
446 if (transid
>= KVASER_USB_HYDRA_MAX_TRANSID
)
447 transid
= KVASER_USB_HYDRA_MIN_TRANSID
;
450 card_data
->transid
= transid
;
451 spin_unlock_irqrestore(&card_data
->transid_lock
, flags
);
456 static size_t kvaser_usb_hydra_cmd_size(struct kvaser_cmd
*cmd
)
460 if (cmd
->header
.cmd_no
== CMD_EXTENDED
)
461 ret
= le16_to_cpu(((struct kvaser_cmd_ext
*)cmd
)->len
);
463 ret
= sizeof(struct kvaser_cmd
);
468 static struct kvaser_usb_net_priv
*
469 kvaser_usb_hydra_net_priv_from_cmd(const struct kvaser_usb
*dev
,
470 const struct kvaser_cmd
*cmd
)
472 struct kvaser_usb_net_priv
*priv
= NULL
;
473 u8 channel
= kvaser_usb_hydra_channel_from_cmd(dev
, cmd
);
475 if (channel
>= dev
->nchannels
)
476 dev_err(&dev
->intf
->dev
,
477 "Invalid channel number (%d)\n", channel
);
479 priv
= dev
->nets
[channel
];
485 kvaser_usb_hydra_ktime_from_rx_cmd(const struct kvaser_usb_dev_cfg
*cfg
,
486 const struct kvaser_cmd
*cmd
)
490 if (cmd
->header
.cmd_no
== CMD_EXTENDED
) {
491 struct kvaser_cmd_ext
*cmd_ext
= (struct kvaser_cmd_ext
*)cmd
;
493 ticks
= le64_to_cpu(cmd_ext
->rx_can
.timestamp
);
495 ticks
= le16_to_cpu(cmd
->rx_can
.timestamp
[0]);
496 ticks
+= (u64
)(le16_to_cpu(cmd
->rx_can
.timestamp
[1])) << 16;
497 ticks
+= (u64
)(le16_to_cpu(cmd
->rx_can
.timestamp
[2])) << 32;
500 return ns_to_ktime(div_u64(ticks
* 1000, cfg
->timestamp_freq
));
503 static int kvaser_usb_hydra_send_simple_cmd(struct kvaser_usb
*dev
,
504 u8 cmd_no
, int channel
)
506 struct kvaser_cmd
*cmd
;
509 cmd
= kcalloc(1, sizeof(struct kvaser_cmd
), GFP_KERNEL
);
513 cmd
->header
.cmd_no
= cmd_no
;
515 kvaser_usb_hydra_set_cmd_dest_he
516 (cmd
, KVASER_USB_HYDRA_HE_ADDRESS_ILLEGAL
);
518 if (channel
>= KVASER_USB_MAX_NET_DEVICES
) {
519 dev_err(&dev
->intf
->dev
, "channel (%d) out of range.\n",
524 kvaser_usb_hydra_set_cmd_dest_he
525 (cmd
, dev
->card_data
.hydra
.channel_to_he
[channel
]);
527 kvaser_usb_hydra_set_cmd_transid
528 (cmd
, kvaser_usb_hydra_get_next_transid(dev
));
530 err
= kvaser_usb_send_cmd(dev
, cmd
, kvaser_usb_hydra_cmd_size(cmd
));
541 kvaser_usb_hydra_send_simple_cmd_async(struct kvaser_usb_net_priv
*priv
,
544 struct kvaser_cmd
*cmd
;
545 struct kvaser_usb
*dev
= priv
->dev
;
548 cmd
= kcalloc(1, sizeof(struct kvaser_cmd
), GFP_ATOMIC
);
552 cmd
->header
.cmd_no
= cmd_no
;
554 kvaser_usb_hydra_set_cmd_dest_he
555 (cmd
, dev
->card_data
.hydra
.channel_to_he
[priv
->channel
]);
556 kvaser_usb_hydra_set_cmd_transid
557 (cmd
, kvaser_usb_hydra_get_next_transid(dev
));
559 err
= kvaser_usb_send_cmd_async(priv
, cmd
,
560 kvaser_usb_hydra_cmd_size(cmd
));
567 /* This function is used for synchronously waiting on hydra control commands.
568 * Note: Compared to kvaser_usb_hydra_read_bulk_callback(), we never need to
569 * handle partial hydra commands. Since hydra control commands are always
570 * non-extended commands.
572 static int kvaser_usb_hydra_wait_cmd(const struct kvaser_usb
*dev
, u8 cmd_no
,
573 struct kvaser_cmd
*cmd
)
577 unsigned long timeout
= jiffies
+ msecs_to_jiffies(KVASER_USB_TIMEOUT
);
579 if (cmd
->header
.cmd_no
== CMD_EXTENDED
) {
580 dev_err(&dev
->intf
->dev
, "Wait for CMD_EXTENDED not allowed\n");
584 buf
= kzalloc(KVASER_USB_RX_BUFFER_SIZE
, GFP_KERNEL
);
592 err
= kvaser_usb_recv_cmd(dev
, buf
, KVASER_USB_RX_BUFFER_SIZE
,
597 while (pos
< actual_len
) {
598 struct kvaser_cmd
*tmp_cmd
;
602 cmd_len
= kvaser_usb_hydra_cmd_size(tmp_cmd
);
603 if (pos
+ cmd_len
> actual_len
) {
604 dev_err_ratelimited(&dev
->intf
->dev
,
609 if (tmp_cmd
->header
.cmd_no
== cmd_no
) {
610 memcpy(cmd
, tmp_cmd
, cmd_len
);
615 } while (time_before(jiffies
, timeout
));
625 static int kvaser_usb_hydra_map_channel_resp(struct kvaser_usb
*dev
,
626 const struct kvaser_cmd
*cmd
)
629 u16 transid
= kvaser_usb_hydra_get_cmd_transid(cmd
);
630 struct kvaser_usb_dev_card_data_hydra
*card_data
=
631 &dev
->card_data
.hydra
;
633 if (transid
> 0x007f || transid
< 0x0040) {
634 dev_err(&dev
->intf
->dev
,
635 "CMD_MAP_CHANNEL_RESP, invalid transid: 0x%x\n",
641 case KVASER_USB_HYDRA_TRANSID_CANHE
:
642 case KVASER_USB_HYDRA_TRANSID_CANHE
+ 1:
643 case KVASER_USB_HYDRA_TRANSID_CANHE
+ 2:
644 case KVASER_USB_HYDRA_TRANSID_CANHE
+ 3:
645 case KVASER_USB_HYDRA_TRANSID_CANHE
+ 4:
646 channel
= transid
& 0x000f;
647 he
= cmd
->map_ch_res
.he_addr
;
648 card_data
->channel_to_he
[channel
] = he
;
650 case KVASER_USB_HYDRA_TRANSID_SYSDBG
:
651 card_data
->sysdbg_he
= cmd
->map_ch_res
.he_addr
;
654 dev_warn(&dev
->intf
->dev
,
655 "Unknown CMD_MAP_CHANNEL_RESP transid=0x%x\n",
663 static int kvaser_usb_hydra_map_channel(struct kvaser_usb
*dev
, u16 transid
,
664 u8 channel
, const char *name
)
666 struct kvaser_cmd
*cmd
;
669 cmd
= kcalloc(1, sizeof(struct kvaser_cmd
), GFP_KERNEL
);
673 strcpy(cmd
->map_ch_req
.name
, name
);
674 cmd
->header
.cmd_no
= CMD_MAP_CHANNEL_REQ
;
675 kvaser_usb_hydra_set_cmd_dest_he
676 (cmd
, KVASER_USB_HYDRA_HE_ADDRESS_ROUTER
);
677 cmd
->map_ch_req
.channel
= channel
;
679 kvaser_usb_hydra_set_cmd_transid(cmd
, transid
);
681 err
= kvaser_usb_send_cmd(dev
, cmd
, kvaser_usb_hydra_cmd_size(cmd
));
685 err
= kvaser_usb_hydra_wait_cmd(dev
, CMD_MAP_CHANNEL_RESP
, cmd
);
689 err
= kvaser_usb_hydra_map_channel_resp(dev
, cmd
);
699 static int kvaser_usb_hydra_get_single_capability(struct kvaser_usb
*dev
,
700 u16 cap_cmd_req
, u16
*status
)
702 struct kvaser_usb_dev_card_data
*card_data
= &dev
->card_data
;
703 struct kvaser_cmd
*cmd
;
710 cmd
= kcalloc(1, sizeof(struct kvaser_cmd
), GFP_KERNEL
);
714 cmd
->header
.cmd_no
= CMD_GET_CAPABILITIES_REQ
;
715 cmd
->cap_req
.cap_cmd
= cpu_to_le16(cap_cmd_req
);
717 kvaser_usb_hydra_set_cmd_dest_he(cmd
, card_data
->hydra
.sysdbg_he
);
718 kvaser_usb_hydra_set_cmd_transid
719 (cmd
, kvaser_usb_hydra_get_next_transid(dev
));
721 err
= kvaser_usb_send_cmd(dev
, cmd
, kvaser_usb_hydra_cmd_size(cmd
));
725 err
= kvaser_usb_hydra_wait_cmd(dev
, CMD_GET_CAPABILITIES_RESP
, cmd
);
729 *status
= le16_to_cpu(cmd
->cap_res
.status
);
731 if (*status
!= KVASER_USB_HYDRA_CAP_STAT_OK
)
734 cap_cmd_res
= le16_to_cpu(cmd
->cap_res
.cap_cmd
);
735 switch (cap_cmd_res
) {
736 case KVASER_USB_HYDRA_CAP_CMD_LISTEN_MODE
:
737 case KVASER_USB_HYDRA_CAP_CMD_ERR_REPORT
:
738 case KVASER_USB_HYDRA_CAP_CMD_ONE_SHOT
:
739 value
= le32_to_cpu(cmd
->cap_res
.value
);
740 mask
= le32_to_cpu(cmd
->cap_res
.mask
);
743 dev_warn(&dev
->intf
->dev
, "Unknown capability command %u\n",
748 for (i
= 0; i
< dev
->nchannels
; i
++) {
749 if (BIT(i
) & (value
& mask
)) {
750 switch (cap_cmd_res
) {
751 case KVASER_USB_HYDRA_CAP_CMD_LISTEN_MODE
:
752 card_data
->ctrlmode_supported
|=
753 CAN_CTRLMODE_LISTENONLY
;
755 case KVASER_USB_HYDRA_CAP_CMD_ERR_REPORT
:
756 card_data
->capabilities
|=
757 KVASER_USB_CAP_BERR_CAP
;
759 case KVASER_USB_HYDRA_CAP_CMD_ONE_SHOT
:
760 card_data
->ctrlmode_supported
|=
761 CAN_CTRLMODE_ONE_SHOT
;
773 static void kvaser_usb_hydra_start_chip_reply(const struct kvaser_usb
*dev
,
774 const struct kvaser_cmd
*cmd
)
776 struct kvaser_usb_net_priv
*priv
;
778 priv
= kvaser_usb_hydra_net_priv_from_cmd(dev
, cmd
);
782 if (completion_done(&priv
->start_comp
) &&
783 netif_queue_stopped(priv
->netdev
)) {
784 netif_wake_queue(priv
->netdev
);
786 netif_start_queue(priv
->netdev
);
787 complete(&priv
->start_comp
);
791 static void kvaser_usb_hydra_stop_chip_reply(const struct kvaser_usb
*dev
,
792 const struct kvaser_cmd
*cmd
)
794 struct kvaser_usb_net_priv
*priv
;
796 priv
= kvaser_usb_hydra_net_priv_from_cmd(dev
, cmd
);
800 complete(&priv
->stop_comp
);
803 static void kvaser_usb_hydra_flush_queue_reply(const struct kvaser_usb
*dev
,
804 const struct kvaser_cmd
*cmd
)
806 struct kvaser_usb_net_priv
*priv
;
808 priv
= kvaser_usb_hydra_net_priv_from_cmd(dev
, cmd
);
812 complete(&priv
->flush_comp
);
816 kvaser_usb_hydra_bus_status_to_can_state(const struct kvaser_usb_net_priv
*priv
,
818 const struct can_berr_counter
*bec
,
819 enum can_state
*new_state
)
821 if (bus_status
& KVASER_USB_HYDRA_BUS_BUS_OFF
) {
822 *new_state
= CAN_STATE_BUS_OFF
;
823 } else if (bus_status
& KVASER_USB_HYDRA_BUS_ERR_PASS
) {
824 *new_state
= CAN_STATE_ERROR_PASSIVE
;
825 } else if (bus_status
== KVASER_USB_HYDRA_BUS_ERR_ACT
) {
826 if (bec
->txerr
>= 128 || bec
->rxerr
>= 128) {
827 netdev_warn(priv
->netdev
,
828 "ERR_ACTIVE but err tx=%u or rx=%u >=128\n",
829 bec
->txerr
, bec
->rxerr
);
830 *new_state
= CAN_STATE_ERROR_PASSIVE
;
831 } else if (bec
->txerr
>= 96 || bec
->rxerr
>= 96) {
832 *new_state
= CAN_STATE_ERROR_WARNING
;
834 *new_state
= CAN_STATE_ERROR_ACTIVE
;
839 static void kvaser_usb_hydra_update_state(struct kvaser_usb_net_priv
*priv
,
841 const struct can_berr_counter
*bec
)
843 struct net_device
*netdev
= priv
->netdev
;
844 struct can_frame
*cf
;
846 struct net_device_stats
*stats
;
847 enum can_state new_state
, old_state
;
849 old_state
= priv
->can
.state
;
851 kvaser_usb_hydra_bus_status_to_can_state(priv
, bus_status
, bec
,
854 if (new_state
== old_state
)
857 /* Ignore state change if previous state was STOPPED and the new state
858 * is BUS_OFF. Firmware always report this as BUS_OFF, since firmware
859 * does not distinguish between BUS_OFF and STOPPED.
861 if (old_state
== CAN_STATE_STOPPED
&& new_state
== CAN_STATE_BUS_OFF
)
864 skb
= alloc_can_err_skb(netdev
, &cf
);
866 enum can_state tx_state
, rx_state
;
868 tx_state
= (bec
->txerr
>= bec
->rxerr
) ?
869 new_state
: CAN_STATE_ERROR_ACTIVE
;
870 rx_state
= (bec
->txerr
<= bec
->rxerr
) ?
871 new_state
: CAN_STATE_ERROR_ACTIVE
;
872 can_change_state(netdev
, cf
, tx_state
, rx_state
);
875 if (new_state
== CAN_STATE_BUS_OFF
&& old_state
< CAN_STATE_BUS_OFF
) {
876 if (!priv
->can
.restart_ms
)
877 kvaser_usb_hydra_send_simple_cmd_async
878 (priv
, CMD_STOP_CHIP_REQ
);
884 netdev_warn(netdev
, "No memory left for err_skb\n");
888 if (priv
->can
.restart_ms
&&
889 old_state
>= CAN_STATE_BUS_OFF
&&
890 new_state
< CAN_STATE_BUS_OFF
)
891 priv
->can
.can_stats
.restarts
++;
893 cf
->data
[6] = bec
->txerr
;
894 cf
->data
[7] = bec
->rxerr
;
896 stats
= &netdev
->stats
;
898 stats
->rx_bytes
+= cf
->can_dlc
;
902 static void kvaser_usb_hydra_state_event(const struct kvaser_usb
*dev
,
903 const struct kvaser_cmd
*cmd
)
905 struct kvaser_usb_net_priv
*priv
;
906 struct can_berr_counter bec
;
909 priv
= kvaser_usb_hydra_net_priv_from_cmd(dev
, cmd
);
913 bus_status
= cmd
->chip_state_event
.bus_status
;
914 bec
.txerr
= cmd
->chip_state_event
.tx_err_counter
;
915 bec
.rxerr
= cmd
->chip_state_event
.rx_err_counter
;
917 kvaser_usb_hydra_update_state(priv
, bus_status
, &bec
);
918 priv
->bec
.txerr
= bec
.txerr
;
919 priv
->bec
.rxerr
= bec
.rxerr
;
922 static void kvaser_usb_hydra_error_event_parameter(const struct kvaser_usb
*dev
,
923 const struct kvaser_cmd
*cmd
)
925 /* info1 will contain the offending cmd_no */
926 switch (le16_to_cpu(cmd
->error_event
.info1
)) {
927 case CMD_START_CHIP_REQ
:
928 dev_warn(&dev
->intf
->dev
,
929 "CMD_START_CHIP_REQ error in parameter\n");
932 case CMD_STOP_CHIP_REQ
:
933 dev_warn(&dev
->intf
->dev
,
934 "CMD_STOP_CHIP_REQ error in parameter\n");
937 case CMD_FLUSH_QUEUE
:
938 dev_warn(&dev
->intf
->dev
,
939 "CMD_FLUSH_QUEUE error in parameter\n");
942 case CMD_SET_BUSPARAMS_REQ
:
943 dev_warn(&dev
->intf
->dev
,
944 "Set bittiming failed. Error in parameter\n");
947 case CMD_SET_BUSPARAMS_FD_REQ
:
948 dev_warn(&dev
->intf
->dev
,
949 "Set data bittiming failed. Error in parameter\n");
953 dev_warn(&dev
->intf
->dev
,
954 "Unhandled parameter error event cmd_no (%u)\n",
955 le16_to_cpu(cmd
->error_event
.info1
));
960 static void kvaser_usb_hydra_error_event(const struct kvaser_usb
*dev
,
961 const struct kvaser_cmd
*cmd
)
963 switch (cmd
->error_event
.error_code
) {
964 case KVASER_USB_HYDRA_ERROR_EVENT_PARAM
:
965 kvaser_usb_hydra_error_event_parameter(dev
, cmd
);
968 case KVASER_USB_HYDRA_ERROR_EVENT_CAN
:
969 /* Wrong channel mapping?! This should never happen!
970 * info1 will contain the offending cmd_no
972 dev_err(&dev
->intf
->dev
,
973 "Received CAN error event for cmd_no (%u)\n",
974 le16_to_cpu(cmd
->error_event
.info1
));
978 dev_warn(&dev
->intf
->dev
,
979 "Unhandled error event (%d)\n",
980 cmd
->error_event
.error_code
);
986 kvaser_usb_hydra_error_frame(struct kvaser_usb_net_priv
*priv
,
987 const struct kvaser_err_frame_data
*err_frame_data
,
990 struct net_device
*netdev
= priv
->netdev
;
991 struct net_device_stats
*stats
= &netdev
->stats
;
992 struct can_frame
*cf
;
994 struct skb_shared_hwtstamps
*shhwtstamps
;
995 struct can_berr_counter bec
;
996 enum can_state new_state
, old_state
;
999 priv
->can
.can_stats
.bus_error
++;
1002 bus_status
= err_frame_data
->bus_status
;
1003 bec
.txerr
= err_frame_data
->tx_err_counter
;
1004 bec
.rxerr
= err_frame_data
->rx_err_counter
;
1006 old_state
= priv
->can
.state
;
1007 kvaser_usb_hydra_bus_status_to_can_state(priv
, bus_status
, &bec
,
1010 skb
= alloc_can_err_skb(netdev
, &cf
);
1012 if (new_state
!= old_state
) {
1014 enum can_state tx_state
, rx_state
;
1016 tx_state
= (bec
.txerr
>= bec
.rxerr
) ?
1017 new_state
: CAN_STATE_ERROR_ACTIVE
;
1018 rx_state
= (bec
.txerr
<= bec
.rxerr
) ?
1019 new_state
: CAN_STATE_ERROR_ACTIVE
;
1021 can_change_state(netdev
, cf
, tx_state
, rx_state
);
1023 if (priv
->can
.restart_ms
&&
1024 old_state
>= CAN_STATE_BUS_OFF
&&
1025 new_state
< CAN_STATE_BUS_OFF
)
1026 cf
->can_id
|= CAN_ERR_RESTARTED
;
1029 if (new_state
== CAN_STATE_BUS_OFF
) {
1030 if (!priv
->can
.restart_ms
)
1031 kvaser_usb_hydra_send_simple_cmd_async
1032 (priv
, CMD_STOP_CHIP_REQ
);
1034 can_bus_off(netdev
);
1039 stats
->rx_dropped
++;
1040 netdev_warn(netdev
, "No memory left for err_skb\n");
1044 shhwtstamps
= skb_hwtstamps(skb
);
1045 shhwtstamps
->hwtstamp
= hwtstamp
;
1047 cf
->can_id
|= CAN_ERR_BUSERROR
;
1048 cf
->data
[6] = bec
.txerr
;
1049 cf
->data
[7] = bec
.rxerr
;
1051 stats
->rx_packets
++;
1052 stats
->rx_bytes
+= cf
->can_dlc
;
1055 priv
->bec
.txerr
= bec
.txerr
;
1056 priv
->bec
.rxerr
= bec
.rxerr
;
1059 static void kvaser_usb_hydra_one_shot_fail(struct kvaser_usb_net_priv
*priv
,
1060 const struct kvaser_cmd_ext
*cmd
)
1062 struct net_device
*netdev
= priv
->netdev
;
1063 struct net_device_stats
*stats
= &netdev
->stats
;
1064 struct can_frame
*cf
;
1065 struct sk_buff
*skb
;
1068 skb
= alloc_can_err_skb(netdev
, &cf
);
1070 stats
->rx_dropped
++;
1071 netdev_warn(netdev
, "No memory left for err_skb\n");
1075 cf
->can_id
|= CAN_ERR_BUSERROR
;
1076 flags
= le32_to_cpu(cmd
->tx_ack
.flags
);
1078 if (flags
& KVASER_USB_HYDRA_CF_FLAG_OSM_NACK
)
1079 cf
->can_id
|= CAN_ERR_ACK
;
1080 if (flags
& KVASER_USB_HYDRA_CF_FLAG_ABL
) {
1081 cf
->can_id
|= CAN_ERR_LOSTARB
;
1082 priv
->can
.can_stats
.arbitration_lost
++;
1086 stats
->rx_packets
++;
1087 stats
->rx_bytes
+= cf
->can_dlc
;
1091 static void kvaser_usb_hydra_tx_acknowledge(const struct kvaser_usb
*dev
,
1092 const struct kvaser_cmd
*cmd
)
1094 struct kvaser_usb_tx_urb_context
*context
;
1095 struct kvaser_usb_net_priv
*priv
;
1096 unsigned long irq_flags
;
1097 bool one_shot_fail
= false;
1098 u16 transid
= kvaser_usb_hydra_get_cmd_transid(cmd
);
1100 priv
= kvaser_usb_hydra_net_priv_from_cmd(dev
, cmd
);
1104 if (!netif_device_present(priv
->netdev
))
1107 if (cmd
->header
.cmd_no
== CMD_EXTENDED
) {
1108 struct kvaser_cmd_ext
*cmd_ext
= (struct kvaser_cmd_ext
*)cmd
;
1109 u32 flags
= le32_to_cpu(cmd_ext
->tx_ack
.flags
);
1111 if (flags
& (KVASER_USB_HYDRA_CF_FLAG_OSM_NACK
|
1112 KVASER_USB_HYDRA_CF_FLAG_ABL
)) {
1113 kvaser_usb_hydra_one_shot_fail(priv
, cmd_ext
);
1114 one_shot_fail
= true;
1118 context
= &priv
->tx_contexts
[transid
% dev
->max_tx_urbs
];
1119 if (!one_shot_fail
) {
1120 struct net_device_stats
*stats
= &priv
->netdev
->stats
;
1122 stats
->tx_packets
++;
1123 stats
->tx_bytes
+= can_dlc2len(context
->dlc
);
1126 spin_lock_irqsave(&priv
->tx_contexts_lock
, irq_flags
);
1128 can_get_echo_skb(priv
->netdev
, context
->echo_index
);
1129 context
->echo_index
= dev
->max_tx_urbs
;
1130 --priv
->active_tx_contexts
;
1131 netif_wake_queue(priv
->netdev
);
1133 spin_unlock_irqrestore(&priv
->tx_contexts_lock
, irq_flags
);
1136 static void kvaser_usb_hydra_rx_msg_std(const struct kvaser_usb
*dev
,
1137 const struct kvaser_cmd
*cmd
)
1139 struct kvaser_usb_net_priv
*priv
= NULL
;
1140 struct can_frame
*cf
;
1141 struct sk_buff
*skb
;
1142 struct skb_shared_hwtstamps
*shhwtstamps
;
1143 struct net_device_stats
*stats
;
1147 priv
= kvaser_usb_hydra_net_priv_from_cmd(dev
, cmd
);
1151 stats
= &priv
->netdev
->stats
;
1153 flags
= cmd
->rx_can
.flags
;
1154 hwtstamp
= kvaser_usb_hydra_ktime_from_rx_cmd(dev
->cfg
, cmd
);
1156 if (flags
& KVASER_USB_HYDRA_CF_FLAG_ERROR_FRAME
) {
1157 kvaser_usb_hydra_error_frame(priv
, &cmd
->rx_can
.err_frame_data
,
1162 skb
= alloc_can_skb(priv
->netdev
, &cf
);
1164 stats
->rx_dropped
++;
1168 shhwtstamps
= skb_hwtstamps(skb
);
1169 shhwtstamps
->hwtstamp
= hwtstamp
;
1171 cf
->can_id
= le32_to_cpu(cmd
->rx_can
.id
);
1173 if (cf
->can_id
& KVASER_USB_HYDRA_EXTENDED_FRAME_ID
) {
1174 cf
->can_id
&= CAN_EFF_MASK
;
1175 cf
->can_id
|= CAN_EFF_FLAG
;
1177 cf
->can_id
&= CAN_SFF_MASK
;
1180 if (flags
& KVASER_USB_HYDRA_CF_FLAG_OVERRUN
)
1181 kvaser_usb_can_rx_over_error(priv
->netdev
);
1183 cf
->can_dlc
= get_can_dlc(cmd
->rx_can
.dlc
);
1185 if (flags
& KVASER_USB_HYDRA_CF_FLAG_REMOTE_FRAME
)
1186 cf
->can_id
|= CAN_RTR_FLAG
;
1188 memcpy(cf
->data
, cmd
->rx_can
.data
, cf
->can_dlc
);
1190 stats
->rx_packets
++;
1191 stats
->rx_bytes
+= cf
->can_dlc
;
1195 static void kvaser_usb_hydra_rx_msg_ext(const struct kvaser_usb
*dev
,
1196 const struct kvaser_cmd_ext
*cmd
)
1198 struct kvaser_cmd
*std_cmd
= (struct kvaser_cmd
*)cmd
;
1199 struct kvaser_usb_net_priv
*priv
;
1200 struct canfd_frame
*cf
;
1201 struct sk_buff
*skb
;
1202 struct skb_shared_hwtstamps
*shhwtstamps
;
1203 struct net_device_stats
*stats
;
1209 priv
= kvaser_usb_hydra_net_priv_from_cmd(dev
, std_cmd
);
1213 stats
= &priv
->netdev
->stats
;
1215 kcan_header
= le32_to_cpu(cmd
->rx_can
.kcan_header
);
1216 dlc
= (kcan_header
& KVASER_USB_KCAN_DATA_DLC_MASK
) >>
1217 KVASER_USB_KCAN_DATA_DLC_SHIFT
;
1219 flags
= le32_to_cpu(cmd
->rx_can
.flags
);
1220 hwtstamp
= kvaser_usb_hydra_ktime_from_rx_cmd(dev
->cfg
, std_cmd
);
1222 if (flags
& KVASER_USB_HYDRA_CF_FLAG_ERROR_FRAME
) {
1223 kvaser_usb_hydra_error_frame(priv
, &cmd
->rx_can
.err_frame_data
,
1228 if (flags
& KVASER_USB_HYDRA_CF_FLAG_FDF
)
1229 skb
= alloc_canfd_skb(priv
->netdev
, &cf
);
1231 skb
= alloc_can_skb(priv
->netdev
, (struct can_frame
**)&cf
);
1234 stats
->rx_dropped
++;
1238 shhwtstamps
= skb_hwtstamps(skb
);
1239 shhwtstamps
->hwtstamp
= hwtstamp
;
1241 cf
->can_id
= le32_to_cpu(cmd
->rx_can
.id
);
1243 if (flags
& KVASER_USB_HYDRA_CF_FLAG_EXTENDED_ID
) {
1244 cf
->can_id
&= CAN_EFF_MASK
;
1245 cf
->can_id
|= CAN_EFF_FLAG
;
1247 cf
->can_id
&= CAN_SFF_MASK
;
1250 if (flags
& KVASER_USB_HYDRA_CF_FLAG_OVERRUN
)
1251 kvaser_usb_can_rx_over_error(priv
->netdev
);
1253 if (flags
& KVASER_USB_HYDRA_CF_FLAG_FDF
) {
1254 cf
->len
= can_dlc2len(get_canfd_dlc(dlc
));
1255 if (flags
& KVASER_USB_HYDRA_CF_FLAG_BRS
)
1256 cf
->flags
|= CANFD_BRS
;
1257 if (flags
& KVASER_USB_HYDRA_CF_FLAG_ESI
)
1258 cf
->flags
|= CANFD_ESI
;
1260 cf
->len
= get_can_dlc(dlc
);
1263 if (flags
& KVASER_USB_HYDRA_CF_FLAG_REMOTE_FRAME
)
1264 cf
->can_id
|= CAN_RTR_FLAG
;
1266 memcpy(cf
->data
, cmd
->rx_can
.kcan_payload
, cf
->len
);
1268 stats
->rx_packets
++;
1269 stats
->rx_bytes
+= cf
->len
;
1273 static void kvaser_usb_hydra_handle_cmd_std(const struct kvaser_usb
*dev
,
1274 const struct kvaser_cmd
*cmd
)
1276 switch (cmd
->header
.cmd_no
) {
1277 case CMD_START_CHIP_RESP
:
1278 kvaser_usb_hydra_start_chip_reply(dev
, cmd
);
1281 case CMD_STOP_CHIP_RESP
:
1282 kvaser_usb_hydra_stop_chip_reply(dev
, cmd
);
1285 case CMD_FLUSH_QUEUE_RESP
:
1286 kvaser_usb_hydra_flush_queue_reply(dev
, cmd
);
1289 case CMD_CHIP_STATE_EVENT
:
1290 kvaser_usb_hydra_state_event(dev
, cmd
);
1293 case CMD_ERROR_EVENT
:
1294 kvaser_usb_hydra_error_event(dev
, cmd
);
1297 case CMD_TX_ACKNOWLEDGE
:
1298 kvaser_usb_hydra_tx_acknowledge(dev
, cmd
);
1301 case CMD_RX_MESSAGE
:
1302 kvaser_usb_hydra_rx_msg_std(dev
, cmd
);
1305 /* Ignored commands */
1306 case CMD_SET_BUSPARAMS_RESP
:
1307 case CMD_SET_BUSPARAMS_FD_RESP
:
1311 dev_warn(&dev
->intf
->dev
, "Unhandled command (%d)\n",
1312 cmd
->header
.cmd_no
);
1317 static void kvaser_usb_hydra_handle_cmd_ext(const struct kvaser_usb
*dev
,
1318 const struct kvaser_cmd_ext
*cmd
)
1320 switch (cmd
->cmd_no_ext
) {
1321 case CMD_TX_ACKNOWLEDGE_FD
:
1322 kvaser_usb_hydra_tx_acknowledge(dev
, (struct kvaser_cmd
*)cmd
);
1325 case CMD_RX_MESSAGE_FD
:
1326 kvaser_usb_hydra_rx_msg_ext(dev
, cmd
);
1330 dev_warn(&dev
->intf
->dev
, "Unhandled extended command (%d)\n",
1331 cmd
->header
.cmd_no
);
1336 static void kvaser_usb_hydra_handle_cmd(const struct kvaser_usb
*dev
,
1337 const struct kvaser_cmd
*cmd
)
1339 if (cmd
->header
.cmd_no
== CMD_EXTENDED
)
1340 kvaser_usb_hydra_handle_cmd_ext
1341 (dev
, (struct kvaser_cmd_ext
*)cmd
);
1343 kvaser_usb_hydra_handle_cmd_std(dev
, cmd
);
1347 kvaser_usb_hydra_frame_to_cmd_ext(const struct kvaser_usb_net_priv
*priv
,
1348 const struct sk_buff
*skb
, int *frame_len
,
1349 int *cmd_len
, u16 transid
)
1351 struct kvaser_usb
*dev
= priv
->dev
;
1352 struct kvaser_cmd_ext
*cmd
;
1353 struct canfd_frame
*cf
= (struct canfd_frame
*)skb
->data
;
1354 u8 dlc
= can_len2dlc(cf
->len
);
1355 u8 nbr_of_bytes
= cf
->len
;
1361 *frame_len
= nbr_of_bytes
;
1363 cmd
= kcalloc(1, sizeof(struct kvaser_cmd_ext
), GFP_ATOMIC
);
1367 kvaser_usb_hydra_set_cmd_dest_he
1368 ((struct kvaser_cmd
*)cmd
,
1369 dev
->card_data
.hydra
.channel_to_he
[priv
->channel
]);
1370 kvaser_usb_hydra_set_cmd_transid((struct kvaser_cmd
*)cmd
, transid
);
1372 cmd
->header
.cmd_no
= CMD_EXTENDED
;
1373 cmd
->cmd_no_ext
= CMD_TX_CAN_MESSAGE_FD
;
1375 *cmd_len
= ALIGN(sizeof(struct kvaser_cmd_ext
) -
1376 sizeof(cmd
->tx_can
.kcan_payload
) + nbr_of_bytes
,
1379 cmd
->len
= cpu_to_le16(*cmd_len
);
1381 cmd
->tx_can
.databytes
= nbr_of_bytes
;
1382 cmd
->tx_can
.dlc
= dlc
;
1384 if (cf
->can_id
& CAN_EFF_FLAG
) {
1385 id
= cf
->can_id
& CAN_EFF_MASK
;
1386 flags
= KVASER_USB_HYDRA_CF_FLAG_EXTENDED_ID
;
1387 kcan_id
= (cf
->can_id
& CAN_EFF_MASK
) |
1388 KVASER_USB_KCAN_DATA_IDE
| KVASER_USB_KCAN_DATA_SRR
;
1390 id
= cf
->can_id
& CAN_SFF_MASK
;
1392 kcan_id
= cf
->can_id
& CAN_SFF_MASK
;
1395 if (cf
->can_id
& CAN_ERR_FLAG
)
1396 flags
|= KVASER_USB_HYDRA_CF_FLAG_ERROR_FRAME
;
1398 kcan_header
= ((dlc
<< KVASER_USB_KCAN_DATA_DLC_SHIFT
) &
1399 KVASER_USB_KCAN_DATA_DLC_MASK
) |
1400 KVASER_USB_KCAN_DATA_AREQ
|
1401 (priv
->can
.ctrlmode
& CAN_CTRLMODE_ONE_SHOT
?
1402 KVASER_USB_KCAN_DATA_OSM
: 0);
1404 if (can_is_canfd_skb(skb
)) {
1405 kcan_header
|= KVASER_USB_KCAN_DATA_FDF
|
1406 (cf
->flags
& CANFD_BRS
?
1407 KVASER_USB_KCAN_DATA_BRS
: 0);
1409 if (cf
->can_id
& CAN_RTR_FLAG
) {
1410 kcan_id
|= KVASER_USB_KCAN_DATA_RTR
;
1411 cmd
->tx_can
.databytes
= 0;
1412 flags
|= KVASER_USB_HYDRA_CF_FLAG_REMOTE_FRAME
;
1416 cmd
->tx_can
.kcan_id
= cpu_to_le32(kcan_id
);
1417 cmd
->tx_can
.id
= cpu_to_le32(id
);
1418 cmd
->tx_can
.flags
= cpu_to_le32(flags
);
1419 cmd
->tx_can
.kcan_header
= cpu_to_le32(kcan_header
);
1421 memcpy(cmd
->tx_can
.kcan_payload
, cf
->data
, nbr_of_bytes
);
1427 kvaser_usb_hydra_frame_to_cmd_std(const struct kvaser_usb_net_priv
*priv
,
1428 const struct sk_buff
*skb
, int *frame_len
,
1429 int *cmd_len
, u16 transid
)
1431 struct kvaser_usb
*dev
= priv
->dev
;
1432 struct kvaser_cmd
*cmd
;
1433 struct can_frame
*cf
= (struct can_frame
*)skb
->data
;
1437 *frame_len
= cf
->can_dlc
;
1439 cmd
= kcalloc(1, sizeof(struct kvaser_cmd
), GFP_ATOMIC
);
1443 kvaser_usb_hydra_set_cmd_dest_he
1444 (cmd
, dev
->card_data
.hydra
.channel_to_he
[priv
->channel
]);
1445 kvaser_usb_hydra_set_cmd_transid(cmd
, transid
);
1447 cmd
->header
.cmd_no
= CMD_TX_CAN_MESSAGE
;
1449 *cmd_len
= ALIGN(sizeof(struct kvaser_cmd
), 8);
1451 if (cf
->can_id
& CAN_EFF_FLAG
) {
1452 id
= (cf
->can_id
& CAN_EFF_MASK
);
1453 id
|= KVASER_USB_HYDRA_EXTENDED_FRAME_ID
;
1455 id
= cf
->can_id
& CAN_SFF_MASK
;
1458 cmd
->tx_can
.dlc
= cf
->can_dlc
;
1460 flags
= (cf
->can_id
& CAN_EFF_FLAG
?
1461 KVASER_USB_HYDRA_CF_FLAG_EXTENDED_ID
: 0);
1463 if (cf
->can_id
& CAN_RTR_FLAG
)
1464 flags
|= KVASER_USB_HYDRA_CF_FLAG_REMOTE_FRAME
;
1466 flags
|= (cf
->can_id
& CAN_ERR_FLAG
?
1467 KVASER_USB_HYDRA_CF_FLAG_ERROR_FRAME
: 0);
1469 cmd
->tx_can
.id
= cpu_to_le32(id
);
1470 cmd
->tx_can
.flags
= flags
;
1472 memcpy(cmd
->tx_can
.data
, cf
->data
, *frame_len
);
1477 static int kvaser_usb_hydra_set_mode(struct net_device
*netdev
,
1483 case CAN_MODE_START
:
1484 /* CAN controller automatically recovers from BUS_OFF */
1493 static int kvaser_usb_hydra_set_bittiming(struct net_device
*netdev
)
1495 struct kvaser_cmd
*cmd
;
1496 struct kvaser_usb_net_priv
*priv
= netdev_priv(netdev
);
1497 struct can_bittiming
*bt
= &priv
->can
.bittiming
;
1498 struct kvaser_usb
*dev
= priv
->dev
;
1499 int tseg1
= bt
->prop_seg
+ bt
->phase_seg1
;
1500 int tseg2
= bt
->phase_seg2
;
1504 cmd
= kcalloc(1, sizeof(struct kvaser_cmd
), GFP_KERNEL
);
1508 cmd
->header
.cmd_no
= CMD_SET_BUSPARAMS_REQ
;
1509 cmd
->set_busparams_req
.bitrate
= cpu_to_le32(bt
->bitrate
);
1510 cmd
->set_busparams_req
.sjw
= (u8
)sjw
;
1511 cmd
->set_busparams_req
.tseg1
= (u8
)tseg1
;
1512 cmd
->set_busparams_req
.tseg2
= (u8
)tseg2
;
1513 cmd
->set_busparams_req
.nsamples
= 1;
1515 kvaser_usb_hydra_set_cmd_dest_he
1516 (cmd
, dev
->card_data
.hydra
.channel_to_he
[priv
->channel
]);
1517 kvaser_usb_hydra_set_cmd_transid
1518 (cmd
, kvaser_usb_hydra_get_next_transid(dev
));
1520 err
= kvaser_usb_send_cmd(dev
, cmd
, kvaser_usb_hydra_cmd_size(cmd
));
1527 static int kvaser_usb_hydra_set_data_bittiming(struct net_device
*netdev
)
1529 struct kvaser_cmd
*cmd
;
1530 struct kvaser_usb_net_priv
*priv
= netdev_priv(netdev
);
1531 struct can_bittiming
*dbt
= &priv
->can
.data_bittiming
;
1532 struct kvaser_usb
*dev
= priv
->dev
;
1533 int tseg1
= dbt
->prop_seg
+ dbt
->phase_seg1
;
1534 int tseg2
= dbt
->phase_seg2
;
1538 cmd
= kcalloc(1, sizeof(struct kvaser_cmd
), GFP_KERNEL
);
1542 cmd
->header
.cmd_no
= CMD_SET_BUSPARAMS_FD_REQ
;
1543 cmd
->set_busparams_req
.bitrate_d
= cpu_to_le32(dbt
->bitrate
);
1544 cmd
->set_busparams_req
.sjw_d
= (u8
)sjw
;
1545 cmd
->set_busparams_req
.tseg1_d
= (u8
)tseg1
;
1546 cmd
->set_busparams_req
.tseg2_d
= (u8
)tseg2
;
1547 cmd
->set_busparams_req
.nsamples_d
= 1;
1549 if (priv
->can
.ctrlmode
& CAN_CTRLMODE_FD
) {
1550 if (priv
->can
.ctrlmode
& CAN_CTRLMODE_FD_NON_ISO
)
1551 cmd
->set_busparams_req
.canfd_mode
=
1552 KVASER_USB_HYDRA_BUS_MODE_NONISO
;
1554 cmd
->set_busparams_req
.canfd_mode
=
1555 KVASER_USB_HYDRA_BUS_MODE_CANFD_ISO
;
1558 kvaser_usb_hydra_set_cmd_dest_he
1559 (cmd
, dev
->card_data
.hydra
.channel_to_he
[priv
->channel
]);
1560 kvaser_usb_hydra_set_cmd_transid
1561 (cmd
, kvaser_usb_hydra_get_next_transid(dev
));
1563 err
= kvaser_usb_send_cmd(dev
, cmd
, kvaser_usb_hydra_cmd_size(cmd
));
1570 static int kvaser_usb_hydra_get_berr_counter(const struct net_device
*netdev
,
1571 struct can_berr_counter
*bec
)
1573 struct kvaser_usb_net_priv
*priv
= netdev_priv(netdev
);
1576 err
= kvaser_usb_hydra_send_simple_cmd(priv
->dev
,
1577 CMD_GET_CHIP_STATE_REQ
,
1587 static int kvaser_usb_hydra_setup_endpoints(struct kvaser_usb
*dev
)
1589 const struct usb_host_interface
*iface_desc
;
1590 struct usb_endpoint_descriptor
*ep
;
1593 iface_desc
= dev
->intf
->cur_altsetting
;
1595 for (i
= 0; i
< iface_desc
->desc
.bNumEndpoints
; ++i
) {
1596 ep
= &iface_desc
->endpoint
[i
].desc
;
1598 if (!dev
->bulk_in
&& usb_endpoint_is_bulk_in(ep
) &&
1599 ep
->bEndpointAddress
== KVASER_USB_HYDRA_BULK_EP_IN_ADDR
)
1602 if (!dev
->bulk_out
&& usb_endpoint_is_bulk_out(ep
) &&
1603 ep
->bEndpointAddress
== KVASER_USB_HYDRA_BULK_EP_OUT_ADDR
)
1606 if (dev
->bulk_in
&& dev
->bulk_out
)
1613 static int kvaser_usb_hydra_init_card(struct kvaser_usb
*dev
)
1617 struct kvaser_usb_dev_card_data_hydra
*card_data
=
1618 &dev
->card_data
.hydra
;
1620 card_data
->transid
= KVASER_USB_HYDRA_MIN_TRANSID
;
1621 spin_lock_init(&card_data
->transid_lock
);
1623 memset(card_data
->usb_rx_leftover
, 0, KVASER_USB_HYDRA_MAX_CMD_LEN
);
1624 card_data
->usb_rx_leftover_len
= 0;
1625 spin_lock_init(&card_data
->usb_rx_leftover_lock
);
1627 memset(card_data
->channel_to_he
, KVASER_USB_HYDRA_HE_ADDRESS_ILLEGAL
,
1628 sizeof(card_data
->channel_to_he
));
1629 card_data
->sysdbg_he
= 0;
1631 for (i
= 0; i
< KVASER_USB_MAX_NET_DEVICES
; i
++) {
1632 err
= kvaser_usb_hydra_map_channel
1634 (KVASER_USB_HYDRA_TRANSID_CANHE
| i
),
1637 dev_err(&dev
->intf
->dev
,
1638 "CMD_MAP_CHANNEL_REQ failed for CAN%u\n", i
);
1643 err
= kvaser_usb_hydra_map_channel(dev
, KVASER_USB_HYDRA_TRANSID_SYSDBG
,
1646 dev_err(&dev
->intf
->dev
,
1647 "CMD_MAP_CHANNEL_REQ failed for SYSDBG\n");
1654 static int kvaser_usb_hydra_get_software_info(struct kvaser_usb
*dev
)
1656 struct kvaser_cmd cmd
;
1659 err
= kvaser_usb_hydra_send_simple_cmd(dev
, CMD_GET_SOFTWARE_INFO_REQ
,
1664 memset(&cmd
, 0, sizeof(struct kvaser_cmd
));
1665 err
= kvaser_usb_hydra_wait_cmd(dev
, CMD_GET_SOFTWARE_INFO_RESP
, &cmd
);
1669 dev
->max_tx_urbs
= min_t(unsigned int, KVASER_USB_MAX_TX_URBS
,
1670 le16_to_cpu(cmd
.sw_info
.max_outstanding_tx
));
1675 static int kvaser_usb_hydra_get_software_details(struct kvaser_usb
*dev
)
1677 struct kvaser_cmd
*cmd
;
1680 struct kvaser_usb_dev_card_data
*card_data
= &dev
->card_data
;
1682 cmd
= kcalloc(1, sizeof(struct kvaser_cmd
), GFP_KERNEL
);
1686 cmd
->header
.cmd_no
= CMD_GET_SOFTWARE_DETAILS_REQ
;
1687 cmd
->sw_detail_req
.use_ext_cmd
= 1;
1688 kvaser_usb_hydra_set_cmd_dest_he
1689 (cmd
, KVASER_USB_HYDRA_HE_ADDRESS_ILLEGAL
);
1691 kvaser_usb_hydra_set_cmd_transid
1692 (cmd
, kvaser_usb_hydra_get_next_transid(dev
));
1694 err
= kvaser_usb_send_cmd(dev
, cmd
, kvaser_usb_hydra_cmd_size(cmd
));
1698 err
= kvaser_usb_hydra_wait_cmd(dev
, CMD_GET_SOFTWARE_DETAILS_RESP
,
1703 dev
->fw_version
= le32_to_cpu(cmd
->sw_detail_res
.sw_version
);
1704 flags
= le32_to_cpu(cmd
->sw_detail_res
.sw_flags
);
1706 if (flags
& KVASER_USB_HYDRA_SW_FLAG_FW_BAD
) {
1707 dev_err(&dev
->intf
->dev
,
1708 "Bad firmware, device refuse to run!\n");
1713 if (flags
& KVASER_USB_HYDRA_SW_FLAG_FW_BETA
)
1714 dev_info(&dev
->intf
->dev
, "Beta firmware in use\n");
1716 if (flags
& KVASER_USB_HYDRA_SW_FLAG_EXT_CAP
)
1717 card_data
->capabilities
|= KVASER_USB_CAP_EXT_CAP
;
1719 if (flags
& KVASER_USB_HYDRA_SW_FLAG_EXT_CMD
)
1720 card_data
->capabilities
|= KVASER_USB_HYDRA_CAP_EXT_CMD
;
1722 if (flags
& KVASER_USB_HYDRA_SW_FLAG_CANFD
)
1723 card_data
->ctrlmode_supported
|= CAN_CTRLMODE_FD
;
1725 if (flags
& KVASER_USB_HYDRA_SW_FLAG_NONISO
)
1726 card_data
->ctrlmode_supported
|= CAN_CTRLMODE_FD_NON_ISO
;
1728 if (flags
& KVASER_USB_HYDRA_SW_FLAG_FREQ_80M
)
1729 dev
->cfg
= &kvaser_usb_hydra_dev_cfg_kcan
;
1731 dev
->cfg
= &kvaser_usb_hydra_dev_cfg_flexc
;
1739 static int kvaser_usb_hydra_get_card_info(struct kvaser_usb
*dev
)
1741 struct kvaser_cmd cmd
;
1744 err
= kvaser_usb_hydra_send_simple_cmd(dev
, CMD_GET_CARD_INFO_REQ
, -1);
1748 memset(&cmd
, 0, sizeof(struct kvaser_cmd
));
1749 err
= kvaser_usb_hydra_wait_cmd(dev
, CMD_GET_CARD_INFO_RESP
, &cmd
);
1753 dev
->nchannels
= cmd
.card_info
.nchannels
;
1754 if (dev
->nchannels
> KVASER_USB_MAX_NET_DEVICES
)
1760 static int kvaser_usb_hydra_get_capabilities(struct kvaser_usb
*dev
)
1765 if (!(dev
->card_data
.capabilities
& KVASER_USB_CAP_EXT_CAP
)) {
1766 dev_info(&dev
->intf
->dev
,
1767 "No extended capability support. Upgrade your device.\n");
1771 err
= kvaser_usb_hydra_get_single_capability
1773 KVASER_USB_HYDRA_CAP_CMD_LISTEN_MODE
,
1778 dev_info(&dev
->intf
->dev
,
1779 "KVASER_USB_HYDRA_CAP_CMD_LISTEN_MODE failed %u\n",
1782 err
= kvaser_usb_hydra_get_single_capability
1784 KVASER_USB_HYDRA_CAP_CMD_ERR_REPORT
,
1789 dev_info(&dev
->intf
->dev
,
1790 "KVASER_USB_HYDRA_CAP_CMD_ERR_REPORT failed %u\n",
1793 err
= kvaser_usb_hydra_get_single_capability
1794 (dev
, KVASER_USB_HYDRA_CAP_CMD_ONE_SHOT
,
1799 dev_info(&dev
->intf
->dev
,
1800 "KVASER_USB_HYDRA_CAP_CMD_ONE_SHOT failed %u\n",
1806 static int kvaser_usb_hydra_set_opt_mode(const struct kvaser_usb_net_priv
*priv
)
1808 struct kvaser_usb
*dev
= priv
->dev
;
1809 struct kvaser_cmd
*cmd
;
1812 if ((priv
->can
.ctrlmode
&
1813 (CAN_CTRLMODE_FD
| CAN_CTRLMODE_FD_NON_ISO
)) ==
1814 CAN_CTRLMODE_FD_NON_ISO
) {
1815 netdev_warn(priv
->netdev
,
1816 "CTRLMODE_FD shall be on if CTRLMODE_FD_NON_ISO is on\n");
1820 cmd
= kcalloc(1, sizeof(struct kvaser_cmd
), GFP_KERNEL
);
1824 cmd
->header
.cmd_no
= CMD_SET_DRIVERMODE_REQ
;
1825 kvaser_usb_hydra_set_cmd_dest_he
1826 (cmd
, dev
->card_data
.hydra
.channel_to_he
[priv
->channel
]);
1827 kvaser_usb_hydra_set_cmd_transid
1828 (cmd
, kvaser_usb_hydra_get_next_transid(dev
));
1829 if (priv
->can
.ctrlmode
& CAN_CTRLMODE_LISTENONLY
)
1830 cmd
->set_ctrlmode
.mode
= KVASER_USB_HYDRA_CTRLMODE_LISTEN
;
1832 cmd
->set_ctrlmode
.mode
= KVASER_USB_HYDRA_CTRLMODE_NORMAL
;
1834 err
= kvaser_usb_send_cmd(dev
, cmd
, kvaser_usb_hydra_cmd_size(cmd
));
1840 static int kvaser_usb_hydra_start_chip(struct kvaser_usb_net_priv
*priv
)
1844 init_completion(&priv
->start_comp
);
1846 err
= kvaser_usb_hydra_send_simple_cmd(priv
->dev
, CMD_START_CHIP_REQ
,
1851 if (!wait_for_completion_timeout(&priv
->start_comp
,
1852 msecs_to_jiffies(KVASER_USB_TIMEOUT
)))
1858 static int kvaser_usb_hydra_stop_chip(struct kvaser_usb_net_priv
*priv
)
1862 init_completion(&priv
->stop_comp
);
1864 /* Make sure we do not report invalid BUS_OFF from CMD_CHIP_STATE_EVENT
1865 * see comment in kvaser_usb_hydra_update_state()
1867 priv
->can
.state
= CAN_STATE_STOPPED
;
1869 err
= kvaser_usb_hydra_send_simple_cmd(priv
->dev
, CMD_STOP_CHIP_REQ
,
1874 if (!wait_for_completion_timeout(&priv
->stop_comp
,
1875 msecs_to_jiffies(KVASER_USB_TIMEOUT
)))
1881 static int kvaser_usb_hydra_flush_queue(struct kvaser_usb_net_priv
*priv
)
1885 init_completion(&priv
->flush_comp
);
1887 err
= kvaser_usb_hydra_send_simple_cmd(priv
->dev
, CMD_FLUSH_QUEUE
,
1892 if (!wait_for_completion_timeout(&priv
->flush_comp
,
1893 msecs_to_jiffies(KVASER_USB_TIMEOUT
)))
1899 /* A single extended hydra command can be transmitted in multiple transfers
1900 * We have to buffer partial hydra commands, and handle them on next callback.
1902 static void kvaser_usb_hydra_read_bulk_callback(struct kvaser_usb
*dev
,
1905 unsigned long irq_flags
;
1906 struct kvaser_cmd
*cmd
;
1909 struct kvaser_usb_dev_card_data_hydra
*card_data
=
1910 &dev
->card_data
.hydra
;
1911 int usb_rx_leftover_len
;
1912 spinlock_t
*usb_rx_leftover_lock
= &card_data
->usb_rx_leftover_lock
;
1914 spin_lock_irqsave(usb_rx_leftover_lock
, irq_flags
);
1915 usb_rx_leftover_len
= card_data
->usb_rx_leftover_len
;
1916 if (usb_rx_leftover_len
) {
1917 int remaining_bytes
;
1919 cmd
= (struct kvaser_cmd
*)card_data
->usb_rx_leftover
;
1921 cmd_len
= kvaser_usb_hydra_cmd_size(cmd
);
1923 remaining_bytes
= min_t(unsigned int, len
,
1924 cmd_len
- usb_rx_leftover_len
);
1925 /* Make sure we do not overflow usb_rx_leftover */
1926 if (remaining_bytes
+ usb_rx_leftover_len
>
1927 KVASER_USB_HYDRA_MAX_CMD_LEN
) {
1928 dev_err(&dev
->intf
->dev
, "Format error\n");
1929 spin_unlock_irqrestore(usb_rx_leftover_lock
, irq_flags
);
1933 memcpy(card_data
->usb_rx_leftover
+ usb_rx_leftover_len
, buf
,
1935 pos
+= remaining_bytes
;
1937 if (remaining_bytes
+ usb_rx_leftover_len
== cmd_len
) {
1938 kvaser_usb_hydra_handle_cmd(dev
, cmd
);
1939 usb_rx_leftover_len
= 0;
1941 /* Command still not complete */
1942 usb_rx_leftover_len
+= remaining_bytes
;
1944 card_data
->usb_rx_leftover_len
= usb_rx_leftover_len
;
1946 spin_unlock_irqrestore(usb_rx_leftover_lock
, irq_flags
);
1951 cmd_len
= kvaser_usb_hydra_cmd_size(cmd
);
1953 if (pos
+ cmd_len
> len
) {
1954 /* We got first part of a command */
1957 leftover_bytes
= len
- pos
;
1958 /* Make sure we do not overflow usb_rx_leftover */
1959 if (leftover_bytes
> KVASER_USB_HYDRA_MAX_CMD_LEN
) {
1960 dev_err(&dev
->intf
->dev
, "Format error\n");
1963 spin_lock_irqsave(usb_rx_leftover_lock
, irq_flags
);
1964 memcpy(card_data
->usb_rx_leftover
, buf
+ pos
,
1966 card_data
->usb_rx_leftover_len
= leftover_bytes
;
1967 spin_unlock_irqrestore(usb_rx_leftover_lock
, irq_flags
);
1971 kvaser_usb_hydra_handle_cmd(dev
, cmd
);
1977 kvaser_usb_hydra_frame_to_cmd(const struct kvaser_usb_net_priv
*priv
,
1978 const struct sk_buff
*skb
, int *frame_len
,
1979 int *cmd_len
, u16 transid
)
1983 if (priv
->dev
->card_data
.capabilities
& KVASER_USB_HYDRA_CAP_EXT_CMD
)
1984 buf
= kvaser_usb_hydra_frame_to_cmd_ext(priv
, skb
, frame_len
,
1987 buf
= kvaser_usb_hydra_frame_to_cmd_std(priv
, skb
, frame_len
,
1993 const struct kvaser_usb_dev_ops kvaser_usb_hydra_dev_ops
= {
1994 .dev_set_mode
= kvaser_usb_hydra_set_mode
,
1995 .dev_set_bittiming
= kvaser_usb_hydra_set_bittiming
,
1996 .dev_set_data_bittiming
= kvaser_usb_hydra_set_data_bittiming
,
1997 .dev_get_berr_counter
= kvaser_usb_hydra_get_berr_counter
,
1998 .dev_setup_endpoints
= kvaser_usb_hydra_setup_endpoints
,
1999 .dev_init_card
= kvaser_usb_hydra_init_card
,
2000 .dev_get_software_info
= kvaser_usb_hydra_get_software_info
,
2001 .dev_get_software_details
= kvaser_usb_hydra_get_software_details
,
2002 .dev_get_card_info
= kvaser_usb_hydra_get_card_info
,
2003 .dev_get_capabilities
= kvaser_usb_hydra_get_capabilities
,
2004 .dev_set_opt_mode
= kvaser_usb_hydra_set_opt_mode
,
2005 .dev_start_chip
= kvaser_usb_hydra_start_chip
,
2006 .dev_stop_chip
= kvaser_usb_hydra_stop_chip
,
2007 .dev_reset_chip
= NULL
,
2008 .dev_flush_queue
= kvaser_usb_hydra_flush_queue
,
2009 .dev_read_bulk_callback
= kvaser_usb_hydra_read_bulk_callback
,
2010 .dev_frame_to_cmd
= kvaser_usb_hydra_frame_to_cmd
,
2013 static const struct kvaser_usb_dev_cfg kvaser_usb_hydra_dev_cfg_kcan
= {
2017 .timestamp_freq
= 80,
2018 .bittiming_const
= &kvaser_usb_hydra_kcan_bittiming_c
,
2019 .data_bittiming_const
= &kvaser_usb_hydra_kcan_bittiming_c
,
2022 static const struct kvaser_usb_dev_cfg kvaser_usb_hydra_dev_cfg_flexc
= {
2026 .timestamp_freq
= 1,
2027 .bittiming_const
= &kvaser_usb_hydra_flexc_bittiming_c
,