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