xref: /linux/drivers/net/can/usb/kvaser_usb/kvaser_usb_leaf.c (revision 8be4d31cb8aaeea27bde4b7ddb26e28a89062ebf)
1 // SPDX-License-Identifier: GPL-2.0
2 /* Parts of this driver are based on the following:
3  *  - Kvaser linux leaf driver (version 4.78)
4  *  - CAN driver for esd CAN-USB/2
5  *  - Kvaser linux usbcanII driver (version 5.3)
6  *
7  * Copyright (C) 2002-2018 KVASER AB, Sweden. All rights reserved.
8  * Copyright (C) 2010 Matthias Fuchs <matthias.fuchs@esd.eu>, esd gmbh
9  * Copyright (C) 2012 Olivier Sobrie <olivier@sobrie.be>
10  * Copyright (C) 2015 Valeo S.A.
11  */
12 
13 #include <linux/bitfield.h>
14 #include <linux/completion.h>
15 #include <linux/device.h>
16 #include <linux/gfp.h>
17 #include <linux/jiffies.h>
18 #include <linux/kernel.h>
19 #include <linux/netdevice.h>
20 #include <linux/spinlock.h>
21 #include <linux/string.h>
22 #include <linux/types.h>
23 #include <linux/units.h>
24 #include <linux/usb.h>
25 #include <linux/workqueue.h>
26 
27 #include <linux/can.h>
28 #include <linux/can/dev.h>
29 #include <linux/can/error.h>
30 #include <linux/can/netlink.h>
31 
32 #include "kvaser_usb.h"
33 
34 #define MAX_USBCAN_NET_DEVICES		2
35 
36 /* Command header size */
37 #define CMD_HEADER_LEN			2
38 
39 /* Kvaser CAN message flags */
40 #define MSG_FLAG_ERROR_FRAME		BIT(0)
41 #define MSG_FLAG_OVERRUN		BIT(1)
42 #define MSG_FLAG_NERR			BIT(2)
43 #define MSG_FLAG_WAKEUP			BIT(3)
44 #define MSG_FLAG_REMOTE_FRAME		BIT(4)
45 #define MSG_FLAG_RESERVED		BIT(5)
46 #define MSG_FLAG_TX_ACK			BIT(6)
47 #define MSG_FLAG_TX_REQUEST		BIT(7)
48 
49 /* CAN states (M16C CxSTRH register) */
50 #define M16C_STATE_BUS_RESET		BIT(0)
51 #define M16C_STATE_BUS_ERROR		BIT(4)
52 #define M16C_STATE_BUS_PASSIVE		BIT(5)
53 #define M16C_STATE_BUS_OFF		BIT(6)
54 
55 /* Leaf/usbcan command ids */
56 #define CMD_RX_STD_MESSAGE		12
57 #define CMD_TX_STD_MESSAGE		13
58 #define CMD_RX_EXT_MESSAGE		14
59 #define CMD_TX_EXT_MESSAGE		15
60 #define CMD_SET_BUS_PARAMS		16
61 #define CMD_GET_BUS_PARAMS		17
62 #define CMD_GET_BUS_PARAMS_REPLY	18
63 #define CMD_GET_CHIP_STATE		19
64 #define CMD_CHIP_STATE_EVENT		20
65 #define CMD_SET_CTRL_MODE		21
66 #define CMD_RESET_CHIP			24
67 #define CMD_START_CHIP			26
68 #define CMD_START_CHIP_REPLY		27
69 #define CMD_STOP_CHIP			28
70 #define CMD_STOP_CHIP_REPLY		29
71 
72 #define CMD_USBCAN_CLOCK_OVERFLOW_EVENT	33
73 
74 #define CMD_GET_CARD_INFO		34
75 #define CMD_GET_CARD_INFO_REPLY		35
76 #define CMD_GET_SOFTWARE_INFO		38
77 #define CMD_GET_SOFTWARE_INFO_REPLY	39
78 #define CMD_ERROR_EVENT			45
79 #define CMD_FLUSH_QUEUE			48
80 #define CMD_TX_ACKNOWLEDGE		50
81 #define CMD_CAN_ERROR_EVENT		51
82 #define CMD_FLUSH_QUEUE_REPLY		68
83 #define CMD_GET_CAPABILITIES_REQ	95
84 #define CMD_GET_CAPABILITIES_RESP	96
85 #define CMD_LED_ACTION_REQ		101
86 #define CMD_LED_ACTION_RESP		102
87 
88 #define CMD_LEAF_LOG_MESSAGE		106
89 
90 /* Leaf frequency options */
91 #define KVASER_USB_LEAF_SWOPTION_FREQ_MASK 0x60
92 #define KVASER_USB_LEAF_SWOPTION_FREQ_16_MHZ_CLK 0
93 #define KVASER_USB_LEAF_SWOPTION_FREQ_32_MHZ_CLK BIT(5)
94 #define KVASER_USB_LEAF_SWOPTION_FREQ_24_MHZ_CLK BIT(6)
95 
96 #define KVASER_USB_LEAF_SWOPTION_EXT_CAP BIT(12)
97 
98 /* error factors */
99 #define M16C_EF_ACKE			BIT(0)
100 #define M16C_EF_CRCE			BIT(1)
101 #define M16C_EF_FORME			BIT(2)
102 #define M16C_EF_STFE			BIT(3)
103 #define M16C_EF_BITE0			BIT(4)
104 #define M16C_EF_BITE1			BIT(5)
105 #define M16C_EF_RCVE			BIT(6)
106 #define M16C_EF_TRE			BIT(7)
107 
108 /* Only Leaf-based devices can report M16C error factors,
109  * thus define our own error status flags for USBCANII
110  */
111 #define USBCAN_ERROR_STATE_NONE		0
112 #define USBCAN_ERROR_STATE_TX_ERROR	BIT(0)
113 #define USBCAN_ERROR_STATE_RX_ERROR	BIT(1)
114 #define USBCAN_ERROR_STATE_BUSERROR	BIT(2)
115 
116 /* ctrl modes */
117 #define KVASER_CTRL_MODE_NORMAL		1
118 #define KVASER_CTRL_MODE_SILENT		2
119 #define KVASER_CTRL_MODE_SELFRECEPTION	3
120 #define KVASER_CTRL_MODE_OFF		4
121 
122 /* Extended CAN identifier flag */
123 #define KVASER_EXTENDED_FRAME		BIT(31)
124 
125 /* USBCanII timestamp */
126 #define KVASER_USB_USBCAN_CLK_OVERFLOW_MASK GENMASK(31, 16)
127 #define KVASER_USB_USBCAN_TIMESTAMP_FACTOR 10
128 
129 struct kvaser_cmd_simple {
130 	u8 tid;
131 	u8 channel;
132 } __packed;
133 
134 struct kvaser_cmd_cardinfo {
135 	u8 tid;
136 	u8 nchannels;
137 	__le32 serial_number;
138 	__le32 padding0;
139 	__le32 clock_resolution;
140 	__le32 mfgdate;
141 	__le32 ean[2];
142 	u8 hw_revision;
143 	union {
144 		struct {
145 			u8 usb_hs_mode;
146 		} __packed leaf1;
147 		struct {
148 			u8 padding;
149 		} __packed usbcan1;
150 	} __packed;
151 	__le16 padding1;
152 } __packed;
153 
154 struct leaf_cmd_softinfo {
155 	u8 tid;
156 	u8 padding0;
157 	__le32 sw_options;
158 	__le32 fw_version;
159 	__le16 max_outstanding_tx;
160 	__le16 padding1[9];
161 } __packed;
162 
163 struct usbcan_cmd_softinfo {
164 	u8 tid;
165 	u8 fw_name[5];
166 	__le16 max_outstanding_tx;
167 	u8 padding[6];
168 	__le32 fw_version;
169 	__le16 checksum;
170 	__le16 sw_options;
171 } __packed;
172 
173 struct kvaser_cmd_busparams {
174 	u8 tid;
175 	u8 channel;
176 	struct kvaser_usb_busparams busparams;
177 } __packed;
178 
179 /* The device has one LED per CAN channel
180  * The LSB of action field controls the state:
181  *   0 = ON
182  *   1 = OFF
183  * The remaining bits of action field is the LED index
184  */
185 #define KVASER_USB_LEAF_LED_IDX_MASK GENMASK(31, 1)
186 #define KVASER_USB_LEAF_LED_YELLOW_CH0_IDX 2
187 struct kvaser_cmd_led_action_req {
188 	u8 tid;
189 	u8 action;
190 	__le16 duration_ms;
191 	u8 padding[24];
192 } __packed;
193 
194 struct kvaser_cmd_tx_can {
195 	u8 channel;
196 	u8 tid;
197 	u8 data[14];
198 	union {
199 		struct {
200 			u8 padding;
201 			u8 flags;
202 		} __packed leaf;
203 		struct {
204 			u8 flags;
205 			u8 padding;
206 		} __packed usbcan;
207 	} __packed;
208 } __packed;
209 
210 struct kvaser_cmd_rx_can_header {
211 	u8 channel;
212 	u8 flag;
213 } __packed;
214 
215 struct leaf_cmd_rx_can {
216 	u8 channel;
217 	u8 flag;
218 
219 	__le16 time[3];
220 	u8 data[14];
221 } __packed;
222 
223 struct usbcan_cmd_rx_can {
224 	u8 channel;
225 	u8 flag;
226 
227 	u8 data[14];
228 	__le16 time;
229 } __packed;
230 
231 struct leaf_cmd_chip_state_event {
232 	u8 tid;
233 	u8 channel;
234 
235 	__le16 time[3];
236 	u8 tx_errors_count;
237 	u8 rx_errors_count;
238 
239 	u8 status;
240 	u8 padding[3];
241 } __packed;
242 
243 struct usbcan_cmd_chip_state_event {
244 	u8 tid;
245 	u8 channel;
246 
247 	u8 tx_errors_count;
248 	u8 rx_errors_count;
249 	__le16 time;
250 
251 	u8 status;
252 	u8 padding[3];
253 } __packed;
254 
255 struct kvaser_cmd_tx_acknowledge_header {
256 	u8 channel;
257 	u8 tid;
258 } __packed;
259 
260 struct leaf_cmd_tx_acknowledge {
261 	u8 channel;
262 	u8 tid;
263 	__le16 time[3];
264 	u8 padding[2];
265 } __packed;
266 
267 struct usbcan_cmd_tx_acknowledge {
268 	u8 channel;
269 	u8 tid;
270 	__le16 time;
271 	u8 padding[2];
272 } __packed;
273 
274 struct leaf_cmd_can_error_event {
275 	u8 tid;
276 	u8 flags;
277 	__le16 time[3];
278 	u8 channel;
279 	u8 padding;
280 	u8 tx_errors_count;
281 	u8 rx_errors_count;
282 	u8 status;
283 	u8 error_factor;
284 } __packed;
285 
286 struct usbcan_cmd_can_error_event {
287 	u8 tid;
288 	u8 padding;
289 	u8 tx_errors_count_ch0;
290 	u8 rx_errors_count_ch0;
291 	u8 tx_errors_count_ch1;
292 	u8 rx_errors_count_ch1;
293 	u8 status_ch0;
294 	u8 status_ch1;
295 	__le16 time;
296 } __packed;
297 
298 /* CMD_ERROR_EVENT error codes */
299 #define KVASER_USB_LEAF_ERROR_EVENT_TX_QUEUE_FULL 0x8
300 #define KVASER_USB_LEAF_ERROR_EVENT_PARAM 0x9
301 
302 struct leaf_cmd_error_event {
303 	u8 tid;
304 	u8 error_code;
305 	__le16 timestamp[3];
306 	__le16 padding;
307 	__le16 info1;
308 	__le16 info2;
309 } __packed;
310 
311 struct usbcan_cmd_error_event {
312 	u8 tid;
313 	u8 error_code;
314 	__le16 info1;
315 	__le16 info2;
316 	__le16 timestamp;
317 	__le16 padding;
318 } __packed;
319 
320 struct usbcan_cmd_clk_overflow_event {
321 	u8 tid;
322 	u8 padding;
323 	__le32 time;
324 } __packed;
325 
326 struct kvaser_cmd_ctrl_mode {
327 	u8 tid;
328 	u8 channel;
329 	u8 ctrl_mode;
330 	u8 padding[3];
331 } __packed;
332 
333 struct kvaser_cmd_flush_queue {
334 	u8 tid;
335 	u8 channel;
336 	u8 flags;
337 	u8 padding[3];
338 } __packed;
339 
340 struct leaf_cmd_log_message {
341 	u8 channel;
342 	u8 flags;
343 	__le16 time[3];
344 	u8 dlc;
345 	u8 time_offset;
346 	__le32 id;
347 	u8 data[8];
348 } __packed;
349 
350 /* Sub commands for cap_req and cap_res */
351 #define KVASER_USB_LEAF_CAP_CMD_LISTEN_MODE 0x02
352 #define KVASER_USB_LEAF_CAP_CMD_ERR_REPORT 0x05
353 struct kvaser_cmd_cap_req {
354 	__le16 padding0;
355 	__le16 cap_cmd;
356 	__le16 padding1;
357 	__le16 channel;
358 } __packed;
359 
360 /* Status codes for cap_res */
361 #define KVASER_USB_LEAF_CAP_STAT_OK 0x00
362 #define KVASER_USB_LEAF_CAP_STAT_NOT_IMPL 0x01
363 #define KVASER_USB_LEAF_CAP_STAT_UNAVAIL 0x02
364 struct kvaser_cmd_cap_res {
365 	__le16 padding;
366 	__le16 cap_cmd;
367 	__le16 status;
368 	__le32 mask;
369 	__le32 value;
370 } __packed;
371 
372 struct kvaser_cmd {
373 	u8 len;
374 	u8 id;
375 	union	{
376 		struct kvaser_cmd_simple simple;
377 		struct kvaser_cmd_cardinfo cardinfo;
378 		struct kvaser_cmd_busparams busparams;
379 
380 		struct kvaser_cmd_led_action_req led_action_req;
381 
382 		struct kvaser_cmd_rx_can_header rx_can_header;
383 		struct kvaser_cmd_tx_acknowledge_header tx_acknowledge_header;
384 
385 		union {
386 			struct leaf_cmd_softinfo softinfo;
387 			struct leaf_cmd_rx_can rx_can;
388 			struct leaf_cmd_chip_state_event chip_state_event;
389 			struct leaf_cmd_can_error_event can_error_event;
390 			struct leaf_cmd_log_message log_message;
391 			struct leaf_cmd_error_event error_event;
392 			struct kvaser_cmd_cap_req cap_req;
393 			struct kvaser_cmd_cap_res cap_res;
394 			struct leaf_cmd_tx_acknowledge tx_ack;
395 		} __packed leaf;
396 
397 		union {
398 			struct usbcan_cmd_softinfo softinfo;
399 			struct usbcan_cmd_rx_can rx_can;
400 			struct usbcan_cmd_chip_state_event chip_state_event;
401 			struct usbcan_cmd_can_error_event can_error_event;
402 			struct usbcan_cmd_error_event error_event;
403 			struct usbcan_cmd_tx_acknowledge tx_ack;
404 			struct usbcan_cmd_clk_overflow_event clk_overflow_event;
405 		} __packed usbcan;
406 
407 		struct kvaser_cmd_tx_can tx_can;
408 		struct kvaser_cmd_ctrl_mode ctrl_mode;
409 		struct kvaser_cmd_flush_queue flush_queue;
410 	} u;
411 } __packed;
412 
413 #define CMD_SIZE_ANY 0xff
414 #define kvaser_fsize(field) sizeof_field(struct kvaser_cmd, field)
415 
416 static const u8 kvaser_usb_leaf_cmd_sizes_leaf[] = {
417 	[CMD_START_CHIP_REPLY]		= kvaser_fsize(u.simple),
418 	[CMD_STOP_CHIP_REPLY]		= kvaser_fsize(u.simple),
419 	[CMD_GET_CARD_INFO_REPLY]	= kvaser_fsize(u.cardinfo),
420 	[CMD_TX_ACKNOWLEDGE]		= kvaser_fsize(u.leaf.tx_ack),
421 	[CMD_GET_SOFTWARE_INFO_REPLY]	= kvaser_fsize(u.leaf.softinfo),
422 	[CMD_RX_STD_MESSAGE]		= kvaser_fsize(u.leaf.rx_can),
423 	[CMD_RX_EXT_MESSAGE]		= kvaser_fsize(u.leaf.rx_can),
424 	[CMD_LEAF_LOG_MESSAGE]		= kvaser_fsize(u.leaf.log_message),
425 	[CMD_CHIP_STATE_EVENT]		= kvaser_fsize(u.leaf.chip_state_event),
426 	[CMD_CAN_ERROR_EVENT]		= kvaser_fsize(u.leaf.can_error_event),
427 	[CMD_GET_CAPABILITIES_RESP]	= kvaser_fsize(u.leaf.cap_res),
428 	[CMD_GET_BUS_PARAMS_REPLY]	= kvaser_fsize(u.busparams),
429 	[CMD_ERROR_EVENT]		= kvaser_fsize(u.leaf.error_event),
430 	/* ignored events: */
431 	[CMD_FLUSH_QUEUE_REPLY]		= CMD_SIZE_ANY,
432 	[CMD_LED_ACTION_RESP]		= CMD_SIZE_ANY,
433 };
434 
435 static const u8 kvaser_usb_leaf_cmd_sizes_usbcan[] = {
436 	[CMD_START_CHIP_REPLY]		= kvaser_fsize(u.simple),
437 	[CMD_STOP_CHIP_REPLY]		= kvaser_fsize(u.simple),
438 	[CMD_GET_CARD_INFO_REPLY]	= kvaser_fsize(u.cardinfo),
439 	[CMD_TX_ACKNOWLEDGE]		= kvaser_fsize(u.usbcan.tx_ack),
440 	[CMD_GET_SOFTWARE_INFO_REPLY]	= kvaser_fsize(u.usbcan.softinfo),
441 	[CMD_RX_STD_MESSAGE]		= kvaser_fsize(u.usbcan.rx_can),
442 	[CMD_RX_EXT_MESSAGE]		= kvaser_fsize(u.usbcan.rx_can),
443 	[CMD_CHIP_STATE_EVENT]		= kvaser_fsize(u.usbcan.chip_state_event),
444 	[CMD_CAN_ERROR_EVENT]		= kvaser_fsize(u.usbcan.can_error_event),
445 	[CMD_ERROR_EVENT]		= kvaser_fsize(u.usbcan.error_event),
446 	[CMD_USBCAN_CLOCK_OVERFLOW_EVENT] = kvaser_fsize(u.usbcan.clk_overflow_event),
447 	/* ignored events: */
448 	[CMD_LED_ACTION_RESP]		= CMD_SIZE_ANY,
449 };
450 
451 /* Summary of a kvaser error event, for a unified Leaf/Usbcan error
452  * handling. Some discrepancies between the two families exist:
453  *
454  * - USBCAN firmware does not report M16C "error factors"
455  * - USBCAN controllers has difficulties reporting if the raised error
456  *   event is for ch0 or ch1. They leave such arbitration to the OS
457  *   driver by letting it compare error counters with previous values
458  *   and decide the error event's channel. Thus for USBCAN, the channel
459  *   field is only advisory.
460  */
461 struct kvaser_usb_err_summary {
462 	u8 channel, status, txerr, rxerr;
463 	union {
464 		struct {
465 			u8 error_factor;
466 		} leaf;
467 		struct {
468 			u8 other_ch_status;
469 			u8 error_state;
470 		} usbcan;
471 	};
472 };
473 
474 struct kvaser_usb_net_leaf_priv {
475 	struct kvaser_usb_net_priv *net;
476 
477 	struct delayed_work chip_state_req_work;
478 
479 	/* started but not reported as bus-on yet */
480 	bool joining_bus;
481 };
482 
483 static const struct can_bittiming_const kvaser_usb_leaf_m16c_bittiming_const = {
484 	.name = "kvaser_usb_ucii",
485 	.tseg1_min = 4,
486 	.tseg1_max = 16,
487 	.tseg2_min = 2,
488 	.tseg2_max = 8,
489 	.sjw_max = 4,
490 	.brp_min = 1,
491 	.brp_max = 16,
492 	.brp_inc = 1,
493 };
494 
495 static const struct can_bittiming_const kvaser_usb_leaf_m32c_bittiming_const = {
496 	.name = "kvaser_usb_leaf",
497 	.tseg1_min = 3,
498 	.tseg1_max = 16,
499 	.tseg2_min = 2,
500 	.tseg2_max = 8,
501 	.sjw_max = 4,
502 	.brp_min = 2,
503 	.brp_max = 128,
504 	.brp_inc = 2,
505 };
506 
507 static const struct kvaser_usb_dev_cfg kvaser_usb_leaf_usbcan_dev_cfg = {
508 	.clock = {
509 		.freq = 8 * MEGA /* Hz */,
510 	},
511 	.timestamp_freq = 1,
512 	.bittiming_const = &kvaser_usb_leaf_m16c_bittiming_const,
513 };
514 
515 static const struct kvaser_usb_dev_cfg kvaser_usb_leaf_m32c_dev_cfg_16mhz = {
516 	.clock = {
517 		.freq = 16 * MEGA /* Hz */,
518 	},
519 	.timestamp_freq = 16,
520 	.bittiming_const = &kvaser_usb_leaf_m32c_bittiming_const,
521 };
522 
523 static const struct kvaser_usb_dev_cfg kvaser_usb_leaf_m32c_dev_cfg_24mhz = {
524 	.clock = {
525 		.freq = 16 * MEGA /* Hz */,
526 	},
527 	.timestamp_freq = 24,
528 	.bittiming_const = &kvaser_usb_leaf_m32c_bittiming_const,
529 };
530 
531 static const struct kvaser_usb_dev_cfg kvaser_usb_leaf_m32c_dev_cfg_32mhz = {
532 	.clock = {
533 		.freq = 16 * MEGA /* Hz */,
534 	},
535 	.timestamp_freq = 32,
536 	.bittiming_const = &kvaser_usb_leaf_m32c_bittiming_const,
537 };
538 
539 static const struct kvaser_usb_dev_cfg kvaser_usb_leaf_imx_dev_cfg_16mhz = {
540 	.clock = {
541 		.freq = 16 * MEGA /* Hz */,
542 	},
543 	.timestamp_freq = 16,
544 	.bittiming_const = &kvaser_usb_flexc_bittiming_const,
545 };
546 
547 static const struct kvaser_usb_dev_cfg kvaser_usb_leaf_imx_dev_cfg_24mhz = {
548 	.clock = {
549 		.freq = 24 * MEGA /* Hz */,
550 	},
551 	.timestamp_freq = 24,
552 	.bittiming_const = &kvaser_usb_flexc_bittiming_const,
553 };
554 
555 static const struct kvaser_usb_dev_cfg kvaser_usb_leaf_imx_dev_cfg_32mhz = {
556 	.clock = {
557 		.freq = 32 * MEGA /* Hz */,
558 	},
559 	.timestamp_freq = 32,
560 	.bittiming_const = &kvaser_usb_flexc_bittiming_const,
561 };
562 
kvaser_usb_usbcan_timestamp_to_ktime(const struct kvaser_usb * dev,__le16 timestamp)563 static inline ktime_t kvaser_usb_usbcan_timestamp_to_ktime(const struct kvaser_usb *dev,
564 							   __le16 timestamp)
565 {
566 	u64 ticks = le16_to_cpu(timestamp) |
567 		    dev->card_data.usbcan_timestamp_msb;
568 
569 	return kvaser_usb_ticks_to_ktime(dev->cfg, ticks * KVASER_USB_USBCAN_TIMESTAMP_FACTOR);
570 }
571 
kvaser_usb_leaf_verify_size(const struct kvaser_usb * dev,const struct kvaser_cmd * cmd)572 static int kvaser_usb_leaf_verify_size(const struct kvaser_usb *dev,
573 				       const struct kvaser_cmd *cmd)
574 {
575 	/* buffer size >= cmd->len ensured by caller */
576 	u8 min_size = 0;
577 
578 	switch (dev->driver_info->family) {
579 	case KVASER_LEAF:
580 		if (cmd->id < ARRAY_SIZE(kvaser_usb_leaf_cmd_sizes_leaf))
581 			min_size = kvaser_usb_leaf_cmd_sizes_leaf[cmd->id];
582 		break;
583 	case KVASER_USBCAN:
584 		if (cmd->id < ARRAY_SIZE(kvaser_usb_leaf_cmd_sizes_usbcan))
585 			min_size = kvaser_usb_leaf_cmd_sizes_usbcan[cmd->id];
586 		break;
587 	}
588 
589 	if (min_size == CMD_SIZE_ANY)
590 		return 0;
591 
592 	if (min_size) {
593 		min_size += CMD_HEADER_LEN;
594 		if (cmd->len >= min_size)
595 			return 0;
596 
597 		dev_err_ratelimited(&dev->intf->dev,
598 				    "Received command %u too short (size %u, needed %u)",
599 				    cmd->id, cmd->len, min_size);
600 		return -EIO;
601 	}
602 
603 	dev_warn_ratelimited(&dev->intf->dev,
604 			     "Unhandled command (%d, size %d)\n",
605 			     cmd->id, cmd->len);
606 	return -EINVAL;
607 }
608 
609 static void *
kvaser_usb_leaf_frame_to_cmd(const struct kvaser_usb_net_priv * priv,const struct sk_buff * skb,int * cmd_len,u16 transid)610 kvaser_usb_leaf_frame_to_cmd(const struct kvaser_usb_net_priv *priv,
611 			     const struct sk_buff *skb, int *cmd_len,
612 			     u16 transid)
613 {
614 	struct kvaser_usb *dev = priv->dev;
615 	struct kvaser_cmd *cmd;
616 	u8 *cmd_tx_can_flags = NULL;		/* GCC */
617 	struct can_frame *cf = (struct can_frame *)skb->data;
618 
619 	cmd = kmalloc(sizeof(*cmd), GFP_ATOMIC);
620 	if (cmd) {
621 		cmd->u.tx_can.tid = transid & 0xff;
622 		cmd->len = *cmd_len = CMD_HEADER_LEN +
623 				      sizeof(struct kvaser_cmd_tx_can);
624 		cmd->u.tx_can.channel = priv->channel;
625 
626 		switch (dev->driver_info->family) {
627 		case KVASER_LEAF:
628 			cmd_tx_can_flags = &cmd->u.tx_can.leaf.flags;
629 			break;
630 		case KVASER_USBCAN:
631 			cmd_tx_can_flags = &cmd->u.tx_can.usbcan.flags;
632 			break;
633 		}
634 
635 		*cmd_tx_can_flags = 0;
636 
637 		if (cf->can_id & CAN_EFF_FLAG) {
638 			cmd->id = CMD_TX_EXT_MESSAGE;
639 			cmd->u.tx_can.data[0] = (cf->can_id >> 24) & 0x1f;
640 			cmd->u.tx_can.data[1] = (cf->can_id >> 18) & 0x3f;
641 			cmd->u.tx_can.data[2] = (cf->can_id >> 14) & 0x0f;
642 			cmd->u.tx_can.data[3] = (cf->can_id >> 6) & 0xff;
643 			cmd->u.tx_can.data[4] = cf->can_id & 0x3f;
644 		} else {
645 			cmd->id = CMD_TX_STD_MESSAGE;
646 			cmd->u.tx_can.data[0] = (cf->can_id >> 6) & 0x1f;
647 			cmd->u.tx_can.data[1] = cf->can_id & 0x3f;
648 		}
649 
650 		cmd->u.tx_can.data[5] = can_get_cc_dlc(cf, priv->can.ctrlmode);
651 		memcpy(&cmd->u.tx_can.data[6], cf->data, cf->len);
652 
653 		if (cf->can_id & CAN_RTR_FLAG)
654 			*cmd_tx_can_flags |= MSG_FLAG_REMOTE_FRAME;
655 	}
656 	return cmd;
657 }
658 
kvaser_usb_leaf_wait_cmd(const struct kvaser_usb * dev,u8 id,struct kvaser_cmd * cmd)659 static int kvaser_usb_leaf_wait_cmd(const struct kvaser_usb *dev, u8 id,
660 				    struct kvaser_cmd *cmd)
661 {
662 	struct kvaser_cmd *tmp;
663 	void *buf;
664 	int actual_len;
665 	int err;
666 	int pos;
667 	unsigned long to = jiffies + msecs_to_jiffies(KVASER_USB_TIMEOUT);
668 
669 	buf = kzalloc(KVASER_USB_RX_BUFFER_SIZE, GFP_KERNEL);
670 	if (!buf)
671 		return -ENOMEM;
672 
673 	do {
674 		err = kvaser_usb_recv_cmd(dev, buf, KVASER_USB_RX_BUFFER_SIZE,
675 					  &actual_len);
676 		if (err < 0)
677 			goto end;
678 
679 		pos = 0;
680 		while (pos <= actual_len - CMD_HEADER_LEN) {
681 			tmp = buf + pos;
682 
683 			/* Handle commands crossing the USB endpoint max packet
684 			 * size boundary. Check kvaser_usb_read_bulk_callback()
685 			 * for further details.
686 			 */
687 			if (tmp->len == 0) {
688 				pos = round_up(pos,
689 					       le16_to_cpu
690 						(dev->bulk_in->wMaxPacketSize));
691 				continue;
692 			}
693 
694 			if (pos + tmp->len > actual_len) {
695 				dev_err_ratelimited(&dev->intf->dev,
696 						    "Format error\n");
697 				break;
698 			}
699 
700 			if (tmp->id == id) {
701 				memcpy(cmd, tmp, tmp->len);
702 				goto end;
703 			}
704 
705 			pos += tmp->len;
706 		}
707 	} while (time_before(jiffies, to));
708 
709 	err = -EINVAL;
710 
711 end:
712 	kfree(buf);
713 
714 	if (err == 0)
715 		err = kvaser_usb_leaf_verify_size(dev, cmd);
716 
717 	return err;
718 }
719 
kvaser_usb_leaf_send_simple_cmd(const struct kvaser_usb * dev,u8 cmd_id,int channel)720 static int kvaser_usb_leaf_send_simple_cmd(const struct kvaser_usb *dev,
721 					   u8 cmd_id, int channel)
722 {
723 	struct kvaser_cmd *cmd;
724 	int rc;
725 
726 	cmd = kmalloc(sizeof(*cmd), GFP_KERNEL);
727 	if (!cmd)
728 		return -ENOMEM;
729 
730 	cmd->id = cmd_id;
731 	cmd->len = CMD_HEADER_LEN + sizeof(struct kvaser_cmd_simple);
732 	cmd->u.simple.channel = channel;
733 	cmd->u.simple.tid = 0xff;
734 
735 	rc = kvaser_usb_send_cmd(dev, cmd, cmd->len);
736 
737 	kfree(cmd);
738 	return rc;
739 }
740 
kvaser_usb_leaf_get_software_info_leaf(struct kvaser_usb * dev,const struct leaf_cmd_softinfo * softinfo)741 static void kvaser_usb_leaf_get_software_info_leaf(struct kvaser_usb *dev,
742 						   const struct leaf_cmd_softinfo *softinfo)
743 {
744 	u32 fw_version;
745 	u32 sw_options = le32_to_cpu(softinfo->sw_options);
746 
747 	fw_version = le32_to_cpu(softinfo->fw_version);
748 	dev->fw_version.major = FIELD_GET(KVASER_USB_SW_VERSION_MAJOR_MASK, fw_version);
749 	dev->fw_version.minor = FIELD_GET(KVASER_USB_SW_VERSION_MINOR_MASK, fw_version);
750 	dev->fw_version.build = FIELD_GET(KVASER_USB_SW_VERSION_BUILD_MASK, fw_version);
751 	dev->max_tx_urbs = le16_to_cpu(softinfo->max_outstanding_tx);
752 
753 	if (sw_options & KVASER_USB_LEAF_SWOPTION_EXT_CAP)
754 		dev->card_data.capabilities |= KVASER_USB_CAP_EXT_CAP;
755 
756 	if (dev->driver_info->quirks & KVASER_USB_QUIRK_IGNORE_CLK_FREQ) {
757 		/* Firmware expects bittiming parameters calculated for 16MHz
758 		 * clock, regardless of the actual clock
759 		 * Though, the reported freq is used for timestamps
760 		 */
761 		switch (sw_options & KVASER_USB_LEAF_SWOPTION_FREQ_MASK) {
762 		case KVASER_USB_LEAF_SWOPTION_FREQ_16_MHZ_CLK:
763 			dev->cfg = &kvaser_usb_leaf_m32c_dev_cfg_16mhz;
764 			break;
765 		case KVASER_USB_LEAF_SWOPTION_FREQ_24_MHZ_CLK:
766 			dev->cfg = &kvaser_usb_leaf_m32c_dev_cfg_24mhz;
767 			break;
768 		case KVASER_USB_LEAF_SWOPTION_FREQ_32_MHZ_CLK:
769 			dev->cfg = &kvaser_usb_leaf_m32c_dev_cfg_32mhz;
770 			break;
771 		}
772 	} else {
773 		switch (sw_options & KVASER_USB_LEAF_SWOPTION_FREQ_MASK) {
774 		case KVASER_USB_LEAF_SWOPTION_FREQ_16_MHZ_CLK:
775 			dev->cfg = &kvaser_usb_leaf_imx_dev_cfg_16mhz;
776 			break;
777 		case KVASER_USB_LEAF_SWOPTION_FREQ_24_MHZ_CLK:
778 			dev->cfg = &kvaser_usb_leaf_imx_dev_cfg_24mhz;
779 			break;
780 		case KVASER_USB_LEAF_SWOPTION_FREQ_32_MHZ_CLK:
781 			dev->cfg = &kvaser_usb_leaf_imx_dev_cfg_32mhz;
782 			break;
783 		}
784 	}
785 }
786 
kvaser_usb_leaf_get_software_info_inner(struct kvaser_usb * dev)787 static int kvaser_usb_leaf_get_software_info_inner(struct kvaser_usb *dev)
788 {
789 	struct kvaser_cmd cmd;
790 	int err;
791 	u32 fw_version;
792 
793 	err = kvaser_usb_leaf_send_simple_cmd(dev, CMD_GET_SOFTWARE_INFO, 0);
794 	if (err)
795 		return err;
796 
797 	err = kvaser_usb_leaf_wait_cmd(dev, CMD_GET_SOFTWARE_INFO_REPLY, &cmd);
798 	if (err)
799 		return err;
800 
801 	switch (dev->driver_info->family) {
802 	case KVASER_LEAF:
803 		kvaser_usb_leaf_get_software_info_leaf(dev, &cmd.u.leaf.softinfo);
804 		break;
805 	case KVASER_USBCAN:
806 		fw_version = le32_to_cpu(cmd.u.usbcan.softinfo.fw_version);
807 		dev->fw_version.major = FIELD_GET(KVASER_USB_SW_VERSION_MAJOR_MASK,
808 						  fw_version);
809 		dev->fw_version.minor = FIELD_GET(KVASER_USB_SW_VERSION_MINOR_MASK,
810 						  fw_version);
811 		dev->fw_version.build = FIELD_GET(KVASER_USB_SW_VERSION_BUILD_MASK,
812 						  fw_version);
813 		dev->max_tx_urbs =
814 			le16_to_cpu(cmd.u.usbcan.softinfo.max_outstanding_tx);
815 		dev->cfg = &kvaser_usb_leaf_usbcan_dev_cfg;
816 		break;
817 	}
818 
819 	return 0;
820 }
821 
kvaser_usb_leaf_get_software_info(struct kvaser_usb * dev)822 static int kvaser_usb_leaf_get_software_info(struct kvaser_usb *dev)
823 {
824 	int err;
825 	int retry = 3;
826 
827 	/* On some x86 laptops, plugging a Kvaser device again after
828 	 * an unplug makes the firmware always ignore the very first
829 	 * command. For such a case, provide some room for retries
830 	 * instead of completely exiting the driver.
831 	 */
832 	do {
833 		err = kvaser_usb_leaf_get_software_info_inner(dev);
834 	} while (--retry && err == -ETIMEDOUT);
835 
836 	return err;
837 }
838 
kvaser_usb_leaf_get_card_info(struct kvaser_usb * dev)839 static int kvaser_usb_leaf_get_card_info(struct kvaser_usb *dev)
840 {
841 	struct kvaser_cmd cmd;
842 	int err;
843 
844 	err = kvaser_usb_leaf_send_simple_cmd(dev, CMD_GET_CARD_INFO, 0);
845 	if (err)
846 		return err;
847 
848 	err = kvaser_usb_leaf_wait_cmd(dev, CMD_GET_CARD_INFO_REPLY, &cmd);
849 	if (err)
850 		return err;
851 
852 	dev->nchannels = cmd.u.cardinfo.nchannels;
853 	if (dev->nchannels > KVASER_USB_MAX_NET_DEVICES ||
854 	    (dev->driver_info->family == KVASER_USBCAN &&
855 	     dev->nchannels > MAX_USBCAN_NET_DEVICES))
856 		return -EINVAL;
857 	dev->ean[1] = le32_to_cpu(cmd.u.cardinfo.ean[1]);
858 	dev->ean[0] = le32_to_cpu(cmd.u.cardinfo.ean[0]);
859 	dev->serial_number = le32_to_cpu(cmd.u.cardinfo.serial_number);
860 	dev->hw_revision = cmd.u.cardinfo.hw_revision;
861 
862 	return 0;
863 }
864 
kvaser_usb_leaf_get_single_capability(struct kvaser_usb * dev,u16 cap_cmd_req,u16 * status)865 static int kvaser_usb_leaf_get_single_capability(struct kvaser_usb *dev,
866 						 u16 cap_cmd_req, u16 *status)
867 {
868 	struct kvaser_usb_dev_card_data *card_data = &dev->card_data;
869 	struct kvaser_cmd *cmd;
870 	u32 value = 0;
871 	u32 mask = 0;
872 	u16 cap_cmd_res;
873 	int err;
874 	int i;
875 
876 	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
877 	if (!cmd)
878 		return -ENOMEM;
879 
880 	cmd->id = CMD_GET_CAPABILITIES_REQ;
881 	cmd->u.leaf.cap_req.cap_cmd = cpu_to_le16(cap_cmd_req);
882 	cmd->len = CMD_HEADER_LEN + sizeof(struct kvaser_cmd_cap_req);
883 
884 	err = kvaser_usb_send_cmd(dev, cmd, cmd->len);
885 	if (err)
886 		goto end;
887 
888 	err = kvaser_usb_leaf_wait_cmd(dev, CMD_GET_CAPABILITIES_RESP, cmd);
889 	if (err)
890 		goto end;
891 
892 	*status = le16_to_cpu(cmd->u.leaf.cap_res.status);
893 
894 	if (*status != KVASER_USB_LEAF_CAP_STAT_OK)
895 		goto end;
896 
897 	cap_cmd_res = le16_to_cpu(cmd->u.leaf.cap_res.cap_cmd);
898 	switch (cap_cmd_res) {
899 	case KVASER_USB_LEAF_CAP_CMD_LISTEN_MODE:
900 	case KVASER_USB_LEAF_CAP_CMD_ERR_REPORT:
901 		value = le32_to_cpu(cmd->u.leaf.cap_res.value);
902 		mask = le32_to_cpu(cmd->u.leaf.cap_res.mask);
903 		break;
904 	default:
905 		dev_warn(&dev->intf->dev, "Unknown capability command %u\n",
906 			 cap_cmd_res);
907 		break;
908 	}
909 
910 	for (i = 0; i < dev->nchannels; i++) {
911 		if (BIT(i) & (value & mask)) {
912 			switch (cap_cmd_res) {
913 			case KVASER_USB_LEAF_CAP_CMD_LISTEN_MODE:
914 				card_data->ctrlmode_supported |=
915 						CAN_CTRLMODE_LISTENONLY;
916 				break;
917 			case KVASER_USB_LEAF_CAP_CMD_ERR_REPORT:
918 				card_data->capabilities |=
919 						KVASER_USB_CAP_BERR_CAP;
920 				break;
921 			}
922 		}
923 	}
924 
925 end:
926 	kfree(cmd);
927 
928 	return err;
929 }
930 
kvaser_usb_leaf_get_capabilities_leaf(struct kvaser_usb * dev)931 static int kvaser_usb_leaf_get_capabilities_leaf(struct kvaser_usb *dev)
932 {
933 	int err;
934 	u16 status;
935 
936 	if (!(dev->card_data.capabilities & KVASER_USB_CAP_EXT_CAP)) {
937 		dev_info(&dev->intf->dev,
938 			 "No extended capability support. Upgrade device firmware.\n");
939 		return 0;
940 	}
941 
942 	err = kvaser_usb_leaf_get_single_capability(dev,
943 						    KVASER_USB_LEAF_CAP_CMD_LISTEN_MODE,
944 						    &status);
945 	if (err)
946 		return err;
947 	if (status)
948 		dev_info(&dev->intf->dev,
949 			 "KVASER_USB_LEAF_CAP_CMD_LISTEN_MODE failed %u\n",
950 			 status);
951 
952 	err = kvaser_usb_leaf_get_single_capability(dev,
953 						    KVASER_USB_LEAF_CAP_CMD_ERR_REPORT,
954 						    &status);
955 	if (err)
956 		return err;
957 	if (status)
958 		dev_info(&dev->intf->dev,
959 			 "KVASER_USB_LEAF_CAP_CMD_ERR_REPORT failed %u\n",
960 			 status);
961 
962 	return 0;
963 }
964 
kvaser_usb_leaf_set_led(struct kvaser_usb_net_priv * priv,enum kvaser_usb_led_state state,u16 duration_ms)965 static int kvaser_usb_leaf_set_led(struct kvaser_usb_net_priv *priv,
966 				   enum kvaser_usb_led_state state,
967 				   u16 duration_ms)
968 {
969 	struct kvaser_usb *dev = priv->dev;
970 	struct kvaser_cmd *cmd;
971 	int ret;
972 
973 	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
974 	if (!cmd)
975 		return -ENOMEM;
976 
977 	cmd->id = CMD_LED_ACTION_REQ;
978 	cmd->len = CMD_HEADER_LEN + sizeof(struct kvaser_cmd_led_action_req);
979 	cmd->u.led_action_req.tid = 0xff;
980 
981 	cmd->u.led_action_req.duration_ms = cpu_to_le16(duration_ms);
982 	cmd->u.led_action_req.action = state |
983 				       FIELD_PREP(KVASER_USB_LEAF_LED_IDX_MASK,
984 						  KVASER_USB_LEAF_LED_YELLOW_CH0_IDX +
985 						  priv->channel);
986 
987 	ret = kvaser_usb_send_cmd(dev, cmd, cmd->len);
988 	kfree(cmd);
989 
990 	return ret;
991 }
992 
kvaser_usb_leaf_get_capabilities(struct kvaser_usb * dev)993 static int kvaser_usb_leaf_get_capabilities(struct kvaser_usb *dev)
994 {
995 	int err = 0;
996 
997 	if (dev->driver_info->family == KVASER_LEAF)
998 		err = kvaser_usb_leaf_get_capabilities_leaf(dev);
999 
1000 	return err;
1001 }
1002 
kvaser_usb_leaf_tx_acknowledge(const struct kvaser_usb * dev,const struct kvaser_cmd * cmd)1003 static void kvaser_usb_leaf_tx_acknowledge(const struct kvaser_usb *dev,
1004 					   const struct kvaser_cmd *cmd)
1005 {
1006 	struct net_device_stats *stats;
1007 	struct kvaser_usb_tx_urb_context *context;
1008 	struct kvaser_usb_net_priv *priv;
1009 	unsigned long flags;
1010 	u8 channel, tid;
1011 	struct sk_buff *skb;
1012 	ktime_t hwtstamp = 0;
1013 
1014 	channel = cmd->u.tx_acknowledge_header.channel;
1015 	tid = cmd->u.tx_acknowledge_header.tid;
1016 
1017 	if (channel >= dev->nchannels) {
1018 		dev_err(&dev->intf->dev,
1019 			"Invalid channel number (%d)\n", channel);
1020 		return;
1021 	}
1022 
1023 	priv = dev->nets[channel];
1024 
1025 	if (!netif_device_present(priv->netdev))
1026 		return;
1027 
1028 	stats = &priv->netdev->stats;
1029 
1030 	context = &priv->tx_contexts[tid % dev->max_tx_urbs];
1031 
1032 	/* Sometimes the state change doesn't come after a bus-off event */
1033 	if (priv->can.restart_ms && priv->can.state == CAN_STATE_BUS_OFF) {
1034 		struct sk_buff *err_skb;
1035 		struct can_frame *cf;
1036 
1037 		err_skb = alloc_can_err_skb(priv->netdev, &cf);
1038 		if (err_skb) {
1039 			cf->can_id |= CAN_ERR_RESTARTED;
1040 
1041 			netif_rx(err_skb);
1042 		} else {
1043 			netdev_err(priv->netdev,
1044 				   "No memory left for err_skb\n");
1045 		}
1046 
1047 		priv->can.can_stats.restarts++;
1048 		netif_carrier_on(priv->netdev);
1049 
1050 		priv->can.state = CAN_STATE_ERROR_ACTIVE;
1051 	}
1052 	switch (dev->driver_info->family) {
1053 	case KVASER_LEAF:
1054 		hwtstamp = kvaser_usb_timestamp48_to_ktime(dev->cfg, cmd->u.leaf.tx_ack.time);
1055 		break;
1056 	case KVASER_USBCAN:
1057 		hwtstamp = kvaser_usb_usbcan_timestamp_to_ktime(dev, cmd->u.usbcan.tx_ack.time);
1058 		break;
1059 	}
1060 
1061 	spin_lock_irqsave(&priv->tx_contexts_lock, flags);
1062 
1063 	skb = priv->can.echo_skb[context->echo_index];
1064 	if (skb)
1065 		skb_hwtstamps(skb)->hwtstamp = hwtstamp;
1066 	stats->tx_packets++;
1067 	stats->tx_bytes += can_get_echo_skb(priv->netdev,
1068 					    context->echo_index, NULL);
1069 	context->echo_index = dev->max_tx_urbs;
1070 	--priv->active_tx_contexts;
1071 	netif_wake_queue(priv->netdev);
1072 
1073 	spin_unlock_irqrestore(&priv->tx_contexts_lock, flags);
1074 }
1075 
kvaser_usb_leaf_simple_cmd_async(struct kvaser_usb_net_priv * priv,u8 cmd_id)1076 static int kvaser_usb_leaf_simple_cmd_async(struct kvaser_usb_net_priv *priv,
1077 					    u8 cmd_id)
1078 {
1079 	struct kvaser_cmd *cmd;
1080 	int err;
1081 
1082 	cmd = kzalloc(sizeof(*cmd), GFP_ATOMIC);
1083 	if (!cmd)
1084 		return -ENOMEM;
1085 
1086 	cmd->len = CMD_HEADER_LEN + sizeof(struct kvaser_cmd_simple);
1087 	cmd->id = cmd_id;
1088 	cmd->u.simple.channel = priv->channel;
1089 
1090 	err = kvaser_usb_send_cmd_async(priv, cmd, cmd->len);
1091 	if (err)
1092 		kfree(cmd);
1093 
1094 	return err;
1095 }
1096 
kvaser_usb_leaf_chip_state_req_work(struct work_struct * work)1097 static void kvaser_usb_leaf_chip_state_req_work(struct work_struct *work)
1098 {
1099 	struct kvaser_usb_net_leaf_priv *leaf =
1100 		container_of(work, struct kvaser_usb_net_leaf_priv,
1101 			     chip_state_req_work.work);
1102 	struct kvaser_usb_net_priv *priv = leaf->net;
1103 
1104 	kvaser_usb_leaf_simple_cmd_async(priv, CMD_GET_CHIP_STATE);
1105 }
1106 
1107 static void
kvaser_usb_leaf_rx_error_update_can_state(struct kvaser_usb_net_priv * priv,const struct kvaser_usb_err_summary * es,struct can_frame * cf)1108 kvaser_usb_leaf_rx_error_update_can_state(struct kvaser_usb_net_priv *priv,
1109 					const struct kvaser_usb_err_summary *es,
1110 					struct can_frame *cf)
1111 {
1112 	struct kvaser_usb_net_leaf_priv *leaf = priv->sub_priv;
1113 	struct kvaser_usb *dev = priv->dev;
1114 	struct net_device_stats *stats = &priv->netdev->stats;
1115 	enum can_state cur_state, new_state, tx_state, rx_state;
1116 
1117 	netdev_dbg(priv->netdev, "Error status: 0x%02x\n", es->status);
1118 
1119 	new_state = priv->can.state;
1120 	cur_state = priv->can.state;
1121 
1122 	if (es->status & (M16C_STATE_BUS_OFF | M16C_STATE_BUS_RESET)) {
1123 		new_state = CAN_STATE_BUS_OFF;
1124 	} else if (es->status & M16C_STATE_BUS_PASSIVE) {
1125 		new_state = CAN_STATE_ERROR_PASSIVE;
1126 	} else if ((es->status & M16C_STATE_BUS_ERROR) &&
1127 		   cur_state >= CAN_STATE_BUS_OFF) {
1128 		/* Guard against spurious error events after a busoff */
1129 	} else if (es->txerr >= 128 || es->rxerr >= 128) {
1130 		new_state = CAN_STATE_ERROR_PASSIVE;
1131 	} else if (es->txerr >= 96 || es->rxerr >= 96) {
1132 		new_state = CAN_STATE_ERROR_WARNING;
1133 	} else {
1134 		new_state = CAN_STATE_ERROR_ACTIVE;
1135 	}
1136 
1137 	/* 0bfd:0124 FW 4.18.778 was observed to send the initial
1138 	 * CMD_CHIP_STATE_EVENT after CMD_START_CHIP with M16C_STATE_BUS_OFF
1139 	 * bit set if the channel was bus-off when it was last stopped (even
1140 	 * across chip resets). This bit will clear shortly afterwards, without
1141 	 * triggering a second unsolicited chip state event.
1142 	 * Ignore this initial bus-off.
1143 	 */
1144 	if (leaf->joining_bus) {
1145 		if (new_state == CAN_STATE_BUS_OFF) {
1146 			netdev_dbg(priv->netdev, "ignoring bus-off during startup");
1147 			new_state = cur_state;
1148 		} else {
1149 			leaf->joining_bus = false;
1150 		}
1151 	}
1152 
1153 	if (new_state != cur_state) {
1154 		tx_state = (es->txerr >= es->rxerr) ? new_state : 0;
1155 		rx_state = (es->txerr <= es->rxerr) ? new_state : 0;
1156 
1157 		can_change_state(priv->netdev, cf, tx_state, rx_state);
1158 	}
1159 
1160 	if (priv->can.restart_ms &&
1161 	    cur_state == CAN_STATE_BUS_OFF &&
1162 	    new_state < CAN_STATE_BUS_OFF)
1163 		priv->can.can_stats.restarts++;
1164 
1165 	switch (dev->driver_info->family) {
1166 	case KVASER_LEAF:
1167 		if (es->leaf.error_factor) {
1168 			priv->can.can_stats.bus_error++;
1169 			stats->rx_errors++;
1170 		}
1171 		break;
1172 	case KVASER_USBCAN:
1173 		if (es->usbcan.error_state & USBCAN_ERROR_STATE_TX_ERROR)
1174 			stats->tx_errors++;
1175 		if (es->usbcan.error_state & USBCAN_ERROR_STATE_RX_ERROR)
1176 			stats->rx_errors++;
1177 		if (es->usbcan.error_state & USBCAN_ERROR_STATE_BUSERROR)
1178 			priv->can.can_stats.bus_error++;
1179 		break;
1180 	}
1181 
1182 	priv->bec.txerr = es->txerr;
1183 	priv->bec.rxerr = es->rxerr;
1184 }
1185 
kvaser_usb_leaf_rx_error(const struct kvaser_usb * dev,const struct kvaser_usb_err_summary * es)1186 static void kvaser_usb_leaf_rx_error(const struct kvaser_usb *dev,
1187 				     const struct kvaser_usb_err_summary *es)
1188 {
1189 	struct can_frame *cf = NULL;
1190 	struct sk_buff *skb = NULL;
1191 	struct net_device_stats *stats;
1192 	struct kvaser_usb_net_priv *priv;
1193 	struct kvaser_usb_net_leaf_priv *leaf;
1194 	enum can_state old_state, new_state;
1195 
1196 	if (es->channel >= dev->nchannels) {
1197 		dev_err(&dev->intf->dev,
1198 			"Invalid channel number (%d)\n", es->channel);
1199 		return;
1200 	}
1201 
1202 	priv = dev->nets[es->channel];
1203 	leaf = priv->sub_priv;
1204 	stats = &priv->netdev->stats;
1205 
1206 	/* Ignore e.g. state change to bus-off reported just after stopping */
1207 	if (!netif_running(priv->netdev))
1208 		return;
1209 
1210 	old_state = priv->can.state;
1211 	if (priv->can.ctrlmode & CAN_CTRLMODE_BERR_REPORTING)
1212 		skb = alloc_can_err_skb(priv->netdev, &cf);
1213 	kvaser_usb_leaf_rx_error_update_can_state(priv, es, cf);
1214 	new_state = priv->can.state;
1215 
1216 	/* If there are errors, request status updates periodically as we do
1217 	 * not get automatic notifications of improved state.
1218 	 * Also request updates if we saw a stale BUS_OFF during startup
1219 	 * (joining_bus).
1220 	 */
1221 	if (new_state < CAN_STATE_BUS_OFF &&
1222 	    (es->rxerr || es->txerr || new_state == CAN_STATE_ERROR_PASSIVE ||
1223 	     leaf->joining_bus))
1224 		schedule_delayed_work(&leaf->chip_state_req_work,
1225 				      msecs_to_jiffies(500));
1226 
1227 	if (new_state != old_state) {
1228 		if (es->status &
1229 		    (M16C_STATE_BUS_OFF | M16C_STATE_BUS_RESET)) {
1230 			if (!priv->can.restart_ms)
1231 				kvaser_usb_leaf_simple_cmd_async(priv,
1232 								 CMD_STOP_CHIP);
1233 			netif_carrier_off(priv->netdev);
1234 		}
1235 
1236 		if (priv->can.restart_ms &&
1237 		    old_state == CAN_STATE_BUS_OFF &&
1238 		    new_state < CAN_STATE_BUS_OFF) {
1239 			if (cf)
1240 				cf->can_id |= CAN_ERR_RESTARTED;
1241 			netif_carrier_on(priv->netdev);
1242 		}
1243 	}
1244 
1245 	if (!skb) {
1246 		if (priv->can.ctrlmode & CAN_CTRLMODE_BERR_REPORTING) {
1247 			stats->rx_dropped++;
1248 			netdev_warn(priv->netdev, "No memory left for err_skb\n");
1249 		}
1250 		return;
1251 	}
1252 
1253 	switch (dev->driver_info->family) {
1254 	case KVASER_LEAF:
1255 		if (es->leaf.error_factor) {
1256 			cf->can_id |= CAN_ERR_BUSERROR | CAN_ERR_PROT;
1257 
1258 			if (es->leaf.error_factor & M16C_EF_ACKE)
1259 				cf->data[3] = CAN_ERR_PROT_LOC_ACK;
1260 			if (es->leaf.error_factor & M16C_EF_CRCE)
1261 				cf->data[3] = CAN_ERR_PROT_LOC_CRC_SEQ;
1262 			if (es->leaf.error_factor & M16C_EF_FORME)
1263 				cf->data[2] |= CAN_ERR_PROT_FORM;
1264 			if (es->leaf.error_factor & M16C_EF_STFE)
1265 				cf->data[2] |= CAN_ERR_PROT_STUFF;
1266 			if (es->leaf.error_factor & M16C_EF_BITE0)
1267 				cf->data[2] |= CAN_ERR_PROT_BIT0;
1268 			if (es->leaf.error_factor & M16C_EF_BITE1)
1269 				cf->data[2] |= CAN_ERR_PROT_BIT1;
1270 			if (es->leaf.error_factor & M16C_EF_TRE)
1271 				cf->data[2] |= CAN_ERR_PROT_TX;
1272 		}
1273 		break;
1274 	case KVASER_USBCAN:
1275 		if (es->usbcan.error_state & USBCAN_ERROR_STATE_BUSERROR)
1276 			cf->can_id |= CAN_ERR_BUSERROR;
1277 		break;
1278 	}
1279 
1280 	if (new_state != CAN_STATE_BUS_OFF) {
1281 		cf->can_id |= CAN_ERR_CNT;
1282 		cf->data[6] = es->txerr;
1283 		cf->data[7] = es->rxerr;
1284 	}
1285 
1286 	netif_rx(skb);
1287 }
1288 
1289 /* For USBCAN, report error to userspace if the channels's errors counter
1290  * has changed, or we're the only channel seeing a bus error state.
1291  */
1292 static void
kvaser_usb_leaf_usbcan_conditionally_rx_error(const struct kvaser_usb * dev,struct kvaser_usb_err_summary * es)1293 kvaser_usb_leaf_usbcan_conditionally_rx_error(const struct kvaser_usb *dev,
1294 					      struct kvaser_usb_err_summary *es)
1295 {
1296 	struct kvaser_usb_net_priv *priv;
1297 	unsigned int channel;
1298 	bool report_error;
1299 
1300 	channel = es->channel;
1301 	if (channel >= dev->nchannels) {
1302 		dev_err(&dev->intf->dev,
1303 			"Invalid channel number (%d)\n", channel);
1304 		return;
1305 	}
1306 
1307 	priv = dev->nets[channel];
1308 	report_error = false;
1309 
1310 	if (es->txerr != priv->bec.txerr) {
1311 		es->usbcan.error_state |= USBCAN_ERROR_STATE_TX_ERROR;
1312 		report_error = true;
1313 	}
1314 	if (es->rxerr != priv->bec.rxerr) {
1315 		es->usbcan.error_state |= USBCAN_ERROR_STATE_RX_ERROR;
1316 		report_error = true;
1317 	}
1318 	if ((es->status & M16C_STATE_BUS_ERROR) &&
1319 	    !(es->usbcan.other_ch_status & M16C_STATE_BUS_ERROR)) {
1320 		es->usbcan.error_state |= USBCAN_ERROR_STATE_BUSERROR;
1321 		report_error = true;
1322 	}
1323 
1324 	if (report_error)
1325 		kvaser_usb_leaf_rx_error(dev, es);
1326 }
1327 
kvaser_usb_leaf_usbcan_rx_error(const struct kvaser_usb * dev,const struct kvaser_cmd * cmd)1328 static void kvaser_usb_leaf_usbcan_rx_error(const struct kvaser_usb *dev,
1329 					    const struct kvaser_cmd *cmd)
1330 {
1331 	struct kvaser_usb_err_summary es = { };
1332 
1333 	switch (cmd->id) {
1334 	/* Sometimes errors are sent as unsolicited chip state events */
1335 	case CMD_CHIP_STATE_EVENT:
1336 		es.channel = cmd->u.usbcan.chip_state_event.channel;
1337 		es.status = cmd->u.usbcan.chip_state_event.status;
1338 		es.txerr = cmd->u.usbcan.chip_state_event.tx_errors_count;
1339 		es.rxerr = cmd->u.usbcan.chip_state_event.rx_errors_count;
1340 		kvaser_usb_leaf_usbcan_conditionally_rx_error(dev, &es);
1341 		break;
1342 
1343 	case CMD_CAN_ERROR_EVENT:
1344 		es.channel = 0;
1345 		es.status = cmd->u.usbcan.can_error_event.status_ch0;
1346 		es.txerr = cmd->u.usbcan.can_error_event.tx_errors_count_ch0;
1347 		es.rxerr = cmd->u.usbcan.can_error_event.rx_errors_count_ch0;
1348 		es.usbcan.other_ch_status =
1349 			cmd->u.usbcan.can_error_event.status_ch1;
1350 		kvaser_usb_leaf_usbcan_conditionally_rx_error(dev, &es);
1351 
1352 		/* The USBCAN firmware supports up to 2 channels.
1353 		 * Now that ch0 was checked, check if ch1 has any errors.
1354 		 */
1355 		if (dev->nchannels == MAX_USBCAN_NET_DEVICES) {
1356 			es.channel = 1;
1357 			es.status = cmd->u.usbcan.can_error_event.status_ch1;
1358 			es.txerr =
1359 				cmd->u.usbcan.can_error_event.tx_errors_count_ch1;
1360 			es.rxerr =
1361 				cmd->u.usbcan.can_error_event.rx_errors_count_ch1;
1362 			es.usbcan.other_ch_status =
1363 				cmd->u.usbcan.can_error_event.status_ch0;
1364 			kvaser_usb_leaf_usbcan_conditionally_rx_error(dev, &es);
1365 		}
1366 		break;
1367 
1368 	default:
1369 		dev_err(&dev->intf->dev, "Invalid cmd id (%d)\n", cmd->id);
1370 	}
1371 }
1372 
kvaser_usb_leaf_leaf_rx_error(const struct kvaser_usb * dev,const struct kvaser_cmd * cmd)1373 static void kvaser_usb_leaf_leaf_rx_error(const struct kvaser_usb *dev,
1374 					  const struct kvaser_cmd *cmd)
1375 {
1376 	struct kvaser_usb_err_summary es = { };
1377 
1378 	switch (cmd->id) {
1379 	case CMD_CAN_ERROR_EVENT:
1380 		es.channel = cmd->u.leaf.can_error_event.channel;
1381 		es.status = cmd->u.leaf.can_error_event.status;
1382 		es.txerr = cmd->u.leaf.can_error_event.tx_errors_count;
1383 		es.rxerr = cmd->u.leaf.can_error_event.rx_errors_count;
1384 		es.leaf.error_factor = cmd->u.leaf.can_error_event.error_factor;
1385 		break;
1386 	case CMD_LEAF_LOG_MESSAGE:
1387 		es.channel = cmd->u.leaf.log_message.channel;
1388 		es.status = cmd->u.leaf.log_message.data[0];
1389 		es.txerr = cmd->u.leaf.log_message.data[2];
1390 		es.rxerr = cmd->u.leaf.log_message.data[3];
1391 		es.leaf.error_factor = cmd->u.leaf.log_message.data[1];
1392 		break;
1393 	case CMD_CHIP_STATE_EVENT:
1394 		es.channel = cmd->u.leaf.chip_state_event.channel;
1395 		es.status = cmd->u.leaf.chip_state_event.status;
1396 		es.txerr = cmd->u.leaf.chip_state_event.tx_errors_count;
1397 		es.rxerr = cmd->u.leaf.chip_state_event.rx_errors_count;
1398 		es.leaf.error_factor = 0;
1399 		break;
1400 	default:
1401 		dev_err(&dev->intf->dev, "Invalid cmd id (%d)\n", cmd->id);
1402 		return;
1403 	}
1404 
1405 	kvaser_usb_leaf_rx_error(dev, &es);
1406 }
1407 
kvaser_usb_leaf_rx_can_err(const struct kvaser_usb_net_priv * priv,const struct kvaser_cmd * cmd)1408 static void kvaser_usb_leaf_rx_can_err(const struct kvaser_usb_net_priv *priv,
1409 				       const struct kvaser_cmd *cmd)
1410 {
1411 	if (cmd->u.rx_can_header.flag & (MSG_FLAG_ERROR_FRAME |
1412 					 MSG_FLAG_NERR)) {
1413 		struct net_device_stats *stats = &priv->netdev->stats;
1414 
1415 		netdev_err(priv->netdev, "Unknown error (flags: 0x%02x)\n",
1416 			   cmd->u.rx_can_header.flag);
1417 
1418 		stats->rx_errors++;
1419 		return;
1420 	}
1421 
1422 	if (cmd->u.rx_can_header.flag & MSG_FLAG_OVERRUN)
1423 		kvaser_usb_can_rx_over_error(priv->netdev);
1424 }
1425 
kvaser_usb_leaf_rx_can_msg(const struct kvaser_usb * dev,const struct kvaser_cmd * cmd)1426 static void kvaser_usb_leaf_rx_can_msg(const struct kvaser_usb *dev,
1427 				       const struct kvaser_cmd *cmd)
1428 {
1429 	struct kvaser_usb_net_priv *priv;
1430 	struct can_frame *cf;
1431 	struct sk_buff *skb;
1432 	struct net_device_stats *stats;
1433 	u8 channel = cmd->u.rx_can_header.channel;
1434 	const u8 *rx_data = NULL;	/* GCC */
1435 	ktime_t hwtstamp = 0;
1436 
1437 	if (channel >= dev->nchannels) {
1438 		dev_err(&dev->intf->dev,
1439 			"Invalid channel number (%d)\n", channel);
1440 		return;
1441 	}
1442 
1443 	priv = dev->nets[channel];
1444 	stats = &priv->netdev->stats;
1445 
1446 	if ((cmd->u.rx_can_header.flag & MSG_FLAG_ERROR_FRAME) &&
1447 	    (dev->driver_info->family == KVASER_LEAF &&
1448 	     cmd->id == CMD_LEAF_LOG_MESSAGE)) {
1449 		kvaser_usb_leaf_leaf_rx_error(dev, cmd);
1450 		return;
1451 	} else if (cmd->u.rx_can_header.flag & (MSG_FLAG_ERROR_FRAME |
1452 						MSG_FLAG_NERR |
1453 						MSG_FLAG_OVERRUN)) {
1454 		kvaser_usb_leaf_rx_can_err(priv, cmd);
1455 		return;
1456 	} else if (cmd->u.rx_can_header.flag & ~MSG_FLAG_REMOTE_FRAME) {
1457 		netdev_warn(priv->netdev,
1458 			    "Unhandled frame (flags: 0x%02x)\n",
1459 			    cmd->u.rx_can_header.flag);
1460 		return;
1461 	}
1462 
1463 	switch (dev->driver_info->family) {
1464 	case KVASER_LEAF:
1465 		rx_data = cmd->u.leaf.rx_can.data;
1466 		hwtstamp = kvaser_usb_timestamp48_to_ktime(dev->cfg, cmd->u.leaf.rx_can.time);
1467 		break;
1468 	case KVASER_USBCAN:
1469 		rx_data = cmd->u.usbcan.rx_can.data;
1470 		hwtstamp = kvaser_usb_usbcan_timestamp_to_ktime(dev, cmd->u.usbcan.rx_can.time);
1471 		break;
1472 	}
1473 
1474 	skb = alloc_can_skb(priv->netdev, &cf);
1475 	if (!skb) {
1476 		stats->rx_dropped++;
1477 		return;
1478 	}
1479 
1480 	if (dev->driver_info->family == KVASER_LEAF && cmd->id ==
1481 	    CMD_LEAF_LOG_MESSAGE) {
1482 		cf->can_id = le32_to_cpu(cmd->u.leaf.log_message.id);
1483 		if (cf->can_id & KVASER_EXTENDED_FRAME)
1484 			cf->can_id &= CAN_EFF_MASK | CAN_EFF_FLAG;
1485 		else
1486 			cf->can_id &= CAN_SFF_MASK;
1487 
1488 		can_frame_set_cc_len(cf, cmd->u.leaf.log_message.dlc & 0xF, priv->can.ctrlmode);
1489 
1490 		if (cmd->u.leaf.log_message.flags & MSG_FLAG_REMOTE_FRAME)
1491 			cf->can_id |= CAN_RTR_FLAG;
1492 		else
1493 			memcpy(cf->data, &cmd->u.leaf.log_message.data,
1494 			       cf->len);
1495 	} else {
1496 		cf->can_id = ((rx_data[0] & 0x1f) << 6) | (rx_data[1] & 0x3f);
1497 
1498 		if (cmd->id == CMD_RX_EXT_MESSAGE) {
1499 			cf->can_id <<= 18;
1500 			cf->can_id |= ((rx_data[2] & 0x0f) << 14) |
1501 				      ((rx_data[3] & 0xff) << 6) |
1502 				      (rx_data[4] & 0x3f);
1503 			cf->can_id |= CAN_EFF_FLAG;
1504 		}
1505 
1506 		can_frame_set_cc_len(cf, rx_data[5] & 0xF, priv->can.ctrlmode);
1507 
1508 		if (cmd->u.rx_can_header.flag & MSG_FLAG_REMOTE_FRAME)
1509 			cf->can_id |= CAN_RTR_FLAG;
1510 		else
1511 			memcpy(cf->data, &rx_data[6], cf->len);
1512 	}
1513 
1514 	skb_hwtstamps(skb)->hwtstamp = hwtstamp;
1515 	stats->rx_packets++;
1516 	if (!(cf->can_id & CAN_RTR_FLAG))
1517 		stats->rx_bytes += cf->len;
1518 	netif_rx(skb);
1519 }
1520 
kvaser_usb_leaf_error_event_parameter(const struct kvaser_usb * dev,const struct kvaser_cmd * cmd)1521 static void kvaser_usb_leaf_error_event_parameter(const struct kvaser_usb *dev,
1522 						  const struct kvaser_cmd *cmd)
1523 {
1524 	u16 info1 = 0;
1525 
1526 	switch (dev->driver_info->family) {
1527 	case KVASER_LEAF:
1528 		info1 = le16_to_cpu(cmd->u.leaf.error_event.info1);
1529 		break;
1530 	case KVASER_USBCAN:
1531 		info1 = le16_to_cpu(cmd->u.usbcan.error_event.info1);
1532 		break;
1533 	}
1534 
1535 	/* info1 will contain the offending cmd_no */
1536 	switch (info1) {
1537 	case CMD_SET_CTRL_MODE:
1538 		dev_warn(&dev->intf->dev,
1539 			 "CMD_SET_CTRL_MODE error in parameter\n");
1540 		break;
1541 
1542 	case CMD_SET_BUS_PARAMS:
1543 		dev_warn(&dev->intf->dev,
1544 			 "CMD_SET_BUS_PARAMS error in parameter\n");
1545 		break;
1546 
1547 	default:
1548 		dev_warn(&dev->intf->dev,
1549 			 "Unhandled parameter error event cmd_no (%u)\n",
1550 			 info1);
1551 		break;
1552 	}
1553 }
1554 
kvaser_usb_leaf_error_event(const struct kvaser_usb * dev,const struct kvaser_cmd * cmd)1555 static void kvaser_usb_leaf_error_event(const struct kvaser_usb *dev,
1556 					const struct kvaser_cmd *cmd)
1557 {
1558 	u8 error_code = 0;
1559 
1560 	switch (dev->driver_info->family) {
1561 	case KVASER_LEAF:
1562 		error_code = cmd->u.leaf.error_event.error_code;
1563 		break;
1564 	case KVASER_USBCAN:
1565 		error_code = cmd->u.usbcan.error_event.error_code;
1566 		break;
1567 	}
1568 
1569 	switch (error_code) {
1570 	case KVASER_USB_LEAF_ERROR_EVENT_TX_QUEUE_FULL:
1571 		/* Received additional CAN message, when firmware TX queue is
1572 		 * already full. Something is wrong with the driver.
1573 		 * This should never happen!
1574 		 */
1575 		dev_err(&dev->intf->dev,
1576 			"Received error event TX_QUEUE_FULL\n");
1577 		break;
1578 	case KVASER_USB_LEAF_ERROR_EVENT_PARAM:
1579 		kvaser_usb_leaf_error_event_parameter(dev, cmd);
1580 		break;
1581 
1582 	default:
1583 		dev_warn(&dev->intf->dev,
1584 			 "Unhandled error event (%d)\n", error_code);
1585 		break;
1586 	}
1587 }
1588 
kvaser_usb_leaf_start_chip_reply(const struct kvaser_usb * dev,const struct kvaser_cmd * cmd)1589 static void kvaser_usb_leaf_start_chip_reply(const struct kvaser_usb *dev,
1590 					     const struct kvaser_cmd *cmd)
1591 {
1592 	struct kvaser_usb_net_priv *priv;
1593 	u8 channel = cmd->u.simple.channel;
1594 
1595 	if (channel >= dev->nchannels) {
1596 		dev_err(&dev->intf->dev,
1597 			"Invalid channel number (%d)\n", channel);
1598 		return;
1599 	}
1600 
1601 	priv = dev->nets[channel];
1602 
1603 	if (completion_done(&priv->start_comp) &&
1604 	    netif_queue_stopped(priv->netdev)) {
1605 		netif_wake_queue(priv->netdev);
1606 	} else {
1607 		netif_start_queue(priv->netdev);
1608 		complete(&priv->start_comp);
1609 	}
1610 }
1611 
kvaser_usb_leaf_stop_chip_reply(const struct kvaser_usb * dev,const struct kvaser_cmd * cmd)1612 static void kvaser_usb_leaf_stop_chip_reply(const struct kvaser_usb *dev,
1613 					    const struct kvaser_cmd *cmd)
1614 {
1615 	struct kvaser_usb_net_priv *priv;
1616 	u8 channel = cmd->u.simple.channel;
1617 
1618 	if (channel >= dev->nchannels) {
1619 		dev_err(&dev->intf->dev,
1620 			"Invalid channel number (%d)\n", channel);
1621 		return;
1622 	}
1623 
1624 	priv = dev->nets[channel];
1625 
1626 	complete(&priv->stop_comp);
1627 }
1628 
kvaser_usb_leaf_get_busparams_reply(const struct kvaser_usb * dev,const struct kvaser_cmd * cmd)1629 static void kvaser_usb_leaf_get_busparams_reply(const struct kvaser_usb *dev,
1630 						const struct kvaser_cmd *cmd)
1631 {
1632 	struct kvaser_usb_net_priv *priv;
1633 	u8 channel = cmd->u.busparams.channel;
1634 
1635 	if (channel >= dev->nchannels) {
1636 		dev_err(&dev->intf->dev,
1637 			"Invalid channel number (%d)\n", channel);
1638 		return;
1639 	}
1640 
1641 	priv = dev->nets[channel];
1642 	memcpy(&priv->busparams_nominal, &cmd->u.busparams.busparams,
1643 	       sizeof(priv->busparams_nominal));
1644 
1645 	complete(&priv->get_busparams_comp);
1646 }
1647 
kvaser_usb_leaf_handle_command(struct kvaser_usb * dev,const struct kvaser_cmd * cmd)1648 static void kvaser_usb_leaf_handle_command(struct kvaser_usb *dev,
1649 					   const struct kvaser_cmd *cmd)
1650 {
1651 	if (kvaser_usb_leaf_verify_size(dev, cmd) < 0)
1652 		return;
1653 
1654 	switch (cmd->id) {
1655 	case CMD_START_CHIP_REPLY:
1656 		kvaser_usb_leaf_start_chip_reply(dev, cmd);
1657 		break;
1658 
1659 	case CMD_STOP_CHIP_REPLY:
1660 		kvaser_usb_leaf_stop_chip_reply(dev, cmd);
1661 		break;
1662 
1663 	case CMD_RX_STD_MESSAGE:
1664 	case CMD_RX_EXT_MESSAGE:
1665 		kvaser_usb_leaf_rx_can_msg(dev, cmd);
1666 		break;
1667 
1668 	case CMD_LEAF_LOG_MESSAGE:
1669 		if (dev->driver_info->family != KVASER_LEAF)
1670 			goto warn;
1671 		kvaser_usb_leaf_rx_can_msg(dev, cmd);
1672 		break;
1673 
1674 	case CMD_CHIP_STATE_EVENT:
1675 	case CMD_CAN_ERROR_EVENT:
1676 		if (dev->driver_info->family == KVASER_LEAF)
1677 			kvaser_usb_leaf_leaf_rx_error(dev, cmd);
1678 		else
1679 			kvaser_usb_leaf_usbcan_rx_error(dev, cmd);
1680 		break;
1681 
1682 	case CMD_TX_ACKNOWLEDGE:
1683 		kvaser_usb_leaf_tx_acknowledge(dev, cmd);
1684 		break;
1685 
1686 	case CMD_ERROR_EVENT:
1687 		kvaser_usb_leaf_error_event(dev, cmd);
1688 		break;
1689 
1690 	case CMD_GET_BUS_PARAMS_REPLY:
1691 		kvaser_usb_leaf_get_busparams_reply(dev, cmd);
1692 		break;
1693 
1694 	case CMD_USBCAN_CLOCK_OVERFLOW_EVENT:
1695 		if (dev->driver_info->family != KVASER_USBCAN)
1696 			goto warn;
1697 		dev->card_data.usbcan_timestamp_msb =
1698 					le32_to_cpu(cmd->u.usbcan.clk_overflow_event.time) &
1699 					KVASER_USB_USBCAN_CLK_OVERFLOW_MASK;
1700 		break;
1701 
1702 	/* Ignored commands */
1703 	case CMD_FLUSH_QUEUE_REPLY:
1704 		if (dev->driver_info->family != KVASER_LEAF)
1705 			goto warn;
1706 		break;
1707 	case CMD_LED_ACTION_RESP:
1708 		break;
1709 
1710 	default:
1711 warn:		dev_warn(&dev->intf->dev, "Unhandled command (%d)\n", cmd->id);
1712 		break;
1713 	}
1714 }
1715 
kvaser_usb_leaf_read_bulk_callback(struct kvaser_usb * dev,void * buf,int len)1716 static void kvaser_usb_leaf_read_bulk_callback(struct kvaser_usb *dev,
1717 					       void *buf, int len)
1718 {
1719 	struct kvaser_cmd *cmd;
1720 	int pos = 0;
1721 
1722 	while (pos <= len - CMD_HEADER_LEN) {
1723 		cmd = buf + pos;
1724 
1725 		/* The Kvaser firmware can only read and write commands that
1726 		 * does not cross the USB's endpoint wMaxPacketSize boundary.
1727 		 * If a follow-up command crosses such boundary, firmware puts
1728 		 * a placeholder zero-length command in its place then aligns
1729 		 * the real command to the next max packet size.
1730 		 *
1731 		 * Handle such cases or we're going to miss a significant
1732 		 * number of events in case of a heavy rx load on the bus.
1733 		 */
1734 		if (cmd->len == 0) {
1735 			pos = round_up(pos, le16_to_cpu
1736 						(dev->bulk_in->wMaxPacketSize));
1737 			continue;
1738 		}
1739 
1740 		if (pos + cmd->len > len) {
1741 			dev_err_ratelimited(&dev->intf->dev, "Format error\n");
1742 			break;
1743 		}
1744 
1745 		kvaser_usb_leaf_handle_command(dev, cmd);
1746 		pos += cmd->len;
1747 	}
1748 }
1749 
kvaser_usb_leaf_set_opt_mode(const struct kvaser_usb_net_priv * priv)1750 static int kvaser_usb_leaf_set_opt_mode(const struct kvaser_usb_net_priv *priv)
1751 {
1752 	struct kvaser_cmd *cmd;
1753 	int rc;
1754 
1755 	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1756 	if (!cmd)
1757 		return -ENOMEM;
1758 
1759 	cmd->id = CMD_SET_CTRL_MODE;
1760 	cmd->len = CMD_HEADER_LEN + sizeof(struct kvaser_cmd_ctrl_mode);
1761 	cmd->u.ctrl_mode.tid = 0xff;
1762 	cmd->u.ctrl_mode.channel = priv->channel;
1763 
1764 	if (priv->can.ctrlmode & CAN_CTRLMODE_LISTENONLY)
1765 		cmd->u.ctrl_mode.ctrl_mode = KVASER_CTRL_MODE_SILENT;
1766 	else
1767 		cmd->u.ctrl_mode.ctrl_mode = KVASER_CTRL_MODE_NORMAL;
1768 
1769 	rc = kvaser_usb_send_cmd(priv->dev, cmd, cmd->len);
1770 
1771 	kfree(cmd);
1772 	return rc;
1773 }
1774 
kvaser_usb_leaf_start_chip(struct kvaser_usb_net_priv * priv)1775 static int kvaser_usb_leaf_start_chip(struct kvaser_usb_net_priv *priv)
1776 {
1777 	struct kvaser_usb_net_leaf_priv *leaf = priv->sub_priv;
1778 	int err;
1779 
1780 	leaf->joining_bus = true;
1781 
1782 	reinit_completion(&priv->start_comp);
1783 
1784 	err = kvaser_usb_leaf_send_simple_cmd(priv->dev, CMD_START_CHIP,
1785 					      priv->channel);
1786 	if (err)
1787 		return err;
1788 
1789 	if (!wait_for_completion_timeout(&priv->start_comp,
1790 					 msecs_to_jiffies(KVASER_USB_TIMEOUT)))
1791 		return -ETIMEDOUT;
1792 
1793 	return 0;
1794 }
1795 
kvaser_usb_leaf_stop_chip(struct kvaser_usb_net_priv * priv)1796 static int kvaser_usb_leaf_stop_chip(struct kvaser_usb_net_priv *priv)
1797 {
1798 	struct kvaser_usb_net_leaf_priv *leaf = priv->sub_priv;
1799 	int err;
1800 
1801 	reinit_completion(&priv->stop_comp);
1802 
1803 	cancel_delayed_work(&leaf->chip_state_req_work);
1804 
1805 	err = kvaser_usb_leaf_send_simple_cmd(priv->dev, CMD_STOP_CHIP,
1806 					      priv->channel);
1807 	if (err)
1808 		return err;
1809 
1810 	if (!wait_for_completion_timeout(&priv->stop_comp,
1811 					 msecs_to_jiffies(KVASER_USB_TIMEOUT)))
1812 		return -ETIMEDOUT;
1813 
1814 	return 0;
1815 }
1816 
kvaser_usb_leaf_reset_chip(struct kvaser_usb * dev,int channel)1817 static int kvaser_usb_leaf_reset_chip(struct kvaser_usb *dev, int channel)
1818 {
1819 	return kvaser_usb_leaf_send_simple_cmd(dev, CMD_RESET_CHIP, channel);
1820 }
1821 
kvaser_usb_leaf_flush_queue(struct kvaser_usb_net_priv * priv)1822 static int kvaser_usb_leaf_flush_queue(struct kvaser_usb_net_priv *priv)
1823 {
1824 	struct kvaser_cmd *cmd;
1825 	int rc;
1826 
1827 	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1828 	if (!cmd)
1829 		return -ENOMEM;
1830 
1831 	cmd->id = CMD_FLUSH_QUEUE;
1832 	cmd->len = CMD_HEADER_LEN + sizeof(struct kvaser_cmd_flush_queue);
1833 	cmd->u.flush_queue.channel = priv->channel;
1834 	cmd->u.flush_queue.flags = 0x00;
1835 
1836 	rc = kvaser_usb_send_cmd(priv->dev, cmd, cmd->len);
1837 
1838 	kfree(cmd);
1839 	return rc;
1840 }
1841 
kvaser_usb_leaf_init_card(struct kvaser_usb * dev)1842 static int kvaser_usb_leaf_init_card(struct kvaser_usb *dev)
1843 {
1844 	struct kvaser_usb_dev_card_data *card_data = &dev->card_data;
1845 
1846 	card_data->ctrlmode_supported |= CAN_CTRLMODE_3_SAMPLES;
1847 
1848 	return 0;
1849 }
1850 
kvaser_usb_leaf_init_channel(struct kvaser_usb_net_priv * priv)1851 static int kvaser_usb_leaf_init_channel(struct kvaser_usb_net_priv *priv)
1852 {
1853 	struct kvaser_usb_net_leaf_priv *leaf;
1854 
1855 	leaf = devm_kzalloc(&priv->dev->intf->dev, sizeof(*leaf), GFP_KERNEL);
1856 	if (!leaf)
1857 		return -ENOMEM;
1858 
1859 	leaf->net = priv;
1860 	INIT_DELAYED_WORK(&leaf->chip_state_req_work,
1861 			  kvaser_usb_leaf_chip_state_req_work);
1862 
1863 	priv->sub_priv = leaf;
1864 
1865 	return 0;
1866 }
1867 
kvaser_usb_leaf_remove_channel(struct kvaser_usb_net_priv * priv)1868 static void kvaser_usb_leaf_remove_channel(struct kvaser_usb_net_priv *priv)
1869 {
1870 	struct kvaser_usb_net_leaf_priv *leaf = priv->sub_priv;
1871 
1872 	if (leaf)
1873 		cancel_delayed_work_sync(&leaf->chip_state_req_work);
1874 }
1875 
kvaser_usb_leaf_set_bittiming(const struct net_device * netdev,const struct kvaser_usb_busparams * busparams)1876 static int kvaser_usb_leaf_set_bittiming(const struct net_device *netdev,
1877 					 const struct kvaser_usb_busparams *busparams)
1878 {
1879 	struct kvaser_usb_net_priv *priv = netdev_priv(netdev);
1880 	struct kvaser_usb *dev = priv->dev;
1881 	struct kvaser_cmd *cmd;
1882 	int rc;
1883 
1884 	cmd = kmalloc(sizeof(*cmd), GFP_KERNEL);
1885 	if (!cmd)
1886 		return -ENOMEM;
1887 
1888 	cmd->id = CMD_SET_BUS_PARAMS;
1889 	cmd->len = CMD_HEADER_LEN + sizeof(struct kvaser_cmd_busparams);
1890 	cmd->u.busparams.channel = priv->channel;
1891 	cmd->u.busparams.tid = 0xff;
1892 	memcpy(&cmd->u.busparams.busparams, busparams,
1893 	       sizeof(cmd->u.busparams.busparams));
1894 
1895 	rc = kvaser_usb_send_cmd(dev, cmd, cmd->len);
1896 
1897 	kfree(cmd);
1898 	return rc;
1899 }
1900 
kvaser_usb_leaf_get_busparams(struct kvaser_usb_net_priv * priv)1901 static int kvaser_usb_leaf_get_busparams(struct kvaser_usb_net_priv *priv)
1902 {
1903 	int err;
1904 
1905 	if (priv->dev->driver_info->family == KVASER_USBCAN)
1906 		return -EOPNOTSUPP;
1907 
1908 	reinit_completion(&priv->get_busparams_comp);
1909 
1910 	err = kvaser_usb_leaf_send_simple_cmd(priv->dev, CMD_GET_BUS_PARAMS,
1911 					      priv->channel);
1912 	if (err)
1913 		return err;
1914 
1915 	if (!wait_for_completion_timeout(&priv->get_busparams_comp,
1916 					 msecs_to_jiffies(KVASER_USB_TIMEOUT)))
1917 		return -ETIMEDOUT;
1918 
1919 	return 0;
1920 }
1921 
kvaser_usb_leaf_set_mode(struct net_device * netdev,enum can_mode mode)1922 static int kvaser_usb_leaf_set_mode(struct net_device *netdev,
1923 				    enum can_mode mode)
1924 {
1925 	struct kvaser_usb_net_priv *priv = netdev_priv(netdev);
1926 	struct kvaser_usb_net_leaf_priv *leaf = priv->sub_priv;
1927 	int err;
1928 
1929 	switch (mode) {
1930 	case CAN_MODE_START:
1931 		kvaser_usb_unlink_tx_urbs(priv);
1932 
1933 		leaf->joining_bus = true;
1934 
1935 		err = kvaser_usb_leaf_simple_cmd_async(priv, CMD_START_CHIP);
1936 		if (err)
1937 			return err;
1938 
1939 		priv->can.state = CAN_STATE_ERROR_ACTIVE;
1940 		break;
1941 	default:
1942 		return -EOPNOTSUPP;
1943 	}
1944 
1945 	return 0;
1946 }
1947 
kvaser_usb_leaf_get_berr_counter(const struct net_device * netdev,struct can_berr_counter * bec)1948 static int kvaser_usb_leaf_get_berr_counter(const struct net_device *netdev,
1949 					    struct can_berr_counter *bec)
1950 {
1951 	struct kvaser_usb_net_priv *priv = netdev_priv(netdev);
1952 
1953 	*bec = priv->bec;
1954 
1955 	return 0;
1956 }
1957 
kvaser_usb_leaf_setup_endpoints(struct kvaser_usb * dev)1958 static int kvaser_usb_leaf_setup_endpoints(struct kvaser_usb *dev)
1959 {
1960 	const struct usb_host_interface *iface_desc;
1961 	struct usb_endpoint_descriptor *endpoint;
1962 	int i;
1963 
1964 	iface_desc = dev->intf->cur_altsetting;
1965 
1966 	for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
1967 		endpoint = &iface_desc->endpoint[i].desc;
1968 
1969 		if (!dev->bulk_in && usb_endpoint_is_bulk_in(endpoint))
1970 			dev->bulk_in = endpoint;
1971 
1972 		if (!dev->bulk_out && usb_endpoint_is_bulk_out(endpoint))
1973 			dev->bulk_out = endpoint;
1974 
1975 		/* use first bulk endpoint for in and out */
1976 		if (dev->bulk_in && dev->bulk_out)
1977 			return 0;
1978 	}
1979 
1980 	return -ENODEV;
1981 }
1982 
1983 const struct kvaser_usb_dev_ops kvaser_usb_leaf_dev_ops = {
1984 	.dev_set_mode = kvaser_usb_leaf_set_mode,
1985 	.dev_set_bittiming = kvaser_usb_leaf_set_bittiming,
1986 	.dev_get_busparams = kvaser_usb_leaf_get_busparams,
1987 	.dev_set_data_bittiming = NULL,
1988 	.dev_get_data_busparams = NULL,
1989 	.dev_get_berr_counter = kvaser_usb_leaf_get_berr_counter,
1990 	.dev_setup_endpoints = kvaser_usb_leaf_setup_endpoints,
1991 	.dev_init_card = kvaser_usb_leaf_init_card,
1992 	.dev_init_channel = kvaser_usb_leaf_init_channel,
1993 	.dev_remove_channel = kvaser_usb_leaf_remove_channel,
1994 	.dev_get_software_info = kvaser_usb_leaf_get_software_info,
1995 	.dev_get_software_details = NULL,
1996 	.dev_get_card_info = kvaser_usb_leaf_get_card_info,
1997 	.dev_get_capabilities = kvaser_usb_leaf_get_capabilities,
1998 	.dev_set_led = kvaser_usb_leaf_set_led,
1999 	.dev_set_opt_mode = kvaser_usb_leaf_set_opt_mode,
2000 	.dev_start_chip = kvaser_usb_leaf_start_chip,
2001 	.dev_stop_chip = kvaser_usb_leaf_stop_chip,
2002 	.dev_reset_chip = kvaser_usb_leaf_reset_chip,
2003 	.dev_flush_queue = kvaser_usb_leaf_flush_queue,
2004 	.dev_read_bulk_callback = kvaser_usb_leaf_read_bulk_callback,
2005 	.dev_frame_to_cmd = kvaser_usb_leaf_frame_to_cmd,
2006 };
2007