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 /* started but not reported as bus-on yet */
431 bool joining_bus;
432 };
433
434 static const struct can_bittiming_const kvaser_usb_leaf_m16c_bittiming_const = {
435 .name = "kvaser_usb_ucii",
436 .tseg1_min = 4,
437 .tseg1_max = 16,
438 .tseg2_min = 2,
439 .tseg2_max = 8,
440 .sjw_max = 4,
441 .brp_min = 1,
442 .brp_max = 16,
443 .brp_inc = 1,
444 };
445
446 static const struct can_bittiming_const kvaser_usb_leaf_m32c_bittiming_const = {
447 .name = "kvaser_usb_leaf",
448 .tseg1_min = 3,
449 .tseg1_max = 16,
450 .tseg2_min = 2,
451 .tseg2_max = 8,
452 .sjw_max = 4,
453 .brp_min = 2,
454 .brp_max = 128,
455 .brp_inc = 2,
456 };
457
458 static const struct kvaser_usb_dev_cfg kvaser_usb_leaf_usbcan_dev_cfg = {
459 .clock = {
460 .freq = 8 * MEGA /* Hz */,
461 },
462 .timestamp_freq = 1,
463 .bittiming_const = &kvaser_usb_leaf_m16c_bittiming_const,
464 };
465
466 static const struct kvaser_usb_dev_cfg kvaser_usb_leaf_m32c_dev_cfg = {
467 .clock = {
468 .freq = 16 * MEGA /* Hz */,
469 },
470 .timestamp_freq = 1,
471 .bittiming_const = &kvaser_usb_leaf_m32c_bittiming_const,
472 };
473
474 static const struct kvaser_usb_dev_cfg kvaser_usb_leaf_imx_dev_cfg_16mhz = {
475 .clock = {
476 .freq = 16 * MEGA /* Hz */,
477 },
478 .timestamp_freq = 1,
479 .bittiming_const = &kvaser_usb_flexc_bittiming_const,
480 };
481
482 static const struct kvaser_usb_dev_cfg kvaser_usb_leaf_imx_dev_cfg_24mhz = {
483 .clock = {
484 .freq = 24 * MEGA /* Hz */,
485 },
486 .timestamp_freq = 1,
487 .bittiming_const = &kvaser_usb_flexc_bittiming_const,
488 };
489
490 static const struct kvaser_usb_dev_cfg kvaser_usb_leaf_imx_dev_cfg_32mhz = {
491 .clock = {
492 .freq = 32 * MEGA /* Hz */,
493 },
494 .timestamp_freq = 1,
495 .bittiming_const = &kvaser_usb_flexc_bittiming_const,
496 };
497
kvaser_usb_leaf_verify_size(const struct kvaser_usb * dev,const struct kvaser_cmd * cmd)498 static int kvaser_usb_leaf_verify_size(const struct kvaser_usb *dev,
499 const struct kvaser_cmd *cmd)
500 {
501 /* buffer size >= cmd->len ensured by caller */
502 u8 min_size = 0;
503
504 switch (dev->driver_info->family) {
505 case KVASER_LEAF:
506 if (cmd->id < ARRAY_SIZE(kvaser_usb_leaf_cmd_sizes_leaf))
507 min_size = kvaser_usb_leaf_cmd_sizes_leaf[cmd->id];
508 break;
509 case KVASER_USBCAN:
510 if (cmd->id < ARRAY_SIZE(kvaser_usb_leaf_cmd_sizes_usbcan))
511 min_size = kvaser_usb_leaf_cmd_sizes_usbcan[cmd->id];
512 break;
513 }
514
515 if (min_size == CMD_SIZE_ANY)
516 return 0;
517
518 if (min_size) {
519 min_size += CMD_HEADER_LEN;
520 if (cmd->len >= min_size)
521 return 0;
522
523 dev_err_ratelimited(&dev->intf->dev,
524 "Received command %u too short (size %u, needed %u)",
525 cmd->id, cmd->len, min_size);
526 return -EIO;
527 }
528
529 dev_warn_ratelimited(&dev->intf->dev,
530 "Unhandled command (%d, size %d)\n",
531 cmd->id, cmd->len);
532 return -EINVAL;
533 }
534
535 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)536 kvaser_usb_leaf_frame_to_cmd(const struct kvaser_usb_net_priv *priv,
537 const struct sk_buff *skb, int *cmd_len,
538 u16 transid)
539 {
540 struct kvaser_usb *dev = priv->dev;
541 struct kvaser_cmd *cmd;
542 u8 *cmd_tx_can_flags = NULL; /* GCC */
543 struct can_frame *cf = (struct can_frame *)skb->data;
544
545 cmd = kmalloc(sizeof(*cmd), GFP_ATOMIC);
546 if (cmd) {
547 cmd->u.tx_can.tid = transid & 0xff;
548 cmd->len = *cmd_len = CMD_HEADER_LEN +
549 sizeof(struct kvaser_cmd_tx_can);
550 cmd->u.tx_can.channel = priv->channel;
551
552 switch (dev->driver_info->family) {
553 case KVASER_LEAF:
554 cmd_tx_can_flags = &cmd->u.tx_can.leaf.flags;
555 break;
556 case KVASER_USBCAN:
557 cmd_tx_can_flags = &cmd->u.tx_can.usbcan.flags;
558 break;
559 }
560
561 *cmd_tx_can_flags = 0;
562
563 if (cf->can_id & CAN_EFF_FLAG) {
564 cmd->id = CMD_TX_EXT_MESSAGE;
565 cmd->u.tx_can.data[0] = (cf->can_id >> 24) & 0x1f;
566 cmd->u.tx_can.data[1] = (cf->can_id >> 18) & 0x3f;
567 cmd->u.tx_can.data[2] = (cf->can_id >> 14) & 0x0f;
568 cmd->u.tx_can.data[3] = (cf->can_id >> 6) & 0xff;
569 cmd->u.tx_can.data[4] = cf->can_id & 0x3f;
570 } else {
571 cmd->id = CMD_TX_STD_MESSAGE;
572 cmd->u.tx_can.data[0] = (cf->can_id >> 6) & 0x1f;
573 cmd->u.tx_can.data[1] = cf->can_id & 0x3f;
574 }
575
576 cmd->u.tx_can.data[5] = can_get_cc_dlc(cf, priv->can.ctrlmode);
577 memcpy(&cmd->u.tx_can.data[6], cf->data, cf->len);
578
579 if (cf->can_id & CAN_RTR_FLAG)
580 *cmd_tx_can_flags |= MSG_FLAG_REMOTE_FRAME;
581 }
582 return cmd;
583 }
584
kvaser_usb_leaf_wait_cmd(const struct kvaser_usb * dev,u8 id,struct kvaser_cmd * cmd)585 static int kvaser_usb_leaf_wait_cmd(const struct kvaser_usb *dev, u8 id,
586 struct kvaser_cmd *cmd)
587 {
588 struct kvaser_cmd *tmp;
589 void *buf;
590 int actual_len;
591 int err;
592 int pos;
593 unsigned long to = jiffies + msecs_to_jiffies(KVASER_USB_TIMEOUT);
594
595 buf = kzalloc(KVASER_USB_RX_BUFFER_SIZE, GFP_KERNEL);
596 if (!buf)
597 return -ENOMEM;
598
599 do {
600 err = kvaser_usb_recv_cmd(dev, buf, KVASER_USB_RX_BUFFER_SIZE,
601 &actual_len);
602 if (err < 0)
603 goto end;
604
605 pos = 0;
606 while (pos <= actual_len - CMD_HEADER_LEN) {
607 tmp = buf + pos;
608
609 /* Handle commands crossing the USB endpoint max packet
610 * size boundary. Check kvaser_usb_read_bulk_callback()
611 * for further details.
612 */
613 if (tmp->len == 0) {
614 pos = round_up(pos,
615 le16_to_cpu
616 (dev->bulk_in->wMaxPacketSize));
617 continue;
618 }
619
620 if (pos + tmp->len > actual_len) {
621 dev_err_ratelimited(&dev->intf->dev,
622 "Format error\n");
623 break;
624 }
625
626 if (tmp->id == id) {
627 memcpy(cmd, tmp, tmp->len);
628 goto end;
629 }
630
631 pos += tmp->len;
632 }
633 } while (time_before(jiffies, to));
634
635 err = -EINVAL;
636
637 end:
638 kfree(buf);
639
640 if (err == 0)
641 err = kvaser_usb_leaf_verify_size(dev, cmd);
642
643 return err;
644 }
645
kvaser_usb_leaf_send_simple_cmd(const struct kvaser_usb * dev,u8 cmd_id,int channel)646 static int kvaser_usb_leaf_send_simple_cmd(const struct kvaser_usb *dev,
647 u8 cmd_id, int channel)
648 {
649 struct kvaser_cmd *cmd;
650 int rc;
651
652 cmd = kmalloc(sizeof(*cmd), GFP_KERNEL);
653 if (!cmd)
654 return -ENOMEM;
655
656 cmd->id = cmd_id;
657 cmd->len = CMD_HEADER_LEN + sizeof(struct kvaser_cmd_simple);
658 cmd->u.simple.channel = channel;
659 cmd->u.simple.tid = 0xff;
660
661 rc = kvaser_usb_send_cmd(dev, cmd, cmd->len);
662
663 kfree(cmd);
664 return rc;
665 }
666
kvaser_usb_leaf_get_software_info_leaf(struct kvaser_usb * dev,const struct leaf_cmd_softinfo * softinfo)667 static void kvaser_usb_leaf_get_software_info_leaf(struct kvaser_usb *dev,
668 const struct leaf_cmd_softinfo *softinfo)
669 {
670 u32 sw_options = le32_to_cpu(softinfo->sw_options);
671
672 dev->fw_version = le32_to_cpu(softinfo->fw_version);
673 dev->max_tx_urbs = le16_to_cpu(softinfo->max_outstanding_tx);
674
675 if (sw_options & KVASER_USB_LEAF_SWOPTION_EXT_CAP)
676 dev->card_data.capabilities |= KVASER_USB_CAP_EXT_CAP;
677
678 if (dev->driver_info->quirks & KVASER_USB_QUIRK_IGNORE_CLK_FREQ) {
679 /* Firmware expects bittiming parameters calculated for 16MHz
680 * clock, regardless of the actual clock
681 */
682 dev->cfg = &kvaser_usb_leaf_m32c_dev_cfg;
683 } else {
684 switch (sw_options & KVASER_USB_LEAF_SWOPTION_FREQ_MASK) {
685 case KVASER_USB_LEAF_SWOPTION_FREQ_16_MHZ_CLK:
686 dev->cfg = &kvaser_usb_leaf_imx_dev_cfg_16mhz;
687 break;
688 case KVASER_USB_LEAF_SWOPTION_FREQ_24_MHZ_CLK:
689 dev->cfg = &kvaser_usb_leaf_imx_dev_cfg_24mhz;
690 break;
691 case KVASER_USB_LEAF_SWOPTION_FREQ_32_MHZ_CLK:
692 dev->cfg = &kvaser_usb_leaf_imx_dev_cfg_32mhz;
693 break;
694 }
695 }
696 }
697
kvaser_usb_leaf_get_software_info_inner(struct kvaser_usb * dev)698 static int kvaser_usb_leaf_get_software_info_inner(struct kvaser_usb *dev)
699 {
700 struct kvaser_cmd cmd;
701 int err;
702
703 err = kvaser_usb_leaf_send_simple_cmd(dev, CMD_GET_SOFTWARE_INFO, 0);
704 if (err)
705 return err;
706
707 err = kvaser_usb_leaf_wait_cmd(dev, CMD_GET_SOFTWARE_INFO_REPLY, &cmd);
708 if (err)
709 return err;
710
711 switch (dev->driver_info->family) {
712 case KVASER_LEAF:
713 kvaser_usb_leaf_get_software_info_leaf(dev, &cmd.u.leaf.softinfo);
714 break;
715 case KVASER_USBCAN:
716 dev->fw_version = le32_to_cpu(cmd.u.usbcan.softinfo.fw_version);
717 dev->max_tx_urbs =
718 le16_to_cpu(cmd.u.usbcan.softinfo.max_outstanding_tx);
719 dev->cfg = &kvaser_usb_leaf_usbcan_dev_cfg;
720 break;
721 }
722
723 return 0;
724 }
725
kvaser_usb_leaf_get_software_info(struct kvaser_usb * dev)726 static int kvaser_usb_leaf_get_software_info(struct kvaser_usb *dev)
727 {
728 int err;
729 int retry = 3;
730
731 /* On some x86 laptops, plugging a Kvaser device again after
732 * an unplug makes the firmware always ignore the very first
733 * command. For such a case, provide some room for retries
734 * instead of completely exiting the driver.
735 */
736 do {
737 err = kvaser_usb_leaf_get_software_info_inner(dev);
738 } while (--retry && err == -ETIMEDOUT);
739
740 return err;
741 }
742
kvaser_usb_leaf_get_card_info(struct kvaser_usb * dev)743 static int kvaser_usb_leaf_get_card_info(struct kvaser_usb *dev)
744 {
745 struct kvaser_cmd cmd;
746 int err;
747
748 err = kvaser_usb_leaf_send_simple_cmd(dev, CMD_GET_CARD_INFO, 0);
749 if (err)
750 return err;
751
752 err = kvaser_usb_leaf_wait_cmd(dev, CMD_GET_CARD_INFO_REPLY, &cmd);
753 if (err)
754 return err;
755
756 dev->nchannels = cmd.u.cardinfo.nchannels;
757 if (dev->nchannels > KVASER_USB_MAX_NET_DEVICES ||
758 (dev->driver_info->family == KVASER_USBCAN &&
759 dev->nchannels > MAX_USBCAN_NET_DEVICES))
760 return -EINVAL;
761
762 return 0;
763 }
764
kvaser_usb_leaf_get_single_capability(struct kvaser_usb * dev,u16 cap_cmd_req,u16 * status)765 static int kvaser_usb_leaf_get_single_capability(struct kvaser_usb *dev,
766 u16 cap_cmd_req, u16 *status)
767 {
768 struct kvaser_usb_dev_card_data *card_data = &dev->card_data;
769 struct kvaser_cmd *cmd;
770 u32 value = 0;
771 u32 mask = 0;
772 u16 cap_cmd_res;
773 int err;
774 int i;
775
776 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
777 if (!cmd)
778 return -ENOMEM;
779
780 cmd->id = CMD_GET_CAPABILITIES_REQ;
781 cmd->u.leaf.cap_req.cap_cmd = cpu_to_le16(cap_cmd_req);
782 cmd->len = CMD_HEADER_LEN + sizeof(struct kvaser_cmd_cap_req);
783
784 err = kvaser_usb_send_cmd(dev, cmd, cmd->len);
785 if (err)
786 goto end;
787
788 err = kvaser_usb_leaf_wait_cmd(dev, CMD_GET_CAPABILITIES_RESP, cmd);
789 if (err)
790 goto end;
791
792 *status = le16_to_cpu(cmd->u.leaf.cap_res.status);
793
794 if (*status != KVASER_USB_LEAF_CAP_STAT_OK)
795 goto end;
796
797 cap_cmd_res = le16_to_cpu(cmd->u.leaf.cap_res.cap_cmd);
798 switch (cap_cmd_res) {
799 case KVASER_USB_LEAF_CAP_CMD_LISTEN_MODE:
800 case KVASER_USB_LEAF_CAP_CMD_ERR_REPORT:
801 value = le32_to_cpu(cmd->u.leaf.cap_res.value);
802 mask = le32_to_cpu(cmd->u.leaf.cap_res.mask);
803 break;
804 default:
805 dev_warn(&dev->intf->dev, "Unknown capability command %u\n",
806 cap_cmd_res);
807 break;
808 }
809
810 for (i = 0; i < dev->nchannels; i++) {
811 if (BIT(i) & (value & mask)) {
812 switch (cap_cmd_res) {
813 case KVASER_USB_LEAF_CAP_CMD_LISTEN_MODE:
814 card_data->ctrlmode_supported |=
815 CAN_CTRLMODE_LISTENONLY;
816 break;
817 case KVASER_USB_LEAF_CAP_CMD_ERR_REPORT:
818 card_data->capabilities |=
819 KVASER_USB_CAP_BERR_CAP;
820 break;
821 }
822 }
823 }
824
825 end:
826 kfree(cmd);
827
828 return err;
829 }
830
kvaser_usb_leaf_get_capabilities_leaf(struct kvaser_usb * dev)831 static int kvaser_usb_leaf_get_capabilities_leaf(struct kvaser_usb *dev)
832 {
833 int err;
834 u16 status;
835
836 if (!(dev->card_data.capabilities & KVASER_USB_CAP_EXT_CAP)) {
837 dev_info(&dev->intf->dev,
838 "No extended capability support. Upgrade device firmware.\n");
839 return 0;
840 }
841
842 err = kvaser_usb_leaf_get_single_capability(dev,
843 KVASER_USB_LEAF_CAP_CMD_LISTEN_MODE,
844 &status);
845 if (err)
846 return err;
847 if (status)
848 dev_info(&dev->intf->dev,
849 "KVASER_USB_LEAF_CAP_CMD_LISTEN_MODE failed %u\n",
850 status);
851
852 err = kvaser_usb_leaf_get_single_capability(dev,
853 KVASER_USB_LEAF_CAP_CMD_ERR_REPORT,
854 &status);
855 if (err)
856 return err;
857 if (status)
858 dev_info(&dev->intf->dev,
859 "KVASER_USB_LEAF_CAP_CMD_ERR_REPORT failed %u\n",
860 status);
861
862 return 0;
863 }
864
kvaser_usb_leaf_get_capabilities(struct kvaser_usb * dev)865 static int kvaser_usb_leaf_get_capabilities(struct kvaser_usb *dev)
866 {
867 int err = 0;
868
869 if (dev->driver_info->family == KVASER_LEAF)
870 err = kvaser_usb_leaf_get_capabilities_leaf(dev);
871
872 return err;
873 }
874
kvaser_usb_leaf_tx_acknowledge(const struct kvaser_usb * dev,const struct kvaser_cmd * cmd)875 static void kvaser_usb_leaf_tx_acknowledge(const struct kvaser_usb *dev,
876 const struct kvaser_cmd *cmd)
877 {
878 struct net_device_stats *stats;
879 struct kvaser_usb_tx_urb_context *context;
880 struct kvaser_usb_net_priv *priv;
881 unsigned long flags;
882 u8 channel, tid;
883
884 channel = cmd->u.tx_acknowledge_header.channel;
885 tid = cmd->u.tx_acknowledge_header.tid;
886
887 if (channel >= dev->nchannels) {
888 dev_err(&dev->intf->dev,
889 "Invalid channel number (%d)\n", channel);
890 return;
891 }
892
893 priv = dev->nets[channel];
894
895 if (!netif_device_present(priv->netdev))
896 return;
897
898 stats = &priv->netdev->stats;
899
900 context = &priv->tx_contexts[tid % dev->max_tx_urbs];
901
902 /* Sometimes the state change doesn't come after a bus-off event */
903 if (priv->can.restart_ms && priv->can.state == CAN_STATE_BUS_OFF) {
904 struct sk_buff *skb;
905 struct can_frame *cf;
906
907 skb = alloc_can_err_skb(priv->netdev, &cf);
908 if (skb) {
909 cf->can_id |= CAN_ERR_RESTARTED;
910
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 spin_lock_irqsave(&priv->tx_contexts_lock, flags);
924
925 stats->tx_packets++;
926 stats->tx_bytes += can_get_echo_skb(priv->netdev,
927 context->echo_index, NULL);
928 context->echo_index = dev->max_tx_urbs;
929 --priv->active_tx_contexts;
930 netif_wake_queue(priv->netdev);
931
932 spin_unlock_irqrestore(&priv->tx_contexts_lock, flags);
933 }
934
kvaser_usb_leaf_simple_cmd_async(struct kvaser_usb_net_priv * priv,u8 cmd_id)935 static int kvaser_usb_leaf_simple_cmd_async(struct kvaser_usb_net_priv *priv,
936 u8 cmd_id)
937 {
938 struct kvaser_cmd *cmd;
939 int err;
940
941 cmd = kzalloc(sizeof(*cmd), GFP_ATOMIC);
942 if (!cmd)
943 return -ENOMEM;
944
945 cmd->len = CMD_HEADER_LEN + sizeof(struct kvaser_cmd_simple);
946 cmd->id = cmd_id;
947 cmd->u.simple.channel = priv->channel;
948
949 err = kvaser_usb_send_cmd_async(priv, cmd, cmd->len);
950 if (err)
951 kfree(cmd);
952
953 return err;
954 }
955
kvaser_usb_leaf_chip_state_req_work(struct work_struct * work)956 static void kvaser_usb_leaf_chip_state_req_work(struct work_struct *work)
957 {
958 struct kvaser_usb_net_leaf_priv *leaf =
959 container_of(work, struct kvaser_usb_net_leaf_priv,
960 chip_state_req_work.work);
961 struct kvaser_usb_net_priv *priv = leaf->net;
962
963 kvaser_usb_leaf_simple_cmd_async(priv, CMD_GET_CHIP_STATE);
964 }
965
966 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)967 kvaser_usb_leaf_rx_error_update_can_state(struct kvaser_usb_net_priv *priv,
968 const struct kvaser_usb_err_summary *es,
969 struct can_frame *cf)
970 {
971 struct kvaser_usb_net_leaf_priv *leaf = priv->sub_priv;
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 /* 0bfd:0124 FW 4.18.778 was observed to send the initial
997 * CMD_CHIP_STATE_EVENT after CMD_START_CHIP with M16C_STATE_BUS_OFF
998 * bit set if the channel was bus-off when it was last stopped (even
999 * across chip resets). This bit will clear shortly afterwards, without
1000 * triggering a second unsolicited chip state event.
1001 * Ignore this initial bus-off.
1002 */
1003 if (leaf->joining_bus) {
1004 if (new_state == CAN_STATE_BUS_OFF) {
1005 netdev_dbg(priv->netdev, "ignoring bus-off during startup");
1006 new_state = cur_state;
1007 } else {
1008 leaf->joining_bus = false;
1009 }
1010 }
1011
1012 if (new_state != cur_state) {
1013 tx_state = (es->txerr >= es->rxerr) ? new_state : 0;
1014 rx_state = (es->txerr <= es->rxerr) ? new_state : 0;
1015
1016 can_change_state(priv->netdev, cf, tx_state, rx_state);
1017 }
1018
1019 if (priv->can.restart_ms &&
1020 cur_state == CAN_STATE_BUS_OFF &&
1021 new_state < CAN_STATE_BUS_OFF)
1022 priv->can.can_stats.restarts++;
1023
1024 switch (dev->driver_info->family) {
1025 case KVASER_LEAF:
1026 if (es->leaf.error_factor) {
1027 priv->can.can_stats.bus_error++;
1028 stats->rx_errors++;
1029 }
1030 break;
1031 case KVASER_USBCAN:
1032 if (es->usbcan.error_state & USBCAN_ERROR_STATE_TX_ERROR)
1033 stats->tx_errors++;
1034 if (es->usbcan.error_state & USBCAN_ERROR_STATE_RX_ERROR)
1035 stats->rx_errors++;
1036 if (es->usbcan.error_state & USBCAN_ERROR_STATE_BUSERROR)
1037 priv->can.can_stats.bus_error++;
1038 break;
1039 }
1040
1041 priv->bec.txerr = es->txerr;
1042 priv->bec.rxerr = es->rxerr;
1043 }
1044
kvaser_usb_leaf_rx_error(const struct kvaser_usb * dev,const struct kvaser_usb_err_summary * es)1045 static void kvaser_usb_leaf_rx_error(const struct kvaser_usb *dev,
1046 const struct kvaser_usb_err_summary *es)
1047 {
1048 struct can_frame *cf;
1049 struct can_frame tmp_cf = { .can_id = CAN_ERR_FLAG,
1050 .len = CAN_ERR_DLC };
1051 struct sk_buff *skb;
1052 struct net_device_stats *stats;
1053 struct kvaser_usb_net_priv *priv;
1054 struct kvaser_usb_net_leaf_priv *leaf;
1055 enum can_state old_state, new_state;
1056
1057 if (es->channel >= dev->nchannels) {
1058 dev_err(&dev->intf->dev,
1059 "Invalid channel number (%d)\n", es->channel);
1060 return;
1061 }
1062
1063 priv = dev->nets[es->channel];
1064 leaf = priv->sub_priv;
1065 stats = &priv->netdev->stats;
1066
1067 /* Ignore e.g. state change to bus-off reported just after stopping */
1068 if (!netif_running(priv->netdev))
1069 return;
1070
1071 /* Update all of the CAN interface's state and error counters before
1072 * trying any memory allocation that can actually fail with -ENOMEM.
1073 *
1074 * We send a temporary stack-allocated error CAN frame to
1075 * can_change_state() for the very same reason.
1076 *
1077 * TODO: Split can_change_state() responsibility between updating the
1078 * CAN interface's state and counters, and the setting up of CAN error
1079 * frame ID and data to userspace. Remove stack allocation afterwards.
1080 */
1081 old_state = priv->can.state;
1082 kvaser_usb_leaf_rx_error_update_can_state(priv, es, &tmp_cf);
1083 new_state = priv->can.state;
1084
1085 /* If there are errors, request status updates periodically as we do
1086 * not get automatic notifications of improved state.
1087 * Also request updates if we saw a stale BUS_OFF during startup
1088 * (joining_bus).
1089 */
1090 if (new_state < CAN_STATE_BUS_OFF &&
1091 (es->rxerr || es->txerr || new_state == CAN_STATE_ERROR_PASSIVE ||
1092 leaf->joining_bus))
1093 schedule_delayed_work(&leaf->chip_state_req_work,
1094 msecs_to_jiffies(500));
1095
1096 skb = alloc_can_err_skb(priv->netdev, &cf);
1097 if (!skb) {
1098 stats->rx_dropped++;
1099 return;
1100 }
1101 memcpy(cf, &tmp_cf, sizeof(*cf));
1102
1103 if (new_state != old_state) {
1104 if (es->status &
1105 (M16C_STATE_BUS_OFF | M16C_STATE_BUS_RESET)) {
1106 if (!priv->can.restart_ms)
1107 kvaser_usb_leaf_simple_cmd_async(priv,
1108 CMD_STOP_CHIP);
1109 netif_carrier_off(priv->netdev);
1110 }
1111
1112 if (priv->can.restart_ms &&
1113 old_state == CAN_STATE_BUS_OFF &&
1114 new_state < CAN_STATE_BUS_OFF) {
1115 cf->can_id |= CAN_ERR_RESTARTED;
1116 netif_carrier_on(priv->netdev);
1117 }
1118 }
1119
1120 switch (dev->driver_info->family) {
1121 case KVASER_LEAF:
1122 if (es->leaf.error_factor) {
1123 cf->can_id |= CAN_ERR_BUSERROR | CAN_ERR_PROT;
1124
1125 if (es->leaf.error_factor & M16C_EF_ACKE)
1126 cf->data[3] = CAN_ERR_PROT_LOC_ACK;
1127 if (es->leaf.error_factor & M16C_EF_CRCE)
1128 cf->data[3] = CAN_ERR_PROT_LOC_CRC_SEQ;
1129 if (es->leaf.error_factor & M16C_EF_FORME)
1130 cf->data[2] |= CAN_ERR_PROT_FORM;
1131 if (es->leaf.error_factor & M16C_EF_STFE)
1132 cf->data[2] |= CAN_ERR_PROT_STUFF;
1133 if (es->leaf.error_factor & M16C_EF_BITE0)
1134 cf->data[2] |= CAN_ERR_PROT_BIT0;
1135 if (es->leaf.error_factor & M16C_EF_BITE1)
1136 cf->data[2] |= CAN_ERR_PROT_BIT1;
1137 if (es->leaf.error_factor & M16C_EF_TRE)
1138 cf->data[2] |= CAN_ERR_PROT_TX;
1139 }
1140 break;
1141 case KVASER_USBCAN:
1142 if (es->usbcan.error_state & USBCAN_ERROR_STATE_BUSERROR)
1143 cf->can_id |= CAN_ERR_BUSERROR;
1144 break;
1145 }
1146
1147 if (new_state != CAN_STATE_BUS_OFF) {
1148 cf->can_id |= CAN_ERR_CNT;
1149 cf->data[6] = es->txerr;
1150 cf->data[7] = es->rxerr;
1151 }
1152
1153 netif_rx(skb);
1154 }
1155
1156 /* For USBCAN, report error to userspace if the channels's errors counter
1157 * has changed, or we're the only channel seeing a bus error state.
1158 */
1159 static void
kvaser_usb_leaf_usbcan_conditionally_rx_error(const struct kvaser_usb * dev,struct kvaser_usb_err_summary * es)1160 kvaser_usb_leaf_usbcan_conditionally_rx_error(const struct kvaser_usb *dev,
1161 struct kvaser_usb_err_summary *es)
1162 {
1163 struct kvaser_usb_net_priv *priv;
1164 unsigned int channel;
1165 bool report_error;
1166
1167 channel = es->channel;
1168 if (channel >= dev->nchannels) {
1169 dev_err(&dev->intf->dev,
1170 "Invalid channel number (%d)\n", channel);
1171 return;
1172 }
1173
1174 priv = dev->nets[channel];
1175 report_error = false;
1176
1177 if (es->txerr != priv->bec.txerr) {
1178 es->usbcan.error_state |= USBCAN_ERROR_STATE_TX_ERROR;
1179 report_error = true;
1180 }
1181 if (es->rxerr != priv->bec.rxerr) {
1182 es->usbcan.error_state |= USBCAN_ERROR_STATE_RX_ERROR;
1183 report_error = true;
1184 }
1185 if ((es->status & M16C_STATE_BUS_ERROR) &&
1186 !(es->usbcan.other_ch_status & M16C_STATE_BUS_ERROR)) {
1187 es->usbcan.error_state |= USBCAN_ERROR_STATE_BUSERROR;
1188 report_error = true;
1189 }
1190
1191 if (report_error)
1192 kvaser_usb_leaf_rx_error(dev, es);
1193 }
1194
kvaser_usb_leaf_usbcan_rx_error(const struct kvaser_usb * dev,const struct kvaser_cmd * cmd)1195 static void kvaser_usb_leaf_usbcan_rx_error(const struct kvaser_usb *dev,
1196 const struct kvaser_cmd *cmd)
1197 {
1198 struct kvaser_usb_err_summary es = { };
1199
1200 switch (cmd->id) {
1201 /* Sometimes errors are sent as unsolicited chip state events */
1202 case CMD_CHIP_STATE_EVENT:
1203 es.channel = cmd->u.usbcan.chip_state_event.channel;
1204 es.status = cmd->u.usbcan.chip_state_event.status;
1205 es.txerr = cmd->u.usbcan.chip_state_event.tx_errors_count;
1206 es.rxerr = cmd->u.usbcan.chip_state_event.rx_errors_count;
1207 kvaser_usb_leaf_usbcan_conditionally_rx_error(dev, &es);
1208 break;
1209
1210 case CMD_CAN_ERROR_EVENT:
1211 es.channel = 0;
1212 es.status = cmd->u.usbcan.can_error_event.status_ch0;
1213 es.txerr = cmd->u.usbcan.can_error_event.tx_errors_count_ch0;
1214 es.rxerr = cmd->u.usbcan.can_error_event.rx_errors_count_ch0;
1215 es.usbcan.other_ch_status =
1216 cmd->u.usbcan.can_error_event.status_ch1;
1217 kvaser_usb_leaf_usbcan_conditionally_rx_error(dev, &es);
1218
1219 /* The USBCAN firmware supports up to 2 channels.
1220 * Now that ch0 was checked, check if ch1 has any errors.
1221 */
1222 if (dev->nchannels == MAX_USBCAN_NET_DEVICES) {
1223 es.channel = 1;
1224 es.status = cmd->u.usbcan.can_error_event.status_ch1;
1225 es.txerr =
1226 cmd->u.usbcan.can_error_event.tx_errors_count_ch1;
1227 es.rxerr =
1228 cmd->u.usbcan.can_error_event.rx_errors_count_ch1;
1229 es.usbcan.other_ch_status =
1230 cmd->u.usbcan.can_error_event.status_ch0;
1231 kvaser_usb_leaf_usbcan_conditionally_rx_error(dev, &es);
1232 }
1233 break;
1234
1235 default:
1236 dev_err(&dev->intf->dev, "Invalid cmd id (%d)\n", cmd->id);
1237 }
1238 }
1239
kvaser_usb_leaf_leaf_rx_error(const struct kvaser_usb * dev,const struct kvaser_cmd * cmd)1240 static void kvaser_usb_leaf_leaf_rx_error(const struct kvaser_usb *dev,
1241 const struct kvaser_cmd *cmd)
1242 {
1243 struct kvaser_usb_err_summary es = { };
1244
1245 switch (cmd->id) {
1246 case CMD_CAN_ERROR_EVENT:
1247 es.channel = cmd->u.leaf.can_error_event.channel;
1248 es.status = cmd->u.leaf.can_error_event.status;
1249 es.txerr = cmd->u.leaf.can_error_event.tx_errors_count;
1250 es.rxerr = cmd->u.leaf.can_error_event.rx_errors_count;
1251 es.leaf.error_factor = cmd->u.leaf.can_error_event.error_factor;
1252 break;
1253 case CMD_LEAF_LOG_MESSAGE:
1254 es.channel = cmd->u.leaf.log_message.channel;
1255 es.status = cmd->u.leaf.log_message.data[0];
1256 es.txerr = cmd->u.leaf.log_message.data[2];
1257 es.rxerr = cmd->u.leaf.log_message.data[3];
1258 es.leaf.error_factor = cmd->u.leaf.log_message.data[1];
1259 break;
1260 case CMD_CHIP_STATE_EVENT:
1261 es.channel = cmd->u.leaf.chip_state_event.channel;
1262 es.status = cmd->u.leaf.chip_state_event.status;
1263 es.txerr = cmd->u.leaf.chip_state_event.tx_errors_count;
1264 es.rxerr = cmd->u.leaf.chip_state_event.rx_errors_count;
1265 es.leaf.error_factor = 0;
1266 break;
1267 default:
1268 dev_err(&dev->intf->dev, "Invalid cmd id (%d)\n", cmd->id);
1269 return;
1270 }
1271
1272 kvaser_usb_leaf_rx_error(dev, &es);
1273 }
1274
kvaser_usb_leaf_rx_can_err(const struct kvaser_usb_net_priv * priv,const struct kvaser_cmd * cmd)1275 static void kvaser_usb_leaf_rx_can_err(const struct kvaser_usb_net_priv *priv,
1276 const struct kvaser_cmd *cmd)
1277 {
1278 if (cmd->u.rx_can_header.flag & (MSG_FLAG_ERROR_FRAME |
1279 MSG_FLAG_NERR)) {
1280 struct net_device_stats *stats = &priv->netdev->stats;
1281
1282 netdev_err(priv->netdev, "Unknown error (flags: 0x%02x)\n",
1283 cmd->u.rx_can_header.flag);
1284
1285 stats->rx_errors++;
1286 return;
1287 }
1288
1289 if (cmd->u.rx_can_header.flag & MSG_FLAG_OVERRUN)
1290 kvaser_usb_can_rx_over_error(priv->netdev);
1291 }
1292
kvaser_usb_leaf_rx_can_msg(const struct kvaser_usb * dev,const struct kvaser_cmd * cmd)1293 static void kvaser_usb_leaf_rx_can_msg(const struct kvaser_usb *dev,
1294 const struct kvaser_cmd *cmd)
1295 {
1296 struct kvaser_usb_net_priv *priv;
1297 struct can_frame *cf;
1298 struct sk_buff *skb;
1299 struct net_device_stats *stats;
1300 u8 channel = cmd->u.rx_can_header.channel;
1301 const u8 *rx_data = NULL; /* GCC */
1302
1303 if (channel >= dev->nchannels) {
1304 dev_err(&dev->intf->dev,
1305 "Invalid channel number (%d)\n", channel);
1306 return;
1307 }
1308
1309 priv = dev->nets[channel];
1310 stats = &priv->netdev->stats;
1311
1312 if ((cmd->u.rx_can_header.flag & MSG_FLAG_ERROR_FRAME) &&
1313 (dev->driver_info->family == KVASER_LEAF &&
1314 cmd->id == CMD_LEAF_LOG_MESSAGE)) {
1315 kvaser_usb_leaf_leaf_rx_error(dev, cmd);
1316 return;
1317 } else if (cmd->u.rx_can_header.flag & (MSG_FLAG_ERROR_FRAME |
1318 MSG_FLAG_NERR |
1319 MSG_FLAG_OVERRUN)) {
1320 kvaser_usb_leaf_rx_can_err(priv, cmd);
1321 return;
1322 } else if (cmd->u.rx_can_header.flag & ~MSG_FLAG_REMOTE_FRAME) {
1323 netdev_warn(priv->netdev,
1324 "Unhandled frame (flags: 0x%02x)\n",
1325 cmd->u.rx_can_header.flag);
1326 return;
1327 }
1328
1329 switch (dev->driver_info->family) {
1330 case KVASER_LEAF:
1331 rx_data = cmd->u.leaf.rx_can.data;
1332 break;
1333 case KVASER_USBCAN:
1334 rx_data = cmd->u.usbcan.rx_can.data;
1335 break;
1336 }
1337
1338 skb = alloc_can_skb(priv->netdev, &cf);
1339 if (!skb) {
1340 stats->rx_dropped++;
1341 return;
1342 }
1343
1344 if (dev->driver_info->family == KVASER_LEAF && cmd->id ==
1345 CMD_LEAF_LOG_MESSAGE) {
1346 cf->can_id = le32_to_cpu(cmd->u.leaf.log_message.id);
1347 if (cf->can_id & KVASER_EXTENDED_FRAME)
1348 cf->can_id &= CAN_EFF_MASK | CAN_EFF_FLAG;
1349 else
1350 cf->can_id &= CAN_SFF_MASK;
1351
1352 can_frame_set_cc_len(cf, cmd->u.leaf.log_message.dlc & 0xF, priv->can.ctrlmode);
1353
1354 if (cmd->u.leaf.log_message.flags & MSG_FLAG_REMOTE_FRAME)
1355 cf->can_id |= CAN_RTR_FLAG;
1356 else
1357 memcpy(cf->data, &cmd->u.leaf.log_message.data,
1358 cf->len);
1359 } else {
1360 cf->can_id = ((rx_data[0] & 0x1f) << 6) | (rx_data[1] & 0x3f);
1361
1362 if (cmd->id == CMD_RX_EXT_MESSAGE) {
1363 cf->can_id <<= 18;
1364 cf->can_id |= ((rx_data[2] & 0x0f) << 14) |
1365 ((rx_data[3] & 0xff) << 6) |
1366 (rx_data[4] & 0x3f);
1367 cf->can_id |= CAN_EFF_FLAG;
1368 }
1369
1370 can_frame_set_cc_len(cf, rx_data[5] & 0xF, priv->can.ctrlmode);
1371
1372 if (cmd->u.rx_can_header.flag & MSG_FLAG_REMOTE_FRAME)
1373 cf->can_id |= CAN_RTR_FLAG;
1374 else
1375 memcpy(cf->data, &rx_data[6], cf->len);
1376 }
1377
1378 stats->rx_packets++;
1379 if (!(cf->can_id & CAN_RTR_FLAG))
1380 stats->rx_bytes += cf->len;
1381 netif_rx(skb);
1382 }
1383
kvaser_usb_leaf_error_event_parameter(const struct kvaser_usb * dev,const struct kvaser_cmd * cmd)1384 static void kvaser_usb_leaf_error_event_parameter(const struct kvaser_usb *dev,
1385 const struct kvaser_cmd *cmd)
1386 {
1387 u16 info1 = 0;
1388
1389 switch (dev->driver_info->family) {
1390 case KVASER_LEAF:
1391 info1 = le16_to_cpu(cmd->u.leaf.error_event.info1);
1392 break;
1393 case KVASER_USBCAN:
1394 info1 = le16_to_cpu(cmd->u.usbcan.error_event.info1);
1395 break;
1396 }
1397
1398 /* info1 will contain the offending cmd_no */
1399 switch (info1) {
1400 case CMD_SET_CTRL_MODE:
1401 dev_warn(&dev->intf->dev,
1402 "CMD_SET_CTRL_MODE error in parameter\n");
1403 break;
1404
1405 case CMD_SET_BUS_PARAMS:
1406 dev_warn(&dev->intf->dev,
1407 "CMD_SET_BUS_PARAMS error in parameter\n");
1408 break;
1409
1410 default:
1411 dev_warn(&dev->intf->dev,
1412 "Unhandled parameter error event cmd_no (%u)\n",
1413 info1);
1414 break;
1415 }
1416 }
1417
kvaser_usb_leaf_error_event(const struct kvaser_usb * dev,const struct kvaser_cmd * cmd)1418 static void kvaser_usb_leaf_error_event(const struct kvaser_usb *dev,
1419 const struct kvaser_cmd *cmd)
1420 {
1421 u8 error_code = 0;
1422
1423 switch (dev->driver_info->family) {
1424 case KVASER_LEAF:
1425 error_code = cmd->u.leaf.error_event.error_code;
1426 break;
1427 case KVASER_USBCAN:
1428 error_code = cmd->u.usbcan.error_event.error_code;
1429 break;
1430 }
1431
1432 switch (error_code) {
1433 case KVASER_USB_LEAF_ERROR_EVENT_TX_QUEUE_FULL:
1434 /* Received additional CAN message, when firmware TX queue is
1435 * already full. Something is wrong with the driver.
1436 * This should never happen!
1437 */
1438 dev_err(&dev->intf->dev,
1439 "Received error event TX_QUEUE_FULL\n");
1440 break;
1441 case KVASER_USB_LEAF_ERROR_EVENT_PARAM:
1442 kvaser_usb_leaf_error_event_parameter(dev, cmd);
1443 break;
1444
1445 default:
1446 dev_warn(&dev->intf->dev,
1447 "Unhandled error event (%d)\n", error_code);
1448 break;
1449 }
1450 }
1451
kvaser_usb_leaf_start_chip_reply(const struct kvaser_usb * dev,const struct kvaser_cmd * cmd)1452 static void kvaser_usb_leaf_start_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 if (completion_done(&priv->start_comp) &&
1467 netif_queue_stopped(priv->netdev)) {
1468 netif_wake_queue(priv->netdev);
1469 } else {
1470 netif_start_queue(priv->netdev);
1471 complete(&priv->start_comp);
1472 }
1473 }
1474
kvaser_usb_leaf_stop_chip_reply(const struct kvaser_usb * dev,const struct kvaser_cmd * cmd)1475 static void kvaser_usb_leaf_stop_chip_reply(const struct kvaser_usb *dev,
1476 const struct kvaser_cmd *cmd)
1477 {
1478 struct kvaser_usb_net_priv *priv;
1479 u8 channel = cmd->u.simple.channel;
1480
1481 if (channel >= dev->nchannels) {
1482 dev_err(&dev->intf->dev,
1483 "Invalid channel number (%d)\n", channel);
1484 return;
1485 }
1486
1487 priv = dev->nets[channel];
1488
1489 complete(&priv->stop_comp);
1490 }
1491
kvaser_usb_leaf_get_busparams_reply(const struct kvaser_usb * dev,const struct kvaser_cmd * cmd)1492 static void kvaser_usb_leaf_get_busparams_reply(const struct kvaser_usb *dev,
1493 const struct kvaser_cmd *cmd)
1494 {
1495 struct kvaser_usb_net_priv *priv;
1496 u8 channel = cmd->u.busparams.channel;
1497
1498 if (channel >= dev->nchannels) {
1499 dev_err(&dev->intf->dev,
1500 "Invalid channel number (%d)\n", channel);
1501 return;
1502 }
1503
1504 priv = dev->nets[channel];
1505 memcpy(&priv->busparams_nominal, &cmd->u.busparams.busparams,
1506 sizeof(priv->busparams_nominal));
1507
1508 complete(&priv->get_busparams_comp);
1509 }
1510
kvaser_usb_leaf_handle_command(const struct kvaser_usb * dev,const struct kvaser_cmd * cmd)1511 static void kvaser_usb_leaf_handle_command(const struct kvaser_usb *dev,
1512 const struct kvaser_cmd *cmd)
1513 {
1514 if (kvaser_usb_leaf_verify_size(dev, cmd) < 0)
1515 return;
1516
1517 switch (cmd->id) {
1518 case CMD_START_CHIP_REPLY:
1519 kvaser_usb_leaf_start_chip_reply(dev, cmd);
1520 break;
1521
1522 case CMD_STOP_CHIP_REPLY:
1523 kvaser_usb_leaf_stop_chip_reply(dev, cmd);
1524 break;
1525
1526 case CMD_RX_STD_MESSAGE:
1527 case CMD_RX_EXT_MESSAGE:
1528 kvaser_usb_leaf_rx_can_msg(dev, cmd);
1529 break;
1530
1531 case CMD_LEAF_LOG_MESSAGE:
1532 if (dev->driver_info->family != KVASER_LEAF)
1533 goto warn;
1534 kvaser_usb_leaf_rx_can_msg(dev, cmd);
1535 break;
1536
1537 case CMD_CHIP_STATE_EVENT:
1538 case CMD_CAN_ERROR_EVENT:
1539 if (dev->driver_info->family == KVASER_LEAF)
1540 kvaser_usb_leaf_leaf_rx_error(dev, cmd);
1541 else
1542 kvaser_usb_leaf_usbcan_rx_error(dev, cmd);
1543 break;
1544
1545 case CMD_TX_ACKNOWLEDGE:
1546 kvaser_usb_leaf_tx_acknowledge(dev, cmd);
1547 break;
1548
1549 case CMD_ERROR_EVENT:
1550 kvaser_usb_leaf_error_event(dev, cmd);
1551 break;
1552
1553 case CMD_GET_BUS_PARAMS_REPLY:
1554 kvaser_usb_leaf_get_busparams_reply(dev, cmd);
1555 break;
1556
1557 /* Ignored commands */
1558 case CMD_USBCAN_CLOCK_OVERFLOW_EVENT:
1559 if (dev->driver_info->family != KVASER_USBCAN)
1560 goto warn;
1561 break;
1562
1563 case CMD_FLUSH_QUEUE_REPLY:
1564 if (dev->driver_info->family != KVASER_LEAF)
1565 goto warn;
1566 break;
1567
1568 default:
1569 warn: dev_warn(&dev->intf->dev, "Unhandled command (%d)\n", cmd->id);
1570 break;
1571 }
1572 }
1573
kvaser_usb_leaf_read_bulk_callback(struct kvaser_usb * dev,void * buf,int len)1574 static void kvaser_usb_leaf_read_bulk_callback(struct kvaser_usb *dev,
1575 void *buf, int len)
1576 {
1577 struct kvaser_cmd *cmd;
1578 int pos = 0;
1579
1580 while (pos <= len - CMD_HEADER_LEN) {
1581 cmd = buf + pos;
1582
1583 /* The Kvaser firmware can only read and write commands that
1584 * does not cross the USB's endpoint wMaxPacketSize boundary.
1585 * If a follow-up command crosses such boundary, firmware puts
1586 * a placeholder zero-length command in its place then aligns
1587 * the real command to the next max packet size.
1588 *
1589 * Handle such cases or we're going to miss a significant
1590 * number of events in case of a heavy rx load on the bus.
1591 */
1592 if (cmd->len == 0) {
1593 pos = round_up(pos, le16_to_cpu
1594 (dev->bulk_in->wMaxPacketSize));
1595 continue;
1596 }
1597
1598 if (pos + cmd->len > len) {
1599 dev_err_ratelimited(&dev->intf->dev, "Format error\n");
1600 break;
1601 }
1602
1603 kvaser_usb_leaf_handle_command(dev, cmd);
1604 pos += cmd->len;
1605 }
1606 }
1607
kvaser_usb_leaf_set_opt_mode(const struct kvaser_usb_net_priv * priv)1608 static int kvaser_usb_leaf_set_opt_mode(const struct kvaser_usb_net_priv *priv)
1609 {
1610 struct kvaser_cmd *cmd;
1611 int rc;
1612
1613 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1614 if (!cmd)
1615 return -ENOMEM;
1616
1617 cmd->id = CMD_SET_CTRL_MODE;
1618 cmd->len = CMD_HEADER_LEN + sizeof(struct kvaser_cmd_ctrl_mode);
1619 cmd->u.ctrl_mode.tid = 0xff;
1620 cmd->u.ctrl_mode.channel = priv->channel;
1621
1622 if (priv->can.ctrlmode & CAN_CTRLMODE_LISTENONLY)
1623 cmd->u.ctrl_mode.ctrl_mode = KVASER_CTRL_MODE_SILENT;
1624 else
1625 cmd->u.ctrl_mode.ctrl_mode = KVASER_CTRL_MODE_NORMAL;
1626
1627 rc = kvaser_usb_send_cmd(priv->dev, cmd, cmd->len);
1628
1629 kfree(cmd);
1630 return rc;
1631 }
1632
kvaser_usb_leaf_start_chip(struct kvaser_usb_net_priv * priv)1633 static int kvaser_usb_leaf_start_chip(struct kvaser_usb_net_priv *priv)
1634 {
1635 struct kvaser_usb_net_leaf_priv *leaf = priv->sub_priv;
1636 int err;
1637
1638 leaf->joining_bus = true;
1639
1640 reinit_completion(&priv->start_comp);
1641
1642 err = kvaser_usb_leaf_send_simple_cmd(priv->dev, CMD_START_CHIP,
1643 priv->channel);
1644 if (err)
1645 return err;
1646
1647 if (!wait_for_completion_timeout(&priv->start_comp,
1648 msecs_to_jiffies(KVASER_USB_TIMEOUT)))
1649 return -ETIMEDOUT;
1650
1651 return 0;
1652 }
1653
kvaser_usb_leaf_stop_chip(struct kvaser_usb_net_priv * priv)1654 static int kvaser_usb_leaf_stop_chip(struct kvaser_usb_net_priv *priv)
1655 {
1656 struct kvaser_usb_net_leaf_priv *leaf = priv->sub_priv;
1657 int err;
1658
1659 reinit_completion(&priv->stop_comp);
1660
1661 cancel_delayed_work(&leaf->chip_state_req_work);
1662
1663 err = kvaser_usb_leaf_send_simple_cmd(priv->dev, CMD_STOP_CHIP,
1664 priv->channel);
1665 if (err)
1666 return err;
1667
1668 if (!wait_for_completion_timeout(&priv->stop_comp,
1669 msecs_to_jiffies(KVASER_USB_TIMEOUT)))
1670 return -ETIMEDOUT;
1671
1672 return 0;
1673 }
1674
kvaser_usb_leaf_reset_chip(struct kvaser_usb * dev,int channel)1675 static int kvaser_usb_leaf_reset_chip(struct kvaser_usb *dev, int channel)
1676 {
1677 return kvaser_usb_leaf_send_simple_cmd(dev, CMD_RESET_CHIP, channel);
1678 }
1679
kvaser_usb_leaf_flush_queue(struct kvaser_usb_net_priv * priv)1680 static int kvaser_usb_leaf_flush_queue(struct kvaser_usb_net_priv *priv)
1681 {
1682 struct kvaser_cmd *cmd;
1683 int rc;
1684
1685 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1686 if (!cmd)
1687 return -ENOMEM;
1688
1689 cmd->id = CMD_FLUSH_QUEUE;
1690 cmd->len = CMD_HEADER_LEN + sizeof(struct kvaser_cmd_flush_queue);
1691 cmd->u.flush_queue.channel = priv->channel;
1692 cmd->u.flush_queue.flags = 0x00;
1693
1694 rc = kvaser_usb_send_cmd(priv->dev, cmd, cmd->len);
1695
1696 kfree(cmd);
1697 return rc;
1698 }
1699
kvaser_usb_leaf_init_card(struct kvaser_usb * dev)1700 static int kvaser_usb_leaf_init_card(struct kvaser_usb *dev)
1701 {
1702 struct kvaser_usb_dev_card_data *card_data = &dev->card_data;
1703
1704 card_data->ctrlmode_supported |= CAN_CTRLMODE_3_SAMPLES;
1705
1706 return 0;
1707 }
1708
kvaser_usb_leaf_init_channel(struct kvaser_usb_net_priv * priv)1709 static int kvaser_usb_leaf_init_channel(struct kvaser_usb_net_priv *priv)
1710 {
1711 struct kvaser_usb_net_leaf_priv *leaf;
1712
1713 leaf = devm_kzalloc(&priv->dev->intf->dev, sizeof(*leaf), GFP_KERNEL);
1714 if (!leaf)
1715 return -ENOMEM;
1716
1717 leaf->net = priv;
1718 INIT_DELAYED_WORK(&leaf->chip_state_req_work,
1719 kvaser_usb_leaf_chip_state_req_work);
1720
1721 priv->sub_priv = leaf;
1722
1723 return 0;
1724 }
1725
kvaser_usb_leaf_remove_channel(struct kvaser_usb_net_priv * priv)1726 static void kvaser_usb_leaf_remove_channel(struct kvaser_usb_net_priv *priv)
1727 {
1728 struct kvaser_usb_net_leaf_priv *leaf = priv->sub_priv;
1729
1730 if (leaf)
1731 cancel_delayed_work_sync(&leaf->chip_state_req_work);
1732 }
1733
kvaser_usb_leaf_set_bittiming(const struct net_device * netdev,const struct kvaser_usb_busparams * busparams)1734 static int kvaser_usb_leaf_set_bittiming(const struct net_device *netdev,
1735 const struct kvaser_usb_busparams *busparams)
1736 {
1737 struct kvaser_usb_net_priv *priv = netdev_priv(netdev);
1738 struct kvaser_usb *dev = priv->dev;
1739 struct kvaser_cmd *cmd;
1740 int rc;
1741
1742 cmd = kmalloc(sizeof(*cmd), GFP_KERNEL);
1743 if (!cmd)
1744 return -ENOMEM;
1745
1746 cmd->id = CMD_SET_BUS_PARAMS;
1747 cmd->len = CMD_HEADER_LEN + sizeof(struct kvaser_cmd_busparams);
1748 cmd->u.busparams.channel = priv->channel;
1749 cmd->u.busparams.tid = 0xff;
1750 memcpy(&cmd->u.busparams.busparams, busparams,
1751 sizeof(cmd->u.busparams.busparams));
1752
1753 rc = kvaser_usb_send_cmd(dev, cmd, cmd->len);
1754
1755 kfree(cmd);
1756 return rc;
1757 }
1758
kvaser_usb_leaf_get_busparams(struct kvaser_usb_net_priv * priv)1759 static int kvaser_usb_leaf_get_busparams(struct kvaser_usb_net_priv *priv)
1760 {
1761 int err;
1762
1763 if (priv->dev->driver_info->family == KVASER_USBCAN)
1764 return -EOPNOTSUPP;
1765
1766 reinit_completion(&priv->get_busparams_comp);
1767
1768 err = kvaser_usb_leaf_send_simple_cmd(priv->dev, CMD_GET_BUS_PARAMS,
1769 priv->channel);
1770 if (err)
1771 return err;
1772
1773 if (!wait_for_completion_timeout(&priv->get_busparams_comp,
1774 msecs_to_jiffies(KVASER_USB_TIMEOUT)))
1775 return -ETIMEDOUT;
1776
1777 return 0;
1778 }
1779
kvaser_usb_leaf_set_mode(struct net_device * netdev,enum can_mode mode)1780 static int kvaser_usb_leaf_set_mode(struct net_device *netdev,
1781 enum can_mode mode)
1782 {
1783 struct kvaser_usb_net_priv *priv = netdev_priv(netdev);
1784 struct kvaser_usb_net_leaf_priv *leaf = priv->sub_priv;
1785 int err;
1786
1787 switch (mode) {
1788 case CAN_MODE_START:
1789 kvaser_usb_unlink_tx_urbs(priv);
1790
1791 leaf->joining_bus = true;
1792
1793 err = kvaser_usb_leaf_simple_cmd_async(priv, CMD_START_CHIP);
1794 if (err)
1795 return err;
1796
1797 priv->can.state = CAN_STATE_ERROR_ACTIVE;
1798 break;
1799 default:
1800 return -EOPNOTSUPP;
1801 }
1802
1803 return 0;
1804 }
1805
kvaser_usb_leaf_get_berr_counter(const struct net_device * netdev,struct can_berr_counter * bec)1806 static int kvaser_usb_leaf_get_berr_counter(const struct net_device *netdev,
1807 struct can_berr_counter *bec)
1808 {
1809 struct kvaser_usb_net_priv *priv = netdev_priv(netdev);
1810
1811 *bec = priv->bec;
1812
1813 return 0;
1814 }
1815
kvaser_usb_leaf_setup_endpoints(struct kvaser_usb * dev)1816 static int kvaser_usb_leaf_setup_endpoints(struct kvaser_usb *dev)
1817 {
1818 const struct usb_host_interface *iface_desc;
1819 struct usb_endpoint_descriptor *endpoint;
1820 int i;
1821
1822 iface_desc = dev->intf->cur_altsetting;
1823
1824 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
1825 endpoint = &iface_desc->endpoint[i].desc;
1826
1827 if (!dev->bulk_in && usb_endpoint_is_bulk_in(endpoint))
1828 dev->bulk_in = endpoint;
1829
1830 if (!dev->bulk_out && usb_endpoint_is_bulk_out(endpoint))
1831 dev->bulk_out = endpoint;
1832
1833 /* use first bulk endpoint for in and out */
1834 if (dev->bulk_in && dev->bulk_out)
1835 return 0;
1836 }
1837
1838 return -ENODEV;
1839 }
1840
1841 const struct kvaser_usb_dev_ops kvaser_usb_leaf_dev_ops = {
1842 .dev_set_mode = kvaser_usb_leaf_set_mode,
1843 .dev_set_bittiming = kvaser_usb_leaf_set_bittiming,
1844 .dev_get_busparams = kvaser_usb_leaf_get_busparams,
1845 .dev_set_data_bittiming = NULL,
1846 .dev_get_data_busparams = NULL,
1847 .dev_get_berr_counter = kvaser_usb_leaf_get_berr_counter,
1848 .dev_setup_endpoints = kvaser_usb_leaf_setup_endpoints,
1849 .dev_init_card = kvaser_usb_leaf_init_card,
1850 .dev_init_channel = kvaser_usb_leaf_init_channel,
1851 .dev_remove_channel = kvaser_usb_leaf_remove_channel,
1852 .dev_get_software_info = kvaser_usb_leaf_get_software_info,
1853 .dev_get_software_details = NULL,
1854 .dev_get_card_info = kvaser_usb_leaf_get_card_info,
1855 .dev_get_capabilities = kvaser_usb_leaf_get_capabilities,
1856 .dev_set_opt_mode = kvaser_usb_leaf_set_opt_mode,
1857 .dev_start_chip = kvaser_usb_leaf_start_chip,
1858 .dev_stop_chip = kvaser_usb_leaf_stop_chip,
1859 .dev_reset_chip = kvaser_usb_leaf_reset_chip,
1860 .dev_flush_queue = kvaser_usb_leaf_flush_queue,
1861 .dev_read_bulk_callback = kvaser_usb_leaf_read_bulk_callback,
1862 .dev_frame_to_cmd = kvaser_usb_leaf_frame_to_cmd,
1863 };
1864