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 
25 #include <linux/can.h>
26 #include <linux/can/dev.h>
27 #include <linux/can/error.h>
28 #include <linux/can/netlink.h>
29 
30 #include "kvaser_usb.h"
31 
32 #define MAX_USBCAN_NET_DEVICES		2
33 
34 /* Command header size */
35 #define CMD_HEADER_LEN			2
36 
37 /* Kvaser CAN message flags */
38 #define MSG_FLAG_ERROR_FRAME		BIT(0)
39 #define MSG_FLAG_OVERRUN		BIT(1)
40 #define MSG_FLAG_NERR			BIT(2)
41 #define MSG_FLAG_WAKEUP			BIT(3)
42 #define MSG_FLAG_REMOTE_FRAME		BIT(4)
43 #define MSG_FLAG_RESERVED		BIT(5)
44 #define MSG_FLAG_TX_ACK			BIT(6)
45 #define MSG_FLAG_TX_REQUEST		BIT(7)
46 
47 /* CAN states (M16C CxSTRH register) */
48 #define M16C_STATE_BUS_RESET		BIT(0)
49 #define M16C_STATE_BUS_ERROR		BIT(4)
50 #define M16C_STATE_BUS_PASSIVE		BIT(5)
51 #define M16C_STATE_BUS_OFF		BIT(6)
52 
53 /* Leaf/usbcan command ids */
54 #define CMD_RX_STD_MESSAGE		12
55 #define CMD_TX_STD_MESSAGE		13
56 #define CMD_RX_EXT_MESSAGE		14
57 #define CMD_TX_EXT_MESSAGE		15
58 #define CMD_SET_BUS_PARAMS		16
59 #define CMD_CHIP_STATE_EVENT		20
60 #define CMD_SET_CTRL_MODE		21
61 #define CMD_RESET_CHIP			24
62 #define CMD_START_CHIP			26
63 #define CMD_START_CHIP_REPLY		27
64 #define CMD_STOP_CHIP			28
65 #define CMD_STOP_CHIP_REPLY		29
66 
67 #define CMD_USBCAN_CLOCK_OVERFLOW_EVENT	33
68 
69 #define CMD_GET_CARD_INFO		34
70 #define CMD_GET_CARD_INFO_REPLY		35
71 #define CMD_GET_SOFTWARE_INFO		38
72 #define CMD_GET_SOFTWARE_INFO_REPLY	39
73 #define CMD_FLUSH_QUEUE			48
74 #define CMD_TX_ACKNOWLEDGE		50
75 #define CMD_CAN_ERROR_EVENT		51
76 #define CMD_FLUSH_QUEUE_REPLY		68
77 
78 #define CMD_LEAF_LOG_MESSAGE		106
79 
80 /* Leaf frequency options */
81 #define KVASER_USB_LEAF_SWOPTION_FREQ_MASK 0x60
82 #define KVASER_USB_LEAF_SWOPTION_FREQ_16_MHZ_CLK 0
83 #define KVASER_USB_LEAF_SWOPTION_FREQ_32_MHZ_CLK BIT(5)
84 #define KVASER_USB_LEAF_SWOPTION_FREQ_24_MHZ_CLK BIT(6)
85 
86 /* error factors */
87 #define M16C_EF_ACKE			BIT(0)
88 #define M16C_EF_CRCE			BIT(1)
89 #define M16C_EF_FORME			BIT(2)
90 #define M16C_EF_STFE			BIT(3)
91 #define M16C_EF_BITE0			BIT(4)
92 #define M16C_EF_BITE1			BIT(5)
93 #define M16C_EF_RCVE			BIT(6)
94 #define M16C_EF_TRE			BIT(7)
95 
96 /* Only Leaf-based devices can report M16C error factors,
97  * thus define our own error status flags for USBCANII
98  */
99 #define USBCAN_ERROR_STATE_NONE		0
100 #define USBCAN_ERROR_STATE_TX_ERROR	BIT(0)
101 #define USBCAN_ERROR_STATE_RX_ERROR	BIT(1)
102 #define USBCAN_ERROR_STATE_BUSERROR	BIT(2)
103 
104 /* ctrl modes */
105 #define KVASER_CTRL_MODE_NORMAL		1
106 #define KVASER_CTRL_MODE_SILENT		2
107 #define KVASER_CTRL_MODE_SELFRECEPTION	3
108 #define KVASER_CTRL_MODE_OFF		4
109 
110 /* Extended CAN identifier flag */
111 #define KVASER_EXTENDED_FRAME		BIT(31)
112 
113 struct kvaser_cmd_simple {
114 	u8 tid;
115 	u8 channel;
116 } __packed;
117 
118 struct kvaser_cmd_cardinfo {
119 	u8 tid;
120 	u8 nchannels;
121 	__le32 serial_number;
122 	__le32 padding0;
123 	__le32 clock_resolution;
124 	__le32 mfgdate;
125 	u8 ean[8];
126 	u8 hw_revision;
127 	union {
128 		struct {
129 			u8 usb_hs_mode;
130 		} __packed leaf1;
131 		struct {
132 			u8 padding;
133 		} __packed usbcan1;
134 	} __packed;
135 	__le16 padding1;
136 } __packed;
137 
138 struct leaf_cmd_softinfo {
139 	u8 tid;
140 	u8 padding0;
141 	__le32 sw_options;
142 	__le32 fw_version;
143 	__le16 max_outstanding_tx;
144 	__le16 padding1[9];
145 } __packed;
146 
147 struct usbcan_cmd_softinfo {
148 	u8 tid;
149 	u8 fw_name[5];
150 	__le16 max_outstanding_tx;
151 	u8 padding[6];
152 	__le32 fw_version;
153 	__le16 checksum;
154 	__le16 sw_options;
155 } __packed;
156 
157 struct kvaser_cmd_busparams {
158 	u8 tid;
159 	u8 channel;
160 	__le32 bitrate;
161 	u8 tseg1;
162 	u8 tseg2;
163 	u8 sjw;
164 	u8 no_samp;
165 } __packed;
166 
167 struct kvaser_cmd_tx_can {
168 	u8 channel;
169 	u8 tid;
170 	u8 data[14];
171 	union {
172 		struct {
173 			u8 padding;
174 			u8 flags;
175 		} __packed leaf;
176 		struct {
177 			u8 flags;
178 			u8 padding;
179 		} __packed usbcan;
180 	} __packed;
181 } __packed;
182 
183 struct kvaser_cmd_rx_can_header {
184 	u8 channel;
185 	u8 flag;
186 } __packed;
187 
188 struct leaf_cmd_rx_can {
189 	u8 channel;
190 	u8 flag;
191 
192 	__le16 time[3];
193 	u8 data[14];
194 } __packed;
195 
196 struct usbcan_cmd_rx_can {
197 	u8 channel;
198 	u8 flag;
199 
200 	u8 data[14];
201 	__le16 time;
202 } __packed;
203 
204 struct leaf_cmd_chip_state_event {
205 	u8 tid;
206 	u8 channel;
207 
208 	__le16 time[3];
209 	u8 tx_errors_count;
210 	u8 rx_errors_count;
211 
212 	u8 status;
213 	u8 padding[3];
214 } __packed;
215 
216 struct usbcan_cmd_chip_state_event {
217 	u8 tid;
218 	u8 channel;
219 
220 	u8 tx_errors_count;
221 	u8 rx_errors_count;
222 	__le16 time;
223 
224 	u8 status;
225 	u8 padding[3];
226 } __packed;
227 
228 struct kvaser_cmd_tx_acknowledge_header {
229 	u8 channel;
230 	u8 tid;
231 } __packed;
232 
233 struct leaf_cmd_error_event {
234 	u8 tid;
235 	u8 flags;
236 	__le16 time[3];
237 	u8 channel;
238 	u8 padding;
239 	u8 tx_errors_count;
240 	u8 rx_errors_count;
241 	u8 status;
242 	u8 error_factor;
243 } __packed;
244 
245 struct usbcan_cmd_error_event {
246 	u8 tid;
247 	u8 padding;
248 	u8 tx_errors_count_ch0;
249 	u8 rx_errors_count_ch0;
250 	u8 tx_errors_count_ch1;
251 	u8 rx_errors_count_ch1;
252 	u8 status_ch0;
253 	u8 status_ch1;
254 	__le16 time;
255 } __packed;
256 
257 struct kvaser_cmd_ctrl_mode {
258 	u8 tid;
259 	u8 channel;
260 	u8 ctrl_mode;
261 	u8 padding[3];
262 } __packed;
263 
264 struct kvaser_cmd_flush_queue {
265 	u8 tid;
266 	u8 channel;
267 	u8 flags;
268 	u8 padding[3];
269 } __packed;
270 
271 struct leaf_cmd_log_message {
272 	u8 channel;
273 	u8 flags;
274 	__le16 time[3];
275 	u8 dlc;
276 	u8 time_offset;
277 	__le32 id;
278 	u8 data[8];
279 } __packed;
280 
281 struct kvaser_cmd {
282 	u8 len;
283 	u8 id;
284 	union	{
285 		struct kvaser_cmd_simple simple;
286 		struct kvaser_cmd_cardinfo cardinfo;
287 		struct kvaser_cmd_busparams busparams;
288 
289 		struct kvaser_cmd_rx_can_header rx_can_header;
290 		struct kvaser_cmd_tx_acknowledge_header tx_acknowledge_header;
291 
292 		union {
293 			struct leaf_cmd_softinfo softinfo;
294 			struct leaf_cmd_rx_can rx_can;
295 			struct leaf_cmd_chip_state_event chip_state_event;
296 			struct leaf_cmd_error_event error_event;
297 			struct leaf_cmd_log_message log_message;
298 		} __packed leaf;
299 
300 		union {
301 			struct usbcan_cmd_softinfo softinfo;
302 			struct usbcan_cmd_rx_can rx_can;
303 			struct usbcan_cmd_chip_state_event chip_state_event;
304 			struct usbcan_cmd_error_event error_event;
305 		} __packed usbcan;
306 
307 		struct kvaser_cmd_tx_can tx_can;
308 		struct kvaser_cmd_ctrl_mode ctrl_mode;
309 		struct kvaser_cmd_flush_queue flush_queue;
310 	} u;
311 } __packed;
312 
313 /* Summary of a kvaser error event, for a unified Leaf/Usbcan error
314  * handling. Some discrepancies between the two families exist:
315  *
316  * - USBCAN firmware does not report M16C "error factors"
317  * - USBCAN controllers has difficulties reporting if the raised error
318  *   event is for ch0 or ch1. They leave such arbitration to the OS
319  *   driver by letting it compare error counters with previous values
320  *   and decide the error event's channel. Thus for USBCAN, the channel
321  *   field is only advisory.
322  */
323 struct kvaser_usb_err_summary {
324 	u8 channel, status, txerr, rxerr;
325 	union {
326 		struct {
327 			u8 error_factor;
328 		} leaf;
329 		struct {
330 			u8 other_ch_status;
331 			u8 error_state;
332 		} usbcan;
333 	};
334 };
335 
336 static const struct can_bittiming_const kvaser_usb_leaf_m16c_bittiming_const = {
337 	.name = "kvaser_usb_ucii",
338 	.tseg1_min = 4,
339 	.tseg1_max = 16,
340 	.tseg2_min = 2,
341 	.tseg2_max = 8,
342 	.sjw_max = 4,
343 	.brp_min = 1,
344 	.brp_max = 16,
345 	.brp_inc = 1,
346 };
347 
348 static const struct can_bittiming_const kvaser_usb_leaf_m32c_bittiming_const = {
349 	.name = "kvaser_usb_leaf",
350 	.tseg1_min = 3,
351 	.tseg1_max = 16,
352 	.tseg2_min = 2,
353 	.tseg2_max = 8,
354 	.sjw_max = 4,
355 	.brp_min = 2,
356 	.brp_max = 128,
357 	.brp_inc = 2,
358 };
359 
360 static const struct kvaser_usb_dev_cfg kvaser_usb_leaf_usbcan_dev_cfg = {
361 	.clock = {
362 		.freq = 8 * MEGA /* Hz */,
363 	},
364 	.timestamp_freq = 1,
365 	.bittiming_const = &kvaser_usb_leaf_m16c_bittiming_const,
366 };
367 
368 static const struct kvaser_usb_dev_cfg kvaser_usb_leaf_m32c_dev_cfg = {
369 	.clock = {
370 		.freq = 16 * MEGA /* Hz */,
371 	},
372 	.timestamp_freq = 1,
373 	.bittiming_const = &kvaser_usb_leaf_m32c_bittiming_const,
374 };
375 
376 static const struct kvaser_usb_dev_cfg kvaser_usb_leaf_imx_dev_cfg_16mhz = {
377 	.clock = {
378 		.freq = 16 * MEGA /* Hz */,
379 	},
380 	.timestamp_freq = 1,
381 	.bittiming_const = &kvaser_usb_flexc_bittiming_const,
382 };
383 
384 static const struct kvaser_usb_dev_cfg kvaser_usb_leaf_imx_dev_cfg_24mhz = {
385 	.clock = {
386 		.freq = 24 * MEGA /* Hz */,
387 	},
388 	.timestamp_freq = 1,
389 	.bittiming_const = &kvaser_usb_flexc_bittiming_const,
390 };
391 
392 static const struct kvaser_usb_dev_cfg kvaser_usb_leaf_imx_dev_cfg_32mhz = {
393 	.clock = {
394 		.freq = 32 * MEGA /* Hz */,
395 	},
396 	.timestamp_freq = 1,
397 	.bittiming_const = &kvaser_usb_flexc_bittiming_const,
398 };
399 
400 static void *
401 kvaser_usb_leaf_frame_to_cmd(const struct kvaser_usb_net_priv *priv,
402 			     const struct sk_buff *skb, int *cmd_len,
403 			     u16 transid)
404 {
405 	struct kvaser_usb *dev = priv->dev;
406 	struct kvaser_cmd *cmd;
407 	u8 *cmd_tx_can_flags = NULL;		/* GCC */
408 	struct can_frame *cf = (struct can_frame *)skb->data;
409 
410 	cmd = kmalloc(sizeof(*cmd), GFP_ATOMIC);
411 	if (cmd) {
412 		cmd->u.tx_can.tid = transid & 0xff;
413 		cmd->len = *cmd_len = CMD_HEADER_LEN +
414 				      sizeof(struct kvaser_cmd_tx_can);
415 		cmd->u.tx_can.channel = priv->channel;
416 
417 		switch (dev->driver_info->family) {
418 		case KVASER_LEAF:
419 			cmd_tx_can_flags = &cmd->u.tx_can.leaf.flags;
420 			break;
421 		case KVASER_USBCAN:
422 			cmd_tx_can_flags = &cmd->u.tx_can.usbcan.flags;
423 			break;
424 		}
425 
426 		*cmd_tx_can_flags = 0;
427 
428 		if (cf->can_id & CAN_EFF_FLAG) {
429 			cmd->id = CMD_TX_EXT_MESSAGE;
430 			cmd->u.tx_can.data[0] = (cf->can_id >> 24) & 0x1f;
431 			cmd->u.tx_can.data[1] = (cf->can_id >> 18) & 0x3f;
432 			cmd->u.tx_can.data[2] = (cf->can_id >> 14) & 0x0f;
433 			cmd->u.tx_can.data[3] = (cf->can_id >> 6) & 0xff;
434 			cmd->u.tx_can.data[4] = cf->can_id & 0x3f;
435 		} else {
436 			cmd->id = CMD_TX_STD_MESSAGE;
437 			cmd->u.tx_can.data[0] = (cf->can_id >> 6) & 0x1f;
438 			cmd->u.tx_can.data[1] = cf->can_id & 0x3f;
439 		}
440 
441 		cmd->u.tx_can.data[5] = cf->len;
442 		memcpy(&cmd->u.tx_can.data[6], cf->data, cf->len);
443 
444 		if (cf->can_id & CAN_RTR_FLAG)
445 			*cmd_tx_can_flags |= MSG_FLAG_REMOTE_FRAME;
446 	}
447 	return cmd;
448 }
449 
450 static int kvaser_usb_leaf_wait_cmd(const struct kvaser_usb *dev, u8 id,
451 				    struct kvaser_cmd *cmd)
452 {
453 	struct kvaser_cmd *tmp;
454 	void *buf;
455 	int actual_len;
456 	int err;
457 	int pos;
458 	unsigned long to = jiffies + msecs_to_jiffies(KVASER_USB_TIMEOUT);
459 
460 	buf = kzalloc(KVASER_USB_RX_BUFFER_SIZE, GFP_KERNEL);
461 	if (!buf)
462 		return -ENOMEM;
463 
464 	do {
465 		err = kvaser_usb_recv_cmd(dev, buf, KVASER_USB_RX_BUFFER_SIZE,
466 					  &actual_len);
467 		if (err < 0)
468 			goto end;
469 
470 		pos = 0;
471 		while (pos <= actual_len - CMD_HEADER_LEN) {
472 			tmp = buf + pos;
473 
474 			/* Handle commands crossing the USB endpoint max packet
475 			 * size boundary. Check kvaser_usb_read_bulk_callback()
476 			 * for further details.
477 			 */
478 			if (tmp->len == 0) {
479 				pos = round_up(pos,
480 					       le16_to_cpu
481 						(dev->bulk_in->wMaxPacketSize));
482 				continue;
483 			}
484 
485 			if (pos + tmp->len > actual_len) {
486 				dev_err_ratelimited(&dev->intf->dev,
487 						    "Format error\n");
488 				break;
489 			}
490 
491 			if (tmp->id == id) {
492 				memcpy(cmd, tmp, tmp->len);
493 				goto end;
494 			}
495 
496 			pos += tmp->len;
497 		}
498 	} while (time_before(jiffies, to));
499 
500 	err = -EINVAL;
501 
502 end:
503 	kfree(buf);
504 
505 	return err;
506 }
507 
508 static int kvaser_usb_leaf_send_simple_cmd(const struct kvaser_usb *dev,
509 					   u8 cmd_id, int channel)
510 {
511 	struct kvaser_cmd *cmd;
512 	int rc;
513 
514 	cmd = kmalloc(sizeof(*cmd), GFP_KERNEL);
515 	if (!cmd)
516 		return -ENOMEM;
517 
518 	cmd->id = cmd_id;
519 	cmd->len = CMD_HEADER_LEN + sizeof(struct kvaser_cmd_simple);
520 	cmd->u.simple.channel = channel;
521 	cmd->u.simple.tid = 0xff;
522 
523 	rc = kvaser_usb_send_cmd(dev, cmd, cmd->len);
524 
525 	kfree(cmd);
526 	return rc;
527 }
528 
529 static void kvaser_usb_leaf_get_software_info_leaf(struct kvaser_usb *dev,
530 						   const struct leaf_cmd_softinfo *softinfo)
531 {
532 	u32 sw_options = le32_to_cpu(softinfo->sw_options);
533 
534 	dev->fw_version = le32_to_cpu(softinfo->fw_version);
535 	dev->max_tx_urbs = le16_to_cpu(softinfo->max_outstanding_tx);
536 
537 	if (dev->driver_info->quirks & KVASER_USB_QUIRK_IGNORE_CLK_FREQ) {
538 		/* Firmware expects bittiming parameters calculated for 16MHz
539 		 * clock, regardless of the actual clock
540 		 */
541 		dev->cfg = &kvaser_usb_leaf_m32c_dev_cfg;
542 	} else {
543 		switch (sw_options & KVASER_USB_LEAF_SWOPTION_FREQ_MASK) {
544 		case KVASER_USB_LEAF_SWOPTION_FREQ_16_MHZ_CLK:
545 			dev->cfg = &kvaser_usb_leaf_imx_dev_cfg_16mhz;
546 			break;
547 		case KVASER_USB_LEAF_SWOPTION_FREQ_24_MHZ_CLK:
548 			dev->cfg = &kvaser_usb_leaf_imx_dev_cfg_24mhz;
549 			break;
550 		case KVASER_USB_LEAF_SWOPTION_FREQ_32_MHZ_CLK:
551 			dev->cfg = &kvaser_usb_leaf_imx_dev_cfg_32mhz;
552 			break;
553 		}
554 	}
555 }
556 
557 static int kvaser_usb_leaf_get_software_info_inner(struct kvaser_usb *dev)
558 {
559 	struct kvaser_cmd cmd;
560 	int err;
561 
562 	err = kvaser_usb_leaf_send_simple_cmd(dev, CMD_GET_SOFTWARE_INFO, 0);
563 	if (err)
564 		return err;
565 
566 	err = kvaser_usb_leaf_wait_cmd(dev, CMD_GET_SOFTWARE_INFO_REPLY, &cmd);
567 	if (err)
568 		return err;
569 
570 	switch (dev->driver_info->family) {
571 	case KVASER_LEAF:
572 		kvaser_usb_leaf_get_software_info_leaf(dev, &cmd.u.leaf.softinfo);
573 		break;
574 	case KVASER_USBCAN:
575 		dev->fw_version = le32_to_cpu(cmd.u.usbcan.softinfo.fw_version);
576 		dev->max_tx_urbs =
577 			le16_to_cpu(cmd.u.usbcan.softinfo.max_outstanding_tx);
578 		dev->cfg = &kvaser_usb_leaf_usbcan_dev_cfg;
579 		break;
580 	}
581 
582 	return 0;
583 }
584 
585 static int kvaser_usb_leaf_get_software_info(struct kvaser_usb *dev)
586 {
587 	int err;
588 	int retry = 3;
589 
590 	/* On some x86 laptops, plugging a Kvaser device again after
591 	 * an unplug makes the firmware always ignore the very first
592 	 * command. For such a case, provide some room for retries
593 	 * instead of completely exiting the driver.
594 	 */
595 	do {
596 		err = kvaser_usb_leaf_get_software_info_inner(dev);
597 	} while (--retry && err == -ETIMEDOUT);
598 
599 	return err;
600 }
601 
602 static int kvaser_usb_leaf_get_card_info(struct kvaser_usb *dev)
603 {
604 	struct kvaser_cmd cmd;
605 	int err;
606 
607 	err = kvaser_usb_leaf_send_simple_cmd(dev, CMD_GET_CARD_INFO, 0);
608 	if (err)
609 		return err;
610 
611 	err = kvaser_usb_leaf_wait_cmd(dev, CMD_GET_CARD_INFO_REPLY, &cmd);
612 	if (err)
613 		return err;
614 
615 	dev->nchannels = cmd.u.cardinfo.nchannels;
616 	if (dev->nchannels > KVASER_USB_MAX_NET_DEVICES ||
617 	    (dev->driver_info->family == KVASER_USBCAN &&
618 	     dev->nchannels > MAX_USBCAN_NET_DEVICES))
619 		return -EINVAL;
620 
621 	return 0;
622 }
623 
624 static void kvaser_usb_leaf_tx_acknowledge(const struct kvaser_usb *dev,
625 					   const struct kvaser_cmd *cmd)
626 {
627 	struct net_device_stats *stats;
628 	struct kvaser_usb_tx_urb_context *context;
629 	struct kvaser_usb_net_priv *priv;
630 	unsigned long flags;
631 	u8 channel, tid;
632 
633 	channel = cmd->u.tx_acknowledge_header.channel;
634 	tid = cmd->u.tx_acknowledge_header.tid;
635 
636 	if (channel >= dev->nchannels) {
637 		dev_err(&dev->intf->dev,
638 			"Invalid channel number (%d)\n", channel);
639 		return;
640 	}
641 
642 	priv = dev->nets[channel];
643 
644 	if (!netif_device_present(priv->netdev))
645 		return;
646 
647 	stats = &priv->netdev->stats;
648 
649 	context = &priv->tx_contexts[tid % dev->max_tx_urbs];
650 
651 	/* Sometimes the state change doesn't come after a bus-off event */
652 	if (priv->can.restart_ms && priv->can.state >= CAN_STATE_BUS_OFF) {
653 		struct sk_buff *skb;
654 		struct can_frame *cf;
655 
656 		skb = alloc_can_err_skb(priv->netdev, &cf);
657 		if (skb) {
658 			cf->can_id |= CAN_ERR_RESTARTED;
659 
660 			netif_rx(skb);
661 		} else {
662 			netdev_err(priv->netdev,
663 				   "No memory left for err_skb\n");
664 		}
665 
666 		priv->can.can_stats.restarts++;
667 		netif_carrier_on(priv->netdev);
668 
669 		priv->can.state = CAN_STATE_ERROR_ACTIVE;
670 	}
671 
672 	spin_lock_irqsave(&priv->tx_contexts_lock, flags);
673 
674 	stats->tx_packets++;
675 	stats->tx_bytes += can_get_echo_skb(priv->netdev,
676 					    context->echo_index, NULL);
677 	context->echo_index = dev->max_tx_urbs;
678 	--priv->active_tx_contexts;
679 	netif_wake_queue(priv->netdev);
680 
681 	spin_unlock_irqrestore(&priv->tx_contexts_lock, flags);
682 }
683 
684 static int kvaser_usb_leaf_simple_cmd_async(struct kvaser_usb_net_priv *priv,
685 					    u8 cmd_id)
686 {
687 	struct kvaser_cmd *cmd;
688 	int err;
689 
690 	cmd = kzalloc(sizeof(*cmd), GFP_ATOMIC);
691 	if (!cmd)
692 		return -ENOMEM;
693 
694 	cmd->len = CMD_HEADER_LEN + sizeof(struct kvaser_cmd_simple);
695 	cmd->id = cmd_id;
696 	cmd->u.simple.channel = priv->channel;
697 
698 	err = kvaser_usb_send_cmd_async(priv, cmd, cmd->len);
699 	if (err)
700 		kfree(cmd);
701 
702 	return err;
703 }
704 
705 static void
706 kvaser_usb_leaf_rx_error_update_can_state(struct kvaser_usb_net_priv *priv,
707 					const struct kvaser_usb_err_summary *es,
708 					struct can_frame *cf)
709 {
710 	struct kvaser_usb *dev = priv->dev;
711 	struct net_device_stats *stats = &priv->netdev->stats;
712 	enum can_state cur_state, new_state, tx_state, rx_state;
713 
714 	netdev_dbg(priv->netdev, "Error status: 0x%02x\n", es->status);
715 
716 	new_state = priv->can.state;
717 	cur_state = priv->can.state;
718 
719 	if (es->status & (M16C_STATE_BUS_OFF | M16C_STATE_BUS_RESET)) {
720 		new_state = CAN_STATE_BUS_OFF;
721 	} else if (es->status & M16C_STATE_BUS_PASSIVE) {
722 		new_state = CAN_STATE_ERROR_PASSIVE;
723 	} else if (es->status & M16C_STATE_BUS_ERROR) {
724 		/* Guard against spurious error events after a busoff */
725 		if (cur_state < CAN_STATE_BUS_OFF) {
726 			if (es->txerr >= 128 || es->rxerr >= 128)
727 				new_state = CAN_STATE_ERROR_PASSIVE;
728 			else if (es->txerr >= 96 || es->rxerr >= 96)
729 				new_state = CAN_STATE_ERROR_WARNING;
730 			else if (cur_state > CAN_STATE_ERROR_ACTIVE)
731 				new_state = CAN_STATE_ERROR_ACTIVE;
732 		}
733 	}
734 
735 	if (!es->status)
736 		new_state = CAN_STATE_ERROR_ACTIVE;
737 
738 	if (new_state != cur_state) {
739 		tx_state = (es->txerr >= es->rxerr) ? new_state : 0;
740 		rx_state = (es->txerr <= es->rxerr) ? new_state : 0;
741 
742 		can_change_state(priv->netdev, cf, tx_state, rx_state);
743 	}
744 
745 	if (priv->can.restart_ms &&
746 	    cur_state >= CAN_STATE_BUS_OFF &&
747 	    new_state < CAN_STATE_BUS_OFF)
748 		priv->can.can_stats.restarts++;
749 
750 	switch (dev->driver_info->family) {
751 	case KVASER_LEAF:
752 		if (es->leaf.error_factor) {
753 			priv->can.can_stats.bus_error++;
754 			stats->rx_errors++;
755 		}
756 		break;
757 	case KVASER_USBCAN:
758 		if (es->usbcan.error_state & USBCAN_ERROR_STATE_TX_ERROR)
759 			stats->tx_errors++;
760 		if (es->usbcan.error_state & USBCAN_ERROR_STATE_RX_ERROR)
761 			stats->rx_errors++;
762 		if (es->usbcan.error_state & USBCAN_ERROR_STATE_BUSERROR)
763 			priv->can.can_stats.bus_error++;
764 		break;
765 	}
766 
767 	priv->bec.txerr = es->txerr;
768 	priv->bec.rxerr = es->rxerr;
769 }
770 
771 static void kvaser_usb_leaf_rx_error(const struct kvaser_usb *dev,
772 				     const struct kvaser_usb_err_summary *es)
773 {
774 	struct can_frame *cf;
775 	struct can_frame tmp_cf = { .can_id = CAN_ERR_FLAG,
776 				    .len = CAN_ERR_DLC };
777 	struct sk_buff *skb;
778 	struct net_device_stats *stats;
779 	struct kvaser_usb_net_priv *priv;
780 	enum can_state old_state, new_state;
781 
782 	if (es->channel >= dev->nchannels) {
783 		dev_err(&dev->intf->dev,
784 			"Invalid channel number (%d)\n", es->channel);
785 		return;
786 	}
787 
788 	priv = dev->nets[es->channel];
789 	stats = &priv->netdev->stats;
790 
791 	/* Update all of the CAN interface's state and error counters before
792 	 * trying any memory allocation that can actually fail with -ENOMEM.
793 	 *
794 	 * We send a temporary stack-allocated error CAN frame to
795 	 * can_change_state() for the very same reason.
796 	 *
797 	 * TODO: Split can_change_state() responsibility between updating the
798 	 * CAN interface's state and counters, and the setting up of CAN error
799 	 * frame ID and data to userspace. Remove stack allocation afterwards.
800 	 */
801 	old_state = priv->can.state;
802 	kvaser_usb_leaf_rx_error_update_can_state(priv, es, &tmp_cf);
803 	new_state = priv->can.state;
804 
805 	skb = alloc_can_err_skb(priv->netdev, &cf);
806 	if (!skb) {
807 		stats->rx_dropped++;
808 		return;
809 	}
810 	memcpy(cf, &tmp_cf, sizeof(*cf));
811 
812 	if (new_state != old_state) {
813 		if (es->status &
814 		    (M16C_STATE_BUS_OFF | M16C_STATE_BUS_RESET)) {
815 			if (!priv->can.restart_ms)
816 				kvaser_usb_leaf_simple_cmd_async(priv,
817 								 CMD_STOP_CHIP);
818 			netif_carrier_off(priv->netdev);
819 		}
820 
821 		if (priv->can.restart_ms &&
822 		    old_state >= CAN_STATE_BUS_OFF &&
823 		    new_state < CAN_STATE_BUS_OFF) {
824 			cf->can_id |= CAN_ERR_RESTARTED;
825 			netif_carrier_on(priv->netdev);
826 		}
827 	}
828 
829 	switch (dev->driver_info->family) {
830 	case KVASER_LEAF:
831 		if (es->leaf.error_factor) {
832 			cf->can_id |= CAN_ERR_BUSERROR | CAN_ERR_PROT;
833 
834 			if (es->leaf.error_factor & M16C_EF_ACKE)
835 				cf->data[3] = CAN_ERR_PROT_LOC_ACK;
836 			if (es->leaf.error_factor & M16C_EF_CRCE)
837 				cf->data[3] = CAN_ERR_PROT_LOC_CRC_SEQ;
838 			if (es->leaf.error_factor & M16C_EF_FORME)
839 				cf->data[2] |= CAN_ERR_PROT_FORM;
840 			if (es->leaf.error_factor & M16C_EF_STFE)
841 				cf->data[2] |= CAN_ERR_PROT_STUFF;
842 			if (es->leaf.error_factor & M16C_EF_BITE0)
843 				cf->data[2] |= CAN_ERR_PROT_BIT0;
844 			if (es->leaf.error_factor & M16C_EF_BITE1)
845 				cf->data[2] |= CAN_ERR_PROT_BIT1;
846 			if (es->leaf.error_factor & M16C_EF_TRE)
847 				cf->data[2] |= CAN_ERR_PROT_TX;
848 		}
849 		break;
850 	case KVASER_USBCAN:
851 		if (es->usbcan.error_state & USBCAN_ERROR_STATE_BUSERROR)
852 			cf->can_id |= CAN_ERR_BUSERROR;
853 		break;
854 	}
855 
856 	if (new_state != CAN_STATE_BUS_OFF) {
857 		cf->can_id |= CAN_ERR_CNT;
858 		cf->data[6] = es->txerr;
859 		cf->data[7] = es->rxerr;
860 	}
861 
862 	netif_rx(skb);
863 }
864 
865 /* For USBCAN, report error to userspace if the channels's errors counter
866  * has changed, or we're the only channel seeing a bus error state.
867  */
868 static void
869 kvaser_usb_leaf_usbcan_conditionally_rx_error(const struct kvaser_usb *dev,
870 					      struct kvaser_usb_err_summary *es)
871 {
872 	struct kvaser_usb_net_priv *priv;
873 	unsigned int channel;
874 	bool report_error;
875 
876 	channel = es->channel;
877 	if (channel >= dev->nchannels) {
878 		dev_err(&dev->intf->dev,
879 			"Invalid channel number (%d)\n", channel);
880 		return;
881 	}
882 
883 	priv = dev->nets[channel];
884 	report_error = false;
885 
886 	if (es->txerr != priv->bec.txerr) {
887 		es->usbcan.error_state |= USBCAN_ERROR_STATE_TX_ERROR;
888 		report_error = true;
889 	}
890 	if (es->rxerr != priv->bec.rxerr) {
891 		es->usbcan.error_state |= USBCAN_ERROR_STATE_RX_ERROR;
892 		report_error = true;
893 	}
894 	if ((es->status & M16C_STATE_BUS_ERROR) &&
895 	    !(es->usbcan.other_ch_status & M16C_STATE_BUS_ERROR)) {
896 		es->usbcan.error_state |= USBCAN_ERROR_STATE_BUSERROR;
897 		report_error = true;
898 	}
899 
900 	if (report_error)
901 		kvaser_usb_leaf_rx_error(dev, es);
902 }
903 
904 static void kvaser_usb_leaf_usbcan_rx_error(const struct kvaser_usb *dev,
905 					    const struct kvaser_cmd *cmd)
906 {
907 	struct kvaser_usb_err_summary es = { };
908 
909 	switch (cmd->id) {
910 	/* Sometimes errors are sent as unsolicited chip state events */
911 	case CMD_CHIP_STATE_EVENT:
912 		es.channel = cmd->u.usbcan.chip_state_event.channel;
913 		es.status = cmd->u.usbcan.chip_state_event.status;
914 		es.txerr = cmd->u.usbcan.chip_state_event.tx_errors_count;
915 		es.rxerr = cmd->u.usbcan.chip_state_event.rx_errors_count;
916 		kvaser_usb_leaf_usbcan_conditionally_rx_error(dev, &es);
917 		break;
918 
919 	case CMD_CAN_ERROR_EVENT:
920 		es.channel = 0;
921 		es.status = cmd->u.usbcan.error_event.status_ch0;
922 		es.txerr = cmd->u.usbcan.error_event.tx_errors_count_ch0;
923 		es.rxerr = cmd->u.usbcan.error_event.rx_errors_count_ch0;
924 		es.usbcan.other_ch_status =
925 			cmd->u.usbcan.error_event.status_ch1;
926 		kvaser_usb_leaf_usbcan_conditionally_rx_error(dev, &es);
927 
928 		/* The USBCAN firmware supports up to 2 channels.
929 		 * Now that ch0 was checked, check if ch1 has any errors.
930 		 */
931 		if (dev->nchannels == MAX_USBCAN_NET_DEVICES) {
932 			es.channel = 1;
933 			es.status = cmd->u.usbcan.error_event.status_ch1;
934 			es.txerr =
935 				cmd->u.usbcan.error_event.tx_errors_count_ch1;
936 			es.rxerr =
937 				cmd->u.usbcan.error_event.rx_errors_count_ch1;
938 			es.usbcan.other_ch_status =
939 				cmd->u.usbcan.error_event.status_ch0;
940 			kvaser_usb_leaf_usbcan_conditionally_rx_error(dev, &es);
941 		}
942 		break;
943 
944 	default:
945 		dev_err(&dev->intf->dev, "Invalid cmd id (%d)\n", cmd->id);
946 	}
947 }
948 
949 static void kvaser_usb_leaf_leaf_rx_error(const struct kvaser_usb *dev,
950 					  const struct kvaser_cmd *cmd)
951 {
952 	struct kvaser_usb_err_summary es = { };
953 
954 	switch (cmd->id) {
955 	case CMD_CAN_ERROR_EVENT:
956 		es.channel = cmd->u.leaf.error_event.channel;
957 		es.status = cmd->u.leaf.error_event.status;
958 		es.txerr = cmd->u.leaf.error_event.tx_errors_count;
959 		es.rxerr = cmd->u.leaf.error_event.rx_errors_count;
960 		es.leaf.error_factor = cmd->u.leaf.error_event.error_factor;
961 		break;
962 	case CMD_LEAF_LOG_MESSAGE:
963 		es.channel = cmd->u.leaf.log_message.channel;
964 		es.status = cmd->u.leaf.log_message.data[0];
965 		es.txerr = cmd->u.leaf.log_message.data[2];
966 		es.rxerr = cmd->u.leaf.log_message.data[3];
967 		es.leaf.error_factor = cmd->u.leaf.log_message.data[1];
968 		break;
969 	case CMD_CHIP_STATE_EVENT:
970 		es.channel = cmd->u.leaf.chip_state_event.channel;
971 		es.status = cmd->u.leaf.chip_state_event.status;
972 		es.txerr = cmd->u.leaf.chip_state_event.tx_errors_count;
973 		es.rxerr = cmd->u.leaf.chip_state_event.rx_errors_count;
974 		es.leaf.error_factor = 0;
975 		break;
976 	default:
977 		dev_err(&dev->intf->dev, "Invalid cmd id (%d)\n", cmd->id);
978 		return;
979 	}
980 
981 	kvaser_usb_leaf_rx_error(dev, &es);
982 }
983 
984 static void kvaser_usb_leaf_rx_can_err(const struct kvaser_usb_net_priv *priv,
985 				       const struct kvaser_cmd *cmd)
986 {
987 	if (cmd->u.rx_can_header.flag & (MSG_FLAG_ERROR_FRAME |
988 					 MSG_FLAG_NERR)) {
989 		struct net_device_stats *stats = &priv->netdev->stats;
990 
991 		netdev_err(priv->netdev, "Unknown error (flags: 0x%02x)\n",
992 			   cmd->u.rx_can_header.flag);
993 
994 		stats->rx_errors++;
995 		return;
996 	}
997 
998 	if (cmd->u.rx_can_header.flag & MSG_FLAG_OVERRUN)
999 		kvaser_usb_can_rx_over_error(priv->netdev);
1000 }
1001 
1002 static void kvaser_usb_leaf_rx_can_msg(const struct kvaser_usb *dev,
1003 				       const struct kvaser_cmd *cmd)
1004 {
1005 	struct kvaser_usb_net_priv *priv;
1006 	struct can_frame *cf;
1007 	struct sk_buff *skb;
1008 	struct net_device_stats *stats;
1009 	u8 channel = cmd->u.rx_can_header.channel;
1010 	const u8 *rx_data = NULL;	/* GCC */
1011 
1012 	if (channel >= dev->nchannels) {
1013 		dev_err(&dev->intf->dev,
1014 			"Invalid channel number (%d)\n", channel);
1015 		return;
1016 	}
1017 
1018 	priv = dev->nets[channel];
1019 	stats = &priv->netdev->stats;
1020 
1021 	if ((cmd->u.rx_can_header.flag & MSG_FLAG_ERROR_FRAME) &&
1022 	    (dev->driver_info->family == KVASER_LEAF &&
1023 	     cmd->id == CMD_LEAF_LOG_MESSAGE)) {
1024 		kvaser_usb_leaf_leaf_rx_error(dev, cmd);
1025 		return;
1026 	} else if (cmd->u.rx_can_header.flag & (MSG_FLAG_ERROR_FRAME |
1027 						MSG_FLAG_NERR |
1028 						MSG_FLAG_OVERRUN)) {
1029 		kvaser_usb_leaf_rx_can_err(priv, cmd);
1030 		return;
1031 	} else if (cmd->u.rx_can_header.flag & ~MSG_FLAG_REMOTE_FRAME) {
1032 		netdev_warn(priv->netdev,
1033 			    "Unhandled frame (flags: 0x%02x)\n",
1034 			    cmd->u.rx_can_header.flag);
1035 		return;
1036 	}
1037 
1038 	switch (dev->driver_info->family) {
1039 	case KVASER_LEAF:
1040 		rx_data = cmd->u.leaf.rx_can.data;
1041 		break;
1042 	case KVASER_USBCAN:
1043 		rx_data = cmd->u.usbcan.rx_can.data;
1044 		break;
1045 	}
1046 
1047 	skb = alloc_can_skb(priv->netdev, &cf);
1048 	if (!skb) {
1049 		stats->rx_dropped++;
1050 		return;
1051 	}
1052 
1053 	if (dev->driver_info->family == KVASER_LEAF && cmd->id ==
1054 	    CMD_LEAF_LOG_MESSAGE) {
1055 		cf->can_id = le32_to_cpu(cmd->u.leaf.log_message.id);
1056 		if (cf->can_id & KVASER_EXTENDED_FRAME)
1057 			cf->can_id &= CAN_EFF_MASK | CAN_EFF_FLAG;
1058 		else
1059 			cf->can_id &= CAN_SFF_MASK;
1060 
1061 		cf->len = can_cc_dlc2len(cmd->u.leaf.log_message.dlc);
1062 
1063 		if (cmd->u.leaf.log_message.flags & MSG_FLAG_REMOTE_FRAME)
1064 			cf->can_id |= CAN_RTR_FLAG;
1065 		else
1066 			memcpy(cf->data, &cmd->u.leaf.log_message.data,
1067 			       cf->len);
1068 	} else {
1069 		cf->can_id = ((rx_data[0] & 0x1f) << 6) | (rx_data[1] & 0x3f);
1070 
1071 		if (cmd->id == CMD_RX_EXT_MESSAGE) {
1072 			cf->can_id <<= 18;
1073 			cf->can_id |= ((rx_data[2] & 0x0f) << 14) |
1074 				      ((rx_data[3] & 0xff) << 6) |
1075 				      (rx_data[4] & 0x3f);
1076 			cf->can_id |= CAN_EFF_FLAG;
1077 		}
1078 
1079 		cf->len = can_cc_dlc2len(rx_data[5]);
1080 
1081 		if (cmd->u.rx_can_header.flag & MSG_FLAG_REMOTE_FRAME)
1082 			cf->can_id |= CAN_RTR_FLAG;
1083 		else
1084 			memcpy(cf->data, &rx_data[6], cf->len);
1085 	}
1086 
1087 	stats->rx_packets++;
1088 	if (!(cf->can_id & CAN_RTR_FLAG))
1089 		stats->rx_bytes += cf->len;
1090 	netif_rx(skb);
1091 }
1092 
1093 static void kvaser_usb_leaf_start_chip_reply(const struct kvaser_usb *dev,
1094 					     const struct kvaser_cmd *cmd)
1095 {
1096 	struct kvaser_usb_net_priv *priv;
1097 	u8 channel = cmd->u.simple.channel;
1098 
1099 	if (channel >= dev->nchannels) {
1100 		dev_err(&dev->intf->dev,
1101 			"Invalid channel number (%d)\n", channel);
1102 		return;
1103 	}
1104 
1105 	priv = dev->nets[channel];
1106 
1107 	if (completion_done(&priv->start_comp) &&
1108 	    netif_queue_stopped(priv->netdev)) {
1109 		netif_wake_queue(priv->netdev);
1110 	} else {
1111 		netif_start_queue(priv->netdev);
1112 		complete(&priv->start_comp);
1113 	}
1114 }
1115 
1116 static void kvaser_usb_leaf_stop_chip_reply(const struct kvaser_usb *dev,
1117 					    const struct kvaser_cmd *cmd)
1118 {
1119 	struct kvaser_usb_net_priv *priv;
1120 	u8 channel = cmd->u.simple.channel;
1121 
1122 	if (channel >= dev->nchannels) {
1123 		dev_err(&dev->intf->dev,
1124 			"Invalid channel number (%d)\n", channel);
1125 		return;
1126 	}
1127 
1128 	priv = dev->nets[channel];
1129 
1130 	complete(&priv->stop_comp);
1131 }
1132 
1133 static void kvaser_usb_leaf_handle_command(const struct kvaser_usb *dev,
1134 					   const struct kvaser_cmd *cmd)
1135 {
1136 	switch (cmd->id) {
1137 	case CMD_START_CHIP_REPLY:
1138 		kvaser_usb_leaf_start_chip_reply(dev, cmd);
1139 		break;
1140 
1141 	case CMD_STOP_CHIP_REPLY:
1142 		kvaser_usb_leaf_stop_chip_reply(dev, cmd);
1143 		break;
1144 
1145 	case CMD_RX_STD_MESSAGE:
1146 	case CMD_RX_EXT_MESSAGE:
1147 		kvaser_usb_leaf_rx_can_msg(dev, cmd);
1148 		break;
1149 
1150 	case CMD_LEAF_LOG_MESSAGE:
1151 		if (dev->driver_info->family != KVASER_LEAF)
1152 			goto warn;
1153 		kvaser_usb_leaf_rx_can_msg(dev, cmd);
1154 		break;
1155 
1156 	case CMD_CHIP_STATE_EVENT:
1157 	case CMD_CAN_ERROR_EVENT:
1158 		if (dev->driver_info->family == KVASER_LEAF)
1159 			kvaser_usb_leaf_leaf_rx_error(dev, cmd);
1160 		else
1161 			kvaser_usb_leaf_usbcan_rx_error(dev, cmd);
1162 		break;
1163 
1164 	case CMD_TX_ACKNOWLEDGE:
1165 		kvaser_usb_leaf_tx_acknowledge(dev, cmd);
1166 		break;
1167 
1168 	/* Ignored commands */
1169 	case CMD_USBCAN_CLOCK_OVERFLOW_EVENT:
1170 		if (dev->driver_info->family != KVASER_USBCAN)
1171 			goto warn;
1172 		break;
1173 
1174 	case CMD_FLUSH_QUEUE_REPLY:
1175 		if (dev->driver_info->family != KVASER_LEAF)
1176 			goto warn;
1177 		break;
1178 
1179 	default:
1180 warn:		dev_warn(&dev->intf->dev, "Unhandled command (%d)\n", cmd->id);
1181 		break;
1182 	}
1183 }
1184 
1185 static void kvaser_usb_leaf_read_bulk_callback(struct kvaser_usb *dev,
1186 					       void *buf, int len)
1187 {
1188 	struct kvaser_cmd *cmd;
1189 	int pos = 0;
1190 
1191 	while (pos <= len - CMD_HEADER_LEN) {
1192 		cmd = buf + pos;
1193 
1194 		/* The Kvaser firmware can only read and write commands that
1195 		 * does not cross the USB's endpoint wMaxPacketSize boundary.
1196 		 * If a follow-up command crosses such boundary, firmware puts
1197 		 * a placeholder zero-length command in its place then aligns
1198 		 * the real command to the next max packet size.
1199 		 *
1200 		 * Handle such cases or we're going to miss a significant
1201 		 * number of events in case of a heavy rx load on the bus.
1202 		 */
1203 		if (cmd->len == 0) {
1204 			pos = round_up(pos, le16_to_cpu
1205 						(dev->bulk_in->wMaxPacketSize));
1206 			continue;
1207 		}
1208 
1209 		if (pos + cmd->len > len) {
1210 			dev_err_ratelimited(&dev->intf->dev, "Format error\n");
1211 			break;
1212 		}
1213 
1214 		kvaser_usb_leaf_handle_command(dev, cmd);
1215 		pos += cmd->len;
1216 	}
1217 }
1218 
1219 static int kvaser_usb_leaf_set_opt_mode(const struct kvaser_usb_net_priv *priv)
1220 {
1221 	struct kvaser_cmd *cmd;
1222 	int rc;
1223 
1224 	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1225 	if (!cmd)
1226 		return -ENOMEM;
1227 
1228 	cmd->id = CMD_SET_CTRL_MODE;
1229 	cmd->len = CMD_HEADER_LEN + sizeof(struct kvaser_cmd_ctrl_mode);
1230 	cmd->u.ctrl_mode.tid = 0xff;
1231 	cmd->u.ctrl_mode.channel = priv->channel;
1232 
1233 	if (priv->can.ctrlmode & CAN_CTRLMODE_LISTENONLY)
1234 		cmd->u.ctrl_mode.ctrl_mode = KVASER_CTRL_MODE_SILENT;
1235 	else
1236 		cmd->u.ctrl_mode.ctrl_mode = KVASER_CTRL_MODE_NORMAL;
1237 
1238 	rc = kvaser_usb_send_cmd(priv->dev, cmd, cmd->len);
1239 
1240 	kfree(cmd);
1241 	return rc;
1242 }
1243 
1244 static int kvaser_usb_leaf_start_chip(struct kvaser_usb_net_priv *priv)
1245 {
1246 	int err;
1247 
1248 	init_completion(&priv->start_comp);
1249 
1250 	err = kvaser_usb_leaf_send_simple_cmd(priv->dev, CMD_START_CHIP,
1251 					      priv->channel);
1252 	if (err)
1253 		return err;
1254 
1255 	if (!wait_for_completion_timeout(&priv->start_comp,
1256 					 msecs_to_jiffies(KVASER_USB_TIMEOUT)))
1257 		return -ETIMEDOUT;
1258 
1259 	return 0;
1260 }
1261 
1262 static int kvaser_usb_leaf_stop_chip(struct kvaser_usb_net_priv *priv)
1263 {
1264 	int err;
1265 
1266 	init_completion(&priv->stop_comp);
1267 
1268 	err = kvaser_usb_leaf_send_simple_cmd(priv->dev, CMD_STOP_CHIP,
1269 					      priv->channel);
1270 	if (err)
1271 		return err;
1272 
1273 	if (!wait_for_completion_timeout(&priv->stop_comp,
1274 					 msecs_to_jiffies(KVASER_USB_TIMEOUT)))
1275 		return -ETIMEDOUT;
1276 
1277 	return 0;
1278 }
1279 
1280 static int kvaser_usb_leaf_reset_chip(struct kvaser_usb *dev, int channel)
1281 {
1282 	return kvaser_usb_leaf_send_simple_cmd(dev, CMD_RESET_CHIP, channel);
1283 }
1284 
1285 static int kvaser_usb_leaf_flush_queue(struct kvaser_usb_net_priv *priv)
1286 {
1287 	struct kvaser_cmd *cmd;
1288 	int rc;
1289 
1290 	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1291 	if (!cmd)
1292 		return -ENOMEM;
1293 
1294 	cmd->id = CMD_FLUSH_QUEUE;
1295 	cmd->len = CMD_HEADER_LEN + sizeof(struct kvaser_cmd_flush_queue);
1296 	cmd->u.flush_queue.channel = priv->channel;
1297 	cmd->u.flush_queue.flags = 0x00;
1298 
1299 	rc = kvaser_usb_send_cmd(priv->dev, cmd, cmd->len);
1300 
1301 	kfree(cmd);
1302 	return rc;
1303 }
1304 
1305 static int kvaser_usb_leaf_init_card(struct kvaser_usb *dev)
1306 {
1307 	struct kvaser_usb_dev_card_data *card_data = &dev->card_data;
1308 
1309 	card_data->ctrlmode_supported |= CAN_CTRLMODE_3_SAMPLES;
1310 
1311 	return 0;
1312 }
1313 
1314 static int kvaser_usb_leaf_set_bittiming(struct net_device *netdev)
1315 {
1316 	struct kvaser_usb_net_priv *priv = netdev_priv(netdev);
1317 	struct can_bittiming *bt = &priv->can.bittiming;
1318 	struct kvaser_usb *dev = priv->dev;
1319 	struct kvaser_cmd *cmd;
1320 	int rc;
1321 
1322 	cmd = kmalloc(sizeof(*cmd), GFP_KERNEL);
1323 	if (!cmd)
1324 		return -ENOMEM;
1325 
1326 	cmd->id = CMD_SET_BUS_PARAMS;
1327 	cmd->len = CMD_HEADER_LEN + sizeof(struct kvaser_cmd_busparams);
1328 	cmd->u.busparams.channel = priv->channel;
1329 	cmd->u.busparams.tid = 0xff;
1330 	cmd->u.busparams.bitrate = cpu_to_le32(bt->bitrate);
1331 	cmd->u.busparams.sjw = bt->sjw;
1332 	cmd->u.busparams.tseg1 = bt->prop_seg + bt->phase_seg1;
1333 	cmd->u.busparams.tseg2 = bt->phase_seg2;
1334 
1335 	if (priv->can.ctrlmode & CAN_CTRLMODE_3_SAMPLES)
1336 		cmd->u.busparams.no_samp = 3;
1337 	else
1338 		cmd->u.busparams.no_samp = 1;
1339 
1340 	rc = kvaser_usb_send_cmd(dev, cmd, cmd->len);
1341 
1342 	kfree(cmd);
1343 	return rc;
1344 }
1345 
1346 static int kvaser_usb_leaf_set_mode(struct net_device *netdev,
1347 				    enum can_mode mode)
1348 {
1349 	struct kvaser_usb_net_priv *priv = netdev_priv(netdev);
1350 	int err;
1351 
1352 	switch (mode) {
1353 	case CAN_MODE_START:
1354 		err = kvaser_usb_leaf_simple_cmd_async(priv, CMD_START_CHIP);
1355 		if (err)
1356 			return err;
1357 		break;
1358 	default:
1359 		return -EOPNOTSUPP;
1360 	}
1361 
1362 	return 0;
1363 }
1364 
1365 static int kvaser_usb_leaf_get_berr_counter(const struct net_device *netdev,
1366 					    struct can_berr_counter *bec)
1367 {
1368 	struct kvaser_usb_net_priv *priv = netdev_priv(netdev);
1369 
1370 	*bec = priv->bec;
1371 
1372 	return 0;
1373 }
1374 
1375 static int kvaser_usb_leaf_setup_endpoints(struct kvaser_usb *dev)
1376 {
1377 	const struct usb_host_interface *iface_desc;
1378 	struct usb_endpoint_descriptor *endpoint;
1379 	int i;
1380 
1381 	iface_desc = dev->intf->cur_altsetting;
1382 
1383 	for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
1384 		endpoint = &iface_desc->endpoint[i].desc;
1385 
1386 		if (!dev->bulk_in && usb_endpoint_is_bulk_in(endpoint))
1387 			dev->bulk_in = endpoint;
1388 
1389 		if (!dev->bulk_out && usb_endpoint_is_bulk_out(endpoint))
1390 			dev->bulk_out = endpoint;
1391 
1392 		/* use first bulk endpoint for in and out */
1393 		if (dev->bulk_in && dev->bulk_out)
1394 			return 0;
1395 	}
1396 
1397 	return -ENODEV;
1398 }
1399 
1400 const struct kvaser_usb_dev_ops kvaser_usb_leaf_dev_ops = {
1401 	.dev_set_mode = kvaser_usb_leaf_set_mode,
1402 	.dev_set_bittiming = kvaser_usb_leaf_set_bittiming,
1403 	.dev_set_data_bittiming = NULL,
1404 	.dev_get_berr_counter = kvaser_usb_leaf_get_berr_counter,
1405 	.dev_setup_endpoints = kvaser_usb_leaf_setup_endpoints,
1406 	.dev_init_card = kvaser_usb_leaf_init_card,
1407 	.dev_get_software_info = kvaser_usb_leaf_get_software_info,
1408 	.dev_get_software_details = NULL,
1409 	.dev_get_card_info = kvaser_usb_leaf_get_card_info,
1410 	.dev_get_capabilities = NULL,
1411 	.dev_set_opt_mode = kvaser_usb_leaf_set_opt_mode,
1412 	.dev_start_chip = kvaser_usb_leaf_start_chip,
1413 	.dev_stop_chip = kvaser_usb_leaf_stop_chip,
1414 	.dev_reset_chip = kvaser_usb_leaf_reset_chip,
1415 	.dev_flush_queue = kvaser_usb_leaf_flush_queue,
1416 	.dev_read_bulk_callback = kvaser_usb_leaf_read_bulk_callback,
1417 	.dev_frame_to_cmd = kvaser_usb_leaf_frame_to_cmd,
1418 };
1419