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