summaryrefslogtreecommitdiff
path: root/sys/dev/ic/sl811hs.c
blob: 88d835518594bf64c1b334e7536fd3abd9de165a (plain)
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
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
/*	$NetBSD: sl811hs.c,v 1.112 2022/05/03 20:52:32 andvar Exp $	*/

/*
 * Not (c) 2007 Matthew Orgass
 * This file is public domain, meaning anyone can make any use of part or all
 * of this file including copying into other works without credit.  Any use,
 * modified or not, is solely the responsibility of the user.  If this file is
 * part of a collection then use in the collection is governed by the terms of
 * the collection.
 */

/*
 * Cypress/ScanLogic SL811HS/T USB Host Controller
 * Datasheet, Errata, and App Note available at www.cypress.com
 *
 * Uses: Ratoc CFU1U PCMCIA USB Host Controller, Nereid X68k USB HC, ISA
 * HCs.  The Ratoc CFU2 uses a different chip.
 *
 * This chip puts the serial in USB.  It implements USB by means of an eight
 * bit I/O interface.  It can be used for ISA, PCMCIA/CF, parallel port,
 * serial port, or any eight bit interface.  It has 256 bytes of memory, the
 * first 16 of which are used for register access.  There are two sets of
 * registers for sending individual bus transactions.  Because USB is polled,
 * this organization means that some amount of card access must often be made
 * when devices are attached, even if when they are not directly being used.
 * A per-ms frame interrupt is necessary and many devices will poll with a
 * per-frame bulk transfer.
 *
 * It is possible to write a little over two bytes to the chip (auto
 * incremented) per full speed byte time on the USB.  Unfortunately,
 * auto-increment does not work reliably so write and bus speed is
 * approximately the same for full speed devices.
 *
 * In addition to the 240 byte packet size limit for isochronous transfers,
 * this chip has no means of determining the current frame number other than
 * getting all 1ms SOF interrupts, which is not always possible even on a fast
 * system.  Isochronous transfers guarantee that transfers will never be
 * retried in a later frame, so this can cause problems with devices beyond
 * the difficulty in actually performing the transfer most frames.  I tried
 * implementing isoc transfers and was able to play CD-derrived audio via an
 * iMic on a 2GHz PC, however it would still be interrupted at times and
 * once interrupted, would stay out of sync.  All isoc support has been
 * removed.
 *
 * BUGS: all chip revisions have problems with low speed devices through hubs.
 * The chip stops generating SOF with hubs that send SE0 during SOF.  See
 * comment in dointr().  All performance enhancing features of this chip seem
 * not to work properly, most confirmed buggy in errata doc.
 *
 */

/*
 * The hard interrupt is the main entry point.  Start, callbacks, and repeat
 * are the only others called frequently.
 *
 * Since this driver attaches to pcmcia, card removal at any point should be
 * expected and not cause panics or infinite loops.
 */

/*
 * XXX TODO:
 *   copy next output packet while transferring
 *   usb suspend
 *   could keep track of known values of all buffer space?
 *   combined print/log function for errors
 *
 *   ub_usepolling support is untested and may not work
 */

#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: sl811hs.c,v 1.112 2022/05/03 20:52:32 andvar Exp $");

#ifdef _KERNEL_OPT
#include "opt_slhci.h"
#include "opt_usb.h"
#endif

#include <sys/param.h>

#include <sys/bus.h>
#include <sys/cpu.h>
#include <sys/device.h>
#include <sys/gcq.h>
#include <sys/intr.h>
#include <sys/kernel.h>
#include <sys/kmem.h>
#include <sys/proc.h>
#include <sys/queue.h>
#include <sys/sysctl.h>
#include <sys/systm.h>

#include <dev/usb/usb.h>
#include <dev/usb/usbdi.h>
#include <dev/usb/usbdivar.h>
#include <dev/usb/usbhist.h>
#include <dev/usb/usb_mem.h>
#include <dev/usb/usbdevs.h>
#include <dev/usb/usbroothub.h>

#include <dev/ic/sl811hsreg.h>
#include <dev/ic/sl811hsvar.h>

#define Q_CB 0				/* Control/Bulk */
#define Q_NEXT_CB 1
#define Q_MAX_XFER Q_CB
#define Q_CALLBACKS 2
#define Q_MAX Q_CALLBACKS

#define F_AREADY		(0x00000001)
#define F_BREADY		(0x00000002)
#define F_AINPROG		(0x00000004)
#define F_BINPROG		(0x00000008)
#define F_LOWSPEED		(0x00000010)
#define F_UDISABLED		(0x00000020) /* Consider disabled for USB */
#define F_NODEV			(0x00000040)
#define F_ROOTINTR		(0x00000080)
#define F_REALPOWER		(0x00000100) /* Actual power state */
#define F_POWER			(0x00000200) /* USB reported power state */
#define F_ACTIVE		(0x00000400)
#define F_CALLBACK		(0x00000800) /* Callback scheduled */
#define F_SOFCHECK1		(0x00001000)
#define F_SOFCHECK2		(0x00002000)
#define F_CRESET		(0x00004000) /* Reset done not reported */
#define F_CCONNECT		(0x00008000) /* Connect change not reported */
#define F_RESET			(0x00010000)
#define F_ISOC_WARNED		(0x00020000)
#define F_LSVH_WARNED		(0x00040000)

#define F_DISABLED		(F_NODEV|F_UDISABLED)
#define F_CHANGE		(F_CRESET|F_CCONNECT)

#ifdef SLHCI_TRY_LSVH
unsigned int slhci_try_lsvh = 1;
#else
unsigned int slhci_try_lsvh = 0;
#endif

#define ADR 0
#define LEN 1
#define PID 2
#define DEV 3
#define STAT 2
#define CONT 3

#define A 0
#define B 1

static const uint8_t slhci_tregs[2][4] =
{{SL11_E0ADDR, SL11_E0LEN, SL11_E0PID, SL11_E0DEV },
 {SL11_E1ADDR, SL11_E1LEN, SL11_E1PID, SL11_E1DEV }};

#define PT_ROOT_CTRL	0
#define PT_ROOT_INTR	1
#define PT_CTRL_SETUP	2
#define PT_CTRL_DATA	3
#define PT_CTRL_STATUS	4
#define PT_INTR		5
#define PT_BULK		6
#define PT_MAX		6

#ifdef SLHCI_DEBUG
#define SLHCI_MEM_ACCOUNTING
#endif

/*
 * Maximum allowable reserved bus time.  Since intr/isoc transfers have
 * unconditional priority, this is all that ensures control and bulk transfers
 * get a chance.  It is a single value for all frames since all transfers can
 * use multiple consecutive frames if an error is encountered.  Note that it
 * is not really possible to fill the bus with transfers, so this value should
 * be on the low side.  Defaults to giving a warning unless SLHCI_NO_OVERTIME
 * is defined.  Full time is 12000 - END_BUSTIME.
 */
#ifndef SLHCI_RESERVED_BUSTIME
#define SLHCI_RESERVED_BUSTIME 5000
#endif

/*
 * Rate for "exceeds reserved bus time" warnings (default) or errors.
 * Warnings only happen when an endpoint open causes the time to go above
 * SLHCI_RESERVED_BUSTIME, not if it is already above.
 */
#ifndef SLHCI_OVERTIME_WARNING_RATE
#define SLHCI_OVERTIME_WARNING_RATE { 60, 0 } /* 60 seconds */
#endif
static const struct timeval reserved_warn_rate = SLHCI_OVERTIME_WARNING_RATE;

/*
 * For EOF, the spec says 42 bit times, plus (I think) a possible hub skew of
 * 20 bit times.  By default leave 66 bit times to start the transfer beyond
 * the required time.  Units are full-speed bit times (a bit over 5us per 64).
 * Only multiples of 64 are significant.
 */
#define SLHCI_STANDARD_END_BUSTIME 128
#ifndef SLHCI_EXTRA_END_BUSTIME
#define SLHCI_EXTRA_END_BUSTIME 0
#endif

#define SLHCI_END_BUSTIME (SLHCI_STANDARD_END_BUSTIME+SLHCI_EXTRA_END_BUSTIME)

/*
 * This is an approximation of the USB worst-case timings presented on p. 54 of
 * the USB 1.1 spec translated to full speed bit times.
 * FS = full speed with handshake, FSII = isoc in, FSIO = isoc out,
 * FSI = isoc (worst case), LS = low speed
 */
#define SLHCI_FS_CONST		114
#define SLHCI_FSII_CONST	92
#define SLHCI_FSIO_CONST	80
#define SLHCI_FSI_CONST		92
#define SLHCI_LS_CONST		804
#ifndef SLHCI_PRECICE_BUSTIME
/*
 * These values are < 3% too high (compared to the multiply and divide) for
 * max sized packets.
 */
#define SLHCI_FS_DATA_TIME(len) (((u_int)(len)<<3)+(len)+((len)>>1))
#define SLHCI_LS_DATA_TIME(len) (((u_int)(len)<<6)+((u_int)(len)<<4))
#else
#define SLHCI_FS_DATA_TIME(len) (56*(len)/6)
#define SLHCI_LS_DATA_TIME(len) (449*(len)/6)
#endif

/*
 * Set SLHCI_WAIT_SIZE to the desired maximum size of single FS transfer
 * to poll for after starting a transfer.  64 gets all full speed transfers.
 * Note that even if 0 polling will occur if data equal or greater than the
 * transfer size is copied to the chip while the transfer is in progress.
 * Setting SLHCI_WAIT_TIME to -12000 will disable polling.
 */
#ifndef SLHCI_WAIT_SIZE
#define SLHCI_WAIT_SIZE 8
#endif
#ifndef SLHCI_WAIT_TIME
#define SLHCI_WAIT_TIME (SLHCI_FS_CONST + \
    SLHCI_FS_DATA_TIME(SLHCI_WAIT_SIZE))
#endif
const int slhci_wait_time = SLHCI_WAIT_TIME;

#ifndef SLHCI_MAX_RETRIES
#define SLHCI_MAX_RETRIES 3
#endif

/* Check IER values for corruption after this many unrecognized interrupts. */
#ifndef SLHCI_IER_CHECK_FREQUENCY
#ifdef SLHCI_DEBUG
#define SLHCI_IER_CHECK_FREQUENCY 1
#else
#define SLHCI_IER_CHECK_FREQUENCY 100
#endif
#endif

/* Note that buffer points to the start of the buffer for this transfer.  */
struct slhci_pipe {
	struct usbd_pipe pipe;
	struct usbd_xfer *xfer;		/* xfer in progress */
	uint8_t		*buffer;	/* I/O buffer (if needed) */
	struct gcq 	ap;		/* All pipes */
	struct gcq 	to;		/* Timeout list */
	struct gcq 	xq;		/* Xfer queues */
	unsigned int	pflags;		/* Pipe flags */
#define PF_GONE		(0x01)		/* Pipe is on disabled device */
#define PF_TOGGLE 	(0x02)		/* Data toggle status */
#define PF_LS		(0x04)		/* Pipe is low speed */
#define PF_PREAMBLE	(0x08)		/* Needs preamble */
	Frame		to_frame;	/* Frame number for timeout */
	Frame		frame;		/* Frame number for intr xfer */
	Frame		lastframe;	/* Previous frame number for intr */
	uint16_t	bustime;	/* Worst case bus time usage */
	uint16_t	newbustime[2];	/* new bustimes (see index below) */
	uint8_t		tregs[4];	/* ADR, LEN, PID, DEV */
	uint8_t		newlen[2];	/* 0 = short data, 1 = ctrl data */
	uint8_t		newpid;		/* for ctrl */
	uint8_t		wantshort;	/* last xfer must be short */
	uint8_t		control;	/* Host control register settings */
	uint8_t		nerrs;		/* Current number of errors */
	uint8_t 	ptype;		/* Pipe type */
};

#define SLHCI_BUS2SC(bus)	((bus)->ub_hcpriv)
#define SLHCI_PIPE2SC(pipe)	SLHCI_BUS2SC((pipe)->up_dev->ud_bus)
#define SLHCI_XFER2SC(xfer)	SLHCI_BUS2SC((xfer)->ux_bus)

#define SLHCI_PIPE2SPIPE(pipe)	((struct slhci_pipe *)(pipe))
#define SLHCI_XFER2SPIPE(xfer)	SLHCI_PIPE2SPIPE((xfer)->ux_pipe)

#define SLHCI_XFER_TYPE(x)	(SLHCI_XFER2SPIPE(xfer)->ptype)

#ifdef SLHCI_PROFILE_TRANSFER
#if defined(__mips__)
/*
 * MIPS cycle counter does not directly count cpu cycles but is a different
 * fraction of cpu cycles depending on the cpu.
 */
typedef uint32_t cc_type;
#define CC_TYPE_FMT "%u"
#define slhci_cc_set(x) __asm volatile ("mfc0 %[cc], $9\n\tnop\n\tnop\n\tnop" \
    : [cc] "=r"(x))
#elif defined(__i386__)
typedef uint64_t cc_type;
#define CC_TYPE_FMT "%llu"
#define slhci_cc_set(x) __asm volatile ("rdtsc" : "=A"(x))
#else
#error "SLHCI_PROFILE_TRANSFER not implemented on this MACHINE_ARCH (see sys/dev/ic/sl811hs.c)"
#endif
struct slhci_cc_time {
	cc_type start;
	cc_type stop;
	unsigned int miscdata;
};
#ifndef SLHCI_N_TIMES
#define SLHCI_N_TIMES 200
#endif
struct slhci_cc_times {
	struct slhci_cc_time times[SLHCI_N_TIMES];
	int current;
	int wraparound;
};

static struct slhci_cc_times t_ab[2];
static struct slhci_cc_times t_abdone;
static struct slhci_cc_times t_copy_to_dev;
static struct slhci_cc_times t_copy_from_dev;
static struct slhci_cc_times t_intr;
static struct slhci_cc_times t_lock;
static struct slhci_cc_times t_delay;
static struct slhci_cc_times t_hard_int;
static struct slhci_cc_times t_callback;

static inline void
start_cc_time(struct slhci_cc_times *times, unsigned int misc) {
	times->times[times->current].miscdata = misc;
	slhci_cc_set(times->times[times->current].start);
}
static inline void
stop_cc_time(struct slhci_cc_times *times) {
	slhci_cc_set(times->times[times->current].stop);
	if (++times->current >= SLHCI_N_TIMES) {
		times->current = 0;
		times->wraparound = 1;
	}
}

void slhci_dump_cc_times(int);

void
slhci_dump_cc_times(int n) {
	struct slhci_cc_times *times;
	int i;

	switch (n) {
	default:
	case 0:
		printf("USBA start transfer to intr:\n");
		times = &t_ab[A];
		break;
	case 1:
		printf("USBB start transfer to intr:\n");
		times = &t_ab[B];
		break;
	case 2:
		printf("abdone:\n");
		times = &t_abdone;
		break;
	case 3:
		printf("copy to device:\n");
		times = &t_copy_to_dev;
		break;
	case 4:
		printf("copy from device:\n");
		times = &t_copy_from_dev;
		break;
	case 5:
		printf("intr to intr:\n");
		times = &t_intr;
		break;
	case 6:
		printf("lock to release:\n");
		times = &t_lock;
		break;
	case 7:
		printf("delay time:\n");
		times = &t_delay;
		break;
	case 8:
		printf("hard interrupt enter to exit:\n");
		times = &t_hard_int;
		break;
	case 9:
		printf("callback:\n");
		times = &t_callback;
		break;
	}

	if (times->wraparound)
		for (i = times->current + 1; i < SLHCI_N_TIMES; i++)
			printf("start " CC_TYPE_FMT " stop " CC_TYPE_FMT
			    " difference %8i miscdata %#x\n",
			    times->times[i].start, times->times[i].stop,
			    (int)(times->times[i].stop -
			    times->times[i].start), times->times[i].miscdata);

	for (i = 0; i < times->current; i++)
		printf("start " CC_TYPE_FMT " stop " CC_TYPE_FMT
		    " difference %8i miscdata %#x\n", times->times[i].start,
		    times->times[i].stop, (int)(times->times[i].stop -
		    times->times[i].start), times->times[i].miscdata);
}
#else
#define start_cc_time(x, y)
#define stop_cc_time(x)
#endif /* SLHCI_PROFILE_TRANSFER */

typedef usbd_status (*LockCallFunc)(struct slhci_softc *, struct slhci_pipe
    *, struct usbd_xfer *);

struct usbd_xfer * slhci_allocx(struct usbd_bus *, unsigned int);
void slhci_freex(struct usbd_bus *, struct usbd_xfer *);
static void slhci_get_lock(struct usbd_bus *, kmutex_t **);

usbd_status slhci_transfer(struct usbd_xfer *);
usbd_status slhci_start(struct usbd_xfer *);
usbd_status slhci_root_start(struct usbd_xfer *);
usbd_status slhci_open(struct usbd_pipe *);

static int slhci_roothub_ctrl(struct usbd_bus *, usb_device_request_t *,
    void *, int);

/*
 * slhci_supported_rev, slhci_preinit, slhci_attach, slhci_detach,
 * slhci_activate
 */

void slhci_abort(struct usbd_xfer *);
void slhci_close(struct usbd_pipe *);
void slhci_clear_toggle(struct usbd_pipe *);
void slhci_poll(struct usbd_bus *);
void slhci_done(struct usbd_xfer *);
void slhci_void(void *);

/* lock entry functions */

#ifdef SLHCI_MEM_ACCOUNTING
void slhci_mem_use(struct usbd_bus *, int);
#endif

void slhci_reset_entry(void *);
usbd_status slhci_lock_call(struct slhci_softc *, LockCallFunc,
    struct slhci_pipe *, struct usbd_xfer *);
void slhci_start_entry(struct slhci_softc *, struct slhci_pipe *);
void slhci_callback_entry(void *arg);
void slhci_do_callback(struct slhci_softc *, struct usbd_xfer *);

/* slhci_intr */

void slhci_main(struct slhci_softc *);

/* in lock functions */

static void slhci_write(struct slhci_softc *, uint8_t, uint8_t);
static uint8_t slhci_read(struct slhci_softc *, uint8_t);
static void slhci_write_multi(struct slhci_softc *, uint8_t, uint8_t *, int);
static void slhci_read_multi(struct slhci_softc *, uint8_t, uint8_t *, int);

static void slhci_waitintr(struct slhci_softc *, int);
static int slhci_dointr(struct slhci_softc *);
static void slhci_abdone(struct slhci_softc *, int);
static void slhci_tstart(struct slhci_softc *);
static void slhci_dotransfer(struct slhci_softc *);

static void slhci_callback(struct slhci_softc *);
static void slhci_enter_xfer(struct slhci_softc *, struct slhci_pipe *);
static void slhci_enter_xfers(struct slhci_softc *);
static void slhci_queue_timed(struct slhci_softc *, struct slhci_pipe *);
static void slhci_xfer_timer(struct slhci_softc *, struct slhci_pipe *);

static void slhci_callback_schedule(struct slhci_softc *);
static void slhci_do_callback_schedule(struct slhci_softc *);
#if 0
void slhci_pollxfer(struct slhci_softc *, struct usbd_xfer *); /* XXX */
#endif

static usbd_status slhci_do_poll(struct slhci_softc *, struct slhci_pipe *,
    struct usbd_xfer *);
static usbd_status slhci_lsvh_warn(struct slhci_softc *, struct slhci_pipe *,
    struct usbd_xfer *);
static usbd_status slhci_isoc_warn(struct slhci_softc *, struct slhci_pipe *,
    struct usbd_xfer *);
static usbd_status slhci_open_pipe(struct slhci_softc *, struct slhci_pipe *,
    struct usbd_xfer *);
static usbd_status slhci_close_pipe(struct slhci_softc *, struct slhci_pipe *,
    struct usbd_xfer *);
static usbd_status slhci_do_abort(struct slhci_softc *, struct slhci_pipe *,
    struct usbd_xfer *);
static usbd_status slhci_halt(struct slhci_softc *, struct slhci_pipe *,
    struct usbd_xfer *);

static void slhci_intrchange(struct slhci_softc *, uint8_t);
static void slhci_drain(struct slhci_softc *);
static void slhci_reset(struct slhci_softc *);
static int slhci_reserve_bustime(struct slhci_softc *, struct slhci_pipe *,
    int);
static void slhci_insert(struct slhci_softc *);

static usbd_status slhci_clear_feature(struct slhci_softc *, unsigned int);
static usbd_status slhci_set_feature(struct slhci_softc *, unsigned int);
static void slhci_get_status(struct slhci_softc *, usb_port_status_t *);

#define	SLHCIHIST_FUNC()	USBHIST_FUNC()
#define	SLHCIHIST_CALLED()	USBHIST_CALLED(slhcidebug)

#ifdef SLHCI_DEBUG
static int slhci_memtest(struct slhci_softc *);

void slhci_log_buffer(struct usbd_xfer *);
void slhci_log_req(usb_device_request_t *);
void slhci_log_dumpreg(void);
void slhci_log_xfer(struct usbd_xfer *);
void slhci_log_spipe(struct slhci_pipe *);
void slhci_print_intr(void);
void slhci_log_sc(void);
void slhci_log_slreq(struct slhci_pipe *);

/* Constified so you can read the values from ddb */
const int SLHCI_D_TRACE =	0x0001;
const int SLHCI_D_MSG = 	0x0002;
const int SLHCI_D_XFER =	0x0004;
const int SLHCI_D_MEM = 	0x0008;
const int SLHCI_D_INTR =	0x0010;
const int SLHCI_D_SXFER =	0x0020;
const int SLHCI_D_ERR = 	0x0080;
const int SLHCI_D_BUF = 	0x0100;
const int SLHCI_D_SOFT =	0x0200;
const int SLHCI_D_WAIT =	0x0400;
const int SLHCI_D_ROOT =	0x0800;
/* SOF/NAK alone normally ignored, SOF also needs D_INTR */
const int SLHCI_D_SOF =		0x1000;
const int SLHCI_D_NAK =		0x2000;

int slhcidebug = 0x1cbc; /* 0xc8c; */ /* 0xffff; */ /* 0xd8c; */

SYSCTL_SETUP(sysctl_hw_slhci_setup, "sysctl hw.slhci setup")
{
	int err;
	const struct sysctlnode *rnode;
	const struct sysctlnode *cnode;

	err = sysctl_createv(clog, 0, NULL, &rnode,
	    CTLFLAG_PERMANENT, CTLTYPE_NODE, "slhci",
	    SYSCTL_DESCR("slhci global controls"),
	    NULL, 0, NULL, 0, CTL_HW, CTL_CREATE, CTL_EOL);

	if (err)
		goto fail;

	/* control debugging printfs */
	err = sysctl_createv(clog, 0, &rnode, &cnode,
	    CTLFLAG_PERMANENT|CTLFLAG_READWRITE, CTLTYPE_INT,
	    "debug", SYSCTL_DESCR("Enable debugging output"),
	    NULL, 0, &slhcidebug, sizeof(slhcidebug), CTL_CREATE, CTL_EOL);
	if (err)
		goto fail;

	return;
fail:
	aprint_error("%s: sysctl_createv failed (err = %d)\n", __func__, err);
}

struct slhci_softc *ssc;

#define SLHCI_DEXEC(x, y) do { if ((slhcidebug & SLHCI_ ## x)) { y; } \
} while (/*CONSTCOND*/ 0)
#define DDOLOG(f, a, b, c, d) do { KERNHIST_LOG(usbhist, f, a, b, c, d); \
} while (/*CONSTCOND*/0)
#define DLOG(x, f, a, b, c, d) SLHCI_DEXEC(x, DDOLOG(f, a, b, c, d))

/*
 * DDOLOGBUF logs a buffer up to 8 bytes at a time. No identifier so that we
 * can make it a real function.
 */
static void
DDOLOGBUF(uint8_t *buf, unsigned int length)
{
	SLHCIHIST_FUNC(); SLHCIHIST_CALLED();
	int i;

	for(i = 0; i + 8 <= length; i += 8)
		DDOLOG("%.4x %.4x %.4x %.4x", (buf[i] << 8) | buf[i+1],
		    (buf[i+2] << 8) | buf[i+3], (buf[i+4] << 8) | buf[i+5],
		    (buf[i+6] << 8) | buf[i+7]);
	if (length == i + 7)
		DDOLOG("%.4x %.4x %.4x %.2x", (buf[i] << 8) | buf[i+1],
		    (buf[i+2] << 8) | buf[i+3], (buf[i+4] << 8) | buf[i+5],
		    buf[i+6]);
	else if (length == i + 6)
		DDOLOG("%.4x %.4x %.4x", (buf[i] << 8) | buf[i+1],
		    (buf[i+2] << 8) | buf[i+3], (buf[i+4] << 8) | buf[i+5], 0);
	else if (length == i + 5)
		DDOLOG("%.4x %.4x %.2x", (buf[i] << 8) | buf[i+1],
		    (buf[i+2] << 8) | buf[i+3], buf[i+4], 0);
	else if (length == i + 4)
		DDOLOG("%.4x %.4x", (buf[i] << 8) | buf[i+1],
		    (buf[i+2] << 8) | buf[i+3], 0,0);
	else if (length == i + 3)
		DDOLOG("%.4x %.2x", (buf[i] << 8) | buf[i+1], buf[i+2], 0,0);
	else if (length == i + 2)
		DDOLOG("%.4x", (buf[i] << 8) | buf[i+1], 0,0,0);
	else if (length == i + 1)
		DDOLOG("%.2x", buf[i], 0,0,0);
}
#define DLOGBUF(x, b, l) SLHCI_DEXEC(x, DDOLOGBUF(b, l))

#define DDOLOGCTRL(x)	do {						\
    DDOLOG("CTRL suspend=%jd", !!((x) & SL11_CTRL_SUSPEND), 0, 0, 0);	\
    DDOLOG("CTRL ls     =%jd  jk     =%jd  reset  =%jd  sof    =%jd",	\
	!!((x) & SL11_CTRL_LOWSPEED), !!((x) & SL11_CTRL_JKSTATE),	\
	!!((x) & SL11_CTRL_RESETENGINE), !!((x) & SL11_CTRL_ENABLESOF));\
} while (0)

#define DDOLOGISR(r)	do {						\
    DDOLOG("ISR  data   =%jd  det/res=%jd  insert =%jd  sof    =%jd",	\
	!!((r) & SL11_ISR_DATA), !!((r) & SL11_ISR_RESUME),		\
	!!((r) & SL11_ISR_INSERT), !!!!((r) & SL11_ISR_SOF));		\
    DDOLOG("ISR             babble =%jd  usbb   =%jd  usba   =%jd",	\
	!!((r) & SL11_ISR_BABBLE), !!((r) & SL11_ISR_USBB),		\
	!!((r) & SL11_ISR_USBA), 0);					\
} while (0)

#define DDOLOGIER(r)	do {						\
    DDOLOG("IER              det/res=%d  insert =%d  sof    =%d",	\
	!!((r) & SL11_IER_RESUME),					\
	!!((r) & SL11_IER_INSERT), !!!!((r) & SL11_IER_SOF), 0);		\
    DDOLOG("IER              babble =%d  usbb   =%d  usba   =%d",	\
	!!((r) & SL11_IER_BABBLE), !!((r) & SL11_IER_USBB),		\
	!!((r) & SL11_IER_USBA), 0);					\
} while (0)

#define DDOLOGSTATUS(s)	do {						\
    DDOLOG("STAT stall   =%d  nak     =%d  overflow =%d  setup   =%d",	\
	!!((s) & SL11_EPSTAT_STALL), !!((s) & SL11_EPSTAT_NAK),		\
	!!((s) & SL11_EPSTAT_OVERFLOW), !!((s) & SL11_EPSTAT_SETUP));	\
    DDOLOG("STAT sequence=%d  timeout =%d  error    =%d  ack     =%d",	\
	!!((s) & SL11_EPSTAT_SEQUENCE),	!!((s) & SL11_EPSTAT_TIMEOUT),	\
	!!((s) & SL11_EPSTAT_ERROR), !!((s) & SL11_EPSTAT_ACK));	\
} while (0)

#define DDOLOGEPCTRL(r)	do {						\
    DDOLOG("CTRL preamble=%d  toggle  =%d  sof     =%d  iso     =%d",	\
	!!((r) & SL11_EPCTRL_PREAMBLE), !!((r) & SL11_EPCTRL_DATATOGGLE),\
	!!((r) & SL11_EPCTRL_SOF), !!((r) & SL11_EPCTRL_ISO));		\
    DDOLOG("CTRL              out     =%d  enable  =%d  arm     =%d",	\
	!!((r) & SL11_EPCTRL_DIRECTION),				\
	!!((r) & SL11_EPCTRL_ENABLE), !!((r) & SL11_EPCTRL_ARM), 0);	\
} while (0)

#define DDOLOGEPSTAT(r)	do {						\
    DDOLOG("STAT stall   =%d  nak     =%d  overflow =%d  setup   =%d",	\
	!!((r) & SL11_EPSTAT_STALL), !!((r) & SL11_EPSTAT_NAK),		\
	!!((r) & SL11_EPSTAT_OVERFLOW), !!((r) & SL11_EPSTAT_SETUP));	\
    DDOLOG("STAT sequence=%d  timeout =%d  error    =%d  ack   =%d",	\
	!!((r) & SL11_EPSTAT_SEQUENCE), !!((r) & SL11_EPSTAT_TIMEOUT),	\
	!!((r) & SL11_EPSTAT_ERROR), !!((r) & SL11_EPSTAT_ACK));	\
} while (0)
#else /* now !SLHCI_DEBUG */
#define slhcidebug 0
#define slhci_log_spipe(spipe) ((void)0)
#define slhci_log_xfer(xfer) ((void)0)
#define SLHCI_DEXEC(x, y) ((void)0)
#define DDOLOG(f, a, b, c, d) ((void)0)
#define DLOG(x, f, a, b, c, d) ((void)0)
#define DDOLOGBUF(b, l) ((void)0)
#define DLOGBUF(x, b, l) ((void)0)
#define DDOLOGCTRL(x) ((void)0)
#define DDOLOGISR(r) ((void)0)
#define DDOLOGIER(r) ((void)0)
#define DDOLOGSTATUS(s) ((void)0)
#define DDOLOGEPCTRL(r) ((void)0)
#define DDOLOGEPSTAT(r) ((void)0)
#endif /* SLHCI_DEBUG */

#ifdef DIAGNOSTIC
#define LK_SLASSERT(exp, sc, spipe, xfer, ext) do {			\
	if (!(exp)) {							\
		printf("%s: assertion %s failed line %u function %s!"	\
		" halted\n", SC_NAME(sc), #exp, __LINE__, __func__);\
		slhci_halt(sc, spipe, xfer);				\
		ext;							\
	}								\
} while (/*CONSTCOND*/0)
#define UL_SLASSERT(exp, sc, spipe, xfer, ext) do {			\
	if (!(exp)) {							\
		printf("%s: assertion %s failed line %u function %s!"	\
		" halted\n", SC_NAME(sc), #exp, __LINE__, __func__);	\
		slhci_lock_call(sc, &slhci_halt, spipe, xfer);		\
		ext;							\
	}								\
} while (/*CONSTCOND*/0)
#else
#define LK_SLASSERT(exp, sc, spipe, xfer, ext) ((void)0)
#define UL_SLASSERT(exp, sc, spipe, xfer, ext) ((void)0)
#endif

const struct usbd_bus_methods slhci_bus_methods = {
	.ubm_open = slhci_open,
	.ubm_softint = slhci_void,
	.ubm_dopoll = slhci_poll,
	.ubm_allocx = slhci_allocx,
	.ubm_freex = slhci_freex,
	.ubm_getlock = slhci_get_lock,
	.ubm_rhctrl = slhci_roothub_ctrl,
};

const struct usbd_pipe_methods slhci_pipe_methods = {
	.upm_transfer = slhci_transfer,
	.upm_start = slhci_start,
	.upm_abort = slhci_abort,
	.upm_close = slhci_close,
	.upm_cleartoggle = slhci_clear_toggle,
	.upm_done = slhci_done,
};

const struct usbd_pipe_methods slhci_root_methods = {
	.upm_transfer = slhci_transfer,
	.upm_start = slhci_root_start,
	.upm_abort = slhci_abort,
	.upm_close = (void (*)(struct usbd_pipe *))slhci_void, /* XXX safe? */
	.upm_cleartoggle = slhci_clear_toggle,
	.upm_done = slhci_done,
};

/* Queue inlines */

#define GOT_FIRST_TO(tvar, t) \
    GCQ_GOT_FIRST_TYPED(tvar, &(t)->to, struct slhci_pipe, to)

#define FIND_TO(var, t, tvar, cond) \
    GCQ_FIND_TYPED(var, &(t)->to, tvar, struct slhci_pipe, to, cond)

#define FOREACH_AP(var, t, tvar) \
    GCQ_FOREACH_TYPED(var, &(t)->ap, tvar, struct slhci_pipe, ap)

#define GOT_FIRST_TIMED_COND(tvar, t, cond) \
    GCQ_GOT_FIRST_COND_TYPED(tvar, &(t)->timed, struct slhci_pipe, xq, cond)

#define GOT_FIRST_CB(tvar, t) \
    GCQ_GOT_FIRST_TYPED(tvar, &(t)->q[Q_CB], struct slhci_pipe, xq)

#define DEQUEUED_CALLBACK(tvar, t) \
    GCQ_DEQUEUED_FIRST_TYPED(tvar, &(t)->q[Q_CALLBACKS], struct slhci_pipe, xq)

#define FIND_TIMED(var, t, tvar, cond) \
   GCQ_FIND_TYPED(var, &(t)->timed, tvar, struct slhci_pipe, xq, cond)

#define DEQUEUED_WAITQ(tvar, sc) \
    GCQ_DEQUEUED_FIRST_TYPED(tvar, &(sc)->sc_waitq, struct slhci_pipe, xq)

static inline void
enter_waitq(struct slhci_softc *sc, struct slhci_pipe *spipe)
{
	gcq_insert_tail(&sc->sc_waitq, &spipe->xq);
}

static inline void
enter_q(struct slhci_transfers *t, struct slhci_pipe *spipe, int i)
{
	gcq_insert_tail(&t->q[i], &spipe->xq);
}

static inline void
enter_callback(struct slhci_transfers *t, struct slhci_pipe *spipe)
{
	gcq_insert_tail(&t->q[Q_CALLBACKS], &spipe->xq);
}

static inline void
enter_all_pipes(struct slhci_transfers *t, struct slhci_pipe *spipe)
{
	gcq_insert_tail(&t->ap, &spipe->ap);
}

/* Start out of lock functions. */

struct usbd_xfer *
slhci_allocx(struct usbd_bus *bus, unsigned int nframes)
{
	SLHCIHIST_FUNC(); SLHCIHIST_CALLED();
	struct usbd_xfer *xfer;

	xfer = kmem_zalloc(sizeof(*xfer), KM_SLEEP);

	DLOG(D_MEM, "allocx %#jx", (uintptr_t)xfer, 0,0,0);

#ifdef SLHCI_MEM_ACCOUNTING
	slhci_mem_use(bus, 1);
#endif
#ifdef DIAGNOSTIC
	if (xfer != NULL)
		xfer->ux_state = XFER_BUSY;
#endif
	return xfer;
}

void
slhci_freex(struct usbd_bus *bus, struct usbd_xfer *xfer)
{
	SLHCIHIST_FUNC(); SLHCIHIST_CALLED();
	DLOG(D_MEM, "freex xfer %#jx spipe %#jx",
	    (uintptr_t)xfer, (uintptr_t)xfer->ux_pipe,0,0);

#ifdef SLHCI_MEM_ACCOUNTING
	slhci_mem_use(bus, -1);
#endif
#ifdef DIAGNOSTIC
	if (xfer->ux_state != XFER_BUSY &&
	    xfer->ux_status != USBD_NOT_STARTED) {
		struct slhci_softc *sc = SLHCI_BUS2SC(bus);
		printf("%s: slhci_freex: xfer=%p not busy, %#08x halted\n",
		    SC_NAME(sc), xfer, xfer->ux_state);
		DDOLOG("xfer=%p not busy, %#08x halted\n", xfer,
		    xfer->ux_state, 0, 0);
		slhci_lock_call(sc, &slhci_halt, NULL, NULL);
		return;
	}
	xfer->ux_state = XFER_FREE;
#endif

	kmem_free(xfer, sizeof(*xfer));
}

static void
slhci_get_lock(struct usbd_bus *bus, kmutex_t **lock)
{
	struct slhci_softc *sc = SLHCI_BUS2SC(bus);

	*lock = &sc->sc_lock;
}

usbd_status
slhci_transfer(struct usbd_xfer *xfer)
{
	SLHCIHIST_FUNC(); SLHCIHIST_CALLED();
	usbd_status error;

	DLOG(D_TRACE, "transfer type %jd xfer %#jx spipe %#jx ",
	    SLHCI_XFER_TYPE(xfer), (uintptr_t)xfer, (uintptr_t)xfer->ux_pipe,
	    0);

	/* Pipe isn't running, so start it first.  */
	error = xfer->ux_pipe->up_methods->upm_start(SIMPLEQ_FIRST(&xfer->ux_pipe->up_queue));

	return error;
}

/* It is not safe for start to return anything other than USBD_INPROG. */
usbd_status
slhci_start(struct usbd_xfer *xfer)
{
	SLHCIHIST_FUNC(); SLHCIHIST_CALLED();
	struct slhci_softc *sc = SLHCI_XFER2SC(xfer);
	struct usbd_pipe *pipe = xfer->ux_pipe;
	struct slhci_pipe *spipe = SLHCI_PIPE2SPIPE(pipe);
	struct slhci_transfers *t = &sc->sc_transfers;
	usb_endpoint_descriptor_t *ed = pipe->up_endpoint->ue_edesc;
	unsigned int max_packet;

	KASSERT(sc->sc_bus.ub_usepolling || mutex_owned(&sc->sc_lock));

	max_packet = UGETW(ed->wMaxPacketSize);

	DLOG(D_TRACE, "transfer type %jd start xfer %#jx spipe %#jx length %jd",
	    spipe->ptype, (uintptr_t)xfer, (uintptr_t)spipe, xfer->ux_length);

	/* root transfers use slhci_root_start */

	KASSERT(spipe->xfer == NULL); /* not SLASSERT */

	xfer->ux_actlen = 0;
	xfer->ux_status = USBD_IN_PROGRESS;

	spipe->xfer = xfer;

	spipe->nerrs = 0;
	spipe->frame = t->frame;
	spipe->control = SL11_EPCTRL_ARM_ENABLE;
	spipe->tregs[DEV] = pipe->up_dev->ud_addr;
	spipe->tregs[PID] = spipe->newpid = UE_GET_ADDR(ed->bEndpointAddress)
	    | (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN ? SL11_PID_IN :
	    SL11_PID_OUT);
	spipe->newlen[0] = xfer->ux_length % max_packet;
	spipe->newlen[1] = uimin(xfer->ux_length, max_packet);

	if (spipe->ptype == PT_BULK || spipe->ptype == PT_INTR) {
		if (spipe->pflags & PF_TOGGLE)
			spipe->control |= SL11_EPCTRL_DATATOGGLE;
		spipe->tregs[LEN] = spipe->newlen[1];
		if (spipe->tregs[LEN])
			spipe->buffer = xfer->ux_buf;
		else
			spipe->buffer = NULL;
		spipe->lastframe = t->frame;
		if (spipe->ptype == PT_INTR) {
			spipe->frame = spipe->lastframe +
			    spipe->pipe.up_interval;
		}

#if defined(DEBUG) || defined(SLHCI_DEBUG)
		if (__predict_false(spipe->ptype == PT_INTR &&
		    xfer->ux_length > spipe->tregs[LEN])) {
			printf("%s: Long INTR transfer not supported!\n",
			    SC_NAME(sc));
			DDOLOG("Long INTR transfer not supported!", 0, 0, 0, 0);
			xfer->ux_status = USBD_INVAL;
		}
#endif
	} else {
		/* ptype may be currently set to any control transfer type. */
		SLHCI_DEXEC(D_TRACE, slhci_log_xfer(xfer));

		/* SETUP contains IN/OUT bits also */
		spipe->tregs[PID] |= SL11_PID_SETUP;
		spipe->tregs[LEN] = 8;
		spipe->buffer = (uint8_t *)&xfer->ux_request;
		DLOGBUF(D_XFER, spipe->buffer, spipe->tregs[LEN]);
		spipe->ptype = PT_CTRL_SETUP;
		spipe->newpid &= ~SL11_PID_BITS;
		if (xfer->ux_length == 0 ||
		    (xfer->ux_request.bmRequestType & UT_READ))
			spipe->newpid |= SL11_PID_IN;
		else
			spipe->newpid |= SL11_PID_OUT;
	}

	if (xfer->ux_flags & USBD_FORCE_SHORT_XFER &&
	    spipe->tregs[LEN] == max_packet &&
	    (spipe->newpid & SL11_PID_BITS) == SL11_PID_OUT)
		spipe->wantshort = 1;
	else
		spipe->wantshort = 0;

	/*
	 * The goal of newbustime and newlen is to avoid bustime calculation
	 * in the interrupt.  The calculations are not too complex, but they
	 * complicate the conditional logic somewhat and doing them all in the
	 * same place shares constants. Index 0 is "short length" for bulk and
	 * ctrl data and 1 is "full length" for ctrl data (bulk/intr are
	 * already set to full length).
	 */
	if (spipe->pflags & PF_LS) {
		/*
		 * Setting PREAMBLE for directly connected LS devices will
		 * lock up the chip.
		 */
		if (spipe->pflags & PF_PREAMBLE)
			spipe->control |= SL11_EPCTRL_PREAMBLE;
		if (max_packet <= 8) {
			spipe->bustime = SLHCI_LS_CONST +
			    SLHCI_LS_DATA_TIME(spipe->tregs[LEN]);
			spipe->newbustime[0] = SLHCI_LS_CONST +
			    SLHCI_LS_DATA_TIME(spipe->newlen[0]);
			spipe->newbustime[1] = SLHCI_LS_CONST +
			    SLHCI_LS_DATA_TIME(spipe->newlen[1]);
		} else
			xfer->ux_status = USBD_INVAL;
	} else {
		UL_SLASSERT(pipe->up_dev->ud_speed == USB_SPEED_FULL, sc,
		    spipe, xfer, return USBD_IN_PROGRESS);
		if (max_packet <= SL11_MAX_PACKET_SIZE) {
			spipe->bustime = SLHCI_FS_CONST +
			    SLHCI_FS_DATA_TIME(spipe->tregs[LEN]);
			spipe->newbustime[0] = SLHCI_FS_CONST +
			    SLHCI_FS_DATA_TIME(spipe->newlen[0]);
			spipe->newbustime[1] = SLHCI_FS_CONST +
			    SLHCI_FS_DATA_TIME(spipe->newlen[1]);
		} else
			xfer->ux_status = USBD_INVAL;
	}

	/*
	 * The datasheet incorrectly indicates that DIRECTION is for
	 * "transmit to host".  It is for OUT and SETUP.  The app note
	 * describes its use correctly.
	 */
	if ((spipe->tregs[PID] & SL11_PID_BITS) != SL11_PID_IN)
		spipe->control |= SL11_EPCTRL_DIRECTION;

	slhci_start_entry(sc, spipe);

	return USBD_IN_PROGRESS;
}

usbd_status
slhci_root_start(struct usbd_xfer *xfer)
{
	SLHCIHIST_FUNC(); SLHCIHIST_CALLED();
	struct slhci_softc *sc;
	struct slhci_pipe *spipe __diagused;

	spipe = SLHCI_PIPE2SPIPE(xfer->ux_pipe);
	sc = SLHCI_XFER2SC(xfer);

	struct slhci_transfers *t = &sc->sc_transfers;

	LK_SLASSERT(spipe != NULL && xfer != NULL, sc, spipe, xfer, return
	    USBD_CANCELLED);

	DLOG(D_TRACE, "transfer type %jd start",
	    SLHCI_XFER_TYPE(xfer), 0, 0, 0);

	KASSERT(sc->sc_bus.ub_usepolling || mutex_owned(&sc->sc_lock));

	KASSERT(spipe->ptype == PT_ROOT_INTR);

	KASSERT(t->rootintr == NULL);
	t->rootintr = xfer;
	xfer->ux_status = USBD_IN_PROGRESS;

	return USBD_IN_PROGRESS;
}

usbd_status
slhci_open(struct usbd_pipe *pipe)
{
	SLHCIHIST_FUNC(); SLHCIHIST_CALLED();
	struct usbd_device *dev;
	struct slhci_softc *sc;
	struct slhci_pipe *spipe;
	usb_endpoint_descriptor_t *ed;
	unsigned int max_packet, pmaxpkt;
	uint8_t rhaddr;

	dev = pipe->up_dev;
	sc = SLHCI_PIPE2SC(pipe);
	spipe = SLHCI_PIPE2SPIPE(pipe);
	ed = pipe->up_endpoint->ue_edesc;
	rhaddr = dev->ud_bus->ub_rhaddr;

	DLOG(D_TRACE, "slhci_open(addr=%jd,ep=%jd,rootaddr=%jd)",
		dev->ud_addr, ed->bEndpointAddress, rhaddr, 0);

	spipe->pflags = 0;
	spipe->frame = 0;
	spipe->lastframe = 0;
	spipe->xfer = NULL;
	spipe->buffer = NULL;

	gcq_init(&spipe->ap);
	gcq_init(&spipe->to);
	gcq_init(&spipe->xq);

	/*
	 * The endpoint descriptor will not have been set up yet in the case
	 * of the standard control pipe, so the max packet checks are also
	 * necessary in start.
	 */

	max_packet = UGETW(ed->wMaxPacketSize);

	if (dev->ud_speed == USB_SPEED_LOW) {
		spipe->pflags |= PF_LS;
		if (dev->ud_myhub->ud_addr != rhaddr) {
			spipe->pflags |= PF_PREAMBLE;
			if (!slhci_try_lsvh)
				return slhci_lock_call(sc, &slhci_lsvh_warn,
				    spipe, NULL);
		}
		pmaxpkt = 8;
	} else
		pmaxpkt = SL11_MAX_PACKET_SIZE;

	if (max_packet > pmaxpkt) {
		DLOG(D_ERR, "packet too large! size %jd spipe %#jx", max_packet,
		    (uintptr_t)spipe, 0,0);
		return USBD_INVAL;
	}

	if (dev->ud_addr == rhaddr) {
		switch (ed->bEndpointAddress) {
		case USB_CONTROL_ENDPOINT:
			spipe->ptype = PT_ROOT_CTRL;
			pipe->up_interval = 0;
			pipe->up_methods = &roothub_ctrl_methods;
			break;
		case UE_DIR_IN | USBROOTHUB_INTR_ENDPT:
			spipe->ptype = PT_ROOT_INTR;
			pipe->up_interval = 1;
			pipe->up_methods = &slhci_root_methods;
			break;
		default:
			printf("%s: Invalid root endpoint!\n", SC_NAME(sc));
			DDOLOG("Invalid root endpoint", 0, 0, 0, 0);
			return USBD_INVAL;
		}
		return USBD_NORMAL_COMPLETION;
	} else {
		switch (ed->bmAttributes & UE_XFERTYPE) {
		case UE_CONTROL:
			spipe->ptype = PT_CTRL_SETUP;
			pipe->up_interval = 0;
			break;
		case UE_INTERRUPT:
			spipe->ptype = PT_INTR;
			if (pipe->up_interval == USBD_DEFAULT_INTERVAL)
				pipe->up_interval = ed->bInterval;
			break;
		case UE_ISOCHRONOUS:
			return slhci_lock_call(sc, &slhci_isoc_warn, spipe,
			    NULL);
		case UE_BULK:
			spipe->ptype = PT_BULK;
			pipe->up_interval = 0;
			break;
		}

		DLOG(D_MSG, "open pipe type %jd interval %jd", spipe->ptype,
		    pipe->up_interval, 0,0);

		pipe->up_methods = __UNCONST(&slhci_pipe_methods);

		return slhci_lock_call(sc, &slhci_open_pipe, spipe, NULL);
	}
}

int
slhci_supported_rev(uint8_t rev)
{
	return rev >= SLTYPE_SL811HS_R12 && rev <= SLTYPE_SL811HS_R15;
}

/*
 * Must be called before the ISR is registered. Interrupts can be shared so
 * slhci_intr could be called as soon as the ISR is registered.
 * Note max_current argument is actual current, but stored as current/2
 */
void
slhci_preinit(struct slhci_softc *sc, PowerFunc pow, bus_space_tag_t iot,
    bus_space_handle_t ioh, uint16_t max_current, uint32_t stride)
{
	struct slhci_transfers *t;
	int i;

	t = &sc->sc_transfers;

#ifdef SLHCI_DEBUG
	ssc = sc;
#endif

	mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_SOFTUSB);
	mutex_init(&sc->sc_intr_lock, MUTEX_DEFAULT, IPL_USB);

	/* sc->sc_ier = 0;	*/
	/* t->rootintr = NULL;	*/
	t->flags = F_NODEV|F_UDISABLED;
	t->pend = INT_MAX;
	KASSERT(slhci_wait_time != INT_MAX);
	t->len[0] = t->len[1] = -1;
	if (max_current > 500)
		max_current = 500;
	t->max_current = (uint8_t)(max_current / 2);
	sc->sc_enable_power = pow;
	sc->sc_iot = iot;
	sc->sc_ioh = ioh;
	sc->sc_stride = stride;

	KASSERT(Q_MAX+1 == sizeof(t->q) / sizeof(t->q[0]));

	for (i = 0; i <= Q_MAX; i++)
		gcq_init_head(&t->q[i]);
	gcq_init_head(&t->timed);
	gcq_init_head(&t->to);
	gcq_init_head(&t->ap);
	gcq_init_head(&sc->sc_waitq);
}

int
slhci_attach(struct slhci_softc *sc)
{
	struct slhci_transfers *t;
	const char *rev;

	t = &sc->sc_transfers;

	/* Detect and check the controller type */
	t->sltype = SL11_GET_REV(slhci_read(sc, SL11_REV));

	/* SL11H not supported */
	if (!slhci_supported_rev(t->sltype)) {
		if (t->sltype == SLTYPE_SL11H)
			printf("%s: SL11H unsupported or bus error!\n",
			    SC_NAME(sc));
		else
			printf("%s: Unknown chip revision!\n", SC_NAME(sc));
		return -1;
	}

#ifdef SLHCI_DEBUG
	if (slhci_memtest(sc)) {
		printf("%s: memory/bus error!\n", SC_NAME(sc));
		return -1;
	}
#endif

	callout_init(&sc->sc_timer, CALLOUT_MPSAFE);
	callout_setfunc(&sc->sc_timer, slhci_reset_entry, sc);

	/*
	 * It is not safe to call the soft interrupt directly as
	 * usb_schedsoftintr does in the ub_usepolling case (due to locking).
	 */
	sc->sc_cb_softintr = softint_establish(SOFTINT_NET,
	    slhci_callback_entry, sc);

	if (t->sltype == SLTYPE_SL811HS_R12)
		rev = "(rev 1.2)";
	else if (t->sltype == SLTYPE_SL811HS_R14)
		rev = "(rev 1.4 or 1.5)";
	else
		rev = "(unknown revision)";

	aprint_normal("%s: ScanLogic SL811HS/T USB Host Controller %s\n",
	    SC_NAME(sc), rev);

	aprint_normal("%s: Max Current %u mA (value by code, not by probe)\n",
	    SC_NAME(sc), t->max_current * 2);

#if defined(SLHCI_DEBUG) || defined(SLHCI_NO_OVERTIME) || \
    defined(SLHCI_TRY_LSVH) || defined(SLHCI_PROFILE_TRANSFER)
	aprint_normal("%s: driver options:"
#ifdef SLHCI_DEBUG
	" SLHCI_DEBUG"
#endif
#ifdef SLHCI_TRY_LSVH
	" SLHCI_TRY_LSVH"
#endif
#ifdef SLHCI_NO_OVERTIME
	" SLHCI_NO_OVERTIME"
#endif
#ifdef SLHCI_PROFILE_TRANSFER
	" SLHCI_PROFILE_TRANSFER"
#endif
	"\n", SC_NAME(sc));
#endif
	sc->sc_bus.ub_revision = USBREV_1_1;
	sc->sc_bus.ub_methods = __UNCONST(&slhci_bus_methods);
	sc->sc_bus.ub_pipesize = sizeof(struct slhci_pipe);
	sc->sc_bus.ub_usedma = false;

	if (!sc->sc_enable_power)
		t->flags |= F_REALPOWER;

	t->flags |= F_ACTIVE;

	/* Attach usb and uhub. */
	sc->sc_child = config_found(SC_DEV(sc), &sc->sc_bus, usbctlprint,
	    CFARGS_NONE);

	if (!sc->sc_child)
		return -1;
	else
		return 0;
}

int
slhci_detach(struct slhci_softc *sc, int flags)
{
	struct slhci_transfers *t;
	int ret;

	t = &sc->sc_transfers;

	/* By this point bus access is no longer allowed. */

	KASSERT(!(t->flags & F_ACTIVE));

	/*
	 * To be MPSAFE is not sufficient to cancel callouts and soft
	 * interrupts and assume they are dead since the code could already be
	 * running or about to run.  Wait until they are known to be done.
	 */
	while (t->flags & (F_RESET|F_CALLBACK))
		tsleep(&sc, PPAUSE, "slhci_detach", hz);

	softint_disestablish(sc->sc_cb_softintr);

	mutex_destroy(&sc->sc_lock);
	mutex_destroy(&sc->sc_intr_lock);

	ret = 0;

	if (sc->sc_child)
		ret = config_detach(sc->sc_child, flags);

#ifdef SLHCI_MEM_ACCOUNTING
	SLHCIHIST_FUNC(); SLHCIHIST_CALLED();
	if (sc->sc_mem_use) {
		printf("%s: Memory still in use after detach! mem_use (count)"
		    " = %d\n", SC_NAME(sc), sc->sc_mem_use);
		DDOLOG("Memory still in use after detach! mem_use (count)"
		    " = %d", sc->sc_mem_use, 0, 0, 0);
	}
#endif

	return ret;
}

int
slhci_activate(device_t self, enum devact act)
{
	struct slhci_softc *sc = device_private(self);

	switch (act) {
	case DVACT_DEACTIVATE:
		slhci_lock_call(sc, &slhci_halt, NULL, NULL);
		return 0;
	default:
		return EOPNOTSUPP;
	}
}

void
slhci_abort(struct usbd_xfer *xfer)
{
	SLHCIHIST_FUNC(); SLHCIHIST_CALLED();
	struct slhci_softc *sc;
	struct slhci_pipe *spipe;

	spipe = SLHCI_PIPE2SPIPE(xfer->ux_pipe);

	if (spipe == NULL)
		goto callback;

	sc = SLHCI_XFER2SC(xfer);
	KASSERT(mutex_owned(&sc->sc_lock));

	DLOG(D_TRACE, "transfer type %jd abort xfer %#jx spipe %#jx "
	    " spipe->xfer %#jx", spipe->ptype, (uintptr_t)xfer,
	    (uintptr_t)spipe, (uintptr_t)spipe->xfer);

	slhci_lock_call(sc, &slhci_do_abort, spipe, xfer);

callback:
	xfer->ux_status = USBD_CANCELLED;
	usb_transfer_complete(xfer);
}

void
slhci_close(struct usbd_pipe *pipe)
{
	SLHCIHIST_FUNC(); SLHCIHIST_CALLED();
	struct slhci_softc *sc;
	struct slhci_pipe *spipe;

	sc = SLHCI_PIPE2SC(pipe);
	spipe = SLHCI_PIPE2SPIPE(pipe);

	DLOG(D_TRACE, "transfer type %jd close spipe %#jx spipe->xfer %#jx",
	    spipe->ptype, (uintptr_t)spipe, (uintptr_t)spipe->xfer, 0);

	slhci_lock_call(sc, &slhci_close_pipe, spipe, NULL);
}

void
slhci_clear_toggle(struct usbd_pipe *pipe)
{
	SLHCIHIST_FUNC(); SLHCIHIST_CALLED();
	struct slhci_pipe *spipe;

	spipe = SLHCI_PIPE2SPIPE(pipe);

	DLOG(D_TRACE, "transfer type %jd toggle spipe %#jx", spipe->ptype,
	    (uintptr_t)spipe, 0, 0);

	spipe->pflags &= ~PF_TOGGLE;

#ifdef DIAGNOSTIC
	if (spipe->xfer != NULL) {
		struct slhci_softc *sc = (struct slhci_softc
		    *)pipe->up_dev->ud_bus;

		printf("%s: Clear toggle on transfer in progress! halted\n",
		    SC_NAME(sc));
		DDOLOG("Clear toggle on transfer in progress! halted",
		    0, 0, 0, 0);
		slhci_halt(sc, NULL, NULL);
	}
#endif
}

void
slhci_poll(struct usbd_bus *bus) /* XXX necessary? */
{
	SLHCIHIST_FUNC(); SLHCIHIST_CALLED();
	struct slhci_softc *sc;

	sc = SLHCI_BUS2SC(bus);

	DLOG(D_TRACE, "slhci_poll", 0,0,0,0);

	slhci_lock_call(sc, &slhci_do_poll, NULL, NULL);
}

void
slhci_done(struct usbd_xfer *xfer)
{
}

void
slhci_void(void *v) {}

/* End out of lock functions. Start lock entry functions. */

#ifdef SLHCI_MEM_ACCOUNTING
void
slhci_mem_use(struct usbd_bus *bus, int val)
{
	struct slhci_softc *sc = SLHCI_BUS2SC(bus);

	mutex_enter(&sc->sc_intr_lock);
	sc->sc_mem_use += val;
	mutex_exit(&sc->sc_intr_lock);
}
#endif

void
slhci_reset_entry(void *arg)
{
	SLHCIHIST_FUNC(); SLHCIHIST_CALLED();
	struct slhci_softc *sc = arg;

	mutex_enter(&sc->sc_intr_lock);
	slhci_reset(sc);
	/*
	 * We cannot call the callback directly since we could then be reset
	 * again before finishing and need the callout delay for timing.
	 * Scheduling the callout again before we exit would defeat the reap
	 * mechanism since we could be unlocked while the reset flag is not
	 * set. The callback code will check the wait queue.
	 */
	slhci_callback_schedule(sc);
	mutex_exit(&sc->sc_intr_lock);
}

usbd_status
slhci_lock_call(struct slhci_softc *sc, LockCallFunc lcf, struct slhci_pipe
    *spipe, struct usbd_xfer *xfer)
{
	usbd_status ret;

	mutex_enter(&sc->sc_intr_lock);
	ret = (*lcf)(sc, spipe, xfer);
	slhci_main(sc);
	mutex_exit(&sc->sc_intr_lock);

	return ret;
}

void
slhci_start_entry(struct slhci_softc *sc, struct slhci_pipe *spipe)
{
	struct slhci_transfers *t;

	mutex_enter(&sc->sc_intr_lock);
	t = &sc->sc_transfers;

	if (!(t->flags & (F_AINPROG|F_BINPROG))) {
		slhci_enter_xfer(sc, spipe);
		slhci_dotransfer(sc);
		slhci_main(sc);
	} else {
		enter_waitq(sc, spipe);
	}
	mutex_exit(&sc->sc_intr_lock);
}

void
slhci_callback_entry(void *arg)
{
	SLHCIHIST_FUNC(); SLHCIHIST_CALLED();
	struct slhci_softc *sc;
	struct slhci_transfers *t;

	sc = (struct slhci_softc *)arg;

	mutex_enter(&sc->sc_intr_lock);
	t = &sc->sc_transfers;
	DLOG(D_SOFT, "callback_entry flags %#jx", t->flags, 0,0,0);

repeat:
	slhci_callback(sc);

	if (!gcq_empty(&sc->sc_waitq)) {
		slhci_enter_xfers(sc);
		slhci_dotransfer(sc);
		slhci_waitintr(sc, 0);
		goto repeat;
	}

	t->flags &= ~F_CALLBACK;
	mutex_exit(&sc->sc_intr_lock);
}

void
slhci_do_callback(struct slhci_softc *sc, struct usbd_xfer *xfer)
{
	SLHCIHIST_FUNC(); SLHCIHIST_CALLED();
	KASSERT(mutex_owned(&sc->sc_intr_lock));

	start_cc_time(&t_callback, (u_int)xfer);
	mutex_exit(&sc->sc_intr_lock);

	mutex_enter(&sc->sc_lock);
	usb_transfer_complete(xfer);
	mutex_exit(&sc->sc_lock);

	mutex_enter(&sc->sc_intr_lock);
	stop_cc_time(&t_callback);
}

int
slhci_intr(void *arg)
{
	SLHCIHIST_FUNC(); SLHCIHIST_CALLED();
	struct slhci_softc *sc = arg;
	int ret = 0;
	int irq;

	start_cc_time(&t_hard_int, (unsigned int)arg);
	mutex_enter(&sc->sc_intr_lock);

	do {
		irq = slhci_dointr(sc);
		ret |= irq;
		slhci_main(sc);
	} while (irq);
	mutex_exit(&sc->sc_intr_lock);

	stop_cc_time(&t_hard_int);
	return ret;
}

/* called with interrupt lock only held. */
void
slhci_main(struct slhci_softc *sc)
{
	SLHCIHIST_FUNC(); SLHCIHIST_CALLED();
	struct slhci_transfers *t;

	t = &sc->sc_transfers;

	KASSERT(mutex_owned(&sc->sc_intr_lock));

waitcheck:
	slhci_waitintr(sc, slhci_wait_time);

	/*
	 * The direct call is needed in the ub_usepolling and disabled cases
	 * since the soft interrupt is not available.  In the disabled case,
	 * this code can be reached from the usb detach, after the reaping of
	 * the soft interrupt.  That test could be !F_ACTIVE, but there is no
	 * reason not to make the callbacks directly in the other DISABLED
	 * cases.
	 */
	if ((t->flags & F_ROOTINTR) || !gcq_empty(&t->q[Q_CALLBACKS])) {
		if (__predict_false(sc->sc_bus.ub_usepolling ||
		    t->flags & F_DISABLED))
			slhci_callback(sc);
		else
			slhci_callback_schedule(sc);
	}

	if (!gcq_empty(&sc->sc_waitq)) {
		slhci_enter_xfers(sc);
		slhci_dotransfer(sc);
		goto waitcheck;
	}
	DLOG(D_INTR, "... done", 0, 0, 0, 0);
}

/* End lock entry functions. Start in lock function. */

/* Register read/write routines and barriers. */
#ifdef SLHCI_BUS_SPACE_BARRIERS
#define BSB(a, b, c, d, e) bus_space_barrier(a, b, c, d, BUS_SPACE_BARRIER_ # e)
#define BSB_SYNC(a, b, c, d) bus_space_barrier(a, b, c, d, BUS_SPACE_BARRIER_READ|BUS_SPACE_BARRIER_WRITE)
#else /* now !SLHCI_BUS_SPACE_BARRIERS */
#define BSB(a, b, c, d, e) __USE(d)
#define BSB_SYNC(a, b, c, d)
#endif /* SLHCI_BUS_SPACE_BARRIERS */

static void
slhci_write(struct slhci_softc *sc, uint8_t addr, uint8_t data)
{
	bus_size_t paddr, pdata, pst, psz;
	bus_space_tag_t iot;
	bus_space_handle_t ioh;

	paddr = pst = 0;
	pdata = sc->sc_stride;
	psz = pdata * 2;
	iot = sc->sc_iot;
	ioh = sc->sc_ioh;

	bus_space_write_1(iot, ioh, paddr, addr);
	BSB(iot, ioh, pst, psz, WRITE_BEFORE_WRITE);
	bus_space_write_1(iot, ioh, pdata, data);
	BSB(iot, ioh, pst, psz, WRITE_BEFORE_WRITE);
}

static uint8_t
slhci_read(struct slhci_softc *sc, uint8_t addr)
{
	bus_size_t paddr, pdata, pst, psz;
	bus_space_tag_t iot;
	bus_space_handle_t ioh;
	uint8_t data;

	paddr = pst = 0;
	pdata = sc->sc_stride;
	psz = pdata * 2;
	iot = sc->sc_iot;
	ioh = sc->sc_ioh;

	bus_space_write_1(iot, ioh, paddr, addr);
	BSB(iot, ioh, pst, psz, WRITE_BEFORE_READ);
	data = bus_space_read_1(iot, ioh, pdata);
	BSB(iot, ioh, pst, psz, READ_BEFORE_WRITE);
	return data;
}

#if 0 /* auto-increment mode broken, see errata doc */
static void
slhci_write_multi(struct slhci_softc *sc, uint8_t addr, uint8_t *buf, int l)
{
	bus_size_t paddr, pdata, pst, psz;
	bus_space_tag_t iot;
	bus_space_handle_t ioh;

	paddr = pst = 0;
	pdata = sc->sc_stride;
	psz = pdata * 2;
	iot = sc->sc_iot;
	ioh = sc->sc_ioh;

	bus_space_write_1(iot, ioh, paddr, addr);
	BSB(iot, ioh, pst, psz, WRITE_BEFORE_WRITE);
	bus_space_write_multi_1(iot, ioh, pdata, buf, l);
	BSB(iot, ioh, pst, psz, WRITE_BEFORE_WRITE);
}

static void
slhci_read_multi(struct slhci_softc *sc, uint8_t addr, uint8_t *buf, int l)
{
	bus_size_t paddr, pdata, pst, psz;
	bus_space_tag_t iot;
	bus_space_handle_t ioh;

	paddr = pst = 0;
	pdata = sc->sc_stride;
	psz = pdata * 2;
	iot = sc->sc_iot;
	ioh = sc->sc_ioh;

	bus_space_write_1(iot, ioh, paddr, addr);
	BSB(iot, ioh, pst, psz, WRITE_BEFORE_READ);
	bus_space_read_multi_1(iot, ioh, pdata, buf, l);
	BSB(iot, ioh, pst, psz, READ_BEFORE_WRITE);
}
#else
static void
slhci_write_multi(struct slhci_softc *sc, uint8_t addr, uint8_t *buf, int l)
{
#if 1
	for (; l; addr++, buf++, l--)
		slhci_write(sc, addr, *buf);
#else
	bus_size_t paddr, pdata, pst, psz;
	bus_space_tag_t iot;
	bus_space_handle_t ioh;

	paddr = pst = 0;
	pdata = sc->sc_stride;
	psz = pdata * 2;
	iot = sc->sc_iot;
	ioh = sc->sc_ioh;

	for (; l; addr++, buf++, l--) {
		bus_space_write_1(iot, ioh, paddr, addr);
		BSB(iot, ioh, pst, psz, WRITE_BEFORE_WRITE);
		bus_space_write_1(iot, ioh, pdata, *buf);
		BSB(iot, ioh, pst, psz, WRITE_BEFORE_WRITE);
	}
#endif
}

static void
slhci_read_multi(struct slhci_softc *sc, uint8_t addr, uint8_t *buf, int l)
{
#if 1
	for (; l; addr++, buf++, l--)
		*buf = slhci_read(sc, addr);
#else
	bus_size_t paddr, pdata, pst, psz;
	bus_space_tag_t iot;
	bus_space_handle_t ioh;

	paddr = pst = 0;
	pdata = sc->sc_stride;
	psz = pdata * 2;
	iot = sc->sc_iot;
	ioh = sc->sc_ioh;

	for (; l; addr++, buf++, l--) {
		bus_space_write_1(iot, ioh, paddr, addr);
		BSB(iot, ioh, pst, psz, WRITE_BEFORE_READ);
		*buf = bus_space_read_1(iot, ioh, pdata);
		BSB(iot, ioh, pst, psz, READ_BEFORE_WRITE);
	}
#endif
}
#endif

/*
 * After calling waitintr it is necessary to either call slhci_callback or
 * schedule the callback if necessary.  The callback cannot be called directly
 * from the hard interrupt since it interrupts at a high IPL and callbacks
 * can do copyout and such.
 */
static void
slhci_waitintr(struct slhci_softc *sc, int wait_time)
{
	SLHCIHIST_FUNC(); SLHCIHIST_CALLED();
	struct slhci_transfers *t;

	t = &sc->sc_transfers;

	KASSERT(mutex_owned(&sc->sc_intr_lock));

	if (__predict_false(sc->sc_bus.ub_usepolling))
		wait_time = 12000;

	while (t->pend <= wait_time) {
		DLOG(D_WAIT, "waiting... frame %jd pend %jd flags %#jx",
		    t->frame, t->pend, t->flags, 0);
		LK_SLASSERT(t->flags & F_ACTIVE, sc, NULL, NULL, return);
		LK_SLASSERT(t->flags & (F_AINPROG|F_BINPROG), sc, NULL, NULL,
		    return);
		slhci_dointr(sc);
	}
	DLOG(D_WAIT, "... done", 0, 0, 0, 0);
}

static int
slhci_dointr(struct slhci_softc *sc)
{
	SLHCIHIST_FUNC(); SLHCIHIST_CALLED();
	struct slhci_transfers *t;
	struct slhci_pipe *tosp;
	uint8_t r;

	t = &sc->sc_transfers;

	KASSERT(mutex_owned(&sc->sc_intr_lock));

	if (sc->sc_ier == 0) {
		DLOG(D_INTR, "sc_ier is zero", 0, 0, 0, 0);
		return 0;
	}

	r = slhci_read(sc, SL11_ISR);

#ifdef SLHCI_DEBUG
	if (slhcidebug & SLHCI_D_INTR && r & sc->sc_ier &&
	    ((r & ~(SL11_ISR_SOF|SL11_ISR_DATA)) || slhcidebug & SLHCI_D_SOF)) {
		uint8_t e, f;

		e = slhci_read(sc, SL11_IER);
		f = slhci_read(sc, SL11_CTRL);
		DDOLOG("Flags=%#x IER=%#x ISR=%#x CTRL=%#x", t->flags, e, r, f);
		DDOLOGCTRL(f);
		DDOLOGISR(r);
	}
#endif

	/*
	 * check IER for corruption occasionally.  Assume that the above
	 * sc_ier == 0 case works correctly.
	 */
	if (__predict_false(sc->sc_ier_check++ > SLHCI_IER_CHECK_FREQUENCY)) {
		sc->sc_ier_check = 0;
		if (sc->sc_ier != slhci_read(sc, SL11_IER)) {
			printf("%s: IER value corrupted! halted\n",
			    SC_NAME(sc));
			DDOLOG("IER value corrupted! halted", 0, 0, 0, 0);
			slhci_halt(sc, NULL, NULL);
			return 1;
		}
	}

	r &= sc->sc_ier;

	if (r == 0) {
		DLOG(D_INTR, "r is zero", 0, 0, 0, 0);
		return 0;
	}

	sc->sc_ier_check = 0;

	slhci_write(sc, SL11_ISR, r);
	BSB_SYNC(sc->iot, sc->ioh, sc->pst, sc->psz);

	/* If we have an insertion event we do not care about anything else. */
	if (__predict_false(r & SL11_ISR_INSERT)) {
		slhci_insert(sc);
		DLOG(D_INTR, "... done", 0, 0, 0, 0);
		return 1;
	}

	stop_cc_time(&t_intr);
	start_cc_time(&t_intr, r);

	if (r & SL11_ISR_SOF) {
		t->frame++;

		gcq_merge_tail(&t->q[Q_CB], &t->q[Q_NEXT_CB]);

		/*
		 * SOFCHECK flags are cleared in tstart.  Two flags are needed
		 * since the first SOF interrupt processed after the transfer
		 * is started might have been generated before the transfer
		 * was started.
		 */
		if (__predict_false(t->flags & F_SOFCHECK2 && t->flags &
		    (F_AINPROG|F_BINPROG))) {
			printf("%s: Missed transfer completion. halted\n",
			    SC_NAME(sc));
			DDOLOG("Missed transfer completion. halted", 0, 0, 0,
			    0);
			slhci_halt(sc, NULL, NULL);
			return 1;
		} else if (t->flags & F_SOFCHECK1) {
			t->flags |= F_SOFCHECK2;
		} else
			t->flags |= F_SOFCHECK1;

		if (t->flags & F_CHANGE)
			t->flags |= F_ROOTINTR;

		while (__predict_true(GOT_FIRST_TO(tosp, t)) &&
		    __predict_false(tosp->to_frame <= t->frame)) {
			tosp->xfer->ux_status = USBD_TIMEOUT;
			slhci_do_abort(sc, tosp, tosp->xfer);
			enter_callback(t, tosp);
		}

		/*
		 * Start any waiting transfers right away.  If none, we will
		 * start any new transfers later.
		 */
		slhci_tstart(sc);
	}

	if (r & (SL11_ISR_USBA|SL11_ISR_USBB)) {
		int ab;

		if ((r & (SL11_ISR_USBA|SL11_ISR_USBB)) ==
		    (SL11_ISR_USBA|SL11_ISR_USBB)) {
			if (!(t->flags & (F_AINPROG|F_BINPROG)))
				return 1; /* presume card pulled */

			LK_SLASSERT((t->flags & (F_AINPROG|F_BINPROG)) !=
			    (F_AINPROG|F_BINPROG), sc, NULL, NULL, return 1);

			/*
			 * This should never happen (unless card removal just
			 * occurred) but appeared frequently when both
			 * transfers were started at the same time and was
			 * accompanied by data corruption.  It still happens
			 * at times.  I have not seen data correption except
			 * when the STATUS bit gets set, which now causes the
			 * driver to halt, however this should still not
			 * happen so the warning is kept.  See comment in
			 * abdone, below.
			 */
			printf("%s: Transfer reported done but not started! "
			    "Verify data integrity if not detaching. "
			    " flags %#x r %x\n", SC_NAME(sc), t->flags, r);

			if (!(t->flags & F_AINPROG))
				r &= ~SL11_ISR_USBA;
			else
				r &= ~SL11_ISR_USBB;
		}
		t->pend = INT_MAX;

		if (r & SL11_ISR_USBA)
			ab = A;
		else
			ab = B;

		/*
		 * This happens when a low speed device is attached to
		 * a hub with chip rev 1.5.  SOF stops, but a few transfers
		 * still work before causing this error.
		 */
		if (!(t->flags & (ab ? F_BINPROG : F_AINPROG))) {
			printf("%s: %s done but not in progress! halted\n",
			    SC_NAME(sc), ab ? "B" : "A");
			DDOLOG("AB=%d done but not in progress! halted", ab,
			    0, 0, 0);
			slhci_halt(sc, NULL, NULL);
			return 1;
		}

		t->flags &= ~(ab ? F_BINPROG : F_AINPROG);
		slhci_tstart(sc);
		stop_cc_time(&t_ab[ab]);
		start_cc_time(&t_abdone, t->flags);
		slhci_abdone(sc, ab);
		stop_cc_time(&t_abdone);
	}

	slhci_dotransfer(sc);

	DLOG(D_INTR, "... done", 0, 0, 0, 0);

	return 1;
}

static void
slhci_abdone(struct slhci_softc *sc, int ab)
{
	SLHCIHIST_FUNC(); SLHCIHIST_CALLED();
	struct slhci_transfers *t;
	struct slhci_pipe *spipe;
	struct usbd_xfer *xfer;
	uint8_t status, buf_start;
	uint8_t *target_buf;
	unsigned int actlen;
	int head;

	t = &sc->sc_transfers;

	KASSERT(mutex_owned(&sc->sc_intr_lock));

	DLOG(D_TRACE, "ABDONE flags %#jx", t->flags, 0,0,0);

	DLOG(D_MSG, "DONE AB=%jd spipe %#jx len %jd xfer %#jx", ab,
	    t->spipe[ab], (uintptr_t)t->len[ab],
	    (uintptr_t)(t->spipe[ab] ? t->spipe[ab]->xfer : NULL));

	spipe = t->spipe[ab];

	/*
	 * skip this one if aborted; do not call return from the rest of the
	 * function unless halting, else t->len will not be cleared.
	 */
	if (spipe == NULL)
		goto done;

	t->spipe[ab] = NULL;

	xfer = spipe->xfer;

	gcq_remove(&spipe->to);

	LK_SLASSERT(xfer != NULL, sc, spipe, NULL, return);

	status = slhci_read(sc, slhci_tregs[ab][STAT]);

	/*
	 * I saw no status or remaining length greater than the requested
	 * length in early driver versions in circumstances I assumed caused
	 * excess power draw.  I am no longer able to reproduce this when
	 * causing excess power draw circumstances.
	 *
	 * Disabling a power check and attaching aue to a keyboard and hub
	 * that is directly attached (to CFU1U, 100mA max, aue 160mA, keyboard
	 * 98mA) sometimes works and sometimes fails to configure.  After
	 * removing the aue and attaching a self-powered umass dvd reader
	 * (unknown if it draws power from the host also) soon a single Error
	 * status occurs then only timeouts. The controller soon halts freeing
	 * memory due to being ONQU instead of BUSY.  This may be the same
	 * basic sequence that caused the no status/bad length errors.  The
	 * umass device seems to work (better at least) with the keyboard hub
	 * when not first attaching aue (tested once reading an approximately
	 * 200MB file).
	 *
	 * Overflow can indicate that the device and host disagree about how
	 * much data has been transferred.  This may indicate a problem at any
	 * point during the transfer, not just when the error occurs.  It may
	 * indicate data corruption.  A warning message is printed.
	 *
	 * Trying to use both A and B transfers at the same time results in
	 * incorrect transfer completion ISR reports and the status will then
	 * include SL11_EPSTAT_SETUP, which is apparently set while the
	 * transfer is in progress.  I also noticed data corruption, even
	 * after waiting for the transfer to complete. The driver now avoids
	 * trying to start both at the same time.
	 *
	 * I had accidently initialized the B registers before they were valid
	 * in some driver versions.  Since every other performance enhancing
	 * feature has been confirmed buggy in the errata doc, I have not
	 * tried both transfers at once again with the documented
	 * initialization order.
	 *
	 * However, I have seen this problem again ("done but not started"
	 * errors), which in some cases cases the SETUP status bit to remain
	 * set on future transfers.  In other cases, the SETUP bit is not set
	 * and no data corruption occurs.  This occurred while using both umass
	 * and aue on a powered hub (maybe triggered by some local activity
	 * also) and needs several reads of the 200MB file to trigger.  The
	 * driver now halts if SETUP is detected.
 	 */

	actlen = 0;

	if (__predict_false(!status)) {
		DDOLOG("no status! xfer %p spipe %p", xfer, spipe, 0,0);
		printf("%s: no status! halted\n", SC_NAME(sc));
		slhci_halt(sc, spipe, xfer);
		return;
	}

#ifdef SLHCI_DEBUG
	if ((slhcidebug & SLHCI_D_NAK) ||
	    (status & SL11_EPSTAT_ERRBITS) != SL11_EPSTAT_NAK) {
	    	DDOLOG("USB Status = %#.2x", status, 0, 0, 0);
		DDOLOGSTATUS(status);
	}
#endif

	if (!(status & SL11_EPSTAT_ERRBITS)) {
		unsigned int cont = slhci_read(sc, slhci_tregs[ab][CONT]);
		unsigned int len = spipe->tregs[LEN];
		DLOG(D_XFER, "cont %jd len %jd", cont, len, 0, 0);
		if ((status & SL11_EPSTAT_OVERFLOW) || cont > len) {
			DDOLOG("overflow - cont %d len %d xfer->ux_length %d "
			    "xfer->actlen %d", cont, len, xfer->ux_length,
			    xfer->ux_actlen);
			printf("%s: overflow cont %d len %d xfer->ux_length"
			    " %d xfer->ux_actlen %d\n", SC_NAME(sc), cont,
			    len, xfer->ux_length, xfer->ux_actlen);
			actlen = len;
		} else {
			actlen = len - cont;
		}
		spipe->nerrs = 0;
	}

	/* Actual copyin done after starting next transfer. */
	if (actlen && (spipe->tregs[PID] & SL11_PID_BITS) == SL11_PID_IN) {
		target_buf = spipe->buffer;
		buf_start = spipe->tregs[ADR];
	} else {
		target_buf = NULL;
		buf_start = 0; /* XXX gcc uninitialized warnings */
	}

	if (status & SL11_EPSTAT_ERRBITS) {
		status &= SL11_EPSTAT_ERRBITS;
		if (status & SL11_EPSTAT_SETUP) {
			printf("%s: Invalid controller state detected! "
			    "halted\n", SC_NAME(sc));
			DDOLOG("Invalid controller state detected! "
			    "halted", 0, 0, 0, 0);
			slhci_halt(sc, spipe, xfer);
			return;
		} else if (__predict_false(sc->sc_bus.ub_usepolling)) {
			head = Q_CALLBACKS;
			if (status & SL11_EPSTAT_STALL)
				xfer->ux_status = USBD_STALLED;
			else if (status & SL11_EPSTAT_TIMEOUT)
				xfer->ux_status = USBD_TIMEOUT;
			else if (status & SL11_EPSTAT_NAK)
				head = Q_NEXT_CB;
			else
				xfer->ux_status = USBD_IOERROR;
		} else if (status & SL11_EPSTAT_NAK) {
			int i = spipe->pipe.up_interval;
			if (i == 0)
				i = 1;
			DDOLOG("xfer %p spipe %p NAK delay by %d", xfer, spipe,
			    i, 0);
			spipe->lastframe = spipe->frame = t->frame + i;
			slhci_queue_timed(sc, spipe);
			goto queued;
		} else if (++spipe->nerrs > SLHCI_MAX_RETRIES ||
		    (status & SL11_EPSTAT_STALL)) {
			DDOLOG("xfer %p spipe %p nerrs %d", xfer, spipe,
			    spipe->nerrs, 0);
			if (status & SL11_EPSTAT_STALL)
				xfer->ux_status = USBD_STALLED;
			else if (status & SL11_EPSTAT_TIMEOUT)
				xfer->ux_status = USBD_TIMEOUT;
			else
				xfer->ux_status = USBD_IOERROR;

			DLOG(D_ERR, "Max retries reached! status %#jx "
			    "xfer->ux_status %jd", status, xfer->ux_status, 0,
			    0);
			DDOLOGSTATUS(status);

			head = Q_CALLBACKS;
		} else {
			head = Q_NEXT_CB;
		}
	} else if (spipe->ptype == PT_CTRL_SETUP) {
		spipe->tregs[PID] = spipe->newpid;

		if (xfer->ux_length) {
			LK_SLASSERT(spipe->newlen[1] != 0, sc, spipe, xfer,
			    return);
			spipe->tregs[LEN] = spipe->newlen[1];
			spipe->bustime = spipe->newbustime[1];
			spipe->buffer = xfer->ux_buf;
			spipe->ptype = PT_CTRL_DATA;
		} else {
status_setup:
			/* CTRL_DATA swaps direction in PID then jumps here */
			spipe->tregs[LEN] = 0;
			if (spipe->pflags & PF_LS)
				spipe->bustime = SLHCI_LS_CONST;
			else
				spipe->bustime = SLHCI_FS_CONST;
			spipe->ptype = PT_CTRL_STATUS;
			spipe->buffer = NULL;
		}

		/* Status or first data packet must be DATA1. */
		spipe->control |= SL11_EPCTRL_DATATOGGLE;
		if ((spipe->tregs[PID] & SL11_PID_BITS) == SL11_PID_IN)
			spipe->control &= ~SL11_EPCTRL_DIRECTION;
		else
			spipe->control |= SL11_EPCTRL_DIRECTION;

		head = Q_CB;
	} else if (spipe->ptype == PT_CTRL_STATUS) {
		head = Q_CALLBACKS;
	} else { /* bulk, intr, control data */
		xfer->ux_actlen += actlen;
		spipe->control ^= SL11_EPCTRL_DATATOGGLE;

		if (actlen == spipe->tregs[LEN] &&
		    (xfer->ux_length > xfer->ux_actlen || spipe->wantshort)) {
			spipe->buffer += actlen;
			LK_SLASSERT(xfer->ux_length >= xfer->ux_actlen, sc,
			    spipe, xfer, return);
			if (xfer->ux_length - xfer->ux_actlen < actlen) {
				spipe->wantshort = 0;
				spipe->tregs[LEN] = spipe->newlen[0];
				spipe->bustime = spipe->newbustime[0];
				LK_SLASSERT(xfer->ux_actlen +
				    spipe->tregs[LEN] == xfer->ux_length, sc,
				    spipe, xfer, return);
			}
			head = Q_CB;
		} else if (spipe->ptype == PT_CTRL_DATA) {
			spipe->tregs[PID] ^= SLHCI_PID_SWAP_IN_OUT;
			goto status_setup;
		} else {
			if (spipe->ptype == PT_INTR) {
				spipe->lastframe +=
				    spipe->pipe.up_interval;
				/*
				 * If ack, we try to keep the
				 * interrupt rate by using lastframe
				 * instead of the current frame.
				 */
				spipe->frame = spipe->lastframe +
				    spipe->pipe.up_interval;
			}

			/*
			 * Set the toggle for the next transfer.  It
			 * has already been toggled above, so the
			 * current setting will apply to the next
			 * transfer.
			 */
			if (spipe->control & SL11_EPCTRL_DATATOGGLE)
				spipe->pflags |= PF_TOGGLE;
			else
				spipe->pflags &= ~PF_TOGGLE;

			head = Q_CALLBACKS;
		}
	}

	if (head == Q_CALLBACKS) {
		gcq_remove(&spipe->to);

	 	if (xfer->ux_status == USBD_IN_PROGRESS) {
			LK_SLASSERT(xfer->ux_actlen <= xfer->ux_length, sc,
			    spipe, xfer, return);
			xfer->ux_status = USBD_NORMAL_COMPLETION;
		}
	}

	enter_q(t, spipe, head);

queued:
	if (target_buf != NULL) {
		slhci_dotransfer(sc);
		start_cc_time(&t_copy_from_dev, actlen);
		slhci_read_multi(sc, buf_start, target_buf, actlen);
		stop_cc_time(&t_copy_from_dev);
		DLOGBUF(D_BUF, target_buf, actlen);
		t->pend -= SLHCI_FS_CONST + SLHCI_FS_DATA_TIME(actlen);
	}

done:
	t->len[ab] = -1;
}

static void
slhci_tstart(struct slhci_softc *sc)
{
	struct slhci_transfers *t;
	struct slhci_pipe *spipe;
	int remaining_bustime;

	t = &sc->sc_transfers;

	KASSERT(mutex_owned(&sc->sc_intr_lock));

	if (!(t->flags & (F_AREADY|F_BREADY)))
		return;

	if (t->flags & (F_AINPROG|F_BINPROG|F_DISABLED))
		return;

	/*
	 * We have about 6 us to get from the bus time check to
	 * starting the transfer or we might babble or the chip might fail to
	 * signal transfer complete.  This leaves no time for any other
	 * interrupts.
	 */
	remaining_bustime = (int)(slhci_read(sc, SL811_CSOF)) << 6;
	remaining_bustime -= SLHCI_END_BUSTIME;

	/*
	 * Start one transfer only, clearing any aborted transfers that are
	 * not yet in progress and skipping missed isoc. It is easier to copy
	 * & paste most of the A/B sections than to make the logic work
	 * otherwise and this allows better constant use.
	 */
	if (t->flags & F_AREADY) {
		spipe = t->spipe[A];
		if (spipe == NULL) {
			t->flags &= ~F_AREADY;
			t->len[A] = -1;
		} else if (remaining_bustime >= spipe->bustime) {
			t->flags &= ~(F_AREADY|F_SOFCHECK1|F_SOFCHECK2);
			t->flags |= F_AINPROG;
			start_cc_time(&t_ab[A], spipe->tregs[LEN]);
			slhci_write(sc, SL11_E0CTRL, spipe->control);
			goto pend;
		}
	}
	if (t->flags & F_BREADY) {
		spipe = t->spipe[B];
		if (spipe == NULL) {
			t->flags &= ~F_BREADY;
			t->len[B] = -1;
		} else if (remaining_bustime >= spipe->bustime) {
			t->flags &= ~(F_BREADY|F_SOFCHECK1|F_SOFCHECK2);
			t->flags |= F_BINPROG;
			start_cc_time(&t_ab[B], spipe->tregs[LEN]);
			slhci_write(sc, SL11_E1CTRL, spipe->control);
pend:
			t->pend = spipe->bustime;
		}
	}
}

static void
slhci_dotransfer(struct slhci_softc *sc)
{
	SLHCIHIST_FUNC(); SLHCIHIST_CALLED();
	struct slhci_transfers *t;
	struct slhci_pipe *spipe;
	int ab, i;

	t = &sc->sc_transfers;

	KASSERT(mutex_owned(&sc->sc_intr_lock));

 	while ((t->len[A] == -1 || t->len[B] == -1) &&
	    (GOT_FIRST_TIMED_COND(spipe, t, spipe->frame <= t->frame) ||
	    GOT_FIRST_CB(spipe, t))) {
		LK_SLASSERT(spipe->xfer != NULL, sc, spipe, NULL, return);
		LK_SLASSERT(spipe->ptype != PT_ROOT_CTRL && spipe->ptype !=
		    PT_ROOT_INTR, sc, spipe, NULL, return);

		/* Check that this transfer can fit in the remaining memory. */
		if (t->len[A] + t->len[B] + spipe->tregs[LEN] + 1 >
		    SL11_MAX_PACKET_SIZE) {
			DLOG(D_XFER, "Transfer does not fit. alen %jd blen %jd "
			    "len %jd", t->len[A], t->len[B], spipe->tregs[LEN],
			    0);
			return;
		}

		gcq_remove(&spipe->xq);

		if (t->len[A] == -1) {
			ab = A;
			spipe->tregs[ADR] = SL11_BUFFER_START;
		} else {
			ab = B;
			spipe->tregs[ADR] = SL11_BUFFER_END -
			    spipe->tregs[LEN];
		}

		t->len[ab] = spipe->tregs[LEN];

		if (spipe->tregs[LEN] && (spipe->tregs[PID] & SL11_PID_BITS)
		    != SL11_PID_IN) {
			start_cc_time(&t_copy_to_dev,
			    spipe->tregs[LEN]);
			slhci_write_multi(sc, spipe->tregs[ADR],
			    spipe->buffer, spipe->tregs[LEN]);
			stop_cc_time(&t_copy_to_dev);
			t->pend -= SLHCI_FS_CONST +
			    SLHCI_FS_DATA_TIME(spipe->tregs[LEN]);
		}

		DLOG(D_MSG, "NEW TRANSFER AB=%jd flags %#jx alen %jd blen %jd",
		    ab, t->flags, t->len[0], t->len[1]);

		if (spipe->tregs[LEN])
			i = 0;
		else
			i = 1;

		for (; i <= 3; i++)
			if (t->current_tregs[ab][i] != spipe->tregs[i]) {
				t->current_tregs[ab][i] = spipe->tregs[i];
				slhci_write(sc, slhci_tregs[ab][i],
				    spipe->tregs[i]);
			}

		DLOG(D_SXFER, "Transfer len %jd pid %#jx dev %jd type %jd",
		    spipe->tregs[LEN], spipe->tregs[PID], spipe->tregs[DEV],
	    	    spipe->ptype);

		t->spipe[ab] = spipe;
		t->flags |= ab ? F_BREADY : F_AREADY;

		slhci_tstart(sc);
	}
}

/*
 * slhci_callback is called after the lock is taken.
 */
static void
slhci_callback(struct slhci_softc *sc)
{
	SLHCIHIST_FUNC(); SLHCIHIST_CALLED();
	struct slhci_transfers *t;
	struct slhci_pipe *spipe;
	struct usbd_xfer *xfer;

	t = &sc->sc_transfers;

	KASSERT(mutex_owned(&sc->sc_intr_lock));

	DLOG(D_SOFT, "CB flags %#jx", t->flags, 0,0,0);
	for (;;) {
		if (__predict_false(t->flags & F_ROOTINTR)) {
			t->flags &= ~F_ROOTINTR;
			if (t->rootintr != NULL) {
				u_char *p;

				KASSERT(t->rootintr->ux_status ==
				    USBD_IN_PROGRESS);
				p = t->rootintr->ux_buf;
				p[0] = 2;
				t->rootintr->ux_actlen = 1;
				t->rootintr->ux_status = USBD_NORMAL_COMPLETION;
				xfer = t->rootintr;
				goto do_callback;
			}
		}


		if (!DEQUEUED_CALLBACK(spipe, t))
			return;

		xfer = spipe->xfer;
		LK_SLASSERT(xfer != NULL, sc, spipe, NULL, return);
		spipe->xfer = NULL;
		DLOG(D_XFER, "xfer callback length %jd actlen %jd spipe %#jx "
		    "type %jd", xfer->ux_length, (uintptr_t)xfer->ux_actlen,
		    (uintptr_t)spipe, spipe->ptype);
do_callback:
		slhci_do_callback(sc, xfer);
	}
}

static void
slhci_enter_xfer(struct slhci_softc *sc, struct slhci_pipe *spipe)
{
	SLHCIHIST_FUNC(); SLHCIHIST_CALLED();
	struct slhci_transfers *t;

	t = &sc->sc_transfers;

	KASSERT(mutex_owned(&sc->sc_intr_lock));

	if (__predict_false(t->flags & F_DISABLED) ||
	    __predict_false(spipe->pflags & PF_GONE)) {
		DLOG(D_MSG, "slhci_enter_xfer: DISABLED or GONE", 0,0,0,0);
		spipe->xfer->ux_status = USBD_CANCELLED;
	}

	if (spipe->xfer->ux_status == USBD_IN_PROGRESS) {
		if (spipe->xfer->ux_timeout) {
			spipe->to_frame = t->frame + spipe->xfer->ux_timeout;
			slhci_xfer_timer(sc, spipe);
		}
		if (spipe->pipe.up_interval)
			slhci_queue_timed(sc, spipe);
		else
			enter_q(t, spipe, Q_CB);
	} else
		enter_callback(t, spipe);
}

static void
slhci_enter_xfers(struct slhci_softc *sc)
{
	struct slhci_pipe *spipe;

	KASSERT(mutex_owned(&sc->sc_intr_lock));

	while (DEQUEUED_WAITQ(spipe, sc))
		slhci_enter_xfer(sc, spipe);
}

static void
slhci_queue_timed(struct slhci_softc *sc, struct slhci_pipe *spipe)
{
	struct slhci_transfers *t;
	struct gcq *q;
	struct slhci_pipe *spp;

	t = &sc->sc_transfers;

	KASSERT(mutex_owned(&sc->sc_intr_lock));

	FIND_TIMED(q, t, spp, spp->frame > spipe->frame);
	gcq_insert_before(q, &spipe->xq);
}

static void
slhci_xfer_timer(struct slhci_softc *sc, struct slhci_pipe *spipe)
{
	struct slhci_transfers *t;
	struct gcq *q;
	struct slhci_pipe *spp;

	t = &sc->sc_transfers;

	KASSERT(mutex_owned(&sc->sc_intr_lock));

	FIND_TO(q, t, spp, spp->to_frame >= spipe->to_frame);
	gcq_insert_before(q, &spipe->to);
}

static void
slhci_callback_schedule(struct slhci_softc *sc)
{
	SLHCIHIST_FUNC(); SLHCIHIST_CALLED();
	struct slhci_transfers *t;

	t = &sc->sc_transfers;

	KASSERT(mutex_owned(&sc->sc_intr_lock));

	if (t->flags & F_ACTIVE)
		slhci_do_callback_schedule(sc);
}

static void
slhci_do_callback_schedule(struct slhci_softc *sc)
{
	SLHCIHIST_FUNC(); SLHCIHIST_CALLED();
	struct slhci_transfers *t;

	t = &sc->sc_transfers;

	KASSERT(mutex_owned(&sc->sc_intr_lock));

	DLOG(D_MSG, "flags %#jx", t->flags, 0, 0, 0);
	if (!(t->flags & F_CALLBACK)) {
		t->flags |= F_CALLBACK;
		softint_schedule(sc->sc_cb_softintr);
	}
}

#if 0
/* must be called with lock taken. */
/* XXX static */ void
slhci_pollxfer(struct slhci_softc *sc, struct usbd_xfer *xfer)
{
	KASSERT(mutex_owned(&sc->sc_intr_lock));
	slhci_dotransfer(sc);
	do {
		slhci_dointr(sc);
	} while (xfer->ux_status == USBD_IN_PROGRESS);
	slhci_do_callback(sc, xfer);
}
#endif

static usbd_status
slhci_do_poll(struct slhci_softc *sc, struct slhci_pipe *spipe, struct
    usbd_xfer *xfer)
{
	slhci_waitintr(sc, 0);

	return USBD_NORMAL_COMPLETION;
}

static usbd_status
slhci_lsvh_warn(struct slhci_softc *sc, struct slhci_pipe *spipe, struct
    usbd_xfer *xfer)
{
	SLHCIHIST_FUNC(); SLHCIHIST_CALLED();
	struct slhci_transfers *t;

	t = &sc->sc_transfers;

	if (!(t->flags & F_LSVH_WARNED)) {
		printf("%s: Low speed device via hub disabled, "
		    "see slhci(4)\n", SC_NAME(sc));
		DDOLOG("Low speed device via hub disabled, "
		    "see slhci(4)", SC_NAME(sc), 0,0,0);
		t->flags |= F_LSVH_WARNED;
	}
	return USBD_INVAL;
}

static usbd_status
slhci_isoc_warn(struct slhci_softc *sc, struct slhci_pipe *spipe, struct
    usbd_xfer *xfer)
{
	SLHCIHIST_FUNC(); SLHCIHIST_CALLED();
	struct slhci_transfers *t;

	t = &sc->sc_transfers;

	if (!(t->flags & F_ISOC_WARNED)) {
		printf("%s: ISOC transfer not supported "
		    "(see slhci(4))\n", SC_NAME(sc));
		DDOLOG("ISOC transfer not supported "
		    "(see slhci(4))", 0, 0, 0, 0);
		t->flags |= F_ISOC_WARNED;
	}
	return USBD_INVAL;
}

static usbd_status
slhci_open_pipe(struct slhci_softc *sc, struct slhci_pipe *spipe, struct
    usbd_xfer *xfer)
{
	struct slhci_transfers *t;
	struct usbd_pipe *pipe;

	t = &sc->sc_transfers;
	pipe = &spipe->pipe;

	if (t->flags & F_DISABLED)
		return USBD_CANCELLED;
	else if (pipe->up_interval && !slhci_reserve_bustime(sc, spipe, 1))
		return USBD_PENDING_REQUESTS;
	else {
		enter_all_pipes(t, spipe);
		return USBD_NORMAL_COMPLETION;
	}
}

static usbd_status
slhci_close_pipe(struct slhci_softc *sc, struct slhci_pipe *spipe, struct
    usbd_xfer *xfer)
{
	struct usbd_pipe *pipe;

	pipe = &spipe->pipe;

	if (pipe->up_interval && spipe->ptype != PT_ROOT_INTR)
		slhci_reserve_bustime(sc, spipe, 0);
	gcq_remove(&spipe->ap);
	return USBD_NORMAL_COMPLETION;
}

static usbd_status
slhci_do_abort(struct slhci_softc *sc, struct slhci_pipe *spipe, struct
    usbd_xfer *xfer)
{
	struct slhci_transfers *t;

	t = &sc->sc_transfers;

	KASSERT(mutex_owned(&sc->sc_intr_lock));

	if (spipe->xfer == xfer) {
		if (spipe->ptype == PT_ROOT_INTR) {
			if (t->rootintr == spipe->xfer) /* XXX assert? */
				t->rootintr = NULL;
		} else {
			gcq_remove(&spipe->to);
			gcq_remove(&spipe->xq);

			if (t->spipe[A] == spipe) {
				t->spipe[A] = NULL;
				if (!(t->flags & F_AINPROG))
					t->len[A] = -1;
			} else if (t->spipe[B] == spipe) {
					t->spipe[B] = NULL;
				if (!(t->flags & F_BINPROG))
					t->len[B] = -1;
			}
		}

		if (xfer->ux_status != USBD_TIMEOUT) {
			spipe->xfer = NULL;
			spipe->pipe.up_repeat = 0; /* XXX timeout? */
		}
	}

	return USBD_NORMAL_COMPLETION;
}

/*
 * Called to deactivate or stop use of the controller instead of panicking.
 * Will cancel the xfer correctly even when not on a list.
 */
static usbd_status
slhci_halt(struct slhci_softc *sc, struct slhci_pipe *spipe,
    struct usbd_xfer *xfer)
{
	SLHCIHIST_FUNC(); SLHCIHIST_CALLED();
	struct slhci_transfers *t;

	KASSERT(mutex_owned(&sc->sc_intr_lock));

	t = &sc->sc_transfers;

	DDOLOG("Halt! sc %p spipe %p xfer %p", sc, spipe, xfer, 0);

	if (spipe != NULL)
		slhci_log_spipe(spipe);

	if (xfer != NULL)
		slhci_log_xfer(xfer);

	if (spipe != NULL && xfer != NULL && spipe->xfer == xfer &&
	    !gcq_onlist(&spipe->xq) && t->spipe[A] != spipe && t->spipe[B] !=
	    spipe) {
		xfer->ux_status = USBD_CANCELLED;
		enter_callback(t, spipe);
	}

	if (t->flags & F_ACTIVE) {
		slhci_intrchange(sc, 0);
		/*
		 * leave power on when halting in case flash devices or disks
		 * are attached, which may be writing and could be damaged
		 * by abrupt power loss.  The root hub clear power feature
		 * should still work after halting.
		 */
	}

	t->flags &= ~F_ACTIVE;
	t->flags |= F_UDISABLED;
	if (!(t->flags & F_NODEV))
		t->flags |= F_NODEV|F_CCONNECT|F_ROOTINTR;
	slhci_drain(sc);

	/* One last callback for the drain and device removal. */
	slhci_do_callback_schedule(sc);

	return USBD_NORMAL_COMPLETION;
}

/*
 * There are three interrupt states: no interrupts during reset and after
 * device deactivation, INSERT only for no device present but power on, and
 * SOF, INSERT, ADONE, and BDONE when device is present.
 */
static void
slhci_intrchange(struct slhci_softc *sc, uint8_t new_ier)
{
	SLHCIHIST_FUNC(); SLHCIHIST_CALLED();
	KASSERT(mutex_owned(&sc->sc_intr_lock));
	if (sc->sc_ier != new_ier) {
		DLOG(D_INTR, "New IER %#jx", new_ier, 0, 0, 0);
		sc->sc_ier = new_ier;
		slhci_write(sc, SL11_IER, new_ier);
		BSB_SYNC(sc->iot, sc->ioh, sc->pst, sc->psz);
	}
}

/*
 * Drain: cancel all pending transfers and put them on the callback list and
 * set the UDISABLED flag.  UDISABLED is cleared only by reset.
 */
static void
slhci_drain(struct slhci_softc *sc)
{
	SLHCIHIST_FUNC(); SLHCIHIST_CALLED();
	struct slhci_transfers *t;
	struct slhci_pipe *spipe;
	struct gcq *q;
	int i;

 	KASSERT(mutex_owned(&sc->sc_intr_lock));

	t = &sc->sc_transfers;

	DLOG(D_MSG, "DRAIN flags %#jx", t->flags, 0,0,0);

	t->pend = INT_MAX;

	for (i = 0; i <= 1; i++) {
		t->len[i] = -1;
		if (t->spipe[i] != NULL) {
			enter_callback(t, t->spipe[i]);
			t->spipe[i] = NULL;
		}
	}

	/* Merge the queues into the callback queue. */
	gcq_merge_tail(&t->q[Q_CALLBACKS], &t->q[Q_CB]);
	gcq_merge_tail(&t->q[Q_CALLBACKS], &t->q[Q_NEXT_CB]);
	gcq_merge_tail(&t->q[Q_CALLBACKS], &t->timed);

	/*
	 * Cancel all pipes.  Note that not all of these may be on the
	 * callback queue yet; some could be in slhci_start, for example.
	 */
	FOREACH_AP(q, t, spipe) {
		spipe->pflags |= PF_GONE;
		spipe->pipe.up_repeat = 0;
		spipe->pipe.up_aborting = 1;
		if (spipe->xfer != NULL)
			spipe->xfer->ux_status = USBD_CANCELLED;
	}

	gcq_remove_all(&t->to);

	t->flags |= F_UDISABLED;
	t->flags &= ~(F_AREADY|F_BREADY|F_AINPROG|F_BINPROG|F_LOWSPEED);
}

/*
 * RESET: SL11_CTRL_RESETENGINE=1 and SL11_CTRL_JKSTATE=0 for 50ms
 * reconfigure SOF after reset, must wait 2.5us before USB bus activity (SOF)
 * check attached device speed.
 * must wait 100ms before USB transaction according to app note, 10ms
 * by spec.  uhub does this delay
 *
 * Started from root hub set feature reset, which does step one.
 * ub_usepolling will call slhci_reset directly, otherwise the callout goes
 * through slhci_reset_entry.
 */
void
slhci_reset(struct slhci_softc *sc)
{
	SLHCIHIST_FUNC(); SLHCIHIST_CALLED();
	struct slhci_transfers *t;
	struct slhci_pipe *spipe;
	struct gcq *q;
	uint8_t r, pol, ctrl;

	t = &sc->sc_transfers;
	KASSERT(mutex_owned(&sc->sc_intr_lock));

	stop_cc_time(&t_delay);

	KASSERT(t->flags & F_ACTIVE);

	start_cc_time(&t_delay, 0);
	stop_cc_time(&t_delay);

	slhci_write(sc, SL11_CTRL, 0);
	start_cc_time(&t_delay, 3);
	DELAY(3);
	stop_cc_time(&t_delay);
	slhci_write(sc, SL11_ISR, 0xff);

	r = slhci_read(sc, SL11_ISR);

	if (r & SL11_ISR_INSERT)
		slhci_write(sc, SL11_ISR, SL11_ISR_INSERT);

	if (r & SL11_ISR_NODEV) {
		DLOG(D_MSG, "NC", 0,0,0,0);
		/*
		 * Normally, the hard interrupt insert routine will issue
		 * CCONNECT, however we need to do it here if the detach
		 * happened during reset.
		 */
		if (!(t->flags & F_NODEV))
			t->flags |= F_CCONNECT|F_ROOTINTR|F_NODEV;
		slhci_intrchange(sc, SL11_IER_INSERT);
	} else {
		if (t->flags & F_NODEV)
			t->flags |= F_CCONNECT;
		t->flags &= ~(F_NODEV|F_LOWSPEED);
		if (r & SL11_ISR_DATA) {
			DLOG(D_MSG, "FS", 0,0,0,0);
			pol = ctrl = 0;
		} else {
			DLOG(D_MSG, "LS", 0,0,0,0);
			pol  = SL811_CSOF_POLARITY;
			ctrl = SL11_CTRL_LOWSPEED;
			t->flags |= F_LOWSPEED;
		}

		/* Enable SOF auto-generation */
		t->frame = 0;	/* write to SL811_CSOF will reset frame */
		slhci_write(sc, SL11_SOFTIME, 0xe0);
		slhci_write(sc, SL811_CSOF, pol|SL811_CSOF_MASTER|0x2e);
		slhci_write(sc, SL11_CTRL, ctrl|SL11_CTRL_ENABLESOF);

		/*
		 * According to the app note, ARM must be set
		 * for SOF generation to work.  We initialize all
		 * USBA registers here for current_tregs.
		 */
		slhci_write(sc, SL11_E0ADDR, SL11_BUFFER_START);
		slhci_write(sc, SL11_E0LEN, 0);
		slhci_write(sc, SL11_E0PID, SL11_PID_SOF);
		slhci_write(sc, SL11_E0DEV, 0);
		slhci_write(sc, SL11_E0CTRL, SL11_EPCTRL_ARM);

		/*
		 * Initialize B registers.  This can't be done earlier since
		 * they are not valid until the SL811_CSOF register is written
		 * above due to SL11H compatibility.
		 */
		slhci_write(sc, SL11_E1ADDR, SL11_BUFFER_END - 8);
		slhci_write(sc, SL11_E1LEN, 0);
		slhci_write(sc, SL11_E1PID, 0);
		slhci_write(sc, SL11_E1DEV, 0);

		t->current_tregs[0][ADR] = SL11_BUFFER_START;
		t->current_tregs[0][LEN] = 0;
		t->current_tregs[0][PID] = SL11_PID_SOF;
		t->current_tregs[0][DEV] = 0;
		t->current_tregs[1][ADR] = SL11_BUFFER_END - 8;
		t->current_tregs[1][LEN] = 0;
		t->current_tregs[1][PID] = 0;
		t->current_tregs[1][DEV] = 0;

		/* SOF start will produce USBA interrupt */
		t->len[A] = 0;
		t->flags |= F_AINPROG;

		slhci_intrchange(sc, SLHCI_NORMAL_INTERRUPTS);
	}

	t->flags &= ~(F_UDISABLED|F_RESET);
	t->flags |= F_CRESET|F_ROOTINTR;
	FOREACH_AP(q, t, spipe) {
		spipe->pflags &= ~PF_GONE;
		spipe->pipe.up_aborting = 0;
	}
	DLOG(D_MSG, "RESET done flags %#jx", t->flags, 0,0,0);
}


#ifdef SLHCI_DEBUG
static int
slhci_memtest(struct slhci_softc *sc)
{
	enum { ASC, DESC, EITHER = ASC };	/* direction */
	enum { READ, WRITE };			/* operation */
	const char *ptr, *elem;
	size_t i;
	const int low = SL11_BUFFER_START, high = SL11_BUFFER_END;
	int addr = 0, dir = ASC, op = READ;
	/* Extended March C- test algorithm (SOFs also) */
	const char test[] = "E(w0) A(r0w1r1) A(r1w0r0) D(r0w1) D(r1w0) E(r0)";
	char c;
	const uint8_t dbs[] = { 0x00, 0x0f, 0x33, 0x55 }; /* data backgrounds */
	uint8_t db;

	/* Perform memory test for all data backgrounds. */
	for (i = 0; i < __arraycount(dbs); i++) {
		ptr = test;
		elem = ptr;
		/* Walk test algorithm string. */
		while ((c = *ptr++) != '\0')
			switch (tolower((int)c)) {
			case 'a':
				/* Address sequence is in ascending order. */
				dir = ASC;
				break;
			case 'd':
				/* Address sequence is in descending order. */
				dir = DESC;
				break;
			case 'e':
				/* Address sequence is in either order. */
				dir = EITHER;
				break;
			case '(':
				/* Start of test element (sequence). */
				elem = ptr;
				addr = (dir == ASC) ? low : high;
				break;
			case 'r':
				/* read operation */
				op = READ;
				break;
			case 'w':
				/* write operation */
				op = WRITE;
				break;
			case '0':
			case '1':
				/*
				 * Execute previously set-up operation by
				 * reading/writing non-inverted ('0') or
				 * inverted ('1') data background.
				 */
				db = (c - '0') ? ~dbs[i] : dbs[i];
				if (op == READ) {
					if (slhci_read(sc, addr) != db)
						return -1;
				} else
					slhci_write(sc, addr, db);
				break;
			case ')':
				/*
				 * End of element: Repeat same element with next
				 * address or continue to next element.
				 */
				addr = (dir == ASC) ? addr + 1 : addr - 1;
				if (addr >= low && addr <= high)
					ptr = elem;
				break;
			default:
				/* Do nothing. */
				break;
			}
	}

	return 0;
}
#endif

/* returns 1 if succeeded, 0 if failed, reserve == 0 is unreserve */
static int
slhci_reserve_bustime(struct slhci_softc *sc, struct slhci_pipe *spipe, int
    reserve)
{
	SLHCIHIST_FUNC(); SLHCIHIST_CALLED();
	struct slhci_transfers *t;
	int bustime, max_packet;

	KASSERT(mutex_owned(&sc->sc_intr_lock));

	t = &sc->sc_transfers;
	max_packet = UGETW(spipe->pipe.up_endpoint->ue_edesc->wMaxPacketSize);

	if (spipe->pflags & PF_LS)
		bustime = SLHCI_LS_CONST + SLHCI_LS_DATA_TIME(max_packet);
	else
		bustime = SLHCI_FS_CONST + SLHCI_FS_DATA_TIME(max_packet);

	if (!reserve) {
		t->reserved_bustime -= bustime;
#ifdef DIAGNOSTIC
		if (t->reserved_bustime < 0) {
			printf("%s: reserved_bustime %d < 0!\n",
			    SC_NAME(sc), t->reserved_bustime);
			DDOLOG("reserved_bustime %d < 0!",
			    t->reserved_bustime, 0, 0, 0);
			t->reserved_bustime = 0;
		}
#endif
		return 1;
	}

	if (t->reserved_bustime + bustime > SLHCI_RESERVED_BUSTIME) {
		if (ratecheck(&sc->sc_reserved_warn_rate,
		    &reserved_warn_rate))
#ifdef SLHCI_NO_OVERTIME
		{
			printf("%s: Max reserved bus time exceeded! "
			    "Erroring request.\n", SC_NAME(sc));
			DDOLOG("%s: Max reserved bus time exceeded! "
			    "Erroring request.", 0, 0, 0, 0);
		}
		return 0;
#else
		{
			printf("%s: Reserved bus time exceeds %d!\n",
			    SC_NAME(sc), SLHCI_RESERVED_BUSTIME);
			DDOLOG("Reserved bus time exceeds %d!",
			    SLHCI_RESERVED_BUSTIME, 0, 0, 0);
		}
#endif
	}

	t->reserved_bustime += bustime;
	return 1;
}

/* Device insertion/removal interrupt */
static void
slhci_insert(struct slhci_softc *sc)
{
	SLHCIHIST_FUNC(); SLHCIHIST_CALLED();
	struct slhci_transfers *t;

	t = &sc->sc_transfers;

	KASSERT(mutex_owned(&sc->sc_intr_lock));

	if (t->flags & F_NODEV)
		slhci_intrchange(sc, 0);
	else {
		slhci_drain(sc);
		slhci_intrchange(sc, SL11_IER_INSERT);
	}
	t->flags ^= F_NODEV;
	t->flags |= F_ROOTINTR|F_CCONNECT;
	DLOG(D_MSG, "INSERT intr: flags after %#jx", t->flags, 0,0,0);
}

/*
 * Data structures and routines to emulate the root hub.
 */

static usbd_status
slhci_clear_feature(struct slhci_softc *sc, unsigned int what)
{
	SLHCIHIST_FUNC(); SLHCIHIST_CALLED();
	struct slhci_transfers *t;
	usbd_status error;

	t = &sc->sc_transfers;
	error = USBD_NORMAL_COMPLETION;

	KASSERT(mutex_owned(&sc->sc_intr_lock));

	if (what == UHF_PORT_POWER) {
		DLOG(D_MSG, "POWER_OFF", 0,0,0,0);
		t->flags &= ~F_POWER;
		if (!(t->flags & F_NODEV))
			t->flags |= F_NODEV|F_CCONNECT|F_ROOTINTR;
		/* for x68k Nereid USB controller */
		if (sc->sc_enable_power && (t->flags & F_REALPOWER)) {
			t->flags &= ~F_REALPOWER;
			sc->sc_enable_power(sc, POWER_OFF);
		}
		slhci_intrchange(sc, 0);
		slhci_drain(sc);
	} else if (what == UHF_C_PORT_CONNECTION) {
		t->flags &= ~F_CCONNECT;
	} else if (what == UHF_C_PORT_RESET) {
		t->flags &= ~F_CRESET;
	} else if (what == UHF_PORT_ENABLE) {
		slhci_drain(sc);
	} else if (what != UHF_PORT_SUSPEND) {
		DDOLOG("ClrPortFeatERR:value=%#.4x", what, 0,0,0);
		error = USBD_IOERROR;
	}

	return error;
}

static usbd_status
slhci_set_feature(struct slhci_softc *sc, unsigned int what)
{
	SLHCIHIST_FUNC(); SLHCIHIST_CALLED();
	struct slhci_transfers *t;
	uint8_t r;

	t = &sc->sc_transfers;

	KASSERT(mutex_owned(&sc->sc_intr_lock));

	if (what == UHF_PORT_RESET) {
		if (!(t->flags & F_ACTIVE)) {
			DDOLOG("SET PORT_RESET when not ACTIVE!",
			    0,0,0,0);
			return USBD_INVAL;
		}
		if (!(t->flags & F_POWER)) {
			DDOLOG("SET PORT_RESET without PORT_POWER! flags %p",
			    t->flags, 0,0,0);
			return USBD_INVAL;
		}
		if (t->flags & F_RESET)
			return USBD_NORMAL_COMPLETION;
		DLOG(D_MSG, "RESET flags %#jx", t->flags, 0,0,0);
		slhci_intrchange(sc, 0);
		slhci_drain(sc);
		slhci_write(sc, SL11_CTRL, SL11_CTRL_RESETENGINE);
		/* usb spec says delay >= 10ms, app note 50ms */
 		start_cc_time(&t_delay, 50000);
		if (sc->sc_bus.ub_usepolling) {
			DELAY(50000);
			slhci_reset(sc);
		} else {
			t->flags |= F_RESET;
			callout_schedule(&sc->sc_timer, uimax(mstohz(50), 2));
		}
	} else if (what == UHF_PORT_SUSPEND) {
		printf("%s: USB Suspend not implemented!\n", SC_NAME(sc));
		DDOLOG("USB Suspend not implemented!", 0, 0, 0, 0);
	} else if (what == UHF_PORT_POWER) {
		DLOG(D_MSG, "PORT_POWER", 0,0,0,0);
		/* for x68k Nereid USB controller */
		if (!(t->flags & F_ACTIVE))
			return USBD_INVAL;
		if (t->flags & F_POWER)
			return USBD_NORMAL_COMPLETION;
		if (!(t->flags & F_REALPOWER)) {
			if (sc->sc_enable_power)
				sc->sc_enable_power(sc, POWER_ON);
			t->flags |= F_REALPOWER;
		}
		t->flags |= F_POWER;
		r = slhci_read(sc, SL11_ISR);
		if (r & SL11_ISR_INSERT)
			slhci_write(sc, SL11_ISR, SL11_ISR_INSERT);
		if (r & SL11_ISR_NODEV) {
			slhci_intrchange(sc, SL11_IER_INSERT);
			t->flags |= F_NODEV;
		} else {
			t->flags &= ~F_NODEV;
			t->flags |= F_CCONNECT|F_ROOTINTR;
		}
	} else {
		DDOLOG("SetPortFeatERR=%#.8x", what, 0,0,0);
		return USBD_IOERROR;
	}

	return USBD_NORMAL_COMPLETION;
}

static void
slhci_get_status(struct slhci_softc *sc, usb_port_status_t *ps)
{
	SLHCIHIST_FUNC(); SLHCIHIST_CALLED();
	struct slhci_transfers *t;
	unsigned int status, change;

	t = &sc->sc_transfers;

	KASSERT(mutex_owned(&sc->sc_intr_lock));

	/*
	 * We do not have a way to detect over current or babble and
	 * suspend is currently not implemented, so connect and reset
	 * are the only changes that need to be reported.
	 */
	change = 0;
	if (t->flags & F_CCONNECT)
		change |= UPS_C_CONNECT_STATUS;
	if (t->flags & F_CRESET)
		change |= UPS_C_PORT_RESET;

	status = 0;
	if (!(t->flags & F_NODEV))
		status |= UPS_CURRENT_CONNECT_STATUS;
	if (!(t->flags & F_UDISABLED))
		status |= UPS_PORT_ENABLED;
	if (t->flags & F_RESET)
		status |= UPS_RESET;
	if (t->flags & F_POWER)
		status |= UPS_PORT_POWER;
	if (t->flags & F_LOWSPEED)
		status |= UPS_LOW_SPEED;
	USETW(ps->wPortStatus, status);
	USETW(ps->wPortChange, change);
	DLOG(D_ROOT, "status=%#.4jx, change=%#.4jx", status, change, 0,0);
}

static int
slhci_roothub_ctrl(struct usbd_bus *bus, usb_device_request_t *req,
    void *buf, int buflen)
{
	SLHCIHIST_FUNC(); SLHCIHIST_CALLED();
	struct slhci_softc *sc = SLHCI_BUS2SC(bus);
	struct slhci_transfers *t = &sc->sc_transfers;
	usbd_status error = USBD_IOERROR; /* XXX should be STALL */
	uint16_t len, value, index;
	uint8_t type;
	int actlen = 0;

	len = UGETW(req->wLength);
	value = UGETW(req->wValue);
	index = UGETW(req->wIndex);

	type = req->bmRequestType;

	SLHCI_DEXEC(D_TRACE, slhci_log_req(req));

	/*
	 * USB requests for hubs have two basic types, standard and class.
	 * Each could potentially have recipients of device, interface,
	 * endpoint, or other.  For the hub class, CLASS_OTHER means the port
	 * and CLASS_DEVICE means the hub.  For standard requests, OTHER
	 * is not used.  Standard request are described in section 9.4 of the
	 * standard, hub class requests in 11.16.  Each request is either read
	 * or write.
	 *
	 * Clear Feature, Set Feature, and Status are defined for each of the
	 * used recipients.  Get Descriptor and Set Descriptor are defined for
	 * both standard and hub class types with different descriptors.
	 * Other requests have only one defined recipient and type.  These
	 * include: Get/Set Address, Get/Set Configuration, Get/Set Interface,
	 * and Synch Frame for standard requests and Get Bus State for hub
	 * class.
	 *
	 * When a device is first powered up it has address 0 until the
	 * address is set.
	 *
	 * Hubs are only allowed to support one interface and may not have
	 * isochronous endpoints.  The results of the related requests are
	 * undefined.
	 *
	 * The standard requires invalid or unsupported requests to return
	 * STALL in the data stage, however this does not work well with
	 * current error handling. XXX
	 *
	 * Some unsupported fields:
	 * Clear Hub Feature is for C_HUB_LOCAL_POWER and C_HUB_OVER_CURRENT
	 * Set Device Features is for ENDPOINT_HALT and DEVICE_REMOTE_WAKEUP
	 * Get Bus State is optional sample of D- and D+ at EOF2
	 */

	switch (req->bRequest) {
	/* Write Requests */
	case UR_CLEAR_FEATURE:
		if (type == UT_WRITE_CLASS_OTHER) {
			if (index == 1 /* Port */) {
				mutex_enter(&sc->sc_intr_lock);
				error = slhci_clear_feature(sc, value);
				mutex_exit(&sc->sc_intr_lock);
			} else
				DLOG(D_ROOT, "Clear Port Feature "
				    "index = %#.4jx", index, 0,0,0);
		}
		break;
	case UR_SET_FEATURE:
		if (type == UT_WRITE_CLASS_OTHER) {
			if (index == 1 /* Port */) {
				mutex_enter(&sc->sc_intr_lock);
				error = slhci_set_feature(sc, value);
				mutex_exit(&sc->sc_intr_lock);
			} else
				DLOG(D_ROOT, "Set Port Feature "
				    "index = %#.4jx", index, 0,0,0);
		} else if (type != UT_WRITE_CLASS_DEVICE)
			DLOG(D_ROOT, "Set Device Feature "
			    "ENDPOINT_HALT or DEVICE_REMOTE_WAKEUP "
			    "not supported", 0,0,0,0);
		break;

	/* Read Requests */
	case UR_GET_STATUS:
		if (type == UT_READ_CLASS_OTHER) {
			if (index == 1 /* Port */ && len == /* XXX >=? */
			    sizeof(usb_port_status_t)) {
				mutex_enter(&sc->sc_intr_lock);
				slhci_get_status(sc, (usb_port_status_t *)
				    buf);
				mutex_exit(&sc->sc_intr_lock);
				actlen = sizeof(usb_port_status_t);
				error = USBD_NORMAL_COMPLETION;
			} else
				DLOG(D_ROOT, "Get Port Status index = %#.4jx "
				    "len = %#.4jx", index, len, 0,0);
		} else if (type == UT_READ_CLASS_DEVICE) { /* XXX index? */
			if (len == sizeof(usb_hub_status_t)) {
				DLOG(D_ROOT, "Get Hub Status",
				    0,0,0,0);
				actlen = sizeof(usb_hub_status_t);
				memset(buf, 0, actlen);
				error = USBD_NORMAL_COMPLETION;
			} else
				DLOG(D_ROOT, "Get Hub Status bad len %#.4jx",
				    len, 0,0,0);
		}
		break;
	case UR_GET_DESCRIPTOR:
		if (type == UT_READ_DEVICE) {
			/* value is type (&0xff00) and index (0xff) */
			if (value == (UDESC_DEVICE<<8)) {
				actlen = buflen;
				error = USBD_NORMAL_COMPLETION;
			} else if (value == (UDESC_CONFIG<<8)) {
				struct usb_roothub_descriptors confd;

				actlen = uimin(buflen, sizeof(confd));
				memcpy(&confd, buf, actlen);

				/* 2 mA units */
				confd.urh_confd.bMaxPower = t->max_current;
				memcpy(buf, &confd, actlen);
				error = USBD_NORMAL_COMPLETION;
			} else if (value == ((UDESC_STRING<<8)|1)) {
				/* Vendor */
				actlen = buflen;
				error = USBD_NORMAL_COMPLETION;
			} else if (value == ((UDESC_STRING<<8)|2)) {
				/* Product */
				actlen = usb_makestrdesc((usb_string_descriptor_t *)
				    buf, len, "SL811HS/T root hub");
				error = USBD_NORMAL_COMPLETION;
			} else
				DDOLOG("Unknown Get Descriptor %#.4x",
				    value, 0,0,0);
		} else if (type == UT_READ_CLASS_DEVICE) {
			/* Descriptor number is 0 */
			if (value == (UDESC_HUB<<8)) {
				usb_hub_descriptor_t hubd;

				actlen = uimin(buflen, sizeof(hubd));
				memcpy(&hubd, buf, actlen);
				hubd.bHubContrCurrent =
				    500 - t->max_current;
				memcpy(buf, &hubd, actlen);
				error = USBD_NORMAL_COMPLETION;
			} else
				DDOLOG("Unknown Get Hub Descriptor %#.4x",
				    value, 0,0,0);
		}
		break;
	default:
		/* default from usbroothub */
		return buflen;
	}

	if (error == USBD_NORMAL_COMPLETION)
		return actlen;

	return -1;
}

/* End in lock functions. Start debug functions. */

#ifdef SLHCI_DEBUG
void
slhci_log_buffer(struct usbd_xfer *xfer)
{
	SLHCIHIST_FUNC(); SLHCIHIST_CALLED();
	u_char *buf;

	if(xfer->ux_length > 0 &&
	    UE_GET_DIR(xfer->ux_pipe->up_endpoint->ue_edesc->bEndpointAddress) ==
	    UE_DIR_IN) {
		buf = xfer->ux_buf;
		DDOLOGBUF(buf, xfer->ux_actlen);
		DDOLOG("len %d actlen %d short %d", xfer->ux_length,
		    xfer->ux_actlen, xfer->ux_length - xfer->ux_actlen, 0);
	}
}

void
slhci_log_req(usb_device_request_t *r)
{
	SLHCIHIST_FUNC(); SLHCIHIST_CALLED();
	int req, type, value, index, len;

	req   = r->bRequest;
	type  = r->bmRequestType;
	value = UGETW(r->wValue);
	index = UGETW(r->wIndex);
	len   = UGETW(r->wLength);

	DDOLOG("request: type %#x", type, 0, 0, 0);
	DDOLOG("request: r=%d,v=%d,i=%d,l=%d ", req, value, index, len);
}

void
slhci_log_dumpreg(void)
{
	SLHCIHIST_FUNC(); SLHCIHIST_CALLED();
	uint8_t r;
	unsigned int aaddr, alen, baddr, blen;
	static u_char buf[240];

	r = slhci_read(ssc, SL11_E0CTRL);
	DDOLOG("USB A Host Control = %#.2x", r, 0, 0, 0);
	DDOLOGEPCTRL(r);

	aaddr = slhci_read(ssc, SL11_E0ADDR);
	DDOLOG("USB A Base Address = %u", aaddr, 0,0,0);
	alen = slhci_read(ssc, SL11_E0LEN);
	DDOLOG("USB A Length = %u", alen, 0,0,0);
	r = slhci_read(ssc, SL11_E0STAT);
	DDOLOG("USB A Status = %#.2x", r, 0,0,0);
	DDOLOGEPSTAT(r);

	r = slhci_read(ssc, SL11_E0CONT);
	DDOLOG("USB A Remaining or Overflow Length = %u", r, 0,0,0);
	r = slhci_read(ssc, SL11_E1CTRL);
	DDOLOG("USB B Host Control = %#.2x", r, 0,0,0);
	DDOLOGEPCTRL(r);

	baddr = slhci_read(ssc, SL11_E1ADDR);
	DDOLOG("USB B Base Address = %u", baddr, 0,0,0);
	blen = slhci_read(ssc, SL11_E1LEN);
	DDOLOG("USB B Length = %u", blen, 0,0,0);
	r = slhci_read(ssc, SL11_E1STAT);
	DDOLOG("USB B Status = %#.2x", r, 0,0,0);
	DDOLOGEPSTAT(r);

	r = slhci_read(ssc, SL11_E1CONT);
	DDOLOG("USB B Remaining or Overflow Length = %u", r, 0,0,0);

	r = slhci_read(ssc, SL11_CTRL);
	DDOLOG("Control = %#.2x", r, 0,0,0);
	DDOLOGCTRL(r);

	r = slhci_read(ssc, SL11_IER);
	DDOLOG("Interrupt Enable = %#.2x", r, 0,0,0);
	DDOLOGIER(r);

	r = slhci_read(ssc, SL11_ISR);
	DDOLOG("Interrupt Status = %#.2x", r, 0,0,0);
	DDOLOGISR(r);

	r = slhci_read(ssc, SL11_REV);
	DDOLOG("Revision = %#.2x", r, 0,0,0);
	r = slhci_read(ssc, SL811_CSOF);
	DDOLOG("SOF Counter = %#.2x", r, 0,0,0);

	if (alen && aaddr >= SL11_BUFFER_START && aaddr < SL11_BUFFER_END &&
	    alen <= SL11_MAX_PACKET_SIZE && aaddr + alen <= SL11_BUFFER_END) {
		slhci_read_multi(ssc, aaddr, buf, alen);
		DDOLOG("USBA Buffer: start %u len %u", aaddr, alen, 0,0);
		DDOLOGBUF(buf, alen);
	} else if (alen)
		DDOLOG("USBA Buffer Invalid", 0,0,0,0);

	if (blen && baddr >= SL11_BUFFER_START && baddr < SL11_BUFFER_END &&
	    blen <= SL11_MAX_PACKET_SIZE && baddr + blen <= SL11_BUFFER_END) {
		slhci_read_multi(ssc, baddr, buf, blen);
		DDOLOG("USBB Buffer: start %u len %u", baddr, blen, 0,0);
		DDOLOGBUF(buf, blen);
	} else if (blen)
		DDOLOG("USBB Buffer Invalid", 0,0,0,0);
}

void
slhci_log_xfer(struct usbd_xfer *xfer)
{
	SLHCIHIST_FUNC(); SLHCIHIST_CALLED();
	DDOLOG("xfer: length=%u, actlen=%u, flags=%#x, timeout=%u,",
		xfer->ux_length, xfer->ux_actlen, xfer->ux_flags, xfer->ux_timeout);
	DDOLOG("buffer=%p", xfer->ux_buf, 0,0,0);
	slhci_log_req(&xfer->ux_request);
}

void
slhci_log_spipe(struct slhci_pipe *spipe)
{
	SLHCIHIST_FUNC(); SLHCIHIST_CALLED();
	DDOLOG("spipe %p onlists: AP=%d TO=%d XQ=%d", spipe,
	    gcq_onlist(&spipe->ap) ? 1 : 0,
	    gcq_onlist(&spipe->to) ? 1 : 0,
	    gcq_onlist(&spipe->xq) ? 1 : 0);
	DDOLOG("spipe: xfer %p buffer %p pflags %#x ptype %d",
	    spipe->xfer, spipe->buffer, spipe->pflags, spipe->ptype);
}

void
slhci_print_intr(void)
{
	unsigned int ier, isr;
	ier = slhci_read(ssc, SL11_IER);
	isr = slhci_read(ssc, SL11_ISR);
	printf("IER: %#x ISR: %#x \n", ier, isr);
}

#if 0
void
slhci_log_sc(void)
{
	SLHCIHIST_FUNC(); SLHCIHIST_CALLED();

	struct slhci_transfers *t;
	int i;

	t = &ssc->sc_transfers;

	DDOLOG("Flags=%#x", t->flags, 0,0,0);
	DDOLOG("a = %p Alen=%d b = %p Blen=%d", t->spipe[0], t->len[0],
	    t->spipe[1], t->len[1]);

	for (i = 0; i <= Q_MAX; i++)
		DDOLOG("Q %d: %p", i, gcq_hq(&t->q[i]), 0,0);

	DDOLOG("TIMED: %p", GCQ_ITEM(gcq_hq(&t->to),
	    struct slhci_pipe, to), 0,0,0);

	DDOLOG("frame=%d rootintr=%p", t->frame, t->rootintr, 0,0);

	DDOLOG("ub_usepolling=%d", ssc->sc_bus.ub_usepolling, 0, 0, 0);
}

void
slhci_log_slreq(struct slhci_pipe *r)
{
	SLHCIHIST_FUNC(); SLHCIHIST_CALLED();
	DDOLOG("xfer: %p", r->xfer, 0,0,0);
	DDOLOG("buffer: %p", r->buffer, 0,0,0);
	DDOLOG("bustime: %u", r->bustime, 0,0,0);
	DDOLOG("control: %#x", r->control, 0,0,0);
	DDOLOGEPCTRL(r->control);

	DDOLOG("pid: %#x", r->tregs[PID], 0,0,0);
	DDOLOG("dev: %u", r->tregs[DEV], 0,0,0);
	DDOLOG("len: %u", r->tregs[LEN], 0,0,0);

	if (r->xfer)
		slhci_log_xfer(r->xfer);
}
#endif
#endif /* SLHCI_DEBUG */
/* End debug functions. */