Training courses

Kernel and Embedded Linux

Bootlin training courses

Embedded Linux, kernel,
Yocto Project, Buildroot, real-time,
graphics, boot time, debugging...

Bootlin logo

Elixir Cross Referencer

   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
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
/*	$NetBSD: ohci.c,v 1.323 2022/03/09 22:18:54 riastradh Exp $	*/

/*
 * Copyright (c) 1998, 2004, 2005, 2012, 2016, 2020 The NetBSD Foundation, Inc.
 * All rights reserved.
 *
 * This code is derived from software contributed to The NetBSD Foundation
 * by Lennart Augustsson (lennart@augustsson.net) at
 * Carlstedt Research & Technology, Jared D. McNeill (jmcneill@invisible.ca),
 * Matthew R. Green (mrg@eterna.com.au), and Nick Hudson.
 *
 * This code is derived from software contributed to The NetBSD Foundation
 * by Charles M. Hannum.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

/*
 * USB Open Host Controller driver.
 *
 * OHCI spec: http://www.compaq.com/productinfo/development/openhci.html
 * USB spec: http://www.usb.org/developers/docs/
 */

#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: ohci.c,v 1.323 2022/03/09 22:18:54 riastradh Exp $");

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

#include <sys/param.h>

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

#include <machine/endian.h>

#include <dev/usb/usb.h>
#include <dev/usb/usbdi.h>
#include <dev/usb/usbdivar.h>
#include <dev/usb/usb_mem.h>
#include <dev/usb/usb_quirks.h>

#include <dev/usb/ohcireg.h>
#include <dev/usb/ohcivar.h>
#include <dev/usb/usbroothub.h>
#include <dev/usb/usbhist.h>

#ifdef USB_DEBUG
#ifndef OHCI_DEBUG
#define ohcidebug 0
#else
static int ohcidebug = 0;

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

	err = sysctl_createv(clog, 0, NULL, &rnode,
	    CTLFLAG_PERMANENT, CTLTYPE_NODE, "ohci",
	    SYSCTL_DESCR("ohci 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, &ohcidebug, sizeof(ohcidebug), CTL_CREATE, CTL_EOL);
	if (err)
		goto fail;

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

#endif /* OHCI_DEBUG */
#endif /* USB_DEBUG */

#define	DPRINTF(FMT,A,B,C,D)	USBHIST_LOG(ohcidebug,FMT,A,B,C,D)
#define	DPRINTFN(N,FMT,A,B,C,D)	USBHIST_LOGN(ohcidebug,N,FMT,A,B,C,D)
#define	OHCIHIST_FUNC()		USBHIST_FUNC()
#define	OHCIHIST_CALLED(name)	USBHIST_CALLED(ohcidebug)

#if BYTE_ORDER == BIG_ENDIAN
#define	SWAP_ENDIAN	OHCI_LITTLE_ENDIAN
#else
#define	SWAP_ENDIAN	OHCI_BIG_ENDIAN
#endif

#define	O16TOH(val)	(sc->sc_endian == SWAP_ENDIAN ? bswap16(val) : val)
#define	O32TOH(val)	(sc->sc_endian == SWAP_ENDIAN ? bswap32(val) : val)
#define	HTOO16(val)	O16TOH(val)
#define	HTOO32(val)	O32TOH(val)

struct ohci_pipe;

Static ohci_soft_ed_t  *ohci_alloc_sed(ohci_softc_t *);
Static void		ohci_free_sed(ohci_softc_t *, ohci_soft_ed_t *);

Static ohci_soft_td_t  *ohci_alloc_std(ohci_softc_t *);
Static void		ohci_free_std(ohci_softc_t *, ohci_soft_td_t *);
Static void		ohci_free_std_locked(ohci_softc_t *, ohci_soft_td_t *);

Static ohci_soft_itd_t *ohci_alloc_sitd(ohci_softc_t *);
Static void		ohci_free_sitd(ohci_softc_t *,ohci_soft_itd_t *);
Static void		ohci_free_sitd_locked(ohci_softc_t *,
			    ohci_soft_itd_t *);

Static int		ohci_alloc_std_chain(ohci_softc_t *, struct usbd_xfer *,
			    int, int);
Static void		ohci_free_stds(ohci_softc_t *, struct ohci_xfer *);

Static void		ohci_reset_std_chain(ohci_softc_t *, struct usbd_xfer *,
			    int, int, ohci_soft_td_t *, ohci_soft_td_t **);

Static usbd_status	ohci_open(struct usbd_pipe *);
Static void		ohci_poll(struct usbd_bus *);
Static void		ohci_softintr(void *);
Static void		ohci_rhsc(ohci_softc_t *, struct usbd_xfer *);
Static void		ohci_rhsc_softint(void *);

Static void		ohci_add_ed(ohci_softc_t *, ohci_soft_ed_t *,
			    ohci_soft_ed_t *);

Static void		ohci_rem_ed(ohci_softc_t *, ohci_soft_ed_t *,
				    ohci_soft_ed_t *);
Static void		ohci_hash_add_td(ohci_softc_t *, ohci_soft_td_t *);
Static void		ohci_hash_rem_td(ohci_softc_t *, ohci_soft_td_t *);
Static ohci_soft_td_t  *ohci_hash_find_td(ohci_softc_t *, ohci_physaddr_t);
Static void		ohci_hash_add_itd(ohci_softc_t *, ohci_soft_itd_t *);
Static void		ohci_hash_rem_itd(ohci_softc_t *, ohci_soft_itd_t *);
Static ohci_soft_itd_t  *ohci_hash_find_itd(ohci_softc_t *, ohci_physaddr_t);

Static usbd_status	ohci_setup_isoc(struct usbd_pipe *);
Static void		ohci_device_isoc_enter(struct usbd_xfer *);

Static struct usbd_xfer *
			ohci_allocx(struct usbd_bus *, unsigned int);
Static void		ohci_freex(struct usbd_bus *, struct usbd_xfer *);
Static bool		ohci_dying(struct usbd_bus *);
Static void		ohci_get_lock(struct usbd_bus *, kmutex_t **);
Static int		ohci_roothub_ctrl(struct usbd_bus *,
			    usb_device_request_t *, void *, int);

Static usbd_status	ohci_root_intr_transfer(struct usbd_xfer *);
Static usbd_status	ohci_root_intr_start(struct usbd_xfer *);
Static void		ohci_root_intr_abort(struct usbd_xfer *);
Static void		ohci_root_intr_close(struct usbd_pipe *);
Static void		ohci_root_intr_done(struct usbd_xfer *);

Static int		ohci_device_ctrl_init(struct usbd_xfer *);
Static void		ohci_device_ctrl_fini(struct usbd_xfer *);
Static usbd_status	ohci_device_ctrl_transfer(struct usbd_xfer *);
Static usbd_status	ohci_device_ctrl_start(struct usbd_xfer *);
Static void		ohci_device_ctrl_abort(struct usbd_xfer *);
Static void		ohci_device_ctrl_close(struct usbd_pipe *);
Static void		ohci_device_ctrl_done(struct usbd_xfer *);

Static int		ohci_device_bulk_init(struct usbd_xfer *);
Static void		ohci_device_bulk_fini(struct usbd_xfer *);
Static usbd_status	ohci_device_bulk_transfer(struct usbd_xfer *);
Static usbd_status	ohci_device_bulk_start(struct usbd_xfer *);
Static void		ohci_device_bulk_abort(struct usbd_xfer *);
Static void		ohci_device_bulk_close(struct usbd_pipe *);
Static void		ohci_device_bulk_done(struct usbd_xfer *);

Static int		ohci_device_intr_init(struct usbd_xfer *);
Static void		ohci_device_intr_fini(struct usbd_xfer *);
Static usbd_status	ohci_device_intr_transfer(struct usbd_xfer *);
Static usbd_status	ohci_device_intr_start(struct usbd_xfer *);
Static void		ohci_device_intr_abort(struct usbd_xfer *);
Static void		ohci_device_intr_close(struct usbd_pipe *);
Static void		ohci_device_intr_done(struct usbd_xfer *);

Static int		ohci_device_isoc_init(struct usbd_xfer *);
Static void		ohci_device_isoc_fini(struct usbd_xfer *);
Static usbd_status	ohci_device_isoc_transfer(struct usbd_xfer *);
Static void		ohci_device_isoc_abort(struct usbd_xfer *);
Static void		ohci_device_isoc_close(struct usbd_pipe *);
Static void		ohci_device_isoc_done(struct usbd_xfer *);

Static usbd_status	ohci_device_setintr(ohci_softc_t *,
			    struct ohci_pipe *, int);

Static void		ohci_rhsc_enable(void *);

Static void		ohci_close_pipe(struct usbd_pipe *, ohci_soft_ed_t *);
Static void		ohci_abortx(struct usbd_xfer *);

Static void		ohci_device_clear_toggle(struct usbd_pipe *);
Static void		ohci_noop(struct usbd_pipe *);

#ifdef OHCI_DEBUG
Static void		ohci_dumpregs(ohci_softc_t *);
Static void		ohci_dump_tds(ohci_softc_t *, ohci_soft_td_t *);
Static void		ohci_dump_td(ohci_softc_t *, ohci_soft_td_t *);
Static void		ohci_dump_ed(ohci_softc_t *, ohci_soft_ed_t *);
Static void		ohci_dump_itd(ohci_softc_t *, ohci_soft_itd_t *);
Static void		ohci_dump_itds(ohci_softc_t *, ohci_soft_itd_t *);
#endif

#define OBARR(sc) bus_space_barrier((sc)->iot, (sc)->ioh, 0, (sc)->sc_size, \
			BUS_SPACE_BARRIER_READ|BUS_SPACE_BARRIER_WRITE)
#define OWRITE1(sc, r, x) \
 do { OBARR(sc); bus_space_write_1((sc)->iot, (sc)->ioh, (r), (x)); } while (0)
#define OWRITE2(sc, r, x) \
 do { OBARR(sc); bus_space_write_2((sc)->iot, (sc)->ioh, (r), (x)); } while (0)
#define OWRITE4(sc, r, x) \
 do { OBARR(sc); bus_space_write_4((sc)->iot, (sc)->ioh, (r), (x)); } while (0)

static __inline uint32_t
OREAD4(ohci_softc_t *sc, bus_size_t r)
{

	OBARR(sc);
	return bus_space_read_4(sc->iot, sc->ioh, r);
}

/* Reverse the bits in a value 0 .. 31 */
Static uint8_t revbits[OHCI_NO_INTRS] =
  { 0x00, 0x10, 0x08, 0x18, 0x04, 0x14, 0x0c, 0x1c,
    0x02, 0x12, 0x0a, 0x1a, 0x06, 0x16, 0x0e, 0x1e,
    0x01, 0x11, 0x09, 0x19, 0x05, 0x15, 0x0d, 0x1d,
    0x03, 0x13, 0x0b, 0x1b, 0x07, 0x17, 0x0f, 0x1f };

struct ohci_pipe {
	struct usbd_pipe pipe;
	ohci_soft_ed_t *sed;
	union {
		ohci_soft_td_t *td;
		ohci_soft_itd_t *itd;
	} tail;
	/* Info needed for different pipe kinds. */
	union {
		/* Control pipe */
		struct {
			usb_dma_t reqdma;
		} ctrl;
		/* Interrupt pipe */
		struct {
			int nslots;
			int pos;
		} intr;
		/* Isochronous pipe */
		struct isoc {
			int next, inuse;
		} isoc;
	};
};

Static const struct usbd_bus_methods ohci_bus_methods = {
	.ubm_open =	ohci_open,
	.ubm_softint =	ohci_softintr,
	.ubm_dopoll =	ohci_poll,
	.ubm_allocx =	ohci_allocx,
	.ubm_freex =	ohci_freex,
	.ubm_abortx =	ohci_abortx,
	.ubm_dying =	ohci_dying,
	.ubm_getlock =	ohci_get_lock,
	.ubm_rhctrl =	ohci_roothub_ctrl,
};

Static const struct usbd_pipe_methods ohci_root_intr_methods = {
	.upm_transfer =	ohci_root_intr_transfer,
	.upm_start =	ohci_root_intr_start,
	.upm_abort =	ohci_root_intr_abort,
	.upm_close =	ohci_root_intr_close,
	.upm_cleartoggle =	ohci_noop,
	.upm_done =	ohci_root_intr_done,
};

Static const struct usbd_pipe_methods ohci_device_ctrl_methods = {
	.upm_init =	ohci_device_ctrl_init,
	.upm_fini =	ohci_device_ctrl_fini,
	.upm_transfer =	ohci_device_ctrl_transfer,
	.upm_start =	ohci_device_ctrl_start,
	.upm_abort =	ohci_device_ctrl_abort,
	.upm_close =	ohci_device_ctrl_close,
	.upm_cleartoggle =	ohci_noop,
	.upm_done =	ohci_device_ctrl_done,
};

Static const struct usbd_pipe_methods ohci_device_intr_methods = {
	.upm_init =	ohci_device_intr_init,
	.upm_fini =	ohci_device_intr_fini,
	.upm_transfer =	ohci_device_intr_transfer,
	.upm_start =	ohci_device_intr_start,
	.upm_abort =	ohci_device_intr_abort,
	.upm_close =	ohci_device_intr_close,
	.upm_cleartoggle =	ohci_device_clear_toggle,
	.upm_done =	ohci_device_intr_done,
};

Static const struct usbd_pipe_methods ohci_device_bulk_methods = {
	.upm_init =	ohci_device_bulk_init,
	.upm_fini =	ohci_device_bulk_fini,
	.upm_transfer =	ohci_device_bulk_transfer,
	.upm_start =	ohci_device_bulk_start,
	.upm_abort =	ohci_device_bulk_abort,
	.upm_close =	ohci_device_bulk_close,
	.upm_cleartoggle =	ohci_device_clear_toggle,
	.upm_done =	ohci_device_bulk_done,
};

Static const struct usbd_pipe_methods ohci_device_isoc_methods = {
	.upm_init =	ohci_device_isoc_init,
	.upm_fini =	ohci_device_isoc_fini,
	.upm_transfer =	ohci_device_isoc_transfer,
	.upm_abort =	ohci_device_isoc_abort,
	.upm_close =	ohci_device_isoc_close,
	.upm_cleartoggle =	ohci_noop,
	.upm_done =	ohci_device_isoc_done,
};

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

	switch (act) {
	case DVACT_DEACTIVATE:
		sc->sc_dying = 1;
		return 0;
	default:
		return EOPNOTSUPP;
	}
}

void
ohci_childdet(device_t self, device_t child)
{
	struct ohci_softc *sc = device_private(self);

	KASSERT(sc->sc_child == child);
	sc->sc_child = NULL;
}

int
ohci_detach(struct ohci_softc *sc, int flags)
{
	int rv = 0;

	if (sc->sc_child != NULL)
		rv = config_detach(sc->sc_child, flags);

	if (rv != 0)
		return rv;

	softint_disestablish(sc->sc_rhsc_si);

	callout_halt(&sc->sc_tmo_rhsc, NULL);
	callout_destroy(&sc->sc_tmo_rhsc);

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

	if (sc->sc_hcca != NULL)
		usb_freemem(&sc->sc_hccadma);
	pool_cache_destroy(sc->sc_xferpool);
	cv_destroy(&sc->sc_abort_cv);

	return rv;
}

ohci_soft_ed_t *
ohci_alloc_sed(ohci_softc_t *sc)
{
	ohci_soft_ed_t *sed;
	int i, offs;
	usb_dma_t dma;

	OHCIHIST_FUNC(); OHCIHIST_CALLED();

	mutex_enter(&sc->sc_lock);
	if (sc->sc_freeeds == NULL) {
		DPRINTFN(2, "allocating chunk", 0, 0, 0, 0);
		mutex_exit(&sc->sc_lock);

		int err = usb_allocmem(sc->sc_bus.ub_dmatag, OHCI_SED_SIZE * OHCI_SED_CHUNK,
		    OHCI_ED_ALIGN, 0 /*!USBMALLOC_COHERENT*/, &dma);
		if (err)
			return NULL;

		mutex_enter(&sc->sc_lock);
		for (i = 0; i < OHCI_SED_CHUNK; i++) {
			offs = i * OHCI_SED_SIZE;
			sed = KERNADDR(&dma, offs);
			sed->physaddr = DMAADDR(&dma, offs);
			sed->dma = dma;
			sed->offs = offs;
			sed->next = sc->sc_freeeds;
			sc->sc_freeeds = sed;
		}
	}
	sed = sc->sc_freeeds;
	sc->sc_freeeds = sed->next;
	mutex_exit(&sc->sc_lock);

	memset(&sed->ed, 0, sizeof(ohci_ed_t));
	sed->next = 0;
	return sed;
}

static inline void
ohci_free_sed_locked(ohci_softc_t *sc, ohci_soft_ed_t *sed)
{

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

	sed->next = sc->sc_freeeds;
	sc->sc_freeeds = sed;
}

void
ohci_free_sed(ohci_softc_t *sc, ohci_soft_ed_t *sed)
{

	mutex_enter(&sc->sc_lock);
	ohci_free_sed_locked(sc, sed);
	mutex_exit(&sc->sc_lock);
}

ohci_soft_td_t *
ohci_alloc_std(ohci_softc_t *sc)
{
	ohci_soft_td_t *std;
	int i, offs;
	usb_dma_t dma;

	OHCIHIST_FUNC(); OHCIHIST_CALLED();

	mutex_enter(&sc->sc_lock);
	if (sc->sc_freetds == NULL) {
		DPRINTFN(2, "allocating chunk", 0, 0, 0, 0);
		mutex_exit(&sc->sc_lock);

		int err = usb_allocmem(sc->sc_bus.ub_dmatag, OHCI_STD_SIZE * OHCI_STD_CHUNK,
		   OHCI_TD_ALIGN, USBMALLOC_COHERENT, &dma);
		if (err)
			return NULL;

		mutex_enter(&sc->sc_lock);
		for (i = 0; i < OHCI_STD_CHUNK; i++) {
			offs = i * OHCI_STD_SIZE;
			std = KERNADDR(&dma, offs);
			std->physaddr = DMAADDR(&dma, offs);
			std->dma = dma;
			std->offs = offs;
			std->nexttd = sc->sc_freetds;
			sc->sc_freetds = std;
		}
	}

	std = sc->sc_freetds;
	sc->sc_freetds = std->nexttd;
	mutex_exit(&sc->sc_lock);

	memset(&std->td, 0, sizeof(ohci_td_t));
	std->nexttd = NULL;
	std->xfer = NULL;
	std->held = NULL;

	return std;
}

void
ohci_free_std_locked(ohci_softc_t *sc, ohci_soft_td_t *std)
{

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

	std->nexttd = sc->sc_freetds;
	sc->sc_freetds = std;
}

void
ohci_free_std(ohci_softc_t *sc, ohci_soft_td_t *std)
{

	mutex_enter(&sc->sc_lock);
	ohci_free_std_locked(sc, std);
	mutex_exit(&sc->sc_lock);
}

Static int
ohci_alloc_std_chain(ohci_softc_t *sc, struct usbd_xfer *xfer, int length, int rd)
{
	struct ohci_xfer *ox = OHCI_XFER2OXFER(xfer);
	uint16_t flags = xfer->ux_flags;

	OHCIHIST_FUNC(); OHCIHIST_CALLED();

	DPRINTFN(8, "addr=%jd endpt=%jd len=%jd speed=%jd",
	    xfer->ux_pipe->up_dev->ud_addr,
	    UE_GET_ADDR(xfer->ux_pipe->up_endpoint->ue_edesc->bEndpointAddress),
	    length, xfer->ux_pipe->up_dev->ud_speed);

	ASSERT_SLEEPABLE();
	KASSERT(length != 0 || (!rd && (flags & USBD_FORCE_SHORT_XFER)));

	size_t nstd = (!rd && (flags & USBD_FORCE_SHORT_XFER)) ? 1 : 0;
	nstd += howmany(length, OHCI_PAGE_SIZE);
	ox->ox_stds = kmem_zalloc(sizeof(ohci_soft_td_t *) * nstd,
	    KM_SLEEP);
	ox->ox_nstd = nstd;

	DPRINTFN(8, "xfer %#jx nstd %jd", (uintptr_t)xfer, nstd, 0, 0);

	for (size_t j = 0; j < ox->ox_nstd; j++) {
		ohci_soft_td_t *cur = ohci_alloc_std(sc);
		if (cur == NULL)
			goto nomem;

		ox->ox_stds[j] = cur;
		cur->held = &ox->ox_stds[j];
		cur->xfer = xfer;
		cur->flags = 0;
		DPRINTFN(10, "xfer=%#jx new std=%#jx held at %#jx", (uintptr_t)ox,
		    (uintptr_t)cur, (uintptr_t)cur->held, 0);
	}

	return 0;

 nomem:
	ohci_free_stds(sc, ox);
	kmem_free(ox->ox_stds, sizeof(ohci_soft_td_t *) * nstd);

	return ENOMEM;
}

Static void
ohci_free_stds(ohci_softc_t *sc, struct ohci_xfer *ox)
{
	OHCIHIST_FUNC(); OHCIHIST_CALLED();
	DPRINTF("ox=%#jx", (uintptr_t)ox, 0, 0, 0);

	mutex_enter(&sc->sc_lock);
	for (size_t i = 0; i < ox->ox_nstd; i++) {
		ohci_soft_td_t *std = ox->ox_stds[i];
		if (std == NULL)
			break;
		ohci_free_std_locked(sc, std);
	}
	mutex_exit(&sc->sc_lock);
}

void
ohci_reset_std_chain(ohci_softc_t *sc, struct usbd_xfer *xfer,
    int alen, int rd, ohci_soft_td_t *sp, ohci_soft_td_t **ep)
{
	struct ohci_xfer *ox = OHCI_XFER2OXFER(xfer);
	ohci_soft_td_t *next, *cur;
	int len, curlen;
	usb_dma_t *dma = &xfer->ux_dmabuf;
	uint16_t flags = xfer->ux_flags;

	OHCIHIST_FUNC(); OHCIHIST_CALLED();
	DPRINTF("start len=%jd", alen, 0, 0, 0);

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

	DPRINTFN(8, "addr=%jd endpt=%jd len=%jd speed=%jd",
	    xfer->ux_pipe->up_dev->ud_addr,
	    UE_GET_ADDR(xfer->ux_pipe->up_endpoint->ue_edesc->bEndpointAddress),
	    alen, xfer->ux_pipe->up_dev->ud_speed);

	KASSERT(sp);

	int mps = UGETW(xfer->ux_pipe->up_endpoint->ue_edesc->wMaxPacketSize);

	/*
	 * Assign next for the len == 0 case where we don't go through the
	 * main loop.
	 */
	len = alen;
	cur = next = sp;

	usb_syncmem(dma, 0, len,
	    rd ? BUS_DMASYNC_PREREAD : BUS_DMASYNC_PREWRITE);
	const uint32_t tdflags = HTOO32(
	    OHCI_TD_SET_DP(rd ? OHCI_TD_DP_IN : OHCI_TD_DP_OUT) |
	    OHCI_TD_SET_CC(OHCI_TD_NOCC) |
	    OHCI_TD_SET_TOGGLE(OHCI_TD_TOGGLE_CARRY) |
	    OHCI_TD_SET_DI(OHCI_TD_NOINTR)
	    );

	size_t curoffs = 0;
	for (size_t j = 1; len != 0;) {
		if (j == ox->ox_nstd)
			next = NULL;
		else
			next = ox->ox_stds[j++];
		KASSERT(next != cur);

		curlen = len;
		/*
		 * The OHCI hardware can handle at most one page crossing per
		 * TD.  That is, 2 * OHCI_PAGE_SIZE as a maximum.  Limit the
		 * length in this TD accordingly.
		 */
		const ohci_physaddr_t sdataphys = DMAADDR(dma, curoffs);

		int maxlen = (2 * OHCI_PAGE_SIZE) - OHCI_PAGE_OFFSET(sdataphys);
		if (curlen > maxlen) {
			curlen = maxlen;

			/*
			 * the length must be a multiple of
			 * the max size
			 */
			curlen -= curlen % mps;
		}

		const int edataoffs = curoffs + curlen - 1;
		const ohci_physaddr_t edataphys = DMAADDR(dma, edataoffs);

		KASSERT(curlen != 0);
		DPRINTFN(4, "sdataphys=0x%08jx edataphys=0x%08jx "
		    "len=%jd curlen=%jd", sdataphys, edataphys, len, curlen);

		cur->td.td_flags = tdflags;
		cur->td.td_cbp = HTOO32(sdataphys);
		cur->td.td_be = HTOO32(edataphys);
		cur->td.td_nexttd = (next != NULL) ? HTOO32(next->physaddr) : 0;
		cur->nexttd = next;
		cur->len = curlen;
		cur->flags = OHCI_ADD_LEN;
		cur->xfer = xfer;
	 	ohci_hash_add_td(sc, cur);

		curoffs += curlen;
		len -= curlen;

		if (len != 0) {
			KASSERT(next != NULL);
			DPRINTFN(10, "extend chain", 0, 0, 0, 0);
			usb_syncmem(&cur->dma, cur->offs, sizeof(cur->td),
			    BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);

			cur = next;
		}
	}
	cur->td.td_flags |=
	    HTOO32(xfer->ux_flags & USBD_SHORT_XFER_OK ? OHCI_TD_R : 0);

	if (!rd &&
	    (flags & USBD_FORCE_SHORT_XFER) &&
	    alen % mps == 0) {
		/* We're adding a ZLP so sync the previous TD */
		usb_syncmem(&cur->dma, cur->offs, sizeof(cur->td),
		    BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);

		/* Force a 0 length transfer at the end. */

		KASSERT(next != NULL);
		cur = next;

		cur->td.td_flags = tdflags;
		cur->td.td_cbp = 0; /* indicate 0 length packet */
		cur->td.td_nexttd = 0;
		cur->td.td_be = ~0;
		cur->nexttd = NULL;
		cur->len = 0;
		cur->flags = 0;
		cur->xfer = xfer;
	 	ohci_hash_add_td(sc, cur);

		DPRINTFN(2, "add 0 xfer", 0, 0, 0, 0);
	}

	/* Last TD gets usb_syncmem'ed by caller */
	*ep = cur;
}

ohci_soft_itd_t *
ohci_alloc_sitd(ohci_softc_t *sc)
{
	ohci_soft_itd_t *sitd;
	int i, offs;
	usb_dma_t dma;

	OHCIHIST_FUNC(); OHCIHIST_CALLED();

	mutex_enter(&sc->sc_lock);
	if (sc->sc_freeitds == NULL) {
		DPRINTFN(2, "allocating chunk", 0, 0, 0, 0);
		mutex_exit(&sc->sc_lock);

		int err = usb_allocmem(sc->sc_bus.ub_dmatag, OHCI_SITD_SIZE * OHCI_SITD_CHUNK,
		    OHCI_ITD_ALIGN, USBMALLOC_COHERENT, &dma);
		if (err)
			return NULL;
		mutex_enter(&sc->sc_lock);
		for (i = 0; i < OHCI_SITD_CHUNK; i++) {
			offs = i * OHCI_SITD_SIZE;
			sitd = KERNADDR(&dma, offs);
			sitd->physaddr = DMAADDR(&dma, offs);
			sitd->dma = dma;
			sitd->offs = offs;
			sitd->nextitd = sc->sc_freeitds;
			sc->sc_freeitds = sitd;
		}
	}

	sitd = sc->sc_freeitds;
	sc->sc_freeitds = sitd->nextitd;
	mutex_exit(&sc->sc_lock);

	memset(&sitd->itd, 0, sizeof(ohci_itd_t));
	sitd->nextitd = NULL;
	sitd->xfer = NULL;

#ifdef DIAGNOSTIC
	sitd->isdone = true;
#endif

	return sitd;
}

Static void
ohci_free_sitd_locked(ohci_softc_t *sc, ohci_soft_itd_t *sitd)
{

	OHCIHIST_FUNC(); OHCIHIST_CALLED();
	DPRINTFN(10, "sitd=%#jx", (uintptr_t)sitd, 0, 0, 0);

	KASSERT(sitd->isdone);
#ifdef DIAGNOSTIC
	/* Warn double free */
	sitd->isdone = false;
#endif

	sitd->nextitd = sc->sc_freeitds;
	sc->sc_freeitds = sitd;
}

void
ohci_free_sitd(ohci_softc_t *sc, ohci_soft_itd_t *sitd)
{

	OHCIHIST_FUNC(); OHCIHIST_CALLED();

	mutex_enter(&sc->sc_lock);
	ohci_free_sitd_locked(sc, sitd);
	mutex_exit(&sc->sc_lock);
}

int
ohci_init(ohci_softc_t *sc)
{
	ohci_soft_ed_t *sed, *psed;
	usbd_status err;
	int i;
	uint32_t s, ctl, rwc, ival, hcr, fm, per, rev, desca /*, descb */;

	OHCIHIST_FUNC(); OHCIHIST_CALLED();

	aprint_normal_dev(sc->sc_dev, "");

	sc->sc_hcca = NULL;
	callout_init(&sc->sc_tmo_rhsc, CALLOUT_MPSAFE);

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

	sc->sc_rhsc_si = softint_establish(SOFTINT_USB | SOFTINT_MPSAFE,
	    ohci_rhsc_softint, sc);

	for (i = 0; i < OHCI_HASH_SIZE; i++)
		LIST_INIT(&sc->sc_hash_tds[i]);
	for (i = 0; i < OHCI_HASH_SIZE; i++)
		LIST_INIT(&sc->sc_hash_itds[i]);

	TAILQ_INIT(&sc->sc_abortingxfers);
	cv_init(&sc->sc_abort_cv, "ohciabt");

	sc->sc_xferpool = pool_cache_init(sizeof(struct ohci_xfer), 0, 0, 0,
	    "ohcixfer", NULL, IPL_USB, NULL, NULL, NULL);

	rev = OREAD4(sc, OHCI_REVISION);
	aprint_normal("OHCI version %" __PRIuBITS ".%" __PRIuBITS "%s\n",
	    OHCI_REV_HI(rev), OHCI_REV_LO(rev),
	    OHCI_REV_LEGACY(rev) ? ", legacy support" : "");

	if (OHCI_REV_HI(rev) != 1 || OHCI_REV_LO(rev) != 0) {
		aprint_error_dev(sc->sc_dev, "unsupported OHCI revision\n");
		sc->sc_bus.ub_revision = USBREV_UNKNOWN;
		return -1;
	}
	sc->sc_bus.ub_revision = USBREV_1_0;
	sc->sc_bus.ub_usedma = true;
	sc->sc_bus.ub_dmaflags = USBMALLOC_MULTISEG;

	/* XXX determine alignment by R/W */
	/* Allocate the HCCA area. */
	err = usb_allocmem(sc->sc_bus.ub_dmatag, OHCI_HCCA_SIZE,	OHCI_HCCA_ALIGN,
	    USBMALLOC_COHERENT, &sc->sc_hccadma);
	if (err) {
		sc->sc_hcca = NULL;
		return err;
	}
	sc->sc_hcca = KERNADDR(&sc->sc_hccadma, 0);
	memset(sc->sc_hcca, 0, OHCI_HCCA_SIZE);

	sc->sc_eintrs = OHCI_NORMAL_INTRS;

	/* Allocate dummy ED that starts the control list. */
	sc->sc_ctrl_head = ohci_alloc_sed(sc);
	if (sc->sc_ctrl_head == NULL) {
		err = ENOMEM;
		goto bad1;
	}
	sc->sc_ctrl_head->ed.ed_flags |= HTOO32(OHCI_ED_SKIP);

	/* Allocate dummy ED that starts the bulk list. */
	sc->sc_bulk_head = ohci_alloc_sed(sc);
	if (sc->sc_bulk_head == NULL) {
		err = ENOMEM;
		goto bad2;
	}
	sc->sc_bulk_head->ed.ed_flags |= HTOO32(OHCI_ED_SKIP);
	usb_syncmem(&sc->sc_bulk_head->dma, sc->sc_bulk_head->offs,
	    sizeof(sc->sc_bulk_head->ed),
	    BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);

	/* Allocate dummy ED that starts the isochronous list. */
	sc->sc_isoc_head = ohci_alloc_sed(sc);
	if (sc->sc_isoc_head == NULL) {
		err = ENOMEM;
		goto bad3;
	}
	sc->sc_isoc_head->ed.ed_flags |= HTOO32(OHCI_ED_SKIP);
	usb_syncmem(&sc->sc_isoc_head->dma, sc->sc_isoc_head->offs,
	    sizeof(sc->sc_isoc_head->ed),
	    BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);

	/* Allocate all the dummy EDs that make up the interrupt tree. */
	for (i = 0; i < OHCI_NO_EDS; i++) {
		sed = ohci_alloc_sed(sc);
		if (sed == NULL) {
			while (--i >= 0)
				ohci_free_sed(sc, sc->sc_eds[i]);
			err = ENOMEM;
			goto bad4;
		}
		/* All ED fields are set to 0. */
		sc->sc_eds[i] = sed;
		sed->ed.ed_flags |= HTOO32(OHCI_ED_SKIP);
		if (i != 0)
			psed = sc->sc_eds[(i-1) / 2];
		else
			psed= sc->sc_isoc_head;
		sed->next = psed;
		sed->ed.ed_nexted = HTOO32(psed->physaddr);
		usb_syncmem(&sed->dma, sed->offs, sizeof(sed->ed),
		    BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
	}
	/*
	 * Fill HCCA interrupt table.  The bit reversal is to get
	 * the tree set up properly to spread the interrupts.
	 */
	for (i = 0; i < OHCI_NO_INTRS; i++)
		sc->sc_hcca->hcca_interrupt_table[revbits[i]] =
		    HTOO32(sc->sc_eds[OHCI_NO_EDS-OHCI_NO_INTRS+i]->physaddr);
	usb_syncmem(&sc->sc_hccadma, 0, OHCI_HCCA_SIZE,
	    BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);

#ifdef OHCI_DEBUG
	DPRINTFN(15, "--- dump start ---", 0, 0, 0 ,0);
	if (ohcidebug >= 15) {
		for (i = 0; i < OHCI_NO_EDS; i++) {
			DPRINTFN(15, "ed#%jd ", i, 0, 0, 0);
			ohci_dump_ed(sc, sc->sc_eds[i]);
		}
		DPRINTFN(15, "iso", 0, 0, 0 ,0);
		ohci_dump_ed(sc, sc->sc_isoc_head);
	}
	DPRINTFN(15, "--- dump end ---", 0, 0, 0 ,0);
#endif

	/* Preserve values programmed by SMM/BIOS but lost over reset. */
	ctl = OREAD4(sc, OHCI_CONTROL);
	rwc = ctl & OHCI_RWC;
	fm = OREAD4(sc, OHCI_FM_INTERVAL);
	desca = OREAD4(sc, OHCI_RH_DESCRIPTOR_A);
	/* descb = OREAD4(sc, OHCI_RH_DESCRIPTOR_B); */

	/* Determine in what context we are running. */
	if (ctl & OHCI_IR) {
		/* SMM active, request change */
		DPRINTF("SMM active, request owner change", 0, 0, 0, 0);
		if ((sc->sc_intre & (OHCI_OC | OHCI_MIE)) ==
		    (OHCI_OC | OHCI_MIE))
			OWRITE4(sc, OHCI_INTERRUPT_ENABLE, OHCI_MIE);
		s = OREAD4(sc, OHCI_COMMAND_STATUS);
		OWRITE4(sc, OHCI_COMMAND_STATUS, s | OHCI_OCR);
		for (i = 0; i < 100 && (ctl & OHCI_IR); i++) {
			usb_delay_ms(&sc->sc_bus, 1);
			ctl = OREAD4(sc, OHCI_CONTROL);
		}
		OWRITE4(sc, OHCI_INTERRUPT_DISABLE, OHCI_MIE);
		if ((ctl & OHCI_IR) == 0) {
			aprint_error_dev(sc->sc_dev,
			    "SMM does not respond, resetting\n");
			OWRITE4(sc, OHCI_CONTROL,
			    OHCI_SET_HCFS(OHCI_HCFS_RESET) | rwc);
			goto reset;
		}
#if 0
	/*
	 * Don't bother trying to reuse the BIOS init, we'll reset it
	 * anyway.
	 */
	} else if (OHCI_GET_HCFS(ctl) != OHCI_HCFS_RESET) {
		/* BIOS started controller. */
		DPRINTF("BIOS active", 0, 0, 0, 0);
		if (OHCI_GET_HCFS(ctl) != OHCI_HCFS_OPERATIONAL) {
			OWRITE4(sc, OHCI_CONTROL,
			    OHCI_SET_HCFS(OHCI_HCFS_OPERATIONAL) | rwc);
			usb_delay_ms(&sc->sc_bus, USB_RESUME_DELAY);
		}
#endif
	} else {
		DPRINTF("cold started", 0 ,0 ,0 ,0);
	reset:
		/* Controller was cold started. */
		usb_delay_ms(&sc->sc_bus, USB_BUS_RESET_DELAY);
	}

	/*
	 * This reset should not be necessary according to the OHCI spec, but
	 * without it some controllers do not start.
	 */
	DPRINTF("sc %#jx: resetting", (uintptr_t)sc, 0, 0, 0);
	OWRITE4(sc, OHCI_CONTROL, OHCI_SET_HCFS(OHCI_HCFS_RESET) | rwc);
	usb_delay_ms(&sc->sc_bus, USB_BUS_RESET_DELAY);

	/* We now own the host controller and the bus has been reset. */

	OWRITE4(sc, OHCI_COMMAND_STATUS, OHCI_HCR); /* Reset HC */
	/* Nominal time for a reset is 10 us. */
	for (i = 0; i < 10; i++) {
		delay(10);
		hcr = OREAD4(sc, OHCI_COMMAND_STATUS) & OHCI_HCR;
		if (!hcr)
			break;
	}
	if (hcr) {
		aprint_error_dev(sc->sc_dev, "reset timeout\n");
		err = EIO;
		goto bad5;
	}
#ifdef OHCI_DEBUG
	if (ohcidebug >= 15)
		ohci_dumpregs(sc);
#endif

	/* The controller is now in SUSPEND state, we have 2ms to finish. */

	/* Set up HC registers. */
	OWRITE4(sc, OHCI_HCCA, DMAADDR(&sc->sc_hccadma, 0));
	OWRITE4(sc, OHCI_CONTROL_HEAD_ED, sc->sc_ctrl_head->physaddr);
	OWRITE4(sc, OHCI_BULK_HEAD_ED, sc->sc_bulk_head->physaddr);
	/* disable all interrupts and then switch on all desired interrupts */
	OWRITE4(sc, OHCI_INTERRUPT_DISABLE, OHCI_ALL_INTRS);
	/* switch on desired functional features */
	ctl = OREAD4(sc, OHCI_CONTROL);
	ctl &= ~(OHCI_CBSR_MASK | OHCI_LES | OHCI_HCFS_MASK | OHCI_IR);
	ctl |= OHCI_PLE | OHCI_IE | OHCI_CLE | OHCI_BLE |
		OHCI_CBSR_SET(OHCI_RATIO_1_4) |
		OHCI_SET_HCFS(OHCI_HCFS_OPERATIONAL) | rwc;
	/* And finally start it! */
	OWRITE4(sc, OHCI_CONTROL, ctl);

	/*
	 * The controller is now OPERATIONAL.  Set a some final
	 * registers that should be set earlier, but that the
	 * controller ignores when in the SUSPEND state.
	 */
	ival = OHCI_FM_GET_IVAL(fm);
	fm = (OREAD4(sc, OHCI_FM_INTERVAL) & OHCI_FM_FIT) ^ OHCI_FM_FIT;
	fm |= OHCI_FSMPS(ival) | ival;
	OWRITE4(sc, OHCI_FM_INTERVAL, fm);
	per = OHCI_PERIODIC(ival); /* 90% periodic */
	OWRITE4(sc, OHCI_PERIODIC_START, per);

	if (sc->sc_flags & OHCIF_SUPERIO) {
		/* no overcurrent protection */
		desca |= OHCI_RHD_NOCP;
		/*
		 * Clear NoPowerSwitching and PowerOnToPowerGoodTime meaning
		 * that
		 *  - ports are always power switched
		 *  - don't wait for powered root hub port
		 */
		desca &= ~(OHCI_RHD_POTPGT_MASK | OHCI_RHD_NPS);
	}

	/* Fiddle the No OverCurrent Protection bit to avoid chip bug. */
	OWRITE4(sc, OHCI_RH_DESCRIPTOR_A, desca | OHCI_RHD_NOCP);
	OWRITE4(sc, OHCI_RH_STATUS, OHCI_RHS_LPSC); /* Enable port power */
	usb_delay_ms(&sc->sc_bus, OHCI_ENABLE_POWER_DELAY);
	OWRITE4(sc, OHCI_RH_DESCRIPTOR_A, desca);

	/*
	 * The AMD756 requires a delay before re-reading the register,
	 * otherwise it will occasionally report 0 ports.
	 */
	sc->sc_noport = 0;
	for (i = 0; i < 10 && sc->sc_noport == 0; i++) {
		usb_delay_ms(&sc->sc_bus, OHCI_READ_DESC_DELAY);
		sc->sc_noport =
		    OHCI_RHD_GET_NDP(OREAD4(sc, OHCI_RH_DESCRIPTOR_A));
	}

#ifdef OHCI_DEBUG
	if (ohcidebug >= 5)
		ohci_dumpregs(sc);
#endif

	/* Set up the bus struct. */
	sc->sc_bus.ub_methods = &ohci_bus_methods;
	sc->sc_bus.ub_pipesize = sizeof(struct ohci_pipe);

	sc->sc_control = sc->sc_intre = 0;

	/* Finally, turn on interrupts. */
	DPRINTF("enabling %#jx", sc->sc_eintrs | OHCI_MIE, 0, 0, 0);
	OWRITE4(sc, OHCI_INTERRUPT_ENABLE, sc->sc_eintrs | OHCI_MIE);

	return 0;

 bad5:
	for (i = 0; i < OHCI_NO_EDS; i++)
		ohci_free_sed(sc, sc->sc_eds[i]);
 bad4:
	ohci_free_sed(sc, sc->sc_isoc_head);
 bad3:
	ohci_free_sed(sc, sc->sc_bulk_head);
 bad2:
	ohci_free_sed(sc, sc->sc_ctrl_head);
 bad1:
	usb_freemem(&sc->sc_hccadma);
	sc->sc_hcca = NULL;
	return err;
}

struct usbd_xfer *
ohci_allocx(struct usbd_bus *bus, unsigned int nframes)
{
	ohci_softc_t *sc = OHCI_BUS2SC(bus);
	struct usbd_xfer *xfer;

	xfer = pool_cache_get(sc->sc_xferpool, PR_WAITOK);
	if (xfer != NULL) {
		memset(xfer, 0, sizeof(struct ohci_xfer));

#ifdef DIAGNOSTIC
		xfer->ux_state = XFER_BUSY;
#endif
	}
	return xfer;
}

void
ohci_freex(struct usbd_bus *bus, struct usbd_xfer *xfer)
{
	ohci_softc_t *sc = OHCI_BUS2SC(bus);

	KASSERTMSG(xfer->ux_state == XFER_BUSY ||
	    xfer->ux_status == USBD_NOT_STARTED,
	    "xfer=%p not busy, 0x%08x\n", xfer, xfer->ux_state);
#ifdef DIAGNOSTIC
	xfer->ux_state = XFER_FREE;
#endif
	pool_cache_put(sc->sc_xferpool, xfer);
}

Static bool
ohci_dying(struct usbd_bus *bus)
{
	ohci_softc_t *sc = OHCI_BUS2SC(bus);

	return sc->sc_dying;
}

Static void
ohci_get_lock(struct usbd_bus *bus, kmutex_t **lock)
{
	ohci_softc_t *sc = OHCI_BUS2SC(bus);

	*lock = &sc->sc_lock;
}

/*
 * Shut down the controller when the system is going down.
 */
bool
ohci_shutdown(device_t self, int flags)
{
	ohci_softc_t *sc = device_private(self);

	OHCIHIST_FUNC(); OHCIHIST_CALLED();

	DPRINTF("stopping the HC", 0, 0, 0, 0);
	OWRITE4(sc, OHCI_INTERRUPT_DISABLE, OHCI_ALL_INTRS);
	OWRITE4(sc, OHCI_CONTROL, OHCI_SET_HCFS(OHCI_HCFS_RESET));
	return true;
}

bool
ohci_resume(device_t dv, const pmf_qual_t *qual)
{
	ohci_softc_t *sc = device_private(dv);
	uint32_t ctl;

	/* Some broken BIOSes do not recover these values */
	OWRITE4(sc, OHCI_HCCA, DMAADDR(&sc->sc_hccadma, 0));
	OWRITE4(sc, OHCI_CONTROL_HEAD_ED,
	    sc->sc_ctrl_head->physaddr);
	OWRITE4(sc, OHCI_BULK_HEAD_ED,
	    sc->sc_bulk_head->physaddr);
	if (sc->sc_intre)
		OWRITE4(sc, OHCI_INTERRUPT_ENABLE, sc->sc_intre &
		    (OHCI_ALL_INTRS | OHCI_MIE));
	if (sc->sc_control)
		ctl = sc->sc_control;
	else
		ctl = OREAD4(sc, OHCI_CONTROL);
	ctl |= OHCI_SET_HCFS(OHCI_HCFS_RESUME);
	OWRITE4(sc, OHCI_CONTROL, ctl);
	usb_delay_ms(&sc->sc_bus, USB_RESUME_DELAY);
	ctl = (ctl & ~OHCI_HCFS_MASK) | OHCI_SET_HCFS(OHCI_HCFS_OPERATIONAL);
	OWRITE4(sc, OHCI_CONTROL, ctl);
	usb_delay_ms(&sc->sc_bus, USB_RESUME_RECOVERY);
	sc->sc_control = sc->sc_intre = 0;

	return true;
}

bool
ohci_suspend(device_t dv, const pmf_qual_t *qual)
{
	ohci_softc_t *sc = device_private(dv);
	uint32_t ctl;

	ctl = OREAD4(sc, OHCI_CONTROL) & ~OHCI_HCFS_MASK;
	if (sc->sc_control == 0) {
		/*
		 * Preserve register values, in case that BIOS
		 * does not recover them.
		 */
		sc->sc_control = ctl;
		sc->sc_intre = OREAD4(sc,
		    OHCI_INTERRUPT_ENABLE);
	}
	ctl |= OHCI_SET_HCFS(OHCI_HCFS_SUSPEND);
	OWRITE4(sc, OHCI_CONTROL, ctl);
	usb_delay_ms(&sc->sc_bus, USB_RESUME_WAIT);

	return true;
}

#ifdef OHCI_DEBUG
void
ohci_dumpregs(ohci_softc_t *sc)
{
	OHCIHIST_FUNC(); OHCIHIST_CALLED();

	DPRINTF("rev=0x%08jx control=0x%08jx command=0x%08jx",
		 OREAD4(sc, OHCI_REVISION),
		 OREAD4(sc, OHCI_CONTROL),
		 OREAD4(sc, OHCI_COMMAND_STATUS), 0);
	DPRINTF("               intrstat=0x%08jx intre=0x%08jx intrd=0x%08jx",
		 OREAD4(sc, OHCI_INTERRUPT_STATUS),
		 OREAD4(sc, OHCI_INTERRUPT_ENABLE),
		 OREAD4(sc, OHCI_INTERRUPT_DISABLE), 0);
	DPRINTF("               hcca=0x%08jx percur=0x%08jx ctrlhd=0x%08jx",
		 OREAD4(sc, OHCI_HCCA),
		 OREAD4(sc, OHCI_PERIOD_CURRENT_ED),
		 OREAD4(sc, OHCI_CONTROL_HEAD_ED), 0);
	DPRINTF("               ctrlcur=0x%08jx bulkhd=0x%08jx bulkcur=0x%08jx",
		 OREAD4(sc, OHCI_CONTROL_CURRENT_ED),
		 OREAD4(sc, OHCI_BULK_HEAD_ED),
		 OREAD4(sc, OHCI_BULK_CURRENT_ED) ,0);
	DPRINTF("               done=0x%08jx fmival=0x%08jx fmrem=0x%08jx",
		 OREAD4(sc, OHCI_DONE_HEAD),
		 OREAD4(sc, OHCI_FM_INTERVAL),
		 OREAD4(sc, OHCI_FM_REMAINING), 0);
	DPRINTF("               fmnum=0x%08jx perst=0x%08jx lsthrs=0x%08jx",
		 OREAD4(sc, OHCI_FM_NUMBER),
		 OREAD4(sc, OHCI_PERIODIC_START),
		 OREAD4(sc, OHCI_LS_THRESHOLD), 0);
	DPRINTF("               desca=0x%08jx descb=0x%08jx stat=0x%08jx",
		 OREAD4(sc, OHCI_RH_DESCRIPTOR_A),
		 OREAD4(sc, OHCI_RH_DESCRIPTOR_B),
		 OREAD4(sc, OHCI_RH_STATUS), 0);
	DPRINTF("               port1=0x%08jx port2=0x%08jx",
		 OREAD4(sc, OHCI_RH_PORT_STATUS(1)),
		 OREAD4(sc, OHCI_RH_PORT_STATUS(2)), 0, 0);
	usb_syncmem(&sc->sc_hccadma,
	    offsetof(struct ohci_hcca, hcca_frame_number),
	    sizeof(sc->sc_hcca->hcca_frame_number) +
	    sizeof(sc->sc_hcca->hcca_done_head),
	    BUS_DMASYNC_POSTREAD);
	DPRINTF("         HCCA: frame_number=0x%04jx done_head=0x%08jx",
		 O32TOH(sc->sc_hcca->hcca_frame_number),
		 O32TOH(sc->sc_hcca->hcca_done_head), 0, 0);
}
#endif

Static int ohci_intr1(ohci_softc_t *);

int
ohci_intr(void *p)
{
	ohci_softc_t *sc = p;
	int ret = 0;

	OHCIHIST_FUNC(); OHCIHIST_CALLED();

	if (sc == NULL)
		return 0;

	mutex_spin_enter(&sc->sc_intr_lock);

	if (sc->sc_dying || !device_has_power(sc->sc_dev))
		goto done;

	/* If we get an interrupt while polling, then just ignore it. */
	if (sc->sc_bus.ub_usepolling) {
		DPRINTFN(16, "ignored interrupt while polling", 0, 0, 0, 0);
		/* for level triggered intrs, should do something to ack */
		OWRITE4(sc, OHCI_INTERRUPT_STATUS,
			OREAD4(sc, OHCI_INTERRUPT_STATUS));

		goto done;
	}

	ret = ohci_intr1(sc);

done:
	mutex_spin_exit(&sc->sc_intr_lock);
	return ret;
}

Static int
ohci_intr1(ohci_softc_t *sc)
{
	uint32_t intrs, eintrs;

	OHCIHIST_FUNC(); OHCIHIST_CALLED();

	/* In case the interrupt occurs before initialization has completed. */
	if (sc == NULL || sc->sc_hcca == NULL) {
#ifdef DIAGNOSTIC
		printf("ohci_intr: sc->sc_hcca == NULL\n");
#endif
		return 0;
	}

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

	intrs = OREAD4(sc, OHCI_INTERRUPT_STATUS);
	if (!intrs)
		return 0;

	/* Acknowledge */
	OWRITE4(sc, OHCI_INTERRUPT_STATUS, intrs & ~(OHCI_MIE|OHCI_WDH));
	eintrs = intrs & sc->sc_eintrs;
	DPRINTFN(7, "sc=%#jx", (uintptr_t)sc, 0, 0, 0);
	DPRINTFN(7, "intrs=%#jx(%#jx) eintrs=%#jx(%#jx)",
	    intrs, OREAD4(sc, OHCI_INTERRUPT_STATUS), eintrs,
	    sc->sc_eintrs);

	if (!eintrs) {
		return 0;
	}

	if (eintrs & OHCI_SO) {
		sc->sc_overrun_cnt++;
		if (usbd_ratecheck(&sc->sc_overrun_ntc)) {
			printf("%s: %u scheduling overruns\n",
			    device_xname(sc->sc_dev), sc->sc_overrun_cnt);
			sc->sc_overrun_cnt = 0;
		}
		/* XXX do what */
		eintrs &= ~OHCI_SO;
	}
	if (eintrs & OHCI_WDH) {
		/*
		 * We block the interrupt below, and reenable it later from
		 * ohci_softintr().
		 */
		usb_schedsoftintr(&sc->sc_bus);
	}
	if (eintrs & OHCI_SF) {
		struct ohci_xfer *ox, *tmp;
		TAILQ_FOREACH_SAFE(ox, &sc->sc_abortingxfers, ox_abnext, tmp) {
			DPRINTFN(10, "SF %#jx xfer %#jx", (uintptr_t)sc,
			    (uintptr_t)ox, 0, 0);
			ox->ox_abintrs &= ~OHCI_SF;
			KASSERT(ox->ox_abintrs == 0);
			TAILQ_REMOVE(&sc->sc_abortingxfers, ox, ox_abnext);
		}
		cv_broadcast(&sc->sc_abort_cv);

		KASSERT(TAILQ_EMPTY(&sc->sc_abortingxfers));
		DPRINTFN(10, "end SOF %#jx", (uintptr_t)sc, 0, 0, 0);
		/* Don't remove OHIC_SF from eintrs so it is blocked below */
	}
	if (eintrs & OHCI_RD) {
		DPRINTFN(5, "resume detect sc=%#jx", (uintptr_t)sc, 0, 0, 0);
		printf("%s: resume detect\n", device_xname(sc->sc_dev));
		/* XXX process resume detect */
	}
	if (eintrs & OHCI_UE) {
		DPRINTFN(5, "unrecoverable error sc=%#jx", (uintptr_t)sc, 0, 0, 0);
		printf("%s: unrecoverable error, controller halted\n",
		       device_xname(sc->sc_dev));
		OWRITE4(sc, OHCI_CONTROL, OHCI_SET_HCFS(OHCI_HCFS_RESET));
		/* XXX what else */
	}
	if (eintrs & OHCI_RHSC) {
		/*
		 * We block the interrupt below, and reenable it later from
		 * a timeout.
		 */
		softint_schedule(sc->sc_rhsc_si);
	}

	if (eintrs != 0) {
		/* Block unprocessed interrupts. */
		OWRITE4(sc, OHCI_INTERRUPT_DISABLE, eintrs);
		sc->sc_eintrs &= ~eintrs;
		DPRINTF("sc %#jx blocking intrs %#jx", (uintptr_t)sc,
		    eintrs, 0, 0);
	}

	return 1;
}

void
ohci_rhsc_enable(void *v_sc)
{
	ohci_softc_t *sc = v_sc;

	OHCIHIST_FUNC(); OHCIHIST_CALLED();
	DPRINTF("sc %#jx", (uintptr_t)sc, 0, 0, 0);
	mutex_spin_enter(&sc->sc_intr_lock);
	sc->sc_eintrs |= OHCI_RHSC;
	OWRITE4(sc, OHCI_INTERRUPT_ENABLE, OHCI_RHSC);
	mutex_spin_exit(&sc->sc_intr_lock);
}

#ifdef OHCI_DEBUG
const char *const ohci_cc_strs[] = {
	"NO_ERROR",
	"CRC",
	"BIT_STUFFING",
	"DATA_TOGGLE_MISMATCH",
	"STALL",
	"DEVICE_NOT_RESPONDING",
	"PID_CHECK_FAILURE",
	"UNEXPECTED_PID",
	"DATA_OVERRUN",
	"DATA_UNDERRUN",
	"BUFFER_OVERRUN",
	"BUFFER_UNDERRUN",
	"reserved",
	"reserved",
	"NOT_ACCESSED",
	"NOT_ACCESSED",
};
#endif

void
ohci_softintr(void *v)
{
	struct usbd_bus *bus = v;
	ohci_softc_t *sc = OHCI_BUS2SC(bus);
	ohci_soft_itd_t *sitd, *sidone, *sitdnext;
	ohci_soft_td_t  *std,  *sdone,  *stdnext;
	struct usbd_xfer *xfer;
	struct ohci_pipe *opipe;
	int len, cc;
	int i, j, actlen, iframes, uedir;
	ohci_physaddr_t done = 0;

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

	OHCIHIST_FUNC(); OHCIHIST_CALLED();

	/*
	 * Only read hccadone if WDH is set - we might get here from places
	 * other than an interrupt
	 */
	if (!(OREAD4(sc, OHCI_INTERRUPT_STATUS) & OHCI_WDH)) {
		DPRINTFN(10, "no WDH %#jx", (uintptr_t)sc, 0, 0, 0);
		return;
	}

	DPRINTFN(10, "WDH %#jx", (uintptr_t)sc, 0, 0, 0);
	usb_syncmem(&sc->sc_hccadma, offsetof(struct ohci_hcca, hcca_done_head),
	    sizeof(sc->sc_hcca->hcca_done_head),
	    BUS_DMASYNC_POSTWRITE | BUS_DMASYNC_POSTREAD);
	done = O32TOH(sc->sc_hcca->hcca_done_head) & ~OHCI_DONE_INTRS;
	sc->sc_hcca->hcca_done_head = 0;
	usb_syncmem(&sc->sc_hccadma, offsetof(struct ohci_hcca, hcca_done_head),
	    sizeof(sc->sc_hcca->hcca_done_head),
	    BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
	OWRITE4(sc, OHCI_INTERRUPT_STATUS, OHCI_WDH);
	sc->sc_eintrs |= OHCI_WDH;
	OWRITE4(sc, OHCI_INTERRUPT_ENABLE, OHCI_WDH);

	/* Reverse the done list. */
	for (sdone = NULL, sidone = NULL; done != 0; ) {
		std = ohci_hash_find_td(sc, done);
		if (std != NULL) {
			usb_syncmem(&std->dma, std->offs, sizeof(std->td),
			    BUS_DMASYNC_POSTWRITE | BUS_DMASYNC_POSTREAD);
			std->dnext = sdone;
			done = O32TOH(std->td.td_nexttd);
			sdone = std;
			DPRINTFN(10, "add TD %#jx", (uintptr_t)std, 0, 0, 0);
			continue;
		}
		sitd = ohci_hash_find_itd(sc, done);
		if (sitd != NULL) {
			usb_syncmem(&sitd->dma, sitd->offs, sizeof(sitd->itd),
			    BUS_DMASYNC_POSTWRITE | BUS_DMASYNC_POSTREAD);
			sitd->dnext = sidone;
			done = O32TOH(sitd->itd.itd_nextitd);
			sidone = sitd;
			DPRINTFN(5, "add ITD %#jx", (uintptr_t)sitd, 0, 0, 0);
			continue;
		}
		DPRINTFN(10, "addr %#jx not found", (uintptr_t)done, 0, 0, 0);
		device_printf(sc->sc_dev, "WARNING: addr 0x%08lx not found\n",
		    (u_long)done);
		break;
	}

	DPRINTFN(10, "sdone=%#jx sidone=%#jx", (uintptr_t)sdone,
	    (uintptr_t)sidone, 0, 0);
	DPRINTFN(10, "--- TD dump start ---", 0, 0, 0, 0);
#ifdef OHCI_DEBUG
	if (ohcidebug >= 10) {
		for (std = sdone; std; std = std->dnext)
			ohci_dump_td(sc, std);
	}
#endif
	DPRINTFN(10, "--- TD dump end ---", 0, 0, 0, 0);

	for (std = sdone; std; std = stdnext) {
		stdnext = std->dnext;
		if (std->held == NULL) {
			DPRINTFN(10, "std=%#jx held is null", (uintptr_t)std,
			    0, 0, 0);
			ohci_hash_rem_td(sc, std);
			ohci_free_std_locked(sc, std);
			continue;
		}

		xfer = std->xfer;
		DPRINTFN(10, "std=%#jx xfer=%#jx hcpriv=%#jx dnext=%#jx",
		    (uintptr_t)std, (uintptr_t)xfer,
		    (uintptr_t)(xfer ? xfer->ux_hcpriv : 0), (uintptr_t)stdnext);
		if (xfer == NULL) {
			/*
			 * xfer == NULL: There seems to be no xfer associated
			 * with this TD. It is tailp that happened to end up on
			 * the done queue.
			 * Shouldn't happen, but some chips are broken(?).
			 */
			continue;
		}
		/*
		 * Try to claim this xfer for completion.  If it has
		 * already completed or aborted, drop it on the floor.
		 */
		if (!usbd_xfer_trycomplete(xfer))
			continue;

		len = std->len;
		if (std->td.td_cbp != 0)
			len -= O32TOH(std->td.td_be) -
			       O32TOH(std->td.td_cbp) + 1;
		DPRINTFN(10, "len=%jd, flags=%#jx", len, std->flags, 0, 0);
		if (std->flags & OHCI_ADD_LEN)
			xfer->ux_actlen += len;

		cc = OHCI_TD_GET_CC(O32TOH(std->td.td_flags));
		if (cc == OHCI_CC_NO_ERROR) {
			ohci_hash_rem_td(sc, std);
			if (std->flags & OHCI_CALL_DONE) {
				xfer->ux_status = USBD_NORMAL_COMPLETION;
				usb_transfer_complete(xfer);
			}
		} else {
			/*
			 * Endpoint is halted.  First unlink all the TDs
			 * belonging to the failed transfer, and then restart
			 * the endpoint.
			 */
			ohci_soft_td_t *p, *n;
			opipe = OHCI_PIPE2OPIPE(xfer->ux_pipe);

			DPRINTFN(10, "error cc=%jd", cc, 0, 0, 0);

			/* remove xfer's TDs from the hash */
			for (p = std; p->xfer == xfer; p = n) {
				n = p->nexttd;
				ohci_hash_rem_td(sc, p);
			}

			ohci_soft_ed_t *sed = opipe->sed;

			/* clear halt and TD chain, preserving toggle carry */
			sed->ed.ed_headp = HTOO32(p->physaddr |
			    (O32TOH(sed->ed.ed_headp) & OHCI_TOGGLECARRY));
			usb_syncmem(&sed->dma,
			    sed->offs + offsetof(ohci_ed_t, ed_headp),
			    sizeof(sed->ed.ed_headp),
			    BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);

			OWRITE4(sc, OHCI_COMMAND_STATUS, OHCI_CLF);

			if (cc == OHCI_CC_DATA_UNDERRUN)
				xfer->ux_status = USBD_NORMAL_COMPLETION;
			else if (cc == OHCI_CC_STALL)
				xfer->ux_status = USBD_STALLED;
			else
				xfer->ux_status = USBD_IOERROR;
			usb_transfer_complete(xfer);
		}
	}
	DPRINTFN(10, "--- ITD dump start ---", 0, 0, 0, 0);
#ifdef OHCI_DEBUG
	if (ohcidebug >= 10) {
		for (sitd = sidone; sitd; sitd = sitd->dnext)
			ohci_dump_itd(sc, sitd);
	}
#endif
	DPRINTFN(10, "--- ITD dump end ---", 0, 0, 0, 0);

	for (sitd = sidone; sitd != NULL; sitd = sitdnext) {
		xfer = sitd->xfer;
		sitdnext = sitd->dnext;
		DPRINTFN(1, "sitd=%#jx xfer=%#jx hcpriv=%#jx", (uintptr_t)sitd,
		    (uintptr_t)xfer, (uintptr_t)(xfer ? xfer->ux_hcpriv : 0),
		    0);
		if (xfer == NULL)
			continue;

		/*
		 * Try to claim this xfer for completion.  If it has
		 * already completed or aborted, drop it on the floor.
		 */
		if (!usbd_xfer_trycomplete(xfer))
			continue;

		KASSERT(!sitd->isdone);
#ifdef DIAGNOSTIC
		sitd->isdone = true;
#endif
		if (sitd->flags & OHCI_CALL_DONE) {
			ohci_soft_itd_t *next;

			opipe = OHCI_PIPE2OPIPE(xfer->ux_pipe);
			opipe->isoc.inuse -= xfer->ux_nframes;
			uedir = UE_GET_DIR(xfer->ux_pipe->up_endpoint->ue_edesc->
			    bEndpointAddress);
			xfer->ux_status = USBD_NORMAL_COMPLETION;
			actlen = 0;
			for (i = 0, sitd = xfer->ux_hcpriv;;
			    sitd = next) {
				next = sitd->nextitd;
				if (OHCI_ITD_GET_CC(O32TOH(sitd->
				    itd.itd_flags)) != OHCI_CC_NO_ERROR)
					xfer->ux_status = USBD_IOERROR;
				/* For input, update frlengths with actual */
				/* XXX anything necessary for output? */
				if (uedir == UE_DIR_IN &&
				    xfer->ux_status == USBD_NORMAL_COMPLETION) {
					iframes = OHCI_ITD_GET_FC(O32TOH(
					    sitd->itd.itd_flags));
					for (j = 0; j < iframes; i++, j++) {
						len = O16TOH(sitd->
						    itd.itd_offset[j]);
						if ((OHCI_ITD_PSW_GET_CC(len) &
						    OHCI_CC_NOT_ACCESSED_MASK)
						    == OHCI_CC_NOT_ACCESSED)
							len = 0;
						else
							len = OHCI_ITD_PSW_SIZE(len);
						xfer->ux_frlengths[i] = len;
						actlen += len;
					}
				}
				if (sitd->flags & OHCI_CALL_DONE)
					break;
				ohci_hash_rem_itd(sc, sitd);

			}
			ohci_hash_rem_itd(sc, sitd);
			if (uedir == UE_DIR_IN &&
			    xfer->ux_status == USBD_NORMAL_COMPLETION)
				xfer->ux_actlen = actlen;
			xfer->ux_hcpriv = NULL;

			usb_transfer_complete(xfer);
		}
	}

	DPRINTFN(10, "done", 0, 0, 0, 0);
	KASSERT(sc->sc_bus.ub_usepolling || mutex_owned(&sc->sc_lock));
}

void
ohci_device_ctrl_done(struct usbd_xfer *xfer)
{
	struct ohci_pipe *opipe = OHCI_PIPE2OPIPE(xfer->ux_pipe);
	ohci_softc_t *sc __diagused = OHCI_XFER2SC(xfer);
	int len = UGETW(xfer->ux_request.wLength);
	int isread = (xfer->ux_request.bmRequestType & UT_READ);

	OHCIHIST_FUNC(); OHCIHIST_CALLED();
	DPRINTFN(10, "xfer=%#jx", (uintptr_t)xfer, 0, 0, 0);

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

	if (len)
		usb_syncmem(&xfer->ux_dmabuf, 0, len,
		    isread ? BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE);
	usb_syncmem(&opipe->ctrl.reqdma, 0,
	    sizeof(usb_device_request_t),  BUS_DMASYNC_POSTWRITE);
}

void
ohci_device_intr_done(struct usbd_xfer *xfer)
{
	ohci_softc_t *sc __diagused = OHCI_XFER2SC(xfer);
	int isread =
	    (UE_GET_DIR(xfer->ux_pipe->up_endpoint->ue_edesc->bEndpointAddress) == UE_DIR_IN);

	OHCIHIST_FUNC(); OHCIHIST_CALLED();
	DPRINTFN(10, "xfer=%#jx, actlen=%jd", (uintptr_t)xfer,
	    xfer->ux_actlen, 0, 0);

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

	usb_syncmem(&xfer->ux_dmabuf, 0, xfer->ux_length,
	    isread ? BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE);
}

void
ohci_device_bulk_done(struct usbd_xfer *xfer)
{
	ohci_softc_t *sc __diagused = OHCI_XFER2SC(xfer);

	int isread =
	    (UE_GET_DIR(xfer->ux_pipe->up_endpoint->ue_edesc->bEndpointAddress) == UE_DIR_IN);

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

	OHCIHIST_FUNC(); OHCIHIST_CALLED();
	DPRINTFN(10, "xfer=%#jx, actlen=%jd", (uintptr_t)xfer, xfer->ux_actlen,
	    0, 0);
	usb_syncmem(&xfer->ux_dmabuf, 0, xfer->ux_length,
	    isread ? BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE);
}

Static void
ohci_rhsc_softint(void *arg)
{
	ohci_softc_t *sc = arg;

	mutex_enter(&sc->sc_lock);

	ohci_rhsc(sc, sc->sc_intrxfer);

	/* Do not allow RHSC interrupts > 1 per second */
	callout_reset(&sc->sc_tmo_rhsc, hz, ohci_rhsc_enable, sc);

	mutex_exit(&sc->sc_lock);
}

void
ohci_rhsc(ohci_softc_t *sc, struct usbd_xfer *xfer)
{
	u_char *p;
	int i, m;
	int hstatus __unused;
	OHCIHIST_FUNC(); OHCIHIST_CALLED();

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

	hstatus = OREAD4(sc, OHCI_RH_STATUS);
	DPRINTF("sc=%#jx xfer=%#jx hstatus=0x%08jx", (uintptr_t)sc,
	    (uintptr_t)xfer, hstatus, 0);

	if (xfer == NULL) {
		/* Just ignore the change. */
		return;
	}
	KASSERT(xfer == sc->sc_intrxfer);
	KASSERT(xfer->ux_status == USBD_IN_PROGRESS);

	p = xfer->ux_buf;
	m = uimin(sc->sc_noport, xfer->ux_length * 8 - 1);
	memset(p, 0, xfer->ux_length);
	for (i = 1; i <= m; i++) {
		/* Pick out CHANGE bits from the status reg. */
		if (OREAD4(sc, OHCI_RH_PORT_STATUS(i)) >> 16)
			p[i/8] |= 1 << (i%8);
	}
	DPRINTF("change=0x%02jx", *p, 0, 0, 0);
	KASSERT(xfer->ux_status == USBD_IN_PROGRESS);
	xfer->ux_actlen = xfer->ux_length;
	xfer->ux_status = USBD_NORMAL_COMPLETION;

	usb_transfer_complete(xfer);
}

void
ohci_root_intr_done(struct usbd_xfer *xfer)
{
	ohci_softc_t *sc = OHCI_XFER2SC(xfer);

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

	/* Claim the xfer so it doesn't get completed again.  */
	KASSERT(sc->sc_intrxfer == xfer);
	KASSERT(xfer->ux_status != USBD_IN_PROGRESS);
	sc->sc_intrxfer = NULL;
}

void
ohci_poll(struct usbd_bus *bus)
{
	ohci_softc_t *sc = OHCI_BUS2SC(bus);
	OHCIHIST_FUNC(); OHCIHIST_CALLED();

#ifdef OHCI_DEBUG
	static int last;
	int new;
	new = OREAD4(sc, OHCI_INTERRUPT_STATUS);
	if (new != last) {
		DPRINTFN(10, "intrs=0x%04jx", new, 0, 0, 0);
		last = new;
	}
#endif
	sc->sc_eintrs |= OHCI_WDH;
	if (OREAD4(sc, OHCI_INTERRUPT_STATUS) & sc->sc_eintrs) {
		mutex_spin_enter(&sc->sc_intr_lock);
		ohci_intr1(sc);
		mutex_spin_exit(&sc->sc_intr_lock);
	}
}

/*
 * Add an ED to the schedule.  Called with USB lock held.
 */
Static void
ohci_add_ed(ohci_softc_t *sc, ohci_soft_ed_t *sed, ohci_soft_ed_t *head)
{
	OHCIHIST_FUNC(); OHCIHIST_CALLED();
	DPRINTFN(8, "sed=%#jx head=%#jx", (uintptr_t)sed, (uintptr_t)head, 0,
	    0);

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

	usb_syncmem(&head->dma, head->offs + offsetof(ohci_ed_t, ed_nexted),
	    sizeof(head->ed.ed_nexted),
	    BUS_DMASYNC_POSTWRITE | BUS_DMASYNC_POSTREAD);
	sed->next = head->next;
	sed->ed.ed_nexted = head->ed.ed_nexted;
	usb_syncmem(&sed->dma, sed->offs + offsetof(ohci_ed_t, ed_nexted),
	    sizeof(sed->ed.ed_nexted),
	    BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
	head->next = sed;
	head->ed.ed_nexted = HTOO32(sed->physaddr);
	usb_syncmem(&head->dma, head->offs + offsetof(ohci_ed_t, ed_nexted),
	    sizeof(head->ed.ed_nexted),
	    BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
}

/*
 * Remove an ED from the schedule.  Called with USB lock held.
 */
Static void
ohci_rem_ed(ohci_softc_t *sc, ohci_soft_ed_t *sed, ohci_soft_ed_t *head)
{
	ohci_soft_ed_t *p;

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

	/* XXX */
	for (p = head; p != NULL && p->next != sed; p = p->next)
		;
	KASSERT(p != NULL);

	usb_syncmem(&sed->dma, sed->offs + offsetof(ohci_ed_t, ed_nexted),
	    sizeof(sed->ed.ed_nexted),
	    BUS_DMASYNC_POSTWRITE | BUS_DMASYNC_POSTREAD);
	p->next = sed->next;
	p->ed.ed_nexted = sed->ed.ed_nexted;
	usb_syncmem(&p->dma, p->offs + offsetof(ohci_ed_t, ed_nexted),
	    sizeof(p->ed.ed_nexted),
	    BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
}

/*
 * When a transfer is completed the TD is added to the done queue by
 * the host controller.  This queue is the processed by software.
 * Unfortunately the queue contains the physical address of the TD
 * and we have no simple way to translate this back to a kernel address.
 * To make the translation possible (and fast) we use a hash table of
 * TDs currently in the schedule.  The physical address is used as the
 * hash value.
 */

#define HASH(a) (((a) >> 4) % OHCI_HASH_SIZE)
/* Called with USB lock held. */
void
ohci_hash_add_td(ohci_softc_t *sc, ohci_soft_td_t *std)
{
	int h = HASH(std->physaddr);

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

	LIST_INSERT_HEAD(&sc->sc_hash_tds[h], std, hnext);
}

/* Called with USB lock held. */
void
ohci_hash_rem_td(ohci_softc_t *sc, ohci_soft_td_t *std)
{

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

	LIST_REMOVE(std, hnext);
}

ohci_soft_td_t *
ohci_hash_find_td(ohci_softc_t *sc, ohci_physaddr_t a)
{
	int h = HASH(a);
	ohci_soft_td_t *std;

	for (std = LIST_FIRST(&sc->sc_hash_tds[h]);
	     std != NULL;
	     std = LIST_NEXT(std, hnext))
		if (std->physaddr == a)
			return std;
	return NULL;
}

/* Called with USB lock held. */
void
ohci_hash_add_itd(ohci_softc_t *sc, ohci_soft_itd_t *sitd)
{
	int h = HASH(sitd->physaddr);

	OHCIHIST_FUNC(); OHCIHIST_CALLED();

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

	DPRINTFN(10, "sitd=%#jx physaddr=0x%08jx",
	    (uintptr_t)sitd, (u_long)sitd->physaddr, 0, 0);

	LIST_INSERT_HEAD(&sc->sc_hash_itds[h], sitd, hnext);
}

/* Called with USB lock held. */
void
ohci_hash_rem_itd(ohci_softc_t *sc, ohci_soft_itd_t *sitd)
{

	OHCIHIST_FUNC(); OHCIHIST_CALLED();

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

	DPRINTFN(10, "sitd=%#jx physaddr=0x%08jx", (uintptr_t)sitd,
	    sitd->physaddr, 0, 0);

	LIST_REMOVE(sitd, hnext);
}

ohci_soft_itd_t *
ohci_hash_find_itd(ohci_softc_t *sc, ohci_physaddr_t a)
{
	int h = HASH(a);
	ohci_soft_itd_t *sitd;

	for (sitd = LIST_FIRST(&sc->sc_hash_itds[h]);
	     sitd != NULL;
	     sitd = LIST_NEXT(sitd, hnext))
		if (sitd->physaddr == a)
			return sitd;
	return NULL;
}

#ifdef OHCI_DEBUG
void
ohci_dump_tds(ohci_softc_t *sc, ohci_soft_td_t *std)
{
	for (; std; std = std->nexttd) {
		ohci_dump_td(sc, std);
		KASSERTMSG(std->nexttd == NULL || std != std->nexttd,
		    "std %p next %p", std, std->nexttd);
	}
}

void
ohci_dump_td(ohci_softc_t *sc, ohci_soft_td_t *std)
{
	OHCIHIST_FUNC(); OHCIHIST_CALLED();

	usb_syncmem(&std->dma, std->offs, sizeof(std->td),
	    BUS_DMASYNC_POSTWRITE | BUS_DMASYNC_POSTREAD);

	uint32_t flags = O32TOH(std->td.td_flags);
	DPRINTF("TD(%#jx) at 0x%08jx:", (uintptr_t)std, std->physaddr, 0, 0);
	DPRINTF("    round=%jd DP=%jx DI=%jx T=%jx",
	    !!(flags & OHCI_TD_R),
	    OHCI_TD_GET_DP(flags),
	    OHCI_TD_GET_DI(flags),
	    OHCI_TD_GET_TOGGLE(flags));
	DPRINTF("    EC=%jd CC=%jd", OHCI_TD_GET_EC(flags),
	    OHCI_TD_GET_CC(flags), 0, 0);
	DPRINTF("    td_cbp=0x%08jx td_nexttd=0x%08jx td_be=0x%08jx",
	       (u_long)O32TOH(std->td.td_cbp),
	       (u_long)O32TOH(std->td.td_nexttd),
	       (u_long)O32TOH(std->td.td_be), 0);
}

void
ohci_dump_itd(ohci_softc_t *sc, ohci_soft_itd_t *sitd)
{
	OHCIHIST_FUNC(); OHCIHIST_CALLED();

	usb_syncmem(&sitd->dma, sitd->offs, sizeof(sitd->itd),
	    BUS_DMASYNC_POSTWRITE | BUS_DMASYNC_POSTREAD);

	uint32_t flags = O32TOH(sitd->itd.itd_flags);
	DPRINTF("ITD(%#jx) at 0x%08jx", (uintptr_t)sitd, sitd->physaddr, 0, 0);
	DPRINTF("    sf=%jd di=%jd fc=%jd cc=%jd",
	    OHCI_ITD_GET_SF(flags), OHCI_ITD_GET_DI(flags),
	    OHCI_ITD_GET_FC(flags), OHCI_ITD_GET_CC(flags));
	DPRINTF("    bp0=0x%08jx next=0x%08jx be=0x%08jx",
	    O32TOH(sitd->itd.itd_bp0),
	    O32TOH(sitd->itd.itd_nextitd),
	    O32TOH(sitd->itd.itd_be), 0);
	CTASSERT(OHCI_ITD_NOFFSET == 8);
	DPRINTF("    offs[0] = 0x%04jx  offs[1] = 0x%04jx  "
	    "offs[2] = 0x%04jx  offs[3] = 0x%04jx",
	    O16TOH(sitd->itd.itd_offset[0]),
	    O16TOH(sitd->itd.itd_offset[1]),
	    O16TOH(sitd->itd.itd_offset[2]),
	    O16TOH(sitd->itd.itd_offset[3]));
	DPRINTF("    offs[4] = 0x%04jx  offs[5] = 0x%04jx  "
	    "offs[6] = 0x%04jx  offs[7] = 0x%04jx",
	    O16TOH(sitd->itd.itd_offset[4]),
	    O16TOH(sitd->itd.itd_offset[5]),
	    O16TOH(sitd->itd.itd_offset[6]),
	    O16TOH(sitd->itd.itd_offset[7]));
}

void
ohci_dump_itds(ohci_softc_t *sc, ohci_soft_itd_t *sitd)
{
	for (; sitd; sitd = sitd->nextitd)
		ohci_dump_itd(sc, sitd);
}

void
ohci_dump_ed(ohci_softc_t *sc, ohci_soft_ed_t *sed)
{
	OHCIHIST_FUNC(); OHCIHIST_CALLED();

	usb_syncmem(&sed->dma, sed->offs, sizeof(sed->ed),
	    BUS_DMASYNC_POSTWRITE | BUS_DMASYNC_POSTREAD);

	uint32_t flags = O32TOH(sed->ed.ed_flags);
	DPRINTF("ED(%#jx) at 0x%08jx:", (uintptr_t)sed, sed->physaddr, 0, 0);
	DPRINTF("    addr=%jd endpt=%jd maxp=%jd",
	    OHCI_ED_GET_FA(flags),
	    OHCI_ED_GET_EN(flags),
	    OHCI_ED_GET_MAXP(flags),
	    0);
	DPRINTF("    dir=%jd speed=%jd skip=%jd iso=%jd",
	    OHCI_ED_GET_DIR(flags),
	    __SHIFTOUT(flags, OHCI_ED_SPEED),
	    __SHIFTOUT(flags, OHCI_ED_SKIP),
	    OHCI_ED_GET_FORMAT(flags));
	DPRINTF("    tailp=0x%08jx", (u_long)O32TOH(sed->ed.ed_tailp),
	    0, 0, 0);
	DPRINTF("    headp=0x%08jx nexted=0x%08jx halted=%jd carry=%jd",
	    O32TOH(sed->ed.ed_headp), O32TOH(sed->ed.ed_nexted),
	    !!(O32TOH(sed->ed.ed_headp) & OHCI_HALTED),
	    !!(O32TOH(sed->ed.ed_headp) & OHCI_TOGGLECARRY));
}
#endif

usbd_status
ohci_open(struct usbd_pipe *pipe)
{
	struct usbd_device *dev = pipe->up_dev;
	struct usbd_bus *bus = dev->ud_bus;
	ohci_softc_t *sc = OHCI_PIPE2SC(pipe);
	usb_endpoint_descriptor_t *ed = pipe->up_endpoint->ue_edesc;
	struct ohci_pipe *opipe = OHCI_PIPE2OPIPE(pipe);
	uint8_t addr = dev->ud_addr;
	uint8_t xfertype = ed->bmAttributes & UE_XFERTYPE;
	ohci_soft_ed_t *sed;
	ohci_soft_td_t *std;
	ohci_soft_itd_t *sitd;
	ohci_physaddr_t tdphys;
	uint32_t fmt;
	usbd_status err = USBD_NOMEM;
	int ival;

	OHCIHIST_FUNC(); OHCIHIST_CALLED();
	DPRINTFN(1, "pipe=%#jx, addr=%jd, endpt=%jd (%jd)", (uintptr_t)pipe,
	    addr, ed->bEndpointAddress, bus->ub_rhaddr);

	if (sc->sc_dying) {
		return USBD_IOERROR;
	}

	std = NULL;
	sed = NULL;

	if (addr == bus->ub_rhaddr) {
		switch (ed->bEndpointAddress) {
		case USB_CONTROL_ENDPOINT:
			pipe->up_methods = &roothub_ctrl_methods;
			break;
		case UE_DIR_IN | USBROOTHUB_INTR_ENDPT:
			pipe->up_methods = &ohci_root_intr_methods;
			break;
		default:
			err = USBD_INVAL;
			goto bad;
		}
	} else {
		sed = ohci_alloc_sed(sc);
		if (sed == NULL)
			goto bad;
		opipe->sed = sed;
		if (xfertype == UE_ISOCHRONOUS) {
			sitd = ohci_alloc_sitd(sc);
			if (sitd == NULL)
				goto bad;

			opipe->tail.itd = sitd;
			sitd->held = &opipe->tail.itd;
			tdphys = sitd->physaddr;
			fmt = OHCI_ED_SET_FORMAT(OHCI_ED_FORMAT_ISO);
			if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN)
				fmt |= OHCI_ED_SET_DIR(OHCI_ED_DIR_IN);
			else
				fmt |= OHCI_ED_SET_DIR(OHCI_ED_DIR_OUT);
		} else {
			std = ohci_alloc_std(sc);
			if (std == NULL)
				goto bad;

			opipe->tail.td = std;
			std->held = &opipe->tail.td;
			tdphys = std->physaddr;
			fmt =
			    OHCI_ED_SET_FORMAT(OHCI_ED_FORMAT_GEN) |
			    OHCI_ED_SET_DIR(OHCI_ED_DIR_TD);
		}
		sed->ed.ed_flags = HTOO32(
			OHCI_ED_SET_FA(addr) |
			OHCI_ED_SET_EN(UE_GET_ADDR(ed->bEndpointAddress)) |
			(dev->ud_speed == USB_SPEED_LOW ? OHCI_ED_SPEED : 0) |
			fmt |
			OHCI_ED_SET_MAXP(UGETW(ed->wMaxPacketSize)));
		sed->ed.ed_headp = HTOO32(tdphys |
		    (pipe->up_endpoint->ue_toggle ? OHCI_TOGGLECARRY : 0));
		sed->ed.ed_tailp = HTOO32(tdphys);
		usb_syncmem(&sed->dma, sed->offs, sizeof(sed->ed),
		    BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);

		switch (xfertype) {
		case UE_CONTROL:
			pipe->up_methods = &ohci_device_ctrl_methods;
			int error = usb_allocmem(sc->sc_bus.ub_dmatag,
			    sizeof(usb_device_request_t), 0,
			    USBMALLOC_COHERENT, &opipe->ctrl.reqdma);
			if (error)
				goto bad;
			mutex_enter(&sc->sc_lock);
			ohci_add_ed(sc, sed, sc->sc_ctrl_head);
			mutex_exit(&sc->sc_lock);
			break;
		case UE_INTERRUPT:
			pipe->up_methods = &ohci_device_intr_methods;
			ival = pipe->up_interval;
			if (ival == USBD_DEFAULT_INTERVAL)
				ival = ed->bInterval;
			err = ohci_device_setintr(sc, opipe, ival);
			if (err)
				goto bad;
			break;
		case UE_ISOCHRONOUS:
			pipe->up_serialise = false;
			pipe->up_methods = &ohci_device_isoc_methods;
			return ohci_setup_isoc(pipe);
		case UE_BULK:
			pipe->up_methods = &ohci_device_bulk_methods;
			mutex_enter(&sc->sc_lock);
			ohci_add_ed(sc, sed, sc->sc_bulk_head);
			mutex_exit(&sc->sc_lock);
			break;
		}
	}

	return USBD_NORMAL_COMPLETION;

 bad:
	if (std != NULL) {
		ohci_free_std(sc, std);
	}
	if (sed != NULL)
		ohci_free_sed(sc, sed);
	return err;

}

/*
 * Close a reqular pipe.
 * Assumes that there are no pending transactions.
 */
void
ohci_close_pipe(struct usbd_pipe *pipe, ohci_soft_ed_t *head)
{
	struct ohci_pipe *opipe = OHCI_PIPE2OPIPE(pipe);
	ohci_softc_t *sc = OHCI_PIPE2SC(pipe);
	ohci_soft_ed_t *sed = opipe->sed;

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

#ifdef DIAGNOSTIC
	sed->ed.ed_flags |= HTOO32(OHCI_ED_SKIP);
	if ((O32TOH(sed->ed.ed_tailp) & OHCI_HEADMASK) !=
	    (O32TOH(sed->ed.ed_headp) & OHCI_HEADMASK)) {
		ohci_soft_td_t *std;
		std = ohci_hash_find_td(sc, O32TOH(sed->ed.ed_headp));
		printf("ohci_close_pipe: pipe not empty sed=%p hd=%#x "
		       "tl=%#x pipe=%p, std=%p\n", sed,
		       (int)O32TOH(sed->ed.ed_headp),
		       (int)O32TOH(sed->ed.ed_tailp),
		       pipe, std);
#ifdef OHCI_DEBUG
		usbd_dump_pipe(&opipe->pipe);
		ohci_dump_ed(sc, sed);
		if (std)
			ohci_dump_td(sc, std);
#endif
		usb_delay_ms(&sc->sc_bus, 2);
		if ((O32TOH(sed->ed.ed_tailp) & OHCI_HEADMASK) !=
		    (O32TOH(sed->ed.ed_headp) & OHCI_HEADMASK))
			printf("ohci_close_pipe: pipe still not empty\n");
	}
#endif
	ohci_rem_ed(sc, sed, head);
	/* Make sure the host controller is not touching this ED */
	usb_delay_ms(&sc->sc_bus, 1);
	pipe->up_endpoint->ue_toggle =
	    (O32TOH(sed->ed.ed_headp) & OHCI_TOGGLECARRY) ? 1 : 0;
	ohci_free_sed_locked(sc, opipe->sed);
}

/*
 * Arrange for the hardware to tells us that it is not still processing
 * the TDs by setting the sKip bit and requesting a SOF interrupt
 *
 * Once we see the SOF interrupt we can check the transfer TDs/iTDs to see if
 * they've been processed and either
 * 	a) if they're unused recover them for later use, or
 *	b) if they've been used allocate new TD/iTDs to replace those
 *         used.  The softint handler will free the old ones.
 */
void
ohci_abortx(struct usbd_xfer *xfer)
{
	OHCIHIST_FUNC(); OHCIHIST_CALLED();
	struct ohci_pipe *opipe = OHCI_PIPE2OPIPE(xfer->ux_pipe);
	ohci_softc_t *sc = OHCI_XFER2SC(xfer);
	ohci_soft_ed_t *sed = opipe->sed;
	ohci_soft_td_t *p, *n;
	ohci_physaddr_t headp;
	int hit;

	DPRINTF("xfer=%#jx pipe=%#jx sed=%#jx", (uintptr_t)xfer,
	    (uintptr_t)opipe, (uintptr_t)sed, 0);

	KASSERT(mutex_owned(&sc->sc_lock));
	ASSERT_SLEEPABLE();

	KASSERTMSG((xfer->ux_status == USBD_CANCELLED ||
		xfer->ux_status == USBD_TIMEOUT),
	    "bad abort status: %d", xfer->ux_status);

	/*
	 * If we're dying, skip the hardware action and just notify the
	 * software that we're done.
	 */
	if (sc->sc_dying) {
		DPRINTFN(4, "xfer %#jx dying %ju", (uintptr_t)xfer,
		    xfer->ux_status, 0, 0);
		goto dying;
	}

	/*
	 * HC Step 1: Unless the endpoint is already halted, we set the
	 * endpoint descriptor sKip bit and wait for hardware to complete
	 * processing.  We ensure the HC stops processing the endpoint by
	 * waiting for the next start of frame (OHCI_SF)
	 */
	DPRINTFN(1, "stop ed=%#jx", (uintptr_t)sed, 0, 0, 0);
	usb_syncmem(&sed->dma, sed->offs + offsetof(ohci_ed_t, ed_flags),
	    sizeof(sed->ed.ed_flags),
	    BUS_DMASYNC_POSTWRITE | BUS_DMASYNC_POSTREAD);
	if (!(sed->ed.ed_flags & OHCI_HALTED)) {
		/* force hardware skip */
		DPRINTFN(1, "pausing ed=%#jx", (uintptr_t)sed, 0, 0, 0);
		sed->ed.ed_flags |= HTOO32(OHCI_ED_SKIP);
		usb_syncmem(&sed->dma,
		    sed->offs + offsetof(ohci_ed_t, ed_flags),
		    sizeof(sed->ed.ed_flags),
		    BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);

		DPRINTFN(10, "SF %#jx xfer %#jx", (uintptr_t)sc,
		    (uintptr_t)xfer, 0, 0);

		struct ohci_xfer *ox = OHCI_XFER2OXFER(xfer);
		ox->ox_abintrs = OHCI_SF;

		mutex_enter(&sc->sc_intr_lock);
		TAILQ_INSERT_TAIL(&sc->sc_abortingxfers, ox, ox_abnext);

		/* Clear any previous SF interrupt */
		OWRITE4(sc, OHCI_INTERRUPT_STATUS, OHCI_SF);

		/* Tell interrupt handler and HC SF interrupt is requested */
		sc->sc_eintrs |= OHCI_SF;
		OWRITE4(sc, OHCI_INTERRUPT_ENABLE, OHCI_SF);
		/*
		 * Step 2: Wait until we know hardware has finished any
		 * processing of the end-point.
		 */
		while (ox->ox_abintrs != 0) {
			DPRINTFN(10, "SF %#jx xfer %#jx intrs %#x",
			    (uintptr_t)sc, (uintptr_t)xfer,
			    (uintptr_t)ox->ox_abintrs, 0);
			cv_wait(&sc->sc_abort_cv, &sc->sc_intr_lock);
		}
		mutex_exit(&sc->sc_intr_lock);
	} else {
		DPRINTFN(1, "halted ed=%#jx", (uintptr_t)sed, 0, 0, 0);
	}

	/*
	 * HC Step 3: Remove any vestiges of the xfer from the hardware.
	 * There are two complications here
	 *
	 * 1) the hardware may have executed beyond the xfer we're trying to
	 *    abort.  So as we're scanning the TDs of this xfer we check if
	 *    the hardware points to any  of them.
	 *
	 * 2) the hardware may have only partially excuted the transfer
	 *    which means some TDs will appear on the done list.  Wait for
	 *    WDH so we can remove them safely.
	 */
	p = xfer->ux_hcpriv;
	KASSERT(p);

#ifdef OHCI_DEBUG
	DPRINTF("--- dump start ---", 0, 0, 0, 0);

	if (ohcidebug >= 2) {
		DPRINTF("sed:", 0, 0, 0, 0);
		ohci_dump_ed(sc, sed);
		ohci_dump_tds(sc, p);
	}
	DPRINTF("--- dump end ---", 0, 0, 0, 0);
#endif


#define OHCI_CC_ACCESSED_P(x)	\
    (((x) & OHCI_CC_NOT_ACCESSED_MASK) != OHCI_CC_NOT_ACCESSED)

	headp = O32TOH(sed->ed.ed_headp) & OHCI_HEADMASK;
	hit = 0;
	for (; p->xfer == xfer; p = n) {
		hit |= headp == p->physaddr;
		n = p->nexttd;

		int cc = OHCI_TD_GET_CC(O32TOH(p->td.td_flags));
		if (!OHCI_CC_ACCESSED_P(cc)) {
			ohci_hash_rem_td(sc, p);
			continue;
		}
		DPRINTFN(10, "xfer=%#jx has been touched by HC", (uintptr_t)p,
		   0, 0, 0);

		mutex_exit(&sc->sc_lock);
		ohci_soft_td_t *std;
		for (;;) {
			std = ohci_alloc_std(sc);
			if (std)
				break;
			kpause("ohciabt2", true, hz, NULL);
		}

		mutex_enter(&sc->sc_lock);
		if (sc->sc_dying) {
			DPRINTFN(4, "xfer %#jx dying %ju", (uintptr_t)xfer,
			    xfer->ux_status, 0, 0);
			goto dying;
		}

		DPRINTFN(10, "new std=%#jx now held at %#jx", (uintptr_t)std,
		    (uintptr_t)p->held, 0, 0);
		*(p->held) = std;
		std->held = p->held;
		std->xfer = xfer;
		p->held = NULL;
	}
	/* Zap headp register if hardware pointed inside the xfer. */
	if (hit) {
		DPRINTFN(1, "set hd=0x%08jx, tl=0x%08jx",  (int)p->physaddr,
		    (int)O32TOH(sed->ed.ed_tailp), 0, 0);
		/* unlink TDs, preserving toggle carry */
		sed->ed.ed_headp = HTOO32(p->physaddr |
		    (O32TOH(sed->ed.ed_headp) & OHCI_TOGGLECARRY));
		usb_syncmem(&sed->dma,
		    sed->offs + offsetof(ohci_ed_t, ed_headp),
		    sizeof(sed->ed.ed_headp),
		    BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
	} else {
		DPRINTFN(1, "no hit", 0, 0, 0, 0);
	}

	/*
	 * HC Step 4: Turn on hardware again.
	 */
	usb_syncmem(&sed->dma, sed->offs + offsetof(ohci_ed_t, ed_flags),
	    sizeof(sed->ed.ed_flags),
	    BUS_DMASYNC_POSTWRITE | BUS_DMASYNC_POSTREAD);
	sed->ed.ed_flags &= HTOO32(~OHCI_ED_SKIP); /* remove hardware skip */
	usb_syncmem(&sed->dma, sed->offs + offsetof(ohci_ed_t, ed_flags),
	    sizeof(sed->ed.ed_flags),
	    BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
dying:
	DPRINTFN(14, "end", 0, 0, 0, 0);

	KASSERT(mutex_owned(&sc->sc_lock));
}

/*
 * Data structures and routines to emulate the root hub.
 */
Static int
ohci_roothub_ctrl(struct usbd_bus *bus, usb_device_request_t *req,
    void *buf, int buflen)
{
	ohci_softc_t *sc = OHCI_BUS2SC(bus);
	usb_port_status_t ps;
	uint16_t len, value, index;
	int l, totlen = 0;
	int port, i;
	uint32_t v;

	OHCIHIST_FUNC(); OHCIHIST_CALLED();

	if (sc->sc_dying)
		return -1;

	DPRINTFN(4, "type=0x%02jx request=%02jx", req->bmRequestType,
	    req->bRequest, 0, 0);

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

#define C(x,y) ((x) | ((y) << 8))
	switch (C(req->bRequest, req->bmRequestType)) {
	case C(UR_GET_DESCRIPTOR, UT_READ_DEVICE):
		DPRINTFN(8, "wValue=0x%04jx", value, 0, 0, 0);
		if (len == 0)
			break;
		switch (value) {
#define sd ((usb_string_descriptor_t *)buf)
		case C(2, UDESC_STRING):
			/* Product */
			totlen = usb_makestrdesc(sd, len, "OHCI root hub");
			break;
#undef sd
		default:
			/* default from usbroothub */
			return buflen;
		}
		break;

	/* Hub requests */
	case C(UR_CLEAR_FEATURE, UT_WRITE_CLASS_DEVICE):
		break;
	case C(UR_CLEAR_FEATURE, UT_WRITE_CLASS_OTHER):
		DPRINTFN(8, "UR_CLEAR_PORT_FEATURE port=%jd feature=%jd",
		    index, value, 0, 0);
		if (index < 1 || index > sc->sc_noport) {
			return -1;
		}
		port = OHCI_RH_PORT_STATUS(index);
		switch(value) {
		case UHF_PORT_ENABLE:
			OWRITE4(sc, port, UPS_CURRENT_CONNECT_STATUS);
			break;
		case UHF_PORT_SUSPEND:
			OWRITE4(sc, port, UPS_OVERCURRENT_INDICATOR);
			break;
		case UHF_PORT_POWER:
			/* Yes, writing to the LOW_SPEED bit clears power. */
			OWRITE4(sc, port, UPS_LOW_SPEED);
			break;
		case UHF_C_PORT_CONNECTION:
			OWRITE4(sc, port, UPS_C_CONNECT_STATUS << 16);
			break;
		case UHF_C_PORT_ENABLE:
			OWRITE4(sc, port, UPS_C_PORT_ENABLED << 16);
			break;
		case UHF_C_PORT_SUSPEND:
			OWRITE4(sc, port, UPS_C_SUSPEND << 16);
			break;
		case UHF_C_PORT_OVER_CURRENT:
			OWRITE4(sc, port, UPS_C_OVERCURRENT_INDICATOR << 16);
			break;
		case UHF_C_PORT_RESET:
			OWRITE4(sc, port, UPS_C_PORT_RESET << 16);
			break;
		default:
			return -1;
		}
		switch(value) {
		case UHF_C_PORT_CONNECTION:
		case UHF_C_PORT_ENABLE:
		case UHF_C_PORT_SUSPEND:
		case UHF_C_PORT_OVER_CURRENT:
		case UHF_C_PORT_RESET:
			/* Enable RHSC interrupt if condition is cleared. */
			if ((OREAD4(sc, port) >> 16) == 0)
				ohci_rhsc_enable(sc);
			break;
		default:
			break;
		}
		break;
	case C(UR_GET_DESCRIPTOR, UT_READ_CLASS_DEVICE):
		if (len == 0)
			break;
		if ((value & 0xff) != 0) {
			return -1;
		}
		usb_hub_descriptor_t hubd;

		totlen = uimin(buflen, sizeof(hubd));
		memcpy(&hubd, buf, totlen);

		v = OREAD4(sc, OHCI_RH_DESCRIPTOR_A);
		hubd.bNbrPorts = sc->sc_noport;
		USETW(hubd.wHubCharacteristics,
		      (v & OHCI_RHD_NPS ? UHD_PWR_NO_SWITCH :
		       v & OHCI_RHD_PSM ? UHD_PWR_GANGED : UHD_PWR_INDIVIDUAL)
		      /* XXX overcurrent */
		      );
		hubd.bPwrOn2PwrGood = OHCI_RHD_GET_POTPGT(v);
		v = OREAD4(sc, OHCI_RH_DESCRIPTOR_B);
		for (i = 0, l = sc->sc_noport; l > 0; i++, l -= 8, v >>= 8)
			hubd.DeviceRemovable[i++] = (uint8_t)v;
		hubd.bDescLength = USB_HUB_DESCRIPTOR_SIZE + i;
		totlen = uimin(totlen, hubd.bDescLength);
		memcpy(buf, &hubd, totlen);
		break;
	case C(UR_GET_STATUS, UT_READ_CLASS_DEVICE):
		if (len != 4) {
			return -1;
		}
		memset(buf, 0, len); /* ? XXX */
		totlen = len;
		break;
	case C(UR_GET_STATUS, UT_READ_CLASS_OTHER):
		DPRINTFN(8, "get port status i=%jd", index, 0, 0, 0);
		if (index < 1 || index > sc->sc_noport) {
			return -1;
		}
		if (len != 4) {
			return -1;
		}
		v = OREAD4(sc, OHCI_RH_PORT_STATUS(index));
		DPRINTFN(8, "port status=0x%04jx", v, 0, 0, 0);
		USETW(ps.wPortStatus, v);
		USETW(ps.wPortChange, v >> 16);
		totlen = uimin(len, sizeof(ps));
		memcpy(buf, &ps, totlen);
		break;
	case C(UR_SET_DESCRIPTOR, UT_WRITE_CLASS_DEVICE):
		return -1;
	case C(UR_SET_FEATURE, UT_WRITE_CLASS_DEVICE):
		break;
	case C(UR_SET_FEATURE, UT_WRITE_CLASS_OTHER):
		if (index < 1 || index > sc->sc_noport) {
			return -1;
		}
		port = OHCI_RH_PORT_STATUS(index);
		switch(value) {
		case UHF_PORT_ENABLE:
			OWRITE4(sc, port, UPS_PORT_ENABLED);
			break;
		case UHF_PORT_SUSPEND:
			OWRITE4(sc, port, UPS_SUSPEND);
			break;
		case UHF_PORT_RESET:
			DPRINTFN(5, "reset port %jd", index, 0, 0, 0);
			OWRITE4(sc, port, UPS_RESET);
			for (i = 0; i < 5; i++) {
				usb_delay_ms(&sc->sc_bus,
					     USB_PORT_ROOT_RESET_DELAY);
				if (sc->sc_dying) {
					return -1;
				}
				if ((OREAD4(sc, port) & UPS_RESET) == 0)
					break;
			}
			DPRINTFN(8, "port %jd reset, status = 0x%04jx", index,
			    OREAD4(sc, port), 0, 0);
			break;
		case UHF_PORT_POWER:
			DPRINTFN(2, "set port power %jd", index, 0, 0, 0);
			OWRITE4(sc, port, UPS_PORT_POWER);
			break;
		default:
			return -1;
		}
		break;
	default:
		/* default from usbroothub */
		return buflen;
	}

	return totlen;
}

Static usbd_status
ohci_root_intr_transfer(struct usbd_xfer *xfer)
{

	/* Pipe isn't running, start first */
	return ohci_root_intr_start(SIMPLEQ_FIRST(&xfer->ux_pipe->up_queue));
}

Static usbd_status
ohci_root_intr_start(struct usbd_xfer *xfer)
{
	ohci_softc_t *sc = OHCI_XFER2SC(xfer);

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

	if (sc->sc_dying)
		return USBD_IOERROR;

	KASSERT(sc->sc_intrxfer == NULL);
	sc->sc_intrxfer = xfer;
	xfer->ux_status = USBD_IN_PROGRESS;

	return USBD_IN_PROGRESS;
}

/* Abort a root interrupt request. */
Static void
ohci_root_intr_abort(struct usbd_xfer *xfer)
{
	ohci_softc_t *sc = OHCI_XFER2SC(xfer);

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

	/* If xfer has already completed, nothing to do here.  */
	if (sc->sc_intrxfer == NULL)
		return;

	/*
	 * Otherwise, sc->sc_intrxfer had better be this transfer.
	 * Cancel it.
	 */
	KASSERT(sc->sc_intrxfer == xfer);
	KASSERT(xfer->ux_status == USBD_IN_PROGRESS);
	xfer->ux_status = USBD_CANCELLED;
	usb_transfer_complete(xfer);
}

/* Close the root pipe. */
Static void
ohci_root_intr_close(struct usbd_pipe *pipe)
{
	ohci_softc_t *sc __diagused = OHCI_PIPE2SC(pipe);

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

	OHCIHIST_FUNC(); OHCIHIST_CALLED();

	/*
	 * Caller must guarantee the xfer has completed first, by
	 * closing the pipe only after normal completion or an abort.
	 */
	KASSERT(sc->sc_intrxfer == NULL);
}

/************************/

int
ohci_device_ctrl_init(struct usbd_xfer *xfer)
{
	struct ohci_xfer *ox = OHCI_XFER2OXFER(xfer);
	usb_device_request_t *req = &xfer->ux_request;
	ohci_softc_t *sc = OHCI_XFER2SC(xfer);
	ohci_soft_td_t *stat, *setup;
	int isread = req->bmRequestType & UT_READ;
	int len = xfer->ux_bufsize;
	int err = ENOMEM;

	OHCIHIST_FUNC(); OHCIHIST_CALLED();

	setup = ohci_alloc_std(sc);
	if (setup == NULL) {
		goto bad1;
	}
	stat = ohci_alloc_std(sc);
	if (stat == NULL) {
		goto bad2;
	}

	ox->ox_setup = setup;
	ox->ox_stat = stat;
	ox->ox_nstd = 0;
	setup->held = &ox->ox_setup;
	stat->held = &ox->ox_stat;

	DPRINTFN(10, "xfer=%#jx setup=%#jx held at %#jx", (uintptr_t)ox,
	    (uintptr_t)setup, (uintptr_t)setup->held, 0);
	DPRINTFN(10, "xfer=%#jx stat= %#jx held at %#jx", (uintptr_t)ox,
	    (uintptr_t)stat, (uintptr_t)stat->held, 0);

	/* Set up data transaction */
	if (len != 0) {
		err = ohci_alloc_std_chain(sc, xfer, len, isread);
		if (err) {
			goto bad3;
		}
	}
	return 0;

 bad3:
	ohci_free_std(sc, stat);
 bad2:
	ohci_free_std(sc, setup);
 bad1:
	return err;
}

void
ohci_device_ctrl_fini(struct usbd_xfer *xfer)
{
	struct ohci_xfer *ox = OHCI_XFER2OXFER(xfer);
	ohci_softc_t *sc = OHCI_XFER2SC(xfer);
	struct ohci_pipe *opipe = OHCI_PIPE2OPIPE(xfer->ux_pipe);

	OHCIHIST_FUNC(); OHCIHIST_CALLED();
	DPRINTFN(8, "xfer %#jx nstd %jd", (uintptr_t)xfer, ox->ox_nstd, 0, 0);

	mutex_enter(&sc->sc_lock);
	if (ox->ox_setup != opipe->tail.td) {
		ohci_free_std_locked(sc, ox->ox_setup);
	}
	for (size_t i = 0; i < ox->ox_nstd; i++) {
		ohci_soft_td_t *std = ox->ox_stds[i];
		if (std == NULL)
			break;
		ohci_free_std_locked(sc, std);
	}
	ohci_free_std_locked(sc, ox->ox_stat);
	mutex_exit(&sc->sc_lock);

	if (ox->ox_nstd) {
		const size_t sz = sizeof(ohci_soft_td_t *) * ox->ox_nstd;
		kmem_free(ox->ox_stds, sz);
	}
}

Static usbd_status
ohci_device_ctrl_transfer(struct usbd_xfer *xfer)
{

	/* Pipe isn't running, start first */
	return ohci_device_ctrl_start(SIMPLEQ_FIRST(&xfer->ux_pipe->up_queue));
}

Static usbd_status
ohci_device_ctrl_start(struct usbd_xfer *xfer)
{
	ohci_softc_t *sc = OHCI_XFER2SC(xfer);
	struct ohci_xfer *ox = OHCI_XFER2OXFER(xfer);
	struct ohci_pipe *opipe = OHCI_PIPE2OPIPE(xfer->ux_pipe);
	usb_device_request_t *req = &xfer->ux_request;
	struct usbd_device *dev __diagused = opipe->pipe.up_dev;
	ohci_soft_td_t *setup, *stat, *next, *tail;
	ohci_soft_ed_t *sed;
	int isread;
	int len;

	OHCIHIST_FUNC(); OHCIHIST_CALLED();

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

	if (sc->sc_dying)
		return USBD_IOERROR;

	KASSERT(xfer->ux_rqflags & URQ_REQUEST);

	isread = req->bmRequestType & UT_READ;
	len = UGETW(req->wLength);

	DPRINTF("xfer=%#jx len=%jd, addr=%jd, endpt=%jd", (uintptr_t)xfer, len,
	    dev->ud_addr, opipe->pipe.up_endpoint->ue_edesc->bEndpointAddress);
	DPRINTF("type=0x%02jx, request=0x%02jx, wValue=0x%04jx, wIndex=0x%04jx",
	    req->bmRequestType, req->bRequest, UGETW(req->wValue),
	    UGETW(req->wIndex));

	/*
	 * Use the pipe "tail" TD as our first and loan our first TD to the
	 * next transfer
	 */
	setup = opipe->tail.td;
	opipe->tail.td = ox->ox_setup;
	ox->ox_setup = setup;
	setup->held = &ox->ox_setup;

	DPRINTFN(10, "xfer=%#jx new setup=%#jx held at %#jx", (uintptr_t)ox,
	    (uintptr_t)setup, (uintptr_t)setup->held, 0);

	stat = ox->ox_stat;

	/* point at sentinel */
	tail = opipe->tail.td;
	tail->held = &opipe->tail.td;
	sed = opipe->sed;

	DPRINTFN(10, "xfer=%#jx new tail=%#jx held at %#jx", (uintptr_t)ox,
	    (uintptr_t)tail, (uintptr_t)tail->held, 0);

	KASSERTMSG(OHCI_ED_GET_FA(O32TOH(sed->ed.ed_flags)) == dev->ud_addr,
	    "address ED %" __PRIuBITS " pipe %d\n",
	    OHCI_ED_GET_FA(O32TOH(sed->ed.ed_flags)), dev->ud_addr);
	KASSERTMSG(OHCI_ED_GET_MAXP(O32TOH(sed->ed.ed_flags)) ==
	    UGETW(opipe->pipe.up_endpoint->ue_edesc->wMaxPacketSize),
	    "MPL ED %" __PRIuBITS " pipe %d\n",
	    OHCI_ED_GET_MAXP(O32TOH(sed->ed.ed_flags)),
	    UGETW(opipe->pipe.up_endpoint->ue_edesc->wMaxPacketSize));

	/* next will point to data if len != 0 */
	next = stat;

	/* Set up data transaction */
	if (len != 0) {
		ohci_soft_td_t *std;
		ohci_soft_td_t *end;

		next = ox->ox_stds[0];
		ohci_reset_std_chain(sc, xfer, len, isread, next, &end);

		end->td.td_nexttd = HTOO32(stat->physaddr);
		end->nexttd = stat;

		usb_syncmem(&end->dma, end->offs, sizeof(end->td),
		    BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);

		usb_syncmem(&xfer->ux_dmabuf, 0, len,
		    isread ? BUS_DMASYNC_PREREAD : BUS_DMASYNC_PREWRITE);
		std = ox->ox_stds[0];
		/* Start toggle at 1 and then use the carried toggle. */
		std->td.td_flags &= HTOO32(~OHCI_TD_TOGGLE_MASK);
		std->td.td_flags |= HTOO32(OHCI_TD_SET_TOGGLE(OHCI_TD_TOGGLE_1));
		usb_syncmem(&std->dma,
		    std->offs + offsetof(ohci_td_t, td_flags),
		    sizeof(std->td.td_flags),
		    BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
	}

	DPRINTFN(8, "setup %#jx data %#jx stat %#jx tail %#jx",
	    (uintptr_t)setup,
	    (uintptr_t)(len != 0 ? ox->ox_stds[0] : NULL), (uintptr_t)stat,
	    (uintptr_t)tail);
	KASSERT(opipe->tail.td == tail);

	memcpy(KERNADDR(&opipe->ctrl.reqdma, 0), req, sizeof(*req));
	usb_syncmem(&opipe->ctrl.reqdma, 0, sizeof(*req), BUS_DMASYNC_PREWRITE);

	setup->td.td_flags = HTOO32(
	    OHCI_TD_SET_DP(OHCI_TD_DP_SETUP) |
	    OHCI_TD_SET_CC(OHCI_TD_NOCC) |
	    OHCI_TD_SET_TOGGLE(OHCI_TD_TOGGLE_0) |
	    OHCI_TD_SET_DI(OHCI_TD_NOINTR)
	    );
	setup->td.td_cbp = HTOO32(DMAADDR(&opipe->ctrl.reqdma, 0));
	setup->td.td_nexttd = HTOO32(next->physaddr);
	setup->td.td_be = HTOO32(O32TOH(setup->td.td_cbp) + sizeof(*req) - 1);
	setup->nexttd = next;
	setup->len = 0;
	setup->xfer = xfer;
	setup->flags = 0;
	ohci_hash_add_td(sc, setup);

	xfer->ux_hcpriv = setup;
	usb_syncmem(&setup->dma, setup->offs, sizeof(setup->td),
	    BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);

	stat->td.td_flags = HTOO32(
	    OHCI_TD_SET_DP(isread ? OHCI_TD_DP_OUT : OHCI_TD_DP_IN) |
	    OHCI_TD_SET_CC(OHCI_TD_NOCC) |
	    OHCI_TD_SET_TOGGLE(OHCI_TD_TOGGLE_1) |
	    OHCI_TD_SET_DI(1)
	    );
	stat->td.td_cbp = 0;
	stat->td.td_nexttd = HTOO32(tail->physaddr);
	stat->td.td_be = 0;
	stat->nexttd = tail;
	stat->flags = OHCI_CALL_DONE;
	stat->len = 0;
	stat->xfer = xfer;
	ohci_hash_add_td(sc, stat);

	usb_syncmem(&stat->dma, stat->offs, sizeof(stat->td),
	    BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);

	memset(&tail->td, 0, sizeof(tail->td));
	tail->nexttd = NULL;
	tail->xfer = NULL;

	usb_syncmem(&tail->dma, tail->offs, sizeof(tail->td),
	    BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);

#ifdef OHCI_DEBUG
	USBHIST_LOGN(ohcidebug, 5, "--- dump start ---", 0, 0, 0, 0);
	if (ohcidebug >= 5) {
		ohci_dump_ed(sc, sed);
		ohci_dump_tds(sc, setup);
	}
	USBHIST_LOGN(ohcidebug, 5, "--- dump end ---", 0, 0, 0, 0);
#endif

	/* Insert ED in schedule */
	sed->ed.ed_tailp = HTOO32(tail->physaddr);
	usb_syncmem(&sed->dma,
	    sed->offs + offsetof(ohci_ed_t, ed_tailp),
	    sizeof(sed->ed.ed_tailp),
	    BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
	OWRITE4(sc, OHCI_COMMAND_STATUS, OHCI_CLF);
	usbd_xfer_schedule_timeout(xfer);

	DPRINTF("done", 0, 0, 0, 0);

	xfer->ux_status = USBD_IN_PROGRESS;

	return USBD_IN_PROGRESS;
}

/* Abort a device control request. */
Static void
ohci_device_ctrl_abort(struct usbd_xfer *xfer)
{
	ohci_softc_t *sc __diagused = OHCI_XFER2SC(xfer);

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

	OHCIHIST_FUNC(); OHCIHIST_CALLED();
	DPRINTF("xfer=%#jx", (uintptr_t)xfer, 0, 0, 0);
	usbd_xfer_abort(xfer);
}

/* Close a device control pipe. */
Static void
ohci_device_ctrl_close(struct usbd_pipe *pipe)
{
	struct ohci_pipe *opipe = OHCI_PIPE2OPIPE(pipe);
	ohci_softc_t *sc = OHCI_PIPE2SC(pipe);

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

	OHCIHIST_FUNC(); OHCIHIST_CALLED();
	DPRINTF("pipe=%#jx", (uintptr_t)pipe, 0, 0, 0);
	ohci_close_pipe(pipe, sc->sc_ctrl_head);
	ohci_free_std_locked(sc, opipe->tail.td);

	usb_freemem(&opipe->ctrl.reqdma);
}

/************************/

Static void
ohci_device_clear_toggle(struct usbd_pipe *pipe)
{
	struct ohci_pipe *opipe = OHCI_PIPE2OPIPE(pipe);
	ohci_softc_t *sc = OHCI_PIPE2SC(pipe);

	opipe->sed->ed.ed_headp &= HTOO32(~OHCI_TOGGLECARRY);
}

Static void
ohci_noop(struct usbd_pipe *pipe)
{
}

Static int
ohci_device_bulk_init(struct usbd_xfer *xfer)
{
	ohci_softc_t *sc = OHCI_XFER2SC(xfer);
	int len = xfer->ux_bufsize;
	int endpt = xfer->ux_pipe->up_endpoint->ue_edesc->bEndpointAddress;
	int isread = UE_GET_DIR(endpt) == UE_DIR_IN;
	int err;

	OHCIHIST_FUNC(); OHCIHIST_CALLED();

	KASSERT(!(xfer->ux_rqflags & URQ_REQUEST));

	DPRINTFN(4, "xfer=%#jx len=%jd isread=%jd flags=%jd", (uintptr_t)xfer,
	    len, isread, xfer->ux_flags);
	DPRINTFN(4, "endpt=%jd", endpt, 0, 0, 0);

	/* Allocate a chain of new TDs (including a new tail). */
	err = ohci_alloc_std_chain(sc, xfer, len, isread);
	if (err)
		return err;

	return 0;
}

Static void
ohci_device_bulk_fini(struct usbd_xfer *xfer)
{
	ohci_softc_t *sc = OHCI_XFER2SC(xfer);
	struct ohci_xfer *ox = OHCI_XFER2OXFER(xfer);
	struct ohci_pipe *opipe = OHCI_PIPE2OPIPE(xfer->ux_pipe);

	OHCIHIST_FUNC(); OHCIHIST_CALLED();
	DPRINTFN(8, "xfer %#jx nstd %jd", (uintptr_t)xfer, ox->ox_nstd, 0, 0);

	mutex_enter(&sc->sc_lock);
	for (size_t i = 0; i < ox->ox_nstd; i++) {
		ohci_soft_td_t *std = ox->ox_stds[i];
		if (std == NULL)
			break;
		if (std != opipe->tail.td)
			ohci_free_std_locked(sc, std);
	}
	mutex_exit(&sc->sc_lock);

	if (ox->ox_nstd) {
		const size_t sz = sizeof(ohci_soft_td_t *) * ox->ox_nstd;
		kmem_free(ox->ox_stds, sz);
	}
}

Static usbd_status
ohci_device_bulk_transfer(struct usbd_xfer *xfer)
{

	/* Pipe isn't running, start first */
	return ohci_device_bulk_start(SIMPLEQ_FIRST(&xfer->ux_pipe->up_queue));
}

Static usbd_status
ohci_device_bulk_start(struct usbd_xfer *xfer)
{
	struct ohci_xfer *ox = OHCI_XFER2OXFER(xfer);
	struct ohci_pipe *opipe = OHCI_PIPE2OPIPE(xfer->ux_pipe);
	ohci_softc_t *sc = OHCI_XFER2SC(xfer);
	ohci_soft_td_t *last;
	ohci_soft_td_t *data, *tail, *tdp;
	ohci_soft_ed_t *sed;
	int len, isread, endpt;

	OHCIHIST_FUNC(); OHCIHIST_CALLED();

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

	if (sc->sc_dying)
		return USBD_IOERROR;

	KASSERT(!(xfer->ux_rqflags & URQ_REQUEST));

	len = xfer->ux_length;
	endpt = xfer->ux_pipe->up_endpoint->ue_edesc->bEndpointAddress;
	isread = UE_GET_DIR(endpt) == UE_DIR_IN;
	sed = opipe->sed;

	DPRINTFN(4, "xfer=%#jx len=%jd isread=%jd flags=%jd", (uintptr_t)xfer,
	    len, isread, xfer->ux_flags);
	DPRINTFN(4, "endpt=%jd", endpt, 0, 0, 0);

	/*
	 * Use the pipe "tail" TD as our first and loan our first TD to the
	 * next transfer
	 */
	data = opipe->tail.td;
	opipe->tail.td = ox->ox_stds[0];
	ox->ox_stds[0] = data;
	data->held = &ox->ox_stds[0];
	ohci_reset_std_chain(sc, xfer, len, isread, data, &last);
	DPRINTFN(10, "xfer=%#jx new data=%#jx held at %#jx",
	    (uintptr_t)ox, (uintptr_t)data, (uintptr_t)data->held, 0);

	/* point at sentinel */
	tail = opipe->tail.td;
	memset(&tail->td, 0, sizeof(tail->td));
	tail->held = &opipe->tail.td;
	tail->nexttd = NULL;
	tail->xfer = NULL;
	DPRINTFN(10, "xfer=%#jx new tail=%#jx held at %#ux",
	    (uintptr_t)ox, (uintptr_t)tail, (uintptr_t)tail->held, 0);
	usb_syncmem(&tail->dma, tail->offs, sizeof(tail->td),
	    BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
	xfer->ux_hcpriv = data;

	DPRINTFN(8, "xfer %#jx data %#jx tail %#jx", (uintptr_t)xfer,
	    (uintptr_t)ox->ox_stds[0], (uintptr_t)tail, 0);
	KASSERT(opipe->tail.td == tail);

	/* We want interrupt at the end of the transfer. */
	last->td.td_flags &= HTOO32(~OHCI_TD_DI_MASK);
	last->td.td_flags |= HTOO32(OHCI_TD_SET_DI(1));
	last->td.td_nexttd = HTOO32(tail->physaddr);
	last->nexttd = tail;
	last->flags |= OHCI_CALL_DONE;
	usb_syncmem(&last->dma, last->offs, sizeof(last->td),
	    BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);

	DPRINTFN(4, "ed_flags=0x%08jx td_flags=0x%08jx "
		    "td_cbp=0x%08jx td_be=0x%08jx",
		    (int)O32TOH(sed->ed.ed_flags),
		    (int)O32TOH(data->td.td_flags),
		    (int)O32TOH(data->td.td_cbp),
		    (int)O32TOH(data->td.td_be));

#ifdef OHCI_DEBUG
	DPRINTFN(5, "--- dump start ---", 0, 0, 0, 0);
	if (ohcidebug >= 5) {
		ohci_dump_ed(sc, sed);
		ohci_dump_tds(sc, data);
	}
	DPRINTFN(5, "--- dump end ---", 0, 0, 0, 0);
#endif

	/* Insert ED in schedule */
	for (tdp = data; tdp != tail; tdp = tdp->nexttd) {
		KASSERT(tdp->xfer == xfer);
	}
	usb_syncmem(&sed->dma, sed->offs, sizeof(sed->ed),
	    BUS_DMASYNC_POSTWRITE | BUS_DMASYNC_POSTREAD);
	sed->ed.ed_tailp = HTOO32(tail->physaddr);
	sed->ed.ed_flags &= HTOO32(~OHCI_ED_SKIP);
	usb_syncmem(&sed->dma, sed->offs, sizeof(sed->ed),
	    BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
	OWRITE4(sc, OHCI_COMMAND_STATUS, OHCI_BLF);
	usbd_xfer_schedule_timeout(xfer);
	xfer->ux_status = USBD_IN_PROGRESS;

	return USBD_IN_PROGRESS;
}

Static void
ohci_device_bulk_abort(struct usbd_xfer *xfer)
{
	ohci_softc_t *sc __diagused = OHCI_XFER2SC(xfer);

	OHCIHIST_FUNC(); OHCIHIST_CALLED();

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

	DPRINTF("xfer=%#jx", (uintptr_t)xfer, 0, 0, 0);
	usbd_xfer_abort(xfer);
}

/*
 * Close a device bulk pipe.
 */
Static void
ohci_device_bulk_close(struct usbd_pipe *pipe)
{
	struct ohci_pipe *opipe = OHCI_PIPE2OPIPE(pipe);
	ohci_softc_t *sc = OHCI_PIPE2SC(pipe);

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

	OHCIHIST_FUNC(); OHCIHIST_CALLED();

	DPRINTF("pipe=%#jx", (uintptr_t)pipe, 0, 0, 0);
	ohci_close_pipe(pipe, sc->sc_bulk_head);
	ohci_free_std_locked(sc, opipe->tail.td);
}

/************************/

Static int
ohci_device_intr_init(struct usbd_xfer *xfer)
{
	struct ohci_xfer *ox = OHCI_XFER2OXFER(xfer);
	ohci_softc_t *sc = OHCI_XFER2SC(xfer);
	int len = xfer->ux_bufsize;
	int endpt = xfer->ux_pipe->up_endpoint->ue_edesc->bEndpointAddress;
	int isread = UE_GET_DIR(endpt) == UE_DIR_IN;
	int err;

	OHCIHIST_FUNC(); OHCIHIST_CALLED();

	KASSERT(!(xfer->ux_rqflags & URQ_REQUEST));
	KASSERT(len != 0);

	DPRINTFN(4, "xfer=%#jx len=%jd isread=%jd flags=%jd", (uintptr_t)xfer,
	    len, isread, xfer->ux_flags);
	DPRINTFN(4, "endpt=%jd", endpt, 0, 0, 0);

	ox->ox_nstd = 0;

	err = ohci_alloc_std_chain(sc, xfer, len, isread);
	if (err) {
		return err;
	}

	return 0;
}

Static void
ohci_device_intr_fini(struct usbd_xfer *xfer)
{
	ohci_softc_t *sc = OHCI_XFER2SC(xfer);
	struct ohci_xfer *ox = OHCI_XFER2OXFER(xfer);
	struct ohci_pipe *opipe = OHCI_PIPE2OPIPE(xfer->ux_pipe);

	OHCIHIST_FUNC(); OHCIHIST_CALLED();
	DPRINTFN(8, "xfer %#jx nstd %jd", (uintptr_t)xfer, ox->ox_nstd, 0, 0);

	mutex_enter(&sc->sc_lock);
	for (size_t i = 0; i < ox->ox_nstd; i++) {
		ohci_soft_td_t *std = ox->ox_stds[i];
		if (std != NULL)
			break;
		if (std != opipe->tail.td)
			ohci_free_std_locked(sc, std);
	}
	mutex_exit(&sc->sc_lock);

	if (ox->ox_nstd) {
		const size_t sz = sizeof(ohci_soft_td_t *) * ox->ox_nstd;
		kmem_free(ox->ox_stds, sz);
	}
}

Static usbd_status
ohci_device_intr_transfer(struct usbd_xfer *xfer)
{

	/* Pipe isn't running, start first */
	return ohci_device_intr_start(SIMPLEQ_FIRST(&xfer->ux_pipe->up_queue));
}

Static usbd_status
ohci_device_intr_start(struct usbd_xfer *xfer)
{
	struct ohci_xfer *ox = OHCI_XFER2OXFER(xfer);
	struct ohci_pipe *opipe = OHCI_PIPE2OPIPE(xfer->ux_pipe);
	ohci_softc_t *sc = OHCI_XFER2SC(xfer);
	ohci_soft_ed_t *sed = opipe->sed;
	ohci_soft_td_t *data, *last, *tail;
	int len, isread, endpt;

	OHCIHIST_FUNC(); OHCIHIST_CALLED();

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

	if (sc->sc_dying)
		return USBD_IOERROR;

	DPRINTFN(3, "xfer=%#jx len=%jd flags=%jd priv=%#jx", (uintptr_t)xfer,
	    xfer->ux_length, xfer->ux_flags, (uintptr_t)xfer->ux_priv);

	KASSERT(!(xfer->ux_rqflags & URQ_REQUEST));

	len = xfer->ux_length;
	endpt = xfer->ux_pipe->up_endpoint->ue_edesc->bEndpointAddress;
	isread = UE_GET_DIR(endpt) == UE_DIR_IN;

	/*
	 * Use the pipe "tail" TD as our first and loan our first TD to the
	 * next transfer.
	 */
	data = opipe->tail.td;
	opipe->tail.td = ox->ox_stds[0];
	ox->ox_stds[0] = data;
	data->held = &ox->ox_stds[0];
	ohci_reset_std_chain(sc, xfer, len, isread, data, &last);
	DPRINTFN(10, "xfer=%#jx new data=%#jx held at %#jx",
	    (uintptr_t)ox, (uintptr_t)data, (uintptr_t)data->held, 0);

	/* point at sentinel */
	tail = opipe->tail.td;
	memset(&tail->td, 0, sizeof(tail->td));
	tail->held = &opipe->tail.td;
	tail->nexttd = NULL;
	tail->xfer = NULL;
	DPRINTFN(10, "xfer=%#jx new tail=%#jx held at %#jx",
	    (uintptr_t)ox, (uintptr_t)tail, (uintptr_t)tail->held, 0);
	usb_syncmem(&tail->dma, tail->offs, sizeof(tail->td),
	    BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
	xfer->ux_hcpriv = data;

	DPRINTFN(8, "data %#jx tail %#jx", (uintptr_t)ox->ox_stds[0],
	    (uintptr_t)tail, 0, 0);
	KASSERT(opipe->tail.td == tail);

	/* We want interrupt at the end of the transfer. */
	last->td.td_flags &= HTOO32(~OHCI_TD_DI_MASK);
	last->td.td_flags |= HTOO32(OHCI_TD_SET_DI(1));

	last->td.td_nexttd = HTOO32(tail->physaddr);
	last->nexttd = tail;
	last->flags |= OHCI_CALL_DONE;
	usb_syncmem(&last->dma, last->offs, sizeof(last->td),
	    BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);

#ifdef OHCI_DEBUG
	DPRINTFN(5, "--- dump start ---", 0, 0, 0, 0);
	if (ohcidebug >= 5) {
		ohci_dump_ed(sc, sed);
		ohci_dump_tds(sc, data);
	}
	DPRINTFN(5, "--- dump end ---", 0, 0, 0, 0);
#endif

	/* Insert ED in schedule */
	usb_syncmem(&sed->dma, sed->offs, sizeof(sed->ed),
	    BUS_DMASYNC_POSTWRITE | BUS_DMASYNC_POSTREAD);
	sed->ed.ed_tailp = HTOO32(tail->physaddr);
	sed->ed.ed_flags &= HTOO32(~OHCI_ED_SKIP);
	usb_syncmem(&sed->dma, sed->offs, sizeof(sed->ed),
	    BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);

	xfer->ux_status = USBD_IN_PROGRESS;

	return USBD_IN_PROGRESS;
}

/* Abort a device interrupt request. */
Static void
ohci_device_intr_abort(struct usbd_xfer *xfer)
{
	ohci_softc_t *sc __diagused = OHCI_XFER2SC(xfer);

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

	usbd_xfer_abort(xfer);
}

/* Close a device interrupt pipe. */
Static void
ohci_device_intr_close(struct usbd_pipe *pipe)
{
	struct ohci_pipe *opipe = OHCI_PIPE2OPIPE(pipe);
	ohci_softc_t *sc = OHCI_PIPE2SC(pipe);
	int nslots = opipe->intr.nslots;
	int pos = opipe->intr.pos;
	int j;
	ohci_soft_ed_t *p, *sed = opipe->sed;

	OHCIHIST_FUNC(); OHCIHIST_CALLED();

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

	DPRINTFN(1, "pipe=%#jx nslots=%jd pos=%jd", (uintptr_t)pipe, nslots,
	    pos, 0);
	usb_syncmem(&sed->dma, sed->offs,
	    sizeof(sed->ed), BUS_DMASYNC_POSTWRITE | BUS_DMASYNC_POSTREAD);
	sed->ed.ed_flags |= HTOO32(OHCI_ED_SKIP);
	usb_syncmem(&sed->dma, sed->offs + offsetof(ohci_ed_t, ed_flags),
	    sizeof(sed->ed.ed_flags),
	    BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
	if ((O32TOH(sed->ed.ed_tailp) & OHCI_HEADMASK) !=
	    (O32TOH(sed->ed.ed_headp) & OHCI_HEADMASK))
		usb_delay_ms_locked(&sc->sc_bus, 2, &sc->sc_lock);

	for (p = sc->sc_eds[pos]; p && p->next != sed; p = p->next)
		continue;
	KASSERT(p);
	p->next = sed->next;
	p->ed.ed_nexted = sed->ed.ed_nexted;
	usb_syncmem(&p->dma, p->offs + offsetof(ohci_ed_t, ed_nexted),
	    sizeof(p->ed.ed_nexted),
	    BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);

	for (j = 0; j < nslots; j++)
		--sc->sc_bws[(pos * nslots + j) % OHCI_NO_INTRS];

	ohci_free_std_locked(sc, opipe->tail.td);
	ohci_free_sed_locked(sc, opipe->sed);
}

Static usbd_status
ohci_device_setintr(ohci_softc_t *sc, struct ohci_pipe *opipe, int ival)
{
	int i, j, best;
	u_int npoll, slow, shigh, nslots;
	u_int bestbw, bw;
	ohci_soft_ed_t *hsed, *sed = opipe->sed;

	OHCIHIST_FUNC(); OHCIHIST_CALLED();

	DPRINTFN(2, "pipe=%#jx", (uintptr_t)opipe, 0, 0, 0);
	if (ival == 0) {
		printf("ohci_setintr: 0 interval\n");
		return USBD_INVAL;
	}

	npoll = OHCI_NO_INTRS;
	while (npoll > ival)
		npoll /= 2;
	DPRINTFN(2, "ival=%jd npoll=%jd", ival, npoll, 0, 0);

	/*
	 * We now know which level in the tree the ED must go into.
	 * Figure out which slot has most bandwidth left over.
	 * Slots to examine:
	 * npoll
	 * 1	0
	 * 2	1 2
	 * 4	3 4 5 6
	 * 8	7 8 9 10 11 12 13 14
	 * N    (N-1) .. (N-1+N-1)
	 */
	slow = npoll-1;
	shigh = slow + npoll;
	nslots = OHCI_NO_INTRS / npoll;
	for (best = i = slow, bestbw = ~0; i < shigh; i++) {
		bw = 0;
		for (j = 0; j < nslots; j++)
			bw += sc->sc_bws[(i * nslots + j) % OHCI_NO_INTRS];
		if (bw < bestbw) {
			best = i;
			bestbw = bw;
		}
	}
	DPRINTFN(2, "best=%jd(%jd..%jd) bestbw=%jd", best, slow, shigh, bestbw);

	mutex_enter(&sc->sc_lock);
	hsed = sc->sc_eds[best];
	sed->next = hsed->next;
	usb_syncmem(&hsed->dma, hsed->offs + offsetof(ohci_ed_t, ed_flags),
	    sizeof(hsed->ed.ed_flags),
	    BUS_DMASYNC_POSTWRITE | BUS_DMASYNC_POSTREAD);
	sed->ed.ed_nexted = hsed->ed.ed_nexted;
	usb_syncmem(&sed->dma, sed->offs + offsetof(ohci_ed_t, ed_flags),
	    sizeof(sed->ed.ed_flags),
	    BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
	hsed->next = sed;
	hsed->ed.ed_nexted = HTOO32(sed->physaddr);
	usb_syncmem(&hsed->dma, hsed->offs + offsetof(ohci_ed_t, ed_flags),
	    sizeof(hsed->ed.ed_flags),
	    BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
	mutex_exit(&sc->sc_lock);

	for (j = 0; j < nslots; j++)
		++sc->sc_bws[(best * nslots + j) % OHCI_NO_INTRS];
	opipe->intr.nslots = nslots;
	opipe->intr.pos = best;

	DPRINTFN(5, "returns %#jx", (uintptr_t)opipe, 0, 0, 0);
	return USBD_NORMAL_COMPLETION;
}

/***********************/

Static int
ohci_device_isoc_init(struct usbd_xfer *xfer)
{
	struct ohci_xfer *ox = OHCI_XFER2OXFER(xfer);
	ohci_softc_t *sc = OHCI_XFER2SC(xfer);
	ohci_soft_itd_t *sitd;
	size_t i;
	int err;

	OHCIHIST_FUNC(); OHCIHIST_CALLED();

	DPRINTFN(1, "xfer %#jx len %jd flags %jd", (uintptr_t)xfer,
	    xfer->ux_length, xfer->ux_flags, 0);

	const size_t nfsitd = howmany(xfer->ux_nframes, OHCI_ITD_NOFFSET);
	const size_t nbsitd = xfer->ux_bufsize / OHCI_PAGE_SIZE;
	const size_t nsitd = MAX(nfsitd, nbsitd) + 1;

	ox->ox_sitds = kmem_zalloc(sizeof(ohci_soft_itd_t *) * nsitd,
	    KM_SLEEP);
	ox->ox_nsitd = nsitd;

	for (i = 0; i < nsitd; i++) {
		/* Allocate next ITD */
		sitd = ohci_alloc_sitd(sc);
		if (sitd == NULL) {
			err = ENOMEM;
			goto fail;
		}
		ox->ox_sitds[i] = sitd;
		sitd->held = &ox->ox_sitds[i];
		sitd->xfer = xfer;
		sitd->flags = 0;
//		DPRINTFN(10, "xfer=%#jx new tail=%#jx held at %#jx",
//		    (uintptr_t)ox, (uintptr_t)tail, (uintptr_t)tail->held, 0);
	}

	return 0;
fail:
	for (; i > 0;) {
		ohci_free_sitd(sc, ox->ox_sitds[--i]);
	}
	return err;
}

Static void
ohci_device_isoc_fini(struct usbd_xfer *xfer)
{
	struct ohci_xfer *ox = OHCI_XFER2OXFER(xfer);
	ohci_softc_t *sc = OHCI_XFER2SC(xfer);
	struct ohci_pipe *opipe = OHCI_PIPE2OPIPE(xfer->ux_pipe);

	OHCIHIST_FUNC(); OHCIHIST_CALLED();

	mutex_enter(&sc->sc_lock);
	for (size_t i = 0; i < ox->ox_nsitd; i++) {
		if (ox->ox_sitds[i] != opipe->tail.itd) {
			ohci_free_sitd_locked(sc, ox->ox_sitds[i]);
		}
	}
	mutex_exit(&sc->sc_lock);

	if (ox->ox_nsitd) {
		const size_t sz = sizeof(ohci_soft_itd_t *) * ox->ox_nsitd;
		kmem_free(ox->ox_sitds, sz);
	}
}


usbd_status
ohci_device_isoc_transfer(struct usbd_xfer *xfer)
{
	OHCIHIST_FUNC(); OHCIHIST_CALLED();

	DPRINTFN(5, "xfer=%#jx", (uintptr_t)xfer, 0, 0, 0);

	/* insert into schedule, */
	ohci_device_isoc_enter(xfer);

	/* and start if the pipe wasn't running */
	return USBD_IN_PROGRESS;
}

void
ohci_device_isoc_enter(struct usbd_xfer *xfer)
{
	struct ohci_xfer *ox = OHCI_XFER2OXFER(xfer);
	struct ohci_pipe *opipe = OHCI_PIPE2OPIPE(xfer->ux_pipe);
	ohci_softc_t *sc = OHCI_XFER2SC(xfer);
	ohci_soft_ed_t *sed = opipe->sed;
	ohci_soft_itd_t *sitd, *nsitd, *tail;
	ohci_physaddr_t buf, offs, bp0, bp1;
	int i, ncur, nframes;
	size_t boff, frlen;

	OHCIHIST_FUNC(); OHCIHIST_CALLED();
	DPRINTFN(5, "xfer=%#jx", (uintptr_t)xfer, 0, 0, 0);

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

	if (sc->sc_dying)
		return;

	struct isoc *isoc = &opipe->isoc;

	DPRINTFN(1, "used=%jd next=%jd xfer=%#jx nframes=%jd",
	     isoc->inuse, isoc->next, (uintptr_t)xfer, xfer->ux_nframes);

	int isread =
	    (UE_GET_DIR(xfer->ux_pipe->up_endpoint->ue_edesc->bEndpointAddress) == UE_DIR_IN);

	if (xfer->ux_length)
		usb_syncmem(&xfer->ux_dmabuf, 0, xfer->ux_length,
		    isread ? BUS_DMASYNC_PREREAD : BUS_DMASYNC_PREWRITE);

	if (isoc->next == -1) {
		/* Not in use yet, schedule it a few frames ahead. */
		usb_syncmem(&sc->sc_hccadma,
		    offsetof(struct ohci_hcca, hcca_frame_number),
		    sizeof(sc->sc_hcca->hcca_frame_number),
		    BUS_DMASYNC_POSTREAD);
		isoc->next = O32TOH(sc->sc_hcca->hcca_frame_number) + 5;
		DPRINTFN(2,"start next=%jd", isoc->next, 0, 0, 0);
	}

	sitd = opipe->tail.itd;
	opipe->tail.itd = ox->ox_sitds[0];
	ox->ox_sitds[0] = sitd;
	sitd->held = &ox->ox_sitds[0];

	boff = 0;
	buf = DMAADDR(&xfer->ux_dmabuf, 0);
	bp0 = bp1 = OHCI_PAGE(buf);
	offs = OHCI_PAGE_OFFSET(buf);

	ohci_physaddr_t end = bp0;	/* XXX stupid GCC */

	nframes = xfer->ux_nframes;
	xfer->ux_hcpriv = sitd;
	size_t j = 1;
	for (i = ncur = 0; i < nframes; i++, ncur++) {
		frlen = xfer->ux_frlengths[i];

		DPRINTFN(1, "frame=%jd ux_frlengths[%jd]=%jd", i, i,
		    xfer->ux_frlengths[i], 0);
		/*
		 * XXXNH: The loop assumes this is never true, because
		 * incrementing 'i' assumes all the ux_frlengths[i] is covered.
		 */
		if (frlen > 2 * OHCI_PAGE_SIZE - offs)
			frlen = 2 * OHCI_PAGE_SIZE - offs;

		boff += frlen;
		buf = DMAADDR(&xfer->ux_dmabuf, boff);
		ohci_physaddr_t noffs = OHCI_PAGE_OFFSET(buf);

		ohci_physaddr_t nend = DMAADDR(&xfer->ux_dmabuf, boff - 1);
		const ohci_physaddr_t nep = OHCI_PAGE(nend);

		/* Note the first page crossing in bp1 */
		if (bp0 == bp1 && bp1 != nep)
			bp1 = nep;

		DPRINTFN(1, "ncur=%jd bp0=%#jx bp1=%#jx nend=%#jx",
		    ncur, bp0, bp1, nend);

		/* all offsets used or too many page crossings */
		if (ncur == OHCI_ITD_NOFFSET || (bp0 != bp1 && bp1 != nep)) {
			/* Allocate next ITD */
			nsitd = ox->ox_sitds[j++];
			KASSERT(nsitd != NULL);
			KASSERT(j < ox->ox_nsitd);

			/* Fill current ITD */
			sitd->itd.itd_flags = HTOO32(
			    OHCI_ITD_SET_CC(OHCI_ITD_NOCC) |
			    OHCI_ITD_SET_SF(isoc->next) |
			    OHCI_ITD_SET_DI(6) | /* delay intr a little */
			    OHCI_ITD_SET_FC(ncur)
			    );
			sitd->itd.itd_bp0 = HTOO32(bp0);
			sitd->itd.itd_nextitd = HTOO32(nsitd->physaddr);
			sitd->itd.itd_be = HTOO32(end);
			sitd->nextitd = nsitd;
			sitd->xfer = xfer;
			sitd->flags = 0;
#ifdef DIAGNOSTIC
			sitd->isdone = false;
#endif
			ohci_hash_add_itd(sc, sitd);
			usb_syncmem(&sitd->dma, sitd->offs, sizeof(sitd->itd),
			    BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);

			sitd = nsitd;
			isoc->next = isoc->next + ncur;
			bp0 = bp1 = OHCI_PAGE(buf);
			ncur = 0;
		}
		sitd->itd.itd_offset[ncur] = HTOO16(OHCI_ITD_MK_OFFS(offs));
		end = nend;
		offs = noffs;
	}
	KASSERT(j <= ox->ox_nsitd);

	/* point at sentinel */
	tail = opipe->tail.itd;
	memset(&tail->itd, 0, sizeof(tail->itd));
	tail->held = &opipe->tail.itd;
	tail->nextitd = NULL;
	tail->xfer = NULL;
	usb_syncmem(&tail->dma, tail->offs, sizeof(tail->itd),
	    BUS_DMASYNC_PREWRITE);

	/* Fixup last used ITD */
	sitd->itd.itd_flags = HTOO32(
	    OHCI_ITD_SET_CC(OHCI_ITD_NOCC) |
	    OHCI_ITD_SET_SF(isoc->next) |
	    OHCI_ITD_SET_DI(0) |
	    OHCI_ITD_SET_FC(ncur)
	    );
	sitd->itd.itd_bp0 = HTOO32(bp0);
	sitd->itd.itd_nextitd = HTOO32(tail->physaddr);
	sitd->itd.itd_be = HTOO32(end);
	sitd->nextitd = tail;
	sitd->xfer = xfer;
	sitd->flags = OHCI_CALL_DONE;
#ifdef DIAGNOSTIC
	sitd->isdone = false;
#endif
	ohci_hash_add_itd(sc, sitd);
	usb_syncmem(&sitd->dma, sitd->offs, sizeof(sitd->itd),
	    BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);

	isoc->next = isoc->next + ncur;
	isoc->inuse += nframes;

	/* XXX pretend we did it all */
	xfer->ux_actlen = offs;
	xfer->ux_status = USBD_IN_PROGRESS;

#ifdef OHCI_DEBUG
	if (ohcidebug >= 5) {
		usb_syncmem(&sc->sc_hccadma,
		    offsetof(struct ohci_hcca, hcca_frame_number),
		    sizeof(sc->sc_hcca->hcca_frame_number),
		    BUS_DMASYNC_POSTREAD);
		DPRINTF("frame=%jd", O32TOH(sc->sc_hcca->hcca_frame_number),
		    0, 0, 0);
		ohci_dump_itds(sc, xfer->ux_hcpriv);
		ohci_dump_ed(sc, sed);
	}
#endif

	usb_syncmem(&sed->dma, sed->offs, sizeof(sed->ed),
	    BUS_DMASYNC_POSTWRITE | BUS_DMASYNC_POSTREAD);
	sed->ed.ed_tailp = HTOO32(tail->physaddr);
	sed->ed.ed_flags &= HTOO32(~OHCI_ED_SKIP);
	usb_syncmem(&sed->dma, sed->offs + offsetof(ohci_ed_t, ed_flags),
	    sizeof(sed->ed.ed_flags),
	    BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
}

void
ohci_device_isoc_abort(struct usbd_xfer *xfer)
{
	struct ohci_pipe *opipe = OHCI_PIPE2OPIPE(xfer->ux_pipe);
	ohci_softc_t *sc = OHCI_XFER2SC(xfer);
	ohci_soft_ed_t *sed;
	ohci_soft_itd_t *sitd;

	OHCIHIST_FUNC(); OHCIHIST_CALLED();
	DPRINTFN(1, "xfer=%#jx", (uintptr_t)xfer, 0, 0, 0);

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

	/* Transfer is already done. */
	if (xfer->ux_status != USBD_NOT_STARTED &&
	    xfer->ux_status != USBD_IN_PROGRESS) {
		printf("ohci_device_isoc_abort: early return\n");
		goto done;
	}

	/* Give xfer the requested abort code. */
	xfer->ux_status = USBD_CANCELLED;

	sed = opipe->sed;
	usb_syncmem(&sed->dma, sed->offs, sizeof(sed->ed),
	    BUS_DMASYNC_POSTWRITE | BUS_DMASYNC_POSTREAD);
	sed->ed.ed_flags |= HTOO32(OHCI_ED_SKIP); /* force hardware skip */
	usb_syncmem(&sed->dma, sed->offs + offsetof(ohci_ed_t, ed_flags),
	    sizeof(sed->ed.ed_flags),
	    BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);

	sitd = xfer->ux_hcpriv;
	KASSERT(sitd);

	usb_delay_ms_locked(&sc->sc_bus, OHCI_ITD_NOFFSET, &sc->sc_lock);

	for (; sitd->xfer == xfer; sitd = sitd->nextitd) {
		ohci_hash_rem_itd(sc, sitd);
#ifdef DIAGNOSTIC
		DPRINTFN(1, "abort sets done sitd=%#jx", (uintptr_t)sitd,
		    0, 0, 0);
		sitd->isdone = true;
#endif
	}

	/* Run callback. */
	usb_transfer_complete(xfer);

	sed->ed.ed_headp = HTOO32(sitd->physaddr); /* unlink TDs */
	sed->ed.ed_flags &= HTOO32(~OHCI_ED_SKIP); /* remove hardware skip */
	usb_syncmem(&sed->dma, sed->offs, sizeof(sed->ed),
	    BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);

 done:
	KASSERT(mutex_owned(&sc->sc_lock));
}

void
ohci_device_isoc_done(struct usbd_xfer *xfer)
{
	OHCIHIST_FUNC(); OHCIHIST_CALLED();
	DPRINTFN(1, "xfer=%#jx", (uintptr_t)xfer, 0, 0, 0);

	int isread =
	    (UE_GET_DIR(xfer->ux_pipe->up_endpoint->ue_edesc->bEndpointAddress) == UE_DIR_IN);

	DPRINTFN(10, "xfer=%#jx, actlen=%jd", (uintptr_t)xfer, xfer->ux_actlen,
	    0, 0);
	usb_syncmem(&xfer->ux_dmabuf, 0, xfer->ux_length,
	    isread ? BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE);
}

usbd_status
ohci_setup_isoc(struct usbd_pipe *pipe)
{
	struct ohci_pipe *opipe = OHCI_PIPE2OPIPE(pipe);
	ohci_softc_t *sc = OHCI_PIPE2SC(pipe);
	struct isoc *isoc = &opipe->isoc;

	isoc->next = -1;
	isoc->inuse = 0;

	mutex_enter(&sc->sc_lock);
	ohci_add_ed(sc, opipe->sed, sc->sc_isoc_head);
	mutex_exit(&sc->sc_lock);

	return USBD_NORMAL_COMPLETION;
}

void
ohci_device_isoc_close(struct usbd_pipe *pipe)
{
	struct ohci_pipe *opipe = OHCI_PIPE2OPIPE(pipe);
	ohci_softc_t *sc = OHCI_PIPE2SC(pipe);

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

	OHCIHIST_FUNC(); OHCIHIST_CALLED();
	DPRINTF("pipe=%#jx", (uintptr_t)pipe, 0, 0, 0);
	ohci_close_pipe(pipe, sc->sc_isoc_head);
#ifdef DIAGNOSTIC
	opipe->tail.itd->isdone = true;
#endif
	ohci_free_sitd_locked(sc, opipe->tail.itd);
}