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