xref: /linux/drivers/net/can/usb/kvaser_usb/kvaser_usb_leaf.c (revision fab183d632628381b466a41479489541ac0e29a0)
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_obj(*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 + 1,
689 					       le16_to_cpu
690 						(dev->bulk_in->wMaxPacketSize));
691 				continue;
692 			}
693 
694 			if (tmp->len < CMD_HEADER_LEN ||
695 			    tmp->len > actual_len - pos) {
696 				dev_err_ratelimited(&dev->intf->dev,
697 						    "Format error\n");
698 				break;
699 			}
700 
701 			if (tmp->id == id) {
702 				if (tmp->len > sizeof(*cmd)) {
703 					dev_err_ratelimited(&dev->intf->dev,
704 							    "Received command %u too large (%u)\n",
705 							    tmp->id, tmp->len);
706 					err = -EIO;
707 					goto end;
708 				}
709 
710 				memcpy(cmd, tmp, tmp->len);
711 				goto end;
712 			}
713 
714 			pos += tmp->len;
715 		}
716 	} while (time_before(jiffies, to));
717 
718 	err = -EINVAL;
719 
720 end:
721 	kfree(buf);
722 
723 	if (err == 0)
724 		err = kvaser_usb_leaf_verify_size(dev, cmd);
725 
726 	return err;
727 }
728 
kvaser_usb_leaf_send_simple_cmd(const struct kvaser_usb * dev,u8 cmd_id,int channel)729 static int kvaser_usb_leaf_send_simple_cmd(const struct kvaser_usb *dev,
730 					   u8 cmd_id, int channel)
731 {
732 	struct kvaser_cmd *cmd;
733 	int rc;
734 
735 	cmd = kmalloc_obj(*cmd);
736 	if (!cmd)
737 		return -ENOMEM;
738 
739 	cmd->id = cmd_id;
740 	cmd->len = CMD_HEADER_LEN + sizeof(struct kvaser_cmd_simple);
741 	cmd->u.simple.channel = channel;
742 	cmd->u.simple.tid = 0xff;
743 
744 	rc = kvaser_usb_send_cmd(dev, cmd, cmd->len);
745 
746 	kfree(cmd);
747 	return rc;
748 }
749 
kvaser_usb_leaf_get_software_info_leaf(struct kvaser_usb * dev,const struct leaf_cmd_softinfo * softinfo)750 static void kvaser_usb_leaf_get_software_info_leaf(struct kvaser_usb *dev,
751 						   const struct leaf_cmd_softinfo *softinfo)
752 {
753 	u32 fw_version;
754 	u32 sw_options = le32_to_cpu(softinfo->sw_options);
755 
756 	fw_version = le32_to_cpu(softinfo->fw_version);
757 	dev->fw_version.major = FIELD_GET(KVASER_USB_SW_VERSION_MAJOR_MASK, fw_version);
758 	dev->fw_version.minor = FIELD_GET(KVASER_USB_SW_VERSION_MINOR_MASK, fw_version);
759 	dev->fw_version.build = FIELD_GET(KVASER_USB_SW_VERSION_BUILD_MASK, fw_version);
760 	dev->max_tx_urbs = le16_to_cpu(softinfo->max_outstanding_tx);
761 
762 	if (sw_options & KVASER_USB_LEAF_SWOPTION_EXT_CAP)
763 		dev->card_data.capabilities |= KVASER_USB_CAP_EXT_CAP;
764 
765 	if (dev->driver_info->quirks & KVASER_USB_QUIRK_IGNORE_CLK_FREQ) {
766 		/* Firmware expects bittiming parameters calculated for 16MHz
767 		 * clock, regardless of the actual clock
768 		 * Though, the reported freq is used for timestamps
769 		 */
770 		switch (sw_options & KVASER_USB_LEAF_SWOPTION_FREQ_MASK) {
771 		case KVASER_USB_LEAF_SWOPTION_FREQ_16_MHZ_CLK:
772 			dev->cfg = &kvaser_usb_leaf_m32c_dev_cfg_16mhz;
773 			break;
774 		case KVASER_USB_LEAF_SWOPTION_FREQ_24_MHZ_CLK:
775 			dev->cfg = &kvaser_usb_leaf_m32c_dev_cfg_24mhz;
776 			break;
777 		case KVASER_USB_LEAF_SWOPTION_FREQ_32_MHZ_CLK:
778 			dev->cfg = &kvaser_usb_leaf_m32c_dev_cfg_32mhz;
779 			break;
780 		}
781 	} else {
782 		switch (sw_options & KVASER_USB_LEAF_SWOPTION_FREQ_MASK) {
783 		case KVASER_USB_LEAF_SWOPTION_FREQ_16_MHZ_CLK:
784 			dev->cfg = &kvaser_usb_leaf_imx_dev_cfg_16mhz;
785 			break;
786 		case KVASER_USB_LEAF_SWOPTION_FREQ_24_MHZ_CLK:
787 			dev->cfg = &kvaser_usb_leaf_imx_dev_cfg_24mhz;
788 			break;
789 		case KVASER_USB_LEAF_SWOPTION_FREQ_32_MHZ_CLK:
790 			dev->cfg = &kvaser_usb_leaf_imx_dev_cfg_32mhz;
791 			break;
792 		}
793 	}
794 }
795 
kvaser_usb_leaf_get_software_info_inner(struct kvaser_usb * dev)796 static int kvaser_usb_leaf_get_software_info_inner(struct kvaser_usb *dev)
797 {
798 	struct kvaser_cmd cmd;
799 	int err;
800 	u32 fw_version;
801 
802 	err = kvaser_usb_leaf_send_simple_cmd(dev, CMD_GET_SOFTWARE_INFO, 0);
803 	if (err)
804 		return err;
805 
806 	err = kvaser_usb_leaf_wait_cmd(dev, CMD_GET_SOFTWARE_INFO_REPLY, &cmd);
807 	if (err)
808 		return err;
809 
810 	switch (dev->driver_info->family) {
811 	case KVASER_LEAF:
812 		kvaser_usb_leaf_get_software_info_leaf(dev, &cmd.u.leaf.softinfo);
813 		break;
814 	case KVASER_USBCAN:
815 		fw_version = le32_to_cpu(cmd.u.usbcan.softinfo.fw_version);
816 		dev->fw_version.major = FIELD_GET(KVASER_USB_SW_VERSION_MAJOR_MASK,
817 						  fw_version);
818 		dev->fw_version.minor = FIELD_GET(KVASER_USB_SW_VERSION_MINOR_MASK,
819 						  fw_version);
820 		dev->fw_version.build = FIELD_GET(KVASER_USB_SW_VERSION_BUILD_MASK,
821 						  fw_version);
822 		dev->max_tx_urbs =
823 			le16_to_cpu(cmd.u.usbcan.softinfo.max_outstanding_tx);
824 		dev->cfg = &kvaser_usb_leaf_usbcan_dev_cfg;
825 		break;
826 	}
827 
828 	return 0;
829 }
830 
kvaser_usb_leaf_get_software_info(struct kvaser_usb * dev)831 static int kvaser_usb_leaf_get_software_info(struct kvaser_usb *dev)
832 {
833 	int err;
834 	int retry = 3;
835 
836 	/* On some x86 laptops, plugging a Kvaser device again after
837 	 * an unplug makes the firmware always ignore the very first
838 	 * command. For such a case, provide some room for retries
839 	 * instead of completely exiting the driver.
840 	 */
841 	do {
842 		err = kvaser_usb_leaf_get_software_info_inner(dev);
843 	} while (--retry && err == -ETIMEDOUT);
844 
845 	return err;
846 }
847 
kvaser_usb_leaf_get_card_info(struct kvaser_usb * dev)848 static int kvaser_usb_leaf_get_card_info(struct kvaser_usb *dev)
849 {
850 	struct kvaser_cmd cmd;
851 	int err;
852 
853 	err = kvaser_usb_leaf_send_simple_cmd(dev, CMD_GET_CARD_INFO, 0);
854 	if (err)
855 		return err;
856 
857 	err = kvaser_usb_leaf_wait_cmd(dev, CMD_GET_CARD_INFO_REPLY, &cmd);
858 	if (err)
859 		return err;
860 
861 	dev->nchannels = cmd.u.cardinfo.nchannels;
862 	if (dev->nchannels > KVASER_USB_MAX_NET_DEVICES ||
863 	    (dev->driver_info->family == KVASER_USBCAN &&
864 	     dev->nchannels > MAX_USBCAN_NET_DEVICES))
865 		return -EINVAL;
866 	dev->ean[1] = le32_to_cpu(cmd.u.cardinfo.ean[1]);
867 	dev->ean[0] = le32_to_cpu(cmd.u.cardinfo.ean[0]);
868 	dev->serial_number = le32_to_cpu(cmd.u.cardinfo.serial_number);
869 	dev->hw_revision = cmd.u.cardinfo.hw_revision;
870 
871 	return 0;
872 }
873 
kvaser_usb_leaf_get_single_capability(struct kvaser_usb * dev,u16 cap_cmd_req,u16 * status)874 static int kvaser_usb_leaf_get_single_capability(struct kvaser_usb *dev,
875 						 u16 cap_cmd_req, u16 *status)
876 {
877 	struct kvaser_usb_dev_card_data *card_data = &dev->card_data;
878 	struct kvaser_cmd *cmd;
879 	u32 value = 0;
880 	u32 mask = 0;
881 	u16 cap_cmd_res;
882 	int err;
883 	int i;
884 
885 	cmd = kzalloc_obj(*cmd);
886 	if (!cmd)
887 		return -ENOMEM;
888 
889 	cmd->id = CMD_GET_CAPABILITIES_REQ;
890 	cmd->u.leaf.cap_req.cap_cmd = cpu_to_le16(cap_cmd_req);
891 	cmd->len = CMD_HEADER_LEN + sizeof(struct kvaser_cmd_cap_req);
892 
893 	err = kvaser_usb_send_cmd(dev, cmd, cmd->len);
894 	if (err)
895 		goto end;
896 
897 	err = kvaser_usb_leaf_wait_cmd(dev, CMD_GET_CAPABILITIES_RESP, cmd);
898 	if (err)
899 		goto end;
900 
901 	*status = le16_to_cpu(cmd->u.leaf.cap_res.status);
902 
903 	if (*status != KVASER_USB_LEAF_CAP_STAT_OK)
904 		goto end;
905 
906 	cap_cmd_res = le16_to_cpu(cmd->u.leaf.cap_res.cap_cmd);
907 	switch (cap_cmd_res) {
908 	case KVASER_USB_LEAF_CAP_CMD_LISTEN_MODE:
909 	case KVASER_USB_LEAF_CAP_CMD_ERR_REPORT:
910 		value = le32_to_cpu(cmd->u.leaf.cap_res.value);
911 		mask = le32_to_cpu(cmd->u.leaf.cap_res.mask);
912 		break;
913 	default:
914 		dev_warn(&dev->intf->dev, "Unknown capability command %u\n",
915 			 cap_cmd_res);
916 		break;
917 	}
918 
919 	for (i = 0; i < dev->nchannels; i++) {
920 		if (BIT(i) & (value & mask)) {
921 			switch (cap_cmd_res) {
922 			case KVASER_USB_LEAF_CAP_CMD_LISTEN_MODE:
923 				card_data->ctrlmode_supported |=
924 						CAN_CTRLMODE_LISTENONLY;
925 				break;
926 			case KVASER_USB_LEAF_CAP_CMD_ERR_REPORT:
927 				card_data->capabilities |=
928 						KVASER_USB_CAP_BERR_CAP;
929 				break;
930 			}
931 		}
932 	}
933 
934 end:
935 	kfree(cmd);
936 
937 	return err;
938 }
939 
kvaser_usb_leaf_get_capabilities_leaf(struct kvaser_usb * dev)940 static int kvaser_usb_leaf_get_capabilities_leaf(struct kvaser_usb *dev)
941 {
942 	int err;
943 	u16 status;
944 
945 	if (!(dev->card_data.capabilities & KVASER_USB_CAP_EXT_CAP)) {
946 		dev_info(&dev->intf->dev,
947 			 "No extended capability support. Upgrade device firmware.\n");
948 		return 0;
949 	}
950 
951 	err = kvaser_usb_leaf_get_single_capability(dev,
952 						    KVASER_USB_LEAF_CAP_CMD_LISTEN_MODE,
953 						    &status);
954 	if (err)
955 		return err;
956 	if (status)
957 		dev_info(&dev->intf->dev,
958 			 "KVASER_USB_LEAF_CAP_CMD_LISTEN_MODE failed %u\n",
959 			 status);
960 
961 	err = kvaser_usb_leaf_get_single_capability(dev,
962 						    KVASER_USB_LEAF_CAP_CMD_ERR_REPORT,
963 						    &status);
964 	if (err)
965 		return err;
966 	if (status)
967 		dev_info(&dev->intf->dev,
968 			 "KVASER_USB_LEAF_CAP_CMD_ERR_REPORT failed %u\n",
969 			 status);
970 
971 	return 0;
972 }
973 
kvaser_usb_leaf_set_led(struct kvaser_usb_net_priv * priv,enum kvaser_usb_led_state state,u16 duration_ms)974 static int kvaser_usb_leaf_set_led(struct kvaser_usb_net_priv *priv,
975 				   enum kvaser_usb_led_state state,
976 				   u16 duration_ms)
977 {
978 	struct kvaser_usb *dev = priv->dev;
979 	struct kvaser_cmd *cmd;
980 	int ret;
981 
982 	cmd = kzalloc_obj(*cmd);
983 	if (!cmd)
984 		return -ENOMEM;
985 
986 	cmd->id = CMD_LED_ACTION_REQ;
987 	cmd->len = CMD_HEADER_LEN + sizeof(struct kvaser_cmd_led_action_req);
988 	cmd->u.led_action_req.tid = 0xff;
989 
990 	cmd->u.led_action_req.duration_ms = cpu_to_le16(duration_ms);
991 	cmd->u.led_action_req.action = state |
992 				       FIELD_PREP(KVASER_USB_LEAF_LED_IDX_MASK,
993 						  KVASER_USB_LEAF_LED_YELLOW_CH0_IDX +
994 						  priv->channel);
995 
996 	ret = kvaser_usb_send_cmd(dev, cmd, cmd->len);
997 	kfree(cmd);
998 
999 	return ret;
1000 }
1001 
kvaser_usb_leaf_get_capabilities(struct kvaser_usb * dev)1002 static int kvaser_usb_leaf_get_capabilities(struct kvaser_usb *dev)
1003 {
1004 	int err = 0;
1005 
1006 	if (dev->driver_info->family == KVASER_LEAF)
1007 		err = kvaser_usb_leaf_get_capabilities_leaf(dev);
1008 
1009 	return err;
1010 }
1011 
kvaser_usb_leaf_tx_acknowledge(const struct kvaser_usb * dev,const struct kvaser_cmd * cmd)1012 static void kvaser_usb_leaf_tx_acknowledge(const struct kvaser_usb *dev,
1013 					   const struct kvaser_cmd *cmd)
1014 {
1015 	struct net_device_stats *stats;
1016 	struct kvaser_usb_tx_urb_context *context;
1017 	struct kvaser_usb_net_priv *priv;
1018 	unsigned long flags;
1019 	u8 channel, tid;
1020 	struct sk_buff *skb;
1021 	ktime_t hwtstamp = 0;
1022 
1023 	channel = cmd->u.tx_acknowledge_header.channel;
1024 	tid = cmd->u.tx_acknowledge_header.tid;
1025 
1026 	if (channel >= dev->nchannels) {
1027 		dev_err(&dev->intf->dev,
1028 			"Invalid channel number (%d)\n", channel);
1029 		return;
1030 	}
1031 
1032 	priv = dev->nets[channel];
1033 
1034 	if (!netif_device_present(priv->netdev))
1035 		return;
1036 
1037 	stats = &priv->netdev->stats;
1038 
1039 	context = &priv->tx_contexts[tid % dev->max_tx_urbs];
1040 
1041 	/* Sometimes the state change doesn't come after a bus-off event */
1042 	if (priv->can.restart_ms && priv->can.state == CAN_STATE_BUS_OFF) {
1043 		struct sk_buff *err_skb;
1044 		struct can_frame *cf;
1045 
1046 		err_skb = alloc_can_err_skb(priv->netdev, &cf);
1047 		if (err_skb) {
1048 			cf->can_id |= CAN_ERR_RESTARTED;
1049 
1050 			netif_rx(err_skb);
1051 		} else {
1052 			netdev_err(priv->netdev,
1053 				   "No memory left for err_skb\n");
1054 		}
1055 
1056 		priv->can.can_stats.restarts++;
1057 		netif_carrier_on(priv->netdev);
1058 
1059 		priv->can.state = CAN_STATE_ERROR_ACTIVE;
1060 	}
1061 	switch (dev->driver_info->family) {
1062 	case KVASER_LEAF:
1063 		hwtstamp = kvaser_usb_timestamp48_to_ktime(dev->cfg, cmd->u.leaf.tx_ack.time);
1064 		break;
1065 	case KVASER_USBCAN:
1066 		hwtstamp = kvaser_usb_usbcan_timestamp_to_ktime(dev, cmd->u.usbcan.tx_ack.time);
1067 		break;
1068 	}
1069 
1070 	spin_lock_irqsave(&priv->tx_contexts_lock, flags);
1071 
1072 	skb = priv->can.echo_skb[context->echo_index];
1073 	if (skb)
1074 		skb_hwtstamps(skb)->hwtstamp = hwtstamp;
1075 	stats->tx_packets++;
1076 	stats->tx_bytes += can_get_echo_skb(priv->netdev,
1077 					    context->echo_index, NULL);
1078 	context->echo_index = dev->max_tx_urbs;
1079 	--priv->active_tx_contexts;
1080 	netif_wake_queue(priv->netdev);
1081 
1082 	spin_unlock_irqrestore(&priv->tx_contexts_lock, flags);
1083 }
1084 
kvaser_usb_leaf_simple_cmd_async(struct kvaser_usb_net_priv * priv,u8 cmd_id)1085 static int kvaser_usb_leaf_simple_cmd_async(struct kvaser_usb_net_priv *priv,
1086 					    u8 cmd_id)
1087 {
1088 	struct kvaser_cmd *cmd;
1089 	int err;
1090 
1091 	cmd = kzalloc_obj(*cmd, GFP_ATOMIC);
1092 	if (!cmd)
1093 		return -ENOMEM;
1094 
1095 	cmd->len = CMD_HEADER_LEN + sizeof(struct kvaser_cmd_simple);
1096 	cmd->id = cmd_id;
1097 	cmd->u.simple.channel = priv->channel;
1098 
1099 	err = kvaser_usb_send_cmd_async(priv, cmd, cmd->len);
1100 	if (err)
1101 		kfree(cmd);
1102 
1103 	return err;
1104 }
1105 
kvaser_usb_leaf_chip_state_req_work(struct work_struct * work)1106 static void kvaser_usb_leaf_chip_state_req_work(struct work_struct *work)
1107 {
1108 	struct kvaser_usb_net_leaf_priv *leaf =
1109 		container_of(work, struct kvaser_usb_net_leaf_priv,
1110 			     chip_state_req_work.work);
1111 	struct kvaser_usb_net_priv *priv = leaf->net;
1112 
1113 	kvaser_usb_leaf_simple_cmd_async(priv, CMD_GET_CHIP_STATE);
1114 }
1115 
1116 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)1117 kvaser_usb_leaf_rx_error_update_can_state(struct kvaser_usb_net_priv *priv,
1118 					const struct kvaser_usb_err_summary *es,
1119 					struct can_frame *cf)
1120 {
1121 	struct kvaser_usb_net_leaf_priv *leaf = priv->sub_priv;
1122 	struct kvaser_usb *dev = priv->dev;
1123 	struct net_device_stats *stats = &priv->netdev->stats;
1124 	enum can_state cur_state, new_state, tx_state, rx_state;
1125 
1126 	netdev_dbg(priv->netdev, "Error status: 0x%02x\n", es->status);
1127 
1128 	new_state = priv->can.state;
1129 	cur_state = priv->can.state;
1130 
1131 	if (es->status & (M16C_STATE_BUS_OFF | M16C_STATE_BUS_RESET)) {
1132 		new_state = CAN_STATE_BUS_OFF;
1133 	} else if (es->status & M16C_STATE_BUS_PASSIVE) {
1134 		new_state = CAN_STATE_ERROR_PASSIVE;
1135 	} else if ((es->status & M16C_STATE_BUS_ERROR) &&
1136 		   cur_state >= CAN_STATE_BUS_OFF) {
1137 		/* Guard against spurious error events after a busoff */
1138 	} else if (es->txerr >= 128 || es->rxerr >= 128) {
1139 		new_state = CAN_STATE_ERROR_PASSIVE;
1140 	} else if (es->txerr >= 96 || es->rxerr >= 96) {
1141 		new_state = CAN_STATE_ERROR_WARNING;
1142 	} else {
1143 		new_state = CAN_STATE_ERROR_ACTIVE;
1144 	}
1145 
1146 	/* 0bfd:0124 FW 4.18.778 was observed to send the initial
1147 	 * CMD_CHIP_STATE_EVENT after CMD_START_CHIP with M16C_STATE_BUS_OFF
1148 	 * bit set if the channel was bus-off when it was last stopped (even
1149 	 * across chip resets). This bit will clear shortly afterwards, without
1150 	 * triggering a second unsolicited chip state event.
1151 	 * Ignore this initial bus-off.
1152 	 */
1153 	if (leaf->joining_bus) {
1154 		if (new_state == CAN_STATE_BUS_OFF) {
1155 			netdev_dbg(priv->netdev, "ignoring bus-off during startup");
1156 			new_state = cur_state;
1157 		} else {
1158 			leaf->joining_bus = false;
1159 		}
1160 	}
1161 
1162 	if (new_state != cur_state) {
1163 		tx_state = (es->txerr >= es->rxerr) ? new_state : 0;
1164 		rx_state = (es->txerr <= es->rxerr) ? new_state : 0;
1165 
1166 		can_change_state(priv->netdev, cf, tx_state, rx_state);
1167 	}
1168 
1169 	if (priv->can.restart_ms &&
1170 	    cur_state == CAN_STATE_BUS_OFF &&
1171 	    new_state < CAN_STATE_BUS_OFF)
1172 		priv->can.can_stats.restarts++;
1173 
1174 	switch (dev->driver_info->family) {
1175 	case KVASER_LEAF:
1176 		if (es->leaf.error_factor) {
1177 			priv->can.can_stats.bus_error++;
1178 			stats->rx_errors++;
1179 		}
1180 		break;
1181 	case KVASER_USBCAN:
1182 		if (es->usbcan.error_state & USBCAN_ERROR_STATE_TX_ERROR)
1183 			stats->tx_errors++;
1184 		if (es->usbcan.error_state & USBCAN_ERROR_STATE_RX_ERROR)
1185 			stats->rx_errors++;
1186 		if (es->usbcan.error_state & USBCAN_ERROR_STATE_BUSERROR)
1187 			priv->can.can_stats.bus_error++;
1188 		break;
1189 	}
1190 
1191 	priv->bec.txerr = es->txerr;
1192 	priv->bec.rxerr = es->rxerr;
1193 }
1194 
kvaser_usb_leaf_rx_error(const struct kvaser_usb * dev,const struct kvaser_usb_err_summary * es)1195 static void kvaser_usb_leaf_rx_error(const struct kvaser_usb *dev,
1196 				     const struct kvaser_usb_err_summary *es)
1197 {
1198 	struct can_frame *cf = NULL;
1199 	struct sk_buff *skb = NULL;
1200 	struct net_device_stats *stats;
1201 	struct kvaser_usb_net_priv *priv;
1202 	struct kvaser_usb_net_leaf_priv *leaf;
1203 	enum can_state old_state, new_state;
1204 
1205 	if (es->channel >= dev->nchannels) {
1206 		dev_err(&dev->intf->dev,
1207 			"Invalid channel number (%d)\n", es->channel);
1208 		return;
1209 	}
1210 
1211 	priv = dev->nets[es->channel];
1212 	leaf = priv->sub_priv;
1213 	stats = &priv->netdev->stats;
1214 
1215 	/* Ignore e.g. state change to bus-off reported just after stopping */
1216 	if (!netif_running(priv->netdev))
1217 		return;
1218 
1219 	old_state = priv->can.state;
1220 	if (priv->can.ctrlmode & CAN_CTRLMODE_BERR_REPORTING)
1221 		skb = alloc_can_err_skb(priv->netdev, &cf);
1222 	kvaser_usb_leaf_rx_error_update_can_state(priv, es, cf);
1223 	new_state = priv->can.state;
1224 
1225 	/* If there are errors, request status updates periodically as we do
1226 	 * not get automatic notifications of improved state.
1227 	 * Also request updates if we saw a stale BUS_OFF during startup
1228 	 * (joining_bus).
1229 	 */
1230 	if (new_state < CAN_STATE_BUS_OFF &&
1231 	    (es->rxerr || es->txerr || new_state == CAN_STATE_ERROR_PASSIVE ||
1232 	     leaf->joining_bus))
1233 		schedule_delayed_work(&leaf->chip_state_req_work,
1234 				      msecs_to_jiffies(500));
1235 
1236 	if (new_state != old_state) {
1237 		if (es->status &
1238 		    (M16C_STATE_BUS_OFF | M16C_STATE_BUS_RESET)) {
1239 			if (!priv->can.restart_ms)
1240 				kvaser_usb_leaf_simple_cmd_async(priv,
1241 								 CMD_STOP_CHIP);
1242 			netif_carrier_off(priv->netdev);
1243 		}
1244 
1245 		if (priv->can.restart_ms &&
1246 		    old_state == CAN_STATE_BUS_OFF &&
1247 		    new_state < CAN_STATE_BUS_OFF) {
1248 			if (cf)
1249 				cf->can_id |= CAN_ERR_RESTARTED;
1250 			netif_carrier_on(priv->netdev);
1251 		}
1252 	}
1253 
1254 	if (!skb) {
1255 		if (priv->can.ctrlmode & CAN_CTRLMODE_BERR_REPORTING) {
1256 			stats->rx_dropped++;
1257 			netdev_warn(priv->netdev, "No memory left for err_skb\n");
1258 		}
1259 		return;
1260 	}
1261 
1262 	switch (dev->driver_info->family) {
1263 	case KVASER_LEAF:
1264 		if (es->leaf.error_factor) {
1265 			cf->can_id |= CAN_ERR_BUSERROR | CAN_ERR_PROT;
1266 
1267 			if (es->leaf.error_factor & M16C_EF_ACKE)
1268 				cf->data[3] = CAN_ERR_PROT_LOC_ACK;
1269 			if (es->leaf.error_factor & M16C_EF_CRCE)
1270 				cf->data[3] = CAN_ERR_PROT_LOC_CRC_SEQ;
1271 			if (es->leaf.error_factor & M16C_EF_FORME)
1272 				cf->data[2] |= CAN_ERR_PROT_FORM;
1273 			if (es->leaf.error_factor & M16C_EF_STFE)
1274 				cf->data[2] |= CAN_ERR_PROT_STUFF;
1275 			if (es->leaf.error_factor & M16C_EF_BITE0)
1276 				cf->data[2] |= CAN_ERR_PROT_BIT0;
1277 			if (es->leaf.error_factor & M16C_EF_BITE1)
1278 				cf->data[2] |= CAN_ERR_PROT_BIT1;
1279 			if (es->leaf.error_factor & M16C_EF_TRE)
1280 				cf->data[2] |= CAN_ERR_PROT_TX;
1281 		}
1282 		break;
1283 	case KVASER_USBCAN:
1284 		if (es->usbcan.error_state & USBCAN_ERROR_STATE_BUSERROR)
1285 			cf->can_id |= CAN_ERR_BUSERROR;
1286 		break;
1287 	}
1288 
1289 	if (new_state != CAN_STATE_BUS_OFF) {
1290 		cf->can_id |= CAN_ERR_CNT;
1291 		cf->data[6] = es->txerr;
1292 		cf->data[7] = es->rxerr;
1293 	}
1294 
1295 	netif_rx(skb);
1296 }
1297 
1298 /* For USBCAN, report error to userspace if the channels's errors counter
1299  * has changed, or we're the only channel seeing a bus error state.
1300  */
1301 static void
kvaser_usb_leaf_usbcan_conditionally_rx_error(const struct kvaser_usb * dev,struct kvaser_usb_err_summary * es)1302 kvaser_usb_leaf_usbcan_conditionally_rx_error(const struct kvaser_usb *dev,
1303 					      struct kvaser_usb_err_summary *es)
1304 {
1305 	struct kvaser_usb_net_priv *priv;
1306 	unsigned int channel;
1307 	bool report_error;
1308 
1309 	channel = es->channel;
1310 	if (channel >= dev->nchannels) {
1311 		dev_err(&dev->intf->dev,
1312 			"Invalid channel number (%d)\n", channel);
1313 		return;
1314 	}
1315 
1316 	priv = dev->nets[channel];
1317 	report_error = false;
1318 
1319 	if (es->txerr != priv->bec.txerr) {
1320 		es->usbcan.error_state |= USBCAN_ERROR_STATE_TX_ERROR;
1321 		report_error = true;
1322 	}
1323 	if (es->rxerr != priv->bec.rxerr) {
1324 		es->usbcan.error_state |= USBCAN_ERROR_STATE_RX_ERROR;
1325 		report_error = true;
1326 	}
1327 	if ((es->status & M16C_STATE_BUS_ERROR) &&
1328 	    !(es->usbcan.other_ch_status & M16C_STATE_BUS_ERROR)) {
1329 		es->usbcan.error_state |= USBCAN_ERROR_STATE_BUSERROR;
1330 		report_error = true;
1331 	}
1332 
1333 	if (report_error)
1334 		kvaser_usb_leaf_rx_error(dev, es);
1335 }
1336 
kvaser_usb_leaf_usbcan_rx_error(const struct kvaser_usb * dev,const struct kvaser_cmd * cmd)1337 static void kvaser_usb_leaf_usbcan_rx_error(const struct kvaser_usb *dev,
1338 					    const struct kvaser_cmd *cmd)
1339 {
1340 	struct kvaser_usb_err_summary es = { };
1341 
1342 	switch (cmd->id) {
1343 	/* Sometimes errors are sent as unsolicited chip state events */
1344 	case CMD_CHIP_STATE_EVENT:
1345 		es.channel = cmd->u.usbcan.chip_state_event.channel;
1346 		es.status = cmd->u.usbcan.chip_state_event.status;
1347 		es.txerr = cmd->u.usbcan.chip_state_event.tx_errors_count;
1348 		es.rxerr = cmd->u.usbcan.chip_state_event.rx_errors_count;
1349 		kvaser_usb_leaf_usbcan_conditionally_rx_error(dev, &es);
1350 		break;
1351 
1352 	case CMD_CAN_ERROR_EVENT:
1353 		es.channel = 0;
1354 		es.status = cmd->u.usbcan.can_error_event.status_ch0;
1355 		es.txerr = cmd->u.usbcan.can_error_event.tx_errors_count_ch0;
1356 		es.rxerr = cmd->u.usbcan.can_error_event.rx_errors_count_ch0;
1357 		es.usbcan.other_ch_status =
1358 			cmd->u.usbcan.can_error_event.status_ch1;
1359 		kvaser_usb_leaf_usbcan_conditionally_rx_error(dev, &es);
1360 
1361 		/* The USBCAN firmware supports up to 2 channels.
1362 		 * Now that ch0 was checked, check if ch1 has any errors.
1363 		 */
1364 		if (dev->nchannels == MAX_USBCAN_NET_DEVICES) {
1365 			es.channel = 1;
1366 			es.status = cmd->u.usbcan.can_error_event.status_ch1;
1367 			es.txerr =
1368 				cmd->u.usbcan.can_error_event.tx_errors_count_ch1;
1369 			es.rxerr =
1370 				cmd->u.usbcan.can_error_event.rx_errors_count_ch1;
1371 			es.usbcan.other_ch_status =
1372 				cmd->u.usbcan.can_error_event.status_ch0;
1373 			kvaser_usb_leaf_usbcan_conditionally_rx_error(dev, &es);
1374 		}
1375 		break;
1376 
1377 	default:
1378 		dev_err(&dev->intf->dev, "Invalid cmd id (%d)\n", cmd->id);
1379 	}
1380 }
1381 
kvaser_usb_leaf_leaf_rx_error(const struct kvaser_usb * dev,const struct kvaser_cmd * cmd)1382 static void kvaser_usb_leaf_leaf_rx_error(const struct kvaser_usb *dev,
1383 					  const struct kvaser_cmd *cmd)
1384 {
1385 	struct kvaser_usb_err_summary es = { };
1386 
1387 	switch (cmd->id) {
1388 	case CMD_CAN_ERROR_EVENT:
1389 		es.channel = cmd->u.leaf.can_error_event.channel;
1390 		es.status = cmd->u.leaf.can_error_event.status;
1391 		es.txerr = cmd->u.leaf.can_error_event.tx_errors_count;
1392 		es.rxerr = cmd->u.leaf.can_error_event.rx_errors_count;
1393 		es.leaf.error_factor = cmd->u.leaf.can_error_event.error_factor;
1394 		break;
1395 	case CMD_LEAF_LOG_MESSAGE:
1396 		es.channel = cmd->u.leaf.log_message.channel;
1397 		es.status = cmd->u.leaf.log_message.data[0];
1398 		es.txerr = cmd->u.leaf.log_message.data[2];
1399 		es.rxerr = cmd->u.leaf.log_message.data[3];
1400 		es.leaf.error_factor = cmd->u.leaf.log_message.data[1];
1401 		break;
1402 	case CMD_CHIP_STATE_EVENT:
1403 		es.channel = cmd->u.leaf.chip_state_event.channel;
1404 		es.status = cmd->u.leaf.chip_state_event.status;
1405 		es.txerr = cmd->u.leaf.chip_state_event.tx_errors_count;
1406 		es.rxerr = cmd->u.leaf.chip_state_event.rx_errors_count;
1407 		es.leaf.error_factor = 0;
1408 		break;
1409 	default:
1410 		dev_err(&dev->intf->dev, "Invalid cmd id (%d)\n", cmd->id);
1411 		return;
1412 	}
1413 
1414 	kvaser_usb_leaf_rx_error(dev, &es);
1415 }
1416 
kvaser_usb_leaf_rx_can_err(const struct kvaser_usb_net_priv * priv,const struct kvaser_cmd * cmd)1417 static void kvaser_usb_leaf_rx_can_err(const struct kvaser_usb_net_priv *priv,
1418 				       const struct kvaser_cmd *cmd)
1419 {
1420 	if (cmd->u.rx_can_header.flag & (MSG_FLAG_ERROR_FRAME |
1421 					 MSG_FLAG_NERR)) {
1422 		struct net_device_stats *stats = &priv->netdev->stats;
1423 
1424 		netdev_err(priv->netdev, "Unknown error (flags: 0x%02x)\n",
1425 			   cmd->u.rx_can_header.flag);
1426 
1427 		stats->rx_errors++;
1428 		return;
1429 	}
1430 
1431 	if (cmd->u.rx_can_header.flag & MSG_FLAG_OVERRUN)
1432 		kvaser_usb_can_rx_over_error(priv->netdev);
1433 }
1434 
kvaser_usb_leaf_rx_can_msg(const struct kvaser_usb * dev,const struct kvaser_cmd * cmd)1435 static void kvaser_usb_leaf_rx_can_msg(const struct kvaser_usb *dev,
1436 				       const struct kvaser_cmd *cmd)
1437 {
1438 	struct kvaser_usb_net_priv *priv;
1439 	struct can_frame *cf;
1440 	struct sk_buff *skb;
1441 	struct net_device_stats *stats;
1442 	u8 channel = cmd->u.rx_can_header.channel;
1443 	const u8 *rx_data = NULL;	/* GCC */
1444 	ktime_t hwtstamp = 0;
1445 
1446 	if (channel >= dev->nchannels) {
1447 		dev_err(&dev->intf->dev,
1448 			"Invalid channel number (%d)\n", channel);
1449 		return;
1450 	}
1451 
1452 	priv = dev->nets[channel];
1453 	stats = &priv->netdev->stats;
1454 
1455 	if ((cmd->u.rx_can_header.flag & MSG_FLAG_ERROR_FRAME) &&
1456 	    (dev->driver_info->family == KVASER_LEAF &&
1457 	     cmd->id == CMD_LEAF_LOG_MESSAGE)) {
1458 		kvaser_usb_leaf_leaf_rx_error(dev, cmd);
1459 		return;
1460 	} else if (cmd->u.rx_can_header.flag & (MSG_FLAG_ERROR_FRAME |
1461 						MSG_FLAG_NERR |
1462 						MSG_FLAG_OVERRUN)) {
1463 		kvaser_usb_leaf_rx_can_err(priv, cmd);
1464 		return;
1465 	} else if (cmd->u.rx_can_header.flag & ~MSG_FLAG_REMOTE_FRAME) {
1466 		netdev_warn(priv->netdev,
1467 			    "Unhandled frame (flags: 0x%02x)\n",
1468 			    cmd->u.rx_can_header.flag);
1469 		return;
1470 	}
1471 
1472 	switch (dev->driver_info->family) {
1473 	case KVASER_LEAF:
1474 		rx_data = cmd->u.leaf.rx_can.data;
1475 		hwtstamp = kvaser_usb_timestamp48_to_ktime(dev->cfg, cmd->u.leaf.rx_can.time);
1476 		break;
1477 	case KVASER_USBCAN:
1478 		rx_data = cmd->u.usbcan.rx_can.data;
1479 		hwtstamp = kvaser_usb_usbcan_timestamp_to_ktime(dev, cmd->u.usbcan.rx_can.time);
1480 		break;
1481 	}
1482 
1483 	skb = alloc_can_skb(priv->netdev, &cf);
1484 	if (!skb) {
1485 		stats->rx_dropped++;
1486 		return;
1487 	}
1488 
1489 	if (dev->driver_info->family == KVASER_LEAF && cmd->id ==
1490 	    CMD_LEAF_LOG_MESSAGE) {
1491 		cf->can_id = le32_to_cpu(cmd->u.leaf.log_message.id);
1492 		if (cf->can_id & KVASER_EXTENDED_FRAME)
1493 			cf->can_id &= CAN_EFF_MASK | CAN_EFF_FLAG;
1494 		else
1495 			cf->can_id &= CAN_SFF_MASK;
1496 
1497 		can_frame_set_cc_len(cf, cmd->u.leaf.log_message.dlc & 0xF, priv->can.ctrlmode);
1498 
1499 		if (cmd->u.leaf.log_message.flags & MSG_FLAG_REMOTE_FRAME)
1500 			cf->can_id |= CAN_RTR_FLAG;
1501 		else
1502 			memcpy(cf->data, &cmd->u.leaf.log_message.data,
1503 			       cf->len);
1504 	} else {
1505 		cf->can_id = ((rx_data[0] & 0x1f) << 6) | (rx_data[1] & 0x3f);
1506 
1507 		if (cmd->id == CMD_RX_EXT_MESSAGE) {
1508 			cf->can_id <<= 18;
1509 			cf->can_id |= ((rx_data[2] & 0x0f) << 14) |
1510 				      ((rx_data[3] & 0xff) << 6) |
1511 				      (rx_data[4] & 0x3f);
1512 			cf->can_id |= CAN_EFF_FLAG;
1513 		}
1514 
1515 		can_frame_set_cc_len(cf, rx_data[5] & 0xF, priv->can.ctrlmode);
1516 
1517 		if (cmd->u.rx_can_header.flag & MSG_FLAG_REMOTE_FRAME)
1518 			cf->can_id |= CAN_RTR_FLAG;
1519 		else
1520 			memcpy(cf->data, &rx_data[6], cf->len);
1521 	}
1522 
1523 	skb_hwtstamps(skb)->hwtstamp = hwtstamp;
1524 	stats->rx_packets++;
1525 	if (!(cf->can_id & CAN_RTR_FLAG))
1526 		stats->rx_bytes += cf->len;
1527 	netif_rx(skb);
1528 }
1529 
kvaser_usb_leaf_error_event_parameter(const struct kvaser_usb * dev,const struct kvaser_cmd * cmd)1530 static void kvaser_usb_leaf_error_event_parameter(const struct kvaser_usb *dev,
1531 						  const struct kvaser_cmd *cmd)
1532 {
1533 	u16 info1 = 0;
1534 
1535 	switch (dev->driver_info->family) {
1536 	case KVASER_LEAF:
1537 		info1 = le16_to_cpu(cmd->u.leaf.error_event.info1);
1538 		break;
1539 	case KVASER_USBCAN:
1540 		info1 = le16_to_cpu(cmd->u.usbcan.error_event.info1);
1541 		break;
1542 	}
1543 
1544 	/* info1 will contain the offending cmd_no */
1545 	switch (info1) {
1546 	case CMD_SET_CTRL_MODE:
1547 		dev_warn(&dev->intf->dev,
1548 			 "CMD_SET_CTRL_MODE error in parameter\n");
1549 		break;
1550 
1551 	case CMD_SET_BUS_PARAMS:
1552 		dev_warn(&dev->intf->dev,
1553 			 "CMD_SET_BUS_PARAMS error in parameter\n");
1554 		break;
1555 
1556 	default:
1557 		dev_warn(&dev->intf->dev,
1558 			 "Unhandled parameter error event cmd_no (%u)\n",
1559 			 info1);
1560 		break;
1561 	}
1562 }
1563 
kvaser_usb_leaf_error_event(const struct kvaser_usb * dev,const struct kvaser_cmd * cmd)1564 static void kvaser_usb_leaf_error_event(const struct kvaser_usb *dev,
1565 					const struct kvaser_cmd *cmd)
1566 {
1567 	u8 error_code = 0;
1568 
1569 	switch (dev->driver_info->family) {
1570 	case KVASER_LEAF:
1571 		error_code = cmd->u.leaf.error_event.error_code;
1572 		break;
1573 	case KVASER_USBCAN:
1574 		error_code = cmd->u.usbcan.error_event.error_code;
1575 		break;
1576 	}
1577 
1578 	switch (error_code) {
1579 	case KVASER_USB_LEAF_ERROR_EVENT_TX_QUEUE_FULL:
1580 		/* Received additional CAN message, when firmware TX queue is
1581 		 * already full. Something is wrong with the driver.
1582 		 * This should never happen!
1583 		 */
1584 		dev_err(&dev->intf->dev,
1585 			"Received error event TX_QUEUE_FULL\n");
1586 		break;
1587 	case KVASER_USB_LEAF_ERROR_EVENT_PARAM:
1588 		kvaser_usb_leaf_error_event_parameter(dev, cmd);
1589 		break;
1590 
1591 	default:
1592 		dev_warn(&dev->intf->dev,
1593 			 "Unhandled error event (%d)\n", error_code);
1594 		break;
1595 	}
1596 }
1597 
kvaser_usb_leaf_start_chip_reply(const struct kvaser_usb * dev,const struct kvaser_cmd * cmd)1598 static void kvaser_usb_leaf_start_chip_reply(const struct kvaser_usb *dev,
1599 					     const struct kvaser_cmd *cmd)
1600 {
1601 	struct kvaser_usb_net_priv *priv;
1602 	u8 channel = cmd->u.simple.channel;
1603 
1604 	if (channel >= dev->nchannels) {
1605 		dev_err(&dev->intf->dev,
1606 			"Invalid channel number (%d)\n", channel);
1607 		return;
1608 	}
1609 
1610 	priv = dev->nets[channel];
1611 
1612 	if (completion_done(&priv->start_comp) &&
1613 	    netif_queue_stopped(priv->netdev)) {
1614 		netif_wake_queue(priv->netdev);
1615 	} else {
1616 		netif_start_queue(priv->netdev);
1617 		complete(&priv->start_comp);
1618 	}
1619 }
1620 
kvaser_usb_leaf_stop_chip_reply(const struct kvaser_usb * dev,const struct kvaser_cmd * cmd)1621 static void kvaser_usb_leaf_stop_chip_reply(const struct kvaser_usb *dev,
1622 					    const struct kvaser_cmd *cmd)
1623 {
1624 	struct kvaser_usb_net_priv *priv;
1625 	u8 channel = cmd->u.simple.channel;
1626 
1627 	if (channel >= dev->nchannels) {
1628 		dev_err(&dev->intf->dev,
1629 			"Invalid channel number (%d)\n", channel);
1630 		return;
1631 	}
1632 
1633 	priv = dev->nets[channel];
1634 
1635 	complete(&priv->stop_comp);
1636 }
1637 
kvaser_usb_leaf_get_busparams_reply(const struct kvaser_usb * dev,const struct kvaser_cmd * cmd)1638 static void kvaser_usb_leaf_get_busparams_reply(const struct kvaser_usb *dev,
1639 						const struct kvaser_cmd *cmd)
1640 {
1641 	struct kvaser_usb_net_priv *priv;
1642 	u8 channel = cmd->u.busparams.channel;
1643 
1644 	if (channel >= dev->nchannels) {
1645 		dev_err(&dev->intf->dev,
1646 			"Invalid channel number (%d)\n", channel);
1647 		return;
1648 	}
1649 
1650 	priv = dev->nets[channel];
1651 	memcpy(&priv->busparams_nominal, &cmd->u.busparams.busparams,
1652 	       sizeof(priv->busparams_nominal));
1653 
1654 	complete(&priv->get_busparams_comp);
1655 }
1656 
kvaser_usb_leaf_handle_command(struct kvaser_usb * dev,const struct kvaser_cmd * cmd)1657 static void kvaser_usb_leaf_handle_command(struct kvaser_usb *dev,
1658 					   const struct kvaser_cmd *cmd)
1659 {
1660 	if (kvaser_usb_leaf_verify_size(dev, cmd) < 0)
1661 		return;
1662 
1663 	switch (cmd->id) {
1664 	case CMD_START_CHIP_REPLY:
1665 		kvaser_usb_leaf_start_chip_reply(dev, cmd);
1666 		break;
1667 
1668 	case CMD_STOP_CHIP_REPLY:
1669 		kvaser_usb_leaf_stop_chip_reply(dev, cmd);
1670 		break;
1671 
1672 	case CMD_RX_STD_MESSAGE:
1673 	case CMD_RX_EXT_MESSAGE:
1674 		kvaser_usb_leaf_rx_can_msg(dev, cmd);
1675 		break;
1676 
1677 	case CMD_LEAF_LOG_MESSAGE:
1678 		if (dev->driver_info->family != KVASER_LEAF)
1679 			goto warn;
1680 		kvaser_usb_leaf_rx_can_msg(dev, cmd);
1681 		break;
1682 
1683 	case CMD_CHIP_STATE_EVENT:
1684 	case CMD_CAN_ERROR_EVENT:
1685 		if (dev->driver_info->family == KVASER_LEAF)
1686 			kvaser_usb_leaf_leaf_rx_error(dev, cmd);
1687 		else
1688 			kvaser_usb_leaf_usbcan_rx_error(dev, cmd);
1689 		break;
1690 
1691 	case CMD_TX_ACKNOWLEDGE:
1692 		kvaser_usb_leaf_tx_acknowledge(dev, cmd);
1693 		break;
1694 
1695 	case CMD_ERROR_EVENT:
1696 		kvaser_usb_leaf_error_event(dev, cmd);
1697 		break;
1698 
1699 	case CMD_GET_BUS_PARAMS_REPLY:
1700 		kvaser_usb_leaf_get_busparams_reply(dev, cmd);
1701 		break;
1702 
1703 	case CMD_USBCAN_CLOCK_OVERFLOW_EVENT:
1704 		if (dev->driver_info->family != KVASER_USBCAN)
1705 			goto warn;
1706 		dev->card_data.usbcan_timestamp_msb =
1707 					le32_to_cpu(cmd->u.usbcan.clk_overflow_event.time) &
1708 					KVASER_USB_USBCAN_CLK_OVERFLOW_MASK;
1709 		break;
1710 
1711 	/* Ignored commands */
1712 	case CMD_FLUSH_QUEUE_REPLY:
1713 		if (dev->driver_info->family != KVASER_LEAF)
1714 			goto warn;
1715 		break;
1716 	case CMD_LED_ACTION_RESP:
1717 		break;
1718 
1719 	default:
1720 warn:		dev_warn(&dev->intf->dev, "Unhandled command (%d)\n", cmd->id);
1721 		break;
1722 	}
1723 }
1724 
kvaser_usb_leaf_read_bulk_callback(struct kvaser_usb * dev,void * buf,int len)1725 static void kvaser_usb_leaf_read_bulk_callback(struct kvaser_usb *dev,
1726 					       void *buf, int len)
1727 {
1728 	struct kvaser_cmd *cmd;
1729 	int pos = 0;
1730 
1731 	while (pos <= len - CMD_HEADER_LEN) {
1732 		cmd = buf + pos;
1733 
1734 		/* The Kvaser firmware can only read and write commands that
1735 		 * does not cross the USB's endpoint wMaxPacketSize boundary.
1736 		 * If a follow-up command crosses such boundary, firmware puts
1737 		 * a placeholder zero-length command in its place then aligns
1738 		 * the real command to the next max packet size.
1739 		 *
1740 		 * Handle such cases or we're going to miss a significant
1741 		 * number of events in case of a heavy rx load on the bus.
1742 		 */
1743 		if (cmd->len == 0) {
1744 			pos = round_up(pos + 1, le16_to_cpu
1745 						(dev->bulk_in->wMaxPacketSize));
1746 			continue;
1747 		}
1748 
1749 		if (cmd->len < CMD_HEADER_LEN || cmd->len > len - pos) {
1750 			dev_err_ratelimited(&dev->intf->dev, "Format error\n");
1751 			break;
1752 		}
1753 
1754 		kvaser_usb_leaf_handle_command(dev, cmd);
1755 		pos += cmd->len;
1756 	}
1757 }
1758 
kvaser_usb_leaf_set_opt_mode(const struct kvaser_usb_net_priv * priv)1759 static int kvaser_usb_leaf_set_opt_mode(const struct kvaser_usb_net_priv *priv)
1760 {
1761 	struct kvaser_cmd *cmd;
1762 	int rc;
1763 
1764 	cmd = kzalloc_obj(*cmd);
1765 	if (!cmd)
1766 		return -ENOMEM;
1767 
1768 	cmd->id = CMD_SET_CTRL_MODE;
1769 	cmd->len = CMD_HEADER_LEN + sizeof(struct kvaser_cmd_ctrl_mode);
1770 	cmd->u.ctrl_mode.tid = 0xff;
1771 	cmd->u.ctrl_mode.channel = priv->channel;
1772 
1773 	if (priv->can.ctrlmode & CAN_CTRLMODE_LISTENONLY)
1774 		cmd->u.ctrl_mode.ctrl_mode = KVASER_CTRL_MODE_SILENT;
1775 	else
1776 		cmd->u.ctrl_mode.ctrl_mode = KVASER_CTRL_MODE_NORMAL;
1777 
1778 	rc = kvaser_usb_send_cmd(priv->dev, cmd, cmd->len);
1779 
1780 	kfree(cmd);
1781 	return rc;
1782 }
1783 
kvaser_usb_leaf_start_chip(struct kvaser_usb_net_priv * priv)1784 static int kvaser_usb_leaf_start_chip(struct kvaser_usb_net_priv *priv)
1785 {
1786 	struct kvaser_usb_net_leaf_priv *leaf = priv->sub_priv;
1787 	int err;
1788 
1789 	leaf->joining_bus = true;
1790 
1791 	reinit_completion(&priv->start_comp);
1792 
1793 	err = kvaser_usb_leaf_send_simple_cmd(priv->dev, CMD_START_CHIP,
1794 					      priv->channel);
1795 	if (err)
1796 		return err;
1797 
1798 	if (!wait_for_completion_timeout(&priv->start_comp,
1799 					 msecs_to_jiffies(KVASER_USB_TIMEOUT)))
1800 		return -ETIMEDOUT;
1801 
1802 	return 0;
1803 }
1804 
kvaser_usb_leaf_stop_chip(struct kvaser_usb_net_priv * priv)1805 static int kvaser_usb_leaf_stop_chip(struct kvaser_usb_net_priv *priv)
1806 {
1807 	struct kvaser_usb_net_leaf_priv *leaf = priv->sub_priv;
1808 	int err;
1809 
1810 	reinit_completion(&priv->stop_comp);
1811 
1812 	cancel_delayed_work(&leaf->chip_state_req_work);
1813 
1814 	err = kvaser_usb_leaf_send_simple_cmd(priv->dev, CMD_STOP_CHIP,
1815 					      priv->channel);
1816 	if (err)
1817 		return err;
1818 
1819 	if (!wait_for_completion_timeout(&priv->stop_comp,
1820 					 msecs_to_jiffies(KVASER_USB_TIMEOUT)))
1821 		return -ETIMEDOUT;
1822 
1823 	return 0;
1824 }
1825 
kvaser_usb_leaf_reset_chip(struct kvaser_usb * dev,int channel)1826 static int kvaser_usb_leaf_reset_chip(struct kvaser_usb *dev, int channel)
1827 {
1828 	return kvaser_usb_leaf_send_simple_cmd(dev, CMD_RESET_CHIP, channel);
1829 }
1830 
kvaser_usb_leaf_flush_queue(struct kvaser_usb_net_priv * priv)1831 static int kvaser_usb_leaf_flush_queue(struct kvaser_usb_net_priv *priv)
1832 {
1833 	struct kvaser_cmd *cmd;
1834 	int rc;
1835 
1836 	cmd = kzalloc_obj(*cmd);
1837 	if (!cmd)
1838 		return -ENOMEM;
1839 
1840 	cmd->id = CMD_FLUSH_QUEUE;
1841 	cmd->len = CMD_HEADER_LEN + sizeof(struct kvaser_cmd_flush_queue);
1842 	cmd->u.flush_queue.channel = priv->channel;
1843 	cmd->u.flush_queue.flags = 0x00;
1844 
1845 	rc = kvaser_usb_send_cmd(priv->dev, cmd, cmd->len);
1846 
1847 	kfree(cmd);
1848 	return rc;
1849 }
1850 
kvaser_usb_leaf_init_card(struct kvaser_usb * dev)1851 static int kvaser_usb_leaf_init_card(struct kvaser_usb *dev)
1852 {
1853 	struct kvaser_usb_dev_card_data *card_data = &dev->card_data;
1854 
1855 	card_data->ctrlmode_supported |= CAN_CTRLMODE_3_SAMPLES;
1856 
1857 	return 0;
1858 }
1859 
kvaser_usb_leaf_init_channel(struct kvaser_usb_net_priv * priv)1860 static int kvaser_usb_leaf_init_channel(struct kvaser_usb_net_priv *priv)
1861 {
1862 	struct kvaser_usb_net_leaf_priv *leaf;
1863 
1864 	leaf = devm_kzalloc(&priv->dev->intf->dev, sizeof(*leaf), GFP_KERNEL);
1865 	if (!leaf)
1866 		return -ENOMEM;
1867 
1868 	leaf->net = priv;
1869 	INIT_DELAYED_WORK(&leaf->chip_state_req_work,
1870 			  kvaser_usb_leaf_chip_state_req_work);
1871 
1872 	priv->sub_priv = leaf;
1873 
1874 	return 0;
1875 }
1876 
kvaser_usb_leaf_remove_channel(struct kvaser_usb_net_priv * priv)1877 static void kvaser_usb_leaf_remove_channel(struct kvaser_usb_net_priv *priv)
1878 {
1879 	struct kvaser_usb_net_leaf_priv *leaf = priv->sub_priv;
1880 
1881 	if (leaf)
1882 		cancel_delayed_work_sync(&leaf->chip_state_req_work);
1883 }
1884 
kvaser_usb_leaf_set_bittiming(const struct net_device * netdev,const struct kvaser_usb_busparams * busparams)1885 static int kvaser_usb_leaf_set_bittiming(const struct net_device *netdev,
1886 					 const struct kvaser_usb_busparams *busparams)
1887 {
1888 	struct kvaser_usb_net_priv *priv = netdev_priv(netdev);
1889 	struct kvaser_usb *dev = priv->dev;
1890 	struct kvaser_cmd *cmd;
1891 	int rc;
1892 
1893 	cmd = kmalloc_obj(*cmd);
1894 	if (!cmd)
1895 		return -ENOMEM;
1896 
1897 	cmd->id = CMD_SET_BUS_PARAMS;
1898 	cmd->len = CMD_HEADER_LEN + sizeof(struct kvaser_cmd_busparams);
1899 	cmd->u.busparams.channel = priv->channel;
1900 	cmd->u.busparams.tid = 0xff;
1901 	memcpy(&cmd->u.busparams.busparams, busparams,
1902 	       sizeof(cmd->u.busparams.busparams));
1903 
1904 	rc = kvaser_usb_send_cmd(dev, cmd, cmd->len);
1905 
1906 	kfree(cmd);
1907 	return rc;
1908 }
1909 
kvaser_usb_leaf_get_busparams(struct kvaser_usb_net_priv * priv)1910 static int kvaser_usb_leaf_get_busparams(struct kvaser_usb_net_priv *priv)
1911 {
1912 	int err;
1913 
1914 	if (priv->dev->driver_info->family == KVASER_USBCAN)
1915 		return -EOPNOTSUPP;
1916 
1917 	reinit_completion(&priv->get_busparams_comp);
1918 
1919 	err = kvaser_usb_leaf_send_simple_cmd(priv->dev, CMD_GET_BUS_PARAMS,
1920 					      priv->channel);
1921 	if (err)
1922 		return err;
1923 
1924 	if (!wait_for_completion_timeout(&priv->get_busparams_comp,
1925 					 msecs_to_jiffies(KVASER_USB_TIMEOUT)))
1926 		return -ETIMEDOUT;
1927 
1928 	return 0;
1929 }
1930 
kvaser_usb_leaf_set_mode(struct net_device * netdev,enum can_mode mode)1931 static int kvaser_usb_leaf_set_mode(struct net_device *netdev,
1932 				    enum can_mode mode)
1933 {
1934 	struct kvaser_usb_net_priv *priv = netdev_priv(netdev);
1935 	struct kvaser_usb_net_leaf_priv *leaf = priv->sub_priv;
1936 	int err;
1937 
1938 	switch (mode) {
1939 	case CAN_MODE_START:
1940 		kvaser_usb_unlink_tx_urbs(priv);
1941 
1942 		leaf->joining_bus = true;
1943 
1944 		err = kvaser_usb_leaf_simple_cmd_async(priv, CMD_START_CHIP);
1945 		if (err)
1946 			return err;
1947 
1948 		priv->can.state = CAN_STATE_ERROR_ACTIVE;
1949 		break;
1950 	default:
1951 		return -EOPNOTSUPP;
1952 	}
1953 
1954 	return 0;
1955 }
1956 
kvaser_usb_leaf_get_berr_counter(const struct net_device * netdev,struct can_berr_counter * bec)1957 static int kvaser_usb_leaf_get_berr_counter(const struct net_device *netdev,
1958 					    struct can_berr_counter *bec)
1959 {
1960 	struct kvaser_usb_net_priv *priv = netdev_priv(netdev);
1961 
1962 	*bec = priv->bec;
1963 
1964 	return 0;
1965 }
1966 
kvaser_usb_leaf_setup_endpoints(struct kvaser_usb * dev)1967 static int kvaser_usb_leaf_setup_endpoints(struct kvaser_usb *dev)
1968 {
1969 	struct usb_host_interface *iface_desc;
1970 	int ret;
1971 
1972 	iface_desc = dev->intf->cur_altsetting;
1973 
1974 	/* use first bulk endpoint for in and out */
1975 	ret = usb_find_common_endpoints(iface_desc, &dev->bulk_in, &dev->bulk_out,
1976 					NULL, NULL);
1977 	if (ret)
1978 		return -ENODEV;
1979 
1980 	return 0;
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