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