summaryrefslogtreecommitdiffstats
path: root/cfe/cfe/usb/ohci.c
blob: c002091f136b6b601e5acd5216d5bfa54730e3f1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
/*  *********************************************************************
    *  Broadcom Common Firmware Environment (CFE)
    *  
    *  OHCI device driver			File: ohci.c
    *  
    *  Open Host Controller Interface low-level routines
    *  
    *  Author:  Mitch Lichtenberg (mpl@broadcom.com)
    *  
    *********************************************************************  
    *
    *  Copyright 2000,2001,2002,2003
    *  Broadcom Corporation. All rights reserved.
    *  
    *  This software is furnished under license and may be used and 
    *  copied only in accordance with the following terms and 
    *  conditions.  Subject to these conditions, you may download, 
    *  copy, install, use, modify and distribute modified or unmodified 
    *  copies of this software in source and/or binary form.  No title 
    *  or ownership is transferred hereby.
    *  
    *  1) Any source code used, modified or distributed must reproduce 
    *     and retain this copyright notice and list of conditions 
    *     as they appear in the source file.
    *  
    *  2) No right is granted to use any trade name, trademark, or 
    *     logo of Broadcom Corporation.  The "Broadcom Corporation" 
    *     name may not be used to endorse or promote products derived 
    *     from this software without the prior written permission of 
    *     Broadcom Corporation.
    *  
    *  3) THIS SOFTWARE IS PROVIDED "AS-IS" AND ANY EXPRESS OR
    *     IMPLIED WARRANTIES, INCLUDING BUT NOT LIMITED TO, ANY IMPLIED
    *     WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR 
    *     PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED. IN NO EVENT 
    *     SHALL BROADCOM BE LIABLE FOR ANY DAMAGES WHATSOEVER, AND IN 
    *     PARTICULAR, BROADCOM SHALL NOT BE LIABLE FOR DIRECT, INDIRECT,
    *     INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 
    *     (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
    *     GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
    *     BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY 
    *     OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR 
    *     TORT (INCLUDING NEGLIGENCE OR OTHERWISE), EVEN IF ADVISED OF 
    *     THE POSSIBILITY OF SUCH DAMAGE.
    ********************************************************************* */


#ifndef _CFE_
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <stdint.h>
#include "usbhack.h"
#define CPUCFG_COHERENT_DMA 1	/* hack runs on a PC, PCs are coherent */
#else
#include "lib_types.h"
#include "lib_printf.h"
#include "lib_string.h"
#include "lib_physio.h"
#include "addrspace.h"
#include "cpu_config.h"		/* for CPUCFG_COHERENT_DMA */
#endif

#include "lib_malloc.h"
#include "lib_queue.h"
#include "usbchap9.h"
#include "usbd.h"
#include "ohci.h"


/*  *********************************************************************
    *  Macros for dealing with hardware
    *
    *  This is all yucky stuff that needs to be made more
    *  processor-independent.  It's mostly here now to help us with
    *  our test harness.
    ********************************************************************* */

#if defined(_CFE_) && defined(__MIPSEB)
#define BSWAP32(x) __swap32(x)
static inline uint32_t __swap32(uint32_t x)
{
    uint32_t y;

    y = ((x & 0xFF) << 24) |
	((x & 0xFF00) << 8) |
	((x & 0xFF0000) >> 8) |
	((x & 0xFF000000) >> 24);

    return y;
}
#else
#define BSWAP32(x) (x)
#endif


#ifndef _CFE_
extern uint32_t vtop(void *ptr);
extern void *ptov(uint32_t x);
#define OHCI_VTOP(ptr) vtop(ptr)
#define OHCI_PTOV(ptr) ptov(ptr)
#define OHCI_WRITECSR(softc,x,y) \
    *((volatile uint32_t *) ((softc)->ohci_regs + ((x)/sizeof(uint32_t)))) = (y)
#define OHCI_READCSR(softc,x) \
    *((volatile uint32_t *) ((softc)->ohci_regs + ((x)/sizeof(uint32_t))))
#else
#define OHCI_VTOP(ptr) ((uint32_t)PHYSADDR((long)(ptr)))

#if CPUCFG_COHERENT_DMA
#define OHCI_PTOV(ptr) ((void *)(KERNADDR(ptr)))
#else
#define OHCI_PTOV(ptr) ((void *)(UNCADDR(ptr)))
#endif

#define OHCI_WRITECSR(softc,x,y) \
    phys_write32(((softc)->ohci_regs + (x)),(y))
#define OHCI_READCSR(softc,x) \
    phys_read32(((softc)->ohci_regs + (x)))
#endif

#if CPUCFG_COHERENT_DMA
#define OHCI_INVAL_RANGE(s,l)
#define OHCI_FLUSH_RANGE(s,l)
#else	 /* not coherent */
#define CFE_CACHE_INVAL_RANGE	32	/* XXX belongs in include file */
#define CFE_CACHE_FLUSH_RANGE	64
extern void _cfe_flushcache(int,uint8_t *,uint8_t *);
#define OHCI_INVAL_RANGE(s,l) _cfe_flushcache(CFE_CACHE_INVAL_RANGE,((uint8_t *) (s)),((uint8_t *) (s))+(l))
#define OHCI_FLUSH_RANGE(s,l) _cfe_flushcache(CFE_CACHE_FLUSH_RANGE,((uint8_t *) (s)),((uint8_t *) (s))+(l))
#endif


/*  *********************************************************************
    *  Bit-reverse table - this table consists of the numbers
    *  at its index, listed in reverse.  So, the reverse of 0000 0010
    *  is 0100 0000.
    ********************************************************************* */

const static int ohci_revbits[OHCI_INTTABLE_SIZE] = {
    0x00, 0x10, 0x08, 0x18, 0x04, 0x14, 0x0c, 0x1c,
    0x02, 0x12, 0x0a, 0x1a, 0x06, 0x16, 0x0e, 0x1e,
    0x01, 0x11, 0x09, 0x19, 0x05, 0x15, 0x0d, 0x1d,
    0x03, 0x13, 0x0b, 0x1b, 0x07, 0x17, 0x0f, 0x1f
};


/*  *********************************************************************
    *  Macros to convert from "hardware" endpoint and transfer
    *  descriptors (ohci_ed_t, ohci_td_t) to "software"
    *  data structures (ohci_transfer_t, ohci_endpoint_t).  
    *  
    *  Basically, there are two tables, indexed by the same value
    *  By subtracting the base of one pool from a pointer, we get
    *  the index into the other table.  
    *  
    *  We *could* have included the ed and td in the software
    *  data structures, but placing all the hardware stuff in one
    *  pool will make it easier for hardware that does not handle
    *  coherent DMA, since we can be less careful about what we flush
    *  and what we invalidate.
    ********************************************************************* */

#define ohci_td_from_transfer(softc,transfer) \
     ((softc)->ohci_hwtdpool + ((transfer) - (softc)->ohci_transfer_pool))

#define ohci_transfer_from_td(softc,td) \
     ((softc)->ohci_transfer_pool + ((td) - (softc)->ohci_hwtdpool))

#define ohci_ed_from_endpoint(softc,endpoint) \
     ((softc)->ohci_hwedpool + ((endpoint) - (softc)->ohci_endpoint_pool))

#define ohci_endpoint_from_ed(softc,ed) \
     ((softc)->ohci_endpoint_pool + ((ed) - (softc)->ohci_hwedpool))

/*  *********************************************************************
    *  Forward declarations
    ********************************************************************* */

static int ohci_roothub_xfer(usbbus_t *bus,usb_ept_t *uept,usbreq_t *ur);
static void ohci_roothub_statchg(ohci_softc_t *softc);
extern usb_hcdrv_t ohci_driver;

/*  *********************************************************************
    *  Globals
    ********************************************************************* */

int ohcidebug = 0;
void ohci_dumprhstat(uint32_t reg);
void ohci_dumpportstat(int idx,uint32_t reg);
void ohci_dumptd(ohci_td_t *td);
void ohci_dumptdchain(ohci_td_t *td);
void ohci_dumped(ohci_ed_t *ed);
void ohci_dumpedchain(ohci_ed_t *ed);


/*  *********************************************************************
    *  Some debug routines
    ********************************************************************* */

#if 1
void ohci_dumprhstat(uint32_t reg)
{
    printf("HubStatus: %08X  ",reg);

    if (reg & M_OHCI_RHSTATUS_LPS) printf("LocalPowerStatus ");
    if (reg & M_OHCI_RHSTATUS_OCI) printf("OverCurrent ");
    if (reg & M_OHCI_RHSTATUS_DRWE) printf("DeviceRemoteWakeupEnable ");
    if (reg & M_OHCI_RHSTATUS_LPSC) printf("LocalPowerStatusChange ");
    if (reg & M_OHCI_RHSTATUS_OCIC) printf("OverCurrentIndicatorChange ");
    printf("\n");

}

void ohci_dumpportstat(int idx,uint32_t reg)
{
    printf("Port %d: %08X  ",idx,reg);
    if (reg & M_OHCI_RHPORTSTAT_CCS) printf("Connected ");
    if (reg & M_OHCI_RHPORTSTAT_PES) printf("PortEnabled ");
    if (reg & M_OHCI_RHPORTSTAT_PSS) printf("PortSuspended ");
    if (reg & M_OHCI_RHPORTSTAT_POCI) printf("PortOverCurrent ");
    if (reg & M_OHCI_RHPORTSTAT_PRS) printf("PortReset ");
    if (reg & M_OHCI_RHPORTSTAT_PPS) printf("PortPowered ");
    if (reg & M_OHCI_RHPORTSTAT_LSDA) printf("LowSpeed ");
    if (reg & M_OHCI_RHPORTSTAT_CSC) printf("ConnectStatusChange ");
    if (reg & M_OHCI_RHPORTSTAT_PESC) printf("PortEnableStatusChange ");
    if (reg & M_OHCI_RHPORTSTAT_PSSC) printf("PortSuspendStatusChange ");
    if (reg & M_OHCI_RHPORTSTAT_OCIC) printf("OverCurrentIndicatorChange ");
    if (reg & M_OHCI_RHPORTSTAT_PRSC) printf("PortResetStatusChange ");
    printf("\n");
}

void ohci_dumptd(ohci_td_t *td)
{
    uint32_t ctl;
    static char *pids[4] = {"SETUP","OUT","IN","RSVD"};

    ctl = BSWAP32(td->td_control);

    printf("[%08X] ctl=%08X (DP=%s,DI=%d,T=%d,EC=%d,CC=%d%s) cbp=%08X be=%08X next=%08X\n",
	   OHCI_VTOP(td),
	   ctl,
	   pids[G_OHCI_TD_PID(ctl)],
	   G_OHCI_TD_DI(ctl),
	   G_OHCI_TD_DT(ctl),
	   G_OHCI_TD_EC(ctl),
	   G_OHCI_TD_CC(ctl),
	   (ctl & M_OHCI_TD_SHORTOK) ? ",R" : "",
	   BSWAP32(td->td_cbp),
	   BSWAP32(td->td_be),
	   BSWAP32(td->td_next_td));
}

void ohci_dumptdchain(ohci_td_t *td)
{
    int idx = 0;
    for (;;) {
	printf("%d:[%08X] ctl=%08X cbp=%08X be=%08X next=%08X\n",
	       idx,
	   OHCI_VTOP(td),
	   BSWAP32(td->td_control),
	   BSWAP32(td->td_cbp),
	   BSWAP32(td->td_be),
	   BSWAP32(td->td_next_td));
	if (!td->td_next_td) break;
	td = (ohci_td_t *) OHCI_PTOV(BSWAP32(td->td_next_td));
	idx++;
	}
}

void ohci_dumped(ohci_ed_t *ed)
{
    uint32_t ctl;
    static char *pids[4] = {"FTD","OUT","IN","FTD"};

    ctl = BSWAP32(ed->ed_control),

    printf("[%08X] Ctl=%08X (MPS=%d%s%s%s,EN=%d,FA=%d,D=%s) Tailp=%08X headp=%08X next=%08X %s\n",
	   OHCI_VTOP(ed),
	   ctl,
	   G_OHCI_ED_MPS(ctl),
	   (ctl & M_OHCI_ED_LOWSPEED) ? ",LS" : "",
	   (ctl & M_OHCI_ED_SKIP) ? ",SKIP" : "",
	   (ctl & M_OHCI_ED_ISOCFMT) ? ",ISOC" : "",
	   G_OHCI_ED_EN(ctl),
	   G_OHCI_ED_FA(ctl),
	   pids[G_OHCI_ED_DIR(ctl)],
	   BSWAP32(ed->ed_tailp),
	   BSWAP32(ed->ed_headp),
	   BSWAP32(ed->ed_next_ed),
	   BSWAP32(ed->ed_headp) & M_OHCI_ED_HALT ? "HALT" : "");
    if ((ed->ed_headp & M_OHCI_ED_PTRMASK) == 0) return;
    ohci_dumptdchain(OHCI_PTOV(BSWAP32(ed->ed_headp) & M_OHCI_ED_PTRMASK));
}

void ohci_dumpedchain(ohci_ed_t *ed)
{
    int idx = 0;
    for (;;) {
	printf("---\nED#%d -> ",idx);
	ohci_dumped(ed);
	if (!ed->ed_next_ed) break;
	if (idx > 50) break;
	ed = (ohci_ed_t *) OHCI_PTOV(BSWAP32(ed->ed_next_ed));
	idx++;
	}
}

#endif


static void eptstats(ohci_softc_t *softc)
{
    int cnt;
    ohci_endpoint_t *e;
    cnt = 0; 

    e = softc->ohci_endpoint_freelist;
    while (e) { e = e->ep_next; cnt++; }
    printf("%d left, %d inuse\n",cnt,OHCI_EDPOOL_SIZE-cnt);
}

/*  *********************************************************************
    *  _ohci_allocept(softc)
    *  
    *  Allocate an endpoint data structure from the pool, and 
    *  make it ready for use.  The endpoint is NOT attached to 
    *  the hardware at this time.
    *  
    *  Input parameters: 
    *  	   softc - our OHCI controller
    *  	   
    *  Return value:
    *  	   pointer to endpoint or NULL
    ********************************************************************* */

static ohci_endpoint_t *_ohci_allocept(ohci_softc_t *softc)
{
    ohci_endpoint_t *e;
    ohci_ed_t *ed;

    if (ohcidebug > 2) {
	printf("AllocEpt: ");eptstats(softc);
	}

    e = softc->ohci_endpoint_freelist;

    if (!e) {
	printf("No endpoints left!\n");
	return NULL;
	}

    softc->ohci_endpoint_freelist = e->ep_next;

    ed = ohci_ed_from_endpoint(softc,e);

    ed->ed_control = BSWAP32(M_OHCI_ED_SKIP);
    ed->ed_tailp   = BSWAP32(0);
    ed->ed_headp   = BSWAP32(0);
    ed->ed_next_ed = BSWAP32(0);

    e->ep_phys = OHCI_VTOP(ed);
    e->ep_next = NULL;

    return e;
}

/*  *********************************************************************
    *  _ohci_allocxfer(softc)
    *  
    *  Allocate a transfer descriptor.  It is prepared for use
    *  but not attached to the hardware.
    *  
    *  Input parameters: 
    *  	   softc - our OHCI controller
    *  	   
    *  Return value:
    *  	   transfer descriptor, or NULL
    ********************************************************************* */

static ohci_transfer_t *_ohci_allocxfer(ohci_softc_t *softc)
{
    ohci_transfer_t *t;
    ohci_td_t *td;

    if (ohcidebug > 2) {
	int cnt;
	cnt = 0; 
	t = softc->ohci_transfer_freelist;
	while (t) { t = t->t_next; cnt++; }
	printf("AllocXfer: %d left, %d inuse\n",cnt,OHCI_TDPOOL_SIZE-cnt);
	}

    t = softc->ohci_transfer_freelist;

    if (!t) {
	printf("No more transfer descriptors!\n");
	return NULL;
	}

    softc->ohci_transfer_freelist = t->t_next;

    td = ohci_td_from_transfer(softc,t);

    td->td_control = BSWAP32(0);
    td->td_cbp     = BSWAP32(0);
    td->td_next_td = BSWAP32(0);
    td->td_be      = BSWAP32(0);

    t->t_ref  = NULL;
    t->t_next = NULL;

    return t;
}

/*  *********************************************************************
    *  _ohci_freeept(softc,e)
    *  
    *  Free an endpoint, returning it to the pool.
    *  
    *  Input parameters: 
    *  	   softc - our OHCI controller
    *  	   e - endpoint descriptor to return
    *  	   
    *  Return value:
    *  	   nothing
    ********************************************************************* */

static void _ohci_freeept(ohci_softc_t *softc,ohci_endpoint_t *e)
{
    if (ohcidebug > 2) {
	int cnt;
	ohci_endpoint_t *ee;
	cnt = 0; 
	ee = softc->ohci_endpoint_freelist;
	while (ee) { ee = ee->ep_next; cnt++; }
	printf("FreeEpt[%p]: %d left, %d inuse\n",e,cnt,OHCI_EDPOOL_SIZE-cnt);
	}

    e->ep_next = softc->ohci_endpoint_freelist;
    softc->ohci_endpoint_freelist = e;
}

/*  *********************************************************************
    *  _ohci_freexfer(softc,t)
    *  
    *  Free a transfer descriptor, returning it to the pool.
    *  
    *  Input parameters: 
    *  	   softc - our OHCI controller
    *  	   t - transfer descriptor to return
    *  	   
    *  Return value:
    *  	   nothing
    ********************************************************************* */

static void _ohci_freexfer(ohci_softc_t *softc,ohci_transfer_t *t)
{
    t->t_next = softc->ohci_transfer_freelist;
    softc->ohci_transfer_freelist = t;
}

/*  *********************************************************************
    *  _ohci_initpools(softc)
    *  
    *  Allocate and initialize the various pools of things that
    *  we use in the OHCI driver.  We do this by allocating some
    *  big chunks from the heap and carving them up.
    *  
    *  Input parameters: 
    *  	   softc - our OHCI controller
    *  	   
    *  Return value:
    *  	   0 if ok
    *  	   else error code
    ********************************************************************* */

static int _ohci_initpools(ohci_softc_t *softc)
{
    int idx;

    /*
     * Do the transfer descriptor pool
     */

    softc->ohci_transfer_pool = KMALLOC(OHCI_TDPOOL_SIZE*sizeof(ohci_transfer_t),0);
    softc->ohci_hwtdpool = KMALLOC(OHCI_TDPOOL_SIZE*sizeof(ohci_td_t),OHCI_TD_ALIGN);

    /*
     * In the case of noncoherent DMA, make these uncached addresses.
     * This way all our descriptors will be uncached.  Makes life easier, as we
     * do not need to worry about flushing descriptors, etc.
     */

#if (!CPUCFG_COHERENT_DMA)
    softc->ohci_hwtdpool = (void *) UNCADDR(PHYSADDR((uint32_t)(softc->ohci_hwtdpool)));
#endif

    if (!softc->ohci_transfer_pool || !softc->ohci_hwtdpool) {
	printf("Could not allocate transfer descriptors\n");
	return -1;
	}

    softc->ohci_transfer_freelist = NULL;

    for (idx = 0; idx < OHCI_TDPOOL_SIZE; idx++) {
	_ohci_freexfer(softc,softc->ohci_transfer_pool+idx);
	}

    /*
     * Do the endpoint descriptor pool
     */
	 
    softc->ohci_endpoint_pool = KMALLOC(OHCI_EDPOOL_SIZE*sizeof(ohci_endpoint_t),0);

    softc->ohci_hwedpool       = KMALLOC(OHCI_EDPOOL_SIZE*sizeof(ohci_ed_t),OHCI_ED_ALIGN);

#if (!CPUCFG_COHERENT_DMA)
    softc->ohci_hwedpool = (void *) UNCADDR(PHYSADDR((uint32_t)(softc->ohci_hwedpool)));
#endif

    if (!softc->ohci_endpoint_pool || !softc->ohci_hwedpool) {
	printf("Could not allocate transfer descriptors\n");
	return -1;
	}

    softc->ohci_endpoint_freelist = NULL;

    for (idx = 0; idx < OHCI_EDPOOL_SIZE; idx++) {
	_ohci_freeept(softc,softc->ohci_endpoint_pool+idx);
	}

    /*
     * Finally the host communications area
     */

    softc->ohci_hcca = KMALLOC(sizeof(ohci_hcca_t),sizeof(ohci_hcca_t));

#if (!CPUCFG_COHERENT_DMA)
    softc->ohci_hcca = (void *) UNCADDR(PHYSADDR((uint32_t)(softc->ohci_hcca)));
#endif

    memset(softc->ohci_hcca,0,sizeof(ohci_hcca_t));

    return 0;
}


/*  *********************************************************************
    *  ohci_start(bus)
    *  
    *  Start the OHCI controller.  After this routine is called,
    *  the hardware will be operational and ready to accept
    *  descriptors and interrupt calls.
    *  
    *  Input parameters: 
    *  	   bus - bus structure, from ohci_create
    *  	   
    *  Return value:
    *  	   0 if ok
    *  	   else error code
    ********************************************************************* */

static int ohci_start(usbbus_t *bus)
{
    ohci_softc_t *softc = (ohci_softc_t *) bus->ub_hwsoftc;
    uint32_t frameint;
    uint32_t reg;
    int idx;

    /*
     * Force a reset to the controller, followed by a short delay
     */

    OHCI_WRITECSR(softc,R_OHCI_CONTROL,V_OHCI_CONTROL_HCFS(K_OHCI_HCFS_RESET));
    usb_delay_ms(bus,OHCI_RESET_DELAY);

    /* Host controller state is now "RESET" */

    /*
     * We need the frame interval later, so get a copy of it now.
     */
    frameint = G_OHCI_FMINTERVAL_FI(OHCI_READCSR(softc,R_OHCI_FMINTERVAL));
    
    /*
     * Reset the host controller.  When you set the HCR bit
     * if self-clears when the reset is complete.
     */

    OHCI_WRITECSR(softc,R_OHCI_CMDSTATUS,M_OHCI_CMDSTATUS_HCR);
    for (idx = 0; idx < 10000; idx++) {
	if (!(OHCI_READCSR(softc,R_OHCI_CMDSTATUS) & M_OHCI_CMDSTATUS_HCR)) break;
	}

    if (OHCI_READCSR(softc,R_OHCI_CMDSTATUS) & M_OHCI_CMDSTATUS_HCR) {
	/* controller never came out of reset */
	return -1;
	}

    /* 
     * Host controller state is now "SUSPEND".  We must exit
     * from this state within 2ms.  (5.1.1.4)
     * 
     * Set up pointers to data structures.
     */

    OHCI_WRITECSR(softc,R_OHCI_HCCA,OHCI_VTOP(softc->ohci_hcca));
    OHCI_WRITECSR(softc,R_OHCI_CONTROLHEADED,softc->ohci_ctl_list->ep_phys);
    OHCI_WRITECSR(softc,R_OHCI_BULKHEADED,softc->ohci_bulk_list->ep_phys);

    /*
     * Our driver is polled, turn off interrupts
     */

    OHCI_WRITECSR(softc,R_OHCI_INTDISABLE,M_OHCI_INT_ALL);

    /*
     * Set up the control register.
     */

    reg = OHCI_READCSR(softc,R_OHCI_CONTROL);

    reg = M_OHCI_CONTROL_PLE | M_OHCI_CONTROL_CLE | M_OHCI_CONTROL_BLE |
	M_OHCI_CONTROL_IE |
	V_OHCI_CONTROL_CBSR(K_OHCI_CBSR_41) | 
	V_OHCI_CONTROL_HCFS(K_OHCI_HCFS_OPERATIONAL);

    OHCI_WRITECSR(softc,R_OHCI_CONTROL,reg);


    /* 
     * controller state is now OPERATIONAL
     */

    reg = OHCI_READCSR(softc,R_OHCI_FMINTERVAL);
    reg &= M_OHCI_FMINTERVAL_FIT;
    reg ^= M_OHCI_FMINTERVAL_FIT;
    reg |= V_OHCI_FMINTERVAL_FSMPS(OHCI_CALC_FSMPS(frameint)) |
	V_OHCI_FMINTERVAL_FI(frameint);
    OHCI_WRITECSR(softc,R_OHCI_FMINTERVAL,reg);

    reg = frameint * 9 / 10;		/* calculate 90% */
    OHCI_WRITECSR(softc,R_OHCI_PERIODICSTART,reg);

    usb_delay_ms(softc->ohci_bus,10);

    /*
     * Remember how many ports we have
     */

    reg = OHCI_READCSR(softc,R_OHCI_RHDSCRA);
    softc->ohci_ndp = G_OHCI_RHDSCRA_NDP(reg);


    /*
     * Enable port power
     */

    OHCI_WRITECSR(softc,R_OHCI_RHSTATUS,M_OHCI_RHSTATUS_LPSC);
    usb_delay_ms(softc->ohci_bus,10);

    return 0;
}


/*  *********************************************************************
    *  _ohci_setupepts(softc)
    *  
    *  Set up the endpoint tree, as described in the OHCI manual.
    *  Basically the hardware knows how to scan lists of lists,
    *  so we build a tree where each level is pointed to by two
    *  parent nodes.  We can choose our scanning rate by attaching
    *  endpoints anywhere within this tree.
    *  
    *  Input parameters: 
    *  	   softc - our OHCI controller
    *  	   
    *  Return value:
    *  	   0 if ok
    *  	   else error (out of descriptors)
    ********************************************************************* */

static int _ohci_setupepts(ohci_softc_t *softc)
{
    int idx;
    ohci_endpoint_t *e;
    ohci_ed_t *ed;
    ohci_endpoint_t *child;

    /*
     * Set up the list heads for the isochronous, control,
     * and bulk transfer lists.  They don't get the same "tree"
     * treatment that the interrupt devices get.
     * 
     * For the purposes of CFE, it's probably not necessary
     * to be this fancy.  The only device we're planning to
     * talk to is the keyboard and some hubs, which should 
     * have pretty minimal requirements.  It's conceivable
     * that this firmware may find a new home in other
     * devices, so we'll meet halfway and do some things
     * "fancy."
     */

    softc->ohci_isoc_list = _ohci_allocept(softc);
    softc->ohci_ctl_list  = _ohci_allocept(softc);
    softc->ohci_bulk_list = _ohci_allocept(softc);

    /*
     * Set up a tree of empty endpoint descriptors.  This is
     * tree is scanned by the hardware from the leaves up to
     * the root.  Once a millisecond, the hardware picks the
     * next leaf and starts scanning descriptors looking
     * for something to do.  It traverses all of the endpoints
     * along the way until it gets to the root.  
     * 
     * The idea here is if you put a transfer descriptor on the
     * root node, the hardware will see it every millisecond, 
     * since the root will be examined each time.  If you 
     * put the TD on the leaf, it will be 1/32 millisecond.
     * The tree therefore is six levels deep.
     */

    for (idx = 0; idx < OHCI_INTTREE_SIZE; idx++) {
	e = _ohci_allocept(softc);		/* allocated with sKip bit set */
	softc->ohci_edtable[idx] = e;
	child = (idx == 0) ? softc->ohci_isoc_list : softc->ohci_edtable[(idx-1)/2];
	ed = ohci_ed_from_endpoint(softc,e);
	ed->ed_next_ed = BSWAP32(child->ep_phys);
	e->ep_next = child;
	}

    /*
     * We maintain both physical and virtual copies of the interrupt
     * table (leaves of the tree).
     */

    for (idx = 0; idx < OHCI_INTTABLE_SIZE; idx++) {
	child = softc->ohci_edtable[OHCI_INTTREE_SIZE-OHCI_INTTABLE_SIZE+idx];
	softc->ohci_inttable[ohci_revbits[idx]] = child;
	softc->ohci_hcca->hcca_inttable[ohci_revbits[idx]] = BSWAP32(child->ep_phys);
	}

    /*
     * Okay, at this point the tree is built.
     */
    return 0;
}

/*  *********************************************************************
    *  ohci_stop(bus)
    *  
    *  Stop the OHCI hardware.
    *  
    *  Input parameters: 
    *  	   bus - our bus structure
    *  	   
    *  Return value:
    *  	   nothing
    ********************************************************************* */

static void ohci_stop(usbbus_t *bus)
{
    ohci_softc_t *softc = (ohci_softc_t *) bus->ub_hwsoftc;

    OHCI_WRITECSR(softc,R_OHCI_CONTROL,V_OHCI_CONTROL_HCFS(K_OHCI_HCFS_RESET));
}


/*  *********************************************************************
    *  _ohci_queueept(softc,queue,e)
    *  
    *  Add an endpoint to a list of endpoints.  This routine
    *  does things in a particular way according to the OHCI
    *  spec so we can add endpoints while the hardware is running.
    *  
    *  Input parameters: 
    *  	   queue - endpoint descriptor for head of queue
    *  	   e - endpoint to add to queue
    *  	   
    *  Return value:
    *  	   nothing
    ********************************************************************* */

static void _ohci_queueept(ohci_softc_t *softc,ohci_endpoint_t *queue,ohci_endpoint_t *newept)
{
    ohci_ed_t *qed;
    ohci_ed_t *newed;

    qed = ohci_ed_from_endpoint(softc,queue);
    newed = ohci_ed_from_endpoint(softc,newept);

    newept->ep_next = queue->ep_next;
    newed->ed_next_ed = qed->ed_next_ed;

    queue->ep_next = newept;
    qed->ed_next_ed = BSWAP32(newept->ep_phys);

    if (ohcidebug > 1) ohci_dumped(newed);

}

/*  *********************************************************************
    *  _ohci_deqept(queue,e)
    *  
    *  Remove and endpoint from the list of endpoints.  This
    *  routine does things in a particular way according to
    *  the OHCI specification, since we are operating on
    *  a running list.
    *  
    *  Input parameters: 
    *  	   queue - base of queue to look for endpoint on
    *  	   e - endpoint to remove
    *  	   
    *  Return value:
    *  	   nothing
    ********************************************************************* */

static void _ohci_deqept(ohci_softc_t *softc,ohci_endpoint_t *queue,ohci_endpoint_t *e)
{
    ohci_endpoint_t *cur;
    ohci_ed_t *cured;
    ohci_ed_t *ed;

    cur = queue;

    while (cur && (cur->ep_next != e)) cur = cur->ep_next;

    if (cur == NULL) {
	printf("Could not remove EP %08X: not on the list!\n",(uint32_t) (intptr_t)e);
	return;
	}

    /*
     * Remove from our regular list
     */

    cur->ep_next = e->ep_next;

    /*
     * now remove from the hardware's list
     */

    cured = ohci_ed_from_endpoint(softc,cur);
    ed = ohci_ed_from_endpoint(softc,e);

    cured->ed_next_ed = ed->ed_next_ed;
}


/*  *********************************************************************
    *  ohci_intr_procdoneq(softc)
    *  
    *  Process the "done" queue for this ohci controller.  As 
    *  descriptors are retired, the hardware links them to the
    *  "done" queue so we can examine the results.
    *  
    *  Input parameters: 
    *  	   softc - our OHCI controller
    *  	   
    *  Return value:
    *  	   nothing
    ********************************************************************* */

static void ohci_intr_procdoneq(ohci_softc_t *softc)
{
    uint32_t doneq;
    ohci_transfer_t *transfer;
    ohci_td_t *td;
    int val;
    usbreq_t *ur;

    /*	
     * Get the head of the queue
     */

    doneq = softc->ohci_hcca->hcca_donehead;
    doneq = BSWAP32(doneq);

    td = (ohci_td_t *) OHCI_PTOV(doneq);
    transfer = ohci_transfer_from_td(softc,td);

    /*
     * Process all elements from the queue
     */

    while (doneq) {

	ohci_ed_t *ed;
	ohci_endpoint_t *ept;
	usbreq_t *xur = transfer->t_ref;

	if (ohcidebug > 1) {
	    if (xur) {
		ept = (ohci_endpoint_t *) xur->ur_pipe->up_hwendpoint;
		ed = ohci_ed_from_endpoint(softc,ept);
//		printf("ProcDoneQ:ED [%08X] -> ",ept->ep_phys);
//		ohci_dumped(ed);
		}
	    }

	/* 
	 * Get the pointer to next one before freeing this one
	 */

	if (ohcidebug > 1) {
	    ur = transfer->t_ref;
	    printf("Done(%d): ",ur ? ur->ur_tdcount : -1);
	    ohci_dumptd(td);
	    }

	doneq = BSWAP32(td->td_next_td);

	val = G_OHCI_TD_CC(BSWAP32(td->td_control));

	if (val != 0) printf("[Transfer error: %d]\n",val);

	/*
	 * See if it's time to call the callback.
	 */
	ur = transfer->t_ref;
	if (ur) {
	    ur->ur_status = val;
	    ur->ur_tdcount--;
	    if (BSWAP32(td->td_cbp) == 0) {
		ur->ur_xferred += transfer->t_length; 
		}
	    else {
		ur->ur_xferred += transfer->t_length - 
		    (BSWAP32(td->td_be) - BSWAP32(td->td_cbp) + 1);
		}
	    if (ur->ur_tdcount == 0) {
		/* Noncoherent DMA: need to invalidate, since data is in phys mem */
		OHCI_INVAL_RANGE(ur->ur_buffer,ur->ur_xferred);
		usb_complete_request(ur,val);
		}
	    }


	/*
	 * Free up the request
	 */
	_ohci_freexfer(softc,transfer);


	/*
	 * Advance to the next request.
	 */

	td = (ohci_td_t *) OHCI_PTOV(doneq);
	transfer = ohci_transfer_from_td(softc,td);
	}

}

/*  *********************************************************************
    *  ohci_intr(bus)
    *  
    *  Process pending interrupts for the OHCI controller.
    *  
    *  Input parameters: 
    *  	   bus - our bus structure
    *  	   
    *  Return value:
    *  	   0 if we did nothing
    *  	   nonzero if we did something.
    ********************************************************************* */

static int ohci_intr(usbbus_t *bus)
{
    uint32_t reg;
    ohci_softc_t *softc = (ohci_softc_t *) bus->ub_hwsoftc;

    /*
     * Read the interrupt status register.
     */

    reg = OHCI_READCSR(softc,R_OHCI_INTSTATUS);

    /*
     * Don't bother doing anything if nothing happened.
     */
    if (reg == 0) {
	return 0;
	}

    /* Scheduling Overruns */
    if (reg & M_OHCI_INT_SO) {
	printf("SchedOverrun\n");
	}

    /* Done Queue */
    if (reg & M_OHCI_INT_WDH) {
	/* printf("DoneQueue\n"); */
	ohci_intr_procdoneq(softc);
	}

    /* Start of Frame */
    if (reg & M_OHCI_INT_SF) {
	/* don't be noisy about this */
	}

    /* Resume Detect */
    if (reg & M_OHCI_INT_RD) {
	printf("ResumeDetect\n");
	}

    /* Unrecoverable errors */
    if (reg & M_OHCI_INT_UE) {
	printf("UnrecoverableError\n");
	}

    /* Frame number overflow */
    if (reg & M_OHCI_INT_FNO) {
	/*printf("FrameNumberOverflow\n"); */
	}

    /* Root Hub Status Change */
    if ((reg & ~softc->ohci_intdisable) & M_OHCI_INT_RHSC) {
	uint32_t reg;
	if (ohcidebug > 0) {
	    printf("RootHubStatusChange: ");
	    reg = OHCI_READCSR(softc,R_OHCI_RHSTATUS);
	    ohci_dumprhstat(reg);
	    reg = OHCI_READCSR(softc,R_OHCI_RHPORTSTATUS(1));
	    ohci_dumpportstat(1,reg);
	    reg = OHCI_READCSR(softc,R_OHCI_RHPORTSTATUS(2));
	    ohci_dumpportstat(2,reg);
	    }
	ohci_roothub_statchg(softc);
	}

    /* Ownership Change */
    if (reg & M_OHCI_INT_OC) {
	printf("OwnershipChange\n");
	}

    /*	
     * Write the value back to the interrupt
     * register to clear the bits that were set.
     */

    OHCI_WRITECSR(softc,R_OHCI_INTSTATUS,reg);

    return 1;
}


/*  *********************************************************************
    *  ohci_delete(bus)
    *  
    *  Remove an OHCI bus structure and all resources allocated to
    *  it (used when shutting down USB)
    *  
    *  Input parameters: 
    *  	   bus - our USB bus structure
    *  	   
    *  Return value:
    *  	   nothing
    ********************************************************************* */

static void ohci_delete(usbbus_t *bus)
{
    // xxx fill in later.
}


/*  *********************************************************************
    *  ohci_create(addr)
    *  
    *  Create a USB bus structure and associate it with our OHCI
    *  controller device.
    *  
    *  Input parameters: 
    *  	   addr - physical address of controller
    *  	   
    *  Return value:
    *  	   usbbus structure pointer
    ********************************************************************* */

static usbbus_t *ohci_create(physaddr_t addr)
{
    int res;
    ohci_softc_t *softc;
    usbbus_t *bus;

    softc = KMALLOC(sizeof(ohci_softc_t),0);
    if (!softc) return NULL;

    bus = KMALLOC(sizeof(usbbus_t),0);
    if (!bus) return NULL;

    memset(softc,0,sizeof(ohci_softc_t));
    memset(bus,0,sizeof(usbbus_t));

    bus->ub_hwsoftc  = (usb_hc_t *) softc;
    bus->ub_hwdisp   = &ohci_driver;

    q_init(&(softc->ohci_rh_intrq));

#ifdef _CFE_
    softc->ohci_regs =  addr;
#else
    softc->ohci_regs = (volatile uint32_t *) addr;
#endif

    softc->ohci_rh_newaddr = -1;
    softc->ohci_bus = bus;
	
    if ((res = _ohci_initpools(softc)) != 0) goto error;
    if ((res = _ohci_setupepts(softc)) != 0) goto error;

    OHCI_WRITECSR(softc,R_OHCI_CONTROL,V_OHCI_CONTROL_HCFS(K_OHCI_HCFS_RESET));

    return bus;

error:
    KFREE(softc);
    return NULL;
}


/*  *********************************************************************
    *  ohci_ept_create(bus,usbaddr,eptnum,mps,flags)
    *  
    *  Create a hardware endpoint structure and attach it to
    *  the hardware's endpoint list.  The hardware manages lists
    *  of queues, and this routine adds a new queue to the appropriate
    *  list of queues for the endpoint in question.  It roughly
    *  corresponds to the information in the OHCI specification.
    *  
    *  Input parameters: 
    *  	   bus - the USB bus we're dealing with
    *  	   usbaddr - USB address (0 means default address)
    *  	   eptnum - the endpoint number
    *  	   mps - the packet size for this endpoint
    *  	   flags - various flags to control endpoint creation
    *  	   
    *  Return value:
    *  	   endpoint structure poihter, or NULL
    ********************************************************************* */

static usb_ept_t *ohci_ept_create(usbbus_t *bus,
				  int usbaddr,
				  int eptnum,
				  int mps,
				  int flags)
{
    uint32_t eptflags;
    ohci_endpoint_t *ept;
    ohci_ed_t *ed;
    ohci_transfer_t *tailtransfer;
    ohci_td_t *tailtd;
    ohci_softc_t *softc = (ohci_softc_t *) bus->ub_hwsoftc;

    ept = _ohci_allocept(softc);
    ed = ohci_ed_from_endpoint(softc,ept);

    tailtransfer = _ohci_allocxfer(softc);
    tailtd = ohci_td_from_transfer(softc,tailtransfer);

    /*
     * Set up functional address, endpoint number, and packet size
     */

    eptflags = V_OHCI_ED_FA(usbaddr) |
	V_OHCI_ED_EN(eptnum) |
	V_OHCI_ED_MPS(mps) |
	0;

    /*
     * Set up the endpoint type based on the flags
     * passed to us 
     */

    if (flags & UP_TYPE_IN) {
	eptflags |= V_OHCI_ED_DIR(K_OHCI_ED_DIR_IN);
	}
    else if (flags & UP_TYPE_OUT) {
	eptflags |= V_OHCI_ED_DIR(K_OHCI_ED_DIR_OUT);
	}
    else {
	eptflags |= V_OHCI_ED_DIR(K_OHCI_ED_DIR_FROMTD);
	}

    /*
     * Don't forget about lowspeed devices.
     */

    if (flags & UP_TYPE_LOWSPEED) {
	eptflags |= M_OHCI_ED_LOWSPEED;
	}
    
    if (ohcidebug > 0) {
	printf("Create endpoint %d addr %d flags %08X mps %d\n",
	   eptnum,usbaddr,eptflags,mps);
	}

    /*
     * Transfer this info into the endpoint descriptor.
     * No need to flush the cache here, it'll get done when
     * we add to the hardware list.
     */

    ed->ed_control = BSWAP32(eptflags);
    ed->ed_tailp   = BSWAP32(OHCI_VTOP(tailtd));
    ed->ed_headp   = BSWAP32(OHCI_VTOP(tailtd));
    ept->ep_flags = flags;
    ept->ep_mps = mps;
    ept->ep_num = eptnum;

    /*
     * Put it on the right queue
     */

    if (flags & UP_TYPE_CONTROL) {
	_ohci_queueept(softc,softc->ohci_ctl_list,ept);
	}
    else if (flags & UP_TYPE_BULK) {
	_ohci_queueept(softc,softc->ohci_bulk_list,ept);
	}
    else if (flags & UP_TYPE_INTR) {
	/* XXX Choose place in inttable properly. */
	_ohci_queueept(softc,softc->ohci_inttable[0],ept);
	}

    return (usb_ept_t *) ept;
}

/*  *********************************************************************
    *  ohci_ept_setaddr(bus,ept,usbaddr)
    *  
    *  Change the functional address for a USB endpoint.  We do this
    *  when we switch the device's state from DEFAULT to ADDRESSED
    *  and we've already got the default pipe open.  This
    *  routine mucks with the descriptor and changes its address
    *  bits.
    *  
    *  Input parameters: 
    *  	   bus - usb bus structure
    *  	   ept - an open endpoint descriptor
    *  	   usbaddr - new address for this endpoint
    *  	   
    *  Return value:
    *  	   nothing
    ********************************************************************* */

static void ohci_ept_setaddr(usbbus_t *bus,usb_ept_t *uept,int usbaddr)
{
    uint32_t eptflags;
    ohci_endpoint_t *ept = (ohci_endpoint_t *) uept;
    ohci_softc_t *softc = (ohci_softc_t *) bus->ub_hwsoftc;
    ohci_ed_t *ed = ohci_ed_from_endpoint(softc,ept);

    eptflags = BSWAP32(ed->ed_control);
    eptflags &= ~M_OHCI_ED_FA;
    eptflags |= V_OHCI_ED_FA(usbaddr);
    ed->ed_control = BSWAP32(eptflags);
}


/*  *********************************************************************
    *  ohci_ept_setmps(bus,ept,mps)
    *  
    *  Set the maximum packet size of this endpoint.  This is
    *  normally used during the processing of endpoint 0 (default
    *  pipe) after we find out how big ep0's packets can be.
    *  
    *  Input parameters: 
    *  	   bus - our USB bus structure
    *  	   ept - endpoint structure
    *  	   mps - new packet size
    *  	   
    *  Return value:
    *  	   nothing
    ********************************************************************* */

static void ohci_ept_setmps(usbbus_t *bus,usb_ept_t *uept,int mps)
{
    uint32_t eptflags;
    ohci_softc_t *softc = (ohci_softc_t *) bus->ub_hwsoftc;
    ohci_endpoint_t *ept = (ohci_endpoint_t *) uept;
    ohci_ed_t *ed = ohci_ed_from_endpoint(softc,ept);

    eptflags = BSWAP32(ed->ed_control);
    eptflags &= ~M_OHCI_ED_MPS;
    eptflags |= V_OHCI_ED_MPS(mps);
    ed->ed_control = BSWAP32(eptflags);
    ept->ep_mps = mps;

}

/*  *********************************************************************
    *  ohci_ept_cleartoggle(bus,ept,mps)
    *  
    *  Clear the data toggle for the specified endpoint.
    *  
    *  Input parameters: 
    *  	   bus - our USB bus structure
    *  	   ept - endpoint structure
    *  	   
    *  Return value:
    *  	   nothing
    ********************************************************************* */

static void ohci_ept_cleartoggle(usbbus_t *bus,usb_ept_t *uept)
{
    uint32_t eptflags;
    ohci_softc_t *softc = (ohci_softc_t *) bus->ub_hwsoftc;
    ohci_endpoint_t *ept = (ohci_endpoint_t *) uept;
    ohci_ed_t *ed = ohci_ed_from_endpoint(softc,ept);

    eptflags = BSWAP32(ed->ed_headp);
    eptflags &= ~(M_OHCI_ED_HALT | M_OHCI_ED_TOGGLECARRY);
    ed->ed_headp = BSWAP32(eptflags);

    OHCI_WRITECSR(softc,R_OHCI_CMDSTATUS,M_OHCI_CMDSTATUS_CLF);
}

/*  *********************************************************************
    *  ohci_ept_delete(bus,ept)
    *  
    *  Deletes an endpoint from the OHCI controller.  This
    *  routine also completes pending transfers for the 
    *  endpoint and gets rid of the hardware ept (queue base).
    *  
    *  Input parameters: 
    *  	   bus - ohci bus structure
    *  	   ept - endpoint to remove
    *  	   
    *  Return value:
    *  	   nothing
    ********************************************************************* */

static void ohci_ept_delete(usbbus_t *bus,usb_ept_t *uept)
{
    ohci_endpoint_t *queue;
    ohci_softc_t *softc = (ohci_softc_t *) bus->ub_hwsoftc;
    ohci_endpoint_t *ept = (ohci_endpoint_t *) uept;
    ohci_ed_t *ed = ohci_ed_from_endpoint(softc,ept);
    uint32_t framenum;
    uint32_t tdphys;
    usbreq_t *ur;
    ohci_td_t *td;
    ohci_transfer_t *transfer;

    if (ept->ep_flags & UP_TYPE_CONTROL) {
	queue = softc->ohci_ctl_list;
	}
    else if (ept->ep_flags & UP_TYPE_BULK) {
	queue = softc->ohci_bulk_list;
	}
    else if (ept->ep_flags & UP_TYPE_INTR) {
	queue = softc->ohci_inttable[0];
	}
    else {
	printf("Invalid endpoint\n");
	return;
	}


    /*
     * Set the SKIP bit on the endpoint and
     * wait for two SOFs to guarantee that we're
     * not processing this ED anymore.
     */

    ((volatile uint32_t) ed->ed_control) |= BSWAP32(M_OHCI_ED_SKIP);

    framenum = OHCI_READCSR(softc,R_OHCI_FMNUMBER) & 0xFFFF;
    while ((OHCI_READCSR(softc,R_OHCI_FMNUMBER) & 0xFFFF) == framenum) ; /* NULL LOOP */

    framenum = OHCI_READCSR(softc,R_OHCI_FMNUMBER) & 0xFFFF;
    while ((OHCI_READCSR(softc,R_OHCI_FMNUMBER) & 0xFFFF) == framenum) ; /* NULL LOOP */

    /* 
     * Remove endpoint from queue
     */

    _ohci_deqept(softc,queue,ept);

    /*
     * Free/complete the TDs on the queue
     */

    tdphys = BSWAP32(ed->ed_headp) & M_OHCI_ED_PTRMASK;

    while (tdphys != BSWAP32(ed->ed_tailp)) {
	td = (ohci_td_t *) OHCI_PTOV(tdphys);
	tdphys = BSWAP32(td->td_next_td);
	transfer = ohci_transfer_from_td(softc,td);

	ur = transfer->t_ref;
	if (ur) {	
	    ur->ur_status = K_OHCI_CC_CANCELLED;
	    ur->ur_tdcount--;
	    if (ur->ur_tdcount == 0) {
		if (ohcidebug > 0) printf("Completing request due to closed pipe: %p\n",ur);
		usb_complete_request(ur,K_OHCI_CC_CANCELLED);
		/* XXX it is expected that the callee will free the usbreq. */
		}
	    }

	_ohci_freexfer(softc,transfer);
	}

    /*
     * tdphys now points at the tail TD.  Just free it.
     */

    td = (ohci_td_t *) OHCI_PTOV(tdphys);
    _ohci_freexfer(softc,ohci_transfer_from_td(softc,td));

    /*
     * Return endpoint to free pool
     */

    _ohci_freeept(softc,ept);
}



/*  *********************************************************************
    *  ohci_xfer(bus,ept,ur)
    *  
    *  Queue a transfer for the specified endpoint.  Depending on
    *  the transfer type, the transfer may go on one of many queues.
    *  When the transfer completes, a callback will be called.
    *  
    *  Input parameters: 
    *  	   bus - bus structure
    *  	   ept - endpoint descriptor
    *  	   ur - request (includes pointer to user buffer)
    *  	   
    *  Return value:
    *  	   0 if ok
    *  	   else error
    ********************************************************************* */

static int ohci_xfer(usbbus_t *bus,usb_ept_t *uept,usbreq_t *ur)
{
    ohci_softc_t *softc = (ohci_softc_t *) bus->ub_hwsoftc;
    ohci_endpoint_t *ept = (ohci_endpoint_t *) uept;
    ohci_ed_t *ed = ohci_ed_from_endpoint(softc,ept);
    ohci_transfer_t *newtailtransfer = 0;
    ohci_td_t *newtailtd = NULL;
    ohci_transfer_t *curtransfer;
    ohci_td_t *curtd;
    uint8_t *ptr;
    int len;
    int amtcopy;
    int pktlen;
    uint32_t tdcontrol = 0;

    /*
     * If the destination USB address matches
     * the address of the root hub, shunt the request
     * over to our root hub emulation.
     */
    
    if (ur->ur_dev->ud_address == softc->ohci_rh_addr) {
	return ohci_roothub_xfer(bus,uept,ur);
	}

    /*
     * Set up the TD flags based on the 
     * request type.
     */

//    pktlen = ept->ep_mps;
    pktlen = OHCI_TD_MAX_DATA - 16;

    if (ur->ur_flags & UR_FLAG_SETUP) {
	tdcontrol = V_OHCI_TD_PID(K_OHCI_TD_SETUP) |
	    V_OHCI_TD_DT(K_OHCI_TD_DT_DATA0) |
	    V_OHCI_TD_CC(K_OHCI_CC_NOTACCESSED) |
	    V_OHCI_TD_DI(1);
	}
    else if (ur->ur_flags & UR_FLAG_IN) {
	tdcontrol = V_OHCI_TD_PID(K_OHCI_TD_IN) |
	    V_OHCI_TD_DT(K_OHCI_TD_DT_TCARRY) |
	    V_OHCI_TD_CC(K_OHCI_CC_NOTACCESSED) |
	    V_OHCI_TD_DI(1);
	}
    else if (ur->ur_flags & UR_FLAG_OUT) {
	tdcontrol = V_OHCI_TD_PID(K_OHCI_TD_OUT) |
	    V_OHCI_TD_DT(K_OHCI_TD_DT_TCARRY) |
	    V_OHCI_TD_CC(K_OHCI_CC_NOTACCESSED) |
	    V_OHCI_TD_DI(1);
	}
    else if (ur->ur_flags & UR_FLAG_STATUS_OUT) {
	tdcontrol = V_OHCI_TD_PID(K_OHCI_TD_OUT) |
	    V_OHCI_TD_DT(K_OHCI_TD_DT_DATA1) |
	    V_OHCI_TD_CC(K_OHCI_CC_NOTACCESSED) |
	    V_OHCI_TD_DI(1);
	}
    else if (ur->ur_flags & UR_FLAG_STATUS_IN) {
	tdcontrol = V_OHCI_TD_PID(K_OHCI_TD_IN) |
	    V_OHCI_TD_DT(K_OHCI_TD_DT_DATA1) |
	    V_OHCI_TD_CC(K_OHCI_CC_NOTACCESSED) |
	    V_OHCI_TD_DI(1);
	}
    else {
	printf("Shouldn't happen!\n");
	}

    if (ur->ur_flags & UR_FLAG_SHORTOK) {
	tdcontrol |= M_OHCI_TD_SHORTOK;
	}


    ptr = ur->ur_buffer;
    len = ur->ur_length;
    ur->ur_tdcount = 0;

    if (ohcidebug > 1) {
	printf(">> Queueing xfer addr %d pipe %d ED %08X ptr %016llX length %d\n",
	       ur->ur_dev->ud_address,
	       ur->ur_pipe->up_num,
	       ept->ep_phys,
	       (uint64_t) (uintptr_t) ptr,
	       len);
//	ohci_dumped(ed);
	}

    curtd = OHCI_PTOV(BSWAP32(ed->ed_tailp));
    curtransfer = ohci_transfer_from_td(softc,curtd);

    if (len == 0) {
	newtailtransfer = _ohci_allocxfer(softc);
	newtailtd = ohci_td_from_transfer(softc,newtailtransfer);
	curtd->td_cbp = 0;
	curtd->td_be = 0;
	curtd->td_next_td = BSWAP32(OHCI_VTOP(newtailtd));
	curtd->td_control = BSWAP32(tdcontrol);
	curtransfer->t_next = newtailtransfer;
	curtransfer->t_ref = ur;
	curtransfer->t_length = 0;
	if (ohcidebug > 1) { printf("QueueTD: "); ohci_dumptd(curtd); }
	ur->ur_tdcount++;
	}
    else {
	/* Noncoherent DMA: need to flush user buffer to real memory first */
	OHCI_FLUSH_RANGE(ptr,len);
	while (len > 0) {
	    amtcopy = len;
	    if (amtcopy > pktlen) amtcopy =  pktlen;
	    newtailtransfer = _ohci_allocxfer(softc);
	    newtailtd = ohci_td_from_transfer(softc,newtailtransfer);
	    curtd->td_cbp = BSWAP32(OHCI_VTOP(ptr));
	    curtd->td_be = BSWAP32(OHCI_VTOP(ptr+amtcopy)-1);
	    curtd->td_next_td = BSWAP32(OHCI_VTOP(newtailtd));
	    curtd->td_control = BSWAP32(tdcontrol);
	    curtransfer->t_next = newtailtransfer;
	    curtransfer->t_ref = ur;
	    curtransfer->t_length = amtcopy;
	    if (ohcidebug > 1) { printf("QueueTD: "); ohci_dumptd(curtd); }
	    curtd = newtailtd;
	    curtransfer = ohci_transfer_from_td(softc,curtd);
	    ptr += amtcopy;
	    len -= amtcopy;
	    ur->ur_tdcount++;
	    }
	}

    curtd = OHCI_PTOV(BSWAP32(ed->ed_headp & M_OHCI_ED_PTRMASK));
    ed->ed_tailp = BSWAP32(OHCI_VTOP(newtailtd));

    /*
     * Prod the controller depending on what type of list we put
     * a TD on.
     */

    if (ept->ep_flags & UP_TYPE_BULK) {
	OHCI_WRITECSR(softc,R_OHCI_CMDSTATUS,M_OHCI_CMDSTATUS_BLF);
	}
    else {
	/* XXX should probably make sure we're UP_TYPE_CONTROL here */
	OHCI_WRITECSR(softc,R_OHCI_CMDSTATUS,M_OHCI_CMDSTATUS_CLF);
	}

    return 0;
}

/*  *********************************************************************
    *  Driver structure
    ********************************************************************* */

usb_hcdrv_t ohci_driver = {
    ohci_create,
    ohci_delete,
    ohci_start,
    ohci_stop,
    ohci_intr,
    ohci_ept_create,
    ohci_ept_delete,
    ohci_ept_setmps,
    ohci_ept_setaddr,
    ohci_ept_cleartoggle,
    ohci_xfer
};

/*  *********************************************************************
    *  Root Hub
    *
    *  Data structures and functions
    ********************************************************************* */

/*
 * Data structures and routines to emulate the root hub.
 */
static usb_device_descr_t ohci_root_devdsc = {
    sizeof(usb_device_descr_t),		/* bLength */
    USB_DEVICE_DESCRIPTOR_TYPE,		/* bDescriptorType */
    USBWORD(0x0100),			/* bcdUSB */
    USB_DEVICE_CLASS_HUB,		/* bDeviceClass */
    0,					/* bDeviceSubClass */
    0,					/* bDeviceProtocol */
    64,					/* bMaxPacketSize0 */
    USBWORD(0),				/* idVendor */
    USBWORD(0),				/* idProduct */
    USBWORD(0x0100),			/* bcdDevice */
    1,					/* iManufacturer */
    2,					/* iProduct */
    0,					/* iSerialNumber */
    1					/* bNumConfigurations */
};

static usb_config_descr_t ohci_root_cfgdsc = {
    sizeof(usb_config_descr_t),		/* bLength */
    USB_CONFIGURATION_DESCRIPTOR_TYPE,	/* bDescriptorType */
    USBWORD(
	sizeof(usb_config_descr_t) +
	sizeof(usb_interface_descr_t) +
	sizeof(usb_endpoint_descr_t)),	/* wTotalLength */
    1,					/* bNumInterfaces */
    1,					/* bConfigurationValue */
    0,					/* iConfiguration */
    USB_CONFIG_SELF_POWERED,		/* bmAttributes */
    0					/* MaxPower */
};

static usb_interface_descr_t ohci_root_ifdsc = {
    sizeof(usb_interface_descr_t),	/* bLength */
    USB_INTERFACE_DESCRIPTOR_TYPE,	/* bDescriptorType */
    0,					/* bInterfaceNumber */
    0,					/* bAlternateSetting */
    1,					/* bNumEndpoints */
    USB_INTERFACE_CLASS_HUB,		/* bInterfaceClass */
    0,					/* bInterfaceSubClass */
    0,					/* bInterfaceProtocol */
    0					/* iInterface */
};

static usb_endpoint_descr_t ohci_root_epdsc = {
    sizeof(usb_endpoint_descr_t),	/* bLength */
    USB_ENDPOINT_DESCRIPTOR_TYPE,	/* bDescriptorType */
    (USB_ENDPOINT_DIRECTION_IN | 1),	/* bEndpointAddress */
    USB_ENDPOINT_TYPE_INTERRUPT,	/* bmAttributes */
    USBWORD(8),				/* wMaxPacketSize */
    255					/* bInterval */
};

static usb_hub_descr_t ohci_root_hubdsc = {
    USB_HUB_DESCR_SIZE,			/* bLength */
    USB_HUB_DESCRIPTOR_TYPE,		/* bDescriptorType */
    0,					/* bNumberOfPorts */
    USBWORD(0),				/* wHubCharacteristics */
    0,					/* bPowreOnToPowerGood */
    0,					/* bHubControl Current */
    {0}					/* bRemoveAndPowerMask */
};

/*  *********************************************************************
    *  ohci_roothb_strdscr(ptr,str)
    *  
    *  Construct a string descriptor for root hub requests
    *  
    *  Input parameters: 
    *  	   ptr - pointer to where to put descriptor
    *  	   str - regular string to put into descriptor
    *  	   
    *  Return value:
    *  	   number of bytes written to descriptor
    ********************************************************************* */

static int ohci_roothub_strdscr(uint8_t *ptr,char *str)
{
    uint8_t *p = ptr;

    *p++ = strlen(str)*2 + 2;	/* Unicode strings */
    *p++ = USB_STRING_DESCRIPTOR_TYPE;
    while (*str) {
	*p++ = *str++;
	*p++ = 0;
	}
    return (p - ptr);
}

/*  *********************************************************************
    *  ohci_roothub_req(softc,req)
    *  
    *  Handle a descriptor request on the control pipe for the
    *  root hub.  We pretend to be a real root hub here and
    *  return all the standard descriptors.
    *  
    *  Input parameters: 
    *  	   softc - our OHCI controller
    *  	   req - a usb request (completed immediately)
    *  	   
    *  Return value:
    *  	   0 if ok
    *  	   else error code
    ********************************************************************* */

static int ohci_roothub_req(ohci_softc_t *softc,usb_device_request_t *req)
{
    uint8_t *ptr;
    uint16_t wLength;
    uint16_t wValue;
    uint16_t wIndex;
    usb_port_status_t ups;
    usb_hub_descr_t hdsc;
    uint32_t status;
    uint32_t statport;
    uint32_t tmpval;
    int res = 0;

    ptr = softc->ohci_rh_buf;

    wLength = GETUSBFIELD(req,wLength);
    wValue  = GETUSBFIELD(req,wValue);
    wIndex  = GETUSBFIELD(req,wIndex);

    switch (REQSW(req->bRequest,req->bmRequestType)) {

	case REQCODE(USB_REQUEST_GET_STATUS,USBREQ_DIR_IN,USBREQ_TYPE_STD,USBREQ_REC_DEVICE):
	    *ptr++ = (USB_GETSTATUS_SELF_POWERED & 0xFF);
	    *ptr++ = (USB_GETSTATUS_SELF_POWERED >> 8);
	    break;

	case REQCODE(USB_REQUEST_GET_STATUS,USBREQ_DIR_IN,USBREQ_TYPE_STD,USBREQ_REC_ENDPOINT):
	case REQCODE(USB_REQUEST_GET_STATUS,USBREQ_DIR_IN,USBREQ_TYPE_STD,USBREQ_REC_INTERFACE):
	    *ptr++ = 0;
	    *ptr++ = 0;
	    break;

	case REQCODE(USB_REQUEST_GET_STATUS,USBREQ_DIR_IN,USBREQ_TYPE_CLASS,USBREQ_REC_OTHER):
	    status = OHCI_READCSR(softc,(R_OHCI_RHPORTSTATUS(wIndex)));
	    if (ohcidebug > 0) { printf("RHGetStatus: "); ohci_dumpportstat(wIndex,status);}
	    PUTUSBFIELD((&ups),wPortStatus,(status & 0xFFFF));
	    PUTUSBFIELD((&ups),wPortChange,(status >> 16));
	    memcpy(ptr,&ups,sizeof(ups));
	    ptr += sizeof(ups);
	    break;

	case REQCODE(USB_REQUEST_GET_STATUS,USBREQ_DIR_IN,USBREQ_TYPE_CLASS,USBREQ_REC_DEVICE):
	    *ptr++ = 0;
	    *ptr++ = 0;
	    *ptr++ = 0;
	    *ptr++ = 0;
	    break;

	case REQCODE(USB_REQUEST_CLEAR_FEATURE,USBREQ_DIR_OUT,USBREQ_TYPE_STD,USBREQ_REC_DEVICE):
	case REQCODE(USB_REQUEST_CLEAR_FEATURE,USBREQ_DIR_OUT,USBREQ_TYPE_STD,USBREQ_REC_INTERFACE):
	case REQCODE(USB_REQUEST_CLEAR_FEATURE,USBREQ_DIR_OUT,USBREQ_TYPE_STD,USBREQ_REC_ENDPOINT):
	    /* do nothing, not supported */
	    break;

	case REQCODE(USB_REQUEST_CLEAR_FEATURE,USBREQ_DIR_OUT,USBREQ_TYPE_CLASS,USBREQ_REC_OTHER):
	    statport = R_OHCI_RHPORTSTATUS(wIndex);
	    if (ohcidebug> 0) {
		printf("RHClearFeature(%d): ",wValue); ohci_dumpportstat(wIndex,OHCI_READCSR(softc,statport));
		}
	    switch (wValue) {
		case USB_PORT_FEATURE_CONNECTION:
		    break;
		case USB_PORT_FEATURE_ENABLE:
		    OHCI_WRITECSR(softc,statport,M_OHCI_RHPORTSTAT_CCS);
		    break;
		case USB_PORT_FEATURE_SUSPEND:
		    OHCI_WRITECSR(softc,statport,M_OHCI_RHPORTSTAT_POCI);
		    break;
		case USB_PORT_FEATURE_OVER_CURRENT:
		    break;
		case USB_PORT_FEATURE_RESET:
		    break;
		case USB_PORT_FEATURE_POWER:
		    OHCI_WRITECSR(softc,statport,M_OHCI_RHPORTSTAT_LSDA);
		    break;
		case USB_PORT_FEATURE_LOW_SPEED:
		    break;
		case USB_PORT_FEATURE_C_PORT_CONNECTION:
		    OHCI_WRITECSR(softc,statport,M_OHCI_RHPORTSTAT_CSC);
		    break;
		case USB_PORT_FEATURE_C_PORT_ENABLE:
		    OHCI_WRITECSR(softc,statport,M_OHCI_RHPORTSTAT_PESC);
		    break;
		case USB_PORT_FEATURE_C_PORT_SUSPEND:
		    OHCI_WRITECSR(softc,statport,M_OHCI_RHPORTSTAT_PSSC);
		    break;
		case USB_PORT_FEATURE_C_PORT_OVER_CURRENT:
		    OHCI_WRITECSR(softc,statport,M_OHCI_RHPORTSTAT_OCIC);
		    break;
		case USB_PORT_FEATURE_C_PORT_RESET:
		    OHCI_WRITECSR(softc,statport,M_OHCI_RHPORTSTAT_PRSC);
		    break;

		}

	    /*
	     * If we've cleared all of the conditions that
	     * want our attention on the port status,
	     * then we can accept port status interrupts again.
	     */

	    if ((wValue >= USB_PORT_FEATURE_C_PORT_CONNECTION) &&
		(wValue <= USB_PORT_FEATURE_C_PORT_RESET)) {
		status = OHCI_READCSR(softc,statport);
		if ((status & M_OHCI_RHPORTSTAT_ALLC) == 0) {
		    softc->ohci_intdisable &= ~M_OHCI_INT_RHSC;
		    }
		}
	    break;

	case REQCODE(USB_REQUEST_SET_FEATURE,USBREQ_DIR_OUT,USBREQ_TYPE_STD,USBREQ_REC_DEVICE):
	case REQCODE(USB_REQUEST_SET_FEATURE,USBREQ_DIR_OUT,USBREQ_TYPE_STD,USBREQ_REC_INTERFACE):
	case REQCODE(USB_REQUEST_SET_FEATURE,USBREQ_DIR_OUT,USBREQ_TYPE_STD,USBREQ_REC_ENDPOINT):
	    res = -1;
	    break;

	case REQCODE(USB_REQUEST_SET_FEATURE,USBREQ_DIR_OUT,USBREQ_TYPE_CLASS,USBREQ_REC_DEVICE):
	    /* nothing */
	    break;

	case REQCODE(USB_REQUEST_SET_FEATURE,USBREQ_DIR_OUT,USBREQ_TYPE_CLASS,USBREQ_REC_OTHER):
	    statport = R_OHCI_RHPORTSTATUS(wIndex);
	    switch (wValue) {
		case USB_PORT_FEATURE_CONNECTION:
		    break;
		case USB_PORT_FEATURE_ENABLE:
		    OHCI_WRITECSR(softc,statport,M_OHCI_RHPORTSTAT_PES);
		    break;
		case USB_PORT_FEATURE_SUSPEND:
		    OHCI_WRITECSR(softc,statport,M_OHCI_RHPORTSTAT_PSS);
		    break;
		case USB_PORT_FEATURE_OVER_CURRENT:
		    break;
		case USB_PORT_FEATURE_RESET:
		    OHCI_WRITECSR(softc,statport,M_OHCI_RHPORTSTAT_PRS);
		    for (;;) {	/* XXX timer */
			usb_delay_ms(softc->ohci_bus,100);
			if (!(OHCI_READCSR(softc,statport) & M_OHCI_RHPORTSTAT_PRS)) break;
			}
		    break;
		case USB_PORT_FEATURE_POWER:
		    OHCI_WRITECSR(softc,statport,M_OHCI_RHPORTSTAT_PPS);
		    break;
		case USB_PORT_FEATURE_LOW_SPEED:
		    break;
		case USB_PORT_FEATURE_C_PORT_CONNECTION:
		    break;
		case USB_PORT_FEATURE_C_PORT_ENABLE:
		    break;
		case USB_PORT_FEATURE_C_PORT_SUSPEND:
		    break;
		case USB_PORT_FEATURE_C_PORT_OVER_CURRENT:
		    break;
		case USB_PORT_FEATURE_C_PORT_RESET:
		    break;

		}
    
	    break;

	case REQCODE(USB_REQUEST_SET_ADDRESS,USBREQ_DIR_OUT,USBREQ_TYPE_STD,USBREQ_REC_DEVICE):
	    softc->ohci_rh_newaddr = wValue;
	    break;

	case REQCODE(USB_REQUEST_GET_DESCRIPTOR,USBREQ_DIR_IN,USBREQ_TYPE_STD,USBREQ_REC_DEVICE):
	    switch (wValue >> 8) {
		case USB_DEVICE_DESCRIPTOR_TYPE:
		    memcpy(ptr,&ohci_root_devdsc,sizeof(ohci_root_devdsc));
		    ptr += sizeof(ohci_root_devdsc);
		    break;
		case USB_CONFIGURATION_DESCRIPTOR_TYPE:
		    memcpy(ptr,&ohci_root_cfgdsc,sizeof(ohci_root_cfgdsc));
		    ptr += sizeof(ohci_root_cfgdsc);
		    memcpy(ptr,&ohci_root_ifdsc,sizeof(ohci_root_ifdsc));
		    ptr += sizeof(ohci_root_ifdsc);
		    memcpy(ptr,&ohci_root_epdsc,sizeof(ohci_root_epdsc));
		    ptr += sizeof(ohci_root_epdsc);
		    break;
		case USB_STRING_DESCRIPTOR_TYPE:
		    switch (wValue & 0xFF) {
			case 1:	 
			    ptr += ohci_roothub_strdscr(ptr,"Generic");
			    break;
			case 2:
			    ptr += ohci_roothub_strdscr(ptr,"Root Hub");
			    break;
			default:
			    *ptr++ = 0;
			    break;
			}
		    break;
		default:
		    res = -1;
		    break;
		}
	    break;

	case REQCODE(USB_REQUEST_GET_DESCRIPTOR,USBREQ_DIR_IN,USBREQ_TYPE_CLASS,USBREQ_REC_DEVICE):
	    memcpy(&hdsc,&ohci_root_hubdsc,sizeof(hdsc));
	    hdsc.bNumberOfPorts = softc->ohci_ndp;
	    status = OHCI_READCSR(softc,R_OHCI_RHDSCRA);
	    tmpval = 0;
	    if (status & M_OHCI_RHDSCRA_NPS) tmpval |= USB_HUBCHAR_PWR_NONE;
	    if (status & M_OHCI_RHDSCRA_PSM) tmpval |= USB_HUBCHAR_PWR_GANGED;
	    else tmpval |= USB_HUBCHAR_PWR_IND;
	    PUTUSBFIELD((&hdsc),wHubCharacteristics,tmpval);
	    tmpval = G_OHCI_RHDSCRA_POTPGT(status);
	    hdsc.bPowerOnToPowerGood = tmpval;
	    hdsc.bDescriptorLength = USB_HUB_DESCR_SIZE + 1;
	    status = OHCI_READCSR(softc,R_OHCI_RHDSCRB);
	    hdsc.bRemoveAndPowerMask[0] = (uint8_t) status;
	    memcpy(ptr,&hdsc,sizeof(hdsc));
	    ptr += sizeof(hdsc);
	    break;

	case REQCODE(USB_REQUEST_SET_DESCRIPTOR,USBREQ_DIR_OUT,USBREQ_TYPE_CLASS,USBREQ_REC_DEVICE):
	    /* nothing */
	    break;

	case REQCODE(USB_REQUEST_GET_CONFIGURATION,USBREQ_DIR_IN,USBREQ_TYPE_STD,USBREQ_REC_DEVICE):
	    *ptr++ = softc->ohci_rh_conf;
	    break;

	case REQCODE(USB_REQUEST_SET_CONFIGURATION,USBREQ_DIR_OUT,USBREQ_TYPE_STD,USBREQ_REC_DEVICE):
	    softc->ohci_rh_conf = wValue;
	    break;

	case REQCODE(USB_REQUEST_GET_INTERFACE,USBREQ_DIR_IN,USBREQ_TYPE_STD,USBREQ_REC_INTERFACE):
	    *ptr++ = 0;
	    break;

	case REQCODE(USB_REQUEST_SET_INTERFACE,USBREQ_DIR_OUT,USBREQ_TYPE_STD,USBREQ_REC_INTERFACE):
	    /* nothing */
	    break;

	case REQCODE(USB_REQUEST_SYNC_FRAME,USBREQ_DIR_OUT,USBREQ_TYPE_STD,USBREQ_REC_ENDPOINT):
	    /* nothing */
	    break;
	}

    softc->ohci_rh_ptr = softc->ohci_rh_buf;
    softc->ohci_rh_len = ptr - softc->ohci_rh_buf;

    return res;
}

/*  *********************************************************************
    *  ohci_roothub_statchg(softc)
    *  
    *  This routine is called from the interrupt service routine
    *  (well, polling routine) for the ohci controller.  If the 
    *  controller notices a root hub status change, it dequeues an
    *  interrupt transfer from the root hub's queue and completes
    *  it here.
    *  
    *  Input parameters: 
    *  	   softc - our OHCI controller
    *  	   
    *  Return value:
    *  	   nothing
    ********************************************************************* */

static void ohci_roothub_statchg(ohci_softc_t *softc)
{
    usbreq_t *ur;
    uint32_t status;
    uint8_t portstat = 0;
    int idx;

    /* Note: this only works up to 8 ports */
    for (idx = 1; idx <= softc->ohci_ndp; idx++) {
	status = OHCI_READCSR(softc,R_OHCI_RHPORTSTATUS(idx));
	if (status & M_OHCI_RHPORTSTAT_ALLC) {
	    portstat = (1<<idx);
	    }
	}

    if (portstat != 0) {
	softc->ohci_intdisable |= M_OHCI_INT_RHSC;
	}

    ur = (usbreq_t *) q_deqnext(&(softc->ohci_rh_intrq));
    if (!ur) return;		/* no requests pending, ignore it */

    memset(ur->ur_buffer,0,ur->ur_length);
    ur->ur_buffer[0] = portstat;
    ur->ur_xferred = ur->ur_length;

    usb_complete_request(ur,0);
}

/*  *********************************************************************
    *  ohci_roothub_xfer(softc,req)
    *  
    *  Handle a root hub xfer - ohci_xfer transfers control here
    *  if we detect the address of the root hub - no actual transfers
    *  go out on the wire, we just handle the requests directly to
    *  make it look like a hub is attached.
    *  
    *  This seems to be common practice in the USB world, so we do
    *  it here too.
    *  
    *  Input parameters: 
    *  	   softc - our OHCI controller structure
    *  	   req - usb request destined for host controller
    *  	   
    *  Return value:
    *  	   0 if ok
    *  	   else error
    ********************************************************************* */

static int ohci_roothub_xfer(usbbus_t *bus,usb_ept_t *uept,usbreq_t *ur)
{
    ohci_softc_t *softc = (ohci_softc_t *) bus->ub_hwsoftc;
    ohci_endpoint_t *ept = (ohci_endpoint_t *) uept;
    int res;

    switch (ept->ep_num) {

	/*
	 * CONTROL ENDPOINT
	 */
	case 0:

	    /*
	     * Three types of transfers:  OUT (SETUP), IN (data), or STATUS.
	     * figure out which is which.
	     */

	    if (ur->ur_flags & UR_FLAG_SETUP) {
		/*
		 * SETUP packet - this is an OUT request to the control
		 * pipe.  We emulate the hub request here.
		 */
		usb_device_request_t *req;

		req = (usb_device_request_t *) ur->ur_buffer;

		res = ohci_roothub_req(softc,req);
		if (res != 0) printf("Root hub request returned an error\n");

		ur->ur_xferred = ur->ur_length;
		ur->ur_status = 0;
		usb_complete_request(ur,0);
		}

	    else if (ur->ur_flags & UR_FLAG_STATUS_IN) {
		/*
		 * STATUS IN : it's sort of like a dummy IN request
		 * to acknowledge a SETUP packet that otherwise has no 
		 * status.  Just complete the usbreq.
		 */

		if (softc->ohci_rh_newaddr != -1) {
		    softc->ohci_rh_addr = softc->ohci_rh_newaddr;
		    softc->ohci_rh_newaddr = -1;
		    }

		ur->ur_status = 0;
		ur->ur_xferred = 0;
		usb_complete_request(ur,0);
		}

	    else if (ur->ur_flags & UR_FLAG_STATUS_OUT) {
		/*
		 * STATUS OUT : it's sort of like a dummy OUT request
		 */
		ur->ur_status = 0;
		ur->ur_xferred = 0;
		usb_complete_request(ur,0);
		}

	    else if (ur->ur_flags & UR_FLAG_IN) {
		/*
		 * IN : return data from the root hub
		 */
		int amtcopy;

		amtcopy = softc->ohci_rh_len;
		if (amtcopy > ur->ur_length) amtcopy = ur->ur_length;

		memcpy(ur->ur_buffer,softc->ohci_rh_ptr,amtcopy);

		softc->ohci_rh_ptr += amtcopy;
		softc->ohci_rh_len -= amtcopy;

		ur->ur_status = 0;
		ur->ur_xferred = amtcopy;
		usb_complete_request(ur,0);
		}

	    else {
		printf("Unknown root hub transfer type\n");
		return -1;
		}
	    break;

	/*
	 * INTERRUPT ENDPOINT 
	 */

	case 1:			/* interrupt pipe */
	    if (ur->ur_flags & UR_FLAG_IN) {
		q_enqueue(&(softc->ohci_rh_intrq),(queue_t *) ur);
		}
 	    break;

	}


    return 0;
}