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
5  *
6  * Copyright (C) 2018 KVASER AB, Sweden. All rights reserved.
7  * Copyright (C) 2010 Matthias Fuchs <matthias.fuchs@esd.eu>, esd gmbh
8  *
9  * Known issues:
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.
14  */
15 
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>
26 
27 #include <linux/can.h>
28 #include <linux/can/dev.h>
29 #include <linux/can/error.h>
30 #include <linux/can/netlink.h>
31 
32 #include "kvaser_usb.h"
33 
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;
37 static const struct kvaser_usb_dev_cfg kvaser_usb_hydra_dev_cfg_rt;
38 
39 #define KVASER_USB_HYDRA_BULK_EP_IN_ADDR	0x82
40 #define KVASER_USB_HYDRA_BULK_EP_OUT_ADDR	0x02
41 
42 #define KVASER_USB_HYDRA_MAX_TRANSID		0xff
43 #define KVASER_USB_HYDRA_MIN_TRANSID		0x01
44 
45 /* Minihydra command IDs */
46 #define CMD_SET_BUSPARAMS_REQ			16
47 #define CMD_GET_CHIP_STATE_REQ			19
48 #define CMD_CHIP_STATE_EVENT			20
49 #define CMD_SET_DRIVERMODE_REQ			21
50 #define CMD_START_CHIP_REQ			26
51 #define CMD_START_CHIP_RESP			27
52 #define CMD_STOP_CHIP_REQ			28
53 #define CMD_STOP_CHIP_RESP			29
54 #define CMD_TX_CAN_MESSAGE			33
55 #define CMD_GET_CARD_INFO_REQ			34
56 #define CMD_GET_CARD_INFO_RESP			35
57 #define CMD_GET_SOFTWARE_INFO_REQ		38
58 #define CMD_GET_SOFTWARE_INFO_RESP		39
59 #define CMD_ERROR_EVENT				45
60 #define CMD_FLUSH_QUEUE				48
61 #define CMD_TX_ACKNOWLEDGE			50
62 #define CMD_FLUSH_QUEUE_RESP			66
63 #define CMD_SET_BUSPARAMS_FD_REQ		69
64 #define CMD_SET_BUSPARAMS_FD_RESP		70
65 #define CMD_SET_BUSPARAMS_RESP			85
66 #define CMD_GET_CAPABILITIES_REQ		95
67 #define CMD_GET_CAPABILITIES_RESP		96
68 #define CMD_RX_MESSAGE				106
69 #define CMD_MAP_CHANNEL_REQ			200
70 #define CMD_MAP_CHANNEL_RESP			201
71 #define CMD_GET_SOFTWARE_DETAILS_REQ		202
72 #define CMD_GET_SOFTWARE_DETAILS_RESP		203
73 #define CMD_EXTENDED				255
74 
75 /* Minihydra extended command IDs */
76 #define CMD_TX_CAN_MESSAGE_FD			224
77 #define CMD_TX_ACKNOWLEDGE_FD			225
78 #define CMD_RX_MESSAGE_FD			226
79 
80 /* Hydra commands are handled by different threads in firmware.
81  * The threads are denoted hydra entity (HE). Each HE got a unique 6-bit
82  * address. The address is used in hydra commands to get/set source and
83  * destination HE. There are two predefined HE addresses, the remaining
84  * addresses are different between devices and firmware versions. Hence, we need
85  * to enumerate the addresses (see kvaser_usb_hydra_map_channel()).
86  */
87 
88 /* Well-known HE addresses */
89 #define KVASER_USB_HYDRA_HE_ADDRESS_ROUTER	0x00
90 #define KVASER_USB_HYDRA_HE_ADDRESS_ILLEGAL	0x3e
91 
92 #define KVASER_USB_HYDRA_TRANSID_CANHE		0x40
93 #define KVASER_USB_HYDRA_TRANSID_SYSDBG		0x61
94 
95 struct kvaser_cmd_map_ch_req {
96 	char name[16];
97 	u8 channel;
98 	u8 reserved[11];
99 } __packed;
100 
101 struct kvaser_cmd_map_ch_res {
102 	u8 he_addr;
103 	u8 channel;
104 	u8 reserved[26];
105 } __packed;
106 
107 struct kvaser_cmd_card_info {
108 	__le32 serial_number;
109 	__le32 clock_res;
110 	__le32 mfg_date;
111 	__le32 ean[2];
112 	u8 hw_version;
113 	u8 usb_mode;
114 	u8 hw_type;
115 	u8 reserved0;
116 	u8 nchannels;
117 	u8 reserved1[3];
118 } __packed;
119 
120 struct kvaser_cmd_sw_info {
121 	u8 reserved0[8];
122 	__le16 max_outstanding_tx;
123 	u8 reserved1[18];
124 } __packed;
125 
126 struct kvaser_cmd_sw_detail_req {
127 	u8 use_ext_cmd;
128 	u8 reserved[27];
129 } __packed;
130 
131 /* Software detail flags */
132 #define KVASER_USB_HYDRA_SW_FLAG_FW_BETA	BIT(2)
133 #define KVASER_USB_HYDRA_SW_FLAG_FW_BAD		BIT(4)
134 #define KVASER_USB_HYDRA_SW_FLAG_FREQ_80M	BIT(5)
135 #define KVASER_USB_HYDRA_SW_FLAG_EXT_CMD	BIT(9)
136 #define KVASER_USB_HYDRA_SW_FLAG_CANFD		BIT(10)
137 #define KVASER_USB_HYDRA_SW_FLAG_NONISO		BIT(11)
138 #define KVASER_USB_HYDRA_SW_FLAG_EXT_CAP	BIT(12)
139 #define KVASER_USB_HYDRA_SW_FLAG_CAN_FREQ_80M	BIT(13)
140 struct kvaser_cmd_sw_detail_res {
141 	__le32 sw_flags;
142 	__le32 sw_version;
143 	__le32 sw_name;
144 	__le32 ean[2];
145 	__le32 max_bitrate;
146 	u8 reserved[4];
147 } __packed;
148 
149 /* Sub commands for cap_req and cap_res */
150 #define KVASER_USB_HYDRA_CAP_CMD_LISTEN_MODE	0x02
151 #define KVASER_USB_HYDRA_CAP_CMD_ERR_REPORT	0x05
152 #define KVASER_USB_HYDRA_CAP_CMD_ONE_SHOT	0x06
153 struct kvaser_cmd_cap_req {
154 	__le16 cap_cmd;
155 	u8 reserved[26];
156 } __packed;
157 
158 /* Status codes for cap_res */
159 #define KVASER_USB_HYDRA_CAP_STAT_OK		0x00
160 #define KVASER_USB_HYDRA_CAP_STAT_NOT_IMPL	0x01
161 #define KVASER_USB_HYDRA_CAP_STAT_UNAVAIL	0x02
162 struct kvaser_cmd_cap_res {
163 	__le16 cap_cmd;
164 	__le16 status;
165 	__le32 mask;
166 	__le32 value;
167 	u8 reserved[16];
168 } __packed;
169 
170 /* CMD_ERROR_EVENT error codes */
171 #define KVASER_USB_HYDRA_ERROR_EVENT_CAN	0x01
172 #define KVASER_USB_HYDRA_ERROR_EVENT_PARAM	0x09
173 struct kvaser_cmd_error_event {
174 	__le16 timestamp[3];
175 	u8 reserved;
176 	u8 error_code;
177 	__le16 info1;
178 	__le16 info2;
179 } __packed;
180 
181 /* Chip state status flags. Used for chip_state_event and err_frame_data. */
182 #define KVASER_USB_HYDRA_BUS_ERR_ACT		0x00
183 #define KVASER_USB_HYDRA_BUS_ERR_PASS		BIT(5)
184 #define KVASER_USB_HYDRA_BUS_BUS_OFF		BIT(6)
185 struct kvaser_cmd_chip_state_event {
186 	__le16 timestamp[3];
187 	u8 tx_err_counter;
188 	u8 rx_err_counter;
189 	u8 bus_status;
190 	u8 reserved[19];
191 } __packed;
192 
193 /* Busparam modes */
194 #define KVASER_USB_HYDRA_BUS_MODE_CAN		0x00
195 #define KVASER_USB_HYDRA_BUS_MODE_CANFD_ISO	0x01
196 #define KVASER_USB_HYDRA_BUS_MODE_NONISO	0x02
197 struct kvaser_cmd_set_busparams {
198 	__le32 bitrate;
199 	u8 tseg1;
200 	u8 tseg2;
201 	u8 sjw;
202 	u8 nsamples;
203 	u8 reserved0[4];
204 	__le32 bitrate_d;
205 	u8 tseg1_d;
206 	u8 tseg2_d;
207 	u8 sjw_d;
208 	u8 nsamples_d;
209 	u8 canfd_mode;
210 	u8 reserved1[7];
211 } __packed;
212 
213 /* Ctrl modes */
214 #define KVASER_USB_HYDRA_CTRLMODE_NORMAL	0x01
215 #define KVASER_USB_HYDRA_CTRLMODE_LISTEN	0x02
216 struct kvaser_cmd_set_ctrlmode {
217 	u8 mode;
218 	u8 reserved[27];
219 } __packed;
220 
221 struct kvaser_err_frame_data {
222 	u8 bus_status;
223 	u8 reserved0;
224 	u8 tx_err_counter;
225 	u8 rx_err_counter;
226 	u8 reserved1[4];
227 } __packed;
228 
229 struct kvaser_cmd_rx_can {
230 	u8 cmd_len;
231 	u8 cmd_no;
232 	u8 channel;
233 	u8 flags;
234 	__le16 timestamp[3];
235 	u8 dlc;
236 	u8 padding;
237 	__le32 id;
238 	union {
239 		u8 data[8];
240 		struct kvaser_err_frame_data err_frame_data;
241 	};
242 } __packed;
243 
244 /* Extended CAN ID flag. Used in rx_can and tx_can */
245 #define KVASER_USB_HYDRA_EXTENDED_FRAME_ID	BIT(31)
246 struct kvaser_cmd_tx_can {
247 	__le32 id;
248 	u8 data[8];
249 	u8 dlc;
250 	u8 flags;
251 	__le16 transid;
252 	u8 channel;
253 	u8 reserved[11];
254 } __packed;
255 
256 struct kvaser_cmd_header {
257 	u8 cmd_no;
258 	/* The destination HE address is stored in 0..5 of he_addr.
259 	 * The upper part of source HE address is stored in 6..7 of he_addr, and
260 	 * the lower part is stored in 12..15 of transid.
261 	 */
262 	u8 he_addr;
263 	__le16 transid;
264 } __packed;
265 
266 struct kvaser_cmd {
267 	struct kvaser_cmd_header header;
268 	union {
269 		struct kvaser_cmd_map_ch_req map_ch_req;
270 		struct kvaser_cmd_map_ch_res map_ch_res;
271 
272 		struct kvaser_cmd_card_info card_info;
273 		struct kvaser_cmd_sw_info sw_info;
274 		struct kvaser_cmd_sw_detail_req sw_detail_req;
275 		struct kvaser_cmd_sw_detail_res sw_detail_res;
276 
277 		struct kvaser_cmd_cap_req cap_req;
278 		struct kvaser_cmd_cap_res cap_res;
279 
280 		struct kvaser_cmd_error_event error_event;
281 
282 		struct kvaser_cmd_set_busparams set_busparams_req;
283 
284 		struct kvaser_cmd_chip_state_event chip_state_event;
285 
286 		struct kvaser_cmd_set_ctrlmode set_ctrlmode;
287 
288 		struct kvaser_cmd_rx_can rx_can;
289 		struct kvaser_cmd_tx_can tx_can;
290 	} __packed;
291 } __packed;
292 
293 /* CAN frame flags. Used in rx_can, ext_rx_can, tx_can and ext_tx_can */
294 #define KVASER_USB_HYDRA_CF_FLAG_ERROR_FRAME	BIT(0)
295 #define KVASER_USB_HYDRA_CF_FLAG_OVERRUN	BIT(1)
296 #define KVASER_USB_HYDRA_CF_FLAG_REMOTE_FRAME	BIT(4)
297 #define KVASER_USB_HYDRA_CF_FLAG_EXTENDED_ID	BIT(5)
298 /* CAN frame flags. Used in ext_rx_can and ext_tx_can */
299 #define KVASER_USB_HYDRA_CF_FLAG_OSM_NACK	BIT(12)
300 #define KVASER_USB_HYDRA_CF_FLAG_ABL		BIT(13)
301 #define KVASER_USB_HYDRA_CF_FLAG_FDF		BIT(16)
302 #define KVASER_USB_HYDRA_CF_FLAG_BRS		BIT(17)
303 #define KVASER_USB_HYDRA_CF_FLAG_ESI		BIT(18)
304 
305 /* KCAN packet header macros. Used in ext_rx_can and ext_tx_can */
306 #define KVASER_USB_KCAN_DATA_DLC_BITS		4
307 #define KVASER_USB_KCAN_DATA_DLC_SHIFT		8
308 #define KVASER_USB_KCAN_DATA_DLC_MASK \
309 				GENMASK(KVASER_USB_KCAN_DATA_DLC_BITS - 1 + \
310 				KVASER_USB_KCAN_DATA_DLC_SHIFT, \
311 				KVASER_USB_KCAN_DATA_DLC_SHIFT)
312 
313 #define KVASER_USB_KCAN_DATA_BRS		BIT(14)
314 #define KVASER_USB_KCAN_DATA_FDF		BIT(15)
315 #define KVASER_USB_KCAN_DATA_OSM		BIT(16)
316 #define KVASER_USB_KCAN_DATA_AREQ		BIT(31)
317 #define KVASER_USB_KCAN_DATA_SRR		BIT(31)
318 #define KVASER_USB_KCAN_DATA_RTR		BIT(29)
319 #define KVASER_USB_KCAN_DATA_IDE		BIT(30)
320 struct kvaser_cmd_ext_rx_can {
321 	__le32 flags;
322 	__le32 id;
323 	__le32 kcan_id;
324 	__le32 kcan_header;
325 	__le64 timestamp;
326 	union {
327 		u8 kcan_payload[64];
328 		struct kvaser_err_frame_data err_frame_data;
329 	};
330 } __packed;
331 
332 struct kvaser_cmd_ext_tx_can {
333 	__le32 flags;
334 	__le32 id;
335 	__le32 kcan_id;
336 	__le32 kcan_header;
337 	u8 databytes;
338 	u8 dlc;
339 	u8 reserved[6];
340 	u8 kcan_payload[64];
341 } __packed;
342 
343 struct kvaser_cmd_ext_tx_ack {
344 	__le32 flags;
345 	u8 reserved0[4];
346 	__le64 timestamp;
347 	u8 reserved1[8];
348 } __packed;
349 
350 /* struct for extended commands (CMD_EXTENDED) */
351 struct kvaser_cmd_ext {
352 	struct kvaser_cmd_header header;
353 	__le16 len;
354 	u8 cmd_no_ext;
355 	u8 reserved;
356 
357 	union {
358 		struct kvaser_cmd_ext_rx_can rx_can;
359 		struct kvaser_cmd_ext_tx_can tx_can;
360 		struct kvaser_cmd_ext_tx_ack tx_ack;
361 	} __packed;
362 } __packed;
363 
364 static const struct can_bittiming_const kvaser_usb_hydra_kcan_bittiming_c = {
365 	.name = "kvaser_usb_kcan",
366 	.tseg1_min = 1,
367 	.tseg1_max = 255,
368 	.tseg2_min = 1,
369 	.tseg2_max = 32,
370 	.sjw_max = 16,
371 	.brp_min = 1,
372 	.brp_max = 8192,
373 	.brp_inc = 1,
374 };
375 
376 static const struct can_bittiming_const kvaser_usb_hydra_flexc_bittiming_c = {
377 	.name = "kvaser_usb_flex",
378 	.tseg1_min = 4,
379 	.tseg1_max = 16,
380 	.tseg2_min = 2,
381 	.tseg2_max = 8,
382 	.sjw_max = 4,
383 	.brp_min = 1,
384 	.brp_max = 256,
385 	.brp_inc = 1,
386 };
387 
388 static const struct can_bittiming_const kvaser_usb_hydra_rt_bittiming_c = {
389 	.name = "kvaser_usb_rt",
390 	.tseg1_min = 2,
391 	.tseg1_max = 96,
392 	.tseg2_min = 2,
393 	.tseg2_max = 32,
394 	.sjw_max = 32,
395 	.brp_min = 1,
396 	.brp_max = 1024,
397 	.brp_inc = 1,
398 };
399 
400 static const struct can_bittiming_const kvaser_usb_hydra_rtd_bittiming_c = {
401 	.name = "kvaser_usb_rt",
402 	.tseg1_min = 2,
403 	.tseg1_max = 39,
404 	.tseg2_min = 2,
405 	.tseg2_max = 8,
406 	.sjw_max = 8,
407 	.brp_min = 1,
408 	.brp_max = 1024,
409 	.brp_inc = 1,
410 };
411 
412 #define KVASER_USB_HYDRA_TRANSID_BITS		12
413 #define KVASER_USB_HYDRA_TRANSID_MASK \
414 				GENMASK(KVASER_USB_HYDRA_TRANSID_BITS - 1, 0)
415 #define KVASER_USB_HYDRA_HE_ADDR_SRC_MASK	GENMASK(7, 6)
416 #define KVASER_USB_HYDRA_HE_ADDR_DEST_MASK	GENMASK(5, 0)
417 #define KVASER_USB_HYDRA_HE_ADDR_SRC_BITS	2
418 static inline u16 kvaser_usb_hydra_get_cmd_transid(const struct kvaser_cmd *cmd)
419 {
420 	return le16_to_cpu(cmd->header.transid) & KVASER_USB_HYDRA_TRANSID_MASK;
421 }
422 
423 static inline void kvaser_usb_hydra_set_cmd_transid(struct kvaser_cmd *cmd,
424 						    u16 transid)
425 {
426 	cmd->header.transid =
427 			cpu_to_le16(transid & KVASER_USB_HYDRA_TRANSID_MASK);
428 }
429 
430 static inline u8 kvaser_usb_hydra_get_cmd_src_he(const struct kvaser_cmd *cmd)
431 {
432 	return (cmd->header.he_addr & KVASER_USB_HYDRA_HE_ADDR_SRC_MASK) >>
433 		KVASER_USB_HYDRA_HE_ADDR_SRC_BITS |
434 		le16_to_cpu(cmd->header.transid) >>
435 		KVASER_USB_HYDRA_TRANSID_BITS;
436 }
437 
438 static inline void kvaser_usb_hydra_set_cmd_dest_he(struct kvaser_cmd *cmd,
439 						    u8 dest_he)
440 {
441 	cmd->header.he_addr =
442 		(cmd->header.he_addr & KVASER_USB_HYDRA_HE_ADDR_SRC_MASK) |
443 		(dest_he & KVASER_USB_HYDRA_HE_ADDR_DEST_MASK);
444 }
445 
446 static u8 kvaser_usb_hydra_channel_from_cmd(const struct kvaser_usb *dev,
447 					    const struct kvaser_cmd *cmd)
448 {
449 	int i;
450 	u8 channel = 0xff;
451 	u8 src_he = kvaser_usb_hydra_get_cmd_src_he(cmd);
452 
453 	for (i = 0; i < KVASER_USB_MAX_NET_DEVICES; i++) {
454 		if (dev->card_data.hydra.channel_to_he[i] == src_he) {
455 			channel = i;
456 			break;
457 		}
458 	}
459 
460 	return channel;
461 }
462 
463 static u16 kvaser_usb_hydra_get_next_transid(struct kvaser_usb *dev)
464 {
465 	unsigned long flags;
466 	u16 transid;
467 	struct kvaser_usb_dev_card_data_hydra *card_data =
468 							&dev->card_data.hydra;
469 
470 	spin_lock_irqsave(&card_data->transid_lock, flags);
471 	transid = card_data->transid;
472 	if (transid >= KVASER_USB_HYDRA_MAX_TRANSID)
473 		transid = KVASER_USB_HYDRA_MIN_TRANSID;
474 	else
475 		transid++;
476 	card_data->transid = transid;
477 	spin_unlock_irqrestore(&card_data->transid_lock, flags);
478 
479 	return transid;
480 }
481 
482 static size_t kvaser_usb_hydra_cmd_size(struct kvaser_cmd *cmd)
483 {
484 	size_t ret;
485 
486 	if (cmd->header.cmd_no == CMD_EXTENDED)
487 		ret = le16_to_cpu(((struct kvaser_cmd_ext *)cmd)->len);
488 	else
489 		ret = sizeof(struct kvaser_cmd);
490 
491 	return ret;
492 }
493 
494 static struct kvaser_usb_net_priv *
495 kvaser_usb_hydra_net_priv_from_cmd(const struct kvaser_usb *dev,
496 				   const struct kvaser_cmd *cmd)
497 {
498 	struct kvaser_usb_net_priv *priv = NULL;
499 	u8 channel = kvaser_usb_hydra_channel_from_cmd(dev, cmd);
500 
501 	if (channel >= dev->nchannels)
502 		dev_err(&dev->intf->dev,
503 			"Invalid channel number (%d)\n", channel);
504 	else
505 		priv = dev->nets[channel];
506 
507 	return priv;
508 }
509 
510 static ktime_t
511 kvaser_usb_hydra_ktime_from_rx_cmd(const struct kvaser_usb_dev_cfg *cfg,
512 				   const struct kvaser_cmd *cmd)
513 {
514 	u64 ticks;
515 
516 	if (cmd->header.cmd_no == CMD_EXTENDED) {
517 		struct kvaser_cmd_ext *cmd_ext = (struct kvaser_cmd_ext *)cmd;
518 
519 		ticks = le64_to_cpu(cmd_ext->rx_can.timestamp);
520 	} else {
521 		ticks = le16_to_cpu(cmd->rx_can.timestamp[0]);
522 		ticks += (u64)(le16_to_cpu(cmd->rx_can.timestamp[1])) << 16;
523 		ticks += (u64)(le16_to_cpu(cmd->rx_can.timestamp[2])) << 32;
524 	}
525 
526 	return ns_to_ktime(div_u64(ticks * 1000, cfg->timestamp_freq));
527 }
528 
529 static int kvaser_usb_hydra_send_simple_cmd(struct kvaser_usb *dev,
530 					    u8 cmd_no, int channel)
531 {
532 	struct kvaser_cmd *cmd;
533 	int err;
534 
535 	cmd = kcalloc(1, sizeof(struct kvaser_cmd), GFP_KERNEL);
536 	if (!cmd)
537 		return -ENOMEM;
538 
539 	cmd->header.cmd_no = cmd_no;
540 	if (channel < 0) {
541 		kvaser_usb_hydra_set_cmd_dest_he
542 				(cmd, KVASER_USB_HYDRA_HE_ADDRESS_ILLEGAL);
543 	} else {
544 		if (channel >= KVASER_USB_MAX_NET_DEVICES) {
545 			dev_err(&dev->intf->dev, "channel (%d) out of range.\n",
546 				channel);
547 			err = -EINVAL;
548 			goto end;
549 		}
550 		kvaser_usb_hydra_set_cmd_dest_he
551 			(cmd, dev->card_data.hydra.channel_to_he[channel]);
552 	}
553 	kvaser_usb_hydra_set_cmd_transid
554 				(cmd, kvaser_usb_hydra_get_next_transid(dev));
555 
556 	err = kvaser_usb_send_cmd(dev, cmd, kvaser_usb_hydra_cmd_size(cmd));
557 	if (err)
558 		goto end;
559 
560 end:
561 	kfree(cmd);
562 
563 	return err;
564 }
565 
566 static int
567 kvaser_usb_hydra_send_simple_cmd_async(struct kvaser_usb_net_priv *priv,
568 				       u8 cmd_no)
569 {
570 	struct kvaser_cmd *cmd;
571 	struct kvaser_usb *dev = priv->dev;
572 	int err;
573 
574 	cmd = kcalloc(1, sizeof(struct kvaser_cmd), GFP_ATOMIC);
575 	if (!cmd)
576 		return -ENOMEM;
577 
578 	cmd->header.cmd_no = cmd_no;
579 
580 	kvaser_usb_hydra_set_cmd_dest_he
581 		(cmd, dev->card_data.hydra.channel_to_he[priv->channel]);
582 	kvaser_usb_hydra_set_cmd_transid
583 				(cmd, kvaser_usb_hydra_get_next_transid(dev));
584 
585 	err = kvaser_usb_send_cmd_async(priv, cmd,
586 					kvaser_usb_hydra_cmd_size(cmd));
587 	if (err)
588 		kfree(cmd);
589 
590 	return err;
591 }
592 
593 /* This function is used for synchronously waiting on hydra control commands.
594  * Note: Compared to kvaser_usb_hydra_read_bulk_callback(), we never need to
595  *       handle partial hydra commands. Since hydra control commands are always
596  *       non-extended commands.
597  */
598 static int kvaser_usb_hydra_wait_cmd(const struct kvaser_usb *dev, u8 cmd_no,
599 				     struct kvaser_cmd *cmd)
600 {
601 	void *buf;
602 	int err;
603 	unsigned long timeout = jiffies + msecs_to_jiffies(KVASER_USB_TIMEOUT);
604 
605 	if (cmd->header.cmd_no == CMD_EXTENDED) {
606 		dev_err(&dev->intf->dev, "Wait for CMD_EXTENDED not allowed\n");
607 		return -EINVAL;
608 	}
609 
610 	buf = kzalloc(KVASER_USB_RX_BUFFER_SIZE, GFP_KERNEL);
611 	if (!buf)
612 		return -ENOMEM;
613 
614 	do {
615 		int actual_len = 0;
616 		int pos = 0;
617 
618 		err = kvaser_usb_recv_cmd(dev, buf, KVASER_USB_RX_BUFFER_SIZE,
619 					  &actual_len);
620 		if (err < 0)
621 			goto end;
622 
623 		while (pos < actual_len) {
624 			struct kvaser_cmd *tmp_cmd;
625 			size_t cmd_len;
626 
627 			tmp_cmd = buf + pos;
628 			cmd_len = kvaser_usb_hydra_cmd_size(tmp_cmd);
629 			if (pos + cmd_len > actual_len) {
630 				dev_err_ratelimited(&dev->intf->dev,
631 						    "Format error\n");
632 				break;
633 			}
634 
635 			if (tmp_cmd->header.cmd_no == cmd_no) {
636 				memcpy(cmd, tmp_cmd, cmd_len);
637 				goto end;
638 			}
639 			pos += cmd_len;
640 		}
641 	} while (time_before(jiffies, timeout));
642 
643 	err = -EINVAL;
644 
645 end:
646 	kfree(buf);
647 
648 	return err;
649 }
650 
651 static int kvaser_usb_hydra_map_channel_resp(struct kvaser_usb *dev,
652 					     const struct kvaser_cmd *cmd)
653 {
654 	u8 he, channel;
655 	u16 transid = kvaser_usb_hydra_get_cmd_transid(cmd);
656 	struct kvaser_usb_dev_card_data_hydra *card_data =
657 							&dev->card_data.hydra;
658 
659 	if (transid > 0x007f || transid < 0x0040) {
660 		dev_err(&dev->intf->dev,
661 			"CMD_MAP_CHANNEL_RESP, invalid transid: 0x%x\n",
662 			transid);
663 		return -EINVAL;
664 	}
665 
666 	switch (transid) {
667 	case KVASER_USB_HYDRA_TRANSID_CANHE:
668 	case KVASER_USB_HYDRA_TRANSID_CANHE + 1:
669 	case KVASER_USB_HYDRA_TRANSID_CANHE + 2:
670 	case KVASER_USB_HYDRA_TRANSID_CANHE + 3:
671 	case KVASER_USB_HYDRA_TRANSID_CANHE + 4:
672 		channel = transid & 0x000f;
673 		he = cmd->map_ch_res.he_addr;
674 		card_data->channel_to_he[channel] = he;
675 		break;
676 	case KVASER_USB_HYDRA_TRANSID_SYSDBG:
677 		card_data->sysdbg_he = cmd->map_ch_res.he_addr;
678 		break;
679 	default:
680 		dev_warn(&dev->intf->dev,
681 			 "Unknown CMD_MAP_CHANNEL_RESP transid=0x%x\n",
682 			 transid);
683 		break;
684 	}
685 
686 	return 0;
687 }
688 
689 static int kvaser_usb_hydra_map_channel(struct kvaser_usb *dev, u16 transid,
690 					u8 channel, const char *name)
691 {
692 	struct kvaser_cmd *cmd;
693 	int err;
694 
695 	cmd = kcalloc(1, sizeof(struct kvaser_cmd), GFP_KERNEL);
696 	if (!cmd)
697 		return -ENOMEM;
698 
699 	strcpy(cmd->map_ch_req.name, name);
700 	cmd->header.cmd_no = CMD_MAP_CHANNEL_REQ;
701 	kvaser_usb_hydra_set_cmd_dest_he
702 				(cmd, KVASER_USB_HYDRA_HE_ADDRESS_ROUTER);
703 	cmd->map_ch_req.channel = channel;
704 
705 	kvaser_usb_hydra_set_cmd_transid(cmd, transid);
706 
707 	err = kvaser_usb_send_cmd(dev, cmd, kvaser_usb_hydra_cmd_size(cmd));
708 	if (err)
709 		goto end;
710 
711 	err = kvaser_usb_hydra_wait_cmd(dev, CMD_MAP_CHANNEL_RESP, cmd);
712 	if (err)
713 		goto end;
714 
715 	err = kvaser_usb_hydra_map_channel_resp(dev, cmd);
716 	if (err)
717 		goto end;
718 
719 end:
720 	kfree(cmd);
721 
722 	return err;
723 }
724 
725 static int kvaser_usb_hydra_get_single_capability(struct kvaser_usb *dev,
726 						  u16 cap_cmd_req, u16 *status)
727 {
728 	struct kvaser_usb_dev_card_data *card_data = &dev->card_data;
729 	struct kvaser_cmd *cmd;
730 	u32 value = 0;
731 	u32 mask = 0;
732 	u16 cap_cmd_res;
733 	int err;
734 	int i;
735 
736 	cmd = kcalloc(1, sizeof(struct kvaser_cmd), GFP_KERNEL);
737 	if (!cmd)
738 		return -ENOMEM;
739 
740 	cmd->header.cmd_no = CMD_GET_CAPABILITIES_REQ;
741 	cmd->cap_req.cap_cmd = cpu_to_le16(cap_cmd_req);
742 
743 	kvaser_usb_hydra_set_cmd_dest_he(cmd, card_data->hydra.sysdbg_he);
744 	kvaser_usb_hydra_set_cmd_transid
745 				(cmd, kvaser_usb_hydra_get_next_transid(dev));
746 
747 	err = kvaser_usb_send_cmd(dev, cmd, kvaser_usb_hydra_cmd_size(cmd));
748 	if (err)
749 		goto end;
750 
751 	err = kvaser_usb_hydra_wait_cmd(dev, CMD_GET_CAPABILITIES_RESP, cmd);
752 	if (err)
753 		goto end;
754 
755 	*status = le16_to_cpu(cmd->cap_res.status);
756 
757 	if (*status != KVASER_USB_HYDRA_CAP_STAT_OK)
758 		goto end;
759 
760 	cap_cmd_res = le16_to_cpu(cmd->cap_res.cap_cmd);
761 	switch (cap_cmd_res) {
762 	case KVASER_USB_HYDRA_CAP_CMD_LISTEN_MODE:
763 	case KVASER_USB_HYDRA_CAP_CMD_ERR_REPORT:
764 	case KVASER_USB_HYDRA_CAP_CMD_ONE_SHOT:
765 		value = le32_to_cpu(cmd->cap_res.value);
766 		mask = le32_to_cpu(cmd->cap_res.mask);
767 		break;
768 	default:
769 		dev_warn(&dev->intf->dev, "Unknown capability command %u\n",
770 			 cap_cmd_res);
771 		break;
772 	}
773 
774 	for (i = 0; i < dev->nchannels; i++) {
775 		if (BIT(i) & (value & mask)) {
776 			switch (cap_cmd_res) {
777 			case KVASER_USB_HYDRA_CAP_CMD_LISTEN_MODE:
778 				card_data->ctrlmode_supported |=
779 						CAN_CTRLMODE_LISTENONLY;
780 				break;
781 			case KVASER_USB_HYDRA_CAP_CMD_ERR_REPORT:
782 				card_data->capabilities |=
783 						KVASER_USB_CAP_BERR_CAP;
784 				break;
785 			case KVASER_USB_HYDRA_CAP_CMD_ONE_SHOT:
786 				card_data->ctrlmode_supported |=
787 						CAN_CTRLMODE_ONE_SHOT;
788 				break;
789 			}
790 		}
791 	}
792 
793 end:
794 	kfree(cmd);
795 
796 	return err;
797 }
798 
799 static void kvaser_usb_hydra_start_chip_reply(const struct kvaser_usb *dev,
800 					      const struct kvaser_cmd *cmd)
801 {
802 	struct kvaser_usb_net_priv *priv;
803 
804 	priv = kvaser_usb_hydra_net_priv_from_cmd(dev, cmd);
805 	if (!priv)
806 		return;
807 
808 	if (completion_done(&priv->start_comp) &&
809 	    netif_queue_stopped(priv->netdev)) {
810 		netif_wake_queue(priv->netdev);
811 	} else {
812 		netif_start_queue(priv->netdev);
813 		complete(&priv->start_comp);
814 	}
815 }
816 
817 static void kvaser_usb_hydra_stop_chip_reply(const struct kvaser_usb *dev,
818 					     const struct kvaser_cmd *cmd)
819 {
820 	struct kvaser_usb_net_priv *priv;
821 
822 	priv = kvaser_usb_hydra_net_priv_from_cmd(dev, cmd);
823 	if (!priv)
824 		return;
825 
826 	complete(&priv->stop_comp);
827 }
828 
829 static void kvaser_usb_hydra_flush_queue_reply(const struct kvaser_usb *dev,
830 					       const struct kvaser_cmd *cmd)
831 {
832 	struct kvaser_usb_net_priv *priv;
833 
834 	priv = kvaser_usb_hydra_net_priv_from_cmd(dev, cmd);
835 	if (!priv)
836 		return;
837 
838 	complete(&priv->flush_comp);
839 }
840 
841 static void
842 kvaser_usb_hydra_bus_status_to_can_state(const struct kvaser_usb_net_priv *priv,
843 					 u8 bus_status,
844 					 const struct can_berr_counter *bec,
845 					 enum can_state *new_state)
846 {
847 	if (bus_status & KVASER_USB_HYDRA_BUS_BUS_OFF) {
848 		*new_state = CAN_STATE_BUS_OFF;
849 	} else if (bus_status & KVASER_USB_HYDRA_BUS_ERR_PASS) {
850 		*new_state = CAN_STATE_ERROR_PASSIVE;
851 	} else if (bus_status == KVASER_USB_HYDRA_BUS_ERR_ACT) {
852 		if (bec->txerr >= 128 || bec->rxerr >= 128) {
853 			netdev_warn(priv->netdev,
854 				    "ERR_ACTIVE but err tx=%u or rx=%u >=128\n",
855 				    bec->txerr, bec->rxerr);
856 			*new_state = CAN_STATE_ERROR_PASSIVE;
857 		} else if (bec->txerr >= 96 || bec->rxerr >= 96) {
858 			*new_state = CAN_STATE_ERROR_WARNING;
859 		} else {
860 			*new_state = CAN_STATE_ERROR_ACTIVE;
861 		}
862 	}
863 }
864 
865 static void kvaser_usb_hydra_update_state(struct kvaser_usb_net_priv *priv,
866 					  u8 bus_status,
867 					  const struct can_berr_counter *bec)
868 {
869 	struct net_device *netdev = priv->netdev;
870 	struct can_frame *cf;
871 	struct sk_buff *skb;
872 	struct net_device_stats *stats;
873 	enum can_state new_state, old_state;
874 
875 	old_state = priv->can.state;
876 
877 	kvaser_usb_hydra_bus_status_to_can_state(priv, bus_status, bec,
878 						 &new_state);
879 
880 	if (new_state == old_state)
881 		return;
882 
883 	/* Ignore state change if previous state was STOPPED and the new state
884 	 * is BUS_OFF. Firmware always report this as BUS_OFF, since firmware
885 	 * does not distinguish between BUS_OFF and STOPPED.
886 	 */
887 	if (old_state == CAN_STATE_STOPPED && new_state == CAN_STATE_BUS_OFF)
888 		return;
889 
890 	skb = alloc_can_err_skb(netdev, &cf);
891 	if (skb) {
892 		enum can_state tx_state, rx_state;
893 
894 		tx_state = (bec->txerr >= bec->rxerr) ?
895 					new_state : CAN_STATE_ERROR_ACTIVE;
896 		rx_state = (bec->txerr <= bec->rxerr) ?
897 					new_state : CAN_STATE_ERROR_ACTIVE;
898 		can_change_state(netdev, cf, tx_state, rx_state);
899 	}
900 
901 	if (new_state == CAN_STATE_BUS_OFF && old_state < CAN_STATE_BUS_OFF) {
902 		if (!priv->can.restart_ms)
903 			kvaser_usb_hydra_send_simple_cmd_async
904 						(priv, CMD_STOP_CHIP_REQ);
905 
906 		can_bus_off(netdev);
907 	}
908 
909 	if (!skb) {
910 		netdev_warn(netdev, "No memory left for err_skb\n");
911 		return;
912 	}
913 
914 	if (priv->can.restart_ms &&
915 	    old_state >= CAN_STATE_BUS_OFF &&
916 	    new_state < CAN_STATE_BUS_OFF)
917 		priv->can.can_stats.restarts++;
918 
919 	cf->data[6] = bec->txerr;
920 	cf->data[7] = bec->rxerr;
921 
922 	stats = &netdev->stats;
923 	stats->rx_packets++;
924 	stats->rx_bytes += cf->len;
925 	netif_rx(skb);
926 }
927 
928 static void kvaser_usb_hydra_state_event(const struct kvaser_usb *dev,
929 					 const struct kvaser_cmd *cmd)
930 {
931 	struct kvaser_usb_net_priv *priv;
932 	struct can_berr_counter bec;
933 	u8 bus_status;
934 
935 	priv = kvaser_usb_hydra_net_priv_from_cmd(dev, cmd);
936 	if (!priv)
937 		return;
938 
939 	bus_status = cmd->chip_state_event.bus_status;
940 	bec.txerr = cmd->chip_state_event.tx_err_counter;
941 	bec.rxerr = cmd->chip_state_event.rx_err_counter;
942 
943 	kvaser_usb_hydra_update_state(priv, bus_status, &bec);
944 	priv->bec.txerr = bec.txerr;
945 	priv->bec.rxerr = bec.rxerr;
946 }
947 
948 static void kvaser_usb_hydra_error_event_parameter(const struct kvaser_usb *dev,
949 						   const struct kvaser_cmd *cmd)
950 {
951 	/* info1 will contain the offending cmd_no */
952 	switch (le16_to_cpu(cmd->error_event.info1)) {
953 	case CMD_START_CHIP_REQ:
954 		dev_warn(&dev->intf->dev,
955 			 "CMD_START_CHIP_REQ error in parameter\n");
956 		break;
957 
958 	case CMD_STOP_CHIP_REQ:
959 		dev_warn(&dev->intf->dev,
960 			 "CMD_STOP_CHIP_REQ error in parameter\n");
961 		break;
962 
963 	case CMD_FLUSH_QUEUE:
964 		dev_warn(&dev->intf->dev,
965 			 "CMD_FLUSH_QUEUE error in parameter\n");
966 		break;
967 
968 	case CMD_SET_BUSPARAMS_REQ:
969 		dev_warn(&dev->intf->dev,
970 			 "Set bittiming failed. Error in parameter\n");
971 		break;
972 
973 	case CMD_SET_BUSPARAMS_FD_REQ:
974 		dev_warn(&dev->intf->dev,
975 			 "Set data bittiming failed. Error in parameter\n");
976 		break;
977 
978 	default:
979 		dev_warn(&dev->intf->dev,
980 			 "Unhandled parameter error event cmd_no (%u)\n",
981 			 le16_to_cpu(cmd->error_event.info1));
982 		break;
983 	}
984 }
985 
986 static void kvaser_usb_hydra_error_event(const struct kvaser_usb *dev,
987 					 const struct kvaser_cmd *cmd)
988 {
989 	switch (cmd->error_event.error_code) {
990 	case KVASER_USB_HYDRA_ERROR_EVENT_PARAM:
991 		kvaser_usb_hydra_error_event_parameter(dev, cmd);
992 		break;
993 
994 	case KVASER_USB_HYDRA_ERROR_EVENT_CAN:
995 		/* Wrong channel mapping?! This should never happen!
996 		 * info1 will contain the offending cmd_no
997 		 */
998 		dev_err(&dev->intf->dev,
999 			"Received CAN error event for cmd_no (%u)\n",
1000 			le16_to_cpu(cmd->error_event.info1));
1001 		break;
1002 
1003 	default:
1004 		dev_warn(&dev->intf->dev,
1005 			 "Unhandled error event (%d)\n",
1006 			 cmd->error_event.error_code);
1007 		break;
1008 	}
1009 }
1010 
1011 static void
1012 kvaser_usb_hydra_error_frame(struct kvaser_usb_net_priv *priv,
1013 			     const struct kvaser_err_frame_data *err_frame_data,
1014 			     ktime_t hwtstamp)
1015 {
1016 	struct net_device *netdev = priv->netdev;
1017 	struct net_device_stats *stats = &netdev->stats;
1018 	struct can_frame *cf;
1019 	struct sk_buff *skb;
1020 	struct skb_shared_hwtstamps *shhwtstamps;
1021 	struct can_berr_counter bec;
1022 	enum can_state new_state, old_state;
1023 	u8 bus_status;
1024 
1025 	priv->can.can_stats.bus_error++;
1026 	stats->rx_errors++;
1027 
1028 	bus_status = err_frame_data->bus_status;
1029 	bec.txerr = err_frame_data->tx_err_counter;
1030 	bec.rxerr = err_frame_data->rx_err_counter;
1031 
1032 	old_state = priv->can.state;
1033 	kvaser_usb_hydra_bus_status_to_can_state(priv, bus_status, &bec,
1034 						 &new_state);
1035 
1036 	skb = alloc_can_err_skb(netdev, &cf);
1037 
1038 	if (new_state != old_state) {
1039 		if (skb) {
1040 			enum can_state tx_state, rx_state;
1041 
1042 			tx_state = (bec.txerr >= bec.rxerr) ?
1043 					new_state : CAN_STATE_ERROR_ACTIVE;
1044 			rx_state = (bec.txerr <= bec.rxerr) ?
1045 					new_state : CAN_STATE_ERROR_ACTIVE;
1046 
1047 			can_change_state(netdev, cf, tx_state, rx_state);
1048 
1049 			if (priv->can.restart_ms &&
1050 			    old_state >= CAN_STATE_BUS_OFF &&
1051 			    new_state < CAN_STATE_BUS_OFF)
1052 				cf->can_id |= CAN_ERR_RESTARTED;
1053 		}
1054 
1055 		if (new_state == CAN_STATE_BUS_OFF) {
1056 			if (!priv->can.restart_ms)
1057 				kvaser_usb_hydra_send_simple_cmd_async
1058 						(priv, CMD_STOP_CHIP_REQ);
1059 
1060 			can_bus_off(netdev);
1061 		}
1062 	}
1063 
1064 	if (!skb) {
1065 		stats->rx_dropped++;
1066 		netdev_warn(netdev, "No memory left for err_skb\n");
1067 		return;
1068 	}
1069 
1070 	shhwtstamps = skb_hwtstamps(skb);
1071 	shhwtstamps->hwtstamp = hwtstamp;
1072 
1073 	cf->can_id |= CAN_ERR_BUSERROR;
1074 	cf->data[6] = bec.txerr;
1075 	cf->data[7] = bec.rxerr;
1076 
1077 	stats->rx_packets++;
1078 	stats->rx_bytes += cf->len;
1079 	netif_rx(skb);
1080 
1081 	priv->bec.txerr = bec.txerr;
1082 	priv->bec.rxerr = bec.rxerr;
1083 }
1084 
1085 static void kvaser_usb_hydra_one_shot_fail(struct kvaser_usb_net_priv *priv,
1086 					   const struct kvaser_cmd_ext *cmd)
1087 {
1088 	struct net_device *netdev = priv->netdev;
1089 	struct net_device_stats *stats = &netdev->stats;
1090 	struct can_frame *cf;
1091 	struct sk_buff *skb;
1092 	u32 flags;
1093 
1094 	skb = alloc_can_err_skb(netdev, &cf);
1095 	if (!skb) {
1096 		stats->rx_dropped++;
1097 		netdev_warn(netdev, "No memory left for err_skb\n");
1098 		return;
1099 	}
1100 
1101 	cf->can_id |= CAN_ERR_BUSERROR;
1102 	flags = le32_to_cpu(cmd->tx_ack.flags);
1103 
1104 	if (flags & KVASER_USB_HYDRA_CF_FLAG_OSM_NACK)
1105 		cf->can_id |= CAN_ERR_ACK;
1106 	if (flags & KVASER_USB_HYDRA_CF_FLAG_ABL) {
1107 		cf->can_id |= CAN_ERR_LOSTARB;
1108 		priv->can.can_stats.arbitration_lost++;
1109 	}
1110 
1111 	stats->tx_errors++;
1112 	stats->rx_packets++;
1113 	stats->rx_bytes += cf->len;
1114 	netif_rx(skb);
1115 }
1116 
1117 static void kvaser_usb_hydra_tx_acknowledge(const struct kvaser_usb *dev,
1118 					    const struct kvaser_cmd *cmd)
1119 {
1120 	struct kvaser_usb_tx_urb_context *context;
1121 	struct kvaser_usb_net_priv *priv;
1122 	unsigned long irq_flags;
1123 	bool one_shot_fail = false;
1124 	u16 transid = kvaser_usb_hydra_get_cmd_transid(cmd);
1125 
1126 	priv = kvaser_usb_hydra_net_priv_from_cmd(dev, cmd);
1127 	if (!priv)
1128 		return;
1129 
1130 	if (!netif_device_present(priv->netdev))
1131 		return;
1132 
1133 	if (cmd->header.cmd_no == CMD_EXTENDED) {
1134 		struct kvaser_cmd_ext *cmd_ext = (struct kvaser_cmd_ext *)cmd;
1135 		u32 flags = le32_to_cpu(cmd_ext->tx_ack.flags);
1136 
1137 		if (flags & (KVASER_USB_HYDRA_CF_FLAG_OSM_NACK |
1138 			     KVASER_USB_HYDRA_CF_FLAG_ABL)) {
1139 			kvaser_usb_hydra_one_shot_fail(priv, cmd_ext);
1140 			one_shot_fail = true;
1141 		}
1142 	}
1143 
1144 	context = &priv->tx_contexts[transid % dev->max_tx_urbs];
1145 	if (!one_shot_fail) {
1146 		struct net_device_stats *stats = &priv->netdev->stats;
1147 
1148 		stats->tx_packets++;
1149 		stats->tx_bytes += can_fd_dlc2len(context->dlc);
1150 	}
1151 
1152 	spin_lock_irqsave(&priv->tx_contexts_lock, irq_flags);
1153 
1154 	can_get_echo_skb(priv->netdev, context->echo_index);
1155 	context->echo_index = dev->max_tx_urbs;
1156 	--priv->active_tx_contexts;
1157 	netif_wake_queue(priv->netdev);
1158 
1159 	spin_unlock_irqrestore(&priv->tx_contexts_lock, irq_flags);
1160 }
1161 
1162 static void kvaser_usb_hydra_rx_msg_std(const struct kvaser_usb *dev,
1163 					const struct kvaser_cmd *cmd)
1164 {
1165 	struct kvaser_usb_net_priv *priv = NULL;
1166 	struct can_frame *cf;
1167 	struct sk_buff *skb;
1168 	struct skb_shared_hwtstamps *shhwtstamps;
1169 	struct net_device_stats *stats;
1170 	u8 flags;
1171 	ktime_t hwtstamp;
1172 
1173 	priv = kvaser_usb_hydra_net_priv_from_cmd(dev, cmd);
1174 	if (!priv)
1175 		return;
1176 
1177 	stats = &priv->netdev->stats;
1178 
1179 	flags = cmd->rx_can.flags;
1180 	hwtstamp = kvaser_usb_hydra_ktime_from_rx_cmd(dev->cfg, cmd);
1181 
1182 	if (flags & KVASER_USB_HYDRA_CF_FLAG_ERROR_FRAME) {
1183 		kvaser_usb_hydra_error_frame(priv, &cmd->rx_can.err_frame_data,
1184 					     hwtstamp);
1185 		return;
1186 	}
1187 
1188 	skb = alloc_can_skb(priv->netdev, &cf);
1189 	if (!skb) {
1190 		stats->rx_dropped++;
1191 		return;
1192 	}
1193 
1194 	shhwtstamps = skb_hwtstamps(skb);
1195 	shhwtstamps->hwtstamp = hwtstamp;
1196 
1197 	cf->can_id = le32_to_cpu(cmd->rx_can.id);
1198 
1199 	if (cf->can_id &  KVASER_USB_HYDRA_EXTENDED_FRAME_ID) {
1200 		cf->can_id &= CAN_EFF_MASK;
1201 		cf->can_id |= CAN_EFF_FLAG;
1202 	} else {
1203 		cf->can_id &= CAN_SFF_MASK;
1204 	}
1205 
1206 	if (flags & KVASER_USB_HYDRA_CF_FLAG_OVERRUN)
1207 		kvaser_usb_can_rx_over_error(priv->netdev);
1208 
1209 	cf->len = can_cc_dlc2len(cmd->rx_can.dlc);
1210 
1211 	if (flags & KVASER_USB_HYDRA_CF_FLAG_REMOTE_FRAME)
1212 		cf->can_id |= CAN_RTR_FLAG;
1213 	else
1214 		memcpy(cf->data, cmd->rx_can.data, cf->len);
1215 
1216 	stats->rx_packets++;
1217 	stats->rx_bytes += cf->len;
1218 	netif_rx(skb);
1219 }
1220 
1221 static void kvaser_usb_hydra_rx_msg_ext(const struct kvaser_usb *dev,
1222 					const struct kvaser_cmd_ext *cmd)
1223 {
1224 	struct kvaser_cmd *std_cmd = (struct kvaser_cmd *)cmd;
1225 	struct kvaser_usb_net_priv *priv;
1226 	struct canfd_frame *cf;
1227 	struct sk_buff *skb;
1228 	struct skb_shared_hwtstamps *shhwtstamps;
1229 	struct net_device_stats *stats;
1230 	u32 flags;
1231 	u8 dlc;
1232 	u32 kcan_header;
1233 	ktime_t hwtstamp;
1234 
1235 	priv = kvaser_usb_hydra_net_priv_from_cmd(dev, std_cmd);
1236 	if (!priv)
1237 		return;
1238 
1239 	stats = &priv->netdev->stats;
1240 
1241 	kcan_header = le32_to_cpu(cmd->rx_can.kcan_header);
1242 	dlc = (kcan_header & KVASER_USB_KCAN_DATA_DLC_MASK) >>
1243 		KVASER_USB_KCAN_DATA_DLC_SHIFT;
1244 
1245 	flags = le32_to_cpu(cmd->rx_can.flags);
1246 	hwtstamp = kvaser_usb_hydra_ktime_from_rx_cmd(dev->cfg, std_cmd);
1247 
1248 	if (flags & KVASER_USB_HYDRA_CF_FLAG_ERROR_FRAME) {
1249 		kvaser_usb_hydra_error_frame(priv, &cmd->rx_can.err_frame_data,
1250 					     hwtstamp);
1251 		return;
1252 	}
1253 
1254 	if (flags & KVASER_USB_HYDRA_CF_FLAG_FDF)
1255 		skb = alloc_canfd_skb(priv->netdev, &cf);
1256 	else
1257 		skb = alloc_can_skb(priv->netdev, (struct can_frame **)&cf);
1258 
1259 	if (!skb) {
1260 		stats->rx_dropped++;
1261 		return;
1262 	}
1263 
1264 	shhwtstamps = skb_hwtstamps(skb);
1265 	shhwtstamps->hwtstamp = hwtstamp;
1266 
1267 	cf->can_id = le32_to_cpu(cmd->rx_can.id);
1268 
1269 	if (flags & KVASER_USB_HYDRA_CF_FLAG_EXTENDED_ID) {
1270 		cf->can_id &= CAN_EFF_MASK;
1271 		cf->can_id |= CAN_EFF_FLAG;
1272 	} else {
1273 		cf->can_id &= CAN_SFF_MASK;
1274 	}
1275 
1276 	if (flags & KVASER_USB_HYDRA_CF_FLAG_OVERRUN)
1277 		kvaser_usb_can_rx_over_error(priv->netdev);
1278 
1279 	if (flags & KVASER_USB_HYDRA_CF_FLAG_FDF) {
1280 		cf->len = can_fd_dlc2len(dlc);
1281 		if (flags & KVASER_USB_HYDRA_CF_FLAG_BRS)
1282 			cf->flags |= CANFD_BRS;
1283 		if (flags & KVASER_USB_HYDRA_CF_FLAG_ESI)
1284 			cf->flags |= CANFD_ESI;
1285 	} else {
1286 		cf->len = can_cc_dlc2len(dlc);
1287 	}
1288 
1289 	if (flags & KVASER_USB_HYDRA_CF_FLAG_REMOTE_FRAME)
1290 		cf->can_id |= CAN_RTR_FLAG;
1291 	else
1292 		memcpy(cf->data, cmd->rx_can.kcan_payload, cf->len);
1293 
1294 	stats->rx_packets++;
1295 	stats->rx_bytes += cf->len;
1296 	netif_rx(skb);
1297 }
1298 
1299 static void kvaser_usb_hydra_handle_cmd_std(const struct kvaser_usb *dev,
1300 					    const struct kvaser_cmd *cmd)
1301 {
1302 	switch (cmd->header.cmd_no) {
1303 	case CMD_START_CHIP_RESP:
1304 		kvaser_usb_hydra_start_chip_reply(dev, cmd);
1305 		break;
1306 
1307 	case CMD_STOP_CHIP_RESP:
1308 		kvaser_usb_hydra_stop_chip_reply(dev, cmd);
1309 		break;
1310 
1311 	case CMD_FLUSH_QUEUE_RESP:
1312 		kvaser_usb_hydra_flush_queue_reply(dev, cmd);
1313 		break;
1314 
1315 	case CMD_CHIP_STATE_EVENT:
1316 		kvaser_usb_hydra_state_event(dev, cmd);
1317 		break;
1318 
1319 	case CMD_ERROR_EVENT:
1320 		kvaser_usb_hydra_error_event(dev, cmd);
1321 		break;
1322 
1323 	case CMD_TX_ACKNOWLEDGE:
1324 		kvaser_usb_hydra_tx_acknowledge(dev, cmd);
1325 		break;
1326 
1327 	case CMD_RX_MESSAGE:
1328 		kvaser_usb_hydra_rx_msg_std(dev, cmd);
1329 		break;
1330 
1331 	/* Ignored commands */
1332 	case CMD_SET_BUSPARAMS_RESP:
1333 	case CMD_SET_BUSPARAMS_FD_RESP:
1334 		break;
1335 
1336 	default:
1337 		dev_warn(&dev->intf->dev, "Unhandled command (%d)\n",
1338 			 cmd->header.cmd_no);
1339 		break;
1340 	}
1341 }
1342 
1343 static void kvaser_usb_hydra_handle_cmd_ext(const struct kvaser_usb *dev,
1344 					    const struct kvaser_cmd_ext *cmd)
1345 {
1346 	switch (cmd->cmd_no_ext) {
1347 	case CMD_TX_ACKNOWLEDGE_FD:
1348 		kvaser_usb_hydra_tx_acknowledge(dev, (struct kvaser_cmd *)cmd);
1349 		break;
1350 
1351 	case CMD_RX_MESSAGE_FD:
1352 		kvaser_usb_hydra_rx_msg_ext(dev, cmd);
1353 		break;
1354 
1355 	default:
1356 		dev_warn(&dev->intf->dev, "Unhandled extended command (%d)\n",
1357 			 cmd->header.cmd_no);
1358 		break;
1359 	}
1360 }
1361 
1362 static void kvaser_usb_hydra_handle_cmd(const struct kvaser_usb *dev,
1363 					const struct kvaser_cmd *cmd)
1364 {
1365 		if (cmd->header.cmd_no == CMD_EXTENDED)
1366 			kvaser_usb_hydra_handle_cmd_ext
1367 					(dev, (struct kvaser_cmd_ext *)cmd);
1368 		else
1369 			kvaser_usb_hydra_handle_cmd_std(dev, cmd);
1370 }
1371 
1372 static void *
1373 kvaser_usb_hydra_frame_to_cmd_ext(const struct kvaser_usb_net_priv *priv,
1374 				  const struct sk_buff *skb, int *frame_len,
1375 				  int *cmd_len, u16 transid)
1376 {
1377 	struct kvaser_usb *dev = priv->dev;
1378 	struct kvaser_cmd_ext *cmd;
1379 	struct canfd_frame *cf = (struct canfd_frame *)skb->data;
1380 	u8 dlc = can_fd_len2dlc(cf->len);
1381 	u8 nbr_of_bytes = cf->len;
1382 	u32 flags;
1383 	u32 id;
1384 	u32 kcan_id;
1385 	u32 kcan_header;
1386 
1387 	*frame_len = nbr_of_bytes;
1388 
1389 	cmd = kcalloc(1, sizeof(struct kvaser_cmd_ext), GFP_ATOMIC);
1390 	if (!cmd)
1391 		return NULL;
1392 
1393 	kvaser_usb_hydra_set_cmd_dest_he
1394 			((struct kvaser_cmd *)cmd,
1395 			 dev->card_data.hydra.channel_to_he[priv->channel]);
1396 	kvaser_usb_hydra_set_cmd_transid((struct kvaser_cmd *)cmd, transid);
1397 
1398 	cmd->header.cmd_no = CMD_EXTENDED;
1399 	cmd->cmd_no_ext = CMD_TX_CAN_MESSAGE_FD;
1400 
1401 	*cmd_len = ALIGN(sizeof(struct kvaser_cmd_ext) -
1402 			 sizeof(cmd->tx_can.kcan_payload) + nbr_of_bytes,
1403 			 8);
1404 
1405 	cmd->len = cpu_to_le16(*cmd_len);
1406 
1407 	cmd->tx_can.databytes = nbr_of_bytes;
1408 	cmd->tx_can.dlc = dlc;
1409 
1410 	if (cf->can_id & CAN_EFF_FLAG) {
1411 		id = cf->can_id & CAN_EFF_MASK;
1412 		flags = KVASER_USB_HYDRA_CF_FLAG_EXTENDED_ID;
1413 		kcan_id = (cf->can_id & CAN_EFF_MASK) |
1414 			  KVASER_USB_KCAN_DATA_IDE | KVASER_USB_KCAN_DATA_SRR;
1415 	} else {
1416 		id = cf->can_id & CAN_SFF_MASK;
1417 		flags = 0;
1418 		kcan_id = cf->can_id & CAN_SFF_MASK;
1419 	}
1420 
1421 	if (cf->can_id & CAN_ERR_FLAG)
1422 		flags |= KVASER_USB_HYDRA_CF_FLAG_ERROR_FRAME;
1423 
1424 	kcan_header = ((dlc << KVASER_USB_KCAN_DATA_DLC_SHIFT) &
1425 				KVASER_USB_KCAN_DATA_DLC_MASK) |
1426 			KVASER_USB_KCAN_DATA_AREQ |
1427 			(priv->can.ctrlmode & CAN_CTRLMODE_ONE_SHOT ?
1428 				KVASER_USB_KCAN_DATA_OSM : 0);
1429 
1430 	if (can_is_canfd_skb(skb)) {
1431 		kcan_header |= KVASER_USB_KCAN_DATA_FDF |
1432 			       (cf->flags & CANFD_BRS ?
1433 					KVASER_USB_KCAN_DATA_BRS : 0);
1434 	} else {
1435 		if (cf->can_id & CAN_RTR_FLAG) {
1436 			kcan_id |= KVASER_USB_KCAN_DATA_RTR;
1437 			cmd->tx_can.databytes = 0;
1438 			flags |= KVASER_USB_HYDRA_CF_FLAG_REMOTE_FRAME;
1439 		}
1440 	}
1441 
1442 	cmd->tx_can.kcan_id = cpu_to_le32(kcan_id);
1443 	cmd->tx_can.id = cpu_to_le32(id);
1444 	cmd->tx_can.flags = cpu_to_le32(flags);
1445 	cmd->tx_can.kcan_header = cpu_to_le32(kcan_header);
1446 
1447 	memcpy(cmd->tx_can.kcan_payload, cf->data, nbr_of_bytes);
1448 
1449 	return cmd;
1450 }
1451 
1452 static void *
1453 kvaser_usb_hydra_frame_to_cmd_std(const struct kvaser_usb_net_priv *priv,
1454 				  const struct sk_buff *skb, int *frame_len,
1455 				  int *cmd_len, u16 transid)
1456 {
1457 	struct kvaser_usb *dev = priv->dev;
1458 	struct kvaser_cmd *cmd;
1459 	struct can_frame *cf = (struct can_frame *)skb->data;
1460 	u32 flags;
1461 	u32 id;
1462 
1463 	*frame_len = cf->len;
1464 
1465 	cmd = kcalloc(1, sizeof(struct kvaser_cmd), GFP_ATOMIC);
1466 	if (!cmd)
1467 		return NULL;
1468 
1469 	kvaser_usb_hydra_set_cmd_dest_he
1470 		(cmd, dev->card_data.hydra.channel_to_he[priv->channel]);
1471 	kvaser_usb_hydra_set_cmd_transid(cmd, transid);
1472 
1473 	cmd->header.cmd_no = CMD_TX_CAN_MESSAGE;
1474 
1475 	*cmd_len = ALIGN(sizeof(struct kvaser_cmd), 8);
1476 
1477 	if (cf->can_id & CAN_EFF_FLAG) {
1478 		id = (cf->can_id & CAN_EFF_MASK);
1479 		id |= KVASER_USB_HYDRA_EXTENDED_FRAME_ID;
1480 	} else {
1481 		id = cf->can_id & CAN_SFF_MASK;
1482 	}
1483 
1484 	cmd->tx_can.dlc = cf->len;
1485 
1486 	flags = (cf->can_id & CAN_EFF_FLAG ?
1487 		 KVASER_USB_HYDRA_CF_FLAG_EXTENDED_ID : 0);
1488 
1489 	if (cf->can_id & CAN_RTR_FLAG)
1490 		flags |= KVASER_USB_HYDRA_CF_FLAG_REMOTE_FRAME;
1491 
1492 	flags |= (cf->can_id & CAN_ERR_FLAG ?
1493 		  KVASER_USB_HYDRA_CF_FLAG_ERROR_FRAME : 0);
1494 
1495 	cmd->tx_can.id = cpu_to_le32(id);
1496 	cmd->tx_can.flags = flags;
1497 
1498 	memcpy(cmd->tx_can.data, cf->data, *frame_len);
1499 
1500 	return cmd;
1501 }
1502 
1503 static int kvaser_usb_hydra_set_mode(struct net_device *netdev,
1504 				     enum can_mode mode)
1505 {
1506 	int err = 0;
1507 
1508 	switch (mode) {
1509 	case CAN_MODE_START:
1510 		/* CAN controller automatically recovers from BUS_OFF */
1511 		break;
1512 	default:
1513 		err = -EOPNOTSUPP;
1514 	}
1515 
1516 	return err;
1517 }
1518 
1519 static int kvaser_usb_hydra_set_bittiming(struct net_device *netdev)
1520 {
1521 	struct kvaser_cmd *cmd;
1522 	struct kvaser_usb_net_priv *priv = netdev_priv(netdev);
1523 	struct can_bittiming *bt = &priv->can.bittiming;
1524 	struct kvaser_usb *dev = priv->dev;
1525 	int tseg1 = bt->prop_seg + bt->phase_seg1;
1526 	int tseg2 = bt->phase_seg2;
1527 	int sjw = bt->sjw;
1528 	int err;
1529 
1530 	cmd = kcalloc(1, sizeof(struct kvaser_cmd), GFP_KERNEL);
1531 	if (!cmd)
1532 		return -ENOMEM;
1533 
1534 	cmd->header.cmd_no = CMD_SET_BUSPARAMS_REQ;
1535 	cmd->set_busparams_req.bitrate = cpu_to_le32(bt->bitrate);
1536 	cmd->set_busparams_req.sjw = (u8)sjw;
1537 	cmd->set_busparams_req.tseg1 = (u8)tseg1;
1538 	cmd->set_busparams_req.tseg2 = (u8)tseg2;
1539 	cmd->set_busparams_req.nsamples = 1;
1540 
1541 	kvaser_usb_hydra_set_cmd_dest_he
1542 		(cmd, dev->card_data.hydra.channel_to_he[priv->channel]);
1543 	kvaser_usb_hydra_set_cmd_transid
1544 				(cmd, kvaser_usb_hydra_get_next_transid(dev));
1545 
1546 	err = kvaser_usb_send_cmd(dev, cmd, kvaser_usb_hydra_cmd_size(cmd));
1547 
1548 	kfree(cmd);
1549 
1550 	return err;
1551 }
1552 
1553 static int kvaser_usb_hydra_set_data_bittiming(struct net_device *netdev)
1554 {
1555 	struct kvaser_cmd *cmd;
1556 	struct kvaser_usb_net_priv *priv = netdev_priv(netdev);
1557 	struct can_bittiming *dbt = &priv->can.data_bittiming;
1558 	struct kvaser_usb *dev = priv->dev;
1559 	int tseg1 = dbt->prop_seg + dbt->phase_seg1;
1560 	int tseg2 = dbt->phase_seg2;
1561 	int sjw = dbt->sjw;
1562 	int err;
1563 
1564 	cmd = kcalloc(1, sizeof(struct kvaser_cmd), GFP_KERNEL);
1565 	if (!cmd)
1566 		return -ENOMEM;
1567 
1568 	cmd->header.cmd_no = CMD_SET_BUSPARAMS_FD_REQ;
1569 	cmd->set_busparams_req.bitrate_d = cpu_to_le32(dbt->bitrate);
1570 	cmd->set_busparams_req.sjw_d = (u8)sjw;
1571 	cmd->set_busparams_req.tseg1_d = (u8)tseg1;
1572 	cmd->set_busparams_req.tseg2_d = (u8)tseg2;
1573 	cmd->set_busparams_req.nsamples_d = 1;
1574 
1575 	if (priv->can.ctrlmode & CAN_CTRLMODE_FD) {
1576 		if (priv->can.ctrlmode & CAN_CTRLMODE_FD_NON_ISO)
1577 			cmd->set_busparams_req.canfd_mode =
1578 					KVASER_USB_HYDRA_BUS_MODE_NONISO;
1579 		else
1580 			cmd->set_busparams_req.canfd_mode =
1581 					KVASER_USB_HYDRA_BUS_MODE_CANFD_ISO;
1582 	}
1583 
1584 	kvaser_usb_hydra_set_cmd_dest_he
1585 		(cmd, dev->card_data.hydra.channel_to_he[priv->channel]);
1586 	kvaser_usb_hydra_set_cmd_transid
1587 				(cmd, kvaser_usb_hydra_get_next_transid(dev));
1588 
1589 	err = kvaser_usb_send_cmd(dev, cmd, kvaser_usb_hydra_cmd_size(cmd));
1590 
1591 	kfree(cmd);
1592 
1593 	return err;
1594 }
1595 
1596 static int kvaser_usb_hydra_get_berr_counter(const struct net_device *netdev,
1597 					     struct can_berr_counter *bec)
1598 {
1599 	struct kvaser_usb_net_priv *priv = netdev_priv(netdev);
1600 	int err;
1601 
1602 	err = kvaser_usb_hydra_send_simple_cmd(priv->dev,
1603 					       CMD_GET_CHIP_STATE_REQ,
1604 					       priv->channel);
1605 	if (err)
1606 		return err;
1607 
1608 	*bec = priv->bec;
1609 
1610 	return 0;
1611 }
1612 
1613 static int kvaser_usb_hydra_setup_endpoints(struct kvaser_usb *dev)
1614 {
1615 	const struct usb_host_interface *iface_desc;
1616 	struct usb_endpoint_descriptor *ep;
1617 	int i;
1618 
1619 	iface_desc = dev->intf->cur_altsetting;
1620 
1621 	for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
1622 		ep = &iface_desc->endpoint[i].desc;
1623 
1624 		if (!dev->bulk_in && usb_endpoint_is_bulk_in(ep) &&
1625 		    ep->bEndpointAddress == KVASER_USB_HYDRA_BULK_EP_IN_ADDR)
1626 			dev->bulk_in = ep;
1627 
1628 		if (!dev->bulk_out && usb_endpoint_is_bulk_out(ep) &&
1629 		    ep->bEndpointAddress == KVASER_USB_HYDRA_BULK_EP_OUT_ADDR)
1630 			dev->bulk_out = ep;
1631 
1632 		if (dev->bulk_in && dev->bulk_out)
1633 			return 0;
1634 	}
1635 
1636 	return -ENODEV;
1637 }
1638 
1639 static int kvaser_usb_hydra_init_card(struct kvaser_usb *dev)
1640 {
1641 	int err;
1642 	unsigned int i;
1643 	struct kvaser_usb_dev_card_data_hydra *card_data =
1644 							&dev->card_data.hydra;
1645 
1646 	card_data->transid = KVASER_USB_HYDRA_MIN_TRANSID;
1647 	spin_lock_init(&card_data->transid_lock);
1648 
1649 	memset(card_data->usb_rx_leftover, 0, KVASER_USB_HYDRA_MAX_CMD_LEN);
1650 	card_data->usb_rx_leftover_len = 0;
1651 	spin_lock_init(&card_data->usb_rx_leftover_lock);
1652 
1653 	memset(card_data->channel_to_he, KVASER_USB_HYDRA_HE_ADDRESS_ILLEGAL,
1654 	       sizeof(card_data->channel_to_he));
1655 	card_data->sysdbg_he = 0;
1656 
1657 	for (i = 0; i < KVASER_USB_MAX_NET_DEVICES; i++) {
1658 		err = kvaser_usb_hydra_map_channel
1659 					(dev,
1660 					 (KVASER_USB_HYDRA_TRANSID_CANHE | i),
1661 					 i, "CAN");
1662 		if (err) {
1663 			dev_err(&dev->intf->dev,
1664 				"CMD_MAP_CHANNEL_REQ failed for CAN%u\n", i);
1665 			return err;
1666 		}
1667 	}
1668 
1669 	err = kvaser_usb_hydra_map_channel(dev, KVASER_USB_HYDRA_TRANSID_SYSDBG,
1670 					   0, "SYSDBG");
1671 	if (err) {
1672 		dev_err(&dev->intf->dev,
1673 			"CMD_MAP_CHANNEL_REQ failed for SYSDBG\n");
1674 		return err;
1675 	}
1676 
1677 	return 0;
1678 }
1679 
1680 static int kvaser_usb_hydra_get_software_info(struct kvaser_usb *dev)
1681 {
1682 	struct kvaser_cmd cmd;
1683 	int err;
1684 
1685 	err = kvaser_usb_hydra_send_simple_cmd(dev, CMD_GET_SOFTWARE_INFO_REQ,
1686 					       -1);
1687 	if (err)
1688 		return err;
1689 
1690 	memset(&cmd, 0, sizeof(struct kvaser_cmd));
1691 	err = kvaser_usb_hydra_wait_cmd(dev, CMD_GET_SOFTWARE_INFO_RESP, &cmd);
1692 	if (err)
1693 		return err;
1694 
1695 	dev->max_tx_urbs = min_t(unsigned int, KVASER_USB_MAX_TX_URBS,
1696 				 le16_to_cpu(cmd.sw_info.max_outstanding_tx));
1697 
1698 	return 0;
1699 }
1700 
1701 static int kvaser_usb_hydra_get_software_details(struct kvaser_usb *dev)
1702 {
1703 	struct kvaser_cmd *cmd;
1704 	int err;
1705 	u32 flags;
1706 	struct kvaser_usb_dev_card_data *card_data = &dev->card_data;
1707 
1708 	cmd = kcalloc(1, sizeof(struct kvaser_cmd), GFP_KERNEL);
1709 	if (!cmd)
1710 		return -ENOMEM;
1711 
1712 	cmd->header.cmd_no = CMD_GET_SOFTWARE_DETAILS_REQ;
1713 	cmd->sw_detail_req.use_ext_cmd = 1;
1714 	kvaser_usb_hydra_set_cmd_dest_he
1715 				(cmd, KVASER_USB_HYDRA_HE_ADDRESS_ILLEGAL);
1716 
1717 	kvaser_usb_hydra_set_cmd_transid
1718 				(cmd, kvaser_usb_hydra_get_next_transid(dev));
1719 
1720 	err = kvaser_usb_send_cmd(dev, cmd, kvaser_usb_hydra_cmd_size(cmd));
1721 	if (err)
1722 		goto end;
1723 
1724 	err = kvaser_usb_hydra_wait_cmd(dev, CMD_GET_SOFTWARE_DETAILS_RESP,
1725 					cmd);
1726 	if (err)
1727 		goto end;
1728 
1729 	dev->fw_version = le32_to_cpu(cmd->sw_detail_res.sw_version);
1730 	flags = le32_to_cpu(cmd->sw_detail_res.sw_flags);
1731 
1732 	if (flags & KVASER_USB_HYDRA_SW_FLAG_FW_BAD) {
1733 		dev_err(&dev->intf->dev,
1734 			"Bad firmware, device refuse to run!\n");
1735 		err = -EINVAL;
1736 		goto end;
1737 	}
1738 
1739 	if (flags & KVASER_USB_HYDRA_SW_FLAG_FW_BETA)
1740 		dev_info(&dev->intf->dev, "Beta firmware in use\n");
1741 
1742 	if (flags & KVASER_USB_HYDRA_SW_FLAG_EXT_CAP)
1743 		card_data->capabilities |= KVASER_USB_CAP_EXT_CAP;
1744 
1745 	if (flags & KVASER_USB_HYDRA_SW_FLAG_EXT_CMD)
1746 		card_data->capabilities |= KVASER_USB_HYDRA_CAP_EXT_CMD;
1747 
1748 	if (flags & KVASER_USB_HYDRA_SW_FLAG_CANFD)
1749 		card_data->ctrlmode_supported |= CAN_CTRLMODE_FD;
1750 
1751 	if (flags & KVASER_USB_HYDRA_SW_FLAG_NONISO)
1752 		card_data->ctrlmode_supported |= CAN_CTRLMODE_FD_NON_ISO;
1753 
1754 	if (flags &  KVASER_USB_HYDRA_SW_FLAG_FREQ_80M)
1755 		dev->cfg = &kvaser_usb_hydra_dev_cfg_kcan;
1756 	else if (flags & KVASER_USB_HYDRA_SW_FLAG_CAN_FREQ_80M)
1757 		dev->cfg = &kvaser_usb_hydra_dev_cfg_rt;
1758 	else
1759 		dev->cfg = &kvaser_usb_hydra_dev_cfg_flexc;
1760 
1761 end:
1762 	kfree(cmd);
1763 
1764 	return err;
1765 }
1766 
1767 static int kvaser_usb_hydra_get_card_info(struct kvaser_usb *dev)
1768 {
1769 	struct kvaser_cmd cmd;
1770 	int err;
1771 
1772 	err = kvaser_usb_hydra_send_simple_cmd(dev, CMD_GET_CARD_INFO_REQ, -1);
1773 	if (err)
1774 		return err;
1775 
1776 	memset(&cmd, 0, sizeof(struct kvaser_cmd));
1777 	err = kvaser_usb_hydra_wait_cmd(dev, CMD_GET_CARD_INFO_RESP, &cmd);
1778 	if (err)
1779 		return err;
1780 
1781 	dev->nchannels = cmd.card_info.nchannels;
1782 	if (dev->nchannels > KVASER_USB_MAX_NET_DEVICES)
1783 		return -EINVAL;
1784 
1785 	return 0;
1786 }
1787 
1788 static int kvaser_usb_hydra_get_capabilities(struct kvaser_usb *dev)
1789 {
1790 	int err;
1791 	u16 status;
1792 
1793 	if (!(dev->card_data.capabilities & KVASER_USB_CAP_EXT_CAP)) {
1794 		dev_info(&dev->intf->dev,
1795 			 "No extended capability support. Upgrade your device.\n");
1796 		return 0;
1797 	}
1798 
1799 	err = kvaser_usb_hydra_get_single_capability
1800 					(dev,
1801 					 KVASER_USB_HYDRA_CAP_CMD_LISTEN_MODE,
1802 					 &status);
1803 	if (err)
1804 		return err;
1805 	if (status)
1806 		dev_info(&dev->intf->dev,
1807 			 "KVASER_USB_HYDRA_CAP_CMD_LISTEN_MODE failed %u\n",
1808 			 status);
1809 
1810 	err = kvaser_usb_hydra_get_single_capability
1811 					(dev,
1812 					 KVASER_USB_HYDRA_CAP_CMD_ERR_REPORT,
1813 					 &status);
1814 	if (err)
1815 		return err;
1816 	if (status)
1817 		dev_info(&dev->intf->dev,
1818 			 "KVASER_USB_HYDRA_CAP_CMD_ERR_REPORT failed %u\n",
1819 			 status);
1820 
1821 	err = kvaser_usb_hydra_get_single_capability
1822 					(dev, KVASER_USB_HYDRA_CAP_CMD_ONE_SHOT,
1823 					 &status);
1824 	if (err)
1825 		return err;
1826 	if (status)
1827 		dev_info(&dev->intf->dev,
1828 			 "KVASER_USB_HYDRA_CAP_CMD_ONE_SHOT failed %u\n",
1829 			 status);
1830 
1831 	return 0;
1832 }
1833 
1834 static int kvaser_usb_hydra_set_opt_mode(const struct kvaser_usb_net_priv *priv)
1835 {
1836 	struct kvaser_usb *dev = priv->dev;
1837 	struct kvaser_cmd *cmd;
1838 	int err;
1839 
1840 	if ((priv->can.ctrlmode &
1841 	    (CAN_CTRLMODE_FD | CAN_CTRLMODE_FD_NON_ISO)) ==
1842 	    CAN_CTRLMODE_FD_NON_ISO) {
1843 		netdev_warn(priv->netdev,
1844 			    "CTRLMODE_FD shall be on if CTRLMODE_FD_NON_ISO is on\n");
1845 		return -EINVAL;
1846 	}
1847 
1848 	cmd = kcalloc(1, sizeof(struct kvaser_cmd), GFP_KERNEL);
1849 	if (!cmd)
1850 		return -ENOMEM;
1851 
1852 	cmd->header.cmd_no = CMD_SET_DRIVERMODE_REQ;
1853 	kvaser_usb_hydra_set_cmd_dest_he
1854 		(cmd, dev->card_data.hydra.channel_to_he[priv->channel]);
1855 	kvaser_usb_hydra_set_cmd_transid
1856 				(cmd, kvaser_usb_hydra_get_next_transid(dev));
1857 	if (priv->can.ctrlmode & CAN_CTRLMODE_LISTENONLY)
1858 		cmd->set_ctrlmode.mode = KVASER_USB_HYDRA_CTRLMODE_LISTEN;
1859 	else
1860 		cmd->set_ctrlmode.mode = KVASER_USB_HYDRA_CTRLMODE_NORMAL;
1861 
1862 	err = kvaser_usb_send_cmd(dev, cmd, kvaser_usb_hydra_cmd_size(cmd));
1863 	kfree(cmd);
1864 
1865 	return err;
1866 }
1867 
1868 static int kvaser_usb_hydra_start_chip(struct kvaser_usb_net_priv *priv)
1869 {
1870 	int err;
1871 
1872 	init_completion(&priv->start_comp);
1873 
1874 	err = kvaser_usb_hydra_send_simple_cmd(priv->dev, CMD_START_CHIP_REQ,
1875 					       priv->channel);
1876 	if (err)
1877 		return err;
1878 
1879 	if (!wait_for_completion_timeout(&priv->start_comp,
1880 					 msecs_to_jiffies(KVASER_USB_TIMEOUT)))
1881 		return -ETIMEDOUT;
1882 
1883 	return 0;
1884 }
1885 
1886 static int kvaser_usb_hydra_stop_chip(struct kvaser_usb_net_priv *priv)
1887 {
1888 	int err;
1889 
1890 	init_completion(&priv->stop_comp);
1891 
1892 	/* Make sure we do not report invalid BUS_OFF from CMD_CHIP_STATE_EVENT
1893 	 * see comment in kvaser_usb_hydra_update_state()
1894 	 */
1895 	priv->can.state = CAN_STATE_STOPPED;
1896 
1897 	err = kvaser_usb_hydra_send_simple_cmd(priv->dev, CMD_STOP_CHIP_REQ,
1898 					       priv->channel);
1899 	if (err)
1900 		return err;
1901 
1902 	if (!wait_for_completion_timeout(&priv->stop_comp,
1903 					 msecs_to_jiffies(KVASER_USB_TIMEOUT)))
1904 		return -ETIMEDOUT;
1905 
1906 	return 0;
1907 }
1908 
1909 static int kvaser_usb_hydra_flush_queue(struct kvaser_usb_net_priv *priv)
1910 {
1911 	int err;
1912 
1913 	init_completion(&priv->flush_comp);
1914 
1915 	err = kvaser_usb_hydra_send_simple_cmd(priv->dev, CMD_FLUSH_QUEUE,
1916 					       priv->channel);
1917 	if (err)
1918 		return err;
1919 
1920 	if (!wait_for_completion_timeout(&priv->flush_comp,
1921 					 msecs_to_jiffies(KVASER_USB_TIMEOUT)))
1922 		return -ETIMEDOUT;
1923 
1924 	return 0;
1925 }
1926 
1927 /* A single extended hydra command can be transmitted in multiple transfers
1928  * We have to buffer partial hydra commands, and handle them on next callback.
1929  */
1930 static void kvaser_usb_hydra_read_bulk_callback(struct kvaser_usb *dev,
1931 						void *buf, int len)
1932 {
1933 	unsigned long irq_flags;
1934 	struct kvaser_cmd *cmd;
1935 	int pos = 0;
1936 	size_t cmd_len;
1937 	struct kvaser_usb_dev_card_data_hydra *card_data =
1938 							&dev->card_data.hydra;
1939 	int usb_rx_leftover_len;
1940 	spinlock_t *usb_rx_leftover_lock = &card_data->usb_rx_leftover_lock;
1941 
1942 	spin_lock_irqsave(usb_rx_leftover_lock, irq_flags);
1943 	usb_rx_leftover_len = card_data->usb_rx_leftover_len;
1944 	if (usb_rx_leftover_len) {
1945 		int remaining_bytes;
1946 
1947 		cmd = (struct kvaser_cmd *)card_data->usb_rx_leftover;
1948 
1949 		cmd_len = kvaser_usb_hydra_cmd_size(cmd);
1950 
1951 		remaining_bytes = min_t(unsigned int, len,
1952 					cmd_len - usb_rx_leftover_len);
1953 		/* Make sure we do not overflow usb_rx_leftover */
1954 		if (remaining_bytes + usb_rx_leftover_len >
1955 						KVASER_USB_HYDRA_MAX_CMD_LEN) {
1956 			dev_err(&dev->intf->dev, "Format error\n");
1957 			spin_unlock_irqrestore(usb_rx_leftover_lock, irq_flags);
1958 			return;
1959 		}
1960 
1961 		memcpy(card_data->usb_rx_leftover + usb_rx_leftover_len, buf,
1962 		       remaining_bytes);
1963 		pos += remaining_bytes;
1964 
1965 		if (remaining_bytes + usb_rx_leftover_len == cmd_len) {
1966 			kvaser_usb_hydra_handle_cmd(dev, cmd);
1967 			usb_rx_leftover_len = 0;
1968 		} else {
1969 			/* Command still not complete */
1970 			usb_rx_leftover_len += remaining_bytes;
1971 		}
1972 		card_data->usb_rx_leftover_len = usb_rx_leftover_len;
1973 	}
1974 	spin_unlock_irqrestore(usb_rx_leftover_lock, irq_flags);
1975 
1976 	while (pos < len) {
1977 		cmd = buf + pos;
1978 
1979 		cmd_len = kvaser_usb_hydra_cmd_size(cmd);
1980 
1981 		if (pos + cmd_len > len) {
1982 			/* We got first part of a command */
1983 			int leftover_bytes;
1984 
1985 			leftover_bytes = len - pos;
1986 			/* Make sure we do not overflow usb_rx_leftover */
1987 			if (leftover_bytes > KVASER_USB_HYDRA_MAX_CMD_LEN) {
1988 				dev_err(&dev->intf->dev, "Format error\n");
1989 				return;
1990 			}
1991 			spin_lock_irqsave(usb_rx_leftover_lock, irq_flags);
1992 			memcpy(card_data->usb_rx_leftover, buf + pos,
1993 			       leftover_bytes);
1994 			card_data->usb_rx_leftover_len = leftover_bytes;
1995 			spin_unlock_irqrestore(usb_rx_leftover_lock, irq_flags);
1996 			break;
1997 		}
1998 
1999 		kvaser_usb_hydra_handle_cmd(dev, cmd);
2000 		pos += cmd_len;
2001 	}
2002 }
2003 
2004 static void *
2005 kvaser_usb_hydra_frame_to_cmd(const struct kvaser_usb_net_priv *priv,
2006 			      const struct sk_buff *skb, int *frame_len,
2007 			      int *cmd_len, u16 transid)
2008 {
2009 	void *buf;
2010 
2011 	if (priv->dev->card_data.capabilities & KVASER_USB_HYDRA_CAP_EXT_CMD)
2012 		buf = kvaser_usb_hydra_frame_to_cmd_ext(priv, skb, frame_len,
2013 							cmd_len, transid);
2014 	else
2015 		buf = kvaser_usb_hydra_frame_to_cmd_std(priv, skb, frame_len,
2016 							cmd_len, transid);
2017 
2018 	return buf;
2019 }
2020 
2021 const struct kvaser_usb_dev_ops kvaser_usb_hydra_dev_ops = {
2022 	.dev_set_mode = kvaser_usb_hydra_set_mode,
2023 	.dev_set_bittiming = kvaser_usb_hydra_set_bittiming,
2024 	.dev_set_data_bittiming = kvaser_usb_hydra_set_data_bittiming,
2025 	.dev_get_berr_counter = kvaser_usb_hydra_get_berr_counter,
2026 	.dev_setup_endpoints = kvaser_usb_hydra_setup_endpoints,
2027 	.dev_init_card = kvaser_usb_hydra_init_card,
2028 	.dev_get_software_info = kvaser_usb_hydra_get_software_info,
2029 	.dev_get_software_details = kvaser_usb_hydra_get_software_details,
2030 	.dev_get_card_info = kvaser_usb_hydra_get_card_info,
2031 	.dev_get_capabilities = kvaser_usb_hydra_get_capabilities,
2032 	.dev_set_opt_mode = kvaser_usb_hydra_set_opt_mode,
2033 	.dev_start_chip = kvaser_usb_hydra_start_chip,
2034 	.dev_stop_chip = kvaser_usb_hydra_stop_chip,
2035 	.dev_reset_chip = NULL,
2036 	.dev_flush_queue = kvaser_usb_hydra_flush_queue,
2037 	.dev_read_bulk_callback = kvaser_usb_hydra_read_bulk_callback,
2038 	.dev_frame_to_cmd = kvaser_usb_hydra_frame_to_cmd,
2039 };
2040 
2041 static const struct kvaser_usb_dev_cfg kvaser_usb_hydra_dev_cfg_kcan = {
2042 	.clock = {
2043 		.freq = 80000000,
2044 	},
2045 	.timestamp_freq = 80,
2046 	.bittiming_const = &kvaser_usb_hydra_kcan_bittiming_c,
2047 	.data_bittiming_const = &kvaser_usb_hydra_kcan_bittiming_c,
2048 };
2049 
2050 static const struct kvaser_usb_dev_cfg kvaser_usb_hydra_dev_cfg_flexc = {
2051 	.clock = {
2052 		.freq = 24000000,
2053 	},
2054 	.timestamp_freq = 1,
2055 	.bittiming_const = &kvaser_usb_hydra_flexc_bittiming_c,
2056 };
2057 
2058 static const struct kvaser_usb_dev_cfg kvaser_usb_hydra_dev_cfg_rt = {
2059 	.clock = {
2060 		.freq = 80000000,
2061 	},
2062 	.timestamp_freq = 24,
2063 	.bittiming_const = &kvaser_usb_hydra_rt_bittiming_c,
2064 	.data_bittiming_const = &kvaser_usb_hydra_rtd_bittiming_c,
2065 };
2066