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 	size_t cmd_len;
549 	int err;
550 
551 	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
552 	if (!cmd)
553 		return -ENOMEM;
554 
555 	cmd->header.cmd_no = cmd_no;
556 	cmd_len = kvaser_usb_hydra_cmd_size(cmd);
557 	if (channel < 0) {
558 		kvaser_usb_hydra_set_cmd_dest_he
559 				(cmd, KVASER_USB_HYDRA_HE_ADDRESS_ILLEGAL);
560 	} else {
561 		if (channel >= KVASER_USB_MAX_NET_DEVICES) {
562 			dev_err(&dev->intf->dev, "channel (%d) out of range.\n",
563 				channel);
564 			err = -EINVAL;
565 			goto end;
566 		}
567 		kvaser_usb_hydra_set_cmd_dest_he
568 			(cmd, dev->card_data.hydra.channel_to_he[channel]);
569 	}
570 	kvaser_usb_hydra_set_cmd_transid
571 				(cmd, kvaser_usb_hydra_get_next_transid(dev));
572 
573 	err = kvaser_usb_send_cmd(dev, cmd, cmd_len);
574 	if (err)
575 		goto end;
576 
577 end:
578 	kfree(cmd);
579 
580 	return err;
581 }
582 
583 static int
kvaser_usb_hydra_send_simple_cmd_async(struct kvaser_usb_net_priv * priv,u8 cmd_no)584 kvaser_usb_hydra_send_simple_cmd_async(struct kvaser_usb_net_priv *priv,
585 				       u8 cmd_no)
586 {
587 	struct kvaser_cmd *cmd;
588 	struct kvaser_usb *dev = priv->dev;
589 	size_t cmd_len;
590 	int err;
591 
592 	cmd = kzalloc(sizeof(*cmd), GFP_ATOMIC);
593 	if (!cmd)
594 		return -ENOMEM;
595 
596 	cmd->header.cmd_no = cmd_no;
597 	cmd_len = kvaser_usb_hydra_cmd_size(cmd);
598 
599 	kvaser_usb_hydra_set_cmd_dest_he
600 		(cmd, dev->card_data.hydra.channel_to_he[priv->channel]);
601 	kvaser_usb_hydra_set_cmd_transid
602 				(cmd, kvaser_usb_hydra_get_next_transid(dev));
603 
604 	err = kvaser_usb_send_cmd_async(priv, cmd, cmd_len);
605 	if (err)
606 		kfree(cmd);
607 
608 	return err;
609 }
610 
611 /* This function is used for synchronously waiting on hydra control commands.
612  * Note: Compared to kvaser_usb_hydra_read_bulk_callback(), we never need to
613  *       handle partial hydra commands. Since hydra control commands are always
614  *       non-extended commands.
615  */
kvaser_usb_hydra_wait_cmd(const struct kvaser_usb * dev,u8 cmd_no,struct kvaser_cmd * cmd)616 static int kvaser_usb_hydra_wait_cmd(const struct kvaser_usb *dev, u8 cmd_no,
617 				     struct kvaser_cmd *cmd)
618 {
619 	void *buf;
620 	int err;
621 	unsigned long timeout = jiffies + msecs_to_jiffies(KVASER_USB_TIMEOUT);
622 
623 	if (cmd->header.cmd_no == CMD_EXTENDED) {
624 		dev_err(&dev->intf->dev, "Wait for CMD_EXTENDED not allowed\n");
625 		return -EINVAL;
626 	}
627 
628 	buf = kzalloc(KVASER_USB_RX_BUFFER_SIZE, GFP_KERNEL);
629 	if (!buf)
630 		return -ENOMEM;
631 
632 	do {
633 		int actual_len = 0;
634 		int pos = 0;
635 
636 		err = kvaser_usb_recv_cmd(dev, buf, KVASER_USB_RX_BUFFER_SIZE,
637 					  &actual_len);
638 		if (err < 0)
639 			goto end;
640 
641 		while (pos < actual_len) {
642 			struct kvaser_cmd *tmp_cmd;
643 			size_t cmd_len;
644 
645 			tmp_cmd = buf + pos;
646 			cmd_len = kvaser_usb_hydra_cmd_size(tmp_cmd);
647 			if (pos + cmd_len > actual_len) {
648 				dev_err_ratelimited(&dev->intf->dev,
649 						    "Format error\n");
650 				break;
651 			}
652 
653 			if (tmp_cmd->header.cmd_no == cmd_no) {
654 				memcpy(cmd, tmp_cmd, cmd_len);
655 				goto end;
656 			}
657 			pos += cmd_len;
658 		}
659 	} while (time_before(jiffies, timeout));
660 
661 	err = -EINVAL;
662 
663 end:
664 	kfree(buf);
665 
666 	return err;
667 }
668 
kvaser_usb_hydra_map_channel_resp(struct kvaser_usb * dev,const struct kvaser_cmd * cmd)669 static int kvaser_usb_hydra_map_channel_resp(struct kvaser_usb *dev,
670 					     const struct kvaser_cmd *cmd)
671 {
672 	u8 he, channel;
673 	u16 transid = kvaser_usb_hydra_get_cmd_transid(cmd);
674 	struct kvaser_usb_dev_card_data_hydra *card_data =
675 							&dev->card_data.hydra;
676 
677 	if (transid > 0x007f || transid < 0x0040) {
678 		dev_err(&dev->intf->dev,
679 			"CMD_MAP_CHANNEL_RESP, invalid transid: 0x%x\n",
680 			transid);
681 		return -EINVAL;
682 	}
683 
684 	switch (transid) {
685 	case KVASER_USB_HYDRA_TRANSID_CANHE:
686 	case KVASER_USB_HYDRA_TRANSID_CANHE + 1:
687 	case KVASER_USB_HYDRA_TRANSID_CANHE + 2:
688 	case KVASER_USB_HYDRA_TRANSID_CANHE + 3:
689 	case KVASER_USB_HYDRA_TRANSID_CANHE + 4:
690 		channel = transid & 0x000f;
691 		he = cmd->map_ch_res.he_addr;
692 		card_data->channel_to_he[channel] = he;
693 		break;
694 	case KVASER_USB_HYDRA_TRANSID_SYSDBG:
695 		card_data->sysdbg_he = cmd->map_ch_res.he_addr;
696 		break;
697 	default:
698 		dev_warn(&dev->intf->dev,
699 			 "Unknown CMD_MAP_CHANNEL_RESP transid=0x%x\n",
700 			 transid);
701 		break;
702 	}
703 
704 	return 0;
705 }
706 
kvaser_usb_hydra_map_channel(struct kvaser_usb * dev,u16 transid,u8 channel,const char * name)707 static int kvaser_usb_hydra_map_channel(struct kvaser_usb *dev, u16 transid,
708 					u8 channel, const char *name)
709 {
710 	struct kvaser_cmd *cmd;
711 	int err;
712 
713 	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
714 	if (!cmd)
715 		return -ENOMEM;
716 
717 	strcpy(cmd->map_ch_req.name, name);
718 	cmd->header.cmd_no = CMD_MAP_CHANNEL_REQ;
719 	kvaser_usb_hydra_set_cmd_dest_he
720 				(cmd, KVASER_USB_HYDRA_HE_ADDRESS_ROUTER);
721 	cmd->map_ch_req.channel = channel;
722 
723 	kvaser_usb_hydra_set_cmd_transid(cmd, transid);
724 
725 	err = kvaser_usb_send_cmd(dev, cmd, kvaser_usb_hydra_cmd_size(cmd));
726 	if (err)
727 		goto end;
728 
729 	err = kvaser_usb_hydra_wait_cmd(dev, CMD_MAP_CHANNEL_RESP, cmd);
730 	if (err)
731 		goto end;
732 
733 	err = kvaser_usb_hydra_map_channel_resp(dev, cmd);
734 	if (err)
735 		goto end;
736 
737 end:
738 	kfree(cmd);
739 
740 	return err;
741 }
742 
kvaser_usb_hydra_get_single_capability(struct kvaser_usb * dev,u16 cap_cmd_req,u16 * status)743 static int kvaser_usb_hydra_get_single_capability(struct kvaser_usb *dev,
744 						  u16 cap_cmd_req, u16 *status)
745 {
746 	struct kvaser_usb_dev_card_data *card_data = &dev->card_data;
747 	struct kvaser_cmd *cmd;
748 	size_t cmd_len;
749 	u32 value = 0;
750 	u32 mask = 0;
751 	u16 cap_cmd_res;
752 	int err;
753 	int i;
754 
755 	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
756 	if (!cmd)
757 		return -ENOMEM;
758 
759 	cmd->header.cmd_no = CMD_GET_CAPABILITIES_REQ;
760 	cmd_len = kvaser_usb_hydra_cmd_size(cmd);
761 	cmd->cap_req.cap_cmd = cpu_to_le16(cap_cmd_req);
762 
763 	kvaser_usb_hydra_set_cmd_dest_he(cmd, card_data->hydra.sysdbg_he);
764 	kvaser_usb_hydra_set_cmd_transid
765 				(cmd, kvaser_usb_hydra_get_next_transid(dev));
766 
767 	err = kvaser_usb_send_cmd(dev, cmd, cmd_len);
768 	if (err)
769 		goto end;
770 
771 	err = kvaser_usb_hydra_wait_cmd(dev, CMD_GET_CAPABILITIES_RESP, cmd);
772 	if (err)
773 		goto end;
774 
775 	*status = le16_to_cpu(cmd->cap_res.status);
776 
777 	if (*status != KVASER_USB_HYDRA_CAP_STAT_OK)
778 		goto end;
779 
780 	cap_cmd_res = le16_to_cpu(cmd->cap_res.cap_cmd);
781 	switch (cap_cmd_res) {
782 	case KVASER_USB_HYDRA_CAP_CMD_LISTEN_MODE:
783 	case KVASER_USB_HYDRA_CAP_CMD_ERR_REPORT:
784 	case KVASER_USB_HYDRA_CAP_CMD_ONE_SHOT:
785 		value = le32_to_cpu(cmd->cap_res.value);
786 		mask = le32_to_cpu(cmd->cap_res.mask);
787 		break;
788 	default:
789 		dev_warn(&dev->intf->dev, "Unknown capability command %u\n",
790 			 cap_cmd_res);
791 		break;
792 	}
793 
794 	for (i = 0; i < dev->nchannels; i++) {
795 		if (BIT(i) & (value & mask)) {
796 			switch (cap_cmd_res) {
797 			case KVASER_USB_HYDRA_CAP_CMD_LISTEN_MODE:
798 				card_data->ctrlmode_supported |=
799 						CAN_CTRLMODE_LISTENONLY;
800 				break;
801 			case KVASER_USB_HYDRA_CAP_CMD_ERR_REPORT:
802 				card_data->capabilities |=
803 						KVASER_USB_CAP_BERR_CAP;
804 				break;
805 			case KVASER_USB_HYDRA_CAP_CMD_ONE_SHOT:
806 				card_data->ctrlmode_supported |=
807 						CAN_CTRLMODE_ONE_SHOT;
808 				break;
809 			}
810 		}
811 	}
812 
813 end:
814 	kfree(cmd);
815 
816 	return err;
817 }
818 
kvaser_usb_hydra_start_chip_reply(const struct kvaser_usb * dev,const struct kvaser_cmd * cmd)819 static void kvaser_usb_hydra_start_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 	if (completion_done(&priv->start_comp) &&
829 	    netif_queue_stopped(priv->netdev)) {
830 		netif_wake_queue(priv->netdev);
831 	} else {
832 		netif_start_queue(priv->netdev);
833 		complete(&priv->start_comp);
834 	}
835 }
836 
kvaser_usb_hydra_stop_chip_reply(const struct kvaser_usb * dev,const struct kvaser_cmd * cmd)837 static void kvaser_usb_hydra_stop_chip_reply(const struct kvaser_usb *dev,
838 					     const struct kvaser_cmd *cmd)
839 {
840 	struct kvaser_usb_net_priv *priv;
841 
842 	priv = kvaser_usb_hydra_net_priv_from_cmd(dev, cmd);
843 	if (!priv)
844 		return;
845 
846 	complete(&priv->stop_comp);
847 }
848 
kvaser_usb_hydra_flush_queue_reply(const struct kvaser_usb * dev,const struct kvaser_cmd * cmd)849 static void kvaser_usb_hydra_flush_queue_reply(const struct kvaser_usb *dev,
850 					       const struct kvaser_cmd *cmd)
851 {
852 	struct kvaser_usb_net_priv *priv;
853 
854 	priv = kvaser_usb_hydra_net_priv_from_cmd(dev, cmd);
855 	if (!priv)
856 		return;
857 
858 	complete(&priv->flush_comp);
859 }
860 
kvaser_usb_hydra_get_busparams_reply(const struct kvaser_usb * dev,const struct kvaser_cmd * cmd)861 static void kvaser_usb_hydra_get_busparams_reply(const struct kvaser_usb *dev,
862 						 const struct kvaser_cmd *cmd)
863 {
864 	struct kvaser_usb_net_priv *priv;
865 	struct kvaser_usb_net_hydra_priv *hydra;
866 
867 	priv = kvaser_usb_hydra_net_priv_from_cmd(dev, cmd);
868 	if (!priv)
869 		return;
870 
871 	hydra = priv->sub_priv;
872 	if (!hydra)
873 		return;
874 
875 	switch (hydra->pending_get_busparams_type) {
876 	case KVASER_USB_HYDRA_BUSPARAM_TYPE_CAN:
877 		memcpy(&priv->busparams_nominal, &cmd->get_busparams_res.busparams,
878 		       sizeof(priv->busparams_nominal));
879 		break;
880 	case KVASER_USB_HYDRA_BUSPARAM_TYPE_CANFD:
881 		memcpy(&priv->busparams_data, &cmd->get_busparams_res.busparams,
882 		       sizeof(priv->busparams_nominal));
883 		break;
884 	default:
885 		dev_warn(&dev->intf->dev, "Unknown get_busparams_type %d\n",
886 			 hydra->pending_get_busparams_type);
887 		break;
888 	}
889 	hydra->pending_get_busparams_type = -1;
890 
891 	complete(&priv->get_busparams_comp);
892 }
893 
894 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)895 kvaser_usb_hydra_bus_status_to_can_state(const struct kvaser_usb_net_priv *priv,
896 					 u8 bus_status,
897 					 const struct can_berr_counter *bec,
898 					 enum can_state *new_state)
899 {
900 	if (bus_status & KVASER_USB_HYDRA_BUS_BUS_OFF) {
901 		*new_state = CAN_STATE_BUS_OFF;
902 	} else if (bus_status & KVASER_USB_HYDRA_BUS_ERR_PASS) {
903 		*new_state = CAN_STATE_ERROR_PASSIVE;
904 	} else if (bus_status == KVASER_USB_HYDRA_BUS_ERR_ACT) {
905 		if (bec->txerr >= 128 || bec->rxerr >= 128) {
906 			netdev_warn(priv->netdev,
907 				    "ERR_ACTIVE but err tx=%u or rx=%u >=128\n",
908 				    bec->txerr, bec->rxerr);
909 			*new_state = CAN_STATE_ERROR_PASSIVE;
910 		} else if (bec->txerr >= 96 || bec->rxerr >= 96) {
911 			*new_state = CAN_STATE_ERROR_WARNING;
912 		} else {
913 			*new_state = CAN_STATE_ERROR_ACTIVE;
914 		}
915 	}
916 }
917 
kvaser_usb_hydra_update_state(struct kvaser_usb_net_priv * priv,u8 bus_status,const struct can_berr_counter * bec)918 static void kvaser_usb_hydra_update_state(struct kvaser_usb_net_priv *priv,
919 					  u8 bus_status,
920 					  const struct can_berr_counter *bec)
921 {
922 	struct net_device *netdev = priv->netdev;
923 	struct can_frame *cf;
924 	struct sk_buff *skb;
925 	enum can_state new_state, old_state;
926 
927 	old_state = priv->can.state;
928 
929 	kvaser_usb_hydra_bus_status_to_can_state(priv, bus_status, bec,
930 						 &new_state);
931 
932 	if (new_state == old_state)
933 		return;
934 
935 	/* Ignore state change if previous state was STOPPED and the new state
936 	 * is BUS_OFF. Firmware always report this as BUS_OFF, since firmware
937 	 * does not distinguish between BUS_OFF and STOPPED.
938 	 */
939 	if (old_state == CAN_STATE_STOPPED && new_state == CAN_STATE_BUS_OFF)
940 		return;
941 
942 	skb = alloc_can_err_skb(netdev, &cf);
943 	if (skb) {
944 		enum can_state tx_state, rx_state;
945 
946 		tx_state = (bec->txerr >= bec->rxerr) ?
947 					new_state : CAN_STATE_ERROR_ACTIVE;
948 		rx_state = (bec->txerr <= bec->rxerr) ?
949 					new_state : CAN_STATE_ERROR_ACTIVE;
950 		can_change_state(netdev, cf, tx_state, rx_state);
951 	}
952 
953 	if (new_state == CAN_STATE_BUS_OFF && old_state < CAN_STATE_BUS_OFF) {
954 		if (!priv->can.restart_ms)
955 			kvaser_usb_hydra_send_simple_cmd_async
956 						(priv, CMD_STOP_CHIP_REQ);
957 
958 		can_bus_off(netdev);
959 	}
960 
961 	if (!skb) {
962 		netdev_warn(netdev, "No memory left for err_skb\n");
963 		return;
964 	}
965 
966 	if (priv->can.restart_ms &&
967 	    old_state >= CAN_STATE_BUS_OFF &&
968 	    new_state < CAN_STATE_BUS_OFF)
969 		priv->can.can_stats.restarts++;
970 
971 	if (new_state != CAN_STATE_BUS_OFF) {
972 		cf->can_id |= CAN_ERR_CNT;
973 		cf->data[6] = bec->txerr;
974 		cf->data[7] = bec->rxerr;
975 	}
976 
977 	netif_rx(skb);
978 }
979 
kvaser_usb_hydra_state_event(const struct kvaser_usb * dev,const struct kvaser_cmd * cmd)980 static void kvaser_usb_hydra_state_event(const struct kvaser_usb *dev,
981 					 const struct kvaser_cmd *cmd)
982 {
983 	struct kvaser_usb_net_priv *priv;
984 	struct can_berr_counter bec;
985 	u8 bus_status;
986 
987 	priv = kvaser_usb_hydra_net_priv_from_cmd(dev, cmd);
988 	if (!priv)
989 		return;
990 
991 	bus_status = cmd->chip_state_event.bus_status;
992 	bec.txerr = cmd->chip_state_event.tx_err_counter;
993 	bec.rxerr = cmd->chip_state_event.rx_err_counter;
994 
995 	kvaser_usb_hydra_update_state(priv, bus_status, &bec);
996 	priv->bec.txerr = bec.txerr;
997 	priv->bec.rxerr = bec.rxerr;
998 }
999 
kvaser_usb_hydra_error_event_parameter(const struct kvaser_usb * dev,const struct kvaser_cmd * cmd)1000 static void kvaser_usb_hydra_error_event_parameter(const struct kvaser_usb *dev,
1001 						   const struct kvaser_cmd *cmd)
1002 {
1003 	/* info1 will contain the offending cmd_no */
1004 	switch (le16_to_cpu(cmd->error_event.info1)) {
1005 	case CMD_START_CHIP_REQ:
1006 		dev_warn(&dev->intf->dev,
1007 			 "CMD_START_CHIP_REQ error in parameter\n");
1008 		break;
1009 
1010 	case CMD_STOP_CHIP_REQ:
1011 		dev_warn(&dev->intf->dev,
1012 			 "CMD_STOP_CHIP_REQ error in parameter\n");
1013 		break;
1014 
1015 	case CMD_FLUSH_QUEUE:
1016 		dev_warn(&dev->intf->dev,
1017 			 "CMD_FLUSH_QUEUE error in parameter\n");
1018 		break;
1019 
1020 	case CMD_SET_BUSPARAMS_REQ:
1021 		dev_warn(&dev->intf->dev,
1022 			 "Set bittiming failed. Error in parameter\n");
1023 		break;
1024 
1025 	case CMD_SET_BUSPARAMS_FD_REQ:
1026 		dev_warn(&dev->intf->dev,
1027 			 "Set data bittiming failed. Error in parameter\n");
1028 		break;
1029 
1030 	default:
1031 		dev_warn(&dev->intf->dev,
1032 			 "Unhandled parameter error event cmd_no (%u)\n",
1033 			 le16_to_cpu(cmd->error_event.info1));
1034 		break;
1035 	}
1036 }
1037 
kvaser_usb_hydra_error_event(const struct kvaser_usb * dev,const struct kvaser_cmd * cmd)1038 static void kvaser_usb_hydra_error_event(const struct kvaser_usb *dev,
1039 					 const struct kvaser_cmd *cmd)
1040 {
1041 	switch (cmd->error_event.error_code) {
1042 	case KVASER_USB_HYDRA_ERROR_EVENT_PARAM:
1043 		kvaser_usb_hydra_error_event_parameter(dev, cmd);
1044 		break;
1045 
1046 	case KVASER_USB_HYDRA_ERROR_EVENT_CAN:
1047 		/* Wrong channel mapping?! This should never happen!
1048 		 * info1 will contain the offending cmd_no
1049 		 */
1050 		dev_err(&dev->intf->dev,
1051 			"Received CAN error event for cmd_no (%u)\n",
1052 			le16_to_cpu(cmd->error_event.info1));
1053 		break;
1054 
1055 	default:
1056 		dev_warn(&dev->intf->dev,
1057 			 "Unhandled error event (%d)\n",
1058 			 cmd->error_event.error_code);
1059 		break;
1060 	}
1061 }
1062 
1063 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)1064 kvaser_usb_hydra_error_frame(struct kvaser_usb_net_priv *priv,
1065 			     const struct kvaser_err_frame_data *err_frame_data,
1066 			     ktime_t hwtstamp)
1067 {
1068 	struct net_device *netdev = priv->netdev;
1069 	struct net_device_stats *stats = &netdev->stats;
1070 	struct can_frame *cf;
1071 	struct sk_buff *skb;
1072 	struct skb_shared_hwtstamps *shhwtstamps;
1073 	struct can_berr_counter bec;
1074 	enum can_state new_state, old_state;
1075 	u8 bus_status;
1076 
1077 	priv->can.can_stats.bus_error++;
1078 	stats->rx_errors++;
1079 
1080 	bus_status = err_frame_data->bus_status;
1081 	bec.txerr = err_frame_data->tx_err_counter;
1082 	bec.rxerr = err_frame_data->rx_err_counter;
1083 
1084 	old_state = priv->can.state;
1085 	kvaser_usb_hydra_bus_status_to_can_state(priv, bus_status, &bec,
1086 						 &new_state);
1087 
1088 	skb = alloc_can_err_skb(netdev, &cf);
1089 
1090 	if (new_state != old_state) {
1091 		if (skb) {
1092 			enum can_state tx_state, rx_state;
1093 
1094 			tx_state = (bec.txerr >= bec.rxerr) ?
1095 					new_state : CAN_STATE_ERROR_ACTIVE;
1096 			rx_state = (bec.txerr <= bec.rxerr) ?
1097 					new_state : CAN_STATE_ERROR_ACTIVE;
1098 
1099 			can_change_state(netdev, cf, tx_state, rx_state);
1100 
1101 			if (priv->can.restart_ms &&
1102 			    old_state >= CAN_STATE_BUS_OFF &&
1103 			    new_state < CAN_STATE_BUS_OFF)
1104 				cf->can_id |= CAN_ERR_RESTARTED;
1105 		}
1106 
1107 		if (new_state == CAN_STATE_BUS_OFF) {
1108 			if (!priv->can.restart_ms)
1109 				kvaser_usb_hydra_send_simple_cmd_async
1110 						(priv, CMD_STOP_CHIP_REQ);
1111 
1112 			can_bus_off(netdev);
1113 		}
1114 	}
1115 
1116 	if (!skb) {
1117 		stats->rx_dropped++;
1118 		netdev_warn(netdev, "No memory left for err_skb\n");
1119 		return;
1120 	}
1121 
1122 	shhwtstamps = skb_hwtstamps(skb);
1123 	shhwtstamps->hwtstamp = hwtstamp;
1124 
1125 	cf->can_id |= CAN_ERR_BUSERROR;
1126 	if (new_state != CAN_STATE_BUS_OFF) {
1127 		cf->can_id |= CAN_ERR_CNT;
1128 		cf->data[6] = bec.txerr;
1129 		cf->data[7] = bec.rxerr;
1130 	}
1131 
1132 	netif_rx(skb);
1133 
1134 	priv->bec.txerr = bec.txerr;
1135 	priv->bec.rxerr = bec.rxerr;
1136 }
1137 
kvaser_usb_hydra_one_shot_fail(struct kvaser_usb_net_priv * priv,const struct kvaser_cmd_ext * cmd)1138 static void kvaser_usb_hydra_one_shot_fail(struct kvaser_usb_net_priv *priv,
1139 					   const struct kvaser_cmd_ext *cmd)
1140 {
1141 	struct net_device *netdev = priv->netdev;
1142 	struct net_device_stats *stats = &netdev->stats;
1143 	struct can_frame *cf;
1144 	struct sk_buff *skb;
1145 	u32 flags;
1146 
1147 	skb = alloc_can_err_skb(netdev, &cf);
1148 	if (!skb) {
1149 		stats->rx_dropped++;
1150 		netdev_warn(netdev, "No memory left for err_skb\n");
1151 		return;
1152 	}
1153 
1154 	cf->can_id |= CAN_ERR_BUSERROR;
1155 	flags = le32_to_cpu(cmd->tx_ack.flags);
1156 
1157 	if (flags & KVASER_USB_HYDRA_CF_FLAG_OSM_NACK)
1158 		cf->can_id |= CAN_ERR_ACK;
1159 	if (flags & KVASER_USB_HYDRA_CF_FLAG_ABL) {
1160 		cf->can_id |= CAN_ERR_LOSTARB;
1161 		priv->can.can_stats.arbitration_lost++;
1162 	}
1163 
1164 	stats->tx_errors++;
1165 	netif_rx(skb);
1166 }
1167 
kvaser_usb_hydra_tx_acknowledge(const struct kvaser_usb * dev,const struct kvaser_cmd * cmd)1168 static void kvaser_usb_hydra_tx_acknowledge(const struct kvaser_usb *dev,
1169 					    const struct kvaser_cmd *cmd)
1170 {
1171 	struct kvaser_usb_tx_urb_context *context;
1172 	struct kvaser_usb_net_priv *priv;
1173 	unsigned long irq_flags;
1174 	unsigned int len;
1175 	bool one_shot_fail = false;
1176 	bool is_err_frame = false;
1177 	u16 transid = kvaser_usb_hydra_get_cmd_transid(cmd);
1178 
1179 	priv = kvaser_usb_hydra_net_priv_from_cmd(dev, cmd);
1180 	if (!priv)
1181 		return;
1182 
1183 	if (!netif_device_present(priv->netdev))
1184 		return;
1185 
1186 	if (cmd->header.cmd_no == CMD_EXTENDED) {
1187 		struct kvaser_cmd_ext *cmd_ext = (struct kvaser_cmd_ext *)cmd;
1188 		u32 flags = le32_to_cpu(cmd_ext->tx_ack.flags);
1189 
1190 		if (flags & (KVASER_USB_HYDRA_CF_FLAG_OSM_NACK |
1191 			     KVASER_USB_HYDRA_CF_FLAG_ABL)) {
1192 			kvaser_usb_hydra_one_shot_fail(priv, cmd_ext);
1193 			one_shot_fail = true;
1194 		}
1195 
1196 		is_err_frame = flags & KVASER_USB_HYDRA_CF_FLAG_TX_ACK &&
1197 			       flags & KVASER_USB_HYDRA_CF_FLAG_ERROR_FRAME;
1198 	}
1199 
1200 	context = &priv->tx_contexts[transid % dev->max_tx_urbs];
1201 
1202 	spin_lock_irqsave(&priv->tx_contexts_lock, irq_flags);
1203 
1204 	len = can_get_echo_skb(priv->netdev, context->echo_index, NULL);
1205 	context->echo_index = dev->max_tx_urbs;
1206 	--priv->active_tx_contexts;
1207 	netif_wake_queue(priv->netdev);
1208 
1209 	spin_unlock_irqrestore(&priv->tx_contexts_lock, irq_flags);
1210 
1211 	if (!one_shot_fail && !is_err_frame) {
1212 		struct net_device_stats *stats = &priv->netdev->stats;
1213 
1214 		stats->tx_packets++;
1215 		stats->tx_bytes += len;
1216 	}
1217 }
1218 
kvaser_usb_hydra_rx_msg_std(const struct kvaser_usb * dev,const struct kvaser_cmd * cmd)1219 static void kvaser_usb_hydra_rx_msg_std(const struct kvaser_usb *dev,
1220 					const struct kvaser_cmd *cmd)
1221 {
1222 	struct kvaser_usb_net_priv *priv = NULL;
1223 	struct can_frame *cf;
1224 	struct sk_buff *skb;
1225 	struct skb_shared_hwtstamps *shhwtstamps;
1226 	struct net_device_stats *stats;
1227 	u8 flags;
1228 	ktime_t hwtstamp;
1229 
1230 	priv = kvaser_usb_hydra_net_priv_from_cmd(dev, cmd);
1231 	if (!priv)
1232 		return;
1233 
1234 	stats = &priv->netdev->stats;
1235 
1236 	flags = cmd->rx_can.flags;
1237 	hwtstamp = kvaser_usb_hydra_ktime_from_rx_cmd(dev->cfg, cmd);
1238 
1239 	if (flags & KVASER_USB_HYDRA_CF_FLAG_ERROR_FRAME) {
1240 		kvaser_usb_hydra_error_frame(priv, &cmd->rx_can.err_frame_data,
1241 					     hwtstamp);
1242 		return;
1243 	}
1244 
1245 	skb = alloc_can_skb(priv->netdev, &cf);
1246 	if (!skb) {
1247 		stats->rx_dropped++;
1248 		return;
1249 	}
1250 
1251 	shhwtstamps = skb_hwtstamps(skb);
1252 	shhwtstamps->hwtstamp = hwtstamp;
1253 
1254 	cf->can_id = le32_to_cpu(cmd->rx_can.id);
1255 
1256 	if (cf->can_id &  KVASER_USB_HYDRA_EXTENDED_FRAME_ID) {
1257 		cf->can_id &= CAN_EFF_MASK;
1258 		cf->can_id |= CAN_EFF_FLAG;
1259 	} else {
1260 		cf->can_id &= CAN_SFF_MASK;
1261 	}
1262 
1263 	if (flags & KVASER_USB_HYDRA_CF_FLAG_OVERRUN)
1264 		kvaser_usb_can_rx_over_error(priv->netdev);
1265 
1266 	can_frame_set_cc_len((struct can_frame *)cf, cmd->rx_can.dlc, priv->can.ctrlmode);
1267 
1268 	if (flags & KVASER_USB_HYDRA_CF_FLAG_REMOTE_FRAME) {
1269 		cf->can_id |= CAN_RTR_FLAG;
1270 	} else {
1271 		memcpy(cf->data, cmd->rx_can.data, cf->len);
1272 
1273 		stats->rx_bytes += cf->len;
1274 	}
1275 	stats->rx_packets++;
1276 
1277 	netif_rx(skb);
1278 }
1279 
kvaser_usb_hydra_rx_msg_ext(const struct kvaser_usb * dev,const struct kvaser_cmd_ext * cmd)1280 static void kvaser_usb_hydra_rx_msg_ext(const struct kvaser_usb *dev,
1281 					const struct kvaser_cmd_ext *cmd)
1282 {
1283 	struct kvaser_cmd *std_cmd = (struct kvaser_cmd *)cmd;
1284 	struct kvaser_usb_net_priv *priv;
1285 	struct canfd_frame *cf;
1286 	struct sk_buff *skb;
1287 	struct skb_shared_hwtstamps *shhwtstamps;
1288 	struct net_device_stats *stats;
1289 	u32 flags;
1290 	u8 dlc;
1291 	u32 kcan_header;
1292 	ktime_t hwtstamp;
1293 
1294 	priv = kvaser_usb_hydra_net_priv_from_cmd(dev, std_cmd);
1295 	if (!priv)
1296 		return;
1297 
1298 	stats = &priv->netdev->stats;
1299 
1300 	kcan_header = le32_to_cpu(cmd->rx_can.kcan_header);
1301 	dlc = (kcan_header & KVASER_USB_KCAN_DATA_DLC_MASK) >>
1302 		KVASER_USB_KCAN_DATA_DLC_SHIFT;
1303 
1304 	flags = le32_to_cpu(cmd->rx_can.flags);
1305 	hwtstamp = kvaser_usb_hydra_ktime_from_rx_cmd(dev->cfg, std_cmd);
1306 
1307 	if (flags & KVASER_USB_HYDRA_CF_FLAG_ERROR_FRAME) {
1308 		kvaser_usb_hydra_error_frame(priv, &cmd->rx_can.err_frame_data,
1309 					     hwtstamp);
1310 		return;
1311 	}
1312 
1313 	if (flags & KVASER_USB_HYDRA_CF_FLAG_FDF)
1314 		skb = alloc_canfd_skb(priv->netdev, &cf);
1315 	else
1316 		skb = alloc_can_skb(priv->netdev, (struct can_frame **)&cf);
1317 
1318 	if (!skb) {
1319 		stats->rx_dropped++;
1320 		return;
1321 	}
1322 
1323 	shhwtstamps = skb_hwtstamps(skb);
1324 	shhwtstamps->hwtstamp = hwtstamp;
1325 
1326 	cf->can_id = le32_to_cpu(cmd->rx_can.id);
1327 
1328 	if (flags & KVASER_USB_HYDRA_CF_FLAG_EXTENDED_ID) {
1329 		cf->can_id &= CAN_EFF_MASK;
1330 		cf->can_id |= CAN_EFF_FLAG;
1331 	} else {
1332 		cf->can_id &= CAN_SFF_MASK;
1333 	}
1334 
1335 	if (flags & KVASER_USB_HYDRA_CF_FLAG_OVERRUN)
1336 		kvaser_usb_can_rx_over_error(priv->netdev);
1337 
1338 	if (flags & KVASER_USB_HYDRA_CF_FLAG_FDF) {
1339 		cf->len = can_fd_dlc2len(dlc);
1340 		if (flags & KVASER_USB_HYDRA_CF_FLAG_BRS)
1341 			cf->flags |= CANFD_BRS;
1342 		if (flags & KVASER_USB_HYDRA_CF_FLAG_ESI)
1343 			cf->flags |= CANFD_ESI;
1344 	} else {
1345 		can_frame_set_cc_len((struct can_frame *)cf, dlc, priv->can.ctrlmode);
1346 	}
1347 
1348 	if (flags & KVASER_USB_HYDRA_CF_FLAG_REMOTE_FRAME) {
1349 		cf->can_id |= CAN_RTR_FLAG;
1350 	} else {
1351 		memcpy(cf->data, cmd->rx_can.kcan_payload, cf->len);
1352 
1353 		stats->rx_bytes += cf->len;
1354 	}
1355 	stats->rx_packets++;
1356 
1357 	netif_rx(skb);
1358 }
1359 
kvaser_usb_hydra_handle_cmd_std(const struct kvaser_usb * dev,const struct kvaser_cmd * cmd)1360 static void kvaser_usb_hydra_handle_cmd_std(const struct kvaser_usb *dev,
1361 					    const struct kvaser_cmd *cmd)
1362 {
1363 	switch (cmd->header.cmd_no) {
1364 	case CMD_START_CHIP_RESP:
1365 		kvaser_usb_hydra_start_chip_reply(dev, cmd);
1366 		break;
1367 
1368 	case CMD_STOP_CHIP_RESP:
1369 		kvaser_usb_hydra_stop_chip_reply(dev, cmd);
1370 		break;
1371 
1372 	case CMD_FLUSH_QUEUE_RESP:
1373 		kvaser_usb_hydra_flush_queue_reply(dev, cmd);
1374 		break;
1375 
1376 	case CMD_CHIP_STATE_EVENT:
1377 		kvaser_usb_hydra_state_event(dev, cmd);
1378 		break;
1379 
1380 	case CMD_GET_BUSPARAMS_RESP:
1381 		kvaser_usb_hydra_get_busparams_reply(dev, cmd);
1382 		break;
1383 
1384 	case CMD_ERROR_EVENT:
1385 		kvaser_usb_hydra_error_event(dev, cmd);
1386 		break;
1387 
1388 	case CMD_TX_ACKNOWLEDGE:
1389 		kvaser_usb_hydra_tx_acknowledge(dev, cmd);
1390 		break;
1391 
1392 	case CMD_RX_MESSAGE:
1393 		kvaser_usb_hydra_rx_msg_std(dev, cmd);
1394 		break;
1395 
1396 	/* Ignored commands */
1397 	case CMD_SET_BUSPARAMS_RESP:
1398 	case CMD_SET_BUSPARAMS_FD_RESP:
1399 		break;
1400 
1401 	default:
1402 		dev_warn(&dev->intf->dev, "Unhandled command (%d)\n",
1403 			 cmd->header.cmd_no);
1404 		break;
1405 	}
1406 }
1407 
kvaser_usb_hydra_handle_cmd_ext(const struct kvaser_usb * dev,const struct kvaser_cmd_ext * cmd)1408 static void kvaser_usb_hydra_handle_cmd_ext(const struct kvaser_usb *dev,
1409 					    const struct kvaser_cmd_ext *cmd)
1410 {
1411 	switch (cmd->cmd_no_ext) {
1412 	case CMD_TX_ACKNOWLEDGE_FD:
1413 		kvaser_usb_hydra_tx_acknowledge(dev, (struct kvaser_cmd *)cmd);
1414 		break;
1415 
1416 	case CMD_RX_MESSAGE_FD:
1417 		kvaser_usb_hydra_rx_msg_ext(dev, cmd);
1418 		break;
1419 
1420 	default:
1421 		dev_warn(&dev->intf->dev, "Unhandled extended command (%d)\n",
1422 			 cmd->header.cmd_no);
1423 		break;
1424 	}
1425 }
1426 
kvaser_usb_hydra_handle_cmd(const struct kvaser_usb * dev,const struct kvaser_cmd * cmd)1427 static void kvaser_usb_hydra_handle_cmd(const struct kvaser_usb *dev,
1428 					const struct kvaser_cmd *cmd)
1429 {
1430 		if (cmd->header.cmd_no == CMD_EXTENDED)
1431 			kvaser_usb_hydra_handle_cmd_ext
1432 					(dev, (struct kvaser_cmd_ext *)cmd);
1433 		else
1434 			kvaser_usb_hydra_handle_cmd_std(dev, cmd);
1435 }
1436 
1437 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)1438 kvaser_usb_hydra_frame_to_cmd_ext(const struct kvaser_usb_net_priv *priv,
1439 				  const struct sk_buff *skb, int *cmd_len,
1440 				  u16 transid)
1441 {
1442 	struct kvaser_usb *dev = priv->dev;
1443 	struct kvaser_cmd_ext *cmd;
1444 	struct canfd_frame *cf = (struct canfd_frame *)skb->data;
1445 	u8 dlc;
1446 	u8 nbr_of_bytes = cf->len;
1447 	u32 flags;
1448 	u32 id;
1449 	u32 kcan_id;
1450 	u32 kcan_header;
1451 
1452 	cmd = kzalloc(sizeof(*cmd), GFP_ATOMIC);
1453 	if (!cmd)
1454 		return NULL;
1455 
1456 	kvaser_usb_hydra_set_cmd_dest_he
1457 			((struct kvaser_cmd *)cmd,
1458 			 dev->card_data.hydra.channel_to_he[priv->channel]);
1459 	kvaser_usb_hydra_set_cmd_transid((struct kvaser_cmd *)cmd, transid);
1460 
1461 	cmd->header.cmd_no = CMD_EXTENDED;
1462 	cmd->cmd_no_ext = CMD_TX_CAN_MESSAGE_FD;
1463 
1464 	*cmd_len = ALIGN(sizeof(struct kvaser_cmd_ext) -
1465 			 sizeof(cmd->tx_can.kcan_payload) + nbr_of_bytes,
1466 			 8);
1467 
1468 	cmd->len = cpu_to_le16(*cmd_len);
1469 
1470 	if (can_is_canfd_skb(skb))
1471 		dlc = can_fd_len2dlc(cf->len);
1472 	else
1473 		dlc = can_get_cc_dlc((struct can_frame *)cf, priv->can.ctrlmode);
1474 
1475 	cmd->tx_can.databytes = nbr_of_bytes;
1476 	cmd->tx_can.dlc = dlc;
1477 
1478 	if (cf->can_id & CAN_EFF_FLAG) {
1479 		id = cf->can_id & CAN_EFF_MASK;
1480 		flags = KVASER_USB_HYDRA_CF_FLAG_EXTENDED_ID;
1481 		kcan_id = (cf->can_id & CAN_EFF_MASK) |
1482 			  KVASER_USB_KCAN_DATA_IDE | KVASER_USB_KCAN_DATA_SRR;
1483 	} else {
1484 		id = cf->can_id & CAN_SFF_MASK;
1485 		flags = 0;
1486 		kcan_id = cf->can_id & CAN_SFF_MASK;
1487 	}
1488 
1489 	if (cf->can_id & CAN_ERR_FLAG)
1490 		flags |= KVASER_USB_HYDRA_CF_FLAG_ERROR_FRAME;
1491 
1492 	kcan_header = ((dlc << KVASER_USB_KCAN_DATA_DLC_SHIFT) &
1493 				KVASER_USB_KCAN_DATA_DLC_MASK) |
1494 			KVASER_USB_KCAN_DATA_AREQ |
1495 			(priv->can.ctrlmode & CAN_CTRLMODE_ONE_SHOT ?
1496 				KVASER_USB_KCAN_DATA_OSM : 0);
1497 
1498 	if (can_is_canfd_skb(skb)) {
1499 		kcan_header |= KVASER_USB_KCAN_DATA_FDF |
1500 			       (cf->flags & CANFD_BRS ?
1501 					KVASER_USB_KCAN_DATA_BRS : 0);
1502 	} else {
1503 		if (cf->can_id & CAN_RTR_FLAG) {
1504 			kcan_id |= KVASER_USB_KCAN_DATA_RTR;
1505 			cmd->tx_can.databytes = 0;
1506 			flags |= KVASER_USB_HYDRA_CF_FLAG_REMOTE_FRAME;
1507 		}
1508 	}
1509 
1510 	cmd->tx_can.kcan_id = cpu_to_le32(kcan_id);
1511 	cmd->tx_can.id = cpu_to_le32(id);
1512 	cmd->tx_can.flags = cpu_to_le32(flags);
1513 	cmd->tx_can.kcan_header = cpu_to_le32(kcan_header);
1514 
1515 	memcpy(cmd->tx_can.kcan_payload, cf->data, nbr_of_bytes);
1516 
1517 	return cmd;
1518 }
1519 
1520 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)1521 kvaser_usb_hydra_frame_to_cmd_std(const struct kvaser_usb_net_priv *priv,
1522 				  const struct sk_buff *skb, int *cmd_len,
1523 				  u16 transid)
1524 {
1525 	struct kvaser_usb *dev = priv->dev;
1526 	struct kvaser_cmd *cmd;
1527 	struct can_frame *cf = (struct can_frame *)skb->data;
1528 	u32 flags;
1529 	u32 id;
1530 
1531 	cmd = kzalloc(sizeof(*cmd), GFP_ATOMIC);
1532 	if (!cmd)
1533 		return NULL;
1534 
1535 	kvaser_usb_hydra_set_cmd_dest_he
1536 		(cmd, dev->card_data.hydra.channel_to_he[priv->channel]);
1537 	kvaser_usb_hydra_set_cmd_transid(cmd, transid);
1538 
1539 	cmd->header.cmd_no = CMD_TX_CAN_MESSAGE;
1540 
1541 	*cmd_len = ALIGN(sizeof(struct kvaser_cmd), 8);
1542 
1543 	if (cf->can_id & CAN_EFF_FLAG) {
1544 		id = (cf->can_id & CAN_EFF_MASK);
1545 		id |= KVASER_USB_HYDRA_EXTENDED_FRAME_ID;
1546 	} else {
1547 		id = cf->can_id & CAN_SFF_MASK;
1548 	}
1549 
1550 	cmd->tx_can.dlc = can_get_cc_dlc(cf, priv->can.ctrlmode);
1551 
1552 	flags = (cf->can_id & CAN_EFF_FLAG ?
1553 		 KVASER_USB_HYDRA_CF_FLAG_EXTENDED_ID : 0);
1554 
1555 	if (cf->can_id & CAN_RTR_FLAG)
1556 		flags |= KVASER_USB_HYDRA_CF_FLAG_REMOTE_FRAME;
1557 
1558 	flags |= (cf->can_id & CAN_ERR_FLAG ?
1559 		  KVASER_USB_HYDRA_CF_FLAG_ERROR_FRAME : 0);
1560 
1561 	cmd->tx_can.id = cpu_to_le32(id);
1562 	cmd->tx_can.flags = flags;
1563 
1564 	memcpy(cmd->tx_can.data, cf->data, cf->len);
1565 
1566 	return cmd;
1567 }
1568 
kvaser_usb_hydra_set_mode(struct net_device * netdev,enum can_mode mode)1569 static int kvaser_usb_hydra_set_mode(struct net_device *netdev,
1570 				     enum can_mode mode)
1571 {
1572 	int err = 0;
1573 
1574 	switch (mode) {
1575 	case CAN_MODE_START:
1576 		/* CAN controller automatically recovers from BUS_OFF */
1577 		break;
1578 	default:
1579 		err = -EOPNOTSUPP;
1580 	}
1581 
1582 	return err;
1583 }
1584 
kvaser_usb_hydra_get_busparams(struct kvaser_usb_net_priv * priv,int busparams_type)1585 static int kvaser_usb_hydra_get_busparams(struct kvaser_usb_net_priv *priv,
1586 					  int busparams_type)
1587 {
1588 	struct kvaser_usb *dev = priv->dev;
1589 	struct kvaser_usb_net_hydra_priv *hydra = priv->sub_priv;
1590 	struct kvaser_cmd *cmd;
1591 	size_t cmd_len;
1592 	int err;
1593 
1594 	if (!hydra)
1595 		return -EINVAL;
1596 
1597 	cmd = kcalloc(1, sizeof(struct kvaser_cmd), GFP_KERNEL);
1598 	if (!cmd)
1599 		return -ENOMEM;
1600 
1601 	cmd->header.cmd_no = CMD_GET_BUSPARAMS_REQ;
1602 	cmd_len = kvaser_usb_hydra_cmd_size(cmd);
1603 	kvaser_usb_hydra_set_cmd_dest_he
1604 		(cmd, dev->card_data.hydra.channel_to_he[priv->channel]);
1605 	kvaser_usb_hydra_set_cmd_transid
1606 				(cmd, kvaser_usb_hydra_get_next_transid(dev));
1607 	cmd->get_busparams_req.type = busparams_type;
1608 	hydra->pending_get_busparams_type = busparams_type;
1609 
1610 	reinit_completion(&priv->get_busparams_comp);
1611 
1612 	err = kvaser_usb_send_cmd(dev, cmd, cmd_len);
1613 	if (err)
1614 		return err;
1615 
1616 	if (!wait_for_completion_timeout(&priv->get_busparams_comp,
1617 					 msecs_to_jiffies(KVASER_USB_TIMEOUT)))
1618 		return -ETIMEDOUT;
1619 
1620 	return err;
1621 }
1622 
kvaser_usb_hydra_get_nominal_busparams(struct kvaser_usb_net_priv * priv)1623 static int kvaser_usb_hydra_get_nominal_busparams(struct kvaser_usb_net_priv *priv)
1624 {
1625 	return kvaser_usb_hydra_get_busparams(priv, KVASER_USB_HYDRA_BUSPARAM_TYPE_CAN);
1626 }
1627 
kvaser_usb_hydra_get_data_busparams(struct kvaser_usb_net_priv * priv)1628 static int kvaser_usb_hydra_get_data_busparams(struct kvaser_usb_net_priv *priv)
1629 {
1630 	return kvaser_usb_hydra_get_busparams(priv, KVASER_USB_HYDRA_BUSPARAM_TYPE_CANFD);
1631 }
1632 
kvaser_usb_hydra_set_bittiming(const struct net_device * netdev,const struct kvaser_usb_busparams * busparams)1633 static int kvaser_usb_hydra_set_bittiming(const struct net_device *netdev,
1634 					  const struct kvaser_usb_busparams *busparams)
1635 {
1636 	struct kvaser_cmd *cmd;
1637 	struct kvaser_usb_net_priv *priv = netdev_priv(netdev);
1638 	struct kvaser_usb *dev = priv->dev;
1639 	size_t cmd_len;
1640 	int err;
1641 
1642 	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1643 	if (!cmd)
1644 		return -ENOMEM;
1645 
1646 	cmd->header.cmd_no = CMD_SET_BUSPARAMS_REQ;
1647 	cmd_len = kvaser_usb_hydra_cmd_size(cmd);
1648 	memcpy(&cmd->set_busparams_req.busparams_nominal, busparams,
1649 	       sizeof(cmd->set_busparams_req.busparams_nominal));
1650 
1651 	kvaser_usb_hydra_set_cmd_dest_he
1652 		(cmd, dev->card_data.hydra.channel_to_he[priv->channel]);
1653 	kvaser_usb_hydra_set_cmd_transid
1654 				(cmd, kvaser_usb_hydra_get_next_transid(dev));
1655 
1656 	err = kvaser_usb_send_cmd(dev, cmd, cmd_len);
1657 
1658 	kfree(cmd);
1659 
1660 	return err;
1661 }
1662 
kvaser_usb_hydra_set_data_bittiming(const struct net_device * netdev,const struct kvaser_usb_busparams * busparams)1663 static int kvaser_usb_hydra_set_data_bittiming(const struct net_device *netdev,
1664 					       const struct kvaser_usb_busparams *busparams)
1665 {
1666 	struct kvaser_cmd *cmd;
1667 	struct kvaser_usb_net_priv *priv = netdev_priv(netdev);
1668 	struct kvaser_usb *dev = priv->dev;
1669 	size_t cmd_len;
1670 	int err;
1671 
1672 	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1673 	if (!cmd)
1674 		return -ENOMEM;
1675 
1676 	cmd->header.cmd_no = CMD_SET_BUSPARAMS_FD_REQ;
1677 	cmd_len = kvaser_usb_hydra_cmd_size(cmd);
1678 	memcpy(&cmd->set_busparams_req.busparams_data, busparams,
1679 	       sizeof(cmd->set_busparams_req.busparams_data));
1680 
1681 	if (priv->can.ctrlmode & CAN_CTRLMODE_FD) {
1682 		if (priv->can.ctrlmode & CAN_CTRLMODE_FD_NON_ISO)
1683 			cmd->set_busparams_req.canfd_mode =
1684 					KVASER_USB_HYDRA_BUS_MODE_NONISO;
1685 		else
1686 			cmd->set_busparams_req.canfd_mode =
1687 					KVASER_USB_HYDRA_BUS_MODE_CANFD_ISO;
1688 	}
1689 
1690 	kvaser_usb_hydra_set_cmd_dest_he
1691 		(cmd, dev->card_data.hydra.channel_to_he[priv->channel]);
1692 	kvaser_usb_hydra_set_cmd_transid
1693 				(cmd, kvaser_usb_hydra_get_next_transid(dev));
1694 
1695 	err = kvaser_usb_send_cmd(dev, cmd, cmd_len);
1696 
1697 	kfree(cmd);
1698 
1699 	return err;
1700 }
1701 
kvaser_usb_hydra_get_berr_counter(const struct net_device * netdev,struct can_berr_counter * bec)1702 static int kvaser_usb_hydra_get_berr_counter(const struct net_device *netdev,
1703 					     struct can_berr_counter *bec)
1704 {
1705 	struct kvaser_usb_net_priv *priv = netdev_priv(netdev);
1706 	int err;
1707 
1708 	err = kvaser_usb_hydra_send_simple_cmd(priv->dev,
1709 					       CMD_GET_CHIP_STATE_REQ,
1710 					       priv->channel);
1711 	if (err)
1712 		return err;
1713 
1714 	*bec = priv->bec;
1715 
1716 	return 0;
1717 }
1718 
kvaser_usb_hydra_setup_endpoints(struct kvaser_usb * dev)1719 static int kvaser_usb_hydra_setup_endpoints(struct kvaser_usb *dev)
1720 {
1721 	const struct usb_host_interface *iface_desc;
1722 	struct usb_endpoint_descriptor *ep;
1723 	int i;
1724 
1725 	iface_desc = dev->intf->cur_altsetting;
1726 
1727 	for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
1728 		ep = &iface_desc->endpoint[i].desc;
1729 
1730 		if (!dev->bulk_in && usb_endpoint_is_bulk_in(ep) &&
1731 		    ep->bEndpointAddress == KVASER_USB_HYDRA_BULK_EP_IN_ADDR)
1732 			dev->bulk_in = ep;
1733 
1734 		if (!dev->bulk_out && usb_endpoint_is_bulk_out(ep) &&
1735 		    ep->bEndpointAddress == KVASER_USB_HYDRA_BULK_EP_OUT_ADDR)
1736 			dev->bulk_out = ep;
1737 
1738 		if (dev->bulk_in && dev->bulk_out)
1739 			return 0;
1740 	}
1741 
1742 	return -ENODEV;
1743 }
1744 
kvaser_usb_hydra_init_card(struct kvaser_usb * dev)1745 static int kvaser_usb_hydra_init_card(struct kvaser_usb *dev)
1746 {
1747 	int err;
1748 	unsigned int i;
1749 	struct kvaser_usb_dev_card_data_hydra *card_data =
1750 							&dev->card_data.hydra;
1751 
1752 	card_data->transid = KVASER_USB_HYDRA_MIN_TRANSID;
1753 	spin_lock_init(&card_data->transid_lock);
1754 
1755 	memset(card_data->usb_rx_leftover, 0, KVASER_USB_HYDRA_MAX_CMD_LEN);
1756 	card_data->usb_rx_leftover_len = 0;
1757 	spin_lock_init(&card_data->usb_rx_leftover_lock);
1758 
1759 	memset(card_data->channel_to_he, KVASER_USB_HYDRA_HE_ADDRESS_ILLEGAL,
1760 	       sizeof(card_data->channel_to_he));
1761 	card_data->sysdbg_he = 0;
1762 
1763 	for (i = 0; i < KVASER_USB_MAX_NET_DEVICES; i++) {
1764 		err = kvaser_usb_hydra_map_channel
1765 					(dev,
1766 					 (KVASER_USB_HYDRA_TRANSID_CANHE | i),
1767 					 i, "CAN");
1768 		if (err) {
1769 			dev_err(&dev->intf->dev,
1770 				"CMD_MAP_CHANNEL_REQ failed for CAN%u\n", i);
1771 			return err;
1772 		}
1773 	}
1774 
1775 	err = kvaser_usb_hydra_map_channel(dev, KVASER_USB_HYDRA_TRANSID_SYSDBG,
1776 					   0, "SYSDBG");
1777 	if (err) {
1778 		dev_err(&dev->intf->dev,
1779 			"CMD_MAP_CHANNEL_REQ failed for SYSDBG\n");
1780 		return err;
1781 	}
1782 
1783 	return 0;
1784 }
1785 
kvaser_usb_hydra_init_channel(struct kvaser_usb_net_priv * priv)1786 static int kvaser_usb_hydra_init_channel(struct kvaser_usb_net_priv *priv)
1787 {
1788 	struct kvaser_usb_net_hydra_priv *hydra;
1789 
1790 	hydra = devm_kzalloc(&priv->dev->intf->dev, sizeof(*hydra), GFP_KERNEL);
1791 	if (!hydra)
1792 		return -ENOMEM;
1793 
1794 	priv->sub_priv = hydra;
1795 
1796 	return 0;
1797 }
1798 
kvaser_usb_hydra_get_software_info(struct kvaser_usb * dev)1799 static int kvaser_usb_hydra_get_software_info(struct kvaser_usb *dev)
1800 {
1801 	struct kvaser_cmd cmd;
1802 	int err;
1803 
1804 	err = kvaser_usb_hydra_send_simple_cmd(dev, CMD_GET_SOFTWARE_INFO_REQ,
1805 					       -1);
1806 	if (err)
1807 		return err;
1808 
1809 	memset(&cmd, 0, sizeof(struct kvaser_cmd));
1810 	err = kvaser_usb_hydra_wait_cmd(dev, CMD_GET_SOFTWARE_INFO_RESP, &cmd);
1811 	if (err)
1812 		return err;
1813 
1814 	dev->max_tx_urbs = min_t(unsigned int, KVASER_USB_MAX_TX_URBS,
1815 				 le16_to_cpu(cmd.sw_info.max_outstanding_tx));
1816 
1817 	return 0;
1818 }
1819 
kvaser_usb_hydra_get_software_details(struct kvaser_usb * dev)1820 static int kvaser_usb_hydra_get_software_details(struct kvaser_usb *dev)
1821 {
1822 	struct kvaser_cmd *cmd;
1823 	size_t cmd_len;
1824 	int err;
1825 	u32 flags;
1826 	struct kvaser_usb_dev_card_data *card_data = &dev->card_data;
1827 
1828 	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1829 	if (!cmd)
1830 		return -ENOMEM;
1831 
1832 	cmd->header.cmd_no = CMD_GET_SOFTWARE_DETAILS_REQ;
1833 	cmd_len = kvaser_usb_hydra_cmd_size(cmd);
1834 	cmd->sw_detail_req.use_ext_cmd = 1;
1835 	kvaser_usb_hydra_set_cmd_dest_he
1836 				(cmd, KVASER_USB_HYDRA_HE_ADDRESS_ILLEGAL);
1837 
1838 	kvaser_usb_hydra_set_cmd_transid
1839 				(cmd, kvaser_usb_hydra_get_next_transid(dev));
1840 
1841 	err = kvaser_usb_send_cmd(dev, cmd, cmd_len);
1842 	if (err)
1843 		goto end;
1844 
1845 	err = kvaser_usb_hydra_wait_cmd(dev, CMD_GET_SOFTWARE_DETAILS_RESP,
1846 					cmd);
1847 	if (err)
1848 		goto end;
1849 
1850 	dev->fw_version = le32_to_cpu(cmd->sw_detail_res.sw_version);
1851 	flags = le32_to_cpu(cmd->sw_detail_res.sw_flags);
1852 
1853 	if (flags & KVASER_USB_HYDRA_SW_FLAG_FW_BAD) {
1854 		dev_err(&dev->intf->dev,
1855 			"Bad firmware, device refuse to run!\n");
1856 		err = -EINVAL;
1857 		goto end;
1858 	}
1859 
1860 	if (flags & KVASER_USB_HYDRA_SW_FLAG_FW_BETA)
1861 		dev_info(&dev->intf->dev, "Beta firmware in use\n");
1862 
1863 	if (flags & KVASER_USB_HYDRA_SW_FLAG_EXT_CAP)
1864 		card_data->capabilities |= KVASER_USB_CAP_EXT_CAP;
1865 
1866 	if (flags & KVASER_USB_HYDRA_SW_FLAG_EXT_CMD)
1867 		card_data->capabilities |= KVASER_USB_HYDRA_CAP_EXT_CMD;
1868 
1869 	if (flags & KVASER_USB_HYDRA_SW_FLAG_CANFD)
1870 		card_data->ctrlmode_supported |= CAN_CTRLMODE_FD;
1871 
1872 	if (flags & KVASER_USB_HYDRA_SW_FLAG_NONISO)
1873 		card_data->ctrlmode_supported |= CAN_CTRLMODE_FD_NON_ISO;
1874 
1875 	if (flags &  KVASER_USB_HYDRA_SW_FLAG_FREQ_80M)
1876 		dev->cfg = &kvaser_usb_hydra_dev_cfg_kcan;
1877 	else if (flags & KVASER_USB_HYDRA_SW_FLAG_CAN_FREQ_80M)
1878 		dev->cfg = &kvaser_usb_hydra_dev_cfg_rt;
1879 	else
1880 		dev->cfg = &kvaser_usb_hydra_dev_cfg_flexc;
1881 
1882 end:
1883 	kfree(cmd);
1884 
1885 	return err;
1886 }
1887 
kvaser_usb_hydra_get_card_info(struct kvaser_usb * dev)1888 static int kvaser_usb_hydra_get_card_info(struct kvaser_usb *dev)
1889 {
1890 	struct kvaser_cmd cmd;
1891 	int err;
1892 
1893 	err = kvaser_usb_hydra_send_simple_cmd(dev, CMD_GET_CARD_INFO_REQ, -1);
1894 	if (err)
1895 		return err;
1896 
1897 	memset(&cmd, 0, sizeof(struct kvaser_cmd));
1898 	err = kvaser_usb_hydra_wait_cmd(dev, CMD_GET_CARD_INFO_RESP, &cmd);
1899 	if (err)
1900 		return err;
1901 
1902 	dev->nchannels = cmd.card_info.nchannels;
1903 	if (dev->nchannels > KVASER_USB_MAX_NET_DEVICES)
1904 		return -EINVAL;
1905 
1906 	return 0;
1907 }
1908 
kvaser_usb_hydra_get_capabilities(struct kvaser_usb * dev)1909 static int kvaser_usb_hydra_get_capabilities(struct kvaser_usb *dev)
1910 {
1911 	int err;
1912 	u16 status;
1913 
1914 	if (!(dev->card_data.capabilities & KVASER_USB_CAP_EXT_CAP)) {
1915 		dev_info(&dev->intf->dev,
1916 			 "No extended capability support. Upgrade your device.\n");
1917 		return 0;
1918 	}
1919 
1920 	err = kvaser_usb_hydra_get_single_capability
1921 					(dev,
1922 					 KVASER_USB_HYDRA_CAP_CMD_LISTEN_MODE,
1923 					 &status);
1924 	if (err)
1925 		return err;
1926 	if (status)
1927 		dev_info(&dev->intf->dev,
1928 			 "KVASER_USB_HYDRA_CAP_CMD_LISTEN_MODE failed %u\n",
1929 			 status);
1930 
1931 	err = kvaser_usb_hydra_get_single_capability
1932 					(dev,
1933 					 KVASER_USB_HYDRA_CAP_CMD_ERR_REPORT,
1934 					 &status);
1935 	if (err)
1936 		return err;
1937 	if (status)
1938 		dev_info(&dev->intf->dev,
1939 			 "KVASER_USB_HYDRA_CAP_CMD_ERR_REPORT failed %u\n",
1940 			 status);
1941 
1942 	err = kvaser_usb_hydra_get_single_capability
1943 					(dev, KVASER_USB_HYDRA_CAP_CMD_ONE_SHOT,
1944 					 &status);
1945 	if (err)
1946 		return err;
1947 	if (status)
1948 		dev_info(&dev->intf->dev,
1949 			 "KVASER_USB_HYDRA_CAP_CMD_ONE_SHOT failed %u\n",
1950 			 status);
1951 
1952 	return 0;
1953 }
1954 
kvaser_usb_hydra_set_opt_mode(const struct kvaser_usb_net_priv * priv)1955 static int kvaser_usb_hydra_set_opt_mode(const struct kvaser_usb_net_priv *priv)
1956 {
1957 	struct kvaser_usb *dev = priv->dev;
1958 	struct kvaser_cmd *cmd;
1959 	size_t cmd_len;
1960 	int err;
1961 
1962 	if ((priv->can.ctrlmode &
1963 	    (CAN_CTRLMODE_FD | CAN_CTRLMODE_FD_NON_ISO)) ==
1964 	    CAN_CTRLMODE_FD_NON_ISO) {
1965 		netdev_warn(priv->netdev,
1966 			    "CTRLMODE_FD shall be on if CTRLMODE_FD_NON_ISO is on\n");
1967 		return -EINVAL;
1968 	}
1969 
1970 	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1971 	if (!cmd)
1972 		return -ENOMEM;
1973 
1974 	cmd->header.cmd_no = CMD_SET_DRIVERMODE_REQ;
1975 	cmd_len = kvaser_usb_hydra_cmd_size(cmd);
1976 	kvaser_usb_hydra_set_cmd_dest_he
1977 		(cmd, dev->card_data.hydra.channel_to_he[priv->channel]);
1978 	kvaser_usb_hydra_set_cmd_transid
1979 				(cmd, kvaser_usb_hydra_get_next_transid(dev));
1980 	if (priv->can.ctrlmode & CAN_CTRLMODE_LISTENONLY)
1981 		cmd->set_ctrlmode.mode = KVASER_USB_HYDRA_CTRLMODE_LISTEN;
1982 	else
1983 		cmd->set_ctrlmode.mode = KVASER_USB_HYDRA_CTRLMODE_NORMAL;
1984 
1985 	err = kvaser_usb_send_cmd(dev, cmd, cmd_len);
1986 	kfree(cmd);
1987 
1988 	return err;
1989 }
1990 
kvaser_usb_hydra_start_chip(struct kvaser_usb_net_priv * priv)1991 static int kvaser_usb_hydra_start_chip(struct kvaser_usb_net_priv *priv)
1992 {
1993 	int err;
1994 
1995 	reinit_completion(&priv->start_comp);
1996 
1997 	err = kvaser_usb_hydra_send_simple_cmd(priv->dev, CMD_START_CHIP_REQ,
1998 					       priv->channel);
1999 	if (err)
2000 		return err;
2001 
2002 	if (!wait_for_completion_timeout(&priv->start_comp,
2003 					 msecs_to_jiffies(KVASER_USB_TIMEOUT)))
2004 		return -ETIMEDOUT;
2005 
2006 	return 0;
2007 }
2008 
kvaser_usb_hydra_stop_chip(struct kvaser_usb_net_priv * priv)2009 static int kvaser_usb_hydra_stop_chip(struct kvaser_usb_net_priv *priv)
2010 {
2011 	int err;
2012 
2013 	reinit_completion(&priv->stop_comp);
2014 
2015 	/* Make sure we do not report invalid BUS_OFF from CMD_CHIP_STATE_EVENT
2016 	 * see comment in kvaser_usb_hydra_update_state()
2017 	 */
2018 	priv->can.state = CAN_STATE_STOPPED;
2019 
2020 	err = kvaser_usb_hydra_send_simple_cmd(priv->dev, CMD_STOP_CHIP_REQ,
2021 					       priv->channel);
2022 	if (err)
2023 		return err;
2024 
2025 	if (!wait_for_completion_timeout(&priv->stop_comp,
2026 					 msecs_to_jiffies(KVASER_USB_TIMEOUT)))
2027 		return -ETIMEDOUT;
2028 
2029 	return 0;
2030 }
2031 
kvaser_usb_hydra_flush_queue(struct kvaser_usb_net_priv * priv)2032 static int kvaser_usb_hydra_flush_queue(struct kvaser_usb_net_priv *priv)
2033 {
2034 	int err;
2035 
2036 	reinit_completion(&priv->flush_comp);
2037 
2038 	err = kvaser_usb_hydra_send_simple_cmd(priv->dev, CMD_FLUSH_QUEUE,
2039 					       priv->channel);
2040 	if (err)
2041 		return err;
2042 
2043 	if (!wait_for_completion_timeout(&priv->flush_comp,
2044 					 msecs_to_jiffies(KVASER_USB_TIMEOUT)))
2045 		return -ETIMEDOUT;
2046 
2047 	return 0;
2048 }
2049 
2050 /* A single extended hydra command can be transmitted in multiple transfers
2051  * We have to buffer partial hydra commands, and handle them on next callback.
2052  */
kvaser_usb_hydra_read_bulk_callback(struct kvaser_usb * dev,void * buf,int len)2053 static void kvaser_usb_hydra_read_bulk_callback(struct kvaser_usb *dev,
2054 						void *buf, int len)
2055 {
2056 	unsigned long irq_flags;
2057 	struct kvaser_cmd *cmd;
2058 	int pos = 0;
2059 	size_t cmd_len;
2060 	struct kvaser_usb_dev_card_data_hydra *card_data =
2061 							&dev->card_data.hydra;
2062 	int usb_rx_leftover_len;
2063 	spinlock_t *usb_rx_leftover_lock = &card_data->usb_rx_leftover_lock;
2064 
2065 	spin_lock_irqsave(usb_rx_leftover_lock, irq_flags);
2066 	usb_rx_leftover_len = card_data->usb_rx_leftover_len;
2067 	if (usb_rx_leftover_len) {
2068 		int remaining_bytes;
2069 
2070 		cmd = (struct kvaser_cmd *)card_data->usb_rx_leftover;
2071 
2072 		cmd_len = kvaser_usb_hydra_cmd_size(cmd);
2073 
2074 		remaining_bytes = min_t(unsigned int, len,
2075 					cmd_len - usb_rx_leftover_len);
2076 		/* Make sure we do not overflow usb_rx_leftover */
2077 		if (remaining_bytes + usb_rx_leftover_len >
2078 						KVASER_USB_HYDRA_MAX_CMD_LEN) {
2079 			dev_err(&dev->intf->dev, "Format error\n");
2080 			spin_unlock_irqrestore(usb_rx_leftover_lock, irq_flags);
2081 			return;
2082 		}
2083 
2084 		memcpy(card_data->usb_rx_leftover + usb_rx_leftover_len, buf,
2085 		       remaining_bytes);
2086 		pos += remaining_bytes;
2087 
2088 		if (remaining_bytes + usb_rx_leftover_len == cmd_len) {
2089 			kvaser_usb_hydra_handle_cmd(dev, cmd);
2090 			usb_rx_leftover_len = 0;
2091 		} else {
2092 			/* Command still not complete */
2093 			usb_rx_leftover_len += remaining_bytes;
2094 		}
2095 		card_data->usb_rx_leftover_len = usb_rx_leftover_len;
2096 	}
2097 	spin_unlock_irqrestore(usb_rx_leftover_lock, irq_flags);
2098 
2099 	while (pos < len) {
2100 		cmd = buf + pos;
2101 
2102 		cmd_len = kvaser_usb_hydra_cmd_size(cmd);
2103 
2104 		if (pos + cmd_len > len) {
2105 			/* We got first part of a command */
2106 			int leftover_bytes;
2107 
2108 			leftover_bytes = len - pos;
2109 			/* Make sure we do not overflow usb_rx_leftover */
2110 			if (leftover_bytes > KVASER_USB_HYDRA_MAX_CMD_LEN) {
2111 				dev_err(&dev->intf->dev, "Format error\n");
2112 				return;
2113 			}
2114 			spin_lock_irqsave(usb_rx_leftover_lock, irq_flags);
2115 			memcpy(card_data->usb_rx_leftover, buf + pos,
2116 			       leftover_bytes);
2117 			card_data->usb_rx_leftover_len = leftover_bytes;
2118 			spin_unlock_irqrestore(usb_rx_leftover_lock, irq_flags);
2119 			break;
2120 		}
2121 
2122 		kvaser_usb_hydra_handle_cmd(dev, cmd);
2123 		pos += cmd_len;
2124 	}
2125 }
2126 
2127 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)2128 kvaser_usb_hydra_frame_to_cmd(const struct kvaser_usb_net_priv *priv,
2129 			      const struct sk_buff *skb, int *cmd_len,
2130 			      u16 transid)
2131 {
2132 	void *buf;
2133 
2134 	if (priv->dev->card_data.capabilities & KVASER_USB_HYDRA_CAP_EXT_CMD)
2135 		buf = kvaser_usb_hydra_frame_to_cmd_ext(priv, skb, cmd_len,
2136 							transid);
2137 	else
2138 		buf = kvaser_usb_hydra_frame_to_cmd_std(priv, skb, cmd_len,
2139 							transid);
2140 
2141 	return buf;
2142 }
2143 
2144 const struct kvaser_usb_dev_ops kvaser_usb_hydra_dev_ops = {
2145 	.dev_set_mode = kvaser_usb_hydra_set_mode,
2146 	.dev_set_bittiming = kvaser_usb_hydra_set_bittiming,
2147 	.dev_get_busparams = kvaser_usb_hydra_get_nominal_busparams,
2148 	.dev_set_data_bittiming = kvaser_usb_hydra_set_data_bittiming,
2149 	.dev_get_data_busparams = kvaser_usb_hydra_get_data_busparams,
2150 	.dev_get_berr_counter = kvaser_usb_hydra_get_berr_counter,
2151 	.dev_setup_endpoints = kvaser_usb_hydra_setup_endpoints,
2152 	.dev_init_card = kvaser_usb_hydra_init_card,
2153 	.dev_init_channel = kvaser_usb_hydra_init_channel,
2154 	.dev_get_software_info = kvaser_usb_hydra_get_software_info,
2155 	.dev_get_software_details = kvaser_usb_hydra_get_software_details,
2156 	.dev_get_card_info = kvaser_usb_hydra_get_card_info,
2157 	.dev_get_capabilities = kvaser_usb_hydra_get_capabilities,
2158 	.dev_set_opt_mode = kvaser_usb_hydra_set_opt_mode,
2159 	.dev_start_chip = kvaser_usb_hydra_start_chip,
2160 	.dev_stop_chip = kvaser_usb_hydra_stop_chip,
2161 	.dev_reset_chip = NULL,
2162 	.dev_flush_queue = kvaser_usb_hydra_flush_queue,
2163 	.dev_read_bulk_callback = kvaser_usb_hydra_read_bulk_callback,
2164 	.dev_frame_to_cmd = kvaser_usb_hydra_frame_to_cmd,
2165 };
2166 
2167 static const struct kvaser_usb_dev_cfg kvaser_usb_hydra_dev_cfg_kcan = {
2168 	.clock = {
2169 		.freq = 80 * MEGA /* Hz */,
2170 	},
2171 	.timestamp_freq = 80,
2172 	.bittiming_const = &kvaser_usb_hydra_kcan_bittiming_c,
2173 	.data_bittiming_const = &kvaser_usb_hydra_kcan_bittiming_c,
2174 };
2175 
2176 static const struct kvaser_usb_dev_cfg kvaser_usb_hydra_dev_cfg_flexc = {
2177 	.clock = {
2178 		.freq = 24 * MEGA /* Hz */,
2179 	},
2180 	.timestamp_freq = 1,
2181 	.bittiming_const = &kvaser_usb_flexc_bittiming_const,
2182 };
2183 
2184 static const struct kvaser_usb_dev_cfg kvaser_usb_hydra_dev_cfg_rt = {
2185 	.clock = {
2186 		.freq = 80 * MEGA /* Hz */,
2187 	},
2188 	.timestamp_freq = 24,
2189 	.bittiming_const = &kvaser_usb_hydra_rt_bittiming_c,
2190 	.data_bittiming_const = &kvaser_usb_hydra_rtd_bittiming_c,
2191 };
2192