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
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
|
// SPDX-License-Identifier: GPL-2.0
/*
* USB FTDI client driver for Elan Digital Systems's Uxxx adapters
*
* Copyright(C) 2006 Elan Digital Systems Limited
* http://www.elandigitalsystems.com
*
* Author and Maintainer - Tony Olech - Elan Digital Systems
* tony.olech@elandigitalsystems.com
*
* This driver was written by Tony Olech(tony.olech@elandigitalsystems.com)
* based on various USB client drivers in the 2.6.15 linux kernel
* with constant reference to the 3rd Edition of Linux Device Drivers
* published by O'Reilly
*
* The U132 adapter is a USB to CardBus adapter specifically designed
* for PC cards that contain an OHCI host controller. Typical PC cards
* are the Orange Mobile 3G Option GlobeTrotter Fusion card.
*
* The U132 adapter will *NOT *work with PC cards that do not contain
* an OHCI controller. A simple way to test whether a PC card has an
* OHCI controller as an interface is to insert the PC card directly
* into a laptop(or desktop) with a CardBus slot and if "lspci" shows
* a new USB controller and "lsusb -v" shows a new OHCI Host Controller
* then there is a good chance that the U132 adapter will support the
* PC card.(you also need the specific client driver for the PC card)
*
* Please inform the Author and Maintainer about any PC cards that
* contain OHCI Host Controller and work when directly connected to
* an embedded CardBus slot but do not work when they are connected
* via an ELAN U132 adapter.
*
*/
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
#include <linux/kernel.h>
#include <linux/errno.h>
#include <linux/init.h>
#include <linux/list.h>
#include <linux/ioctl.h>
#include <linux/pci_ids.h>
#include <linux/slab.h>
#include <linux/module.h>
#include <linux/kref.h>
#include <linux/mutex.h>
#include <linux/uaccess.h>
#include <linux/usb.h>
#include <linux/workqueue.h>
#include <linux/platform_device.h>
MODULE_AUTHOR("Tony Olech");
MODULE_DESCRIPTION("FTDI ELAN driver");
MODULE_LICENSE("GPL");
#define INT_MODULE_PARM(n, v) static int n = v;module_param(n, int, 0444)
static bool distrust_firmware = 1;
module_param(distrust_firmware, bool, 0);
MODULE_PARM_DESC(distrust_firmware,
"true to distrust firmware power/overcurrent setup");
extern struct platform_driver u132_platform_driver;
/*
* ftdi_module_lock exists to protect access to global variables
*
*/
static struct mutex ftdi_module_lock;
static int ftdi_instances = 0;
static struct list_head ftdi_static_list;
/*
* end of the global variables protected by ftdi_module_lock
*/
#include "usb_u132.h"
#include <asm/io.h>
#include <linux/usb/hcd.h>
/* FIXME ohci.h is ONLY for internal use by the OHCI driver.
* If you're going to try stuff like this, you need to split
* out shareable stuff (register declarations?) into its own
* file, maybe name <linux/usb/ohci.h>
*/
#include "../host/ohci.h"
/* Define these values to match your devices*/
#define USB_FTDI_ELAN_VENDOR_ID 0x0403
#define USB_FTDI_ELAN_PRODUCT_ID 0xd6ea
/* table of devices that work with this driver*/
static const struct usb_device_id ftdi_elan_table[] = {
{USB_DEVICE(USB_FTDI_ELAN_VENDOR_ID, USB_FTDI_ELAN_PRODUCT_ID)},
{ /* Terminating entry */ }
};
MODULE_DEVICE_TABLE(usb, ftdi_elan_table);
/* only the jtag(firmware upgrade device) interface requires
* a device file and corresponding minor number, but the
* interface is created unconditionally - I suppose it could
* be configured or not according to a module parameter.
* But since we(now) require one interface per device,
* and since it unlikely that a normal installation would
* require more than a couple of elan-ftdi devices, 8 seems
* like a reasonable limit to have here, and if someone
* really requires more than 8 devices, then they can frig the
* code and recompile
*/
#define USB_FTDI_ELAN_MINOR_BASE 192
#define COMMAND_BITS 5
#define COMMAND_SIZE (1<<COMMAND_BITS)
#define COMMAND_MASK (COMMAND_SIZE-1)
struct u132_command {
u8 header;
u16 length;
u8 address;
u8 width;
u32 value;
int follows;
void *buffer;
};
#define RESPOND_BITS 5
#define RESPOND_SIZE (1<<RESPOND_BITS)
#define RESPOND_MASK (RESPOND_SIZE-1)
struct u132_respond {
u8 header;
u8 address;
u32 *value;
int *result;
struct completion wait_completion;
};
struct u132_target {
void *endp;
struct urb *urb;
int toggle_bits;
int error_count;
int condition_code;
int repeat_number;
int halted;
int skipped;
int actual;
int non_null;
int active;
int abandoning;
void (*callback)(void *endp, struct urb *urb, u8 *buf, int len,
int toggle_bits, int error_count, int condition_code,
int repeat_number, int halted, int skipped, int actual,
int non_null);
};
/* Structure to hold all of our device specific stuff*/
struct usb_ftdi {
struct list_head ftdi_list;
struct mutex u132_lock;
int command_next;
int command_head;
struct u132_command command[COMMAND_SIZE];
int respond_next;
int respond_head;
struct u132_respond respond[RESPOND_SIZE];
struct u132_target target[4];
char device_name[16];
unsigned synchronized:1;
unsigned enumerated:1;
unsigned registered:1;
unsigned initialized:1;
unsigned card_ejected:1;
int function;
int sequence_num;
int disconnected;
int gone_away;
int stuck_status;
int status_queue_delay;
struct semaphore sw_lock;
struct usb_device *udev;
struct usb_interface *interface;
struct usb_class_driver *class;
struct delayed_work status_work;
struct delayed_work command_work;
struct delayed_work respond_work;
struct u132_platform_data platform_data;
struct resource resources[0];
struct platform_device platform_dev;
unsigned char *bulk_in_buffer;
size_t bulk_in_size;
size_t bulk_in_last;
size_t bulk_in_left;
__u8 bulk_in_endpointAddr;
__u8 bulk_out_endpointAddr;
struct kref kref;
u32 controlreg;
u8 response[4 + 1024];
int expected;
int received;
int ed_found;
};
#define kref_to_usb_ftdi(d) container_of(d, struct usb_ftdi, kref)
#define platform_device_to_usb_ftdi(d) container_of(d, struct usb_ftdi, \
platform_dev)
static struct usb_driver ftdi_elan_driver;
static void ftdi_elan_delete(struct kref *kref)
{
struct usb_ftdi *ftdi = kref_to_usb_ftdi(kref);
dev_warn(&ftdi->udev->dev, "FREEING ftdi=%p\n", ftdi);
usb_put_dev(ftdi->udev);
ftdi->disconnected += 1;
mutex_lock(&ftdi_module_lock);
list_del_init(&ftdi->ftdi_list);
ftdi_instances -= 1;
mutex_unlock(&ftdi_module_lock);
kfree(ftdi->bulk_in_buffer);
ftdi->bulk_in_buffer = NULL;
kfree(ftdi);
}
static void ftdi_elan_put_kref(struct usb_ftdi *ftdi)
{
kref_put(&ftdi->kref, ftdi_elan_delete);
}
static void ftdi_elan_get_kref(struct usb_ftdi *ftdi)
{
kref_get(&ftdi->kref);
}
static void ftdi_elan_init_kref(struct usb_ftdi *ftdi)
{
kref_init(&ftdi->kref);
}
static void ftdi_status_requeue_work(struct usb_ftdi *ftdi, unsigned int delta)
{
if (!schedule_delayed_work(&ftdi->status_work, delta))
kref_put(&ftdi->kref, ftdi_elan_delete);
}
static void ftdi_status_queue_work(struct usb_ftdi *ftdi, unsigned int delta)
{
if (schedule_delayed_work(&ftdi->status_work, delta))
kref_get(&ftdi->kref);
}
static void ftdi_status_cancel_work(struct usb_ftdi *ftdi)
{
if (cancel_delayed_work_sync(&ftdi->status_work))
kref_put(&ftdi->kref, ftdi_elan_delete);
}
static void ftdi_command_requeue_work(struct usb_ftdi *ftdi, unsigned int delta)
{
if (!schedule_delayed_work(&ftdi->command_work, delta))
kref_put(&ftdi->kref, ftdi_elan_delete);
}
static void ftdi_command_queue_work(struct usb_ftdi *ftdi, unsigned int delta)
{
if (schedule_delayed_work(&ftdi->command_work, delta))
kref_get(&ftdi->kref);
}
static void ftdi_command_cancel_work(struct usb_ftdi *ftdi)
{
if (cancel_delayed_work_sync(&ftdi->command_work))
kref_put(&ftdi->kref, ftdi_elan_delete);
}
static void ftdi_response_requeue_work(struct usb_ftdi *ftdi,
unsigned int delta)
{
if (!schedule_delayed_work(&ftdi->respond_work, delta))
kref_put(&ftdi->kref, ftdi_elan_delete);
}
static void ftdi_respond_queue_work(struct usb_ftdi *ftdi, unsigned int delta)
{
if (schedule_delayed_work(&ftdi->respond_work, delta))
kref_get(&ftdi->kref);
}
static void ftdi_response_cancel_work(struct usb_ftdi *ftdi)
{
if (cancel_delayed_work_sync(&ftdi->respond_work))
kref_put(&ftdi->kref, ftdi_elan_delete);
}
void ftdi_elan_gone_away(struct platform_device *pdev)
{
struct usb_ftdi *ftdi = platform_device_to_usb_ftdi(pdev);
ftdi->gone_away += 1;
ftdi_elan_put_kref(ftdi);
}
EXPORT_SYMBOL_GPL(ftdi_elan_gone_away);
static void ftdi_release_platform_dev(struct device *dev)
{
dev->parent = NULL;
}
static void ftdi_elan_do_callback(struct usb_ftdi *ftdi,
struct u132_target *target, u8 *buffer, int length);
static void ftdi_elan_kick_command_queue(struct usb_ftdi *ftdi);
static void ftdi_elan_kick_respond_queue(struct usb_ftdi *ftdi);
static int ftdi_elan_setupOHCI(struct usb_ftdi *ftdi);
static int ftdi_elan_checkingPCI(struct usb_ftdi *ftdi);
static int ftdi_elan_enumeratePCI(struct usb_ftdi *ftdi);
static int ftdi_elan_synchronize(struct usb_ftdi *ftdi);
static int ftdi_elan_stuck_waiting(struct usb_ftdi *ftdi);
static int ftdi_elan_command_engine(struct usb_ftdi *ftdi);
static int ftdi_elan_respond_engine(struct usb_ftdi *ftdi);
static int ftdi_elan_hcd_init(struct usb_ftdi *ftdi)
{
if (ftdi->platform_dev.dev.parent)
return -EBUSY;
ftdi_elan_get_kref(ftdi);
ftdi->platform_data.potpg = 100;
ftdi->platform_data.reset = NULL;
ftdi->platform_dev.id = ftdi->sequence_num;
ftdi->platform_dev.resource = ftdi->resources;
ftdi->platform_dev.num_resources = ARRAY_SIZE(ftdi->resources);
ftdi->platform_dev.dev.platform_data = &ftdi->platform_data;
ftdi->platform_dev.dev.parent = NULL;
ftdi->platform_dev.dev.release = ftdi_release_platform_dev;
ftdi->platform_dev.dev.dma_mask = NULL;
snprintf(ftdi->device_name, sizeof(ftdi->device_name), "u132_hcd");
ftdi->platform_dev.name = ftdi->device_name;
dev_info(&ftdi->udev->dev, "requesting module '%s'\n", "u132_hcd");
request_module("u132_hcd");
dev_info(&ftdi->udev->dev, "registering '%s'\n",
ftdi->platform_dev.name);
return platform_device_register(&ftdi->platform_dev);
}
static void ftdi_elan_abandon_completions(struct usb_ftdi *ftdi)
{
mutex_lock(&ftdi->u132_lock);
while (ftdi->respond_next > ftdi->respond_head) {
struct u132_respond *respond = &ftdi->respond[RESPOND_MASK &
ftdi->respond_head++];
*respond->result = -ESHUTDOWN;
*respond->value = 0;
complete(&respond->wait_completion);
} mutex_unlock(&ftdi->u132_lock);
}
static void ftdi_elan_abandon_targets(struct usb_ftdi *ftdi)
{
int ed_number = 4;
mutex_lock(&ftdi->u132_lock);
while (ed_number-- > 0) {
struct u132_target *target = &ftdi->target[ed_number];
if (target->active == 1) {
target->condition_code = TD_DEVNOTRESP;
mutex_unlock(&ftdi->u132_lock);
ftdi_elan_do_callback(ftdi, target, NULL, 0);
mutex_lock(&ftdi->u132_lock);
}
}
ftdi->received = 0;
ftdi->expected = 4;
ftdi->ed_found = 0;
mutex_unlock(&ftdi->u132_lock);
}
static void ftdi_elan_flush_targets(struct usb_ftdi *ftdi)
{
int ed_number = 4;
mutex_lock(&ftdi->u132_lock);
while (ed_number-- > 0) {
struct u132_target *target = &ftdi->target[ed_number];
target->abandoning = 1;
wait_1:if (target->active == 1) {
int command_size = ftdi->command_next -
ftdi->command_head;
if (command_size < COMMAND_SIZE) {
struct u132_command *command = &ftdi->command[
COMMAND_MASK & ftdi->command_next];
command->header = 0x80 | (ed_number << 5) | 0x4;
command->length = 0x00;
command->address = 0x00;
command->width = 0x00;
command->follows = 0;
command->value = 0;
command->buffer = &command->value;
ftdi->command_next += 1;
ftdi_elan_kick_command_queue(ftdi);
} else {
mutex_unlock(&ftdi->u132_lock);
msleep(100);
mutex_lock(&ftdi->u132_lock);
goto wait_1;
}
}
wait_2:if (target->active == 1) {
int command_size = ftdi->command_next -
ftdi->command_head;
if (command_size < COMMAND_SIZE) {
struct u132_command *command = &ftdi->command[
COMMAND_MASK & ftdi->command_next];
command->header = 0x90 | (ed_number << 5);
command->length = 0x00;
command->address = 0x00;
command->width = 0x00;
command->follows = 0;
command->value = 0;
command->buffer = &command->value;
ftdi->command_next += 1;
ftdi_elan_kick_command_queue(ftdi);
} else {
mutex_unlock(&ftdi->u132_lock);
msleep(100);
mutex_lock(&ftdi->u132_lock);
goto wait_2;
}
}
}
ftdi->received = 0;
ftdi->expected = 4;
ftdi->ed_found = 0;
mutex_unlock(&ftdi->u132_lock);
}
static void ftdi_elan_cancel_targets(struct usb_ftdi *ftdi)
{
int ed_number = 4;
mutex_lock(&ftdi->u132_lock);
while (ed_number-- > 0) {
struct u132_target *target = &ftdi->target[ed_number];
target->abandoning = 1;
wait:if (target->active == 1) {
int command_size = ftdi->command_next -
ftdi->command_head;
if (command_size < COMMAND_SIZE) {
struct u132_command *command = &ftdi->command[
COMMAND_MASK & ftdi->command_next];
command->header = 0x80 | (ed_number << 5) | 0x4;
command->length = 0x00;
command->address = 0x00;
command->width = 0x00;
command->follows = 0;
command->value = 0;
command->buffer = &command->value;
ftdi->command_next += 1;
ftdi_elan_kick_command_queue(ftdi);
} else {
mutex_unlock(&ftdi->u132_lock);
msleep(100);
mutex_lock(&ftdi->u132_lock);
goto wait;
}
}
}
ftdi->received = 0;
ftdi->expected = 4;
ftdi->ed_found = 0;
mutex_unlock(&ftdi->u132_lock);
}
static void ftdi_elan_kick_command_queue(struct usb_ftdi *ftdi)
{
ftdi_command_queue_work(ftdi, 0);
}
static void ftdi_elan_command_work(struct work_struct *work)
{
struct usb_ftdi *ftdi =
container_of(work, struct usb_ftdi, command_work.work);
if (ftdi->disconnected > 0) {
ftdi_elan_put_kref(ftdi);
return;
} else {
int retval = ftdi_elan_command_engine(ftdi);
if (retval == -ESHUTDOWN) {
ftdi->disconnected += 1;
} else if (retval == -ENODEV) {
ftdi->disconnected += 1;
} else if (retval)
dev_err(&ftdi->udev->dev, "command error %d\n", retval);
ftdi_command_requeue_work(ftdi, msecs_to_jiffies(10));
return;
}
}
static void ftdi_elan_kick_respond_queue(struct usb_ftdi *ftdi)
{
ftdi_respond_queue_work(ftdi, 0);
}
static void ftdi_elan_respond_work(struct work_struct *work)
{
struct usb_ftdi *ftdi =
container_of(work, struct usb_ftdi, respond_work.work);
if (ftdi->disconnected > 0) {
ftdi_elan_put_kref(ftdi);
return;
} else {
int retval = ftdi_elan_respond_engine(ftdi);
if (retval == 0) {
} else if (retval == -ESHUTDOWN) {
ftdi->disconnected += 1;
} else if (retval == -ENODEV) {
ftdi->disconnected += 1;
} else if (retval == -EILSEQ) {
ftdi->disconnected += 1;
} else {
ftdi->disconnected += 1;
dev_err(&ftdi->udev->dev, "respond error %d\n", retval);
}
if (ftdi->disconnected > 0) {
ftdi_elan_abandon_completions(ftdi);
ftdi_elan_abandon_targets(ftdi);
}
ftdi_response_requeue_work(ftdi, msecs_to_jiffies(10));
return;
}
}
/*
* the sw_lock is initially held and will be freed
* after the FTDI has been synchronized
*
*/
static void ftdi_elan_status_work(struct work_struct *work)
{
struct usb_ftdi *ftdi =
container_of(work, struct usb_ftdi, status_work.work);
int work_delay_in_msec = 0;
if (ftdi->disconnected > 0) {
ftdi_elan_put_kref(ftdi);
return;
} else if (ftdi->synchronized == 0) {
down(&ftdi->sw_lock);
if (ftdi_elan_synchronize(ftdi) == 0) {
ftdi->synchronized = 1;
ftdi_command_queue_work(ftdi, 1);
ftdi_respond_queue_work(ftdi, 1);
up(&ftdi->sw_lock);
work_delay_in_msec = 100;
} else {
dev_err(&ftdi->udev->dev, "synchronize failed\n");
up(&ftdi->sw_lock);
work_delay_in_msec = 10 *1000;
}
} else if (ftdi->stuck_status > 0) {
if (ftdi_elan_stuck_waiting(ftdi) == 0) {
ftdi->stuck_status = 0;
ftdi->synchronized = 0;
} else if ((ftdi->stuck_status++ % 60) == 1) {
dev_err(&ftdi->udev->dev, "WRONG type of card inserted - please remove\n");
} else
dev_err(&ftdi->udev->dev, "WRONG type of card inserted - checked %d times\n",
ftdi->stuck_status);
work_delay_in_msec = 100;
} else if (ftdi->enumerated == 0) {
if (ftdi_elan_enumeratePCI(ftdi) == 0) {
ftdi->enumerated = 1;
work_delay_in_msec = 250;
} else
work_delay_in_msec = 1000;
} else if (ftdi->initialized == 0) {
if (ftdi_elan_setupOHCI(ftdi) == 0) {
ftdi->initialized = 1;
work_delay_in_msec = 500;
} else {
dev_err(&ftdi->udev->dev, "initialized failed - trying again in 10 seconds\n");
work_delay_in_msec = 1 *1000;
}
} else if (ftdi->registered == 0) {
work_delay_in_msec = 10;
if (ftdi_elan_hcd_init(ftdi) == 0) {
ftdi->registered = 1;
} else
dev_err(&ftdi->udev->dev, "register failed\n");
work_delay_in_msec = 250;
} else {
if (ftdi_elan_checkingPCI(ftdi) == 0) {
work_delay_in_msec = 250;
} else if (ftdi->controlreg & 0x00400000) {
if (ftdi->gone_away > 0) {
dev_err(&ftdi->udev->dev, "PCI device eject confirmed platform_dev.dev.parent=%p platform_dev.dev=%p\n",
ftdi->platform_dev.dev.parent,
&ftdi->platform_dev.dev);
platform_device_unregister(&ftdi->platform_dev);
ftdi->platform_dev.dev.parent = NULL;
ftdi->registered = 0;
ftdi->enumerated = 0;
ftdi->card_ejected = 0;
ftdi->initialized = 0;
ftdi->gone_away = 0;
} else
ftdi_elan_flush_targets(ftdi);
work_delay_in_msec = 250;
} else {
dev_err(&ftdi->udev->dev, "PCI device has disappeared\n");
ftdi_elan_cancel_targets(ftdi);
work_delay_in_msec = 500;
ftdi->enumerated = 0;
ftdi->initialized = 0;
}
}
if (ftdi->disconnected > 0) {
ftdi_elan_put_kref(ftdi);
return;
} else {
ftdi_status_requeue_work(ftdi,
msecs_to_jiffies(work_delay_in_msec));
return;
}
}
/*
* file_operations for the jtag interface
*
* the usage count for the device is incremented on open()
* and decremented on release()
*/
static int ftdi_elan_open(struct inode *inode, struct file *file)
{
int subminor;
struct usb_interface *interface;
subminor = iminor(inode);
interface = usb_find_interface(&ftdi_elan_driver, subminor);
if (!interface) {
pr_err("can't find device for minor %d\n", subminor);
return -ENODEV;
} else {
struct usb_ftdi *ftdi = usb_get_intfdata(interface);
if (!ftdi) {
return -ENODEV;
} else {
if (down_interruptible(&ftdi->sw_lock)) {
return -EINTR;
} else {
ftdi_elan_get_kref(ftdi);
file->private_data = ftdi;
return 0;
}
}
}
}
static int ftdi_elan_release(struct inode *inode, struct file *file)
{
struct usb_ftdi *ftdi = file->private_data;
if (ftdi == NULL)
return -ENODEV;
up(&ftdi->sw_lock); /* decrement the count on our device */
ftdi_elan_put_kref(ftdi);
return 0;
}
/*
*
* blocking bulk reads are used to get data from the device
*
*/
static ssize_t ftdi_elan_read(struct file *file, char __user *buffer,
size_t count, loff_t *ppos)
{
char data[30 *3 + 4];
char *d = data;
int m = (sizeof(data) - 1) / 3 - 1;
int bytes_read = 0;
int retry_on_empty = 10;
int retry_on_timeout = 5;
struct usb_ftdi *ftdi = file->private_data;
if (ftdi->disconnected > 0) {
return -ENODEV;
}
data[0] = 0;
have:if (ftdi->bulk_in_left > 0) {
if (count-- > 0) {
char *p = ++ftdi->bulk_in_last + ftdi->bulk_in_buffer;
ftdi->bulk_in_left -= 1;
if (bytes_read < m) {
d += sprintf(d, " %02X", 0x000000FF & *p);
} else if (bytes_read > m) {
} else
d += sprintf(d, " ..");
if (copy_to_user(buffer++, p, 1)) {
return -EFAULT;
} else {
bytes_read += 1;
goto have;
}
} else
return bytes_read;
}
more:if (count > 0) {
int packet_bytes = 0;
int retval = usb_bulk_msg(ftdi->udev,
usb_rcvbulkpipe(ftdi->udev, ftdi->bulk_in_endpointAddr),
ftdi->bulk_in_buffer, ftdi->bulk_in_size,
&packet_bytes, 50);
if (packet_bytes > 2) {
ftdi->bulk_in_left = packet_bytes - 2;
ftdi->bulk_in_last = 1;
goto have;
} else if (retval == -ETIMEDOUT) {
if (retry_on_timeout-- > 0) {
goto more;
} else if (bytes_read > 0) {
return bytes_read;
} else
return retval;
} else if (retval == 0) {
if (retry_on_empty-- > 0) {
goto more;
} else
return bytes_read;
} else
return retval;
} else
return bytes_read;
}
static void ftdi_elan_write_bulk_callback(struct urb *urb)
{
struct usb_ftdi *ftdi = urb->context;
int status = urb->status;
if (status && !(status == -ENOENT || status == -ECONNRESET ||
status == -ESHUTDOWN)) {
dev_err(&ftdi->udev->dev,
"urb=%p write bulk status received: %d\n", urb, status);
}
usb_free_coherent(urb->dev, urb->transfer_buffer_length,
urb->transfer_buffer, urb->transfer_dma);
}
static int fill_buffer_with_all_queued_commands(struct usb_ftdi *ftdi,
char *buf, int command_size, int total_size)
{
int ed_commands = 0;
int b = 0;
int I = command_size;
int i = ftdi->command_head;
while (I-- > 0) {
struct u132_command *command = &ftdi->command[COMMAND_MASK &
i++];
int F = command->follows;
u8 *f = command->buffer;
if (command->header & 0x80) {
ed_commands |= 1 << (0x3 & (command->header >> 5));
}
buf[b++] = command->header;
buf[b++] = (command->length >> 0) & 0x00FF;
buf[b++] = (command->length >> 8) & 0x00FF;
buf[b++] = command->address;
buf[b++] = command->width;
while (F-- > 0) {
buf[b++] = *f++;
}
}
return ed_commands;
}
static int ftdi_elan_total_command_size(struct usb_ftdi *ftdi, int command_size)
{
int total_size = 0;
int I = command_size;
int i = ftdi->command_head;
while (I-- > 0) {
struct u132_command *command = &ftdi->command[COMMAND_MASK &
i++];
total_size += 5 + command->follows;
} return total_size;
}
static int ftdi_elan_command_engine(struct usb_ftdi *ftdi)
{
int retval;
char *buf;
int ed_commands;
int total_size;
struct urb *urb;
int command_size = ftdi->command_next - ftdi->command_head;
if (command_size == 0)
return 0;
total_size = ftdi_elan_total_command_size(ftdi, command_size);
urb = usb_alloc_urb(0, GFP_KERNEL);
if (!urb)
return -ENOMEM;
buf = usb_alloc_coherent(ftdi->udev, total_size, GFP_KERNEL,
&urb->transfer_dma);
if (!buf) {
dev_err(&ftdi->udev->dev, "could not get a buffer to write %d commands totaling %d bytes to the Uxxx\n",
command_size, total_size);
usb_free_urb(urb);
return -ENOMEM;
}
ed_commands = fill_buffer_with_all_queued_commands(ftdi, buf,
command_size, total_size);
usb_fill_bulk_urb(urb, ftdi->udev, usb_sndbulkpipe(ftdi->udev,
ftdi->bulk_out_endpointAddr), buf, total_size,
ftdi_elan_write_bulk_callback, ftdi);
urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
if (ed_commands) {
char diag[40 *3 + 4];
char *d = diag;
int m = total_size;
u8 *c = buf;
int s = (sizeof(diag) - 1) / 3;
diag[0] = 0;
while (s-- > 0 && m-- > 0) {
if (s > 0 || m == 0) {
d += sprintf(d, " %02X", *c++);
} else
d += sprintf(d, " ..");
}
}
retval = usb_submit_urb(urb, GFP_KERNEL);
if (retval) {
dev_err(&ftdi->udev->dev, "failed %d to submit urb %p to write %d commands totaling %d bytes to the Uxxx\n",
retval, urb, command_size, total_size);
usb_free_coherent(ftdi->udev, total_size, buf, urb->transfer_dma);
usb_free_urb(urb);
return retval;
}
usb_free_urb(urb); /* release our reference to this urb,
the USB core will eventually free it entirely */
ftdi->command_head += command_size;
ftdi_elan_kick_respond_queue(ftdi);
return 0;
}
static void ftdi_elan_do_callback(struct usb_ftdi *ftdi,
struct u132_target *target, u8 *buffer, int length)
{
struct urb *urb = target->urb;
int halted = target->halted;
int skipped = target->skipped;
int actual = target->actual;
int non_null = target->non_null;
int toggle_bits = target->toggle_bits;
int error_count = target->error_count;
int condition_code = target->condition_code;
int repeat_number = target->repeat_number;
void (*callback) (void *, struct urb *, u8 *, int, int, int, int, int,
int, int, int, int) = target->callback;
target->active -= 1;
target->callback = NULL;
(*callback) (target->endp, urb, buffer, length, toggle_bits,
error_count, condition_code, repeat_number, halted, skipped,
actual, non_null);
}
static char *have_ed_set_response(struct usb_ftdi *ftdi,
struct u132_target *target, u16 ed_length, int ed_number, int ed_type,
char *b)
{
int payload = (ed_length >> 0) & 0x07FF;
mutex_lock(&ftdi->u132_lock);
target->actual = 0;
target->non_null = (ed_length >> 15) & 0x0001;
target->repeat_number = (ed_length >> 11) & 0x000F;
if (ed_type == 0x02 || ed_type == 0x03) {
if (payload == 0 || target->abandoning > 0) {
target->abandoning = 0;
mutex_unlock(&ftdi->u132_lock);
ftdi_elan_do_callback(ftdi, target, 4 + ftdi->response,
payload);
ftdi->received = 0;
ftdi->expected = 4;
ftdi->ed_found = 0;
return ftdi->response;
} else {
ftdi->expected = 4 + payload;
ftdi->ed_found = 1;
mutex_unlock(&ftdi->u132_lock);
return b;
}
} else {
target->abandoning = 0;
mutex_unlock(&ftdi->u132_lock);
ftdi_elan_do_callback(ftdi, target, 4 + ftdi->response,
payload);
ftdi->received = 0;
ftdi->expected = 4;
ftdi->ed_found = 0;
return ftdi->response;
}
}
static char *have_ed_get_response(struct usb_ftdi *ftdi,
struct u132_target *target, u16 ed_length, int ed_number, int ed_type,
char *b)
{
mutex_lock(&ftdi->u132_lock);
target->condition_code = TD_DEVNOTRESP;
target->actual = (ed_length >> 0) & 0x01FF;
target->non_null = (ed_length >> 15) & 0x0001;
target->repeat_number = (ed_length >> 11) & 0x000F;
mutex_unlock(&ftdi->u132_lock);
if (target->active)
ftdi_elan_do_callback(ftdi, target, NULL, 0);
target->abandoning = 0;
ftdi->received = 0;
ftdi->expected = 4;
ftdi->ed_found = 0;
return ftdi->response;
}
/*
* The engine tries to empty the FTDI fifo
*
* all responses found in the fifo data are dispatched thus
* the response buffer can only ever hold a maximum sized
* response from the Uxxx.
*
*/
static int ftdi_elan_respond_engine(struct usb_ftdi *ftdi)
{
u8 *b = ftdi->response + ftdi->received;
int bytes_read = 0;
int retry_on_empty = 1;
int retry_on_timeout = 3;
int empty_packets = 0;
read:{
int packet_bytes = 0;
int retval = usb_bulk_msg(ftdi->udev,
usb_rcvbulkpipe(ftdi->udev, ftdi->bulk_in_endpointAddr),
ftdi->bulk_in_buffer, ftdi->bulk_in_size,
&packet_bytes, 500);
char diag[30 *3 + 4];
char *d = diag;
int m = packet_bytes;
u8 *c = ftdi->bulk_in_buffer;
int s = (sizeof(diag) - 1) / 3;
diag[0] = 0;
while (s-- > 0 && m-- > 0) {
if (s > 0 || m == 0) {
d += sprintf(d, " %02X", *c++);
} else
d += sprintf(d, " ..");
}
if (packet_bytes > 2) {
ftdi->bulk_in_left = packet_bytes - 2;
ftdi->bulk_in_last = 1;
goto have;
} else if (retval == -ETIMEDOUT) {
if (retry_on_timeout-- > 0) {
dev_err(&ftdi->udev->dev, "TIMED OUT with packet_bytes = %d with total %d bytes%s\n",
packet_bytes, bytes_read, diag);
goto more;
} else if (bytes_read > 0) {
dev_err(&ftdi->udev->dev, "ONLY %d bytes%s\n",
bytes_read, diag);
return -ENOMEM;
} else {
dev_err(&ftdi->udev->dev, "TIMED OUT with packet_bytes = %d with total %d bytes%s\n",
packet_bytes, bytes_read, diag);
return -ENOMEM;
}
} else if (retval == -EILSEQ) {
dev_err(&ftdi->udev->dev, "error = %d with packet_bytes = %d with total %d bytes%s\n",
retval, packet_bytes, bytes_read, diag);
return retval;
} else if (retval) {
dev_err(&ftdi->udev->dev, "error = %d with packet_bytes = %d with total %d bytes%s\n",
retval, packet_bytes, bytes_read, diag);
return retval;
} else if (packet_bytes == 2) {
unsigned char s0 = ftdi->bulk_in_buffer[0];
unsigned char s1 = ftdi->bulk_in_buffer[1];
empty_packets += 1;
if (s0 == 0x31 && s1 == 0x60) {
if (retry_on_empty-- > 0) {
goto more;
} else
return 0;
} else if (s0 == 0x31 && s1 == 0x00) {
if (retry_on_empty-- > 0) {
goto more;
} else
return 0;
} else {
if (retry_on_empty-- > 0) {
goto more;
} else
return 0;
}
} else if (packet_bytes == 1) {
if (retry_on_empty-- > 0) {
goto more;
} else
return 0;
} else {
if (retry_on_empty-- > 0) {
goto more;
} else
return 0;
}
}
more:{
goto read;
}
have:if (ftdi->bulk_in_left > 0) {
u8 c = ftdi->bulk_in_buffer[++ftdi->bulk_in_last];
bytes_read += 1;
ftdi->bulk_in_left -= 1;
if (ftdi->received == 0 && c == 0xFF) {
goto have;
} else
*b++ = c;
if (++ftdi->received < ftdi->expected) {
goto have;
} else if (ftdi->ed_found) {
int ed_number = (ftdi->response[0] >> 5) & 0x03;
u16 ed_length = (ftdi->response[2] << 8) |
ftdi->response[1];
struct u132_target *target = &ftdi->target[ed_number];
int payload = (ed_length >> 0) & 0x07FF;
char diag[30 *3 + 4];
char *d = diag;
int m = payload;
u8 *c = 4 + ftdi->response;
int s = (sizeof(diag) - 1) / 3;
diag[0] = 0;
while (s-- > 0 && m-- > 0) {
if (s > 0 || m == 0) {
d += sprintf(d, " %02X", *c++);
} else
d += sprintf(d, " ..");
}
ftdi_elan_do_callback(ftdi, target, 4 + ftdi->response,
payload);
ftdi->received = 0;
ftdi->expected = 4;
ftdi->ed_found = 0;
b = ftdi->response;
goto have;
} else if (ftdi->expected == 8) {
u8 buscmd;
int respond_head = ftdi->respond_head++;
struct u132_respond *respond = &ftdi->respond[
RESPOND_MASK & respond_head];
u32 data = ftdi->response[7];
data <<= 8;
data |= ftdi->response[6];
data <<= 8;
data |= ftdi->response[5];
data <<= 8;
data |= ftdi->response[4];
*respond->value = data;
*respond->result = 0;
complete(&respond->wait_completion);
ftdi->received = 0;
ftdi->expected = 4;
ftdi->ed_found = 0;
b = ftdi->response;
buscmd = (ftdi->response[0] >> 0) & 0x0F;
if (buscmd == 0x00) {
} else if (buscmd == 0x02) {
} else if (buscmd == 0x06) {
} else if (buscmd == 0x0A) {
} else
dev_err(&ftdi->udev->dev, "Uxxx unknown(%0X) value = %08X\n",
buscmd, data);
goto have;
} else {
if ((ftdi->response[0] & 0x80) == 0x00) {
ftdi->expected = 8;
goto have;
} else {
int ed_number = (ftdi->response[0] >> 5) & 0x03;
int ed_type = (ftdi->response[0] >> 0) & 0x03;
u16 ed_length = (ftdi->response[2] << 8) |
ftdi->response[1];
struct u132_target *target = &ftdi->target[
ed_number];
target->halted = (ftdi->response[0] >> 3) &
0x01;
target->skipped = (ftdi->response[0] >> 2) &
0x01;
target->toggle_bits = (ftdi->response[3] >> 6)
& 0x03;
target->error_count = (ftdi->response[3] >> 4)
& 0x03;
target->condition_code = (ftdi->response[
3] >> 0) & 0x0F;
if ((ftdi->response[0] & 0x10) == 0x00) {
b = have_ed_set_response(ftdi, target,
ed_length, ed_number, ed_type,
b);
goto have;
} else {
b = have_ed_get_response(ftdi, target,
ed_length, ed_number, ed_type,
b);
goto have;
}
}
}
} else
goto more;
}
/*
* create a urb, and a buffer for it, and copy the data to the urb
*
*/
static ssize_t ftdi_elan_write(struct file *file,
const char __user *user_buffer, size_t count,
loff_t *ppos)
{
int retval = 0;
struct urb *urb;
char *buf;
struct usb_ftdi *ftdi = file->private_data;
if (ftdi->disconnected > 0) {
return -ENODEV;
}
if (count == 0) {
goto exit;
}
urb = usb_alloc_urb(0, GFP_KERNEL);
if (!urb) {
retval = -ENOMEM;
goto error_1;
}
buf = usb_alloc_coherent(ftdi->udev, count, GFP_KERNEL,
&urb->transfer_dma);
if (!buf) {
retval = -ENOMEM;
goto error_2;
}
if (copy_from_user(buf, user_buffer, count)) {
retval = -EFAULT;
goto error_3;
}
usb_fill_bulk_urb(urb, ftdi->udev, usb_sndbulkpipe(ftdi->udev,
ftdi->bulk_out_endpointAddr), buf, count,
ftdi_elan_write_bulk_callback, ftdi);
urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
retval = usb_submit_urb(urb, GFP_KERNEL);
if (retval) {
dev_err(&ftdi->udev->dev,
"failed submitting write urb, error %d\n", retval);
goto error_3;
}
usb_free_urb(urb);
exit:
return count;
error_3:
usb_free_coherent(ftdi->udev, count, buf, urb->transfer_dma);
error_2:
usb_free_urb(urb);
error_1:
return retval;
}
static const struct file_operations ftdi_elan_fops = {
.owner = THIS_MODULE,
.llseek = no_llseek,
.read = ftdi_elan_read,
.write = ftdi_elan_write,
.open = ftdi_elan_open,
.release = ftdi_elan_release,
};
/*
* usb class driver info in order to get a minor number from the usb core,
* and to have the device registered with the driver core
*/
static struct usb_class_driver ftdi_elan_jtag_class = {
.name = "ftdi-%d-jtag",
.fops = &ftdi_elan_fops,
.minor_base = USB_FTDI_ELAN_MINOR_BASE,
};
/*
* the following definitions are for the
* ELAN FPGA state machgine processor that
* lies on the other side of the FTDI chip
*/
#define cPCIu132rd 0x0
#define cPCIu132wr 0x1
#define cPCIiord 0x2
#define cPCIiowr 0x3
#define cPCImemrd 0x6
#define cPCImemwr 0x7
#define cPCIcfgrd 0xA
#define cPCIcfgwr 0xB
#define cPCInull 0xF
#define cU132cmd_status 0x0
#define cU132flash 0x1
#define cPIDsetup 0x0
#define cPIDout 0x1
#define cPIDin 0x2
#define cPIDinonce 0x3
#define cCCnoerror 0x0
#define cCCcrc 0x1
#define cCCbitstuff 0x2
#define cCCtoggle 0x3
#define cCCstall 0x4
#define cCCnoresp 0x5
#define cCCbadpid1 0x6
#define cCCbadpid2 0x7
#define cCCdataoverrun 0x8
#define cCCdataunderrun 0x9
#define cCCbuffoverrun 0xC
#define cCCbuffunderrun 0xD
#define cCCnotaccessed 0xF
static int ftdi_elan_write_reg(struct usb_ftdi *ftdi, u32 data)
{
wait:if (ftdi->disconnected > 0) {
return -ENODEV;
} else {
int command_size;
mutex_lock(&ftdi->u132_lock);
command_size = ftdi->command_next - ftdi->command_head;
if (command_size < COMMAND_SIZE) {
struct u132_command *command = &ftdi->command[
COMMAND_MASK & ftdi->command_next];
command->header = 0x00 | cPCIu132wr;
command->length = 0x04;
command->address = 0x00;
command->width = 0x00;
command->follows = 4;
command->value = data;
command->buffer = &command->value;
ftdi->command_next += 1;
ftdi_elan_kick_command_queue(ftdi);
mutex_unlock(&ftdi->u132_lock);
return 0;
} else {
mutex_unlock(&ftdi->u132_lock);
msleep(100);
goto wait;
}
}
}
static int ftdi_elan_write_config(struct usb_ftdi *ftdi, int config_offset,
u8 width, u32 data)
{
u8 addressofs = config_offset / 4;
wait:if (ftdi->disconnected > 0) {
return -ENODEV;
} else {
int command_size;
mutex_lock(&ftdi->u132_lock);
command_size = ftdi->command_next - ftdi->command_head;
if (command_size < COMMAND_SIZE) {
struct u132_command *command = &ftdi->command[
COMMAND_MASK & ftdi->command_next];
command->header = 0x00 | (cPCIcfgwr & 0x0F);
command->length = 0x04;
command->address = addressofs;
command->width = 0x00 | (width & 0x0F);
command->follows = 4;
command->value = data;
command->buffer = &command->value;
ftdi->command_next += 1;
ftdi_elan_kick_command_queue(ftdi);
mutex_unlock(&ftdi->u132_lock);
return 0;
} else {
mutex_unlock(&ftdi->u132_lock);
msleep(100);
goto wait;
}
}
}
static int ftdi_elan_write_pcimem(struct usb_ftdi *ftdi, int mem_offset,
u8 width, u32 data)
{
u8 addressofs = mem_offset / 4;
wait:if (ftdi->disconnected > 0) {
return -ENODEV;
} else {
int command_size;
mutex_lock(&ftdi->u132_lock);
command_size = ftdi->command_next - ftdi->command_head;
if (command_size < COMMAND_SIZE) {
struct u132_command *command = &ftdi->command[
COMMAND_MASK & ftdi->command_next];
command->header = 0x00 | (cPCImemwr & 0x0F);
command->length = 0x04;
command->address = addressofs;
command->width = 0x00 | (width & 0x0F);
command->follows = 4;
command->value = data;
command->buffer = &command->value;
ftdi->command_next += 1;
ftdi_elan_kick_command_queue(ftdi);
mutex_unlock(&ftdi->u132_lock);
return 0;
} else {
mutex_unlock(&ftdi->u132_lock);
msleep(100);
goto wait;
}
}
}
int usb_ftdi_elan_write_pcimem(struct platform_device *pdev, int mem_offset,
u8 width, u32 data)
{
struct usb_ftdi *ftdi = platform_device_to_usb_ftdi(pdev);
return ftdi_elan_write_pcimem(ftdi, mem_offset, width, data);
}
EXPORT_SYMBOL_GPL(usb_ftdi_elan_write_pcimem);
static int ftdi_elan_read_reg(struct usb_ftdi *ftdi, u32 *data)
{
wait:if (ftdi->disconnected > 0) {
return -ENODEV;
} else {
int command_size;
int respond_size;
mutex_lock(&ftdi->u132_lock);
command_size = ftdi->command_next - ftdi->command_head;
respond_size = ftdi->respond_next - ftdi->respond_head;
if (command_size < COMMAND_SIZE && respond_size < RESPOND_SIZE)
{
struct u132_command *command = &ftdi->command[
COMMAND_MASK & ftdi->command_next];
struct u132_respond *respond = &ftdi->respond[
RESPOND_MASK & ftdi->respond_next];
int result = -ENODEV;
respond->result = &result;
respond->header = command->header = 0x00 | cPCIu132rd;
command->length = 0x04;
respond->address = command->address = cU132cmd_status;
command->width = 0x00;
command->follows = 0;
command->value = 0;
command->buffer = NULL;
respond->value = data;
init_completion(&respond->wait_completion);
ftdi->command_next += 1;
ftdi->respond_next += 1;
ftdi_elan_kick_command_queue(ftdi);
mutex_unlock(&ftdi->u132_lock);
wait_for_completion(&respond->wait_completion);
return result;
} else {
mutex_unlock(&ftdi->u132_lock);
msleep(100);
goto wait;
}
}
}
static int ftdi_elan_read_config(struct usb_ftdi *ftdi, int config_offset,
u8 width, u32 *data)
{
u8 addressofs = config_offset / 4;
wait:if (ftdi->disconnected > 0) {
return -ENODEV;
} else {
int command_size;
int respond_size;
mutex_lock(&ftdi->u132_lock);
command_size = ftdi->command_next - ftdi->command_head;
respond_size = ftdi->respond_next - ftdi->respond_head;
if (command_size < COMMAND_SIZE && respond_size < RESPOND_SIZE)
{
struct u132_command *command = &ftdi->command[
COMMAND_MASK & ftdi->command_next];
struct u132_respond *respond = &ftdi->respond[
RESPOND_MASK & ftdi->respond_next];
int result = -ENODEV;
respond->result = &result;
respond->header = command->header = 0x00 | (cPCIcfgrd &
0x0F);
command->length = 0x04;
respond->address = command->address = addressofs;
command->width = 0x00 | (width & 0x0F);
command->follows = 0;
command->value = 0;
command->buffer = NULL;
respond->value = data;
init_completion(&respond->wait_completion);
ftdi->command_next += 1;
ftdi->respond_next += 1;
ftdi_elan_kick_command_queue(ftdi);
mutex_unlock(&ftdi->u132_lock);
wait_for_completion(&respond->wait_completion);
return result;
} else {
mutex_unlock(&ftdi->u132_lock);
msleep(100);
goto wait;
}
}
}
static int ftdi_elan_read_pcimem(struct usb_ftdi *ftdi, int mem_offset,
u8 width, u32 *data)
{
u8 addressofs = mem_offset / 4;
wait:if (ftdi->disconnected > 0) {
return -ENODEV;
} else {
int command_size;
int respond_size;
mutex_lock(&ftdi->u132_lock);
command_size = ftdi->command_next - ftdi->command_head;
respond_size = ftdi->respond_next - ftdi->respond_head;
if (command_size < COMMAND_SIZE && respond_size < RESPOND_SIZE)
{
struct u132_command *command = &ftdi->command[
COMMAND_MASK & ftdi->command_next];
struct u132_respond *respond = &ftdi->respond[
RESPOND_MASK & ftdi->respond_next];
int result = -ENODEV;
respond->result = &result;
respond->header = command->header = 0x00 | (cPCImemrd &
0x0F);
command->length = 0x04;
respond->address = command->address = addressofs;
command->width = 0x00 | (width & 0x0F);
command->follows = 0;
command->value = 0;
command->buffer = NULL;
respond->value = data;
init_completion(&respond->wait_completion);
ftdi->command_next += 1;
ftdi->respond_next += 1;
ftdi_elan_kick_command_queue(ftdi);
mutex_unlock(&ftdi->u132_lock);
wait_for_completion(&respond->wait_completion);
return result;
} else {
mutex_unlock(&ftdi->u132_lock);
msleep(100);
goto wait;
}
}
}
int usb_ftdi_elan_read_pcimem(struct platform_device *pdev, int mem_offset,
u8 width, u32 *data)
{
struct usb_ftdi *ftdi = platform_device_to_usb_ftdi(pdev);
if (ftdi->initialized == 0) {
return -ENODEV;
} else
return ftdi_elan_read_pcimem(ftdi, mem_offset, width, data);
}
EXPORT_SYMBOL_GPL(usb_ftdi_elan_read_pcimem);
static int ftdi_elan_edset_setup(struct usb_ftdi *ftdi, u8 ed_number,
void *endp, struct urb *urb, u8 address, u8 ep_number, u8 toggle_bits,
void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
int toggle_bits, int error_count, int condition_code, int repeat_number,
int halted, int skipped, int actual, int non_null))
{
u8 ed = ed_number - 1;
wait:if (ftdi->disconnected > 0) {
return -ENODEV;
} else if (ftdi->initialized == 0) {
return -ENODEV;
} else {
int command_size;
mutex_lock(&ftdi->u132_lock);
command_size = ftdi->command_next - ftdi->command_head;
if (command_size < COMMAND_SIZE) {
struct u132_target *target = &ftdi->target[ed];
struct u132_command *command = &ftdi->command[
COMMAND_MASK & ftdi->command_next];
command->header = 0x80 | (ed << 5);
command->length = 0x8007;
command->address = (toggle_bits << 6) | (ep_number << 2)
| (address << 0);
command->width = usb_maxpacket(urb->dev, urb->pipe,
usb_pipeout(urb->pipe));
command->follows = 8;
command->value = 0;
command->buffer = urb->setup_packet;
target->callback = callback;
target->endp = endp;
target->urb = urb;
target->active = 1;
ftdi->command_next += 1;
ftdi_elan_kick_command_queue(ftdi);
mutex_unlock(&ftdi->u132_lock);
return 0;
} else {
mutex_unlock(&ftdi->u132_lock);
msleep(100);
goto wait;
}
}
}
int usb_ftdi_elan_edset_setup(struct platform_device *pdev, u8 ed_number,
void *endp, struct urb *urb, u8 address, u8 ep_number, u8 toggle_bits,
void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
int toggle_bits, int error_count, int condition_code, int repeat_number,
int halted, int skipped, int actual, int non_null))
{
struct usb_ftdi *ftdi = platform_device_to_usb_ftdi(pdev);
return ftdi_elan_edset_setup(ftdi, ed_number, endp, urb, address,
ep_number, toggle_bits, callback);
}
EXPORT_SYMBOL_GPL(usb_ftdi_elan_edset_setup);
static int ftdi_elan_edset_input(struct usb_ftdi *ftdi, u8 ed_number,
void *endp, struct urb *urb, u8 address, u8 ep_number, u8 toggle_bits,
void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
int toggle_bits, int error_count, int condition_code, int repeat_number,
int halted, int skipped, int actual, int non_null))
{
u8 ed = ed_number - 1;
wait:if (ftdi->disconnected > 0) {
return -ENODEV;
} else if (ftdi->initialized == 0) {
return -ENODEV;
} else {
int command_size;
mutex_lock(&ftdi->u132_lock);
command_size = ftdi->command_next - ftdi->command_head;
if (command_size < COMMAND_SIZE) {
struct u132_target *target = &ftdi->target[ed];
struct u132_command *command = &ftdi->command[
COMMAND_MASK & ftdi->command_next];
u32 remaining_length = urb->transfer_buffer_length -
urb->actual_length;
command->header = 0x82 | (ed << 5);
if (remaining_length == 0) {
command->length = 0x0000;
} else if (remaining_length > 1024) {
command->length = 0x8000 | 1023;
} else
command->length = 0x8000 | (remaining_length -
1);
command->address = (toggle_bits << 6) | (ep_number << 2)
| (address << 0);
command->width = usb_maxpacket(urb->dev, urb->pipe,
usb_pipeout(urb->pipe));
command->follows = 0;
command->value = 0;
command->buffer = NULL;
target->callback = callback;
target->endp = endp;
target->urb = urb;
target->active = 1;
ftdi->command_next += 1;
ftdi_elan_kick_command_queue(ftdi);
mutex_unlock(&ftdi->u132_lock);
return 0;
} else {
mutex_unlock(&ftdi->u132_lock);
msleep(100);
goto wait;
}
}
}
int usb_ftdi_elan_edset_input(struct platform_device *pdev, u8 ed_number,
void *endp, struct urb *urb, u8 address, u8 ep_number, u8 toggle_bits,
void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
int toggle_bits, int error_count, int condition_code, int repeat_number,
int halted, int skipped, int actual, int non_null))
{
struct usb_ftdi *ftdi = platform_device_to_usb_ftdi(pdev);
return ftdi_elan_edset_input(ftdi, ed_number, endp, urb, address,
ep_number, toggle_bits, callback);
}
EXPORT_SYMBOL_GPL(usb_ftdi_elan_edset_input);
static int ftdi_elan_edset_empty(struct usb_ftdi *ftdi, u8 ed_number,
void *endp, struct urb *urb, u8 address, u8 ep_number, u8 toggle_bits,
void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
int toggle_bits, int error_count, int condition_code, int repeat_number,
int halted, int skipped, int actual, int non_null))
{
u8 ed = ed_number - 1;
wait:if (ftdi->disconnected > 0) {
return -ENODEV;
} else if (ftdi->initialized == 0) {
return -ENODEV;
} else {
int command_size;
mutex_lock(&ftdi->u132_lock);
command_size = ftdi->command_next - ftdi->command_head;
if (command_size < COMMAND_SIZE) {
struct u132_target *target = &ftdi->target[ed];
struct u132_command *command = &ftdi->command[
COMMAND_MASK & ftdi->command_next];
command->header = 0x81 | (ed << 5);
command->length = 0x0000;
command->address = (toggle_bits << 6) | (ep_number << 2)
| (address << 0);
command->width = usb_maxpacket(urb->dev, urb->pipe,
usb_pipeout(urb->pipe));
command->follows = 0;
command->value = 0;
command->buffer = NULL;
target->callback = callback;
target->endp = endp;
target->urb = urb;
target->active = 1;
ftdi->command_next += 1;
ftdi_elan_kick_command_queue(ftdi);
mutex_unlock(&ftdi->u132_lock);
return 0;
} else {
mutex_unlock(&ftdi->u132_lock);
msleep(100);
goto wait;
}
}
}
int usb_ftdi_elan_edset_empty(struct platform_device *pdev, u8 ed_number,
void *endp, struct urb *urb, u8 address, u8 ep_number, u8 toggle_bits,
void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
int toggle_bits, int error_count, int condition_code, int repeat_number,
int halted, int skipped, int actual, int non_null))
{
struct usb_ftdi *ftdi = platform_device_to_usb_ftdi(pdev);
return ftdi_elan_edset_empty(ftdi, ed_number, endp, urb, address,
ep_number, toggle_bits, callback);
}
EXPORT_SYMBOL_GPL(usb_ftdi_elan_edset_empty);
static int ftdi_elan_edset_output(struct usb_ftdi *ftdi, u8 ed_number,
void *endp, struct urb *urb, u8 address, u8 ep_number, u8 toggle_bits,
void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
int toggle_bits, int error_count, int condition_code, int repeat_number,
int halted, int skipped, int actual, int non_null))
{
u8 ed = ed_number - 1;
wait:if (ftdi->disconnected > 0) {
return -ENODEV;
} else if (ftdi->initialized == 0) {
return -ENODEV;
} else {
int command_size;
mutex_lock(&ftdi->u132_lock);
command_size = ftdi->command_next - ftdi->command_head;
if (command_size < COMMAND_SIZE) {
u8 *b;
u16 urb_size;
int i = 0;
char data[30 *3 + 4];
char *d = data;
int m = (sizeof(data) - 1) / 3 - 1;
int l = 0;
struct u132_target *target = &ftdi->target[ed];
struct u132_command *command = &ftdi->command[
COMMAND_MASK & ftdi->command_next];
command->header = 0x81 | (ed << 5);
command->address = (toggle_bits << 6) | (ep_number << 2)
| (address << 0);
command->width = usb_maxpacket(urb->dev, urb->pipe,
usb_pipeout(urb->pipe));
command->follows = min_t(u32, 1024,
urb->transfer_buffer_length -
urb->actual_length);
command->value = 0;
command->buffer = urb->transfer_buffer +
urb->actual_length;
command->length = 0x8000 | (command->follows - 1);
b = command->buffer;
urb_size = command->follows;
data[0] = 0;
while (urb_size-- > 0) {
if (i > m) {
} else if (i++ < m) {
int w = sprintf(d, " %02X", *b++);
d += w;
l += w;
} else
d += sprintf(d, " ..");
}
target->callback = callback;
target->endp = endp;
target->urb = urb;
target->active = 1;
ftdi->command_next += 1;
ftdi_elan_kick_command_queue(ftdi);
mutex_unlock(&ftdi->u132_lock);
return 0;
} else {
mutex_unlock(&ftdi->u132_lock);
msleep(100);
goto wait;
}
}
}
int usb_ftdi_elan_edset_output(struct platform_device *pdev, u8 ed_number,
void *endp, struct urb *urb, u8 address, u8 ep_number, u8 toggle_bits,
void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
int toggle_bits, int error_count, int condition_code, int repeat_number,
int halted, int skipped, int actual, int non_null))
{
struct usb_ftdi *ftdi = platform_device_to_usb_ftdi(pdev);
return ftdi_elan_edset_output(ftdi, ed_number, endp, urb, address,
ep_number, toggle_bits, callback);
}
EXPORT_SYMBOL_GPL(usb_ftdi_elan_edset_output);
static int ftdi_elan_edset_single(struct usb_ftdi *ftdi, u8 ed_number,
void *endp, struct urb *urb, u8 address, u8 ep_number, u8 toggle_bits,
void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
int toggle_bits, int error_count, int condition_code, int repeat_number,
int halted, int skipped, int actual, int non_null))
{
u8 ed = ed_number - 1;
wait:if (ftdi->disconnected > 0) {
return -ENODEV;
} else if (ftdi->initialized == 0) {
return -ENODEV;
} else {
int command_size;
mutex_lock(&ftdi->u132_lock);
command_size = ftdi->command_next - ftdi->command_head;
if (command_size < COMMAND_SIZE) {
u32 remaining_length = urb->transfer_buffer_length -
urb->actual_length;
struct u132_target *target = &ftdi->target[ed];
struct u132_command *command = &ftdi->command[
COMMAND_MASK & ftdi->command_next];
command->header = 0x83 | (ed << 5);
if (remaining_length == 0) {
command->length = 0x0000;
} else if (remaining_length > 1024) {
command->length = 0x8000 | 1023;
} else
command->length = 0x8000 | (remaining_length -
1);
command->address = (toggle_bits << 6) | (ep_number << 2)
| (address << 0);
command->width = usb_maxpacket(urb->dev, urb->pipe,
usb_pipeout(urb->pipe));
command->follows = 0;
command->value = 0;
command->buffer = NULL;
target->callback = callback;
target->endp = endp;
target->urb = urb;
target->active = 1;
ftdi->command_next += 1;
ftdi_elan_kick_command_queue(ftdi);
mutex_unlock(&ftdi->u132_lock);
return 0;
} else {
mutex_unlock(&ftdi->u132_lock);
msleep(100);
goto wait;
}
}
}
int usb_ftdi_elan_edset_single(struct platform_device *pdev, u8 ed_number,
void *endp, struct urb *urb, u8 address, u8 ep_number, u8 toggle_bits,
void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
int toggle_bits, int error_count, int condition_code, int repeat_number,
int halted, int skipped, int actual, int non_null))
{
struct usb_ftdi *ftdi = platform_device_to_usb_ftdi(pdev);
return ftdi_elan_edset_single(ftdi, ed_number, endp, urb, address,
ep_number, toggle_bits, callback);
}
EXPORT_SYMBOL_GPL(usb_ftdi_elan_edset_single);
static int ftdi_elan_edset_flush(struct usb_ftdi *ftdi, u8 ed_number,
void *endp)
{
u8 ed = ed_number - 1;
if (ftdi->disconnected > 0) {
return -ENODEV;
} else if (ftdi->initialized == 0) {
return -ENODEV;
} else {
struct u132_target *target = &ftdi->target[ed];
mutex_lock(&ftdi->u132_lock);
if (target->abandoning > 0) {
mutex_unlock(&ftdi->u132_lock);
return 0;
} else {
target->abandoning = 1;
wait_1:if (target->active == 1) {
int command_size = ftdi->command_next -
ftdi->command_head;
if (command_size < COMMAND_SIZE) {
struct u132_command *command =
&ftdi->command[COMMAND_MASK &
ftdi->command_next];
command->header = 0x80 | (ed << 5) |
0x4;
command->length = 0x00;
command->address = 0x00;
command->width = 0x00;
command->follows = 0;
command->value = 0;
command->buffer = &command->value;
ftdi->command_next += 1;
ftdi_elan_kick_command_queue(ftdi);
} else {
mutex_unlock(&ftdi->u132_lock);
msleep(100);
mutex_lock(&ftdi->u132_lock);
goto wait_1;
}
}
mutex_unlock(&ftdi->u132_lock);
return 0;
}
}
}
int usb_ftdi_elan_edset_flush(struct platform_device *pdev, u8 ed_number,
void *endp)
{
struct usb_ftdi *ftdi = platform_device_to_usb_ftdi(pdev);
return ftdi_elan_edset_flush(ftdi, ed_number, endp);
}
EXPORT_SYMBOL_GPL(usb_ftdi_elan_edset_flush);
static int ftdi_elan_flush_input_fifo(struct usb_ftdi *ftdi)
{
int retry_on_empty = 10;
int retry_on_timeout = 5;
int retry_on_status = 20;
more:{
int packet_bytes = 0;
int retval = usb_bulk_msg(ftdi->udev,
usb_rcvbulkpipe(ftdi->udev, ftdi->bulk_in_endpointAddr),
ftdi->bulk_in_buffer, ftdi->bulk_in_size,
&packet_bytes, 100);
if (packet_bytes > 2) {
char diag[30 *3 + 4];
char *d = diag;
int m = (sizeof(diag) - 1) / 3 - 1;
char *b = ftdi->bulk_in_buffer;
int bytes_read = 0;
diag[0] = 0;
while (packet_bytes-- > 0) {
char c = *b++;
if (bytes_read < m) {
d += sprintf(d, " %02X",
0x000000FF & c);
} else if (bytes_read > m) {
} else
d += sprintf(d, " ..");
bytes_read += 1;
continue;
}
goto more;
} else if (packet_bytes > 1) {
char s1 = ftdi->bulk_in_buffer[0];
char s2 = ftdi->bulk_in_buffer[1];
if (s1 == 0x31 && s2 == 0x60) {
return 0;
} else if (retry_on_status-- > 0) {
goto more;
} else {
dev_err(&ftdi->udev->dev, "STATUS ERROR retry limit reached\n");
return -EFAULT;
}
} else if (packet_bytes > 0) {
char b1 = ftdi->bulk_in_buffer[0];
dev_err(&ftdi->udev->dev, "only one byte flushed from FTDI = %02X\n",
b1);
if (retry_on_status-- > 0) {
goto more;
} else {
dev_err(&ftdi->udev->dev, "STATUS ERROR retry limit reached\n");
return -EFAULT;
}
} else if (retval == -ETIMEDOUT) {
if (retry_on_timeout-- > 0) {
goto more;
} else {
dev_err(&ftdi->udev->dev, "TIMED OUT retry limit reached\n");
return -ENOMEM;
}
} else if (retval == 0) {
if (retry_on_empty-- > 0) {
goto more;
} else {
dev_err(&ftdi->udev->dev, "empty packet retry limit reached\n");
return -ENOMEM;
}
} else {
dev_err(&ftdi->udev->dev, "error = %d\n", retval);
return retval;
}
}
return -1;
}
/*
* send the long flush sequence
*
*/
static int ftdi_elan_synchronize_flush(struct usb_ftdi *ftdi)
{
int retval;
struct urb *urb;
char *buf;
int I = 257;
int i = 0;
urb = usb_alloc_urb(0, GFP_KERNEL);
if (!urb)
return -ENOMEM;
buf = usb_alloc_coherent(ftdi->udev, I, GFP_KERNEL, &urb->transfer_dma);
if (!buf) {
dev_err(&ftdi->udev->dev, "could not get a buffer for flush sequence\n");
usb_free_urb(urb);
return -ENOMEM;
}
while (I-- > 0)
buf[i++] = 0x55;
usb_fill_bulk_urb(urb, ftdi->udev, usb_sndbulkpipe(ftdi->udev,
ftdi->bulk_out_endpointAddr), buf, i,
ftdi_elan_write_bulk_callback, ftdi);
urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
retval = usb_submit_urb(urb, GFP_KERNEL);
if (retval) {
dev_err(&ftdi->udev->dev, "failed to submit urb containing the flush sequence\n");
usb_free_coherent(ftdi->udev, i, buf, urb->transfer_dma);
usb_free_urb(urb);
return -ENOMEM;
}
usb_free_urb(urb);
return 0;
}
/*
* send the reset sequence
*
*/
static int ftdi_elan_synchronize_reset(struct usb_ftdi *ftdi)
{
int retval;
struct urb *urb;
char *buf;
int I = 4;
int i = 0;
urb = usb_alloc_urb(0, GFP_KERNEL);
if (!urb)
return -ENOMEM;
buf = usb_alloc_coherent(ftdi->udev, I, GFP_KERNEL, &urb->transfer_dma);
if (!buf) {
dev_err(&ftdi->udev->dev, "could not get a buffer for the reset sequence\n");
usb_free_urb(urb);
return -ENOMEM;
}
buf[i++] = 0x55;
buf[i++] = 0xAA;
buf[i++] = 0x5A;
buf[i++] = 0xA5;
usb_fill_bulk_urb(urb, ftdi->udev, usb_sndbulkpipe(ftdi->udev,
ftdi->bulk_out_endpointAddr), buf, i,
ftdi_elan_write_bulk_callback, ftdi);
urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
retval = usb_submit_urb(urb, GFP_KERNEL);
if (retval) {
dev_err(&ftdi->udev->dev, "failed to submit urb containing the reset sequence\n");
usb_free_coherent(ftdi->udev, i, buf, urb->transfer_dma);
usb_free_urb(urb);
return -ENOMEM;
}
usb_free_urb(urb);
return 0;
}
static int ftdi_elan_synchronize(struct usb_ftdi *ftdi)
{
int retval;
int long_stop = 10;
int retry_on_timeout = 5;
int retry_on_empty = 10;
int err_count = 0;
retval = ftdi_elan_flush_input_fifo(ftdi);
if (retval)
return retval;
ftdi->bulk_in_left = 0;
ftdi->bulk_in_last = -1;
while (long_stop-- > 0) {
int read_stop;
int read_stuck;
retval = ftdi_elan_synchronize_flush(ftdi);
if (retval)
return retval;
retval = ftdi_elan_flush_input_fifo(ftdi);
if (retval)
return retval;
reset:retval = ftdi_elan_synchronize_reset(ftdi);
if (retval)
return retval;
read_stop = 100;
read_stuck = 10;
read:{
int packet_bytes = 0;
retval = usb_bulk_msg(ftdi->udev,
usb_rcvbulkpipe(ftdi->udev,
ftdi->bulk_in_endpointAddr),
ftdi->bulk_in_buffer, ftdi->bulk_in_size,
&packet_bytes, 500);
if (packet_bytes > 2) {
char diag[30 *3 + 4];
char *d = diag;
int m = (sizeof(diag) - 1) / 3 - 1;
char *b = ftdi->bulk_in_buffer;
int bytes_read = 0;
unsigned char c = 0;
diag[0] = 0;
while (packet_bytes-- > 0) {
c = *b++;
if (bytes_read < m) {
d += sprintf(d, " %02X", c);
} else if (bytes_read > m) {
} else
d += sprintf(d, " ..");
bytes_read += 1;
continue;
}
if (c == 0x7E) {
return 0;
} else {
if (c == 0x55) {
goto read;
} else if (read_stop-- > 0) {
goto read;
} else {
dev_err(&ftdi->udev->dev, "retry limit reached\n");
continue;
}
}
} else if (packet_bytes > 1) {
unsigned char s1 = ftdi->bulk_in_buffer[0];
unsigned char s2 = ftdi->bulk_in_buffer[1];
if (s1 == 0x31 && s2 == 0x00) {
if (read_stuck-- > 0) {
goto read;
} else
goto reset;
} else if (s1 == 0x31 && s2 == 0x60) {
if (read_stop-- > 0) {
goto read;
} else {
dev_err(&ftdi->udev->dev, "retry limit reached\n");
continue;
}
} else {
if (read_stop-- > 0) {
goto read;
} else {
dev_err(&ftdi->udev->dev, "retry limit reached\n");
continue;
}
}
} else if (packet_bytes > 0) {
if (read_stop-- > 0) {
goto read;
} else {
dev_err(&ftdi->udev->dev, "retry limit reached\n");
continue;
}
} else if (retval == -ETIMEDOUT) {
if (retry_on_timeout-- > 0) {
goto read;
} else {
dev_err(&ftdi->udev->dev, "TIMED OUT retry limit reached\n");
continue;
}
} else if (retval == 0) {
if (retry_on_empty-- > 0) {
goto read;
} else {
dev_err(&ftdi->udev->dev, "empty packet retry limit reached\n");
continue;
}
} else {
err_count += 1;
dev_err(&ftdi->udev->dev, "error = %d\n",
retval);
if (read_stop-- > 0) {
goto read;
} else {
dev_err(&ftdi->udev->dev, "retry limit reached\n");
continue;
}
}
}
}
dev_err(&ftdi->udev->dev, "failed to synchronize\n");
return -EFAULT;
}
static int ftdi_elan_stuck_waiting(struct usb_ftdi *ftdi)
{
int retry_on_empty = 10;
int retry_on_timeout = 5;
int retry_on_status = 50;
more:{
int packet_bytes = 0;
int retval = usb_bulk_msg(ftdi->udev,
usb_rcvbulkpipe(ftdi->udev, ftdi->bulk_in_endpointAddr),
ftdi->bulk_in_buffer, ftdi->bulk_in_size,
&packet_bytes, 1000);
if (packet_bytes > 2) {
char diag[30 *3 + 4];
char *d = diag;
int m = (sizeof(diag) - 1) / 3 - 1;
char *b = ftdi->bulk_in_buffer;
int bytes_read = 0;
diag[0] = 0;
while (packet_bytes-- > 0) {
char c = *b++;
if (bytes_read < m) {
d += sprintf(d, " %02X",
0x000000FF & c);
} else if (bytes_read > m) {
} else
d += sprintf(d, " ..");
bytes_read += 1;
continue;
}
goto more;
} else if (packet_bytes > 1) {
char s1 = ftdi->bulk_in_buffer[0];
char s2 = ftdi->bulk_in_buffer[1];
if (s1 == 0x31 && s2 == 0x60) {
return 0;
} else if (retry_on_status-- > 0) {
msleep(5);
goto more;
} else
return -EFAULT;
} else if (packet_bytes > 0) {
char b1 = ftdi->bulk_in_buffer[0];
dev_err(&ftdi->udev->dev, "only one byte flushed from FTDI = %02X\n", b1);
if (retry_on_status-- > 0) {
msleep(5);
goto more;
} else {
dev_err(&ftdi->udev->dev, "STATUS ERROR retry limit reached\n");
return -EFAULT;
}
} else if (retval == -ETIMEDOUT) {
if (retry_on_timeout-- > 0) {
goto more;
} else {
dev_err(&ftdi->udev->dev, "TIMED OUT retry limit reached\n");
return -ENOMEM;
}
} else if (retval == 0) {
if (retry_on_empty-- > 0) {
goto more;
} else {
dev_err(&ftdi->udev->dev, "empty packet retry limit reached\n");
return -ENOMEM;
}
} else {
dev_err(&ftdi->udev->dev, "error = %d\n", retval);
return -ENOMEM;
}
}
return -1;
}
static int ftdi_elan_checkingPCI(struct usb_ftdi *ftdi)
{
int UxxxStatus = ftdi_elan_read_reg(ftdi, &ftdi->controlreg);
if (UxxxStatus)
return UxxxStatus;
if (ftdi->controlreg & 0x00400000) {
if (ftdi->card_ejected) {
} else {
ftdi->card_ejected = 1;
dev_err(&ftdi->udev->dev, "CARD EJECTED - controlreg = %08X\n",
ftdi->controlreg);
}
return -ENODEV;
} else {
u8 fn = ftdi->function - 1;
int activePCIfn = fn << 8;
u32 pcidata;
u32 pciVID;
u32 pciPID;
int reg = 0;
UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
&pcidata);
if (UxxxStatus)
return UxxxStatus;
pciVID = pcidata & 0xFFFF;
pciPID = (pcidata >> 16) & 0xFFFF;
if (pciVID == ftdi->platform_data.vendor && pciPID ==
ftdi->platform_data.device) {
return 0;
} else {
dev_err(&ftdi->udev->dev, "vendor=%04X pciVID=%04X device=%04X pciPID=%04X\n",
ftdi->platform_data.vendor, pciVID,
ftdi->platform_data.device, pciPID);
return -ENODEV;
}
}
}
#define ftdi_read_pcimem(ftdi, member, data) ftdi_elan_read_pcimem(ftdi, \
offsetof(struct ohci_regs, member), 0, data);
#define ftdi_write_pcimem(ftdi, member, data) ftdi_elan_write_pcimem(ftdi, \
offsetof(struct ohci_regs, member), 0, data);
#define OHCI_CONTROL_INIT OHCI_CTRL_CBSR
#define OHCI_INTR_INIT (OHCI_INTR_MIE | OHCI_INTR_UE | OHCI_INTR_RD | \
OHCI_INTR_WDH)
static int ftdi_elan_check_controller(struct usb_ftdi *ftdi, int quirk)
{
int devices = 0;
int retval;
u32 hc_control;
int num_ports;
u32 control;
u32 rh_a = -1;
u32 status;
u32 fminterval;
u32 hc_fminterval;
u32 periodicstart;
u32 cmdstatus;
u32 roothub_a;
int mask = OHCI_INTR_INIT;
int sleep_time = 0;
int reset_timeout = 30; /* ... allow extra time */
int temp;
retval = ftdi_write_pcimem(ftdi, intrdisable, OHCI_INTR_MIE);
if (retval)
return retval;
retval = ftdi_read_pcimem(ftdi, control, &control);
if (retval)
return retval;
retval = ftdi_read_pcimem(ftdi, roothub.a, &rh_a);
if (retval)
return retval;
num_ports = rh_a & RH_A_NDP;
retval = ftdi_read_pcimem(ftdi, fminterval, &hc_fminterval);
if (retval)
return retval;
hc_fminterval &= 0x3fff;
if (hc_fminterval != FI) {
}
hc_fminterval |= FSMP(hc_fminterval) << 16;
retval = ftdi_read_pcimem(ftdi, control, &hc_control);
if (retval)
return retval;
switch (hc_control & OHCI_CTRL_HCFS) {
case OHCI_USB_OPER:
sleep_time = 0;
break;
case OHCI_USB_SUSPEND:
case OHCI_USB_RESUME:
hc_control &= OHCI_CTRL_RWC;
hc_control |= OHCI_USB_RESUME;
sleep_time = 10;
break;
default:
hc_control &= OHCI_CTRL_RWC;
hc_control |= OHCI_USB_RESET;
sleep_time = 50;
break;
}
retval = ftdi_write_pcimem(ftdi, control, hc_control);
if (retval)
return retval;
retval = ftdi_read_pcimem(ftdi, control, &control);
if (retval)
return retval;
msleep(sleep_time);
retval = ftdi_read_pcimem(ftdi, roothub.a, &roothub_a);
if (retval)
return retval;
if (!(roothub_a & RH_A_NPS)) { /* power down each port */
for (temp = 0; temp < num_ports; temp++) {
retval = ftdi_write_pcimem(ftdi,
roothub.portstatus[temp], RH_PS_LSDA);
if (retval)
return retval;
}
}
retval = ftdi_read_pcimem(ftdi, control, &control);
if (retval)
return retval;
retry:retval = ftdi_read_pcimem(ftdi, cmdstatus, &status);
if (retval)
return retval;
retval = ftdi_write_pcimem(ftdi, cmdstatus, OHCI_HCR);
if (retval)
return retval;
extra:{
retval = ftdi_read_pcimem(ftdi, cmdstatus, &status);
if (retval)
return retval;
if (0 != (status & OHCI_HCR)) {
if (--reset_timeout == 0) {
dev_err(&ftdi->udev->dev, "USB HC reset timed out!\n");
return -ENODEV;
} else {
msleep(5);
goto extra;
}
}
}
if (quirk & OHCI_QUIRK_INITRESET) {
retval = ftdi_write_pcimem(ftdi, control, hc_control);
if (retval)
return retval;
retval = ftdi_read_pcimem(ftdi, control, &control);
if (retval)
return retval;
}
retval = ftdi_write_pcimem(ftdi, ed_controlhead, 0x00000000);
if (retval)
return retval;
retval = ftdi_write_pcimem(ftdi, ed_bulkhead, 0x11000000);
if (retval)
return retval;
retval = ftdi_write_pcimem(ftdi, hcca, 0x00000000);
if (retval)
return retval;
retval = ftdi_read_pcimem(ftdi, fminterval, &fminterval);
if (retval)
return retval;
retval = ftdi_write_pcimem(ftdi, fminterval,
((fminterval & FIT) ^ FIT) | hc_fminterval);
if (retval)
return retval;
retval = ftdi_write_pcimem(ftdi, periodicstart,
((9 *hc_fminterval) / 10) & 0x3fff);
if (retval)
return retval;
retval = ftdi_read_pcimem(ftdi, fminterval, &fminterval);
if (retval)
return retval;
retval = ftdi_read_pcimem(ftdi, periodicstart, &periodicstart);
if (retval)
return retval;
if (0 == (fminterval & 0x3fff0000) || 0 == periodicstart) {
if (!(quirk & OHCI_QUIRK_INITRESET)) {
quirk |= OHCI_QUIRK_INITRESET;
goto retry;
} else
dev_err(&ftdi->udev->dev, "init err(%08x %04x)\n",
fminterval, periodicstart);
} /* start controller operations */
hc_control &= OHCI_CTRL_RWC;
hc_control |= OHCI_CONTROL_INIT | OHCI_CTRL_BLE | OHCI_USB_OPER;
retval = ftdi_write_pcimem(ftdi, control, hc_control);
if (retval)
return retval;
retval = ftdi_write_pcimem(ftdi, cmdstatus, OHCI_BLF);
if (retval)
return retval;
retval = ftdi_read_pcimem(ftdi, cmdstatus, &cmdstatus);
if (retval)
return retval;
retval = ftdi_read_pcimem(ftdi, control, &control);
if (retval)
return retval;
retval = ftdi_write_pcimem(ftdi, roothub.status, RH_HS_DRWE);
if (retval)
return retval;
retval = ftdi_write_pcimem(ftdi, intrstatus, mask);
if (retval)
return retval;
retval = ftdi_write_pcimem(ftdi, intrdisable,
OHCI_INTR_MIE | OHCI_INTR_OC | OHCI_INTR_RHSC | OHCI_INTR_FNO |
OHCI_INTR_UE | OHCI_INTR_RD | OHCI_INTR_SF | OHCI_INTR_WDH |
OHCI_INTR_SO);
if (retval)
return retval; /* handle root hub init quirks ... */
retval = ftdi_read_pcimem(ftdi, roothub.a, &roothub_a);
if (retval)
return retval;
roothub_a &= ~(RH_A_PSM | RH_A_OCPM);
if (quirk & OHCI_QUIRK_SUPERIO) {
roothub_a |= RH_A_NOCP;
roothub_a &= ~(RH_A_POTPGT | RH_A_NPS);
retval = ftdi_write_pcimem(ftdi, roothub.a, roothub_a);
if (retval)
return retval;
} else if ((quirk & OHCI_QUIRK_AMD756) || distrust_firmware) {
roothub_a |= RH_A_NPS;
retval = ftdi_write_pcimem(ftdi, roothub.a, roothub_a);
if (retval)
return retval;
}
retval = ftdi_write_pcimem(ftdi, roothub.status, RH_HS_LPSC);
if (retval)
return retval;
retval = ftdi_write_pcimem(ftdi, roothub.b,
(roothub_a & RH_A_NPS) ? 0 : RH_B_PPCM);
if (retval)
return retval;
retval = ftdi_read_pcimem(ftdi, control, &control);
if (retval)
return retval;
mdelay((roothub_a >> 23) & 0x1fe);
for (temp = 0; temp < num_ports; temp++) {
u32 portstatus;
retval = ftdi_read_pcimem(ftdi, roothub.portstatus[temp],
&portstatus);
if (retval)
return retval;
if (1 & portstatus)
devices += 1;
}
return devices;
}
static int ftdi_elan_setup_controller(struct usb_ftdi *ftdi, int fn)
{
u32 latence_timer;
int UxxxStatus;
u32 pcidata;
int reg = 0;
int activePCIfn = fn << 8;
UxxxStatus = ftdi_elan_write_reg(ftdi, 0x0000025FL | 0x2800);
if (UxxxStatus)
return UxxxStatus;
reg = 16;
UxxxStatus = ftdi_elan_write_config(ftdi, activePCIfn | reg, 0,
0xFFFFFFFF);
if (UxxxStatus)
return UxxxStatus;
UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
&pcidata);
if (UxxxStatus)
return UxxxStatus;
UxxxStatus = ftdi_elan_write_config(ftdi, activePCIfn | reg, 0,
0xF0000000);
if (UxxxStatus)
return UxxxStatus;
UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
&pcidata);
if (UxxxStatus)
return UxxxStatus;
reg = 12;
UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
&latence_timer);
if (UxxxStatus)
return UxxxStatus;
latence_timer &= 0xFFFF00FF;
latence_timer |= 0x00001600;
UxxxStatus = ftdi_elan_write_config(ftdi, activePCIfn | reg, 0x00,
latence_timer);
if (UxxxStatus)
return UxxxStatus;
UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
&pcidata);
if (UxxxStatus)
return UxxxStatus;
reg = 4;
UxxxStatus = ftdi_elan_write_config(ftdi, activePCIfn | reg, 0x00,
0x06);
if (UxxxStatus)
return UxxxStatus;
UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
&pcidata);
if (UxxxStatus)
return UxxxStatus;
for (reg = 0; reg <= 0x54; reg += 4) {
UxxxStatus = ftdi_elan_read_pcimem(ftdi, reg, 0, &pcidata);
if (UxxxStatus)
return UxxxStatus;
}
return 0;
}
static int ftdi_elan_close_controller(struct usb_ftdi *ftdi, int fn)
{
u32 latence_timer;
int UxxxStatus;
u32 pcidata;
int reg = 0;
int activePCIfn = fn << 8;
UxxxStatus = ftdi_elan_write_reg(ftdi, 0x0000025FL | 0x2800);
if (UxxxStatus)
return UxxxStatus;
reg = 16;
UxxxStatus = ftdi_elan_write_config(ftdi, activePCIfn | reg, 0,
0xFFFFFFFF);
if (UxxxStatus)
return UxxxStatus;
UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
&pcidata);
if (UxxxStatus)
return UxxxStatus;
UxxxStatus = ftdi_elan_write_config(ftdi, activePCIfn | reg, 0,
0x00000000);
if (UxxxStatus)
return UxxxStatus;
UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
&pcidata);
if (UxxxStatus)
return UxxxStatus;
reg = 12;
UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
&latence_timer);
if (UxxxStatus)
return UxxxStatus;
latence_timer &= 0xFFFF00FF;
latence_timer |= 0x00001600;
UxxxStatus = ftdi_elan_write_config(ftdi, activePCIfn | reg, 0x00,
latence_timer);
if (UxxxStatus)
return UxxxStatus;
UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
&pcidata);
if (UxxxStatus)
return UxxxStatus;
reg = 4;
UxxxStatus = ftdi_elan_write_config(ftdi, activePCIfn | reg, 0x00,
0x00);
if (UxxxStatus)
return UxxxStatus;
return ftdi_elan_read_config(ftdi, activePCIfn | reg, 0, &pcidata);
}
static int ftdi_elan_found_controller(struct usb_ftdi *ftdi, int fn, int quirk)
{
int result;
int UxxxStatus;
UxxxStatus = ftdi_elan_setup_controller(ftdi, fn);
if (UxxxStatus)
return UxxxStatus;
result = ftdi_elan_check_controller(ftdi, quirk);
UxxxStatus = ftdi_elan_close_controller(ftdi, fn);
if (UxxxStatus)
return UxxxStatus;
return result;
}
static int ftdi_elan_enumeratePCI(struct usb_ftdi *ftdi)
{
u32 controlreg;
u8 sensebits;
int UxxxStatus;
UxxxStatus = ftdi_elan_read_reg(ftdi, &controlreg);
if (UxxxStatus)
return UxxxStatus;
UxxxStatus = ftdi_elan_write_reg(ftdi, 0x00000000L);
if (UxxxStatus)
return UxxxStatus;
msleep(750);
UxxxStatus = ftdi_elan_write_reg(ftdi, 0x00000200L | 0x100);
if (UxxxStatus)
return UxxxStatus;
UxxxStatus = ftdi_elan_write_reg(ftdi, 0x00000200L | 0x500);
if (UxxxStatus)
return UxxxStatus;
UxxxStatus = ftdi_elan_read_reg(ftdi, &controlreg);
if (UxxxStatus)
return UxxxStatus;
UxxxStatus = ftdi_elan_write_reg(ftdi, 0x0000020CL | 0x000);
if (UxxxStatus)
return UxxxStatus;
UxxxStatus = ftdi_elan_write_reg(ftdi, 0x0000020DL | 0x000);
if (UxxxStatus)
return UxxxStatus;
msleep(250);
UxxxStatus = ftdi_elan_write_reg(ftdi, 0x0000020FL | 0x000);
if (UxxxStatus)
return UxxxStatus;
UxxxStatus = ftdi_elan_read_reg(ftdi, &controlreg);
if (UxxxStatus)
return UxxxStatus;
UxxxStatus = ftdi_elan_write_reg(ftdi, 0x0000025FL | 0x800);
if (UxxxStatus)
return UxxxStatus;
UxxxStatus = ftdi_elan_read_reg(ftdi, &controlreg);
if (UxxxStatus)
return UxxxStatus;
UxxxStatus = ftdi_elan_read_reg(ftdi, &controlreg);
if (UxxxStatus)
return UxxxStatus;
msleep(1000);
sensebits = (controlreg >> 16) & 0x000F;
if (0x0D == sensebits)
return 0;
else
return - ENXIO;
}
static int ftdi_elan_setupOHCI(struct usb_ftdi *ftdi)
{
int UxxxStatus;
u32 pcidata;
int reg = 0;
u8 fn;
int activePCIfn = 0;
int max_devices = 0;
int controllers = 0;
int unrecognized = 0;
ftdi->function = 0;
for (fn = 0; (fn < 4); fn++) {
u32 pciVID = 0;
u32 pciPID = 0;
int devices = 0;
activePCIfn = fn << 8;
UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
&pcidata);
if (UxxxStatus)
return UxxxStatus;
pciVID = pcidata & 0xFFFF;
pciPID = (pcidata >> 16) & 0xFFFF;
if ((pciVID == PCI_VENDOR_ID_OPTI) && (pciPID == 0xc861)) {
devices = ftdi_elan_found_controller(ftdi, fn, 0);
controllers += 1;
} else if ((pciVID == PCI_VENDOR_ID_NEC) && (pciPID == 0x0035))
{
devices = ftdi_elan_found_controller(ftdi, fn, 0);
controllers += 1;
} else if ((pciVID == PCI_VENDOR_ID_AL) && (pciPID == 0x5237)) {
devices = ftdi_elan_found_controller(ftdi, fn, 0);
controllers += 1;
} else if ((pciVID == PCI_VENDOR_ID_ATT) && (pciPID == 0x5802))
{
devices = ftdi_elan_found_controller(ftdi, fn, 0);
controllers += 1;
} else if (pciVID == PCI_VENDOR_ID_AMD && pciPID == 0x740c) {
devices = ftdi_elan_found_controller(ftdi, fn,
OHCI_QUIRK_AMD756);
controllers += 1;
} else if (pciVID == PCI_VENDOR_ID_COMPAQ && pciPID == 0xa0f8) {
devices = ftdi_elan_found_controller(ftdi, fn,
OHCI_QUIRK_ZFMICRO);
controllers += 1;
} else if (0 == pcidata) {
} else
unrecognized += 1;
if (devices > max_devices) {
max_devices = devices;
ftdi->function = fn + 1;
ftdi->platform_data.vendor = pciVID;
ftdi->platform_data.device = pciPID;
}
}
if (ftdi->function > 0) {
return ftdi_elan_setup_controller(ftdi, ftdi->function - 1);
} else if (controllers > 0) {
return -ENXIO;
} else if (unrecognized > 0) {
return -ENXIO;
} else {
ftdi->enumerated = 0;
return -ENXIO;
}
}
/*
* we use only the first bulk-in and bulk-out endpoints
*/
static int ftdi_elan_probe(struct usb_interface *interface,
const struct usb_device_id *id)
{
struct usb_host_interface *iface_desc;
struct usb_endpoint_descriptor *bulk_in, *bulk_out;
int retval;
struct usb_ftdi *ftdi;
ftdi = kzalloc(sizeof(struct usb_ftdi), GFP_KERNEL);
if (!ftdi)
return -ENOMEM;
mutex_lock(&ftdi_module_lock);
list_add_tail(&ftdi->ftdi_list, &ftdi_static_list);
ftdi->sequence_num = ++ftdi_instances;
mutex_unlock(&ftdi_module_lock);
ftdi_elan_init_kref(ftdi);
sema_init(&ftdi->sw_lock, 1);
ftdi->udev = usb_get_dev(interface_to_usbdev(interface));
ftdi->interface = interface;
mutex_init(&ftdi->u132_lock);
ftdi->expected = 4;
iface_desc = interface->cur_altsetting;
retval = usb_find_common_endpoints(iface_desc,
&bulk_in, &bulk_out, NULL, NULL);
if (retval) {
dev_err(&ftdi->udev->dev, "Could not find both bulk-in and bulk-out endpoints\n");
goto error;
}
ftdi->bulk_in_size = usb_endpoint_maxp(bulk_in);
ftdi->bulk_in_endpointAddr = bulk_in->bEndpointAddress;
ftdi->bulk_in_buffer = kmalloc(ftdi->bulk_in_size, GFP_KERNEL);
if (!ftdi->bulk_in_buffer) {
retval = -ENOMEM;
goto error;
}
ftdi->bulk_out_endpointAddr = bulk_out->bEndpointAddress;
dev_info(&ftdi->udev->dev, "interface %d has I=%02X O=%02X\n",
iface_desc->desc.bInterfaceNumber, ftdi->bulk_in_endpointAddr,
ftdi->bulk_out_endpointAddr);
usb_set_intfdata(interface, ftdi);
if (iface_desc->desc.bInterfaceNumber == 0 &&
ftdi->bulk_in_endpointAddr == 0x81 &&
ftdi->bulk_out_endpointAddr == 0x02) {
retval = usb_register_dev(interface, &ftdi_elan_jtag_class);
if (retval) {
dev_err(&ftdi->udev->dev, "Not able to get a minor for this device\n");
usb_set_intfdata(interface, NULL);
retval = -ENOMEM;
goto error;
} else {
ftdi->class = &ftdi_elan_jtag_class;
dev_info(&ftdi->udev->dev, "USB FDTI=%p JTAG interface %d now attached to ftdi%d\n",
ftdi, iface_desc->desc.bInterfaceNumber,
interface->minor);
return 0;
}
} else if (iface_desc->desc.bInterfaceNumber == 1 &&
ftdi->bulk_in_endpointAddr == 0x83 &&
ftdi->bulk_out_endpointAddr == 0x04) {
ftdi->class = NULL;
dev_info(&ftdi->udev->dev, "USB FDTI=%p ELAN interface %d now activated\n",
ftdi, iface_desc->desc.bInterfaceNumber);
INIT_DELAYED_WORK(&ftdi->status_work, ftdi_elan_status_work);
INIT_DELAYED_WORK(&ftdi->command_work, ftdi_elan_command_work);
INIT_DELAYED_WORK(&ftdi->respond_work, ftdi_elan_respond_work);
ftdi_status_queue_work(ftdi, msecs_to_jiffies(3 *1000));
return 0;
} else {
dev_err(&ftdi->udev->dev,
"Could not find ELAN's U132 device\n");
retval = -ENODEV;
goto error;
}
error:if (ftdi) {
ftdi_elan_put_kref(ftdi);
}
return retval;
}
static void ftdi_elan_disconnect(struct usb_interface *interface)
{
struct usb_ftdi *ftdi = usb_get_intfdata(interface);
ftdi->disconnected += 1;
if (ftdi->class) {
int minor = interface->minor;
struct usb_class_driver *class = ftdi->class;
usb_set_intfdata(interface, NULL);
usb_deregister_dev(interface, class);
dev_info(&ftdi->udev->dev, "USB FTDI U132 jtag interface on minor %d now disconnected\n",
minor);
} else {
ftdi_status_cancel_work(ftdi);
ftdi_command_cancel_work(ftdi);
ftdi_response_cancel_work(ftdi);
ftdi_elan_abandon_completions(ftdi);
ftdi_elan_abandon_targets(ftdi);
if (ftdi->registered) {
platform_device_unregister(&ftdi->platform_dev);
ftdi->synchronized = 0;
ftdi->enumerated = 0;
ftdi->initialized = 0;
ftdi->registered = 0;
}
ftdi->disconnected += 1;
usb_set_intfdata(interface, NULL);
dev_info(&ftdi->udev->dev, "USB FTDI U132 host controller interface now disconnected\n");
}
ftdi_elan_put_kref(ftdi);
}
static struct usb_driver ftdi_elan_driver = {
.name = "ftdi-elan",
.probe = ftdi_elan_probe,
.disconnect = ftdi_elan_disconnect,
.id_table = ftdi_elan_table,
};
static int __init ftdi_elan_init(void)
{
int result;
pr_info("driver %s\n", ftdi_elan_driver.name);
mutex_init(&ftdi_module_lock);
INIT_LIST_HEAD(&ftdi_static_list);
result = usb_register(&ftdi_elan_driver);
if (result) {
pr_err("usb_register failed. Error number %d\n", result);
}
return result;
}
static void __exit ftdi_elan_exit(void)
{
struct usb_ftdi *ftdi;
struct usb_ftdi *temp;
usb_deregister(&ftdi_elan_driver);
pr_info("ftdi_u132 driver deregistered\n");
list_for_each_entry_safe(ftdi, temp, &ftdi_static_list, ftdi_list) {
ftdi_status_cancel_work(ftdi);
ftdi_command_cancel_work(ftdi);
ftdi_response_cancel_work(ftdi);
}
}
module_init(ftdi_elan_init);
module_exit(ftdi_elan_exit);
|