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