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