1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
6031
6032
6033
6034
6035
6036
6037
6038
6039
6040
6041
6042
6043
6044
6045
6046
6047
6048
6049
6050
6051
6052
6053
6054
6055
6056
6057
6058
6059
6060
6061
6062
6063
6064
6065
6066
6067
6068
6069
6070
6071
6072
6073
6074
6075
6076
6077
6078
6079
6080
6081
6082
6083
6084
6085
6086
6087
6088
6089
6090
6091
6092
6093
6094
6095
6096
6097
6098
6099
6100
6101
6102
6103
6104
6105
6106
6107
6108
6109
6110
6111
6112
6113
6114
6115
6116
6117
6118
6119
6120
6121
6122
6123
6124
6125
6126
6127
6128
6129
6130
6131
6132
6133
6134
6135
6136
6137
6138
6139
|
/* $Id: DevOHCI.cpp $ */
/** @file
* DevOHCI - Open Host Controller Interface for USB.
*/
/*
* Copyright (C) 2006-2019 Oracle Corporation
*
* This file is part of VirtualBox Open Source Edition (OSE), as
* available from http://www.virtualbox.org. This file is free software;
* you can redistribute it and/or modify it under the terms of the GNU
* General Public License (GPL) as published by the Free Software
* Foundation, in version 2 as it comes in the "COPYING" file of the
* VirtualBox OSE distribution. VirtualBox OSE is distributed in the
* hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
*/
/** @page pg_dev_ohci OHCI - Open Host Controller Interface Emulation.
*
* This component implements an OHCI USB controller. It is split roughly in
* to two main parts, the first part implements the register level
* specification of USB OHCI and the second part maintains the root hub (which
* is an integrated component of the device).
*
* The OHCI registers are used for the usual stuff like enabling and disabling
* interrupts. Since the USB time is divided in to 1ms frames and various
* interrupts may need to be triggered at frame boundary time, a timer-based
* approach was taken. Whenever the bus is enabled ohci->eof_timer will be set.
*
* The actual USB transfers are stored in main memory (along with endpoint and
* transfer descriptors). The ED's for all the control and bulk endpoints are
* found by consulting the HcControlHeadED and HcBulkHeadED registers
* respectively. Interrupt ED's are different, they are found by looking
* in the HCCA (another communication area in main memory).
*
* At the start of every frame (in function ohci_sof) we traverse all enabled
* ED lists and queue up as many transfers as possible. No attention is paid
* to control/bulk service ratios or bandwidth requirements since our USB
* could conceivably contain a dozen high speed busses so this would
* artificially limit the performance.
*
* Once we have a transfer ready to go (in function ohciR3ServiceTd) we
* allocate an URB on the stack, fill in all the relevant fields and submit
* it using the VUSBIRhSubmitUrb function. The roothub device and the virtual
* USB core code (vusb.c) coordinates everything else from this point onwards.
*
* When the URB has been successfully handed to the lower level driver, our
* prepare callback gets called and we can remove the TD from the ED transfer
* list. This stops us queueing it twice while it completes.
* bird: no, we don't remove it because that confuses the guest! (=> crashes)
*
* Completed URBs are reaped at the end of every frame (in function
* ohci_frame_boundary). Our completion routine makes use of the ED and TD
* fields in the URB to store the physical addresses of the descriptors so
* that they may be modified in the roothub callbacks. Our completion
* routine (ohciRhXferComplete) carries out a number of tasks:
* -# Retires the TD associated with the transfer, setting the
* relevant error code etc.
* -# Updates done-queue interrupt timer and potentially causes
* a writeback of the done-queue.
* -# If the transfer was device-to-host, we copy the data in to
* the host memory.
*
* As for error handling OHCI allows for 3 retries before failing a transfer,
* an error count is stored in each transfer descriptor. A halt flag is also
* stored in the transfer descriptor. That allows for ED's to be disabled
* without stopping the bus and de-queuing them.
*
* When the bus is started and stopped we call VUSBIDevPowerOn/Off() on our
* roothub to indicate it's powering up and powering down. Whenever we power
* down, the USB core makes sure to synchronously complete all outstanding
* requests so that the OHCI is never seen in an inconsistent state by the
* guest OS (Transfers are not meant to be unlinked until they've actually
* completed, but we can't do that unless we work synchronously, so we just
* have to fake it).
* bird: we do work synchronously now, anything causes guest crashes.
*/
/*********************************************************************************************************************************
* Header Files *
*********************************************************************************************************************************/
#define LOG_GROUP LOG_GROUP_DEV_OHCI
#include <VBox/pci.h>
#include <VBox/vmm/pdm.h>
#include <VBox/vmm/mm.h>
#include <VBox/err.h>
#include <VBox/log.h>
#include <iprt/assert.h>
#include <iprt/string.h>
#include <iprt/asm.h>
#include <iprt/asm-math.h>
#include <iprt/semaphore.h>
#include <iprt/critsect.h>
#include <iprt/param.h>
#ifdef IN_RING3
# include <iprt/alloca.h>
# include <iprt/mem.h>
# include <iprt/thread.h>
# include <iprt/uuid.h>
#endif
#include <VBox/vusb.h>
#include "VBoxDD.h"
#define VBOX_WITH_OHCI_PHYS_READ_CACHE
//#define VBOX_WITH_OHCI_PHYS_READ_STATS
/*********************************************************************************************************************************
* Structures and Typedefs *
*********************************************************************************************************************************/
/** The current saved state version. */
#define OHCI_SAVED_STATE_VERSION 5 /* Introduced post-4.3. */
/** The saved state with support of up to 8 ports. */
#define OHCI_SAVED_STATE_VERSION_8PORTS 4 /* Introduced in 3.1 or so. */
/** Maximum supported number of Downstream Ports on the root hub. 15 ports
* is the maximum defined by the OHCI spec. Must match the number of status
* register words to the 'opreg' array.
*/
#define OHCI_NDP_MAX 15
/** Default NDP, chosen to be compatible with everything. */
#define OHCI_NDP_DEFAULT 12
/* Macro to query the number of currently configured ports. */
#define OHCI_NDP_CFG(pohci) ((pohci)->RootHub.desc_a & OHCI_RHA_NDP)
/** Pointer to OHCI device data. */
typedef struct OHCI *POHCI;
/** Read-only pointer to the OHCI device data. */
typedef struct OHCI const *PCOHCI;
#ifndef VBOX_DEVICE_STRUCT_TESTCASE
/**
* Host controller transfer descriptor data.
*/
typedef struct VUSBURBHCITDINT
{
/** Type of TD. */
uint32_t TdType;
/** The address of the */
RTGCPHYS32 TdAddr;
/** A copy of the TD. */
uint32_t TdCopy[16];
} VUSBURBHCITDINT;
/**
* The host controller data associated with each URB.
*/
typedef struct VUSBURBHCIINT
{
/** The endpoint descriptor address. */
RTGCPHYS32 EdAddr;
/** Number of Tds in the array. */
uint32_t cTds;
/** When this URB was created.
* (Used for isochronous frames and for logging.) */
uint32_t u32FrameNo;
/** Flag indicating that the TDs have been unlinked. */
bool fUnlinked;
} VUSBURBHCIINT;
#endif
/**
* An OHCI root hub port.
*/
typedef struct OHCIHUBPORT
{
/** The port register. */
uint32_t fReg;
#if HC_ARCH_BITS == 64
uint32_t Alignment0; /**< Align the pointer correctly. */
#endif
/** The device attached to the port. */
R3PTRTYPE(PVUSBIDEVICE) pDev;
} OHCIHUBPORT;
#if HC_ARCH_BITS == 64
AssertCompile(sizeof(OHCIHUBPORT) == 16); /* saved state */
#endif
/** Pointer to an OHCI hub port. */
typedef OHCIHUBPORT *POHCIHUBPORT;
/**
* The OHCI root hub.
*
* @implements PDMIBASE
* @implements VUSBIROOTHUBPORT
* @implements PDMILEDPORTS
*/
typedef struct ohci_roothub
{
/** Pointer to the base interface of the VUSB RootHub. */
R3PTRTYPE(PPDMIBASE) pIBase;
/** Pointer to the connector interface of the VUSB RootHub. */
R3PTRTYPE(PVUSBIROOTHUBCONNECTOR) pIRhConn;
/** Pointer to the device interface of the VUSB RootHub. */
R3PTRTYPE(PVUSBIDEVICE) pIDev;
/** The base interface exposed to the roothub driver. */
PDMIBASE IBase;
/** The roothub port interface exposed to the roothub driver. */
VUSBIROOTHUBPORT IRhPort;
/** The LED. */
PDMLED Led;
/** The LED ports. */
PDMILEDPORTS ILeds;
/** Partner of ILeds. */
R3PTRTYPE(PPDMILEDCONNECTORS) pLedsConnector;
uint32_t status;
uint32_t desc_a;
uint32_t desc_b;
#if HC_ARCH_BITS == 64
uint32_t Alignment0; /**< Align aPorts on a 8 byte boundary. */
#endif
OHCIHUBPORT aPorts[OHCI_NDP_MAX];
R3PTRTYPE(POHCI) pOhci;
} OHCIROOTHUB;
/** Pointer to the OHCI root hub. */
typedef OHCIROOTHUB *POHCIROOTHUB;
/**
* Data used for reattaching devices on a state load.
*/
typedef struct ohci_load {
/** Timer used once after state load to inform the guest about new devices.
* We do this to be sure the guest get any disconnect / reconnect on the
* same port. */
PTMTIMERR3 pTimer;
/** Number of detached devices. */
unsigned cDevs;
/** Array of devices which were detached. */
PVUSBIDEVICE apDevs[OHCI_NDP_MAX];
} OHCILOAD;
/** Pointer to an OHCILOAD structure. */
typedef OHCILOAD *POHCILOAD;
#ifdef VBOX_WITH_OHCI_PHYS_READ_CACHE
typedef struct OHCIPAGECACHE
{
/** Last read physical page address. */
RTGCPHYS GCPhysReadCacheAddr;
/** Copy of last read physical page. */
uint8_t au8PhysReadCache[PAGE_SIZE];
} OHCIPAGECACHE, *POHCIPAGECACHE;
#endif
/**
* OHCI device data.
*/
typedef struct OHCI
{
/** The PCI device. */
PDMPCIDEV PciDev;
/** Pointer to the device instance - R3 ptr. */
PPDMDEVINSR3 pDevInsR3;
/** The End-Of-Frame timer - R3 Ptr. */
PTMTIMERR3 pEndOfFrameTimerR3;
/** Pointer to the device instance - R0 ptr */
PPDMDEVINSR0 pDevInsR0;
/** The End-Of-Frame timer - R0 Ptr. */
PTMTIMERR0 pEndOfFrameTimerR0;
/** Pointer to the device instance - RC ptr. */
PPDMDEVINSRC pDevInsRC;
/** The End-Of-Frame timer - RC Ptr. */
PTMTIMERRC pEndOfFrameTimerRC;
/** Start of current frame. */
uint64_t SofTime;
/* done queue interrupt counter */
uint32_t dqic : 3;
/** frame number overflow. */
uint32_t fno : 1;
/** Address of the MMIO region assigned by PCI. */
RTGCPHYS32 MMIOBase;
/* Root hub device */
OHCIROOTHUB RootHub;
/* OHCI registers */
/** @name Control partition
* @{ */
/** HcControl. */
uint32_t ctl;
/** HcCommandStatus. */
uint32_t status;
/** HcInterruptStatus. */
uint32_t intr_status;
/** HcInterruptEnabled. */
uint32_t intr;
/** @} */
/** @name Memory pointer partition
* @{ */
/** HcHCCA. */
uint32_t hcca;
/** HcPeriodCurrentEd. */
uint32_t per_cur;
/** HcControlCurrentED. */
uint32_t ctrl_cur;
/** HcControlHeadED. */
uint32_t ctrl_head;
/** HcBlockCurrendED. */
uint32_t bulk_cur;
/** HcBlockHeadED. */
uint32_t bulk_head;
/** HcDoneHead. */
uint32_t done;
/** @} */
/** @name Frame counter partition
* @{ */
/** HcFmInterval.FSMPS - FSLargestDataPacket */
uint32_t fsmps : 15;
/** HcFmInterval.FIT - FrameItervalToggle */
uint32_t fit : 1;
/** HcFmInterval.FI - FrameInterval */
uint32_t fi : 14;
/** HcFmRemaining.FRT - toggle bit. */
uint32_t frt : 1;
/** HcFmNumber.
* @remark The register size is 16-bit, but for debugging and performance
* reasons we maintain a 32-bit counter. */
uint32_t HcFmNumber;
/** HcPeriodicStart */
uint32_t pstart;
/** @} */
/** The number of virtual time ticks per frame. */
uint64_t cTicksPerFrame;
/** The number of virtual time ticks per USB bus tick. */
uint64_t cTicksPerUsbTick;
/** Number of in-flight TDs. */
unsigned cInFlight;
unsigned Alignment0; /**< Align aInFlight on a 8 byte boundary. */
/** Array of in-flight TDs. */
struct ohci_td_in_flight
{
/** Address of the transport descriptor. */
uint32_t GCPhysTD;
/** Flag indicating an inactive (not-linked) URB. */
bool fInactive;
/** Pointer to the URB. */
R3PTRTYPE(PVUSBURB) pUrb;
} aInFlight[257];
#if HC_ARCH_BITS == 32
uint32_t Alignment1;
#endif
/** Number of in-done-queue TDs. */
unsigned cInDoneQueue;
/** Array of in-done-queue TDs. */
struct ohci_td_in_done_queue
{
/** Address of the transport descriptor. */
uint32_t GCPhysTD;
} aInDoneQueue[64];
/** When the tail of the done queue was added.
* Used to calculate the age of the done queue. */
uint32_t u32FmDoneQueueTail;
#if R3_ARCH_BITS == 32
/** Align pLoad, the stats and the struct size correctly. */
uint32_t Alignment2;
#endif
/** Pointer to state load data. */
R3PTRTYPE(POHCILOAD) pLoad;
/** Detected canceled isochronous URBs. */
STAMCOUNTER StatCanceledIsocUrbs;
/** Detected canceled general URBs. */
STAMCOUNTER StatCanceledGenUrbs;
/** Dropped URBs (endpoint halted, or URB canceled). */
STAMCOUNTER StatDroppedUrbs;
/** Profiling ohciR3FrameBoundaryTimer. */
STAMPROFILE StatTimer;
/** This member and all the following are not part of saved state. */
uint64_t SavedStateEnd;
/** VM timer frequency used for frame timer calculations. */
uint64_t u64TimerHz;
/** Idle detection flag; must be cleared at start of frame */
bool fIdle;
/** A flag indicating that the bulk list may have in-flight URBs. */
bool fBulkNeedsCleaning;
/** Whether RC/R0 is enabled. */
bool fRZEnabled;
uint32_t Alignment3; /**< Align size on a 8 byte boundary. */
/** Critical section synchronising interrupt handling. */
PDMCRITSECT CsIrq;
/** Critical section to synchronize the framer and URB completion handler. */
RTCRITSECT CritSect;
#ifdef VBOX_WITH_OHCI_PHYS_READ_CACHE
/** Last read physical page for caching ED reads in the framer thread. */
R3PTRTYPE(POHCIPAGECACHE) pCacheED;
/** Last read physical page for caching TD reads in the framer thread. */
R3PTRTYPE(POHCIPAGECACHE) pCacheTD;
#endif
} OHCI;
/* Standard OHCI bus speed */
#define OHCI_DEFAULT_TIMER_FREQ 1000
/* Host Controller Communications Area */
#define OHCI_HCCA_NUM_INTR 32
#define OHCI_HCCA_OFS (OHCI_HCCA_NUM_INTR * sizeof(uint32_t))
struct ohci_hcca
{
uint16_t frame;
uint16_t pad;
uint32_t done;
};
AssertCompileSize(ohci_hcca, 8);
/** @name OHCI Endpoint Descriptor
* @{ */
#define ED_PTR_MASK (~(uint32_t)0xf)
#define ED_HWINFO_MPS 0x07ff0000
#define ED_HWINFO_ISO RT_BIT(15)
#define ED_HWINFO_SKIP RT_BIT(14)
#define ED_HWINFO_LOWSPEED RT_BIT(13)
#define ED_HWINFO_IN RT_BIT(12)
#define ED_HWINFO_OUT RT_BIT(11)
#define ED_HWINFO_DIR (RT_BIT(11) | RT_BIT(12))
#define ED_HWINFO_ENDPOINT 0x780 /* 4 bits */
#define ED_HWINFO_ENDPOINT_SHIFT 7
#define ED_HWINFO_FUNCTION 0x7f /* 7 bits */
#define ED_HEAD_CARRY RT_BIT(1)
#define ED_HEAD_HALTED RT_BIT(0)
/**
* OHCI Endpoint Descriptor.
*/
typedef struct OHCIED
{
/** Flags and stuff. */
uint32_t hwinfo;
/** TailP - TD Queue Tail pointer. Bits 0-3 ignored / preserved. */
uint32_t TailP;
/** HeadP - TD Queue head pointer. Bit 0 - Halted, Bit 1 - toggleCarry. Bit 2&3 - 0. */
uint32_t HeadP;
/** NextED - Next Endpoint Descriptor. Bits 0-3 ignored / preserved. */
uint32_t NextED;
} OHCIED, *POHCIED;
typedef const OHCIED *PCOHCIED;
/** @} */
AssertCompileSize(OHCIED, 16);
/** @name Completion Codes
* @{ */
#define OHCI_CC_NO_ERROR (UINT32_C(0x00) << 28)
#define OHCI_CC_CRC (UINT32_C(0x01) << 28)
#define OHCI_CC_STALL (UINT32_C(0x04) << 28)
#define OHCI_CC_DEVICE_NOT_RESPONDING (UINT32_C(0x05) << 28)
#define OHCI_CC_DNR OHCI_CC_DEVICE_NOT_RESPONDING
#define OHCI_CC_PID_CHECK_FAILURE (UINT32_C(0x06) << 28)
#define OHCI_CC_UNEXPECTED_PID (UINT32_C(0x07) << 28)
#define OHCI_CC_DATA_OVERRUN (UINT32_C(0x08) << 28)
#define OHCI_CC_DATA_UNDERRUN (UINT32_C(0x09) << 28)
/* 0x0a..0x0b - reserved */
#define OHCI_CC_BUFFER_OVERRUN (UINT32_C(0x0c) << 28)
#define OHCI_CC_BUFFER_UNDERRUN (UINT32_C(0x0d) << 28)
#define OHCI_CC_NOT_ACCESSED_0 (UINT32_C(0x0e) << 28)
#define OHCI_CC_NOT_ACCESSED_1 (UINT32_C(0x0f) << 28)
/** @} */
/** @name OHCI General transfer descriptor
* @{ */
/** Error count (EC) shift. */
#define TD_ERRORS_SHIFT 26
/** Error count max. (One greater than what the EC field can hold.) */
#define TD_ERRORS_MAX 4
/** CC - Condition code mask. */
#define TD_HWINFO_CC (UINT32_C(0xf0000000))
#define TD_HWINFO_CC_SHIFT 28
/** EC - Error count. */
#define TD_HWINFO_ERRORS (RT_BIT(26) | RT_BIT(27))
/** T - Data toggle. */
#define TD_HWINFO_TOGGLE (RT_BIT(24) | RT_BIT(25))
#define TD_HWINFO_TOGGLE_HI (RT_BIT(25))
#define TD_HWINFO_TOGGLE_LO (RT_BIT(24))
/** DI - Delay interrupt. */
#define TD_HWINFO_DI (RT_BIT(21) | RT_BIT(22) | RT_BIT(23))
#define TD_HWINFO_IN (RT_BIT(20))
#define TD_HWINFO_OUT (RT_BIT(19))
/** DP - Direction / PID. */
#define TD_HWINFO_DIR (RT_BIT(19) | RT_BIT(20))
/** R - Buffer rounding. */
#define TD_HWINFO_ROUNDING (RT_BIT(18))
/** Bits that are reserved / unknown. */
#define TD_HWINFO_UNKNOWN_MASK (UINT32_C(0x0003ffff))
/** SETUP - to endpoint. */
#define OHCI_TD_DIR_SETUP 0x0
/** OUT - to endpoint. */
#define OHCI_TD_DIR_OUT 0x1
/** IN - from endpoint. */
#define OHCI_TD_DIR_IN 0x2
/** Reserved. */
#define OHCI_TD_DIR_RESERVED 0x3
/**
* OHCI general transfer descriptor
*/
typedef struct OHCITD
{
uint32_t hwinfo;
/** CBP - Current Buffer Pointer. (32-bit physical address) */
uint32_t cbp;
/** NextTD - Link to the next transfer descriptor. (32-bit physical address, dword aligned) */
uint32_t NextTD;
/** BE - Buffer End (inclusive). (32-bit physical address) */
uint32_t be;
} OHCITD, *POHCITD;
typedef const OHCITD *PCOHCITD;
/** @} */
AssertCompileSize(OHCIED, 16);
/** @name OHCI isochronous transfer descriptor.
* @{ */
/** SF - Start frame number. */
#define ITD_HWINFO_SF 0xffff
/** DI - Delay interrupt. (TD_HWINFO_DI) */
#define ITD_HWINFO_DI (RT_BIT(21) | RT_BIT(22) | RT_BIT(23))
#define ITD_HWINFO_DI_SHIFT 21
/** FC - Frame count. */
#define ITD_HWINFO_FC (RT_BIT(24) | RT_BIT(25) | RT_BIT(26))
#define ITD_HWINFO_FC_SHIFT 24
/** CC - Condition code mask. (=TD_HWINFO_CC) */
#define ITD_HWINFO_CC UINT32_C(0xf0000000)
#define ITD_HWINFO_CC_SHIFT 28
/** The buffer page 0 mask (lower 12 bits are ignored). */
#define ITD_BP0_MASK UINT32_C(0xfffff000)
#define ITD_NUM_PSW 8
/** OFFSET - offset of the package into the buffer page.
* (Only valid when CC set to Not Accessed.)
*
* Note that the top bit of the OFFSET field is overlapping with the
* first bit in the CC field. This is ok because both 0xf and 0xe are
* defined as "Not Accessed".
*/
#define ITD_PSW_OFFSET 0x1fff
/** SIZE field mask for IN bound transfers.
* (Only valid when CC isn't Not Accessed.)*/
#define ITD_PSW_SIZE 0x07ff
/** CC field mask.
* USed to indicate the format of SIZE (Not Accessed -> OFFSET). */
#define ITD_PSW_CC 0xf000
#define ITD_PSW_CC_SHIFT 12
/**
* OHCI isochronous transfer descriptor.
*/
typedef struct OHCIITD
{
uint32_t HwInfo;
/** BP0 - Buffer Page 0. The lower 12 bits are ignored. */
uint32_t BP0;
/** NextTD - Link to the next transfer descriptor. (32-bit physical address, dword aligned) */
uint32_t NextTD;
/** BE - Buffer End (inclusive). (32-bit physical address) */
uint32_t BE;
/** (OffsetN/)PSWN - package status word array (0..7).
* The format varies depending on whether the package has been completed or not. */
uint16_t aPSW[ITD_NUM_PSW];
} OHCIITD, *POHCIITD;
typedef const OHCIITD *PCOHCIITD;
/** @} */
AssertCompileSize(OHCIITD, 32);
/**
* OHCI register operator.
*/
typedef struct ohci_opreg
{
const char *pszName;
int (*pfnRead )(PCOHCI pThis, uint32_t iReg, uint32_t *pu32Value);
int (*pfnWrite)(POHCI pThis, uint32_t iReg, uint32_t u32Value);
} OHCIOPREG;
/* OHCI Local stuff */
#define OHCI_CTL_CBSR ((1<<0)|(1<<1)) /* Control/Bulk Service Ratio. */
#define OHCI_CTL_PLE (1<<2) /* Periodic List Enable. */
#define OHCI_CTL_IE (1<<3) /* Isochronous Enable. */
#define OHCI_CTL_CLE (1<<4) /* Control List Enable. */
#define OHCI_CTL_BLE (1<<5) /* Bulk List Enable. */
#define OHCI_CTL_HCFS ((1<<6)|(1<<7)) /* Host Controller Functional State. */
#define OHCI_USB_RESET 0x00
#define OHCI_USB_RESUME 0x40
#define OHCI_USB_OPERATIONAL 0x80
#define OHCI_USB_SUSPEND 0xc0
#define OHCI_CTL_IR (1<<8) /* Interrupt Routing (host/SMI). */
#define OHCI_CTL_RWC (1<<9) /* Remote Wakeup Connected. */
#define OHCI_CTL_RWE (1<<10) /* Remote Wakeup Enabled. */
#define OHCI_STATUS_HCR (1<<0) /* Host Controller Reset. */
#define OHCI_STATUS_CLF (1<<1) /* Control List Filled. */
#define OHCI_STATUS_BLF (1<<2) /* Bulk List Filled. */
#define OHCI_STATUS_OCR (1<<3) /* Ownership Change Request. */
#define OHCI_STATUS_SOC ((1<<6)|(1<<7)) /* Scheduling Overrun Count. */
/** @name Interrupt Status and Enabled/Disabled Flags
* @{ */
/** SO - Scheduling overrun. */
#define OHCI_INTR_SCHEDULING_OVERRUN RT_BIT(0)
/** WDH - HcDoneHead writeback. */
#define OHCI_INTR_WRITE_DONE_HEAD RT_BIT(1)
/** SF - Start of frame. */
#define OHCI_INTR_START_OF_FRAME RT_BIT(2)
/** RD - Resume detect. */
#define OHCI_INTR_RESUME_DETECT RT_BIT(3)
/** UE - Unrecoverable error. */
#define OHCI_INTR_UNRECOVERABLE_ERROR RT_BIT(4)
/** FNO - Frame number overflow. */
#define OHCI_INTR_FRAMENUMBER_OVERFLOW RT_BIT(5)
/** RHSC- Root hub status change. */
#define OHCI_INTR_ROOT_HUB_STATUS_CHANGE RT_BIT(6)
/** OC - Ownership change. */
#define OHCI_INTR_OWNERSHIP_CHANGE RT_BIT(30)
/** MIE - Master interrupt enable. */
#define OHCI_INTR_MASTER_INTERRUPT_ENABLED RT_BIT(31)
/** @} */
#define OHCI_HCCA_SIZE 0x100
#define OHCI_HCCA_MASK UINT32_C(0xffffff00)
#define OHCI_FMI_FI UINT32_C(0x00003fff) /* Frame Interval. */
#define OHCI_FMI_FSMPS UINT32_C(0x7fff0000) /* Full-Speed Max Packet Size. */
#define OHCI_FMI_FSMPS_SHIFT 16
#define OHCI_FMI_FIT UINT32_C(0x80000000) /* Frame Interval Toggle. */
#define OHCI_FMI_FIT_SHIFT 31
#define OHCI_FR_FRT RT_BIT_32(31) /* Frame Remaining Toggle */
#define OHCI_LS_THRESH 0x628 /* Low-Speed Threshold. */
#define OHCI_RHA_NDP (0xff) /* Number of Downstream Ports. */
#define OHCI_RHA_PSM RT_BIT_32(8) /* Power Switching Mode. */
#define OHCI_RHA_NPS RT_BIT_32(9) /* No Power Switching. */
#define OHCI_RHA_DT RT_BIT_32(10) /* Device Type. */
#define OHCI_RHA_OCPM RT_BIT_32(11) /* Over-Current Protection Mode. */
#define OHCI_RHA_NOCP RT_BIT_32(12) /* No Over-Current Protection. */
#define OHCI_RHA_POTPGP UINT32_C(0xff000000) /* Power On To Power Good Time. */
#define OHCI_RHS_LPS RT_BIT_32(0) /* Local Power Status. */
#define OHCI_RHS_OCI RT_BIT_32(1) /* Over-Current Indicator. */
#define OHCI_RHS_DRWE RT_BIT_32(15) /* Device Remote Wakeup Enable. */
#define OHCI_RHS_LPSC RT_BIT_32(16) /* Local Power Status Change. */
#define OHCI_RHS_OCIC RT_BIT_32(17) /* Over-Current Indicator Change. */
#define OHCI_RHS_CRWE RT_BIT_32(31) /* Clear Remote Wakeup Enable. */
/** @name HcRhPortStatus[n] - RH Port Status register (read).
* @{ */
/** CCS - CurrentConnectionStatus - 0 = no device, 1 = device. */
#define OHCI_PORT_CCS RT_BIT(0)
/** ClearPortEnable (when writing CCS). */
#define OHCI_PORT_CLRPE OHCI_PORT_CCS
/** PES - PortEnableStatus. */
#define OHCI_PORT_PES RT_BIT(1)
/** PSS - PortSuspendStatus */
#define OHCI_PORT_PSS RT_BIT(2)
/** POCI- PortOverCurrentIndicator. */
#define OHCI_PORT_POCI RT_BIT(3)
/** ClearSuspendStatus (when writing POCI). */
#define OHCI_PORT_CLRSS OHCI_PORT_POCI
/** PRS - PortResetStatus */
#define OHCI_PORT_PRS RT_BIT(4)
/** PPS - PortPowerStatus */
#define OHCI_PORT_PPS RT_BIT(8)
/** LSDA - LowSpeedDeviceAttached */
#define OHCI_PORT_LSDA RT_BIT(9)
/** ClearPortPower (when writing LSDA). */
#define OHCI_PORT_CLRPP OHCI_PORT_LSDA
/** CSC - ConnectStatusChange */
#define OHCI_PORT_CSC RT_BIT(16)
/** PESC - PortEnableStatusChange */
#define OHCI_PORT_PESC RT_BIT(17)
/** PSSC - PortSuspendStatusChange */
#define OHCI_PORT_PSSC RT_BIT(18)
/** OCIC - OverCurrentIndicatorChange */
#define OHCI_PORT_OCIC RT_BIT(19)
/** PRSC - PortResetStatusChange */
#define OHCI_PORT_PRSC RT_BIT(20)
/** The mask of RW1C bits. */
#define OHCI_PORT_CLEAR_CHANGE_MASK (OHCI_PORT_CSC | OHCI_PORT_PESC | OHCI_PORT_PSSC | OHCI_PORT_OCIC | OHCI_PORT_PRSC)
/** @} */
#ifndef VBOX_DEVICE_STRUCT_TESTCASE
#ifdef VBOX_WITH_OHCI_PHYS_READ_STATS
/*
* Explain
*/
typedef struct OHCIDESCREADSTATS
{
uint32_t cReads;
uint32_t cPageChange;
uint32_t cMinReadsPerPage;
uint32_t cMaxReadsPerPage;
uint32_t cReadsLastPage;
uint32_t u32LastPageAddr;
} OHCIDESCREADSTATS;
typedef OHCIDESCREADSTATS *POHCIDESCREADSTATS;
typedef struct OHCIPHYSREADSTATS
{
OHCIDESCREADSTATS ed;
OHCIDESCREADSTATS td;
OHCIDESCREADSTATS all;
uint32_t cCrossReads;
uint32_t cCacheReads;
uint32_t cPageReads;
} OHCIPHYSREADSTATS;
typedef OHCIPHYSREADSTATS *POHCIPHYSREADSTATS;
typedef OHCIPHYSREADSTATS const *PCOHCIPHYSREADSTATS;
#endif /* VBOX_WITH_OHCI_PHYS_READ_STATS */
/*********************************************************************************************************************************
* Global Variables *
*********************************************************************************************************************************/
#if defined(VBOX_WITH_OHCI_PHYS_READ_STATS) && defined(IN_RING3)
static OHCIPHYSREADSTATS g_PhysReadState;
#endif
#if defined(LOG_ENABLED) && defined(IN_RING3)
static bool g_fLogBulkEPs = false;
static bool g_fLogControlEPs = false;
static bool g_fLogInterruptEPs = false;
#endif
#ifdef IN_RING3
/**
* SSM descriptor table for the OHCI structure.
*/
static SSMFIELD const g_aOhciFields[] =
{
SSMFIELD_ENTRY( OHCI, SofTime),
SSMFIELD_ENTRY_CUSTOM( dpic+fno, RT_OFFSETOF(OHCI, SofTime) + RT_SIZEOFMEMB(OHCI, SofTime), 4),
SSMFIELD_ENTRY( OHCI, RootHub.status),
SSMFIELD_ENTRY( OHCI, RootHub.desc_a),
SSMFIELD_ENTRY( OHCI, RootHub.desc_b),
SSMFIELD_ENTRY( OHCI, RootHub.aPorts[0].fReg),
SSMFIELD_ENTRY( OHCI, RootHub.aPorts[1].fReg),
SSMFIELD_ENTRY( OHCI, RootHub.aPorts[2].fReg),
SSMFIELD_ENTRY( OHCI, RootHub.aPorts[3].fReg),
SSMFIELD_ENTRY( OHCI, RootHub.aPorts[4].fReg),
SSMFIELD_ENTRY( OHCI, RootHub.aPorts[5].fReg),
SSMFIELD_ENTRY( OHCI, RootHub.aPorts[6].fReg),
SSMFIELD_ENTRY( OHCI, RootHub.aPorts[7].fReg),
SSMFIELD_ENTRY( OHCI, RootHub.aPorts[8].fReg),
SSMFIELD_ENTRY( OHCI, RootHub.aPorts[9].fReg),
SSMFIELD_ENTRY( OHCI, RootHub.aPorts[10].fReg),
SSMFIELD_ENTRY( OHCI, RootHub.aPorts[11].fReg),
SSMFIELD_ENTRY( OHCI, RootHub.aPorts[12].fReg),
SSMFIELD_ENTRY( OHCI, RootHub.aPorts[13].fReg),
SSMFIELD_ENTRY( OHCI, RootHub.aPorts[14].fReg),
SSMFIELD_ENTRY( OHCI, ctl),
SSMFIELD_ENTRY( OHCI, status),
SSMFIELD_ENTRY( OHCI, intr_status),
SSMFIELD_ENTRY( OHCI, intr),
SSMFIELD_ENTRY( OHCI, hcca),
SSMFIELD_ENTRY( OHCI, per_cur),
SSMFIELD_ENTRY( OHCI, ctrl_cur),
SSMFIELD_ENTRY( OHCI, ctrl_head),
SSMFIELD_ENTRY( OHCI, bulk_cur),
SSMFIELD_ENTRY( OHCI, bulk_head),
SSMFIELD_ENTRY( OHCI, done),
SSMFIELD_ENTRY_CUSTOM( fsmps+fit+fi+frt, RT_OFFSETOF(OHCI, done) + RT_SIZEOFMEMB(OHCI, done), 4),
SSMFIELD_ENTRY( OHCI, HcFmNumber),
SSMFIELD_ENTRY( OHCI, pstart),
SSMFIELD_ENTRY_TERM()
};
/**
* SSM descriptor table for the older 8-port OHCI structure.
*/
static SSMFIELD const g_aOhciFields8Ports[] =
{
SSMFIELD_ENTRY( OHCI, SofTime),
SSMFIELD_ENTRY_CUSTOM( dpic+fno, RT_OFFSETOF(OHCI, SofTime) + RT_SIZEOFMEMB(OHCI, SofTime), 4),
SSMFIELD_ENTRY( OHCI, RootHub.status),
SSMFIELD_ENTRY( OHCI, RootHub.desc_a),
SSMFIELD_ENTRY( OHCI, RootHub.desc_b),
SSMFIELD_ENTRY( OHCI, RootHub.aPorts[0].fReg),
SSMFIELD_ENTRY( OHCI, RootHub.aPorts[1].fReg),
SSMFIELD_ENTRY( OHCI, RootHub.aPorts[2].fReg),
SSMFIELD_ENTRY( OHCI, RootHub.aPorts[3].fReg),
SSMFIELD_ENTRY( OHCI, RootHub.aPorts[4].fReg),
SSMFIELD_ENTRY( OHCI, RootHub.aPorts[5].fReg),
SSMFIELD_ENTRY( OHCI, RootHub.aPorts[6].fReg),
SSMFIELD_ENTRY( OHCI, RootHub.aPorts[7].fReg),
SSMFIELD_ENTRY( OHCI, ctl),
SSMFIELD_ENTRY( OHCI, status),
SSMFIELD_ENTRY( OHCI, intr_status),
SSMFIELD_ENTRY( OHCI, intr),
SSMFIELD_ENTRY( OHCI, hcca),
SSMFIELD_ENTRY( OHCI, per_cur),
SSMFIELD_ENTRY( OHCI, ctrl_cur),
SSMFIELD_ENTRY( OHCI, ctrl_head),
SSMFIELD_ENTRY( OHCI, bulk_cur),
SSMFIELD_ENTRY( OHCI, bulk_head),
SSMFIELD_ENTRY( OHCI, done),
SSMFIELD_ENTRY_CUSTOM( fsmps+fit+fi+frt, RT_OFFSETOF(OHCI, done) + RT_SIZEOFMEMB(OHCI, done), 4),
SSMFIELD_ENTRY( OHCI, HcFmNumber),
SSMFIELD_ENTRY( OHCI, pstart),
SSMFIELD_ENTRY_TERM()
};
#endif
/*********************************************************************************************************************************
* Internal Functions *
*********************************************************************************************************************************/
RT_C_DECLS_BEGIN
#ifdef IN_RING3
/* Update host controller state to reflect a device attach */
static void ohciR3RhPortPower(POHCIROOTHUB pRh, unsigned iPort, bool fPowerUp);
static void ohciR3BusResume(POHCI ohci, bool fHardware);
static void ohciR3BusStop(POHCI pThis);
#ifdef VBOX_WITH_OHCI_PHYS_READ_CACHE
static void ohciR3PhysReadCacheClear(POHCIPAGECACHE pPageCache);
#endif
static DECLCALLBACK(void) ohciR3RhXferCompletion(PVUSBIROOTHUBPORT pInterface, PVUSBURB pUrb);
static DECLCALLBACK(bool) ohciR3RhXferError(PVUSBIROOTHUBPORT pInterface, PVUSBURB pUrb);
static int ohciR3InFlightFind(POHCI pThis, uint32_t GCPhysTD);
# if defined(VBOX_STRICT) || defined(LOG_ENABLED)
static int ohciR3InDoneQueueFind(POHCI pThis, uint32_t GCPhysTD);
# endif
static DECLCALLBACK(void) ohciR3LoadReattachDevices(PPDMDEVINS pDevIns, PTMTIMER pTimer, void *pvUser);
#endif /* IN_RING3 */
RT_C_DECLS_END
/**
* Update PCI IRQ levels
*/
static void ohciUpdateInterruptLocked(POHCI ohci, const char *msg)
{
int level = 0;
if ( (ohci->intr & OHCI_INTR_MASTER_INTERRUPT_ENABLED)
&& (ohci->intr_status & ohci->intr)
&& !(ohci->ctl & OHCI_CTL_IR))
level = 1;
PDMDevHlpPCISetIrq(ohci->CTX_SUFF(pDevIns), 0, level);
if (level)
{
uint32_t val = ohci->intr_status & ohci->intr;
Log2(("ohci: Fired off interrupt %#010x - SO=%d WDH=%d SF=%d RD=%d UE=%d FNO=%d RHSC=%d OC=%d - %s\n",
val, val & 1, (val >> 1) & 1, (val >> 2) & 1, (val >> 3) & 1, (val >> 4) & 1, (val >> 5) & 1,
(val >> 6) & 1, (val >> 30) & 1, msg)); NOREF(val); NOREF(msg);
}
}
#ifdef IN_RING3
/**
* Set an interrupt, use the wrapper ohciSetInterrupt.
*/
DECLINLINE(int) ohciR3SetInterruptInt(POHCI ohci, int rcBusy, uint32_t intr, const char *msg)
{
int rc = PDMCritSectEnter(&ohci->CsIrq, rcBusy);
if (rc != VINF_SUCCESS)
return rc;
if ( (ohci->intr_status & intr) != intr )
{
ohci->intr_status |= intr;
ohciUpdateInterruptLocked(ohci, msg);
}
PDMCritSectLeave(&ohci->CsIrq);
return rc;
}
/**
* Set an interrupt wrapper macro for logging purposes.
*/
# define ohciR3SetInterrupt(ohci, intr) ohciR3SetInterruptInt(ohci, VERR_IGNORED, intr, #intr)
/* Carry out a hardware remote wakeup */
static void ohciR3RemoteWakeup(POHCI pThis)
{
if ((pThis->ctl & OHCI_CTL_HCFS) != OHCI_USB_SUSPEND)
return;
if (!(pThis->RootHub.status & OHCI_RHS_DRWE))
return;
ohciR3BusResume(pThis, true /* hardware */);
}
/**
* Query interface method for the roothub LUN.
*/
static DECLCALLBACK(void *) ohciR3RhQueryInterface(PPDMIBASE pInterface, const char *pszIID)
{
POHCI pThis = RT_FROM_MEMBER(pInterface, OHCI, RootHub.IBase);
PDMIBASE_RETURN_INTERFACE(pszIID, PDMIBASE, &pThis->RootHub.IBase);
PDMIBASE_RETURN_INTERFACE(pszIID, VUSBIROOTHUBPORT, &pThis->RootHub.IRhPort);
PDMIBASE_RETURN_INTERFACE(pszIID, PDMILEDPORTS, &pThis->RootHub.ILeds);
return NULL;
}
/**
* Gets the pointer to the status LED of a unit.
*
* @returns VBox status code.
* @param pInterface Pointer to the interface structure containing the called function pointer.
* @param iLUN The unit which status LED we desire.
* @param ppLed Where to store the LED pointer.
*/
static DECLCALLBACK(int) ohciR3RhQueryStatusLed(PPDMILEDPORTS pInterface, unsigned iLUN, PPDMLED *ppLed)
{
POHCI pThis = (POHCI)((uintptr_t)pInterface - RT_OFFSETOF(OHCI, RootHub.ILeds));
if (iLUN == 0)
{
*ppLed = &pThis->RootHub.Led;
return VINF_SUCCESS;
}
return VERR_PDM_LUN_NOT_FOUND;
}
/** Converts a OHCI.roothub.IRhPort pointer to a POHCI. */
#define VUSBIROOTHUBPORT_2_OHCI(pInterface) ((POHCI)( (uintptr_t)(pInterface) - RT_OFFSETOF(OHCI, RootHub.IRhPort) ))
/**
* Get the number of available ports in the hub.
*
* @returns The number of ports available.
* @param pInterface Pointer to this structure.
* @param pAvailable Bitmap indicating the available ports. Set bit == available port.
*/
static DECLCALLBACK(unsigned) ohciR3RhGetAvailablePorts(PVUSBIROOTHUBPORT pInterface, PVUSBPORTBITMAP pAvailable)
{
POHCI pThis = VUSBIROOTHUBPORT_2_OHCI(pInterface);
unsigned iPort;
unsigned cPorts = 0;
memset(pAvailable, 0, sizeof(*pAvailable));
PDMCritSectEnter(pThis->pDevInsR3->pCritSectRoR3, VERR_IGNORED);
for (iPort = 0; iPort < OHCI_NDP_CFG(pThis); iPort++)
{
if (!pThis->RootHub.aPorts[iPort].pDev)
{
cPorts++;
ASMBitSet(pAvailable, iPort + 1);
}
}
PDMCritSectLeave(pThis->pDevInsR3->pCritSectRoR3);
return cPorts;
}
/**
* Gets the supported USB versions.
*
* @returns The mask of supported USB versions.
* @param pInterface Pointer to this structure.
*/
static DECLCALLBACK(uint32_t) ohciR3RhGetUSBVersions(PVUSBIROOTHUBPORT pInterface)
{
RT_NOREF(pInterface);
return VUSB_STDVER_11;
}
/**
* A device is being attached to a port in the roothub.
*
* @param pInterface Pointer to this structure.
* @param pDev Pointer to the device being attached.
* @param uPort The port number assigned to the device.
*/
static DECLCALLBACK(int) ohciR3RhAttach(PVUSBIROOTHUBPORT pInterface, PVUSBIDEVICE pDev, unsigned uPort)
{
POHCI pThis = VUSBIROOTHUBPORT_2_OHCI(pInterface);
VUSBSPEED enmSpeed;
LogFlow(("ohciR3RhAttach: pDev=%p uPort=%u\n", pDev, uPort));
PDMCritSectEnter(pThis->pDevInsR3->pCritSectRoR3, VERR_IGNORED);
/*
* Validate and adjust input.
*/
Assert(uPort >= 1 && uPort <= OHCI_NDP_CFG(pThis));
uPort--;
Assert(!pThis->RootHub.aPorts[uPort].pDev);
enmSpeed = pDev->pfnGetSpeed(pDev);
/* Only LS/FS devices can end up here. */
Assert(enmSpeed == VUSB_SPEED_LOW || enmSpeed == VUSB_SPEED_FULL);
/*
* Attach it.
*/
pThis->RootHub.aPorts[uPort].fReg = OHCI_PORT_CCS | OHCI_PORT_CSC;
if (enmSpeed == VUSB_SPEED_LOW)
pThis->RootHub.aPorts[uPort].fReg |= OHCI_PORT_LSDA;
pThis->RootHub.aPorts[uPort].pDev = pDev;
ohciR3RhPortPower(&pThis->RootHub, uPort, 1 /* power on */);
ohciR3RemoteWakeup(pThis);
ohciR3SetInterrupt(pThis, OHCI_INTR_ROOT_HUB_STATUS_CHANGE);
PDMCritSectLeave(pThis->pDevInsR3->pCritSectRoR3);
return VINF_SUCCESS;
}
/**
* A device is being detached from a port in the roothub.
*
* @param pInterface Pointer to this structure.
* @param pDev Pointer to the device being detached.
* @param uPort The port number assigned to the device.
*/
static DECLCALLBACK(void) ohciR3RhDetach(PVUSBIROOTHUBPORT pInterface, PVUSBIDEVICE pDev, unsigned uPort)
{
RT_NOREF(pDev);
POHCI pThis = VUSBIROOTHUBPORT_2_OHCI(pInterface);
LogFlow(("ohciR3RhDetach: pDev=%p uPort=%u\n", pDev, uPort));
PDMCritSectEnter(pThis->pDevInsR3->pCritSectRoR3, VERR_IGNORED);
/*
* Validate and adjust input.
*/
Assert(uPort >= 1 && uPort <= OHCI_NDP_CFG(pThis));
uPort--;
Assert(pThis->RootHub.aPorts[uPort].pDev == pDev);
/*
* Detach it.
*/
pThis->RootHub.aPorts[uPort].pDev = NULL;
if (pThis->RootHub.aPorts[uPort].fReg & OHCI_PORT_PES)
pThis->RootHub.aPorts[uPort].fReg = OHCI_PORT_CSC | OHCI_PORT_PESC;
else
pThis->RootHub.aPorts[uPort].fReg = OHCI_PORT_CSC;
ohciR3RemoteWakeup(pThis);
ohciR3SetInterrupt(pThis, OHCI_INTR_ROOT_HUB_STATUS_CHANGE);
PDMCritSectLeave(pThis->pDevInsR3->pCritSectRoR3);
}
/**
* One of the roothub devices has completed its reset operation.
*
* Currently, we don't think anything is required to be done here
* so it's just a stub for forcing async resetting of the devices
* during a root hub reset.
*
* @param pDev The root hub device.
* @param rc The result of the operation.
* @param pvUser Pointer to the controller.
*/
static DECLCALLBACK(void) ohciR3RhResetDoneOneDev(PVUSBIDEVICE pDev, int rc, void *pvUser)
{
LogRel(("OHCI: root hub reset completed with %Rrc\n", rc));
NOREF(pDev); NOREF(rc); NOREF(pvUser);
}
/**
* Reset the root hub.
*
* @returns VBox status code.
* @param pInterface Pointer to this structure.
* @param fResetOnLinux This is used to indicate whether we're at VM reset time and
* can do real resets or if we're at any other time where that
* isn't such a good idea.
* @remark Do NOT call VUSBIDevReset on the root hub in an async fashion!
* @thread EMT
*/
static DECLCALLBACK(int) ohciR3RhReset(PVUSBIROOTHUBPORT pInterface, bool fResetOnLinux)
{
POHCI pThis = VUSBIROOTHUBPORT_2_OHCI(pInterface);
PDMCritSectEnter(pThis->pDevInsR3->pCritSectRoR3, VERR_IGNORED);
Log(("ohci: root hub reset%s\n", fResetOnLinux ? " (reset on linux)" : ""));
pThis->RootHub.status = 0;
pThis->RootHub.desc_a = OHCI_RHA_NPS | OHCI_NDP_CFG(pThis); /* Preserve NDP value. */
pThis->RootHub.desc_b = 0x0; /* Impl. specific */
/*
* We're pending to _reattach_ the device without resetting them.
* Except, during VM reset where we use the opportunity to do a proper
* reset before the guest comes along and expect things.
*
* However, it's very very likely that we're not doing the right thing
* here if coming from the guest (USB Reset state). The docs talks about
* root hub resetting, however what exact behaviour in terms of root hub
* status and changed bits, and HC interrupts aren't stated clearly. IF we
* get trouble and see the guest doing "USB Resets" we will have to look
* into this. For the time being we stick with simple.
*/
for (unsigned iPort = 0; iPort < OHCI_NDP_CFG(pThis); iPort++)
{
if (pThis->RootHub.aPorts[iPort].pDev)
{
pThis->RootHub.aPorts[iPort].fReg = OHCI_PORT_CCS | OHCI_PORT_CSC | OHCI_PORT_PPS;
if (fResetOnLinux)
{
PVM pVM = PDMDevHlpGetVM(pThis->CTX_SUFF(pDevIns));
VUSBIDevReset(pThis->RootHub.aPorts[iPort].pDev, fResetOnLinux, ohciR3RhResetDoneOneDev, pThis, pVM);
}
}
else
pThis->RootHub.aPorts[iPort].fReg = 0;
}
ohciR3SetInterrupt(pThis, OHCI_INTR_ROOT_HUB_STATUS_CHANGE);
PDMCritSectLeave(pThis->pDevInsR3->pCritSectRoR3);
return VINF_SUCCESS;
}
/**
* Does a software or hardware reset of the controller.
*
* This is called in response to setting HcCommandStatus.HCR, hardware reset,
* and device construction.
*
* @param pThis The ohci instance data.
* @param fNewMode The new mode of operation. This is UsbSuspend if it's a
* software reset, and UsbReset if it's a hardware reset / cold boot.
* @param fResetOnLinux Set if we can do a real reset of the devices attached to the root hub.
* This is really a just a hack for the non-working linux device reset.
* Linux has this feature called 'logical disconnect' if device reset fails
* which prevents us from doing resets when the guest asks for it - the guest
* will get confused when the device seems to be reconnected everytime it tries
* to reset it. But if we're at hardware reset time, we can allow a device to
* be 'reconnected' without upsetting the guest.
*
* @remark This hasn't got anything to do with software setting the mode to UsbReset.
*/
static void ohciR3DoReset(POHCI pThis, uint32_t fNewMode, bool fResetOnLinux)
{
Log(("ohci: %s reset%s\n", fNewMode == OHCI_USB_RESET ? "hardware" : "software",
fResetOnLinux ? " (reset on linux)" : ""));
/* Stop the bus in any case, disabling walking the lists. */
ohciR3BusStop(pThis);
/*
* Cancel all outstanding URBs.
*
* We can't, and won't, deal with URBs until we're moved out of the
* suspend/reset state. Also, a real HC isn't going to send anything
* any more when a reset has been signaled.
*/
pThis->RootHub.pIRhConn->pfnCancelAllUrbs(pThis->RootHub.pIRhConn);
/*
* Reset the hardware registers.
*/
if (fNewMode == OHCI_USB_RESET)
pThis->ctl = OHCI_CTL_RWC; /* We're the firmware, set RemoteWakeupConnected. */
else
pThis->ctl &= OHCI_CTL_IR | OHCI_CTL_RWC; /* IR and RWC are preserved on software reset. */
/* Clear the HCFS bits first to make setting the new state work. */
pThis->ctl &= ~OHCI_CTL_HCFS;
pThis->ctl |= fNewMode;
pThis->status = 0;
pThis->intr_status = 0;
pThis->intr = 0;
PDMDevHlpPCISetIrq(pThis->CTX_SUFF(pDevIns), 0, 0);
pThis->hcca = 0;
pThis->per_cur = 0;
pThis->ctrl_head = pThis->ctrl_cur = 0;
pThis->bulk_head = pThis->bulk_cur = 0;
pThis->done = 0;
pThis->fsmps = 0x2778; /* To-Be-Defined, use the value linux sets...*/
pThis->fit = 0;
pThis->fi = 11999; /* (12MHz ticks, one frame is 1ms) */
pThis->frt = 0;
pThis->HcFmNumber = 0;
pThis->pstart = 0;
pThis->dqic = 0x7;
pThis->fno = 0;
#ifdef VBOX_WITH_OHCI_PHYS_READ_CACHE
ohciR3PhysReadCacheClear(pThis->pCacheED);
ohciR3PhysReadCacheClear(pThis->pCacheTD);
#endif
/*
* If this is a hardware reset, we will initialize the root hub too.
* Software resets doesn't do this according to the specs.
* (It's not possible to have device connected at the time of the
* device construction, so nothing to worry about there.)
*/
if (fNewMode == OHCI_USB_RESET)
VUSBIDevReset(pThis->RootHub.pIDev, fResetOnLinux, NULL, NULL, NULL);
}
/**
* Reads physical memory.
*/
DECLINLINE(void) ohciR3PhysRead(POHCI pThis, uint32_t Addr, void *pvBuf, size_t cbBuf)
{
if (cbBuf)
PDMDevHlpPhysRead(pThis->CTX_SUFF(pDevIns), Addr, pvBuf, cbBuf);
}
/**
* Writes physical memory.
*/
DECLINLINE(void) ohciR3PhysWrite(POHCI pThis, uint32_t Addr, const void *pvBuf, size_t cbBuf)
{
if (cbBuf)
PDMDevHlpPCIPhysWrite(pThis->CTX_SUFF(pDevIns), Addr, pvBuf, cbBuf);
}
/**
* Read an array of dwords from physical memory and correct endianness.
*/
DECLINLINE(void) ohciR3GetDWords(POHCI pThis, uint32_t Addr, uint32_t *pau32s, int c32s)
{
ohciR3PhysRead(pThis, Addr, pau32s, c32s * sizeof(uint32_t));
# ifndef RT_LITTLE_ENDIAN
for(int i = 0; i < c32s; i++)
pau32s[i] = RT_H2LE_U32(pau32s[i]);
# endif
}
/**
* Write an array of dwords from physical memory and correct endianness.
*/
DECLINLINE(void) ohciR3PutDWords(POHCI pThis, uint32_t Addr, const uint32_t *pau32s, int cu32s)
{
# ifdef RT_LITTLE_ENDIAN
ohciR3PhysWrite(pThis, Addr, pau32s, cu32s << 2);
# else
for (int i = 0; i < c32s; i++, pau32s++, Addr += sizeof(*pau32s))
{
uint32_t u32Tmp = RT_H2LE_U32(*pau32s);
ohciR3PhysWrite(pThis, Addr, (uint8_t *)&u32Tmp, sizeof(u32Tmp));
}
# endif
}
# ifdef VBOX_WITH_OHCI_PHYS_READ_STATS
static void descReadStatsReset(POHCIDESCREADSTATS p)
{
p->cReads = 0;
p->cPageChange = 0;
p->cMinReadsPerPage = UINT32_MAX;
p->cMaxReadsPerPage = 0;
p->cReadsLastPage = 0;
p->u32LastPageAddr = 0;
}
static void physReadStatsReset(POHCIPHYSREADSTATS p)
{
descReadStatsReset(&p->ed);
descReadStatsReset(&p->td);
descReadStatsReset(&p->all);
p->cCrossReads = 0;
p->cCacheReads = 0;
p->cPageReads = 0;
}
static void physReadStatsUpdateDesc(POHCIDESCREADSTATS p, uint32_t u32Addr)
{
const uint32_t u32PageAddr = u32Addr & ~UINT32_C(0xFFF);
++p->cReads;
if (p->u32LastPageAddr == 0)
{
/* First call. */
++p->cReadsLastPage;
p->u32LastPageAddr = u32PageAddr;
}
else if (u32PageAddr != p->u32LastPageAddr)
{
/* New page. */
++p->cPageChange;
p->cMinReadsPerPage = RT_MIN(p->cMinReadsPerPage, p->cReadsLastPage);
p->cMaxReadsPerPage = RT_MAX(p->cMaxReadsPerPage, p->cReadsLastPage);;
p->cReadsLastPage = 1;
p->u32LastPageAddr = u32PageAddr;
}
else
{
/* Read on the same page. */
++p->cReadsLastPage;
}
}
static void physReadStatsPrint(POHCIPHYSREADSTATS p)
{
p->ed.cMinReadsPerPage = RT_MIN(p->ed.cMinReadsPerPage, p->ed.cReadsLastPage);
p->ed.cMaxReadsPerPage = RT_MAX(p->ed.cMaxReadsPerPage, p->ed.cReadsLastPage);;
p->td.cMinReadsPerPage = RT_MIN(p->td.cMinReadsPerPage, p->td.cReadsLastPage);
p->td.cMaxReadsPerPage = RT_MAX(p->td.cMaxReadsPerPage, p->td.cReadsLastPage);;
p->all.cMinReadsPerPage = RT_MIN(p->all.cMinReadsPerPage, p->all.cReadsLastPage);
p->all.cMaxReadsPerPage = RT_MAX(p->all.cMaxReadsPerPage, p->all.cReadsLastPage);;
LogRel(("PHYSREAD:\n"
" ED: %d, %d, %d/%d\n"
" TD: %d, %d, %d/%d\n"
" ALL: %d, %d, %d/%d\n"
" C: %d, %d, %d\n"
"",
p->ed.cReads, p->ed.cPageChange, p->ed.cMinReadsPerPage, p->ed.cMaxReadsPerPage,
p->td.cReads, p->td.cPageChange, p->td.cMinReadsPerPage, p->td.cMaxReadsPerPage,
p->all.cReads, p->all.cPageChange, p->all.cMinReadsPerPage, p->all.cMaxReadsPerPage,
p->cCrossReads, p->cCacheReads, p->cPageReads
));
physReadStatsReset(p);
}
# endif /* VBOX_WITH_OHCI_PHYS_READ_STATS */
# ifdef VBOX_WITH_OHCI_PHYS_READ_CACHE
static POHCIPAGECACHE ohciR3PhysReadCacheAlloc(void)
{
return (POHCIPAGECACHE)RTMemAlloc(sizeof(OHCIPAGECACHE));
}
static void ohciR3PhysReadCacheFree(POHCIPAGECACHE pPageCache)
{
RTMemFree(pPageCache);
}
static void ohciR3PhysReadCacheClear(POHCIPAGECACHE pPageCache)
{
pPageCache->GCPhysReadCacheAddr = NIL_RTGCPHYS;
}
static void ohciR3PhysReadCacheRead(POHCI pThis, POHCIPAGECACHE pPageCache, RTGCPHYS GCPhys, void *pvBuf, size_t cbBuf)
{
const RTGCPHYS PageAddr = PAGE_ADDRESS(GCPhys);
if (PageAddr == PAGE_ADDRESS(GCPhys + cbBuf))
{
if (PageAddr != pPageCache->GCPhysReadCacheAddr)
{
PDMDevHlpPhysRead(pThis->pDevInsR3, PageAddr,
pPageCache->au8PhysReadCache, sizeof(pPageCache->au8PhysReadCache));
pPageCache->GCPhysReadCacheAddr = PageAddr;
# ifdef VBOX_WITH_OHCI_PHYS_READ_STATS
++g_PhysReadState.cPageReads;
# endif
}
memcpy(pvBuf, &pPageCache->au8PhysReadCache[GCPhys & PAGE_OFFSET_MASK], cbBuf);
# ifdef VBOX_WITH_OHCI_PHYS_READ_STATS
++g_PhysReadState.cCacheReads;
# endif
}
else
{
PDMDevHlpPhysRead(pThis->pDevInsR3, GCPhys, pvBuf, cbBuf);
# ifdef VBOX_WITH_OHCI_PHYS_READ_STATS
++g_PhysReadState.cCrossReads;
# endif
}
}
/**
* Updates the data in the given page cache if the given guest physical address is currently contained
* in the cache.
*
* @returns nothing.
* @param pPageCache The page cache to update.
* @param GCPhys The guest physical address needing the update.
* @param pvBuf Pointer to the buffer to update the page cache with.
* @param cbBuf Number of bytes to update.
*/
static void ohciR3PhysCacheUpdate(POHCIPAGECACHE pPageCache, RTGCPHYS GCPhys, const void *pvBuf, size_t cbBuf)
{
const RTGCPHYS GCPhysPage = PAGE_ADDRESS(GCPhys);
if (GCPhysPage == pPageCache->GCPhysReadCacheAddr)
{
uint32_t offPage = GCPhys & PAGE_OFFSET_MASK;
memcpy(&pPageCache->au8PhysReadCache[offPage], pvBuf, RT_MIN(PAGE_SIZE - offPage, cbBuf));
}
}
/**
* Update any cached ED data with the given endpoint descriptor at the given address.
*
* @returns nothing.
* @param pThis The OHCI instance data.
* @param EdAddr Endpoint descriptor address.
* @param pEd The endpoint descriptor which got updated.
*/
DECLINLINE(void) ohciR3CacheEdUpdate(POHCI pThis, RTGCPHYS32 EdAddr, PCOHCIED pEd)
{
ohciR3PhysCacheUpdate(pThis->pCacheED, EdAddr + RT_OFFSETOF(OHCIED, HeadP), &pEd->HeadP, sizeof(uint32_t));
}
/**
* Update any cached TD data with the given transfer descriptor at the given address.
*
* @returns nothing.
* @param pThis The OHCI instance data.
* @param TdAddr Transfer descriptor address.
* @param pTd The transfer descriptor which got updated.
*/
DECLINLINE(void) ohciR3CacheTdUpdate(POHCI pThis, RTGCPHYS32 TdAddr, PCOHCITD pTd)
{
ohciR3PhysCacheUpdate(pThis->pCacheTD, TdAddr, pTd, sizeof(*pTd));
}
# endif /* VBOX_WITH_OHCI_PHYS_READ_CACHE */
/**
* Reads an OHCIED.
*/
DECLINLINE(void) ohciR3ReadEd(POHCI pThis, uint32_t EdAddr, POHCIED pEd)
{
# ifdef VBOX_WITH_OHCI_PHYS_READ_STATS
physReadStatsUpdateDesc(&g_PhysReadState.ed, EdAddr);
physReadStatsUpdateDesc(&g_PhysReadState.all, EdAddr);
# endif
#ifdef VBOX_WITH_OHCI_PHYS_READ_CACHE
ohciR3PhysReadCacheRead(pThis, pThis->pCacheED, EdAddr, pEd, sizeof(*pEd));
#else
ohciR3GetDWords(pThis, EdAddr, (uint32_t *)pEd, sizeof(*pEd) >> 2);
#endif
}
/**
* Reads an OHCITD.
*/
DECLINLINE(void) ohciR3ReadTd(POHCI pThis, uint32_t TdAddr, POHCITD pTd)
{
# ifdef VBOX_WITH_OHCI_PHYS_READ_STATS
physReadStatsUpdateDesc(&g_PhysReadState.td, TdAddr);
physReadStatsUpdateDesc(&g_PhysReadState.all, TdAddr);
# endif
#ifdef VBOX_WITH_OHCI_PHYS_READ_CACHE
ohciR3PhysReadCacheRead(pThis, pThis->pCacheTD, TdAddr, pTd, sizeof(*pTd));
#else
ohciR3GetDWords(pThis, TdAddr, (uint32_t *)pTd, sizeof(*pTd) >> 2);
#endif
# ifdef LOG_ENABLED
if (LogIs3Enabled())
{
uint32_t hichg;
hichg = pTd->hwinfo;
Log3(("ohciR3ReadTd(,%#010x,): R=%d DP=%d DI=%d T=%d EC=%d CC=%#x CBP=%#010x NextTD=%#010x BE=%#010x UNK=%#x\n",
TdAddr,
(pTd->hwinfo >> 18) & 1,
(pTd->hwinfo >> 19) & 3,
(pTd->hwinfo >> 21) & 7,
(pTd->hwinfo >> 24) & 3,
(pTd->hwinfo >> 26) & 3,
(pTd->hwinfo >> 28) &15,
pTd->cbp,
pTd->NextTD,
pTd->be,
pTd->hwinfo & TD_HWINFO_UNKNOWN_MASK));
# if 0
if (LogIs3Enabled())
{
/*
* usbohci.sys (32-bit XP) allocates 0x80 bytes per TD:
* 0x00-0x0f is the OHCI TD.
* 0x10-0x1f for isochronous TDs
* 0x20 is the physical address of this TD.
* 0x24 is initialized with 0x64745948, probably a magic.
* 0x28 is some kind of flags. the first bit begin the allocated / not allocated indicator.
* 0x30 is a pointer to something. endpoint? interface? device?
* 0x38 is initialized to 0xdeadface. but is changed into a pointer or something.
* 0x40 looks like a pointer.
* The rest is unknown and initialized with zeros.
*/
uint8_t abXpTd[0x80];
ohciR3PhysRead(pThis, TdAddr, abXpTd, sizeof(abXpTd));
Log3(("WinXpTd: alloc=%d PhysSelf=%RX32 s2=%RX32 magic=%RX32 s4=%RX32 s5=%RX32\n"
"%.*Rhxd\n",
abXpTd[28] & RT_BIT(0),
*((uint32_t *)&abXpTd[0x20]), *((uint32_t *)&abXpTd[0x30]),
*((uint32_t *)&abXpTd[0x24]), *((uint32_t *)&abXpTd[0x38]),
*((uint32_t *)&abXpTd[0x40]),
sizeof(abXpTd), &abXpTd[0]));
}
# endif
}
# endif
}
/**
* Reads an OHCIITD.
*/
DECLINLINE(void) ohciR3ReadITd(POHCI pThis, uint32_t ITdAddr, POHCIITD pITd)
{
ohciR3GetDWords(pThis, ITdAddr, (uint32_t *)pITd, sizeof(*pITd) / sizeof(uint32_t));
# ifdef LOG_ENABLED
if (LogIs3Enabled())
{
Log3(("ohciR3ReadITd(,%#010x,): SF=%#06x (%#RX32) DI=%#x FC=%d CC=%#x BP0=%#010x NextTD=%#010x BE=%#010x\n",
ITdAddr,
pITd->HwInfo & 0xffff, pThis->HcFmNumber,
(pITd->HwInfo >> 21) & 7,
(pITd->HwInfo >> 24) & 7,
(pITd->HwInfo >> 28) &15,
pITd->BP0,
pITd->NextTD,
pITd->BE));
Log3(("psw0=%x:%03x psw1=%x:%03x psw2=%x:%03x psw3=%x:%03x psw4=%x:%03x psw5=%x:%03x psw6=%x:%03x psw7=%x:%03x\n",
pITd->aPSW[0] >> 12, pITd->aPSW[0] & 0xfff,
pITd->aPSW[1] >> 12, pITd->aPSW[1] & 0xfff,
pITd->aPSW[2] >> 12, pITd->aPSW[2] & 0xfff,
pITd->aPSW[3] >> 12, pITd->aPSW[3] & 0xfff,
pITd->aPSW[4] >> 12, pITd->aPSW[4] & 0xfff,
pITd->aPSW[5] >> 12, pITd->aPSW[5] & 0xfff,
pITd->aPSW[6] >> 12, pITd->aPSW[6] & 0xfff,
pITd->aPSW[7] >> 12, pITd->aPSW[7] & 0xfff));
}
# endif
}
/**
* Writes an OHCIED.
*/
DECLINLINE(void) ohciR3WriteEd(POHCI pThis, uint32_t EdAddr, PCOHCIED pEd)
{
# ifdef LOG_ENABLED
if (LogIs3Enabled())
{
OHCIED EdOld;
uint32_t hichg;
ohciR3GetDWords(pThis, EdAddr, (uint32_t *)&EdOld, sizeof(EdOld) >> 2);
hichg = EdOld.hwinfo ^ pEd->hwinfo;
Log3(("ohciR3WriteEd(,%#010x,): %sFA=%#x %sEN=%#x %sD=%#x %sS=%d %sK=%d %sF=%d %sMPS=%#x %sTailP=%#010x %sHeadP=%#010x %sH=%d %sC=%d %sNextED=%#010x\n",
EdAddr,
(hichg >> 0) & 0x7f ? "*" : "", (pEd->hwinfo >> 0) & 0x7f,
(hichg >> 7) & 0xf ? "*" : "", (pEd->hwinfo >> 7) & 0xf,
(hichg >> 11) & 3 ? "*" : "", (pEd->hwinfo >> 11) & 3,
(hichg >> 13) & 1 ? "*" : "", (pEd->hwinfo >> 13) & 1,
(hichg >> 14) & 1 ? "*" : "", (pEd->hwinfo >> 14) & 1,
(hichg >> 15) & 1 ? "*" : "", (pEd->hwinfo >> 15) & 1,
(hichg >> 24) &0x3ff ? "*" : "", (pEd->hwinfo >> 16) &0x3ff,
EdOld.TailP != pEd->TailP ? "*" : "", pEd->TailP,
(EdOld.HeadP & ~3) != (pEd->HeadP & ~3) ? "*" : "", pEd->HeadP & ~3,
(EdOld.HeadP ^ pEd->HeadP) & 1 ? "*" : "", pEd->HeadP & 1,
(EdOld.HeadP ^ pEd->HeadP) & 2 ? "*" : "", (pEd->HeadP >> 1) & 1,
EdOld.NextED != pEd->NextED ? "*" : "", pEd->NextED));
}
# endif
ohciR3PutDWords(pThis, EdAddr + RT_OFFSETOF(OHCIED, HeadP), &pEd->HeadP, 1);
#ifdef VBOX_WITH_OHCI_PHYS_READ_CACHE
ohciR3CacheEdUpdate(pThis, EdAddr, pEd);
#endif
}
/**
* Writes an OHCITD.
*/
DECLINLINE(void) ohciR3WriteTd(POHCI pThis, uint32_t TdAddr, PCOHCITD pTd, const char *pszLogMsg)
{
# ifdef LOG_ENABLED
if (LogIs3Enabled())
{
OHCITD TdOld;
ohciR3GetDWords(pThis, TdAddr, (uint32_t *)&TdOld, sizeof(TdOld) >> 2);
uint32_t hichg = TdOld.hwinfo ^ pTd->hwinfo;
Log3(("ohciR3WriteTd(,%#010x,): %sR=%d %sDP=%d %sDI=%#x %sT=%d %sEC=%d %sCC=%#x %sCBP=%#010x %sNextTD=%#010x %sBE=%#010x (%s)\n",
TdAddr,
(hichg >> 18) & 1 ? "*" : "", (pTd->hwinfo >> 18) & 1,
(hichg >> 19) & 3 ? "*" : "", (pTd->hwinfo >> 19) & 3,
(hichg >> 21) & 7 ? "*" : "", (pTd->hwinfo >> 21) & 7,
(hichg >> 24) & 3 ? "*" : "", (pTd->hwinfo >> 24) & 3,
(hichg >> 26) & 3 ? "*" : "", (pTd->hwinfo >> 26) & 3,
(hichg >> 28) &15 ? "*" : "", (pTd->hwinfo >> 28) &15,
TdOld.cbp != pTd->cbp ? "*" : "", pTd->cbp,
TdOld.NextTD != pTd->NextTD ? "*" : "", pTd->NextTD,
TdOld.be != pTd->be ? "*" : "", pTd->be,
pszLogMsg));
}
# else
RT_NOREF(pszLogMsg);
# endif
ohciR3PutDWords(pThis, TdAddr, (uint32_t *)pTd, sizeof(*pTd) >> 2);
#ifdef VBOX_WITH_OHCI_PHYS_READ_CACHE
ohciR3CacheTdUpdate(pThis, TdAddr, pTd);
#endif
}
/**
* Writes an OHCIITD.
*/
DECLINLINE(void) ohciR3WriteITd(POHCI pThis, uint32_t ITdAddr, PCOHCIITD pITd, const char *pszLogMsg)
{
# ifdef LOG_ENABLED
if (LogIs3Enabled())
{
OHCIITD ITdOld;
ohciR3GetDWords(pThis, ITdAddr, (uint32_t *)&ITdOld, sizeof(ITdOld) / sizeof(uint32_t));
uint32_t HIChg = ITdOld.HwInfo ^ pITd->HwInfo;
Log3(("ohciR3WriteITd(,%#010x,): %sSF=%#x (now=%#RX32) %sDI=%#x %sFC=%d %sCC=%#x %sBP0=%#010x %sNextTD=%#010x %sBE=%#010x (%s)\n",
ITdAddr,
(HIChg & 0xffff) & 1 ? "*" : "", pITd->HwInfo & 0xffff, pThis->HcFmNumber,
(HIChg >> 21) & 7 ? "*" : "", (pITd->HwInfo >> 21) & 7,
(HIChg >> 24) & 7 ? "*" : "", (pITd->HwInfo >> 24) & 7,
(HIChg >> 28) &15 ? "*" : "", (pITd->HwInfo >> 28) &15,
ITdOld.BP0 != pITd->BP0 ? "*" : "", pITd->BP0,
ITdOld.NextTD != pITd->NextTD ? "*" : "", pITd->NextTD,
ITdOld.BE != pITd->BE ? "*" : "", pITd->BE,
pszLogMsg));
Log3(("psw0=%s%x:%s%03x psw1=%s%x:%s%03x psw2=%s%x:%s%03x psw3=%s%x:%s%03x psw4=%s%x:%s%03x psw5=%s%x:%s%03x psw6=%s%x:%s%03x psw7=%s%x:%s%03x\n",
(ITdOld.aPSW[0] >> 12) != (pITd->aPSW[0] >> 12) ? "*" : "", pITd->aPSW[0] >> 12, (ITdOld.aPSW[0] & 0xfff) != (pITd->aPSW[0] & 0xfff) ? "*" : "", pITd->aPSW[0] & 0xfff,
(ITdOld.aPSW[1] >> 12) != (pITd->aPSW[1] >> 12) ? "*" : "", pITd->aPSW[1] >> 12, (ITdOld.aPSW[1] & 0xfff) != (pITd->aPSW[1] & 0xfff) ? "*" : "", pITd->aPSW[1] & 0xfff,
(ITdOld.aPSW[2] >> 12) != (pITd->aPSW[2] >> 12) ? "*" : "", pITd->aPSW[2] >> 12, (ITdOld.aPSW[2] & 0xfff) != (pITd->aPSW[2] & 0xfff) ? "*" : "", pITd->aPSW[2] & 0xfff,
(ITdOld.aPSW[3] >> 12) != (pITd->aPSW[3] >> 12) ? "*" : "", pITd->aPSW[3] >> 12, (ITdOld.aPSW[3] & 0xfff) != (pITd->aPSW[3] & 0xfff) ? "*" : "", pITd->aPSW[3] & 0xfff,
(ITdOld.aPSW[4] >> 12) != (pITd->aPSW[4] >> 12) ? "*" : "", pITd->aPSW[4] >> 12, (ITdOld.aPSW[4] & 0xfff) != (pITd->aPSW[4] & 0xfff) ? "*" : "", pITd->aPSW[4] & 0xfff,
(ITdOld.aPSW[5] >> 12) != (pITd->aPSW[5] >> 12) ? "*" : "", pITd->aPSW[5] >> 12, (ITdOld.aPSW[5] & 0xfff) != (pITd->aPSW[5] & 0xfff) ? "*" : "", pITd->aPSW[5] & 0xfff,
(ITdOld.aPSW[6] >> 12) != (pITd->aPSW[6] >> 12) ? "*" : "", pITd->aPSW[6] >> 12, (ITdOld.aPSW[6] & 0xfff) != (pITd->aPSW[6] & 0xfff) ? "*" : "", pITd->aPSW[6] & 0xfff,
(ITdOld.aPSW[7] >> 12) != (pITd->aPSW[7] >> 12) ? "*" : "", pITd->aPSW[7] >> 12, (ITdOld.aPSW[7] & 0xfff) != (pITd->aPSW[7] & 0xfff) ? "*" : "", pITd->aPSW[7] & 0xfff));
}
# else
RT_NOREF(pszLogMsg);
# endif
ohciR3PutDWords(pThis, ITdAddr, (uint32_t *)pITd, sizeof(*pITd) / sizeof(uint32_t));
}
# ifdef LOG_ENABLED
/**
* Core TD queue dumper. LOG_ENABLED builds only.
*/
DECLINLINE(void) ohciR3DumpTdQueueCore(POHCI pThis, uint32_t GCPhysHead, uint32_t GCPhysTail, bool fFull)
{
uint32_t GCPhys = GCPhysHead;
int cMax = 100;
for (;;)
{
OHCITD Td;
Log4(("%#010x%s%s", GCPhys,
GCPhys && ohciR3InFlightFind(pThis, GCPhys) >= 0 ? "~" : "",
GCPhys && ohciR3InDoneQueueFind(pThis, GCPhys) >= 0 ? "^" : ""));
if (GCPhys == 0 || GCPhys == GCPhysTail)
break;
/* can't use ohciR3ReadTd() because of Log4. */
ohciR3GetDWords(pThis, GCPhys, (uint32_t *)&Td, sizeof(Td) >> 2);
if (fFull)
Log4((" [R=%d DP=%d DI=%d T=%d EC=%d CC=%#x CBP=%#010x NextTD=%#010x BE=%#010x] -> ",
(Td.hwinfo >> 18) & 1,
(Td.hwinfo >> 19) & 3,
(Td.hwinfo >> 21) & 7,
(Td.hwinfo >> 24) & 3,
(Td.hwinfo >> 26) & 3,
(Td.hwinfo >> 28) &15,
Td.cbp,
Td.NextTD,
Td.be));
else
Log4((" -> "));
GCPhys = Td.NextTD & ED_PTR_MASK;
Assert(GCPhys != GCPhysHead);
Assert(cMax-- > 0); NOREF(cMax);
}
}
/**
* Dumps a TD queue. LOG_ENABLED builds only.
*/
DECLINLINE(void) ohciR3DumpTdQueue(POHCI pThis, uint32_t GCPhysHead, const char *pszMsg)
{
if (pszMsg)
Log4(("%s: ", pszMsg));
ohciR3DumpTdQueueCore(pThis, GCPhysHead, 0, true);
Log4(("\n"));
}
/**
* Core ITD queue dumper. LOG_ENABLED builds only.
*/
DECLINLINE(void) ohciR3DumpITdQueueCore(POHCI pThis, uint32_t GCPhysHead, uint32_t GCPhysTail, bool fFull)
{
RT_NOREF(fFull);
uint32_t GCPhys = GCPhysHead;
int cMax = 100;
for (;;)
{
OHCIITD ITd;
Log4(("%#010x%s%s", GCPhys,
GCPhys && ohciR3InFlightFind(pThis, GCPhys) >= 0 ? "~" : "",
GCPhys && ohciR3InDoneQueueFind(pThis, GCPhys) >= 0 ? "^" : ""));
if (GCPhys == 0 || GCPhys == GCPhysTail)
break;
/* can't use ohciR3ReadTd() because of Log4. */
ohciR3GetDWords(pThis, GCPhys, (uint32_t *)&ITd, sizeof(ITd) / sizeof(uint32_t));
/*if (fFull)
Log4((" [R=%d DP=%d DI=%d T=%d EC=%d CC=%#x CBP=%#010x NextTD=%#010x BE=%#010x] -> ",
(Td.hwinfo >> 18) & 1,
(Td.hwinfo >> 19) & 3,
(Td.hwinfo >> 21) & 7,
(Td.hwinfo >> 24) & 3,
(Td.hwinfo >> 26) & 3,
(Td.hwinfo >> 28) &15,
Td.cbp,
Td.NextTD,
Td.be));
else*/
Log4((" -> "));
GCPhys = ITd.NextTD & ED_PTR_MASK;
Assert(GCPhys != GCPhysHead);
Assert(cMax-- > 0); NOREF(cMax);
}
}
/**
* Dumps a ED list. LOG_ENABLED builds only.
*/
DECLINLINE(void) ohciR3DumpEdList(POHCI pThis, uint32_t GCPhysHead, const char *pszMsg, bool fTDs)
{
RT_NOREF(fTDs);
uint32_t GCPhys = GCPhysHead;
if (pszMsg)
Log4(("%s:", pszMsg));
for (;;)
{
OHCIED Ed;
/* ED */
Log4((" %#010x={", GCPhys));
if (!GCPhys)
{
Log4(("END}\n"));
return;
}
/* TDs */
ohciR3ReadEd(pThis, GCPhys, &Ed);
if (Ed.hwinfo & ED_HWINFO_ISO)
Log4(("[I]"));
if ((Ed.HeadP & ED_HEAD_HALTED) || (Ed.hwinfo & ED_HWINFO_SKIP))
{
if ((Ed.HeadP & ED_HEAD_HALTED) && (Ed.hwinfo & ED_HWINFO_SKIP))
Log4(("SH}"));
else if (Ed.hwinfo & ED_HWINFO_SKIP)
Log4(("S-}"));
else
Log4(("-H}"));
}
else
{
if (Ed.hwinfo & ED_HWINFO_ISO)
ohciR3DumpITdQueueCore(pThis, Ed.HeadP & ED_PTR_MASK, Ed.TailP & ED_PTR_MASK, false);
else
ohciR3DumpTdQueueCore(pThis, Ed.HeadP & ED_PTR_MASK, Ed.TailP & ED_PTR_MASK, false);
Log4(("}"));
}
/* next */
GCPhys = Ed.NextED & ED_PTR_MASK;
Assert(GCPhys != GCPhysHead);
}
/* not reached */
}
# endif /* LOG_ENABLED */
DECLINLINE(int) ohciR3InFlightFindFree(POHCI pThis, const int iStart)
{
unsigned i = iStart;
while (i < RT_ELEMENTS(pThis->aInFlight))
{
if (pThis->aInFlight[i].GCPhysTD == 0)
return i;
i++;
}
i = iStart;
while (i-- > 0)
{
if (pThis->aInFlight[i].GCPhysTD == 0)
return i;
}
return -1;
}
/**
* Record an in-flight TD.
*
* @param pThis OHCI instance data.
* @param GCPhysTD Physical address of the TD.
* @param pUrb The URB.
*/
static void ohciR3InFlightAdd(POHCI pThis, uint32_t GCPhysTD, PVUSBURB pUrb)
{
int i = ohciR3InFlightFindFree(pThis, (GCPhysTD >> 4) % RT_ELEMENTS(pThis->aInFlight));
if (i >= 0)
{
# ifdef LOG_ENABLED
pUrb->pHci->u32FrameNo = pThis->HcFmNumber;
# endif
pThis->aInFlight[i].GCPhysTD = GCPhysTD;
pThis->aInFlight[i].pUrb = pUrb;
pThis->cInFlight++;
return;
}
AssertMsgFailed(("Out of space cInFlight=%d!\n", pThis->cInFlight));
}
/**
* Record in-flight TDs for an URB.
*
* @param pThis OHCI instance data.
* @param pUrb The URB.
*/
static void ohciR3InFlightAddUrb(POHCI pThis, PVUSBURB pUrb)
{
for (unsigned iTd = 0; iTd < pUrb->pHci->cTds; iTd++)
ohciR3InFlightAdd(pThis, pUrb->paTds[iTd].TdAddr, pUrb);
}
/**
* Finds a in-flight TD.
*
* @returns Index of the record.
* @returns -1 if not found.
* @param pThis OHCI instance data.
* @param GCPhysTD Physical address of the TD.
* @remark This has to be fast.
*/
static int ohciR3InFlightFind(POHCI pThis, uint32_t GCPhysTD)
{
unsigned cLeft = pThis->cInFlight;
unsigned i = (GCPhysTD >> 4) % RT_ELEMENTS(pThis->aInFlight);
const int iLast = i;
while (i < RT_ELEMENTS(pThis->aInFlight))
{
if (pThis->aInFlight[i].GCPhysTD == GCPhysTD)
return i;
if (pThis->aInFlight[i].GCPhysTD)
if (cLeft-- <= 1)
return -1;
i++;
}
i = iLast;
while (i-- > 0)
{
if (pThis->aInFlight[i].GCPhysTD == GCPhysTD)
return i;
if (pThis->aInFlight[i].GCPhysTD)
if (cLeft-- <= 1)
return -1;
}
return -1;
}
/**
* Checks if a TD is in-flight.
*
* @returns true if in flight, false if not.
* @param pThis OHCI instance data.
* @param GCPhysTD Physical address of the TD.
*/
static bool ohciR3IsTdInFlight(POHCI pThis, uint32_t GCPhysTD)
{
return ohciR3InFlightFind(pThis, GCPhysTD) >= 0;
}
/**
* Returns a URB associated with an in-flight TD, if any.
*
* @returns pointer to URB if TD is in flight.
* @returns NULL if not in flight.
* @param pThis OHCI instance data.
* @param GCPhysTD Physical address of the TD.
*/
static PVUSBURB ohciR3TdInFlightUrb(POHCI pThis, uint32_t GCPhysTD)
{
int i;
i = ohciR3InFlightFind(pThis, GCPhysTD);
if ( i >= 0 )
return pThis->aInFlight[i].pUrb;
return NULL;
}
/**
* Removes a in-flight TD.
*
* @returns 0 if found. For logged builds this is the number of frames the TD has been in-flight.
* @returns -1 if not found.
* @param pThis OHCI instance data.
* @param GCPhysTD Physical address of the TD.
*/
static int ohciR3InFlightRemove(POHCI pThis, uint32_t GCPhysTD)
{
int i = ohciR3InFlightFind(pThis, GCPhysTD);
if (i >= 0)
{
# ifdef LOG_ENABLED
const int cFramesInFlight = pThis->HcFmNumber - pThis->aInFlight[i].pUrb->pHci->u32FrameNo;
# else
const int cFramesInFlight = 0;
# endif
Log2(("ohciR3InFlightRemove: reaping TD=%#010x %d frames (%#010x-%#010x)\n",
GCPhysTD, cFramesInFlight, pThis->aInFlight[i].pUrb->pHci->u32FrameNo, pThis->HcFmNumber));
pThis->aInFlight[i].GCPhysTD = 0;
pThis->aInFlight[i].pUrb = NULL;
pThis->cInFlight--;
return cFramesInFlight;
}
AssertMsgFailed(("TD %#010x is not in flight\n", GCPhysTD));
return -1;
}
/**
* Removes all TDs associated with a URB from the in-flight tracking.
*
* @returns 0 if found. For logged builds this is the number of frames the TD has been in-flight.
* @returns -1 if not found.
* @param pThis OHCI instance data.
* @param pUrb The URB.
*/
static int ohciR3InFlightRemoveUrb(POHCI pThis, PVUSBURB pUrb)
{
int cFramesInFlight = ohciR3InFlightRemove(pThis, pUrb->paTds[0].TdAddr);
if (pUrb->pHci->cTds > 1)
{
for (unsigned iTd = 1; iTd < pUrb->pHci->cTds; iTd++)
if (ohciR3InFlightRemove(pThis, pUrb->paTds[iTd].TdAddr) < 0)
cFramesInFlight = -1;
}
return cFramesInFlight;
}
# if defined(VBOX_STRICT) || defined(LOG_ENABLED)
/**
* Empties the in-done-queue.
* @param pThis OHCI instance data.
*/
static void ohciR3InDoneQueueZap(POHCI pThis)
{
pThis->cInDoneQueue = 0;
}
/**
* Finds a TD in the in-done-queue.
* @returns >= 0 on success.
* @returns -1 if not found.
* @param pThis OHCI instance data.
* @param GCPhysTD Physical address of the TD.
*/
static int ohciR3InDoneQueueFind(POHCI pThis, uint32_t GCPhysTD)
{
unsigned i = pThis->cInDoneQueue;
while (i-- > 0)
if (pThis->aInDoneQueue[i].GCPhysTD == GCPhysTD)
return i;
return -1;
}
/**
* Checks that the specified TD is not in the done queue.
* @param pThis OHCI instance data.
* @param GCPhysTD Physical address of the TD.
*/
static bool ohciR3InDoneQueueCheck(POHCI pThis, uint32_t GCPhysTD)
{
int i = ohciR3InDoneQueueFind(pThis, GCPhysTD);
# if 0
/* This condition has been observed with the USB tablet emulation or with
* a real USB mouse and an SMP XP guest. I am also not sure if this is
* really a problem for us. The assertion checks that the guest doesn't
* re-submit a TD which is still in the done queue. It seems to me that
* this should only be a problem if we either keep track of TDs in the done
* queue somewhere else as well (in which case we should also free those
* references in time, and I can't see any code doing that) or if we
* manipulate TDs in the done queue in some way that might fail if they are
* re-submitted (can't see anything like that either).
*/
AssertMsg(i < 0, ("TD %#010x (i=%d)\n", GCPhysTD, i));
# endif
return i < 0;
}
# if defined(VBOX_STRICT) && defined(LOG_ENABLED)
/**
* Adds a TD to the in-done-queue tracking, checking that it's not there already.
* @param pThis OHCI instance data.
* @param GCPhysTD Physical address of the TD.
*/
static void ohciR3InDoneQueueAdd(POHCI pThis, uint32_t GCPhysTD)
{
Assert(pThis->cInDoneQueue + 1 <= RT_ELEMENTS(pThis->aInDoneQueue));
if (ohciR3InDoneQueueCheck(pThis, GCPhysTD))
pThis->aInDoneQueue[pThis->cInDoneQueue++].GCPhysTD = GCPhysTD;
}
# endif /* VBOX_STRICT */
# endif /* defined(VBOX_STRICT) || defined(LOG_ENABLED) */
/**
* OHCI Transport Buffer - represents a OHCI Transport Descriptor (TD).
* A TD may be split over max 2 pages.
*/
typedef struct OHCIBUF
{
/** Pages involved. */
struct OHCIBUFVEC
{
/** The 32-bit physical address of this part. */
uint32_t Addr;
/** The length. */
uint32_t cb;
} aVecs[2];
/** Number of valid entries in aVecs. */
uint32_t cVecs;
/** The total length. */
uint32_t cbTotal;
} OHCIBUF, *POHCIBUF;
/**
* Sets up a OHCI transport buffer.
*
* @param pBuf Ohci buffer.
* @param cbp Current buffer pointer. 32-bit physical address.
* @param be Last byte in buffer (BufferEnd). 32-bit physical address.
*/
static void ohciR3BufInit(POHCIBUF pBuf, uint32_t cbp, uint32_t be)
{
if (!cbp || !be)
{
pBuf->cVecs = 0;
pBuf->cbTotal = 0;
Log2(("ohci: cbp=%#010x be=%#010x cbTotal=0 EMPTY\n", cbp, be));
}
else if ((cbp & ~0xfff) == (be & ~0xfff))
{
pBuf->aVecs[0].Addr = cbp;
pBuf->aVecs[0].cb = (be - cbp) + 1;
pBuf->cVecs = 1;
pBuf->cbTotal = pBuf->aVecs[0].cb;
Log2(("ohci: cbp=%#010x be=%#010x cbTotal=%u\n", cbp, be, pBuf->cbTotal));
}
else
{
pBuf->aVecs[0].Addr = cbp;
pBuf->aVecs[0].cb = 0x1000 - (cbp & 0xfff);
pBuf->aVecs[1].Addr = be & ~0xfff;
pBuf->aVecs[1].cb = (be & 0xfff) + 1;
pBuf->cVecs = 2;
pBuf->cbTotal = pBuf->aVecs[0].cb + pBuf->aVecs[1].cb;
Log2(("ohci: cbp=%#010x be=%#010x cbTotal=%u PAGE FLIP\n", cbp, be, pBuf->cbTotal));
}
}
/**
* Updates a OHCI transport buffer.
*
* This is called upon completion to adjust the sector lengths if
* the total length has changed. (received less then we had space for
* or a partial transfer.)
*
* @param pBuf The buffer to update. cbTotal contains the new total on input.
* While the aVecs[*].cb members is updated upon return.
*/
static void ohciR3BufUpdate(POHCIBUF pBuf)
{
for (uint32_t i = 0, cbCur = 0; i < pBuf->cVecs; i++)
{
if (cbCur + pBuf->aVecs[i].cb > pBuf->cbTotal)
{
pBuf->aVecs[i].cb = pBuf->cbTotal - cbCur;
pBuf->cVecs = i + 1;
return;
}
cbCur += pBuf->aVecs[i].cb;
}
}
/** A worker for ohciR3UnlinkTds(). */
static bool ohciR3UnlinkIsochronousTdInList(POHCI pThis, uint32_t TdAddr, POHCIITD pITd, POHCIED pEd)
{
const uint32_t LastTdAddr = pEd->TailP & ED_PTR_MASK;
Log(("ohciUnlinkIsocTdInList: Unlinking non-head ITD! TdAddr=%#010RX32 HeadTdAddr=%#010RX32 LastEdAddr=%#010RX32\n",
TdAddr, pEd->HeadP & ED_PTR_MASK, LastTdAddr));
AssertMsgReturn(LastTdAddr != TdAddr, ("TdAddr=%#010RX32\n", TdAddr), false);
uint32_t cMax = 256;
uint32_t CurTdAddr = pEd->HeadP & ED_PTR_MASK;
while ( CurTdAddr != LastTdAddr
&& cMax-- > 0)
{
OHCIITD ITd;
ohciR3ReadITd(pThis, CurTdAddr, &ITd);
if ((ITd.NextTD & ED_PTR_MASK) == TdAddr)
{
ITd.NextTD = (pITd->NextTD & ED_PTR_MASK) | (ITd.NextTD & ~ED_PTR_MASK);
ohciR3WriteITd(pThis, CurTdAddr, &ITd, "ohciUnlinkIsocTdInList");
pITd->NextTD &= ~ED_PTR_MASK;
return true;
}
/* next */
CurTdAddr = ITd.NextTD & ED_PTR_MASK;
}
Log(("ohciUnlinkIsocTdInList: TdAddr=%#010RX32 wasn't found in the list!!! (cMax=%d)\n", TdAddr, cMax));
return false;
}
/** A worker for ohciR3UnlinkTds(). */
static bool ohciR3UnlinkGeneralTdInList(POHCI pThis, uint32_t TdAddr, POHCITD pTd, POHCIED pEd)
{
const uint32_t LastTdAddr = pEd->TailP & ED_PTR_MASK;
Log(("ohciR3UnlinkGeneralTdInList: Unlinking non-head TD! TdAddr=%#010RX32 HeadTdAddr=%#010RX32 LastEdAddr=%#010RX32\n",
TdAddr, pEd->HeadP & ED_PTR_MASK, LastTdAddr));
AssertMsgReturn(LastTdAddr != TdAddr, ("TdAddr=%#010RX32\n", TdAddr), false);
uint32_t cMax = 256;
uint32_t CurTdAddr = pEd->HeadP & ED_PTR_MASK;
while ( CurTdAddr != LastTdAddr
&& cMax-- > 0)
{
OHCITD Td;
ohciR3ReadTd(pThis, CurTdAddr, &Td);
if ((Td.NextTD & ED_PTR_MASK) == TdAddr)
{
Td.NextTD = (pTd->NextTD & ED_PTR_MASK) | (Td.NextTD & ~ED_PTR_MASK);
ohciR3WriteTd(pThis, CurTdAddr, &Td, "ohciR3UnlinkGeneralTdInList");
pTd->NextTD &= ~ED_PTR_MASK;
return true;
}
/* next */
CurTdAddr = Td.NextTD & ED_PTR_MASK;
}
Log(("ohciR3UnlinkGeneralTdInList: TdAddr=%#010RX32 wasn't found in the list!!! (cMax=%d)\n", TdAddr, cMax));
return false;
}
/**
* Unlinks the TDs that makes up the URB from the ED.
*
* @returns success indicator. true if successfully unlinked.
* @returns false if the TD was not found in the list.
*/
static bool ohciR3UnlinkTds(POHCI pThis, PVUSBURB pUrb, POHCIED pEd)
{
/*
* Don't unlink more than once.
*/
if (pUrb->pHci->fUnlinked)
return true;
pUrb->pHci->fUnlinked = true;
if (pUrb->enmType == VUSBXFERTYPE_ISOC)
{
for (unsigned iTd = 0; iTd < pUrb->pHci->cTds; iTd++)
{
POHCIITD pITd = (POHCIITD)&pUrb->paTds[iTd].TdCopy[0];
const uint32_t ITdAddr = pUrb->paTds[iTd].TdAddr;
/*
* Unlink the TD from the ED list.
* The normal case is that it's at the head of the list.
*/
Assert((ITdAddr & ED_PTR_MASK) == ITdAddr);
if ((pEd->HeadP & ED_PTR_MASK) == ITdAddr)
{
pEd->HeadP = (pITd->NextTD & ED_PTR_MASK) | (pEd->HeadP & ~ED_PTR_MASK);
pITd->NextTD &= ~ED_PTR_MASK;
}
else
{
/*
* It's probably somewhere in the list, not a unlikely situation with
* the current isochronous code.
*/
if (!ohciR3UnlinkIsochronousTdInList(pThis, ITdAddr, pITd, pEd))
return false;
}
}
}
else
{
for (unsigned iTd = 0; iTd < pUrb->pHci->cTds; iTd++)
{
POHCITD pTd = (POHCITD)&pUrb->paTds[iTd].TdCopy[0];
const uint32_t TdAddr = pUrb->paTds[iTd].TdAddr;
/** @todo r=bird: Messing with the toggle flag in prepare is probably not correct
* when we encounter a STALL error, 4.3.1.3.7.2: ''If an endpoint returns a STALL
* PID, the Host Controller retires the General TD with the ConditionCode set
* to STALL and halts the endpoint. The CurrentBufferPointer, ErrorCount, and
* dataToggle fields retain the values that they had at the start of the
* transaction.'' */
/* update toggle and set data toggle carry */
pTd->hwinfo &= ~TD_HWINFO_TOGGLE;
if ( pTd->hwinfo & TD_HWINFO_TOGGLE_HI )
{
if ( !!(pTd->hwinfo & TD_HWINFO_TOGGLE_LO) ) /** @todo r=bird: is it just me or doesn't this make sense at all? */
pTd->hwinfo |= TD_HWINFO_TOGGLE_LO;
else
pTd->hwinfo &= ~TD_HWINFO_TOGGLE_LO;
}
else
{
if ( !!(pEd->HeadP & ED_HEAD_CARRY) ) /** @todo r=bird: is it just me or doesn't this make sense at all? */
pEd->HeadP |= ED_HEAD_CARRY;
else
pEd->HeadP &= ~ED_HEAD_CARRY;
}
/*
* Unlink the TD from the ED list.
* The normal case is that it's at the head of the list.
*/
Assert((TdAddr & ED_PTR_MASK) == TdAddr);
if ((pEd->HeadP & ED_PTR_MASK) == TdAddr)
{
pEd->HeadP = (pTd->NextTD & ED_PTR_MASK) | (pEd->HeadP & ~ED_PTR_MASK);
pTd->NextTD &= ~ED_PTR_MASK;
}
else
{
/*
* The TD is probably somewhere in the list.
*
* This shouldn't ever happen unless there was a failure! Even on failure,
* we can screw up the HCD state by picking out a TD from within the list
* like this! If this turns out to be a problem, we have to find a better
* solution. For now we'll hope the HCD handles it...
*/
if (!ohciR3UnlinkGeneralTdInList(pThis, TdAddr, pTd, pEd))
return false;
}
/*
* Only unlink the first TD on error.
* See comment in ohciR3RhXferCompleteGeneralURB().
*/
if (pUrb->enmStatus != VUSBSTATUS_OK)
break;
}
}
return true;
}
/**
* Checks that the transport descriptors associated with the URB
* hasn't been changed in any way indicating that they may have been canceled.
*
* This rountine also updates the TD copies contained within the URB.
*
* @returns true if the URB has been canceled, otherwise false.
* @param pThis The OHCI instance.
* @param pUrb The URB in question.
* @param pEd The ED pointer (optional).
*/
static bool ohciR3HasUrbBeenCanceled(POHCI pThis, PVUSBURB pUrb, PCOHCIED pEd)
{
if (!pUrb)
return true;
/*
* Make sure we've got an endpoint descriptor so we can
* check for tail TDs.
*/
OHCIED Ed;
if (!pEd)
{
ohciR3ReadEd(pThis, pUrb->pHci->EdAddr, &Ed);
pEd = &Ed;
}
if (pUrb->enmType == VUSBXFERTYPE_ISOC)
{
for (unsigned iTd = 0; iTd < pUrb->pHci->cTds; iTd++)
{
union
{
OHCIITD ITd;
uint32_t au32[8];
} u;
if ( (pUrb->paTds[iTd].TdAddr & ED_PTR_MASK)
== (pEd->TailP & ED_PTR_MASK))
{
Log(("%s: ohciR3HasUrbBeenCanceled: iTd=%d cTds=%d TdAddr=%#010RX32 canceled (tail)! [iso]\n",
pUrb->pszDesc, iTd, pUrb->pHci->cTds, pUrb->paTds[iTd].TdAddr));
STAM_COUNTER_INC(&pThis->StatCanceledIsocUrbs);
return true;
}
ohciR3ReadITd(pThis, pUrb->paTds[iTd].TdAddr, &u.ITd);
if ( u.au32[0] != pUrb->paTds[iTd].TdCopy[0] /* hwinfo */
|| u.au32[1] != pUrb->paTds[iTd].TdCopy[1] /* bp0 */
|| u.au32[3] != pUrb->paTds[iTd].TdCopy[3] /* be */
|| ( u.au32[2] != pUrb->paTds[iTd].TdCopy[2] /* NextTD */
&& iTd + 1 < pUrb->pHci->cTds /* ignore the last one */)
|| u.au32[4] != pUrb->paTds[iTd].TdCopy[4] /* psw0&1 */
|| u.au32[5] != pUrb->paTds[iTd].TdCopy[5] /* psw2&3 */
|| u.au32[6] != pUrb->paTds[iTd].TdCopy[6] /* psw4&5 */
|| u.au32[7] != pUrb->paTds[iTd].TdCopy[7] /* psw6&7 */
)
{
Log(("%s: ohciR3HasUrbBeenCanceled: iTd=%d cTds=%d TdAddr=%#010RX32 canceled! [iso]\n",
pUrb->pszDesc, iTd, pUrb->pHci->cTds, pUrb->paTds[iTd].TdAddr));
Log2((" %.*Rhxs (cur)\n"
"!= %.*Rhxs (copy)\n",
sizeof(u.ITd), &u.ITd, sizeof(u.ITd), &pUrb->paTds[iTd].TdCopy[0]));
STAM_COUNTER_INC(&pThis->StatCanceledIsocUrbs);
return true;
}
pUrb->paTds[iTd].TdCopy[2] = u.au32[2];
}
}
else
{
for (unsigned iTd = 0; iTd < pUrb->pHci->cTds; iTd++)
{
union
{
OHCITD Td;
uint32_t au32[4];
} u;
if ( (pUrb->paTds[iTd].TdAddr & ED_PTR_MASK)
== (pEd->TailP & ED_PTR_MASK))
{
Log(("%s: ohciR3HasUrbBeenCanceled: iTd=%d cTds=%d TdAddr=%#010RX32 canceled (tail)!\n",
pUrb->pszDesc, iTd, pUrb->pHci->cTds, pUrb->paTds[iTd].TdAddr));
STAM_COUNTER_INC(&pThis->StatCanceledGenUrbs);
return true;
}
ohciR3ReadTd(pThis, pUrb->paTds[iTd].TdAddr, &u.Td);
if ( u.au32[0] != pUrb->paTds[iTd].TdCopy[0] /* hwinfo */
|| u.au32[1] != pUrb->paTds[iTd].TdCopy[1] /* cbp */
|| u.au32[3] != pUrb->paTds[iTd].TdCopy[3] /* be */
|| ( u.au32[2] != pUrb->paTds[iTd].TdCopy[2] /* NextTD */
&& iTd + 1 < pUrb->pHci->cTds /* ignore the last one */)
)
{
Log(("%s: ohciR3HasUrbBeenCanceled: iTd=%d cTds=%d TdAddr=%#010RX32 canceled!\n",
pUrb->pszDesc, iTd, pUrb->pHci->cTds, pUrb->paTds[iTd].TdAddr));
Log2((" %.*Rhxs (cur)\n"
"!= %.*Rhxs (copy)\n",
sizeof(u.Td), &u.Td, sizeof(u.Td), &pUrb->paTds[iTd].TdCopy[0]));
STAM_COUNTER_INC(&pThis->StatCanceledGenUrbs);
return true;
}
pUrb->paTds[iTd].TdCopy[2] = u.au32[2];
}
}
return false;
}
/**
* Returns the OHCI_CC_* corresponding to the VUSB status code.
*
* @returns OHCI_CC_* value.
* @param enmStatus The VUSB status code.
*/
static uint32_t ohciR3VUsbStatus2OhciStatus(VUSBSTATUS enmStatus)
{
switch (enmStatus)
{
case VUSBSTATUS_OK: return OHCI_CC_NO_ERROR;
case VUSBSTATUS_STALL: return OHCI_CC_STALL;
case VUSBSTATUS_CRC: return OHCI_CC_CRC;
case VUSBSTATUS_DATA_UNDERRUN: return OHCI_CC_DATA_UNDERRUN;
case VUSBSTATUS_DATA_OVERRUN: return OHCI_CC_DATA_OVERRUN;
case VUSBSTATUS_DNR: return OHCI_CC_DNR;
case VUSBSTATUS_NOT_ACCESSED: return OHCI_CC_NOT_ACCESSED_1;
default:
Log(("pUrb->enmStatus=%#x!!!\n", enmStatus));
return OHCI_CC_DNR;
}
}
/**
* Lock the given OHCI controller instance.
*
* @returns nothing.
* @param pThis The OHCI controller instance to lock.
*/
DECLINLINE(void) ohciR3Lock(POHCI pThis)
{
RTCritSectEnter(&pThis->CritSect);
# ifdef VBOX_WITH_OHCI_PHYS_READ_CACHE
/* Clear all caches here to avoid reading stale data from previous lock holders. */
ohciR3PhysReadCacheClear(pThis->pCacheED);
ohciR3PhysReadCacheClear(pThis->pCacheTD);
# endif
}
/**
* Unlocks the given OHCI controller instance.
*
* @returns nothing.
* @param pThis The OHCI controller instance to unlock.
*/
DECLINLINE(void) ohciR3Unlock(POHCI pThis)
{
# ifdef VBOX_WITH_OHCI_PHYS_READ_CACHE
/*
* Clear all caches here to avoid leaving stale data behind (paranoia^2,
* already done in ohciR3Lock).
*/
ohciR3PhysReadCacheClear(pThis->pCacheED);
ohciR3PhysReadCacheClear(pThis->pCacheTD);
# endif
RTCritSectLeave(&pThis->CritSect);
}
/**
* Worker for ohciR3RhXferCompletion that handles the completion of
* a URB made up of isochronous TDs.
*
* In general, all URBs should have status OK.
*/
static void ohciR3RhXferCompleteIsochronousURB(POHCI pThis, PVUSBURB pUrb /*, POHCIED pEd , int cFmAge*/)
{
/*
* Copy the data back (if IN operation) and update the TDs.
*/
for (unsigned iTd = 0; iTd < pUrb->pHci->cTds; iTd++)
{
POHCIITD pITd = (POHCIITD)&pUrb->paTds[iTd].TdCopy[0];
const uint32_t ITdAddr = pUrb->paTds[iTd].TdAddr;
const unsigned cFrames = ((pITd->HwInfo & ITD_HWINFO_FC) >> ITD_HWINFO_FC_SHIFT) + 1;
unsigned R = (pUrb->pHci->u32FrameNo & ITD_HWINFO_SF) - (pITd->HwInfo & ITD_HWINFO_SF);
if (R >= 8)
R = 0; /* submitted ahead of time. */
/*
* Only one case of TD level condition code is document, so
* just set NO_ERROR here to reduce number duplicate code.
*/
pITd->HwInfo &= ~TD_HWINFO_CC;
AssertCompile(OHCI_CC_NO_ERROR == 0);
if (pUrb->enmStatus == VUSBSTATUS_OK)
{
/*
* Update the frames and copy back the data.
* We assume that we don't get incorrect lengths here.
*/
for (unsigned i = 0; i < cFrames; i++)
{
if ( i < R
|| pUrb->aIsocPkts[i - R].enmStatus == VUSBSTATUS_NOT_ACCESSED)
{
/* It should already be NotAccessed. */
pITd->aPSW[i] |= 0xe000; /* (Don't touch the 12th bit.) */
continue;
}
/* Update the PSW (save the offset first in case of a IN). */
uint32_t off = pITd->aPSW[i] & ITD_PSW_OFFSET;
pITd->aPSW[i] = ohciR3VUsbStatus2OhciStatus(pUrb->aIsocPkts[i - R].enmStatus)
>> (TD_HWINFO_CC_SHIFT - ITD_PSW_CC_SHIFT);
if ( pUrb->enmDir == VUSBDIRECTION_IN
&& ( pUrb->aIsocPkts[i - R].enmStatus == VUSBSTATUS_OK
|| pUrb->aIsocPkts[i - R].enmStatus == VUSBSTATUS_DATA_UNDERRUN
|| pUrb->aIsocPkts[i - R].enmStatus == VUSBSTATUS_DATA_OVERRUN))
{
/* Set the size. */
const unsigned cb = pUrb->aIsocPkts[i - R].cb;
pITd->aPSW[i] |= cb & ITD_PSW_SIZE;
/* Copy data. */
if (cb)
{
uint8_t *pb = &pUrb->abData[pUrb->aIsocPkts[i - R].off];
if (off + cb > 0x1000)
{
if (off < 0x1000)
{
/* both */
const unsigned cb0 = 0x1000 - off;
ohciR3PhysWrite(pThis, (pITd->BP0 & ITD_BP0_MASK) + off, pb, cb0);
ohciR3PhysWrite(pThis, pITd->BE & ITD_BP0_MASK, pb + cb0, cb - cb0);
}
else /* only in the 2nd page */
ohciR3PhysWrite(pThis, (pITd->BE & ITD_BP0_MASK) + (off & ITD_BP0_MASK), pb, cb);
}
else /* only in the 1st page */
ohciR3PhysWrite(pThis, (pITd->BP0 & ITD_BP0_MASK) + off, pb, cb);
Log5(("packet %d: off=%#x cb=%#x pb=%p (%#x)\n"
"%.*Rhxd\n",
i + R, off, cb, pb, pb - &pUrb->abData[0], cb, pb));
//off += cb;
}
}
}
/*
* If the last package ended with a NotAccessed status, set ITD CC
* to DataOverrun to indicate scheduling overrun.
*/
if (pUrb->aIsocPkts[pUrb->cIsocPkts - 1].enmStatus == VUSBSTATUS_NOT_ACCESSED)
pITd->HwInfo |= OHCI_CC_DATA_OVERRUN;
}
else
{
Log(("DevOHCI: Taking untested code path at line %d...\n", __LINE__));
/*
* Most status codes only applies to the individual packets.
*
* If we get a URB level error code of this kind, we'll distribute
* it to all the packages unless some other status is available for
* a package. This is a bit fuzzy, and we will get rid of this code
* before long!
*/
//if (pUrb->enmStatus != VUSBSTATUS_DATA_OVERRUN)
{
const unsigned uCC = ohciR3VUsbStatus2OhciStatus(pUrb->enmStatus)
>> (TD_HWINFO_CC_SHIFT - ITD_PSW_CC_SHIFT);
for (unsigned i = 0; i < cFrames; i++)
pITd->aPSW[i] = uCC;
}
//else
// pITd->HwInfo |= ohciR3VUsbStatus2OhciStatus(pUrb->enmStatus);
}
/*
* Update the done queue interrupt timer.
*/
uint32_t DoneInt = (pITd->HwInfo & ITD_HWINFO_DI) >> ITD_HWINFO_DI_SHIFT;
if ((pITd->HwInfo & TD_HWINFO_CC) != OHCI_CC_NO_ERROR)
DoneInt = 0; /* It's cleared on error. */
if ( DoneInt != 0x7
&& DoneInt < pThis->dqic)
pThis->dqic = DoneInt;
/*
* Move on to the done list and write back the modified TD.
*/
# ifdef LOG_ENABLED
if (!pThis->done)
pThis->u32FmDoneQueueTail = pThis->HcFmNumber;
# ifdef VBOX_STRICT
ohciR3InDoneQueueAdd(pThis, ITdAddr);
# endif
# endif
pITd->NextTD = pThis->done;
pThis->done = ITdAddr;
Log(("%s: ohciR3RhXferCompleteIsochronousURB: ITdAddr=%#010x EdAddr=%#010x SF=%#x (%#x) CC=%#x FC=%d "
"psw0=%x:%x psw1=%x:%x psw2=%x:%x psw3=%x:%x psw4=%x:%x psw5=%x:%x psw6=%x:%x psw7=%x:%x R=%d\n",
pUrb->pszDesc, ITdAddr,
pUrb->pHci->EdAddr,
pITd->HwInfo & ITD_HWINFO_SF, pThis->HcFmNumber,
(pITd->HwInfo & ITD_HWINFO_CC) >> ITD_HWINFO_CC_SHIFT,
(pITd->HwInfo & ITD_HWINFO_FC) >> ITD_HWINFO_FC_SHIFT,
pITd->aPSW[0] >> ITD_PSW_CC_SHIFT, pITd->aPSW[0] & ITD_PSW_SIZE,
pITd->aPSW[1] >> ITD_PSW_CC_SHIFT, pITd->aPSW[1] & ITD_PSW_SIZE,
pITd->aPSW[2] >> ITD_PSW_CC_SHIFT, pITd->aPSW[2] & ITD_PSW_SIZE,
pITd->aPSW[3] >> ITD_PSW_CC_SHIFT, pITd->aPSW[3] & ITD_PSW_SIZE,
pITd->aPSW[4] >> ITD_PSW_CC_SHIFT, pITd->aPSW[4] & ITD_PSW_SIZE,
pITd->aPSW[5] >> ITD_PSW_CC_SHIFT, pITd->aPSW[5] & ITD_PSW_SIZE,
pITd->aPSW[6] >> ITD_PSW_CC_SHIFT, pITd->aPSW[6] & ITD_PSW_SIZE,
pITd->aPSW[7] >> ITD_PSW_CC_SHIFT, pITd->aPSW[7] & ITD_PSW_SIZE,
R));
ohciR3WriteITd(pThis, ITdAddr, pITd, "retired");
}
}
/**
* Worker for ohciR3RhXferCompletion that handles the completion of
* a URB made up of general TDs.
*/
static void ohciR3RhXferCompleteGeneralURB(POHCI pThis, PVUSBURB pUrb, POHCIED pEd, int cFmAge)
{
RT_NOREF(cFmAge);
/*
* Copy the data back (if IN operation) and update the TDs.
*/
unsigned cbLeft = pUrb->cbData;
uint8_t *pb = &pUrb->abData[0];
for (unsigned iTd = 0; iTd < pUrb->pHci->cTds; iTd++)
{
POHCITD pTd = (POHCITD)&pUrb->paTds[iTd].TdCopy[0];
const uint32_t TdAddr = pUrb->paTds[iTd].TdAddr;
/*
* Setup a ohci transfer buffer and calc the new cbp value.
*/
OHCIBUF Buf;
ohciR3BufInit(&Buf, pTd->cbp, pTd->be);
uint32_t NewCbp;
if (cbLeft >= Buf.cbTotal)
NewCbp = 0;
else
{
/* (len may have changed for short transfers) */
Buf.cbTotal = cbLeft;
ohciR3BufUpdate(&Buf);
Assert(Buf.cVecs >= 1);
NewCbp = Buf.aVecs[Buf.cVecs-1].Addr + Buf.aVecs[Buf.cVecs-1].cb;
}
/*
* Write back IN buffers.
*/
if ( pUrb->enmDir == VUSBDIRECTION_IN
&& ( pUrb->enmStatus == VUSBSTATUS_OK
|| pUrb->enmStatus == VUSBSTATUS_DATA_OVERRUN
|| pUrb->enmStatus == VUSBSTATUS_DATA_UNDERRUN)
&& Buf.cbTotal > 0)
{
Assert(Buf.cVecs > 0);
ohciR3PhysWrite(pThis, Buf.aVecs[0].Addr, pb, Buf.aVecs[0].cb);
if (Buf.cVecs > 1)
ohciR3PhysWrite(pThis, Buf.aVecs[1].Addr, pb + Buf.aVecs[0].cb, Buf.aVecs[1].cb);
}
/* advance the data buffer. */
cbLeft -= Buf.cbTotal;
pb += Buf.cbTotal;
/*
* Set writeback field.
*/
/* zero out writeback fields for retirement */
pTd->hwinfo &= ~TD_HWINFO_CC;
/* always update the CurrentBufferPointer; essential for underrun/overrun errors */
pTd->cbp = NewCbp;
if (pUrb->enmStatus == VUSBSTATUS_OK)
{
pTd->hwinfo &= ~TD_HWINFO_ERRORS;
/* update done queue interrupt timer */
uint32_t DoneInt = (pTd->hwinfo & TD_HWINFO_DI) >> 21;
if ( DoneInt != 0x7
&& DoneInt < pThis->dqic)
pThis->dqic = DoneInt;
Log(("%s: ohciR3RhXferCompleteGeneralURB: ED=%#010x TD=%#010x Age=%d enmStatus=%d cbTotal=%#x NewCbp=%#010RX32 dqic=%d\n",
pUrb->pszDesc, pUrb->pHci->EdAddr, TdAddr, cFmAge, pUrb->enmStatus, Buf.cbTotal, NewCbp, pThis->dqic));
}
else
{
Log(("%s: ohciR3RhXferCompleteGeneralURB: HALTED ED=%#010x TD=%#010x (age %d) pUrb->enmStatus=%d\n",
pUrb->pszDesc, pUrb->pHci->EdAddr, TdAddr, cFmAge, pUrb->enmStatus));
pEd->HeadP |= ED_HEAD_HALTED;
pThis->dqic = 0; /* "If the Transfer Descriptor is being retired with an error,
* then the Done Queue Interrupt Counter is cleared as if the
* InterruptDelay field were zero."
*/
switch (pUrb->enmStatus)
{
case VUSBSTATUS_STALL:
pTd->hwinfo |= OHCI_CC_STALL;
break;
case VUSBSTATUS_CRC:
pTd->hwinfo |= OHCI_CC_CRC;
break;
case VUSBSTATUS_DATA_UNDERRUN:
pTd->hwinfo |= OHCI_CC_DATA_UNDERRUN;
break;
case VUSBSTATUS_DATA_OVERRUN:
pTd->hwinfo |= OHCI_CC_DATA_OVERRUN;
break;
default: /* what the hell */
Log(("pUrb->enmStatus=%#x!!!\n", pUrb->enmStatus));
RT_FALL_THRU();
case VUSBSTATUS_DNR:
pTd->hwinfo |= OHCI_CC_DNR;
break;
}
}
/*
* Move on to the done list and write back the modified TD.
*/
# ifdef LOG_ENABLED
if (!pThis->done)
pThis->u32FmDoneQueueTail = pThis->HcFmNumber;
# ifdef VBOX_STRICT
ohciR3InDoneQueueAdd(pThis, TdAddr);
# endif
# endif
pTd->NextTD = pThis->done;
pThis->done = TdAddr;
ohciR3WriteTd(pThis, TdAddr, pTd, "retired");
/*
* If we've halted the endpoint, we stop here.
* ohciR3UnlinkTds() will make sure we've only unliked the first TD.
*
* The reason for this is that while we can have more than one TD in a URB, real
* OHCI hardware will only deal with one TD at the time and it's therefore incorrect
* to retire TDs after the endpoint has been halted. Win2k will crash or enter infinite
* kernel loop if we don't behave correctly. (See @bugref{1646}.)
*/
if (pEd->HeadP & ED_HEAD_HALTED)
break;
}
}
/**
* Transfer completion callback routine.
*
* VUSB will call this when a transfer have been completed
* in a one or another way.
*
* @param pInterface Pointer to OHCI::ROOTHUB::IRhPort.
* @param pUrb Pointer to the URB in question.
*/
static DECLCALLBACK(void) ohciR3RhXferCompletion(PVUSBIROOTHUBPORT pInterface, PVUSBURB pUrb)
{
POHCI pThis = VUSBIROOTHUBPORT_2_OHCI(pInterface);
LogFlow(("%s: ohciR3RhXferCompletion: EdAddr=%#010RX32 cTds=%d TdAddr0=%#010RX32\n",
pUrb->pszDesc, pUrb->pHci->EdAddr, pUrb->pHci->cTds, pUrb->paTds[0].TdAddr));
ohciR3Lock(pThis);
pThis->fIdle = false; /* Mark as active */
/* get the current end point descriptor. */
OHCIED Ed;
ohciR3ReadEd(pThis, pUrb->pHci->EdAddr, &Ed);
/*
* Check that the URB hasn't been canceled and then try unlink the TDs.
*
* We drop the URB if the ED is marked halted/skip ASSUMING that this
* means the HCD has canceled the URB.
*
* If we succeed here (i.e. not dropping the URB), the TdCopy members will
* be updated but not yet written. We will delay the writing till we're done
* with the data copying, buffer pointer advancing and error handling.
*/
int cFmAge = ohciR3InFlightRemoveUrb(pThis, pUrb);
if (pUrb->enmStatus == VUSBSTATUS_UNDO)
{
/* Leave the TD alone - the HCD doesn't want us talking to the device. */
Log(("%s: ohciR3RhXferCompletion: CANCELED {ED=%#010x cTds=%d TD0=%#010x age %d}\n",
pUrb->pszDesc, pUrb->pHci->EdAddr, pUrb->pHci->cTds, pUrb->paTds[0].TdAddr, cFmAge));
STAM_COUNTER_INC(&pThis->StatDroppedUrbs);
ohciR3Unlock(pThis);
return;
}
bool fHasBeenCanceled = false;
if ( (Ed.HeadP & ED_HEAD_HALTED)
|| (Ed.hwinfo & ED_HWINFO_SKIP)
|| cFmAge < 0
|| (fHasBeenCanceled = ohciR3HasUrbBeenCanceled(pThis, pUrb, &Ed))
|| !ohciR3UnlinkTds(pThis, pUrb, &Ed)
)
{
Log(("%s: ohciR3RhXferCompletion: DROPPED {ED=%#010x cTds=%d TD0=%#010x age %d} because:%s%s%s%s%s!!!\n",
pUrb->pszDesc, pUrb->pHci->EdAddr, pUrb->pHci->cTds, pUrb->paTds[0].TdAddr, cFmAge,
(Ed.HeadP & ED_HEAD_HALTED) ? " ep halted" : "",
(Ed.hwinfo & ED_HWINFO_SKIP) ? " ep skip" : "",
(Ed.HeadP & ED_PTR_MASK) != pUrb->paTds[0].TdAddr ? " ep head-changed" : "",
cFmAge < 0 ? " td not-in-flight" : "",
fHasBeenCanceled ? " td canceled" : ""));
NOREF(fHasBeenCanceled);
STAM_COUNTER_INC(&pThis->StatDroppedUrbs);
ohciR3Unlock(pThis);
return;
}
/*
* Complete the TD updating and write the back.
* When appropriate also copy data back to the guest memory.
*/
if (pUrb->enmType == VUSBXFERTYPE_ISOC)
ohciR3RhXferCompleteIsochronousURB(pThis, pUrb /*, &Ed , cFmAge*/);
else
ohciR3RhXferCompleteGeneralURB(pThis, pUrb, &Ed, cFmAge);
/* finally write back the endpoint descriptor. */
ohciR3WriteEd(pThis, pUrb->pHci->EdAddr, &Ed);
ohciR3Unlock(pThis);
}
/**
* Handle transfer errors.
*
* VUSB calls this when a transfer attempt failed. This function will respond
* indicating whether to retry or complete the URB with failure.
*
* @returns true if the URB should be retired.
* @returns false if the URB should be retried.
* @param pInterface Pointer to OHCI::ROOTHUB::IRhPort.
* @param pUrb Pointer to the URB in question.
*/
static DECLCALLBACK(bool) ohciR3RhXferError(PVUSBIROOTHUBPORT pInterface, PVUSBURB pUrb)
{
POHCI pThis = VUSBIROOTHUBPORT_2_OHCI(pInterface);
/*
* Isochronous URBs can't be retried.
*/
if (pUrb->enmType == VUSBXFERTYPE_ISOC)
return true;
/*
* Don't retry on stall.
*/
if (pUrb->enmStatus == VUSBSTATUS_STALL)
{
Log2(("%s: ohciR3RhXferError: STALL, giving up.\n", pUrb->pszDesc));
return true;
}
ohciR3Lock(pThis);
bool fRetire = false;
/*
* Check if the TDs still are valid.
* This will make sure the TdCopy is up to date.
*/
const uint32_t TdAddr = pUrb->paTds[0].TdAddr;
/** @todo IMPORTANT! we must check if the ED is still valid at this point!!! */
if (ohciR3HasUrbBeenCanceled(pThis, pUrb, NULL))
{
Log(("%s: ohciR3RhXferError: TdAddr0=%#x canceled!\n", pUrb->pszDesc, TdAddr));
fRetire = true;
}
else
{
/*
* Get and update the error counter.
*/
POHCITD pTd = (POHCITD)&pUrb->paTds[0].TdCopy[0];
unsigned cErrs = (pTd->hwinfo & TD_HWINFO_ERRORS) >> TD_ERRORS_SHIFT;
pTd->hwinfo &= ~TD_HWINFO_ERRORS;
cErrs++;
pTd->hwinfo |= (cErrs % TD_ERRORS_MAX) << TD_ERRORS_SHIFT;
ohciR3WriteTd(pThis, TdAddr, pTd, "ohciR3RhXferError");
if (cErrs >= TD_ERRORS_MAX - 1)
{
Log2(("%s: ohciR3RhXferError: too many errors, giving up!\n", pUrb->pszDesc));
fRetire = true;
}
else
Log2(("%s: ohciR3RhXferError: cErrs=%d: retrying...\n", pUrb->pszDesc, cErrs));
}
ohciR3Unlock(pThis);
return fRetire;
}
/**
* Service a general transport descriptor.
*/
static bool ohciR3ServiceTd(POHCI pThis, VUSBXFERTYPE enmType, PCOHCIED pEd, uint32_t EdAddr, uint32_t TdAddr,
uint32_t *pNextTdAddr, const char *pszListName)
{
RT_NOREF(pszListName);
/*
* Read the TD and setup the buffer data.
*/
OHCITD Td;
ohciR3ReadTd(pThis, TdAddr, &Td);
OHCIBUF Buf;
ohciR3BufInit(&Buf, Td.cbp, Td.be);
*pNextTdAddr = Td.NextTD & ED_PTR_MASK;
/*
* Determine the direction.
*/
VUSBDIRECTION enmDir;
switch (pEd->hwinfo & ED_HWINFO_DIR)
{
case ED_HWINFO_OUT: enmDir = VUSBDIRECTION_OUT; break;
case ED_HWINFO_IN: enmDir = VUSBDIRECTION_IN; break;
default:
switch (Td.hwinfo & TD_HWINFO_DIR)
{
case TD_HWINFO_OUT: enmDir = VUSBDIRECTION_OUT; break;
case TD_HWINFO_IN: enmDir = VUSBDIRECTION_IN; break;
case 0: enmDir = VUSBDIRECTION_SETUP; break;
default:
Log(("ohciR3ServiceTd: Invalid direction!!!! Td.hwinfo=%#x Ed.hwdinfo=%#x\n", Td.hwinfo, pEd->hwinfo));
/** @todo Do the correct thing here */
return false;
}
break;
}
pThis->fIdle = false; /* Mark as active */
/*
* Allocate and initialize a new URB.
*/
PVUSBURB pUrb = VUSBIRhNewUrb(pThis->RootHub.pIRhConn, pEd->hwinfo & ED_HWINFO_FUNCTION, NULL,
enmType, enmDir, Buf.cbTotal, 1, NULL);
if (!pUrb)
return false; /* retry later... */
pUrb->EndPt = (pEd->hwinfo & ED_HWINFO_ENDPOINT) >> ED_HWINFO_ENDPOINT_SHIFT;
pUrb->fShortNotOk = !(Td.hwinfo & TD_HWINFO_ROUNDING);
pUrb->enmStatus = VUSBSTATUS_OK;
pUrb->pHci->EdAddr = EdAddr;
pUrb->pHci->fUnlinked = false;
pUrb->pHci->cTds = 1;
pUrb->paTds[0].TdAddr = TdAddr;
pUrb->pHci->u32FrameNo = pThis->HcFmNumber;
AssertCompile(sizeof(pUrb->paTds[0].TdCopy) >= sizeof(Td));
memcpy(pUrb->paTds[0].TdCopy, &Td, sizeof(Td));
/* copy data if out bound transfer. */
pUrb->cbData = Buf.cbTotal;
if ( Buf.cbTotal
&& Buf.cVecs > 0
&& enmDir != VUSBDIRECTION_IN)
{
ohciR3PhysRead(pThis, Buf.aVecs[0].Addr, pUrb->abData, Buf.aVecs[0].cb);
if (Buf.cVecs > 1)
ohciR3PhysRead(pThis, Buf.aVecs[1].Addr, &pUrb->abData[Buf.aVecs[0].cb], Buf.aVecs[1].cb);
}
/*
* Submit the URB.
*/
ohciR3InFlightAdd(pThis, TdAddr, pUrb);
Log(("%s: ohciR3ServiceTd: submitting TdAddr=%#010x EdAddr=%#010x cbData=%#x\n",
pUrb->pszDesc, TdAddr, EdAddr, pUrb->cbData));
ohciR3Unlock(pThis);
int rc = VUSBIRhSubmitUrb(pThis->RootHub.pIRhConn, pUrb, &pThis->RootHub.Led);
ohciR3Lock(pThis);
if (RT_SUCCESS(rc))
return true;
/* Failure cleanup. Can happen if we're still resetting the device or out of resources. */
Log(("ohciR3ServiceTd: failed submitting TdAddr=%#010x EdAddr=%#010x pUrb=%p!!\n",
TdAddr, EdAddr, pUrb));
ohciR3InFlightRemove(pThis, TdAddr);
return false;
}
/**
* Service a the head TD of an endpoint.
*/
static bool ohciR3ServiceHeadTd(POHCI pThis, VUSBXFERTYPE enmType, PCOHCIED pEd, uint32_t EdAddr, const char *pszListName)
{
/*
* Read the TD, after first checking if it's already in-flight.
*/
uint32_t TdAddr = pEd->HeadP & ED_PTR_MASK;
if (ohciR3IsTdInFlight(pThis, TdAddr))
return false;
# if defined(VBOX_STRICT) || defined(LOG_ENABLED)
ohciR3InDoneQueueCheck(pThis, TdAddr);
# endif
return ohciR3ServiceTd(pThis, enmType, pEd, EdAddr, TdAddr, &TdAddr, pszListName);
}
/**
* Service one or more general transport descriptors (bulk or interrupt).
*/
static bool ohciR3ServiceTdMultiple(POHCI pThis, VUSBXFERTYPE enmType, PCOHCIED pEd, uint32_t EdAddr,
uint32_t TdAddr, uint32_t *pNextTdAddr, const char *pszListName)
{
RT_NOREF(pszListName);
/*
* Read the TDs involved in this URB.
*/
struct OHCITDENTRY
{
/** The TD. */
OHCITD Td;
/** The associated OHCI buffer tracker. */
OHCIBUF Buf;
/** The TD address. */
uint32_t TdAddr;
/** Pointer to the next element in the chain (stack). */
struct OHCITDENTRY *pNext;
} Head;
# ifdef VBOX_WITH_OHCI_PHYS_READ_CACHE
ohciR3PhysReadCacheClear(pThis->pCacheTD);
# endif
/* read the head */
ohciR3ReadTd(pThis, TdAddr, &Head.Td);
ohciR3BufInit(&Head.Buf, Head.Td.cbp, Head.Td.be);
Head.TdAddr = TdAddr;
Head.pNext = NULL;
/* combine with more TDs. */
struct OHCITDENTRY *pTail = &Head;
unsigned cbTotal = pTail->Buf.cbTotal;
unsigned cTds = 1;
while ( (pTail->Buf.cbTotal == 0x1000 || pTail->Buf.cbTotal == 0x2000)
&& !(pTail->Td.hwinfo & TD_HWINFO_ROUNDING) /* This isn't right for *BSD, but let's not . */
&& (pTail->Td.NextTD & ED_PTR_MASK) != (pEd->TailP & ED_PTR_MASK)
&& cTds < 128)
{
struct OHCITDENTRY *pCur = (struct OHCITDENTRY *)alloca(sizeof(*pCur));
pCur->pNext = NULL;
pCur->TdAddr = pTail->Td.NextTD & ED_PTR_MASK;
ohciR3ReadTd(pThis, pCur->TdAddr, &pCur->Td);
ohciR3BufInit(&pCur->Buf, pCur->Td.cbp, pCur->Td.be);
/* Don't combine if the direction doesn't match up. There can't actually be
* a mismatch for bulk/interrupt EPs unless the guest is buggy.
*/
if ( (pCur->Td.hwinfo & (TD_HWINFO_DIR))
!= (Head.Td.hwinfo & (TD_HWINFO_DIR)))
break;
pTail->pNext = pCur;
pTail = pCur;
cbTotal += pCur->Buf.cbTotal;
cTds++;
}
/* calc next TD address */
*pNextTdAddr = pTail->Td.NextTD & ED_PTR_MASK;
/*
* Determine the direction.
*/
VUSBDIRECTION enmDir;
switch (pEd->hwinfo & ED_HWINFO_DIR)
{
case ED_HWINFO_OUT: enmDir = VUSBDIRECTION_OUT; break;
case ED_HWINFO_IN: enmDir = VUSBDIRECTION_IN; break;
default:
Log(("ohciR3ServiceTdMultiple: WARNING! Ed.hwdinfo=%#x bulk or interrupt EP shouldn't rely on the TD for direction...\n", pEd->hwinfo));
switch (Head.Td.hwinfo & TD_HWINFO_DIR)
{
case TD_HWINFO_OUT: enmDir = VUSBDIRECTION_OUT; break;
case TD_HWINFO_IN: enmDir = VUSBDIRECTION_IN; break;
default:
Log(("ohciR3ServiceTdMultiple: Invalid direction!!!! Head.Td.hwinfo=%#x Ed.hwdinfo=%#x\n", Head.Td.hwinfo, pEd->hwinfo));
/** @todo Do the correct thing here */
return false;
}
break;
}
pThis->fIdle = false; /* Mark as active */
/*
* Allocate and initialize a new URB.
*/
PVUSBURB pUrb = VUSBIRhNewUrb(pThis->RootHub.pIRhConn, pEd->hwinfo & ED_HWINFO_FUNCTION, NULL,
enmType, enmDir, cbTotal, cTds, "ohciR3ServiceTdMultiple");
if (!pUrb)
/* retry later... */
return false;
Assert(pUrb->cbData == cbTotal);
pUrb->enmType = enmType;
pUrb->EndPt = (pEd->hwinfo & ED_HWINFO_ENDPOINT) >> ED_HWINFO_ENDPOINT_SHIFT;
pUrb->enmDir = enmDir;
pUrb->fShortNotOk = !(pTail->Td.hwinfo & TD_HWINFO_ROUNDING);
pUrb->enmStatus = VUSBSTATUS_OK;
pUrb->pHci->cTds = cTds;
pUrb->pHci->EdAddr = EdAddr;
pUrb->pHci->fUnlinked = false;
pUrb->pHci->u32FrameNo = pThis->HcFmNumber;
/* Copy data and TD information. */
unsigned iTd = 0;
uint8_t *pb = &pUrb->abData[0];
for (struct OHCITDENTRY *pCur = &Head; pCur; pCur = pCur->pNext, iTd++)
{
/* data */
if ( cbTotal
&& enmDir != VUSBDIRECTION_IN
&& pCur->Buf.cVecs > 0)
{
ohciR3PhysRead(pThis, pCur->Buf.aVecs[0].Addr, pb, pCur->Buf.aVecs[0].cb);
if (pCur->Buf.cVecs > 1)
ohciR3PhysRead(pThis, pCur->Buf.aVecs[1].Addr, pb + pCur->Buf.aVecs[0].cb, pCur->Buf.aVecs[1].cb);
}
pb += pCur->Buf.cbTotal;
/* TD info */
pUrb->paTds[iTd].TdAddr = pCur->TdAddr;
AssertCompile(sizeof(pUrb->paTds[iTd].TdCopy) >= sizeof(pCur->Td));
memcpy(pUrb->paTds[iTd].TdCopy, &pCur->Td, sizeof(pCur->Td));
}
/*
* Submit the URB.
*/
ohciR3InFlightAddUrb(pThis, pUrb);
Log(("%s: ohciR3ServiceTdMultiple: submitting cbData=%#x EdAddr=%#010x cTds=%d TdAddr0=%#010x\n",
pUrb->pszDesc, pUrb->cbData, EdAddr, cTds, TdAddr));
ohciR3Unlock(pThis);
int rc = VUSBIRhSubmitUrb(pThis->RootHub.pIRhConn, pUrb, &pThis->RootHub.Led);
ohciR3Lock(pThis);
if (RT_SUCCESS(rc))
return true;
/* Failure cleanup. Can happen if we're still resetting the device or out of resources. */
Log(("ohciR3ServiceTdMultiple: failed submitting pUrb=%p cbData=%#x EdAddr=%#010x cTds=%d TdAddr0=%#010x - rc=%Rrc\n",
pUrb, cbTotal, EdAddr, cTds, TdAddr, rc));
for (struct OHCITDENTRY *pCur = &Head; pCur; pCur = pCur->pNext, iTd++)
ohciR3InFlightRemove(pThis, pCur->TdAddr);
return false;
}
/**
* Service the head TD of an endpoint.
*/
static bool ohciR3ServiceHeadTdMultiple(POHCI pThis, VUSBXFERTYPE enmType, PCOHCIED pEd, uint32_t EdAddr, const char *pszListName)
{
/*
* First, check that it's not already in-flight.
*/
uint32_t TdAddr = pEd->HeadP & ED_PTR_MASK;
if (ohciR3IsTdInFlight(pThis, TdAddr))
return false;
# if defined(VBOX_STRICT) || defined(LOG_ENABLED)
ohciR3InDoneQueueCheck(pThis, TdAddr);
# endif
return ohciR3ServiceTdMultiple(pThis, enmType, pEd, EdAddr, TdAddr, &TdAddr, pszListName);
}
/**
* A worker for ohciR3ServiceIsochronousEndpoint which unlinks a ITD
* that belongs to the past.
*/
static bool ohciR3ServiceIsochronousTdUnlink(POHCI pThis, POHCIITD pITd, uint32_t ITdAddr, uint32_t ITdAddrPrev,
PVUSBURB pUrb, POHCIED pEd, uint32_t EdAddr)
{
LogFlow(("%s%sohciR3ServiceIsochronousTdUnlink: Unlinking ITD: ITdAddr=%#010x EdAddr=%#010x ITdAddrPrev=%#010x\n",
pUrb ? pUrb->pszDesc : "", pUrb ? ": " : "", ITdAddr, EdAddr, ITdAddrPrev));
/*
* Do the unlinking.
*/
const uint32_t ITdAddrNext = pITd->NextTD & ED_PTR_MASK;
if (ITdAddrPrev)
{
/* Get validate the previous TD */
int iInFlightPrev = ohciR3InFlightFind(pThis, ITdAddrPrev);
AssertMsgReturn(iInFlightPrev >= 0, ("ITdAddr=%#RX32\n", ITdAddrPrev), false);
PVUSBURB pUrbPrev = pThis->aInFlight[iInFlightPrev].pUrb;
if (ohciR3HasUrbBeenCanceled(pThis, pUrbPrev, pEd)) /* ensures the copy is correct. */
return false;
/* Update the copy and write it back. */
POHCIITD pITdPrev = ((POHCIITD)pUrbPrev->paTds[0].TdCopy);
pITdPrev->NextTD = (pITdPrev->NextTD & ~ED_PTR_MASK) | ITdAddrNext;
ohciR3WriteITd(pThis, ITdAddrPrev, pITdPrev, "ohciR3ServiceIsochronousEndpoint");
}
else
{
/* It's the head node. update the copy from the caller and write it back. */
pEd->HeadP = (pEd->HeadP & ~ED_PTR_MASK) | ITdAddrNext;
ohciR3WriteEd(pThis, EdAddr, pEd);
}
/*
* If it's in flight, just mark the URB as unlinked (there is only one ITD per URB atm).
* Otherwise, retire it to the done queue with an error and cause a done line interrupt (?).
*/
if (pUrb)
{
pUrb->pHci->fUnlinked = true;
if (ohciR3HasUrbBeenCanceled(pThis, pUrb, pEd)) /* ensures the copy is correct (paranoia). */
return false;
POHCIITD pITdCopy = ((POHCIITD)pUrb->paTds[0].TdCopy);
pITd->NextTD = pITdCopy->NextTD &= ~ED_PTR_MASK;
}
else
{
pITd->HwInfo &= ~ITD_HWINFO_CC;
pITd->HwInfo |= OHCI_CC_DATA_OVERRUN;
pITd->NextTD = pThis->done;
pThis->done = ITdAddr;
pThis->dqic = 0;
}
ohciR3WriteITd(pThis, ITdAddr, pITd, "ohciR3ServiceIsochronousTdUnlink");
return true;
}
/**
* A worker for ohciR3ServiceIsochronousEndpoint which submits the specified
* TD.
*
* @returns true on success.
* @returns false on failure to submit.
* @param pThis The OHCI controller instance data.
* @param pITd The transfer descriptor to service.
* @param ITdAddr The address of the transfer descriptor in gues memory.
* @param R The start packet (frame) relative to the start of frame in HwInfo.
* @param pEd The OHCI endpoint descriptor.
* @param EdAddr The endpoint descriptor address in guest memory.
*/
static bool ohciR3ServiceIsochronousTd(POHCI pThis, POHCIITD pITd, uint32_t ITdAddr, const unsigned R, PCOHCIED pEd, uint32_t EdAddr)
{
/*
* Determine the endpoint direction.
*/
VUSBDIRECTION enmDir;
switch (pEd->hwinfo & ED_HWINFO_DIR)
{
case ED_HWINFO_OUT: enmDir = VUSBDIRECTION_OUT; break;
case ED_HWINFO_IN: enmDir = VUSBDIRECTION_IN; break;
default:
Log(("ohciR3ServiceIsochronousTd: Invalid direction!!!! Ed.hwdinfo=%#x\n", pEd->hwinfo));
/* Should probably raise an unrecoverable HC error here */
return false;
}
/*
* Extract the packet sizes and calc the total URB size.
*/
struct
{
uint16_t cb;
uint16_t off;
} aPkts[ITD_NUM_PSW];
/* first entry (R) */
uint32_t cbTotal = 0;
if (((uint32_t)pITd->aPSW[R] >> ITD_PSW_CC_SHIFT) < (OHCI_CC_NOT_ACCESSED_0 >> TD_HWINFO_CC_SHIFT))
Log(("ITdAddr=%RX32 PSW%d.CC=%#x < 'Not Accessed'!\n", ITdAddr, R, pITd->aPSW[R] >> ITD_PSW_CC_SHIFT)); /* => Unrecoverable Error*/
uint16_t offPrev = aPkts[0].off = (pITd->aPSW[R] & ITD_PSW_OFFSET);
/* R+1..cFrames */
const unsigned cFrames = ((pITd->HwInfo & ITD_HWINFO_FC) >> ITD_HWINFO_FC_SHIFT) + 1;
for (unsigned iR = R + 1; iR < cFrames; iR++)
{
const uint16_t PSW = pITd->aPSW[iR];
const uint16_t off = aPkts[iR - R].off = (PSW & ITD_PSW_OFFSET);
cbTotal += aPkts[iR - R - 1].cb = off - offPrev;
if (off < offPrev)
Log(("ITdAddr=%RX32 PSW%d.offset=%#x < offPrev=%#x!\n", ITdAddr, iR, off, offPrev)); /* => Unrecoverable Error*/
if (((uint32_t)PSW >> ITD_PSW_CC_SHIFT) < (OHCI_CC_NOT_ACCESSED_0 >> TD_HWINFO_CC_SHIFT))
Log(("ITdAddr=%RX32 PSW%d.CC=%#x < 'Not Accessed'!\n", ITdAddr, iR, PSW >> ITD_PSW_CC_SHIFT)); /* => Unrecoverable Error*/
offPrev = off;
}
/* calc offEnd and figure out the size of the last packet. */
const uint32_t offEnd = (pITd->BE & 0xfff)
+ (((pITd->BE & ITD_BP0_MASK) != (pITd->BP0 & ITD_BP0_MASK)) << 12)
+ 1 /* BE is inclusive */;
if (offEnd < offPrev)
Log(("ITdAddr=%RX32 offEnd=%#x < offPrev=%#x!\n", ITdAddr, offEnd, offPrev)); /* => Unrecoverable Error*/
cbTotal += aPkts[cFrames - 1 - R].cb = offEnd - offPrev;
Assert(cbTotal <= 0x2000);
pThis->fIdle = false; /* Mark as active */
/*
* Allocate and initialize a new URB.
*/
PVUSBURB pUrb = VUSBIRhNewUrb(pThis->RootHub.pIRhConn, pEd->hwinfo & ED_HWINFO_FUNCTION, NULL,
VUSBXFERTYPE_ISOC, enmDir, cbTotal, 1, NULL);
if (!pUrb)
/* retry later... */
return false;
pUrb->EndPt = (pEd->hwinfo & ED_HWINFO_ENDPOINT) >> ED_HWINFO_ENDPOINT_SHIFT;
pUrb->fShortNotOk = false;
pUrb->enmStatus = VUSBSTATUS_OK;
pUrb->pHci->EdAddr = EdAddr;
pUrb->pHci->cTds = 1;
pUrb->pHci->fUnlinked = false;
pUrb->pHci->u32FrameNo = pThis->HcFmNumber;
pUrb->paTds[0].TdAddr = ITdAddr;
AssertCompile(sizeof(pUrb->paTds[0].TdCopy) >= sizeof(*pITd));
memcpy(pUrb->paTds[0].TdCopy, pITd, sizeof(*pITd));
# if 0 /* color the data */
memset(pUrb->abData, 0xfe, cbTotal);
# endif
/* copy the data */
if ( cbTotal
&& enmDir != VUSBDIRECTION_IN)
{
const uint32_t off0 = pITd->aPSW[R] & ITD_PSW_OFFSET;
if (off0 < 0x1000)
{
if (offEnd > 0x1000)
{
/* both pages. */
const unsigned cb0 = 0x1000 - off0;
ohciR3PhysRead(pThis, (pITd->BP0 & ITD_BP0_MASK) + off0, &pUrb->abData[0], cb0);
ohciR3PhysRead(pThis, pITd->BE & ITD_BP0_MASK, &pUrb->abData[cb0], offEnd & 0xfff);
}
else /* a portion of the 1st page. */
ohciR3PhysRead(pThis, (pITd->BP0 & ITD_BP0_MASK) + off0, pUrb->abData, offEnd - off0);
}
else /* a portion of the 2nd page. */
ohciR3PhysRead(pThis, (pITd->BE & UINT32_C(0xfffff000)) + (off0 & 0xfff), pUrb->abData, cbTotal);
}
/* setup the packets */
pUrb->cIsocPkts = cFrames - R;
unsigned off = 0;
for (unsigned i = 0; i < pUrb->cIsocPkts; i++)
{
pUrb->aIsocPkts[i].enmStatus = VUSBSTATUS_NOT_ACCESSED;
pUrb->aIsocPkts[i].off = off;
off += pUrb->aIsocPkts[i].cb = aPkts[i].cb;
}
Assert(off == cbTotal);
/*
* Submit the URB.
*/
ohciR3InFlightAddUrb(pThis, pUrb);
Log(("%s: ohciR3ServiceIsochronousTd: submitting cbData=%#x cIsocPkts=%d EdAddr=%#010x TdAddr=%#010x SF=%#x (%#x)\n",
pUrb->pszDesc, pUrb->cbData, pUrb->cIsocPkts, EdAddr, ITdAddr, pITd->HwInfo & ITD_HWINFO_SF, pThis->HcFmNumber));
ohciR3Unlock(pThis);
int rc = VUSBIRhSubmitUrb(pThis->RootHub.pIRhConn, pUrb, &pThis->RootHub.Led);
ohciR3Lock(pThis);
if (RT_SUCCESS(rc))
return true;
/* Failure cleanup. Can happen if we're still resetting the device or out of resources. */
Log(("ohciR3ServiceIsochronousTd: failed submitting pUrb=%p cbData=%#x EdAddr=%#010x cTds=%d ITdAddr0=%#010x - rc=%Rrc\n",
pUrb, cbTotal, EdAddr, 1, ITdAddr, rc));
ohciR3InFlightRemove(pThis, ITdAddr);
return false;
}
/**
* Service an isochronous endpoint.
*/
static void ohciR3ServiceIsochronousEndpoint(POHCI pThis, POHCIED pEd, uint32_t EdAddr)
{
/*
* We currently process this as if the guest follows the interrupt end point chaining
* hierarchy described in the documenation. This means that for an isochronous endpoint
* with a 1 ms interval we expect to find in-flight TDs at the head of the list. We will
* skip over all in-flight TDs which timeframe has been exceed. Those which aren't in
* flight but which are too late will be retired (possibly out of order, but, we don't
* care right now).
*
* When we reach a TD which still has a buffer which is due for take off, we will
* stop iterating TDs. If it's in-flight, there isn't anything to be done. Otherwise
* we will push it onto the runway for immediate take off. In this process we
* might have to complete buffers which didn't make it on time, something which
* complicates the kind of status info we need to keep around for the TD.
*
* Note: We're currently not making any attempt at reassembling ITDs into URBs.
* However, this will become necessary because of EMT scheduling and guest
* like linux using one TD for each frame (simple but inefficient for us).
*/
OHCIITD ITd;
uint32_t ITdAddr = pEd->HeadP & ED_PTR_MASK;
uint32_t ITdAddrPrev = 0;
uint32_t u32NextFrame = UINT32_MAX;
const uint16_t u16CurFrame = pThis->HcFmNumber;
for (;;)
{
/* check for end-of-chain. */
if ( ITdAddr == (pEd->TailP & ED_PTR_MASK)
|| !ITdAddr)
break;
/*
* If isochronous endpoints are around, don't slow down the timer. Getting the timing right
* is difficult enough as it is.
*/
pThis->fIdle = false;
/*
* Read the current ITD and check what we're supposed to do about it.
*/
ohciR3ReadITd(pThis, ITdAddr, &ITd);
const uint32_t ITdAddrNext = ITd.NextTD & ED_PTR_MASK;
const int16_t R = u16CurFrame - (uint16_t)(ITd.HwInfo & ITD_HWINFO_SF); /* 4.3.2.3 */
const int16_t cFrames = ((ITd.HwInfo & ITD_HWINFO_FC) >> ITD_HWINFO_FC_SHIFT) + 1;
if (R < cFrames)
{
/*
* It's inside the current or a future launch window.
*
* We will try maximize the TD in flight here to deal with EMT scheduling
* issues and similar stuff which will screw up the time. So, we will only
* stop submitting TD when we reach a gap (in time) or end of the list.
*/
if ( R < 0 /* (a future frame) */
&& (uint16_t)u32NextFrame != (uint16_t)(ITd.HwInfo & ITD_HWINFO_SF))
break;
if (ohciR3InFlightFind(pThis, ITdAddr) < 0)
if (!ohciR3ServiceIsochronousTd(pThis, &ITd, ITdAddr, R < 0 ? 0 : R, pEd, EdAddr))
break;
ITdAddrPrev = ITdAddr;
}
else
{
# if 1
/*
* Ok, the launch window for this TD has passed.
* If it's not in flight it should be retired with a DataOverrun status (TD).
*
* Don't remove in-flight TDs before they complete.
* Windows will, upon the completion of another ITD it seems, check for if
* any other TDs has been unlinked. If we unlink them before they really
* complete all the packet status codes will be NotAccessed and Windows
* will fail the URB with status USBD_STATUS_ISOCH_REQUEST_FAILED.
*
* I don't know if unlinking TDs out of order could cause similar problems,
* time will show.
*/
int iInFlight = ohciR3InFlightFind(pThis, ITdAddr);
if (iInFlight >= 0)
ITdAddrPrev = ITdAddr;
else if (!ohciR3ServiceIsochronousTdUnlink(pThis, &ITd, ITdAddr, ITdAddrPrev,
NULL, pEd, EdAddr))
{
Log(("ohciR3ServiceIsochronousEndpoint: Failed unlinking old ITD.\n"));
break;
}
# else /* BAD IDEA: */
/*
* Ok, the launch window for this TD has passed.
* If it's not in flight it should be retired with a DataOverrun status (TD).
*
* If it's in flight we will try unlink it from the list prematurely to
* help the guest to move on and shorten the list we have to walk. We currently
* are successful with the first URB but then it goes too slowly...
*/
int iInFlight = ohciR3InFlightFind(pThis, ITdAddr);
if (!ohciR3ServiceIsochronousTdUnlink(pThis, &ITd, ITdAddr, ITdAddrPrev,
iInFlight < 0 ? NULL : pThis->aInFlight[iInFlight].pUrb,
pEd, EdAddr))
{
Log(("ohciR3ServiceIsochronousEndpoint: Failed unlinking old ITD.\n"));
break;
}
# endif
}
/* advance to the next ITD */
ITdAddr = ITdAddrNext;
u32NextFrame = (ITd.HwInfo & ITD_HWINFO_SF) + cFrames;
}
}
/**
* Checks if a endpoints has TDs queued and is ready to have them processed.
*
* @returns true if it's ok to process TDs.
* @param pEd The endpoint data.
*/
DECLINLINE(bool) ohciR3IsEdReady(PCOHCIED pEd)
{
return (pEd->HeadP & ED_PTR_MASK) != (pEd->TailP & ED_PTR_MASK)
&& !(pEd->HeadP & ED_HEAD_HALTED)
&& !(pEd->hwinfo & ED_HWINFO_SKIP);
}
/**
* Checks if an endpoint has TDs queued (not necessarily ready to have them processed).
*
* @returns true if endpoint may have TDs queued.
* @param pEd The endpoint data.
*/
DECLINLINE(bool) ohciR3IsEdPresent(PCOHCIED pEd)
{
return (pEd->HeadP & ED_PTR_MASK) != (pEd->TailP & ED_PTR_MASK)
&& !(pEd->HeadP & ED_HEAD_HALTED);
}
/**
* Services the bulk list.
*
* On the bulk list we must reassemble URBs from multiple TDs using heuristics
* derived from USB tracing done in the guests and guest source code (when available).
*/
static void ohciR3ServiceBulkList(POHCI pThis)
{
# ifdef LOG_ENABLED
if (g_fLogBulkEPs)
ohciR3DumpEdList(pThis, pThis->bulk_head, "Bulk before", true);
if (pThis->bulk_cur)
Log(("ohciR3ServiceBulkList: bulk_cur=%#010x before listprocessing!!! HCD have positioned us!!!\n", pThis->bulk_cur));
# endif
/*
* ", HC will start processing the Bulk list and will set BF [BulkListFilled] to 0"
* - We've simplified and are always starting at the head of the list and working
* our way thru to the end each time.
*/
pThis->status &= ~OHCI_STATUS_BLF;
pThis->fBulkNeedsCleaning = false;
pThis->bulk_cur = 0;
uint32_t EdAddr = pThis->bulk_head;
while (EdAddr)
{
OHCIED Ed;
ohciR3ReadEd(pThis, EdAddr, &Ed);
Assert(!(Ed.hwinfo & ED_HWINFO_ISO)); /* the guest is screwing us */
if (ohciR3IsEdReady(&Ed))
{
pThis->status |= OHCI_STATUS_BLF;
pThis->fBulkNeedsCleaning = true;
# if 1
/*
* After we figured out that all the TDs submitted for dealing with MSD
* read/write data really makes up on single URB, and that we must
* reassemble these TDs into an URB before submitting it, there is no
* longer any need for servicing anything other than the head *URB*
* on a bulk endpoint.
*/
ohciR3ServiceHeadTdMultiple(pThis, VUSBXFERTYPE_BULK, &Ed, EdAddr, "Bulk");
# else
/*
* This alternative code was used before we started reassembling URBs from
* multiple TDs. We keep it handy for debugging.
*/
uint32_t TdAddr = Ed.HeadP & ED_PTR_MASK;
if (!ohciR3IsTdInFlight(pThis, TdAddr))
{
do
{
if (!ohciR3ServiceTdMultiple(pThis, VUSBXFERTYPE_BULK, &Ed, EdAddr, TdAddr, &TdAddr, "Bulk"))
{
LogFlow(("ohciR3ServiceBulkList: ohciR3ServiceTdMultiple -> false\n"));
break;
}
if ( (TdAddr & ED_PTR_MASK) == (Ed.TailP & ED_PTR_MASK)
|| !TdAddr /* paranoia */)
{
LogFlow(("ohciR3ServiceBulkList: TdAddr=%#010RX32 Ed.TailP=%#010RX32\n", TdAddr, Ed.TailP));
break;
}
ohciR3ReadEd(pThis, EdAddr, &Ed); /* It might have been updated on URB completion. */
} while (ohciR3IsEdReady(&Ed));
}
# endif
}
else
{
if (Ed.hwinfo & ED_HWINFO_SKIP)
{
LogFlow(("ohciR3ServiceBulkList: Ed=%#010RX32 Ed.TailP=%#010RX32 SKIP\n", EdAddr, Ed.TailP));
/* If the ED is in 'skip' state, no transactions on it are allowed and we must
* cancel outstanding URBs, if any.
*/
uint32_t TdAddr = Ed.HeadP & ED_PTR_MASK;
PVUSBURB pUrb = ohciR3TdInFlightUrb(pThis, TdAddr);
if (pUrb)
pThis->RootHub.pIRhConn->pfnCancelUrbsEp(pThis->RootHub.pIRhConn, pUrb);
}
}
/* next end point */
EdAddr = Ed.NextED & ED_PTR_MASK;
}
# ifdef LOG_ENABLED
if (g_fLogBulkEPs)
ohciR3DumpEdList(pThis, pThis->bulk_head, "Bulk after ", true);
# endif
}
/**
* Abort outstanding transfers on the bulk list.
*
* If the guest disabled bulk list processing, we must abort any outstanding transfers
* (that is, cancel in-flight URBs associated with the list). This is required because
* there may be outstanding read URBs that will never get a response from the device
* and would block further communication.
*/
static void ohciR3UndoBulkList(POHCI pThis)
{
# ifdef LOG_ENABLED
if (g_fLogBulkEPs)
ohciR3DumpEdList(pThis, pThis->bulk_head, "Bulk before", true);
if (pThis->bulk_cur)
Log(("ohciR3UndoBulkList: bulk_cur=%#010x before list processing!!! HCD has positioned us!!!\n", pThis->bulk_cur));
# endif
/* This flag follows OHCI_STATUS_BLF, but BLF doesn't change when list processing is disabled. */
pThis->fBulkNeedsCleaning = false;
uint32_t EdAddr = pThis->bulk_head;
while (EdAddr)
{
OHCIED Ed;
ohciR3ReadEd(pThis, EdAddr, &Ed);
Assert(!(Ed.hwinfo & ED_HWINFO_ISO)); /* the guest is screwing us */
if (ohciR3IsEdPresent(&Ed))
{
uint32_t TdAddr = Ed.HeadP & ED_PTR_MASK;
if (ohciR3IsTdInFlight(pThis, TdAddr))
{
LogFlow(("ohciR3UndoBulkList: Ed=%#010RX32 Ed.TailP=%#010RX32 UNDO\n", EdAddr, Ed.TailP));
PVUSBURB pUrb = ohciR3TdInFlightUrb(pThis, TdAddr);
if (pUrb)
pThis->RootHub.pIRhConn->pfnCancelUrbsEp(pThis->RootHub.pIRhConn, pUrb);
}
}
/* next endpoint */
EdAddr = Ed.NextED & ED_PTR_MASK;
}
}
/**
* Services the control list.
*
* The control list has complex URB assembling, but that's taken
* care of at VUSB level (unlike the other transfer types).
*/
static void ohciR3ServiceCtrlList(POHCI pThis)
{
# ifdef LOG_ENABLED
if (g_fLogControlEPs)
ohciR3DumpEdList(pThis, pThis->ctrl_head, "Ctrl before", true);
if (pThis->ctrl_cur)
Log(("ohciR3ServiceCtrlList: ctrl_cur=%010x before list processing!!! HCD have positioned us!!!\n", pThis->ctrl_cur));
# endif
/*
* ", HC will start processing the list and will set ControlListFilled to 0"
* - We've simplified and are always starting at the head of the list and working
* our way thru to the end each time.
*/
pThis->status &= ~OHCI_STATUS_CLF;
pThis->ctrl_cur = 0;
uint32_t EdAddr = pThis->ctrl_head;
while (EdAddr)
{
OHCIED Ed;
ohciR3ReadEd(pThis, EdAddr, &Ed);
Assert(!(Ed.hwinfo & ED_HWINFO_ISO)); /* the guest is screwing us */
if (ohciR3IsEdReady(&Ed))
{
# if 1
/*
* Control TDs depends on order and stage. Only one can be in-flight
* at any given time. OTOH, some stages are completed immediately,
* so we process the list until we've got a head which is in-flight
* or reach the end of the list.
*/
do
{
if ( !ohciR3ServiceHeadTd(pThis, VUSBXFERTYPE_CTRL, &Ed, EdAddr, "Control")
|| ohciR3IsTdInFlight(pThis, Ed.HeadP & ED_PTR_MASK))
{
pThis->status |= OHCI_STATUS_CLF;
break;
}
ohciR3ReadEd(pThis, EdAddr, &Ed); /* It might have been updated on URB completion. */
} while (ohciR3IsEdReady(&Ed));
# else
/* Simplistic, for debugging. */
ohciR3ServiceHeadTd(pThis, VUSBXFERTYPE_CTRL, &Ed, EdAddr, "Control");
pThis->status |= OHCI_STATUS_CLF;
# endif
}
/* next end point */
EdAddr = Ed.NextED & ED_PTR_MASK;
}
# ifdef LOG_ENABLED
if (g_fLogControlEPs)
ohciR3DumpEdList(pThis, pThis->ctrl_head, "Ctrl after ", true);
# endif
}
/**
* Services the periodic list.
*
* On the interrupt portion of the periodic list we must reassemble URBs from multiple
* TDs using heuristics derived from USB tracing done in the guests and guest source
* code (when available).
*/
static void ohciR3ServicePeriodicList(POHCI pThis)
{
/*
* Read the list head from the HCCA.
*/
const unsigned iList = pThis->HcFmNumber % OHCI_HCCA_NUM_INTR;
uint32_t EdAddr;
ohciR3GetDWords(pThis, pThis->hcca + iList * sizeof(EdAddr), &EdAddr, 1);
# ifdef LOG_ENABLED
const uint32_t EdAddrHead = EdAddr;
if (g_fLogInterruptEPs)
{
char sz[48];
RTStrPrintf(sz, sizeof(sz), "Int%02x before", iList);
ohciR3DumpEdList(pThis, EdAddrHead, sz, true);
}
# endif
/*
* Iterate the endpoint list.
*/
while (EdAddr)
{
OHCIED Ed;
ohciR3ReadEd(pThis, EdAddr, &Ed);
if (ohciR3IsEdReady(&Ed))
{
/*
* "There is no separate head pointer of isochronous transfers. The first
* isochronous Endpoint Descriptor simply links to the last interrupt
* Endpoint Descriptor."
*/
if (!(Ed.hwinfo & ED_HWINFO_ISO))
{
/*
* Presently we will only process the head URB on an interrupt endpoint.
*/
ohciR3ServiceHeadTdMultiple(pThis, VUSBXFERTYPE_INTR, &Ed, EdAddr, "Periodic");
}
else if (pThis->ctl & OHCI_CTL_IE)
{
/*
* Presently only the head ITD.
*/
ohciR3ServiceIsochronousEndpoint(pThis, &Ed, EdAddr);
}
else
break;
}
else
{
if (Ed.hwinfo & ED_HWINFO_SKIP)
{
Log3(("ohciR3ServicePeriodicList: Ed=%#010RX32 Ed.TailP=%#010RX32 SKIP\n", EdAddr, Ed.TailP));
/* If the ED is in 'skip' state, no transactions on it are allowed and we must
* cancel outstanding URBs, if any.
*/
uint32_t TdAddr = Ed.HeadP & ED_PTR_MASK;
PVUSBURB pUrb = ohciR3TdInFlightUrb(pThis, TdAddr);
if (pUrb)
pThis->RootHub.pIRhConn->pfnCancelUrbsEp(pThis->RootHub.pIRhConn, pUrb);
}
}
/* next end point */
EdAddr = Ed.NextED & ED_PTR_MASK;
}
# ifdef LOG_ENABLED
if (g_fLogInterruptEPs)
{
char sz[48];
RTStrPrintf(sz, sizeof(sz), "Int%02x after ", iList);
ohciR3DumpEdList(pThis, EdAddrHead, sz, true);
}
# endif
}
/**
* Update the HCCA.
*
* @param pThis The OHCI instance data.
*/
static void ohciR3UpdateHCCA(POHCI pThis)
{
struct ohci_hcca hcca;
ohciR3PhysRead(pThis, pThis->hcca + OHCI_HCCA_OFS, &hcca, sizeof(hcca));
hcca.frame = RT_H2LE_U16((uint16_t)pThis->HcFmNumber);
hcca.pad = 0;
bool fWriteDoneHeadInterrupt = false;
if ( pThis->dqic == 0
&& (pThis->intr_status & OHCI_INTR_WRITE_DONE_HEAD) == 0)
{
uint32_t done = pThis->done;
if (pThis->intr_status & ~( OHCI_INTR_MASTER_INTERRUPT_ENABLED | OHCI_INTR_OWNERSHIP_CHANGE
| OHCI_INTR_WRITE_DONE_HEAD) )
done |= 0x1;
hcca.done = RT_H2LE_U32(done);
pThis->done = 0;
pThis->dqic = 0x7;
Log(("ohci: Writeback Done (%#010x) on frame %#x (age %#x)\n", hcca.done,
pThis->HcFmNumber, pThis->HcFmNumber - pThis->u32FmDoneQueueTail));
# ifdef LOG_ENABLED
ohciR3DumpTdQueue(pThis, hcca.done & ED_PTR_MASK, "DoneQueue");
# endif
Assert(RT_OFFSETOF(struct ohci_hcca, done) == 4);
# if defined(VBOX_STRICT) || defined(LOG_ENABLED)
ohciR3InDoneQueueZap(pThis);
# endif
fWriteDoneHeadInterrupt = true;
}
Log3(("ohci: Updating HCCA on frame %#x\n", pThis->HcFmNumber));
ohciR3PhysWrite(pThis, pThis->hcca + OHCI_HCCA_OFS, (uint8_t *)&hcca, sizeof(hcca));
if (fWriteDoneHeadInterrupt)
ohciR3SetInterrupt(pThis, OHCI_INTR_WRITE_DONE_HEAD);
}
/**
* Go over the in-flight URB list and cancel any URBs that are no longer in use.
* This occurs when the host removes EDs or TDs from the lists and we don't notice
* the sKip bit. Such URBs must be promptly canceled, otherwise there is a risk
* they might "steal" data destined for another URB.
*/
static void ohciR3CancelOrphanedURBs(POHCI pThis)
{
bool fValidHCCA = !( pThis->hcca >= OHCI_HCCA_MASK
|| pThis->hcca < ~OHCI_HCCA_MASK);
unsigned i, cLeft;
int j;
uint32_t EdAddr;
PVUSBURB pUrb;
/* If the HCCA is not currently valid, or there are no in-flight URBs,
* there's nothing to do.
*/
if (!fValidHCCA || !pThis->cInFlight)
return;
/* Initially mark all in-flight URBs as inactive. */
for (i = 0, cLeft = pThis->cInFlight; cLeft && i < RT_ELEMENTS(pThis->aInFlight); i++)
{
if (pThis->aInFlight[i].pUrb)
{
pThis->aInFlight[i].fInactive = true;
cLeft--;
}
}
Assert(cLeft == 0);
# ifdef VBOX_WITH_OHCI_PHYS_READ_CACHE
/* Get hcca data to minimize calls to ohciR3GetDWords/PDMDevHlpPhysRead. */
uint32_t au32HCCA[OHCI_HCCA_NUM_INTR];
ohciR3GetDWords(pThis, pThis->hcca, au32HCCA, OHCI_HCCA_NUM_INTR);
# endif
/* Go over all bulk/control/interrupt endpoint lists; any URB found in these lists
* is marked as active again.
*/
for (i = 0; i < OHCI_HCCA_NUM_INTR + 2; i++)
{
switch (i)
{
case OHCI_HCCA_NUM_INTR:
EdAddr = pThis->bulk_head;
break;
case OHCI_HCCA_NUM_INTR + 1:
EdAddr = pThis->ctrl_head;
break;
default:
# ifdef VBOX_WITH_OHCI_PHYS_READ_CACHE
EdAddr = au32HCCA[i];
# else
ohciR3GetDWords(pThis, pThis->hcca + i * sizeof(EdAddr), &EdAddr, 1);
# endif
break;
}
while (EdAddr)
{
OHCIED Ed;
OHCITD Td;
ohciR3ReadEd(pThis, EdAddr, &Ed);
uint32_t TdAddr = Ed.HeadP & ED_PTR_MASK;
uint32_t TailP = Ed.TailP & ED_PTR_MASK;
unsigned k = 0;
if ( !(Ed.hwinfo & ED_HWINFO_SKIP)
&& (TdAddr != TailP))
{
# ifdef VBOX_WITH_OHCI_PHYS_READ_CACHE
ohciR3PhysReadCacheClear(pThis->pCacheTD);
# endif
do
{
ohciR3ReadTd(pThis, TdAddr, &Td);
j = ohciR3InFlightFind(pThis, TdAddr);
if (j > -1)
pThis->aInFlight[j].fInactive = false;
TdAddr = Td.NextTD & ED_PTR_MASK;
/* See #8125.
* Sometimes the ED is changed by the guest between ohciR3ReadEd above and here.
* Then the code reads TD pointed by the new TailP, which is not allowed.
* Luckily Windows guests have Td.NextTD = 0 in the tail TD.
* Also having a real TD at 0 is very unlikely.
* So do not continue.
*/
if (TdAddr == 0)
break;
/* Failsafe for temporarily looped lists. */
if (++k == 128)
break;
} while (TdAddr != (Ed.TailP & ED_PTR_MASK));
}
EdAddr = Ed.NextED & ED_PTR_MASK;
}
}
/* In-flight URBs still marked as inactive are not used anymore and need
* to be canceled.
*/
for (i = 0, cLeft = pThis->cInFlight; cLeft && i < RT_ELEMENTS(pThis->aInFlight); i++)
{
if (pThis->aInFlight[i].pUrb)
{
cLeft--;
pUrb = pThis->aInFlight[i].pUrb;
if (pThis->aInFlight[i].fInactive
&& pUrb->enmState == VUSBURBSTATE_IN_FLIGHT
&& pUrb->enmType != VUSBXFERTYPE_CTRL)
pThis->RootHub.pIRhConn->pfnCancelUrbsEp(pThis->RootHub.pIRhConn, pUrb);
}
}
Assert(cLeft == 0);
}
/**
* Generate a Start-Of-Frame event, and set a timer for End-Of-Frame.
*/
static void ohciR3StartOfFrame(POHCI pThis)
{
# ifdef LOG_ENABLED
const uint32_t status_old = pThis->status;
# endif
/*
* Update HcFmRemaining.FRT and update start of frame time.
*/
pThis->frt = pThis->fit;
pThis->SofTime += pThis->cTicksPerFrame;
/*
* Check that the HCCA address isn't bogus. Linux 2.4.x is known to start
* the bus with a hcca of 0 to work around problem with a specific controller.
*/
bool fValidHCCA = !( pThis->hcca >= OHCI_HCCA_MASK
|| pThis->hcca < ~OHCI_HCCA_MASK);
# if 1
/*
* Update the HCCA.
* Should be done after SOF but before HC read first ED in this frame.
*/
if (fValidHCCA)
ohciR3UpdateHCCA(pThis);
# endif
/* "After writing to HCCA, HC will set SF in HcInterruptStatus" - guest isn't executing, so ignore the order! */
ohciR3SetInterrupt(pThis, OHCI_INTR_START_OF_FRAME);
if (pThis->fno)
{
ohciR3SetInterrupt(pThis, OHCI_INTR_FRAMENUMBER_OVERFLOW);
pThis->fno = 0;
}
/* If the HCCA address is invalid, we're quitting here to avoid doing something which cannot be reported to the HCD. */
if (!fValidHCCA)
{
Log(("ohciR3StartOfFrame: skipping hcca part because hcca=%RX32 (our 'valid' range: %RX32-%RX32)\n",
pThis->hcca, ~OHCI_HCCA_MASK, OHCI_HCCA_MASK));
return;
}
/*
* Periodic EPs.
*/
if (pThis->ctl & OHCI_CTL_PLE)
ohciR3ServicePeriodicList(pThis);
/*
* Control EPs.
*/
if ( (pThis->ctl & OHCI_CTL_CLE)
&& (pThis->status & OHCI_STATUS_CLF) )
ohciR3ServiceCtrlList(pThis);
/*
* Bulk EPs.
*/
if ( (pThis->ctl & OHCI_CTL_BLE)
&& (pThis->status & OHCI_STATUS_BLF))
ohciR3ServiceBulkList(pThis);
else if ((pThis->status & OHCI_STATUS_BLF)
&& pThis->fBulkNeedsCleaning)
ohciR3UndoBulkList(pThis); /* If list disabled but not empty, abort endpoints. */
# if 0
/*
* Update the HCCA after processing the lists and everything. A bit experimental.
*
* ASSUME the guest won't be very upset if a TD is completed, retired and handed
* back immediately. The idea is to be able to retire the data and/or status stages
* of a control transfer together with the setup stage, thus saving a frame. This
* behaviour is should be perfectly ok, since the setup (and maybe data) stages
* have already taken at least one frame to complete.
*
* But, when implementing the first synchronous virtual USB devices, we'll have to
* verify that the guest doesn't choke when having a TD returned in the same frame
* as it was submitted.
*/
ohciR3UpdateHCCA(pThis);
# endif
# ifdef LOG_ENABLED
if (pThis->status ^ status_old)
{
uint32_t val = pThis->status;
uint32_t chg = val ^ status_old; NOREF(chg);
Log2(("ohciR3StartOfFrame: HcCommandStatus=%#010x: %sHCR=%d %sCLF=%d %sBLF=%d %sOCR=%d %sSOC=%d\n",
val,
chg & RT_BIT(0) ? "*" : "", val & 1,
chg & RT_BIT(1) ? "*" : "", (val >> 1) & 1,
chg & RT_BIT(2) ? "*" : "", (val >> 2) & 1,
chg & RT_BIT(3) ? "*" : "", (val >> 3) & 1,
chg & (3<<16)? "*" : "", (val >> 16) & 3));
}
# endif
}
/**
* Updates the HcFmNumber and FNO registers.
*/
static void ohciR3BumpFrameNumber(POHCI pThis)
{
const uint16_t u16OldFmNumber = pThis->HcFmNumber++;
if ((u16OldFmNumber ^ pThis->HcFmNumber) & RT_BIT(15))
pThis->fno = 1;
}
/**
* Callback for periodic frame processing.
*/
static DECLCALLBACK(bool) ohciR3StartFrame(PVUSBIROOTHUBPORT pInterface, uint32_t u32FrameNo)
{
RT_NOREF(u32FrameNo);
POHCI pThis = VUSBIROOTHUBPORT_2_OHCI(pInterface);
ohciR3Lock(pThis);
/* Reset idle detection flag */
pThis->fIdle = true;
# ifdef VBOX_WITH_OHCI_PHYS_READ_STATS
physReadStatsReset(&g_PhysReadState);
# endif
/* Frame boundary, so do EOF stuff here. */
ohciR3BumpFrameNumber(pThis);
if ( (pThis->dqic != 0x7) && (pThis->dqic != 0))
pThis->dqic--;
/* Clean up any URBs that have been removed. */
ohciR3CancelOrphanedURBs(pThis);
/* Start the next frame. */
ohciR3StartOfFrame(pThis);
# ifdef VBOX_WITH_OHCI_PHYS_READ_STATS
physReadStatsPrint(&g_PhysReadState);
# endif
ohciR3Unlock(pThis);
return pThis->fIdle;
}
/** @interface_method_impl{VUSBIROOTHUBPORT,pfnFrameRateChanged} */
static DECLCALLBACK(void) ohciR3FrameRateChanged(PVUSBIROOTHUBPORT pInterface, uint32_t u32FrameRate)
{
POHCI pThis = VUSBIROOTHUBPORT_2_OHCI(pInterface);
Assert(u32FrameRate <= OHCI_DEFAULT_TIMER_FREQ);
pThis->cTicksPerFrame = pThis->u64TimerHz / u32FrameRate;
if (!pThis->cTicksPerFrame)
pThis->cTicksPerFrame = 1;
pThis->cTicksPerUsbTick = pThis->u64TimerHz >= VUSB_BUS_HZ ? pThis->u64TimerHz / VUSB_BUS_HZ : 1;
}
/**
* Do frame processing on frame boundary
*/
static DECLCALLBACK(void) ohciR3FrameBoundaryTimer(PPDMDEVINS pDevIns, PTMTIMER pTimer, void *pvUser)
{
RT_NOREF(pDevIns, pTimer, pvUser);
}
/**
* Start sending SOF tokens across the USB bus, lists are processed in
* next frame
*/
static void ohciR3BusStart(POHCI pThis)
{
VUSBIDevPowerOn(pThis->RootHub.pIDev);
pThis->dqic = 0x7;
Log(("ohci: %s: Bus started\n", pThis->PciDev.pszNameR3));
pThis->SofTime = PDMDevHlpTMTimeVirtGet(pThis->CTX_SUFF(pDevIns));
int rc = pThis->RootHub.pIRhConn->pfnSetPeriodicFrameProcessing(pThis->RootHub.pIRhConn, OHCI_DEFAULT_TIMER_FREQ);
AssertRC(rc);
}
/**
* Stop sending SOF tokens on the bus
*/
static void ohciR3BusStop(POHCI pThis)
{
int rc = pThis->RootHub.pIRhConn->pfnSetPeriodicFrameProcessing(pThis->RootHub.pIRhConn, 0);
AssertRC(rc);
VUSBIDevPowerOff(pThis->RootHub.pIDev);
}
/**
* Move in to resume state
*/
static void ohciR3BusResume(POHCI pThis, bool fHardware)
{
pThis->ctl &= ~OHCI_CTL_HCFS;
pThis->ctl |= OHCI_USB_RESUME;
LogFunc(("fHardware=%RTbool RWE=%s\n",
fHardware, (pThis->ctl & OHCI_CTL_RWE) ? "on" : "off"));
if (fHardware && (pThis->ctl & OHCI_CTL_RWE))
ohciR3SetInterrupt(pThis, OHCI_INTR_RESUME_DETECT);
ohciR3BusStart(pThis);
}
/* Power a port up or down */
static void ohciR3RhPortPower(POHCIROOTHUB pRh, unsigned iPort, bool fPowerUp)
{
POHCIHUBPORT pPort = &pRh->aPorts[iPort];
bool fOldPPS = !!(pPort->fReg & OHCI_PORT_PPS);
LogFlowFunc(("iPort=%u fPowerUp=%RTbool\n", iPort, fPowerUp));
if (fPowerUp)
{
/* power up */
if (pPort->pDev)
pPort->fReg |= OHCI_PORT_CCS;
if (pPort->fReg & OHCI_PORT_CCS)
pPort->fReg |= OHCI_PORT_PPS;
if (pPort->pDev && !fOldPPS)
VUSBIDevPowerOn(pPort->pDev);
}
else
{
/* power down */
pPort->fReg &= ~(OHCI_PORT_PPS | OHCI_PORT_CCS | OHCI_PORT_PSS | OHCI_PORT_PRS);
if (pPort->pDev && fOldPPS)
VUSBIDevPowerOff(pPort->pDev);
}
}
#endif /* IN_RING3 */
/**
* Read the HcRevision register.
*/
static int HcRevision_r(PCOHCI pThis, uint32_t iReg, uint32_t *pu32Value)
{
RT_NOREF2(pThis, iReg);
Log2(("HcRevision_r() -> 0x10\n"));
*pu32Value = 0x10; /* OHCI revision 1.0, no emulation. */
return VINF_SUCCESS;
}
/**
* Write to the HcRevision register.
*/
static int HcRevision_w(POHCI pThis, uint32_t iReg, uint32_t u32Value)
{
RT_NOREF3(pThis, iReg, u32Value);
Log2(("HcRevision_w(%#010x) - denied\n", u32Value));
AssertMsgFailed(("Invalid operation!!! u32Value=%#010x\n", u32Value));
return VINF_SUCCESS;
}
/**
* Read the HcControl register.
*/
static int HcControl_r(PCOHCI pThis, uint32_t iReg, uint32_t *pu32Value)
{
RT_NOREF1(iReg);
uint32_t ctl = pThis->ctl;
Log2(("HcControl_r -> %#010x - CBSR=%d PLE=%d IE=%d CLE=%d BLE=%d HCFS=%#x IR=%d RWC=%d RWE=%d\n",
ctl, ctl & 3, (ctl >> 2) & 1, (ctl >> 3) & 1, (ctl >> 4) & 1, (ctl >> 5) & 1, (ctl >> 6) & 3, (ctl >> 8) & 1,
(ctl >> 9) & 1, (ctl >> 10) & 1));
*pu32Value = ctl;
return VINF_SUCCESS;
}
/**
* Write the HcControl register.
*/
static int HcControl_w(POHCI pThis, uint32_t iReg, uint32_t val)
{
RT_NOREF1(iReg);
/* log it. */
uint32_t chg = pThis->ctl ^ val; NOREF(chg);
Log2(("HcControl_w(%#010x) => %sCBSR=%d %sPLE=%d %sIE=%d %sCLE=%d %sBLE=%d %sHCFS=%#x %sIR=%d %sRWC=%d %sRWE=%d\n",
val,
chg & 3 ? "*" : "", val & 3,
chg & RT_BIT(2) ? "*" : "", (val >> 2) & 1,
chg & RT_BIT(3) ? "*" : "", (val >> 3) & 1,
chg & RT_BIT(4) ? "*" : "", (val >> 4) & 1,
chg & RT_BIT(5) ? "*" : "", (val >> 5) & 1,
chg & (3 << 6)? "*" : "", (val >> 6) & 3,
chg & RT_BIT(8) ? "*" : "", (val >> 8) & 1,
chg & RT_BIT(9) ? "*" : "", (val >> 9) & 1,
chg & RT_BIT(10) ? "*" : "", (val >> 10) & 1));
if (val & ~0x07ff)
Log2(("Unknown bits %#x are set!!!\n", val & ~0x07ff));
/* see what changed and take action on that. */
uint32_t old_state = pThis->ctl & OHCI_CTL_HCFS;
uint32_t new_state = val & OHCI_CTL_HCFS;
#ifdef IN_RING3
pThis->ctl = val;
if (new_state != old_state)
{
switch (new_state)
{
case OHCI_USB_OPERATIONAL:
LogRel(("OHCI: USB Operational\n"));
ohciR3BusStart(pThis);
break;
case OHCI_USB_SUSPEND:
ohciR3BusStop(pThis);
LogRel(("OHCI: USB Suspended\n"));
break;
case OHCI_USB_RESUME:
LogRel(("OHCI: USB Resume\n"));
ohciR3BusResume(pThis, false /* not hardware */);
break;
case OHCI_USB_RESET:
{
LogRel(("OHCI: USB Reset\n"));
ohciR3BusStop(pThis);
/** @todo This should probably do a real reset, but we don't implement
* that correctly in the roothub reset callback yet. check it's
* comments and argument for more details. */
VUSBIDevReset(pThis->RootHub.pIDev, false /* don't do a real reset */, NULL, NULL, NULL);
break;
}
}
}
#else /* !IN_RING3 */
if ( new_state != old_state )
{
Log2(("HcControl_w: state changed -> VINF_IOM_R3_MMIO_WRITE\n"));
return VINF_IOM_R3_MMIO_WRITE;
}
pThis->ctl = val;
#endif /* !IN_RING3 */
return VINF_SUCCESS;
}
/**
* Read the HcCommandStatus register.
*/
static int HcCommandStatus_r(PCOHCI pThis, uint32_t iReg, uint32_t *pu32Value)
{
uint32_t status = pThis->status;
Log2(("HcCommandStatus_r() -> %#010x - HCR=%d CLF=%d BLF=%d OCR=%d SOC=%d\n",
status, status & 1, (status >> 1) & 1, (status >> 2) & 1, (status >> 3) & 1, (status >> 16) & 3));
*pu32Value = status;
RT_NOREF1(iReg);
return VINF_SUCCESS;
}
/**
* Write to the HcCommandStatus register.
*/
static int HcCommandStatus_w(POHCI pThis, uint32_t iReg, uint32_t val)
{
RT_NOREF1(iReg);
/* log */
uint32_t chg = pThis->status ^ val; NOREF(chg);
Log2(("HcCommandStatus_w(%#010x) => %sHCR=%d %sCLF=%d %sBLF=%d %sOCR=%d %sSOC=%d\n",
val,
chg & RT_BIT(0) ? "*" : "", val & 1,
chg & RT_BIT(1) ? "*" : "", (val >> 1) & 1,
chg & RT_BIT(2) ? "*" : "", (val >> 2) & 1,
chg & RT_BIT(3) ? "*" : "", (val >> 3) & 1,
chg & (3<<16)? "!!!":"", (pThis->status >> 16) & 3));
if (val & ~0x0003000f)
Log2(("Unknown bits %#x are set!!!\n", val & ~0x0003000f));
/* SOC is read-only */
val = (val & ~OHCI_STATUS_SOC);
#ifdef IN_RING3
/* "bits written as '0' remain unchanged in the register" */
pThis->status |= val;
if (pThis->status & OHCI_STATUS_HCR)
{
LogRel(("OHCI: Software reset\n"));
ohciR3DoReset(pThis, OHCI_USB_SUSPEND, false /* N/A */);
}
#else
if ((pThis->status | val) & OHCI_STATUS_HCR)
{
LogFlow(("HcCommandStatus_w: reset -> VINF_IOM_R3_MMIO_WRITE\n"));
return VINF_IOM_R3_MMIO_WRITE;
}
pThis->status |= val;
#endif
return VINF_SUCCESS;
}
/**
* Read the HcInterruptStatus register.
*/
static int HcInterruptStatus_r(PCOHCI pThis, uint32_t iReg, uint32_t *pu32Value)
{
uint32_t val = pThis->intr_status;
Log2(("HcInterruptStatus_r() -> %#010x - SO=%d WDH=%d SF=%d RD=%d UE=%d FNO=%d RHSC=%d OC=%d\n",
val, val & 1, (val >> 1) & 1, (val >> 2) & 1, (val >> 3) & 1, (val >> 4) & 1, (val >> 5) & 1,
(val >> 6) & 1, (val >> 30) & 1));
*pu32Value = val;
RT_NOREF1(iReg);
return VINF_SUCCESS;
}
/**
* Write to the HcInterruptStatus register.
*/
static int HcInterruptStatus_w(POHCI pThis, uint32_t iReg, uint32_t val)
{
RT_NOREF1(iReg);
uint32_t res = pThis->intr_status & ~val;
uint32_t chg = pThis->intr_status ^ res; NOREF(chg);
int rc = PDMCritSectEnter(&pThis->CsIrq, VINF_IOM_R3_MMIO_WRITE);
if (rc != VINF_SUCCESS)
return rc;
Log2(("HcInterruptStatus_w(%#010x) => %sSO=%d %sWDH=%d %sSF=%d %sRD=%d %sUE=%d %sFNO=%d %sRHSC=%d %sOC=%d\n",
val,
chg & RT_BIT(0) ? "*" : "", res & 1,
chg & RT_BIT(1) ? "*" : "", (res >> 1) & 1,
chg & RT_BIT(2) ? "*" : "", (res >> 2) & 1,
chg & RT_BIT(3) ? "*" : "", (res >> 3) & 1,
chg & RT_BIT(4) ? "*" : "", (res >> 4) & 1,
chg & RT_BIT(5) ? "*" : "", (res >> 5) & 1,
chg & RT_BIT(6) ? "*" : "", (res >> 6) & 1,
chg & RT_BIT(30)? "*" : "", (res >> 30) & 1));
if ( (val & ~0xc000007f)
&& val != 0xffffffff /* ignore clear-all-like requests from xp. */)
Log2(("Unknown bits %#x are set!!!\n", val & ~0xc000007f));
/* "The Host Controller Driver may clear specific bits in this
* register by writing '1' to bit positions to be cleared"
*/
pThis->intr_status &= ~val;
ohciUpdateInterruptLocked(pThis, "HcInterruptStatus_w");
PDMCritSectLeave(&pThis->CsIrq);
return VINF_SUCCESS;
}
/**
* Read the HcInterruptEnable register
*/
static int HcInterruptEnable_r(PCOHCI pThis, uint32_t iReg, uint32_t *pu32Value)
{
uint32_t val = pThis->intr;
Log2(("HcInterruptEnable_r() -> %#010x - SO=%d WDH=%d SF=%d RD=%d UE=%d FNO=%d RHSC=%d OC=%d MIE=%d\n",
val, val & 1, (val >> 1) & 1, (val >> 2) & 1, (val >> 3) & 1, (val >> 4) & 1, (val >> 5) & 1,
(val >> 6) & 1, (val >> 30) & 1, (val >> 31) & 1));
*pu32Value = val;
RT_NOREF1(iReg);
return VINF_SUCCESS;
}
/**
* Writes to the HcInterruptEnable register.
*/
static int HcInterruptEnable_w(POHCI pThis, uint32_t iReg, uint32_t val)
{
RT_NOREF1(iReg);
uint32_t res = pThis->intr | val;
uint32_t chg = pThis->intr ^ res; NOREF(chg);
int rc = PDMCritSectEnter(&pThis->CsIrq, VINF_IOM_R3_MMIO_WRITE);
if (rc != VINF_SUCCESS)
return rc;
Log2(("HcInterruptEnable_w(%#010x) => %sSO=%d %sWDH=%d %sSF=%d %sRD=%d %sUE=%d %sFNO=%d %sRHSC=%d %sOC=%d %sMIE=%d\n",
val,
chg & RT_BIT(0) ? "*" : "", res & 1,
chg & RT_BIT(1) ? "*" : "", (res >> 1) & 1,
chg & RT_BIT(2) ? "*" : "", (res >> 2) & 1,
chg & RT_BIT(3) ? "*" : "", (res >> 3) & 1,
chg & RT_BIT(4) ? "*" : "", (res >> 4) & 1,
chg & RT_BIT(5) ? "*" : "", (res >> 5) & 1,
chg & RT_BIT(6) ? "*" : "", (res >> 6) & 1,
chg & RT_BIT(30) ? "*" : "", (res >> 30) & 1,
chg & RT_BIT(31) ? "*" : "", (res >> 31) & 1));
if (val & ~0xc000007f)
Log2(("Uknown bits %#x are set!!!\n", val & ~0xc000007f));
pThis->intr |= val;
ohciUpdateInterruptLocked(pThis, "HcInterruptEnable_w");
PDMCritSectLeave(&pThis->CsIrq);
return VINF_SUCCESS;
}
/**
* Reads the HcInterruptDisable register.
*/
static int HcInterruptDisable_r(PCOHCI pThis, uint32_t iReg, uint32_t *pu32Value)
{
#if 1 /** @todo r=bird: "On read, the current value of the HcInterruptEnable register is returned." */
uint32_t val = pThis->intr;
#else /* old code. */
uint32_t val = ~pThis->intr;
#endif
Log2(("HcInterruptDisable_r() -> %#010x - SO=%d WDH=%d SF=%d RD=%d UE=%d FNO=%d RHSC=%d OC=%d MIE=%d\n",
val, val & 1, (val >> 1) & 1, (val >> 2) & 1, (val >> 3) & 1, (val >> 4) & 1, (val >> 5) & 1,
(val >> 6) & 1, (val >> 30) & 1, (val >> 31) & 1));
*pu32Value = val;
RT_NOREF1(iReg);
return VINF_SUCCESS;
}
/**
* Writes to the HcInterruptDisable register.
*/
static int HcInterruptDisable_w(POHCI pThis, uint32_t iReg, uint32_t val)
{
RT_NOREF1(iReg);
uint32_t res = pThis->intr & ~val;
uint32_t chg = pThis->intr ^ res; NOREF(chg);
int rc = PDMCritSectEnter(&pThis->CsIrq, VINF_IOM_R3_MMIO_WRITE);
if (rc != VINF_SUCCESS)
return rc;
Log2(("HcInterruptDisable_w(%#010x) => %sSO=%d %sWDH=%d %sSF=%d %sRD=%d %sUE=%d %sFNO=%d %sRHSC=%d %sOC=%d %sMIE=%d\n",
val,
chg & RT_BIT(0) ? "*" : "", res & 1,
chg & RT_BIT(1) ? "*" : "", (res >> 1) & 1,
chg & RT_BIT(2) ? "*" : "", (res >> 2) & 1,
chg & RT_BIT(3) ? "*" : "", (res >> 3) & 1,
chg & RT_BIT(4) ? "*" : "", (res >> 4) & 1,
chg & RT_BIT(5) ? "*" : "", (res >> 5) & 1,
chg & RT_BIT(6) ? "*" : "", (res >> 6) & 1,
chg & RT_BIT(30) ? "*" : "", (res >> 30) & 1,
chg & RT_BIT(31) ? "*" : "", (res >> 31) & 1));
/* Don't bitch about invalid bits here since it makes sense to disable
* interrupts you don't know about. */
pThis->intr &= ~val;
ohciUpdateInterruptLocked(pThis, "HcInterruptDisable_w");
PDMCritSectLeave(&pThis->CsIrq);
return VINF_SUCCESS;
}
/**
* Read the HcHCCA register (Host Controller Communications Area physical address).
*/
static int HcHCCA_r(PCOHCI pThis, uint32_t iReg, uint32_t *pu32Value)
{
Log2(("HcHCCA_r() -> %#010x\n", pThis->hcca));
*pu32Value = pThis->hcca;
RT_NOREF1(iReg);
return VINF_SUCCESS;
}
/**
* Write to the HcHCCA register (Host Controller Communications Area physical address).
*/
static int HcHCCA_w(POHCI pThis, uint32_t iReg, uint32_t Value)
{
Log2(("HcHCCA_w(%#010x) - old=%#010x new=%#010x\n", Value, pThis->hcca, Value & OHCI_HCCA_MASK));
pThis->hcca = Value & OHCI_HCCA_MASK;
RT_NOREF1(iReg);
return VINF_SUCCESS;
}
/**
* Read the HcPeriodCurrentED register.
*/
static int HcPeriodCurrentED_r(PCOHCI pThis, uint32_t iReg, uint32_t *pu32Value)
{
Log2(("HcPeriodCurrentED_r() -> %#010x\n", pThis->per_cur));
*pu32Value = pThis->per_cur;
RT_NOREF1(iReg);
return VINF_SUCCESS;
}
/**
* Write to the HcPeriodCurrentED register.
*/
static int HcPeriodCurrentED_w(POHCI pThis, uint32_t iReg, uint32_t val)
{
Log(("HcPeriodCurrentED_w(%#010x) - old=%#010x new=%#010x (This is a read only register, only the linux guys don't respect that!)\n",
val, pThis->per_cur, val & ~7));
//AssertMsgFailed(("HCD (Host Controller Driver) should not write to HcPeriodCurrentED! val=%#010x (old=%#010x)\n", val, pThis->per_cur));
AssertMsg(!(val & 7), ("Invalid alignment, val=%#010x\n", val));
pThis->per_cur = val & ~7;
RT_NOREF1(iReg);
return VINF_SUCCESS;
}
/**
* Read the HcControlHeadED register.
*/
static int HcControlHeadED_r(PCOHCI pThis, uint32_t iReg, uint32_t *pu32Value)
{
Log2(("HcControlHeadED_r() -> %#010x\n", pThis->ctrl_head));
*pu32Value = pThis->ctrl_head;
RT_NOREF1(iReg);
return VINF_SUCCESS;
}
/**
* Write to the HcControlHeadED register.
*/
static int HcControlHeadED_w(POHCI pThis, uint32_t iReg, uint32_t val)
{
Log2(("HcControlHeadED_w(%#010x) - old=%#010x new=%#010x\n", val, pThis->ctrl_head, val & ~7));
AssertMsg(!(val & 7), ("Invalid alignment, val=%#010x\n", val));
pThis->ctrl_head = val & ~7;
RT_NOREF1(iReg);
return VINF_SUCCESS;
}
/**
* Read the HcControlCurrentED register.
*/
static int HcControlCurrentED_r(PCOHCI pThis, uint32_t iReg, uint32_t *pu32Value)
{
Log2(("HcControlCurrentED_r() -> %#010x\n", pThis->ctrl_cur));
*pu32Value = pThis->ctrl_cur;
RT_NOREF1(iReg);
return VINF_SUCCESS;
}
/**
* Write to the HcControlCurrentED register.
*/
static int HcControlCurrentED_w(POHCI pThis, uint32_t iReg, uint32_t val)
{
Log2(("HcControlCurrentED_w(%#010x) - old=%#010x new=%#010x\n", val, pThis->ctrl_cur, val & ~7));
AssertMsg(!(pThis->ctl & OHCI_CTL_CLE), ("Illegal write! HcControl.ControlListEnabled is set! val=%#010x\n", val));
AssertMsg(!(val & 7), ("Invalid alignment, val=%#010x\n", val));
pThis->ctrl_cur = val & ~7;
RT_NOREF1(iReg);
return VINF_SUCCESS;
}
/**
* Read the HcBulkHeadED register.
*/
static int HcBulkHeadED_r(PCOHCI pThis, uint32_t iReg, uint32_t *pu32Value)
{
Log2(("HcBulkHeadED_r() -> %#010x\n", pThis->bulk_head));
*pu32Value = pThis->bulk_head;
RT_NOREF1(iReg);
return VINF_SUCCESS;
}
/**
* Write to the HcBulkHeadED register.
*/
static int HcBulkHeadED_w(POHCI pThis, uint32_t iReg, uint32_t val)
{
Log2(("HcBulkHeadED_w(%#010x) - old=%#010x new=%#010x\n", val, pThis->bulk_head, val & ~7));
AssertMsg(!(val & 7), ("Invalid alignment, val=%#010x\n", val));
pThis->bulk_head = val & ~7; /** @todo The ATI OHCI controller on my machine enforces 16-byte address alignment. */
RT_NOREF1(iReg);
return VINF_SUCCESS;
}
/**
* Read the HcBulkCurrentED register.
*/
static int HcBulkCurrentED_r(PCOHCI pThis, uint32_t iReg, uint32_t *pu32Value)
{
Log2(("HcBulkCurrentED_r() -> %#010x\n", pThis->bulk_cur));
*pu32Value = pThis->bulk_cur;
RT_NOREF1(iReg);
return VINF_SUCCESS;
}
/**
* Write to the HcBulkCurrentED register.
*/
static int HcBulkCurrentED_w(POHCI pThis, uint32_t iReg, uint32_t val)
{
Log2(("HcBulkCurrentED_w(%#010x) - old=%#010x new=%#010x\n", val, pThis->bulk_cur, val & ~7));
AssertMsg(!(pThis->ctl & OHCI_CTL_BLE), ("Illegal write! HcControl.BulkListEnabled is set! val=%#010x\n", val));
AssertMsg(!(val & 7), ("Invalid alignment, val=%#010x\n", val));
pThis->bulk_cur = val & ~7;
RT_NOREF1(iReg);
return VINF_SUCCESS;
}
/**
* Read the HcDoneHead register.
*/
static int HcDoneHead_r(PCOHCI pThis, uint32_t iReg, uint32_t *pu32Value)
{
Log2(("HcDoneHead_r() -> 0x%#08x\n", pThis->done));
*pu32Value = pThis->done;
RT_NOREF1(iReg);
return VINF_SUCCESS;
}
/**
* Write to the HcDoneHead register.
*/
static int HcDoneHead_w(POHCI pThis, uint32_t iReg, uint32_t val)
{
RT_NOREF3(pThis, iReg, val);
Log2(("HcDoneHead_w(0x%#08x) - denied!!!\n", val));
/*AssertMsgFailed(("Illegal operation!!! val=%#010x\n", val)); - OS/2 does this */
return VINF_SUCCESS;
}
/**
* Read the HcFmInterval (Fm=Frame) register.
*/
static int HcFmInterval_r(PCOHCI pThis, uint32_t iReg, uint32_t *pu32Value)
{
uint32_t val = (pThis->fit << 31) | (pThis->fsmps << 16) | (pThis->fi);
Log2(("HcFmInterval_r() -> 0x%#08x - FI=%d FSMPS=%d FIT=%d\n",
val, val & 0x3fff, (val >> 16) & 0x7fff, val >> 31));
*pu32Value = val;
RT_NOREF1(iReg);
return VINF_SUCCESS;
}
/**
* Write to the HcFmInterval (Fm = Frame) register.
*/
static int HcFmInterval_w(POHCI pThis, uint32_t iReg, uint32_t val)
{
RT_NOREF1(iReg);
/* log */
uint32_t chg = val ^ ((pThis->fit << 31) | (pThis->fsmps << 16) | pThis->fi); NOREF(chg);
Log2(("HcFmInterval_w(%#010x) => %sFI=%d %sFSMPS=%d %sFIT=%d\n",
val,
chg & 0x00003fff ? "*" : "", val & 0x3fff,
chg & 0x7fff0000 ? "*" : "", (val >> 16) & 0x7fff,
chg >> 31 ? "*" : "", (val >> 31) & 1));
if ( pThis->fi != (val & OHCI_FMI_FI) )
{
Log(("ohci: FrameInterval: %#010x -> %#010x\n", pThis->fi, val & OHCI_FMI_FI));
AssertMsg(pThis->fit != ((val >> OHCI_FMI_FIT_SHIFT) & 1), ("HCD didn't toggle the FIT bit!!!\n"));
}
/* update */
pThis->fi = val & OHCI_FMI_FI;
pThis->fit = (val & OHCI_FMI_FIT) >> OHCI_FMI_FIT_SHIFT;
pThis->fsmps = (val & OHCI_FMI_FSMPS) >> OHCI_FMI_FSMPS_SHIFT;
return VINF_SUCCESS;
}
/**
* Read the HcFmRemaining (Fm = Frame) register.
*/
static int HcFmRemaining_r(PCOHCI pThis, uint32_t iReg, uint32_t *pu32Value)
{
RT_NOREF1(iReg);
uint32_t Value = pThis->frt << 31;
if ((pThis->ctl & OHCI_CTL_HCFS) == OHCI_USB_OPERATIONAL)
{
/*
* Being in USB operational state guarantees SofTime was set already.
*/
uint64_t tks = PDMDevHlpTMTimeVirtGet(pThis->CTX_SUFF(pDevIns)) - pThis->SofTime;
if (tks < pThis->cTicksPerFrame) /* avoid muldiv if possible */
{
uint16_t fr;
tks = ASMMultU64ByU32DivByU32(1, tks, pThis->cTicksPerUsbTick);
fr = (uint16_t)(pThis->fi - tks);
Value |= fr;
}
}
Log2(("HcFmRemaining_r() -> %#010x - FR=%d FRT=%d\n", Value, Value & 0x3fff, Value >> 31));
*pu32Value = Value;
return VINF_SUCCESS;
}
/**
* Write to the HcFmRemaining (Fm = Frame) register.
*/
static int HcFmRemaining_w(POHCI pThis, uint32_t iReg, uint32_t val)
{
RT_NOREF3(pThis, iReg, val);
Log2(("HcFmRemaining_w(%#010x) - denied\n", val));
AssertMsgFailed(("Invalid operation!!! val=%#010x\n", val));
return VINF_SUCCESS;
}
/**
* Read the HcFmNumber (Fm = Frame) register.
*/
static int HcFmNumber_r(PCOHCI pThis, uint32_t iReg, uint32_t *pu32Value)
{
RT_NOREF1(iReg);
uint32_t val = (uint16_t)pThis->HcFmNumber;
Log2(("HcFmNumber_r() -> %#010x - FN=%#x(%d) (32-bit=%#x(%d))\n", val, val, val, pThis->HcFmNumber, pThis->HcFmNumber));
*pu32Value = val;
return VINF_SUCCESS;
}
/**
* Write to the HcFmNumber (Fm = Frame) register.
*/
static int HcFmNumber_w(POHCI pThis, uint32_t iReg, uint32_t val)
{
RT_NOREF3(pThis, iReg, val);
Log2(("HcFmNumber_w(%#010x) - denied\n", val));
AssertMsgFailed(("Invalid operation!!! val=%#010x\n", val));
return VINF_SUCCESS;
}
/**
* Read the HcPeriodicStart register.
* The register determines when in a frame to switch from control&bulk to periodic lists.
*/
static int HcPeriodicStart_r(PCOHCI pThis, uint32_t iReg, uint32_t *pu32Value)
{
RT_NOREF1(iReg);
Log2(("HcPeriodicStart_r() -> %#010x - PS=%d\n", pThis->pstart, pThis->pstart & 0x3fff));
*pu32Value = pThis->pstart;
return VINF_SUCCESS;
}
/**
* Write to the HcPeriodicStart register.
* The register determines when in a frame to switch from control&bulk to periodic lists.
*/
static int HcPeriodicStart_w(POHCI pThis, uint32_t iReg, uint32_t val)
{
RT_NOREF1(iReg);
Log2(("HcPeriodicStart_w(%#010x) => PS=%d\n", val, val & 0x3fff));
if (val & ~0x3fff)
Log2(("Unknown bits %#x are set!!!\n", val & ~0x3fff));
pThis->pstart = val; /** @todo r=bird: should we support setting the other bits? */
return VINF_SUCCESS;
}
/**
* Read the HcLSThreshold register.
*/
static int HcLSThreshold_r(PCOHCI pThis, uint32_t iReg, uint32_t *pu32Value)
{
RT_NOREF2(pThis, iReg);
Log2(("HcLSThreshold_r() -> %#010x\n", OHCI_LS_THRESH));
*pu32Value = OHCI_LS_THRESH;
return VINF_SUCCESS;
}
/**
* Write to the HcLSThreshold register.
*
* Docs are inconsistent here:
*
* "Neither the Host Controller nor the Host Controller Driver are allowed to change this value."
*
* "This value is calculated by HCD with the consideration of transmission and setup overhead."
*
* The register is marked "R/W" the HCD column.
*
*/
static int HcLSThreshold_w(POHCI pThis, uint32_t iReg, uint32_t val)
{
RT_NOREF3(pThis, iReg, val);
Log2(("HcLSThreshold_w(%#010x) => LST=0x%03x(%d)\n", val, val & 0x0fff, val & 0x0fff));
AssertMsg(val == OHCI_LS_THRESH,
("HCD tried to write bad LS threshold: 0x%x (see function header)\n", val));
/** @todo the HCD can change this. */
return VINF_SUCCESS;
}
/**
* Read the HcRhDescriptorA register.
*/
static int HcRhDescriptorA_r(PCOHCI pThis, uint32_t iReg, uint32_t *pu32Value)
{
RT_NOREF1(iReg);
uint32_t val = pThis->RootHub.desc_a;
#if 0 /* annoying */
Log2(("HcRhDescriptorA_r() -> %#010x - NDP=%d PSM=%d NPS=%d DT=%d OCPM=%d NOCP=%d POTGT=%#x\n",
val, val & 0xff, (val >> 8) & 1, (val >> 9) & 1, (val >> 10) & 1, (val >> 11) & 1,
(val >> 12) & 1, (val >> 24) & 0xff));
#endif
*pu32Value = val;
return VINF_SUCCESS;
}
/**
* Write to the HcRhDescriptorA register.
*/
static int HcRhDescriptorA_w(POHCI pThis, uint32_t iReg, uint32_t val)
{
RT_NOREF1(iReg);
uint32_t chg = val ^ pThis->RootHub.desc_a; NOREF(chg);
Log2(("HcRhDescriptorA_w(%#010x) => %sNDP=%d %sPSM=%d %sNPS=%d %sDT=%d %sOCPM=%d %sNOCP=%d %sPOTGT=%#x - %sPowerSwitching Set%sPower\n",
val,
chg & 0xff ?"!!!": "", val & 0xff,
(chg >> 8) & 1 ? "*" : "", (val >> 8) & 1,
(chg >> 9) & 1 ? "*" : "", (val >> 9) & 1,
(chg >> 10) & 1 ?"!!!": "", 0,
(chg >> 11) & 1 ? "*" : "", (val >> 11) & 1,
(chg >> 12) & 1 ? "*" : "", (val >> 12) & 1,
(chg >> 24)&0xff? "*" : "", (val >> 24) & 0xff,
val & OHCI_RHA_NPS ? "No" : "",
val & OHCI_RHA_PSM ? "Port" : "Global"));
if (val & ~0xff001fff)
Log2(("Unknown bits %#x are set!!!\n", val & ~0xff001fff));
if ((val & (OHCI_RHA_NDP | OHCI_RHA_DT)) != OHCI_NDP_CFG(pThis))
{
Log(("ohci: %s: invalid write to NDP or DT in roothub descriptor A!!! val=0x%.8x\n",
pThis->PciDev.pszNameR3, val));
val &= ~(OHCI_RHA_NDP | OHCI_RHA_DT);
val |= OHCI_NDP_CFG(pThis);
}
pThis->RootHub.desc_a = val;
return VINF_SUCCESS;
}
/**
* Read the HcRhDescriptorB register.
*/
static int HcRhDescriptorB_r(PCOHCI pThis, uint32_t iReg, uint32_t *pu32Value)
{
uint32_t val = pThis->RootHub.desc_b;
Log2(("HcRhDescriptorB_r() -> %#010x - DR=0x%04x PPCM=0x%04x\n",
val, val & 0xffff, val >> 16));
*pu32Value = val;
RT_NOREF1(iReg);
return VINF_SUCCESS;
}
/**
* Write to the HcRhDescriptorB register.
*/
static int HcRhDescriptorB_w(POHCI pThis, uint32_t iReg, uint32_t val)
{
RT_NOREF1(iReg);
uint32_t chg = pThis->RootHub.desc_b ^ val; NOREF(chg);
Log2(("HcRhDescriptorB_w(%#010x) => %sDR=0x%04x %sPPCM=0x%04x\n",
val,
chg & 0xffff ? "!!!" : "", val & 0xffff,
chg >> 16 ? "!!!" : "", val >> 16));
if ( pThis->RootHub.desc_b != val )
Log(("ohci: %s: unsupported write to root descriptor B!!! 0x%.8x -> 0x%.8x\n",
pThis->PciDev.pszNameR3, pThis->RootHub.desc_b, val));
pThis->RootHub.desc_b = val;
return VINF_SUCCESS;
}
/**
* Read the HcRhStatus (Rh = Root Hub) register.
*/
static int HcRhStatus_r(PCOHCI pThis, uint32_t iReg, uint32_t *pu32Value)
{
uint32_t val = pThis->RootHub.status;
if (val & (OHCI_RHS_LPSC | OHCI_RHS_OCIC))
Log2(("HcRhStatus_r() -> %#010x - LPS=%d OCI=%d DRWE=%d LPSC=%d OCIC=%d CRWE=%d\n",
val, val & 1, (val >> 1) & 1, (val >> 15) & 1, (val >> 16) & 1, (val >> 17) & 1, (val >> 31) & 1));
*pu32Value = val;
RT_NOREF1(iReg);
return VINF_SUCCESS;
}
/**
* Write to the HcRhStatus (Rh = Root Hub) register.
*/
static int HcRhStatus_w(POHCI pThis, uint32_t iReg, uint32_t val)
{
#ifdef IN_RING3
/* log */
uint32_t old = pThis->RootHub.status;
uint32_t chg;
if (val & ~0x80038003)
Log2(("HcRhStatus_w: Unknown bits %#x are set!!!\n", val & ~0x80038003));
if ( (val & OHCI_RHS_LPSC) && (val & OHCI_RHS_LPS) )
Log2(("HcRhStatus_w: Warning both CGP and SGP are set! (Clear/Set Global Power)\n"));
if ( (val & OHCI_RHS_DRWE) && (val & OHCI_RHS_CRWE) )
Log2(("HcRhStatus_w: Warning both CRWE and SRWE are set! (Clear/Set Remote Wakeup Enable)\n"));
/* write 1 to clear OCIC */
if ( val & OHCI_RHS_OCIC )
pThis->RootHub.status &= ~OHCI_RHS_OCIC;
/* SetGlobalPower */
if ( val & OHCI_RHS_LPSC )
{
unsigned i;
Log2(("ohci: %s: global power up\n", pThis->PciDev.pszNameR3));
for (i = 0; i < OHCI_NDP_CFG(pThis); i++)
ohciR3RhPortPower(&pThis->RootHub, i, true /* power up */);
}
/* ClearGlobalPower */
if ( val & OHCI_RHS_LPS )
{
unsigned i;
Log2(("ohci: %s: global power down\n", pThis->PciDev.pszNameR3));
for (i = 0; i < OHCI_NDP_CFG(pThis); i++)
ohciR3RhPortPower(&pThis->RootHub, i, false /* power down */);
}
if ( val & OHCI_RHS_DRWE )
pThis->RootHub.status |= OHCI_RHS_DRWE;
if ( val & OHCI_RHS_CRWE )
pThis->RootHub.status &= ~OHCI_RHS_DRWE;
chg = pThis->RootHub.status ^ old;
Log2(("HcRhStatus_w(%#010x) => %sCGP=%d %sOCI=%d %sSRWE=%d %sSGP=%d %sOCIC=%d %sCRWE=%d\n",
val,
chg & 1 ? "*" : "", val & 1,
(chg >> 1) & 1 ?"!!!": "", (val >> 1) & 1,
(chg >> 15) & 1 ? "*" : "", (val >> 15) & 1,
(chg >> 16) & 1 ? "*" : "", (val >> 16) & 1,
(chg >> 17) & 1 ? "*" : "", (val >> 17) & 1,
(chg >> 31) & 1 ? "*" : "", (val >> 31) & 1));
RT_NOREF1(iReg);
return VINF_SUCCESS;
#else /* !IN_RING3 */
RT_NOREF3(pThis, iReg, val);
return VINF_IOM_R3_MMIO_WRITE;
#endif /* !IN_RING3 */
}
/**
* Read the HcRhPortStatus register of a port.
*/
static int HcRhPortStatus_r(PCOHCI pThis, uint32_t iReg, uint32_t *pu32Value)
{
const unsigned i = iReg - 21;
uint32_t val = pThis->RootHub.aPorts[i].fReg | OHCI_PORT_PPS; /* PortPowerStatus: see todo on power in _w function. */
if (val & OHCI_PORT_PRS)
{
#ifdef IN_RING3
RTThreadYield();
#else
Log2(("HcRhPortStatus_r: yield -> VINF_IOM_R3_MMIO_READ\n"));
return VINF_IOM_R3_MMIO_READ;
#endif
}
if (val & (OHCI_PORT_PRS | OHCI_PORT_CLEAR_CHANGE_MASK))
Log2(("HcRhPortStatus_r(): port %u: -> %#010x - CCS=%d PES=%d PSS=%d POCI=%d RRS=%d PPS=%d LSDA=%d CSC=%d PESC=%d PSSC=%d OCIC=%d PRSC=%d\n",
i, val, val & 1, (val >> 1) & 1, (val >> 2) & 1, (val >> 3) & 1, (val >> 4) & 1, (val >> 8) & 1, (val >> 9) & 1,
(val >> 16) & 1, (val >> 17) & 1, (val >> 18) & 1, (val >> 19) & 1, (val >> 20) & 1));
*pu32Value = val;
return VINF_SUCCESS;
}
#ifdef IN_RING3
/**
* Completion callback for the vusb_dev_reset() operation.
* @thread EMT.
*/
static DECLCALLBACK(void) ohciR3PortResetDone(PVUSBIDEVICE pDev, int rc, void *pvUser)
{
POHCI pThis = (POHCI)pvUser;
/*
* Find the port in question
*/
POHCIHUBPORT pPort = NULL;
unsigned iPort;
for (iPort = 0; iPort < OHCI_NDP_CFG(pThis); iPort++) /* lazy bird */
if (pThis->RootHub.aPorts[iPort].pDev == pDev)
{
pPort = &pThis->RootHub.aPorts[iPort];
break;
}
if (!pPort)
{
Assert(pPort); /* sometimes happens because of @bugref{1510} */
return;
}
if (RT_SUCCESS(rc))
{
/*
* Successful reset.
*/
Log2(("ohciR3PortResetDone: Reset completed.\n"));
pPort->fReg &= ~(OHCI_PORT_PRS | OHCI_PORT_PSS | OHCI_PORT_PSSC);
pPort->fReg |= OHCI_PORT_PES | OHCI_PORT_PRSC;
}
else
{
/* desperate measures. */
if ( pPort->pDev
&& VUSBIDevGetState(pPort->pDev) == VUSB_DEVICE_STATE_ATTACHED)
{
/*
* Damn, something weird happened during reset. We'll pretend the user did an
* incredible fast reconnect or something. (probably not gonna work)
*/
Log2(("ohciR3PortResetDone: The reset failed (rc=%Rrc)!!! Pretending reconnect at the speed of light.\n", rc));
pPort->fReg = OHCI_PORT_CCS | OHCI_PORT_CSC;
}
else
{
/*
* The device have / will be disconnected.
*/
Log2(("ohciR3PortResetDone: Disconnected (rc=%Rrc)!!!\n", rc));
pPort->fReg &= ~(OHCI_PORT_PRS | OHCI_PORT_PSS | OHCI_PORT_PSSC | OHCI_PORT_PRSC);
pPort->fReg |= OHCI_PORT_CSC;
}
}
/* Raise roothub status change interrupt. */
ohciR3SetInterrupt(pThis, OHCI_INTR_ROOT_HUB_STATUS_CHANGE);
}
/**
* Sets a flag in a port status register but only set it if a device is
* connected, if not set ConnectStatusChange flag to force HCD to reevaluate
* connect status.
*
* @returns true if device was connected and the flag was cleared.
*/
static bool ohciR3RhPortSetIfConnected(POHCIROOTHUB pRh, int iPort, uint32_t fValue)
{
/*
* Writing a 0 has no effect
*/
if (fValue == 0)
return false;
/*
* If CurrentConnectStatus is cleared we set ConnectStatusChange.
*/
if (!(pRh->aPorts[iPort].fReg & OHCI_PORT_CCS))
{
pRh->aPorts[iPort].fReg |= OHCI_PORT_CSC;
ohciR3SetInterrupt(pRh->pOhci, OHCI_INTR_ROOT_HUB_STATUS_CHANGE);
return false;
}
bool fRc = !(pRh->aPorts[iPort].fReg & fValue);
/* set the bit */
pRh->aPorts[iPort].fReg |= fValue;
return fRc;
}
#endif /* IN_RING3 */
/**
* Write to the HcRhPortStatus register of a port.
*/
static int HcRhPortStatus_w(POHCI pThis, uint32_t iReg, uint32_t val)
{
#ifdef IN_RING3
const unsigned i = iReg - 21;
POHCIHUBPORT p = &pThis->RootHub.aPorts[i];
uint32_t old_state = p->fReg;
# ifdef LOG_ENABLED
/*
* Log it.
*/
static const char *apszCmdNames[32] =
{
"ClearPortEnable", "SetPortEnable", "SetPortSuspend", "!!!ClearSuspendStatus",
"SetPortReset", "!!!5", "!!!6", "!!!7",
"SetPortPower", "ClearPortPower", "!!!10", "!!!11",
"!!!12", "!!!13", "!!!14", "!!!15",
"ClearCSC", "ClearPESC", "ClearPSSC", "ClearOCIC",
"ClearPRSC", "!!!21", "!!!22", "!!!23",
"!!!24", "!!!25", "!!!26", "!!!27",
"!!!28", "!!!29", "!!!30", "!!!31"
};
Log2(("HcRhPortStatus_w(%#010x): port %u:", val, i));
for (unsigned j = 0; j < RT_ELEMENTS(apszCmdNames); j++)
if (val & (1 << j))
Log2((" %s", apszCmdNames[j]));
Log2(("\n"));
# endif
/* Write to clear any of the change bits: CSC, PESC, PSSC, OCIC and PRSC */
if (val & OHCI_PORT_CLEAR_CHANGE_MASK)
p->fReg &= ~(val & OHCI_PORT_CLEAR_CHANGE_MASK);
if (val & OHCI_PORT_CLRPE)
{
p->fReg &= ~OHCI_PORT_PES;
Log2(("HcRhPortStatus_w(): port %u: DISABLE\n", i));
}
if (ohciR3RhPortSetIfConnected(&pThis->RootHub, i, val & OHCI_PORT_PES))
Log2(("HcRhPortStatus_w(): port %u: ENABLE\n", i));
if (ohciR3RhPortSetIfConnected(&pThis->RootHub, i, val & OHCI_PORT_PSS))
Log2(("HcRhPortStatus_w(): port %u: SUSPEND - not implemented correctly!!!\n", i));
if (val & OHCI_PORT_PRS)
{
if (ohciR3RhPortSetIfConnected(&pThis->RootHub, i, val & OHCI_PORT_PRS))
{
PVM pVM = PDMDevHlpGetVM(pThis->CTX_SUFF(pDevIns));
p->fReg &= ~OHCI_PORT_PRSC;
VUSBIDevReset(p->pDev, false /* don't reset on linux */, ohciR3PortResetDone, pThis, pVM);
}
else if (p->fReg & OHCI_PORT_PRS)
{
/* the guest is getting impatient. */
Log2(("HcRhPortStatus_w(): port %u: Impatient guest!\n", i));
RTThreadYield();
}
}
if (!(pThis->RootHub.desc_a & OHCI_RHA_NPS))
{
/** @todo To implement per-device power-switching
* we need to check PortPowerControlMask to make
* sure it isn't gang powered
*/
if (val & OHCI_PORT_CLRPP)
ohciR3RhPortPower(&pThis->RootHub, i, false /* power down */);
if (val & OHCI_PORT_PPS)
ohciR3RhPortPower(&pThis->RootHub, i, true /* power up */);
}
/** @todo r=frank: ClearSuspendStatus. Timing? */
if (val & OHCI_PORT_CLRSS)
{
ohciR3RhPortPower(&pThis->RootHub, i, true /* power up */);
pThis->RootHub.aPorts[i].fReg &= ~OHCI_PORT_PSS;
pThis->RootHub.aPorts[i].fReg |= OHCI_PORT_PSSC;
ohciR3SetInterrupt(pThis, OHCI_INTR_ROOT_HUB_STATUS_CHANGE);
}
if (p->fReg != old_state)
{
uint32_t res = p->fReg;
uint32_t chg = res ^ old_state; NOREF(chg);
Log2(("HcRhPortStatus_w(%#010x): port %u: => %sCCS=%d %sPES=%d %sPSS=%d %sPOCI=%d %sRRS=%d %sPPS=%d %sLSDA=%d %sCSC=%d %sPESC=%d %sPSSC=%d %sOCIC=%d %sPRSC=%d\n",
val, i,
chg & 1 ? "*" : "", res & 1,
(chg >> 1) & 1 ? "*" : "", (res >> 1) & 1,
(chg >> 2) & 1 ? "*" : "", (res >> 2) & 1,
(chg >> 3) & 1 ? "*" : "", (res >> 3) & 1,
(chg >> 4) & 1 ? "*" : "", (res >> 4) & 1,
(chg >> 8) & 1 ? "*" : "", (res >> 8) & 1,
(chg >> 9) & 1 ? "*" : "", (res >> 9) & 1,
(chg >> 16) & 1 ? "*" : "", (res >> 16) & 1,
(chg >> 17) & 1 ? "*" : "", (res >> 17) & 1,
(chg >> 18) & 1 ? "*" : "", (res >> 18) & 1,
(chg >> 19) & 1 ? "*" : "", (res >> 19) & 1,
(chg >> 20) & 1 ? "*" : "", (res >> 20) & 1));
}
return VINF_SUCCESS;
#else /* !IN_RING3 */
RT_NOREF3(pThis, iReg, val);
return VINF_IOM_R3_MMIO_WRITE;
#endif /* !IN_RING3 */
}
/**
* Register descriptor table
*/
static const OHCIOPREG g_aOpRegs[] =
{
{ "HcRevision", HcRevision_r, HcRevision_w }, /* 0 */
{ "HcControl", HcControl_r, HcControl_w }, /* 1 */
{ "HcCommandStatus", HcCommandStatus_r, HcCommandStatus_w }, /* 2 */
{ "HcInterruptStatus", HcInterruptStatus_r, HcInterruptStatus_w }, /* 3 */
{ "HcInterruptEnable", HcInterruptEnable_r, HcInterruptEnable_w }, /* 4 */
{ "HcInterruptDisable", HcInterruptDisable_r, HcInterruptDisable_w }, /* 5 */
{ "HcHCCA", HcHCCA_r, HcHCCA_w }, /* 6 */
{ "HcPeriodCurrentED", HcPeriodCurrentED_r, HcPeriodCurrentED_w }, /* 7 */
{ "HcControlHeadED", HcControlHeadED_r, HcControlHeadED_w }, /* 8 */
{ "HcControlCurrentED", HcControlCurrentED_r, HcControlCurrentED_w }, /* 9 */
{ "HcBulkHeadED", HcBulkHeadED_r, HcBulkHeadED_w }, /* 10 */
{ "HcBulkCurrentED", HcBulkCurrentED_r, HcBulkCurrentED_w }, /* 11 */
{ "HcDoneHead", HcDoneHead_r, HcDoneHead_w }, /* 12 */
{ "HcFmInterval", HcFmInterval_r, HcFmInterval_w }, /* 13 */
{ "HcFmRemaining", HcFmRemaining_r, HcFmRemaining_w }, /* 14 */
{ "HcFmNumber", HcFmNumber_r, HcFmNumber_w }, /* 15 */
{ "HcPeriodicStart", HcPeriodicStart_r, HcPeriodicStart_w }, /* 16 */
{ "HcLSThreshold", HcLSThreshold_r, HcLSThreshold_w }, /* 17 */
{ "HcRhDescriptorA", HcRhDescriptorA_r, HcRhDescriptorA_w }, /* 18 */
{ "HcRhDescriptorB", HcRhDescriptorB_r, HcRhDescriptorB_w }, /* 19 */
{ "HcRhStatus", HcRhStatus_r, HcRhStatus_w }, /* 20 */
/* The number of port status register depends on the definition
* of OHCI_NDP_MAX macro
*/
{ "HcRhPortStatus[0]", HcRhPortStatus_r, HcRhPortStatus_w }, /* 21 */
{ "HcRhPortStatus[1]", HcRhPortStatus_r, HcRhPortStatus_w }, /* 22 */
{ "HcRhPortStatus[2]", HcRhPortStatus_r, HcRhPortStatus_w }, /* 23 */
{ "HcRhPortStatus[3]", HcRhPortStatus_r, HcRhPortStatus_w }, /* 24 */
{ "HcRhPortStatus[4]", HcRhPortStatus_r, HcRhPortStatus_w }, /* 25 */
{ "HcRhPortStatus[5]", HcRhPortStatus_r, HcRhPortStatus_w }, /* 26 */
{ "HcRhPortStatus[6]", HcRhPortStatus_r, HcRhPortStatus_w }, /* 27 */
{ "HcRhPortStatus[7]", HcRhPortStatus_r, HcRhPortStatus_w }, /* 28 */
{ "HcRhPortStatus[8]", HcRhPortStatus_r, HcRhPortStatus_w }, /* 29 */
{ "HcRhPortStatus[9]", HcRhPortStatus_r, HcRhPortStatus_w }, /* 30 */
{ "HcRhPortStatus[10]", HcRhPortStatus_r, HcRhPortStatus_w }, /* 31 */
{ "HcRhPortStatus[11]", HcRhPortStatus_r, HcRhPortStatus_w }, /* 32 */
{ "HcRhPortStatus[12]", HcRhPortStatus_r, HcRhPortStatus_w }, /* 33 */
{ "HcRhPortStatus[13]", HcRhPortStatus_r, HcRhPortStatus_w }, /* 34 */
{ "HcRhPortStatus[14]", HcRhPortStatus_r, HcRhPortStatus_w }, /* 35 */
};
/* Quick way to determine how many op regs are valid. Since at least one port must
* be configured (and no more than 15), there will be between 22 and 36 registers.
*/
#define NUM_OP_REGS(pohci) (21 + OHCI_NDP_CFG(pohci))
AssertCompile(RT_ELEMENTS(g_aOpRegs) > 21);
AssertCompile(RT_ELEMENTS(g_aOpRegs) <= 36);
/**
* @callback_method_impl{FNIOMMMIOREAD}
*/
PDMBOTHCBDECL(int) ohciMmioRead(PPDMDEVINS pDevIns, void *pvUser, RTGCPHYS GCPhysAddr, void *pv, unsigned cb)
{
POHCI pThis = PDMINS_2_DATA(pDevIns, POHCI);
RT_NOREF1(pvUser);
/* Paranoia: Assert that IOMMMIO_FLAGS_READ_DWORD works. */
AssertReturn(cb == sizeof(uint32_t), VERR_INTERNAL_ERROR_3);
AssertReturn(!(GCPhysAddr & 0x3), VERR_INTERNAL_ERROR_4);
/*
* Validate the register and call the read operator.
*/
int rc;
const uint32_t iReg = (GCPhysAddr - pThis->MMIOBase) >> 2;
if (iReg < NUM_OP_REGS(pThis))
{
const OHCIOPREG *pReg = &g_aOpRegs[iReg];
rc = pReg->pfnRead(pThis, iReg, (uint32_t *)pv);
}
else
{
Log(("ohci: Trying to read register %u/%u!!!\n", iReg, NUM_OP_REGS(pThis)));
rc = VINF_IOM_MMIO_UNUSED_FF;
}
return rc;
}
/**
* @callback_method_impl{FNIOMMMIOWRITE}
*/
PDMBOTHCBDECL(int) ohciMmioWrite(PPDMDEVINS pDevIns, void *pvUser, RTGCPHYS GCPhysAddr, void const *pv, unsigned cb)
{
POHCI pThis = PDMINS_2_DATA(pDevIns, POHCI);
RT_NOREF1(pvUser);
/* Paranoia: Assert that IOMMMIO_FLAGS_WRITE_DWORD_ZEROED works. */
AssertReturn(cb == sizeof(uint32_t), VERR_INTERNAL_ERROR_3);
AssertReturn(!(GCPhysAddr & 0x3), VERR_INTERNAL_ERROR_4);
/*
* Validate the register and call the read operator.
*/
int rc;
const uint32_t iReg = (GCPhysAddr - pThis->MMIOBase) >> 2;
if (iReg < NUM_OP_REGS(pThis))
{
const OHCIOPREG *pReg = &g_aOpRegs[iReg];
rc = pReg->pfnWrite(pThis, iReg, *(uint32_t const *)pv);
}
else
{
Log(("ohci: Trying to write to register %u/%u!!!\n", iReg, NUM_OP_REGS(pThis)));
rc = VINF_SUCCESS;
}
return rc;
}
#ifdef IN_RING3
/**
* @callback_method_impl{FNPCIIOREGIONMAP}
*/
static DECLCALLBACK(int) ohciR3Map(PPDMDEVINS pDevIns, PPDMPCIDEV pPciDev, uint32_t iRegion,
RTGCPHYS GCPhysAddress, RTGCPHYS cb, PCIADDRESSSPACE enmType)
{
RT_NOREF(iRegion, enmType);
POHCI pThis = (POHCI)pPciDev;
int rc = PDMDevHlpMMIORegister(pDevIns, GCPhysAddress, cb, NULL /*pvUser*/,
IOMMMIO_FLAGS_READ_DWORD | IOMMMIO_FLAGS_WRITE_DWORD_ZEROED
| IOMMMIO_FLAGS_DBGSTOP_ON_COMPLICATED_WRITE,
ohciMmioWrite, ohciMmioRead, "USB OHCI");
if (RT_FAILURE(rc))
return rc;
if (pThis->fRZEnabled)
{
rc = PDMDevHlpMMIORegisterRC(pDevIns, GCPhysAddress, cb, NIL_RTRCPTR /*pvUser*/, "ohciMmioWrite", "ohciMmioRead");
if (RT_FAILURE(rc))
return rc;
rc = PDMDevHlpMMIORegisterR0(pDevIns, GCPhysAddress, cb, NIL_RTR0PTR /*pvUser*/, "ohciMmioWrite", "ohciMmioRead");
if (RT_FAILURE(rc))
return rc;
}
pThis->MMIOBase = GCPhysAddress;
return VINF_SUCCESS;
}
/**
* Prepares for state saving.
* All URBs needs to be canceled.
*
* @returns VBox status code.
* @param pDevIns The device instance.
* @param pSSM The handle to save the state to.
*/
static DECLCALLBACK(int) ohciR3SavePrep(PPDMDEVINS pDevIns, PSSMHANDLE pSSM)
{
RT_NOREF(pSSM);
POHCI pThis = PDMINS_2_DATA(pDevIns, POHCI);
POHCIROOTHUB pRh = &pThis->RootHub;
LogFlow(("ohciR3SavePrep: \n"));
/*
* Detach all proxied devices.
*/
PDMCritSectEnter(pThis->pDevInsR3->pCritSectRoR3, VERR_IGNORED);
/** @todo this won't work well when continuing after saving! */
for (unsigned i = 0; i < RT_ELEMENTS(pRh->aPorts); i++)
{
PVUSBIDEVICE pDev = pRh->aPorts[i].pDev;
if (pDev)
{
if (!VUSBIDevIsSavedStateSupported(pDev))
{
VUSBIRhDetachDevice(pRh->pIRhConn, pDev);
/*
* Save the device pointers here so we can reattach them afterwards.
* This will work fine even if the save fails since the Done handler is
* called unconditionally if the Prep handler was called.
*/
pRh->aPorts[i].pDev = pDev;
}
}
}
/*
* If the bus was started set the timer. This is ugly but avoids changing the
* saved state version for now so we can backport the changes to other branches.
*/
/** @todo Do it properly for 4.4 by changing the saved state. */
if (VUSBIRhGetPeriodicFrameRate(pRh->pIRhConn) != 0)
{
/* Calculate a new timer expiration so this saved state works with older releases. */
uint64_t u64Expire = PDMDevHlpTMTimeVirtGet(pThis->CTX_SUFF(pDevIns)) + pThis->cTicksPerFrame;
LogFlowFunc(("Bus is active, setting timer to %llu\n", u64Expire));
int rc = TMTimerSet(pThis->pEndOfFrameTimerR3, u64Expire);
AssertRC(rc);
}
PDMCritSectLeave(pThis->pDevInsR3->pCritSectRoR3);
/*
* Kill old load data which might be hanging around.
*/
if (pThis->pLoad)
{
TMR3TimerDestroy(pThis->pLoad->pTimer);
MMR3HeapFree(pThis->pLoad);
pThis->pLoad = NULL;
}
return VINF_SUCCESS;
}
/**
* Saves the state of the OHCI device.
*
* @returns VBox status code.
* @param pDevIns The device instance.
* @param pSSM The handle to save the state to.
*/
static DECLCALLBACK(int) ohciR3SaveExec(PPDMDEVINS pDevIns, PSSMHANDLE pSSM)
{
POHCI pThis = PDMINS_2_DATA(pDevIns, POHCI);
LogFlow(("ohciR3SaveExec: \n"));
int rc = SSMR3PutStructEx(pSSM, pThis, sizeof(*pThis), 0 /*fFlags*/, &g_aOhciFields[0], NULL);
if (RT_SUCCESS(rc))
rc = TMR3TimerSave(pThis->CTX_SUFF(pEndOfFrameTimer), pSSM);
return rc;
}
/**
* Done state save operation.
*
* @returns VBox load code.
* @param pDevIns Device instance of the device which registered the data unit.
* @param pSSM SSM operation handle.
*/
static DECLCALLBACK(int) ohciR3SaveDone(PPDMDEVINS pDevIns, PSSMHANDLE pSSM)
{
RT_NOREF(pSSM);
POHCI pThis = PDMINS_2_DATA(pDevIns, POHCI);
LogFlow(("ohciR3SaveDone: \n"));
/*
* NULL the dev pointers.
*/
POHCIROOTHUB pRh = &pThis->RootHub;
OHCIROOTHUB Rh = *pRh;
for (unsigned i = 0; i < RT_ELEMENTS(pRh->aPorts); i++)
{
if ( pRh->aPorts[i].pDev
&& !VUSBIDevIsSavedStateSupported(pRh->aPorts[i].pDev))
pRh->aPorts[i].pDev = NULL;
}
/*
* Attach the devices.
*/
for (unsigned i = 0; i < RT_ELEMENTS(pRh->aPorts); i++)
{
PVUSBIDEVICE pDev = Rh.aPorts[i].pDev;
if ( pDev
&& !VUSBIDevIsSavedStateSupported(pDev))
VUSBIRhAttachDevice(pRh->pIRhConn, pDev);
}
return VINF_SUCCESS;
}
/**
* Prepare loading the state of the OHCI device.
* This must detach the devices currently attached and save
* the up for reconnect after the state load have been completed
*
* @returns VBox status code.
* @param pDevIns The device instance.
* @param pSSM The handle to the saved state.
*/
static DECLCALLBACK(int) ohciR3LoadPrep(PPDMDEVINS pDevIns, PSSMHANDLE pSSM)
{
RT_NOREF(pSSM);
POHCI pThis = PDMINS_2_DATA(pDevIns, POHCI);
LogFlow(("ohciR3LoadPrep:\n"));
if (!pThis->pLoad)
{
/*
* Detach all devices which are present in this session. Save them in the load
* structure so we can reattach them after restoring the guest.
*/
POHCIROOTHUB pRh = &pThis->RootHub;
OHCILOAD Load;
Load.pTimer = NULL;
Load.cDevs = 0;
for (unsigned i = 0; i < RT_ELEMENTS(pRh->aPorts); i++)
{
PVUSBIDEVICE pDev = pRh->aPorts[i].pDev;
if ( pDev
&& !VUSBIDevIsSavedStateSupported(pDev))
{
Load.apDevs[Load.cDevs++] = pDev;
VUSBIRhDetachDevice(pRh->pIRhConn, pDev);
Assert(!pRh->aPorts[i].pDev);
}
}
/*
* Any devices to reattach, if so duplicate the Load struct.
*/
if (Load.cDevs)
{
pThis->pLoad = (POHCILOAD)PDMDevHlpMMHeapAlloc(pDevIns, sizeof(Load));
if (!pThis->pLoad)
return VERR_NO_MEMORY;
*pThis->pLoad = Load;
}
}
/* else: we ASSUME no device can be attached or detach in the period
* between a state load and the pLoad stuff is processed. */
return VINF_SUCCESS;
}
/**
* Loads the state of the OHCI device.
*
* @returns VBox status code.
* @param pDevIns The device instance.
* @param pSSM The handle to the saved state.
* @param uVersion The data unit version number.
* @param uPass The data pass.
*/
static DECLCALLBACK(int) ohciR3LoadExec(PPDMDEVINS pDevIns, PSSMHANDLE pSSM, uint32_t uVersion, uint32_t uPass)
{
POHCI pThis = PDMINS_2_DATA(pDevIns, POHCI);
int rc;
LogFlow(("ohciR3LoadExec:\n"));
Assert(uPass == SSM_PASS_FINAL); NOREF(uPass);
if (uVersion == OHCI_SAVED_STATE_VERSION)
{
rc = SSMR3GetStructEx(pSSM, pThis, sizeof(*pThis), 0 /*fFlags*/, &g_aOhciFields[0], NULL);
if (RT_FAILURE(rc))
return rc;
}
else if (uVersion == OHCI_SAVED_STATE_VERSION_8PORTS)
{
rc = SSMR3GetStructEx(pSSM, pThis, sizeof(*pThis), 0 /*fFlags*/, &g_aOhciFields8Ports[0], NULL);
if (RT_FAILURE(rc))
return rc;
}
else
AssertMsgFailedReturn(("%d\n", uVersion), VERR_SSM_UNSUPPORTED_DATA_UNIT_VERSION);
/*
* Finally restore the timer.
*/
return TMR3TimerLoad(pThis->pEndOfFrameTimerR3, pSSM);
}
/**
* Done state load operation.
*
* @returns VBox load code.
* @param pDevIns Device instance of the device which registered the data unit.
* @param pSSM SSM operation handle.
*/
static DECLCALLBACK(int) ohciR3LoadDone(PPDMDEVINS pDevIns, PSSMHANDLE pSSM)
{
RT_NOREF(pSSM);
POHCI pThis = PDMINS_2_DATA(pDevIns, POHCI);
LogFlow(("ohciR3LoadDone:\n"));
/*
* Start a timer if we've got devices to reattach
*/
if (pThis->pLoad)
{
int rc = PDMDevHlpTMTimerCreate(pDevIns, TMCLOCK_VIRTUAL, ohciR3LoadReattachDevices, pThis,
TMTIMER_FLAGS_NO_CRIT_SECT, "OHCI reattach devices on load",
&pThis->pLoad->pTimer);
if (RT_SUCCESS(rc))
rc = TMTimerSetMillies(pThis->pLoad->pTimer, 250);
return rc;
}
return VINF_SUCCESS;
}
/**
* Reattaches devices after a saved state load.
*/
static DECLCALLBACK(void) ohciR3LoadReattachDevices(PPDMDEVINS pDevIns, PTMTIMER pTimer, void *pvUser)
{
RT_NOREF(pDevIns);
POHCI pThis = (POHCI)pvUser;
POHCILOAD pLoad = pThis->pLoad;
POHCIROOTHUB pRh = &pThis->RootHub;
LogFlow(("ohciR3LoadReattachDevices:\n"));
/*
* Reattach devices.
*/
for (unsigned i = 0; i < pLoad->cDevs; i++)
VUSBIRhAttachDevice(pRh->pIRhConn, pLoad->apDevs[i]);
/*
* Cleanup.
*/
TMR3TimerDestroy(pTimer);
MMR3HeapFree(pLoad);
pThis->pLoad = NULL;
}
/**
* Reset notification.
*
* @returns VBox status code.
* @param pDevIns The device instance data.
*/
static DECLCALLBACK(void) ohciR3Reset(PPDMDEVINS pDevIns)
{
POHCI pThis = PDMINS_2_DATA(pDevIns, POHCI);
LogFlow(("ohciR3Reset:\n"));
/*
* There is no distinction between cold boot, warm reboot and software reboots,
* all of these are treated as cold boots. We are also doing the initialization
* job of a BIOS or SMM driver.
*
* Important: Don't confuse UsbReset with hardware reset. Hardware reset is
* just one way of getting into the UsbReset state.
*/
ohciR3DoReset(pThis, OHCI_USB_RESET, true /* reset devices */);
}
/**
* Resume notification.
*
* @returns VBox status code.
* @param pDevIns The device instance data.
*/
static DECLCALLBACK(void) ohciR3Resume(PPDMDEVINS pDevIns)
{
POHCI pThis = PDMINS_2_DATA(pDevIns, POHCI);
LogFlowFunc(("\n"));
/* Restart the frame thread if the timer is active. */
if (TMTimerIsActive(pThis->pEndOfFrameTimerR3))
{
int rc = TMTimerStop(pThis->pEndOfFrameTimerR3);
AssertRC(rc);
LogFlowFunc(("Bus was active, enable periodic frame processing\n"));
rc = pThis->RootHub.pIRhConn->pfnSetPeriodicFrameProcessing(pThis->RootHub.pIRhConn, OHCI_DEFAULT_TIMER_FREQ);
AssertRC(rc);
}
}
/**
* Info handler, device version. Dumps OHCI control registers.
*
* @param pDevIns Device instance which registered the info.
* @param pHlp Callback functions for doing output.
* @param pszArgs Argument string. Optional and specific to the handler.
*/
static DECLCALLBACK(void) ohciR3InfoRegs(PPDMDEVINS pDevIns, PCDBGFINFOHLP pHlp, const char *pszArgs)
{
RT_NOREF(pszArgs);
POHCI pThis = PDMINS_2_DATA(pDevIns, POHCI);
uint32_t val, ctl, status;
/* Control register */
ctl = pThis->ctl;
pHlp->pfnPrintf(pHlp, "HcControl: %08x - CBSR=%d PLE=%d IE=%d CLE=%d BLE=%d HCFS=%#x IR=%d RWC=%d RWE=%d\n",
ctl, ctl & 3, (ctl >> 2) & 1, (ctl >> 3) & 1, (ctl >> 4) & 1, (ctl >> 5) & 1, (ctl >> 6) & 3, (ctl >> 8) & 1,
(ctl >> 9) & 1, (ctl >> 10) & 1);
/* Command status register */
status = pThis->status;
pHlp->pfnPrintf(pHlp, "HcCommandStatus: %08x - HCR=%d CLF=%d BLF=%d OCR=%d SOC=%d\n",
status, status & 1, (status >> 1) & 1, (status >> 2) & 1, (status >> 3) & 1, (status >> 16) & 3);
/* Interrupt status register */
val = pThis->intr_status;
pHlp->pfnPrintf(pHlp, "HcInterruptStatus: %08x - SO=%d WDH=%d SF=%d RD=%d UE=%d FNO=%d RHSC=%d OC=%d\n",
val, val & 1, (val >> 1) & 1, (val >> 2) & 1, (val >> 3) & 1, (val >> 4) & 1, (val >> 5) & 1,
(val >> 6) & 1, (val >> 30) & 1);
/* Interrupt enable register */
val = pThis->intr;
pHlp->pfnPrintf(pHlp, "HcInterruptEnable: %08x - SO=%d WDH=%d SF=%d RD=%d UE=%d FNO=%d RHSC=%d OC=%d MIE=%d\n",
val, val & 1, (val >> 1) & 1, (val >> 2) & 1, (val >> 3) & 1, (val >> 4) & 1, (val >> 5) & 1,
(val >> 6) & 1, (val >> 30) & 1, (val >> 31) & 1);
/* HCCA address register */
pHlp->pfnPrintf(pHlp, "HcHCCA: %08x\n", pThis->hcca);
/* Current periodic ED register */
pHlp->pfnPrintf(pHlp, "HcPeriodCurrentED: %08x\n", pThis->per_cur);
/* Control ED registers */
pHlp->pfnPrintf(pHlp, "HcControlHeadED: %08x\n", pThis->ctrl_head);
pHlp->pfnPrintf(pHlp, "HcControlCurrentED: %08x\n", pThis->ctrl_cur);
/* Bulk ED registers */
pHlp->pfnPrintf(pHlp, "HcBulkHeadED: %08x\n", pThis->bulk_head);
pHlp->pfnPrintf(pHlp, "HcBulkCurrentED: %08x\n", pThis->bulk_cur);
/* Done head register */
pHlp->pfnPrintf(pHlp, "HcDoneHead: %08x\n", pThis->done);
/* Done head register */
pHlp->pfnPrintf(pHlp, "HcDoneHead: %08x\n", pThis->done);
/* Root hub descriptor A */
val = pThis->RootHub.desc_a;
pHlp->pfnPrintf(pHlp, "HcRhDescriptorA: %08x - NDP=%d PSM=%d NPS=%d DT=%d OCPM=%d NOCP=%d POTPGT=%d\n",
val, (uint8_t)val, (val >> 8) & 1, (val >> 9) & 1, (val >> 10) & 1, (val >> 11) & 1, (val >> 12) & 1, (uint8_t)(val >> 24));
/* Root hub descriptor B */
val = pThis->RootHub.desc_b;
pHlp->pfnPrintf(pHlp, "HcRhDescriptorB: %08x - DR=%#04x PPCM=%#04x\n", val, (uint16_t)val, (uint16_t)(val >> 16));
/* Root hub status register */
val = pThis->RootHub.status;
pHlp->pfnPrintf(pHlp, "HcRhStatus: %08x - LPS=%d OCI=%d DRWE=%d LPSC=%d OCIC=%d CRWE=%d\n\n",
val, val & 1, (val >> 1) & 1, (val >> 15) & 1, (val >> 16) & 1, (val >> 17) & 1, (val >> 31) & 1);
/* Port status registers */
for (unsigned i = 0; i < OHCI_NDP_CFG(pThis); ++i)
{
val = pThis->RootHub.aPorts[i].fReg;
pHlp->pfnPrintf(pHlp, "HcRhPortStatus%02d: CCS=%d PES =%d PSS =%d POCI=%d PRS =%d PPS=%d LSDA=%d\n"
" %08x - CSC=%d PESC=%d PSSC=%d OCIC=%d PRSC=%d\n",
i, val & 1, (val >> 1) & 1, (val >> 2) & 1,(val >> 3) & 1, (val >> 4) & 1, (val >> 8) & 1, (val >> 9) & 1,
val, (val >> 16) & 1, (val >> 17) & 1, (val >> 18) & 1, (val >> 19) & 1, (val >> 20) & 1);
}
}
/**
* Relocate device instance data.
*
* @returns VBox status code.
* @param pDevIns The device instance data.
* @param offDelta The relocation delta.
*/
static DECLCALLBACK(void) ohciR3Relocate(PPDMDEVINS pDevIns, RTGCINTPTR offDelta)
{
RT_NOREF(offDelta);
POHCI pThis = PDMINS_2_DATA(pDevIns, POHCI);
pThis->pDevInsRC = PDMDEVINS_2_RCPTR(pDevIns);
pThis->pEndOfFrameTimerRC = TMTimerRCPtr(pThis->pEndOfFrameTimerR3);
}
/**
* Destruct a device instance.
*
* Most VM resources are freed by the VM. This callback is provided so that any non-VM
* resources can be freed correctly.
*
* @returns VBox status code.
* @param pDevIns The device instance data.
*/
static DECLCALLBACK(int) ohciR3Destruct(PPDMDEVINS pDevIns)
{
POHCI pThis = PDMINS_2_DATA(pDevIns, POHCI);
PDMDEV_CHECK_VERSIONS_RETURN_QUIET(pDevIns);
#ifdef VBOX_WITH_OHCI_PHYS_READ_CACHE
ohciR3PhysReadCacheFree(pThis->pCacheED);
pThis->pCacheED = NULL;
ohciR3PhysReadCacheFree(pThis->pCacheTD);
pThis->pCacheTD = NULL;
#endif
if (RTCritSectIsInitialized(&pThis->CritSect))
RTCritSectDelete(&pThis->CritSect);
PDMR3CritSectDelete(&pThis->CsIrq);
/*
* Tear down the per endpoint in-flight tracking...
*/
return VINF_SUCCESS;
}
/**
* @interface_method_impl{PDMDEVREG,pfnConstruct,OHCI constructor}
*/
static DECLCALLBACK(int) ohciR3Construct(PPDMDEVINS pDevIns, int iInstance, PCFGMNODE pCfg)
{
POHCI pThis = PDMINS_2_DATA(pDevIns, POHCI);
uint32_t cPorts;
PDMDEV_CHECK_VERSIONS_RETURN(pDevIns);
/*
* Init instance data.
*/
pThis->pDevInsR3 = pDevIns;
pThis->pDevInsR0 = PDMDEVINS_2_R0PTR(pDevIns);
pThis->pDevInsRC = PDMDEVINS_2_RCPTR(pDevIns);
PCIDevSetVendorId (&pThis->PciDev, 0x106b);
PCIDevSetDeviceId (&pThis->PciDev, 0x003f);
PCIDevSetClassProg (&pThis->PciDev, 0x10); /* OHCI */
PCIDevSetClassSub (&pThis->PciDev, 0x03);
PCIDevSetClassBase (&pThis->PciDev, 0x0c);
PCIDevSetInterruptPin (&pThis->PciDev, 0x01);
#ifdef VBOX_WITH_MSI_DEVICES
PCIDevSetStatus (&pThis->PciDev, VBOX_PCI_STATUS_CAP_LIST);
PCIDevSetCapabilityList(&pThis->PciDev, 0x80);
#endif
pThis->RootHub.pOhci = pThis;
pThis->RootHub.IBase.pfnQueryInterface = ohciR3RhQueryInterface;
pThis->RootHub.IRhPort.pfnGetAvailablePorts = ohciR3RhGetAvailablePorts;
pThis->RootHub.IRhPort.pfnGetUSBVersions = ohciR3RhGetUSBVersions;
pThis->RootHub.IRhPort.pfnAttach = ohciR3RhAttach;
pThis->RootHub.IRhPort.pfnDetach = ohciR3RhDetach;
pThis->RootHub.IRhPort.pfnReset = ohciR3RhReset;
pThis->RootHub.IRhPort.pfnXferCompletion = ohciR3RhXferCompletion;
pThis->RootHub.IRhPort.pfnXferError = ohciR3RhXferError;
pThis->RootHub.IRhPort.pfnStartFrame = ohciR3StartFrame;
pThis->RootHub.IRhPort.pfnFrameRateChanged = ohciR3FrameRateChanged;
/* USB LED */
pThis->RootHub.Led.u32Magic = PDMLED_MAGIC;
pThis->RootHub.ILeds.pfnQueryStatusLed = ohciR3RhQueryStatusLed;
/*
* Read configuration.
*/
PDMDEV_VALIDATE_CONFIG_RETURN(pDevIns, "RZEnabled", "");
int rc = CFGMR3QueryBoolDef(pCfg, "RZEnabled", &pThis->fRZEnabled, true);
AssertLogRelRCReturn(rc, rc);
/* Number of ports option. */
rc = CFGMR3QueryU32Def(pCfg, "Ports", &cPorts, OHCI_NDP_DEFAULT);
if (RT_FAILURE(rc))
return PDMDEV_SET_ERROR(pDevIns, rc,
N_("OHCI configuration error: failed to read Ports as integer"));
if (cPorts == 0 || cPorts > OHCI_NDP_MAX)
return PDMDevHlpVMSetError(pDevIns, VERR_INVALID_PARAMETER, RT_SRC_POS,
N_("OHCI configuration error: Ports must be in range [%u,%u]"),
1, OHCI_NDP_MAX);
/* Store the configured NDP; it will be used everywhere else from now on. */
pThis->RootHub.desc_a = cPorts;
/*
* Register PCI device and I/O region.
*/
rc = PDMDevHlpPCIRegister(pDevIns, &pThis->PciDev);
if (RT_FAILURE(rc))
return rc;
#ifdef VBOX_WITH_MSI_DEVICES
PDMMSIREG MsiReg;
RT_ZERO(MsiReg);
MsiReg.cMsiVectors = 1;
MsiReg.iMsiCapOffset = 0x80;
MsiReg.iMsiNextOffset = 0x00;
rc = PDMDevHlpPCIRegisterMsi(pDevIns, &MsiReg);
if (RT_FAILURE(rc))
{
PCIDevSetCapabilityList(&pThis->PciDev, 0x0);
/* That's OK, we can work without MSI */
}
#endif
rc = PDMDevHlpPCIIORegionRegister(pDevIns, 0, 4096, PCI_ADDRESS_SPACE_MEM, ohciR3Map);
if (RT_FAILURE(rc))
return rc;
/*
* Create the end-of-frame timer.
*/
rc = PDMDevHlpTMTimerCreate(pDevIns, TMCLOCK_VIRTUAL, ohciR3FrameBoundaryTimer, pThis,
TMTIMER_FLAGS_DEFAULT_CRIT_SECT, "USB Frame Timer",
&pThis->pEndOfFrameTimerR3);
if (RT_FAILURE(rc))
return rc;
pThis->pEndOfFrameTimerR0 = TMTimerR0Ptr(pThis->pEndOfFrameTimerR3);
pThis->pEndOfFrameTimerRC = TMTimerRCPtr(pThis->pEndOfFrameTimerR3);
/*
* Register the saved state data unit.
*/
rc = PDMDevHlpSSMRegisterEx(pDevIns, OHCI_SAVED_STATE_VERSION, sizeof(*pThis), NULL,
NULL, NULL, NULL,
ohciR3SavePrep, ohciR3SaveExec, ohciR3SaveDone,
ohciR3LoadPrep, ohciR3LoadExec, ohciR3LoadDone);
if (RT_FAILURE(rc))
return rc;
/*
* Attach to the VBox USB RootHub Driver on LUN #0.
*/
rc = PDMDevHlpDriverAttach(pDevIns, 0, &pThis->RootHub.IBase, &pThis->RootHub.pIBase, "RootHub");
if (RT_FAILURE(rc))
{
AssertMsgFailed(("Configuration error: No roothub driver attached to LUN #0!\n"));
return rc;
}
pThis->RootHub.pIRhConn = PDMIBASE_QUERY_INTERFACE(pThis->RootHub.pIBase, VUSBIROOTHUBCONNECTOR);
AssertMsgReturn(pThis->RootHub.pIRhConn,
("Configuration error: The driver doesn't provide the VUSBIROOTHUBCONNECTOR interface!\n"),
VERR_PDM_MISSING_INTERFACE);
pThis->RootHub.pIDev = PDMIBASE_QUERY_INTERFACE(pThis->RootHub.pIBase, VUSBIDEVICE);
AssertMsgReturn(pThis->RootHub.pIDev,
("Configuration error: The driver doesn't provide the VUSBIDEVICE interface!\n"),
VERR_PDM_MISSING_INTERFACE);
/*
* Attach status driver (optional).
*/
PPDMIBASE pBase;
rc = PDMDevHlpDriverAttach(pDevIns, PDM_STATUS_LUN, &pThis->RootHub.IBase, &pBase, "Status Port");
if (RT_SUCCESS(rc))
pThis->RootHub.pLedsConnector = PDMIBASE_QUERY_INTERFACE(pBase, PDMILEDCONNECTORS);
else if (rc != VERR_PDM_NO_ATTACHED_DRIVER)
{
AssertMsgFailed(("Failed to attach to status driver. rc=%Rrc\n", rc));
return rc;
}
/* Set URB parameters. */
rc = VUSBIRhSetUrbParams(pThis->RootHub.pIRhConn, sizeof(VUSBURBHCIINT), sizeof(VUSBURBHCITDINT));
if (RT_FAILURE(rc))
return PDMDevHlpVMSetError(pDevIns, rc, RT_SRC_POS,
N_("OHCI: Failed to set URB parameters"));
/*
* Calculate the timer intervals.
* This assumes that the VM timer doesn't change frequency during the run.
*/
pThis->u64TimerHz = TMTimerGetFreq(pThis->CTX_SUFF(pEndOfFrameTimer));
rc = PDMDevHlpCritSectInit(pDevIns, &pThis->CsIrq, RT_SRC_POS, "OHCI#%uIrq", iInstance);
if (RT_FAILURE(rc))
return PDMDevHlpVMSetError(pDevIns, rc, RT_SRC_POS,
N_("OHCI: Failed to create critical section"));
rc = RTCritSectInit(&pThis->CritSect);
if (RT_FAILURE(rc))
return PDMDevHlpVMSetError(pDevIns, rc, RT_SRC_POS,
N_("OHCI: Failed to create critical section"));
#ifdef VBOX_WITH_OHCI_PHYS_READ_CACHE
pThis->pCacheED = ohciR3PhysReadCacheAlloc();
pThis->pCacheTD = ohciR3PhysReadCacheAlloc();
if (pThis->pCacheED == NULL || pThis->pCacheTD == NULL)
return PDMDevHlpVMSetError(pDevIns, VERR_NO_MEMORY, RT_SRC_POS,
N_("OHCI: Failed to allocate PhysRead cache"));
#endif
/*
* Do a hardware reset.
*/
ohciR3DoReset(pThis, OHCI_USB_RESET, false /* don't reset devices */);
#ifdef VBOX_WITH_STATISTICS
/*
* Register statistics.
*/
PDMDevHlpSTAMRegister(pDevIns, &pThis->StatCanceledIsocUrbs, STAMTYPE_COUNTER, "/Devices/OHCI/CanceledIsocUrbs", STAMUNIT_OCCURENCES, "Detected canceled isochronous URBs.");
PDMDevHlpSTAMRegister(pDevIns, &pThis->StatCanceledGenUrbs, STAMTYPE_COUNTER, "/Devices/OHCI/CanceledGenUrbs", STAMUNIT_OCCURENCES, "Detected canceled general URBs.");
PDMDevHlpSTAMRegister(pDevIns, &pThis->StatDroppedUrbs, STAMTYPE_COUNTER, "/Devices/OHCI/DroppedUrbs", STAMUNIT_OCCURENCES, "Dropped URBs (endpoint halted, or URB canceled).");
#endif
/*
* Register debugger info callbacks.
*/
PDMDevHlpDBGFInfoRegister(pDevIns, "ohci", "OHCI control registers.", ohciR3InfoRegs);
#if 0/*def DEBUG_bird*/
// g_fLogInterruptEPs = true;
g_fLogControlEPs = true;
g_fLogBulkEPs = true;
#endif
return VINF_SUCCESS;
}
const PDMDEVREG g_DeviceOHCI =
{
/* u32version */
PDM_DEVREG_VERSION,
/* szName */
"usb-ohci",
/* szRCMod */
"VBoxDDRC.rc",
/* szR0Mod */
"VBoxDDR0.r0",
/* pszDescription */
"OHCI USB controller.\n",
/* fFlags */
PDM_DEVREG_FLAGS_DEFAULT_BITS | PDM_DEVREG_FLAGS_RC | PDM_DEVREG_FLAGS_R0,
/* fClass */
PDM_DEVREG_CLASS_BUS_USB,
/* cMaxInstances */
~0U,
/* cbInstance */
sizeof(OHCI),
/* pfnConstruct */
ohciR3Construct,
/* pfnDestruct */
ohciR3Destruct,
/* pfnRelocate */
ohciR3Relocate,
/* pfnMemSetup */
NULL,
/* pfnPowerOn */
NULL,
/* pfnReset */
ohciR3Reset,
/* pfnSuspend */
NULL,
/* pfnResume */
ohciR3Resume,
/* pfnAttach */
NULL,
/* pfnDetach */
NULL,
/* pfnQueryInterface */
NULL,
/* pfnInitComplete */
NULL,
/* pfnPowerOff */
NULL,
/* pfnSoftReset */
NULL,
/* u32VersionEnd */
PDM_DEVREG_VERSION
};
#endif /* IN_RING3 */
#endif /* !VBOX_DEVICE_STRUCT_TESTCASE */
|