[4c6651]: drivers / usb / host / rvl-sthcd.c Maximize Restore History

Download this file

rvl-sthcd.c    2374 lines (1971 with data), 52.2 kB

   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
/*
* drivers/usb/host/rvl-sthcd.c
*
* USB Host Controller driver for the Nintendo Wii
* Copyright (C) 2008-2009 The GameCube Linux Team
* Copyright (C) 2008 Maarten ter Huurne
* Copyright (C) 2008,2009 Albert Herranz
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
*/
/*
*
* TODO
* - cleanup debuging mess
*
*/
#ifdef CONFIG_HIGHMEM
#error Sorry, this driver cannot currently work if HIGHMEM is y
#endif
#define DBG(fmt, arg...) drv_printk(KERN_DEBUG, fmt, ##arg)
#include <linux/device.h>
#include <linux/errno.h>
#include <linux/kernel.h>
#include <linux/kthread.h>
#include <linux/module.h>
#include <linux/of_platform.h>
#include <linux/scatterlist.h>
#include <linux/usb.h>
#include <asm/starlet.h>
#include "../core/hcd.h"
#include "../core/hub.h"
#define DRV_MODULE_NAME "rvl-sthcd"
#define DRV_DESCRIPTION "USB Host Controller driver for the Nintendo Wii"
#define DRV_AUTHOR "Maarten ter Huurne, " \
"Albert Herranz"
static char sthcd_driver_version[] = "0.4i";
#define drv_printk(level, format, arg...) \
printk(level DRV_MODULE_NAME ": " format , ## arg)
/* TODO: Use enum instead? */
#define STHCD_IOCTLV_CONTROLREQ 0
#define STHCD_IOCTLV_BULKREQ 1
#define STHCD_IOCTLV_INTRREQ 2
#define STHCD_IOCTL_SUSPENDDEVICE 5
#define STHCD_IOCTL_RESUMEDEVICE 6
#define STHCD_IOCTLV_GETDEVICELIST 12
#define STHCD_IOCTL_DEVICEREMOVALNOTIFY 26
#define STHCD_IOCTLV_DEVICEINSERTNOTIFY 27
/*
* The Nintendo Wii has only 2 external USB ports (plus 1 internal USB port),
* but the starlet API provides access to USB devices in a port independent
* way. This is true also for USB devices attached to external hubs.
*
* Our HCD model currently maps one starlet USB device to one HCD port, thus
* we need additional ports here.
*/
#define STHCD_MAX_DEVIDS 15
#define STHCD_MAX_PORTS STHCD_MAX_DEVIDS
/*
* We get error -7008 after performing large transfers.
* Using this arbitrary limit makes things work.
*/
#define STHCD_MAX_CHUNK_SIZE (2048)
#define STHCD_PORT_MAX_RESETS 2 /* maximum number of consecutive
* resets allowed for a port */
#define STHCD_RESCAN_INTERVAL 5 /* seconds */
#define starlet_ioh_sg_entry(sg, ptr) \
starlet_ioh_sg_set_buf((sg), (ptr), sizeof(*(ptr)))
struct sthcd_hcd;
struct sthcd_port;
struct sthcd_oh;
/*
* starlet USB device abstraction (udev).
*
*/
struct sthcd_udev {
u16 idVendor;
u16 idProduct;
int fd; /* starlet file descriptor */
u16 devnum; /* USB address set by kernel */
struct list_head node; /* in list of connected devices */
struct sthcd_oh *oh; /* parent Open Host controller */
struct list_head pep_list; /* list of private endpoints */
};
/*
* starlet USB device identifier.
*
*/
struct sthcd_devid {
u32 _unk1;
u16 idVendor;
u16 idProduct;
};
enum {
__STHCD_PORT_INUSE = 0,
__STHCD_PORT_DOOMED,
};
/*
* "Virtual" HCD USB port.
*
*/
struct sthcd_port {
unsigned long flags;
#define STHCD_PORT_INUSE (1 << __STHCD_PORT_INUSE)
#define STHCD_PORT_DOOMED (1 << __STHCD_PORT_DOOMED)
u32 status_change;
unsigned nr_resets;
struct sthcd_udev udev; /* one udev per port */
};
/*
* starlet Open Host controller abstraction (oh).
*
*/
struct sthcd_oh {
unsigned int index;
int fd; /* starlet file descriptor */
unsigned int max_devids;
struct sthcd_devid *new_devids;
struct sthcd_devid *devids;
unsigned int nr_devids; /* actual no of devices */
struct sthcd_hcd *hcd; /* parent Host Controller */
};
/*
* Host Controller (hcd).
*
*/
struct sthcd_hcd {
spinlock_t lock;
struct sthcd_oh oh[2];
struct sthcd_port *ports; /* array of ports */
unsigned int nr_ports;
struct list_head device_list; /* list of connected devices */
wait_queue_head_t rescan_waitq; /* wait queue for the rescan task */
struct task_struct *rescan_task;
};
/*
* Private endpoint (pep).
*
* A pep takes care of the transfers for an endpoint.
*/
struct sthcd_ctrl_params_in {
struct usb_ctrlrequest req;
u8 _unk1; /* timeout? */
};
struct sthcd_ctrl_xfer_ctx {
struct starlet_ioh_sg in[6];
struct sthcd_ctrl_params_in *params_in;
};
struct sthcd_bulk_intr_params_in {
u8 bEndpointAddress;
u16 wLength;
};
struct sthcd_bulk_intr_xfer_ctx {
struct starlet_ioh_sg in[2];
struct sthcd_bulk_intr_params_in *params_in;
};
enum {
__STHCD_PEP_DISABLED = 0,
__STHCD_PEP_XFERBUSY, /* pep is actively xferring data */
};
struct sthcd_pep {
unsigned long flags;
#define STHCD_PEP_DISABLED (1 << __STHCD_PEP_DISABLED)
#define STHCD_PEP_XFERBUSY (1 << __STHCD_PEP_XFERBUSY)
unsigned long outstanding;
struct usb_host_endpoint *ep; /* associated endpoint */
struct sthcd_hcd *sthcd; /* associated hcd */
/* local copy of endpoint descriptor bmAttributes */
__u8 bmAttributes;
/* xfer context data */
struct urb *urb; /* urb being transferred */
struct sthcd_udev *udev; /* udev for this urb */
struct list_head node; /* in list of peps for this udev */
size_t io_xfer_offset; /* number of bytes transferred */
void *io_buf; /* data buffer */
size_t io_buf_len; /* length of io_buf */
int request; /* ioctlv request */
union {
struct sthcd_bulk_intr_xfer_ctx *bulk_intr;
struct sthcd_ctrl_xfer_ctx *ctrl;
} ctx; /* transfer context */
unsigned int nents_in; /* number of input sg entries */
struct starlet_ioh_sg *in; /* input sg list */
struct starlet_ioh_sg io[1]; /* input/output sg list */
};
/*
* Debugging facilities.
*
*/
#if 0
static inline void print_buffer(void *buf, u32 size)
{
int i;
for (i = 0; i < (size + 3) / 4; i += 4) {
u32 *data = &((u32 *)buf)[i];
printk(KERN_INFO " %08X %08X %08X %08X\n",
data[0], data[1], data[2], data[3]
);
}
}
#endif
/*
* Type conversion routines.
*
*/
static inline struct sthcd_hcd *hcd_to_sthcd(struct usb_hcd *hcd)
{
return (struct sthcd_hcd *)(hcd->hcd_priv);
}
static inline struct usb_hcd *sthcd_to_hcd(struct sthcd_hcd *sthcd)
{
return container_of((void *)sthcd, struct usb_hcd, hcd_priv);
}
static inline struct sthcd_port *udev_to_port(struct sthcd_udev *_udev)
{
return container_of(_udev, struct sthcd_port, udev);
}
/*
* Private End Point abstraction.
*
*/
static inline struct sthcd_pep *ep_to_pep(struct usb_host_endpoint *ep)
{
return ep->hcpriv;
}
static inline int pep_is_enabled(struct sthcd_pep *pep)
{
return !test_bit(__STHCD_PEP_DISABLED, &pep->flags);
}
static int sthcd_pep_alloc_ctrl_xfer_ctx(struct sthcd_pep *pep)
{
struct sthcd_ctrl_xfer_ctx *ctx;
struct sthcd_ctrl_params_in *params_in;
int error;
ctx = starlet_kzalloc(sizeof(*ctx), GFP_ATOMIC);
if (!ctx) {
error = -ENOMEM;
goto done;
}
params_in = starlet_ioh_kzalloc(sizeof(*params_in));
if (!params_in) {
starlet_kfree(ctx);
error = -ENOMEM;
goto done;
}
ctx->params_in = params_in;
starlet_ioh_sg_init_table(ctx->in, 6);
starlet_ioh_sg_entry(&ctx->in[0], &params_in->req.bRequestType);
starlet_ioh_sg_entry(&ctx->in[1], &params_in->req.bRequest);
starlet_ioh_sg_entry(&ctx->in[2], &params_in->req.wValue);
starlet_ioh_sg_entry(&ctx->in[3], &params_in->req.wIndex);
starlet_ioh_sg_entry(&ctx->in[4], &params_in->req.wLength);
starlet_ioh_sg_entry(&ctx->in[5], &params_in->_unk1);
pep->ctx.ctrl = ctx;
pep->nents_in = ARRAY_SIZE(ctx->in);
pep->in = ctx->in;
error = 0;
done:
if (error < 0)
DBG("%s: error=%d (%x)\n", __func__, error, error);
return error;
}
static void sthcd_pep_free_ctrl_xfer_ctx(struct sthcd_pep *pep)
{
struct sthcd_ctrl_xfer_ctx *ctx = pep->ctx.ctrl;
if (ctx) {
starlet_ioh_kfree(ctx->params_in);
starlet_kfree(ctx);
pep->ctx.ctrl = NULL;
}
}
static int sthcd_pep_alloc_bulk_intr_xfer_ctx(struct sthcd_pep *pep)
{
struct sthcd_bulk_intr_xfer_ctx *ctx;
struct sthcd_bulk_intr_params_in *params_in;
int error;
ctx = starlet_kzalloc(sizeof(*ctx), GFP_ATOMIC);
if (!ctx) {
error = -ENOMEM;
goto done;
}
params_in = starlet_ioh_kzalloc(sizeof(*params_in));
if (!params_in) {
starlet_kfree(ctx);
error = -ENOMEM;
goto done;
}
ctx->params_in = params_in;
starlet_ioh_sg_init_table(ctx->in, 2);
starlet_ioh_sg_entry(&ctx->in[0], &params_in->bEndpointAddress);
starlet_ioh_sg_entry(&ctx->in[1], &params_in->wLength);
pep->ctx.bulk_intr = ctx;
pep->nents_in = ARRAY_SIZE(ctx->in);
pep->in = ctx->in;
error = 0;
done:
if (error < 0)
DBG("%s: error=%d (%x)\n", __func__, error, error);
return error;
}
static void sthcd_pep_free_bulk_intr_xfer_ctx(struct sthcd_pep *pep)
{
struct sthcd_bulk_intr_xfer_ctx *ctx = pep->ctx.bulk_intr;
if (ctx) {
starlet_ioh_kfree(ctx->params_in);
starlet_kfree(ctx);
pep->ctx.bulk_intr = NULL;
}
}
static int sthcd_pep_alloc_xfer_ctx(struct sthcd_pep *pep)
{
unsigned int xfer_type = pep->bmAttributes &
USB_ENDPOINT_XFERTYPE_MASK;
int error;
switch (xfer_type) {
case USB_ENDPOINT_XFER_CONTROL:
error = sthcd_pep_alloc_ctrl_xfer_ctx(pep);
break;
case USB_ENDPOINT_XFER_BULK:
case USB_ENDPOINT_XFER_INT:
error = sthcd_pep_alloc_bulk_intr_xfer_ctx(pep);
break;
default:
error = -ENXIO;
break;
}
if (error < 0)
DBG("%s: error=%d (%x)\n", __func__, error, error);
return error;
}
static void sthcd_pep_free_xfer_ctx(struct sthcd_pep *pep)
{
unsigned int xfer_type = pep->bmAttributes &
USB_ENDPOINT_XFERTYPE_MASK;
switch (xfer_type) {
case USB_ENDPOINT_XFER_CONTROL:
sthcd_pep_free_ctrl_xfer_ctx(pep);
break;
case USB_ENDPOINT_XFER_BULK:
case USB_ENDPOINT_XFER_INT:
sthcd_pep_free_bulk_intr_xfer_ctx(pep);
break;
default:
DBG("%s: invalid endpoint xfer type %u\n", __func__,
xfer_type);
break;
}
}
static int sthcd_pep_alloc_xfer_io_buf(struct sthcd_pep *pep, size_t size)
{
/* REVISIT, size must be greater than 0 */
size_t io_buf_size = size + 32;
int error;
pep->io_buf = starlet_ioh_kzalloc(io_buf_size);
if (!pep->io_buf) {
error = -ENOMEM;
goto done;
}
starlet_ioh_sg_init_table(pep->io, 1);
starlet_ioh_sg_set_buf(&pep->io[0], pep->io_buf, size);
error = 0;
done:
if (error < 0)
DBG("%s: error=%d (%x)\n", __func__, error, error);
return error;
}
static void sthcd_pep_free_xfer_io_buf(struct sthcd_pep *pep)
{
if (pep->io_buf) {
starlet_ioh_sg_set_buf(&pep->io[0], NULL, 0);
starlet_ioh_kfree(pep->io_buf);
pep->io_buf = NULL;
}
}
/*
*
* Context: interrupts disabled, hcd lock held
*/
static int sthcd_pep_init(struct sthcd_pep *pep, struct sthcd_hcd *sthcd,
struct usb_host_endpoint *ep)
{
int error;
BUG_ON(!ep);
pep->sthcd = sthcd;
pep->ep = ep;
pep->bmAttributes = ep->desc.bmAttributes;
error = sthcd_pep_alloc_xfer_ctx(pep);
if (error)
goto done;
done:
if (error < 0)
DBG("%s: error=%d (%x)\n", __func__, error, error);
return error;
}
/*
*
* Context: interrupts disabled, hcd lock held
*/
static void sthcd_pep_exit(struct sthcd_pep *pep)
{
BUG_ON(pep->urb);
BUG_ON(!pep->ep);
sthcd_pep_free_xfer_ctx(pep);
pep->ep = NULL;
pep->sthcd = NULL;
}
/*
*
* Context: interrupts disabled, hcd lock held
*/
static struct sthcd_pep *sthcd_pep_alloc(struct sthcd_hcd *sthcd,
struct usb_host_endpoint *ep)
{
struct sthcd_pep *pep;
int error;
pep = kzalloc(sizeof(*pep), GFP_ATOMIC);
if (!pep)
return NULL;
error = sthcd_pep_init(pep, sthcd, ep);
if (error) {
kfree(pep);
return NULL;
}
return pep;
}
/*
*
* Context: interrupts disabled, hcd lock held
*/
static void sthcd_pep_free(struct sthcd_pep *pep)
{
sthcd_pep_exit(pep);
kfree(pep);
}
/*
*
* Context: interrupts disabled, hcd lock held
*/
static struct sthcd_udev *sthcd_find_udev_by_num(struct sthcd_hcd *sthcd,
u16 devnum)
{
struct sthcd_udev *udev;
list_for_each_entry(udev, &sthcd->device_list, node) {
if (udev->devnum == devnum)
return udev;
}
DBG("%s: udev %u not found\n", __func__, devnum);
return NULL;
}
/*
*
* Context: interrupts disabled, hcd lock held
*/
static int sthcd_pep_takein_urb(struct sthcd_pep *pep, struct urb *urb)
{
struct sthcd_hcd *sthcd = pep->sthcd;
struct sthcd_udev *udev;
int error = 0;
if (!pep_is_enabled(pep)) {
error = -ESHUTDOWN;
goto done;
}
if (pep->urb) {
error = -EBUSY;
goto done;
}
if (unlikely(!pep->udev)) {
BUG_ON(!urb->dev);
udev = sthcd_find_udev_by_num(sthcd, urb->dev->devnum);
if (!udev) {
error = -ENODEV;
goto done;
}
pep->udev = udev;
list_add_tail(&pep->node, &udev->pep_list);
}
pep->urb = urb;
done:
if (error < 0)
DBG("%s: error=%d (%x)\n", __func__, error, error);
return error;
}
/*
*
* Context: interrupts disabled, hcd lock held
*/
static void sthcd_pep_takeout_urb(struct sthcd_pep *pep)
{
WARN_ON(!pep->urb);
pep->urb = NULL;
if (pep->udev)
list_del_init(&pep->node);
pep->udev = NULL;
}
/*
*
* Context: interrupts disabled, hcd lock held
*/
static void sthcd_pep_setup_ctrl_xfer(struct sthcd_pep *pep)
{
struct urb *urb = pep->urb;
struct sthcd_ctrl_xfer_ctx *ctx = pep->ctx.ctrl;
struct sthcd_ctrl_params_in *params_in;
params_in = ctx->params_in;
memcpy(&params_in->req, urb->setup_packet, sizeof(params_in->req));
params_in->req.wLength = cpu_to_le16(pep->io_buf_len);
}
/*
*
* Context: interrupts disabled, hcd lock held
*/
static void sthcd_pep_setup_bulk_intr_xfer(struct sthcd_pep *pep)
{
struct urb *urb = pep->urb;
struct sthcd_bulk_intr_xfer_ctx *ctx = pep->ctx.bulk_intr;
struct sthcd_bulk_intr_params_in *params_in;
params_in = ctx->params_in;
params_in->bEndpointAddress = urb->ep->desc.bEndpointAddress;
params_in->wLength = pep->io_buf_len;
}
/*
*
* Context: interrupts disabled, hcd lock held
*/
static int sthcd_pep_setup_xfer(struct sthcd_pep *pep)
{
struct urb *urb = pep->urb;
int request;
int error = 0;
switch (usb_pipetype(urb->pipe)) {
case PIPE_CONTROL:
request = STHCD_IOCTLV_CONTROLREQ;
sthcd_pep_setup_ctrl_xfer(pep);
break;
case PIPE_INTERRUPT:
request = STHCD_IOCTLV_INTRREQ;
sthcd_pep_setup_bulk_intr_xfer(pep);
break;
case PIPE_BULK:
request = STHCD_IOCTLV_BULKREQ;
sthcd_pep_setup_bulk_intr_xfer(pep);
break;
default:
error = -EINVAL;
break;
}
if (!error) {
pep->request = request;
starlet_ioh_sg_set_buf(&pep->io[0],
pep->io_buf, pep->io_buf_len);
}
if (error < 0)
DBG("%s: error=%d (%x)\n", __func__, error, error);
return error;
}
/*
*
* Context: interrupts disabled, hcd lock held
*/
static int sthcd_pep_setup_next_xfer(struct sthcd_pep *pep)
{
struct urb *urb = pep->urb;
int retval = 0;
int error;
if (pep->io_xfer_offset < urb->transfer_buffer_length) {
pep->io_buf_len = urb->transfer_buffer_length -
pep->io_xfer_offset;
if (pep->io_buf_len > STHCD_MAX_CHUNK_SIZE)
pep->io_buf_len = STHCD_MAX_CHUNK_SIZE;
retval = pep->io_buf_len;
error = sthcd_pep_setup_xfer(pep);
if (error)
retval = error;
}
if (retval < 0)
DBG("%s: retval=%d (%x)\n", __func__, retval, retval);
return retval;
}
/*
*
* Context: interrupts disabled, hcd lock held
*/
static int sthcd_pep_setup_first_xfer(struct sthcd_pep *pep)
{
struct urb *urb = pep->urb;
int retval;
int error;
pep->io_xfer_offset = 0;
pep->io_buf_len = urb->transfer_buffer_length;
if (pep->io_buf_len > STHCD_MAX_CHUNK_SIZE)
pep->io_buf_len = STHCD_MAX_CHUNK_SIZE;
retval = pep->io_buf_len;
error = sthcd_pep_setup_xfer(pep);
if (error)
retval = error;
if (retval < 0)
DBG("%s: retval=%d (%x)\n", __func__, retval, retval);
return retval;
}
/*
*
* Context: interrupts disabled, hcd lock held
*/
static void sthcd_pep_finish_xfer(struct sthcd_pep *pep, int xfer_len)
{
struct urb *urb = pep->urb;
if (xfer_len <= 0)
goto done;
BUG_ON(!urb);
BUG_ON(!pep->io_buf);
/*
* For IN transfers, copy the received chunk data into the urb
* xfer buffer.
*/
if (usb_urb_dir_in(urb)) {
/* device -> host */
BUG_ON(!urb->transfer_buffer);
memcpy(urb->transfer_buffer + pep->io_xfer_offset,
pep->io_buf, xfer_len);
}
pep->io_xfer_offset += xfer_len;
done:
return;
}
static int sthcd_pep_xfer_callback(struct starlet_ipc_request *req);
/*
*
* Context: interrupts disabled, hcd lock held
*/
static int sthcd_pep_start_xfer(struct sthcd_pep *pep)
{
struct urb *urb = pep->urb;
struct sthcd_udev *udev = pep->udev;
int error;
BUG_ON(!urb);
/* udev was disconnected */
if (unlikely(!udev)) {
error = -ENODEV;
goto done;
}
if (!pep_is_enabled(pep)) {
error = -ESHUTDOWN;
goto done;
}
/* for OUT transfers, copy the data to send into the pep xfer buffer */
if (pep->io_buf_len > 0) {
if (usb_urb_dir_out(urb)) {
/* host -> device */
BUG_ON(!urb->transfer_buffer);
memcpy(pep->io_buf,
urb->transfer_buffer + pep->io_xfer_offset,
pep->io_buf_len);
}
}
starlet_ioh_sg_set_buf(&pep->io[0],
pep->io_buf, pep->io_buf_len);
/* start an async transfer */
error = starlet_ioh_ioctlv_nowait(udev->fd, pep->request,
pep->nents_in, pep->in, 1, pep->io,
sthcd_pep_xfer_callback, pep);
if (!error)
pep->outstanding++;
done:
if (error < 0)
DBG("%s: error=%d (%x)\n", __func__, error, error);
return error;
}
/*
*
* Context: interrupts disabled, hcd lock held
*/
static int sthcd_giveback_urb(struct sthcd_hcd *sthcd,
struct urb *urb, int status)
__releases(sthcd->lock) __acquires(sthcd->lock)
{
struct usb_hcd *hcd = sthcd_to_hcd(sthcd);
/*
* Release the hcd lock here as the callback may need to
* hold it again.
*/
spin_unlock(&sthcd->lock);
usb_hcd_giveback_urb(hcd, urb, status);
spin_lock(&sthcd->lock);
return status;
}
/*
*
* Context: interrupts disabled, hcd lock held
*/
struct urb *sthcd_find_next_urb_in_ep(struct usb_host_endpoint *ep)
{
if (list_empty(&ep->urb_list))
return NULL;
else
return list_first_entry(&ep->urb_list, struct urb, urb_list);
}
static int sthcd_pep_send_urb(struct sthcd_pep *pep, struct urb *urb);
/*
*
* Context: interrupts disabled, hcd lock held
*/
static int sthcd_pep_cond_send_next_urb(struct sthcd_pep *pep)
{
struct urb *urb;
int retval = 0;
int error;
/* schedule next urb if any */
urb = sthcd_find_next_urb_in_ep(pep->ep);
if (urb) {
error = sthcd_pep_send_urb(pep, urb);
if (!error) {
retval = 1;
goto done;
} else {
retval = error;
}
}
done:
if (retval < 0)
DBG("%s: retval=%d (%x)\n", __func__, retval, retval);
return retval;
}
/*
*
* Context: interrupts disabled, hcd lock held
*/
static int sthcd_pep_send_urb(struct sthcd_pep *pep, struct urb *urb)
{
struct sthcd_port *port = NULL;
struct sthcd_hcd *sthcd;
struct usb_ctrlrequest *req;
u16 typeReq, wValue;
int retval, fake;
int error;
/*
* Unconditionally fail urbs targetted at doomed ports.
*/
if (pep->udev) {
port = udev_to_port(pep->udev);
if (test_bit(__STHCD_PORT_DOOMED, &port->flags)) {
error = -ENODEV;
goto done;
}
}
if (test_and_set_bit(__STHCD_PEP_XFERBUSY, &pep->flags)) {
/*
* There is a pep xfer in progress.
* Our urb is already queued on the usb device, so do nothing
* here and rely on the pep xfer callback to do the actual
* work when it's done with the current urb in flight.
*/
error = 0;
goto done;
}
/* we can have one ongoing urb only */
error = sthcd_pep_takein_urb(pep, urb);
if (error)
goto done;
urb->hcpriv = urb; /* mark urb in use */
retval = sthcd_pep_setup_first_xfer(pep);
if (retval < 0) {
error = retval;
goto err_setup_xfer;
}
fake = 0;
if (pep->request == STHCD_IOCTLV_CONTROLREQ) {
req = (struct usb_ctrlrequest *)urb->setup_packet;
typeReq = (req->bRequestType << 8) | req->bRequest;
wValue = le16_to_cpu(req->wValue);
switch (typeReq) {
case DeviceOutRequest | USB_REQ_SET_ADDRESS: /* 0005 */
if (urb->dev->devnum != 0) {
/* REVISIT, never reached */
drv_printk(KERN_WARNING,
"address change %u->%u\n",
urb->dev->devnum, wValue);
}
/*
* We are guaranteed to have an udev because the takein
* was successful.
*/
pep->udev->devnum = wValue;
urb->actual_length = 0;
/* clear the port reset count, we have an address */
if (wValue) {
/*
* We need to retrieve the port again
* as we might have entered the function
* without an udev assigned to the pep.
*/
port = udev_to_port(pep->udev);
port->nr_resets = 0;
}
fake = 1;
break;
default:
break;
}
}
if (fake) {
sthcd = pep->sthcd;
/* finish this fake urb synchronously... */
usb_hcd_unlink_urb_from_ep(sthcd_to_hcd(sthcd), urb);
sthcd_giveback_urb(sthcd, urb, 0);
/* ... and proceed with the next urb, if applicable */
sthcd_pep_cond_send_next_urb(pep);
} else {
/* allocate an io buffer for this transfer */
error = sthcd_pep_alloc_xfer_io_buf(pep, pep->io_buf_len);
if (error)
goto err_alloc_io_buf;
/* ... and start the first transfer */
error = sthcd_pep_start_xfer(pep);
if (error)
goto err_start_xfer;
}
return 0;
err_start_xfer:
sthcd_pep_free_xfer_io_buf(pep);
err_alloc_io_buf:
err_setup_xfer:
sthcd_pep_takeout_urb(pep);
urb->hcpriv = NULL;
done:
if (error < 0)
DBG("%s: error=%d (%x)\n", __func__, error, error);
return error;
}
static void sthcd_pep_print(struct sthcd_pep *pep)
{
struct usb_device *udev;
u16 idVendor, idProduct;
idVendor = idProduct = 0xffff;
if (pep->urb) {
udev = pep->urb->dev;
if (udev) {
idVendor = le16_to_cpu(udev->descriptor.idVendor);
idProduct = le16_to_cpu(udev->descriptor.idProduct);
}
}
DBG("(%04X:%04X) request=%d,"
" io_buf=%p, io_buf_len=%u, io_xfer_offset=%u\n",
idVendor, idProduct,
pep->request,
pep->io_buf,
pep->io_buf_len,
pep->io_xfer_offset);
}
/*
*
* Context: in interrupt
*/
static int sthcd_pep_xfer_callback(struct starlet_ipc_request *req)
{
int xfer_len = req->result;
struct sthcd_pep *pep = req->done_data;
int status = 0;
struct sthcd_port *port;
struct sthcd_hcd *sthcd;
struct usb_hcd *hcd;
struct urb *urb;
int retval;
unsigned long flags;
int error;
starlet_ipc_free_request(req);
sthcd = pep->sthcd;
spin_lock_irqsave(&sthcd->lock, flags);
hcd = sthcd_to_hcd(sthcd);
pep->outstanding--;
urb = pep->urb;
if (!urb) {
/*
* starlet completed an URB that was already dequeued.
*
* We must free here the memory used by the pep, including
* I/O buffers, avoiding dereferencing any USB stack data
* pointed by the pep, as it may be invalid now.
*/
sthcd_pep_free_xfer_io_buf(pep);
sthcd_pep_free(pep);
goto done;
}
/* sanity checks, determine transfer status and length */
if (xfer_len < 0) {
status = xfer_len;
xfer_len = 0;
if (status != -7004 && status != -7003 && status != -7005) {
drv_printk(KERN_ERR, "request completed"
" with error %d\n", status);
sthcd_pep_print(pep);
}
switch (status) {
case -7003:
case -7004:
/* endpoint stall */
status = -EPIPE;
break;
case -7005:
/* nak? */
status = -ECONNRESET;
break;
case -7008:
case -7022:
case -4:
/* FALL-THROUGH */
default:
/*
* We got an unknown, probably un-retryable, error.
* Flag the port as unuseable. The associated
* device will be disconnected ASAP.
*/
port = udev_to_port(pep->udev);
set_bit(__STHCD_PORT_DOOMED, &port->flags);
DBG("%s: error %d on port %d, doomed!\n", __func__,
status, port - pep->sthcd->ports + 1);
/* also, do not use the pep for xfers anymore */
set_bit(__STHCD_PEP_DISABLED, &pep->flags);
status = -ENODEV;
break;
}
} else {
if (usb_pipecontrol(urb->pipe)) {
/*
* starlet includes the length of the request
* into the reply for control transfers.
* We need to substract the request size from
* the reply len to get the actual data size.
*/
xfer_len -= sizeof(struct usb_ctrlrequest);
if (xfer_len < 0) {
drv_printk(KERN_ERR, "request incomplete,"
" %d bytes short\n",
-xfer_len);
status = -EPIPE;
xfer_len = 0;
}
}
if (xfer_len > pep->io_buf_len) {
DBG("%s: xfer len %u larger than xfer buf"
" len %u\n", __func__,
xfer_len, pep->io_buf_len);
xfer_len = pep->io_buf_len;
}
}
if (xfer_len > 0) {
sthcd_pep_finish_xfer(pep, xfer_len);
/*
* Only schedule the next chunk if we didn't get a short xfer
* and the pep is still active
*/
if (xfer_len == pep->io_buf_len && pep_is_enabled(pep)) {
retval = sthcd_pep_setup_next_xfer(pep);
if (retval <= 0) {
/* an error happened or all chunks were done */
status = retval;
} else {
/* next xfer */
sthcd_pep_start_xfer(pep);
goto done;
}
}
}
sthcd_pep_free_xfer_io_buf(pep);
urb->actual_length = pep->io_xfer_offset;
/* at this point, we are done with this urb */
clear_bit(__STHCD_PEP_XFERBUSY, &pep->flags);
sthcd_pep_takeout_urb(pep);
BUG_ON(!sthcd);
BUG_ON(!urb);
error = usb_hcd_check_unlink_urb(hcd, urb, status);
if (!error) {
usb_hcd_unlink_urb_from_ep(hcd, urb);
/* give back this urb */
sthcd_giveback_urb(sthcd, urb, status);
} else {
/* REVISIT, paranoid */
DBG("%s: error checking unlink\n", __func__);
}
/* if applicable, launch the next urb in this endpoint queue */
sthcd_pep_cond_send_next_urb(pep);
done:
spin_unlock_irqrestore(&sthcd->lock, flags);
return 0;
}
/*
* starlet USB device "udev" abstraction.
*
*
*/
static struct sthcd_udev *sthcd_get_free_udev(struct sthcd_hcd *sthcd)
{
struct sthcd_port *port;
struct sthcd_udev *udev;
int i;
port = sthcd->ports;
for (i = 0; i < sthcd->nr_ports; i++, port++) {
udev = &port->udev;
if (!test_and_set_bit(__STHCD_PORT_INUSE, &port->flags))
return udev;
}
return NULL;
}
static struct sthcd_udev *sthcd_find_udev_by_ids(struct sthcd_hcd *sthcd,
u16 idVendor, u16 idProduct)
{
struct sthcd_udev *udev;
list_for_each_entry(udev, &sthcd->device_list, node) {
if (udev->idVendor == idVendor && udev->idProduct == idProduct)
return udev;
}
return NULL;
}
#if 0
static int sthcd_udev_suspend(struct sthcd_udev *udev)
{
int error;
error = starlet_ioctl(udev->fd, STHCD_IOCTL_SUSPENDDEVICE,
NULL, 0, NULL, 0);
if (error < 0)
DBG("%s: error=%d (%x)\n", __func__, error, error);
return error;
}
static int sthcd_udev_resume(struct sthcd_udev *udev)
{
int error;
error = starlet_ioctl(udev->fd, STHCD_IOCTL_RESUMEDEVICE,
NULL, 0, NULL, 0);
if (error < 0)
DBG("%s: error=%d (%x)\n", __func__, error, error);
return error;
}
#endif
static int sthcd_udev_close(struct sthcd_udev *udev)
{
int fd = udev->fd;
udev->fd = -1;
return starlet_close(fd);
}
static int sthcd_udev_open(struct sthcd_udev *udev)
{
struct sthcd_oh *oh = udev->oh;
char pathname[32];
int error;
if (udev->fd != -1) {
drv_printk(KERN_WARNING, "udev %04X.%04X already opened,"
" closing it first\n",
udev->idVendor, udev->idProduct);
sthcd_udev_close(udev);
}
snprintf(pathname, sizeof(pathname), "/dev/usb/oh%u/%04x/%04x",
oh->index, udev->idVendor, udev->idProduct);
error = starlet_open(pathname, 0);
if (error < 0) {
drv_printk(KERN_ERR, "open %s failed\n", pathname);
return error;
}
udev->fd = error;
return 0;
}
static void sthcd_udev_exit(struct sthcd_udev *udev)
{
struct sthcd_hcd *sthcd;
struct sthcd_pep *pep;
unsigned long flags;
sthcd = udev->oh->hcd;
spin_lock_irqsave(&sthcd->lock, flags);
/* remove from the list of connected devices */
list_del_init(&udev->node);
/* unlink all associated peps */
list_for_each_entry(pep, &udev->pep_list, node) {
if (pep->udev) {
pep->udev = NULL;
list_del_init(&pep->node);
}
}
spin_unlock_irqrestore(&sthcd->lock, flags);
sthcd_udev_close(udev);
udev->idVendor = 0;
udev->idProduct = 0;
udev->oh = NULL;
udev->devnum = 0;
}
static int sthcd_udev_init(struct sthcd_udev *udev,
struct sthcd_oh *oh,
u16 idVendor, u16 idProduct)
{
struct sthcd_hcd *sthcd = oh->hcd;
int error;
unsigned long flags;
INIT_LIST_HEAD(&udev->pep_list);
udev->idVendor = idVendor;
udev->idProduct = idProduct;
udev->oh = oh;
udev->fd = -1;
udev->devnum = 0;
error = sthcd_udev_open(udev);
if (error)
return error;
spin_lock_irqsave(&sthcd->lock, flags);
list_add_tail(&udev->node, &sthcd->device_list);
spin_unlock_irqrestore(&sthcd->lock, flags);
return error;
}
/*
* Hub emulation routines.
*
*/
#define STHCD_USB_DT_HUB_TOTAL_SIZE \
(USB_DT_HUB_NONVAR_SIZE + 2*((STHCD_MAX_PORTS + 1 + 7) / 8))
static struct usb_hub_descriptor sthcd_hub_hub_descr = {
.bDescLength = STHCD_USB_DT_HUB_TOTAL_SIZE,
.bDescriptorType = USB_DT_HUB,
.bNbrPorts = STHCD_MAX_PORTS,
.wHubCharacteristics = 0x0000,
.bPwrOn2PwrGood = 0,
.bHubContrCurrent = 0,
};
static int
sthcd_hub_control_standard(struct usb_hcd *hcd, u16 typeReq, u16 wValue,
u16 wIndex, char *buf, u16 wLength)
{
int retval = -EINVAL;
switch (typeReq) {
case DeviceOutRequest | USB_REQ_SET_CONFIGURATION: /* 0009 */
if (wValue != 1) {
drv_printk(KERN_INFO, "invalid configuration %d\n",
wValue);
} else {
retval = 0;
}
break;
case DeviceRequest | USB_REQ_GET_STATUS: /* 8000 */
if (wLength < 2) {
retval = -ENOMEM;
} else {
buf[0] = (1 << USB_DEVICE_SELF_POWERED);
buf[1] = 0;
retval = 2;
}
break;
default:
drv_printk(KERN_WARNING, "%s: request %04X not supported\n",
__func__, typeReq);
break;
}
DBG("%s: retval=%d (%x)\n", __func__, retval, retval);
return retval;
}
static int
sthcd_hub_control_hub(struct usb_hcd *hcd, u16 typeReq, u16 wValue,
u16 wIndex, void *buf, u16 wLength)
{
struct sthcd_hcd *sthcd = hcd_to_sthcd(hcd);
struct usb_hub_status *hub_status;
struct usb_hub_descriptor *hub_descr;
size_t size, port_array_size;
u8 *p;
int retval = -EINVAL;
switch (typeReq) {
case GetHubStatus: /* 0xA000 */
size = sizeof(*hub_status);
if (wLength < size) {
retval = -ENOMEM;
} else {
hub_status = buf;
hub_status->wHubStatus = 0x0000; /* no problems */
hub_status->wHubChange = 0x0000; /* no changes */
retval = size;
}
break;
case GetHubDescriptor: /* 0xA006 */
/*
* For the DeviceRemovable and PortPwrCtrlMask fields:
* bit 0 is reserved.
* bit 1 is the internal (oh1) port, which is non-removable.
* bit 2..nr_ports+1 are the external (oh0) ports.
*/
port_array_size = (1 + sthcd->nr_ports + 7) / 8;
size = USB_DT_HUB_NONVAR_SIZE + 2*port_array_size;
if (wLength < size) {
retval = -ENOMEM;
} else {
p = buf;
memcpy(p, &sthcd_hub_hub_descr, USB_DT_HUB_NONVAR_SIZE);
p += USB_DT_HUB_NONVAR_SIZE;
/* fixup the descriptor with the real number of ports */
hub_descr = buf;
hub_descr->bDescLength = size;
hub_descr->bNbrPorts = sthcd->nr_ports;
/* DeviceRemovable field, table 11-13 Hub Descriptor */
memset(p, 0, port_array_size);
*p |= 0x02; /* port 1 is non-removable */
p += port_array_size;
/* PortPwrCtrlMask field, table 11-13 Hub Descriptor */
memset(p, 0xff, port_array_size);
retval = size;
}
break;
default:
drv_printk(KERN_WARNING, "%s: request %04X not supported\n",
__func__, typeReq);
break;
}
if (retval < 0)
DBG("%s: retval=%d (%x)\n", __func__, retval, retval);
return retval;
}
static int
sthcd_hub_control_port(struct usb_hcd *hcd, u16 typeReq, u16 wValue,
u16 wIndex, void *buf, u16 wLength)
{
struct sthcd_hcd *sthcd = hcd_to_sthcd(hcd);
struct sthcd_port *port;
unsigned long flags;
int retval = 0;
if (wIndex == 0 || wIndex > sthcd->nr_ports) {
DBG("%s: invalid port %u\n", __func__, wIndex);
return -EINVAL;
}
spin_lock_irqsave(&sthcd->lock, flags);
wIndex--;
port = &sthcd->ports[wIndex];
switch (typeReq) {
case GetPortStatus: /* 0xA300 */
if (test_bit(__STHCD_PORT_DOOMED, &port->flags)) {
/* disconnect */
if (!!(port->status_change & USB_PORT_STAT_CONNECTION))
port->status_change |=
(USB_PORT_STAT_C_CONNECTION<<16);
port->status_change &= ~USB_PORT_STAT_CONNECTION;
}
/* REVISIT wait 50ms before clearing the RESET state */
if (port->status_change & USB_PORT_STAT_RESET) {
port->nr_resets++;
if (port->nr_resets > 2) {
DBG("%s: port %d was reset %u time(s),"
" doomed!\n", __func__,
wIndex+1, port->nr_resets);
set_bit(__STHCD_PORT_DOOMED, &port->flags);
}
if (!(port->status_change & USB_PORT_STAT_ENABLE))
port->status_change |=
(USB_PORT_STAT_C_ENABLE << 16);
port->status_change &= ~USB_PORT_STAT_RESET;
port->status_change |= (USB_PORT_STAT_ENABLE |
(USB_PORT_STAT_C_RESET << 16));
port->udev.devnum = 0;
}
retval = 4;
((__le32 *) buf)[0] = cpu_to_le32(port->status_change);
break;
case ClearPortFeature: /* 0x2301 */
switch (wValue) {
case USB_PORT_FEAT_ENABLE:
port->status_change &= USB_PORT_STAT_POWER;
break;
case USB_PORT_FEAT_SUSPEND:
case USB_PORT_FEAT_POWER:
case USB_PORT_FEAT_C_ENABLE:
case USB_PORT_FEAT_C_SUSPEND:
case USB_PORT_FEAT_C_CONNECTION:
case USB_PORT_FEAT_C_OVER_CURRENT:
case USB_PORT_FEAT_C_RESET: /* 0x14 */
break;
default:
goto error;
}
port->status_change &= ~(1 << wValue);
break;
case SetPortFeature: /* 0x2303 */
switch (wValue) {
case USB_PORT_FEAT_ENABLE:
case USB_PORT_FEAT_SUSPEND:
case USB_PORT_FEAT_POWER: /* 0x08 */
break;
case USB_PORT_FEAT_RESET: /* 0x04 */
/* REVISIT, free all related resources here */
break;
default:
goto error;
}
port->status_change |= 1 << wValue;
break;
default:
drv_printk(KERN_WARNING, "%s: request %04X not supported\n",
__func__, typeReq);
error:
retval = -EPIPE;
break;
}
spin_unlock_irqrestore(&sthcd->lock, flags);
return retval;
}
static int sthcd_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue,
u16 wIndex, char *buf, u16 wLength)
{
u8 bmRequestType;
int retval = -EINVAL;
/*
* starlet never answers to requests on device 0/0, so we emulate it.
*/
bmRequestType = typeReq >> 8;
switch (bmRequestType & USB_TYPE_MASK) {
case USB_TYPE_STANDARD:
/* generic requests */
retval = sthcd_hub_control_standard(hcd, typeReq, wValue,
wIndex, buf, wLength);
break;
case USB_TYPE_CLASS:
/* hub-specific requests */
switch (bmRequestType & USB_RECIP_MASK) {
case USB_RECIP_DEVICE:
/* hub */
retval = sthcd_hub_control_hub(hcd, typeReq, wValue,
wIndex, buf, wLength);
break;
case USB_RECIP_OTHER:
/* port */
retval = sthcd_hub_control_port(hcd, typeReq, wValue,
wIndex, buf, wLength);
break;
default:
drv_printk(KERN_WARNING, "%s: request %04X"
" not supported\n", __func__, typeReq);
break;
}
break;
default:
drv_printk(KERN_WARNING, "%s: request %04X not supported\n",
__func__, typeReq);
break;
}
if (retval > 0)
retval = 0;
if (retval < 0)
DBG("%s: retval=%d (%x)\n", __func__, retval, retval);
return retval;
}
static int sthcd_hub_status_data(struct usb_hcd *hcd, char *buf)
{
struct sthcd_hcd *sthcd = hcd_to_sthcd(hcd);
u16 *p = (u16 *)buf;
struct sthcd_port *port;
unsigned long flags;
int i, result;
if (!HC_IS_RUNNING(hcd->state))
return -ESHUTDOWN;
#if 0
if (timer_pending(&hcd->rh_timer))
return 0;
#endif
/* FIXME, this code assumes at least 9 and no more than 15 ports */
BUG_ON(sthcd->nr_ports > 15 || sthcd->nr_ports < 8);
spin_lock_irqsave(&sthcd->lock, flags);
port = sthcd->ports;
for (i = 0, *p = 0; i < sthcd->nr_ports; i++, port++) {
if ((port->status_change & 0xffff0000) != 0) {
*p |= 1 << (i+1);
/* REVISIT */
}
}
*p = le16_to_cpu(*p);
result = (*p != 0) ? 2 : 0;
spin_unlock_irqrestore(&sthcd->lock, flags);
/* DBG("%s: poll cycle, changes=%04x\n", __func__, *p); */
return result;
}
/*
* "OH" abstraction.
*
*/
static int sthcd_oh_insert_udev(struct sthcd_oh *oh,
u16 idVendor, u16 idProduct)
{
struct sthcd_hcd *sthcd = oh->hcd;
struct sthcd_udev *udev;
struct sthcd_port *port;
unsigned long flags;
int error;
drv_printk(KERN_INFO, "inserting device %04X.%04X\n",
idVendor, idProduct);
udev = sthcd_get_free_udev(sthcd);
if (!udev) {
drv_printk(KERN_ERR, "no free udevs!\n");
return -EBUSY;
}
error = sthcd_udev_init(udev, oh, idVendor, idProduct);
if (!error) {
spin_lock_irqsave(&sthcd->lock, flags);
port = udev_to_port(udev);
/* notify a connection event */
port->status_change = USB_PORT_STAT_POWER |
USB_PORT_STAT_CONNECTION |
(USB_PORT_STAT_C_CONNECTION<<16);
spin_unlock_irqrestore(&sthcd->lock, flags);
}
return error;
}
static int sthcd_oh_remove_udev(struct sthcd_oh *oh,
u16 idVendor, u16 idProduct)
{
struct sthcd_hcd *sthcd = oh->hcd;
struct sthcd_udev *udev;
struct sthcd_port *port;
u32 old_status;
unsigned long flags;
int error = 0;
udev = sthcd_find_udev_by_ids(sthcd, idVendor, idProduct);
if (!udev) {
/* normally reached for ignored hubs */
error = -ENODEV;
} else {
drv_printk(KERN_INFO, "removing device %04X.%04X\n",
idVendor, idProduct);
sthcd_udev_exit(udev);
spin_lock_irqsave(&sthcd->lock, flags);
port = udev_to_port(udev);
clear_bit(__STHCD_PORT_INUSE, &port->flags);
clear_bit(__STHCD_PORT_DOOMED, &port->flags);
port->nr_resets = 0;
/* notify a disconnection event */
old_status = port->status_change;
port->status_change = USB_PORT_STAT_POWER;
if ((old_status & USB_PORT_STAT_CONNECTION) != 0)
port->status_change |= (USB_PORT_STAT_C_CONNECTION<<16);
spin_unlock_irqrestore(&sthcd->lock, flags);
}
return error;
}
/*
* Non-atomic context (synchronous call).
*/
static int sthcd_usb_control_msg(int fd,
__u8 request, __u8 requesttype,
__u16 value, __u16 index,
void *data, __u16 size,
int timeout)
{
struct sthcd_ctrl_params_in *params_in;
struct starlet_ioh_sg in[6];
struct starlet_ioh_sg io[1];
int error;
params_in = starlet_ioh_kzalloc(sizeof(*params_in));
if (!params_in) {
error = -ENOMEM;
goto done;
}
params_in->req.bRequestType = requesttype;
params_in->req.bRequest = request;
params_in->req.wValue = cpu_to_le16p(&value);
params_in->req.wIndex = cpu_to_le16p(&index);
params_in->req.wLength = cpu_to_le16p(&size);
params_in->_unk1 = timeout; /* seconds? */
starlet_ioh_sg_init_table(in, 6);
starlet_ioh_sg_entry(&in[0], &params_in->req.bRequestType);
starlet_ioh_sg_entry(&in[1], &params_in->req.bRequest);
starlet_ioh_sg_entry(&in[2], &params_in->req.wValue);
starlet_ioh_sg_entry(&in[3], &params_in->req.wIndex);
starlet_ioh_sg_entry(&in[4], &params_in->req.wLength);
starlet_ioh_sg_entry(&in[5], &params_in->_unk1);
starlet_ioh_sg_init_table(io, 1);
starlet_ioh_sg_set_buf(&io[0], data, size);
error = starlet_ioh_ioctlv(fd, STHCD_IOCTLV_CONTROLREQ,
6, in, 1, io);
starlet_ioh_kfree(params_in);
if (error > 0) {
/* adjust size for successful control xfers */
error -= sizeof(struct usb_ctrlrequest);
if (error < 0)
error = -EINVAL;
}
done:
if (error < 0)
DBG("%s: error=%d (%x)\n", __func__, error, error);
return error;
}
static int sthcd_oh_check_hub(struct sthcd_oh *oh, u16 idVendor, u16 idProduct)
{
char pathname[32];
struct usb_device_descriptor *descriptor;
int fd;
int i;
int retval;
descriptor = starlet_ioh_kzalloc(USB_DT_DEVICE_SIZE);
if (!descriptor) {
retval = -ENOMEM;
goto done;
}
snprintf(pathname, sizeof(pathname), "/dev/usb/oh%u/%04x/%04x",
oh->index, idVendor, idProduct);
retval = starlet_open(pathname, 0);
if (retval < 0) {
drv_printk(KERN_ERR, "open %s failed\n", pathname);
starlet_ioh_kfree(descriptor);
goto done;
}
fd = retval;
for (i = 0; i < 3; i++) {
retval = sthcd_usb_control_msg(fd, USB_REQ_GET_DESCRIPTOR,
USB_DIR_IN,
USB_DT_DEVICE << 8, 0,
descriptor, USB_DT_DEVICE_SIZE,
0);
if (retval != -7005)
break;
DBG("%s: attempt %d, retval=%d (%x)\n", __func__,
i, retval, retval);
}
starlet_close(fd);
if (retval >= USB_DT_DEVICE_SIZE) {
/* tell if a hub was found */
retval = (descriptor->bDeviceClass == USB_CLASS_HUB) ? 1 : 0;
} else {
if (retval >= 0)
retval = -EINVAL; /* short descriptor */
}
starlet_ioh_kfree(descriptor);
done:
if (retval < 0)
DBG("%s: retval=%d (%x)\n", __func__, retval, retval);
return retval;
}
struct sthcd_getdevicelist_params_in {
u8 devid_count;
u8 _type;
};
struct sthcd_getdevicelist_params_io {
u8 devid_count;
struct sthcd_devid devids[0];
};
static int sthcd_get_device_list(struct sthcd_hcd *sthcd, int fd,
struct sthcd_devid *devids, size_t nr_devids)
{
struct starlet_ioh_sg in[2], io[2];
struct sthcd_getdevicelist_params_in *params_in;
struct sthcd_getdevicelist_params_io *params_io;
size_t size = nr_devids * sizeof(struct sthcd_devid);
int error;
if (!nr_devids)
return -EINVAL;
params_in = starlet_ioh_kzalloc(sizeof(*params_in));
if (!params_in)
return -ENOMEM;
params_io = starlet_ioh_kzalloc(sizeof(*params_io) + size);
if (!params_io) {
starlet_ioh_kfree(params_in);
return -ENOMEM;
}
params_in->devid_count = nr_devids;
params_in->_type = 0;
starlet_ioh_sg_init_table(in, 2);
starlet_ioh_sg_entry(&in[0], &params_in->devid_count);
starlet_ioh_sg_entry(&in[1], &params_in->_type);
starlet_ioh_sg_init_table(io, 2);
starlet_ioh_sg_entry(&io[0], &params_io->devid_count);
starlet_ioh_sg_set_buf(&io[1], &params_io->devids, size);
error = starlet_ioh_ioctlv(fd, STHCD_IOCTLV_GETDEVICELIST,
2, in, 2, io);
if (error < 0) {
DBG("%s: error=%d (%x)\n", __func__, error, error);
} else {
memcpy(devids, params_io->devids, size);
error = params_io->devid_count;
}
starlet_ioh_kfree(params_in);
starlet_ioh_kfree(params_io);
return error;
}
static int sthcd_devid_match(struct sthcd_devid *id1, struct sthcd_devid *id2)
{
return id1->idVendor == id2->idVendor &&
id1->idProduct == id2->idProduct;
}
static int sthcd_devid_find(struct sthcd_devid *haystack, size_t count,
struct sthcd_devid *needle)
{
unsigned int i;
for (i = 0; i < count; i++) {
if (sthcd_devid_match(&haystack[i], needle))
return 1;
}
return 0;
}
static int sthcd_oh_rescan(struct sthcd_oh *oh)
{
static unsigned int poll_cycles;
struct usb_hcd *hcd = sthcd_to_hcd(oh->hcd);
struct sthcd_devid *p;
int nr_new_devids, i;
int changes;
int error;
error = sthcd_get_device_list(oh->hcd, oh->fd, oh->new_devids,
oh->max_devids);
if (error < 0)
return error;
nr_new_devids = error;
changes = 0;
for (i = 0; i < oh->nr_devids; i++) {
p = &oh->devids[i];
if (!sthcd_devid_find(oh->new_devids, nr_new_devids, p)) {
/* removal */
error = sthcd_oh_remove_udev(oh, p->idVendor,
p->idProduct);
if (!error)
changes++;
}
}
for (i = 0; i < nr_new_devids; i++) {
p = &oh->new_devids[i];
if (!sthcd_devid_find(oh->devids, oh->nr_devids, p)) {
/* insertion */
error = sthcd_oh_check_hub(oh, p->idVendor,
p->idProduct);
if (error == 0) {
/* not a hub, register the usb device */
error = sthcd_oh_insert_udev(oh, p->idVendor,
p->idProduct);
if (!error)
changes++;
} else {
drv_printk(KERN_INFO,
"ignoring hub %04X.%04X\n",
p->idVendor, p->idProduct);
}
}
}
memcpy(oh->devids, oh->new_devids, nr_new_devids * sizeof(*p));
oh->nr_devids = nr_new_devids;
/*
* FIXME
* We ask here the USB layer to explicitly poll for root hub changes
* until we get at least two complete rescan cycles without changes.
*
* Otherwise, for unknown reasons, we end up missing the detection of
* some devices, even if the insertion/removal of these devices is
* properly signaled in port->status_change.
*/
if (changes) {
#if 1
if (!poll_cycles) {
hcd->poll_rh = 1;
usb_hcd_poll_rh_status(hcd);
}
poll_cycles = 2;
} else {
if (!poll_cycles)
hcd->poll_rh = 0;
else
poll_cycles--;
#else
usb_hcd_poll_rh_status(hcd);
#endif
}
return 0;
}
static int sthcd_oh_init(struct sthcd_oh *oh, unsigned int index,
struct sthcd_hcd *sthcd, size_t max_devids)
{
char pathname[16];
int error;
if (index != 0 && index != 1)
return -EINVAL;
snprintf(pathname, sizeof(pathname), "/dev/usb/oh%u", index);
error = starlet_open(pathname, 0);
if (error < 0)
return error;
oh->fd = error;
oh->devids = kzalloc(2 * max_devids * sizeof(struct sthcd_devid),
GFP_KERNEL);
if (!oh->devids) {
starlet_close(oh->fd);
return -ENOMEM;
}
oh->new_devids = oh->devids + max_devids;
oh->max_devids = max_devids;
oh->nr_devids = 0;
oh->index = index;
oh->hcd = sthcd;
return 0;
}
static void sthcd_oh_exit(struct sthcd_oh *oh)
{
starlet_close(oh->fd);
oh->fd = -1;
kfree(oh->devids);
oh->devids = NULL;
}
static int sthcd_rescan_thread(void *arg)
{
struct sthcd_hcd *sthcd = arg;
struct sthcd_oh *oh;
/*
* REVISIT
* We may need to rescan oh1 if bluetooth dongle disconnects.
*/
/* oh1 has non-removable devices only, so just scan it once */
sthcd_oh_rescan(&sthcd->oh[1]);
oh = &sthcd->oh[0];
while (!kthread_should_stop()) {
sthcd_oh_rescan(oh);
/* re-check again after the configured interval */
sleep_on_timeout(&sthcd->rescan_waitq,
STHCD_RESCAN_INTERVAL*HZ);
}
return 0;
}
/*
*
*
*/
static int sthcd_init(struct usb_hcd *hcd)
{
return 0;
}
static int sthcd_start(struct usb_hcd *hcd)
{
struct sthcd_hcd *sthcd = hcd_to_sthcd(hcd);
int error;
/*
* This is to prevent a spurious error from the kernel usb stack
* as we do not make use of interrupts.
*/
set_bit(HCD_FLAG_SAW_IRQ, &hcd->flags);
hcd->uses_new_polling = 1;
/* oh0 is the external bus */
error = sthcd_oh_init(&sthcd->oh[0], 0, sthcd, STHCD_MAX_DEVIDS);
if (error < 0) {
DBG("%s: error=%d (%x)\n", __func__, error, error);
return error;
}
/* oh1 is the internal bus, used only by the bluetooth dongle */
error = sthcd_oh_init(&sthcd->oh[1], 1, sthcd, 1);
if (error < 0) {
DBG("%s: error=%d (%x)\n", __func__, error, error);
sthcd_oh_exit(&sthcd->oh[0]);
return error;
}
hcd->state = HC_STATE_RUNNING;
/* device insertion/removal is managed by the rescan thread */
sthcd->rescan_task = kthread_run(sthcd_rescan_thread, sthcd, "ksthcd");
if (IS_ERR(sthcd->rescan_task))
drv_printk(KERN_ERR, "failed to start rescan thread\n");
return 0;
}
static void sthcd_stop(struct usb_hcd *hcd)
{
struct sthcd_hcd *sthcd = hcd_to_sthcd(hcd);
if (!IS_ERR(sthcd->rescan_task)) {
kthread_stop(sthcd->rescan_task);
sthcd->rescan_task = ERR_PTR(-EINVAL);
}
sthcd_oh_exit(&sthcd->oh[0]);
sthcd_oh_exit(&sthcd->oh[1]);
hcd->state &= ~HC_STATE_RUNNING;
}
static int sthcd_get_frame_number(struct usb_hcd *hcd)
{
DBG("%s: CALLED\n", __func__);
return 0;
}
static int sthcd_urb_enqueue(struct usb_hcd *hcd, struct urb *urb,
gfp_t mem_flags)
{
struct sthcd_hcd *sthcd = hcd_to_sthcd(hcd);
struct usb_host_endpoint *ep;
struct sthcd_pep *pep;
unsigned long flags;
int error;
spin_lock_irqsave(&sthcd->lock, flags);
/* REVISIT, paranoid */
if (urb->status != -EINPROGRESS) {
DBG("%s: status != -EINPROGRESS\n", __func__);
error = urb->status;
goto done;
}
error = usb_hcd_link_urb_to_ep(hcd, urb);
if (error)
goto done;
ep = urb->ep;
/* allocate a pep for each endpoint on first use */
if (!ep->hcpriv) {
pep = sthcd_pep_alloc(sthcd, ep);
if (!pep) {
error = -ENOMEM;
goto err_linked;
}
ep->hcpriv = pep;
} else {
pep = ep->hcpriv;
}
error = sthcd_pep_send_urb(pep, urb);
if (!error)
goto done;
err_linked:
usb_hcd_unlink_urb_from_ep(hcd, urb);
done:
spin_unlock_irqrestore(&sthcd->lock, flags);
return error;
}
static int sthcd_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
{
struct sthcd_hcd *sthcd = hcd_to_sthcd(hcd);
struct usb_host_endpoint *ep;
struct sthcd_pep *pep;
unsigned long flags;
int error;
spin_lock_irqsave(&sthcd->lock, flags);
error = usb_hcd_check_unlink_urb(hcd, urb, status);
if (error)
goto done;
ep = urb->ep;
pep = ep_to_pep(ep);
if (pep && pep->urb == urb) {
/*
* There is an urb in flight.
*
* We deattach the urb from the pep and leave the pep to the
* callback function, which will free it upon completion,
* without further action.
*/
sthcd_pep_takeout_urb(pep);
ep->hcpriv = NULL;
}
usb_hcd_unlink_urb_from_ep(hcd, urb);
sthcd_giveback_urb(sthcd, urb, status);
done:
spin_unlock_irqrestore(&sthcd->lock, flags);
#if 0
if (error < 0)
DBG("%s: error=%d (%x)\n", __func__, error, error);
#endif
return error;
}
static void sthcd_endpoint_disable(struct usb_hcd *hcd,
struct usb_host_endpoint *ep)
{
struct sthcd_hcd *sthcd = hcd_to_sthcd(hcd);
struct sthcd_pep *pep;
unsigned long flags;
spin_lock_irqsave(&sthcd->lock, flags);
pep = ep->hcpriv;
/* do nothing if the pep was already freed */
if (!pep)
goto done;
if (pep->urb) {
/*
* There is an urb in flight.
*
* Disable the private endpoint and take the urb out of it.
* The callback function will take care of freeing the pep
* when the starlet call completes.
*/
set_bit(__STHCD_PEP_DISABLED, &pep->flags);
sthcd_pep_takeout_urb(pep);
} else {
/* the pep can be freed immediately when no urb is in flight */
sthcd_pep_free(pep);
}
ep->hcpriv = NULL;
done:
spin_unlock_irqrestore(&sthcd->lock, flags);
}
static const struct hc_driver starlet_hc_driver = {
.description = DRV_MODULE_NAME,
.product_desc = "Nintendo Wii USB Host Controller",
.hcd_priv_size = sizeof(struct sthcd_hcd),
.irq = NULL,
.flags = HCD_USB11,
/* REVISIT, power management calls not yet supported */
.reset = sthcd_init,
.start = sthcd_start,
.stop = sthcd_stop,
.get_frame_number = sthcd_get_frame_number,
.urb_enqueue = sthcd_urb_enqueue,
.urb_dequeue = sthcd_urb_dequeue,
.endpoint_disable = sthcd_endpoint_disable,
.hub_status_data = sthcd_hub_status_data,
.hub_control = sthcd_hub_control,
};
static int __devinit sthcd_driver_probe(struct device *dev)
{
struct sthcd_hcd *sthcd;
struct usb_hcd *hcd;
int error = -ENOMEM;
/*
* We can't use normal dma as starlet requires MEM2 buffers
* to work properly in all cases.
*/
dev->dma_mask = NULL;
hcd = usb_create_hcd(&starlet_hc_driver, dev, DRV_MODULE_NAME);
if (!hcd)
goto err;
sthcd = hcd_to_sthcd(hcd);
spin_lock_init(&sthcd->lock);
sthcd->nr_ports = STHCD_MAX_PORTS;
sthcd->ports = kzalloc(sthcd->nr_ports * sizeof(struct sthcd_port),
GFP_KERNEL);
if (!sthcd->ports)
goto err_alloc_ports;
INIT_LIST_HEAD(&sthcd->device_list);
init_waitqueue_head(&sthcd->rescan_waitq);
error = usb_add_hcd(hcd, 0, 0);
if (error) {
drv_printk(KERN_INFO, "%s: error %d adding hcd\n",
__func__, error);
goto err_add;
}
return 0;
err_add:
kfree(sthcd->ports);
err_alloc_ports:
usb_put_hcd(hcd);
err:
return error;
}
static int __devexit sthcd_driver_remove(struct device *dev)
{
struct usb_hcd *hcd = dev_get_drvdata(dev);
usb_remove_hcd(hcd);
usb_put_hcd(hcd);
return 0;
}
/*
* Open Firmware platform device routines
*
*/
static int __init sthcd_of_probe(struct of_device *odev,
const struct of_device_id *match)
{
return sthcd_driver_probe(&odev->dev);
}
static int __exit sthcd_of_remove(struct of_device *odev)
{
return sthcd_driver_remove(&odev->dev);
}
static struct of_device_id sthcd_of_match[] = {
{ .compatible = "nintendo,starlet-hcd" },
{ },
};
MODULE_DEVICE_TABLE(of, sthcd_of_match);
static struct of_platform_driver sthcd_of_driver = {
.owner = THIS_MODULE,
.name = DRV_MODULE_NAME,
.match_table = sthcd_of_match,
.probe = sthcd_of_probe,
.remove = sthcd_of_remove,
};
/*
* Linux module framework
*
*/
static int __init sthcd_module_init(void)
{
if (usb_disabled())
return -ENODEV;
drv_printk(KERN_INFO, "%s - version %s\n", DRV_DESCRIPTION,
sthcd_driver_version);
return of_register_platform_driver(&sthcd_of_driver);
}
static void __exit sthcd_module_exit(void)
{
of_unregister_platform_driver(&sthcd_of_driver);
}
module_init(sthcd_module_init);
module_exit(sthcd_module_exit);
MODULE_AUTHOR(DRV_AUTHOR);
MODULE_DESCRIPTION(DRV_DESCRIPTION);
MODULE_LICENSE("GPL");