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
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
|
/* $FreeBSD$ */
/*-
* Copyright (c) 2008 Hans Petter Selasky. All rights reserved.
* Copyright (c) 1998 The NetBSD Foundation, Inc. All rights reserved.
* Copyright (c) 1998 Lennart Augustsson. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
/*
* USB Universal Host Controller driver.
* Handles e.g. PIIX3 and PIIX4.
*
* UHCI spec: http://developer.intel.com/design/USB/UHCI11D.htm
* USB spec: http://www.usb.org/developers/docs/usbspec.zip
* PIIXn spec: ftp://download.intel.com/design/intarch/datashts/29055002.pdf
* ftp://download.intel.com/design/intarch/datashts/29056201.pdf
*/
#ifdef USB_GLOBAL_INCLUDE_FILE
#include USB_GLOBAL_INCLUDE_FILE
#else
#include <sys/stdint.h>
#include <sys/stddef.h>
#include <sys/param.h>
#include <sys/queue.h>
#include <sys/types.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/bus.h>
#include <sys/module.h>
#include <sys/lock.h>
#include <sys/mutex.h>
#include <sys/condvar.h>
#include <sys/sysctl.h>
#include <sys/sx.h>
#include <sys/unistd.h>
#include <sys/callout.h>
#include <sys/malloc.h>
#include <sys/priv.h>
#include <dev/usb/usb.h>
#include <dev/usb/usbdi.h>
#define USB_DEBUG_VAR uhcidebug
#include <dev/usb/usb_core.h>
#include <dev/usb/usb_debug.h>
#include <dev/usb/usb_busdma.h>
#include <dev/usb/usb_process.h>
#include <dev/usb/usb_transfer.h>
#include <dev/usb/usb_device.h>
#include <dev/usb/usb_hub.h>
#include <dev/usb/usb_util.h>
#include <dev/usb/usb_controller.h>
#include <dev/usb/usb_bus.h>
#endif /* USB_GLOBAL_INCLUDE_FILE */
#include <dev/usb/controller/uhci.h>
#include <dev/usb/controller/uhcireg.h>
#define alt_next next
#define UHCI_BUS2SC(bus) \
((uhci_softc_t *)(((uint8_t *)(bus)) - \
((uint8_t *)&(((uhci_softc_t *)0)->sc_bus))))
#ifdef USB_DEBUG
static int uhcidebug = 0;
static int uhcinoloop = 0;
static SYSCTL_NODE(_hw_usb, OID_AUTO, uhci, CTLFLAG_RW, 0, "USB uhci");
SYSCTL_INT(_hw_usb_uhci, OID_AUTO, debug, CTLFLAG_RW | CTLFLAG_TUN,
&uhcidebug, 0, "uhci debug level");
TUNABLE_INT("hw.usb.uhci.debug", &uhcidebug);
SYSCTL_INT(_hw_usb_uhci, OID_AUTO, loop, CTLFLAG_RW | CTLFLAG_TUN,
&uhcinoloop, 0, "uhci noloop");
TUNABLE_INT("hw.usb.uhci.loop", &uhcinoloop);
static void uhci_dumpregs(uhci_softc_t *sc);
static void uhci_dump_tds(uhci_td_t *td);
#endif
#define UBARR(sc) bus_space_barrier((sc)->sc_io_tag, (sc)->sc_io_hdl, 0, (sc)->sc_io_size, \
BUS_SPACE_BARRIER_READ|BUS_SPACE_BARRIER_WRITE)
#define UWRITE1(sc, r, x) \
do { UBARR(sc); bus_space_write_1((sc)->sc_io_tag, (sc)->sc_io_hdl, (r), (x)); \
} while (/*CONSTCOND*/0)
#define UWRITE2(sc, r, x) \
do { UBARR(sc); bus_space_write_2((sc)->sc_io_tag, (sc)->sc_io_hdl, (r), (x)); \
} while (/*CONSTCOND*/0)
#define UWRITE4(sc, r, x) \
do { UBARR(sc); bus_space_write_4((sc)->sc_io_tag, (sc)->sc_io_hdl, (r), (x)); \
} while (/*CONSTCOND*/0)
#define UREAD1(sc, r) (UBARR(sc), bus_space_read_1((sc)->sc_io_tag, (sc)->sc_io_hdl, (r)))
#define UREAD2(sc, r) (UBARR(sc), bus_space_read_2((sc)->sc_io_tag, (sc)->sc_io_hdl, (r)))
#define UREAD4(sc, r) (UBARR(sc), bus_space_read_4((sc)->sc_io_tag, (sc)->sc_io_hdl, (r)))
#define UHCICMD(sc, cmd) UWRITE2(sc, UHCI_CMD, cmd)
#define UHCISTS(sc) UREAD2(sc, UHCI_STS)
#define UHCI_RESET_TIMEOUT 100 /* ms, reset timeout */
#define UHCI_INTR_ENDPT 1
struct uhci_mem_layout {
struct usb_page_search buf_res;
struct usb_page_search fix_res;
struct usb_page_cache *buf_pc;
struct usb_page_cache *fix_pc;
uint32_t buf_offset;
uint16_t max_frame_size;
};
struct uhci_std_temp {
struct uhci_mem_layout ml;
uhci_td_t *td;
uhci_td_t *td_next;
uint32_t average;
uint32_t td_status;
uint32_t td_token;
uint32_t len;
uint16_t max_frame_size;
uint8_t shortpkt;
uint8_t setup_alt_next;
uint8_t last_frame;
};
extern struct usb_bus_methods uhci_bus_methods;
extern struct usb_pipe_methods uhci_device_bulk_methods;
extern struct usb_pipe_methods uhci_device_ctrl_methods;
extern struct usb_pipe_methods uhci_device_intr_methods;
extern struct usb_pipe_methods uhci_device_isoc_methods;
static uint8_t uhci_restart(uhci_softc_t *sc);
static void uhci_do_poll(struct usb_bus *);
static void uhci_device_done(struct usb_xfer *, usb_error_t);
static void uhci_transfer_intr_enqueue(struct usb_xfer *);
static void uhci_timeout(void *);
static uint8_t uhci_check_transfer(struct usb_xfer *);
static void uhci_root_intr(uhci_softc_t *sc);
void
uhci_iterate_hw_softc(struct usb_bus *bus, usb_bus_mem_sub_cb_t *cb)
{
struct uhci_softc *sc = UHCI_BUS2SC(bus);
uint32_t i;
cb(bus, &sc->sc_hw.pframes_pc, &sc->sc_hw.pframes_pg,
sizeof(uint32_t) * UHCI_FRAMELIST_COUNT, UHCI_FRAMELIST_ALIGN);
cb(bus, &sc->sc_hw.ls_ctl_start_pc, &sc->sc_hw.ls_ctl_start_pg,
sizeof(uhci_qh_t), UHCI_QH_ALIGN);
cb(bus, &sc->sc_hw.fs_ctl_start_pc, &sc->sc_hw.fs_ctl_start_pg,
sizeof(uhci_qh_t), UHCI_QH_ALIGN);
cb(bus, &sc->sc_hw.bulk_start_pc, &sc->sc_hw.bulk_start_pg,
sizeof(uhci_qh_t), UHCI_QH_ALIGN);
cb(bus, &sc->sc_hw.last_qh_pc, &sc->sc_hw.last_qh_pg,
sizeof(uhci_qh_t), UHCI_QH_ALIGN);
cb(bus, &sc->sc_hw.last_td_pc, &sc->sc_hw.last_td_pg,
sizeof(uhci_td_t), UHCI_TD_ALIGN);
for (i = 0; i != UHCI_VFRAMELIST_COUNT; i++) {
cb(bus, sc->sc_hw.isoc_start_pc + i,
sc->sc_hw.isoc_start_pg + i,
sizeof(uhci_td_t), UHCI_TD_ALIGN);
}
for (i = 0; i != UHCI_IFRAMELIST_COUNT; i++) {
cb(bus, sc->sc_hw.intr_start_pc + i,
sc->sc_hw.intr_start_pg + i,
sizeof(uhci_qh_t), UHCI_QH_ALIGN);
}
}
static void
uhci_mem_layout_init(struct uhci_mem_layout *ml, struct usb_xfer *xfer)
{
ml->buf_pc = xfer->frbuffers + 0;
ml->fix_pc = xfer->buf_fixup;
ml->buf_offset = 0;
ml->max_frame_size = xfer->max_frame_size;
}
static void
uhci_mem_layout_fixup(struct uhci_mem_layout *ml, struct uhci_td *td)
{
usbd_get_page(ml->buf_pc, ml->buf_offset, &ml->buf_res);
if (ml->buf_res.length < td->len) {
/* need to do a fixup */
usbd_get_page(ml->fix_pc, 0, &ml->fix_res);
td->td_buffer = htole32(ml->fix_res.physaddr);
/*
* The UHCI driver cannot handle
* page crossings, so a fixup is
* needed:
*
* +----+----+ - - -
* | YYY|Y |
* +----+----+ - - -
* \ \
* \ \
* +----+
* |YYYY| (fixup)
* +----+
*/
if ((td->td_token & htole32(UHCI_TD_PID)) ==
htole32(UHCI_TD_PID_IN)) {
td->fix_pc = ml->fix_pc;
usb_pc_cpu_invalidate(ml->fix_pc);
} else {
td->fix_pc = NULL;
/* copy data to fixup location */
usbd_copy_out(ml->buf_pc, ml->buf_offset,
ml->fix_res.buffer, td->len);
usb_pc_cpu_flush(ml->fix_pc);
}
/* prepare next fixup */
ml->fix_pc++;
} else {
td->td_buffer = htole32(ml->buf_res.physaddr);
td->fix_pc = NULL;
}
/* prepare next data location */
ml->buf_offset += td->len;
}
/*
* Return values:
* 0: Success
* Else: Failure
*/
static uint8_t
uhci_restart(uhci_softc_t *sc)
{
struct usb_page_search buf_res;
USB_BUS_LOCK_ASSERT(&sc->sc_bus, MA_OWNED);
if (UREAD2(sc, UHCI_CMD) & UHCI_CMD_RS) {
DPRINTFN(2, "Already started\n");
return (0);
}
DPRINTFN(2, "Restarting\n");
usbd_get_page(&sc->sc_hw.pframes_pc, 0, &buf_res);
/* Reload fresh base address */
UWRITE4(sc, UHCI_FLBASEADDR, buf_res.physaddr);
/*
* Assume 64 byte packets at frame end and start HC controller:
*/
UHCICMD(sc, (UHCI_CMD_MAXP | UHCI_CMD_RS));
/* wait 10 milliseconds */
usb_pause_mtx(&sc->sc_bus.bus_mtx, hz / 100);
/* check that controller has started */
if (UREAD2(sc, UHCI_STS) & UHCI_STS_HCH) {
DPRINTFN(2, "Failed\n");
return (1);
}
return (0);
}
void
uhci_reset(uhci_softc_t *sc)
{
uint16_t n;
USB_BUS_LOCK_ASSERT(&sc->sc_bus, MA_OWNED);
DPRINTF("resetting the HC\n");
/* disable interrupts */
UWRITE2(sc, UHCI_INTR, 0);
/* global reset */
UHCICMD(sc, UHCI_CMD_GRESET);
/* wait */
usb_pause_mtx(&sc->sc_bus.bus_mtx,
USB_MS_TO_TICKS(USB_BUS_RESET_DELAY));
/* terminate all transfers */
UHCICMD(sc, UHCI_CMD_HCRESET);
/* the reset bit goes low when the controller is done */
n = UHCI_RESET_TIMEOUT;
while (n--) {
/* wait one millisecond */
usb_pause_mtx(&sc->sc_bus.bus_mtx, hz / 1000);
if (!(UREAD2(sc, UHCI_CMD) & UHCI_CMD_HCRESET)) {
goto done_1;
}
}
device_printf(sc->sc_bus.bdev,
"controller did not reset\n");
done_1:
n = 10;
while (n--) {
/* wait one millisecond */
usb_pause_mtx(&sc->sc_bus.bus_mtx, hz / 1000);
/* check if HC is stopped */
if (UREAD2(sc, UHCI_STS) & UHCI_STS_HCH) {
goto done_2;
}
}
device_printf(sc->sc_bus.bdev,
"controller did not stop\n");
done_2:
/* reset frame number */
UWRITE2(sc, UHCI_FRNUM, 0);
/* set default SOF value */
UWRITE1(sc, UHCI_SOF, 0x40);
USB_BUS_UNLOCK(&sc->sc_bus);
/* stop root interrupt */
usb_callout_drain(&sc->sc_root_intr);
USB_BUS_LOCK(&sc->sc_bus);
}
static void
uhci_start(uhci_softc_t *sc)
{
USB_BUS_LOCK_ASSERT(&sc->sc_bus, MA_OWNED);
DPRINTFN(2, "enabling\n");
/* enable interrupts */
UWRITE2(sc, UHCI_INTR,
(UHCI_INTR_TOCRCIE |
UHCI_INTR_RIE |
UHCI_INTR_IOCE |
UHCI_INTR_SPIE));
if (uhci_restart(sc)) {
device_printf(sc->sc_bus.bdev,
"cannot start HC controller\n");
}
/* start root interrupt */
uhci_root_intr(sc);
}
static struct uhci_qh *
uhci_init_qh(struct usb_page_cache *pc)
{
struct usb_page_search buf_res;
struct uhci_qh *qh;
usbd_get_page(pc, 0, &buf_res);
qh = buf_res.buffer;
qh->qh_self =
htole32(buf_res.physaddr) |
htole32(UHCI_PTR_QH);
qh->page_cache = pc;
return (qh);
}
static struct uhci_td *
uhci_init_td(struct usb_page_cache *pc)
{
struct usb_page_search buf_res;
struct uhci_td *td;
usbd_get_page(pc, 0, &buf_res);
td = buf_res.buffer;
td->td_self =
htole32(buf_res.physaddr) |
htole32(UHCI_PTR_TD);
td->page_cache = pc;
return (td);
}
usb_error_t
uhci_init(uhci_softc_t *sc)
{
uint16_t bit;
uint16_t x;
uint16_t y;
DPRINTF("start\n");
usb_callout_init_mtx(&sc->sc_root_intr, &sc->sc_bus.bus_mtx, 0);
#ifdef USB_DEBUG
if (uhcidebug > 2) {
uhci_dumpregs(sc);
}
#endif
/*
* Setup QH's
*/
sc->sc_ls_ctl_p_last =
uhci_init_qh(&sc->sc_hw.ls_ctl_start_pc);
sc->sc_fs_ctl_p_last =
uhci_init_qh(&sc->sc_hw.fs_ctl_start_pc);
sc->sc_bulk_p_last =
uhci_init_qh(&sc->sc_hw.bulk_start_pc);
#if 0
sc->sc_reclaim_qh_p =
sc->sc_fs_ctl_p_last;
#else
/* setup reclaim looping point */
sc->sc_reclaim_qh_p =
sc->sc_bulk_p_last;
#endif
sc->sc_last_qh_p =
uhci_init_qh(&sc->sc_hw.last_qh_pc);
sc->sc_last_td_p =
uhci_init_td(&sc->sc_hw.last_td_pc);
for (x = 0; x != UHCI_VFRAMELIST_COUNT; x++) {
sc->sc_isoc_p_last[x] =
uhci_init_td(sc->sc_hw.isoc_start_pc + x);
}
for (x = 0; x != UHCI_IFRAMELIST_COUNT; x++) {
sc->sc_intr_p_last[x] =
uhci_init_qh(sc->sc_hw.intr_start_pc + x);
}
/*
* the QHs are arranged to give poll intervals that are
* powers of 2 times 1ms
*/
bit = UHCI_IFRAMELIST_COUNT / 2;
while (bit) {
x = bit;
while (x & bit) {
uhci_qh_t *qh_x;
uhci_qh_t *qh_y;
y = (x ^ bit) | (bit / 2);
/*
* the next QH has half the poll interval
*/
qh_x = sc->sc_intr_p_last[x];
qh_y = sc->sc_intr_p_last[y];
qh_x->h_next = NULL;
qh_x->qh_h_next = qh_y->qh_self;
qh_x->e_next = NULL;
qh_x->qh_e_next = htole32(UHCI_PTR_T);
x++;
}
bit >>= 1;
}
if (1) {
uhci_qh_t *qh_ls;
uhci_qh_t *qh_intr;
qh_ls = sc->sc_ls_ctl_p_last;
qh_intr = sc->sc_intr_p_last[0];
/* start QH for interrupt traffic */
qh_intr->h_next = qh_ls;
qh_intr->qh_h_next = qh_ls->qh_self;
qh_intr->e_next = 0;
qh_intr->qh_e_next = htole32(UHCI_PTR_T);
}
for (x = 0; x != UHCI_VFRAMELIST_COUNT; x++) {
uhci_td_t *td_x;
uhci_qh_t *qh_intr;
td_x = sc->sc_isoc_p_last[x];
qh_intr = sc->sc_intr_p_last[x | (UHCI_IFRAMELIST_COUNT / 2)];
/* start TD for isochronous traffic */
td_x->next = NULL;
td_x->td_next = qh_intr->qh_self;
td_x->td_status = htole32(UHCI_TD_IOS);
td_x->td_token = htole32(0);
td_x->td_buffer = htole32(0);
}
if (1) {
uhci_qh_t *qh_ls;
uhci_qh_t *qh_fs;
qh_ls = sc->sc_ls_ctl_p_last;
qh_fs = sc->sc_fs_ctl_p_last;
/* start QH where low speed control traffic will be queued */
qh_ls->h_next = qh_fs;
qh_ls->qh_h_next = qh_fs->qh_self;
qh_ls->e_next = 0;
qh_ls->qh_e_next = htole32(UHCI_PTR_T);
}
if (1) {
uhci_qh_t *qh_ctl;
uhci_qh_t *qh_blk;
uhci_qh_t *qh_lst;
uhci_td_t *td_lst;
qh_ctl = sc->sc_fs_ctl_p_last;
qh_blk = sc->sc_bulk_p_last;
/* start QH where full speed control traffic will be queued */
qh_ctl->h_next = qh_blk;
qh_ctl->qh_h_next = qh_blk->qh_self;
qh_ctl->e_next = 0;
qh_ctl->qh_e_next = htole32(UHCI_PTR_T);
qh_lst = sc->sc_last_qh_p;
/* start QH where bulk traffic will be queued */
qh_blk->h_next = qh_lst;
qh_blk->qh_h_next = qh_lst->qh_self;
qh_blk->e_next = 0;
qh_blk->qh_e_next = htole32(UHCI_PTR_T);
td_lst = sc->sc_last_td_p;
/* end QH which is used for looping the QHs */
qh_lst->h_next = 0;
qh_lst->qh_h_next = htole32(UHCI_PTR_T); /* end of QH chain */
qh_lst->e_next = td_lst;
qh_lst->qh_e_next = td_lst->td_self;
/*
* end TD which hangs from the last QH, to avoid a bug in the PIIX
* that makes it run berserk otherwise
*/
td_lst->next = 0;
td_lst->td_next = htole32(UHCI_PTR_T);
td_lst->td_status = htole32(0); /* inactive */
td_lst->td_token = htole32(0);
td_lst->td_buffer = htole32(0);
}
if (1) {
struct usb_page_search buf_res;
uint32_t *pframes;
usbd_get_page(&sc->sc_hw.pframes_pc, 0, &buf_res);
pframes = buf_res.buffer;
/*
* Setup UHCI framelist
*
* Execution order:
*
* pframes -> full speed isochronous -> interrupt QH's -> low
* speed control -> full speed control -> bulk transfers
*
*/
for (x = 0; x != UHCI_FRAMELIST_COUNT; x++) {
pframes[x] =
sc->sc_isoc_p_last[x % UHCI_VFRAMELIST_COUNT]->td_self;
}
}
/* flush all cache into memory */
usb_bus_mem_flush_all(&sc->sc_bus, &uhci_iterate_hw_softc);
/* set up the bus struct */
sc->sc_bus.methods = &uhci_bus_methods;
USB_BUS_LOCK(&sc->sc_bus);
/* reset the controller */
uhci_reset(sc);
/* start the controller */
uhci_start(sc);
USB_BUS_UNLOCK(&sc->sc_bus);
/* catch lost interrupts */
uhci_do_poll(&sc->sc_bus);
return (0);
}
static void
uhci_suspend(uhci_softc_t *sc)
{
#ifdef USB_DEBUG
if (uhcidebug > 2) {
uhci_dumpregs(sc);
}
#endif
USB_BUS_LOCK(&sc->sc_bus);
/* stop the controller */
uhci_reset(sc);
/* enter global suspend */
UHCICMD(sc, UHCI_CMD_EGSM);
USB_BUS_UNLOCK(&sc->sc_bus);
}
static void
uhci_resume(uhci_softc_t *sc)
{
USB_BUS_LOCK(&sc->sc_bus);
/* reset the controller */
uhci_reset(sc);
/* force global resume */
UHCICMD(sc, UHCI_CMD_FGR);
/* and start traffic again */
uhci_start(sc);
USB_BUS_UNLOCK(&sc->sc_bus);
#ifdef USB_DEBUG
if (uhcidebug > 2)
uhci_dumpregs(sc);
#endif
/* catch lost interrupts */
uhci_do_poll(&sc->sc_bus);
}
#ifdef USB_DEBUG
static void
uhci_dumpregs(uhci_softc_t *sc)
{
DPRINTFN(0, "%s regs: cmd=%04x, sts=%04x, intr=%04x, frnum=%04x, "
"flbase=%08x, sof=%04x, portsc1=%04x, portsc2=%04x\n",
device_get_nameunit(sc->sc_bus.bdev),
UREAD2(sc, UHCI_CMD),
UREAD2(sc, UHCI_STS),
UREAD2(sc, UHCI_INTR),
UREAD2(sc, UHCI_FRNUM),
UREAD4(sc, UHCI_FLBASEADDR),
UREAD1(sc, UHCI_SOF),
UREAD2(sc, UHCI_PORTSC1),
UREAD2(sc, UHCI_PORTSC2));
}
static uint8_t
uhci_dump_td(uhci_td_t *p)
{
uint32_t td_next;
uint32_t td_status;
uint32_t td_token;
uint8_t temp;
usb_pc_cpu_invalidate(p->page_cache);
td_next = le32toh(p->td_next);
td_status = le32toh(p->td_status);
td_token = le32toh(p->td_token);
/*
* Check whether the link pointer in this TD marks the link pointer
* as end of queue:
*/
temp = ((td_next & UHCI_PTR_T) || (td_next == 0));
printf("TD(%p) at 0x%08x = link=0x%08x status=0x%08x "
"token=0x%08x buffer=0x%08x\n",
p,
le32toh(p->td_self),
td_next,
td_status,
td_token,
le32toh(p->td_buffer));
printf("TD(%p) td_next=%s%s%s td_status=%s%s%s%s%s%s%s%s%s%s%s, errcnt=%d, actlen=%d pid=%02x,"
"addr=%d,endpt=%d,D=%d,maxlen=%d\n",
p,
(td_next & 1) ? "-T" : "",
(td_next & 2) ? "-Q" : "",
(td_next & 4) ? "-VF" : "",
(td_status & UHCI_TD_BITSTUFF) ? "-BITSTUFF" : "",
(td_status & UHCI_TD_CRCTO) ? "-CRCTO" : "",
(td_status & UHCI_TD_NAK) ? "-NAK" : "",
(td_status & UHCI_TD_BABBLE) ? "-BABBLE" : "",
(td_status & UHCI_TD_DBUFFER) ? "-DBUFFER" : "",
(td_status & UHCI_TD_STALLED) ? "-STALLED" : "",
(td_status & UHCI_TD_ACTIVE) ? "-ACTIVE" : "",
(td_status & UHCI_TD_IOC) ? "-IOC" : "",
(td_status & UHCI_TD_IOS) ? "-IOS" : "",
(td_status & UHCI_TD_LS) ? "-LS" : "",
(td_status & UHCI_TD_SPD) ? "-SPD" : "",
UHCI_TD_GET_ERRCNT(td_status),
UHCI_TD_GET_ACTLEN(td_status),
UHCI_TD_GET_PID(td_token),
UHCI_TD_GET_DEVADDR(td_token),
UHCI_TD_GET_ENDPT(td_token),
UHCI_TD_GET_DT(td_token),
UHCI_TD_GET_MAXLEN(td_token));
return (temp);
}
static uint8_t
uhci_dump_qh(uhci_qh_t *sqh)
{
uint8_t temp;
uint32_t qh_h_next;
uint32_t qh_e_next;
usb_pc_cpu_invalidate(sqh->page_cache);
qh_h_next = le32toh(sqh->qh_h_next);
qh_e_next = le32toh(sqh->qh_e_next);
DPRINTFN(0, "QH(%p) at 0x%08x: h_next=0x%08x e_next=0x%08x\n", sqh,
le32toh(sqh->qh_self), qh_h_next, qh_e_next);
temp = ((((sqh->h_next != NULL) && !(qh_h_next & UHCI_PTR_T)) ? 1 : 0) |
(((sqh->e_next != NULL) && !(qh_e_next & UHCI_PTR_T)) ? 2 : 0));
return (temp);
}
static void
uhci_dump_all(uhci_softc_t *sc)
{
uhci_dumpregs(sc);
uhci_dump_qh(sc->sc_ls_ctl_p_last);
uhci_dump_qh(sc->sc_fs_ctl_p_last);
uhci_dump_qh(sc->sc_bulk_p_last);
uhci_dump_qh(sc->sc_last_qh_p);
}
static void
uhci_dump_tds(uhci_td_t *td)
{
for (;
td != NULL;
td = td->obj_next) {
if (uhci_dump_td(td)) {
break;
}
}
}
#endif
/*
* Let the last QH loop back to the full speed control transfer QH.
* This is what intel calls "bandwidth reclamation" and improves
* USB performance a lot for some devices.
* If we are already looping, just count it.
*/
static void
uhci_add_loop(uhci_softc_t *sc)
{
struct uhci_qh *qh_lst;
struct uhci_qh *qh_rec;
#ifdef USB_DEBUG
if (uhcinoloop) {
return;
}
#endif
if (++(sc->sc_loops) == 1) {
DPRINTFN(6, "add\n");
qh_lst = sc->sc_last_qh_p;
qh_rec = sc->sc_reclaim_qh_p;
/* NOTE: we don't loop back the soft pointer */
qh_lst->qh_h_next = qh_rec->qh_self;
usb_pc_cpu_flush(qh_lst->page_cache);
}
}
static void
uhci_rem_loop(uhci_softc_t *sc)
{
struct uhci_qh *qh_lst;
#ifdef USB_DEBUG
if (uhcinoloop) {
return;
}
#endif
if (--(sc->sc_loops) == 0) {
DPRINTFN(6, "remove\n");
qh_lst = sc->sc_last_qh_p;
qh_lst->qh_h_next = htole32(UHCI_PTR_T);
usb_pc_cpu_flush(qh_lst->page_cache);
}
}
static void
uhci_transfer_intr_enqueue(struct usb_xfer *xfer)
{
/* check for early completion */
if (uhci_check_transfer(xfer)) {
return;
}
/* put transfer on interrupt queue */
usbd_transfer_enqueue(&xfer->xroot->bus->intr_q, xfer);
/* start timeout, if any */
if (xfer->timeout != 0) {
usbd_transfer_timeout_ms(xfer, &uhci_timeout, xfer->timeout);
}
}
#define UHCI_APPEND_TD(std,last) (last) = _uhci_append_td(std,last)
static uhci_td_t *
_uhci_append_td(uhci_td_t *std, uhci_td_t *last)
{
DPRINTFN(11, "%p to %p\n", std, last);
/* (sc->sc_bus.mtx) must be locked */
std->next = last->next;
std->td_next = last->td_next;
std->prev = last;
usb_pc_cpu_flush(std->page_cache);
/*
* the last->next->prev is never followed: std->next->prev = std;
*/
last->next = std;
last->td_next = std->td_self;
usb_pc_cpu_flush(last->page_cache);
return (std);
}
#define UHCI_APPEND_QH(sqh,last) (last) = _uhci_append_qh(sqh,last)
static uhci_qh_t *
_uhci_append_qh(uhci_qh_t *sqh, uhci_qh_t *last)
{
DPRINTFN(11, "%p to %p\n", sqh, last);
if (sqh->h_prev != NULL) {
/* should not happen */
DPRINTFN(0, "QH already linked!\n");
return (last);
}
/* (sc->sc_bus.mtx) must be locked */
sqh->h_next = last->h_next;
sqh->qh_h_next = last->qh_h_next;
sqh->h_prev = last;
usb_pc_cpu_flush(sqh->page_cache);
/*
* The "last->h_next->h_prev" is never followed:
*
* "sqh->h_next->h_prev" = sqh;
*/
last->h_next = sqh;
last->qh_h_next = sqh->qh_self;
usb_pc_cpu_flush(last->page_cache);
return (sqh);
}
/**/
#define UHCI_REMOVE_TD(std,last) (last) = _uhci_remove_td(std,last)
static uhci_td_t *
_uhci_remove_td(uhci_td_t *std, uhci_td_t *last)
{
DPRINTFN(11, "%p from %p\n", std, last);
/* (sc->sc_bus.mtx) must be locked */
std->prev->next = std->next;
std->prev->td_next = std->td_next;
usb_pc_cpu_flush(std->prev->page_cache);
if (std->next) {
std->next->prev = std->prev;
usb_pc_cpu_flush(std->next->page_cache);
}
return ((last == std) ? std->prev : last);
}
#define UHCI_REMOVE_QH(sqh,last) (last) = _uhci_remove_qh(sqh,last)
static uhci_qh_t *
_uhci_remove_qh(uhci_qh_t *sqh, uhci_qh_t *last)
{
DPRINTFN(11, "%p from %p\n", sqh, last);
/* (sc->sc_bus.mtx) must be locked */
/* only remove if not removed from a queue */
if (sqh->h_prev) {
sqh->h_prev->h_next = sqh->h_next;
sqh->h_prev->qh_h_next = sqh->qh_h_next;
usb_pc_cpu_flush(sqh->h_prev->page_cache);
if (sqh->h_next) {
sqh->h_next->h_prev = sqh->h_prev;
usb_pc_cpu_flush(sqh->h_next->page_cache);
}
last = ((last == sqh) ? sqh->h_prev : last);
sqh->h_prev = 0;
usb_pc_cpu_flush(sqh->page_cache);
}
return (last);
}
static void
uhci_isoc_done(uhci_softc_t *sc, struct usb_xfer *xfer)
{
struct usb_page_search res;
uint32_t nframes = xfer->nframes;
uint32_t status;
uint32_t offset = 0;
uint32_t *plen = xfer->frlengths;
uint16_t len = 0;
uhci_td_t *td = xfer->td_transfer_first;
uhci_td_t **pp_last = &sc->sc_isoc_p_last[xfer->qh_pos];
DPRINTFN(13, "xfer=%p endpoint=%p transfer done\n",
xfer, xfer->endpoint);
/* sync any DMA memory before doing fixups */
usb_bdma_post_sync(xfer);
while (nframes--) {
if (td == NULL) {
panic("%s:%d: out of TD's\n",
__FUNCTION__, __LINE__);
}
if (pp_last >= &sc->sc_isoc_p_last[UHCI_VFRAMELIST_COUNT]) {
pp_last = &sc->sc_isoc_p_last[0];
}
#ifdef USB_DEBUG
if (uhcidebug > 5) {
DPRINTF("isoc TD\n");
uhci_dump_td(td);
}
#endif
usb_pc_cpu_invalidate(td->page_cache);
status = le32toh(td->td_status);
len = UHCI_TD_GET_ACTLEN(status);
if (len > *plen) {
len = *plen;
}
if (td->fix_pc) {
usbd_get_page(td->fix_pc, 0, &res);
/* copy data from fixup location to real location */
usb_pc_cpu_invalidate(td->fix_pc);
usbd_copy_in(xfer->frbuffers, offset,
res.buffer, len);
}
offset += *plen;
*plen = len;
/* remove TD from schedule */
UHCI_REMOVE_TD(td, *pp_last);
pp_last++;
plen++;
td = td->obj_next;
}
xfer->aframes = xfer->nframes;
}
static usb_error_t
uhci_non_isoc_done_sub(struct usb_xfer *xfer)
{
struct usb_page_search res;
uhci_td_t *td;
uhci_td_t *td_alt_next;
uint32_t status;
uint32_t token;
uint16_t len;
td = xfer->td_transfer_cache;
td_alt_next = td->alt_next;
if (xfer->aframes != xfer->nframes) {
usbd_xfer_set_frame_len(xfer, xfer->aframes, 0);
}
while (1) {
usb_pc_cpu_invalidate(td->page_cache);
status = le32toh(td->td_status);
token = le32toh(td->td_token);
/*
* Verify the status and add
* up the actual length:
*/
len = UHCI_TD_GET_ACTLEN(status);
if (len > td->len) {
/* should not happen */
DPRINTF("Invalid status length, "
"0x%04x/0x%04x bytes\n", len, td->len);
status |= UHCI_TD_STALLED;
} else if ((xfer->aframes != xfer->nframes) && (len > 0)) {
if (td->fix_pc) {
usbd_get_page(td->fix_pc, 0, &res);
/*
* copy data from fixup location to real
* location
*/
usb_pc_cpu_invalidate(td->fix_pc);
usbd_copy_in(xfer->frbuffers + xfer->aframes,
xfer->frlengths[xfer->aframes], res.buffer, len);
}
/* update actual length */
xfer->frlengths[xfer->aframes] += len;
}
/* Check for last transfer */
if (((void *)td) == xfer->td_transfer_last) {
td = NULL;
break;
}
if (status & UHCI_TD_STALLED) {
/* the transfer is finished */
td = NULL;
break;
}
/* Check for short transfer */
if (len != td->len) {
if (xfer->flags_int.short_frames_ok) {
/* follow alt next */
td = td->alt_next;
} else {
/* the transfer is finished */
td = NULL;
}
break;
}
td = td->obj_next;
if (td->alt_next != td_alt_next) {
/* this USB frame is complete */
break;
}
}
/* update transfer cache */
xfer->td_transfer_cache = td;
/* update data toggle */
xfer->endpoint->toggle_next = (token & UHCI_TD_SET_DT(1)) ? 0 : 1;
#ifdef USB_DEBUG
if (status & UHCI_TD_ERROR) {
DPRINTFN(11, "error, addr=%d, endpt=0x%02x, frame=0x%02x "
"status=%s%s%s%s%s%s%s%s%s%s%s\n",
xfer->address, xfer->endpointno, xfer->aframes,
(status & UHCI_TD_BITSTUFF) ? "[BITSTUFF]" : "",
(status & UHCI_TD_CRCTO) ? "[CRCTO]" : "",
(status & UHCI_TD_NAK) ? "[NAK]" : "",
(status & UHCI_TD_BABBLE) ? "[BABBLE]" : "",
(status & UHCI_TD_DBUFFER) ? "[DBUFFER]" : "",
(status & UHCI_TD_STALLED) ? "[STALLED]" : "",
(status & UHCI_TD_ACTIVE) ? "[ACTIVE]" : "[NOT_ACTIVE]",
(status & UHCI_TD_IOC) ? "[IOC]" : "",
(status & UHCI_TD_IOS) ? "[IOS]" : "",
(status & UHCI_TD_LS) ? "[LS]" : "",
(status & UHCI_TD_SPD) ? "[SPD]" : "");
}
#endif
if (status & UHCI_TD_STALLED) {
/* try to separate I/O errors from STALL */
if (UHCI_TD_GET_ERRCNT(status) == 0)
return (USB_ERR_IOERROR);
return (USB_ERR_STALLED);
}
return (USB_ERR_NORMAL_COMPLETION);
}
static void
uhci_non_isoc_done(struct usb_xfer *xfer)
{
usb_error_t err = 0;
DPRINTFN(13, "xfer=%p endpoint=%p transfer done\n",
xfer, xfer->endpoint);
#ifdef USB_DEBUG
if (uhcidebug > 10) {
uhci_dump_tds(xfer->td_transfer_first);
}
#endif
/* sync any DMA memory before doing fixups */
usb_bdma_post_sync(xfer);
/* reset scanner */
xfer->td_transfer_cache = xfer->td_transfer_first;
if (xfer->flags_int.control_xfr) {
if (xfer->flags_int.control_hdr) {
err = uhci_non_isoc_done_sub(xfer);
}
xfer->aframes = 1;
if (xfer->td_transfer_cache == NULL) {
goto done;
}
}
while (xfer->aframes != xfer->nframes) {
err = uhci_non_isoc_done_sub(xfer);
xfer->aframes++;
if (xfer->td_transfer_cache == NULL) {
goto done;
}
}
if (xfer->flags_int.control_xfr &&
!xfer->flags_int.control_act) {
err = uhci_non_isoc_done_sub(xfer);
}
done:
uhci_device_done(xfer, err);
}
/*------------------------------------------------------------------------*
* uhci_check_transfer_sub
*
* The main purpose of this function is to update the data-toggle
* in case it is wrong.
*------------------------------------------------------------------------*/
static void
uhci_check_transfer_sub(struct usb_xfer *xfer)
{
uhci_qh_t *qh;
uhci_td_t *td;
uhci_td_t *td_alt_next;
uint32_t td_token;
uint32_t td_self;
td = xfer->td_transfer_cache;
qh = xfer->qh_start[xfer->flags_int.curr_dma_set];
td_token = td->obj_next->td_token;
td = td->alt_next;
xfer->td_transfer_cache = td;
td_self = td->td_self;
td_alt_next = td->alt_next;
if (xfer->flags_int.control_xfr)
goto skip; /* don't touch the DT value! */
if (!((td->td_token ^ td_token) & htole32(UHCI_TD_SET_DT(1))))
goto skip; /* data toggle has correct value */
/*
* The data toggle is wrong and we need to toggle it !
*/
while (1) {
td->td_token ^= htole32(UHCI_TD_SET_DT(1));
usb_pc_cpu_flush(td->page_cache);
if (td == xfer->td_transfer_last) {
/* last transfer */
break;
}
td = td->obj_next;
if (td->alt_next != td_alt_next) {
/* next frame */
break;
}
}
skip:
/* update the QH */
qh->qh_e_next = td_self;
usb_pc_cpu_flush(qh->page_cache);
DPRINTFN(13, "xfer=%p following alt next\n", xfer);
}
/*------------------------------------------------------------------------*
* uhci_check_transfer
*
* Return values:
* 0: USB transfer is not finished
* Else: USB transfer is finished
*------------------------------------------------------------------------*/
static uint8_t
uhci_check_transfer(struct usb_xfer *xfer)
{
uint32_t status;
uint32_t token;
uhci_td_t *td;
DPRINTFN(16, "xfer=%p checking transfer\n", xfer);
if (xfer->endpoint->methods == &uhci_device_isoc_methods) {
/* isochronous transfer */
td = xfer->td_transfer_last;
usb_pc_cpu_invalidate(td->page_cache);
status = le32toh(td->td_status);
/* check also if the first is complete */
td = xfer->td_transfer_first;
usb_pc_cpu_invalidate(td->page_cache);
status |= le32toh(td->td_status);
if (!(status & UHCI_TD_ACTIVE)) {
uhci_device_done(xfer, USB_ERR_NORMAL_COMPLETION);
goto transferred;
}
} else {
/* non-isochronous transfer */
/*
* check whether there is an error somewhere
* in the middle, or whether there was a short
* packet (SPD and not ACTIVE)
*/
td = xfer->td_transfer_cache;
while (1) {
usb_pc_cpu_invalidate(td->page_cache);
status = le32toh(td->td_status);
token = le32toh(td->td_token);
/*
* if there is an active TD the transfer isn't done
*/
if (status & UHCI_TD_ACTIVE) {
/* update cache */
xfer->td_transfer_cache = td;
goto done;
}
/*
* last transfer descriptor makes the transfer done
*/
if (((void *)td) == xfer->td_transfer_last) {
break;
}
/*
* any kind of error makes the transfer done
*/
if (status & UHCI_TD_STALLED) {
break;
}
/*
* check if we reached the last packet
* or if there is a short packet:
*/
if ((td->td_next == htole32(UHCI_PTR_T)) ||
(UHCI_TD_GET_ACTLEN(status) < td->len)) {
if (xfer->flags_int.short_frames_ok) {
/* follow alt next */
if (td->alt_next) {
/* update cache */
xfer->td_transfer_cache = td;
uhci_check_transfer_sub(xfer);
goto done;
}
}
/* transfer is done */
break;
}
td = td->obj_next;
}
uhci_non_isoc_done(xfer);
goto transferred;
}
done:
DPRINTFN(13, "xfer=%p is still active\n", xfer);
return (0);
transferred:
return (1);
}
static void
uhci_interrupt_poll(uhci_softc_t *sc)
{
struct usb_xfer *xfer;
repeat:
TAILQ_FOREACH(xfer, &sc->sc_bus.intr_q.head, wait_entry) {
/*
* check if transfer is transferred
*/
if (uhci_check_transfer(xfer)) {
/* queue has been modified */
goto repeat;
}
}
}
/*------------------------------------------------------------------------*
* uhci_interrupt - UHCI interrupt handler
*
* NOTE: Do not access "sc->sc_bus.bdev" inside the interrupt handler,
* hence the interrupt handler will be setup before "sc->sc_bus.bdev"
* is present !
*------------------------------------------------------------------------*/
void
uhci_interrupt(uhci_softc_t *sc)
{
uint32_t status;
USB_BUS_LOCK(&sc->sc_bus);
DPRINTFN(16, "real interrupt\n");
#ifdef USB_DEBUG
if (uhcidebug > 15) {
uhci_dumpregs(sc);
}
#endif
status = UREAD2(sc, UHCI_STS) & UHCI_STS_ALLINTRS;
if (status == 0) {
/* the interrupt was not for us */
goto done;
}
if (status & (UHCI_STS_RD | UHCI_STS_HSE |
UHCI_STS_HCPE | UHCI_STS_HCH)) {
if (status & UHCI_STS_RD) {
#ifdef USB_DEBUG
printf("%s: resume detect\n",
__FUNCTION__);
#endif
}
if (status & UHCI_STS_HSE) {
printf("%s: host system error\n",
__FUNCTION__);
}
if (status & UHCI_STS_HCPE) {
printf("%s: host controller process error\n",
__FUNCTION__);
}
if (status & UHCI_STS_HCH) {
/* no acknowledge needed */
DPRINTF("%s: host controller halted\n",
__FUNCTION__);
#ifdef USB_DEBUG
if (uhcidebug > 0) {
uhci_dump_all(sc);
}
#endif
}
}
/* get acknowledge bits */
status &= (UHCI_STS_USBINT |
UHCI_STS_USBEI |
UHCI_STS_RD |
UHCI_STS_HSE |
UHCI_STS_HCPE);
if (status == 0) {
/* nothing to acknowledge */
goto done;
}
/* acknowledge interrupts */
UWRITE2(sc, UHCI_STS, status);
/* poll all the USB transfers */
uhci_interrupt_poll(sc);
done:
USB_BUS_UNLOCK(&sc->sc_bus);
}
/*
* called when a request does not complete
*/
static void
uhci_timeout(void *arg)
{
struct usb_xfer *xfer = arg;
DPRINTF("xfer=%p\n", xfer);
USB_BUS_LOCK_ASSERT(xfer->xroot->bus, MA_OWNED);
/* transfer is transferred */
uhci_device_done(xfer, USB_ERR_TIMEOUT);
}
static void
uhci_do_poll(struct usb_bus *bus)
{
struct uhci_softc *sc = UHCI_BUS2SC(bus);
USB_BUS_LOCK(&sc->sc_bus);
uhci_interrupt_poll(sc);
USB_BUS_UNLOCK(&sc->sc_bus);
}
static void
uhci_setup_standard_chain_sub(struct uhci_std_temp *temp)
{
uhci_td_t *td;
uhci_td_t *td_next;
uhci_td_t *td_alt_next;
uint32_t average;
uint32_t len_old;
uint8_t shortpkt_old;
uint8_t precompute;
td_alt_next = NULL;
shortpkt_old = temp->shortpkt;
len_old = temp->len;
precompute = 1;
/* software is used to detect short incoming transfers */
if ((temp->td_token & htole32(UHCI_TD_PID)) == htole32(UHCI_TD_PID_IN)) {
temp->td_status |= htole32(UHCI_TD_SPD);
} else {
temp->td_status &= ~htole32(UHCI_TD_SPD);
}
temp->ml.buf_offset = 0;
restart:
temp->td_token &= ~htole32(UHCI_TD_SET_MAXLEN(0));
temp->td_token |= htole32(UHCI_TD_SET_MAXLEN(temp->average));
td = temp->td;
td_next = temp->td_next;
while (1) {
if (temp->len == 0) {
if (temp->shortpkt) {
break;
}
/* send a Zero Length Packet, ZLP, last */
temp->shortpkt = 1;
temp->td_token |= htole32(UHCI_TD_SET_MAXLEN(0));
average = 0;
} else {
average = temp->average;
if (temp->len < average) {
temp->shortpkt = 1;
temp->td_token &= ~htole32(UHCI_TD_SET_MAXLEN(0));
temp->td_token |= htole32(UHCI_TD_SET_MAXLEN(temp->len));
average = temp->len;
}
}
if (td_next == NULL) {
panic("%s: out of UHCI transfer descriptors!", __FUNCTION__);
}
/* get next TD */
td = td_next;
td_next = td->obj_next;
/* check if we are pre-computing */
if (precompute) {
/* update remaining length */
temp->len -= average;
continue;
}
/* fill out current TD */
td->td_status = temp->td_status;
td->td_token = temp->td_token;
/* update data toggle */
temp->td_token ^= htole32(UHCI_TD_SET_DT(1));
if (average == 0) {
td->len = 0;
td->td_buffer = 0;
td->fix_pc = NULL;
} else {
/* update remaining length */
temp->len -= average;
td->len = average;
/* fill out buffer pointer and do fixup, if any */
uhci_mem_layout_fixup(&temp->ml, td);
}
td->alt_next = td_alt_next;
if ((td_next == td_alt_next) && temp->setup_alt_next) {
/* we need to receive these frames one by one ! */
td->td_status |= htole32(UHCI_TD_IOC);
td->td_next = htole32(UHCI_PTR_T);
} else {
if (td_next) {
/* link the current TD with the next one */
td->td_next = td_next->td_self;
}
}
usb_pc_cpu_flush(td->page_cache);
}
if (precompute) {
precompute = 0;
/* setup alt next pointer, if any */
if (temp->last_frame) {
td_alt_next = NULL;
} else {
/* we use this field internally */
td_alt_next = td_next;
}
/* restore */
temp->shortpkt = shortpkt_old;
temp->len = len_old;
goto restart;
}
temp->td = td;
temp->td_next = td_next;
}
static uhci_td_t *
uhci_setup_standard_chain(struct usb_xfer *xfer)
{
struct uhci_std_temp temp;
uhci_td_t *td;
uint32_t x;
DPRINTFN(9, "addr=%d endpt=%d sumlen=%d speed=%d\n",
xfer->address, UE_GET_ADDR(xfer->endpointno),
xfer->sumlen, usbd_get_speed(xfer->xroot->udev));
temp.average = xfer->max_frame_size;
temp.max_frame_size = xfer->max_frame_size;
/* toggle the DMA set we are using */
xfer->flags_int.curr_dma_set ^= 1;
/* get next DMA set */
td = xfer->td_start[xfer->flags_int.curr_dma_set];
xfer->td_transfer_first = td;
xfer->td_transfer_cache = td;
temp.td = NULL;
temp.td_next = td;
temp.last_frame = 0;
temp.setup_alt_next = xfer->flags_int.short_frames_ok;
uhci_mem_layout_init(&temp.ml, xfer);
temp.td_status =
htole32(UHCI_TD_ZERO_ACTLEN(UHCI_TD_SET_ERRCNT(3) |
UHCI_TD_ACTIVE));
if (xfer->xroot->udev->speed == USB_SPEED_LOW) {
temp.td_status |= htole32(UHCI_TD_LS);
}
temp.td_token =
htole32(UHCI_TD_SET_ENDPT(xfer->endpointno) |
UHCI_TD_SET_DEVADDR(xfer->address));
if (xfer->endpoint->toggle_next) {
/* DATA1 is next */
temp.td_token |= htole32(UHCI_TD_SET_DT(1));
}
/* check if we should prepend a setup message */
if (xfer->flags_int.control_xfr) {
if (xfer->flags_int.control_hdr) {
temp.td_token &= htole32(UHCI_TD_SET_DEVADDR(0x7F) |
UHCI_TD_SET_ENDPT(0xF));
temp.td_token |= htole32(UHCI_TD_PID_SETUP |
UHCI_TD_SET_DT(0));
temp.len = xfer->frlengths[0];
temp.ml.buf_pc = xfer->frbuffers + 0;
temp.shortpkt = temp.len ? 1 : 0;
/* check for last frame */
if (xfer->nframes == 1) {
/* no STATUS stage yet, SETUP is last */
if (xfer->flags_int.control_act) {
temp.last_frame = 1;
temp.setup_alt_next = 0;
}
}
uhci_setup_standard_chain_sub(&temp);
}
x = 1;
} else {
x = 0;
}
while (x != xfer->nframes) {
/* DATA0 / DATA1 message */
temp.len = xfer->frlengths[x];
temp.ml.buf_pc = xfer->frbuffers + x;
x++;
if (x == xfer->nframes) {
if (xfer->flags_int.control_xfr) {
/* no STATUS stage yet, DATA is last */
if (xfer->flags_int.control_act) {
temp.last_frame = 1;
temp.setup_alt_next = 0;
}
} else {
temp.last_frame = 1;
temp.setup_alt_next = 0;
}
}
/*
* Keep previous data toggle,
* device address and endpoint number:
*/
temp.td_token &= htole32(UHCI_TD_SET_DEVADDR(0x7F) |
UHCI_TD_SET_ENDPT(0xF) |
UHCI_TD_SET_DT(1));
if (temp.len == 0) {
/* make sure that we send an USB packet */
temp.shortpkt = 0;
} else {
/* regular data transfer */
temp.shortpkt = (xfer->flags.force_short_xfer) ? 0 : 1;
}
/* set endpoint direction */
temp.td_token |=
(UE_GET_DIR(xfer->endpointno) == UE_DIR_IN) ?
htole32(UHCI_TD_PID_IN) :
htole32(UHCI_TD_PID_OUT);
uhci_setup_standard_chain_sub(&temp);
}
/* check if we should append a status stage */
if (xfer->flags_int.control_xfr &&
!xfer->flags_int.control_act) {
/*
* send a DATA1 message and reverse the current endpoint
* direction
*/
temp.td_token &= htole32(UHCI_TD_SET_DEVADDR(0x7F) |
UHCI_TD_SET_ENDPT(0xF) |
UHCI_TD_SET_DT(1));
temp.td_token |=
(UE_GET_DIR(xfer->endpointno) == UE_DIR_OUT) ?
htole32(UHCI_TD_PID_IN | UHCI_TD_SET_DT(1)) :
htole32(UHCI_TD_PID_OUT | UHCI_TD_SET_DT(1));
temp.len = 0;
temp.ml.buf_pc = NULL;
temp.shortpkt = 0;
temp.last_frame = 1;
temp.setup_alt_next = 0;
uhci_setup_standard_chain_sub(&temp);
}
td = temp.td;
/* Ensure that last TD is terminating: */
td->td_next = htole32(UHCI_PTR_T);
/* set interrupt bit */
td->td_status |= htole32(UHCI_TD_IOC);
usb_pc_cpu_flush(td->page_cache);
/* must have at least one frame! */
xfer->td_transfer_last = td;
#ifdef USB_DEBUG
if (uhcidebug > 8) {
DPRINTF("nexttog=%d; data before transfer:\n",
xfer->endpoint->toggle_next);
uhci_dump_tds(xfer->td_transfer_first);
}
#endif
return (xfer->td_transfer_first);
}
/* NOTE: "done" can be run two times in a row,
* from close and from interrupt
*/
static void
uhci_device_done(struct usb_xfer *xfer, usb_error_t error)
{
struct usb_pipe_methods *methods = xfer->endpoint->methods;
uhci_softc_t *sc = UHCI_BUS2SC(xfer->xroot->bus);
uhci_qh_t *qh;
USB_BUS_LOCK_ASSERT(&sc->sc_bus, MA_OWNED);
DPRINTFN(2, "xfer=%p, endpoint=%p, error=%d\n",
xfer, xfer->endpoint, error);
qh = xfer->qh_start[xfer->flags_int.curr_dma_set];
if (qh) {
usb_pc_cpu_invalidate(qh->page_cache);
}
if (xfer->flags_int.bandwidth_reclaimed) {
xfer->flags_int.bandwidth_reclaimed = 0;
uhci_rem_loop(sc);
}
if (methods == &uhci_device_bulk_methods) {
UHCI_REMOVE_QH(qh, sc->sc_bulk_p_last);
}
if (methods == &uhci_device_ctrl_methods) {
if (xfer->xroot->udev->speed == USB_SPEED_LOW) {
UHCI_REMOVE_QH(qh, sc->sc_ls_ctl_p_last);
} else {
UHCI_REMOVE_QH(qh, sc->sc_fs_ctl_p_last);
}
}
if (methods == &uhci_device_intr_methods) {
UHCI_REMOVE_QH(qh, sc->sc_intr_p_last[xfer->qh_pos]);
}
/*
* Only finish isochronous transfers once
* which will update "xfer->frlengths".
*/
if (xfer->td_transfer_first &&
xfer->td_transfer_last) {
if (methods == &uhci_device_isoc_methods) {
uhci_isoc_done(sc, xfer);
}
xfer->td_transfer_first = NULL;
xfer->td_transfer_last = NULL;
}
/* dequeue transfer and start next transfer */
usbd_transfer_done(xfer, error);
}
/*------------------------------------------------------------------------*
* uhci bulk support
*------------------------------------------------------------------------*/
static void
uhci_device_bulk_open(struct usb_xfer *xfer)
{
return;
}
static void
uhci_device_bulk_close(struct usb_xfer *xfer)
{
uhci_device_done(xfer, USB_ERR_CANCELLED);
}
static void
uhci_device_bulk_enter(struct usb_xfer *xfer)
{
return;
}
static void
uhci_device_bulk_start(struct usb_xfer *xfer)
{
uhci_softc_t *sc = UHCI_BUS2SC(xfer->xroot->bus);
uhci_td_t *td;
uhci_qh_t *qh;
/* setup TD's */
td = uhci_setup_standard_chain(xfer);
/* setup QH */
qh = xfer->qh_start[xfer->flags_int.curr_dma_set];
qh->e_next = td;
qh->qh_e_next = td->td_self;
if (xfer->xroot->udev->flags.self_suspended == 0) {
UHCI_APPEND_QH(qh, sc->sc_bulk_p_last);
uhci_add_loop(sc);
xfer->flags_int.bandwidth_reclaimed = 1;
} else {
usb_pc_cpu_flush(qh->page_cache);
}
/* put transfer on interrupt queue */
uhci_transfer_intr_enqueue(xfer);
}
struct usb_pipe_methods uhci_device_bulk_methods =
{
.open = uhci_device_bulk_open,
.close = uhci_device_bulk_close,
.enter = uhci_device_bulk_enter,
.start = uhci_device_bulk_start,
};
/*------------------------------------------------------------------------*
* uhci control support
*------------------------------------------------------------------------*/
static void
uhci_device_ctrl_open(struct usb_xfer *xfer)
{
return;
}
static void
uhci_device_ctrl_close(struct usb_xfer *xfer)
{
uhci_device_done(xfer, USB_ERR_CANCELLED);
}
static void
uhci_device_ctrl_enter(struct usb_xfer *xfer)
{
return;
}
static void
uhci_device_ctrl_start(struct usb_xfer *xfer)
{
uhci_softc_t *sc = UHCI_BUS2SC(xfer->xroot->bus);
uhci_qh_t *qh;
uhci_td_t *td;
/* setup TD's */
td = uhci_setup_standard_chain(xfer);
/* setup QH */
qh = xfer->qh_start[xfer->flags_int.curr_dma_set];
qh->e_next = td;
qh->qh_e_next = td->td_self;
/*
* NOTE: some devices choke on bandwidth- reclamation for control
* transfers
*/
if (xfer->xroot->udev->flags.self_suspended == 0) {
if (xfer->xroot->udev->speed == USB_SPEED_LOW) {
UHCI_APPEND_QH(qh, sc->sc_ls_ctl_p_last);
} else {
UHCI_APPEND_QH(qh, sc->sc_fs_ctl_p_last);
}
} else {
usb_pc_cpu_flush(qh->page_cache);
}
/* put transfer on interrupt queue */
uhci_transfer_intr_enqueue(xfer);
}
struct usb_pipe_methods uhci_device_ctrl_methods =
{
.open = uhci_device_ctrl_open,
.close = uhci_device_ctrl_close,
.enter = uhci_device_ctrl_enter,
.start = uhci_device_ctrl_start,
};
/*------------------------------------------------------------------------*
* uhci interrupt support
*------------------------------------------------------------------------*/
static void
uhci_device_intr_open(struct usb_xfer *xfer)
{
uhci_softc_t *sc = UHCI_BUS2SC(xfer->xroot->bus);
uint16_t best;
uint16_t bit;
uint16_t x;
best = 0;
bit = UHCI_IFRAMELIST_COUNT / 2;
while (bit) {
if (xfer->interval >= bit) {
x = bit;
best = bit;
while (x & bit) {
if (sc->sc_intr_stat[x] <
sc->sc_intr_stat[best]) {
best = x;
}
x++;
}
break;
}
bit >>= 1;
}
sc->sc_intr_stat[best]++;
xfer->qh_pos = best;
DPRINTFN(3, "best=%d interval=%d\n",
best, xfer->interval);
}
static void
uhci_device_intr_close(struct usb_xfer *xfer)
{
uhci_softc_t *sc = UHCI_BUS2SC(xfer->xroot->bus);
sc->sc_intr_stat[xfer->qh_pos]--;
uhci_device_done(xfer, USB_ERR_CANCELLED);
}
static void
uhci_device_intr_enter(struct usb_xfer *xfer)
{
return;
}
static void
uhci_device_intr_start(struct usb_xfer *xfer)
{
uhci_softc_t *sc = UHCI_BUS2SC(xfer->xroot->bus);
uhci_qh_t *qh;
uhci_td_t *td;
/* setup TD's */
td = uhci_setup_standard_chain(xfer);
/* setup QH */
qh = xfer->qh_start[xfer->flags_int.curr_dma_set];
qh->e_next = td;
qh->qh_e_next = td->td_self;
if (xfer->xroot->udev->flags.self_suspended == 0) {
/* enter QHs into the controller data structures */
UHCI_APPEND_QH(qh, sc->sc_intr_p_last[xfer->qh_pos]);
} else {
usb_pc_cpu_flush(qh->page_cache);
}
/* put transfer on interrupt queue */
uhci_transfer_intr_enqueue(xfer);
}
struct usb_pipe_methods uhci_device_intr_methods =
{
.open = uhci_device_intr_open,
.close = uhci_device_intr_close,
.enter = uhci_device_intr_enter,
.start = uhci_device_intr_start,
};
/*------------------------------------------------------------------------*
* uhci isochronous support
*------------------------------------------------------------------------*/
static void
uhci_device_isoc_open(struct usb_xfer *xfer)
{
uhci_td_t *td;
uint32_t td_token;
uint8_t ds;
td_token =
(UE_GET_DIR(xfer->endpointno) == UE_DIR_IN) ?
UHCI_TD_IN(0, xfer->endpointno, xfer->address, 0) :
UHCI_TD_OUT(0, xfer->endpointno, xfer->address, 0);
td_token = htole32(td_token);
/* initialize all TD's */
for (ds = 0; ds != 2; ds++) {
for (td = xfer->td_start[ds]; td; td = td->obj_next) {
/* mark TD as inactive */
td->td_status = htole32(UHCI_TD_IOS);
td->td_token = td_token;
usb_pc_cpu_flush(td->page_cache);
}
}
}
static void
uhci_device_isoc_close(struct usb_xfer *xfer)
{
uhci_device_done(xfer, USB_ERR_CANCELLED);
}
static void
uhci_device_isoc_enter(struct usb_xfer *xfer)
{
struct uhci_mem_layout ml;
uhci_softc_t *sc = UHCI_BUS2SC(xfer->xroot->bus);
uint32_t nframes;
uint32_t temp;
uint32_t *plen;
#ifdef USB_DEBUG
uint8_t once = 1;
#endif
uhci_td_t *td;
uhci_td_t *td_last = NULL;
uhci_td_t **pp_last;
DPRINTFN(6, "xfer=%p next=%d nframes=%d\n",
xfer, xfer->endpoint->isoc_next, xfer->nframes);
nframes = UREAD2(sc, UHCI_FRNUM);
temp = (nframes - xfer->endpoint->isoc_next) &
(UHCI_VFRAMELIST_COUNT - 1);
if ((xfer->endpoint->is_synced == 0) ||
(temp < xfer->nframes)) {
/*
* If there is data underflow or the pipe queue is empty we
* schedule the transfer a few frames ahead of the current
* frame position. Else two isochronous transfers might
* overlap.
*/
xfer->endpoint->isoc_next = (nframes + 3) & (UHCI_VFRAMELIST_COUNT - 1);
xfer->endpoint->is_synced = 1;
DPRINTFN(3, "start next=%d\n", xfer->endpoint->isoc_next);
}
/*
* compute how many milliseconds the insertion is ahead of the
* current frame position:
*/
temp = (xfer->endpoint->isoc_next - nframes) &
(UHCI_VFRAMELIST_COUNT - 1);
/*
* pre-compute when the isochronous transfer will be finished:
*/
xfer->isoc_time_complete =
usb_isoc_time_expand(&sc->sc_bus, nframes) + temp +
xfer->nframes;
/* get the real number of frames */
nframes = xfer->nframes;
uhci_mem_layout_init(&ml, xfer);
plen = xfer->frlengths;
/* toggle the DMA set we are using */
xfer->flags_int.curr_dma_set ^= 1;
/* get next DMA set */
td = xfer->td_start[xfer->flags_int.curr_dma_set];
xfer->td_transfer_first = td;
pp_last = &sc->sc_isoc_p_last[xfer->endpoint->isoc_next];
/* store starting position */
xfer->qh_pos = xfer->endpoint->isoc_next;
while (nframes--) {
if (td == NULL) {
panic("%s:%d: out of TD's\n",
__FUNCTION__, __LINE__);
}
if (pp_last >= &sc->sc_isoc_p_last[UHCI_VFRAMELIST_COUNT]) {
pp_last = &sc->sc_isoc_p_last[0];
}
if (*plen > xfer->max_frame_size) {
#ifdef USB_DEBUG
if (once) {
once = 0;
printf("%s: frame length(%d) exceeds %d "
"bytes (frame truncated)\n",
__FUNCTION__, *plen,
xfer->max_frame_size);
}
#endif
*plen = xfer->max_frame_size;
}
/* reuse td_token from last transfer */
td->td_token &= htole32(~UHCI_TD_MAXLEN_MASK);
td->td_token |= htole32(UHCI_TD_SET_MAXLEN(*plen));
td->len = *plen;
if (td->len == 0) {
/*
* Do not call "uhci_mem_layout_fixup()" when the
* length is zero!
*/
td->td_buffer = 0;
td->fix_pc = NULL;
} else {
/* fill out buffer pointer and do fixup, if any */
uhci_mem_layout_fixup(&ml, td);
}
/* update status */
if (nframes == 0) {
td->td_status = htole32
(UHCI_TD_ZERO_ACTLEN
(UHCI_TD_SET_ERRCNT(0) |
UHCI_TD_ACTIVE |
UHCI_TD_IOS |
UHCI_TD_IOC));
} else {
td->td_status = htole32
(UHCI_TD_ZERO_ACTLEN
(UHCI_TD_SET_ERRCNT(0) |
UHCI_TD_ACTIVE |
UHCI_TD_IOS));
}
usb_pc_cpu_flush(td->page_cache);
#ifdef USB_DEBUG
if (uhcidebug > 5) {
DPRINTF("TD %d\n", nframes);
uhci_dump_td(td);
}
#endif
/* insert TD into schedule */
UHCI_APPEND_TD(td, *pp_last);
pp_last++;
plen++;
td_last = td;
td = td->obj_next;
}
xfer->td_transfer_last = td_last;
/* update isoc_next */
xfer->endpoint->isoc_next = (pp_last - &sc->sc_isoc_p_last[0]) &
(UHCI_VFRAMELIST_COUNT - 1);
}
static void
uhci_device_isoc_start(struct usb_xfer *xfer)
{
/* put transfer on interrupt queue */
uhci_transfer_intr_enqueue(xfer);
}
struct usb_pipe_methods uhci_device_isoc_methods =
{
.open = uhci_device_isoc_open,
.close = uhci_device_isoc_close,
.enter = uhci_device_isoc_enter,
.start = uhci_device_isoc_start,
};
/*------------------------------------------------------------------------*
* uhci root control support
*------------------------------------------------------------------------*
* Simulate a hardware hub by handling all the necessary requests.
*------------------------------------------------------------------------*/
static const
struct usb_device_descriptor uhci_devd =
{
sizeof(struct usb_device_descriptor),
UDESC_DEVICE, /* type */
{0x00, 0x01}, /* USB version */
UDCLASS_HUB, /* class */
UDSUBCLASS_HUB, /* subclass */
UDPROTO_FSHUB, /* protocol */
64, /* max packet */
{0}, {0}, {0x00, 0x01}, /* device id */
1, 2, 0, /* string indicies */
1 /* # of configurations */
};
static const struct uhci_config_desc uhci_confd = {
.confd = {
.bLength = sizeof(struct usb_config_descriptor),
.bDescriptorType = UDESC_CONFIG,
.wTotalLength[0] = sizeof(uhci_confd),
.bNumInterface = 1,
.bConfigurationValue = 1,
.iConfiguration = 0,
.bmAttributes = UC_SELF_POWERED,
.bMaxPower = 0 /* max power */
},
.ifcd = {
.bLength = sizeof(struct usb_interface_descriptor),
.bDescriptorType = UDESC_INTERFACE,
.bNumEndpoints = 1,
.bInterfaceClass = UICLASS_HUB,
.bInterfaceSubClass = UISUBCLASS_HUB,
.bInterfaceProtocol = UIPROTO_FSHUB,
},
.endpd = {
.bLength = sizeof(struct usb_endpoint_descriptor),
.bDescriptorType = UDESC_ENDPOINT,
.bEndpointAddress = UE_DIR_IN | UHCI_INTR_ENDPT,
.bmAttributes = UE_INTERRUPT,
.wMaxPacketSize[0] = 8, /* max packet (63 ports) */
.bInterval = 255,
},
};
static const
struct usb_hub_descriptor_min uhci_hubd_piix =
{
.bDescLength = sizeof(uhci_hubd_piix),
.bDescriptorType = UDESC_HUB,
.bNbrPorts = 2,
.wHubCharacteristics = {UHD_PWR_NO_SWITCH | UHD_OC_INDIVIDUAL, 0},
.bPwrOn2PwrGood = 50,
};
/*
* The USB hub protocol requires that SET_FEATURE(PORT_RESET) also
* enables the port, and also states that SET_FEATURE(PORT_ENABLE)
* should not be used by the USB subsystem. As we cannot issue a
* SET_FEATURE(PORT_ENABLE) externally, we must ensure that the port
* will be enabled as part of the reset.
*
* On the VT83C572, the port cannot be successfully enabled until the
* outstanding "port enable change" and "connection status change"
* events have been reset.
*/
static usb_error_t
uhci_portreset(uhci_softc_t *sc, uint16_t index)
{
uint16_t port;
uint16_t x;
uint8_t lim;
if (index == 1)
port = UHCI_PORTSC1;
else if (index == 2)
port = UHCI_PORTSC2;
else
return (USB_ERR_IOERROR);
/*
* Before we do anything, turn on SOF messages on the USB
* BUS. Some USB devices do not cope without them!
*/
uhci_restart(sc);
x = URWMASK(UREAD2(sc, port));
UWRITE2(sc, port, x | UHCI_PORTSC_PR);
usb_pause_mtx(&sc->sc_bus.bus_mtx,
USB_MS_TO_TICKS(usb_port_root_reset_delay));
DPRINTFN(4, "uhci port %d reset, status0 = 0x%04x\n",
index, UREAD2(sc, port));
x = URWMASK(UREAD2(sc, port));
UWRITE2(sc, port, x & ~UHCI_PORTSC_PR);
mtx_unlock(&sc->sc_bus.bus_mtx);
/*
* This delay needs to be exactly 100us, else some USB devices
* fail to attach!
*/
DELAY(100);
mtx_lock(&sc->sc_bus.bus_mtx);
DPRINTFN(4, "uhci port %d reset, status1 = 0x%04x\n",
index, UREAD2(sc, port));
x = URWMASK(UREAD2(sc, port));
UWRITE2(sc, port, x | UHCI_PORTSC_PE);
for (lim = 0; lim < 12; lim++) {
usb_pause_mtx(&sc->sc_bus.bus_mtx,
USB_MS_TO_TICKS(usb_port_reset_delay));
x = UREAD2(sc, port);
DPRINTFN(4, "uhci port %d iteration %u, status = 0x%04x\n",
index, lim, x);
if (!(x & UHCI_PORTSC_CCS)) {
/*
* No device is connected (or was disconnected
* during reset). Consider the port reset.
* The delay must be long enough to ensure on
* the initial iteration that the device
* connection will have been registered. 50ms
* appears to be sufficient, but 20ms is not.
*/
DPRINTFN(4, "uhci port %d loop %u, device detached\n",
index, lim);
goto done;
}
if (x & (UHCI_PORTSC_POEDC | UHCI_PORTSC_CSC)) {
/*
* Port enabled changed and/or connection
* status changed were set. Reset either or
* both raised flags (by writing a 1 to that
* bit), and wait again for state to settle.
*/
UWRITE2(sc, port, URWMASK(x) |
(x & (UHCI_PORTSC_POEDC | UHCI_PORTSC_CSC)));
continue;
}
if (x & UHCI_PORTSC_PE) {
/* port is enabled */
goto done;
}
UWRITE2(sc, port, URWMASK(x) | UHCI_PORTSC_PE);
}
DPRINTFN(2, "uhci port %d reset timed out\n", index);
return (USB_ERR_TIMEOUT);
done:
DPRINTFN(4, "uhci port %d reset, status2 = 0x%04x\n",
index, UREAD2(sc, port));
sc->sc_isreset = 1;
return (USB_ERR_NORMAL_COMPLETION);
}
static usb_error_t
uhci_roothub_exec(struct usb_device *udev,
struct usb_device_request *req, const void **pptr, uint16_t *plength)
{
uhci_softc_t *sc = UHCI_BUS2SC(udev->bus);
const void *ptr;
const char *str_ptr;
uint16_t x;
uint16_t port;
uint16_t value;
uint16_t index;
uint16_t status;
uint16_t change;
uint16_t len;
usb_error_t err;
USB_BUS_LOCK_ASSERT(&sc->sc_bus, MA_OWNED);
/* buffer reset */
ptr = (const void *)&sc->sc_hub_desc.temp;
len = 0;
err = 0;
value = UGETW(req->wValue);
index = UGETW(req->wIndex);
DPRINTFN(3, "type=0x%02x request=0x%02x wLen=0x%04x "
"wValue=0x%04x wIndex=0x%04x\n",
req->bmRequestType, req->bRequest,
UGETW(req->wLength), value, index);
#define C(x,y) ((x) | ((y) << 8))
switch (C(req->bRequest, req->bmRequestType)) {
case C(UR_CLEAR_FEATURE, UT_WRITE_DEVICE):
case C(UR_CLEAR_FEATURE, UT_WRITE_INTERFACE):
case C(UR_CLEAR_FEATURE, UT_WRITE_ENDPOINT):
/*
* DEVICE_REMOTE_WAKEUP and ENDPOINT_HALT are no-ops
* for the integrated root hub.
*/
break;
case C(UR_GET_CONFIG, UT_READ_DEVICE):
len = 1;
sc->sc_hub_desc.temp[0] = sc->sc_conf;
break;
case C(UR_GET_DESCRIPTOR, UT_READ_DEVICE):
switch (value >> 8) {
case UDESC_DEVICE:
if ((value & 0xff) != 0) {
err = USB_ERR_IOERROR;
goto done;
}
len = sizeof(uhci_devd);
ptr = (const void *)&uhci_devd;
break;
case UDESC_CONFIG:
if ((value & 0xff) != 0) {
err = USB_ERR_IOERROR;
goto done;
}
len = sizeof(uhci_confd);
ptr = (const void *)&uhci_confd;
break;
case UDESC_STRING:
switch (value & 0xff) {
case 0: /* Language table */
str_ptr = "\001";
break;
case 1: /* Vendor */
str_ptr = sc->sc_vendor;
break;
case 2: /* Product */
str_ptr = "UHCI root HUB";
break;
default:
str_ptr = "";
break;
}
len = usb_make_str_desc
(sc->sc_hub_desc.temp,
sizeof(sc->sc_hub_desc.temp),
str_ptr);
break;
default:
err = USB_ERR_IOERROR;
goto done;
}
break;
case C(UR_GET_INTERFACE, UT_READ_INTERFACE):
len = 1;
sc->sc_hub_desc.temp[0] = 0;
break;
case C(UR_GET_STATUS, UT_READ_DEVICE):
len = 2;
USETW(sc->sc_hub_desc.stat.wStatus, UDS_SELF_POWERED);
break;
case C(UR_GET_STATUS, UT_READ_INTERFACE):
case C(UR_GET_STATUS, UT_READ_ENDPOINT):
len = 2;
USETW(sc->sc_hub_desc.stat.wStatus, 0);
break;
case C(UR_SET_ADDRESS, UT_WRITE_DEVICE):
if (value >= UHCI_MAX_DEVICES) {
err = USB_ERR_IOERROR;
goto done;
}
sc->sc_addr = value;
break;
case C(UR_SET_CONFIG, UT_WRITE_DEVICE):
if ((value != 0) && (value != 1)) {
err = USB_ERR_IOERROR;
goto done;
}
sc->sc_conf = value;
break;
case C(UR_SET_DESCRIPTOR, UT_WRITE_DEVICE):
break;
case C(UR_SET_FEATURE, UT_WRITE_DEVICE):
case C(UR_SET_FEATURE, UT_WRITE_INTERFACE):
case C(UR_SET_FEATURE, UT_WRITE_ENDPOINT):
err = USB_ERR_IOERROR;
goto done;
case C(UR_SET_INTERFACE, UT_WRITE_INTERFACE):
break;
case C(UR_SYNCH_FRAME, UT_WRITE_ENDPOINT):
break;
/* Hub requests */
case C(UR_CLEAR_FEATURE, UT_WRITE_CLASS_DEVICE):
break;
case C(UR_CLEAR_FEATURE, UT_WRITE_CLASS_OTHER):
DPRINTFN(4, "UR_CLEAR_PORT_FEATURE "
"port=%d feature=%d\n",
index, value);
if (index == 1)
port = UHCI_PORTSC1;
else if (index == 2)
port = UHCI_PORTSC2;
else {
err = USB_ERR_IOERROR;
goto done;
}
switch (value) {
case UHF_PORT_ENABLE:
x = URWMASK(UREAD2(sc, port));
UWRITE2(sc, port, x & ~UHCI_PORTSC_PE);
break;
case UHF_PORT_SUSPEND:
x = URWMASK(UREAD2(sc, port));
UWRITE2(sc, port, x & ~(UHCI_PORTSC_SUSP));
break;
case UHF_PORT_RESET:
x = URWMASK(UREAD2(sc, port));
UWRITE2(sc, port, x & ~UHCI_PORTSC_PR);
break;
case UHF_C_PORT_CONNECTION:
x = URWMASK(UREAD2(sc, port));
UWRITE2(sc, port, x | UHCI_PORTSC_CSC);
break;
case UHF_C_PORT_ENABLE:
x = URWMASK(UREAD2(sc, port));
UWRITE2(sc, port, x | UHCI_PORTSC_POEDC);
break;
case UHF_C_PORT_OVER_CURRENT:
x = URWMASK(UREAD2(sc, port));
UWRITE2(sc, port, x | UHCI_PORTSC_OCIC);
break;
case UHF_C_PORT_RESET:
sc->sc_isreset = 0;
err = USB_ERR_NORMAL_COMPLETION;
goto done;
case UHF_C_PORT_SUSPEND:
sc->sc_isresumed &= ~(1 << index);
break;
case UHF_PORT_CONNECTION:
case UHF_PORT_OVER_CURRENT:
case UHF_PORT_POWER:
case UHF_PORT_LOW_SPEED:
default:
err = USB_ERR_IOERROR;
goto done;
}
break;
case C(UR_GET_BUS_STATE, UT_READ_CLASS_OTHER):
if (index == 1)
port = UHCI_PORTSC1;
else if (index == 2)
port = UHCI_PORTSC2;
else {
err = USB_ERR_IOERROR;
goto done;
}
len = 1;
sc->sc_hub_desc.temp[0] =
((UREAD2(sc, port) & UHCI_PORTSC_LS) >>
UHCI_PORTSC_LS_SHIFT);
break;
case C(UR_GET_DESCRIPTOR, UT_READ_CLASS_DEVICE):
if ((value & 0xff) != 0) {
err = USB_ERR_IOERROR;
goto done;
}
len = sizeof(uhci_hubd_piix);
ptr = (const void *)&uhci_hubd_piix;
break;
case C(UR_GET_STATUS, UT_READ_CLASS_DEVICE):
len = 16;
memset(sc->sc_hub_desc.temp, 0, 16);
break;
case C(UR_GET_STATUS, UT_READ_CLASS_OTHER):
if (index == 1)
port = UHCI_PORTSC1;
else if (index == 2)
port = UHCI_PORTSC2;
else {
err = USB_ERR_IOERROR;
goto done;
}
x = UREAD2(sc, port);
status = change = 0;
if (x & UHCI_PORTSC_CCS)
status |= UPS_CURRENT_CONNECT_STATUS;
if (x & UHCI_PORTSC_CSC)
change |= UPS_C_CONNECT_STATUS;
if (x & UHCI_PORTSC_PE)
status |= UPS_PORT_ENABLED;
if (x & UHCI_PORTSC_POEDC)
change |= UPS_C_PORT_ENABLED;
if (x & UHCI_PORTSC_OCI)
status |= UPS_OVERCURRENT_INDICATOR;
if (x & UHCI_PORTSC_OCIC)
change |= UPS_C_OVERCURRENT_INDICATOR;
if (x & UHCI_PORTSC_LSDA)
status |= UPS_LOW_SPEED;
if ((x & UHCI_PORTSC_PE) && (x & UHCI_PORTSC_RD)) {
/* need to do a write back */
UWRITE2(sc, port, URWMASK(x));
/* wait 20ms for resume sequence to complete */
usb_pause_mtx(&sc->sc_bus.bus_mtx, hz / 50);
/* clear suspend and resume detect */
UWRITE2(sc, port, URWMASK(x) & ~(UHCI_PORTSC_RD |
UHCI_PORTSC_SUSP));
/* wait a little bit */
usb_pause_mtx(&sc->sc_bus.bus_mtx, hz / 500);
sc->sc_isresumed |= (1 << index);
} else if (x & UHCI_PORTSC_SUSP) {
status |= UPS_SUSPEND;
}
status |= UPS_PORT_POWER;
if (sc->sc_isresumed & (1 << index))
change |= UPS_C_SUSPEND;
if (sc->sc_isreset)
change |= UPS_C_PORT_RESET;
USETW(sc->sc_hub_desc.ps.wPortStatus, status);
USETW(sc->sc_hub_desc.ps.wPortChange, change);
len = sizeof(sc->sc_hub_desc.ps);
break;
case C(UR_SET_DESCRIPTOR, UT_WRITE_CLASS_DEVICE):
err = USB_ERR_IOERROR;
goto done;
case C(UR_SET_FEATURE, UT_WRITE_CLASS_DEVICE):
break;
case C(UR_SET_FEATURE, UT_WRITE_CLASS_OTHER):
if (index == 1)
port = UHCI_PORTSC1;
else if (index == 2)
port = UHCI_PORTSC2;
else {
err = USB_ERR_IOERROR;
goto done;
}
switch (value) {
case UHF_PORT_ENABLE:
x = URWMASK(UREAD2(sc, port));
UWRITE2(sc, port, x | UHCI_PORTSC_PE);
break;
case UHF_PORT_SUSPEND:
x = URWMASK(UREAD2(sc, port));
UWRITE2(sc, port, x | UHCI_PORTSC_SUSP);
break;
case UHF_PORT_RESET:
err = uhci_portreset(sc, index);
goto done;
case UHF_PORT_POWER:
/* pretend we turned on power */
err = USB_ERR_NORMAL_COMPLETION;
goto done;
case UHF_C_PORT_CONNECTION:
case UHF_C_PORT_ENABLE:
case UHF_C_PORT_OVER_CURRENT:
case UHF_PORT_CONNECTION:
case UHF_PORT_OVER_CURRENT:
case UHF_PORT_LOW_SPEED:
case UHF_C_PORT_SUSPEND:
case UHF_C_PORT_RESET:
default:
err = USB_ERR_IOERROR;
goto done;
}
break;
default:
err = USB_ERR_IOERROR;
goto done;
}
done:
*plength = len;
*pptr = ptr;
return (err);
}
/*
* This routine is executed periodically and simulates interrupts from
* the root controller interrupt pipe for port status change:
*/
static void
uhci_root_intr(uhci_softc_t *sc)
{
DPRINTFN(21, "\n");
USB_BUS_LOCK_ASSERT(&sc->sc_bus, MA_OWNED);
sc->sc_hub_idata[0] = 0;
if (UREAD2(sc, UHCI_PORTSC1) & (UHCI_PORTSC_CSC |
UHCI_PORTSC_OCIC | UHCI_PORTSC_RD)) {
sc->sc_hub_idata[0] |= 1 << 1;
}
if (UREAD2(sc, UHCI_PORTSC2) & (UHCI_PORTSC_CSC |
UHCI_PORTSC_OCIC | UHCI_PORTSC_RD)) {
sc->sc_hub_idata[0] |= 1 << 2;
}
/* restart timer */
usb_callout_reset(&sc->sc_root_intr, hz,
(void *)&uhci_root_intr, sc);
if (sc->sc_hub_idata[0] != 0) {
uhub_root_intr(&sc->sc_bus, sc->sc_hub_idata,
sizeof(sc->sc_hub_idata));
}
}
static void
uhci_xfer_setup(struct usb_setup_params *parm)
{
struct usb_page_search page_info;
struct usb_page_cache *pc;
uhci_softc_t *sc;
struct usb_xfer *xfer;
void *last_obj;
uint32_t ntd;
uint32_t nqh;
uint32_t nfixup;
uint32_t n;
uint16_t align;
sc = UHCI_BUS2SC(parm->udev->bus);
xfer = parm->curr_xfer;
parm->hc_max_packet_size = 0x500;
parm->hc_max_packet_count = 1;
parm->hc_max_frame_size = 0x500;
/*
* compute ntd and nqh
*/
if (parm->methods == &uhci_device_ctrl_methods) {
xfer->flags_int.bdma_enable = 1;
xfer->flags_int.bdma_no_post_sync = 1;
usbd_transfer_setup_sub(parm);
/* see EHCI HC driver for proof of "ntd" formula */
nqh = 1;
ntd = ((2 * xfer->nframes) + 1 /* STATUS */
+ (xfer->max_data_length / xfer->max_frame_size));
} else if (parm->methods == &uhci_device_bulk_methods) {
xfer->flags_int.bdma_enable = 1;
xfer->flags_int.bdma_no_post_sync = 1;
usbd_transfer_setup_sub(parm);
nqh = 1;
ntd = ((2 * xfer->nframes)
+ (xfer->max_data_length / xfer->max_frame_size));
} else if (parm->methods == &uhci_device_intr_methods) {
xfer->flags_int.bdma_enable = 1;
xfer->flags_int.bdma_no_post_sync = 1;
usbd_transfer_setup_sub(parm);
nqh = 1;
ntd = ((2 * xfer->nframes)
+ (xfer->max_data_length / xfer->max_frame_size));
} else if (parm->methods == &uhci_device_isoc_methods) {
xfer->flags_int.bdma_enable = 1;
xfer->flags_int.bdma_no_post_sync = 1;
usbd_transfer_setup_sub(parm);
nqh = 0;
ntd = xfer->nframes;
} else {
usbd_transfer_setup_sub(parm);
nqh = 0;
ntd = 0;
}
if (parm->err) {
return;
}
/*
* NOTE: the UHCI controller requires that
* every packet must be contiguous on
* the same USB memory page !
*/
nfixup = (parm->bufsize / USB_PAGE_SIZE) + 1;
/*
* Compute a suitable power of two alignment
* for our "max_frame_size" fixup buffer(s):
*/
align = xfer->max_frame_size;
n = 0;
while (align) {
align >>= 1;
n++;
}
/* check for power of two */
if (!(xfer->max_frame_size &
(xfer->max_frame_size - 1))) {
n--;
}
/*
* We don't allow alignments of
* less than 8 bytes:
*
* NOTE: Allocating using an aligment
* of 1 byte has special meaning!
*/
if (n < 3) {
n = 3;
}
align = (1 << n);
if (usbd_transfer_setup_sub_malloc(
parm, &pc, xfer->max_frame_size,
align, nfixup)) {
parm->err = USB_ERR_NOMEM;
return;
}
xfer->buf_fixup = pc;
alloc_dma_set:
if (parm->err) {
return;
}
last_obj = NULL;
if (usbd_transfer_setup_sub_malloc(
parm, &pc, sizeof(uhci_td_t),
UHCI_TD_ALIGN, ntd)) {
parm->err = USB_ERR_NOMEM;
return;
}
if (parm->buf) {
for (n = 0; n != ntd; n++) {
uhci_td_t *td;
usbd_get_page(pc + n, 0, &page_info);
td = page_info.buffer;
/* init TD */
if ((parm->methods == &uhci_device_bulk_methods) ||
(parm->methods == &uhci_device_ctrl_methods) ||
(parm->methods == &uhci_device_intr_methods)) {
/* set depth first bit */
td->td_self = htole32(page_info.physaddr |
UHCI_PTR_TD | UHCI_PTR_VF);
} else {
td->td_self = htole32(page_info.physaddr |
UHCI_PTR_TD);
}
td->obj_next = last_obj;
td->page_cache = pc + n;
last_obj = td;
usb_pc_cpu_flush(pc + n);
}
}
xfer->td_start[xfer->flags_int.curr_dma_set] = last_obj;
last_obj = NULL;
if (usbd_transfer_setup_sub_malloc(
parm, &pc, sizeof(uhci_qh_t),
UHCI_QH_ALIGN, nqh)) {
parm->err = USB_ERR_NOMEM;
return;
}
if (parm->buf) {
for (n = 0; n != nqh; n++) {
uhci_qh_t *qh;
usbd_get_page(pc + n, 0, &page_info);
qh = page_info.buffer;
/* init QH */
qh->qh_self = htole32(page_info.physaddr | UHCI_PTR_QH);
qh->obj_next = last_obj;
qh->page_cache = pc + n;
last_obj = qh;
usb_pc_cpu_flush(pc + n);
}
}
xfer->qh_start[xfer->flags_int.curr_dma_set] = last_obj;
if (!xfer->flags_int.curr_dma_set) {
xfer->flags_int.curr_dma_set = 1;
goto alloc_dma_set;
}
}
static void
uhci_ep_init(struct usb_device *udev, struct usb_endpoint_descriptor *edesc,
struct usb_endpoint *ep)
{
uhci_softc_t *sc = UHCI_BUS2SC(udev->bus);
DPRINTFN(2, "endpoint=%p, addr=%d, endpt=%d, mode=%d (%d)\n",
ep, udev->address,
edesc->bEndpointAddress, udev->flags.usb_mode,
sc->sc_addr);
if (udev->device_index != sc->sc_addr) {
switch (edesc->bmAttributes & UE_XFERTYPE) {
case UE_CONTROL:
ep->methods = &uhci_device_ctrl_methods;
break;
case UE_INTERRUPT:
ep->methods = &uhci_device_intr_methods;
break;
case UE_ISOCHRONOUS:
if (udev->speed == USB_SPEED_FULL) {
ep->methods = &uhci_device_isoc_methods;
}
break;
case UE_BULK:
ep->methods = &uhci_device_bulk_methods;
break;
default:
/* do nothing */
break;
}
}
}
static void
uhci_xfer_unsetup(struct usb_xfer *xfer)
{
return;
}
static void
uhci_get_dma_delay(struct usb_device *udev, uint32_t *pus)
{
/*
* Wait until hardware has finished any possible use of the
* transfer descriptor(s) and QH
*/
*pus = (1125); /* microseconds */
}
static void
uhci_device_resume(struct usb_device *udev)
{
struct uhci_softc *sc = UHCI_BUS2SC(udev->bus);
struct usb_xfer *xfer;
struct usb_pipe_methods *methods;
uhci_qh_t *qh;
DPRINTF("\n");
USB_BUS_LOCK(udev->bus);
TAILQ_FOREACH(xfer, &sc->sc_bus.intr_q.head, wait_entry) {
if (xfer->xroot->udev == udev) {
methods = xfer->endpoint->methods;
qh = xfer->qh_start[xfer->flags_int.curr_dma_set];
if (methods == &uhci_device_bulk_methods) {
UHCI_APPEND_QH(qh, sc->sc_bulk_p_last);
uhci_add_loop(sc);
xfer->flags_int.bandwidth_reclaimed = 1;
}
if (methods == &uhci_device_ctrl_methods) {
if (xfer->xroot->udev->speed == USB_SPEED_LOW) {
UHCI_APPEND_QH(qh, sc->sc_ls_ctl_p_last);
} else {
UHCI_APPEND_QH(qh, sc->sc_fs_ctl_p_last);
}
}
if (methods == &uhci_device_intr_methods) {
UHCI_APPEND_QH(qh, sc->sc_intr_p_last[xfer->qh_pos]);
}
}
}
USB_BUS_UNLOCK(udev->bus);
return;
}
static void
uhci_device_suspend(struct usb_device *udev)
{
struct uhci_softc *sc = UHCI_BUS2SC(udev->bus);
struct usb_xfer *xfer;
struct usb_pipe_methods *methods;
uhci_qh_t *qh;
DPRINTF("\n");
USB_BUS_LOCK(udev->bus);
TAILQ_FOREACH(xfer, &sc->sc_bus.intr_q.head, wait_entry) {
if (xfer->xroot->udev == udev) {
methods = xfer->endpoint->methods;
qh = xfer->qh_start[xfer->flags_int.curr_dma_set];
if (xfer->flags_int.bandwidth_reclaimed) {
xfer->flags_int.bandwidth_reclaimed = 0;
uhci_rem_loop(sc);
}
if (methods == &uhci_device_bulk_methods) {
UHCI_REMOVE_QH(qh, sc->sc_bulk_p_last);
}
if (methods == &uhci_device_ctrl_methods) {
if (xfer->xroot->udev->speed == USB_SPEED_LOW) {
UHCI_REMOVE_QH(qh, sc->sc_ls_ctl_p_last);
} else {
UHCI_REMOVE_QH(qh, sc->sc_fs_ctl_p_last);
}
}
if (methods == &uhci_device_intr_methods) {
UHCI_REMOVE_QH(qh, sc->sc_intr_p_last[xfer->qh_pos]);
}
}
}
USB_BUS_UNLOCK(udev->bus);
return;
}
static void
uhci_set_hw_power_sleep(struct usb_bus *bus, uint32_t state)
{
struct uhci_softc *sc = UHCI_BUS2SC(bus);
switch (state) {
case USB_HW_POWER_SUSPEND:
case USB_HW_POWER_SHUTDOWN:
uhci_suspend(sc);
break;
case USB_HW_POWER_RESUME:
uhci_resume(sc);
break;
default:
break;
}
}
static void
uhci_set_hw_power(struct usb_bus *bus)
{
struct uhci_softc *sc = UHCI_BUS2SC(bus);
uint32_t flags;
DPRINTF("\n");
USB_BUS_LOCK(bus);
flags = bus->hw_power_state;
/*
* WARNING: Some FULL speed USB devices require periodic SOF
* messages! If any USB devices are connected through the
* UHCI, power save will be disabled!
*/
if (flags & (USB_HW_POWER_CONTROL |
USB_HW_POWER_NON_ROOT_HUB |
USB_HW_POWER_BULK |
USB_HW_POWER_INTERRUPT |
USB_HW_POWER_ISOC)) {
DPRINTF("Some USB transfer is "
"active on unit %u.\n",
device_get_unit(sc->sc_bus.bdev));
uhci_restart(sc);
} else {
DPRINTF("Power save on unit %u.\n",
device_get_unit(sc->sc_bus.bdev));
UHCICMD(sc, UHCI_CMD_MAXP);
}
USB_BUS_UNLOCK(bus);
return;
}
struct usb_bus_methods uhci_bus_methods =
{
.endpoint_init = uhci_ep_init,
.xfer_setup = uhci_xfer_setup,
.xfer_unsetup = uhci_xfer_unsetup,
.get_dma_delay = uhci_get_dma_delay,
.device_resume = uhci_device_resume,
.device_suspend = uhci_device_suspend,
.set_hw_power = uhci_set_hw_power,
.set_hw_power_sleep = uhci_set_hw_power_sleep,
.roothub_exec = uhci_roothub_exec,
.xfer_poll = uhci_do_poll,
};
|