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