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
/* $FreeBSD$ */
/*-
 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
 *
 * Copyright (c) 2008 Hans Petter Selasky. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#ifdef USB_GLOBAL_INCLUDE_FILE
#include USB_GLOBAL_INCLUDE_FILE
#else
#include <sys/stdint.h>
#include <sys/stddef.h>
#include <sys/param.h>
#include <sys/eventhandler.h>
#include <sys/queue.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/bus.h>
#include <sys/module.h>
#include <sys/lock.h>
#include <sys/mutex.h>
#include <sys/condvar.h>
#include <sys/sysctl.h>
#include <sys/sx.h>
#include <sys/unistd.h>
#include <sys/callout.h>
#include <sys/malloc.h>
#include <sys/priv.h>
#include <sys/conf.h>
#include <sys/fcntl.h>

#include <dev/usb/usb.h>
#include <dev/usb/usbdi.h>
#include <dev/usb/usbdi_util.h>
#include <dev/usb/usb_ioctl.h>

#if USB_HAVE_UGEN
#include <sys/sbuf.h>
#endif

#include "usbdevs.h"

#define	USB_DEBUG_VAR usb_debug

#include <dev/usb/usb_core.h>
#include <dev/usb/usb_debug.h>
#include <dev/usb/usb_process.h>
#include <dev/usb/usb_device.h>
#include <dev/usb/usb_busdma.h>
#include <dev/usb/usb_transfer.h>
#include <dev/usb/usb_request.h>
#include <dev/usb/usb_dynamic.h>
#include <dev/usb/usb_hub.h>
#include <dev/usb/usb_util.h>
#include <dev/usb/usb_msctest.h>
#if USB_HAVE_UGEN
#include <dev/usb/usb_dev.h>
#include <dev/usb/usb_generic.h>
#endif

#include <dev/usb/quirk/usb_quirk.h>

#include <dev/usb/usb_controller.h>
#include <dev/usb/usb_bus.h>
#endif			/* USB_GLOBAL_INCLUDE_FILE */

/* function prototypes  */

static int	sysctl_hw_usb_template(SYSCTL_HANDLER_ARGS);
static void	usb_init_endpoint(struct usb_device *, uint8_t,
		    struct usb_endpoint_descriptor *,
		    struct usb_endpoint_ss_comp_descriptor *,
		    struct usb_endpoint *);
static void	usb_unconfigure(struct usb_device *, uint8_t);
static void	usb_detach_device_sub(struct usb_device *, device_t *,
		    char **, uint8_t);
static uint8_t	usb_probe_and_attach_sub(struct usb_device *,
		    struct usb_attach_arg *);
static void	usb_init_attach_arg(struct usb_device *,
		    struct usb_attach_arg *);
static void	usb_suspend_resume_sub(struct usb_device *, device_t,
		    uint8_t);
static usb_proc_callback_t usbd_clear_stall_proc;
static usb_error_t usb_config_parse(struct usb_device *, uint8_t, uint8_t);
#if USB_HAVE_DEVCTL
static void	usb_notify_addq(const char *type, struct usb_device *);
#endif
#if USB_HAVE_UGEN
static void	usb_fifo_free_wrap(struct usb_device *, uint8_t, uint8_t);
static void	usb_cdev_create(struct usb_device *);
static void	usb_cdev_free(struct usb_device *);
#endif

/* This variable is global to allow easy access to it: */

#ifdef	USB_TEMPLATE
int	usb_template = USB_TEMPLATE;
#else
int	usb_template = -1;
#endif

SYSCTL_PROC(_hw_usb, OID_AUTO, template,
    CTLTYPE_INT | CTLFLAG_RWTUN | CTLFLAG_MPSAFE,
    NULL, 0, sysctl_hw_usb_template,
    "I", "Selected USB device side template");

/*------------------------------------------------------------------------*
 *	usb_trigger_reprobe_on_off
 *
 * This function sets the pull up resistors for all ports currently
 * operating in device mode either on (when on_not_off is 1), or off
 * (when it's 0).
 *------------------------------------------------------------------------*/
static void
usb_trigger_reprobe_on_off(int on_not_off)
{
	struct usb_port_status ps;
	struct usb_bus *bus;
	struct usb_device *udev;
	usb_error_t err;
	int do_unlock, max;

	max = devclass_get_maxunit(usb_devclass_ptr);
	while (max >= 0) {
		mtx_lock(&usb_ref_lock);
		bus = devclass_get_softc(usb_devclass_ptr, max);
		max--;

		if (bus == NULL || bus->devices == NULL ||
		    bus->devices[USB_ROOT_HUB_ADDR] == NULL) {
			mtx_unlock(&usb_ref_lock);
			continue;
		}

		udev = bus->devices[USB_ROOT_HUB_ADDR];

		if (udev->refcount == USB_DEV_REF_MAX) {
			mtx_unlock(&usb_ref_lock);
			continue;
		}

		udev->refcount++;
		mtx_unlock(&usb_ref_lock);

		do_unlock = usbd_enum_lock(udev);
		if (do_unlock > 1) {
			do_unlock = 0;
			goto next;
		}

		err = usbd_req_get_port_status(udev, NULL, &ps, 1);
		if (err != 0) {
			DPRINTF("usbd_req_get_port_status() "
			    "failed: %s\n", usbd_errstr(err));
			goto next;
		}

		if ((UGETW(ps.wPortStatus) & UPS_PORT_MODE_DEVICE) == 0)
			goto next;

		if (on_not_off) {
			err = usbd_req_set_port_feature(udev, NULL, 1,
			    UHF_PORT_POWER);
			if (err != 0) {
				DPRINTF("usbd_req_set_port_feature() "
				    "failed: %s\n", usbd_errstr(err));
			}
		} else {
			err = usbd_req_clear_port_feature(udev, NULL, 1,
			    UHF_PORT_POWER);
			if (err != 0) {
				DPRINTF("usbd_req_clear_port_feature() "
				    "failed: %s\n", usbd_errstr(err));
			}
		}

next:
		mtx_lock(&usb_ref_lock);
		if (do_unlock)
			usbd_enum_unlock(udev);
		if (--(udev->refcount) == 0)
			cv_broadcast(&udev->ref_cv);
		mtx_unlock(&usb_ref_lock);
	}
}

/*------------------------------------------------------------------------*
 *	usb_trigger_reprobe_all
 *
 * This function toggles the pull up resistors for all ports currently
 * operating in device mode, causing the host machine to reenumerate them.
 *------------------------------------------------------------------------*/
static void
usb_trigger_reprobe_all(void)
{

	/*
	 * Set the pull up resistors off for all ports in device mode.
	 */
	usb_trigger_reprobe_on_off(0);

	/*
	 * According to the DWC OTG spec this must be at least 3ms.
	 */
	usb_pause_mtx(NULL, USB_MS_TO_TICKS(USB_POWER_DOWN_TIME));

	/*
	 * Set the pull up resistors back on.
	 */
	usb_trigger_reprobe_on_off(1);
}

static int
sysctl_hw_usb_template(SYSCTL_HANDLER_ARGS)
{
	int error, val;

	val = usb_template;
	error = sysctl_handle_int(oidp, &val, 0, req);
	if (error != 0 || req->newptr == NULL || usb_template == val)
		return (error);

	usb_template = val;

	if (usb_template < 0) {
		usb_trigger_reprobe_on_off(0);
	} else {
		usb_trigger_reprobe_all();
	}

	return (0);
}

/* English is default language */

static int usb_lang_id = 0x0009;
static int usb_lang_mask = 0x00FF;

SYSCTL_INT(_hw_usb, OID_AUTO, usb_lang_id, CTLFLAG_RWTUN,
    &usb_lang_id, 0, "Preferred USB language ID");

SYSCTL_INT(_hw_usb, OID_AUTO, usb_lang_mask, CTLFLAG_RWTUN,
    &usb_lang_mask, 0, "Preferred USB language mask");

static const char* statestr[USB_STATE_MAX] = {
	[USB_STATE_DETACHED]	= "DETACHED",
	[USB_STATE_ATTACHED]	= "ATTACHED",
	[USB_STATE_POWERED]	= "POWERED",
	[USB_STATE_ADDRESSED]	= "ADDRESSED",
	[USB_STATE_CONFIGURED]	= "CONFIGURED",
};

const char *
usb_statestr(enum usb_dev_state state)
{
	return ((state < USB_STATE_MAX) ? statestr[state] : "UNKNOWN");
}

const char *
usb_get_manufacturer(struct usb_device *udev)
{
	return (udev->manufacturer ? udev->manufacturer : "Unknown");
}

const char *
usb_get_product(struct usb_device *udev)
{
	return (udev->product ? udev->product : "");
}

const char *
usb_get_serial(struct usb_device *udev)
{
	return (udev->serial ? udev->serial : "");
}

/*------------------------------------------------------------------------*
 *	usbd_get_ep_by_addr
 *
 * This function searches for an USB ep by endpoint address and
 * direction.
 *
 * Returns:
 * NULL: Failure
 * Else: Success
 *------------------------------------------------------------------------*/
struct usb_endpoint *
usbd_get_ep_by_addr(struct usb_device *udev, uint8_t ea_val)
{
	struct usb_endpoint *ep = udev->endpoints;
	struct usb_endpoint *ep_end = udev->endpoints + udev->endpoints_max;
	enum {
		EA_MASK = (UE_DIR_IN | UE_DIR_OUT | UE_ADDR),
	};

	/*
	 * According to the USB specification not all bits are used
	 * for the endpoint address. Keep defined bits only:
	 */
	ea_val &= EA_MASK;

	/*
	 * Iterate across all the USB endpoints searching for a match
	 * based on the endpoint address:
	 */
	for (; ep != ep_end; ep++) {
		if (ep->edesc == NULL) {
			continue;
		}
		/* do the mask and check the value */
		if ((ep->edesc->bEndpointAddress & EA_MASK) == ea_val) {
			goto found;
		}
	}

	/*
	 * The default endpoint is always present and is checked separately:
	 */
	if ((udev->ctrl_ep.edesc != NULL) &&
	    ((udev->ctrl_ep.edesc->bEndpointAddress & EA_MASK) == ea_val)) {
		ep = &udev->ctrl_ep;
		goto found;
	}
	return (NULL);

found:
	return (ep);
}

/*------------------------------------------------------------------------*
 *	usbd_get_endpoint
 *
 * This function searches for an USB endpoint based on the information
 * given by the passed "struct usb_config" pointer.
 *
 * Return values:
 * NULL: No match.
 * Else: Pointer to "struct usb_endpoint".
 *------------------------------------------------------------------------*/
struct usb_endpoint *
usbd_get_endpoint(struct usb_device *udev, uint8_t iface_index,
    const struct usb_config *setup)
{
	struct usb_endpoint *ep = udev->endpoints;
	struct usb_endpoint *ep_end = udev->endpoints + udev->endpoints_max;
	uint8_t index = setup->ep_index;
	uint8_t ea_mask;
	uint8_t ea_val;
	uint8_t type_mask;
	uint8_t type_val;

	DPRINTFN(10, "udev=%p iface_index=%d address=0x%x "
	    "type=0x%x dir=0x%x index=%d\n",
	    udev, iface_index, setup->endpoint,
	    setup->type, setup->direction, setup->ep_index);

	/* check USB mode */

	if (setup->usb_mode != USB_MODE_DUAL &&
	    udev->flags.usb_mode != setup->usb_mode) {
		/* wrong mode - no endpoint */
		return (NULL);
	}

	/* setup expected endpoint direction mask and value */

	if (setup->direction == UE_DIR_RX) {
		ea_mask = (UE_DIR_IN | UE_DIR_OUT);
		ea_val = (udev->flags.usb_mode == USB_MODE_DEVICE) ?
		    UE_DIR_OUT : UE_DIR_IN;
	} else if (setup->direction == UE_DIR_TX) {
		ea_mask = (UE_DIR_IN | UE_DIR_OUT);
		ea_val = (udev->flags.usb_mode == USB_MODE_DEVICE) ?
		    UE_DIR_IN : UE_DIR_OUT;
	} else if (setup->direction == UE_DIR_ANY) {
		/* match any endpoint direction */
		ea_mask = 0;
		ea_val = 0;
	} else {
		/* match the given endpoint direction */
		ea_mask = (UE_DIR_IN | UE_DIR_OUT);
		ea_val = (setup->direction & (UE_DIR_IN | UE_DIR_OUT));
	}

	/* setup expected endpoint address */

	if (setup->endpoint == UE_ADDR_ANY) {
		/* match any endpoint address */
	} else {
		/* match the given endpoint address */
		ea_mask |= UE_ADDR;
		ea_val |= (setup->endpoint & UE_ADDR);
	}

	/* setup expected endpoint type */

	if (setup->type == UE_BULK_INTR) {
		/* this will match BULK and INTERRUPT endpoints */
		type_mask = 2;
		type_val = 2;
	} else if (setup->type == UE_TYPE_ANY) {
		/* match any endpoint type */
		type_mask = 0;
		type_val = 0;
	} else {
		/* match the given endpoint type */
		type_mask = UE_XFERTYPE;
		type_val = (setup->type & UE_XFERTYPE);
	}

	/*
	 * Iterate across all the USB endpoints searching for a match
	 * based on the endpoint address. Note that we are searching
	 * the endpoints from the beginning of the "udev->endpoints" array.
	 */
	for (; ep != ep_end; ep++) {
		if ((ep->edesc == NULL) ||
		    (ep->iface_index != iface_index)) {
			continue;
		}
		/* do the masks and check the values */

		if (((ep->edesc->bEndpointAddress & ea_mask) == ea_val) &&
		    ((ep->edesc->bmAttributes & type_mask) == type_val)) {
			if (!index--) {
				goto found;
			}
		}
	}

	/*
	 * Match against default endpoint last, so that "any endpoint", "any
	 * address" and "any direction" returns the first endpoint of the
	 * interface. "iface_index" and "direction" is ignored:
	 */
	if ((udev->ctrl_ep.edesc != NULL) &&
	    ((udev->ctrl_ep.edesc->bEndpointAddress & ea_mask) == ea_val) &&
	    ((udev->ctrl_ep.edesc->bmAttributes & type_mask) == type_val) &&
	    (!index)) {
		ep = &udev->ctrl_ep;
		goto found;
	}
	return (NULL);

found:
	return (ep);
}

/*------------------------------------------------------------------------*
 *	usbd_interface_count
 *
 * This function stores the number of USB interfaces excluding
 * alternate settings, which the USB config descriptor reports into
 * the unsigned 8-bit integer pointed to by "count".
 *
 * Returns:
 *    0: Success
 * Else: Failure
 *------------------------------------------------------------------------*/
usb_error_t
usbd_interface_count(struct usb_device *udev, uint8_t *count)
{
	if (udev->cdesc == NULL) {
		*count = 0;
		return (USB_ERR_NOT_CONFIGURED);
	}
	*count = udev->ifaces_max;
	return (USB_ERR_NORMAL_COMPLETION);
}

/*------------------------------------------------------------------------*
 *	usb_init_endpoint
 *
 * This function will initialise the USB endpoint structure pointed to by
 * the "endpoint" argument. The structure pointed to by "endpoint" must be
 * zeroed before calling this function.
 *------------------------------------------------------------------------*/
static void
usb_init_endpoint(struct usb_device *udev, uint8_t iface_index,
    struct usb_endpoint_descriptor *edesc,
    struct usb_endpoint_ss_comp_descriptor *ecomp,
    struct usb_endpoint *ep)
{
	const struct usb_bus_methods *methods;
	usb_stream_t x;

	methods = udev->bus->methods;

	(methods->endpoint_init) (udev, edesc, ep);

	/* initialise USB endpoint structure */
	ep->edesc = edesc;
	ep->ecomp = ecomp;
	ep->iface_index = iface_index;

	/* setup USB stream queues */
	for (x = 0; x != USB_MAX_EP_STREAMS; x++) {
		TAILQ_INIT(&ep->endpoint_q[x].head);
		ep->endpoint_q[x].command = &usbd_pipe_start;
	}

	/* the pipe is not supported by the hardware */
 	if (ep->methods == NULL)
		return;

	/* check for SUPER-speed streams mode endpoint */
	if (udev->speed == USB_SPEED_SUPER && ecomp != NULL &&
	    (edesc->bmAttributes & UE_XFERTYPE) == UE_BULK &&
	    (UE_GET_BULK_STREAMS(ecomp->bmAttributes) != 0)) {
		usbd_set_endpoint_mode(udev, ep, USB_EP_MODE_STREAMS);
	} else {
		usbd_set_endpoint_mode(udev, ep, USB_EP_MODE_DEFAULT);
	}

	/* clear stall, if any */
	if (methods->clear_stall != NULL) {
		USB_BUS_LOCK(udev->bus);
		(methods->clear_stall) (udev, ep);
		USB_BUS_UNLOCK(udev->bus);
	}
}

/*-----------------------------------------------------------------------*
 *	usb_endpoint_foreach
 *
 * This function will iterate all the USB endpoints except the control
 * endpoint. This function is NULL safe.
 *
 * Return values:
 * NULL: End of USB endpoints
 * Else: Pointer to next USB endpoint
 *------------------------------------------------------------------------*/
struct usb_endpoint *
usb_endpoint_foreach(struct usb_device *udev, struct usb_endpoint *ep)
{
	struct usb_endpoint *ep_end;

	/* be NULL safe */
	if (udev == NULL)
		return (NULL);

	ep_end = udev->endpoints + udev->endpoints_max;

	/* get next endpoint */
	if (ep == NULL)
		ep = udev->endpoints;
	else
		ep++;

	/* find next allocated ep */
	while (ep != ep_end) {
		if (ep->edesc != NULL)
			return (ep);
		ep++;
	}
	return (NULL);
}

/*------------------------------------------------------------------------*
 *	usb_wait_pending_refs
 *
 * This function will wait for any USB references to go away before
 * returning. This function is used before freeing a USB device.
 *------------------------------------------------------------------------*/
static void
usb_wait_pending_refs(struct usb_device *udev)
{
#if USB_HAVE_UGEN
	DPRINTF("Refcount = %d\n", (int)udev->refcount); 

	mtx_lock(&usb_ref_lock);
	udev->refcount--;
	while (1) {
		/* wait for any pending references to go away */
		if (udev->refcount == 0) {
			/* prevent further refs being taken, if any */
			udev->refcount = USB_DEV_REF_MAX;
			break;
		}
		cv_wait(&udev->ref_cv, &usb_ref_lock);
	}
	mtx_unlock(&usb_ref_lock);
#endif
}

/*------------------------------------------------------------------------*
 *	usb_unconfigure
 *
 * This function will free all USB interfaces and USB endpoints belonging
 * to an USB device.
 *
 * Flag values, see "USB_UNCFG_FLAG_XXX".
 *------------------------------------------------------------------------*/
static void
usb_unconfigure(struct usb_device *udev, uint8_t flag)
{
	uint8_t do_unlock;

	/* Prevent re-enumeration */
	do_unlock = usbd_enum_lock(udev);

	/* detach all interface drivers */
	usb_detach_device(udev, USB_IFACE_INDEX_ANY, flag);

#if USB_HAVE_UGEN
	/* free all FIFOs except control endpoint FIFOs */
	usb_fifo_free_wrap(udev, USB_IFACE_INDEX_ANY, flag);

	/*
	 * Free all cdev's, if any.
	 */
	usb_cdev_free(udev);
#endif

#if USB_HAVE_COMPAT_LINUX
	/* free Linux compat device, if any */
	if (udev->linux_endpoint_start != NULL) {
		usb_linux_free_device_p(udev);
		udev->linux_endpoint_start = NULL;
	}
#endif

	usb_config_parse(udev, USB_IFACE_INDEX_ANY, USB_CFG_FREE);

	/* free "cdesc" after "ifaces" and "endpoints", if any */
	if (udev->cdesc != NULL) {
		if (udev->flags.usb_mode != USB_MODE_DEVICE)
			usbd_free_config_desc(udev, udev->cdesc);
		udev->cdesc = NULL;
	}
	/* set unconfigured state */
	udev->curr_config_no = USB_UNCONFIG_NO;
	udev->curr_config_index = USB_UNCONFIG_INDEX;

	if (do_unlock)
		usbd_enum_unlock(udev);
}

/*------------------------------------------------------------------------*
 *	usbd_set_config_index
 *
 * This function selects configuration by index, independent of the
 * actual configuration number. This function should not be used by
 * USB drivers.
 *
 * Returns:
 *    0: Success
 * Else: Failure
 *------------------------------------------------------------------------*/
usb_error_t
usbd_set_config_index(struct usb_device *udev, uint8_t index)
{
	struct usb_status ds;
	struct usb_config_descriptor *cdp;
	uint16_t power;
	uint16_t max_power;
	uint8_t selfpowered;
	uint8_t do_unlock;
	usb_error_t err;

	DPRINTFN(6, "udev=%p index=%d\n", udev, index);

	/* Prevent re-enumeration */
	do_unlock = usbd_enum_lock(udev);

	usb_unconfigure(udev, 0);

	if (index == USB_UNCONFIG_INDEX) {
		/*
		 * Leave unallocated when unconfiguring the
		 * device. "usb_unconfigure()" will also reset
		 * the current config number and index.
		 */
		err = usbd_req_set_config(udev, NULL, USB_UNCONFIG_NO);
		if (udev->state == USB_STATE_CONFIGURED)
			usb_set_device_state(udev, USB_STATE_ADDRESSED);
		goto done;
	}
	/* get the full config descriptor */
	if (udev->flags.usb_mode == USB_MODE_DEVICE) {
		/* save some memory */
		err = usbd_req_get_descriptor_ptr(udev, &cdp, 
		    (UDESC_CONFIG << 8) | index);
	} else {
		/* normal request */
		err = usbd_req_get_config_desc_full(udev,
		    NULL, &cdp, index);
	}
	if (err) {
		goto done;
	}
	/* set the new config descriptor */

	udev->cdesc = cdp;

	/* Figure out if the device is self or bus powered. */
	selfpowered = 0;
	if ((!udev->flags.uq_bus_powered) &&
	    (cdp->bmAttributes & UC_SELF_POWERED) &&
	    (udev->flags.usb_mode == USB_MODE_HOST)) {
		/* May be self powered. */
		if (cdp->bmAttributes & UC_BUS_POWERED) {
			/* Must ask device. */
			err = usbd_req_get_device_status(udev, NULL, &ds);
			if (err) {
				DPRINTFN(0, "could not read "
				    "device status: %s\n",
				    usbd_errstr(err));
			} else if (UGETW(ds.wStatus) & UDS_SELF_POWERED) {
				selfpowered = 1;
			}
			DPRINTF("status=0x%04x \n",
				UGETW(ds.wStatus));
		} else
			selfpowered = 1;
	}
	DPRINTF("udev=%p cdesc=%p (addr %d) cno=%d attr=0x%02x, "
	    "selfpowered=%d, power=%d\n",
	    udev, cdp,
	    udev->address, cdp->bConfigurationValue, cdp->bmAttributes,
	    selfpowered, cdp->bMaxPower * 2);

	/* Check if we have enough power. */
	power = cdp->bMaxPower * 2;

	if (udev->parent_hub) {
		max_power = udev->parent_hub->hub->portpower;
	} else {
		max_power = USB_MAX_POWER;
	}

	if (power > max_power) {
		DPRINTFN(0, "power exceeded %d > %d\n", power, max_power);
		err = USB_ERR_NO_POWER;
		goto done;
	}
	/* Only update "self_powered" in USB Host Mode */
	if (udev->flags.usb_mode == USB_MODE_HOST) {
		udev->flags.self_powered = selfpowered;
	}
	udev->power = power;
	udev->curr_config_no = cdp->bConfigurationValue;
	udev->curr_config_index = index;
	usb_set_device_state(udev, USB_STATE_CONFIGURED);

	/* Set the actual configuration value. */
	err = usbd_req_set_config(udev, NULL, cdp->bConfigurationValue);
	if (err) {
		goto done;
	}

	err = usb_config_parse(udev, USB_IFACE_INDEX_ANY, USB_CFG_ALLOC);
	if (err) {
		goto done;
	}

	err = usb_config_parse(udev, USB_IFACE_INDEX_ANY, USB_CFG_INIT);
	if (err) {
		goto done;
	}

#if USB_HAVE_UGEN
	/* create device nodes for each endpoint */
	usb_cdev_create(udev);
#endif

done:
	DPRINTF("error=%s\n", usbd_errstr(err));
	if (err) {
		usb_unconfigure(udev, 0);
	}
	if (do_unlock)
		usbd_enum_unlock(udev);
	return (err);
}

/*------------------------------------------------------------------------*
 *	usb_config_parse
 *
 * This function will allocate and free USB interfaces and USB endpoints,
 * parse the USB configuration structure and initialise the USB endpoints
 * and interfaces. If "iface_index" is not equal to
 * "USB_IFACE_INDEX_ANY" then the "cmd" parameter is the
 * alternate_setting to be selected for the given interface. Else the
 * "cmd" parameter is defined by "USB_CFG_XXX". "iface_index" can be
 * "USB_IFACE_INDEX_ANY" or a valid USB interface index. This function
 * is typically called when setting the configuration or when setting
 * an alternate interface.
 *
 * Returns:
 *    0: Success
 * Else: Failure
 *------------------------------------------------------------------------*/
static usb_error_t
usb_config_parse(struct usb_device *udev, uint8_t iface_index, uint8_t cmd)
{
	struct usb_idesc_parse_state ips;
	struct usb_interface_descriptor *id;
	struct usb_endpoint_descriptor *ed;
	struct usb_interface *iface;
	struct usb_endpoint *ep;
	usb_error_t err;
	uint8_t ep_curr;
	uint8_t ep_max;
	uint8_t temp;
	uint8_t do_init;
	uint8_t alt_index;

	if (iface_index != USB_IFACE_INDEX_ANY) {
		/* parameter overload */
		alt_index = cmd;
		cmd = USB_CFG_INIT;
	} else {
		/* not used */
		alt_index = 0;
	}

	err = 0;

	DPRINTFN(5, "iface_index=%d cmd=%d\n",
	    iface_index, cmd);

	if (cmd == USB_CFG_FREE)
		goto cleanup;

	if (cmd == USB_CFG_INIT) {
		sx_assert(&udev->enum_sx, SA_LOCKED);

		/* check for in-use endpoints */

		ep = udev->endpoints;
		ep_max = udev->endpoints_max;
		while (ep_max--) {
			/* look for matching endpoints */
			if ((iface_index == USB_IFACE_INDEX_ANY) ||
			    (iface_index == ep->iface_index)) {
				if (ep->refcount_alloc != 0) {
					/*
					 * This typically indicates a
					 * more serious error.
					 */
					err = USB_ERR_IN_USE;
				} else {
					/* reset endpoint */
					memset(ep, 0, sizeof(*ep));
					/* make sure we don't zero the endpoint again */
					ep->iface_index = USB_IFACE_INDEX_ANY;
				}
			}
			ep++;
		}

		if (err)
			return (err);
	}

	memset(&ips, 0, sizeof(ips));

	ep_curr = 0;
	ep_max = 0;

	while ((id = usb_idesc_foreach(udev->cdesc, &ips))) {
		iface = udev->ifaces + ips.iface_index;

		/* check for specific interface match */

		if (cmd == USB_CFG_INIT) {
			if ((iface_index != USB_IFACE_INDEX_ANY) && 
			    (iface_index != ips.iface_index)) {
				/* wrong interface */
				do_init = 0;
			} else if (alt_index != ips.iface_index_alt) {
				/* wrong alternate setting */
				do_init = 0;
			} else {
				/* initialise interface */
				do_init = 1;
			}
		} else
			do_init = 0;

		/* check for new interface */
		if (ips.iface_index_alt == 0) {
			/* update current number of endpoints */
			ep_curr = ep_max;
		}
		/* check for init */
		if (do_init) {
			/* setup the USB interface structure */
			iface->idesc = id;
			/* set alternate index */
			iface->alt_index = alt_index;
			/* set default interface parent */
			if (iface_index == USB_IFACE_INDEX_ANY) {
				iface->parent_iface_index =
				    USB_IFACE_INDEX_ANY;
			}
		}

		DPRINTFN(5, "found idesc nendpt=%d\n", id->bNumEndpoints);

		ed = (struct usb_endpoint_descriptor *)id;

		temp = ep_curr;

		/* iterate all the endpoint descriptors */
		while ((ed = usb_edesc_foreach(udev->cdesc, ed))) {
			/* check if endpoint limit has been reached */
			if (temp >= USB_MAX_EP_UNITS) {
				DPRINTF("Endpoint limit reached\n");
				break;
			}

			ep = udev->endpoints + temp;

			if (do_init) {
				void *ecomp;

				ecomp = usb_ed_comp_foreach(udev->cdesc, (void *)ed);
				if (ecomp != NULL)
					DPRINTFN(5, "Found endpoint companion descriptor\n");

				usb_init_endpoint(udev, 
				    ips.iface_index, ed, ecomp, ep);
			}

			temp ++;

			/* find maximum number of endpoints */
			if (ep_max < temp)
				ep_max = temp;
		}
	}

	/* NOTE: It is valid to have no interfaces and no endpoints! */

	if (cmd == USB_CFG_ALLOC) {
		udev->ifaces_max = ips.iface_index;
#if (USB_HAVE_FIXED_IFACE == 0)
		udev->ifaces = NULL;
		if (udev->ifaces_max != 0) {
			udev->ifaces = malloc(sizeof(*iface) * udev->ifaces_max,
			        M_USB, M_WAITOK | M_ZERO);
			if (udev->ifaces == NULL) {
				err = USB_ERR_NOMEM;
				goto done;
			}
		}
#endif
#if (USB_HAVE_FIXED_ENDPOINT == 0)
		if (ep_max != 0) {
			udev->endpoints = malloc(sizeof(*ep) * ep_max,
			        M_USB, M_WAITOK | M_ZERO);
			if (udev->endpoints == NULL) {
				err = USB_ERR_NOMEM;
				goto done;
			}
		} else {
			udev->endpoints = NULL;
		}
#endif
		USB_BUS_LOCK(udev->bus);
		udev->endpoints_max = ep_max;
		/* reset any ongoing clear-stall */
		udev->ep_curr = NULL;
		USB_BUS_UNLOCK(udev->bus);
	}
#if (USB_HAVE_FIXED_IFACE == 0) || (USB_HAVE_FIXED_ENDPOINT == 0) 
done:
#endif
	if (err) {
		if (cmd == USB_CFG_ALLOC) {
cleanup:
			USB_BUS_LOCK(udev->bus);
			udev->endpoints_max = 0;
			/* reset any ongoing clear-stall */
			udev->ep_curr = NULL;
			USB_BUS_UNLOCK(udev->bus);

#if (USB_HAVE_FIXED_IFACE == 0)
			free(udev->ifaces, M_USB);
			udev->ifaces = NULL;
#endif
#if (USB_HAVE_FIXED_ENDPOINT == 0)
			free(udev->endpoints, M_USB);
			udev->endpoints = NULL;
#endif
			udev->ifaces_max = 0;
		}
	}
	return (err);
}

/*------------------------------------------------------------------------*
 *	usbd_set_alt_interface_index
 *
 * This function will select an alternate interface index for the
 * given interface index. The interface should not be in use when this
 * function is called. That means there should not be any open USB
 * transfers. Else an error is returned. If the alternate setting is
 * already set this function will simply return success. This function
 * is called in Host mode and Device mode!
 *
 * Returns:
 *    0: Success
 * Else: Failure
 *------------------------------------------------------------------------*/
usb_error_t
usbd_set_alt_interface_index(struct usb_device *udev,
    uint8_t iface_index, uint8_t alt_index)
{
	struct usb_interface *iface = usbd_get_iface(udev, iface_index);
	usb_error_t err;
	uint8_t do_unlock;

	/* Prevent re-enumeration */
	do_unlock = usbd_enum_lock(udev);

	if (iface == NULL) {
		err = USB_ERR_INVAL;
		goto done;
	}
	if (iface->alt_index == alt_index) {
		/* 
		 * Optimise away duplicate setting of
		 * alternate setting in USB Host Mode!
		 */
		err = 0;
		goto done;
	}
#if USB_HAVE_UGEN
	/*
	 * Free all generic FIFOs for this interface, except control
	 * endpoint FIFOs:
	 */
	usb_fifo_free_wrap(udev, iface_index, 0);
#endif

	err = usb_config_parse(udev, iface_index, alt_index);
	if (err) {
		goto done;
	}
	if (iface->alt_index != alt_index) {
		/* the alternate setting does not exist */
		err = USB_ERR_INVAL;
		goto done;
	}

	err = usbd_req_set_alt_interface_no(udev, NULL, iface_index,
	    iface->idesc->bAlternateSetting);

done:
	if (do_unlock)
		usbd_enum_unlock(udev);
	return (err);
}

/*------------------------------------------------------------------------*
 *	usbd_set_endpoint_stall
 *
 * This function is used to make a BULK or INTERRUPT endpoint send
 * STALL tokens in USB device mode.
 *
 * Returns:
 *    0: Success
 * Else: Failure
 *------------------------------------------------------------------------*/
usb_error_t
usbd_set_endpoint_stall(struct usb_device *udev, struct usb_endpoint *ep,
    uint8_t do_stall)
{
	struct usb_xfer *xfer;
	usb_stream_t x;
	uint8_t et;
	uint8_t was_stalled;

	if (ep == NULL) {
		/* nothing to do */
		DPRINTF("Cannot find endpoint\n");
		/*
		 * Pretend that the clear or set stall request is
		 * successful else some USB host stacks can do
		 * strange things, especially when a control endpoint
		 * stalls.
		 */
		return (0);
	}
	et = (ep->edesc->bmAttributes & UE_XFERTYPE);

	if ((et != UE_BULK) &&
	    (et != UE_INTERRUPT)) {
		/*
	         * Should not stall control
	         * nor isochronous endpoints.
	         */
		DPRINTF("Invalid endpoint\n");
		return (0);
	}
	USB_BUS_LOCK(udev->bus);

	/* store current stall state */
	was_stalled = ep->is_stalled;

	/* check for no change */
	if (was_stalled && do_stall) {
		/* if the endpoint is already stalled do nothing */
		USB_BUS_UNLOCK(udev->bus);
		DPRINTF("No change\n");
		return (0);
	}
	/* set stalled state */
	ep->is_stalled = 1;

	if (do_stall || (!was_stalled)) {
		if (!was_stalled) {
			for (x = 0; x != USB_MAX_EP_STREAMS; x++) {
				/* lookup the current USB transfer, if any */
				xfer = ep->endpoint_q[x].curr;
				if (xfer != NULL) {
					/*
					 * The "xfer_stall" method
					 * will complete the USB
					 * transfer like in case of a
					 * timeout setting the error
					 * code "USB_ERR_STALLED".
					 */
					(udev->bus->methods->xfer_stall) (xfer);
				}
			}
		}
		(udev->bus->methods->set_stall) (udev, ep, &do_stall);
	}
	if (!do_stall) {
		ep->toggle_next = 0;	/* reset data toggle */
		ep->is_stalled = 0;	/* clear stalled state */

		(udev->bus->methods->clear_stall) (udev, ep);

		/* start the current or next transfer, if any */
		for (x = 0; x != USB_MAX_EP_STREAMS; x++) {
			usb_command_wrapper(&ep->endpoint_q[x],
			    ep->endpoint_q[x].curr);
		}
	}
	USB_BUS_UNLOCK(udev->bus);
	return (0);
}

/*------------------------------------------------------------------------*
 *	usb_reset_iface_endpoints - used in USB device side mode
 *------------------------------------------------------------------------*/
usb_error_t
usb_reset_iface_endpoints(struct usb_device *udev, uint8_t iface_index)
{
	struct usb_endpoint *ep;
	struct usb_endpoint *ep_end;

	ep = udev->endpoints;
	ep_end = udev->endpoints + udev->endpoints_max;

	for (; ep != ep_end; ep++) {
		if ((ep->edesc == NULL) ||
		    (ep->iface_index != iface_index)) {
			continue;
		}
		/* simulate a clear stall from the peer */
		usbd_set_endpoint_stall(udev, ep, 0);
	}
	return (0);
}

/*------------------------------------------------------------------------*
 *	usb_detach_device_sub
 *
 * This function will try to detach an USB device. If it fails a panic
 * will result.
 *
 * Flag values, see "USB_UNCFG_FLAG_XXX".
 *------------------------------------------------------------------------*/
static void
usb_detach_device_sub(struct usb_device *udev, device_t *ppdev,
    char **ppnpinfo, uint8_t flag)
{
	device_t dev;
	char *pnpinfo;
	int err;

	dev = *ppdev;
	if (dev) {
		/*
		 * NOTE: It is important to clear "*ppdev" before deleting
		 * the child due to some device methods being called late
		 * during the delete process !
		 */
		*ppdev = NULL;

		if (!rebooting) {
			device_printf(dev, "at %s, port %d, addr %d "
			    "(disconnected)\n",
			    device_get_nameunit(udev->parent_dev),
			    udev->port_no, udev->address);
		}

		if (device_is_attached(dev)) {
			if (udev->flags.peer_suspended) {
				err = DEVICE_RESUME(dev);
				if (err) {
					device_printf(dev, "Resume failed\n");
				}
			}
		}
		/* detach and delete child */
		if (device_delete_child(udev->parent_dev, dev)) {
			goto error;
		}
	}

	pnpinfo = *ppnpinfo;
	if (pnpinfo != NULL) {
		*ppnpinfo = NULL;
		free(pnpinfo, M_USBDEV);
	}
	return;

error:
	/* Detach is not allowed to fail in the USB world */
	panic("usb_detach_device_sub: A USB driver would not detach\n");
}

/*------------------------------------------------------------------------*
 *	usb_detach_device
 *
 * The following function will detach the matching interfaces.
 * This function is NULL safe.
 *
 * Flag values, see "USB_UNCFG_FLAG_XXX".
 *------------------------------------------------------------------------*/
void
usb_detach_device(struct usb_device *udev, uint8_t iface_index,
    uint8_t flag)
{
	struct usb_interface *iface;
	uint8_t i;

	if (udev == NULL) {
		/* nothing to do */
		return;
	}
	DPRINTFN(4, "udev=%p\n", udev);

	sx_assert(&udev->enum_sx, SA_LOCKED);

	/*
	 * First detach the child to give the child's detach routine a
	 * chance to detach the sub-devices in the correct order.
	 * Then delete the child using "device_delete_child()" which
	 * will detach all sub-devices from the bottom and upwards!
	 */
	if (iface_index != USB_IFACE_INDEX_ANY) {
		i = iface_index;
		iface_index = i + 1;
	} else {
		i = 0;
		iface_index = USB_IFACE_MAX;
	}

	/* do the detach */

	for (; i != iface_index; i++) {
		iface = usbd_get_iface(udev, i);
		if (iface == NULL) {
			/* looks like the end of the USB interfaces */
			break;
		}
		usb_detach_device_sub(udev, &iface->subdev,
		    &iface->pnpinfo, flag);
	}
}

/*------------------------------------------------------------------------*
 *	usb_probe_and_attach_sub
 *
 * Returns:
 *    0: Success
 * Else: Failure
 *------------------------------------------------------------------------*/
static uint8_t
usb_probe_and_attach_sub(struct usb_device *udev,
    struct usb_attach_arg *uaa)
{
	struct usb_interface *iface;
	device_t dev;
	int err;

	iface = uaa->iface;
	if (iface->parent_iface_index != USB_IFACE_INDEX_ANY) {
		/* leave interface alone */
		return (0);
	}
	dev = iface->subdev;
	if (dev) {
		/* clean up after module unload */

		if (device_is_attached(dev)) {
			/* already a device there */
			return (0);
		}
		/* clear "iface->subdev" as early as possible */

		iface->subdev = NULL;

		if (device_delete_child(udev->parent_dev, dev)) {
			/*
			 * Panic here, else one can get a double call
			 * to device_detach().  USB devices should
			 * never fail on detach!
			 */
			panic("device_delete_child() failed\n");
		}
	}
	if (uaa->temp_dev == NULL) {
		/* create a new child */
		uaa->temp_dev = device_add_child(udev->parent_dev, NULL, -1);
		if (uaa->temp_dev == NULL) {
			device_printf(udev->parent_dev,
			    "Device creation failed\n");
			return (1);	/* failure */
		}
		device_set_ivars(uaa->temp_dev, uaa);
		device_quiet(uaa->temp_dev);
	}
	/*
	 * Set "subdev" before probe and attach so that "devd" gets
	 * the information it needs.
	 */
	iface->subdev = uaa->temp_dev;

	if (device_probe_and_attach(iface->subdev) == 0) {
		/*
		 * The USB attach arguments are only available during probe
		 * and attach !
		 */
		uaa->temp_dev = NULL;
		device_set_ivars(iface->subdev, NULL);

		if (udev->flags.peer_suspended) {
			err = DEVICE_SUSPEND(iface->subdev);
			if (err)
				device_printf(iface->subdev, "Suspend failed\n");
		}
		return (0);		/* success */
	} else {
		/* No USB driver found */
		iface->subdev = NULL;
	}
	return (1);			/* failure */
}

/*------------------------------------------------------------------------*
 *	usbd_set_parent_iface
 *
 * Using this function will lock the alternate interface setting on an
 * interface. It is typically used for multi interface drivers. In USB
 * device side mode it is assumed that the alternate interfaces all
 * have the same endpoint descriptors. The default parent index value
 * is "USB_IFACE_INDEX_ANY". Then the alternate setting value is not
 * locked.
 *------------------------------------------------------------------------*/
void
usbd_set_parent_iface(struct usb_device *udev, uint8_t iface_index,
    uint8_t parent_index)
{
	struct usb_interface *iface;

	if (udev == NULL || iface_index == parent_index) {
		/* nothing to do */
		return;
	}
	iface = usbd_get_iface(udev, iface_index);
	if (iface != NULL)
		iface->parent_iface_index = parent_index;
}

static void
usb_init_attach_arg(struct usb_device *udev,
    struct usb_attach_arg *uaa)
{
	memset(uaa, 0, sizeof(*uaa));

	uaa->device = udev;
	uaa->usb_mode = udev->flags.usb_mode;
	uaa->port = udev->port_no;
	uaa->dev_state = UAA_DEV_READY;

	uaa->info.idVendor = UGETW(udev->ddesc.idVendor);
	uaa->info.idProduct = UGETW(udev->ddesc.idProduct);
	uaa->info.bcdDevice = UGETW(udev->ddesc.bcdDevice);
	uaa->info.bDeviceClass = udev->ddesc.bDeviceClass;
	uaa->info.bDeviceSubClass = udev->ddesc.bDeviceSubClass;
	uaa->info.bDeviceProtocol = udev->ddesc.bDeviceProtocol;
	uaa->info.bConfigIndex = udev->curr_config_index;
	uaa->info.bConfigNum = udev->curr_config_no;
}

/*------------------------------------------------------------------------*
 *	usb_probe_and_attach
 *
 * This function is called from "uhub_explore_sub()",
 * "usb_handle_set_config()" and "usb_handle_request()".
 *
 * Returns:
 *    0: Success
 * Else: A control transfer failed
 *------------------------------------------------------------------------*/
usb_error_t
usb_probe_and_attach(struct usb_device *udev, uint8_t iface_index)
{
	struct usb_attach_arg uaa;
	struct usb_interface *iface;
	uint8_t i;
	uint8_t j;
	uint8_t do_unlock;

	if (udev == NULL) {
		DPRINTF("udev == NULL\n");
		return (USB_ERR_INVAL);
	}
	/* Prevent re-enumeration */
	do_unlock = usbd_enum_lock(udev);

	if (udev->curr_config_index == USB_UNCONFIG_INDEX) {
		/* do nothing - no configuration has been set */
		goto done;
	}
	/* setup USB attach arguments */

	usb_init_attach_arg(udev, &uaa);

	/*
	 * If the whole USB device is targeted, invoke the USB event
	 * handler(s):
	 */
	if (iface_index == USB_IFACE_INDEX_ANY) {
		if (usb_test_quirk(&uaa, UQ_MSC_DYMO_EJECT) != 0 &&
		    usb_dymo_eject(udev, 0) == 0) {
			/* success, mark the udev as disappearing */
			uaa.dev_state = UAA_DEV_EJECTING;
		}

		EVENTHANDLER_INVOKE(usb_dev_configured, udev, &uaa);

		if (uaa.dev_state != UAA_DEV_READY) {
			/* leave device unconfigured */
			usb_unconfigure(udev, 0);
			goto done;
		}
	}

	/* Check if only one interface should be probed: */
	if (iface_index != USB_IFACE_INDEX_ANY) {
		i = iface_index;
		j = i + 1;
	} else {
		i = 0;
		j = USB_IFACE_MAX;
	}

	/* Do the probe and attach */
	for (; i != j; i++) {
		iface = usbd_get_iface(udev, i);
		if (iface == NULL) {
			/*
			 * Looks like the end of the USB
			 * interfaces !
			 */
			DPRINTFN(2, "end of interfaces "
			    "at %u\n", i);
			break;
		}
		if (iface->idesc == NULL) {
			/* no interface descriptor */
			continue;
		}
		uaa.iface = iface;

		uaa.info.bInterfaceClass =
		    iface->idesc->bInterfaceClass;
		uaa.info.bInterfaceSubClass =
		    iface->idesc->bInterfaceSubClass;
		uaa.info.bInterfaceProtocol =
		    iface->idesc->bInterfaceProtocol;
		uaa.info.bIfaceIndex = i;
		uaa.info.bIfaceNum =
		    iface->idesc->bInterfaceNumber;
		uaa.driver_info = 0;	/* reset driver_info */

		DPRINTFN(2, "iclass=%u/%u/%u iindex=%u/%u\n",
		    uaa.info.bInterfaceClass,
		    uaa.info.bInterfaceSubClass,
		    uaa.info.bInterfaceProtocol,
		    uaa.info.bIfaceIndex,
		    uaa.info.bIfaceNum);

		usb_probe_and_attach_sub(udev, &uaa);

		/*
		 * Remove the leftover child, if any, to enforce that
		 * a new nomatch devd event is generated for the next
		 * interface if no driver is found:
		 */
		if (uaa.temp_dev == NULL)
			continue;
		if (device_delete_child(udev->parent_dev, uaa.temp_dev))
			DPRINTFN(0, "device delete child failed\n");
		uaa.temp_dev = NULL;
	}
done:
	if (do_unlock)
		usbd_enum_unlock(udev);
	return (0);
}

/*------------------------------------------------------------------------*
 *	usb_suspend_resume_sub
 *
 * This function is called when the suspend or resume methods should
 * be executed on an USB device.
 *------------------------------------------------------------------------*/
static void
usb_suspend_resume_sub(struct usb_device *udev, device_t dev, uint8_t do_suspend)
{
	int err;

	if (dev == NULL) {
		return;
	}
	if (!device_is_attached(dev)) {
		return;
	}
	if (do_suspend) {
		err = DEVICE_SUSPEND(dev);
	} else {
		err = DEVICE_RESUME(dev);
	}
	if (err) {
		device_printf(dev, "%s failed\n",
		    do_suspend ? "Suspend" : "Resume");
	}
}

/*------------------------------------------------------------------------*
 *	usb_suspend_resume
 *
 * The following function will suspend or resume the USB device.
 *
 * Returns:
 *    0: Success
 * Else: Failure
 *------------------------------------------------------------------------*/
usb_error_t
usb_suspend_resume(struct usb_device *udev, uint8_t do_suspend)
{
	struct usb_interface *iface;
	uint8_t i;

	if (udev == NULL) {
		/* nothing to do */
		return (0);
	}
	DPRINTFN(4, "udev=%p do_suspend=%d\n", udev, do_suspend);

	sx_assert(&udev->sr_sx, SA_LOCKED);

	USB_BUS_LOCK(udev->bus);
	/* filter the suspend events */
	if (udev->flags.peer_suspended == do_suspend) {
		USB_BUS_UNLOCK(udev->bus);
		/* nothing to do */
		return (0);
	}
	udev->flags.peer_suspended = do_suspend;
	USB_BUS_UNLOCK(udev->bus);

	/* do the suspend or resume */

	for (i = 0; i != USB_IFACE_MAX; i++) {
		iface = usbd_get_iface(udev, i);
		if (iface == NULL) {
			/* looks like the end of the USB interfaces */
			break;
		}
		usb_suspend_resume_sub(udev, iface->subdev, do_suspend);
	}
	return (0);
}

/*------------------------------------------------------------------------*
 *      usbd_clear_stall_proc
 *
 * This function performs generic USB clear stall operations.
 *------------------------------------------------------------------------*/
static void
usbd_clear_stall_proc(struct usb_proc_msg *_pm)
{
	struct usb_udev_msg *pm = (void *)_pm;
	struct usb_device *udev = pm->udev;

	/* Change lock */
	USB_BUS_UNLOCK(udev->bus);
	USB_MTX_LOCK(&udev->device_mtx);

	/* Start clear stall callback */
	usbd_transfer_start(udev->ctrl_xfer[1]);

	/* Change lock */
	USB_MTX_UNLOCK(&udev->device_mtx);
	USB_BUS_LOCK(udev->bus);
}

/*------------------------------------------------------------------------*
 *      usb_get_langid
 *
 * This function tries to figure out the USB string language to use.
 *------------------------------------------------------------------------*/
void
usb_get_langid(struct usb_device *udev)
{
	uint8_t *scratch_ptr;
	uint8_t do_unlock;
	int err;

	/*
	 * Workaround for buggy USB devices.
	 *
	 * It appears that some string-less USB chips will crash and
	 * disappear if any attempts are made to read any string
	 * descriptors.
	 *
	 * Try to detect such chips by checking the strings in the USB
	 * device descriptor. If no strings are present there we
	 * simply disable all USB strings.
	 */

	/* Protect scratch area */
	do_unlock = usbd_ctrl_lock(udev);

	scratch_ptr = udev->scratch.data;

	if (udev->flags.no_strings) {
		err = USB_ERR_INVAL;
	} else if (udev->ddesc.iManufacturer ||
	    udev->ddesc.iProduct ||
	    udev->ddesc.iSerialNumber) {
		/* read out the language ID string */
		err = usbd_req_get_string_desc(udev, NULL,
		    (char *)scratch_ptr, 4, 0, USB_LANGUAGE_TABLE);
	} else {
		err = USB_ERR_INVAL;
	}

	if (err || (scratch_ptr[0] < 4)) {
		udev->flags.no_strings = 1;
	} else {
		uint16_t langid;
		uint16_t pref;
		uint16_t mask;
		uint8_t x;

		/* load preferred value and mask */
		pref = usb_lang_id;
		mask = usb_lang_mask;

		/* align length correctly */
		scratch_ptr[0] &= ~1U;

		/* fix compiler warning */
		langid = 0;

		/* search for preferred language */
		for (x = 2; x < scratch_ptr[0]; x += 2) {
			langid = UGETW(scratch_ptr + x);
			if ((langid & mask) == pref)
				break;
		}
		if (x >= scratch_ptr[0]) {
			/* pick the first language as the default */
			DPRINTFN(1, "Using first language\n");
			langid = UGETW(scratch_ptr + 2);
		}

		DPRINTFN(1, "Language selected: 0x%04x\n", langid);
		udev->langid = langid;
	}

	if (do_unlock)
		usbd_ctrl_unlock(udev);
}

/*------------------------------------------------------------------------*
 *	usb_alloc_device
 *
 * This function allocates a new USB device. This function is called
 * when a new device has been put in the powered state, but not yet in
 * the addressed state. Get initial descriptor, set the address, get
 * full descriptor and get strings.
 *
 * Return values:
 *    0: Failure
 * Else: Success
 *------------------------------------------------------------------------*/
struct usb_device *
usb_alloc_device(device_t parent_dev, struct usb_bus *bus,
    struct usb_device *parent_hub, uint8_t depth, uint8_t port_index,
    uint8_t port_no, enum usb_dev_speed speed, enum usb_hc_mode mode)
{
	struct usb_attach_arg uaa;
	struct usb_device *udev;
	struct usb_device *adev;
	struct usb_device *hub;
	usb_error_t err;
	uint8_t device_index;
	uint8_t config_index;
	uint8_t config_quirk;
	uint8_t set_config_failed;

	DPRINTF("parent_dev=%p, bus=%p, parent_hub=%p, depth=%u, "
	    "port_index=%u, port_no=%u, speed=%u, usb_mode=%u\n",
	    parent_dev, bus, parent_hub, depth, port_index, port_no,
	    speed, mode);

	/*
	 * Find an unused device index. In USB Host mode this is the
	 * same as the device address.
	 *
	 * Device index zero is not used and device index 1 should
	 * always be the root hub.
	 */
	for (device_index = USB_ROOT_HUB_ADDR;
	    (device_index != bus->devices_max) &&
	    (bus->devices[device_index] != NULL);
	    device_index++) /* nop */;

	if (device_index == bus->devices_max) {
		device_printf(bus->bdev,
		    "No free USB device index for new device\n");
		return (NULL);
	}

	if (depth > 0x10) {
		device_printf(bus->bdev,
		    "Invalid device depth\n");
		return (NULL);
	}
	udev = malloc(sizeof(*udev), M_USB, M_WAITOK | M_ZERO);
#if (USB_HAVE_MALLOC_WAITOK == 0)
	if (udev == NULL) {
		return (NULL);
	}
#endif
	/* initialise our SX-lock */
	sx_init_flags(&udev->enum_sx, "USB config SX lock", SX_DUPOK);
	sx_init_flags(&udev->sr_sx, "USB suspend and resume SX lock", SX_NOWITNESS);
	sx_init_flags(&udev->ctrl_sx, "USB control transfer SX lock", SX_DUPOK);

	cv_init(&udev->ctrlreq_cv, "WCTRL");
	cv_init(&udev->ref_cv, "UGONE");

	/* initialise our mutex */
	mtx_init(&udev->device_mtx, "USB device mutex", NULL, MTX_DEF);

	/* initialise generic clear stall */
	udev->cs_msg[0].hdr.pm_callback = &usbd_clear_stall_proc;
	udev->cs_msg[0].udev = udev;
	udev->cs_msg[1].hdr.pm_callback = &usbd_clear_stall_proc;
	udev->cs_msg[1].udev = udev;

	/* initialise some USB device fields */
	udev->parent_hub = parent_hub;
	udev->parent_dev = parent_dev;
	udev->port_index = port_index;
	udev->port_no = port_no;
	udev->depth = depth;
	udev->bus = bus;
	udev->address = USB_START_ADDR;	/* default value */
	udev->plugtime = (usb_ticks_t)ticks;
	/*
	 * We need to force the power mode to "on" because there are plenty
	 * of USB devices out there that do not work very well with
	 * automatic suspend and resume!
	 */
	udev->power_mode = usbd_filter_power_mode(udev, USB_POWER_MODE_ON);
	udev->pwr_save.last_xfer_time = ticks;
	/* we are not ready yet */
	udev->refcount = 1;

	/* set up default endpoint descriptor */
	udev->ctrl_ep_desc.bLength = sizeof(udev->ctrl_ep_desc);
	udev->ctrl_ep_desc.bDescriptorType = UDESC_ENDPOINT;
	udev->ctrl_ep_desc.bEndpointAddress = USB_CONTROL_ENDPOINT;
	udev->ctrl_ep_desc.bmAttributes = UE_CONTROL;
	udev->ctrl_ep_desc.wMaxPacketSize[0] = USB_MAX_IPACKET;
	udev->ctrl_ep_desc.wMaxPacketSize[1] = 0;
	udev->ctrl_ep_desc.bInterval = 0;

	/* set up default endpoint companion descriptor */
	udev->ctrl_ep_comp_desc.bLength = sizeof(udev->ctrl_ep_comp_desc);
	udev->ctrl_ep_comp_desc.bDescriptorType = UDESC_ENDPOINT_SS_COMP;

	udev->ddesc.bMaxPacketSize = USB_MAX_IPACKET;

	udev->speed = speed;
	udev->flags.usb_mode = mode;

	/* search for our High Speed USB HUB, if any */

	adev = udev;
	hub = udev->parent_hub;

	while (hub) {
		if (hub->speed == USB_SPEED_HIGH) {
			udev->hs_hub_addr = hub->address;
			udev->parent_hs_hub = hub;
			udev->hs_port_no = adev->port_no;
			break;
		}
		adev = hub;
		hub = hub->parent_hub;
	}

	/* init the default endpoint */
	usb_init_endpoint(udev, 0,
	    &udev->ctrl_ep_desc,
	    &udev->ctrl_ep_comp_desc,
	    &udev->ctrl_ep);

	/* set device index */
	udev->device_index = device_index;

#if USB_HAVE_UGEN
	/* Create ugen name */
	snprintf(udev->ugen_name, sizeof(udev->ugen_name),
	    USB_GENERIC_NAME "%u.%u", device_get_unit(bus->bdev),
	    device_index);
	LIST_INIT(&udev->pd_list);

	/* Create the control endpoint device */
	udev->ctrl_dev = usb_make_dev(udev, NULL, 0, 0,
	    FREAD|FWRITE, UID_ROOT, GID_OPERATOR, 0600);

	/* Create a link from /dev/ugenX.X to the default endpoint */
	if (udev->ctrl_dev != NULL)
		make_dev_alias(udev->ctrl_dev->cdev, "%s", udev->ugen_name);
#endif
	/* Initialise device */
	if (bus->methods->device_init != NULL) {
		err = (bus->methods->device_init) (udev);
		if (err != 0) {
			DPRINTFN(0, "device init %d failed "
			    "(%s, ignored)\n", device_index, 
			    usbd_errstr(err));
			goto done;
		}
	}
	/* set powered device state after device init is complete */
	usb_set_device_state(udev, USB_STATE_POWERED);

	if (udev->flags.usb_mode == USB_MODE_HOST) {
		err = usbd_req_set_address(udev, NULL, device_index);

		/*
		 * This is the new USB device address from now on, if
		 * the set address request didn't set it already.
		 */
		if (udev->address == USB_START_ADDR)
			udev->address = device_index;

		/*
		 * We ignore any set-address errors, hence there are
		 * buggy USB devices out there that actually receive
		 * the SETUP PID, but manage to set the address before
		 * the STATUS stage is ACK'ed. If the device responds
		 * to the subsequent get-descriptor at the new
		 * address, then we know that the set-address command
		 * was successful.
		 */
		if (err) {
			DPRINTFN(0, "set address %d failed "
			    "(%s, ignored)\n", udev->address, 
			    usbd_errstr(err));
		}
	} else {
		/* We are not self powered */
		udev->flags.self_powered = 0;

		/* Set unconfigured state */
		udev->curr_config_no = USB_UNCONFIG_NO;
		udev->curr_config_index = USB_UNCONFIG_INDEX;

		/* Setup USB descriptors */
		err = (usb_temp_setup_by_index_p) (udev, usb_template);
		if (err) {
			DPRINTFN(0, "setting up USB template failed - "
			    "usb_template(4) not loaded?\n");
			goto done;
		}
	}
	usb_set_device_state(udev, USB_STATE_ADDRESSED);

	/* setup the device descriptor and the initial "wMaxPacketSize" */
	err = usbd_setup_device_desc(udev, NULL);

	if (err != 0) {
		/* try to enumerate two more times */
		err = usbd_req_re_enumerate(udev, NULL);
		if (err != 0) {
			err = usbd_req_re_enumerate(udev, NULL);
			if (err != 0) {
				goto done;
			}
		}
	}

	/*
	 * Setup temporary USB attach args so that we can figure out some
	 * basic quirks for this device.
	 */
	usb_init_attach_arg(udev, &uaa);

	if (usb_test_quirk(&uaa, UQ_BUS_POWERED)) {
		udev->flags.uq_bus_powered = 1;
	}
	if (usb_test_quirk(&uaa, UQ_NO_STRINGS)) {
		udev->flags.no_strings = 1;
	}

	usb_get_langid(udev);

	/* assume 100mA bus powered for now. Changed when configured. */
	udev->power = USB_MIN_POWER;
	/* fetch the vendor and product strings from the device */
	usb_set_device_strings(udev);

	if (udev->flags.usb_mode == USB_MODE_DEVICE) {
		/* USB device mode setup is complete */
		err = 0;
		goto config_done;
	}

	/*
	 * Most USB devices should attach to config index 0 by
	 * default
	 */
	if (usb_test_quirk(&uaa, UQ_CFG_INDEX_0)) {
		config_index = 0;
		config_quirk = 1;
	} else if (usb_test_quirk(&uaa, UQ_CFG_INDEX_1)) {
		config_index = 1;
		config_quirk = 1;
	} else if (usb_test_quirk(&uaa, UQ_CFG_INDEX_2)) {
		config_index = 2;
		config_quirk = 1;
	} else if (usb_test_quirk(&uaa, UQ_CFG_INDEX_3)) {
		config_index = 3;
		config_quirk = 1;
	} else if (usb_test_quirk(&uaa, UQ_CFG_INDEX_4)) {
		config_index = 4;
		config_quirk = 1;
	} else {
		config_index = 0;
		config_quirk = 0;
	}

	set_config_failed = 0;
repeat_set_config:

	DPRINTF("setting config %u\n", config_index);

	/* get the USB device configured */
	err = usbd_set_config_index(udev, config_index);
	if (err) {
		if (udev->ddesc.bNumConfigurations != 0) {
			if (!set_config_failed) {
				set_config_failed = 1;
				/* XXX try to re-enumerate the device */
				err = usbd_req_re_enumerate(udev, NULL);
				if (err == 0)
					goto repeat_set_config;
			}
			DPRINTFN(0, "Failure selecting configuration index %u:"
			    "%s, port %u, addr %u (ignored)\n",
			    config_index, usbd_errstr(err), udev->port_no,
			    udev->address);
		}
		/*
		 * Some USB devices do not have any configurations. Ignore any
		 * set config failures!
		 */
		err = 0;
		goto config_done;
	}
	if (!config_quirk && config_index + 1 < udev->ddesc.bNumConfigurations) {
		if ((udev->cdesc->bNumInterface < 2) &&
		    usbd_get_no_descriptors(udev->cdesc, UDESC_ENDPOINT) == 0) {
			DPRINTFN(0, "Found no endpoints, trying next config\n");
			config_index++;
			goto repeat_set_config;
		}
#if USB_HAVE_MSCTEST
		if (config_index == 0) {
			/*
			 * Try to figure out if we have an
			 * auto-install disk there:
			 */
			if (usb_iface_is_cdrom(udev, 0)) {
				DPRINTFN(0, "Found possible auto-install "
				    "disk (trying next config)\n");
				config_index++;
				goto repeat_set_config;
			}
		}
#endif
	}
#if USB_HAVE_MSCTEST
	if (set_config_failed == 0 && config_index == 0 &&
	    usb_test_quirk(&uaa, UQ_MSC_NO_SYNC_CACHE) == 0 &&
	    usb_test_quirk(&uaa, UQ_MSC_NO_GETMAXLUN) == 0) {
		/*
		 * Try to figure out if there are any MSC quirks we
		 * should apply automatically:
		 */
		err = usb_msc_auto_quirk(udev, 0);

		if (err != 0) {
			set_config_failed = 1;
			goto repeat_set_config;
		}
	}
#endif

config_done:
	DPRINTF("new dev (addr %d), udev=%p, parent_hub=%p\n",
	    udev->address, udev, udev->parent_hub);

	/* register our device - we are ready */
	usb_bus_port_set_device(bus, parent_hub ?
	    parent_hub->hub->ports + port_index : NULL, udev, device_index);

#if USB_HAVE_UGEN
	/* Symlink the ugen device name */
	udev->ugen_symlink = usb_alloc_symlink(udev->ugen_name);

	/* Announce device */
	printf("%s: <%s %s> at %s\n", udev->ugen_name,
	    usb_get_manufacturer(udev), usb_get_product(udev),
	    device_get_nameunit(udev->bus->bdev));
#endif

#if USB_HAVE_DEVCTL
	usb_notify_addq("ATTACH", udev);
#endif
done:
	if (err) {
		/*
		 * Free USB device and all subdevices, if any.
		 */
		usb_free_device(udev, 0);
		udev = NULL;
	}
	return (udev);
}

#if USB_HAVE_UGEN
struct usb_fs_privdata *
usb_make_dev(struct usb_device *udev, const char *devname, int ep,
    int fi, int rwmode, uid_t uid, gid_t gid, int mode)
{
	struct usb_fs_privdata* pd;
	struct make_dev_args args;
	char buffer[32];

	/* Store information to locate ourselves again later */
	pd = malloc(sizeof(struct usb_fs_privdata), M_USBDEV,
	    M_WAITOK | M_ZERO);
	pd->bus_index = device_get_unit(udev->bus->bdev);
	pd->dev_index = udev->device_index;
	pd->ep_addr = ep;
	pd->fifo_index = fi;
	pd->mode = rwmode;

	/* Now, create the device itself */
	if (devname == NULL) {
		devname = buffer;
		snprintf(buffer, sizeof(buffer), USB_DEVICE_DIR "/%u.%u.%u",
		    pd->bus_index, pd->dev_index, pd->ep_addr);
	}

	/* Setup arguments for make_dev_s() */
	make_dev_args_init(&args);
	args.mda_devsw = &usb_devsw;
	args.mda_uid = uid;
	args.mda_gid = gid;
	args.mda_mode = mode;
	args.mda_si_drv1 = pd;

	if (make_dev_s(&args, &pd->cdev, "%s", devname) != 0) {
		DPRINTFN(0, "Failed to create device %s\n", devname);
		free(pd, M_USBDEV);
		return (NULL);
	}
	return (pd);
}

void
usb_destroy_dev_sync(struct usb_fs_privdata *pd)
{
	DPRINTFN(1, "Destroying device at ugen%d.%d\n",
	    pd->bus_index, pd->dev_index);

	/*
	 * Destroy character device synchronously. After this
	 * all system calls are returned. Can block.
	 */
	destroy_dev(pd->cdev);

	free(pd, M_USBDEV);
}

void
usb_destroy_dev(struct usb_fs_privdata *pd)
{
	struct usb_bus *bus;

	if (pd == NULL)
		return;

	mtx_lock(&usb_ref_lock);
	bus = devclass_get_softc(usb_devclass_ptr, pd->bus_index);
	mtx_unlock(&usb_ref_lock);

	if (bus == NULL) {
		usb_destroy_dev_sync(pd);
		return;
	}

	/* make sure we can re-use the device name */
	delist_dev(pd->cdev);

	USB_BUS_LOCK(bus);
	LIST_INSERT_HEAD(&bus->pd_cleanup_list, pd, pd_next);
	/* get cleanup going */
	usb_proc_msignal(USB_BUS_EXPLORE_PROC(bus),
	    &bus->cleanup_msg[0], &bus->cleanup_msg[1]);
	USB_BUS_UNLOCK(bus);
}

static void
usb_cdev_create(struct usb_device *udev)
{
	struct usb_config_descriptor *cd;
	struct usb_endpoint_descriptor *ed;
	struct usb_descriptor *desc;
	struct usb_fs_privdata* pd;
	int inmode, outmode, inmask, outmask, mode;
	uint8_t ep;

	KASSERT(LIST_FIRST(&udev->pd_list) == NULL, ("stale cdev entries"));

	DPRINTFN(2, "Creating device nodes\n");

	if (usbd_get_mode(udev) == USB_MODE_DEVICE) {
		inmode = FWRITE;
		outmode = FREAD;
	} else {		 /* USB_MODE_HOST */
		inmode = FREAD;
		outmode = FWRITE;
	}

	inmask = 0;
	outmask = 0;
	desc = NULL;

	/*
	 * Collect all used endpoint numbers instead of just
	 * generating 16 static endpoints.
	 */
	cd = usbd_get_config_descriptor(udev);
	while ((desc = usb_desc_foreach(cd, desc))) {
		/* filter out all endpoint descriptors */
		if ((desc->bDescriptorType == UDESC_ENDPOINT) &&
		    (desc->bLength >= sizeof(*ed))) {
			ed = (struct usb_endpoint_descriptor *)desc;

			/* update masks */
			ep = ed->bEndpointAddress;
			if (UE_GET_DIR(ep)  == UE_DIR_OUT)
				outmask |= 1 << UE_GET_ADDR(ep);
			else
				inmask |= 1 << UE_GET_ADDR(ep);
		}
	}

	/* Create all available endpoints except EP0 */
	for (ep = 1; ep < 16; ep++) {
		mode = (inmask & (1 << ep)) ? inmode : 0;
		mode |= (outmask & (1 << ep)) ? outmode : 0;
		if (mode == 0)
			continue;	/* no IN or OUT endpoint */

		pd = usb_make_dev(udev, NULL, ep, 0,
		    mode, UID_ROOT, GID_OPERATOR, 0600);

		if (pd != NULL)
			LIST_INSERT_HEAD(&udev->pd_list, pd, pd_next);
	}
}

static void
usb_cdev_free(struct usb_device *udev)
{
	struct usb_fs_privdata* pd;

	DPRINTFN(2, "Freeing device nodes\n");

	while ((pd = LIST_FIRST(&udev->pd_list)) != NULL) {
		KASSERT(pd->cdev->si_drv1 == pd, ("privdata corrupt"));

		LIST_REMOVE(pd, pd_next);

		usb_destroy_dev(pd);
	}
}
#endif

/*------------------------------------------------------------------------*
 *	usb_free_device
 *
 * This function is NULL safe and will free an USB device and its
 * children devices, if any.
 *
 * Flag values: Reserved, set to zero.
 *------------------------------------------------------------------------*/
void
usb_free_device(struct usb_device *udev, uint8_t flag)
{
	struct usb_bus *bus;

	if (udev == NULL)
		return;		/* already freed */

	DPRINTFN(4, "udev=%p port=%d\n", udev, udev->port_no);

	bus = udev->bus;

	/* set DETACHED state to prevent any further references */
	usb_set_device_state(udev, USB_STATE_DETACHED);

#if USB_HAVE_DEVCTL
	usb_notify_addq("DETACH", udev);
#endif

#if USB_HAVE_UGEN
	if (!rebooting) {
		printf("%s: <%s %s> at %s (disconnected)\n", udev->ugen_name,
		    usb_get_manufacturer(udev), usb_get_product(udev),
		    device_get_nameunit(bus->bdev));
	}

	/* Destroy UGEN symlink, if any */
	if (udev->ugen_symlink) {
		usb_free_symlink(udev->ugen_symlink);
		udev->ugen_symlink = NULL;
	}

	usb_destroy_dev(udev->ctrl_dev);
#endif

	if (udev->flags.usb_mode == USB_MODE_DEVICE) {
		/* stop receiving any control transfers (Device Side Mode) */
		usbd_transfer_unsetup(udev->ctrl_xfer, USB_CTRL_XFER_MAX);
	}

	/* the following will get the device unconfigured in software */
	usb_unconfigure(udev, USB_UNCFG_FLAG_FREE_EP0);

	/* final device unregister after all character devices are closed */
	usb_bus_port_set_device(bus, udev->parent_hub ?
	    udev->parent_hub->hub->ports + udev->port_index : NULL,
	    NULL, USB_ROOT_HUB_ADDR);

	/* unsetup any leftover default USB transfers */
	usbd_transfer_unsetup(udev->ctrl_xfer, USB_CTRL_XFER_MAX);

	/* template unsetup, if any */
	(usb_temp_unsetup_p) (udev);

	/* 
	 * Make sure that our clear-stall messages are not queued
	 * anywhere:
	 */
	USB_BUS_LOCK(udev->bus);
	usb_proc_mwait(USB_BUS_CS_PROC(udev->bus),
	    &udev->cs_msg[0], &udev->cs_msg[1]);
	USB_BUS_UNLOCK(udev->bus);

	/* wait for all references to go away */
	usb_wait_pending_refs(udev);

	sx_destroy(&udev->enum_sx);
	sx_destroy(&udev->sr_sx);
	sx_destroy(&udev->ctrl_sx);

	cv_destroy(&udev->ctrlreq_cv);
	cv_destroy(&udev->ref_cv);

	mtx_destroy(&udev->device_mtx);
#if USB_HAVE_UGEN
	KASSERT(LIST_FIRST(&udev->pd_list) == NULL, ("leaked cdev entries"));
#endif

	/* Uninitialise device */
	if (bus->methods->device_uninit != NULL)
		(bus->methods->device_uninit) (udev);

	/* free device */
	free(udev->serial, M_USB);
	free(udev->manufacturer, M_USB);
	free(udev->product, M_USB);
	free(udev, M_USB);
}

/*------------------------------------------------------------------------*
 *	usbd_get_iface
 *
 * This function is the safe way to get the USB interface structure
 * pointer by interface index.
 *
 * Return values:
 *   NULL: Interface not present.
 *   Else: Pointer to USB interface structure.
 *------------------------------------------------------------------------*/
struct usb_interface *
usbd_get_iface(struct usb_device *udev, uint8_t iface_index)
{
	struct usb_interface *iface = udev->ifaces + iface_index;

	if (iface_index >= udev->ifaces_max)
		return (NULL);
	return (iface);
}

/*------------------------------------------------------------------------*
 *	usbd_find_descriptor
 *
 * This function will lookup the first descriptor that matches the
 * criteria given by the arguments "type" and "subtype". Descriptors
 * will only be searched within the interface having the index
 * "iface_index".  If the "id" argument points to an USB descriptor,
 * it will be skipped before the search is started. This allows
 * searching for multiple descriptors using the same criteria. Else
 * the search is started after the interface descriptor.
 *
 * Return values:
 *   NULL: End of descriptors
 *   Else: A descriptor matching the criteria
 *------------------------------------------------------------------------*/
void   *
usbd_find_descriptor(struct usb_device *udev, void *id, uint8_t iface_index,
    uint8_t type, uint8_t type_mask,
    uint8_t subtype, uint8_t subtype_mask)
{
	struct usb_descriptor *desc;
	struct usb_config_descriptor *cd;
	struct usb_interface *iface;

	cd = usbd_get_config_descriptor(udev);
	if (cd == NULL) {
		return (NULL);
	}
	if (id == NULL) {
		iface = usbd_get_iface(udev, iface_index);
		if (iface == NULL) {
			return (NULL);
		}
		id = usbd_get_interface_descriptor(iface);
		if (id == NULL) {
			return (NULL);
		}
	}
	desc = (void *)id;

	while ((desc = usb_desc_foreach(cd, desc))) {
		if (desc->bDescriptorType == UDESC_INTERFACE) {
			break;
		}
		if (((desc->bDescriptorType & type_mask) == type) &&
		    ((desc->bDescriptorSubtype & subtype_mask) == subtype)) {
			return (desc);
		}
	}
	return (NULL);
}

/*------------------------------------------------------------------------*
 *	usb_devinfo
 *
 * This function will dump information from the device descriptor
 * belonging to the USB device pointed to by "udev", to the string
 * pointed to by "dst_ptr" having a maximum length of "dst_len" bytes
 * including the terminating zero.
 *------------------------------------------------------------------------*/
void
usb_devinfo(struct usb_device *udev, char *dst_ptr, uint16_t dst_len)
{
	struct usb_device_descriptor *udd = &udev->ddesc;
	uint16_t bcdDevice;
	uint16_t bcdUSB;

	bcdUSB = UGETW(udd->bcdUSB);
	bcdDevice = UGETW(udd->bcdDevice);

	if (udd->bDeviceClass != 0xFF) {
		snprintf(dst_ptr, dst_len, "%s %s, class %d/%d, rev %x.%02x/"
		    "%x.%02x, addr %d",
		    usb_get_manufacturer(udev),
		    usb_get_product(udev),
		    udd->bDeviceClass, udd->bDeviceSubClass,
		    (bcdUSB >> 8), bcdUSB & 0xFF,
		    (bcdDevice >> 8), bcdDevice & 0xFF,
		    udev->address);
	} else {
		snprintf(dst_ptr, dst_len, "%s %s, rev %x.%02x/"
		    "%x.%02x, addr %d",
		    usb_get_manufacturer(udev),
		    usb_get_product(udev),
		    (bcdUSB >> 8), bcdUSB & 0xFF,
		    (bcdDevice >> 8), bcdDevice & 0xFF,
		    udev->address);
	}
}

#ifdef USB_VERBOSE
/*
 * Descriptions of of known vendors and devices ("products").
 */
struct usb_knowndev {
	uint16_t vendor;
	uint16_t product;
	uint32_t flags;
	const char *vendorname;
	const char *productname;
};

#define	USB_KNOWNDEV_NOPROD	0x01	/* match on vendor only */

#include "usbdevs.h"
#include "usbdevs_data.h"
#endif					/* USB_VERBOSE */

void
usb_set_device_strings(struct usb_device *udev)
{
	struct usb_device_descriptor *udd = &udev->ddesc;
#ifdef USB_VERBOSE
	const struct usb_knowndev *kdp;
#endif
	char *temp_ptr;
	size_t temp_size;
	uint16_t vendor_id;
	uint16_t product_id;
	uint8_t do_unlock;

	/* Protect scratch area */
	do_unlock = usbd_ctrl_lock(udev);

	temp_ptr = (char *)udev->scratch.data;
	temp_size = sizeof(udev->scratch.data);

	vendor_id = UGETW(udd->idVendor);
	product_id = UGETW(udd->idProduct);

	/* cleanup old strings, if any */
	free(udev->serial, M_USB);
	free(udev->manufacturer, M_USB);
	free(udev->product, M_USB);

	/* zero the string pointers */
	udev->serial = NULL;
	udev->manufacturer = NULL;
	udev->product = NULL;

	/* get serial number string */
	usbd_req_get_string_any(udev, NULL, temp_ptr, temp_size,
	    udev->ddesc.iSerialNumber);
	udev->serial = strdup(temp_ptr, M_USB);

	/* get manufacturer string */
	usbd_req_get_string_any(udev, NULL, temp_ptr, temp_size,
	    udev->ddesc.iManufacturer);
	usb_trim_spaces(temp_ptr);
	if (temp_ptr[0] != '\0')
		udev->manufacturer = strdup(temp_ptr, M_USB);

	/* get product string */
	usbd_req_get_string_any(udev, NULL, temp_ptr, temp_size,
	    udev->ddesc.iProduct);
	usb_trim_spaces(temp_ptr);
	if (temp_ptr[0] != '\0')
		udev->product = strdup(temp_ptr, M_USB);

#ifdef USB_VERBOSE
	if (udev->manufacturer == NULL || udev->product == NULL) {
		for (kdp = usb_knowndevs; kdp->vendorname != NULL; kdp++) {
			if (kdp->vendor == vendor_id &&
			    (kdp->product == product_id ||
			    (kdp->flags & USB_KNOWNDEV_NOPROD) != 0))
				break;
		}
		if (kdp->vendorname != NULL) {
			/* XXX should use pointer to knowndevs string */
			if (udev->manufacturer == NULL) {
				udev->manufacturer = strdup(kdp->vendorname,
				    M_USB);
			}
			if (udev->product == NULL &&
			    (kdp->flags & USB_KNOWNDEV_NOPROD) == 0) {
				udev->product = strdup(kdp->productname,
				    M_USB);
			}
		}
	}
#endif
	/* Provide default strings if none were found */
	if (udev->manufacturer == NULL) {
		snprintf(temp_ptr, temp_size, "vendor 0x%04x", vendor_id);
		udev->manufacturer = strdup(temp_ptr, M_USB);
	}
	if (udev->product == NULL) {
		snprintf(temp_ptr, temp_size, "product 0x%04x", product_id);
		udev->product = strdup(temp_ptr, M_USB);
	}

	if (do_unlock)
		usbd_ctrl_unlock(udev);
}

/*
 * Returns:
 * See: USB_MODE_XXX
 */
enum usb_hc_mode
usbd_get_mode(struct usb_device *udev)
{
	return (udev->flags.usb_mode);
}

/*
 * Returns:
 * See: USB_SPEED_XXX
 */
enum usb_dev_speed
usbd_get_speed(struct usb_device *udev)
{
	return (udev->speed);
}

uint32_t
usbd_get_isoc_fps(struct usb_device *udev)
{
	;				/* indent fix */
	switch (udev->speed) {
	case USB_SPEED_LOW:
	case USB_SPEED_FULL:
		return (1000);
	default:
		return (8000);
	}
}

struct usb_device_descriptor *
usbd_get_device_descriptor(struct usb_device *udev)
{
	if (udev == NULL)
		return (NULL);		/* be NULL safe */
	return (&udev->ddesc);
}

struct usb_config_descriptor *
usbd_get_config_descriptor(struct usb_device *udev)
{
	if (udev == NULL)
		return (NULL);		/* be NULL safe */
	return (udev->cdesc);
}

/*------------------------------------------------------------------------*
 *	usb_test_quirk - test a device for a given quirk
 *
 * Return values:
 * 0: The USB device does not have the given quirk.
 * Else: The USB device has the given quirk.
 *------------------------------------------------------------------------*/
uint8_t
usb_test_quirk(const struct usb_attach_arg *uaa, uint16_t quirk)
{
	uint8_t found;
	uint8_t x;

	if (quirk == UQ_NONE)
		return (0);

	/* search the automatic per device quirks first */

	for (x = 0; x != USB_MAX_AUTO_QUIRK; x++) {
		if (uaa->device->autoQuirk[x] == quirk)
			return (1);
	}

	/* search global quirk table, if any */

	found = (usb_test_quirk_p) (&uaa->info, quirk);

	return (found);
}

struct usb_interface_descriptor *
usbd_get_interface_descriptor(struct usb_interface *iface)
{
	if (iface == NULL)
		return (NULL);		/* be NULL safe */
	return (iface->idesc);
}

uint8_t
usbd_get_interface_altindex(struct usb_interface *iface)
{
	return (iface->alt_index);
}

uint8_t
usbd_get_bus_index(struct usb_device *udev)
{
	return ((uint8_t)device_get_unit(udev->bus->bdev));
}

uint8_t
usbd_get_device_index(struct usb_device *udev)
{
	return (udev->device_index);
}

#if USB_HAVE_DEVCTL
static void
usb_notify_addq(const char *type, struct usb_device *udev)
{
	struct usb_interface *iface;
	struct sbuf *sb;
	int i;

	/* announce the device */
	sb = sbuf_new_auto();
	sbuf_printf(sb,
#if USB_HAVE_UGEN
	    "ugen=%s "
	    "cdev=%s "
#endif
	    "vendor=0x%04x "
	    "product=0x%04x "
	    "devclass=0x%02x "
	    "devsubclass=0x%02x "
	    "sernum=\"%s\" "
	    "release=0x%04x "
	    "mode=%s "
	    "port=%u "
#if USB_HAVE_UGEN
	    "parent=%s"
#endif
	    "",
#if USB_HAVE_UGEN
	    udev->ugen_name,
	    udev->ugen_name,
#endif
	    UGETW(udev->ddesc.idVendor),
	    UGETW(udev->ddesc.idProduct),
	    udev->ddesc.bDeviceClass,
	    udev->ddesc.bDeviceSubClass,
	    usb_get_serial(udev),
	    UGETW(udev->ddesc.bcdDevice),
	    (udev->flags.usb_mode == USB_MODE_HOST) ? "host" : "device",
	    udev->port_no
#if USB_HAVE_UGEN
	    , udev->parent_hub != NULL ?
		udev->parent_hub->ugen_name :
		device_get_nameunit(device_get_parent(udev->bus->bdev))
#endif
	    );
	sbuf_finish(sb);
	devctl_notify("USB", "DEVICE", type, sbuf_data(sb));
	sbuf_delete(sb);

	/* announce each interface */
	for (i = 0; i < USB_IFACE_MAX; i++) {
		iface = usbd_get_iface(udev, i);
		if (iface == NULL)
			break;		/* end of interfaces */
		if (iface->idesc == NULL)
			continue;	/* no interface descriptor */

		sb = sbuf_new_auto();
		sbuf_printf(sb,
#if USB_HAVE_UGEN
		    "ugen=%s "
		    "cdev=%s "
#endif
		    "vendor=0x%04x "
		    "product=0x%04x "
		    "devclass=0x%02x "
		    "devsubclass=0x%02x "
		    "sernum=\"%s\" "
		    "release=0x%04x "
		    "mode=%s "
		    "interface=%d "
		    "endpoints=%d "
		    "intclass=0x%02x "
		    "intsubclass=0x%02x "
		    "intprotocol=0x%02x",
#if USB_HAVE_UGEN
		    udev->ugen_name,
		    udev->ugen_name,
#endif
		    UGETW(udev->ddesc.idVendor),
		    UGETW(udev->ddesc.idProduct),
		    udev->ddesc.bDeviceClass,
		    udev->ddesc.bDeviceSubClass,
		    usb_get_serial(udev),
		    UGETW(udev->ddesc.bcdDevice),
		    (udev->flags.usb_mode == USB_MODE_HOST) ? "host" : "device",
		    iface->idesc->bInterfaceNumber,
		    iface->idesc->bNumEndpoints,
		    iface->idesc->bInterfaceClass,
		    iface->idesc->bInterfaceSubClass,
		    iface->idesc->bInterfaceProtocol);
		sbuf_finish(sb);
		devctl_notify("USB", "INTERFACE", type, sbuf_data(sb));
		sbuf_delete(sb);
	}
}
#endif

#if USB_HAVE_UGEN
/*------------------------------------------------------------------------*
 *	usb_fifo_free_wrap
 *
 * This function will free the FIFOs.
 *
 * Description of "flag" argument: If the USB_UNCFG_FLAG_FREE_EP0 flag
 * is set and "iface_index" is set to "USB_IFACE_INDEX_ANY", we free
 * all FIFOs. If the USB_UNCFG_FLAG_FREE_EP0 flag is not set and
 * "iface_index" is set to "USB_IFACE_INDEX_ANY", we free all non
 * control endpoint FIFOs. If "iface_index" is not set to
 * "USB_IFACE_INDEX_ANY" the flag has no effect.
 *------------------------------------------------------------------------*/
static void
usb_fifo_free_wrap(struct usb_device *udev,
    uint8_t iface_index, uint8_t flag)
{
	struct usb_fifo *f;
	uint16_t i;

	/*
	 * Free any USB FIFOs on the given interface:
	 */
	for (i = 0; i != USB_FIFO_MAX; i++) {
		f = udev->fifo[i];
		if (f == NULL) {
			continue;
		}
		/* Check if the interface index matches */
		if (iface_index == f->iface_index) {
			if (f->methods != &usb_ugen_methods) {
				/*
				 * Don't free any non-generic FIFOs in
				 * this case.
				 */
				continue;
			}
			if ((f->dev_ep_index == 0) &&
			    (f->fs_xfer == NULL)) {
				/* no need to free this FIFO */
				continue;
			}
		} else if (iface_index == USB_IFACE_INDEX_ANY) {
			if ((f->methods == &usb_ugen_methods) &&
			    (f->dev_ep_index == 0) &&
			    (!(flag & USB_UNCFG_FLAG_FREE_EP0)) &&
			    (f->fs_xfer == NULL)) {
				/* no need to free this FIFO */
				continue;
			}
		} else {
			/* no need to free this FIFO */
			continue;
		}
		/* free this FIFO */
		usb_fifo_free(f);
	}
}
#endif

/*------------------------------------------------------------------------*
 *	usb_peer_can_wakeup
 *
 * Return values:
 * 0: Peer cannot do resume signalling.
 * Else: Peer can do resume signalling.
 *------------------------------------------------------------------------*/
uint8_t
usb_peer_can_wakeup(struct usb_device *udev)
{
	const struct usb_config_descriptor *cdp;

	cdp = udev->cdesc;
	if ((cdp != NULL) && (udev->flags.usb_mode == USB_MODE_HOST)) {
		return (cdp->bmAttributes & UC_REMOTE_WAKEUP);
	}
	return (0);			/* not supported */
}

void
usb_set_device_state(struct usb_device *udev, enum usb_dev_state state)
{

	KASSERT(state < USB_STATE_MAX, ("invalid udev state"));

	DPRINTF("udev %p state %s -> %s\n", udev,
	    usb_statestr(udev->state), usb_statestr(state));

#if USB_HAVE_UGEN
	mtx_lock(&usb_ref_lock);
#endif
	udev->state = state;
#if USB_HAVE_UGEN
	mtx_unlock(&usb_ref_lock);
#endif
	if (udev->bus->methods->device_state_change != NULL)
		(udev->bus->methods->device_state_change) (udev);
}

enum usb_dev_state
usb_get_device_state(struct usb_device *udev)
{
	if (udev == NULL)
		return (USB_STATE_DETACHED);
	return (udev->state);
}

uint8_t
usbd_device_attached(struct usb_device *udev)
{
	return (udev->state > USB_STATE_DETACHED);
}

/*
 * The following function locks enumerating the given USB device. If
 * the lock is already grabbed this function returns zero. Else a
 * a value of one is returned.
 */
uint8_t
usbd_enum_lock(struct usb_device *udev)
{
	if (sx_xlocked(&udev->enum_sx))
		return (0);

	sx_xlock(&udev->enum_sx);
	sx_xlock(&udev->sr_sx);
	/* 
	 * NEWBUS LOCK NOTE: We should check if any parent SX locks
	 * are locked before locking Giant. Else the lock can be
	 * locked multiple times.
	 */
	mtx_lock(&Giant);
	return (1);
}

#if USB_HAVE_UGEN
/*
 * This function is the same like usbd_enum_lock() except a value of
 * 255 is returned when a signal is pending:
 */
uint8_t
usbd_enum_lock_sig(struct usb_device *udev)
{
	if (sx_xlocked(&udev->enum_sx))
		return (0);
	if (sx_xlock_sig(&udev->enum_sx))
		return (255);
	if (sx_xlock_sig(&udev->sr_sx)) {
		sx_xunlock(&udev->enum_sx);
		return (255);
	}
	mtx_lock(&Giant);
	return (1);
}
#endif

/* The following function unlocks enumerating the given USB device. */

void
usbd_enum_unlock(struct usb_device *udev)
{
	mtx_unlock(&Giant);
	sx_xunlock(&udev->enum_sx);
	sx_xunlock(&udev->sr_sx);
}

/* The following function locks suspend and resume. */

void
usbd_sr_lock(struct usb_device *udev)
{
	sx_xlock(&udev->sr_sx);
	/* 
	 * NEWBUS LOCK NOTE: We should check if any parent SX locks
	 * are locked before locking Giant. Else the lock can be
	 * locked multiple times.
	 */
	mtx_lock(&Giant);
}

/* The following function unlocks suspend and resume. */

void
usbd_sr_unlock(struct usb_device *udev)
{
	mtx_unlock(&Giant);
	sx_xunlock(&udev->sr_sx);
}

/*
 * The following function checks the enumerating lock for the given
 * USB device.
 */

uint8_t
usbd_enum_is_locked(struct usb_device *udev)
{
	return (sx_xlocked(&udev->enum_sx));
}

/*
 * The following function is used to serialize access to USB control
 * transfers and the USB scratch area. If the lock is already grabbed
 * this function returns zero. Else a value of one is returned.
 */
uint8_t
usbd_ctrl_lock(struct usb_device *udev)
{
	if (sx_xlocked(&udev->ctrl_sx))
		return (0);
	sx_xlock(&udev->ctrl_sx);

	/*
	 * We need to allow suspend and resume at this point, else the
	 * control transfer will timeout if the device is suspended!
	 */
	if (usbd_enum_is_locked(udev))
		usbd_sr_unlock(udev);
	return (1);
}

void
usbd_ctrl_unlock(struct usb_device *udev)
{
	sx_xunlock(&udev->ctrl_sx);

	/*
	 * Restore the suspend and resume lock after we have unlocked
	 * the USB control transfer lock to avoid LOR:
	 */
	if (usbd_enum_is_locked(udev))
		usbd_sr_lock(udev);
}

/*
 * The following function is used to set the per-interface specific
 * plug and play information. The string referred to by the pnpinfo
 * argument can safely be freed after calling this function. The
 * pnpinfo of an interface will be reset at device detach or when
 * passing a NULL argument to this function. This function
 * returns zero on success, else a USB_ERR_XXX failure code.
 */

usb_error_t 
usbd_set_pnpinfo(struct usb_device *udev, uint8_t iface_index, const char *pnpinfo)
{
	struct usb_interface *iface;

	iface = usbd_get_iface(udev, iface_index);
	if (iface == NULL)
		return (USB_ERR_INVAL);

	if (iface->pnpinfo != NULL) {
		free(iface->pnpinfo, M_USBDEV);
		iface->pnpinfo = NULL;
	}

	if (pnpinfo == NULL || pnpinfo[0] == 0)
		return (0);		/* success */

	iface->pnpinfo = strdup(pnpinfo, M_USBDEV);
	if (iface->pnpinfo == NULL)
		return (USB_ERR_NOMEM);

	return (0);			/* success */
}

usb_error_t
usbd_add_dynamic_quirk(struct usb_device *udev, uint16_t quirk)
{
	uint8_t x;

	for (x = 0; x != USB_MAX_AUTO_QUIRK; x++) {
		if (udev->autoQuirk[x] == 0 ||
		    udev->autoQuirk[x] == quirk) {
			udev->autoQuirk[x] = quirk;
			return (0);	/* success */
		}
	}
	return (USB_ERR_NOMEM);
}

/*
 * The following function is used to select the endpoint mode. It
 * should not be called outside enumeration context.
 */

usb_error_t
usbd_set_endpoint_mode(struct usb_device *udev, struct usb_endpoint *ep,
    uint8_t ep_mode)
{   
	usb_error_t error;
	uint8_t do_unlock;

	/* Prevent re-enumeration */
	do_unlock = usbd_enum_lock(udev);

	if (udev->bus->methods->set_endpoint_mode != NULL) {
		error = (udev->bus->methods->set_endpoint_mode) (
		    udev, ep, ep_mode);
	} else if (ep_mode != USB_EP_MODE_DEFAULT) {
		error = USB_ERR_INVAL;
	} else {
		error = 0;
	}

	/* only set new mode regardless of error */
	ep->ep_mode = ep_mode;

	if (do_unlock)
		usbd_enum_unlock(udev);
	return (error);
}

uint8_t
usbd_get_endpoint_mode(struct usb_device *udev, struct usb_endpoint *ep)
{
	return (ep->ep_mode);
}