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