1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
|
// SPDX-License-Identifier: GPL-2.0
/* Parts of this driver are based on the following:
* - Kvaser linux mhydra driver (version 5.24)
* - CAN driver for esd CAN-USB/2
*
* Copyright (C) 2018 KVASER AB, Sweden. All rights reserved.
* Copyright (C) 2010 Matthias Fuchs <matthias.fuchs@esd.eu>, esd gmbh
*
* Known issues:
* - Transition from CAN_STATE_ERROR_WARNING to CAN_STATE_ERROR_ACTIVE is only
* reported after a call to do_get_berr_counter(), since firmware does not
* distinguish between ERROR_WARNING and ERROR_ACTIVE.
* - Hardware timestamps are not set for CAN Tx frames.
*/
#include <linux/completion.h>
#include <linux/device.h>
#include <linux/gfp.h>
#include <linux/jiffies.h>
#include <linux/kernel.h>
#include <linux/netdevice.h>
#include <linux/spinlock.h>
#include <linux/string.h>
#include <linux/types.h>
#include <linux/units.h>
#include <linux/usb.h>
#include <linux/can.h>
#include <linux/can/dev.h>
#include <linux/can/error.h>
#include <linux/can/netlink.h>
#include "kvaser_usb.h"
/* Forward declarations */
static const struct kvaser_usb_dev_cfg kvaser_usb_hydra_dev_cfg_kcan;
static const struct kvaser_usb_dev_cfg kvaser_usb_hydra_dev_cfg_flexc;
static const struct kvaser_usb_dev_cfg kvaser_usb_hydra_dev_cfg_rt;
#define KVASER_USB_HYDRA_BULK_EP_IN_ADDR 0x82
#define KVASER_USB_HYDRA_BULK_EP_OUT_ADDR 0x02
#define KVASER_USB_HYDRA_MAX_TRANSID 0xff
#define KVASER_USB_HYDRA_MIN_TRANSID 0x01
/* Minihydra command IDs */
#define CMD_SET_BUSPARAMS_REQ 16
#define CMD_GET_CHIP_STATE_REQ 19
#define CMD_CHIP_STATE_EVENT 20
#define CMD_SET_DRIVERMODE_REQ 21
#define CMD_START_CHIP_REQ 26
#define CMD_START_CHIP_RESP 27
#define CMD_STOP_CHIP_REQ 28
#define CMD_STOP_CHIP_RESP 29
#define CMD_TX_CAN_MESSAGE 33
#define CMD_GET_CARD_INFO_REQ 34
#define CMD_GET_CARD_INFO_RESP 35
#define CMD_GET_SOFTWARE_INFO_REQ 38
#define CMD_GET_SOFTWARE_INFO_RESP 39
#define CMD_ERROR_EVENT 45
#define CMD_FLUSH_QUEUE 48
#define CMD_TX_ACKNOWLEDGE 50
#define CMD_FLUSH_QUEUE_RESP 66
#define CMD_SET_BUSPARAMS_FD_REQ 69
#define CMD_SET_BUSPARAMS_FD_RESP 70
#define CMD_SET_BUSPARAMS_RESP 85
#define CMD_GET_CAPABILITIES_REQ 95
#define CMD_GET_CAPABILITIES_RESP 96
#define CMD_RX_MESSAGE 106
#define CMD_MAP_CHANNEL_REQ 200
#define CMD_MAP_CHANNEL_RESP 201
#define CMD_GET_SOFTWARE_DETAILS_REQ 202
#define CMD_GET_SOFTWARE_DETAILS_RESP 203
#define CMD_EXTENDED 255
/* Minihydra extended command IDs */
#define CMD_TX_CAN_MESSAGE_FD 224
#define CMD_TX_ACKNOWLEDGE_FD 225
#define CMD_RX_MESSAGE_FD 226
/* Hydra commands are handled by different threads in firmware.
* The threads are denoted hydra entity (HE). Each HE got a unique 6-bit
* address. The address is used in hydra commands to get/set source and
* destination HE. There are two predefined HE addresses, the remaining
* addresses are different between devices and firmware versions. Hence, we need
* to enumerate the addresses (see kvaser_usb_hydra_map_channel()).
*/
/* Well-known HE addresses */
#define KVASER_USB_HYDRA_HE_ADDRESS_ROUTER 0x00
#define KVASER_USB_HYDRA_HE_ADDRESS_ILLEGAL 0x3e
#define KVASER_USB_HYDRA_TRANSID_CANHE 0x40
#define KVASER_USB_HYDRA_TRANSID_SYSDBG 0x61
struct kvaser_cmd_map_ch_req {
char name[16];
u8 channel;
u8 reserved[11];
} __packed;
struct kvaser_cmd_map_ch_res {
u8 he_addr;
u8 channel;
u8 reserved[26];
} __packed;
struct kvaser_cmd_card_info {
__le32 serial_number;
__le32 clock_res;
__le32 mfg_date;
__le32 ean[2];
u8 hw_version;
u8 usb_mode;
u8 hw_type;
u8 reserved0;
u8 nchannels;
u8 reserved1[3];
} __packed;
struct kvaser_cmd_sw_info {
u8 reserved0[8];
__le16 max_outstanding_tx;
u8 reserved1[18];
} __packed;
struct kvaser_cmd_sw_detail_req {
u8 use_ext_cmd;
u8 reserved[27];
} __packed;
/* Software detail flags */
#define KVASER_USB_HYDRA_SW_FLAG_FW_BETA BIT(2)
#define KVASER_USB_HYDRA_SW_FLAG_FW_BAD BIT(4)
#define KVASER_USB_HYDRA_SW_FLAG_FREQ_80M BIT(5)
#define KVASER_USB_HYDRA_SW_FLAG_EXT_CMD BIT(9)
#define KVASER_USB_HYDRA_SW_FLAG_CANFD BIT(10)
#define KVASER_USB_HYDRA_SW_FLAG_NONISO BIT(11)
#define KVASER_USB_HYDRA_SW_FLAG_EXT_CAP BIT(12)
#define KVASER_USB_HYDRA_SW_FLAG_CAN_FREQ_80M BIT(13)
struct kvaser_cmd_sw_detail_res {
__le32 sw_flags;
__le32 sw_version;
__le32 sw_name;
__le32 ean[2];
__le32 max_bitrate;
u8 reserved[4];
} __packed;
/* Sub commands for cap_req and cap_res */
#define KVASER_USB_HYDRA_CAP_CMD_LISTEN_MODE 0x02
#define KVASER_USB_HYDRA_CAP_CMD_ERR_REPORT 0x05
#define KVASER_USB_HYDRA_CAP_CMD_ONE_SHOT 0x06
struct kvaser_cmd_cap_req {
__le16 cap_cmd;
u8 reserved[26];
} __packed;
/* Status codes for cap_res */
#define KVASER_USB_HYDRA_CAP_STAT_OK 0x00
#define KVASER_USB_HYDRA_CAP_STAT_NOT_IMPL 0x01
#define KVASER_USB_HYDRA_CAP_STAT_UNAVAIL 0x02
struct kvaser_cmd_cap_res {
__le16 cap_cmd;
__le16 status;
__le32 mask;
__le32 value;
u8 reserved[16];
} __packed;
/* CMD_ERROR_EVENT error codes */
#define KVASER_USB_HYDRA_ERROR_EVENT_CAN 0x01
#define KVASER_USB_HYDRA_ERROR_EVENT_PARAM 0x09
struct kvaser_cmd_error_event {
__le16 timestamp[3];
u8 reserved;
u8 error_code;
__le16 info1;
__le16 info2;
} __packed;
/* Chip state status flags. Used for chip_state_event and err_frame_data. */
#define KVASER_USB_HYDRA_BUS_ERR_ACT 0x00
#define KVASER_USB_HYDRA_BUS_ERR_PASS BIT(5)
#define KVASER_USB_HYDRA_BUS_BUS_OFF BIT(6)
struct kvaser_cmd_chip_state_event {
__le16 timestamp[3];
u8 tx_err_counter;
u8 rx_err_counter;
u8 bus_status;
u8 reserved[19];
} __packed;
/* Busparam modes */
#define KVASER_USB_HYDRA_BUS_MODE_CAN 0x00
#define KVASER_USB_HYDRA_BUS_MODE_CANFD_ISO 0x01
#define KVASER_USB_HYDRA_BUS_MODE_NONISO 0x02
struct kvaser_cmd_set_busparams {
__le32 bitrate;
u8 tseg1;
u8 tseg2;
u8 sjw;
u8 nsamples;
u8 reserved0[4];
__le32 bitrate_d;
u8 tseg1_d;
u8 tseg2_d;
u8 sjw_d;
u8 nsamples_d;
u8 canfd_mode;
u8 reserved1[7];
} __packed;
/* Ctrl modes */
#define KVASER_USB_HYDRA_CTRLMODE_NORMAL 0x01
#define KVASER_USB_HYDRA_CTRLMODE_LISTEN 0x02
struct kvaser_cmd_set_ctrlmode {
u8 mode;
u8 reserved[27];
} __packed;
struct kvaser_err_frame_data {
u8 bus_status;
u8 reserved0;
u8 tx_err_counter;
u8 rx_err_counter;
u8 reserved1[4];
} __packed;
struct kvaser_cmd_rx_can {
u8 cmd_len;
u8 cmd_no;
u8 channel;
u8 flags;
__le16 timestamp[3];
u8 dlc;
u8 padding;
__le32 id;
union {
u8 data[8];
struct kvaser_err_frame_data err_frame_data;
};
} __packed;
/* Extended CAN ID flag. Used in rx_can and tx_can */
#define KVASER_USB_HYDRA_EXTENDED_FRAME_ID BIT(31)
struct kvaser_cmd_tx_can {
__le32 id;
u8 data[8];
u8 dlc;
u8 flags;
__le16 transid;
u8 channel;
u8 reserved[11];
} __packed;
struct kvaser_cmd_header {
u8 cmd_no;
/* The destination HE address is stored in 0..5 of he_addr.
* The upper part of source HE address is stored in 6..7 of he_addr, and
* the lower part is stored in 12..15 of transid.
*/
u8 he_addr;
__le16 transid;
} __packed;
struct kvaser_cmd {
struct kvaser_cmd_header header;
union {
struct kvaser_cmd_map_ch_req map_ch_req;
struct kvaser_cmd_map_ch_res map_ch_res;
struct kvaser_cmd_card_info card_info;
struct kvaser_cmd_sw_info sw_info;
struct kvaser_cmd_sw_detail_req sw_detail_req;
struct kvaser_cmd_sw_detail_res sw_detail_res;
struct kvaser_cmd_cap_req cap_req;
struct kvaser_cmd_cap_res cap_res;
struct kvaser_cmd_error_event error_event;
struct kvaser_cmd_set_busparams set_busparams_req;
struct kvaser_cmd_chip_state_event chip_state_event;
struct kvaser_cmd_set_ctrlmode set_ctrlmode;
struct kvaser_cmd_rx_can rx_can;
struct kvaser_cmd_tx_can tx_can;
} __packed;
} __packed;
/* CAN frame flags. Used in rx_can, ext_rx_can, tx_can and ext_tx_can */
#define KVASER_USB_HYDRA_CF_FLAG_ERROR_FRAME BIT(0)
#define KVASER_USB_HYDRA_CF_FLAG_OVERRUN BIT(1)
#define KVASER_USB_HYDRA_CF_FLAG_REMOTE_FRAME BIT(4)
#define KVASER_USB_HYDRA_CF_FLAG_EXTENDED_ID BIT(5)
#define KVASER_USB_HYDRA_CF_FLAG_TX_ACK BIT(6)
/* CAN frame flags. Used in ext_rx_can and ext_tx_can */
#define KVASER_USB_HYDRA_CF_FLAG_OSM_NACK BIT(12)
#define KVASER_USB_HYDRA_CF_FLAG_ABL BIT(13)
#define KVASER_USB_HYDRA_CF_FLAG_FDF BIT(16)
#define KVASER_USB_HYDRA_CF_FLAG_BRS BIT(17)
#define KVASER_USB_HYDRA_CF_FLAG_ESI BIT(18)
/* KCAN packet header macros. Used in ext_rx_can and ext_tx_can */
#define KVASER_USB_KCAN_DATA_DLC_BITS 4
#define KVASER_USB_KCAN_DATA_DLC_SHIFT 8
#define KVASER_USB_KCAN_DATA_DLC_MASK \
GENMASK(KVASER_USB_KCAN_DATA_DLC_BITS - 1 + \
KVASER_USB_KCAN_DATA_DLC_SHIFT, \
KVASER_USB_KCAN_DATA_DLC_SHIFT)
#define KVASER_USB_KCAN_DATA_BRS BIT(14)
#define KVASER_USB_KCAN_DATA_FDF BIT(15)
#define KVASER_USB_KCAN_DATA_OSM BIT(16)
#define KVASER_USB_KCAN_DATA_AREQ BIT(31)
#define KVASER_USB_KCAN_DATA_SRR BIT(31)
#define KVASER_USB_KCAN_DATA_RTR BIT(29)
#define KVASER_USB_KCAN_DATA_IDE BIT(30)
struct kvaser_cmd_ext_rx_can {
__le32 flags;
__le32 id;
__le32 kcan_id;
__le32 kcan_header;
__le64 timestamp;
union {
u8 kcan_payload[64];
struct kvaser_err_frame_data err_frame_data;
};
} __packed;
struct kvaser_cmd_ext_tx_can {
__le32 flags;
__le32 id;
__le32 kcan_id;
__le32 kcan_header;
u8 databytes;
u8 dlc;
u8 reserved[6];
u8 kcan_payload[64];
} __packed;
struct kvaser_cmd_ext_tx_ack {
__le32 flags;
u8 reserved0[4];
__le64 timestamp;
u8 reserved1[8];
} __packed;
/* struct for extended commands (CMD_EXTENDED) */
struct kvaser_cmd_ext {
struct kvaser_cmd_header header;
__le16 len;
u8 cmd_no_ext;
u8 reserved;
union {
struct kvaser_cmd_ext_rx_can rx_can;
struct kvaser_cmd_ext_tx_can tx_can;
struct kvaser_cmd_ext_tx_ack tx_ack;
} __packed;
} __packed;
static const struct can_bittiming_const kvaser_usb_hydra_kcan_bittiming_c = {
.name = "kvaser_usb_kcan",
.tseg1_min = 1,
.tseg1_max = 255,
.tseg2_min = 1,
.tseg2_max = 32,
.sjw_max = 16,
.brp_min = 1,
.brp_max = 8192,
.brp_inc = 1,
};
const struct can_bittiming_const kvaser_usb_flexc_bittiming_const = {
.name = "kvaser_usb_flex",
.tseg1_min = 4,
.tseg1_max = 16,
.tseg2_min = 2,
.tseg2_max = 8,
.sjw_max = 4,
.brp_min = 1,
.brp_max = 256,
.brp_inc = 1,
};
static const struct can_bittiming_const kvaser_usb_hydra_rt_bittiming_c = {
.name = "kvaser_usb_rt",
.tseg1_min = 2,
.tseg1_max = 96,
.tseg2_min = 2,
.tseg2_max = 32,
.sjw_max = 32,
.brp_min = 1,
.brp_max = 1024,
.brp_inc = 1,
};
static const struct can_bittiming_const kvaser_usb_hydra_rtd_bittiming_c = {
.name = "kvaser_usb_rt",
.tseg1_min = 2,
.tseg1_max = 39,
.tseg2_min = 2,
.tseg2_max = 8,
.sjw_max = 8,
.brp_min = 1,
.brp_max = 1024,
.brp_inc = 1,
};
#define KVASER_USB_HYDRA_TRANSID_BITS 12
#define KVASER_USB_HYDRA_TRANSID_MASK \
GENMASK(KVASER_USB_HYDRA_TRANSID_BITS - 1, 0)
#define KVASER_USB_HYDRA_HE_ADDR_SRC_MASK GENMASK(7, 6)
#define KVASER_USB_HYDRA_HE_ADDR_DEST_MASK GENMASK(5, 0)
#define KVASER_USB_HYDRA_HE_ADDR_SRC_BITS 2
static inline u16 kvaser_usb_hydra_get_cmd_transid(const struct kvaser_cmd *cmd)
{
return le16_to_cpu(cmd->header.transid) & KVASER_USB_HYDRA_TRANSID_MASK;
}
static inline void kvaser_usb_hydra_set_cmd_transid(struct kvaser_cmd *cmd,
u16 transid)
{
cmd->header.transid =
cpu_to_le16(transid & KVASER_USB_HYDRA_TRANSID_MASK);
}
static inline u8 kvaser_usb_hydra_get_cmd_src_he(const struct kvaser_cmd *cmd)
{
return (cmd->header.he_addr & KVASER_USB_HYDRA_HE_ADDR_SRC_MASK) >>
KVASER_USB_HYDRA_HE_ADDR_SRC_BITS |
le16_to_cpu(cmd->header.transid) >>
KVASER_USB_HYDRA_TRANSID_BITS;
}
static inline void kvaser_usb_hydra_set_cmd_dest_he(struct kvaser_cmd *cmd,
u8 dest_he)
{
cmd->header.he_addr =
(cmd->header.he_addr & KVASER_USB_HYDRA_HE_ADDR_SRC_MASK) |
(dest_he & KVASER_USB_HYDRA_HE_ADDR_DEST_MASK);
}
static u8 kvaser_usb_hydra_channel_from_cmd(const struct kvaser_usb *dev,
const struct kvaser_cmd *cmd)
{
int i;
u8 channel = 0xff;
u8 src_he = kvaser_usb_hydra_get_cmd_src_he(cmd);
for (i = 0; i < KVASER_USB_MAX_NET_DEVICES; i++) {
if (dev->card_data.hydra.channel_to_he[i] == src_he) {
channel = i;
break;
}
}
return channel;
}
static u16 kvaser_usb_hydra_get_next_transid(struct kvaser_usb *dev)
{
unsigned long flags;
u16 transid;
struct kvaser_usb_dev_card_data_hydra *card_data =
&dev->card_data.hydra;
spin_lock_irqsave(&card_data->transid_lock, flags);
transid = card_data->transid;
if (transid >= KVASER_USB_HYDRA_MAX_TRANSID)
transid = KVASER_USB_HYDRA_MIN_TRANSID;
else
transid++;
card_data->transid = transid;
spin_unlock_irqrestore(&card_data->transid_lock, flags);
return transid;
}
static size_t kvaser_usb_hydra_cmd_size(struct kvaser_cmd *cmd)
{
size_t ret;
if (cmd->header.cmd_no == CMD_EXTENDED)
ret = le16_to_cpu(((struct kvaser_cmd_ext *)cmd)->len);
else
ret = sizeof(struct kvaser_cmd);
return ret;
}
static struct kvaser_usb_net_priv *
kvaser_usb_hydra_net_priv_from_cmd(const struct kvaser_usb *dev,
const struct kvaser_cmd *cmd)
{
struct kvaser_usb_net_priv *priv = NULL;
u8 channel = kvaser_usb_hydra_channel_from_cmd(dev, cmd);
if (channel >= dev->nchannels)
dev_err(&dev->intf->dev,
"Invalid channel number (%d)\n", channel);
else
priv = dev->nets[channel];
return priv;
}
static ktime_t
kvaser_usb_hydra_ktime_from_rx_cmd(const struct kvaser_usb_dev_cfg *cfg,
const struct kvaser_cmd *cmd)
{
u64 ticks;
if (cmd->header.cmd_no == CMD_EXTENDED) {
struct kvaser_cmd_ext *cmd_ext = (struct kvaser_cmd_ext *)cmd;
ticks = le64_to_cpu(cmd_ext->rx_can.timestamp);
} else {
ticks = le16_to_cpu(cmd->rx_can.timestamp[0]);
ticks += (u64)(le16_to_cpu(cmd->rx_can.timestamp[1])) << 16;
ticks += (u64)(le16_to_cpu(cmd->rx_can.timestamp[2])) << 32;
}
return ns_to_ktime(div_u64(ticks * 1000, cfg->timestamp_freq));
}
static int kvaser_usb_hydra_send_simple_cmd(struct kvaser_usb *dev,
u8 cmd_no, int channel)
{
struct kvaser_cmd *cmd;
int err;
cmd = kcalloc(1, sizeof(struct kvaser_cmd), GFP_KERNEL);
if (!cmd)
return -ENOMEM;
cmd->header.cmd_no = cmd_no;
if (channel < 0) {
kvaser_usb_hydra_set_cmd_dest_he
(cmd, KVASER_USB_HYDRA_HE_ADDRESS_ILLEGAL);
} else {
if (channel >= KVASER_USB_MAX_NET_DEVICES) {
dev_err(&dev->intf->dev, "channel (%d) out of range.\n",
channel);
err = -EINVAL;
goto end;
}
kvaser_usb_hydra_set_cmd_dest_he
(cmd, dev->card_data.hydra.channel_to_he[channel]);
}
kvaser_usb_hydra_set_cmd_transid
(cmd, kvaser_usb_hydra_get_next_transid(dev));
err = kvaser_usb_send_cmd(dev, cmd, kvaser_usb_hydra_cmd_size(cmd));
if (err)
goto end;
end:
kfree(cmd);
return err;
}
static int
kvaser_usb_hydra_send_simple_cmd_async(struct kvaser_usb_net_priv *priv,
u8 cmd_no)
{
struct kvaser_cmd *cmd;
struct kvaser_usb *dev = priv->dev;
int err;
cmd = kcalloc(1, sizeof(struct kvaser_cmd), GFP_ATOMIC);
if (!cmd)
return -ENOMEM;
cmd->header.cmd_no = cmd_no;
kvaser_usb_hydra_set_cmd_dest_he
(cmd, dev->card_data.hydra.channel_to_he[priv->channel]);
kvaser_usb_hydra_set_cmd_transid
(cmd, kvaser_usb_hydra_get_next_transid(dev));
err = kvaser_usb_send_cmd_async(priv, cmd,
kvaser_usb_hydra_cmd_size(cmd));
if (err)
kfree(cmd);
return err;
}
/* This function is used for synchronously waiting on hydra control commands.
* Note: Compared to kvaser_usb_hydra_read_bulk_callback(), we never need to
* handle partial hydra commands. Since hydra control commands are always
* non-extended commands.
*/
static int kvaser_usb_hydra_wait_cmd(const struct kvaser_usb *dev, u8 cmd_no,
struct kvaser_cmd *cmd)
{
void *buf;
int err;
unsigned long timeout = jiffies + msecs_to_jiffies(KVASER_USB_TIMEOUT);
if (cmd->header.cmd_no == CMD_EXTENDED) {
dev_err(&dev->intf->dev, "Wait for CMD_EXTENDED not allowed\n");
return -EINVAL;
}
buf = kzalloc(KVASER_USB_RX_BUFFER_SIZE, GFP_KERNEL);
if (!buf)
return -ENOMEM;
do {
int actual_len = 0;
int pos = 0;
err = kvaser_usb_recv_cmd(dev, buf, KVASER_USB_RX_BUFFER_SIZE,
&actual_len);
if (err < 0)
goto end;
while (pos < actual_len) {
struct kvaser_cmd *tmp_cmd;
size_t cmd_len;
tmp_cmd = buf + pos;
cmd_len = kvaser_usb_hydra_cmd_size(tmp_cmd);
if (pos + cmd_len > actual_len) {
dev_err_ratelimited(&dev->intf->dev,
"Format error\n");
break;
}
if (tmp_cmd->header.cmd_no == cmd_no) {
memcpy(cmd, tmp_cmd, cmd_len);
goto end;
}
pos += cmd_len;
}
} while (time_before(jiffies, timeout));
err = -EINVAL;
end:
kfree(buf);
return err;
}
static int kvaser_usb_hydra_map_channel_resp(struct kvaser_usb *dev,
const struct kvaser_cmd *cmd)
{
u8 he, channel;
u16 transid = kvaser_usb_hydra_get_cmd_transid(cmd);
struct kvaser_usb_dev_card_data_hydra *card_data =
&dev->card_data.hydra;
if (transid > 0x007f || transid < 0x0040) {
dev_err(&dev->intf->dev,
"CMD_MAP_CHANNEL_RESP, invalid transid: 0x%x\n",
transid);
return -EINVAL;
}
switch (transid) {
case KVASER_USB_HYDRA_TRANSID_CANHE:
case KVASER_USB_HYDRA_TRANSID_CANHE + 1:
case KVASER_USB_HYDRA_TRANSID_CANHE + 2:
case KVASER_USB_HYDRA_TRANSID_CANHE + 3:
case KVASER_USB_HYDRA_TRANSID_CANHE + 4:
channel = transid & 0x000f;
he = cmd->map_ch_res.he_addr;
card_data->channel_to_he[channel] = he;
break;
case KVASER_USB_HYDRA_TRANSID_SYSDBG:
card_data->sysdbg_he = cmd->map_ch_res.he_addr;
break;
default:
dev_warn(&dev->intf->dev,
"Unknown CMD_MAP_CHANNEL_RESP transid=0x%x\n",
transid);
break;
}
return 0;
}
static int kvaser_usb_hydra_map_channel(struct kvaser_usb *dev, u16 transid,
u8 channel, const char *name)
{
struct kvaser_cmd *cmd;
int err;
cmd = kcalloc(1, sizeof(struct kvaser_cmd), GFP_KERNEL);
if (!cmd)
return -ENOMEM;
strcpy(cmd->map_ch_req.name, name);
cmd->header.cmd_no = CMD_MAP_CHANNEL_REQ;
kvaser_usb_hydra_set_cmd_dest_he
(cmd, KVASER_USB_HYDRA_HE_ADDRESS_ROUTER);
cmd->map_ch_req.channel = channel;
kvaser_usb_hydra_set_cmd_transid(cmd, transid);
err = kvaser_usb_send_cmd(dev, cmd, kvaser_usb_hydra_cmd_size(cmd));
if (err)
goto end;
err = kvaser_usb_hydra_wait_cmd(dev, CMD_MAP_CHANNEL_RESP, cmd);
if (err)
goto end;
err = kvaser_usb_hydra_map_channel_resp(dev, cmd);
if (err)
goto end;
end:
kfree(cmd);
return err;
}
static int kvaser_usb_hydra_get_single_capability(struct kvaser_usb *dev,
u16 cap_cmd_req, u16 *status)
{
struct kvaser_usb_dev_card_data *card_data = &dev->card_data;
struct kvaser_cmd *cmd;
u32 value = 0;
u32 mask = 0;
u16 cap_cmd_res;
int err;
int i;
cmd = kcalloc(1, sizeof(struct kvaser_cmd), GFP_KERNEL);
if (!cmd)
return -ENOMEM;
cmd->header.cmd_no = CMD_GET_CAPABILITIES_REQ;
cmd->cap_req.cap_cmd = cpu_to_le16(cap_cmd_req);
kvaser_usb_hydra_set_cmd_dest_he(cmd, card_data->hydra.sysdbg_he);
kvaser_usb_hydra_set_cmd_transid
(cmd, kvaser_usb_hydra_get_next_transid(dev));
err = kvaser_usb_send_cmd(dev, cmd, kvaser_usb_hydra_cmd_size(cmd));
if (err)
goto end;
err = kvaser_usb_hydra_wait_cmd(dev, CMD_GET_CAPABILITIES_RESP, cmd);
if (err)
goto end;
*status = le16_to_cpu(cmd->cap_res.status);
if (*status != KVASER_USB_HYDRA_CAP_STAT_OK)
goto end;
cap_cmd_res = le16_to_cpu(cmd->cap_res.cap_cmd);
switch (cap_cmd_res) {
case KVASER_USB_HYDRA_CAP_CMD_LISTEN_MODE:
case KVASER_USB_HYDRA_CAP_CMD_ERR_REPORT:
case KVASER_USB_HYDRA_CAP_CMD_ONE_SHOT:
value = le32_to_cpu(cmd->cap_res.value);
mask = le32_to_cpu(cmd->cap_res.mask);
break;
default:
dev_warn(&dev->intf->dev, "Unknown capability command %u\n",
cap_cmd_res);
break;
}
for (i = 0; i < dev->nchannels; i++) {
if (BIT(i) & (value & mask)) {
switch (cap_cmd_res) {
case KVASER_USB_HYDRA_CAP_CMD_LISTEN_MODE:
card_data->ctrlmode_supported |=
CAN_CTRLMODE_LISTENONLY;
break;
case KVASER_USB_HYDRA_CAP_CMD_ERR_REPORT:
card_data->capabilities |=
KVASER_USB_CAP_BERR_CAP;
break;
case KVASER_USB_HYDRA_CAP_CMD_ONE_SHOT:
card_data->ctrlmode_supported |=
CAN_CTRLMODE_ONE_SHOT;
break;
}
}
}
end:
kfree(cmd);
return err;
}
static void kvaser_usb_hydra_start_chip_reply(const struct kvaser_usb *dev,
const struct kvaser_cmd *cmd)
{
struct kvaser_usb_net_priv *priv;
priv = kvaser_usb_hydra_net_priv_from_cmd(dev, cmd);
if (!priv)
return;
if (completion_done(&priv->start_comp) &&
netif_queue_stopped(priv->netdev)) {
netif_wake_queue(priv->netdev);
} else {
netif_start_queue(priv->netdev);
complete(&priv->start_comp);
}
}
static void kvaser_usb_hydra_stop_chip_reply(const struct kvaser_usb *dev,
const struct kvaser_cmd *cmd)
{
struct kvaser_usb_net_priv *priv;
priv = kvaser_usb_hydra_net_priv_from_cmd(dev, cmd);
if (!priv)
return;
complete(&priv->stop_comp);
}
static void kvaser_usb_hydra_flush_queue_reply(const struct kvaser_usb *dev,
const struct kvaser_cmd *cmd)
{
struct kvaser_usb_net_priv *priv;
priv = kvaser_usb_hydra_net_priv_from_cmd(dev, cmd);
if (!priv)
return;
complete(&priv->flush_comp);
}
static void
kvaser_usb_hydra_bus_status_to_can_state(const struct kvaser_usb_net_priv *priv,
u8 bus_status,
const struct can_berr_counter *bec,
enum can_state *new_state)
{
if (bus_status & KVASER_USB_HYDRA_BUS_BUS_OFF) {
*new_state = CAN_STATE_BUS_OFF;
} else if (bus_status & KVASER_USB_HYDRA_BUS_ERR_PASS) {
*new_state = CAN_STATE_ERROR_PASSIVE;
} else if (bus_status == KVASER_USB_HYDRA_BUS_ERR_ACT) {
if (bec->txerr >= 128 || bec->rxerr >= 128) {
netdev_warn(priv->netdev,
"ERR_ACTIVE but err tx=%u or rx=%u >=128\n",
bec->txerr, bec->rxerr);
*new_state = CAN_STATE_ERROR_PASSIVE;
} else if (bec->txerr >= 96 || bec->rxerr >= 96) {
*new_state = CAN_STATE_ERROR_WARNING;
} else {
*new_state = CAN_STATE_ERROR_ACTIVE;
}
}
}
static void kvaser_usb_hydra_update_state(struct kvaser_usb_net_priv *priv,
u8 bus_status,
const struct can_berr_counter *bec)
{
struct net_device *netdev = priv->netdev;
struct can_frame *cf;
struct sk_buff *skb;
enum can_state new_state, old_state;
old_state = priv->can.state;
kvaser_usb_hydra_bus_status_to_can_state(priv, bus_status, bec,
&new_state);
if (new_state == old_state)
return;
/* Ignore state change if previous state was STOPPED and the new state
* is BUS_OFF. Firmware always report this as BUS_OFF, since firmware
* does not distinguish between BUS_OFF and STOPPED.
*/
if (old_state == CAN_STATE_STOPPED && new_state == CAN_STATE_BUS_OFF)
return;
skb = alloc_can_err_skb(netdev, &cf);
if (skb) {
enum can_state tx_state, rx_state;
tx_state = (bec->txerr >= bec->rxerr) ?
new_state : CAN_STATE_ERROR_ACTIVE;
rx_state = (bec->txerr <= bec->rxerr) ?
new_state : CAN_STATE_ERROR_ACTIVE;
can_change_state(netdev, cf, tx_state, rx_state);
}
if (new_state == CAN_STATE_BUS_OFF && old_state < CAN_STATE_BUS_OFF) {
if (!priv->can.restart_ms)
kvaser_usb_hydra_send_simple_cmd_async
(priv, CMD_STOP_CHIP_REQ);
can_bus_off(netdev);
}
if (!skb) {
netdev_warn(netdev, "No memory left for err_skb\n");
return;
}
if (priv->can.restart_ms &&
old_state >= CAN_STATE_BUS_OFF &&
new_state < CAN_STATE_BUS_OFF)
priv->can.can_stats.restarts++;
cf->data[6] = bec->txerr;
cf->data[7] = bec->rxerr;
netif_rx(skb);
}
static void kvaser_usb_hydra_state_event(const struct kvaser_usb *dev,
const struct kvaser_cmd *cmd)
{
struct kvaser_usb_net_priv *priv;
struct can_berr_counter bec;
u8 bus_status;
priv = kvaser_usb_hydra_net_priv_from_cmd(dev, cmd);
if (!priv)
return;
bus_status = cmd->chip_state_event.bus_status;
bec.txerr = cmd->chip_state_event.tx_err_counter;
bec.rxerr = cmd->chip_state_event.rx_err_counter;
kvaser_usb_hydra_update_state(priv, bus_status, &bec);
priv->bec.txerr = bec.txerr;
priv->bec.rxerr = bec.rxerr;
}
static void kvaser_usb_hydra_error_event_parameter(const struct kvaser_usb *dev,
const struct kvaser_cmd *cmd)
{
/* info1 will contain the offending cmd_no */
switch (le16_to_cpu(cmd->error_event.info1)) {
case CMD_START_CHIP_REQ:
dev_warn(&dev->intf->dev,
"CMD_START_CHIP_REQ error in parameter\n");
break;
case CMD_STOP_CHIP_REQ:
dev_warn(&dev->intf->dev,
"CMD_STOP_CHIP_REQ error in parameter\n");
break;
case CMD_FLUSH_QUEUE:
dev_warn(&dev->intf->dev,
"CMD_FLUSH_QUEUE error in parameter\n");
break;
case CMD_SET_BUSPARAMS_REQ:
dev_warn(&dev->intf->dev,
"Set bittiming failed. Error in parameter\n");
break;
case CMD_SET_BUSPARAMS_FD_REQ:
dev_warn(&dev->intf->dev,
"Set data bittiming failed. Error in parameter\n");
break;
default:
dev_warn(&dev->intf->dev,
"Unhandled parameter error event cmd_no (%u)\n",
le16_to_cpu(cmd->error_event.info1));
break;
}
}
static void kvaser_usb_hydra_error_event(const struct kvaser_usb *dev,
const struct kvaser_cmd *cmd)
{
switch (cmd->error_event.error_code) {
case KVASER_USB_HYDRA_ERROR_EVENT_PARAM:
kvaser_usb_hydra_error_event_parameter(dev, cmd);
break;
case KVASER_USB_HYDRA_ERROR_EVENT_CAN:
/* Wrong channel mapping?! This should never happen!
* info1 will contain the offending cmd_no
*/
dev_err(&dev->intf->dev,
"Received CAN error event for cmd_no (%u)\n",
le16_to_cpu(cmd->error_event.info1));
break;
default:
dev_warn(&dev->intf->dev,
"Unhandled error event (%d)\n",
cmd->error_event.error_code);
break;
}
}
static void
kvaser_usb_hydra_error_frame(struct kvaser_usb_net_priv *priv,
const struct kvaser_err_frame_data *err_frame_data,
ktime_t hwtstamp)
{
struct net_device *netdev = priv->netdev;
struct net_device_stats *stats = &netdev->stats;
struct can_frame *cf;
struct sk_buff *skb;
struct skb_shared_hwtstamps *shhwtstamps;
struct can_berr_counter bec;
enum can_state new_state, old_state;
u8 bus_status;
priv->can.can_stats.bus_error++;
stats->rx_errors++;
bus_status = err_frame_data->bus_status;
bec.txerr = err_frame_data->tx_err_counter;
bec.rxerr = err_frame_data->rx_err_counter;
old_state = priv->can.state;
kvaser_usb_hydra_bus_status_to_can_state(priv, bus_status, &bec,
&new_state);
skb = alloc_can_err_skb(netdev, &cf);
if (new_state != old_state) {
if (skb) {
enum can_state tx_state, rx_state;
tx_state = (bec.txerr >= bec.rxerr) ?
new_state : CAN_STATE_ERROR_ACTIVE;
rx_state = (bec.txerr <= bec.rxerr) ?
new_state : CAN_STATE_ERROR_ACTIVE;
can_change_state(netdev, cf, tx_state, rx_state);
if (priv->can.restart_ms &&
old_state >= CAN_STATE_BUS_OFF &&
new_state < CAN_STATE_BUS_OFF)
cf->can_id |= CAN_ERR_RESTARTED;
}
if (new_state == CAN_STATE_BUS_OFF) {
if (!priv->can.restart_ms)
kvaser_usb_hydra_send_simple_cmd_async
(priv, CMD_STOP_CHIP_REQ);
can_bus_off(netdev);
}
}
if (!skb) {
stats->rx_dropped++;
netdev_warn(netdev, "No memory left for err_skb\n");
return;
}
shhwtstamps = skb_hwtstamps(skb);
shhwtstamps->hwtstamp = hwtstamp;
cf->can_id |= CAN_ERR_BUSERROR;
cf->data[6] = bec.txerr;
cf->data[7] = bec.rxerr;
netif_rx(skb);
priv->bec.txerr = bec.txerr;
priv->bec.rxerr = bec.rxerr;
}
static void kvaser_usb_hydra_one_shot_fail(struct kvaser_usb_net_priv *priv,
const struct kvaser_cmd_ext *cmd)
{
struct net_device *netdev = priv->netdev;
struct net_device_stats *stats = &netdev->stats;
struct can_frame *cf;
struct sk_buff *skb;
u32 flags;
skb = alloc_can_err_skb(netdev, &cf);
if (!skb) {
stats->rx_dropped++;
netdev_warn(netdev, "No memory left for err_skb\n");
return;
}
cf->can_id |= CAN_ERR_BUSERROR;
flags = le32_to_cpu(cmd->tx_ack.flags);
if (flags & KVASER_USB_HYDRA_CF_FLAG_OSM_NACK)
cf->can_id |= CAN_ERR_ACK;
if (flags & KVASER_USB_HYDRA_CF_FLAG_ABL) {
cf->can_id |= CAN_ERR_LOSTARB;
priv->can.can_stats.arbitration_lost++;
}
stats->tx_errors++;
netif_rx(skb);
}
static void kvaser_usb_hydra_tx_acknowledge(const struct kvaser_usb *dev,
const struct kvaser_cmd *cmd)
{
struct kvaser_usb_tx_urb_context *context;
struct kvaser_usb_net_priv *priv;
unsigned long irq_flags;
unsigned int len;
bool one_shot_fail = false;
bool is_err_frame = false;
u16 transid = kvaser_usb_hydra_get_cmd_transid(cmd);
priv = kvaser_usb_hydra_net_priv_from_cmd(dev, cmd);
if (!priv)
return;
if (!netif_device_present(priv->netdev))
return;
if (cmd->header.cmd_no == CMD_EXTENDED) {
struct kvaser_cmd_ext *cmd_ext = (struct kvaser_cmd_ext *)cmd;
u32 flags = le32_to_cpu(cmd_ext->tx_ack.flags);
if (flags & (KVASER_USB_HYDRA_CF_FLAG_OSM_NACK |
KVASER_USB_HYDRA_CF_FLAG_ABL)) {
kvaser_usb_hydra_one_shot_fail(priv, cmd_ext);
one_shot_fail = true;
}
is_err_frame = flags & KVASER_USB_HYDRA_CF_FLAG_TX_ACK &&
flags & KVASER_USB_HYDRA_CF_FLAG_ERROR_FRAME;
}
context = &priv->tx_contexts[transid % dev->max_tx_urbs];
spin_lock_irqsave(&priv->tx_contexts_lock, irq_flags);
len = can_get_echo_skb(priv->netdev, context->echo_index, NULL);
context->echo_index = dev->max_tx_urbs;
--priv->active_tx_contexts;
netif_wake_queue(priv->netdev);
spin_unlock_irqrestore(&priv->tx_contexts_lock, irq_flags);
if (!one_shot_fail && !is_err_frame) {
struct net_device_stats *stats = &priv->netdev->stats;
stats->tx_packets++;
stats->tx_bytes += len;
}
}
static void kvaser_usb_hydra_rx_msg_std(const struct kvaser_usb *dev,
const struct kvaser_cmd *cmd)
{
struct kvaser_usb_net_priv *priv = NULL;
struct can_frame *cf;
struct sk_buff *skb;
struct skb_shared_hwtstamps *shhwtstamps;
struct net_device_stats *stats;
u8 flags;
ktime_t hwtstamp;
priv = kvaser_usb_hydra_net_priv_from_cmd(dev, cmd);
if (!priv)
return;
stats = &priv->netdev->stats;
flags = cmd->rx_can.flags;
hwtstamp = kvaser_usb_hydra_ktime_from_rx_cmd(dev->cfg, cmd);
if (flags & KVASER_USB_HYDRA_CF_FLAG_ERROR_FRAME) {
kvaser_usb_hydra_error_frame(priv, &cmd->rx_can.err_frame_data,
hwtstamp);
return;
}
skb = alloc_can_skb(priv->netdev, &cf);
if (!skb) {
stats->rx_dropped++;
return;
}
shhwtstamps = skb_hwtstamps(skb);
shhwtstamps->hwtstamp = hwtstamp;
cf->can_id = le32_to_cpu(cmd->rx_can.id);
if (cf->can_id & KVASER_USB_HYDRA_EXTENDED_FRAME_ID) {
cf->can_id &= CAN_EFF_MASK;
cf->can_id |= CAN_EFF_FLAG;
} else {
cf->can_id &= CAN_SFF_MASK;
}
if (flags & KVASER_USB_HYDRA_CF_FLAG_OVERRUN)
kvaser_usb_can_rx_over_error(priv->netdev);
cf->len = can_cc_dlc2len(cmd->rx_can.dlc);
if (flags & KVASER_USB_HYDRA_CF_FLAG_REMOTE_FRAME) {
cf->can_id |= CAN_RTR_FLAG;
} else {
memcpy(cf->data, cmd->rx_can.data, cf->len);
stats->rx_bytes += cf->len;
}
stats->rx_packets++;
netif_rx(skb);
}
static void kvaser_usb_hydra_rx_msg_ext(const struct kvaser_usb *dev,
const struct kvaser_cmd_ext *cmd)
{
struct kvaser_cmd *std_cmd = (struct kvaser_cmd *)cmd;
struct kvaser_usb_net_priv *priv;
struct canfd_frame *cf;
struct sk_buff *skb;
struct skb_shared_hwtstamps *shhwtstamps;
struct net_device_stats *stats;
u32 flags;
u8 dlc;
u32 kcan_header;
ktime_t hwtstamp;
priv = kvaser_usb_hydra_net_priv_from_cmd(dev, std_cmd);
if (!priv)
return;
stats = &priv->netdev->stats;
kcan_header = le32_to_cpu(cmd->rx_can.kcan_header);
dlc = (kcan_header & KVASER_USB_KCAN_DATA_DLC_MASK) >>
KVASER_USB_KCAN_DATA_DLC_SHIFT;
flags = le32_to_cpu(cmd->rx_can.flags);
hwtstamp = kvaser_usb_hydra_ktime_from_rx_cmd(dev->cfg, std_cmd);
if (flags & KVASER_USB_HYDRA_CF_FLAG_ERROR_FRAME) {
kvaser_usb_hydra_error_frame(priv, &cmd->rx_can.err_frame_data,
hwtstamp);
return;
}
if (flags & KVASER_USB_HYDRA_CF_FLAG_FDF)
skb = alloc_canfd_skb(priv->netdev, &cf);
else
skb = alloc_can_skb(priv->netdev, (struct can_frame **)&cf);
if (!skb) {
stats->rx_dropped++;
return;
}
shhwtstamps = skb_hwtstamps(skb);
shhwtstamps->hwtstamp = hwtstamp;
cf->can_id = le32_to_cpu(cmd->rx_can.id);
if (flags & KVASER_USB_HYDRA_CF_FLAG_EXTENDED_ID) {
cf->can_id &= CAN_EFF_MASK;
cf->can_id |= CAN_EFF_FLAG;
} else {
cf->can_id &= CAN_SFF_MASK;
}
if (flags & KVASER_USB_HYDRA_CF_FLAG_OVERRUN)
kvaser_usb_can_rx_over_error(priv->netdev);
if (flags & KVASER_USB_HYDRA_CF_FLAG_FDF) {
cf->len = can_fd_dlc2len(dlc);
if (flags & KVASER_USB_HYDRA_CF_FLAG_BRS)
cf->flags |= CANFD_BRS;
if (flags & KVASER_USB_HYDRA_CF_FLAG_ESI)
cf->flags |= CANFD_ESI;
} else {
cf->len = can_cc_dlc2len(dlc);
}
if (flags & KVASER_USB_HYDRA_CF_FLAG_REMOTE_FRAME) {
cf->can_id |= CAN_RTR_FLAG;
} else {
memcpy(cf->data, cmd->rx_can.kcan_payload, cf->len);
stats->rx_bytes += cf->len;
}
stats->rx_packets++;
netif_rx(skb);
}
static void kvaser_usb_hydra_handle_cmd_std(const struct kvaser_usb *dev,
const struct kvaser_cmd *cmd)
{
switch (cmd->header.cmd_no) {
case CMD_START_CHIP_RESP:
kvaser_usb_hydra_start_chip_reply(dev, cmd);
break;
case CMD_STOP_CHIP_RESP:
kvaser_usb_hydra_stop_chip_reply(dev, cmd);
break;
case CMD_FLUSH_QUEUE_RESP:
kvaser_usb_hydra_flush_queue_reply(dev, cmd);
break;
case CMD_CHIP_STATE_EVENT:
kvaser_usb_hydra_state_event(dev, cmd);
break;
case CMD_ERROR_EVENT:
kvaser_usb_hydra_error_event(dev, cmd);
break;
case CMD_TX_ACKNOWLEDGE:
kvaser_usb_hydra_tx_acknowledge(dev, cmd);
break;
case CMD_RX_MESSAGE:
kvaser_usb_hydra_rx_msg_std(dev, cmd);
break;
/* Ignored commands */
case CMD_SET_BUSPARAMS_RESP:
case CMD_SET_BUSPARAMS_FD_RESP:
break;
default:
dev_warn(&dev->intf->dev, "Unhandled command (%d)\n",
cmd->header.cmd_no);
break;
}
}
static void kvaser_usb_hydra_handle_cmd_ext(const struct kvaser_usb *dev,
const struct kvaser_cmd_ext *cmd)
{
switch (cmd->cmd_no_ext) {
case CMD_TX_ACKNOWLEDGE_FD:
kvaser_usb_hydra_tx_acknowledge(dev, (struct kvaser_cmd *)cmd);
break;
case CMD_RX_MESSAGE_FD:
kvaser_usb_hydra_rx_msg_ext(dev, cmd);
break;
default:
dev_warn(&dev->intf->dev, "Unhandled extended command (%d)\n",
cmd->header.cmd_no);
break;
}
}
static void kvaser_usb_hydra_handle_cmd(const struct kvaser_usb *dev,
const struct kvaser_cmd *cmd)
{
if (cmd->header.cmd_no == CMD_EXTENDED)
kvaser_usb_hydra_handle_cmd_ext
(dev, (struct kvaser_cmd_ext *)cmd);
else
kvaser_usb_hydra_handle_cmd_std(dev, cmd);
}
static void *
kvaser_usb_hydra_frame_to_cmd_ext(const struct kvaser_usb_net_priv *priv,
const struct sk_buff *skb, int *cmd_len,
u16 transid)
{
struct kvaser_usb *dev = priv->dev;
struct kvaser_cmd_ext *cmd;
struct canfd_frame *cf = (struct canfd_frame *)skb->data;
u8 dlc = can_fd_len2dlc(cf->len);
u8 nbr_of_bytes = cf->len;
u32 flags;
u32 id;
u32 kcan_id;
u32 kcan_header;
cmd = kcalloc(1, sizeof(struct kvaser_cmd_ext), GFP_ATOMIC);
if (!cmd)
return NULL;
kvaser_usb_hydra_set_cmd_dest_he
((struct kvaser_cmd *)cmd,
dev->card_data.hydra.channel_to_he[priv->channel]);
kvaser_usb_hydra_set_cmd_transid((struct kvaser_cmd *)cmd, transid);
cmd->header.cmd_no = CMD_EXTENDED;
cmd->cmd_no_ext = CMD_TX_CAN_MESSAGE_FD;
*cmd_len = ALIGN(sizeof(struct kvaser_cmd_ext) -
sizeof(cmd->tx_can.kcan_payload) + nbr_of_bytes,
8);
cmd->len = cpu_to_le16(*cmd_len);
cmd->tx_can.databytes = nbr_of_bytes;
cmd->tx_can.dlc = dlc;
if (cf->can_id & CAN_EFF_FLAG) {
id = cf->can_id & CAN_EFF_MASK;
flags = KVASER_USB_HYDRA_CF_FLAG_EXTENDED_ID;
kcan_id = (cf->can_id & CAN_EFF_MASK) |
KVASER_USB_KCAN_DATA_IDE | KVASER_USB_KCAN_DATA_SRR;
} else {
id = cf->can_id & CAN_SFF_MASK;
flags = 0;
kcan_id = cf->can_id & CAN_SFF_MASK;
}
if (cf->can_id & CAN_ERR_FLAG)
flags |= KVASER_USB_HYDRA_CF_FLAG_ERROR_FRAME;
kcan_header = ((dlc << KVASER_USB_KCAN_DATA_DLC_SHIFT) &
KVASER_USB_KCAN_DATA_DLC_MASK) |
KVASER_USB_KCAN_DATA_AREQ |
(priv->can.ctrlmode & CAN_CTRLMODE_ONE_SHOT ?
KVASER_USB_KCAN_DATA_OSM : 0);
if (can_is_canfd_skb(skb)) {
kcan_header |= KVASER_USB_KCAN_DATA_FDF |
(cf->flags & CANFD_BRS ?
KVASER_USB_KCAN_DATA_BRS : 0);
} else {
if (cf->can_id & CAN_RTR_FLAG) {
kcan_id |= KVASER_USB_KCAN_DATA_RTR;
cmd->tx_can.databytes = 0;
flags |= KVASER_USB_HYDRA_CF_FLAG_REMOTE_FRAME;
}
}
cmd->tx_can.kcan_id = cpu_to_le32(kcan_id);
cmd->tx_can.id = cpu_to_le32(id);
cmd->tx_can.flags = cpu_to_le32(flags);
cmd->tx_can.kcan_header = cpu_to_le32(kcan_header);
memcpy(cmd->tx_can.kcan_payload, cf->data, nbr_of_bytes);
return cmd;
}
static void *
kvaser_usb_hydra_frame_to_cmd_std(const struct kvaser_usb_net_priv *priv,
const struct sk_buff *skb, int *cmd_len,
u16 transid)
{
struct kvaser_usb *dev = priv->dev;
struct kvaser_cmd *cmd;
struct can_frame *cf = (struct can_frame *)skb->data;
u32 flags;
u32 id;
cmd = kcalloc(1, sizeof(struct kvaser_cmd), GFP_ATOMIC);
if (!cmd)
return NULL;
kvaser_usb_hydra_set_cmd_dest_he
(cmd, dev->card_data.hydra.channel_to_he[priv->channel]);
kvaser_usb_hydra_set_cmd_transid(cmd, transid);
cmd->header.cmd_no = CMD_TX_CAN_MESSAGE;
*cmd_len = ALIGN(sizeof(struct kvaser_cmd), 8);
if (cf->can_id & CAN_EFF_FLAG) {
id = (cf->can_id & CAN_EFF_MASK);
id |= KVASER_USB_HYDRA_EXTENDED_FRAME_ID;
} else {
id = cf->can_id & CAN_SFF_MASK;
}
cmd->tx_can.dlc = cf->len;
flags = (cf->can_id & CAN_EFF_FLAG ?
KVASER_USB_HYDRA_CF_FLAG_EXTENDED_ID : 0);
if (cf->can_id & CAN_RTR_FLAG)
flags |= KVASER_USB_HYDRA_CF_FLAG_REMOTE_FRAME;
flags |= (cf->can_id & CAN_ERR_FLAG ?
KVASER_USB_HYDRA_CF_FLAG_ERROR_FRAME : 0);
cmd->tx_can.id = cpu_to_le32(id);
cmd->tx_can.flags = flags;
memcpy(cmd->tx_can.data, cf->data, cf->len);
return cmd;
}
static int kvaser_usb_hydra_set_mode(struct net_device *netdev,
enum can_mode mode)
{
int err = 0;
switch (mode) {
case CAN_MODE_START:
/* CAN controller automatically recovers from BUS_OFF */
break;
default:
err = -EOPNOTSUPP;
}
return err;
}
static int kvaser_usb_hydra_set_bittiming(struct net_device *netdev)
{
struct kvaser_cmd *cmd;
struct kvaser_usb_net_priv *priv = netdev_priv(netdev);
struct can_bittiming *bt = &priv->can.bittiming;
struct kvaser_usb *dev = priv->dev;
int tseg1 = bt->prop_seg + bt->phase_seg1;
int tseg2 = bt->phase_seg2;
int sjw = bt->sjw;
int err;
cmd = kcalloc(1, sizeof(struct kvaser_cmd), GFP_KERNEL);
if (!cmd)
return -ENOMEM;
cmd->header.cmd_no = CMD_SET_BUSPARAMS_REQ;
cmd->set_busparams_req.bitrate = cpu_to_le32(bt->bitrate);
cmd->set_busparams_req.sjw = (u8)sjw;
cmd->set_busparams_req.tseg1 = (u8)tseg1;
cmd->set_busparams_req.tseg2 = (u8)tseg2;
cmd->set_busparams_req.nsamples = 1;
kvaser_usb_hydra_set_cmd_dest_he
(cmd, dev->card_data.hydra.channel_to_he[priv->channel]);
kvaser_usb_hydra_set_cmd_transid
(cmd, kvaser_usb_hydra_get_next_transid(dev));
err = kvaser_usb_send_cmd(dev, cmd, kvaser_usb_hydra_cmd_size(cmd));
kfree(cmd);
return err;
}
static int kvaser_usb_hydra_set_data_bittiming(struct net_device *netdev)
{
struct kvaser_cmd *cmd;
struct kvaser_usb_net_priv *priv = netdev_priv(netdev);
struct can_bittiming *dbt = &priv->can.data_bittiming;
struct kvaser_usb *dev = priv->dev;
int tseg1 = dbt->prop_seg + dbt->phase_seg1;
int tseg2 = dbt->phase_seg2;
int sjw = dbt->sjw;
int err;
cmd = kcalloc(1, sizeof(struct kvaser_cmd), GFP_KERNEL);
if (!cmd)
return -ENOMEM;
cmd->header.cmd_no = CMD_SET_BUSPARAMS_FD_REQ;
cmd->set_busparams_req.bitrate_d = cpu_to_le32(dbt->bitrate);
cmd->set_busparams_req.sjw_d = (u8)sjw;
cmd->set_busparams_req.tseg1_d = (u8)tseg1;
cmd->set_busparams_req.tseg2_d = (u8)tseg2;
cmd->set_busparams_req.nsamples_d = 1;
if (priv->can.ctrlmode & CAN_CTRLMODE_FD) {
if (priv->can.ctrlmode & CAN_CTRLMODE_FD_NON_ISO)
cmd->set_busparams_req.canfd_mode =
KVASER_USB_HYDRA_BUS_MODE_NONISO;
else
cmd->set_busparams_req.canfd_mode =
KVASER_USB_HYDRA_BUS_MODE_CANFD_ISO;
}
kvaser_usb_hydra_set_cmd_dest_he
(cmd, dev->card_data.hydra.channel_to_he[priv->channel]);
kvaser_usb_hydra_set_cmd_transid
(cmd, kvaser_usb_hydra_get_next_transid(dev));
err = kvaser_usb_send_cmd(dev, cmd, kvaser_usb_hydra_cmd_size(cmd));
kfree(cmd);
return err;
}
static int kvaser_usb_hydra_get_berr_counter(const struct net_device *netdev,
struct can_berr_counter *bec)
{
struct kvaser_usb_net_priv *priv = netdev_priv(netdev);
int err;
err = kvaser_usb_hydra_send_simple_cmd(priv->dev,
CMD_GET_CHIP_STATE_REQ,
priv->channel);
if (err)
return err;
*bec = priv->bec;
return 0;
}
static int kvaser_usb_hydra_setup_endpoints(struct kvaser_usb *dev)
{
const struct usb_host_interface *iface_desc;
struct usb_endpoint_descriptor *ep;
int i;
iface_desc = dev->intf->cur_altsetting;
for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
ep = &iface_desc->endpoint[i].desc;
if (!dev->bulk_in && usb_endpoint_is_bulk_in(ep) &&
ep->bEndpointAddress == KVASER_USB_HYDRA_BULK_EP_IN_ADDR)
dev->bulk_in = ep;
if (!dev->bulk_out && usb_endpoint_is_bulk_out(ep) &&
ep->bEndpointAddress == KVASER_USB_HYDRA_BULK_EP_OUT_ADDR)
dev->bulk_out = ep;
if (dev->bulk_in && dev->bulk_out)
return 0;
}
return -ENODEV;
}
static int kvaser_usb_hydra_init_card(struct kvaser_usb *dev)
{
int err;
unsigned int i;
struct kvaser_usb_dev_card_data_hydra *card_data =
&dev->card_data.hydra;
card_data->transid = KVASER_USB_HYDRA_MIN_TRANSID;
spin_lock_init(&card_data->transid_lock);
memset(card_data->usb_rx_leftover, 0, KVASER_USB_HYDRA_MAX_CMD_LEN);
card_data->usb_rx_leftover_len = 0;
spin_lock_init(&card_data->usb_rx_leftover_lock);
memset(card_data->channel_to_he, KVASER_USB_HYDRA_HE_ADDRESS_ILLEGAL,
sizeof(card_data->channel_to_he));
card_data->sysdbg_he = 0;
for (i = 0; i < KVASER_USB_MAX_NET_DEVICES; i++) {
err = kvaser_usb_hydra_map_channel
(dev,
(KVASER_USB_HYDRA_TRANSID_CANHE | i),
i, "CAN");
if (err) {
dev_err(&dev->intf->dev,
"CMD_MAP_CHANNEL_REQ failed for CAN%u\n", i);
return err;
}
}
err = kvaser_usb_hydra_map_channel(dev, KVASER_USB_HYDRA_TRANSID_SYSDBG,
0, "SYSDBG");
if (err) {
dev_err(&dev->intf->dev,
"CMD_MAP_CHANNEL_REQ failed for SYSDBG\n");
return err;
}
return 0;
}
static int kvaser_usb_hydra_get_software_info(struct kvaser_usb *dev)
{
struct kvaser_cmd cmd;
int err;
err = kvaser_usb_hydra_send_simple_cmd(dev, CMD_GET_SOFTWARE_INFO_REQ,
-1);
if (err)
return err;
memset(&cmd, 0, sizeof(struct kvaser_cmd));
err = kvaser_usb_hydra_wait_cmd(dev, CMD_GET_SOFTWARE_INFO_RESP, &cmd);
if (err)
return err;
dev->max_tx_urbs = min_t(unsigned int, KVASER_USB_MAX_TX_URBS,
le16_to_cpu(cmd.sw_info.max_outstanding_tx));
return 0;
}
static int kvaser_usb_hydra_get_software_details(struct kvaser_usb *dev)
{
struct kvaser_cmd *cmd;
int err;
u32 flags;
struct kvaser_usb_dev_card_data *card_data = &dev->card_data;
cmd = kcalloc(1, sizeof(struct kvaser_cmd), GFP_KERNEL);
if (!cmd)
return -ENOMEM;
cmd->header.cmd_no = CMD_GET_SOFTWARE_DETAILS_REQ;
cmd->sw_detail_req.use_ext_cmd = 1;
kvaser_usb_hydra_set_cmd_dest_he
(cmd, KVASER_USB_HYDRA_HE_ADDRESS_ILLEGAL);
kvaser_usb_hydra_set_cmd_transid
(cmd, kvaser_usb_hydra_get_next_transid(dev));
err = kvaser_usb_send_cmd(dev, cmd, kvaser_usb_hydra_cmd_size(cmd));
if (err)
goto end;
err = kvaser_usb_hydra_wait_cmd(dev, CMD_GET_SOFTWARE_DETAILS_RESP,
cmd);
if (err)
goto end;
dev->fw_version = le32_to_cpu(cmd->sw_detail_res.sw_version);
flags = le32_to_cpu(cmd->sw_detail_res.sw_flags);
if (flags & KVASER_USB_HYDRA_SW_FLAG_FW_BAD) {
dev_err(&dev->intf->dev,
"Bad firmware, device refuse to run!\n");
err = -EINVAL;
goto end;
}
if (flags & KVASER_USB_HYDRA_SW_FLAG_FW_BETA)
dev_info(&dev->intf->dev, "Beta firmware in use\n");
if (flags & KVASER_USB_HYDRA_SW_FLAG_EXT_CAP)
card_data->capabilities |= KVASER_USB_CAP_EXT_CAP;
if (flags & KVASER_USB_HYDRA_SW_FLAG_EXT_CMD)
card_data->capabilities |= KVASER_USB_HYDRA_CAP_EXT_CMD;
if (flags & KVASER_USB_HYDRA_SW_FLAG_CANFD)
card_data->ctrlmode_supported |= CAN_CTRLMODE_FD;
if (flags & KVASER_USB_HYDRA_SW_FLAG_NONISO)
card_data->ctrlmode_supported |= CAN_CTRLMODE_FD_NON_ISO;
if (flags & KVASER_USB_HYDRA_SW_FLAG_FREQ_80M)
dev->cfg = &kvaser_usb_hydra_dev_cfg_kcan;
else if (flags & KVASER_USB_HYDRA_SW_FLAG_CAN_FREQ_80M)
dev->cfg = &kvaser_usb_hydra_dev_cfg_rt;
else
dev->cfg = &kvaser_usb_hydra_dev_cfg_flexc;
end:
kfree(cmd);
return err;
}
static int kvaser_usb_hydra_get_card_info(struct kvaser_usb *dev)
{
struct kvaser_cmd cmd;
int err;
err = kvaser_usb_hydra_send_simple_cmd(dev, CMD_GET_CARD_INFO_REQ, -1);
if (err)
return err;
memset(&cmd, 0, sizeof(struct kvaser_cmd));
err = kvaser_usb_hydra_wait_cmd(dev, CMD_GET_CARD_INFO_RESP, &cmd);
if (err)
return err;
dev->nchannels = cmd.card_info.nchannels;
if (dev->nchannels > KVASER_USB_MAX_NET_DEVICES)
return -EINVAL;
return 0;
}
static int kvaser_usb_hydra_get_capabilities(struct kvaser_usb *dev)
{
int err;
u16 status;
if (!(dev->card_data.capabilities & KVASER_USB_CAP_EXT_CAP)) {
dev_info(&dev->intf->dev,
"No extended capability support. Upgrade your device.\n");
return 0;
}
err = kvaser_usb_hydra_get_single_capability
(dev,
KVASER_USB_HYDRA_CAP_CMD_LISTEN_MODE,
&status);
if (err)
return err;
if (status)
dev_info(&dev->intf->dev,
"KVASER_USB_HYDRA_CAP_CMD_LISTEN_MODE failed %u\n",
status);
err = kvaser_usb_hydra_get_single_capability
(dev,
KVASER_USB_HYDRA_CAP_CMD_ERR_REPORT,
&status);
if (err)
return err;
if (status)
dev_info(&dev->intf->dev,
"KVASER_USB_HYDRA_CAP_CMD_ERR_REPORT failed %u\n",
status);
err = kvaser_usb_hydra_get_single_capability
(dev, KVASER_USB_HYDRA_CAP_CMD_ONE_SHOT,
&status);
if (err)
return err;
if (status)
dev_info(&dev->intf->dev,
"KVASER_USB_HYDRA_CAP_CMD_ONE_SHOT failed %u\n",
status);
return 0;
}
static int kvaser_usb_hydra_set_opt_mode(const struct kvaser_usb_net_priv *priv)
{
struct kvaser_usb *dev = priv->dev;
struct kvaser_cmd *cmd;
int err;
if ((priv->can.ctrlmode &
(CAN_CTRLMODE_FD | CAN_CTRLMODE_FD_NON_ISO)) ==
CAN_CTRLMODE_FD_NON_ISO) {
netdev_warn(priv->netdev,
"CTRLMODE_FD shall be on if CTRLMODE_FD_NON_ISO is on\n");
return -EINVAL;
}
cmd = kcalloc(1, sizeof(struct kvaser_cmd), GFP_KERNEL);
if (!cmd)
return -ENOMEM;
cmd->header.cmd_no = CMD_SET_DRIVERMODE_REQ;
kvaser_usb_hydra_set_cmd_dest_he
(cmd, dev->card_data.hydra.channel_to_he[priv->channel]);
kvaser_usb_hydra_set_cmd_transid
(cmd, kvaser_usb_hydra_get_next_transid(dev));
if (priv->can.ctrlmode & CAN_CTRLMODE_LISTENONLY)
cmd->set_ctrlmode.mode = KVASER_USB_HYDRA_CTRLMODE_LISTEN;
else
cmd->set_ctrlmode.mode = KVASER_USB_HYDRA_CTRLMODE_NORMAL;
err = kvaser_usb_send_cmd(dev, cmd, kvaser_usb_hydra_cmd_size(cmd));
kfree(cmd);
return err;
}
static int kvaser_usb_hydra_start_chip(struct kvaser_usb_net_priv *priv)
{
int err;
init_completion(&priv->start_comp);
err = kvaser_usb_hydra_send_simple_cmd(priv->dev, CMD_START_CHIP_REQ,
priv->channel);
if (err)
return err;
if (!wait_for_completion_timeout(&priv->start_comp,
msecs_to_jiffies(KVASER_USB_TIMEOUT)))
return -ETIMEDOUT;
return 0;
}
static int kvaser_usb_hydra_stop_chip(struct kvaser_usb_net_priv *priv)
{
int err;
init_completion(&priv->stop_comp);
/* Make sure we do not report invalid BUS_OFF from CMD_CHIP_STATE_EVENT
* see comment in kvaser_usb_hydra_update_state()
*/
priv->can.state = CAN_STATE_STOPPED;
err = kvaser_usb_hydra_send_simple_cmd(priv->dev, CMD_STOP_CHIP_REQ,
priv->channel);
if (err)
return err;
if (!wait_for_completion_timeout(&priv->stop_comp,
msecs_to_jiffies(KVASER_USB_TIMEOUT)))
return -ETIMEDOUT;
return 0;
}
static int kvaser_usb_hydra_flush_queue(struct kvaser_usb_net_priv *priv)
{
int err;
init_completion(&priv->flush_comp);
err = kvaser_usb_hydra_send_simple_cmd(priv->dev, CMD_FLUSH_QUEUE,
priv->channel);
if (err)
return err;
if (!wait_for_completion_timeout(&priv->flush_comp,
msecs_to_jiffies(KVASER_USB_TIMEOUT)))
return -ETIMEDOUT;
return 0;
}
/* A single extended hydra command can be transmitted in multiple transfers
* We have to buffer partial hydra commands, and handle them on next callback.
*/
static void kvaser_usb_hydra_read_bulk_callback(struct kvaser_usb *dev,
void *buf, int len)
{
unsigned long irq_flags;
struct kvaser_cmd *cmd;
int pos = 0;
size_t cmd_len;
struct kvaser_usb_dev_card_data_hydra *card_data =
&dev->card_data.hydra;
int usb_rx_leftover_len;
spinlock_t *usb_rx_leftover_lock = &card_data->usb_rx_leftover_lock;
spin_lock_irqsave(usb_rx_leftover_lock, irq_flags);
usb_rx_leftover_len = card_data->usb_rx_leftover_len;
if (usb_rx_leftover_len) {
int remaining_bytes;
cmd = (struct kvaser_cmd *)card_data->usb_rx_leftover;
cmd_len = kvaser_usb_hydra_cmd_size(cmd);
remaining_bytes = min_t(unsigned int, len,
cmd_len - usb_rx_leftover_len);
/* Make sure we do not overflow usb_rx_leftover */
if (remaining_bytes + usb_rx_leftover_len >
KVASER_USB_HYDRA_MAX_CMD_LEN) {
dev_err(&dev->intf->dev, "Format error\n");
spin_unlock_irqrestore(usb_rx_leftover_lock, irq_flags);
return;
}
memcpy(card_data->usb_rx_leftover + usb_rx_leftover_len, buf,
remaining_bytes);
pos += remaining_bytes;
if (remaining_bytes + usb_rx_leftover_len == cmd_len) {
kvaser_usb_hydra_handle_cmd(dev, cmd);
usb_rx_leftover_len = 0;
} else {
/* Command still not complete */
usb_rx_leftover_len += remaining_bytes;
}
card_data->usb_rx_leftover_len = usb_rx_leftover_len;
}
spin_unlock_irqrestore(usb_rx_leftover_lock, irq_flags);
while (pos < len) {
cmd = buf + pos;
cmd_len = kvaser_usb_hydra_cmd_size(cmd);
if (pos + cmd_len > len) {
/* We got first part of a command */
int leftover_bytes;
leftover_bytes = len - pos;
/* Make sure we do not overflow usb_rx_leftover */
if (leftover_bytes > KVASER_USB_HYDRA_MAX_CMD_LEN) {
dev_err(&dev->intf->dev, "Format error\n");
return;
}
spin_lock_irqsave(usb_rx_leftover_lock, irq_flags);
memcpy(card_data->usb_rx_leftover, buf + pos,
leftover_bytes);
card_data->usb_rx_leftover_len = leftover_bytes;
spin_unlock_irqrestore(usb_rx_leftover_lock, irq_flags);
break;
}
kvaser_usb_hydra_handle_cmd(dev, cmd);
pos += cmd_len;
}
}
static void *
kvaser_usb_hydra_frame_to_cmd(const struct kvaser_usb_net_priv *priv,
const struct sk_buff *skb, int *cmd_len,
u16 transid)
{
void *buf;
if (priv->dev->card_data.capabilities & KVASER_USB_HYDRA_CAP_EXT_CMD)
buf = kvaser_usb_hydra_frame_to_cmd_ext(priv, skb, cmd_len,
transid);
else
buf = kvaser_usb_hydra_frame_to_cmd_std(priv, skb, cmd_len,
transid);
return buf;
}
const struct kvaser_usb_dev_ops kvaser_usb_hydra_dev_ops = {
.dev_set_mode = kvaser_usb_hydra_set_mode,
.dev_set_bittiming = kvaser_usb_hydra_set_bittiming,
.dev_set_data_bittiming = kvaser_usb_hydra_set_data_bittiming,
.dev_get_berr_counter = kvaser_usb_hydra_get_berr_counter,
.dev_setup_endpoints = kvaser_usb_hydra_setup_endpoints,
.dev_init_card = kvaser_usb_hydra_init_card,
.dev_get_software_info = kvaser_usb_hydra_get_software_info,
.dev_get_software_details = kvaser_usb_hydra_get_software_details,
.dev_get_card_info = kvaser_usb_hydra_get_card_info,
.dev_get_capabilities = kvaser_usb_hydra_get_capabilities,
.dev_set_opt_mode = kvaser_usb_hydra_set_opt_mode,
.dev_start_chip = kvaser_usb_hydra_start_chip,
.dev_stop_chip = kvaser_usb_hydra_stop_chip,
.dev_reset_chip = NULL,
.dev_flush_queue = kvaser_usb_hydra_flush_queue,
.dev_read_bulk_callback = kvaser_usb_hydra_read_bulk_callback,
.dev_frame_to_cmd = kvaser_usb_hydra_frame_to_cmd,
};
static const struct kvaser_usb_dev_cfg kvaser_usb_hydra_dev_cfg_kcan = {
.clock = {
.freq = 80 * MEGA /* Hz */,
},
.timestamp_freq = 80,
.bittiming_const = &kvaser_usb_hydra_kcan_bittiming_c,
.data_bittiming_const = &kvaser_usb_hydra_kcan_bittiming_c,
};
static const struct kvaser_usb_dev_cfg kvaser_usb_hydra_dev_cfg_flexc = {
.clock = {
.freq = 24 * MEGA /* Hz */,
},
.timestamp_freq = 1,
.bittiming_const = &kvaser_usb_flexc_bittiming_const,
};
static const struct kvaser_usb_dev_cfg kvaser_usb_hydra_dev_cfg_rt = {
.clock = {
.freq = 80 * MEGA /* Hz */,
},
.timestamp_freq = 24,
.bittiming_const = &kvaser_usb_hydra_rt_bittiming_c,
.data_bittiming_const = &kvaser_usb_hydra_rtd_bittiming_c,
};
|