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