hc
2024-12-19 9370bb92b2d16684ee45cf24e879c93c509162da
kernel/drivers/net/can/usb/kvaser_usb/kvaser_usb_leaf.c
....@@ -20,6 +20,7 @@
2020 #include <linux/string.h>
2121 #include <linux/types.h>
2222 #include <linux/usb.h>
23
+#include <linux/workqueue.h>
2324
2425 #include <linux/can.h>
2526 #include <linux/can/dev.h>
....@@ -55,6 +56,9 @@
5556 #define CMD_RX_EXT_MESSAGE 14
5657 #define CMD_TX_EXT_MESSAGE 15
5758 #define CMD_SET_BUS_PARAMS 16
59
+#define CMD_GET_BUS_PARAMS 17
60
+#define CMD_GET_BUS_PARAMS_REPLY 18
61
+#define CMD_GET_CHIP_STATE 19
5862 #define CMD_CHIP_STATE_EVENT 20
5963 #define CMD_SET_CTRL_MODE 21
6064 #define CMD_RESET_CHIP 24
....@@ -69,10 +73,13 @@
6973 #define CMD_GET_CARD_INFO_REPLY 35
7074 #define CMD_GET_SOFTWARE_INFO 38
7175 #define CMD_GET_SOFTWARE_INFO_REPLY 39
76
+#define CMD_ERROR_EVENT 45
7277 #define CMD_FLUSH_QUEUE 48
7378 #define CMD_TX_ACKNOWLEDGE 50
7479 #define CMD_CAN_ERROR_EVENT 51
7580 #define CMD_FLUSH_QUEUE_REPLY 68
81
+#define CMD_GET_CAPABILITIES_REQ 95
82
+#define CMD_GET_CAPABILITIES_RESP 96
7683
7784 #define CMD_LEAF_LOG_MESSAGE 106
7885
....@@ -81,6 +88,8 @@
8188 #define KVASER_USB_LEAF_SWOPTION_FREQ_16_MHZ_CLK 0
8289 #define KVASER_USB_LEAF_SWOPTION_FREQ_32_MHZ_CLK BIT(5)
8390 #define KVASER_USB_LEAF_SWOPTION_FREQ_24_MHZ_CLK BIT(6)
91
+
92
+#define KVASER_USB_LEAF_SWOPTION_EXT_CAP BIT(12)
8493
8594 /* error factors */
8695 #define M16C_EF_ACKE BIT(0)
....@@ -99,16 +108,6 @@
99108 #define USBCAN_ERROR_STATE_TX_ERROR BIT(0)
100109 #define USBCAN_ERROR_STATE_RX_ERROR BIT(1)
101110 #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
112111
113112 /* ctrl modes */
114113 #define KVASER_CTRL_MODE_NORMAL 1
....@@ -166,11 +165,7 @@
166165 struct kvaser_cmd_busparams {
167166 u8 tid;
168167 u8 channel;
169
- __le32 bitrate;
170
- u8 tseg1;
171
- u8 tseg2;
172
- u8 sjw;
173
- u8 no_samp;
168
+ struct kvaser_usb_busparams busparams;
174169 } __packed;
175170
176171 struct kvaser_cmd_tx_can {
....@@ -239,7 +234,7 @@
239234 u8 tid;
240235 } __packed;
241236
242
-struct leaf_cmd_error_event {
237
+struct leaf_cmd_can_error_event {
243238 u8 tid;
244239 u8 flags;
245240 __le16 time[3];
....@@ -251,7 +246,7 @@
251246 u8 error_factor;
252247 } __packed;
253248
254
-struct usbcan_cmd_error_event {
249
+struct usbcan_cmd_can_error_event {
255250 u8 tid;
256251 u8 padding;
257252 u8 tx_errors_count_ch0;
....@@ -261,6 +256,28 @@
261256 u8 status_ch0;
262257 u8 status_ch1;
263258 __le16 time;
259
+} __packed;
260
+
261
+/* CMD_ERROR_EVENT error codes */
262
+#define KVASER_USB_LEAF_ERROR_EVENT_TX_QUEUE_FULL 0x8
263
+#define KVASER_USB_LEAF_ERROR_EVENT_PARAM 0x9
264
+
265
+struct leaf_cmd_error_event {
266
+ u8 tid;
267
+ u8 error_code;
268
+ __le16 timestamp[3];
269
+ __le16 padding;
270
+ __le16 info1;
271
+ __le16 info2;
272
+} __packed;
273
+
274
+struct usbcan_cmd_error_event {
275
+ u8 tid;
276
+ u8 error_code;
277
+ __le16 info1;
278
+ __le16 info2;
279
+ __le16 timestamp;
280
+ __le16 padding;
264281 } __packed;
265282
266283 struct kvaser_cmd_ctrl_mode {
....@@ -287,6 +304,28 @@
287304 u8 data[8];
288305 } __packed;
289306
307
+/* Sub commands for cap_req and cap_res */
308
+#define KVASER_USB_LEAF_CAP_CMD_LISTEN_MODE 0x02
309
+#define KVASER_USB_LEAF_CAP_CMD_ERR_REPORT 0x05
310
+struct kvaser_cmd_cap_req {
311
+ __le16 padding0;
312
+ __le16 cap_cmd;
313
+ __le16 padding1;
314
+ __le16 channel;
315
+} __packed;
316
+
317
+/* Status codes for cap_res */
318
+#define KVASER_USB_LEAF_CAP_STAT_OK 0x00
319
+#define KVASER_USB_LEAF_CAP_STAT_NOT_IMPL 0x01
320
+#define KVASER_USB_LEAF_CAP_STAT_UNAVAIL 0x02
321
+struct kvaser_cmd_cap_res {
322
+ __le16 padding;
323
+ __le16 cap_cmd;
324
+ __le16 status;
325
+ __le32 mask;
326
+ __le32 value;
327
+} __packed;
328
+
290329 struct kvaser_cmd {
291330 u8 len;
292331 u8 id;
....@@ -302,14 +341,18 @@
302341 struct leaf_cmd_softinfo softinfo;
303342 struct leaf_cmd_rx_can rx_can;
304343 struct leaf_cmd_chip_state_event chip_state_event;
305
- struct leaf_cmd_error_event error_event;
344
+ struct leaf_cmd_can_error_event can_error_event;
306345 struct leaf_cmd_log_message log_message;
346
+ struct leaf_cmd_error_event error_event;
347
+ struct kvaser_cmd_cap_req cap_req;
348
+ struct kvaser_cmd_cap_res cap_res;
307349 } __packed leaf;
308350
309351 union {
310352 struct usbcan_cmd_softinfo softinfo;
311353 struct usbcan_cmd_rx_can rx_can;
312354 struct usbcan_cmd_chip_state_event chip_state_event;
355
+ struct usbcan_cmd_can_error_event can_error_event;
313356 struct usbcan_cmd_error_event error_event;
314357 } __packed usbcan;
315358
....@@ -318,6 +361,42 @@
318361 struct kvaser_cmd_flush_queue flush_queue;
319362 } u;
320363 } __packed;
364
+
365
+#define CMD_SIZE_ANY 0xff
366
+#define kvaser_fsize(field) sizeof_field(struct kvaser_cmd, field)
367
+
368
+static const u8 kvaser_usb_leaf_cmd_sizes_leaf[] = {
369
+ [CMD_START_CHIP_REPLY] = kvaser_fsize(u.simple),
370
+ [CMD_STOP_CHIP_REPLY] = kvaser_fsize(u.simple),
371
+ [CMD_GET_CARD_INFO_REPLY] = kvaser_fsize(u.cardinfo),
372
+ [CMD_TX_ACKNOWLEDGE] = kvaser_fsize(u.tx_acknowledge_header),
373
+ [CMD_GET_SOFTWARE_INFO_REPLY] = kvaser_fsize(u.leaf.softinfo),
374
+ [CMD_RX_STD_MESSAGE] = kvaser_fsize(u.leaf.rx_can),
375
+ [CMD_RX_EXT_MESSAGE] = kvaser_fsize(u.leaf.rx_can),
376
+ [CMD_LEAF_LOG_MESSAGE] = kvaser_fsize(u.leaf.log_message),
377
+ [CMD_CHIP_STATE_EVENT] = kvaser_fsize(u.leaf.chip_state_event),
378
+ [CMD_CAN_ERROR_EVENT] = kvaser_fsize(u.leaf.can_error_event),
379
+ [CMD_GET_CAPABILITIES_RESP] = kvaser_fsize(u.leaf.cap_res),
380
+ [CMD_GET_BUS_PARAMS_REPLY] = kvaser_fsize(u.busparams),
381
+ [CMD_ERROR_EVENT] = kvaser_fsize(u.leaf.error_event),
382
+ /* ignored events: */
383
+ [CMD_FLUSH_QUEUE_REPLY] = CMD_SIZE_ANY,
384
+};
385
+
386
+static const u8 kvaser_usb_leaf_cmd_sizes_usbcan[] = {
387
+ [CMD_START_CHIP_REPLY] = kvaser_fsize(u.simple),
388
+ [CMD_STOP_CHIP_REPLY] = kvaser_fsize(u.simple),
389
+ [CMD_GET_CARD_INFO_REPLY] = kvaser_fsize(u.cardinfo),
390
+ [CMD_TX_ACKNOWLEDGE] = kvaser_fsize(u.tx_acknowledge_header),
391
+ [CMD_GET_SOFTWARE_INFO_REPLY] = kvaser_fsize(u.usbcan.softinfo),
392
+ [CMD_RX_STD_MESSAGE] = kvaser_fsize(u.usbcan.rx_can),
393
+ [CMD_RX_EXT_MESSAGE] = kvaser_fsize(u.usbcan.rx_can),
394
+ [CMD_CHIP_STATE_EVENT] = kvaser_fsize(u.usbcan.chip_state_event),
395
+ [CMD_CAN_ERROR_EVENT] = kvaser_fsize(u.usbcan.can_error_event),
396
+ [CMD_ERROR_EVENT] = kvaser_fsize(u.usbcan.error_event),
397
+ /* ignored events: */
398
+ [CMD_USBCAN_CLOCK_OVERFLOW_EVENT] = CMD_SIZE_ANY,
399
+};
321400
322401 /* Summary of a kvaser error event, for a unified Leaf/Usbcan error
323402 * handling. Some discrepancies between the two families exist:
....@@ -342,49 +421,112 @@
342421 };
343422 };
344423
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,
424
+struct kvaser_usb_net_leaf_priv {
425
+ struct kvaser_usb_net_priv *net;
426
+
427
+ struct delayed_work chip_state_req_work;
355428 };
356429
357
-static const struct kvaser_usb_dev_cfg kvaser_usb_leaf_dev_cfg_8mhz = {
430
+static const struct can_bittiming_const kvaser_usb_leaf_m16c_bittiming_const = {
431
+ .name = "kvaser_usb_ucii",
432
+ .tseg1_min = 4,
433
+ .tseg1_max = 16,
434
+ .tseg2_min = 2,
435
+ .tseg2_max = 8,
436
+ .sjw_max = 4,
437
+ .brp_min = 1,
438
+ .brp_max = 16,
439
+ .brp_inc = 1,
440
+};
441
+
442
+static const struct can_bittiming_const kvaser_usb_leaf_m32c_bittiming_const = {
443
+ .name = "kvaser_usb_leaf",
444
+ .tseg1_min = 3,
445
+ .tseg1_max = 16,
446
+ .tseg2_min = 2,
447
+ .tseg2_max = 8,
448
+ .sjw_max = 4,
449
+ .brp_min = 2,
450
+ .brp_max = 128,
451
+ .brp_inc = 2,
452
+};
453
+
454
+static const struct kvaser_usb_dev_cfg kvaser_usb_leaf_usbcan_dev_cfg = {
358455 .clock = {
359456 .freq = 8000000,
360457 },
361458 .timestamp_freq = 1,
362
- .bittiming_const = &kvaser_usb_leaf_bittiming_const,
459
+ .bittiming_const = &kvaser_usb_leaf_m16c_bittiming_const,
363460 };
364461
365
-static const struct kvaser_usb_dev_cfg kvaser_usb_leaf_dev_cfg_16mhz = {
462
+static const struct kvaser_usb_dev_cfg kvaser_usb_leaf_m32c_dev_cfg = {
366463 .clock = {
367464 .freq = 16000000,
368465 },
369466 .timestamp_freq = 1,
370
- .bittiming_const = &kvaser_usb_leaf_bittiming_const,
467
+ .bittiming_const = &kvaser_usb_leaf_m32c_bittiming_const,
371468 };
372469
373
-static const struct kvaser_usb_dev_cfg kvaser_usb_leaf_dev_cfg_24mhz = {
470
+static const struct kvaser_usb_dev_cfg kvaser_usb_leaf_imx_dev_cfg_16mhz = {
471
+ .clock = {
472
+ .freq = 16000000,
473
+ },
474
+ .timestamp_freq = 1,
475
+ .bittiming_const = &kvaser_usb_flexc_bittiming_const,
476
+};
477
+
478
+static const struct kvaser_usb_dev_cfg kvaser_usb_leaf_imx_dev_cfg_24mhz = {
374479 .clock = {
375480 .freq = 24000000,
376481 },
377482 .timestamp_freq = 1,
378
- .bittiming_const = &kvaser_usb_leaf_bittiming_const,
483
+ .bittiming_const = &kvaser_usb_flexc_bittiming_const,
379484 };
380485
381
-static const struct kvaser_usb_dev_cfg kvaser_usb_leaf_dev_cfg_32mhz = {
486
+static const struct kvaser_usb_dev_cfg kvaser_usb_leaf_imx_dev_cfg_32mhz = {
382487 .clock = {
383488 .freq = 32000000,
384489 },
385490 .timestamp_freq = 1,
386
- .bittiming_const = &kvaser_usb_leaf_bittiming_const,
491
+ .bittiming_const = &kvaser_usb_flexc_bittiming_const,
387492 };
493
+
494
+static int kvaser_usb_leaf_verify_size(const struct kvaser_usb *dev,
495
+ const struct kvaser_cmd *cmd)
496
+{
497
+ /* buffer size >= cmd->len ensured by caller */
498
+ u8 min_size = 0;
499
+
500
+ switch (dev->driver_info->family) {
501
+ case KVASER_LEAF:
502
+ if (cmd->id < ARRAY_SIZE(kvaser_usb_leaf_cmd_sizes_leaf))
503
+ min_size = kvaser_usb_leaf_cmd_sizes_leaf[cmd->id];
504
+ break;
505
+ case KVASER_USBCAN:
506
+ if (cmd->id < ARRAY_SIZE(kvaser_usb_leaf_cmd_sizes_usbcan))
507
+ min_size = kvaser_usb_leaf_cmd_sizes_usbcan[cmd->id];
508
+ break;
509
+ }
510
+
511
+ if (min_size == CMD_SIZE_ANY)
512
+ return 0;
513
+
514
+ if (min_size) {
515
+ min_size += CMD_HEADER_LEN;
516
+ if (cmd->len >= min_size)
517
+ return 0;
518
+
519
+ dev_err_ratelimited(&dev->intf->dev,
520
+ "Received command %u too short (size %u, needed %u)",
521
+ cmd->id, cmd->len, min_size);
522
+ return -EIO;
523
+ }
524
+
525
+ dev_warn_ratelimited(&dev->intf->dev,
526
+ "Unhandled command (%d, size %d)\n",
527
+ cmd->id, cmd->len);
528
+ return -EINVAL;
529
+}
388530
389531 static void *
390532 kvaser_usb_leaf_frame_to_cmd(const struct kvaser_usb_net_priv *priv,
....@@ -405,7 +547,7 @@
405547 sizeof(struct kvaser_cmd_tx_can);
406548 cmd->u.tx_can.channel = priv->channel;
407549
408
- switch (dev->card_data.leaf.family) {
550
+ switch (dev->driver_info->family) {
409551 case KVASER_LEAF:
410552 cmd_tx_can_flags = &cmd->u.tx_can.leaf.flags;
411553 break;
....@@ -493,6 +635,9 @@
493635 end:
494636 kfree(buf);
495637
638
+ if (err == 0)
639
+ err = kvaser_usb_leaf_verify_size(dev, cmd);
640
+
496641 return err;
497642 }
498643
....@@ -525,16 +670,26 @@
525670 dev->fw_version = le32_to_cpu(softinfo->fw_version);
526671 dev->max_tx_urbs = le16_to_cpu(softinfo->max_outstanding_tx);
527672
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;
673
+ if (sw_options & KVASER_USB_LEAF_SWOPTION_EXT_CAP)
674
+ dev->card_data.capabilities |= KVASER_USB_CAP_EXT_CAP;
675
+
676
+ if (dev->driver_info->quirks & KVASER_USB_QUIRK_IGNORE_CLK_FREQ) {
677
+ /* Firmware expects bittiming parameters calculated for 16MHz
678
+ * clock, regardless of the actual clock
679
+ */
680
+ dev->cfg = &kvaser_usb_leaf_m32c_dev_cfg;
681
+ } else {
682
+ switch (sw_options & KVASER_USB_LEAF_SWOPTION_FREQ_MASK) {
683
+ case KVASER_USB_LEAF_SWOPTION_FREQ_16_MHZ_CLK:
684
+ dev->cfg = &kvaser_usb_leaf_imx_dev_cfg_16mhz;
685
+ break;
686
+ case KVASER_USB_LEAF_SWOPTION_FREQ_24_MHZ_CLK:
687
+ dev->cfg = &kvaser_usb_leaf_imx_dev_cfg_24mhz;
688
+ break;
689
+ case KVASER_USB_LEAF_SWOPTION_FREQ_32_MHZ_CLK:
690
+ dev->cfg = &kvaser_usb_leaf_imx_dev_cfg_32mhz;
691
+ break;
692
+ }
538693 }
539694 }
540695
....@@ -551,7 +706,7 @@
551706 if (err)
552707 return err;
553708
554
- switch (dev->card_data.leaf.family) {
709
+ switch (dev->driver_info->family) {
555710 case KVASER_LEAF:
556711 kvaser_usb_leaf_get_software_info_leaf(dev, &cmd.u.leaf.softinfo);
557712 break;
....@@ -559,7 +714,7 @@
559714 dev->fw_version = le32_to_cpu(cmd.u.usbcan.softinfo.fw_version);
560715 dev->max_tx_urbs =
561716 le16_to_cpu(cmd.u.usbcan.softinfo.max_outstanding_tx);
562
- dev->cfg = &kvaser_usb_leaf_dev_cfg_8mhz;
717
+ dev->cfg = &kvaser_usb_leaf_usbcan_dev_cfg;
563718 break;
564719 }
565720
....@@ -598,11 +753,121 @@
598753
599754 dev->nchannels = cmd.u.cardinfo.nchannels;
600755 if (dev->nchannels > KVASER_USB_MAX_NET_DEVICES ||
601
- (dev->card_data.leaf.family == KVASER_USBCAN &&
756
+ (dev->driver_info->family == KVASER_USBCAN &&
602757 dev->nchannels > MAX_USBCAN_NET_DEVICES))
603758 return -EINVAL;
604759
605760 return 0;
761
+}
762
+
763
+static int kvaser_usb_leaf_get_single_capability(struct kvaser_usb *dev,
764
+ u16 cap_cmd_req, u16 *status)
765
+{
766
+ struct kvaser_usb_dev_card_data *card_data = &dev->card_data;
767
+ struct kvaser_cmd *cmd;
768
+ u32 value = 0;
769
+ u32 mask = 0;
770
+ u16 cap_cmd_res;
771
+ int err;
772
+ int i;
773
+
774
+ cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
775
+ if (!cmd)
776
+ return -ENOMEM;
777
+
778
+ cmd->id = CMD_GET_CAPABILITIES_REQ;
779
+ cmd->u.leaf.cap_req.cap_cmd = cpu_to_le16(cap_cmd_req);
780
+ cmd->len = CMD_HEADER_LEN + sizeof(struct kvaser_cmd_cap_req);
781
+
782
+ err = kvaser_usb_send_cmd(dev, cmd, cmd->len);
783
+ if (err)
784
+ goto end;
785
+
786
+ err = kvaser_usb_leaf_wait_cmd(dev, CMD_GET_CAPABILITIES_RESP, cmd);
787
+ if (err)
788
+ goto end;
789
+
790
+ *status = le16_to_cpu(cmd->u.leaf.cap_res.status);
791
+
792
+ if (*status != KVASER_USB_LEAF_CAP_STAT_OK)
793
+ goto end;
794
+
795
+ cap_cmd_res = le16_to_cpu(cmd->u.leaf.cap_res.cap_cmd);
796
+ switch (cap_cmd_res) {
797
+ case KVASER_USB_LEAF_CAP_CMD_LISTEN_MODE:
798
+ case KVASER_USB_LEAF_CAP_CMD_ERR_REPORT:
799
+ value = le32_to_cpu(cmd->u.leaf.cap_res.value);
800
+ mask = le32_to_cpu(cmd->u.leaf.cap_res.mask);
801
+ break;
802
+ default:
803
+ dev_warn(&dev->intf->dev, "Unknown capability command %u\n",
804
+ cap_cmd_res);
805
+ break;
806
+ }
807
+
808
+ for (i = 0; i < dev->nchannels; i++) {
809
+ if (BIT(i) & (value & mask)) {
810
+ switch (cap_cmd_res) {
811
+ case KVASER_USB_LEAF_CAP_CMD_LISTEN_MODE:
812
+ card_data->ctrlmode_supported |=
813
+ CAN_CTRLMODE_LISTENONLY;
814
+ break;
815
+ case KVASER_USB_LEAF_CAP_CMD_ERR_REPORT:
816
+ card_data->capabilities |=
817
+ KVASER_USB_CAP_BERR_CAP;
818
+ break;
819
+ }
820
+ }
821
+ }
822
+
823
+end:
824
+ kfree(cmd);
825
+
826
+ return err;
827
+}
828
+
829
+static int kvaser_usb_leaf_get_capabilities_leaf(struct kvaser_usb *dev)
830
+{
831
+ int err;
832
+ u16 status;
833
+
834
+ if (!(dev->card_data.capabilities & KVASER_USB_CAP_EXT_CAP)) {
835
+ dev_info(&dev->intf->dev,
836
+ "No extended capability support. Upgrade device firmware.\n");
837
+ return 0;
838
+ }
839
+
840
+ err = kvaser_usb_leaf_get_single_capability(dev,
841
+ KVASER_USB_LEAF_CAP_CMD_LISTEN_MODE,
842
+ &status);
843
+ if (err)
844
+ return err;
845
+ if (status)
846
+ dev_info(&dev->intf->dev,
847
+ "KVASER_USB_LEAF_CAP_CMD_LISTEN_MODE failed %u\n",
848
+ status);
849
+
850
+ err = kvaser_usb_leaf_get_single_capability(dev,
851
+ KVASER_USB_LEAF_CAP_CMD_ERR_REPORT,
852
+ &status);
853
+ if (err)
854
+ return err;
855
+ if (status)
856
+ dev_info(&dev->intf->dev,
857
+ "KVASER_USB_LEAF_CAP_CMD_ERR_REPORT failed %u\n",
858
+ status);
859
+
860
+ return 0;
861
+}
862
+
863
+static int kvaser_usb_leaf_get_capabilities(struct kvaser_usb *dev)
864
+{
865
+ int err = 0;
866
+
867
+ if (dev->driver_info->family == KVASER_LEAF)
868
+ err = kvaser_usb_leaf_get_capabilities_leaf(dev);
869
+
870
+ return err;
606871 }
607872
608873 static void kvaser_usb_leaf_tx_acknowledge(const struct kvaser_usb *dev,
....@@ -633,7 +898,7 @@
633898 context = &priv->tx_contexts[tid % dev->max_tx_urbs];
634899
635900 /* 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) {
901
+ if (priv->can.restart_ms && priv->can.state == CAN_STATE_BUS_OFF) {
637902 struct sk_buff *skb;
638903 struct can_frame *cf;
639904
....@@ -689,6 +954,16 @@
689954 return err;
690955 }
691956
957
+static void kvaser_usb_leaf_chip_state_req_work(struct work_struct *work)
958
+{
959
+ struct kvaser_usb_net_leaf_priv *leaf =
960
+ container_of(work, struct kvaser_usb_net_leaf_priv,
961
+ chip_state_req_work.work);
962
+ struct kvaser_usb_net_priv *priv = leaf->net;
963
+
964
+ kvaser_usb_leaf_simple_cmd_async(priv, CMD_GET_CHIP_STATE);
965
+}
966
+
692967 static void
693968 kvaser_usb_leaf_rx_error_update_can_state(struct kvaser_usb_net_priv *priv,
694969 const struct kvaser_usb_err_summary *es,
....@@ -707,20 +982,16 @@
707982 new_state = CAN_STATE_BUS_OFF;
708983 } else if (es->status & M16C_STATE_BUS_PASSIVE) {
709984 new_state = CAN_STATE_ERROR_PASSIVE;
710
- } else if (es->status & M16C_STATE_BUS_ERROR) {
985
+ } else if ((es->status & M16C_STATE_BUS_ERROR) &&
986
+ cur_state >= CAN_STATE_BUS_OFF) {
711987 /* 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)
988
+ } else if (es->txerr >= 128 || es->rxerr >= 128) {
989
+ new_state = CAN_STATE_ERROR_PASSIVE;
990
+ } else if (es->txerr >= 96 || es->rxerr >= 96) {
991
+ new_state = CAN_STATE_ERROR_WARNING;
992
+ } else {
723993 new_state = CAN_STATE_ERROR_ACTIVE;
994
+ }
724995
725996 if (new_state != cur_state) {
726997 tx_state = (es->txerr >= es->rxerr) ? new_state : 0;
....@@ -730,11 +1001,11 @@
7301001 }
7311002
7321003 if (priv->can.restart_ms &&
733
- cur_state >= CAN_STATE_BUS_OFF &&
1004
+ cur_state == CAN_STATE_BUS_OFF &&
7341005 new_state < CAN_STATE_BUS_OFF)
7351006 priv->can.can_stats.restarts++;
7361007
737
- switch (dev->card_data.leaf.family) {
1008
+ switch (dev->driver_info->family) {
7381009 case KVASER_LEAF:
7391010 if (es->leaf.error_factor) {
7401011 priv->can.can_stats.bus_error++;
....@@ -764,6 +1035,7 @@
7641035 struct sk_buff *skb;
7651036 struct net_device_stats *stats;
7661037 struct kvaser_usb_net_priv *priv;
1038
+ struct kvaser_usb_net_leaf_priv *leaf;
7671039 enum can_state old_state, new_state;
7681040
7691041 if (es->channel >= dev->nchannels) {
....@@ -773,7 +1045,12 @@
7731045 }
7741046
7751047 priv = dev->nets[es->channel];
1048
+ leaf = priv->sub_priv;
7761049 stats = &priv->netdev->stats;
1050
+
1051
+ /* Ignore e.g. state change to bus-off reported just after stopping */
1052
+ if (!netif_running(priv->netdev))
1053
+ return;
7771054
7781055 /* Update all of the CAN interface's state and error counters before
7791056 * trying any memory allocation that can actually fail with -ENOMEM.
....@@ -788,6 +1065,14 @@
7881065 old_state = priv->can.state;
7891066 kvaser_usb_leaf_rx_error_update_can_state(priv, es, &tmp_cf);
7901067 new_state = priv->can.state;
1068
+
1069
+ /* If there are errors, request status updates periodically as we do
1070
+ * not get automatic notifications of improved state.
1071
+ */
1072
+ if (new_state < CAN_STATE_BUS_OFF &&
1073
+ (es->rxerr || es->txerr || new_state == CAN_STATE_ERROR_PASSIVE))
1074
+ schedule_delayed_work(&leaf->chip_state_req_work,
1075
+ msecs_to_jiffies(500));
7911076
7921077 skb = alloc_can_err_skb(priv->netdev, &cf);
7931078 if (!skb) {
....@@ -806,14 +1091,14 @@
8061091 }
8071092
8081093 if (priv->can.restart_ms &&
809
- old_state >= CAN_STATE_BUS_OFF &&
1094
+ old_state == CAN_STATE_BUS_OFF &&
8101095 new_state < CAN_STATE_BUS_OFF) {
8111096 cf->can_id |= CAN_ERR_RESTARTED;
8121097 netif_carrier_on(priv->netdev);
8131098 }
8141099 }
8151100
816
- switch (dev->card_data.leaf.family) {
1101
+ switch (dev->driver_info->family) {
8171102 case KVASER_LEAF:
8181103 if (es->leaf.error_factor) {
8191104 cf->can_id |= CAN_ERR_BUSERROR | CAN_ERR_PROT;
....@@ -840,8 +1125,10 @@
8401125 break;
8411126 }
8421127
843
- cf->data[6] = es->txerr;
844
- cf->data[7] = es->rxerr;
1128
+ if (new_state != CAN_STATE_BUS_OFF) {
1129
+ cf->data[6] = es->txerr;
1130
+ cf->data[7] = es->rxerr;
1131
+ }
8451132
8461133 stats->rx_packets++;
8471134 stats->rx_bytes += cf->can_dlc;
....@@ -904,11 +1191,11 @@
9041191
9051192 case CMD_CAN_ERROR_EVENT:
9061193 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;
1194
+ es.status = cmd->u.usbcan.can_error_event.status_ch0;
1195
+ es.txerr = cmd->u.usbcan.can_error_event.tx_errors_count_ch0;
1196
+ es.rxerr = cmd->u.usbcan.can_error_event.rx_errors_count_ch0;
9101197 es.usbcan.other_ch_status =
911
- cmd->u.usbcan.error_event.status_ch1;
1198
+ cmd->u.usbcan.can_error_event.status_ch1;
9121199 kvaser_usb_leaf_usbcan_conditionally_rx_error(dev, &es);
9131200
9141201 /* The USBCAN firmware supports up to 2 channels.
....@@ -916,13 +1203,13 @@
9161203 */
9171204 if (dev->nchannels == MAX_USBCAN_NET_DEVICES) {
9181205 es.channel = 1;
919
- es.status = cmd->u.usbcan.error_event.status_ch1;
1206
+ es.status = cmd->u.usbcan.can_error_event.status_ch1;
9201207 es.txerr =
921
- cmd->u.usbcan.error_event.tx_errors_count_ch1;
1208
+ cmd->u.usbcan.can_error_event.tx_errors_count_ch1;
9221209 es.rxerr =
923
- cmd->u.usbcan.error_event.rx_errors_count_ch1;
1210
+ cmd->u.usbcan.can_error_event.rx_errors_count_ch1;
9241211 es.usbcan.other_ch_status =
925
- cmd->u.usbcan.error_event.status_ch0;
1212
+ cmd->u.usbcan.can_error_event.status_ch0;
9261213 kvaser_usb_leaf_usbcan_conditionally_rx_error(dev, &es);
9271214 }
9281215 break;
....@@ -939,11 +1226,11 @@
9391226
9401227 switch (cmd->id) {
9411228 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;
1229
+ es.channel = cmd->u.leaf.can_error_event.channel;
1230
+ es.status = cmd->u.leaf.can_error_event.status;
1231
+ es.txerr = cmd->u.leaf.can_error_event.tx_errors_count;
1232
+ es.rxerr = cmd->u.leaf.can_error_event.rx_errors_count;
1233
+ es.leaf.error_factor = cmd->u.leaf.can_error_event.error_factor;
9471234 break;
9481235 case CMD_LEAF_LOG_MESSAGE:
9491236 es.channel = cmd->u.leaf.log_message.channel;
....@@ -1005,7 +1292,7 @@
10051292 stats = &priv->netdev->stats;
10061293
10071294 if ((cmd->u.rx_can_header.flag & MSG_FLAG_ERROR_FRAME) &&
1008
- (dev->card_data.leaf.family == KVASER_LEAF &&
1295
+ (dev->driver_info->family == KVASER_LEAF &&
10091296 cmd->id == CMD_LEAF_LOG_MESSAGE)) {
10101297 kvaser_usb_leaf_leaf_rx_error(dev, cmd);
10111298 return;
....@@ -1021,7 +1308,7 @@
10211308 return;
10221309 }
10231310
1024
- switch (dev->card_data.leaf.family) {
1311
+ switch (dev->driver_info->family) {
10251312 case KVASER_LEAF:
10261313 rx_data = cmd->u.leaf.rx_can.data;
10271314 break;
....@@ -1036,7 +1323,7 @@
10361323 return;
10371324 }
10381325
1039
- if (dev->card_data.leaf.family == KVASER_LEAF && cmd->id ==
1326
+ if (dev->driver_info->family == KVASER_LEAF && cmd->id ==
10401327 CMD_LEAF_LOG_MESSAGE) {
10411328 cf->can_id = le32_to_cpu(cmd->u.leaf.log_message.id);
10421329 if (cf->can_id & KVASER_EXTENDED_FRAME)
....@@ -1073,6 +1360,74 @@
10731360 stats->rx_packets++;
10741361 stats->rx_bytes += cf->can_dlc;
10751362 netif_rx(skb);
1363
+}
1364
+
1365
+static void kvaser_usb_leaf_error_event_parameter(const struct kvaser_usb *dev,
1366
+ const struct kvaser_cmd *cmd)
1367
+{
1368
+ u16 info1 = 0;
1369
+
1370
+ switch (dev->driver_info->family) {
1371
+ case KVASER_LEAF:
1372
+ info1 = le16_to_cpu(cmd->u.leaf.error_event.info1);
1373
+ break;
1374
+ case KVASER_USBCAN:
1375
+ info1 = le16_to_cpu(cmd->u.usbcan.error_event.info1);
1376
+ break;
1377
+ }
1378
+
1379
+ /* info1 will contain the offending cmd_no */
1380
+ switch (info1) {
1381
+ case CMD_SET_CTRL_MODE:
1382
+ dev_warn(&dev->intf->dev,
1383
+ "CMD_SET_CTRL_MODE error in parameter\n");
1384
+ break;
1385
+
1386
+ case CMD_SET_BUS_PARAMS:
1387
+ dev_warn(&dev->intf->dev,
1388
+ "CMD_SET_BUS_PARAMS error in parameter\n");
1389
+ break;
1390
+
1391
+ default:
1392
+ dev_warn(&dev->intf->dev,
1393
+ "Unhandled parameter error event cmd_no (%u)\n",
1394
+ info1);
1395
+ break;
1396
+ }
1397
+}
1398
+
1399
+static void kvaser_usb_leaf_error_event(const struct kvaser_usb *dev,
1400
+ const struct kvaser_cmd *cmd)
1401
+{
1402
+ u8 error_code = 0;
1403
+
1404
+ switch (dev->driver_info->family) {
1405
+ case KVASER_LEAF:
1406
+ error_code = cmd->u.leaf.error_event.error_code;
1407
+ break;
1408
+ case KVASER_USBCAN:
1409
+ error_code = cmd->u.usbcan.error_event.error_code;
1410
+ break;
1411
+ }
1412
+
1413
+ switch (error_code) {
1414
+ case KVASER_USB_LEAF_ERROR_EVENT_TX_QUEUE_FULL:
1415
+ /* Received additional CAN message, when firmware TX queue is
1416
+ * already full. Something is wrong with the driver.
1417
+ * This should never happen!
1418
+ */
1419
+ dev_err(&dev->intf->dev,
1420
+ "Received error event TX_QUEUE_FULL\n");
1421
+ break;
1422
+ case KVASER_USB_LEAF_ERROR_EVENT_PARAM:
1423
+ kvaser_usb_leaf_error_event_parameter(dev, cmd);
1424
+ break;
1425
+
1426
+ default:
1427
+ dev_warn(&dev->intf->dev,
1428
+ "Unhandled error event (%d)\n", error_code);
1429
+ break;
1430
+ }
10761431 }
10771432
10781433 static void kvaser_usb_leaf_start_chip_reply(const struct kvaser_usb *dev,
....@@ -1115,9 +1470,31 @@
11151470 complete(&priv->stop_comp);
11161471 }
11171472
1473
+static void kvaser_usb_leaf_get_busparams_reply(const struct kvaser_usb *dev,
1474
+ const struct kvaser_cmd *cmd)
1475
+{
1476
+ struct kvaser_usb_net_priv *priv;
1477
+ u8 channel = cmd->u.busparams.channel;
1478
+
1479
+ if (channel >= dev->nchannels) {
1480
+ dev_err(&dev->intf->dev,
1481
+ "Invalid channel number (%d)\n", channel);
1482
+ return;
1483
+ }
1484
+
1485
+ priv = dev->nets[channel];
1486
+ memcpy(&priv->busparams_nominal, &cmd->u.busparams.busparams,
1487
+ sizeof(priv->busparams_nominal));
1488
+
1489
+ complete(&priv->get_busparams_comp);
1490
+}
1491
+
11181492 static void kvaser_usb_leaf_handle_command(const struct kvaser_usb *dev,
11191493 const struct kvaser_cmd *cmd)
11201494 {
1495
+ if (kvaser_usb_leaf_verify_size(dev, cmd) < 0)
1496
+ return;
1497
+
11211498 switch (cmd->id) {
11221499 case CMD_START_CHIP_REPLY:
11231500 kvaser_usb_leaf_start_chip_reply(dev, cmd);
....@@ -1133,14 +1510,14 @@
11331510 break;
11341511
11351512 case CMD_LEAF_LOG_MESSAGE:
1136
- if (dev->card_data.leaf.family != KVASER_LEAF)
1513
+ if (dev->driver_info->family != KVASER_LEAF)
11371514 goto warn;
11381515 kvaser_usb_leaf_rx_can_msg(dev, cmd);
11391516 break;
11401517
11411518 case CMD_CHIP_STATE_EVENT:
11421519 case CMD_CAN_ERROR_EVENT:
1143
- if (dev->card_data.leaf.family == KVASER_LEAF)
1520
+ if (dev->driver_info->family == KVASER_LEAF)
11441521 kvaser_usb_leaf_leaf_rx_error(dev, cmd);
11451522 else
11461523 kvaser_usb_leaf_usbcan_rx_error(dev, cmd);
....@@ -1150,14 +1527,22 @@
11501527 kvaser_usb_leaf_tx_acknowledge(dev, cmd);
11511528 break;
11521529
1530
+ case CMD_ERROR_EVENT:
1531
+ kvaser_usb_leaf_error_event(dev, cmd);
1532
+ break;
1533
+
1534
+ case CMD_GET_BUS_PARAMS_REPLY:
1535
+ kvaser_usb_leaf_get_busparams_reply(dev, cmd);
1536
+ break;
1537
+
11531538 /* Ignored commands */
11541539 case CMD_USBCAN_CLOCK_OVERFLOW_EVENT:
1155
- if (dev->card_data.leaf.family != KVASER_USBCAN)
1540
+ if (dev->driver_info->family != KVASER_USBCAN)
11561541 goto warn;
11571542 break;
11581543
11591544 case CMD_FLUSH_QUEUE_REPLY:
1160
- if (dev->card_data.leaf.family != KVASER_LEAF)
1545
+ if (dev->driver_info->family != KVASER_LEAF)
11611546 goto warn;
11621547 break;
11631548
....@@ -1230,7 +1615,7 @@
12301615 {
12311616 int err;
12321617
1233
- init_completion(&priv->start_comp);
1618
+ reinit_completion(&priv->start_comp);
12341619
12351620 err = kvaser_usb_leaf_send_simple_cmd(priv->dev, CMD_START_CHIP,
12361621 priv->channel);
....@@ -1246,9 +1631,12 @@
12461631
12471632 static int kvaser_usb_leaf_stop_chip(struct kvaser_usb_net_priv *priv)
12481633 {
1634
+ struct kvaser_usb_net_leaf_priv *leaf = priv->sub_priv;
12491635 int err;
12501636
1251
- init_completion(&priv->stop_comp);
1637
+ reinit_completion(&priv->stop_comp);
1638
+
1639
+ cancel_delayed_work(&leaf->chip_state_req_work);
12521640
12531641 err = kvaser_usb_leaf_send_simple_cmd(priv->dev, CMD_STOP_CHIP,
12541642 priv->channel);
....@@ -1296,10 +1684,35 @@
12961684 return 0;
12971685 }
12981686
1299
-static int kvaser_usb_leaf_set_bittiming(struct net_device *netdev)
1687
+static int kvaser_usb_leaf_init_channel(struct kvaser_usb_net_priv *priv)
1688
+{
1689
+ struct kvaser_usb_net_leaf_priv *leaf;
1690
+
1691
+ leaf = devm_kzalloc(&priv->dev->intf->dev, sizeof(*leaf), GFP_KERNEL);
1692
+ if (!leaf)
1693
+ return -ENOMEM;
1694
+
1695
+ leaf->net = priv;
1696
+ INIT_DELAYED_WORK(&leaf->chip_state_req_work,
1697
+ kvaser_usb_leaf_chip_state_req_work);
1698
+
1699
+ priv->sub_priv = leaf;
1700
+
1701
+ return 0;
1702
+}
1703
+
1704
+static void kvaser_usb_leaf_remove_channel(struct kvaser_usb_net_priv *priv)
1705
+{
1706
+ struct kvaser_usb_net_leaf_priv *leaf = priv->sub_priv;
1707
+
1708
+ if (leaf)
1709
+ cancel_delayed_work_sync(&leaf->chip_state_req_work);
1710
+}
1711
+
1712
+static int kvaser_usb_leaf_set_bittiming(const struct net_device *netdev,
1713
+ const struct kvaser_usb_busparams *busparams)
13001714 {
13011715 struct kvaser_usb_net_priv *priv = netdev_priv(netdev);
1302
- struct can_bittiming *bt = &priv->can.bittiming;
13031716 struct kvaser_usb *dev = priv->dev;
13041717 struct kvaser_cmd *cmd;
13051718 int rc;
....@@ -1312,20 +1725,34 @@
13121725 cmd->len = CMD_HEADER_LEN + sizeof(struct kvaser_cmd_busparams);
13131726 cmd->u.busparams.channel = priv->channel;
13141727 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;
1728
+ memcpy(&cmd->u.busparams.busparams, busparams,
1729
+ sizeof(cmd->u.busparams.busparams));
13241730
13251731 rc = kvaser_usb_send_cmd(dev, cmd, cmd->len);
13261732
13271733 kfree(cmd);
13281734 return rc;
1735
+}
1736
+
1737
+static int kvaser_usb_leaf_get_busparams(struct kvaser_usb_net_priv *priv)
1738
+{
1739
+ int err;
1740
+
1741
+ if (priv->dev->driver_info->family == KVASER_USBCAN)
1742
+ return -EOPNOTSUPP;
1743
+
1744
+ reinit_completion(&priv->get_busparams_comp);
1745
+
1746
+ err = kvaser_usb_leaf_send_simple_cmd(priv->dev, CMD_GET_BUS_PARAMS,
1747
+ priv->channel);
1748
+ if (err)
1749
+ return err;
1750
+
1751
+ if (!wait_for_completion_timeout(&priv->get_busparams_comp,
1752
+ msecs_to_jiffies(KVASER_USB_TIMEOUT)))
1753
+ return -ETIMEDOUT;
1754
+
1755
+ return 0;
13291756 }
13301757
13311758 static int kvaser_usb_leaf_set_mode(struct net_device *netdev,
....@@ -1336,9 +1763,13 @@
13361763
13371764 switch (mode) {
13381765 case CAN_MODE_START:
1766
+ kvaser_usb_unlink_tx_urbs(priv);
1767
+
13391768 err = kvaser_usb_leaf_simple_cmd_async(priv, CMD_START_CHIP);
13401769 if (err)
13411770 return err;
1771
+
1772
+ priv->can.state = CAN_STATE_ERROR_ACTIVE;
13421773 break;
13431774 default:
13441775 return -EOPNOTSUPP;
....@@ -1385,14 +1816,18 @@
13851816 const struct kvaser_usb_dev_ops kvaser_usb_leaf_dev_ops = {
13861817 .dev_set_mode = kvaser_usb_leaf_set_mode,
13871818 .dev_set_bittiming = kvaser_usb_leaf_set_bittiming,
1819
+ .dev_get_busparams = kvaser_usb_leaf_get_busparams,
13881820 .dev_set_data_bittiming = NULL,
1821
+ .dev_get_data_busparams = NULL,
13891822 .dev_get_berr_counter = kvaser_usb_leaf_get_berr_counter,
13901823 .dev_setup_endpoints = kvaser_usb_leaf_setup_endpoints,
13911824 .dev_init_card = kvaser_usb_leaf_init_card,
1825
+ .dev_init_channel = kvaser_usb_leaf_init_channel,
1826
+ .dev_remove_channel = kvaser_usb_leaf_remove_channel,
13921827 .dev_get_software_info = kvaser_usb_leaf_get_software_info,
13931828 .dev_get_software_details = NULL,
13941829 .dev_get_card_info = kvaser_usb_leaf_get_card_info,
1395
- .dev_get_capabilities = NULL,
1830
+ .dev_get_capabilities = kvaser_usb_leaf_get_capabilities,
13961831 .dev_set_opt_mode = kvaser_usb_leaf_set_opt_mode,
13971832 .dev_start_chip = kvaser_usb_leaf_start_chip,
13981833 .dev_stop_chip = kvaser_usb_leaf_stop_chip,