aboutsummaryrefslogtreecommitdiffstats
path: root/packages/linux/linux-mtx-2-2.4.27/46-otg.patch
blob: 2004894b3e612b5c01883bbe2cc885b9a496fef3 (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
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
6140
6141
6142
6143
6144
6145
6146
6147
6148
6149
6150
6151
6152
6153
6154
6155
6156
6157
6158
6159
6160
6161
6162
6163
6164
6165
6166
6167
6168
6169
6170
6171
6172
6173
6174
6175
6176
6177
6178
6179
6180
6181
6182
6183
6184
6185
6186
6187
6188
6189
6190
6191
6192
6193
6194
6195
6196
6197
6198
6199
6200
6201
6202
6203
6204
6205
6206
6207
6208
6209
6210
6211
6212
6213
6214
6215
6216
6217
6218
6219
6220
6221
6222
6223
6224
6225
6226
6227
6228
6229
6230
6231
6232
6233
6234
6235
6236
6237
6238
6239
6240
6241
6242
6243
6244
6245
6246
6247
6248
6249
6250
6251
6252
6253
6254
6255
6256
6257
6258
6259
6260
6261
6262
6263
6264
6265
6266
6267
6268
6269
6270
6271
6272
6273
6274
6275
6276
6277
6278
6279
6280
6281
6282
6283
6284
6285
6286
6287
6288
6289
6290
6291
6292
6293
6294
6295
6296
6297
6298
6299
6300
6301
6302
6303
6304
6305
6306
6307
6308
6309
6310
6311
6312
6313
6314
6315
6316
6317
6318
6319
6320
6321
6322
6323
6324
6325
6326
6327
6328
6329
6330
6331
6332
6333
6334
6335
6336
6337
6338
6339
6340
6341
6342
6343
6344
6345
6346
6347
6348
6349
6350
6351
6352
6353
6354
6355
6356
6357
6358
6359
6360
6361
6362
6363
6364
6365
6366
6367
6368
6369
6370
6371
6372
6373
6374
6375
6376
6377
6378
6379
6380
6381
6382
6383
6384
6385
6386
6387
6388
6389
6390
6391
6392
6393
6394
6395
6396
6397
6398
6399
6400
6401
6402
6403
6404
6405
6406
6407
6408
6409
6410
6411
6412
6413
6414
6415
6416
6417
6418
6419
6420
6421
6422
6423
6424
6425
6426
6427
6428
6429
6430
6431
6432
6433
6434
6435
6436
6437
6438
6439
6440
6441
6442
6443
6444
6445
6446
6447
6448
6449
6450
6451
6452
6453
6454
6455
6456
6457
6458
6459
6460
6461
6462
6463
6464
6465
6466
6467
6468
6469
6470
6471
6472
6473
6474
6475
6476
6477
6478
6479
6480
6481
6482
6483
6484
6485
6486
6487
6488
6489
6490
6491
6492
6493
6494
6495
6496
6497
6498
6499
6500
6501
6502
6503
6504
6505
6506
6507
6508
6509
6510
6511
6512
6513
6514
6515
6516
6517
6518
6519
6520
6521
6522
6523
6524
6525
6526
6527
6528
6529
6530
6531
6532
6533
6534
6535
6536
6537
6538
6539
6540
6541
6542
6543
6544
6545
6546
6547
6548
6549
6550
6551
6552
6553
6554
6555
6556
6557
6558
6559
6560
6561
6562
6563
6564
6565
6566
6567
6568
6569
6570
6571
6572
6573
6574
6575
6576
6577
6578
6579
6580
6581
6582
6583
6584
6585
6586
6587
6588
6589
6590
6591
6592
6593
6594
6595
6596
6597
6598
6599
6600
6601
6602
6603
6604
6605
6606
6607
6608
6609
6610
6611
6612
6613
6614
6615
6616
6617
6618
6619
6620
6621
6622
6623
6624
6625
6626
6627
6628
6629
6630
6631
6632
6633
6634
6635
6636
6637
6638
6639
6640
6641
6642
6643
6644
6645
6646
6647
6648
6649
6650
6651
6652
6653
6654
6655
6656
6657
6658
6659
6660
6661
6662
6663
6664
6665
6666
6667
6668
6669
6670
6671
6672
6673
6674
6675
6676
6677
6678
6679
6680
6681
6682
6683
6684
6685
6686
6687
6688
6689
6690
6691
6692
6693
6694
6695
6696
6697
6698
6699
6700
6701
6702
6703
6704
6705
6706
6707
6708
6709
6710
6711
6712
6713
6714
6715
6716
6717
6718
6719
6720
6721
6722
6723
6724
6725
6726
6727
6728
6729
6730
6731
6732
6733
6734
6735
6736
6737
6738
6739
6740
6741
6742
6743
6744
6745
6746
6747
6748
6749
6750
6751
6752
6753
6754
6755
6756
6757
6758
6759
6760
6761
6762
6763
6764
6765
6766
6767
6768
6769
6770
6771
6772
6773
6774
6775
6776
6777
6778
6779
6780
6781
6782
6783
6784
6785
6786
6787
6788
6789
6790
6791
6792
6793
6794
6795
6796
6797
6798
6799
6800
6801
6802
6803
6804
6805
6806
6807
6808
6809
6810
6811
6812
6813
6814
6815
6816
6817
6818
6819
6820
6821
6822
6823
6824
6825
6826
6827
6828
6829
6830
6831
6832
6833
6834
6835
6836
6837
6838
6839
6840
6841
6842
6843
6844
6845
6846
6847
6848
6849
6850
6851
6852
6853
6854
6855
6856
6857
6858
6859
6860
6861
6862
6863
6864
6865
6866
6867
6868
6869
6870
6871
6872
6873
6874
6875
6876
6877
6878
6879
6880
6881
6882
6883
6884
6885
6886
6887
6888
6889
6890
6891
6892
6893
6894
6895
6896
6897
6898
6899
6900
6901
6902
6903
6904
6905
6906
6907
6908
6909
6910
6911
6912
6913
6914
6915
6916
6917
6918
6919
6920
6921
6922
6923
6924
6925
6926
6927
6928
6929
6930
6931
6932
6933
6934
6935
6936
6937
6938
6939
6940
6941
6942
6943
6944
6945
6946
6947
6948
6949
6950
6951
6952
6953
6954
6955
6956
6957
6958
6959
6960
6961
6962
6963
6964
6965
6966
6967
6968
6969
6970
6971
6972
6973
6974
6975
6976
6977
6978
6979
6980
6981
6982
6983
6984
6985
6986
6987
6988
6989
6990
6991
6992
6993
6994
6995
6996
6997
6998
6999
7000
7001
7002
7003
7004
7005
7006
7007
7008
7009
7010
7011
7012
7013
7014
7015
7016
7017
7018
7019
7020
7021
7022
7023
7024
7025
7026
7027
7028
7029
7030
7031
7032
7033
7034
7035
7036
7037
7038
7039
7040
7041
7042
7043
7044
7045
7046
7047
7048
7049
7050
7051
7052
7053
7054
7055
7056
7057
7058
7059
7060
7061
7062
7063
7064
7065
7066
7067
7068
7069
7070
7071
7072
7073
7074
7075
7076
7077
7078
7079
7080
7081
7082
7083
7084
7085
7086
7087
7088
7089
7090
7091
7092
7093
7094
7095
7096
7097
7098
7099
7100
7101
7102
7103
7104
7105
7106
7107
7108
7109
7110
7111
7112
7113
7114
7115
7116
7117
7118
7119
7120
7121
7122
7123
7124
7125
7126
7127
7128
7129
7130
7131
7132
7133
7134
7135
7136
7137
7138
7139
7140
7141
7142
7143
7144
7145
7146
7147
7148
7149
7150
7151
7152
7153
7154
7155
7156
7157
7158
7159
7160
7161
7162
7163
7164
7165
7166
7167
7168
7169
7170
7171
7172
7173
7174
7175
7176
7177
7178
7179
7180
7181
7182
7183
7184
7185
7186
7187
7188
7189
7190
7191
7192
7193
7194
7195
7196
7197
7198
7199
7200
7201
7202
7203
7204
7205
7206
7207
7208
7209
7210
7211
7212
7213
7214
7215
7216
7217
7218
7219
7220
7221
7222
7223
7224
7225
7226
7227
7228
7229
7230
7231
7232
7233
7234
7235
7236
7237
7238
7239
7240
7241
7242
7243
7244
7245
7246
7247
7248
7249
7250
7251
7252
7253
7254
7255
7256
7257
7258
7259
7260
7261
7262
7263
7264
7265
7266
7267
7268
7269
7270
7271
7272
7273
7274
7275
7276
7277
7278
7279
7280
7281
7282
7283
7284
7285
7286
7287
7288
7289
7290
7291
7292
7293
7294
7295
7296
7297
7298
7299
7300
7301
7302
7303
7304
7305
7306
7307
7308
7309
7310
7311
7312
7313
7314
7315
7316
7317
7318
7319
7320
7321
7322
7323
7324
7325
7326
7327
7328
7329
7330
7331
7332
7333
7334
7335
7336
7337
7338
7339
7340
7341
7342
7343
7344
7345
7346
7347
7348
7349
7350
7351
7352
7353
7354
7355
7356
7357
7358
7359
7360
7361
7362
7363
7364
7365
7366
7367
7368
7369
7370
7371
7372
7373
7374
7375
7376
7377
7378
7379
7380
7381
7382
7383
7384
7385
7386
7387
7388
7389
7390
7391
7392
7393
7394
7395
7396
7397
7398
7399
7400
7401
7402
7403
7404
7405
7406
7407
7408
7409
7410
7411
7412
7413
7414
7415
7416
7417
7418
7419
7420
7421
7422
7423
7424
7425
7426
7427
7428
7429
7430
7431
7432
7433
7434
7435
7436
7437
7438
7439
7440
7441
7442
7443
7444
7445
7446
7447
7448
7449
7450
7451
7452
7453
7454
7455
7456
7457
7458
7459
7460
7461
7462
7463
7464
7465
7466
7467
7468
7469
7470
7471
7472
7473
7474
7475
7476
7477
7478
7479
7480
7481
7482
7483
7484
7485
7486
7487
7488
7489
7490
7491
7492
7493
7494
7495
7496
7497
7498
7499
7500
7501
7502
7503
7504
7505
7506
7507
7508
7509
7510
7511
7512
7513
7514
7515
7516
7517
7518
7519
7520
7521
7522
7523
7524
7525
7526
7527
7528
7529
7530
7531
7532
7533
7534
7535
7536
7537
7538
7539
7540
7541
7542
7543
7544
7545
7546
7547
7548
7549
7550
7551
7552
7553
7554
7555
7556
7557
7558
7559
7560
7561
7562
7563
7564
7565
7566
7567
7568
7569
7570
7571
7572
7573
7574
7575
7576
7577
7578
7579
7580
7581
7582
7583
7584
7585
7586
7587
7588
7589
7590
7591
7592
7593
7594
7595
7596
7597
7598
7599
7600
7601
7602
7603
7604
7605
7606
7607
7608
7609
7610
7611
7612
7613
7614
7615
7616
7617
7618
7619
7620
7621
7622
7623
7624
7625
7626
7627
7628
7629
7630
7631
7632
7633
7634
7635
7636
7637
7638
7639
7640
7641
7642
7643
7644
7645
7646
7647
7648
7649
7650
7651
7652
7653
7654
7655
7656
7657
7658
7659
7660
7661
7662
7663
7664
7665
7666
7667
7668
7669
7670
7671
7672
7673
7674
7675
7676
7677
7678
7679
7680
7681
7682
7683
7684
7685
7686
7687
7688
7689
7690
7691
7692
7693
7694
7695
7696
7697
7698
7699
7700
7701
7702
7703
7704
7705
7706
7707
7708
7709
7710
7711
7712
7713
7714
7715
7716
7717
7718
7719
7720
7721
7722
7723
7724
7725
7726
7727
7728
7729
7730
7731
7732
7733
7734
7735
7736
7737
7738
7739
7740
7741
7742
7743
7744
7745
7746
7747
7748
7749
7750
7751
7752
7753
7754
7755
7756
7757
7758
7759
7760
7761
7762
7763
7764
7765
7766
7767
7768
7769
7770
7771
7772
7773
7774
7775
7776
7777
7778
7779
7780
7781
7782
7783
7784
7785
7786
7787
7788
7789
7790
7791
7792
7793
7794
7795
7796
7797
7798
7799
7800
7801
7802
7803
7804
7805
7806
7807
7808
7809
7810
7811
7812
7813
7814
7815
7816
7817
7818
7819
7820
7821
7822
7823
7824
7825
7826
7827
7828
7829
7830
7831
7832
7833
7834
7835
7836
7837
7838
7839
7840
7841
7842
7843
7844
7845
7846
7847
7848
7849
7850
7851
7852
7853
7854
7855
7856
7857
7858
7859
7860
7861
7862
7863
7864
7865
7866
7867
7868
7869
7870
7871
7872
7873
7874
7875
7876
7877
7878
7879
7880
7881
7882
7883
7884
7885
7886
7887
7888
7889
7890
7891
7892
7893
7894
7895
7896
7897
7898
7899
7900
7901
7902
7903
7904
7905
7906
7907
7908
7909
7910
7911
7912
7913
7914
7915
7916
7917
7918
7919
7920
7921
7922
7923
7924
7925
7926
7927
7928
7929
7930
7931
7932
7933
7934
7935
7936
7937
7938
7939
7940
7941
7942
7943
7944
7945
7946
7947
7948
7949
7950
7951
7952
7953
7954
7955
7956
7957
7958
7959
7960
7961
7962
7963
7964
7965
7966
7967
7968
7969
7970
7971
7972
7973
7974
7975
7976
7977
7978
7979
7980
7981
7982
7983
7984
7985
7986
7987
7988
7989
7990
7991
7992
7993
7994
7995
7996
7997
7998
7999
8000
8001
8002
8003
8004
8005
8006
8007
8008
8009
8010
8011
8012
8013
8014
8015
8016
8017
8018
8019
8020
8021
8022
8023
8024
8025
8026
8027
8028
8029
8030
8031
8032
8033
8034
8035
8036
8037
8038
8039
8040
8041
8042
8043
8044
8045
8046
8047
8048
8049
8050
8051
8052
8053
8054
8055
8056
8057
8058
8059
8060
8061
8062
8063
8064
8065
8066
8067
8068
8069
8070
8071
8072
8073
8074
8075
8076
8077
8078
8079
8080
8081
8082
8083
8084
8085
8086
8087
8088
8089
8090
8091
8092
8093
8094
8095
8096
8097
8098
8099
8100
8101
8102
8103
8104
8105
8106
8107
8108
8109
8110
8111
8112
8113
8114
8115
8116
8117
8118
8119
8120
8121
8122
8123
8124
8125
8126
8127
8128
8129
8130
8131
8132
8133
8134
8135
8136
8137
8138
8139
8140
8141
8142
8143
8144
8145
8146
8147
8148
8149
8150
8151
8152
8153
8154
8155
8156
8157
8158
8159
8160
8161
8162
8163
8164
8165
8166
8167
8168
8169
8170
8171
8172
8173
8174
8175
8176
8177
8178
8179
8180
8181
8182
8183
8184
8185
8186
8187
8188
8189
8190
8191
8192
8193
8194
8195
8196
8197
8198
8199
8200
8201
8202
8203
8204
8205
8206
8207
8208
8209
8210
8211
8212
8213
8214
8215
8216
8217
8218
8219
8220
8221
8222
8223
8224
8225
8226
8227
8228
8229
8230
8231
8232
8233
8234
8235
8236
8237
8238
8239
8240
8241
8242
8243
8244
8245
8246
8247
8248
8249
8250
8251
8252
8253
8254
8255
8256
8257
8258
8259
8260
8261
8262
8263
8264
8265
8266
8267
8268
8269
8270
8271
8272
8273
8274
8275
8276
8277
8278
8279
8280
8281
8282
8283
8284
8285
8286
8287
8288
8289
8290
8291
8292
8293
8294
8295
8296
8297
8298
8299
8300
8301
8302
8303
8304
8305
8306
8307
8308
8309
8310
8311
8312
8313
8314
8315
8316
8317
8318
8319
8320
8321
8322
8323
8324
8325
8326
8327
8328
8329
8330
8331
8332
8333
8334
8335
8336
8337
8338
8339
8340
8341
8342
8343
8344
8345
8346
8347
8348
8349
8350
8351
8352
8353
8354
8355
8356
8357
8358
8359
8360
8361
8362
8363
8364
8365
8366
8367
8368
8369
8370
8371
8372
8373
8374
8375
8376
8377
8378
8379
8380
8381
8382
8383
8384
8385
8386
8387
8388
8389
8390
8391
8392
8393
8394
8395
8396
8397
8398
8399
8400
8401
8402
8403
8404
8405
8406
8407
8408
8409
8410
8411
8412
8413
8414
8415
8416
8417
8418
8419
8420
8421
8422
8423
8424
8425
8426
8427
8428
8429
8430
8431
8432
8433
8434
8435
8436
8437
8438
8439
8440
8441
8442
8443
8444
8445
8446
8447
8448
8449
8450
8451
8452
8453
8454
8455
8456
8457
8458
8459
8460
8461
8462
8463
8464
8465
8466
8467
8468
8469
8470
8471
8472
8473
8474
8475
8476
8477
8478
8479
8480
8481
8482
8483
8484
8485
8486
8487
8488
8489
8490
8491
8492
8493
8494
8495
8496
8497
8498
8499
8500
8501
8502
8503
8504
8505
8506
8507
8508
8509
8510
8511
8512
8513
8514
8515
8516
8517
8518
8519
8520
8521
8522
8523
8524
8525
8526
8527
8528
8529
8530
8531
8532
8533
8534
8535
8536
8537
8538
8539
8540
8541
8542
8543
8544
8545
8546
8547
8548
8549
8550
8551
8552
8553
8554
8555
8556
8557
8558
8559
8560
8561
8562
8563
8564
8565
8566
8567
8568
8569
8570
8571
8572
8573
8574
8575
8576
8577
8578
8579
8580
8581
8582
8583
8584
8585
8586
8587
8588
8589
8590
8591
8592
8593
8594
8595
8596
8597
8598
8599
8600
8601
8602
8603
8604
8605
8606
8607
8608
8609
8610
8611
8612
8613
8614
8615
8616
8617
8618
8619
8620
8621
8622
8623
8624
8625
8626
8627
8628
8629
8630
8631
8632
8633
8634
8635
8636
8637
8638
8639
8640
8641
8642
8643
8644
8645
8646
8647
8648
8649
8650
8651
8652
8653
8654
8655
8656
8657
8658
8659
8660
8661
8662
8663
8664
8665
8666
8667
8668
8669
8670
8671
8672
8673
8674
8675
8676
8677
8678
8679
8680
8681
8682
8683
8684
8685
8686
8687
8688
8689
8690
8691
8692
8693
8694
8695
8696
8697
8698
8699
8700
8701
8702
8703
8704
8705
8706
8707
8708
8709
8710
8711
8712
8713
8714
8715
8716
8717
8718
8719
8720
8721
8722
8723
8724
8725
8726
8727
8728
8729
8730
8731
8732
8733
8734
8735
8736
8737
8738
8739
8740
8741
8742
8743
8744
8745
8746
8747
8748
8749
8750
8751
8752
8753
8754
8755
8756
8757
8758
8759
8760
8761
8762
8763
8764
8765
8766
8767
8768
8769
8770
8771
8772
8773
8774
8775
8776
8777
8778
8779
8780
8781
8782
8783
8784
8785
8786
8787
8788
8789
8790
8791
8792
8793
8794
8795
8796
8797
8798
8799
8800
8801
8802
8803
8804
8805
8806
8807
8808
8809
8810
8811
8812
8813
8814
8815
8816
8817
8818
8819
8820
8821
8822
8823
8824
8825
8826
8827
8828
8829
8830
8831
8832
8833
8834
8835
8836
8837
8838
8839
8840
8841
8842
8843
8844
8845
8846
8847
8848
8849
8850
8851
8852
8853
8854
8855
8856
8857
8858
8859
8860
8861
8862
8863
8864
8865
8866
8867
8868
8869
8870
8871
8872
8873
8874
8875
8876
8877
8878
8879
8880
8881
8882
8883
8884
8885
8886
8887
8888
8889
8890
8891
8892
8893
8894
8895
8896
8897
8898
8899
8900
8901
8902
8903
8904
8905
8906
8907
8908
8909
8910
8911
8912
8913
8914
8915
8916
8917
8918
8919
8920
8921
8922
8923
8924
8925
8926
8927
8928
8929
8930
8931
8932
8933
8934
8935
8936
8937
8938
8939
8940
8941
8942
8943
8944
8945
8946
8947
8948
8949
8950
8951
8952
8953
8954
8955
8956
8957
8958
8959
8960
8961
8962
8963
8964
8965
8966
8967
8968
8969
8970
8971
8972
8973
8974
8975
8976
8977
8978
8979
8980
8981
8982
8983
8984
8985
8986
8987
8988
8989
8990
8991
8992
8993
8994
8995
8996
8997
8998
8999
9000
9001
9002
9003
9004
9005
9006
9007
9008
9009
9010
9011
9012
9013
9014
9015
9016
9017
9018
9019
9020
9021
9022
9023
9024
9025
9026
9027
9028
9029
9030
9031
9032
9033
9034
9035
9036
9037
9038
9039
9040
9041
9042
9043
9044
9045
9046
9047
9048
9049
9050
9051
9052
9053
9054
9055
9056
9057
9058
9059
9060
9061
9062
9063
9064
9065
9066
9067
9068
9069
9070
9071
9072
9073
9074
9075
9076
9077
9078
9079
9080
9081
9082
9083
9084
9085
9086
9087
9088
9089
9090
9091
9092
9093
9094
9095
9096
9097
9098
9099
9100
9101
9102
9103
9104
9105
9106
9107
9108
9109
9110
9111
9112
9113
9114
9115
9116
9117
9118
9119
9120
9121
9122
9123
9124
9125
9126
9127
9128
9129
9130
9131
9132
9133
9134
9135
9136
9137
9138
9139
9140
9141
9142
9143
9144
9145
9146
9147
9148
9149
9150
9151
9152
9153
9154
9155
9156
9157
9158
9159
9160
9161
9162
9163
9164
9165
9166
9167
9168
9169
9170
9171
9172
9173
9174
9175
9176
9177
9178
9179
9180
9181
9182
9183
9184
9185
9186
9187
9188
9189
9190
9191
9192
9193
9194
9195
9196
9197
9198
9199
9200
9201
9202
9203
9204
9205
9206
9207
9208
9209
9210
9211
9212
9213
9214
9215
9216
9217
9218
9219
9220
9221
9222
9223
9224
9225
9226
9227
9228
9229
9230
9231
9232
9233
9234
9235
9236
9237
9238
9239
9240
9241
9242
9243
9244
9245
9246
9247
9248
9249
9250
9251
9252
9253
9254
9255
9256
9257
9258
9259
9260
9261
9262
9263
9264
9265
9266
9267
9268
9269
9270
9271
9272
9273
9274
9275
9276
9277
9278
9279
9280
9281
9282
9283
9284
9285
9286
9287
9288
9289
9290
9291
9292
9293
9294
9295
9296
9297
9298
9299
9300
9301
9302
9303
9304
9305
9306
9307
9308
9309
9310
9311
9312
9313
9314
9315
9316
9317
9318
9319
9320
9321
9322
9323
9324
9325
9326
9327
9328
9329
9330
9331
9332
9333
9334
9335
9336
9337
9338
9339
9340
9341
9342
9343
9344
9345
9346
9347
9348
9349
9350
9351
9352
9353
9354
9355
9356
9357
9358
9359
9360
9361
9362
9363
9364
9365
9366
9367
9368
9369
9370
9371
9372
9373
9374
9375
9376
9377
9378
9379
9380
9381
9382
9383
9384
9385
9386
9387
9388
9389
9390
9391
9392
9393
9394
9395
9396
9397
9398
9399
9400
9401
9402
9403
9404
9405
9406
9407
9408
9409
9410
9411
9412
9413
9414
9415
9416
9417
9418
9419
9420
9421
9422
9423
9424
9425
9426
9427
9428
9429
9430
9431
9432
9433
9434
9435
9436
9437
9438
9439
9440
9441
9442
9443
9444
9445
9446
9447
9448
9449
9450
9451
9452
9453
9454
9455
9456
9457
9458
9459
9460
9461
9462
9463
9464
9465
9466
9467
9468
9469
9470
9471
9472
9473
9474
9475
9476
9477
9478
9479
9480
9481
9482
9483
9484
9485
9486
9487
9488
9489
9490
9491
9492
9493
9494
9495
9496
9497
9498
9499
9500
9501
9502
9503
9504
9505
9506
9507
9508
9509
9510
9511
9512
9513
9514
9515
9516
9517
9518
9519
9520
9521
9522
9523
9524
9525
9526
9527
9528
9529
9530
9531
9532
9533
9534
9535
9536
9537
9538
9539
9540
9541
9542
9543
9544
9545
9546
9547
9548
9549
9550
9551
9552
9553
9554
9555
9556
9557
9558
9559
9560
9561
9562
9563
9564
9565
9566
9567
9568
9569
9570
9571
9572
9573
9574
9575
9576
9577
9578
9579
9580
9581
9582
9583
9584
9585
9586
9587
9588
9589
9590
9591
9592
9593
9594
9595
9596
9597
9598
9599
9600
9601
9602
9603
9604
9605
9606
9607
9608
9609
9610
9611
9612
9613
9614
9615
9616
9617
9618
9619
9620
9621
9622
9623
9624
9625
9626
9627
9628
9629
9630
9631
9632
9633
9634
9635
9636
9637
9638
9639
9640
9641
9642
9643
9644
9645
9646
9647
9648
9649
9650
9651
9652
9653
9654
9655
9656
9657
9658
9659
9660
9661
9662
9663
9664
9665
9666
9667
9668
9669
9670
9671
9672
9673
9674
9675
9676
9677
9678
9679
9680
9681
9682
9683
9684
9685
9686
9687
9688
9689
9690
9691
9692
9693
9694
9695
9696
9697
9698
9699
9700
9701
9702
9703
9704
9705
9706
9707
9708
9709
9710
9711
9712
9713
9714
9715
9716
9717
9718
9719
9720
9721
9722
9723
9724
9725
9726
9727
9728
9729
9730
9731
9732
9733
9734
9735
9736
9737
9738
9739
9740
9741
9742
9743
9744
9745
9746
9747
9748
9749
9750
9751
9752
9753
9754
9755
9756
9757
9758
9759
9760
9761
9762
9763
9764
9765
9766
9767
9768
9769
9770
9771
9772
9773
9774
9775
9776
9777
9778
9779
9780
9781
9782
9783
9784
9785
9786
9787
9788
9789
9790
9791
9792
9793
9794
9795
9796
9797
9798
9799
9800
9801
9802
9803
9804
9805
9806
9807
9808
9809
9810
9811
9812
9813
9814
9815
9816
9817
9818
9819
9820
9821
9822
9823
9824
9825
9826
9827
9828
9829
9830
9831
9832
9833
9834
9835
9836
9837
9838
9839
9840
9841
9842
9843
9844
9845
9846
9847
9848
9849
9850
9851
9852
9853
9854
9855
9856
9857
9858
9859
9860
9861
9862
9863
9864
9865
9866
9867
9868
9869
9870
9871
9872
9873
9874
9875
9876
9877
9878
9879
9880
9881
9882
9883
9884
9885
9886
9887
9888
9889
9890
9891
9892
9893
9894
9895
9896
9897
9898
9899
9900
9901
9902
9903
9904
9905
9906
9907
9908
9909
9910
9911
9912
9913
9914
9915
9916
9917
9918
9919
9920
9921
9922
9923
9924
9925
9926
9927
9928
9929
9930
9931
9932
9933
9934
9935
9936
9937
9938
9939
9940
9941
9942
9943
9944
9945
9946
9947
9948
9949
9950
9951
9952
9953
9954
9955
9956
9957
9958
9959
9960
9961
9962
9963
9964
9965
9966
9967
9968
9969
9970
9971
9972
9973
9974
9975
9976
9977
9978
9979
9980
9981
9982
9983
9984
9985
9986
9987
9988
9989
9990
9991
9992
9993
9994
9995
9996
9997
9998
9999
10000
10001
10002
10003
10004
10005
10006
10007
10008
10009
10010
10011
10012
10013
10014
10015
10016
10017
10018
10019
10020
10021
10022
10023
10024
10025
10026
10027
10028
10029
10030
10031
10032
10033
10034
10035
10036
10037
10038
10039
10040
10041
10042
10043
10044
10045
10046
10047
10048
10049
10050
10051
10052
10053
10054
10055
10056
10057
10058
10059
10060
10061
10062
10063
10064
10065
10066
10067
10068
10069
10070
10071
10072
10073
10074
10075
10076
10077
10078
10079
10080
10081
10082
10083
10084
10085
10086
10087
10088
10089
10090
10091
10092
10093
10094
10095
10096
10097
10098
10099
10100
10101
10102
10103
10104
10105
10106
10107
10108
10109
10110
10111
10112
10113
10114
10115
10116
10117
10118
10119
10120
10121
10122
10123
10124
10125
10126
10127
10128
10129
10130
10131
10132
10133
10134
10135
10136
10137
10138
10139
10140
10141
10142
10143
10144
10145
10146
10147
10148
10149
10150
10151
10152
10153
10154
10155
10156
10157
10158
10159
10160
10161
10162
10163
10164
10165
10166
10167
10168
10169
10170
10171
10172
10173
10174
10175
10176
10177
10178
10179
10180
10181
10182
10183
10184
10185
10186
10187
10188
10189
10190
10191
10192
10193
10194
10195
10196
10197
10198
10199
10200
10201
10202
10203
10204
10205
10206
10207
10208
10209
10210
10211
10212
10213
10214
10215
10216
10217
10218
10219
10220
10221
10222
10223
10224
10225
10226
10227
10228
10229
10230
10231
10232
10233
10234
10235
10236
10237
10238
10239
10240
10241
10242
10243
10244
10245
10246
10247
10248
10249
10250
10251
10252
10253
10254
10255
10256
10257
10258
10259
10260
10261
10262
10263
10264
10265
10266
10267
10268
10269
10270
10271
10272
10273
10274
10275
10276
10277
10278
10279
10280
10281
10282
10283
10284
10285
10286
10287
10288
10289
10290
10291
10292
10293
10294
10295
10296
10297
10298
10299
10300
10301
10302
10303
10304
10305
10306
10307
10308
10309
10310
10311
10312
10313
10314
10315
10316
10317
10318
10319
10320
10321
10322
10323
10324
10325
10326
10327
10328
10329
10330
10331
10332
10333
10334
10335
10336
10337
10338
10339
10340
10341
10342
10343
10344
10345
10346
10347
10348
10349
10350
10351
10352
10353
10354
10355
10356
10357
10358
10359
10360
10361
10362
10363
10364
10365
10366
10367
10368
10369
10370
10371
10372
10373
10374
10375
10376
10377
10378
10379
10380
10381
10382
10383
10384
10385
10386
10387
10388
10389
10390
10391
10392
10393
10394
10395
10396
10397
10398
10399
10400
10401
10402
10403
10404
10405
10406
10407
10408
10409
10410
10411
10412
10413
10414
10415
10416
10417
10418
10419
10420
10421
10422
10423
10424
10425
10426
10427
10428
10429
10430
10431
10432
10433
10434
10435
10436
10437
10438
10439
10440
10441
10442
10443
10444
10445
10446
10447
10448
10449
10450
10451
10452
10453
10454
10455
10456
10457
10458
10459
10460
10461
10462
10463
10464
10465
10466
10467
10468
10469
10470
10471
10472
10473
10474
10475
10476
10477
10478
10479
10480
10481
10482
10483
10484
10485
10486
10487
10488
10489
10490
10491
10492
10493
10494
10495
10496
10497
10498
10499
10500
10501
10502
10503
10504
10505
10506
10507
10508
10509
10510
10511
10512
10513
10514
10515
10516
10517
10518
10519
10520
10521
10522
10523
10524
10525
10526
10527
10528
10529
10530
10531
10532
10533
10534
10535
10536
10537
10538
10539
10540
10541
10542
10543
10544
10545
10546
10547
10548
10549
10550
10551
10552
10553
10554
10555
10556
10557
10558
10559
10560
10561
10562
10563
10564
10565
10566
10567
10568
10569
10570
10571
10572
10573
10574
10575
10576
10577
10578
10579
10580
10581
10582
10583
10584
10585
10586
10587
10588
10589
10590
10591
10592
10593
10594
10595
10596
10597
10598
10599
10600
10601
10602
10603
10604
10605
10606
10607
10608
10609
10610
10611
10612
10613
10614
10615
10616
10617
10618
10619
10620
10621
10622
10623
10624
10625
10626
10627
10628
10629
10630
10631
10632
10633
10634
10635
10636
10637
10638
10639
10640
10641
10642
10643
10644
10645
10646
10647
10648
10649
10650
10651
10652
10653
10654
10655
10656
10657
10658
10659
10660
10661
10662
10663
10664
10665
10666
10667
10668
10669
10670
10671
10672
10673
10674
10675
10676
10677
10678
10679
10680
10681
10682
10683
10684
10685
10686
10687
10688
10689
10690
10691
10692
10693
10694
10695
10696
10697
10698
10699
10700
10701
10702
10703
10704
10705
10706
10707
10708
10709
10710
10711
10712
10713
10714
10715
10716
10717
10718
10719
10720
10721
10722
10723
10724
10725
10726
10727
10728
10729
10730
10731
10732
10733
10734
10735
10736
10737
10738
10739
10740
10741
10742
10743
10744
10745
10746
10747
10748
10749
10750
10751
10752
10753
10754
10755
10756
10757
10758
10759
10760
10761
10762
10763
10764
10765
10766
10767
10768
10769
10770
10771
10772
10773
10774
10775
10776
10777
10778
10779
10780
10781
10782
10783
10784
10785
10786
10787
10788
10789
10790
10791
10792
10793
10794
10795
10796
10797
10798
10799
10800
10801
10802
10803
10804
10805
10806
10807
10808
10809
10810
10811
10812
10813
10814
10815
10816
10817
10818
10819
10820
10821
10822
10823
10824
10825
10826
10827
10828
10829
10830
10831
10832
10833
10834
10835
10836
10837
10838
10839
10840
10841
10842
10843
10844
10845
10846
10847
10848
10849
10850
10851
10852
10853
10854
10855
10856
10857
10858
10859
10860
10861
10862
10863
10864
10865
10866
10867
10868
10869
10870
10871
10872
10873
10874
10875
10876
10877
10878
10879
10880
10881
10882
10883
10884
10885
10886
10887
10888
10889
10890
10891
10892
10893
10894
10895
10896
10897
10898
10899
10900
10901
10902
10903
10904
10905
10906
10907
10908
10909
10910
10911
10912
10913
10914
10915
10916
10917
10918
10919
10920
10921
10922
10923
10924
10925
10926
10927
10928
10929
10930
10931
10932
10933
10934
10935
10936
10937
10938
10939
10940
10941
10942
10943
10944
10945
10946
10947
10948
10949
10950
10951
10952
10953
10954
10955
10956
10957
10958
10959
10960
10961
10962
10963
10964
10965
10966
10967
10968
10969
10970
10971
10972
10973
10974
10975
10976
10977
10978
10979
10980
10981
10982
10983
10984
10985
10986
10987
10988
10989
10990
10991
10992
10993
10994
10995
10996
10997
10998
10999
11000
11001
11002
11003
11004
11005
11006
11007
11008
11009
11010
11011
11012
11013
11014
11015
11016
11017
11018
11019
11020
11021
11022
11023
11024
11025
11026
11027
11028
11029
11030
11031
11032
11033
11034
11035
11036
11037
11038
11039
11040
11041
11042
11043
11044
11045
11046
11047
11048
11049
11050
11051
11052
11053
11054
11055
11056
11057
11058
11059
11060
11061
11062
11063
11064
11065
11066
11067
11068
11069
11070
11071
11072
11073
11074
11075
11076
11077
11078
11079
11080
11081
11082
11083
11084
11085
11086
11087
11088
11089
11090
11091
11092
11093
11094
11095
11096
11097
11098
11099
11100
11101
11102
11103
11104
11105
11106
11107
11108
11109
11110
11111
11112
11113
11114
11115
11116
11117
11118
11119
11120
11121
11122
11123
11124
11125
11126
11127
11128
11129
11130
11131
11132
11133
11134
11135
11136
11137
11138
11139
11140
11141
11142
11143
11144
11145
11146
11147
11148
11149
11150
11151
11152
11153
11154
11155
11156
11157
11158
11159
11160
11161
11162
11163
11164
11165
11166
11167
11168
11169
11170
11171
11172
11173
11174
11175
11176
11177
11178
11179
11180
11181
11182
11183
11184
11185
11186
11187
11188
11189
11190
11191
11192
11193
11194
11195
11196
11197
11198
11199
11200
11201
11202
11203
11204
11205
11206
11207
11208
11209
11210
11211
11212
11213
11214
11215
11216
11217
11218
11219
11220
11221
11222
11223
11224
11225
11226
11227
11228
11229
11230
11231
11232
11233
11234
11235
11236
11237
11238
11239
11240
11241
11242
11243
11244
11245
11246
11247
11248
11249
11250
11251
11252
11253
11254
11255
11256
11257
11258
11259
11260
11261
11262
11263
11264
11265
11266
11267
11268
11269
11270
11271
11272
11273
11274
11275
11276
11277
11278
11279
11280
11281
11282
11283
11284
11285
11286
11287
11288
11289
11290
11291
11292
11293
11294
11295
11296
11297
11298
11299
11300
11301
11302
11303
11304
11305
11306
11307
11308
11309
11310
11311
11312
11313
11314
11315
11316
11317
11318
11319
11320
11321
11322
11323
11324
11325
11326
11327
11328
11329
11330
11331
11332
11333
11334
11335
11336
11337
11338
11339
11340
11341
11342
11343
11344
11345
11346
11347
11348
11349
11350
11351
11352
11353
11354
11355
11356
11357
11358
11359
11360
11361
11362
11363
11364
11365
11366
11367
11368
11369
11370
11371
11372
11373
11374
11375
11376
11377
11378
11379
11380
11381
11382
11383
11384
11385
11386
11387
11388
11389
11390
11391
11392
11393
11394
11395
11396
11397
11398
11399
11400
11401
11402
11403
11404
11405
11406
11407
11408
11409
11410
11411
11412
11413
11414
11415
11416
11417
11418
11419
11420
11421
11422
11423
11424
11425
11426
11427
11428
11429
11430
11431
11432
11433
11434
11435
11436
11437
11438
11439
11440
11441
11442
11443
11444
11445
11446
11447
11448
11449
11450
11451
11452
11453
11454
11455
11456
11457
11458
11459
11460
11461
11462
11463
11464
11465
11466
11467
11468
11469
11470
11471
11472
11473
11474
11475
11476
11477
11478
11479
11480
11481
11482
11483
11484
11485
11486
11487
11488
11489
11490
11491
11492
11493
11494
11495
11496
11497
11498
11499
11500
11501
11502
11503
11504
11505
11506
11507
11508
11509
11510
11511
11512
11513
11514
11515
11516
11517
11518
11519
11520
11521
11522
11523
11524
11525
11526
11527
11528
11529
11530
11531
11532
11533
11534
11535
11536
11537
11538
11539
11540
11541
11542
11543
11544
11545
11546
11547
11548
11549
11550
11551
11552
11553
11554
11555
11556
11557
11558
11559
11560
11561
11562
11563
11564
11565
11566
11567
11568
11569
11570
11571
11572
11573
11574
11575
11576
11577
11578
11579
11580
11581
11582
11583
11584
11585
11586
11587
11588
11589
11590
11591
11592
11593
11594
11595
11596
11597
11598
11599
11600
11601
11602
11603
11604
11605
11606
11607
11608
11609
11610
11611
11612
11613
11614
11615
11616
11617
11618
11619
11620
11621
11622
11623
11624
11625
11626
11627
11628
11629
11630
11631
11632
11633
11634
11635
11636
11637
11638
11639
11640
11641
11642
11643
11644
11645
11646
11647
11648
11649
11650
11651
11652
11653
11654
11655
11656
11657
11658
11659
11660
11661
11662
11663
11664
11665
11666
11667
11668
11669
11670
11671
11672
11673
11674
11675
11676
11677
11678
11679
11680
11681
11682
11683
11684
11685
11686
11687
11688
11689
11690
11691
11692
11693
11694
11695
11696
11697
11698
11699
11700
11701
11702
11703
11704
11705
11706
11707
11708
11709
11710
11711
11712
11713
11714
11715
11716
11717
11718
11719
11720
11721
11722
11723
11724
11725
11726
11727
11728
11729
11730
11731
11732
11733
11734
11735
11736
11737
11738
11739
11740
11741
11742
11743
11744
11745
11746
11747
11748
11749
11750
11751
11752
11753
11754
11755
11756
11757
11758
11759
11760
11761
11762
11763
11764
11765
11766
11767
11768
11769
11770
11771
11772
11773
11774
11775
11776
11777
11778
11779
11780
11781
11782
11783
11784
11785
11786
11787
11788
11789
11790
11791
11792
11793
11794
11795
11796
11797
11798
11799
11800
11801
11802
11803
11804
11805
11806
11807
11808
11809
11810
11811
11812
11813
11814
11815
11816
11817
11818
11819
11820
11821
11822
11823
11824
11825
11826
11827
11828
11829
11830
11831
11832
11833
11834
11835
11836
11837
11838
11839
11840
11841
11842
11843
11844
11845
11846
11847
11848
11849
11850
11851
11852
11853
11854
11855
11856
11857
11858
11859
11860
11861
11862
11863
11864
11865
11866
11867
11868
11869
11870
11871
11872
11873
11874
11875
11876
11877
11878
11879
11880
11881
11882
11883
11884
11885
11886
11887
11888
11889
11890
11891
11892
11893
11894
11895
11896
11897
11898
11899
11900
11901
11902
11903
11904
11905
11906
11907
11908
11909
11910
11911
11912
11913
11914
11915
11916
11917
11918
11919
11920
11921
11922
11923
11924
11925
11926
11927
11928
11929
11930
11931
11932
11933
11934
11935
11936
11937
11938
11939
11940
11941
11942
11943
11944
11945
11946
11947
11948
11949
11950
11951
11952
11953
11954
11955
11956
11957
11958
11959
11960
11961
11962
11963
11964
11965
11966
11967
11968
11969
11970
11971
11972
11973
11974
11975
11976
11977
11978
11979
11980
11981
11982
11983
11984
11985
11986
11987
11988
11989
11990
11991
11992
11993
11994
11995
11996
11997
11998
11999
12000
12001
12002
12003
12004
12005
12006
12007
12008
12009
12010
12011
12012
12013
12014
12015
12016
12017
12018
12019
12020
12021
12022
12023
12024
12025
12026
12027
12028
12029
12030
12031
12032
12033
12034
12035
12036
12037
12038
12039
12040
12041
12042
12043
12044
12045
12046
12047
12048
12049
12050
12051
12052
12053
12054
12055
12056
12057
12058
12059
12060
12061
12062
12063
12064
12065
12066
12067
12068
12069
12070
12071
12072
12073
12074
12075
12076
12077
12078
12079
12080
12081
12082
12083
12084
12085
12086
12087
12088
12089
12090
12091
12092
12093
12094
12095
12096
12097
12098
12099
12100
12101
12102
12103
12104
12105
12106
12107
12108
12109
12110
12111
12112
12113
12114
12115
12116
12117
12118
12119
12120
12121
12122
12123
12124
12125
12126
12127
12128
12129
12130
12131
12132
12133
12134
12135
12136
12137
12138
12139
12140
12141
12142
12143
12144
12145
12146
12147
12148
12149
12150
12151
12152
12153
12154
12155
12156
12157
12158
12159
12160
12161
12162
12163
12164
12165
12166
12167
12168
12169
12170
12171
12172
12173
12174
12175
12176
12177
12178
12179
12180
12181
12182
12183
12184
12185
12186
12187
12188
12189
12190
12191
12192
12193
12194
12195
12196
12197
12198
12199
12200
12201
12202
12203
12204
12205
12206
12207
12208
12209
12210
12211
12212
12213
12214
12215
12216
12217
12218
12219
12220
12221
12222
12223
12224
12225
12226
12227
12228
12229
12230
12231
12232
12233
12234
12235
12236
12237
12238
12239
12240
12241
12242
12243
12244
12245
12246
12247
12248
12249
12250
12251
12252
12253
12254
12255
12256
12257
12258
12259
12260
12261
12262
12263
12264
12265
12266
12267
12268
12269
12270
12271
12272
12273
12274
12275
12276
12277
12278
12279
12280
12281
12282
12283
12284
12285
12286
12287
12288
12289
12290
12291
12292
12293
12294
12295
12296
12297
12298
12299
12300
12301
12302
12303
12304
12305
12306
12307
12308
12309
12310
12311
12312
12313
12314
12315
12316
12317
12318
12319
12320
12321
12322
12323
12324
12325
12326
12327
12328
12329
12330
12331
12332
12333
12334
12335
12336
12337
12338
12339
12340
12341
12342
12343
12344
12345
12346
12347
12348
12349
12350
12351
12352
12353
12354
12355
12356
12357
12358
12359
12360
12361
12362
12363
12364
12365
12366
12367
12368
12369
12370
12371
12372
12373
12374
12375
12376
12377
12378
12379
12380
12381
12382
12383
12384
12385
12386
12387
12388
12389
12390
12391
12392
12393
12394
12395
12396
12397
12398
12399
12400
12401
12402
12403
12404
12405
12406
12407
12408
12409
12410
12411
12412
12413
12414
12415
12416
12417
12418
12419
12420
12421
12422
12423
12424
12425
12426
12427
12428
12429
12430
12431
12432
12433
12434
12435
12436
12437
12438
12439
12440
12441
12442
12443
12444
12445
12446
12447
12448
12449
12450
12451
12452
12453
12454
12455
12456
12457
12458
12459
12460
12461
12462
12463
12464
12465
12466
12467
12468
12469
12470
12471
12472
12473
12474
12475
12476
12477
12478
12479
12480
12481
12482
12483
12484
12485
12486
12487
12488
12489
12490
12491
12492
12493
12494
12495
12496
12497
12498
12499
12500
12501
12502
12503
12504
12505
12506
12507
12508
12509
12510
12511
12512
12513
12514
12515
12516
12517
12518
12519
12520
12521
12522
12523
12524
12525
12526
12527
12528
12529
12530
12531
12532
12533
12534
12535
12536
12537
12538
12539
12540
12541
12542
12543
12544
12545
12546
12547
12548
12549
12550
12551
12552
12553
12554
12555
12556
12557
12558
12559
12560
12561
12562
12563
12564
12565
12566
12567
12568
12569
12570
12571
12572
12573
12574
12575
12576
12577
12578
12579
12580
12581
12582
12583
12584
12585
12586
12587
12588
12589
12590
12591
12592
12593
12594
12595
12596
12597
12598
12599
12600
12601
12602
12603
12604
12605
12606
12607
12608
12609
12610
12611
12612
12613
12614
12615
12616
12617
12618
12619
12620
12621
12622
12623
12624
12625
12626
12627
12628
12629
12630
12631
12632
12633
12634
12635
12636
12637
12638
12639
12640
12641
12642
12643
12644
12645
12646
12647
12648
12649
12650
12651
12652
12653
12654
12655
12656
12657
12658
12659
12660
12661
12662
12663
12664
12665
12666
12667
12668
12669
12670
12671
12672
12673
12674
12675
12676
12677
12678
12679
12680
12681
12682
12683
12684
12685
12686
12687
12688
12689
12690
12691
12692
12693
12694
12695
12696
12697
12698
12699
12700
12701
12702
12703
12704
12705
12706
12707
12708
12709
12710
12711
12712
12713
12714
12715
12716
12717
12718
12719
12720
12721
12722
12723
12724
12725
12726
12727
12728
12729
12730
12731
12732
12733
12734
12735
12736
12737
12738
12739
12740
12741
12742
12743
12744
12745
12746
12747
12748
12749
12750
12751
12752
12753
12754
12755
12756
12757
12758
12759
12760
12761
12762
12763
12764
12765
12766
12767
12768
12769
12770
12771
12772
12773
12774
12775
12776
12777
12778
12779
12780
12781
12782
12783
12784
12785
12786
12787
12788
12789
12790
12791
12792
12793
12794
12795
12796
12797
12798
12799
12800
12801
12802
12803
12804
12805
12806
12807
12808
12809
12810
12811
12812
12813
12814
12815
12816
12817
12818
12819
12820
12821
12822
12823
12824
12825
12826
12827
12828
12829
12830
12831
12832
12833
12834
12835
12836
12837
12838
12839
12840
12841
12842
12843
12844
12845
12846
12847
12848
12849
12850
12851
12852
12853
12854
12855
12856
12857
12858
12859
12860
12861
12862
12863
12864
12865
12866
12867
12868
12869
12870
12871
12872
12873
12874
12875
12876
12877
12878
12879
12880
12881
12882
12883
12884
12885
12886
12887
12888
12889
12890
12891
12892
12893
12894
12895
12896
12897
12898
12899
12900
12901
12902
12903
12904
12905
12906
12907
12908
12909
12910
12911
12912
12913
12914
12915
12916
12917
12918
12919
12920
12921
12922
12923
12924
12925
12926
12927
12928
12929
12930
12931
12932
12933
12934
12935
12936
12937
12938
12939
12940
12941
12942
12943
12944
12945
12946
12947
12948
12949
12950
12951
12952
12953
12954
12955
12956
12957
12958
12959
12960
12961
12962
12963
12964
12965
12966
12967
12968
12969
12970
12971
12972
12973
12974
12975
12976
12977
12978
12979
12980
12981
12982
12983
12984
12985
12986
12987
12988
12989
12990
12991
12992
12993
12994
12995
12996
12997
12998
12999
13000
13001
13002
13003
13004
13005
13006
13007
13008
13009
13010
13011
13012
13013
13014
13015
13016
13017
13018
13019
13020
13021
13022
13023
13024
13025
13026
13027
13028
13029
13030
13031
13032
13033
13034
13035
13036
13037
13038
13039
13040
13041
13042
13043
13044
13045
13046
13047
13048
13049
13050
13051
13052
13053
13054
13055
13056
13057
13058
13059
13060
13061
13062
13063
13064
13065
13066
13067
13068
13069
13070
13071
13072
13073
13074
13075
13076
13077
13078
13079
13080
13081
13082
13083
13084
13085
13086
13087
13088
13089
13090
13091
13092
13093
13094
13095
13096
13097
13098
13099
13100
13101
13102
13103
13104
13105
13106
13107
13108
13109
13110
13111
13112
13113
13114
13115
13116
13117
13118
13119
13120
13121
13122
13123
13124
13125
13126
13127
13128
13129
13130
13131
13132
13133
13134
13135
13136
13137
13138
13139
13140
13141
13142
13143
13144
13145
13146
13147
13148
13149
13150
13151
13152
13153
13154
13155
13156
13157
13158
13159
13160
13161
13162
13163
13164
13165
13166
13167
13168
13169
13170
13171
13172
13173
13174
13175
13176
13177
13178
13179
13180
13181
13182
13183
13184
13185
13186
13187
13188
13189
13190
13191
13192
13193
13194
13195
13196
13197
13198
13199
13200
13201
13202
13203
13204
13205
13206
13207
13208
13209
13210
13211
13212
13213
13214
13215
13216
13217
13218
13219
13220
13221
13222
13223
13224
13225
13226
13227
13228
13229
13230
13231
13232
13233
13234
13235
13236
13237
13238
13239
13240
13241
13242
13243
13244
13245
13246
13247
13248
13249
13250
13251
13252
13253
13254
13255
13256
13257
13258
13259
13260
13261
13262
13263
13264
13265
13266
13267
13268
13269
13270
13271
13272
13273
13274
13275
13276
13277
13278
13279
13280
13281
13282
13283
13284
13285
13286
13287
13288
13289
13290
13291
13292
13293
13294
13295
13296
13297
13298
13299
13300
13301
13302
13303
13304
13305
13306
13307
13308
13309
13310
13311
13312
13313
13314
13315
13316
13317
13318
13319
13320
13321
13322
13323
13324
13325
13326
13327
13328
13329
13330
13331
13332
13333
13334
13335
13336
13337
13338
13339
13340
13341
13342
13343
13344
13345
13346
13347
13348
13349
13350
13351
13352
13353
13354
13355
13356
13357
13358
13359
13360
13361
13362
13363
13364
13365
13366
13367
13368
13369
13370
13371
13372
13373
13374
13375
13376
13377
13378
13379
13380
13381
13382
13383
13384
13385
13386
13387
13388
13389
13390
13391
13392
13393
13394
13395
13396
13397
13398
13399
13400
13401
13402
13403
13404
13405
13406
13407
13408
13409
13410
13411
13412
13413
13414
13415
13416
13417
13418
13419
13420
13421
13422
13423
13424
13425
13426
13427
13428
13429
13430
13431
13432
13433
13434
13435
13436
13437
13438
13439
13440
13441
13442
13443
13444
13445
13446
13447
13448
13449
13450
13451
13452
13453
13454
13455
13456
13457
13458
13459
13460
13461
13462
13463
13464
13465
13466
13467
13468
13469
13470
13471
13472
13473
13474
13475
13476
13477
13478
13479
13480
13481
13482
13483
13484
13485
13486
13487
13488
13489
13490
13491
13492
13493
13494
13495
13496
13497
13498
13499
13500
13501
13502
13503
13504
13505
13506
13507
13508
13509
13510
13511
13512
13513
13514
13515
13516
13517
13518
13519
13520
13521
13522
13523
13524
13525
13526
13527
13528
13529
13530
13531
13532
13533
13534
13535
13536
13537
13538
13539
13540
13541
13542
13543
13544
13545
13546
13547
13548
13549
13550
13551
13552
13553
13554
13555
13556
13557
13558
13559
13560
13561
13562
13563
13564
13565
13566
13567
13568
13569
13570
13571
13572
13573
13574
13575
13576
13577
13578
13579
13580
13581
13582
13583
13584
13585
13586
13587
13588
13589
13590
13591
13592
13593
13594
13595
13596
13597
13598
13599
13600
13601
13602
13603
13604
13605
13606
13607
13608
13609
13610
13611
13612
13613
13614
13615
13616
13617
13618
13619
13620
13621
13622
13623
13624
13625
13626
13627
13628
13629
13630
13631
13632
13633
13634
13635
13636
13637
13638
13639
13640
13641
13642
13643
13644
13645
13646
13647
13648
13649
13650
13651
13652
13653
13654
13655
13656
13657
13658
13659
13660
13661
13662
13663
13664
13665
13666
13667
13668
13669
13670
13671
13672
13673
13674
13675
13676
13677
13678
13679
13680
13681
13682
13683
13684
13685
13686
13687
13688
13689
13690
13691
13692
13693
13694
13695
13696
13697
13698
13699
13700
13701
13702
13703
13704
13705
13706
13707
13708
13709
13710
13711
13712
13713
13714
13715
13716
13717
13718
13719
13720
13721
13722
13723
13724
13725
13726
13727
13728
13729
13730
13731
13732
13733
13734
13735
13736
13737
13738
13739
13740
13741
13742
13743
13744
13745
13746
13747
13748
13749
13750
13751
13752
13753
13754
13755
13756
13757
13758
13759
13760
13761
13762
13763
13764
13765
13766
13767
13768
13769
13770
13771
13772
13773
13774
13775
13776
13777
13778
13779
13780
13781
13782
13783
13784
13785
13786
13787
13788
13789
13790
13791
13792
13793
13794
13795
13796
13797
13798
13799
13800
13801
13802
13803
13804
13805
13806
13807
13808
13809
13810
13811
13812
13813
13814
13815
13816
13817
13818
13819
13820
13821
13822
13823
13824
13825
13826
13827
13828
13829
13830
13831
13832
13833
13834
13835
13836
13837
13838
13839
13840
13841
13842
13843
13844
13845
13846
13847
13848
13849
13850
13851
13852
13853
13854
13855
13856
13857
13858
13859
13860
13861
13862
13863
13864
13865
13866
13867
13868
13869
13870
13871
13872
13873
13874
13875
13876
13877
13878
13879
13880
13881
13882
13883
13884
13885
13886
13887
13888
13889
13890
13891
13892
13893
13894
13895
13896
13897
13898
13899
13900
13901
13902
13903
13904
13905
13906
13907
13908
13909
13910
13911
13912
13913
13914
13915
13916
13917
13918
13919
13920
13921
13922
13923
13924
13925
13926
13927
13928
13929
13930
13931
13932
13933
13934
13935
13936
13937
13938
13939
13940
13941
13942
13943
13944
13945
13946
13947
13948
13949
13950
13951
13952
13953
13954
13955
13956
13957
13958
13959
13960
13961
13962
13963
13964
13965
13966
13967
13968
13969
13970
13971
13972
13973
13974
13975
13976
13977
13978
13979
13980
13981
13982
13983
13984
13985
13986
13987
13988
13989
13990
13991
13992
13993
13994
13995
13996
13997
13998
13999
14000
14001
14002
14003
14004
14005
14006
14007
14008
14009
14010
14011
14012
14013
14014
14015
14016
14017
14018
14019
14020
14021
14022
14023
14024
14025
14026
14027
14028
14029
14030
14031
14032
14033
14034
14035
14036
14037
14038
14039
14040
14041
14042
14043
14044
14045
14046
14047
14048
14049
14050
14051
14052
14053
14054
14055
14056
14057
14058
14059
14060
14061
14062
14063
14064
14065
14066
14067
14068
14069
14070
14071
14072
14073
14074
14075
14076
14077
14078
14079
14080
14081
14082
14083
14084
14085
14086
14087
14088
14089
14090
14091
14092
14093
14094
14095
14096
14097
14098
14099
14100
14101
14102
14103
14104
14105
14106
14107
14108
14109
14110
14111
14112
14113
14114
14115
14116
14117
14118
14119
14120
14121
14122
14123
14124
14125
14126
14127
14128
14129
14130
14131
14132
14133
14134
14135
14136
14137
14138
14139
14140
14141
14142
14143
14144
14145
14146
14147
14148
14149
14150
14151
14152
14153
14154
14155
14156
14157
14158
14159
14160
14161
14162
14163
14164
14165
14166
14167
14168
14169
14170
14171
14172
14173
14174
14175
14176
14177
14178
14179
14180
14181
14182
14183
14184
14185
14186
14187
14188
14189
14190
14191
14192
14193
14194
14195
14196
14197
14198
14199
14200
14201
14202
14203
14204
14205
14206
14207
14208
14209
14210
14211
14212
14213
14214
14215
14216
14217
14218
14219
14220
14221
14222
14223
14224
14225
14226
14227
14228
14229
14230
14231
14232
14233
14234
14235
14236
14237
14238
14239
14240
14241
14242
14243
14244
14245
14246
14247
14248
14249
14250
14251
14252
14253
14254
14255
14256
14257
14258
14259
14260
14261
14262
14263
14264
14265
14266
14267
14268
14269
14270
14271
14272
14273
14274
14275
14276
14277
14278
14279
14280
14281
14282
14283
14284
14285
14286
14287
14288
14289
14290
14291
14292
14293
14294
14295
14296
14297
14298
14299
14300
14301
14302
14303
14304
14305
14306
14307
14308
14309
14310
14311
14312
14313
14314
14315
14316
14317
14318
14319
14320
14321
14322
14323
14324
14325
14326
14327
14328
14329
14330
14331
14332
14333
14334
14335
14336
14337
14338
14339
14340
14341
14342
14343
14344
14345
14346
14347
14348
14349
14350
14351
14352
14353
14354
14355
14356
14357
14358
14359
14360
14361
14362
14363
14364
14365
14366
14367
14368
14369
14370
14371
14372
14373
14374
14375
14376
14377
14378
14379
14380
14381
14382
14383
14384
14385
14386
14387
14388
14389
14390
14391
14392
14393
14394
14395
14396
14397
14398
14399
14400
14401
14402
14403
14404
14405
14406
14407
14408
14409
14410
14411
14412
14413
14414
14415
14416
14417
14418
14419
14420
14421
14422
14423
14424
14425
14426
14427
14428
14429
14430
14431
14432
14433
14434
14435
14436
14437
14438
14439
14440
14441
14442
14443
14444
14445
14446
14447
14448
14449
14450
14451
14452
14453
14454
14455
14456
14457
14458
14459
14460
14461
14462
14463
14464
14465
14466
14467
14468
14469
14470
14471
14472
14473
14474
14475
14476
14477
14478
14479
14480
14481
14482
14483
14484
14485
14486
14487
14488
14489
14490
14491
14492
14493
14494
14495
14496
14497
14498
14499
14500
14501
14502
14503
14504
14505
14506
14507
14508
14509
14510
14511
14512
14513
14514
14515
14516
14517
14518
14519
14520
14521
14522
14523
14524
14525
14526
14527
14528
14529
14530
14531
14532
14533
14534
14535
14536
14537
14538
14539
14540
14541
14542
14543
14544
14545
14546
14547
14548
14549
14550
14551
14552
14553
14554
14555
14556
14557
14558
14559
14560
14561
14562
14563
14564
14565
14566
14567
14568
14569
14570
14571
14572
14573
14574
14575
14576
14577
14578
14579
14580
14581
14582
14583
14584
14585
14586
14587
14588
14589
14590
14591
14592
14593
14594
14595
14596
14597
14598
14599
14600
14601
14602
14603
14604
14605
14606
14607
14608
14609
14610
14611
14612
14613
14614
14615
14616
14617
14618
14619
14620
14621
14622
14623
14624
14625
14626
14627
14628
14629
14630
14631
14632
14633
14634
14635
14636
14637
14638
14639
14640
14641
14642
14643
14644
14645
14646
14647
14648
14649
14650
14651
14652
14653
14654
14655
14656
14657
14658
14659
14660
14661
14662
14663
14664
14665
14666
14667
14668
14669
14670
14671
14672
14673
14674
14675
14676
14677
14678
14679
14680
14681
14682
14683
14684
14685
14686
14687
14688
14689
14690
14691
14692
14693
14694
14695
14696
14697
14698
14699
14700
14701
14702
14703
14704
14705
14706
14707
14708
14709
14710
14711
14712
14713
14714
14715
14716
14717
14718
14719
14720
14721
14722
14723
14724
14725
14726
14727
14728
14729
14730
14731
14732
14733
14734
14735
14736
14737
14738
14739
14740
14741
14742
14743
14744
14745
14746
14747
14748
14749
14750
14751
14752
14753
14754
14755
14756
14757
14758
14759
14760
14761
14762
14763
14764
14765
14766
14767
14768
14769
14770
14771
14772
14773
14774
14775
14776
14777
14778
14779
14780
14781
14782
14783
14784
14785
14786
14787
14788
14789
14790
14791
14792
14793
14794
14795
14796
14797
14798
14799
14800
14801
14802
14803
14804
14805
14806
14807
14808
14809
14810
14811
14812
14813
14814
14815
14816
14817
14818
14819
14820
14821
14822
14823
14824
14825
14826
14827
14828
14829
14830
14831
14832
14833
14834
14835
14836
14837
14838
14839
14840
14841
14842
14843
14844
14845
14846
14847
14848
14849
14850
14851
14852
14853
14854
14855
14856
14857
14858
14859
14860
14861
14862
14863
14864
14865
14866
14867
14868
14869
14870
14871
14872
14873
14874
14875
14876
14877
14878
14879
14880
14881
14882
14883
14884
14885
14886
14887
14888
14889
14890
14891
14892
14893
14894
14895
14896
14897
14898
14899
14900
14901
14902
14903
14904
14905
14906
14907
14908
14909
14910
14911
14912
14913
14914
14915
14916
14917
14918
14919
14920
14921
14922
14923
14924
14925
14926
14927
14928
14929
14930
14931
14932
14933
14934
14935
14936
14937
14938
14939
14940
14941
14942
14943
14944
14945
14946
14947
14948
14949
14950
14951
14952
14953
14954
14955
14956
14957
14958
14959
14960
14961
14962
14963
14964
14965
14966
14967
14968
14969
14970
14971
14972
14973
14974
14975
14976
14977
14978
14979
14980
14981
14982
14983
14984
14985
14986
14987
14988
14989
14990
14991
14992
14993
14994
14995
14996
14997
14998
14999
15000
15001
15002
15003
15004
15005
15006
15007
15008
15009
15010
15011
15012
15013
15014
15015
15016
15017
15018
15019
15020
15021
15022
15023
15024
15025
15026
15027
15028
15029
15030
15031
15032
15033
15034
15035
15036
15037
15038
15039
15040
15041
15042
15043
15044
15045
15046
15047
15048
15049
15050
15051
15052
15053
15054
15055
15056
15057
15058
15059
15060
15061
15062
15063
15064
15065
15066
15067
15068
15069
15070
15071
15072
15073
15074
15075
15076
15077
15078
15079
15080
15081
15082
15083
15084
15085
15086
15087
15088
15089
15090
15091
15092
15093
15094
15095
15096
15097
15098
15099
15100
15101
15102
15103
15104
15105
15106
15107
15108
15109
15110
15111
15112
15113
15114
15115
15116
15117
15118
15119
15120
15121
15122
15123
15124
15125
15126
15127
15128
15129
15130
15131
15132
15133
15134
15135
15136
15137
15138
15139
15140
15141
15142
15143
15144
15145
15146
15147
15148
15149
15150
15151
15152
15153
15154
15155
15156
15157
15158
15159
15160
15161
15162
15163
15164
15165
15166
15167
15168
15169
15170
15171
15172
15173
15174
15175
15176
15177
15178
15179
15180
15181
15182
15183
15184
15185
15186
15187
15188
15189
15190
15191
15192
15193
15194
15195
15196
15197
15198
15199
15200
15201
15202
15203
15204
15205
15206
15207
15208
15209
15210
15211
15212
15213
15214
15215
15216
15217
15218
15219
15220
15221
15222
15223
15224
15225
15226
15227
15228
15229
15230
15231
15232
15233
15234
15235
15236
15237
15238
15239
15240
15241
15242
15243
15244
15245
15246
15247
15248
15249
15250
15251
15252
15253
15254
15255
15256
15257
15258
15259
15260
15261
15262
15263
15264
15265
15266
15267
15268
15269
15270
15271
15272
15273
15274
15275
15276
15277
15278
15279
15280
15281
15282
15283
15284
15285
15286
15287
15288
15289
15290
15291
15292
15293
15294
15295
15296
15297
15298
15299
15300
15301
15302
15303
15304
15305
15306
15307
15308
15309
15310
15311
15312
15313
15314
15315
15316
15317
15318
15319
15320
15321
15322
15323
15324
15325
15326
15327
15328
15329
15330
15331
15332
15333
15334
15335
15336
15337
15338
15339
15340
15341
15342
15343
15344
15345
15346
15347
15348
15349
15350
15351
15352
15353
15354
15355
15356
15357
15358
15359
15360
15361
15362
15363
15364
15365
15366
15367
15368
15369
15370
15371
15372
15373
15374
15375
15376
15377
15378
15379
15380
15381
15382
15383
15384
15385
15386
15387
15388
15389
15390
15391
15392
15393
15394
15395
15396
15397
15398
15399
15400
15401
15402
15403
15404
15405
15406
15407
15408
15409
15410
15411
15412
15413
15414
15415
15416
15417
15418
15419
15420
15421
15422
15423
15424
15425
15426
15427
15428
15429
15430
15431
15432
15433
15434
15435
15436
15437
15438
15439
15440
15441
15442
15443
15444
15445
15446
15447
15448
15449
15450
15451
15452
15453
15454
15455
15456
15457
15458
15459
15460
15461
15462
15463
15464
15465
15466
15467
15468
15469
15470
15471
15472
15473
15474
15475
15476
15477
15478
15479
15480
15481
15482
15483
15484
15485
15486
15487
15488
15489
15490
15491
15492
15493
15494
15495
15496
15497
15498
15499
15500
15501
15502
15503
15504
15505
15506
15507
15508
15509
15510
15511
15512
15513
15514
15515
15516
15517
15518
15519
15520
15521
15522
15523
15524
15525
15526
15527
15528
15529
15530
15531
15532
15533
15534
15535
15536
15537
15538
15539
15540
15541
15542
15543
15544
15545
15546
15547
15548
15549
15550
15551
15552
15553
15554
15555
15556
15557
15558
15559
15560
15561
15562
15563
15564
15565
15566
15567
15568
15569
15570
15571
15572
15573
15574
15575
15576
15577
15578
15579
15580
15581
15582
15583
15584
15585
15586
15587
15588
15589
15590
15591
15592
15593
15594
15595
15596
15597
15598
15599
15600
15601
15602
15603
15604
15605
15606
15607
15608
15609
15610
15611
15612
15613
15614
15615
15616
15617
15618
15619
15620
15621
15622
15623
15624
15625
15626
15627
15628
15629
15630
15631
15632
15633
15634
15635
15636
15637
15638
15639
15640
15641
15642
15643
15644
15645
15646
15647
15648
15649
15650
15651
15652
15653
15654
15655
15656
15657
15658
15659
15660
15661
15662
15663
15664
15665
15666
15667
15668
15669
15670
15671
15672
15673
15674
15675
15676
15677
15678
15679
15680
15681
15682
15683
15684
15685
15686
15687
15688
15689
15690
15691
15692
15693
15694
15695
15696
15697
15698
15699
15700
15701
15702
15703
15704
15705
15706
15707
15708
15709
15710
15711
15712
15713
15714
15715
15716
15717
15718
15719
15720
15721
15722
15723
15724
15725
15726
15727
15728
15729
15730
15731
15732
15733
15734
15735
15736
15737
15738
15739
15740
15741
15742
15743
15744
15745
15746
15747
15748
15749
15750
15751
15752
15753
15754
15755
15756
15757
15758
15759
15760
15761
15762
15763
15764
15765
15766
15767
15768
15769
15770
15771
15772
15773
15774
15775
15776
15777
15778
15779
15780
15781
15782
15783
15784
15785
15786
15787
15788
15789
15790
15791
15792
15793
15794
15795
15796
15797
15798
15799
15800
15801
15802
15803
15804
15805
15806
15807
15808
15809
15810
15811
15812
15813
15814
15815
15816
15817
15818
15819
15820
15821
15822
15823
15824
15825
15826
15827
15828
15829
15830
15831
15832
15833
15834
15835
15836
15837
15838
15839
15840
15841
15842
15843
15844
15845
15846
15847
15848
15849
15850
15851
15852
15853
15854
15855
15856
15857
15858
15859
15860
15861
15862
15863
15864
15865
15866
15867
15868
15869
15870
15871
15872
15873
15874
15875
15876
15877
15878
15879
15880
15881
15882
15883
15884
15885
15886
15887
15888
15889
15890
15891
15892
15893
15894
15895
15896
15897
15898
15899
15900
15901
15902
15903
15904
15905
15906
15907
15908
15909
15910
15911
15912
15913
15914
15915
15916
15917
15918
15919
15920
15921
15922
15923
15924
15925
15926
15927
15928
15929
15930
15931
15932
15933
15934
15935
15936
15937
15938
15939
15940
15941
15942
15943
15944
15945
15946
15947
15948
15949
15950
15951
15952
15953
15954
15955
15956
15957
15958
15959
15960
15961
15962
15963
15964
15965
15966
15967
15968
15969
15970
15971
15972
15973
15974
15975
15976
15977
15978
15979
15980
15981
15982
15983
15984
15985
15986
15987
15988
15989
15990
15991
15992
15993
15994
15995
15996
15997
15998
15999
16000
16001
16002
16003
16004
16005
16006
16007
16008
16009
16010
16011
16012
16013
16014
16015
16016
16017
16018
16019
16020
16021
16022
16023
16024
16025
16026
16027
16028
16029
16030
16031
16032
16033
16034
16035
16036
16037
16038
16039
16040
16041
16042
16043
16044
16045
16046
16047
16048
16049
16050
16051
16052
16053
16054
16055
16056
16057
16058
16059
16060
16061
16062
16063
16064
16065
16066
16067
16068
16069
16070
16071
16072
16073
16074
16075
16076
16077
16078
16079
16080
16081
16082
16083
16084
16085
16086
16087
16088
16089
16090
16091
16092
16093
16094
16095
16096
16097
16098
16099
16100
16101
16102
16103
16104
16105
16106
16107
16108
16109
16110
16111
16112
16113
16114
16115
16116
16117
16118
16119
16120
16121
16122
16123
16124
16125
16126
16127
16128
16129
16130
16131
16132
16133
16134
16135
16136
16137
16138
16139
16140
16141
16142
16143
16144
16145
16146
16147
16148
16149
16150
16151
16152
16153
16154
16155
16156
16157
16158
16159
16160
16161
16162
16163
16164
16165
16166
16167
16168
16169
16170
16171
16172
16173
16174
16175
16176
16177
16178
16179
16180
16181
16182
16183
16184
16185
16186
16187
16188
16189
16190
16191
16192
16193
16194
16195
16196
16197
16198
16199
16200
16201
16202
16203
16204
16205
16206
16207
16208
16209
16210
16211
16212
16213
16214
16215
16216
16217
16218
16219
16220
16221
16222
16223
16224
16225
16226
16227
16228
16229
16230
16231
16232
16233
16234
16235
16236
16237
16238
16239
16240
16241
16242
16243
16244
16245
16246
16247
16248
16249
16250
16251
16252
16253
16254
16255
16256
16257
16258
16259
16260
16261
16262
16263
16264
16265
16266
16267
16268
16269
16270
16271
16272
16273
16274
16275
16276
16277
16278
16279
16280
16281
16282
16283
16284
16285
16286
16287
16288
16289
16290
16291
16292
16293
16294
16295
16296
16297
16298
16299
16300
16301
16302
16303
16304
16305
16306
16307
16308
16309
16310
16311
16312
16313
16314
16315
16316
16317
16318
16319
16320
16321
16322
16323
16324
16325
16326
16327
16328
16329
16330
16331
16332
16333
16334
16335
16336
16337
16338
16339
16340
16341
16342
16343
16344
16345
16346
16347
16348
16349
16350
16351
16352
16353
16354
16355
16356
16357
16358
16359
16360
16361
16362
16363
16364
16365
16366
16367
16368
16369
16370
16371
16372
16373
16374
16375
16376
16377
16378
16379
16380
16381
16382
16383
16384
16385
16386
16387
16388
16389
16390
16391
16392
16393
16394
16395
16396
16397
16398
16399
16400
16401
16402
16403
16404
16405
16406
16407
16408
16409
16410
16411
16412
16413
16414
16415
16416
16417
16418
16419
16420
16421
16422
16423
16424
16425
16426
16427
16428
16429
16430
16431
16432
16433
16434
16435
16436
16437
16438
16439
16440
16441
16442
16443
16444
16445
16446
16447
16448
16449
16450
16451
16452
16453
16454
16455
16456
16457
16458
16459
16460
16461
16462
16463
16464
16465
16466
16467
16468
16469
16470
16471
16472
16473
16474
16475
16476
16477
16478
16479
16480
16481
16482
16483
16484
16485
16486
16487
16488
16489
16490
16491
16492
16493
16494
16495
16496
16497
16498
16499
16500
16501
16502
16503
16504
16505
16506
16507
16508
16509
16510
16511
16512
16513
16514
16515
16516
16517
16518
16519
16520
16521
16522
16523
16524
16525
16526
16527
16528
16529
16530
16531
16532
16533
16534
16535
16536
16537
16538
16539
16540
16541
16542
16543
16544
16545
16546
16547
16548
16549
16550
16551
16552
16553
16554
16555
16556
16557
16558
16559
16560
16561
16562
16563
16564
16565
16566
16567
16568
16569
16570
16571
16572
16573
16574
16575
16576
16577
16578
16579
16580
16581
16582
16583
16584
16585
16586
16587
16588
16589
16590
16591
16592
16593
16594
16595
16596
16597
16598
16599
16600
16601
16602
16603
16604
16605
16606
16607
16608
16609
16610
16611
16612
16613
16614
16615
16616
16617
16618
16619
16620
16621
16622
16623
16624
16625
16626
16627
16628
16629
16630
16631
16632
16633
16634
16635
16636
16637
16638
16639
16640
16641
16642
16643
16644
16645
16646
16647
16648
16649
16650
16651
16652
16653
16654
16655
16656
16657
16658
16659
16660
16661
16662
16663
16664
16665
16666
16667
16668
16669
16670
16671
16672
16673
16674
16675
16676
16677
16678
16679
16680
16681
16682
16683
16684
16685
16686
16687
16688
16689
16690
16691
16692
16693
16694
16695
16696
16697
16698
16699
16700
16701
16702
16703
16704
16705
16706
16707
16708
16709
16710
16711
16712
16713
16714
16715
16716
16717
16718
16719
16720
16721
16722
16723
16724
16725
16726
16727
16728
16729
16730
16731
16732
16733
16734
16735
16736
16737
16738
16739
16740
16741
16742
16743
16744
16745
16746
16747
16748
16749
16750
16751
16752
16753
16754
16755
16756
16757
16758
16759
16760
16761
16762
16763
16764
16765
16766
16767
16768
16769
16770
16771
16772
16773
16774
16775
16776
16777
16778
16779
16780
16781
16782
16783
16784
16785
16786
16787
16788
16789
16790
16791
16792
16793
16794
16795
16796
16797
16798
16799
16800
16801
16802
16803
16804
16805
16806
16807
16808
16809
16810
16811
16812
16813
16814
16815
16816
16817
16818
16819
16820
16821
16822
16823
16824
16825
16826
16827
16828
16829
16830
16831
16832
16833
16834
16835
16836
16837
16838
16839
16840
16841
16842
16843
16844
16845
16846
16847
16848
16849
16850
16851
16852
16853
16854
16855
16856
16857
16858
16859
16860
16861
16862
16863
16864
16865
16866
16867
16868
16869
16870
16871
16872
16873
16874
16875
16876
16877
16878
16879
16880
16881
16882
16883
16884
16885
16886
16887
16888
16889
16890
16891
16892
16893
16894
16895
16896
16897
16898
16899
16900
16901
16902
16903
16904
16905
16906
16907
16908
16909
16910
16911
16912
16913
16914
16915
16916
16917
16918
16919
16920
16921
16922
16923
16924
16925
16926
16927
16928
16929
16930
16931
16932
16933
16934
16935
16936
16937
16938
16939
16940
16941
16942
16943
16944
16945
16946
16947
16948
16949
16950
16951
16952
16953
16954
16955
16956
16957
16958
16959
16960
16961
16962
16963
16964
16965
16966
16967
16968
16969
16970
16971
16972
16973
16974
16975
16976
16977
16978
16979
16980
16981
16982
16983
16984
16985
16986
16987
16988
16989
16990
16991
16992
16993
16994
16995
16996
16997
16998
16999
17000
17001
17002
17003
17004
17005
17006
17007
17008
17009
17010
17011
17012
17013
17014
17015
17016
17017
17018
17019
17020
17021
17022
17023
17024
17025
17026
17027
17028
17029
17030
17031
17032
17033
17034
17035
17036
17037
17038
17039
17040
17041
17042
17043
17044
17045
17046
17047
17048
17049
17050
17051
17052
17053
17054
17055
17056
17057
17058
17059
17060
17061
17062
17063
17064
17065
17066
17067
17068
17069
17070
17071
17072
17073
17074
17075
17076
17077
17078
17079
17080
17081
17082
17083
17084
17085
17086
17087
17088
17089
17090
17091
17092
17093
17094
17095
17096
17097
17098
17099
17100
17101
17102
17103
17104
17105
17106
17107
17108
17109
17110
17111
17112
17113
17114
17115
17116
17117
17118
17119
17120
17121
17122
17123
17124
17125
17126
17127
17128
17129
17130
17131
17132
17133
17134
17135
17136
17137
17138
17139
17140
17141
17142
17143
17144
17145
17146
17147
17148
17149
17150
17151
17152
17153
17154
17155
17156
17157
17158
17159
17160
17161
17162
17163
17164
17165
17166
17167
17168
17169
17170
17171
17172
17173
17174
17175
17176
17177
17178
17179
17180
17181
17182
17183
17184
17185
17186
17187
17188
17189
17190
17191
17192
17193
17194
17195
17196
17197
17198
17199
17200
17201
17202
17203
17204
17205
17206
17207
17208
17209
17210
17211
17212
17213
17214
17215
17216
17217
17218
17219
17220
17221
17222
17223
17224
17225
17226
17227
17228
17229
17230
17231
17232
17233
17234
17235
17236
17237
17238
17239
17240
17241
17242
17243
17244
17245
17246
17247
17248
17249
17250
17251
17252
17253
17254
17255
17256
17257
17258
17259
17260
17261
17262
17263
17264
17265
17266
17267
17268
17269
17270
17271
17272
17273
17274
17275
17276
17277
17278
17279
17280
17281
17282
17283
17284
17285
17286
17287
17288
17289
17290
17291
17292
17293
17294
17295
17296
17297
17298
17299
17300
17301
17302
17303
17304
17305
17306
17307
17308
17309
17310
17311
17312
17313
17314
17315
17316
17317
17318
17319
17320
17321
17322
17323
17324
17325
17326
17327
17328
17329
17330
17331
17332
17333
17334
17335
17336
17337
17338
17339
17340
17341
17342
17343
17344
17345
17346
17347
17348
17349
17350
17351
17352
17353
17354
17355
17356
17357
17358
17359
17360
17361
17362
17363
17364
17365
17366
17367
17368
17369
17370
17371
17372
17373
17374
17375
17376
17377
17378
17379
17380
17381
17382
17383
17384
17385
17386
17387
17388
17389
17390
17391
17392
17393
17394
17395
17396
17397
17398
17399
17400
17401
17402
17403
17404
17405
17406
17407
17408
17409
17410
17411
17412
17413
17414
17415
17416
17417
17418
17419
17420
17421
17422
17423
17424
17425
17426
17427
17428
17429
17430
17431
17432
17433
17434
17435
17436
17437
17438
17439
17440
17441
17442
17443
17444
17445
17446
17447
17448
17449
17450
17451
17452
17453
17454
17455
17456
17457
17458
17459
17460
17461
17462
17463
17464
17465
17466
17467
17468
17469
17470
17471
17472
17473
17474
17475
17476
17477
17478
17479
17480
17481
17482
17483
17484
17485
17486
17487
17488
17489
17490
17491
17492
17493
17494
17495
17496
17497
17498
17499
17500
17501
17502
17503
17504
17505
17506
17507
17508
17509
17510
17511
17512
17513
17514
17515
17516
17517
17518
17519
17520
17521
17522
17523
17524
17525
17526
17527
17528
17529
17530
17531
17532
17533
17534
17535
17536
17537
17538
17539
17540
17541
17542
17543
17544
17545
17546
17547
17548
17549
17550
17551
17552
17553
17554
17555
17556
17557
17558
17559
17560
17561
17562
17563
17564
17565
17566
17567
17568
17569
17570
17571
17572
17573
17574
17575
17576
17577
17578
17579
17580
17581
17582
17583
17584
17585
17586
17587
17588
17589
17590
17591
17592
17593
17594
17595
17596
17597
17598
17599
17600
17601
17602
17603
17604
17605
17606
17607
17608
17609
17610
17611
17612
17613
17614
17615
17616
17617
17618
17619
17620
17621
17622
17623
17624
17625
17626
17627
17628
17629
17630
17631
17632
17633
17634
17635
17636
17637
17638
17639
17640
17641
17642
17643
17644
17645
17646
17647
17648
17649
17650
17651
17652
17653
17654
17655
17656
17657
17658
17659
17660
17661
17662
17663
17664
17665
17666
17667
17668
17669
17670
17671
17672
17673
17674
17675
17676
17677
17678
17679
17680
17681
17682
17683
17684
17685
17686
17687
17688
17689
17690
17691
17692
17693
17694
17695
17696
17697
17698
17699
17700
17701
17702
17703
17704
17705
17706
17707
17708
17709
17710
17711
17712
17713
17714
17715
17716
17717
17718
17719
17720
17721
17722
17723
17724
17725
17726
17727
17728
17729
17730
17731
17732
17733
17734
17735
17736
17737
17738
17739
17740
17741
17742
17743
17744
17745
17746
17747
17748
17749
17750
17751
17752
17753
17754
17755
17756
17757
17758
17759
17760
17761
17762
17763
17764
17765
17766
17767
17768
17769
17770
17771
17772
17773
17774
17775
17776
17777
17778
17779
17780
17781
17782
17783
17784
17785
17786
17787
17788
17789
17790
17791
17792
17793
17794
17795
17796
17797
17798
17799
17800
17801
17802
17803
17804
17805
17806
17807
17808
17809
17810
17811
17812
17813
17814
17815
17816
17817
17818
17819
17820
17821
17822
17823
17824
17825
17826
17827
17828
17829
17830
17831
17832
17833
17834
17835
17836
17837
17838
17839
17840
17841
17842
17843
17844
17845
17846
17847
17848
17849
17850
17851
17852
17853
17854
17855
17856
17857
17858
17859
17860
17861
17862
17863
17864
17865
17866
17867
17868
17869
17870
17871
17872
17873
17874
17875
17876
17877
17878
17879
17880
17881
17882
17883
17884
17885
17886
17887
17888
17889
17890
17891
17892
17893
17894
17895
17896
17897
17898
17899
17900
17901
17902
17903
17904
17905
17906
17907
17908
17909
17910
17911
17912
17913
17914
17915
17916
17917
17918
17919
17920
17921
17922
17923
17924
17925
17926
17927
17928
17929
17930
17931
17932
17933
17934
17935
17936
17937
17938
17939
17940
17941
17942
17943
17944
17945
17946
17947
17948
17949
17950
17951
17952
17953
17954
17955
17956
17957
17958
17959
17960
17961
17962
17963
17964
17965
17966
17967
17968
17969
17970
17971
17972
17973
17974
17975
17976
17977
17978
17979
17980
17981
17982
17983
17984
17985
17986
17987
17988
17989
17990
17991
17992
17993
17994
17995
17996
17997
17998
17999
18000
18001
18002
18003
18004
18005
18006
18007
18008
18009
18010
18011
18012
18013
18014
18015
18016
18017
18018
18019
18020
18021
18022
18023
18024
18025
18026
18027
18028
18029
18030
18031
18032
18033
18034
18035
18036
18037
18038
18039
18040
18041
18042
18043
18044
18045
18046
18047
18048
18049
18050
18051
18052
18053
18054
18055
18056
18057
18058
18059
18060
18061
18062
18063
18064
18065
18066
18067
18068
18069
18070
18071
18072
18073
18074
18075
18076
18077
18078
18079
18080
18081
18082
18083
18084
18085
18086
18087
18088
18089
18090
18091
18092
18093
18094
18095
18096
18097
18098
18099
18100
18101
18102
18103
18104
18105
18106
18107
18108
18109
18110
18111
18112
18113
18114
18115
18116
18117
18118
18119
18120
18121
18122
18123
18124
18125
18126
18127
18128
18129
18130
18131
18132
18133
18134
18135
18136
18137
18138
18139
18140
18141
18142
18143
18144
18145
18146
18147
18148
18149
18150
18151
18152
18153
18154
18155
18156
18157
18158
18159
18160
18161
18162
18163
18164
18165
18166
18167
18168
18169
18170
18171
18172
18173
18174
18175
18176
18177
18178
18179
18180
18181
18182
18183
18184
18185
18186
18187
18188
18189
18190
18191
18192
18193
18194
18195
18196
18197
18198
18199
18200
18201
18202
18203
18204
18205
18206
18207
18208
18209
18210
18211
18212
18213
18214
18215
18216
18217
18218
18219
18220
18221
18222
18223
18224
18225
18226
18227
18228
18229
18230
18231
18232
18233
18234
18235
18236
18237
18238
18239
18240
18241
18242
18243
18244
18245
18246
18247
18248
18249
18250
18251
18252
18253
18254
18255
18256
18257
18258
18259
18260
18261
18262
18263
18264
18265
18266
18267
18268
18269
18270
18271
18272
18273
18274
18275
18276
18277
18278
18279
18280
18281
18282
18283
18284
18285
18286
18287
18288
18289
18290
18291
18292
18293
18294
18295
18296
18297
18298
18299
18300
18301
18302
18303
18304
18305
18306
18307
18308
18309
18310
18311
18312
18313
18314
18315
18316
18317
18318
18319
18320
18321
18322
18323
18324
18325
18326
18327
18328
18329
18330
18331
18332
18333
18334
18335
18336
18337
18338
18339
18340
18341
18342
18343
18344
18345
18346
18347
18348
18349
18350
18351
18352
18353
18354
18355
18356
18357
18358
18359
18360
18361
18362
18363
18364
18365
18366
18367
18368
18369
18370
18371
18372
18373
18374
18375
18376
18377
18378
18379
18380
18381
18382
18383
18384
18385
18386
18387
18388
18389
18390
18391
18392
18393
18394
18395
18396
18397
18398
18399
18400
18401
18402
18403
18404
18405
18406
18407
18408
18409
18410
18411
18412
18413
18414
18415
18416
18417
18418
18419
18420
18421
18422
18423
18424
18425
18426
18427
18428
18429
18430
18431
18432
18433
18434
18435
18436
18437
18438
18439
18440
18441
18442
18443
18444
18445
18446
18447
18448
18449
18450
18451
18452
18453
18454
18455
18456
18457
18458
18459
18460
18461
18462
18463
18464
18465
18466
18467
18468
18469
18470
18471
18472
18473
18474
18475
18476
18477
18478
18479
18480
18481
18482
18483
18484
18485
18486
18487
18488
18489
18490
18491
18492
18493
18494
18495
18496
18497
18498
18499
18500
18501
18502
18503
18504
18505
18506
18507
18508
18509
18510
18511
18512
18513
18514
18515
18516
18517
18518
18519
18520
18521
18522
18523
18524
18525
18526
18527
18528
18529
18530
18531
18532
18533
18534
18535
18536
18537
18538
18539
18540
18541
18542
18543
18544
18545
18546
18547
18548
18549
18550
18551
18552
18553
18554
18555
18556
18557
18558
18559
18560
18561
18562
18563
18564
18565
18566
18567
18568
18569
18570
18571
18572
18573
18574
18575
18576
18577
18578
18579
18580
18581
18582
18583
18584
18585
18586
18587
18588
18589
18590
18591
18592
18593
18594
18595
18596
18597
18598
18599
18600
18601
18602
18603
18604
18605
18606
18607
18608
18609
18610
18611
18612
18613
18614
18615
18616
18617
18618
18619
18620
18621
18622
18623
18624
18625
18626
18627
18628
18629
18630
18631
18632
18633
18634
18635
18636
18637
18638
18639
18640
18641
18642
18643
18644
18645
18646
18647
18648
18649
18650
18651
18652
18653
18654
18655
18656
18657
18658
18659
18660
18661
18662
18663
18664
18665
18666
18667
18668
18669
18670
18671
18672
18673
18674
18675
18676
18677
18678
18679
18680
18681
18682
18683
18684
18685
18686
18687
18688
18689
18690
18691
18692
18693
18694
18695
18696
18697
18698
18699
18700
18701
18702
18703
18704
18705
18706
18707
18708
18709
18710
18711
18712
18713
18714
18715
18716
18717
18718
18719
18720
18721
18722
18723
18724
18725
18726
18727
18728
18729
18730
18731
18732
18733
18734
18735
18736
18737
18738
18739
18740
18741
18742
18743
18744
18745
18746
18747
18748
18749
18750
18751
18752
18753
18754
18755
18756
18757
18758
18759
18760
18761
18762
18763
18764
18765
18766
18767
18768
18769
18770
18771
18772
18773
18774
18775
18776
18777
18778
18779
18780
18781
18782
18783
18784
18785
18786
18787
18788
18789
18790
18791
18792
18793
18794
18795
18796
18797
18798
18799
18800
18801
18802
18803
18804
18805
18806
18807
18808
18809
18810
18811
18812
18813
18814
18815
18816
18817
18818
18819
18820
18821
18822
18823
18824
18825
18826
18827
18828
18829
18830
18831
18832
18833
18834
18835
18836
18837
18838
18839
18840
18841
18842
18843
18844
18845
18846
18847
18848
18849
18850
18851
18852
18853
18854
18855
18856
18857
18858
18859
18860
18861
18862
18863
18864
18865
18866
18867
18868
18869
18870
18871
18872
18873
18874
18875
18876
18877
18878
18879
18880
18881
18882
18883
18884
18885
18886
18887
18888
18889
18890
18891
18892
18893
18894
18895
18896
18897
18898
18899
18900
18901
18902
18903
18904
18905
18906
18907
18908
18909
18910
18911
18912
18913
18914
18915
18916
18917
18918
18919
18920
18921
18922
18923
18924
18925
18926
18927
18928
18929
18930
18931
18932
18933
18934
18935
18936
18937
18938
18939
18940
18941
18942
18943
18944
18945
18946
18947
18948
18949
18950
18951
18952
18953
18954
18955
18956
18957
18958
18959
18960
18961
18962
18963
18964
18965
18966
18967
18968
18969
18970
18971
18972
18973
18974
18975
18976
18977
18978
18979
18980
18981
18982
18983
18984
18985
18986
18987
18988
18989
18990
18991
18992
18993
18994
18995
18996
18997
18998
18999
19000
19001
19002
19003
19004
19005
19006
19007
19008
19009
19010
19011
19012
19013
19014
19015
19016
19017
19018
19019
19020
19021
19022
19023
19024
19025
19026
19027
19028
19029
19030
19031
19032
19033
19034
19035
19036
19037
19038
19039
19040
19041
19042
19043
19044
19045
19046
19047
19048
19049
19050
19051
19052
19053
19054
19055
19056
19057
19058
19059
19060
19061
19062
19063
19064
19065
19066
19067
19068
19069
19070
19071
19072
19073
19074
19075
19076
19077
19078
19079
19080
19081
19082
19083
19084
19085
19086
19087
19088
19089
19090
19091
19092
19093
19094
19095
19096
19097
19098
19099
19100
19101
19102
19103
19104
19105
19106
19107
19108
19109
19110
19111
19112
19113
19114
19115
19116
19117
19118
19119
19120
19121
19122
19123
19124
19125
19126
19127
19128
19129
19130
19131
19132
19133
19134
19135
19136
19137
19138
19139
19140
19141
19142
19143
19144
19145
19146
19147
19148
19149
19150
19151
19152
19153
19154
19155
19156
19157
19158
19159
19160
19161
19162
19163
19164
19165
19166
19167
19168
19169
19170
19171
19172
19173
19174
19175
19176
19177
19178
19179
19180
19181
19182
19183
19184
19185
19186
19187
19188
19189
19190
19191
19192
19193
19194
19195
19196
19197
19198
19199
19200
19201
19202
19203
19204
19205
19206
19207
19208
19209
19210
19211
19212
19213
19214
19215
19216
19217
19218
19219
19220
19221
19222
19223
19224
19225
19226
19227
19228
19229
19230
19231
19232
19233
19234
19235
19236
19237
19238
19239
19240
19241
19242
19243
19244
19245
19246
19247
19248
19249
19250
19251
19252
19253
19254
19255
19256
19257
19258
19259
19260
19261
19262
19263
19264
19265
19266
19267
19268
19269
19270
19271
19272
19273
19274
19275
19276
19277
19278
19279
19280
19281
19282
19283
19284
19285
19286
19287
19288
19289
19290
19291
19292
19293
19294
19295
19296
19297
19298
19299
19300
19301
19302
19303
19304
19305
19306
19307
19308
19309
19310
19311
19312
19313
19314
19315
19316
19317
19318
19319
19320
19321
19322
19323
19324
19325
19326
19327
19328
19329
19330
19331
19332
19333
19334
19335
19336
19337
19338
19339
19340
19341
19342
19343
19344
19345
19346
19347
19348
19349
19350
19351
19352
19353
19354
19355
19356
19357
19358
19359
19360
19361
19362
19363
19364
19365
19366
19367
19368
19369
19370
19371
19372
19373
19374
19375
19376
19377
19378
19379
19380
19381
19382
19383
19384
19385
19386
19387
19388
19389
19390
19391
19392
19393
19394
19395
19396
19397
19398
19399
19400
19401
19402
19403
19404
19405
19406
19407
19408
19409
19410
19411
19412
19413
19414
19415
19416
19417
19418
19419
19420
19421
19422
19423
19424
19425
19426
19427
19428
19429
19430
19431
19432
19433
19434
19435
19436
19437
19438
19439
19440
19441
19442
19443
19444
19445
19446
19447
19448
19449
19450
19451
19452
19453
19454
19455
19456
19457
19458
19459
19460
19461
19462
19463
19464
19465
19466
19467
19468
19469
19470
19471
19472
19473
19474
19475
19476
19477
19478
19479
19480
19481
19482
19483
19484
19485
19486
19487
19488
19489
19490
19491
19492
19493
19494
19495
19496
19497
19498
19499
19500
19501
19502
19503
19504
19505
19506
19507
19508
19509
19510
19511
19512
19513
19514
19515
19516
19517
19518
19519
19520
19521
19522
19523
19524
19525
19526
19527
19528
19529
19530
19531
19532
19533
19534
19535
19536
19537
19538
19539
19540
19541
19542
19543
19544
19545
19546
19547
19548
19549
19550
19551
19552
19553
19554
19555
19556
19557
19558
19559
19560
19561
19562
19563
19564
19565
19566
19567
19568
19569
19570
19571
19572
19573
19574
19575
19576
19577
19578
19579
19580
19581
19582
19583
19584
19585
19586
19587
19588
19589
19590
19591
19592
19593
19594
19595
19596
19597
19598
19599
19600
19601
19602
19603
19604
19605
19606
19607
19608
19609
19610
19611
19612
19613
19614
19615
19616
19617
19618
19619
19620
19621
19622
19623
19624
19625
19626
19627
19628
19629
19630
19631
19632
19633
19634
19635
19636
19637
19638
19639
19640
19641
19642
19643
19644
19645
19646
19647
19648
19649
19650
19651
19652
19653
19654
19655
19656
19657
19658
19659
19660
19661
19662
19663
19664
19665
19666
19667
19668
19669
19670
19671
19672
19673
19674
19675
19676
19677
19678
19679
19680
19681
19682
19683
19684
19685
19686
19687
19688
19689
19690
19691
19692
19693
19694
19695
19696
19697
19698
19699
19700
19701
19702
19703
19704
19705
19706
19707
19708
19709
19710
19711
19712
19713
19714
19715
19716
19717
19718
19719
19720
19721
19722
19723
19724
19725
19726
19727
19728
19729
19730
19731
19732
19733
19734
19735
19736
19737
19738
19739
19740
19741
19742
19743
19744
19745
19746
19747
19748
19749
19750
19751
19752
19753
19754
19755
19756
19757
19758
19759
19760
19761
19762
19763
19764
19765
19766
19767
19768
19769
19770
19771
19772
19773
19774
19775
19776
19777
19778
19779
19780
19781
19782
19783
19784
19785
19786
19787
19788
19789
19790
19791
19792
19793
19794
19795
19796
19797
19798
19799
19800
19801
19802
19803
19804
19805
19806
19807
19808
19809
19810
19811
19812
19813
19814
19815
19816
19817
19818
19819
19820
19821
19822
19823
19824
19825
19826
19827
19828
19829
19830
19831
19832
19833
19834
19835
19836
19837
19838
19839
19840
19841
19842
19843
19844
19845
19846
19847
19848
19849
19850
19851
19852
19853
19854
19855
19856
19857
19858
19859
19860
19861
19862
19863
19864
19865
19866
19867
19868
19869
19870
19871
19872
19873
19874
19875
19876
19877
19878
19879
19880
19881
19882
19883
19884
19885
19886
19887
19888
19889
19890
19891
19892
19893
19894
19895
19896
19897
19898
19899
19900
19901
19902
19903
19904
19905
19906
19907
19908
19909
19910
19911
19912
19913
19914
19915
19916
19917
19918
19919
19920
19921
19922
19923
19924
19925
19926
19927
19928
19929
19930
19931
19932
19933
19934
19935
19936
19937
19938
19939
19940
19941
19942
19943
19944
19945
19946
19947
19948
19949
19950
19951
19952
19953
19954
19955
19956
19957
19958
19959
19960
19961
19962
19963
19964
19965
19966
19967
19968
19969
19970
19971
19972
19973
19974
19975
19976
19977
19978
19979
19980
19981
19982
19983
19984
19985
19986
19987
19988
19989
19990
19991
19992
19993
19994
19995
19996
19997
19998
19999
20000
20001
20002
20003
20004
20005
20006
20007
20008
20009
20010
20011
20012
20013
20014
20015
20016
20017
20018
20019
20020
20021
20022
20023
20024
20025
20026
20027
20028
20029
20030
20031
20032
20033
20034
20035
20036
20037
20038
20039
20040
20041
20042
20043
20044
20045
20046
20047
20048
20049
20050
20051
20052
20053
20054
20055
20056
20057
20058
20059
20060
20061
20062
20063
20064
20065
20066
20067
20068
20069
20070
20071
20072
20073
20074
20075
20076
20077
20078
20079
20080
20081
20082
20083
20084
20085
20086
20087
20088
20089
20090
20091
20092
20093
20094
20095
20096
20097
20098
20099
20100
20101
20102
20103
20104
20105
20106
20107
20108
20109
20110
20111
20112
20113
20114
20115
20116
20117
20118
20119
20120
20121
20122
20123
20124
20125
20126
20127
20128
20129
20130
20131
20132
20133
20134
20135
20136
20137
20138
20139
20140
20141
20142
20143
20144
20145
20146
20147
20148
20149
20150
20151
20152
20153
20154
20155
20156
20157
20158
20159
20160
20161
20162
20163
20164
20165
20166
20167
20168
20169
20170
20171
20172
20173
20174
20175
20176
20177
20178
20179
20180
20181
20182
20183
20184
20185
20186
20187
20188
20189
20190
20191
20192
20193
20194
20195
20196
20197
20198
20199
20200
20201
20202
20203
20204
20205
20206
20207
20208
20209
20210
20211
20212
20213
20214
20215
20216
20217
20218
20219
20220
20221
20222
20223
20224
20225
20226
20227
20228
20229
20230
20231
20232
20233
20234
20235
20236
20237
20238
20239
20240
20241
20242
20243
20244
20245
20246
20247
20248
20249
20250
20251
20252
20253
20254
20255
20256
20257
20258
20259
20260
20261
20262
20263
20264
20265
20266
20267
20268
20269
20270
20271
20272
20273
20274
20275
20276
20277
20278
20279
20280
20281
20282
20283
20284
20285
20286
20287
20288
20289
20290
20291
20292
20293
20294
20295
20296
20297
20298
20299
20300
20301
20302
20303
20304
20305
20306
20307
20308
20309
20310
20311
20312
20313
20314
20315
20316
20317
20318
20319
20320
20321
20322
20323
20324
20325
20326
20327
20328
20329
20330
20331
20332
20333
20334
20335
20336
20337
20338
20339
20340
20341
20342
20343
20344
20345
20346
20347
20348
20349
20350
20351
20352
20353
20354
20355
20356
20357
20358
20359
20360
20361
20362
20363
20364
20365
20366
20367
20368
20369
20370
20371
20372
20373
20374
20375
20376
20377
20378
20379
20380
20381
20382
20383
20384
20385
20386
20387
20388
20389
20390
20391
20392
20393
20394
20395
20396
20397
20398
20399
20400
20401
20402
20403
20404
20405
20406
20407
20408
20409
20410
20411
20412
20413
20414
20415
20416
20417
20418
20419
20420
20421
20422
20423
20424
20425
20426
20427
20428
20429
20430
20431
20432
20433
20434
20435
20436
20437
20438
20439
20440
20441
20442
20443
20444
20445
20446
20447
20448
20449
20450
20451
20452
20453
20454
20455
20456
20457
20458
20459
20460
20461
20462
20463
20464
20465
20466
20467
20468
20469
20470
20471
20472
20473
20474
20475
20476
20477
20478
20479
20480
20481
20482
20483
20484
20485
20486
20487
20488
20489
20490
20491
20492
20493
20494
20495
20496
20497
20498
20499
20500
20501
20502
20503
20504
20505
20506
20507
20508
20509
20510
20511
20512
20513
20514
20515
20516
20517
20518
20519
20520
20521
20522
20523
20524
20525
20526
20527
20528
20529
20530
20531
20532
20533
20534
20535
20536
20537
20538
20539
20540
20541
20542
20543
20544
20545
20546
20547
20548
20549
20550
20551
20552
20553
20554
20555
20556
20557
20558
20559
20560
20561
20562
20563
20564
20565
20566
20567
20568
20569
20570
20571
20572
20573
20574
20575
20576
20577
20578
20579
20580
20581
20582
20583
20584
20585
20586
20587
20588
20589
20590
20591
20592
20593
20594
20595
20596
20597
20598
20599
20600
20601
20602
20603
20604
20605
20606
20607
20608
20609
20610
20611
20612
20613
20614
20615
20616
20617
20618
20619
20620
20621
20622
20623
20624
20625
20626
20627
20628
20629
20630
20631
20632
20633
20634
20635
20636
20637
20638
20639
20640
20641
20642
20643
20644
20645
20646
20647
20648
20649
20650
20651
20652
20653
20654
20655
20656
20657
20658
20659
20660
20661
20662
20663
20664
20665
20666
20667
20668
20669
20670
20671
20672
20673
20674
20675
20676
20677
20678
20679
20680
20681
20682
20683
20684
20685
20686
20687
20688
20689
20690
20691
20692
20693
20694
20695
20696
20697
20698
20699
20700
20701
20702
20703
20704
20705
20706
20707
20708
20709
20710
20711
20712
20713
20714
20715
20716
20717
20718
20719
20720
20721
20722
20723
20724
20725
20726
20727
20728
20729
20730
20731
20732
20733
20734
20735
20736
20737
20738
20739
20740
20741
20742
20743
20744
20745
20746
20747
20748
20749
20750
20751
20752
20753
20754
20755
20756
20757
20758
20759
20760
20761
20762
20763
20764
20765
20766
20767
20768
20769
20770
20771
20772
20773
20774
20775
20776
20777
20778
20779
20780
20781
20782
20783
20784
20785
20786
20787
20788
20789
20790
20791
20792
20793
20794
20795
20796
20797
20798
20799
20800
20801
20802
20803
20804
20805
20806
20807
20808
20809
20810
20811
20812
20813
20814
20815
20816
20817
20818
20819
20820
20821
20822
20823
20824
20825
20826
20827
20828
20829
20830
20831
20832
20833
20834
20835
20836
20837
20838
20839
20840
20841
20842
20843
20844
20845
20846
20847
20848
20849
20850
20851
20852
20853
20854
20855
20856
20857
20858
20859
20860
20861
20862
20863
20864
20865
20866
20867
20868
20869
20870
20871
20872
20873
20874
20875
20876
20877
20878
20879
20880
20881
20882
20883
20884
20885
20886
20887
20888
20889
20890
20891
20892
20893
20894
20895
20896
20897
20898
20899
20900
20901
20902
20903
20904
20905
20906
20907
20908
20909
20910
20911
20912
20913
20914
20915
20916
20917
20918
20919
20920
20921
20922
20923
20924
20925
20926
20927
20928
20929
20930
20931
20932
20933
20934
20935
20936
20937
20938
20939
20940
20941
20942
20943
20944
20945
20946
20947
20948
20949
20950
20951
20952
20953
20954
20955
20956
20957
20958
20959
20960
20961
20962
20963
20964
20965
20966
20967
20968
20969
20970
20971
20972
20973
20974
20975
20976
20977
20978
20979
20980
20981
20982
20983
20984
20985
20986
20987
20988
20989
20990
20991
20992
20993
20994
20995
20996
20997
20998
20999
21000
21001
21002
21003
21004
21005
21006
21007
21008
21009
21010
21011
21012
21013
21014
21015
21016
21017
21018
21019
21020
21021
21022
21023
21024
21025
21026
21027
21028
21029
21030
21031
21032
21033
21034
21035
21036
21037
21038
21039
21040
21041
21042
21043
21044
21045
21046
21047
21048
21049
21050
21051
21052
21053
21054
21055
21056
21057
21058
21059
21060
21061
21062
21063
21064
21065
21066
21067
21068
21069
21070
21071
21072
21073
21074
21075
21076
21077
21078
21079
21080
21081
21082
21083
21084
21085
21086
21087
21088
21089
21090
21091
21092
21093
21094
21095
21096
21097
21098
21099
21100
21101
21102
21103
21104
21105
21106
21107
21108
21109
21110
21111
21112
21113
21114
21115
21116
21117
21118
21119
21120
21121
21122
21123
21124
21125
21126
21127
21128
21129
21130
21131
21132
21133
21134
21135
21136
21137
21138
21139
21140
21141
21142
21143
21144
21145
21146
21147
21148
21149
21150
21151
21152
21153
21154
21155
21156
21157
21158
21159
21160
21161
21162
21163
21164
21165
21166
21167
21168
21169
21170
21171
21172
21173
21174
21175
21176
21177
21178
21179
21180
21181
21182
21183
21184
21185
21186
21187
21188
21189
21190
21191
21192
21193
21194
21195
21196
21197
21198
21199
21200
21201
21202
21203
21204
21205
21206
21207
21208
21209
21210
21211
21212
21213
21214
21215
21216
21217
21218
21219
21220
21221
21222
21223
21224
21225
21226
21227
21228
21229
21230
21231
21232
21233
21234
21235
21236
21237
21238
21239
21240
21241
21242
21243
21244
21245
21246
21247
21248
21249
21250
21251
21252
21253
21254
21255
21256
21257
21258
21259
21260
21261
21262
21263
21264
21265
21266
21267
21268
21269
21270
21271
21272
21273
21274
21275
21276
21277
21278
21279
21280
21281
21282
21283
21284
21285
21286
21287
21288
21289
21290
21291
21292
21293
21294
21295
21296
21297
21298
21299
21300
21301
21302
21303
21304
21305
21306
21307
21308
21309
21310
21311
21312
21313
21314
21315
21316
21317
21318
21319
21320
21321
21322
21323
21324
21325
21326
21327
21328
21329
21330
21331
21332
21333
21334
21335
21336
21337
21338
21339
21340
21341
21342
21343
21344
21345
21346
21347
21348
21349
21350
21351
21352
21353
21354
21355
21356
21357
21358
21359
21360
21361
21362
21363
21364
21365
21366
21367
21368
21369
21370
21371
21372
21373
21374
21375
21376
21377
21378
21379
21380
21381
21382
21383
21384
21385
21386
21387
21388
21389
21390
21391
21392
21393
21394
21395
21396
21397
21398
21399
21400
21401
21402
21403
21404
21405
21406
21407
21408
21409
21410
21411
21412
21413
21414
21415
21416
21417
21418
21419
21420
21421
21422
21423
21424
21425
21426
21427
21428
21429
21430
21431
21432
21433
21434
21435
21436
21437
21438
21439
21440
21441
21442
21443
21444
21445
21446
21447
21448
21449
21450
21451
21452
21453
21454
21455
21456
21457
21458
21459
21460
21461
21462
21463
21464
21465
21466
21467
21468
21469
21470
21471
21472
21473
21474
21475
21476
21477
21478
21479
21480
21481
21482
21483
21484
21485
21486
21487
21488
21489
21490
21491
21492
21493
21494
21495
21496
21497
21498
21499
21500
21501
21502
21503
21504
21505
21506
21507
21508
21509
21510
21511
21512
21513
21514
21515
21516
21517
21518
21519
21520
21521
21522
21523
21524
21525
21526
21527
21528
21529
21530
21531
21532
21533
21534
21535
21536
21537
21538
21539
21540
21541
21542
21543
21544
21545
21546
21547
21548
21549
21550
21551
21552
21553
21554
21555
21556
21557
21558
21559
21560
21561
21562
21563
21564
21565
21566
21567
21568
21569
21570
21571
21572
21573
21574
21575
21576
21577
21578
21579
21580
21581
21582
21583
21584
21585
21586
21587
21588
21589
21590
21591
21592
21593
21594
21595
21596
21597
21598
21599
21600
21601
21602
21603
21604
21605
21606
21607
21608
21609
21610
21611
21612
21613
21614
21615
21616
21617
21618
21619
21620
21621
21622
21623
21624
21625
21626
21627
21628
21629
21630
21631
21632
21633
21634
21635
21636
21637
21638
21639
21640
21641
21642
21643
21644
21645
21646
21647
21648
21649
21650
21651
21652
21653
21654
21655
21656
21657
21658
21659
21660
21661
21662
21663
21664
21665
21666
21667
21668
21669
21670
21671
21672
21673
21674
21675
21676
21677
21678
21679
21680
21681
21682
21683
21684
21685
21686
21687
21688
21689
21690
21691
21692
21693
21694
21695
21696
21697
21698
21699
21700
21701
21702
21703
21704
21705
21706
21707
21708
21709
21710
21711
21712
21713
21714
21715
21716
21717
21718
21719
21720
21721
21722
21723
21724
21725
21726
21727
21728
21729
21730
21731
21732
21733
21734
21735
21736
21737
21738
21739
21740
21741
21742
21743
21744
21745
21746
21747
21748
21749
21750
21751
21752
21753
21754
21755
21756
21757
21758
21759
21760
21761
21762
21763
21764
21765
21766
21767
21768
21769
21770
21771
21772
21773
21774
21775
21776
21777
21778
21779
21780
21781
21782
21783
21784
21785
21786
21787
21788
21789
21790
21791
21792
21793
21794
21795
21796
21797
21798
21799
21800
21801
21802
21803
21804
21805
21806
21807
21808
21809
21810
21811
21812
21813
21814
21815
21816
21817
21818
21819
21820
21821
21822
21823
21824
21825
21826
21827
21828
21829
21830
21831
21832
21833
21834
21835
21836
21837
21838
21839
21840
21841
21842
21843
21844
21845
21846
21847
21848
21849
21850
21851
21852
21853
21854
21855
21856
21857
21858
21859
21860
21861
21862
21863
21864
21865
21866
21867
21868
21869
21870
21871
21872
21873
21874
21875
21876
21877
21878
21879
21880
21881
21882
21883
21884
21885
21886
21887
21888
21889
21890
21891
21892
21893
21894
21895
21896
21897
21898
21899
21900
21901
21902
21903
21904
21905
21906
21907
21908
21909
21910
21911
21912
21913
21914
21915
21916
21917
21918
21919
21920
21921
21922
21923
21924
21925
21926
21927
21928
21929
21930
21931
21932
21933
21934
21935
21936
21937
21938
21939
21940
21941
21942
21943
21944
21945
21946
21947
21948
21949
21950
21951
21952
21953
21954
21955
21956
21957
21958
21959
21960
21961
21962
21963
21964
21965
21966
21967
21968
21969
21970
21971
21972
21973
21974
21975
21976
21977
21978
21979
21980
21981
21982
21983
21984
21985
21986
21987
21988
21989
21990
21991
21992
21993
21994
21995
21996
21997
21998
21999
22000
22001
22002
22003
22004
22005
22006
22007
22008
22009
22010
22011
22012
22013
22014
22015
22016
22017
22018
22019
22020
22021
22022
22023
22024
22025
22026
22027
22028
22029
22030
22031
22032
22033
22034
22035
22036
22037
22038
22039
22040
22041
22042
22043
22044
22045
22046
22047
22048
22049
22050
22051
22052
22053
22054
22055
22056
22057
22058
22059
22060
22061
22062
22063
22064
22065
22066
22067
22068
22069
22070
22071
22072
22073
22074
22075
22076
22077
22078
22079
22080
22081
22082
22083
22084
22085
22086
22087
22088
22089
22090
22091
22092
22093
22094
22095
22096
22097
22098
22099
22100
22101
22102
22103
22104
22105
22106
22107
22108
22109
22110
22111
22112
22113
22114
22115
22116
22117
22118
22119
22120
22121
22122
22123
22124
22125
22126
22127
22128
22129
22130
22131
22132
22133
22134
22135
22136
22137
22138
22139
22140
22141
22142
22143
22144
22145
22146
22147
22148
22149
22150
22151
22152
22153
22154
22155
22156
22157
22158
22159
22160
22161
22162
22163
22164
22165
22166
22167
22168
22169
22170
22171
22172
22173
22174
22175
22176
22177
22178
22179
22180
22181
22182
22183
22184
22185
22186
22187
22188
22189
22190
22191
22192
22193
22194
22195
22196
22197
22198
22199
22200
22201
22202
22203
22204
22205
22206
22207
22208
22209
22210
22211
22212
22213
22214
22215
22216
22217
22218
22219
22220
22221
22222
22223
22224
22225
22226
22227
22228
22229
22230
22231
22232
22233
22234
22235
22236
22237
22238
22239
22240
22241
22242
22243
22244
22245
22246
22247
22248
22249
22250
22251
22252
22253
22254
22255
22256
22257
22258
22259
22260
22261
22262
22263
22264
22265
22266
22267
22268
22269
22270
22271
22272
22273
22274
22275
22276
22277
22278
22279
22280
22281
22282
22283
22284
22285
22286
22287
22288
22289
22290
22291
22292
22293
22294
22295
22296
22297
22298
22299
22300
22301
22302
22303
22304
22305
22306
22307
22308
22309
22310
22311
22312
22313
22314
22315
22316
22317
22318
22319
22320
22321
22322
22323
22324
22325
22326
22327
22328
22329
22330
22331
22332
22333
22334
22335
22336
22337
22338
22339
22340
22341
22342
22343
22344
22345
22346
22347
22348
22349
22350
22351
22352
22353
22354
22355
22356
22357
22358
22359
22360
22361
22362
22363
22364
22365
22366
22367
22368
22369
22370
22371
22372
22373
22374
22375
22376
22377
22378
22379
22380
22381
22382
22383
22384
22385
22386
22387
22388
22389
22390
22391
22392
22393
22394
22395
22396
22397
22398
22399
22400
22401
22402
22403
22404
22405
22406
22407
22408
22409
22410
22411
22412
22413
22414
22415
22416
22417
22418
22419
22420
22421
22422
22423
22424
22425
22426
22427
22428
22429
22430
22431
22432
22433
22434
22435
22436
22437
22438
22439
22440
22441
22442
22443
22444
22445
22446
22447
22448
22449
22450
22451
22452
22453
22454
22455
22456
22457
22458
22459
22460
22461
22462
22463
22464
22465
22466
22467
22468
22469
22470
22471
22472
22473
22474
22475
22476
22477
22478
22479
22480
22481
22482
22483
22484
22485
22486
22487
22488
22489
22490
22491
22492
22493
22494
22495
22496
22497
22498
22499
22500
22501
22502
22503
22504
22505
22506
22507
22508
22509
22510
22511
22512
22513
22514
22515
22516
22517
22518
22519
22520
22521
22522
22523
22524
22525
22526
22527
22528
22529
22530
22531
22532
22533
22534
22535
22536
22537
22538
22539
22540
22541
22542
22543
22544
22545
22546
22547
22548
22549
22550
22551
22552
22553
22554
22555
22556
22557
22558
22559
22560
22561
22562
22563
22564
22565
22566
22567
22568
22569
22570
22571
22572
22573
22574
22575
22576
22577
22578
22579
22580
22581
22582
22583
22584
22585
22586
22587
22588
22589
22590
22591
22592
22593
22594
22595
22596
22597
22598
22599
22600
22601
22602
22603
22604
22605
22606
22607
22608
22609
22610
22611
22612
22613
22614
22615
22616
22617
22618
22619
22620
22621
22622
22623
22624
22625
22626
22627
22628
22629
22630
22631
22632
22633
22634
22635
22636
22637
22638
22639
22640
22641
22642
22643
22644
22645
22646
22647
22648
22649
22650
22651
22652
22653
22654
22655
22656
22657
22658
22659
22660
22661
22662
22663
22664
22665
22666
22667
22668
22669
22670
22671
22672
22673
22674
22675
22676
22677
22678
22679
22680
22681
22682
22683
22684
22685
22686
22687
22688
22689
22690
22691
22692
22693
22694
22695
22696
22697
22698
22699
22700
22701
22702
22703
22704
22705
22706
22707
22708
22709
22710
22711
22712
22713
22714
22715
22716
22717
22718
22719
22720
22721
22722
22723
22724
22725
22726
22727
22728
22729
22730
22731
22732
22733
22734
22735
22736
22737
22738
22739
22740
22741
22742
22743
22744
22745
22746
22747
22748
22749
22750
22751
22752
22753
22754
22755
22756
22757
22758
22759
22760
22761
22762
22763
22764
22765
22766
22767
22768
22769
22770
22771
22772
22773
22774
22775
22776
22777
22778
22779
22780
22781
22782
22783
22784
22785
22786
22787
22788
22789
22790
22791
22792
22793
22794
22795
22796
22797
22798
22799
22800
22801
22802
22803
22804
22805
22806
22807
22808
22809
22810
22811
22812
22813
22814
22815
22816
22817
22818
22819
22820
22821
22822
22823
22824
22825
22826
22827
22828
22829
22830
22831
22832
22833
22834
22835
22836
22837
22838
22839
22840
22841
22842
22843
22844
22845
22846
22847
22848
22849
22850
22851
22852
22853
22854
22855
22856
22857
22858
22859
22860
22861
22862
22863
22864
22865
22866
22867
22868
22869
22870
22871
22872
22873
22874
22875
22876
22877
22878
22879
22880
22881
22882
22883
22884
22885
22886
22887
22888
22889
22890
22891
22892
22893
22894
22895
22896
22897
22898
22899
22900
22901
22902
22903
22904
22905
22906
22907
22908
22909
22910
22911
22912
22913
22914
22915
22916
22917
22918
22919
22920
22921
22922
22923
22924
22925
22926
22927
22928
22929
22930
22931
22932
22933
22934
22935
22936
22937
22938
22939
22940
22941
22942
22943
22944
22945
22946
22947
22948
22949
22950
22951
22952
22953
22954
22955
22956
22957
22958
22959
22960
22961
22962
22963
22964
22965
22966
22967
22968
22969
22970
22971
22972
22973
22974
22975
22976
22977
22978
22979
22980
22981
22982
22983
22984
22985
22986
22987
22988
22989
22990
22991
22992
22993
22994
22995
22996
22997
22998
22999
23000
23001
23002
23003
23004
23005
23006
23007
23008
23009
23010
23011
23012
23013
23014
23015
23016
23017
23018
23019
23020
23021
23022
23023
23024
23025
23026
23027
23028
23029
23030
23031
23032
23033
23034
23035
23036
23037
23038
23039
23040
23041
23042
23043
23044
23045
23046
23047
23048
23049
23050
23051
23052
23053
23054
23055
23056
23057
23058
23059
23060
23061
23062
23063
23064
23065
23066
23067
23068
23069
23070
23071
23072
23073
23074
23075
23076
23077
23078
23079
23080
23081
23082
23083
23084
23085
23086
23087
23088
23089
23090
23091
23092
23093
23094
23095
23096
23097
23098
23099
23100
23101
23102
23103
23104
23105
23106
23107
23108
23109
23110
23111
23112
23113
23114
23115
23116
23117
23118
23119
23120
23121
23122
23123
23124
23125
23126
23127
23128
23129
23130
23131
23132
23133
23134
23135
23136
23137
23138
23139
23140
23141
23142
23143
23144
23145
23146
23147
23148
23149
23150
23151
23152
23153
23154
23155
23156
23157
23158
23159
23160
23161
23162
23163
23164
23165
23166
23167
23168
23169
23170
23171
23172
23173
23174
23175
23176
23177
23178
23179
23180
23181
23182
23183
23184
23185
23186
23187
23188
23189
23190
23191
23192
23193
23194
23195
23196
23197
23198
23199
23200
23201
23202
23203
23204
23205
23206
23207
23208
23209
23210
23211
23212
23213
23214
23215
23216
23217
23218
23219
23220
23221
23222
23223
23224
23225
23226
23227
23228
23229
23230
23231
23232
23233
23234
23235
23236
23237
23238
23239
23240
23241
23242
23243
23244
23245
23246
23247
23248
23249
23250
23251
23252
23253
23254
23255
23256
23257
23258
23259
23260
23261
23262
23263
23264
23265
23266
23267
23268
23269
23270
23271
23272
23273
23274
23275
23276
23277
23278
23279
23280
23281
23282
23283
23284
23285
23286
23287
23288
23289
23290
23291
23292
23293
23294
23295
23296
23297
23298
23299
23300
23301
23302
23303
23304
23305
23306
23307
23308
23309
23310
23311
23312
23313
23314
23315
23316
23317
23318
23319
23320
23321
23322
23323
23324
23325
23326
23327
23328
23329
23330
23331
23332
23333
23334
23335
23336
23337
23338
23339
23340
23341
23342
23343
23344
23345
23346
23347
23348
23349
23350
23351
23352
23353
23354
23355
23356
23357
23358
23359
23360
23361
23362
23363
23364
23365
23366
23367
23368
23369
23370
23371
23372
23373
23374
23375
23376
23377
23378
23379
23380
23381
23382
23383
23384
23385
23386
23387
23388
23389
23390
23391
23392
23393
23394
23395
23396
23397
23398
23399
23400
23401
23402
23403
23404
23405
23406
23407
23408
23409
23410
23411
23412
23413
23414
23415
23416
23417
23418
23419
23420
23421
23422
23423
23424
23425
23426
23427
23428
23429
23430
23431
23432
23433
23434
23435
23436
23437
23438
23439
23440
23441
23442
23443
23444
23445
23446
23447
23448
23449
23450
23451
23452
23453
23454
23455
23456
23457
23458
23459
23460
23461
23462
23463
23464
23465
23466
23467
23468
23469
23470
23471
23472
23473
23474
23475
23476
23477
23478
23479
23480
23481
23482
23483
23484
23485
23486
23487
23488
23489
23490
23491
23492
23493
23494
23495
23496
23497
23498
23499
23500
23501
23502
23503
23504
23505
23506
23507
23508
23509
23510
23511
23512
23513
23514
23515
23516
23517
23518
23519
23520
23521
23522
23523
23524
23525
23526
23527
23528
23529
23530
23531
23532
23533
23534
23535
23536
23537
23538
23539
23540
23541
23542
23543
23544
23545
23546
23547
23548
23549
23550
23551
23552
23553
23554
23555
23556
23557
23558
23559
23560
23561
23562
23563
23564
23565
23566
23567
23568
23569
23570
23571
23572
23573
23574
23575
23576
23577
23578
23579
23580
23581
23582
23583
23584
23585
23586
23587
23588
23589
23590
23591
23592
23593
23594
23595
23596
23597
23598
23599
23600
23601
23602
23603
23604
23605
23606
23607
23608
23609
23610
23611
23612
23613
23614
23615
23616
23617
23618
23619
23620
23621
23622
23623
23624
23625
23626
23627
23628
23629
23630
23631
23632
23633
23634
23635
23636
23637
23638
23639
23640
23641
23642
23643
23644
23645
23646
23647
23648
23649
23650
23651
23652
23653
23654
23655
23656
23657
23658
23659
23660
23661
23662
23663
23664
23665
23666
23667
23668
23669
23670
23671
23672
23673
23674
23675
23676
23677
23678
23679
23680
23681
23682
23683
23684
23685
23686
23687
23688
23689
23690
23691
23692
23693
23694
23695
23696
23697
23698
23699
23700
23701
23702
23703
23704
23705
23706
23707
23708
23709
23710
23711
23712
23713
23714
23715
23716
23717
23718
23719
23720
23721
23722
23723
23724
23725
23726
23727
23728
23729
23730
23731
23732
23733
23734
23735
23736
23737
23738
23739
23740
23741
23742
23743
23744
23745
23746
23747
23748
23749
23750
23751
23752
23753
23754
23755
23756
23757
23758
23759
23760
23761
23762
23763
23764
23765
23766
23767
23768
23769
23770
23771
23772
23773
23774
23775
23776
23777
23778
23779
23780
23781
23782
23783
23784
23785
23786
23787
23788
23789
23790
23791
23792
23793
23794
23795
23796
23797
23798
23799
23800
23801
23802
23803
23804
23805
23806
23807
23808
23809
23810
23811
23812
23813
23814
23815
23816
23817
23818
23819
23820
23821
23822
23823
23824
23825
23826
23827
23828
23829
23830
23831
23832
23833
23834
23835
23836
23837
23838
23839
23840
23841
23842
23843
23844
23845
23846
23847
23848
23849
23850
23851
23852
23853
23854
23855
23856
23857
23858
23859
23860
23861
23862
23863
23864
23865
23866
23867
23868
23869
23870
23871
23872
23873
23874
23875
23876
23877
23878
23879
23880
23881
23882
23883
23884
23885
23886
23887
23888
23889
23890
23891
23892
23893
23894
23895
23896
23897
23898
23899
23900
23901
23902
23903
23904
23905
23906
23907
23908
23909
23910
23911
23912
23913
23914
23915
23916
23917
23918
23919
23920
23921
23922
23923
23924
23925
23926
23927
23928
23929
23930
23931
23932
23933
23934
23935
23936
23937
23938
23939
23940
23941
23942
23943
23944
23945
23946
23947
23948
23949
23950
23951
23952
23953
23954
23955
23956
23957
23958
23959
23960
23961
23962
23963
23964
23965
23966
23967
23968
23969
23970
23971
23972
23973
23974
23975
23976
23977
23978
23979
23980
23981
23982
23983
23984
23985
23986
23987
23988
23989
23990
23991
23992
23993
23994
23995
23996
23997
23998
23999
24000
24001
24002
24003
24004
24005
24006
24007
24008
24009
24010
24011
24012
24013
24014
24015
24016
24017
24018
24019
24020
24021
24022
24023
24024
24025
24026
24027
24028
24029
24030
24031
24032
24033
24034
24035
24036
24037
24038
24039
24040
24041
24042
24043
24044
24045
24046
24047
24048
24049
24050
24051
24052
24053
24054
24055
24056
24057
24058
24059
24060
24061
24062
24063
24064
24065
24066
24067
24068
24069
24070
24071
24072
24073
24074
24075
24076
24077
24078
24079
24080
24081
24082
24083
24084
24085
24086
24087
24088
24089
24090
24091
24092
24093
24094
24095
24096
24097
24098
24099
24100
24101
24102
24103
24104
24105
24106
24107
24108
24109
24110
24111
24112
24113
24114
24115
24116
24117
24118
24119
24120
24121
24122
24123
24124
24125
24126
24127
24128
24129
24130
24131
24132
24133
24134
24135
24136
24137
24138
24139
24140
24141
24142
24143
24144
24145
24146
24147
24148
24149
24150
24151
24152
24153
24154
24155
24156
24157
24158
24159
24160
24161
24162
24163
24164
24165
24166
24167
24168
24169
24170
24171
24172
24173
24174
24175
24176
24177
24178
24179
24180
24181
24182
24183
24184
24185
24186
24187
24188
24189
24190
24191
24192
24193
24194
24195
24196
24197
24198
24199
24200
24201
24202
24203
24204
24205
24206
24207
24208
24209
24210
24211
24212
24213
24214
24215
24216
24217
24218
24219
24220
24221
24222
24223
24224
24225
24226
24227
24228
24229
24230
24231
24232
24233
24234
24235
24236
24237
24238
24239
24240
24241
24242
24243
24244
24245
24246
24247
24248
24249
24250
24251
24252
24253
24254
24255
24256
24257
24258
24259
24260
24261
24262
24263
24264
24265
24266
24267
24268
24269
24270
24271
24272
24273
24274
24275
24276
24277
24278
24279
24280
24281
24282
24283
24284
24285
24286
24287
24288
24289
24290
24291
24292
24293
24294
24295
24296
24297
24298
24299
24300
24301
24302
24303
24304
24305
24306
24307
24308
24309
24310
24311
24312
24313
24314
24315
24316
24317
24318
24319
24320
24321
24322
24323
24324
24325
24326
24327
24328
24329
24330
24331
24332
24333
24334
24335
24336
24337
24338
24339
24340
24341
24342
24343
24344
24345
24346
24347
24348
24349
24350
24351
24352
24353
24354
24355
24356
24357
24358
24359
24360
24361
24362
24363
24364
24365
24366
24367
24368
24369
24370
24371
24372
24373
24374
24375
24376
24377
24378
24379
24380
24381
24382
24383
24384
24385
24386
24387
24388
24389
24390
24391
24392
24393
24394
24395
24396
24397
24398
24399
24400
24401
24402
24403
24404
24405
24406
24407
24408
24409
24410
24411
24412
24413
24414
24415
24416
24417
24418
24419
24420
24421
24422
24423
24424
24425
24426
24427
24428
24429
24430
24431
24432
24433
24434
24435
24436
24437
24438
24439
24440
24441
24442
24443
24444
24445
24446
24447
24448
24449
24450
24451
24452
24453
24454
24455
24456
24457
24458
24459
24460
24461
24462
24463
24464
24465
24466
24467
24468
24469
24470
24471
24472
24473
24474
24475
24476
24477
24478
24479
24480
24481
24482
24483
24484
24485
24486
24487
24488
24489
24490
24491
24492
24493
24494
24495
24496
24497
24498
24499
24500
24501
24502
24503
24504
24505
24506
24507
24508
24509
24510
24511
24512
24513
24514
24515
24516
24517
24518
24519
24520
24521
24522
24523
24524
24525
24526
24527
24528
24529
24530
24531
24532
24533
24534
24535
24536
24537
24538
24539
24540
24541
24542
24543
24544
24545
24546
24547
24548
24549
24550
24551
24552
24553
24554
24555
24556
24557
24558
24559
24560
24561
24562
24563
24564
24565
24566
24567
24568
24569
24570
24571
24572
24573
24574
24575
24576
24577
24578
24579
24580
24581
24582
24583
24584
24585
24586
24587
24588
24589
24590
24591
24592
24593
24594
24595
24596
24597
24598
24599
24600
24601
24602
24603
24604
24605
24606
24607
24608
24609
24610
24611
24612
24613
24614
24615
24616
24617
24618
24619
24620
24621
24622
24623
24624
24625
24626
24627
24628
24629
24630
24631
24632
24633
24634
24635
24636
24637
24638
24639
24640
24641
24642
24643
24644
24645
24646
24647
24648
24649
24650
24651
24652
24653
24654
24655
24656
24657
24658
24659
24660
24661
24662
24663
24664
24665
24666
24667
24668
24669
24670
24671
24672
24673
24674
24675
24676
24677
24678
24679
24680
24681
24682
24683
24684
24685
24686
24687
24688
24689
24690
24691
24692
24693
24694
24695
24696
24697
24698
24699
24700
24701
24702
24703
24704
24705
24706
24707
24708
24709
24710
24711
24712
24713
24714
24715
24716
24717
24718
24719
24720
24721
24722
24723
24724
24725
24726
24727
24728
24729
24730
24731
24732
24733
24734
24735
24736
24737
24738
24739
24740
24741
24742
24743
24744
24745
24746
24747
24748
24749
24750
24751
24752
24753
24754
24755
24756
24757
24758
24759
24760
24761
24762
24763
24764
24765
24766
24767
24768
24769
24770
24771
24772
24773
24774
24775
24776
24777
24778
24779
24780
24781
24782
24783
24784
24785
24786
24787
24788
24789
24790
24791
24792
24793
24794
24795
24796
24797
24798
24799
24800
24801
24802
24803
24804
24805
24806
24807
24808
24809
24810
24811
24812
24813
24814
24815
24816
24817
24818
24819
24820
24821
24822
24823
24824
24825
24826
24827
24828
24829
24830
24831
24832
24833
24834
24835
24836
24837
24838
24839
24840
24841
24842
24843
24844
24845
24846
24847
24848
24849
24850
24851
24852
24853
24854
24855
24856
24857
24858
24859
24860
24861
24862
24863
24864
24865
24866
24867
24868
24869
24870
24871
24872
24873
24874
24875
24876
24877
24878
24879
24880
24881
24882
24883
24884
24885
24886
24887
24888
24889
24890
24891
24892
24893
24894
24895
24896
24897
24898
24899
24900
24901
24902
24903
24904
24905
24906
24907
24908
24909
24910
24911
24912
24913
24914
24915
24916
24917
24918
24919
24920
24921
24922
24923
24924
24925
24926
24927
24928
24929
24930
24931
24932
24933
24934
24935
24936
24937
24938
24939
24940
24941
24942
24943
24944
24945
24946
24947
24948
24949
24950
24951
24952
24953
24954
24955
24956
24957
24958
24959
24960
24961
24962
24963
24964
24965
24966
24967
24968
24969
24970
24971
24972
24973
24974
24975
24976
24977
24978
24979
24980
24981
24982
24983
24984
24985
24986
24987
24988
24989
24990
24991
24992
24993
24994
24995
24996
24997
24998
24999
25000
25001
25002
25003
25004
25005
25006
25007
25008
25009
25010
25011
25012
25013
25014
25015
25016
25017
25018
25019
25020
25021
25022
25023
25024
25025
25026
25027
25028
25029
25030
25031
25032
25033
25034
25035
25036
25037
25038
25039
25040
25041
25042
25043
25044
25045
25046
25047
25048
25049
25050
25051
25052
25053
25054
25055
25056
25057
25058
25059
25060
25061
25062
25063
25064
25065
25066
25067
25068
25069
25070
25071
25072
25073
25074
25075
25076
25077
25078
25079
25080
25081
25082
25083
25084
25085
25086
25087
25088
25089
25090
25091
25092
25093
25094
25095
25096
25097
25098
25099
25100
25101
25102
25103
25104
25105
25106
25107
25108
25109
25110
25111
25112
25113
25114
25115
25116
25117
25118
25119
25120
25121
25122
25123
25124
25125
25126
25127
25128
25129
25130
25131
25132
25133
25134
25135
25136
25137
25138
25139
25140
25141
25142
25143
25144
25145
25146
25147
25148
25149
25150
25151
25152
25153
25154
25155
25156
25157
25158
25159
25160
25161
25162
25163
25164
25165
25166
25167
25168
25169
25170
25171
25172
25173
25174
25175
25176
25177
25178
25179
25180
25181
25182
25183
25184
25185
25186
25187
25188
25189
25190
25191
25192
25193
25194
25195
25196
25197
25198
25199
25200
25201
25202
25203
25204
25205
25206
25207
25208
25209
25210
25211
25212
25213
25214
25215
25216
25217
25218
25219
25220
25221
25222
25223
25224
25225
25226
25227
25228
25229
25230
25231
25232
25233
25234
25235
25236
25237
25238
25239
25240
25241
25242
25243
25244
25245
25246
25247
25248
25249
25250
25251
25252
25253
25254
25255
25256
25257
25258
25259
25260
25261
25262
25263
25264
25265
25266
25267
25268
25269
25270
25271
25272
25273
25274
25275
25276
25277
25278
25279
25280
25281
25282
25283
25284
25285
25286
25287
25288
25289
25290
25291
25292
25293
25294
25295
25296
25297
25298
25299
25300
25301
25302
25303
25304
25305
25306
25307
25308
25309
25310
25311
25312
25313
25314
25315
25316
25317
25318
25319
25320
25321
25322
25323
25324
25325
25326
25327
25328
25329
25330
25331
25332
25333
25334
25335
25336
25337
25338
25339
25340
25341
25342
25343
25344
25345
25346
25347
25348
25349
25350
25351
25352
25353
25354
25355
25356
25357
25358
25359
25360
25361
25362
25363
25364
25365
25366
25367
25368
25369
25370
25371
25372
25373
25374
25375
25376
25377
25378
25379
25380
25381
25382
25383
25384
25385
25386
25387
25388
25389
25390
25391
25392
25393
25394
25395
25396
25397
25398
25399
25400
25401
25402
25403
25404
25405
25406
25407
25408
25409
25410
25411
25412
25413
25414
25415
25416
25417
25418
25419
25420
25421
25422
25423
25424
25425
25426
25427
25428
25429
25430
25431
25432
25433
25434
25435
25436
25437
25438
25439
25440
25441
25442
25443
25444
25445
25446
25447
25448
25449
25450
25451
25452
25453
25454
25455
25456
25457
25458
25459
25460
25461
25462
25463
25464
25465
25466
25467
25468
25469
25470
25471
25472
25473
25474
25475
25476
25477
25478
25479
25480
25481
25482
25483
25484
25485
25486
25487
25488
25489
25490
25491
25492
25493
25494
25495
25496
25497
25498
25499
25500
25501
25502
25503
25504
25505
25506
25507
25508
25509
25510
25511
25512
25513
25514
25515
25516
25517
25518
25519
25520
25521
25522
25523
25524
25525
25526
25527
25528
25529
25530
25531
25532
25533
25534
25535
25536
25537
25538
25539
25540
25541
25542
25543
25544
25545
25546
25547
25548
25549
25550
25551
25552
25553
25554
25555
25556
25557
25558
25559
25560
25561
25562
25563
25564
25565
25566
25567
25568
25569
25570
25571
25572
25573
25574
25575
25576
25577
25578
25579
25580
25581
25582
25583
25584
25585
25586
25587
25588
25589
25590
25591
25592
25593
25594
25595
25596
25597
25598
25599
25600
25601
25602
25603
25604
25605
25606
25607
25608
25609
25610
25611
25612
25613
25614
25615
25616
25617
25618
25619
25620
25621
25622
25623
25624
25625
25626
25627
25628
25629
25630
25631
25632
25633
25634
25635
25636
25637
25638
25639
25640
25641
25642
25643
25644
25645
25646
25647
25648
25649
25650
25651
25652
25653
25654
25655
25656
25657
25658
25659
25660
25661
25662
25663
25664
25665
25666
25667
25668
25669
25670
25671
25672
25673
25674
25675
25676
25677
25678
25679
25680
25681
25682
25683
25684
25685
25686
25687
25688
25689
25690
25691
25692
25693
25694
25695
25696
25697
25698
25699
25700
25701
25702
25703
25704
25705
25706
25707
25708
25709
25710
25711
25712
25713
25714
25715
25716
25717
25718
25719
25720
25721
25722
25723
25724
25725
25726
25727
25728
25729
25730
25731
25732
25733
25734
25735
25736
25737
25738
25739
25740
25741
25742
25743
25744
25745
25746
25747
25748
25749
25750
25751
25752
25753
25754
25755
25756
25757
25758
25759
25760
25761
25762
25763
25764
25765
25766
25767
25768
25769
25770
25771
25772
25773
25774
25775
25776
25777
25778
25779
25780
25781
25782
25783
25784
25785
25786
25787
25788
25789
25790
25791
25792
25793
25794
25795
25796
25797
25798
25799
25800
25801
25802
25803
25804
25805
25806
25807
25808
25809
25810
25811
25812
25813
25814
25815
25816
25817
25818
25819
25820
25821
25822
25823
25824
25825
25826
25827
25828
25829
25830
25831
25832
25833
25834
25835
25836
25837
25838
25839
25840
25841
25842
25843
25844
25845
25846
25847
25848
25849
25850
25851
25852
25853
25854
25855
25856
25857
25858
25859
25860
25861
25862
25863
25864
25865
25866
25867
25868
25869
25870
25871
25872
25873
25874
25875
25876
25877
25878
25879
25880
25881
25882
25883
25884
25885
25886
25887
25888
25889
25890
25891
25892
25893
25894
25895
25896
25897
25898
25899
25900
25901
25902
25903
25904
25905
25906
25907
25908
25909
25910
25911
25912
25913
25914
25915
25916
25917
25918
25919
25920
25921
25922
25923
25924
25925
25926
25927
25928
25929
25930
25931
25932
25933
25934
25935
25936
25937
25938
25939
25940
25941
25942
25943
25944
25945
25946
25947
25948
25949
25950
25951
25952
25953
25954
25955
25956
25957
25958
25959
25960
25961
25962
25963
25964
25965
25966
25967
25968
25969
25970
25971
25972
25973
25974
25975
25976
25977
25978
25979
25980
25981
25982
25983
25984
25985
25986
25987
25988
25989
25990
25991
25992
25993
25994
25995
25996
25997
25998
25999
26000
26001
26002
26003
26004
26005
26006
26007
26008
26009
26010
26011
26012
26013
26014
26015
26016
26017
26018
26019
26020
26021
26022
26023
26024
26025
26026
26027
26028
26029
26030
26031
26032
26033
26034
26035
26036
26037
26038
26039
26040
26041
26042
26043
26044
26045
26046
26047
26048
26049
26050
26051
26052
26053
26054
26055
26056
26057
26058
26059
26060
26061
26062
26063
26064
26065
26066
26067
26068
26069
26070
26071
26072
26073
26074
26075
26076
26077
26078
26079
26080
26081
26082
26083
26084
26085
26086
26087
26088
26089
26090
26091
26092
26093
26094
26095
26096
26097
26098
26099
26100
26101
26102
26103
26104
26105
26106
26107
26108
26109
26110
26111
26112
26113
26114
26115
26116
26117
26118
26119
26120
26121
26122
26123
26124
26125
26126
26127
26128
26129
26130
26131
26132
26133
26134
26135
26136
26137
26138
26139
26140
26141
26142
26143
26144
26145
26146
26147
26148
26149
26150
26151
26152
26153
26154
26155
26156
26157
26158
26159
26160
26161
26162
26163
26164
26165
26166
26167
26168
26169
26170
26171
26172
26173
26174
26175
26176
26177
26178
26179
26180
26181
26182
26183
26184
26185
26186
26187
26188
26189
26190
26191
26192
26193
26194
26195
26196
26197
26198
26199
26200
26201
26202
26203
26204
26205
26206
26207
26208
26209
26210
26211
26212
26213
26214
26215
26216
26217
26218
26219
26220
26221
26222
26223
26224
26225
26226
26227
26228
26229
26230
26231
26232
26233
26234
26235
26236
26237
26238
26239
26240
26241
26242
26243
26244
26245
26246
26247
26248
26249
26250
26251
26252
26253
26254
26255
26256
26257
26258
26259
26260
26261
26262
26263
26264
26265
26266
26267
26268
26269
26270
26271
26272
26273
26274
26275
26276
26277
26278
26279
26280
26281
26282
26283
26284
26285
26286
26287
26288
26289
26290
26291
26292
26293
26294
26295
26296
26297
26298
26299
26300
26301
26302
26303
26304
26305
26306
26307
26308
26309
26310
26311
26312
26313
26314
26315
26316
26317
26318
26319
26320
26321
26322
26323
26324
26325
26326
26327
26328
26329
26330
26331
26332
26333
26334
26335
26336
26337
26338
26339
26340
26341
26342
26343
26344
26345
26346
26347
26348
26349
26350
26351
26352
26353
26354
26355
26356
26357
26358
26359
26360
26361
26362
26363
26364
26365
26366
26367
26368
26369
26370
26371
26372
26373
26374
26375
26376
26377
26378
26379
26380
26381
26382
26383
26384
26385
26386
26387
26388
26389
26390
26391
26392
26393
26394
26395
26396
26397
26398
26399
26400
26401
26402
26403
26404
26405
26406
26407
26408
26409
26410
26411
26412
26413
26414
26415
26416
26417
26418
26419
26420
26421
26422
26423
26424
26425
26426
26427
26428
26429
26430
26431
26432
26433
26434
26435
26436
26437
26438
26439
26440
26441
26442
26443
26444
26445
26446
26447
26448
26449
26450
26451
26452
26453
26454
26455
26456
26457
26458
26459
26460
26461
26462
26463
26464
26465
26466
26467
26468
26469
26470
26471
26472
26473
26474
26475
26476
26477
26478
26479
26480
26481
26482
26483
26484
26485
26486
26487
26488
26489
26490
26491
26492
26493
26494
26495
26496
26497
26498
26499
26500
26501
26502
26503
26504
26505
26506
26507
26508
26509
26510
26511
26512
26513
26514
26515
26516
26517
26518
26519
26520
26521
26522
26523
26524
26525
26526
26527
26528
26529
26530
26531
26532
26533
26534
26535
26536
26537
26538
26539
26540
26541
26542
26543
26544
26545
26546
26547
26548
26549
26550
26551
26552
26553
26554
26555
26556
26557
26558
26559
26560
26561
26562
26563
26564
26565
26566
26567
26568
26569
26570
26571
26572
26573
26574
26575
26576
26577
26578
26579
26580
26581
26582
26583
26584
26585
26586
26587
26588
26589
26590
26591
26592
26593
26594
26595
26596
26597
26598
26599
26600
26601
26602
26603
26604
26605
26606
26607
26608
26609
26610
26611
26612
26613
26614
26615
26616
26617
26618
26619
26620
26621
26622
26623
26624
26625
26626
26627
26628
26629
26630
26631
26632
26633
26634
26635
26636
26637
26638
26639
26640
26641
26642
26643
26644
26645
26646
26647
26648
26649
26650
26651
26652
26653
26654
26655
26656
26657
26658
26659
26660
26661
26662
26663
26664
26665
26666
26667
26668
26669
26670
26671
26672
26673
26674
26675
26676
26677
26678
26679
26680
26681
26682
26683
26684
26685
26686
26687
26688
26689
26690
26691
26692
26693
26694
26695
26696
26697
26698
26699
26700
26701
26702
26703
26704
26705
26706
26707
26708
26709
26710
26711
26712
26713
26714
26715
26716
26717
26718
26719
26720
26721
26722
26723
26724
26725
26726
26727
26728
26729
26730
26731
26732
26733
26734
26735
26736
26737
26738
26739
26740
26741
26742
26743
26744
26745
26746
26747
26748
26749
26750
26751
26752
26753
26754
26755
26756
26757
26758
26759
26760
26761
26762
26763
26764
26765
26766
26767
26768
26769
26770
26771
26772
26773
26774
26775
26776
26777
26778
26779
26780
26781
26782
26783
26784
26785
26786
26787
26788
26789
26790
26791
26792
26793
26794
26795
26796
26797
26798
26799
26800
26801
26802
26803
26804
26805
26806
26807
26808
26809
26810
26811
26812
26813
26814
26815
26816
26817
26818
26819
26820
26821
26822
26823
26824
26825
26826
26827
26828
26829
26830
26831
26832
26833
26834
26835
26836
26837
26838
26839
26840
26841
26842
26843
26844
26845
26846
26847
26848
26849
26850
26851
26852
26853
26854
26855
26856
26857
26858
26859
26860
26861
26862
26863
26864
26865
26866
26867
26868
26869
26870
26871
26872
26873
26874
26875
26876
26877
26878
26879
26880
26881
26882
26883
26884
26885
26886
26887
26888
26889
26890
26891
26892
26893
26894
26895
26896
26897
26898
26899
26900
26901
26902
26903
26904
26905
26906
26907
26908
26909
26910
26911
26912
26913
26914
26915
26916
26917
26918
26919
26920
26921
26922
26923
26924
26925
26926
26927
26928
26929
26930
26931
26932
26933
26934
26935
26936
26937
26938
26939
26940
26941
26942
26943
26944
26945
26946
26947
26948
26949
26950
26951
26952
26953
26954
26955
26956
26957
26958
26959
26960
26961
26962
26963
26964
26965
26966
26967
26968
26969
26970
26971
26972
26973
26974
26975
26976
26977
26978
26979
26980
26981
26982
26983
26984
26985
26986
26987
26988
26989
26990
26991
26992
26993
26994
26995
26996
26997
26998
26999
27000
27001
27002
27003
27004
27005
27006
27007
27008
27009
27010
27011
27012
27013
27014
27015
27016
27017
27018
27019
27020
27021
27022
27023
27024
27025
27026
27027
27028
27029
27030
27031
27032
27033
27034
27035
27036
27037
27038
27039
27040
27041
27042
27043
27044
27045
27046
27047
27048
27049
27050
27051
27052
27053
27054
27055
27056
27057
27058
27059
27060
27061
27062
27063
27064
27065
27066
27067
27068
27069
27070
27071
27072
27073
27074
27075
27076
27077
27078
27079
27080
27081
27082
27083
27084
27085
27086
27087
27088
27089
27090
27091
27092
27093
27094
27095
27096
27097
27098
27099
27100
27101
27102
27103
27104
27105
27106
27107
27108
27109
27110
27111
27112
27113
27114
27115
27116
27117
27118
27119
27120
27121
27122
27123
27124
27125
27126
27127
27128
27129
27130
27131
27132
27133
27134
27135
27136
27137
27138
27139
27140
27141
27142
27143
27144
27145
27146
27147
27148
27149
27150
27151
27152
27153
27154
27155
27156
27157
27158
27159
27160
27161
27162
27163
27164
27165
27166
27167
27168
27169
27170
27171
27172
27173
27174
27175
27176
27177
27178
27179
27180
27181
27182
27183
27184
27185
27186
27187
27188
27189
27190
27191
27192
27193
27194
27195
27196
27197
27198
27199
27200
27201
27202
27203
27204
27205
27206
27207
27208
27209
27210
27211
27212
27213
27214
27215
27216
27217
27218
27219
27220
27221
27222
27223
27224
27225
27226
27227
27228
27229
27230
27231
27232
27233
27234
27235
27236
27237
27238
27239
27240
27241
27242
27243
27244
27245
27246
27247
27248
27249
27250
27251
27252
27253
27254
27255
27256
27257
27258
27259
27260
27261
27262
27263
27264
27265
27266
27267
27268
27269
27270
27271
27272
27273
27274
27275
27276
27277
27278
27279
27280
27281
27282
27283
27284
27285
27286
27287
27288
27289
27290
27291
27292
27293
27294
27295
27296
27297
27298
27299
27300
27301
27302
27303
27304
27305
27306
27307
27308
27309
27310
27311
27312
27313
27314
27315
27316
27317
27318
27319
27320
27321
27322
27323
27324
27325
27326
27327
27328
27329
27330
27331
27332
27333
27334
27335
27336
27337
27338
27339
27340
27341
27342
27343
27344
27345
27346
27347
27348
27349
27350
27351
27352
27353
27354
27355
27356
27357
27358
27359
27360
27361
27362
27363
27364
27365
27366
27367
27368
27369
27370
27371
27372
27373
27374
27375
27376
27377
27378
27379
27380
27381
27382
27383
27384
27385
27386
27387
27388
27389
27390
27391
27392
27393
27394
27395
27396
27397
27398
27399
27400
27401
27402
27403
27404
27405
27406
27407
27408
27409
27410
27411
27412
27413
27414
27415
27416
27417
27418
27419
27420
27421
27422
27423
27424
27425
27426
27427
27428
27429
27430
27431
27432
27433
27434
27435
27436
27437
27438
27439
27440
27441
27442
27443
27444
27445
27446
27447
27448
27449
27450
27451
27452
27453
27454
27455
27456
27457
27458
27459
27460
27461
27462
27463
27464
27465
27466
27467
27468
27469
27470
27471
27472
27473
27474
27475
27476
27477
27478
27479
27480
27481
27482
27483
27484
27485
27486
27487
27488
27489
27490
27491
27492
27493
27494
27495
27496
27497
27498
27499
27500
27501
27502
27503
27504
27505
27506
27507
27508
27509
27510
27511
27512
27513
27514
27515
27516
27517
27518
27519
27520
27521
27522
27523
27524
27525
27526
27527
27528
27529
27530
27531
27532
27533
27534
27535
27536
27537
27538
27539
27540
27541
27542
27543
27544
27545
27546
27547
27548
27549
27550
27551
27552
27553
27554
27555
27556
27557
27558
27559
27560
27561
27562
27563
27564
27565
27566
27567
27568
27569
27570
27571
27572
27573
27574
27575
27576
27577
27578
27579
27580
27581
27582
27583
27584
27585
27586
27587
27588
27589
27590
27591
27592
27593
27594
27595
27596
27597
27598
27599
27600
27601
27602
27603
27604
27605
27606
27607
27608
27609
27610
27611
27612
27613
27614
27615
27616
27617
27618
27619
27620
27621
27622
27623
27624
27625
27626
27627
27628
27629
27630
27631
27632
27633
27634
27635
27636
27637
27638
27639
27640
27641
27642
27643
27644
27645
27646
27647
27648
27649
27650
27651
27652
27653
27654
27655
27656
27657
27658
27659
27660
27661
27662
27663
27664
27665
27666
27667
27668
27669
27670
27671
27672
27673
27674
27675
27676
27677
27678
27679
27680
27681
27682
27683
27684
27685
27686
27687
27688
27689
27690
27691
27692
27693
27694
27695
27696
27697
27698
27699
27700
27701
27702
27703
27704
27705
27706
27707
27708
27709
27710
27711
27712
27713
27714
27715
27716
27717
27718
27719
27720
27721
27722
27723
27724
27725
27726
27727
27728
27729
27730
27731
27732
27733
27734
27735
27736
27737
27738
27739
27740
27741
27742
27743
27744
27745
27746
27747
27748
27749
27750
27751
27752
27753
27754
27755
27756
27757
27758
27759
27760
27761
27762
27763
27764
27765
27766
27767
27768
27769
27770
27771
27772
27773
27774
27775
27776
27777
27778
27779
27780
27781
27782
27783
27784
27785
27786
27787
27788
27789
27790
27791
27792
27793
27794
27795
27796
27797
27798
27799
27800
27801
27802
27803
27804
27805
27806
27807
27808
27809
27810
27811
27812
27813
27814
27815
27816
27817
27818
27819
27820
27821
27822
27823
27824
27825
27826
27827
27828
27829
27830
27831
27832
27833
27834
27835
27836
27837
27838
27839
27840
27841
27842
27843
27844
27845
27846
27847
27848
27849
27850
27851
27852
27853
27854
27855
27856
27857
27858
27859
27860
27861
27862
27863
27864
27865
27866
27867
27868
27869
27870
27871
27872
27873
27874
27875
27876
27877
27878
27879
27880
27881
27882
27883
27884
27885
27886
27887
27888
27889
27890
27891
27892
27893
27894
27895
27896
27897
27898
27899
27900
27901
27902
27903
27904
27905
27906
27907
27908
27909
27910
27911
27912
27913
27914
27915
27916
27917
27918
27919
27920
27921
27922
27923
27924
27925
27926
27927
27928
27929
27930
27931
27932
27933
27934
27935
27936
27937
27938
27939
27940
27941
27942
27943
27944
27945
27946
27947
27948
27949
27950
27951
27952
27953
27954
27955
27956
27957
27958
27959
27960
27961
27962
27963
27964
27965
27966
27967
27968
27969
27970
27971
27972
27973
27974
27975
27976
27977
27978
27979
27980
27981
27982
27983
27984
27985
27986
27987
27988
27989
27990
27991
27992
27993
27994
27995
27996
27997
27998
27999
28000
28001
28002
28003
28004
28005
28006
28007
28008
28009
28010
28011
28012
28013
28014
28015
28016
28017
28018
28019
28020
28021
28022
28023
28024
28025
28026
28027
28028
28029
28030
28031
28032
28033
28034
28035
28036
28037
28038
28039
28040
28041
28042
28043
28044
28045
28046
28047
28048
28049
28050
28051
28052
28053
28054
28055
28056
28057
28058
28059
28060
28061
28062
28063
28064
28065
28066
28067
28068
28069
28070
28071
28072
28073
28074
28075
28076
28077
28078
28079
28080
28081
28082
28083
28084
28085
28086
28087
28088
28089
28090
28091
28092
28093
28094
28095
28096
28097
28098
28099
28100
28101
28102
28103
28104
28105
28106
28107
28108
28109
28110
28111
28112
28113
28114
28115
28116
28117
28118
28119
28120
28121
28122
28123
28124
28125
28126
28127
28128
28129
28130
28131
28132
28133
28134
28135
28136
28137
28138
28139
28140
28141
28142
28143
28144
28145
28146
28147
28148
28149
28150
28151
28152
28153
28154
28155
28156
28157
28158
28159
28160
28161
28162
28163
28164
28165
28166
28167
28168
28169
28170
28171
28172
28173
28174
28175
28176
28177
28178
28179
28180
28181
28182
28183
28184
28185
28186
28187
28188
28189
28190
28191
28192
28193
28194
28195
28196
28197
28198
28199
28200
28201
28202
28203
28204
28205
28206
28207
28208
28209
28210
28211
28212
28213
28214
28215
28216
28217
28218
28219
28220
28221
28222
28223
28224
28225
28226
28227
28228
28229
28230
28231
28232
28233
28234
28235
28236
28237
28238
28239
28240
28241
28242
28243
28244
28245
28246
28247
28248
28249
28250
28251
28252
28253
28254
28255
28256
28257
28258
28259
28260
28261
28262
28263
28264
28265
28266
28267
28268
28269
28270
28271
28272
28273
28274
28275
28276
28277
28278
28279
28280
28281
28282
28283
28284
28285
28286
28287
28288
28289
28290
28291
28292
28293
28294
28295
28296
28297
28298
28299
28300
28301
28302
28303
28304
28305
28306
28307
28308
28309
28310
28311
28312
28313
28314
28315
28316
28317
28318
28319
28320
28321
28322
28323
28324
28325
28326
28327
28328
28329
28330
28331
28332
28333
28334
28335
28336
28337
28338
28339
28340
28341
28342
28343
28344
28345
28346
28347
28348
28349
28350
28351
28352
28353
28354
28355
28356
28357
28358
28359
28360
28361
28362
28363
28364
28365
28366
28367
28368
28369
28370
28371
28372
28373
28374
28375
28376
28377
28378
28379
28380
28381
28382
28383
28384
28385
28386
28387
28388
28389
28390
28391
28392
28393
28394
28395
28396
28397
28398
28399
28400
28401
28402
28403
28404
28405
28406
28407
28408
28409
28410
28411
28412
28413
28414
28415
28416
28417
28418
28419
28420
28421
28422
28423
28424
28425
28426
28427
28428
28429
28430
28431
28432
28433
28434
28435
28436
28437
28438
28439
28440
28441
28442
28443
28444
28445
28446
28447
28448
28449
28450
28451
28452
28453
28454
28455
28456
28457
28458
28459
28460
28461
28462
28463
28464
28465
28466
28467
28468
28469
28470
28471
28472
28473
28474
28475
28476
28477
28478
28479
28480
28481
28482
28483
28484
28485
28486
28487
28488
28489
28490
28491
28492
28493
28494
28495
28496
28497
28498
28499
28500
28501
28502
28503
28504
28505
28506
28507
28508
28509
28510
28511
28512
28513
28514
28515
28516
28517
28518
28519
28520
28521
28522
28523
28524
28525
28526
28527
28528
28529
28530
28531
28532
28533
28534
28535
28536
28537
28538
28539
28540
28541
28542
28543
28544
28545
28546
28547
28548
28549
28550
28551
28552
28553
28554
28555
28556
28557
28558
28559
28560
28561
28562
28563
28564
28565
28566
28567
28568
28569
28570
28571
28572
28573
28574
28575
28576
28577
28578
28579
28580
28581
28582
28583
28584
28585
28586
28587
28588
28589
28590
28591
28592
28593
28594
28595
28596
28597
28598
28599
28600
28601
28602
28603
28604
28605
28606
28607
28608
28609
28610
28611
28612
28613
28614
28615
28616
28617
28618
28619
28620
28621
28622
28623
28624
28625
28626
28627
28628
28629
28630
28631
28632
28633
28634
28635
28636
28637
28638
28639
28640
28641
28642
28643
28644
28645
28646
28647
28648
28649
28650
28651
28652
28653
28654
28655
28656
28657
28658
28659
28660
28661
28662
28663
28664
28665
28666
28667
28668
28669
28670
28671
28672
28673
28674
28675
28676
28677
28678
28679
28680
28681
28682
28683
28684
28685
28686
28687
28688
28689
28690
28691
28692
28693
28694
28695
28696
28697
28698
28699
28700
28701
28702
28703
28704
28705
28706
28707
28708
28709
28710
28711
28712
28713
28714
28715
28716
28717
28718
28719
28720
28721
28722
28723
28724
28725
28726
28727
28728
28729
28730
28731
28732
28733
28734
28735
28736
28737
28738
28739
28740
28741
28742
28743
28744
28745
28746
28747
28748
28749
28750
28751
28752
28753
28754
28755
28756
28757
28758
28759
28760
28761
28762
28763
28764
28765
28766
28767
28768
28769
28770
28771
28772
28773
28774
28775
28776
28777
28778
28779
28780
28781
28782
28783
28784
28785
28786
28787
28788
28789
28790
28791
28792
28793
28794
28795
28796
28797
28798
28799
28800
28801
28802
28803
28804
28805
28806
28807
28808
28809
28810
28811
28812
28813
28814
28815
28816
28817
28818
28819
28820
28821
28822
28823
28824
28825
28826
28827
28828
28829
28830
28831
28832
28833
28834
28835
28836
28837
28838
28839
28840
28841
28842
28843
28844
28845
28846
28847
28848
28849
28850
28851
28852
28853
28854
28855
28856
28857
28858
28859
28860
28861
28862
28863
28864
28865
28866
28867
28868
28869
28870
28871
28872
28873
28874
28875
28876
28877
28878
28879
28880
28881
28882
28883
28884
28885
28886
28887
28888
28889
28890
28891
28892
28893
28894
28895
28896
28897
28898
28899
28900
28901
28902
28903
28904
28905
28906
28907
28908
28909
28910
28911
28912
28913
28914
28915
28916
28917
28918
28919
28920
28921
28922
28923
28924
28925
28926
28927
28928
28929
28930
28931
28932
28933
28934
28935
28936
28937
28938
28939
28940
28941
28942
28943
28944
28945
28946
28947
28948
28949
28950
28951
28952
28953
28954
28955
28956
28957
28958
28959
28960
28961
28962
28963
28964
28965
28966
28967
28968
28969
28970
28971
28972
28973
28974
28975
28976
28977
28978
28979
28980
28981
28982
28983
28984
28985
28986
28987
28988
28989
28990
28991
28992
28993
28994
28995
28996
28997
28998
28999
29000
29001
29002
29003
29004
29005
29006
29007
29008
29009
29010
29011
29012
29013
29014
29015
29016
29017
29018
29019
29020
29021
29022
29023
29024
29025
29026
29027
29028
29029
29030
29031
29032
29033
29034
29035
29036
29037
29038
29039
29040
29041
29042
29043
29044
29045
29046
29047
29048
29049
29050
29051
29052
29053
29054
29055
29056
29057
29058
29059
29060
29061
29062
29063
29064
29065
29066
29067
29068
29069
29070
29071
29072
29073
29074
29075
29076
29077
29078
29079
29080
29081
29082
29083
29084
29085
29086
29087
29088
29089
29090
29091
29092
29093
29094
29095
29096
29097
29098
29099
29100
29101
29102
29103
29104
29105
29106
29107
29108
29109
29110
29111
29112
29113
29114
29115
29116
29117
29118
29119
29120
29121
29122
29123
29124
29125
29126
29127
29128
29129
29130
29131
29132
29133
29134
29135
29136
29137
29138
29139
29140
29141
29142
29143
29144
29145
29146
29147
29148
29149
29150
29151
29152
29153
29154
29155
29156
29157
29158
29159
29160
29161
29162
29163
29164
29165
29166
29167
29168
29169
29170
29171
29172
29173
29174
29175
29176
29177
29178
29179
29180
29181
29182
29183
29184
29185
29186
29187
29188
29189
29190
29191
29192
29193
29194
29195
29196
29197
29198
29199
29200
29201
29202
29203
29204
29205
29206
29207
29208
29209
29210
29211
29212
29213
29214
29215
29216
29217
29218
29219
29220
29221
29222
29223
29224
29225
29226
29227
29228
29229
29230
29231
29232
29233
29234
29235
29236
29237
29238
29239
29240
29241
29242
29243
29244
29245
29246
29247
29248
29249
29250
29251
29252
29253
29254
29255
29256
29257
29258
29259
29260
29261
29262
29263
29264
29265
29266
29267
29268
29269
29270
29271
29272
29273
29274
29275
29276
29277
29278
29279
29280
29281
29282
29283
29284
29285
29286
29287
29288
29289
29290
29291
29292
29293
29294
29295
29296
29297
29298
29299
29300
29301
29302
29303
29304
29305
29306
29307
29308
29309
29310
29311
29312
29313
29314
29315
29316
29317
29318
29319
29320
29321
29322
29323
29324
29325
29326
29327
29328
29329
29330
29331
29332
29333
29334
29335
29336
29337
29338
29339
29340
29341
29342
29343
29344
29345
29346
29347
29348
29349
29350
29351
29352
29353
29354
29355
29356
29357
29358
29359
29360
29361
29362
29363
29364
29365
29366
29367
29368
29369
29370
29371
29372
29373
29374
29375
29376
29377
29378
29379
29380
29381
29382
29383
29384
29385
29386
29387
29388
29389
29390
29391
29392
29393
29394
29395
29396
29397
29398
29399
29400
29401
29402
29403
29404
29405
29406
29407
29408
29409
29410
29411
29412
29413
29414
29415
29416
29417
29418
29419
29420
29421
29422
29423
29424
29425
29426
29427
29428
29429
29430
29431
29432
29433
29434
29435
29436
29437
29438
29439
29440
29441
29442
29443
29444
29445
29446
29447
29448
29449
29450
29451
29452
29453
29454
29455
29456
29457
29458
29459
29460
29461
29462
29463
29464
29465
29466
29467
29468
29469
29470
29471
29472
29473
29474
29475
29476
29477
29478
29479
29480
29481
29482
29483
29484
29485
29486
29487
29488
29489
29490
29491
29492
29493
29494
29495
29496
29497
29498
29499
29500
29501
29502
29503
29504
29505
29506
29507
29508
29509
29510
29511
29512
29513
29514
29515
29516
29517
29518
29519
29520
29521
29522
29523
29524
29525
29526
29527
29528
29529
29530
29531
29532
29533
29534
29535
29536
29537
29538
29539
29540
29541
29542
29543
29544
29545
29546
29547
29548
29549
29550
29551
29552
29553
29554
29555
29556
29557
29558
29559
29560
29561
29562
29563
29564
29565
29566
29567
29568
29569
29570
29571
29572
29573
29574
29575
29576
29577
29578
29579
29580
29581
29582
29583
29584
29585
29586
29587
29588
29589
29590
29591
29592
29593
29594
29595
29596
29597
29598
29599
29600
29601
29602
29603
29604
29605
29606
29607
29608
29609
29610
29611
29612
29613
29614
29615
29616
29617
29618
29619
29620
29621
29622
29623
29624
29625
29626
29627
29628
29629
29630
29631
29632
29633
29634
29635
29636
29637
29638
29639
29640
29641
29642
29643
29644
29645
29646
29647
29648
29649
29650
29651
29652
29653
29654
29655
29656
29657
29658
29659
29660
29661
29662
29663
29664
29665
29666
29667
29668
29669
29670
29671
29672
29673
29674
29675
29676
29677
29678
29679
29680
29681
29682
29683
29684
29685
29686
29687
29688
29689
29690
29691
29692
29693
29694
29695
29696
29697
29698
29699
29700
29701
29702
29703
29704
29705
29706
29707
29708
29709
29710
29711
29712
29713
29714
29715
29716
29717
29718
29719
29720
29721
29722
29723
29724
29725
29726
29727
29728
29729
29730
29731
29732
29733
29734
29735
29736
29737
29738
29739
29740
29741
29742
29743
29744
29745
29746
29747
29748
29749
29750
29751
29752
29753
29754
29755
29756
29757
29758
29759
29760
29761
29762
29763
29764
29765
29766
29767
29768
29769
29770
29771
29772
29773
29774
29775
29776
29777
29778
29779
29780
29781
29782
29783
29784
29785
29786
29787
29788
29789
29790
29791
29792
29793
29794
29795
29796
29797
29798
29799
29800
29801
29802
29803
29804
29805
29806
29807
29808
29809
29810
29811
29812
29813
29814
29815
29816
29817
29818
29819
29820
29821
29822
29823
29824
29825
29826
29827
29828
29829
29830
29831
29832
29833
29834
29835
29836
29837
29838
29839
29840
29841
29842
29843
29844
29845
29846
29847
29848
29849
29850
29851
29852
29853
29854
29855
29856
29857
29858
29859
29860
29861
29862
29863
29864
29865
29866
29867
29868
29869
29870
29871
29872
29873
29874
29875
29876
29877
29878
29879
29880
29881
29882
29883
29884
29885
29886
29887
29888
29889
29890
29891
29892
29893
29894
29895
29896
29897
29898
29899
29900
29901
29902
29903
29904
29905
29906
29907
29908
29909
29910
29911
29912
29913
29914
29915
29916
29917
29918
29919
29920
29921
29922
29923
29924
29925
29926
29927
29928
29929
29930
29931
29932
29933
29934
29935
29936
29937
29938
29939
29940
29941
29942
29943
29944
29945
29946
29947
29948
29949
29950
29951
29952
29953
29954
29955
29956
29957
29958
29959
29960
29961
29962
29963
29964
29965
29966
29967
29968
29969
29970
29971
29972
29973
29974
29975
29976
29977
29978
29979
29980
29981
29982
29983
29984
29985
29986
29987
29988
29989
29990
29991
29992
29993
29994
29995
29996
29997
29998
29999
30000
30001
30002
30003
30004
30005
30006
30007
30008
30009
30010
30011
30012
30013
30014
30015
30016
30017
30018
30019
30020
30021
30022
30023
30024
30025
30026
30027
30028
30029
30030
30031
30032
30033
30034
30035
30036
30037
30038
30039
30040
30041
30042
30043
30044
30045
30046
30047
30048
30049
30050
30051
30052
30053
30054
30055
30056
30057
30058
30059
30060
30061
30062
30063
30064
30065
30066
30067
30068
30069
30070
30071
30072
30073
30074
30075
30076
30077
30078
30079
30080
30081
30082
30083
30084
30085
30086
30087
30088
30089
30090
30091
30092
30093
30094
30095
30096
30097
30098
30099
30100
30101
30102
30103
30104
30105
30106
30107
30108
30109
30110
30111
30112
30113
30114
30115
30116
30117
30118
30119
30120
30121
30122
30123
30124
30125
30126
30127
30128
30129
30130
30131
30132
30133
30134
30135
30136
30137
30138
30139
30140
30141
30142
30143
30144
30145
30146
30147
30148
30149
30150
30151
30152
30153
30154
30155
30156
30157
30158
30159
30160
30161
30162
30163
30164
30165
30166
30167
30168
30169
30170
30171
30172
30173
30174
30175
30176
30177
30178
30179
30180
30181
30182
30183
30184
30185
30186
30187
30188
30189
30190
30191
30192
30193
30194
30195
30196
30197
30198
30199
30200
30201
30202
30203
30204
30205
30206
30207
30208
30209
30210
30211
30212
30213
30214
30215
30216
30217
30218
30219
30220
30221
30222
30223
30224
30225
30226
30227
30228
30229
30230
30231
30232
30233
30234
30235
30236
30237
30238
30239
30240
30241
30242
30243
30244
30245
30246
30247
30248
30249
30250
30251
30252
30253
30254
30255
30256
30257
30258
30259
30260
30261
30262
30263
30264
30265
30266
30267
30268
30269
30270
30271
30272
30273
30274
30275
30276
30277
30278
30279
30280
30281
30282
30283
30284
30285
30286
30287
30288
30289
30290
30291
30292
30293
30294
30295
30296
30297
30298
30299
30300
30301
30302
30303
30304
30305
30306
30307
30308
30309
30310
30311
30312
30313
30314
30315
30316
30317
30318
30319
30320
30321
30322
30323
30324
30325
30326
30327
30328
30329
30330
30331
30332
30333
30334
30335
30336
30337
30338
30339
30340
30341
30342
30343
30344
30345
30346
30347
30348
30349
30350
30351
30352
30353
30354
30355
30356
30357
30358
30359
30360
30361
30362
30363
30364
30365
30366
30367
30368
30369
30370
30371
30372
30373
30374
30375
30376
30377
30378
30379
30380
30381
30382
30383
30384
30385
30386
30387
30388
30389
30390
30391
30392
30393
30394
30395
30396
30397
30398
30399
30400
30401
30402
30403
30404
30405
30406
30407
30408
30409
30410
30411
30412
30413
30414
30415
30416
30417
30418
30419
30420
30421
30422
30423
30424
30425
30426
30427
30428
30429
30430
30431
30432
30433
30434
30435
30436
30437
30438
30439
30440
30441
30442
30443
30444
30445
30446
30447
30448
30449
30450
30451
30452
30453
30454
30455
30456
30457
30458
30459
30460
30461
30462
30463
30464
30465
30466
30467
30468
30469
30470
30471
30472
30473
30474
30475
30476
30477
30478
30479
30480
30481
30482
30483
30484
30485
30486
30487
30488
30489
30490
30491
30492
30493
30494
30495
30496
30497
30498
30499
30500
30501
30502
30503
30504
30505
30506
30507
30508
30509
30510
30511
30512
30513
30514
30515
30516
30517
30518
30519
30520
30521
30522
30523
30524
30525
30526
30527
30528
30529
30530
30531
30532
30533
30534
30535
30536
30537
30538
30539
30540
30541
30542
30543
30544
30545
30546
30547
30548
30549
30550
30551
30552
30553
30554
30555
30556
30557
30558
30559
30560
30561
30562
30563
30564
30565
30566
30567
30568
30569
30570
30571
30572
30573
30574
30575
30576
30577
30578
30579
30580
30581
30582
30583
30584
30585
30586
30587
30588
30589
30590
30591
30592
30593
30594
30595
30596
30597
30598
30599
30600
30601
30602
30603
30604
30605
30606
30607
30608
30609
30610
30611
30612
30613
30614
30615
30616
30617
30618
30619
30620
30621
30622
30623
30624
30625
30626
30627
30628
30629
30630
30631
30632
30633
30634
30635
30636
30637
30638
30639
30640
30641
30642
30643
30644
30645
30646
30647
30648
30649
30650
30651
30652
30653
30654
30655
30656
30657
30658
30659
30660
30661
30662
30663
30664
30665
30666
30667
30668
30669
30670
30671
30672
30673
30674
30675
30676
30677
30678
30679
30680
30681
30682
30683
30684
30685
30686
30687
30688
30689
30690
30691
30692
30693
30694
30695
30696
30697
30698
30699
30700
30701
30702
30703
30704
30705
30706
30707
30708
30709
30710
30711
30712
30713
30714
30715
30716
30717
30718
30719
30720
30721
30722
30723
30724
30725
30726
30727
30728
30729
30730
30731
30732
30733
30734
30735
30736
30737
30738
30739
30740
30741
30742
30743
30744
30745
30746
30747
30748
30749
30750
30751
30752
30753
30754
30755
30756
30757
30758
30759
30760
30761
30762
30763
30764
30765
30766
30767
30768
30769
30770
30771
30772
30773
30774
30775
30776
30777
30778
30779
30780
30781
30782
30783
30784
30785
30786
30787
30788
30789
30790
30791
30792
30793
30794
30795
30796
30797
30798
30799
30800
30801
30802
30803
30804
30805
30806
30807
30808
30809
30810
30811
30812
30813
30814
30815
30816
30817
30818
30819
30820
30821
30822
30823
30824
30825
30826
30827
30828
30829
30830
30831
30832
30833
30834
30835
30836
30837
30838
30839
30840
30841
30842
30843
30844
30845
30846
30847
30848
30849
30850
30851
30852
30853
30854
30855
30856
30857
30858
30859
30860
30861
30862
30863
30864
30865
30866
30867
30868
30869
30870
30871
30872
30873
30874
30875
30876
30877
30878
30879
30880
30881
30882
30883
30884
30885
30886
30887
30888
30889
30890
30891
30892
30893
30894
30895
30896
30897
30898
30899
30900
30901
30902
30903
30904
30905
30906
30907
30908
30909
30910
30911
30912
30913
30914
30915
30916
30917
30918
30919
30920
30921
30922
30923
30924
30925
30926
30927
30928
30929
30930
30931
30932
30933
30934
30935
30936
30937
30938
30939
30940
30941
30942
30943
30944
30945
30946
30947
30948
30949
30950
30951
30952
30953
30954
30955
30956
30957
30958
30959
30960
30961
30962
30963
30964
30965
30966
30967
30968
30969
30970
30971
30972
30973
30974
30975
30976
30977
30978
30979
30980
30981
30982
30983
30984
30985
30986
30987
30988
30989
30990
30991
30992
30993
30994
30995
30996
30997
30998
30999
31000
31001
31002
31003
31004
31005
31006
31007
31008
31009
31010
31011
31012
31013
31014
31015
31016
31017
31018
31019
31020
31021
31022
31023
31024
31025
31026
31027
31028
31029
31030
31031
31032
31033
31034
31035
31036
31037
31038
31039
31040
31041
31042
31043
31044
31045
31046
31047
31048
31049
31050
31051
31052
31053
31054
31055
31056
31057
31058
31059
31060
31061
31062
31063
31064
31065
31066
31067
31068
31069
31070
31071
31072
31073
31074
31075
31076
31077
31078
31079
31080
31081
31082
31083
31084
31085
31086
31087
31088
31089
31090
31091
31092
31093
31094
31095
31096
31097
31098
31099
31100
31101
31102
31103
31104
31105
31106
31107
31108
31109
31110
31111
31112
31113
31114
31115
31116
31117
31118
31119
31120
31121
31122
31123
31124
31125
31126
31127
31128
31129
31130
31131
31132
31133
31134
31135
31136
31137
31138
31139
31140
31141
31142
31143
31144
31145
31146
31147
31148
31149
31150
31151
31152
31153
31154
31155
31156
31157
31158
31159
31160
31161
31162
31163
31164
31165
31166
31167
31168
31169
31170
31171
31172
31173
31174
31175
31176
31177
31178
31179
31180
31181
31182
31183
31184
31185
31186
31187
31188
31189
31190
31191
31192
31193
31194
31195
31196
31197
31198
31199
31200
31201
31202
31203
31204
31205
31206
31207
31208
31209
31210
31211
31212
31213
31214
31215
31216
31217
31218
31219
31220
31221
31222
31223
31224
31225
31226
31227
31228
31229
31230
31231
31232
31233
31234
31235
31236
31237
31238
31239
31240
31241
31242
31243
31244
31245
31246
31247
31248
31249
31250
31251
31252
31253
31254
31255
31256
31257
31258
31259
31260
31261
31262
31263
31264
31265
31266
31267
31268
31269
31270
31271
31272
31273
31274
31275
31276
31277
31278
31279
31280
31281
31282
31283
31284
31285
31286
31287
31288
31289
31290
31291
31292
31293
31294
31295
31296
31297
31298
31299
31300
31301
31302
31303
31304
31305
31306
31307
31308
31309
31310
31311
31312
31313
31314
31315
31316
31317
31318
31319
31320
31321
31322
31323
31324
31325
31326
31327
31328
31329
31330
31331
31332
31333
31334
31335
31336
31337
31338
31339
31340
31341
31342
31343
31344
31345
31346
31347
31348
31349
31350
31351
31352
31353
31354
31355
31356
31357
31358
31359
31360
31361
31362
31363
31364
31365
31366
31367
31368
31369
31370
31371
31372
31373
31374
31375
31376
31377
31378
31379
31380
31381
31382
31383
31384
31385
31386
31387
31388
31389
31390
31391
31392
31393
31394
31395
31396
31397
31398
31399
31400
31401
31402
31403
31404
31405
31406
31407
31408
31409
31410
31411
31412
31413
31414
31415
31416
31417
31418
31419
31420
31421
31422
31423
31424
31425
31426
31427
31428
31429
31430
31431
31432
31433
31434
31435
31436
31437
31438
31439
31440
31441
31442
31443
31444
31445
31446
31447
31448
31449
31450
31451
31452
31453
31454
31455
31456
31457
31458
31459
31460
31461
31462
31463
31464
31465
31466
31467
31468
31469
31470
31471
31472
31473
31474
31475
31476
31477
31478
31479
31480
31481
31482
31483
31484
31485
31486
31487
31488
31489
31490
31491
31492
31493
31494
31495
31496
31497
31498
31499
31500
31501
31502
31503
31504
31505
31506
31507
31508
31509
31510
31511
31512
31513
31514
31515
31516
31517
31518
31519
31520
31521
31522
31523
31524
31525
31526
31527
31528
31529
31530
31531
31532
31533
31534
31535
31536
31537
31538
31539
31540
31541
31542
31543
31544
31545
31546
31547
31548
31549
31550
31551
31552
31553
31554
31555
31556
31557
31558
31559
31560
31561
31562
31563
31564
31565
31566
31567
31568
31569
31570
31571
31572
31573
31574
31575
31576
31577
31578
31579
31580
31581
31582
31583
31584
31585
31586
31587
31588
31589
31590
31591
31592
31593
31594
31595
31596
31597
31598
31599
31600
31601
31602
31603
31604
31605
31606
31607
31608
31609
31610
31611
31612
31613
31614
31615
31616
31617
31618
31619
31620
31621
31622
31623
31624
31625
31626
31627
31628
31629
31630
31631
31632
31633
31634
31635
31636
31637
31638
31639
31640
31641
31642
31643
31644
31645
31646
31647
31648
31649
31650
31651
31652
31653
31654
31655
31656
31657
31658
31659
31660
31661
31662
31663
31664
31665
31666
31667
31668
31669
31670
31671
31672
31673
31674
31675
31676
31677
31678
31679
31680
31681
31682
31683
31684
31685
31686
31687
31688
31689
31690
31691
31692
31693
31694
31695
31696
31697
31698
31699
31700
31701
31702
31703
31704
31705
31706
31707
31708
31709
31710
31711
31712
31713
31714
31715
31716
31717
31718
31719
31720
31721
31722
31723
31724
31725
31726
31727
31728
31729
31730
31731
31732
31733
31734
31735
31736
31737
31738
31739
31740
31741
31742
31743
31744
31745
31746
31747
31748
31749
31750
31751
31752
31753
31754
31755
31756
31757
31758
31759
31760
31761
31762
31763
31764
31765
31766
31767
31768
31769
31770
31771
31772
31773
31774
31775
31776
31777
31778
31779
31780
31781
31782
31783
31784
31785
31786
31787
31788
31789
31790
31791
31792
31793
31794
31795
31796
31797
31798
31799
31800
31801
31802
31803
31804
31805
31806
31807
31808
31809
31810
31811
31812
31813
31814
31815
31816
31817
31818
31819
31820
31821
31822
31823
31824
31825
31826
31827
31828
31829
31830
31831
31832
31833
31834
31835
31836
31837
31838
31839
31840
31841
31842
31843
31844
31845
31846
31847
31848
31849
31850
31851
31852
31853
31854
31855
31856
31857
31858
31859
31860
31861
31862
31863
31864
31865
31866
31867
31868
31869
31870
31871
31872
31873
31874
31875
31876
31877
31878
31879
31880
31881
31882
31883
31884
31885
31886
31887
31888
31889
31890
31891
31892
31893
31894
31895
31896
31897
31898
31899
31900
31901
31902
31903
31904
31905
31906
31907
31908
31909
31910
31911
31912
31913
31914
31915
31916
31917
31918
31919
31920
31921
31922
31923
31924
31925
31926
31927
31928
31929
31930
31931
31932
31933
31934
31935
31936
31937
31938
31939
31940
31941
31942
31943
31944
31945
31946
31947
31948
31949
31950
31951
31952
31953
31954
31955
31956
31957
31958
31959
31960
31961
31962
31963
31964
31965
31966
31967
31968
31969
31970
31971
31972
31973
31974
31975
31976
31977
31978
31979
31980
31981
31982
31983
31984
31985
31986
31987
31988
31989
31990
31991
31992
31993
31994
31995
31996
31997
31998
31999
32000
32001
32002
32003
32004
32005
32006
32007
32008
32009
32010
32011
32012
32013
32014
32015
32016
32017
32018
32019
32020
32021
32022
32023
32024
32025
32026
32027
32028
32029
32030
32031
32032
32033
32034
32035
32036
32037
32038
32039
32040
32041
32042
32043
32044
32045
32046
32047
32048
32049
32050
32051
32052
32053
32054
32055
32056
32057
32058
32059
32060
32061
32062
32063
32064
32065
32066
32067
32068
32069
32070
32071
32072
32073
32074
32075
32076
32077
32078
32079
32080
32081
32082
32083
32084
32085
32086
32087
32088
32089
32090
32091
32092
32093
32094
32095
32096
32097
32098
32099
32100
32101
32102
32103
32104
32105
32106
32107
32108
32109
32110
32111
32112
32113
32114
32115
32116
32117
32118
32119
32120
32121
32122
32123
32124
32125
32126
32127
32128
32129
32130
32131
32132
32133
32134
32135
32136
32137
32138
32139
32140
32141
32142
32143
32144
32145
32146
32147
32148
32149
32150
32151
32152
32153
32154
32155
32156
32157
32158
32159
32160
32161
32162
32163
32164
32165
32166
32167
32168
32169
32170
32171
32172
32173
32174
32175
32176
32177
32178
32179
32180
32181
32182
32183
32184
32185
32186
32187
32188
32189
32190
32191
32192
32193
32194
32195
32196
32197
32198
32199
32200
32201
32202
32203
32204
32205
32206
32207
32208
32209
32210
32211
32212
32213
32214
32215
32216
32217
32218
32219
32220
32221
32222
32223
32224
32225
32226
32227
32228
32229
32230
32231
32232
32233
32234
32235
32236
32237
32238
32239
32240
32241
32242
32243
32244
32245
32246
32247
32248
32249
32250
32251
32252
32253
32254
32255
32256
32257
32258
32259
32260
32261
32262
32263
32264
32265
32266
32267
32268
32269
32270
32271
32272
32273
32274
32275
32276
32277
32278
32279
32280
32281
32282
32283
32284
32285
32286
32287
32288
32289
32290
32291
32292
32293
32294
32295
32296
32297
32298
32299
32300
32301
32302
32303
32304
32305
32306
32307
32308
32309
32310
32311
32312
32313
32314
32315
32316
32317
32318
32319
32320
32321
32322
32323
32324
32325
32326
32327
32328
32329
32330
32331
32332
32333
32334
32335
32336
32337
32338
32339
32340
32341
32342
32343
32344
32345
32346
32347
32348
32349
32350
32351
32352
32353
32354
32355
32356
32357
32358
32359
32360
32361
32362
32363
32364
32365
32366
32367
32368
32369
32370
32371
32372
32373
32374
32375
32376
32377
32378
32379
32380
32381
32382
32383
32384
32385
32386
32387
32388
32389
32390
32391
32392
32393
32394
32395
32396
32397
32398
32399
32400
32401
32402
32403
32404
32405
32406
32407
32408
32409
32410
32411
32412
32413
32414
32415
32416
32417
32418
32419
32420
32421
32422
32423
32424
32425
32426
32427
32428
32429
32430
32431
32432
32433
32434
32435
32436
32437
32438
32439
32440
32441
32442
32443
32444
32445
32446
32447
32448
32449
32450
32451
32452
32453
32454
32455
32456
32457
32458
32459
32460
32461
32462
32463
32464
32465
32466
32467
32468
32469
32470
32471
32472
32473
32474
32475
32476
32477
32478
32479
32480
32481
32482
32483
32484
32485
32486
32487
32488
32489
32490
32491
32492
32493
32494
32495
32496
32497
32498
32499
32500
32501
32502
32503
32504
32505
32506
32507
32508
32509
32510
32511
32512
32513
32514
32515
32516
32517
32518
32519
32520
32521
32522
32523
32524
32525
32526
32527
32528
32529
32530
32531
32532
32533
32534
32535
32536
32537
32538
32539
32540
32541
32542
32543
32544
32545
32546
32547
32548
32549
32550
32551
32552
32553
32554
32555
32556
32557
32558
32559
32560
32561
32562
32563
32564
32565
32566
32567
32568
32569
32570
32571
32572
32573
32574
32575
32576
32577
32578
32579
32580
32581
32582
32583
32584
32585
32586
32587
32588
32589
32590
32591
32592
32593
32594
32595
32596
32597
32598
32599
32600
32601
32602
32603
32604
32605
32606
32607
32608
32609
32610
32611
32612
32613
32614
32615
32616
32617
32618
32619
32620
32621
32622
32623
32624
32625
32626
32627
32628
32629
32630
32631
32632
32633
32634
32635
32636
32637
32638
32639
32640
32641
32642
32643
32644
32645
32646
32647
32648
32649
32650
32651
32652
32653
32654
32655
32656
32657
32658
32659
32660
32661
32662
32663
32664
32665
32666
32667
32668
32669
32670
32671
32672
32673
32674
32675
32676
32677
32678
32679
32680
32681
32682
32683
32684
32685
32686
32687
32688
32689
32690
32691
32692
32693
32694
32695
32696
32697
32698
32699
32700
32701
32702
32703
32704
32705
32706
32707
32708
32709
32710
32711
32712
32713
32714
32715
32716
32717
32718
32719
32720
32721
32722
32723
32724
32725
32726
32727
32728
32729
32730
32731
32732
32733
32734
32735
32736
32737
32738
32739
32740
32741
32742
32743
32744
32745
32746
32747
32748
32749
32750
32751
32752
32753
32754
32755
32756
32757
32758
32759
32760
32761
32762
32763
32764
32765
32766
32767
32768
32769
32770
32771
32772
32773
32774
32775
32776
32777
32778
32779
32780
32781
32782
32783
32784
32785
32786
32787
32788
32789
32790
32791
32792
32793
32794
32795
32796
32797
32798
32799
32800
32801
32802
32803
32804
32805
32806
32807
32808
32809
32810
32811
32812
32813
32814
32815
32816
32817
32818
32819
32820
32821
32822
32823
32824
32825
32826
32827
32828
32829
32830
32831
32832
32833
32834
32835
32836
32837
32838
32839
32840
32841
32842
32843
32844
32845
32846
32847
32848
32849
32850
32851
32852
32853
32854
32855
32856
32857
32858
32859
32860
32861
32862
32863
32864
32865
32866
32867
32868
32869
32870
32871
32872
32873
32874
32875
32876
32877
32878
32879
32880
32881
32882
32883
32884
32885
32886
32887
32888
32889
32890
32891
32892
32893
32894
32895
32896
32897
32898
32899
32900
32901
32902
32903
32904
32905
32906
32907
32908
32909
32910
32911
32912
32913
32914
32915
32916
32917
32918
32919
32920
32921
32922
32923
32924
32925
32926
32927
32928
32929
32930
32931
32932
32933
32934
32935
32936
32937
32938
32939
32940
32941
32942
32943
32944
32945
32946
32947
32948
32949
32950
32951
32952
32953
32954
32955
32956
32957
32958
32959
32960
32961
32962
32963
32964
32965
32966
32967
32968
32969
32970
32971
32972
32973
32974
32975
32976
32977
32978
32979
32980
32981
32982
32983
32984
32985
32986
32987
32988
32989
32990
32991
32992
32993
32994
32995
32996
32997
32998
32999
33000
33001
33002
33003
33004
33005
33006
33007
33008
33009
33010
33011
33012
33013
33014
33015
33016
33017
33018
33019
33020
33021
33022
33023
33024
33025
33026
33027
33028
33029
33030
33031
33032
33033
33034
33035
33036
33037
33038
33039
33040
33041
33042
33043
33044
33045
33046
33047
33048
33049
33050
33051
33052
33053
33054
33055
33056
33057
33058
33059
33060
33061
33062
33063
33064
33065
33066
33067
33068
33069
33070
33071
33072
33073
33074
33075
33076
33077
33078
33079
33080
33081
33082
33083
33084
33085
33086
33087
33088
33089
33090
33091
33092
33093
33094
33095
33096
33097
33098
33099
33100
33101
33102
33103
33104
33105
33106
33107
33108
33109
33110
33111
33112
33113
33114
33115
33116
33117
33118
33119
33120
33121
33122
33123
33124
33125
33126
33127
33128
33129
33130
33131
33132
33133
33134
33135
33136
33137
33138
33139
33140
33141
33142
33143
33144
33145
33146
33147
33148
33149
33150
33151
33152
33153
33154
33155
33156
33157
33158
33159
33160
33161
33162
33163
33164
33165
33166
33167
33168
33169
33170
33171
33172
33173
33174
33175
33176
33177
33178
33179
33180
33181
33182
33183
33184
33185
33186
33187
33188
33189
33190
33191
33192
33193
33194
33195
33196
33197
33198
33199
33200
33201
33202
33203
33204
33205
33206
33207
33208
33209
33210
33211
33212
33213
33214
33215
33216
33217
33218
33219
33220
33221
33222
33223
33224
33225
33226
33227
33228
33229
33230
33231
33232
33233
33234
33235
33236
33237
33238
33239
33240
33241
33242
33243
33244
33245
33246
33247
33248
33249
33250
33251
33252
33253
33254
33255
33256
33257
33258
33259
33260
33261
33262
33263
33264
33265
33266
33267
33268
33269
33270
33271
33272
33273
33274
33275
33276
33277
33278
33279
33280
33281
33282
33283
33284
33285
33286
33287
33288
33289
33290
33291
33292
33293
33294
33295
33296
33297
33298
33299
33300
33301
33302
33303
33304
33305
33306
33307
33308
33309
33310
33311
33312
33313
33314
33315
33316
33317
33318
33319
33320
33321
33322
33323
33324
33325
33326
33327
33328
33329
33330
33331
33332
33333
33334
33335
33336
33337
33338
33339
33340
33341
33342
33343
33344
33345
33346
33347
33348
33349
33350
33351
33352
33353
33354
33355
33356
33357
33358
33359
33360
33361
33362
33363
33364
33365
33366
33367
33368
33369
33370
33371
33372
33373
33374
33375
33376
33377
33378
33379
33380
33381
33382
33383
33384
33385
33386
33387
33388
33389
33390
33391
33392
33393
33394
33395
33396
33397
33398
33399
33400
33401
33402
33403
33404
33405
33406
33407
33408
33409
33410
33411
33412
33413
33414
33415
33416
33417
33418
33419
33420
33421
33422
33423
33424
33425
33426
33427
33428
33429
33430
33431
33432
33433
33434
33435
33436
33437
33438
33439
33440
33441
33442
33443
33444
33445
33446
33447
33448
33449
33450
33451
33452
33453
33454
33455
33456
33457
33458
33459
33460
33461
33462
33463
33464
33465
33466
33467
33468
33469
33470
33471
33472
33473
33474
33475
33476
33477
33478
33479
33480
33481
33482
33483
33484
33485
33486
33487
33488
33489
33490
33491
33492
33493
33494
33495
33496
33497
33498
33499
33500
33501
33502
33503
33504
33505
33506
33507
33508
33509
33510
33511
33512
33513
33514
33515
33516
33517
33518
33519
33520
33521
33522
33523
33524
33525
33526
33527
33528
33529
33530
33531
33532
33533
33534
33535
33536
33537
33538
33539
33540
33541
33542
33543
33544
33545
33546
33547
33548
33549
33550
33551
33552
33553
33554
33555
33556
33557
33558
33559
33560
33561
33562
33563
33564
33565
33566
33567
33568
33569
33570
33571
33572
33573
33574
33575
33576
33577
33578
33579
33580
33581
33582
33583
33584
33585
33586
33587
33588
33589
33590
33591
33592
33593
33594
33595
33596
33597
33598
33599
33600
33601
33602
33603
33604
33605
33606
33607
33608
33609
33610
33611
33612
33613
33614
33615
33616
33617
33618
33619
33620
33621
33622
33623
33624
33625
33626
33627
33628
33629
33630
33631
33632
33633
33634
33635
33636
33637
33638
33639
33640
33641
33642
33643
33644
33645
33646
33647
33648
33649
33650
33651
33652
33653
33654
33655
33656
33657
33658
33659
33660
33661
33662
33663
33664
33665
33666
33667
33668
33669
33670
33671
33672
33673
33674
33675
33676
33677
33678
33679
33680
33681
33682
33683
33684
33685
33686
33687
33688
33689
33690
33691
33692
33693
33694
33695
33696
33697
33698
33699
33700
33701
33702
33703
33704
33705
33706
33707
33708
33709
33710
33711
33712
33713
33714
33715
33716
33717
33718
33719
33720
33721
33722
33723
33724
33725
33726
33727
33728
33729
33730
33731
33732
33733
33734
33735
33736
33737
33738
33739
33740
33741
33742
33743
33744
33745
33746
33747
33748
33749
33750
33751
33752
33753
33754
33755
33756
33757
33758
33759
33760
33761
33762
33763
33764
33765
33766
33767
33768
33769
33770
33771
33772
33773
33774
33775
33776
33777
33778
33779
33780
33781
33782
33783
33784
33785
33786
33787
33788
33789
33790
33791
33792
33793
33794
33795
33796
33797
33798
33799
33800
33801
33802
33803
33804
33805
33806
33807
33808
33809
33810
33811
33812
33813
33814
33815
33816
33817
33818
33819
33820
33821
33822
33823
33824
33825
33826
33827
33828
33829
33830
33831
33832
33833
33834
33835
33836
33837
33838
33839
33840
33841
33842
33843
33844
33845
33846
33847
33848
33849
33850
33851
33852
33853
33854
33855
33856
33857
33858
33859
33860
33861
33862
33863
33864
33865
33866
33867
33868
33869
33870
33871
33872
33873
33874
33875
33876
33877
33878
33879
33880
33881
33882
33883
33884
33885
33886
33887
33888
33889
33890
33891
33892
33893
33894
33895
33896
33897
33898
33899
33900
33901
33902
33903
33904
33905
33906
33907
33908
33909
33910
33911
33912
33913
33914
33915
33916
33917
33918
33919
33920
33921
33922
33923
33924
33925
33926
33927
33928
33929
33930
33931
33932
33933
33934
33935
33936
33937
33938
33939
33940
33941
33942
33943
33944
33945
33946
33947
33948
33949
33950
33951
33952
33953
33954
33955
33956
33957
33958
33959
33960
33961
33962
33963
33964
33965
33966
33967
33968
33969
33970
33971
33972
33973
33974
33975
33976
33977
33978
33979
33980
33981
33982
33983
33984
33985
33986
33987
33988
33989
33990
33991
33992
33993
33994
33995
33996
33997
33998
33999
34000
34001
34002
34003
34004
34005
34006
34007
34008
34009
34010
34011
34012
34013
34014
34015
34016
34017
34018
34019
34020
34021
34022
34023
34024
34025
34026
34027
34028
34029
34030
34031
34032
34033
34034
34035
34036
34037
34038
34039
34040
34041
34042
34043
34044
34045
34046
34047
34048
34049
34050
34051
34052
34053
34054
34055
34056
34057
34058
34059
34060
34061
34062
34063
34064
34065
34066
34067
34068
34069
34070
34071
34072
34073
34074
34075
34076
34077
34078
34079
34080
34081
34082
34083
34084
34085
34086
34087
34088
34089
34090
34091
34092
34093
34094
34095
34096
34097
34098
34099
34100
34101
34102
34103
34104
34105
34106
34107
34108
34109
34110
34111
34112
34113
34114
34115
34116
34117
34118
34119
34120
34121
34122
34123
34124
34125
34126
34127
34128
34129
34130
34131
34132
34133
34134
34135
34136
34137
34138
34139
34140
34141
34142
34143
34144
34145
34146
34147
34148
34149
34150
34151
34152
34153
34154
34155
34156
34157
34158
34159
34160
34161
34162
34163
34164
34165
34166
34167
34168
34169
34170
34171
34172
34173
34174
34175
34176
34177
34178
34179
34180
34181
34182
34183
34184
34185
34186
34187
34188
34189
34190
34191
34192
34193
34194
34195
34196
34197
34198
34199
34200
34201
34202
34203
34204
34205
34206
34207
34208
34209
34210
34211
34212
34213
34214
34215
34216
34217
34218
34219
34220
34221
34222
34223
34224
34225
34226
34227
34228
34229
34230
34231
34232
34233
34234
34235
34236
34237
34238
34239
34240
34241
34242
34243
34244
34245
34246
34247
34248
34249
34250
34251
34252
34253
34254
34255
34256
34257
34258
34259
34260
34261
34262
34263
34264
34265
34266
34267
34268
34269
34270
34271
34272
34273
34274
34275
34276
34277
34278
34279
34280
34281
34282
34283
34284
34285
34286
34287
34288
34289
34290
34291
34292
34293
34294
34295
34296
34297
34298
34299
34300
34301
34302
34303
34304
34305
34306
34307
34308
34309
34310
34311
34312
34313
34314
34315
34316
34317
34318
34319
34320
34321
34322
34323
34324
34325
34326
34327
34328
34329
34330
34331
34332
34333
34334
34335
34336
34337
34338
34339
34340
34341
34342
34343
34344
34345
34346
34347
34348
34349
34350
34351
34352
34353
34354
34355
34356
34357
34358
34359
34360
34361
34362
34363
34364
34365
34366
34367
34368
34369
34370
34371
34372
34373
34374
34375
34376
34377
34378
34379
34380
34381
34382
34383
34384
34385
34386
34387
34388
34389
34390
34391
34392
34393
34394
34395
34396
34397
34398
34399
34400
34401
34402
34403
34404
34405
34406
34407
34408
34409
34410
34411
34412
34413
34414
34415
34416
34417
34418
34419
34420
34421
34422
34423
34424
34425
34426
34427
34428
34429
34430
34431
34432
34433
34434
34435
34436
34437
34438
34439
34440
34441
34442
34443
34444
34445
34446
34447
34448
34449
34450
34451
34452
34453
34454
34455
34456
34457
34458
34459
34460
34461
34462
34463
34464
34465
34466
34467
34468
34469
34470
34471
34472
34473
34474
34475
34476
34477
34478
34479
34480
34481
34482
34483
34484
34485
34486
34487
34488
34489
34490
34491
34492
34493
34494
34495
34496
34497
34498
34499
34500
34501
34502
34503
34504
34505
34506
34507
34508
34509
34510
34511
34512
34513
34514
34515
34516
34517
34518
34519
34520
34521
34522
34523
34524
34525
34526
34527
34528
34529
34530
34531
34532
34533
34534
34535
34536
34537
34538
34539
34540
34541
34542
34543
34544
34545
34546
34547
34548
34549
34550
34551
34552
34553
34554
34555
34556
34557
34558
34559
34560
34561
34562
34563
34564
34565
34566
34567
34568
34569
34570
34571
34572
34573
34574
34575
34576
34577
34578
34579
34580
34581
34582
34583
34584
34585
34586
34587
34588
34589
34590
34591
34592
34593
34594
34595
34596
34597
34598
34599
34600
34601
34602
34603
34604
34605
34606
34607
34608
34609
34610
34611
34612
34613
34614
34615
34616
34617
34618
34619
34620
34621
34622
34623
34624
34625
34626
34627
34628
34629
34630
34631
34632
34633
34634
34635
34636
34637
34638
34639
34640
34641
34642
34643
34644
34645
34646
34647
34648
34649
34650
34651
34652
34653
34654
34655
34656
34657
34658
34659
34660
34661
34662
34663
34664
34665
34666
34667
34668
34669
34670
34671
34672
34673
34674
34675
34676
34677
34678
34679
34680
34681
34682
34683
34684
34685
34686
34687
34688
34689
34690
34691
34692
34693
34694
34695
34696
34697
34698
34699
34700
34701
34702
34703
34704
34705
34706
34707
34708
34709
34710
34711
34712
34713
34714
34715
34716
34717
34718
34719
34720
34721
34722
34723
34724
34725
34726
34727
34728
34729
34730
34731
34732
34733
34734
34735
34736
34737
34738
34739
34740
34741
34742
34743
34744
34745
34746
34747
34748
34749
34750
34751
34752
34753
34754
34755
34756
34757
34758
34759
34760
34761
34762
34763
34764
34765
34766
34767
34768
34769
34770
34771
34772
34773
34774
34775
34776
34777
34778
34779
34780
34781
34782
34783
34784
34785
34786
34787
34788
34789
34790
34791
34792
34793
34794
34795
34796
34797
34798
34799
34800
34801
34802
34803
34804
34805
34806
34807
34808
34809
34810
34811
34812
34813
34814
34815
34816
34817
34818
34819
34820
34821
34822
34823
34824
34825
34826
34827
34828
34829
34830
34831
34832
34833
34834
34835
34836
34837
34838
34839
34840
34841
34842
34843
34844
34845
34846
34847
34848
34849
34850
34851
34852
34853
34854
34855
34856
34857
34858
34859
34860
34861
34862
34863
34864
34865
34866
34867
34868
34869
34870
34871
34872
34873
34874
34875
34876
34877
34878
34879
34880
34881
34882
34883
34884
34885
34886
34887
34888
34889
34890
34891
34892
34893
34894
34895
34896
34897
34898
34899
34900
34901
34902
34903
34904
34905
34906
34907
34908
34909
34910
34911
34912
34913
34914
34915
34916
34917
34918
34919
34920
34921
34922
34923
34924
34925
34926
34927
34928
34929
34930
34931
34932
34933
34934
34935
34936
34937
34938
34939
34940
34941
34942
34943
34944
34945
34946
34947
34948
34949
34950
34951
34952
34953
34954
34955
34956
34957
34958
34959
34960
34961
34962
34963
34964
34965
34966
34967
34968
34969
34970
34971
34972
34973
34974
34975
34976
34977
34978
34979
34980
34981
34982
34983
34984
34985
34986
34987
34988
34989
34990
34991
34992
34993
34994
34995
34996
34997
34998
34999
35000
35001
35002
35003
35004
35005
35006
35007
35008
35009
35010
35011
35012
35013
35014
35015
35016
35017
35018
35019
35020
35021
35022
35023
35024
35025
35026
35027
35028
35029
35030
35031
35032
35033
35034
35035
35036
35037
35038
35039
35040
35041
35042
35043
35044
35045
35046
35047
35048
35049
35050
35051
35052
35053
35054
35055
35056
35057
35058
35059
35060
35061
35062
35063
35064
35065
35066
35067
35068
35069
35070
35071
35072
35073
35074
35075
35076
35077
35078
35079
35080
35081
35082
35083
35084
35085
35086
35087
35088
35089
35090
35091
35092
35093
35094
35095
35096
35097
35098
35099
35100
35101
35102
35103
35104
35105
35106
35107
35108
35109
35110
35111
35112
35113
35114
35115
35116
35117
35118
35119
35120
35121
35122
35123
35124
35125
35126
35127
35128
35129
35130
35131
35132
35133
35134
35135
35136
35137
35138
35139
35140
35141
35142
35143
35144
35145
35146
35147
35148
35149
35150
35151
35152
35153
35154
35155
35156
35157
35158
35159
35160
35161
35162
35163
35164
35165
35166
35167
35168
35169
35170
35171
35172
35173
35174
35175
35176
35177
35178
35179
35180
35181
35182
35183
35184
35185
35186
35187
35188
35189
35190
35191
35192
35193
35194
35195
35196
35197
35198
35199
35200
35201
35202
35203
35204
35205
35206
35207
35208
35209
35210
35211
35212
35213
35214
35215
35216
35217
35218
35219
35220
35221
35222
35223
35224
35225
35226
35227
35228
35229
35230
35231
35232
35233
35234
35235
35236
35237
35238
35239
35240
35241
35242
35243
35244
35245
35246
35247
35248
35249
35250
35251
35252
35253
35254
35255
35256
35257
35258
35259
35260
35261
35262
35263
35264
35265
35266
35267
35268
35269
35270
35271
35272
35273
35274
35275
35276
35277
35278
35279
35280
35281
35282
35283
35284
35285
35286
35287
35288
35289
35290
35291
35292
35293
35294
35295
35296
35297
35298
35299
35300
35301
35302
35303
35304
35305
35306
35307
35308
35309
35310
35311
35312
35313
35314
35315
35316
35317
35318
35319
35320
35321
35322
35323
35324
35325
35326
35327
35328
35329
35330
35331
35332
35333
35334
35335
35336
35337
35338
35339
35340
35341
35342
35343
35344
35345
35346
35347
35348
35349
35350
35351
35352
35353
35354
35355
35356
35357
35358
35359
35360
35361
35362
35363
35364
35365
35366
35367
35368
35369
35370
35371
35372
35373
35374
35375
35376
35377
35378
35379
35380
35381
35382
35383
35384
35385
35386
35387
35388
35389
35390
35391
35392
35393
35394
35395
35396
35397
35398
35399
35400
35401
35402
35403
35404
35405
35406
35407
35408
35409
35410
35411
35412
35413
35414
35415
35416
35417
35418
35419
35420
35421
35422
35423
35424
35425
35426
35427
35428
35429
35430
35431
35432
35433
35434
35435
35436
35437
35438
35439
35440
35441
35442
35443
35444
35445
35446
35447
35448
35449
35450
35451
35452
35453
35454
35455
35456
35457
35458
35459
35460
35461
35462
35463
35464
35465
35466
35467
35468
35469
35470
35471
35472
35473
35474
35475
35476
35477
35478
35479
35480
35481
35482
35483
35484
35485
35486
35487
35488
35489
35490
35491
35492
35493
35494
35495
35496
35497
35498
35499
35500
35501
35502
35503
35504
35505
35506
35507
35508
35509
35510
35511
35512
35513
35514
35515
35516
35517
35518
35519
35520
35521
35522
35523
35524
35525
35526
35527
35528
35529
35530
35531
35532
35533
35534
35535
35536
35537
35538
35539
35540
35541
35542
35543
35544
35545
35546
35547
35548
35549
35550
35551
35552
35553
35554
35555
35556
35557
35558
35559
35560
35561
35562
35563
35564
35565
35566
35567
35568
35569
35570
35571
35572
35573
35574
35575
35576
35577
35578
35579
35580
35581
35582
35583
35584
35585
35586
35587
35588
35589
35590
35591
35592
35593
35594
35595
35596
35597
35598
35599
35600
35601
35602
35603
35604
35605
35606
35607
35608
35609
35610
35611
35612
35613
35614
35615
35616
35617
35618
35619
35620
35621
35622
35623
35624
35625
35626
35627
35628
35629
35630
35631
35632
35633
35634
35635
35636
35637
35638
35639
35640
35641
35642
35643
35644
35645
35646
35647
35648
35649
35650
35651
35652
35653
35654
35655
35656
35657
35658
35659
35660
35661
35662
35663
35664
35665
35666
35667
35668
35669
35670
35671
35672
35673
35674
35675
35676
35677
35678
35679
35680
35681
35682
35683
35684
35685
35686
35687
35688
35689
35690
35691
35692
35693
35694
35695
35696
35697
35698
35699
35700
35701
35702
35703
35704
35705
35706
35707
35708
35709
35710
35711
35712
35713
35714
35715
35716
35717
35718
35719
35720
35721
35722
35723
35724
35725
35726
35727
35728
35729
35730
35731
35732
35733
35734
35735
35736
35737
35738
35739
35740
35741
35742
35743
35744
35745
35746
35747
35748
35749
35750
35751
35752
35753
35754
35755
35756
35757
35758
35759
35760
35761
35762
35763
35764
35765
35766
35767
35768
35769
35770
35771
35772
35773
35774
35775
35776
35777
35778
35779
35780
35781
35782
35783
35784
35785
35786
35787
35788
35789
35790
35791
35792
35793
35794
35795
35796
35797
35798
35799
35800
35801
35802
35803
35804
35805
35806
35807
35808
35809
35810
35811
35812
35813
35814
35815
35816
35817
35818
35819
35820
35821
35822
35823
35824
35825
35826
35827
35828
35829
35830
35831
35832
35833
35834
35835
35836
35837
35838
35839
35840
35841
35842
35843
35844
35845
35846
35847
35848
35849
35850
35851
35852
35853
35854
35855
35856
35857
35858
35859
35860
35861
35862
35863
35864
35865
35866
35867
35868
35869
35870
35871
35872
35873
35874
35875
35876
35877
35878
35879
35880
35881
35882
35883
35884
35885
35886
35887
35888
35889
35890
35891
35892
35893
35894
35895
35896
35897
35898
35899
35900
35901
35902
35903
35904
35905
35906
35907
35908
35909
35910
35911
35912
35913
35914
35915
35916
35917
35918
35919
35920
35921
35922
35923
35924
35925
35926
35927
35928
35929
35930
35931
35932
35933
35934
35935
35936
35937
35938
35939
35940
35941
35942
35943
35944
35945
35946
35947
35948
35949
35950
35951
35952
35953
35954
35955
35956
35957
35958
35959
35960
35961
35962
35963
35964
35965
35966
35967
35968
35969
35970
35971
35972
35973
35974
35975
35976
35977
35978
35979
35980
35981
35982
35983
35984
35985
35986
35987
35988
35989
35990
35991
35992
35993
35994
35995
35996
35997
35998
35999
36000
36001
36002
36003
36004
36005
36006
36007
36008
36009
36010
36011
36012
36013
36014
36015
36016
36017
36018
36019
36020
36021
36022
36023
36024
36025
36026
36027
36028
36029
36030
36031
36032
36033
36034
36035
36036
36037
36038
36039
36040
36041
36042
36043
36044
36045
36046
36047
36048
36049
36050
36051
36052
36053
36054
36055
36056
36057
36058
36059
36060
36061
36062
36063
36064
36065
36066
36067
36068
36069
36070
36071
36072
36073
36074
36075
36076
36077
36078
36079
36080
36081
36082
36083
36084
36085
36086
36087
36088
36089
36090
36091
36092
36093
36094
36095
36096
36097
36098
36099
36100
36101
36102
36103
36104
36105
36106
36107
36108
36109
36110
36111
36112
36113
36114
36115
36116
36117
36118
36119
36120
36121
36122
36123
36124
36125
36126
36127
36128
36129
36130
36131
36132
36133
36134
36135
36136
36137
36138
36139
36140
36141
36142
36143
36144
36145
36146
36147
36148
36149
36150
36151
36152
36153
36154
36155
36156
36157
36158
36159
36160
36161
36162
36163
36164
36165
36166
36167
36168
36169
36170
36171
36172
36173
36174
36175
36176
36177
36178
36179
36180
36181
36182
36183
36184
36185
36186
36187
36188
36189
36190
36191
36192
36193
36194
36195
36196
36197
36198
36199
36200
36201
36202
36203
36204
36205
36206
36207
36208
36209
36210
36211
36212
36213
36214
36215
36216
36217
36218
36219
36220
36221
36222
36223
36224
36225
36226
36227
36228
36229
36230
36231
36232
36233
36234
36235
36236
36237
36238
36239
36240
36241
36242
36243
36244
36245
36246
36247
36248
36249
36250
36251
36252
36253
36254
36255
36256
36257
36258
36259
36260
36261
36262
36263
36264
36265
36266
36267
36268
36269
36270
36271
36272
36273
36274
36275
36276
36277
36278
36279
36280
36281
36282
36283
36284
36285
36286
36287
36288
36289
36290
36291
36292
36293
36294
36295
36296
36297
36298
36299
36300
36301
36302
36303
36304
36305
36306
36307
36308
36309
36310
36311
36312
36313
36314
36315
36316
36317
36318
36319
36320
36321
36322
36323
36324
36325
36326
36327
36328
36329
36330
36331
36332
36333
36334
36335
36336
36337
36338
36339
36340
36341
36342
36343
36344
36345
36346
36347
36348
36349
36350
36351
36352
36353
36354
36355
36356
36357
36358
36359
36360
36361
36362
36363
36364
36365
36366
36367
36368
36369
36370
36371
36372
36373
36374
36375
36376
36377
36378
36379
36380
36381
36382
36383
36384
36385
36386
36387
36388
36389
36390
36391
36392
36393
36394
36395
36396
36397
36398
36399
36400
36401
36402
36403
36404
36405
36406
36407
36408
36409
36410
36411
36412
36413
36414
36415
36416
36417
36418
36419
36420
36421
36422
36423
36424
36425
36426
36427
36428
36429
36430
36431
36432
36433
36434
36435
36436
36437
36438
36439
36440
36441
36442
36443
36444
36445
36446
36447
36448
36449
36450
36451
36452
36453
36454
36455
36456
36457
36458
36459
36460
36461
36462
36463
36464
36465
36466
36467
36468
36469
36470
36471
36472
36473
36474
36475
36476
36477
36478
36479
36480
36481
36482
36483
36484
36485
36486
36487
36488
36489
36490
36491
36492
36493
36494
36495
36496
36497
36498
36499
36500
36501
36502
36503
36504
36505
36506
36507
36508
36509
36510
36511
36512
36513
36514
36515
36516
36517
36518
36519
36520
36521
36522
36523
36524
36525
36526
36527
36528
36529
36530
36531
36532
36533
36534
36535
36536
36537
36538
36539
36540
36541
36542
36543
36544
36545
36546
36547
36548
36549
36550
36551
36552
36553
36554
36555
36556
36557
36558
36559
36560
36561
36562
36563
36564
36565
36566
36567
36568
36569
36570
36571
36572
36573
36574
36575
36576
36577
36578
36579
36580
36581
36582
36583
36584
36585
36586
36587
36588
36589
36590
36591
36592
36593
36594
36595
36596
36597
36598
36599
36600
36601
36602
36603
36604
36605
36606
36607
36608
36609
36610
36611
36612
36613
36614
36615
36616
36617
36618
36619
36620
36621
36622
36623
36624
36625
36626
36627
36628
36629
36630
36631
36632
36633
36634
36635
36636
36637
36638
36639
36640
36641
36642
36643
36644
36645
36646
36647
36648
36649
36650
36651
36652
36653
36654
36655
36656
36657
36658
36659
36660
36661
36662
36663
36664
36665
36666
36667
36668
36669
36670
36671
36672
36673
36674
36675
36676
36677
36678
36679
36680
36681
36682
36683
36684
36685
36686
36687
36688
36689
36690
36691
36692
36693
36694
36695
36696
36697
36698
36699
36700
36701
36702
36703
36704
36705
36706
36707
36708
36709
36710
36711
36712
36713
36714
36715
36716
36717
36718
36719
36720
36721
36722
36723
36724
36725
36726
36727
36728
36729
36730
36731
36732
36733
36734
36735
36736
36737
36738
36739
36740
36741
36742
36743
36744
36745
36746
36747
36748
36749
36750
36751
36752
36753
36754
36755
36756
36757
36758
36759
36760
36761
36762
36763
36764
36765
36766
36767
36768
36769
36770
36771
36772
36773
36774
36775
36776
36777
36778
36779
36780
36781
36782
36783
36784
36785
36786
36787
36788
36789
36790
36791
36792
36793
36794
36795
36796
36797
36798
36799
36800
36801
36802
36803
36804
36805
36806
36807
36808
36809
36810
36811
36812
36813
36814
36815
36816
36817
36818
36819
36820
36821
36822
36823
36824
36825
36826
36827
36828
36829
36830
36831
36832
36833
36834
36835
36836
36837
36838
36839
36840
36841
36842
36843
36844
36845
36846
36847
36848
36849
36850
36851
36852
36853
36854
36855
36856
36857
36858
36859
36860
36861
36862
36863
36864
36865
36866
36867
36868
36869
36870
36871
36872
36873
36874
36875
36876
36877
36878
36879
36880
36881
36882
36883
36884
36885
36886
36887
36888
36889
36890
36891
36892
36893
36894
36895
36896
36897
36898
36899
36900
36901
36902
36903
36904
36905
36906
36907
36908
36909
36910
36911
36912
36913
36914
36915
36916
36917
36918
36919
36920
36921
36922
36923
36924
36925
36926
36927
36928
36929
36930
36931
36932
36933
36934
36935
36936
36937
36938
36939
36940
36941
36942
36943
36944
36945
36946
36947
36948
36949
36950
36951
36952
36953
36954
36955
36956
36957
36958
36959
36960
36961
36962
36963
36964
36965
36966
36967
36968
36969
36970
36971
36972
36973
36974
36975
36976
36977
36978
36979
36980
36981
36982
36983
36984
36985
36986
36987
36988
36989
36990
36991
36992
36993
36994
36995
36996
36997
36998
36999
37000
37001
37002
37003
37004
37005
37006
37007
37008
37009
37010
37011
37012
37013
37014
37015
37016
37017
37018
37019
37020
37021
37022
37023
37024
37025
37026
37027
37028
37029
37030
37031
37032
37033
37034
37035
37036
37037
37038
37039
37040
37041
37042
37043
37044
37045
37046
37047
37048
37049
37050
37051
37052
37053
37054
37055
37056
37057
37058
37059
37060
37061
37062
37063
37064
37065
37066
37067
37068
37069
37070
37071
37072
37073
37074
37075
37076
37077
37078
37079
37080
37081
37082
37083
37084
37085
37086
37087
37088
37089
37090
37091
37092
37093
37094
37095
37096
37097
37098
37099
37100
37101
37102
37103
37104
37105
37106
37107
37108
37109
37110
37111
37112
37113
37114
37115
37116
37117
37118
37119
37120
37121
37122
37123
37124
37125
37126
37127
37128
37129
37130
37131
37132
37133
37134
37135
37136
37137
37138
37139
37140
37141
37142
37143
37144
37145
37146
37147
37148
37149
37150
37151
37152
37153
37154
37155
37156
37157
37158
37159
37160
37161
37162
37163
37164
37165
37166
37167
37168
37169
37170
37171
37172
37173
37174
37175
37176
37177
37178
37179
37180
37181
37182
37183
37184
37185
37186
37187
37188
37189
37190
37191
37192
37193
37194
37195
37196
37197
37198
37199
37200
37201
37202
37203
37204
37205
37206
37207
37208
37209
37210
37211
37212
37213
37214
37215
37216
37217
37218
37219
37220
37221
37222
37223
37224
37225
37226
37227
37228
37229
37230
37231
37232
37233
37234
37235
37236
37237
37238
37239
37240
37241
37242
37243
37244
37245
37246
37247
37248
37249
37250
37251
37252
37253
37254
37255
37256
37257
37258
37259
37260
37261
37262
37263
37264
37265
37266
37267
37268
37269
37270
37271
37272
37273
37274
37275
37276
37277
37278
37279
37280
37281
37282
37283
37284
37285
37286
37287
37288
37289
37290
37291
37292
37293
37294
37295
37296
37297
37298
37299
37300
37301
37302
37303
37304
37305
37306
37307
37308
37309
37310
37311
37312
37313
37314
37315
37316
37317
37318
37319
37320
37321
37322
37323
37324
37325
37326
37327
37328
37329
37330
37331
37332
37333
37334
37335
37336
37337
37338
37339
37340
37341
37342
37343
37344
37345
37346
37347
37348
37349
37350
37351
37352
37353
37354
37355
37356
37357
37358
37359
37360
37361
37362
37363
37364
37365
37366
37367
37368
37369
37370
37371
37372
37373
37374
37375
37376
37377
37378
37379
37380
37381
37382
37383
37384
37385
37386
37387
37388
37389
37390
37391
37392
37393
37394
37395
37396
37397
37398
37399
37400
37401
37402
37403
37404
37405
37406
37407
37408
37409
37410
37411
37412
37413
37414
37415
37416
37417
37418
37419
37420
37421
37422
37423
37424
37425
37426
37427
37428
37429
37430
37431
37432
37433
37434
37435
37436
37437
37438
37439
37440
37441
37442
37443
37444
37445
37446
37447
37448
37449
37450
37451
37452
37453
37454
37455
37456
37457
37458
37459
37460
37461
37462
37463
37464
37465
37466
37467
37468
37469
37470
37471
37472
37473
37474
37475
37476
37477
37478
37479
37480
37481
37482
37483
37484
37485
37486
37487
37488
37489
37490
37491
37492
37493
37494
37495
37496
37497
37498
37499
37500
37501
37502
37503
37504
37505
37506
37507
37508
37509
37510
37511
37512
37513
37514
37515
37516
37517
37518
37519
37520
37521
37522
37523
37524
37525
37526
37527
37528
37529
37530
37531
37532
37533
37534
37535
37536
37537
37538
37539
37540
37541
37542
37543
37544
37545
37546
37547
37548
37549
37550
37551
37552
37553
37554
37555
37556
37557
37558
37559
37560
37561
37562
37563
37564
37565
37566
37567
37568
37569
37570
37571
37572
37573
37574
37575
37576
37577
37578
37579
37580
37581
37582
37583
37584
37585
37586
37587
37588
37589
37590
37591
37592
37593
37594
37595
37596
37597
37598
37599
37600
37601
37602
37603
37604
37605
37606
37607
37608
37609
37610
37611
37612
37613
37614
37615
37616
37617
37618
37619
37620
37621
37622
37623
37624
37625
37626
37627
37628
37629
37630
37631
37632
37633
37634
37635
37636
37637
37638
37639
37640
37641
37642
37643
37644
37645
37646
37647
37648
37649
37650
37651
37652
37653
37654
37655
37656
37657
37658
37659
37660
37661
37662
37663
37664
37665
37666
37667
37668
37669
37670
37671
37672
37673
37674
37675
37676
37677
37678
37679
37680
37681
37682
37683
37684
37685
37686
37687
37688
37689
37690
37691
37692
37693
37694
37695
37696
37697
37698
37699
37700
37701
37702
37703
37704
37705
37706
37707
37708
37709
37710
37711
37712
37713
37714
37715
37716
37717
37718
37719
37720
37721
37722
37723
37724
37725
37726
37727
37728
37729
37730
37731
37732
37733
37734
37735
37736
37737
37738
37739
37740
37741
37742
37743
37744
37745
37746
37747
37748
37749
37750
37751
37752
37753
37754
37755
37756
37757
37758
37759
37760
37761
37762
37763
37764
37765
37766
37767
37768
37769
37770
37771
37772
37773
37774
37775
37776
37777
37778
37779
37780
37781
37782
37783
37784
37785
37786
37787
37788
37789
37790
37791
37792
37793
37794
37795
37796
37797
37798
37799
37800
37801
37802
37803
37804
37805
37806
37807
37808
37809
37810
37811
37812
37813
37814
37815
37816
37817
37818
37819
37820
37821
37822
37823
37824
37825
37826
37827
37828
37829
37830
37831
37832
37833
37834
37835
37836
37837
37838
37839
37840
37841
37842
37843
37844
37845
37846
37847
37848
37849
37850
37851
37852
37853
37854
37855
37856
37857
37858
37859
37860
37861
37862
37863
37864
37865
37866
37867
37868
37869
37870
37871
37872
37873
37874
37875
37876
37877
37878
37879
37880
37881
37882
37883
37884
37885
37886
37887
37888
37889
37890
37891
37892
37893
37894
37895
37896
37897
37898
37899
37900
37901
37902
37903
37904
37905
37906
37907
37908
37909
37910
37911
37912
37913
37914
37915
37916
37917
37918
37919
37920
37921
37922
37923
37924
37925
37926
37927
37928
37929
37930
37931
37932
37933
37934
37935
37936
37937
37938
37939
37940
37941
37942
37943
37944
37945
37946
37947
37948
37949
37950
37951
37952
37953
37954
37955
37956
37957
37958
37959
37960
37961
37962
37963
37964
37965
37966
37967
37968
37969
37970
37971
37972
37973
37974
37975
37976
37977
37978
37979
37980
37981
37982
37983
37984
37985
37986
37987
37988
37989
37990
37991
37992
37993
37994
37995
37996
37997
37998
37999
38000
38001
38002
38003
38004
38005
38006
38007
38008
38009
38010
38011
38012
38013
38014
38015
38016
38017
38018
38019
38020
38021
38022
38023
38024
38025
38026
38027
38028
38029
38030
38031
38032
38033
38034
38035
38036
38037
38038
38039
38040
38041
38042
38043
38044
38045
38046
38047
38048
38049
38050
38051
38052
38053
38054
38055
38056
38057
38058
38059
38060
38061
38062
38063
38064
38065
38066
38067
38068
38069
38070
38071
38072
38073
38074
38075
38076
38077
38078
38079
38080
38081
38082
38083
38084
38085
38086
38087
38088
38089
38090
38091
38092
38093
38094
38095
38096
38097
38098
38099
38100
38101
38102
38103
38104
38105
38106
38107
38108
38109
38110
38111
38112
38113
38114
38115
38116
38117
38118
38119
38120
38121
38122
38123
38124
38125
38126
38127
38128
38129
38130
38131
38132
38133
38134
38135
38136
38137
38138
38139
38140
38141
38142
38143
38144
38145
38146
38147
38148
38149
38150
38151
38152
38153
38154
38155
38156
38157
38158
38159
38160
38161
38162
38163
38164
38165
38166
38167
38168
38169
38170
38171
38172
38173
38174
38175
38176
38177
38178
38179
38180
38181
38182
38183
38184
38185
38186
38187
38188
38189
38190
38191
38192
38193
38194
38195
38196
38197
38198
38199
38200
38201
38202
38203
38204
38205
38206
38207
38208
38209
38210
38211
38212
38213
38214
38215
38216
38217
38218
38219
38220
38221
38222
38223
38224
38225
38226
38227
38228
38229
38230
38231
38232
38233
38234
38235
38236
38237
38238
38239
38240
38241
38242
38243
38244
38245
38246
38247
38248
38249
38250
38251
38252
38253
38254
38255
38256
38257
38258
38259
38260
38261
38262
38263
38264
38265
38266
38267
38268
38269
38270
38271
38272
38273
38274
38275
38276
38277
38278
38279
38280
38281
38282
38283
38284
38285
38286
38287
38288
38289
38290
38291
38292
38293
38294
38295
38296
38297
38298
38299
38300
38301
38302
38303
38304
38305
38306
38307
38308
38309
38310
38311
38312
38313
38314
38315
38316
38317
38318
38319
38320
38321
38322
38323
38324
38325
38326
38327
38328
38329
38330
38331
38332
38333
38334
38335
38336
38337
38338
38339
38340
38341
38342
38343
38344
38345
38346
38347
38348
38349
38350
38351
38352
38353
38354
38355
38356
38357
38358
38359
38360
38361
38362
38363
38364
38365
38366
38367
38368
38369
38370
38371
38372
38373
38374
38375
38376
38377
38378
38379
38380
38381
38382
38383
38384
38385
38386
38387
38388
38389
38390
38391
38392
38393
38394
38395
38396
38397
38398
38399
38400
38401
38402
38403
38404
38405
38406
38407
38408
38409
38410
38411
38412
38413
38414
38415
38416
38417
38418
38419
38420
38421
38422
38423
38424
38425
38426
38427
38428
38429
38430
38431
38432
38433
38434
38435
38436
38437
38438
38439
38440
38441
38442
38443
38444
38445
38446
38447
38448
38449
38450
38451
38452
38453
38454
38455
38456
38457
38458
38459
38460
38461
38462
38463
38464
38465
38466
38467
38468
38469
38470
38471
38472
38473
38474
38475
38476
38477
38478
38479
38480
38481
38482
38483
38484
38485
38486
38487
38488
38489
38490
38491
38492
38493
38494
38495
38496
38497
38498
38499
38500
38501
38502
38503
38504
38505
38506
38507
38508
38509
38510
38511
38512
38513
38514
38515
38516
38517
38518
38519
38520
38521
38522
38523
38524
38525
38526
38527
38528
38529
38530
38531
38532
38533
38534
38535
38536
38537
38538
38539
38540
38541
38542
38543
38544
38545
38546
38547
38548
38549
38550
38551
38552
38553
38554
38555
38556
38557
38558
38559
38560
38561
38562
38563
38564
38565
38566
38567
38568
38569
38570
38571
38572
38573
38574
38575
38576
38577
38578
38579
38580
38581
38582
38583
38584
38585
38586
38587
38588
38589
38590
38591
38592
38593
38594
38595
38596
38597
38598
38599
38600
38601
38602
38603
38604
38605
38606
38607
38608
38609
38610
38611
38612
38613
38614
38615
38616
38617
38618
38619
38620
38621
38622
38623
38624
38625
38626
38627
38628
38629
38630
38631
38632
38633
38634
38635
38636
38637
38638
38639
38640
38641
38642
38643
38644
38645
38646
38647
38648
38649
38650
38651
38652
38653
38654
38655
38656
38657
38658
38659
38660
38661
38662
38663
38664
38665
38666
38667
38668
38669
38670
38671
38672
38673
38674
38675
38676
38677
38678
38679
38680
38681
38682
38683
38684
38685
38686
38687
38688
38689
38690
38691
38692
38693
38694
38695
38696
38697
38698
38699
38700
38701
38702
38703
38704
38705
38706
38707
38708
38709
38710
38711
38712
38713
38714
38715
38716
38717
38718
38719
38720
38721
38722
38723
38724
38725
38726
38727
38728
38729
38730
38731
38732
38733
38734
38735
38736
38737
38738
38739
38740
38741
38742
38743
38744
38745
38746
38747
38748
38749
38750
38751
38752
38753
38754
38755
38756
38757
38758
38759
38760
38761
38762
38763
38764
38765
38766
38767
38768
38769
38770
38771
38772
38773
38774
38775
38776
38777
38778
38779
38780
38781
38782
38783
38784
38785
38786
38787
38788
38789
38790
38791
38792
38793
38794
38795
38796
38797
38798
38799
38800
38801
38802
38803
38804
38805
38806
38807
38808
38809
38810
38811
38812
38813
38814
38815
38816
38817
38818
38819
38820
38821
38822
38823
38824
38825
38826
38827
38828
38829
38830
38831
38832
38833
38834
38835
38836
38837
38838
38839
38840
38841
38842
38843
38844
38845
38846
38847
38848
38849
38850
38851
38852
38853
38854
38855
38856
38857
38858
38859
38860
38861
38862
38863
38864
38865
38866
38867
38868
38869
38870
38871
38872
38873
38874
38875
38876
38877
38878
38879
38880
38881
38882
38883
38884
38885
38886
38887
38888
38889
38890
38891
38892
38893
38894
38895
38896
38897
38898
38899
38900
38901
38902
38903
38904
38905
38906
38907
38908
38909
38910
38911
38912
38913
38914
38915
38916
38917
38918
38919
38920
38921
38922
38923
38924
38925
38926
38927
38928
38929
38930
38931
38932
38933
38934
38935
38936
38937
38938
38939
38940
38941
38942
38943
38944
38945
38946
38947
38948
38949
38950
38951
38952
38953
38954
38955
38956
38957
38958
38959
38960
38961
38962
38963
38964
38965
38966
38967
38968
38969
38970
38971
38972
38973
38974
38975
38976
38977
38978
38979
38980
38981
38982
38983
38984
38985
38986
38987
38988
38989
38990
38991
38992
38993
38994
38995
38996
38997
38998
38999
39000
39001
39002
39003
39004
39005
39006
39007
39008
39009
39010
39011
39012
39013
39014
39015
39016
39017
39018
39019
39020
39021
39022
39023
39024
39025
39026
39027
39028
39029
39030
39031
39032
39033
39034
39035
39036
39037
39038
39039
39040
39041
39042
39043
39044
39045
39046
39047
39048
39049
39050
39051
39052
39053
39054
39055
39056
39057
39058
39059
39060
39061
39062
39063
39064
39065
39066
39067
39068
39069
39070
39071
39072
39073
39074
39075
39076
39077
39078
39079
39080
39081
39082
39083
39084
39085
39086
39087
39088
39089
39090
39091
39092
39093
39094
39095
39096
39097
39098
39099
39100
39101
39102
39103
39104
39105
39106
39107
39108
39109
39110
39111
39112
39113
39114
39115
39116
39117
39118
39119
39120
39121
39122
39123
39124
39125
39126
39127
39128
39129
39130
39131
39132
39133
39134
39135
39136
39137
39138
39139
39140
39141
39142
39143
39144
39145
39146
39147
39148
39149
39150
39151
39152
39153
39154
39155
39156
39157
39158
39159
39160
39161
39162
39163
39164
39165
39166
39167
39168
39169
39170
39171
39172
39173
39174
39175
39176
39177
39178
39179
39180
39181
39182
39183
39184
39185
39186
39187
39188
39189
39190
39191
39192
39193
39194
39195
39196
39197
39198
39199
39200
39201
39202
39203
39204
39205
39206
39207
39208
39209
39210
39211
39212
39213
39214
39215
39216
39217
39218
39219
39220
39221
39222
39223
39224
39225
39226
39227
39228
39229
39230
39231
39232
39233
39234
39235
39236
39237
39238
39239
39240
39241
39242
39243
39244
39245
39246
39247
39248
39249
39250
39251
39252
39253
39254
39255
39256
39257
39258
39259
39260
39261
39262
39263
39264
39265
39266
39267
39268
39269
39270
39271
39272
39273
39274
39275
39276
39277
39278
39279
39280
39281
39282
39283
39284
39285
39286
39287
39288
39289
39290
39291
39292
39293
39294
39295
39296
39297
39298
39299
39300
39301
39302
39303
39304
39305
39306
39307
39308
39309
39310
39311
39312
39313
39314
39315
39316
39317
39318
39319
39320
39321
39322
39323
39324
39325
39326
39327
39328
39329
39330
39331
39332
39333
39334
39335
39336
39337
39338
39339
39340
39341
39342
39343
39344
39345
39346
39347
39348
39349
39350
39351
39352
39353
39354
39355
39356
39357
39358
39359
39360
39361
39362
39363
39364
39365
39366
39367
39368
39369
39370
39371
39372
39373
39374
39375
39376
39377
39378
39379
39380
39381
39382
39383
39384
39385
39386
39387
39388
39389
39390
39391
39392
39393
39394
39395
39396
39397
39398
39399
39400
39401
39402
39403
39404
39405
39406
39407
39408
39409
39410
39411
39412
39413
39414
39415
39416
39417
39418
39419
39420
39421
39422
39423
39424
39425
39426
39427
39428
39429
39430
39431
39432
39433
39434
39435
39436
39437
39438
39439
39440
39441
39442
39443
39444
39445
39446
39447
39448
39449
39450
39451
39452
39453
39454
39455
39456
39457
39458
39459
39460
39461
39462
39463
39464
39465
39466
39467
39468
39469
39470
39471
39472
39473
39474
39475
39476
39477
39478
39479
39480
39481
39482
39483
39484
39485
39486
39487
39488
39489
39490
39491
39492
39493
39494
39495
39496
39497
39498
39499
39500
39501
39502
39503
39504
39505
39506
39507
39508
39509
39510
39511
39512
39513
39514
39515
39516
39517
39518
39519
39520
39521
39522
39523
39524
39525
39526
39527
39528
39529
39530
39531
39532
39533
39534
39535
39536
39537
39538
39539
39540
39541
39542
39543
39544
39545
39546
39547
39548
39549
39550
39551
39552
39553
39554
39555
39556
39557
39558
39559
39560
39561
39562
39563
39564
39565
39566
39567
39568
39569
39570
39571
39572
39573
39574
39575
39576
39577
39578
39579
39580
39581
39582
39583
39584
39585
39586
39587
39588
39589
39590
39591
39592
39593
39594
39595
39596
39597
39598
39599
39600
39601
39602
39603
39604
39605
39606
39607
39608
39609
39610
39611
39612
39613
39614
39615
39616
39617
39618
39619
39620
39621
39622
39623
39624
39625
39626
39627
39628
39629
39630
39631
39632
39633
39634
39635
39636
39637
39638
39639
39640
39641
39642
39643
39644
39645
39646
39647
39648
39649
39650
39651
39652
39653
39654
39655
39656
39657
39658
39659
39660
39661
39662
39663
39664
39665
39666
39667
39668
39669
39670
39671
39672
39673
39674
39675
39676
39677
39678
39679
39680
39681
39682
39683
39684
39685
39686
39687
39688
39689
39690
39691
39692
39693
39694
39695
39696
39697
39698
39699
39700
39701
39702
39703
39704
39705
39706
39707
39708
39709
39710
39711
39712
39713
39714
39715
39716
39717
39718
39719
39720
39721
39722
39723
39724
39725
39726
39727
39728
39729
39730
39731
39732
39733
39734
39735
39736
39737
39738
39739
39740
39741
39742
39743
39744
39745
39746
39747
39748
39749
39750
39751
39752
39753
39754
39755
39756
39757
39758
39759
39760
39761
39762
39763
39764
39765
39766
39767
39768
39769
39770
39771
39772
39773
39774
39775
39776
39777
39778
39779
39780
39781
39782
39783
39784
39785
39786
39787
39788
39789
39790
39791
39792
39793
39794
39795
39796
39797
39798
39799
39800
39801
39802
39803
39804
39805
39806
39807
39808
39809
39810
39811
39812
39813
39814
39815
39816
39817
39818
39819
39820
39821
39822
39823
39824
39825
39826
39827
39828
39829
39830
39831
39832
39833
39834
39835
39836
39837
39838
39839
39840
39841
39842
39843
39844
39845
39846
39847
39848
39849
39850
39851
39852
39853
39854
39855
39856
39857
39858
39859
39860
39861
39862
39863
39864
39865
39866
39867
39868
39869
39870
39871
39872
39873
39874
39875
39876
39877
39878
39879
39880
39881
39882
39883
39884
39885
39886
39887
39888
39889
39890
39891
39892
39893
39894
39895
39896
39897
39898
39899
39900
39901
39902
39903
39904
39905
39906
39907
39908
39909
39910
39911
39912
39913
39914
39915
39916
39917
39918
39919
39920
39921
39922
39923
39924
39925
39926
39927
39928
39929
39930
39931
39932
39933
39934
39935
39936
39937
39938
39939
39940
39941
39942
39943
39944
39945
39946
39947
39948
39949
39950
39951
39952
39953
39954
39955
39956
39957
39958
39959
39960
39961
39962
39963
39964
39965
39966
39967
39968
39969
39970
39971
39972
39973
39974
39975
39976
39977
39978
39979
39980
39981
39982
39983
39984
39985
39986
39987
39988
39989
39990
39991
39992
39993
39994
39995
39996
39997
39998
39999
40000
40001
40002
40003
40004
40005
40006
40007
40008
40009
40010
40011
40012
40013
40014
40015
40016
40017
40018
40019
40020
40021
40022
40023
40024
40025
40026
40027
40028
40029
40030
40031
40032
40033
40034
40035
40036
40037
40038
40039
40040
40041
40042
40043
40044
40045
40046
40047
40048
40049
40050
40051
40052
40053
40054
40055
40056
40057
40058
40059
40060
40061
40062
40063
40064
40065
40066
40067
40068
40069
40070
40071
40072
40073
40074
40075
40076
40077
40078
40079
40080
40081
40082
40083
40084
40085
40086
40087
40088
40089
40090
40091
40092
40093
40094
40095
40096
40097
40098
40099
40100
40101
40102
40103
40104
40105
40106
40107
40108
40109
40110
40111
40112
40113
40114
40115
40116
40117
40118
40119
40120
40121
40122
40123
40124
40125
40126
40127
40128
40129
40130
40131
40132
40133
40134
40135
40136
40137
40138
40139
40140
40141
40142
40143
40144
40145
40146
40147
40148
40149
40150
40151
40152
40153
40154
40155
40156
40157
40158
40159
40160
40161
40162
40163
40164
40165
40166
40167
40168
40169
40170
40171
40172
40173
40174
40175
40176
40177
40178
40179
40180
40181
40182
40183
40184
40185
40186
40187
40188
40189
40190
40191
40192
40193
40194
40195
40196
40197
40198
40199
40200
40201
40202
40203
40204
40205
40206
40207
40208
40209
40210
40211
40212
40213
40214
40215
40216
40217
40218
40219
40220
40221
40222
40223
40224
40225
40226
40227
40228
40229
40230
40231
40232
40233
40234
40235
40236
40237
40238
40239
40240
40241
40242
40243
40244
40245
40246
40247
40248
40249
40250
40251
40252
40253
40254
40255
40256
40257
40258
40259
40260
40261
40262
40263
40264
40265
40266
40267
40268
40269
40270
40271
40272
40273
40274
40275
40276
40277
40278
40279
40280
40281
40282
40283
40284
40285
40286
40287
40288
40289
40290
40291
40292
40293
40294
40295
40296
40297
40298
40299
40300
40301
40302
40303
40304
40305
40306
40307
40308
40309
40310
40311
40312
40313
40314
40315
40316
40317
40318
40319
40320
40321
40322
40323
40324
40325
40326
40327
40328
40329
40330
40331
40332
40333
40334
40335
40336
40337
40338
40339
40340
40341
40342
40343
40344
40345
40346
40347
40348
40349
40350
40351
40352
40353
40354
40355
40356
40357
40358
40359
40360
40361
40362
40363
40364
40365
40366
40367
40368
40369
40370
40371
40372
40373
40374
40375
40376
40377
40378
40379
40380
40381
40382
40383
40384
40385
40386
40387
40388
40389
40390
40391
40392
40393
40394
40395
40396
40397
40398
40399
40400
40401
40402
40403
40404
40405
40406
40407
40408
40409
40410
40411
40412
40413
40414
40415
40416
40417
40418
40419
40420
40421
40422
40423
40424
40425
40426
40427
40428
40429
40430
40431
40432
40433
40434
40435
40436
40437
40438
40439
40440
40441
40442
40443
40444
40445
40446
40447
40448
40449
40450
40451
40452
40453
40454
40455
40456
40457
40458
40459
40460
40461
40462
40463
40464
40465
40466
40467
40468
40469
40470
40471
40472
40473
40474
40475
40476
40477
40478
40479
40480
40481
40482
40483
40484
40485
40486
40487
40488
40489
40490
40491
40492
40493
40494
40495
40496
40497
40498
40499
40500
40501
40502
40503
40504
40505
40506
40507
40508
40509
40510
40511
40512
40513
40514
40515
40516
40517
40518
40519
40520
40521
40522
40523
40524
40525
40526
40527
40528
40529
40530
40531
40532
40533
40534
40535
40536
40537
40538
40539
40540
40541
40542
40543
40544
40545
40546
40547
40548
40549
40550
40551
40552
40553
40554
40555
40556
40557
40558
40559
40560
40561
40562
40563
40564
40565
40566
40567
40568
40569
40570
40571
40572
40573
40574
40575
40576
40577
40578
40579
40580
40581
40582
40583
40584
40585
40586
40587
40588
40589
40590
40591
40592
40593
40594
40595
40596
40597
40598
40599
40600
40601
40602
40603
40604
40605
40606
40607
40608
40609
40610
40611
40612
40613
40614
40615
40616
40617
40618
40619
40620
40621
40622
40623
40624
40625
40626
40627
40628
40629
40630
40631
40632
40633
40634
40635
40636
40637
40638
40639
40640
40641
40642
40643
40644
40645
40646
40647
40648
40649
40650
40651
40652
40653
40654
40655
40656
40657
40658
40659
40660
40661
40662
40663
40664
40665
40666
40667
40668
40669
40670
40671
40672
40673
40674
40675
40676
40677
40678
40679
40680
40681
40682
40683
40684
40685
40686
40687
40688
40689
40690
40691
40692
40693
40694
40695
40696
40697
40698
40699
40700
40701
40702
40703
40704
40705
40706
40707
40708
40709
40710
40711
40712
40713
40714
40715
40716
40717
40718
40719
40720
40721
40722
40723
40724
40725
40726
40727
40728
40729
40730
40731
40732
40733
40734
40735
40736
40737
40738
40739
40740
40741
40742
40743
40744
40745
40746
40747
40748
40749
40750
40751
40752
40753
40754
40755
40756
40757
40758
40759
40760
40761
40762
40763
40764
40765
40766
40767
40768
40769
40770
40771
40772
40773
40774
40775
40776
40777
40778
40779
40780
40781
40782
40783
40784
40785
40786
40787
40788
40789
40790
40791
40792
40793
40794
40795
40796
40797
40798
40799
40800
40801
40802
40803
40804
40805
40806
40807
40808
40809
40810
40811
40812
40813
40814
40815
40816
40817
40818
40819
40820
40821
40822
40823
40824
40825
40826
40827
40828
40829
40830
40831
40832
40833
40834
40835
40836
40837
40838
40839
40840
40841
40842
40843
40844
40845
40846
40847
40848
40849
40850
40851
40852
40853
40854
40855
40856
40857
40858
40859
40860
40861
40862
40863
40864
40865
40866
40867
40868
40869
40870
40871
40872
40873
40874
40875
40876
40877
40878
40879
40880
40881
40882
40883
40884
40885
40886
40887
40888
40889
40890
40891
40892
40893
40894
40895
40896
40897
40898
40899
40900
40901
40902
40903
40904
40905
40906
40907
40908
40909
40910
40911
40912
40913
40914
40915
40916
40917
40918
40919
40920
40921
40922
40923
40924
40925
40926
40927
40928
40929
40930
40931
40932
40933
40934
40935
40936
40937
40938
40939
40940
40941
40942
40943
40944
40945
40946
40947
40948
40949
40950
40951
40952
40953
40954
40955
40956
40957
40958
40959
40960
40961
40962
40963
40964
40965
40966
40967
40968
40969
40970
40971
40972
40973
40974
40975
40976
40977
40978
40979
40980
40981
40982
40983
40984
40985
40986
40987
40988
40989
40990
40991
40992
40993
40994
40995
40996
40997
40998
40999
41000
41001
41002
41003
41004
41005
41006
41007
41008
41009
41010
41011
41012
41013
41014
41015
41016
41017
41018
41019
41020
41021
41022
41023
41024
41025
41026
41027
41028
41029
41030
41031
41032
41033
41034
41035
41036
41037
41038
41039
41040
41041
41042
41043
41044
41045
41046
41047
41048
41049
41050
41051
41052
41053
41054
41055
41056
41057
41058
41059
41060
41061
41062
41063
41064
41065
41066
41067
41068
41069
41070
41071
41072
41073
41074
41075
41076
41077
41078
41079
41080
41081
41082
41083
41084
41085
41086
41087
41088
41089
41090
41091
41092
41093
41094
41095
41096
41097
41098
41099
41100
41101
41102
41103
41104
41105
41106
41107
41108
41109
41110
41111
41112
41113
41114
41115
41116
41117
41118
41119
41120
41121
41122
41123
41124
41125
41126
41127
41128
41129
41130
41131
41132
41133
41134
41135
41136
41137
41138
41139
41140
41141
41142
41143
41144
41145
41146
41147
41148
41149
41150
41151
41152
41153
41154
41155
41156
41157
41158
41159
41160
41161
41162
41163
41164
41165
41166
41167
41168
41169
41170
41171
41172
41173
41174
41175
41176
41177
41178
41179
41180
41181
41182
41183
41184
41185
41186
41187
41188
41189
41190
41191
41192
41193
41194
41195
41196
41197
41198
41199
41200
41201
41202
41203
41204
41205
41206
41207
41208
41209
41210
41211
41212
41213
41214
41215
41216
41217
41218
41219
41220
41221
41222
41223
41224
41225
41226
41227
41228
41229
41230
41231
41232
41233
41234
41235
41236
41237
41238
41239
41240
41241
41242
41243
41244
41245
41246
41247
41248
41249
41250
41251
41252
41253
41254
41255
41256
41257
41258
41259
41260
41261
41262
41263
41264
41265
41266
41267
41268
41269
41270
41271
41272
41273
41274
41275
41276
41277
41278
41279
41280
41281
41282
41283
41284
41285
41286
41287
41288
41289
41290
41291
41292
41293
41294
41295
41296
41297
41298
41299
41300
41301
41302
41303
41304
41305
41306
41307
41308
41309
41310
41311
41312
41313
41314
41315
41316
41317
41318
41319
41320
41321
41322
41323
41324
41325
41326
41327
41328
41329
41330
41331
41332
41333
41334
41335
41336
41337
41338
41339
41340
41341
41342
41343
41344
41345
41346
41347
41348
41349
41350
41351
41352
41353
41354
41355
41356
41357
41358
41359
41360
41361
41362
41363
41364
41365
41366
41367
41368
41369
41370
41371
41372
41373
41374
41375
41376
41377
41378
41379
41380
41381
41382
41383
41384
41385
41386
41387
41388
41389
41390
41391
41392
41393
41394
41395
41396
41397
41398
41399
41400
41401
41402
41403
41404
41405
41406
41407
41408
41409
41410
41411
41412
41413
41414
41415
41416
41417
41418
41419
41420
41421
41422
41423
41424
41425
41426
41427
41428
41429
41430
41431
41432
41433
41434
41435
41436
41437
41438
41439
41440
41441
41442
41443
41444
41445
41446
41447
41448
41449
41450
41451
41452
41453
41454
41455
41456
41457
41458
41459
41460
41461
41462
41463
41464
41465
41466
41467
41468
41469
41470
41471
41472
41473
41474
41475
41476
41477
41478
41479
41480
41481
41482
41483
41484
41485
41486
41487
41488
41489
41490
41491
41492
41493
41494
41495
41496
41497
41498
41499
41500
41501
41502
41503
41504
41505
41506
41507
41508
41509
41510
41511
41512
41513
41514
41515
41516
41517
41518
41519
41520
41521
41522
41523
41524
41525
41526
41527
41528
41529
41530
41531
41532
41533
41534
41535
41536
41537
41538
41539
41540
41541
41542
41543
41544
41545
41546
41547
41548
41549
41550
41551
41552
41553
41554
41555
41556
41557
41558
41559
41560
41561
41562
41563
41564
41565
41566
41567
41568
41569
41570
41571
41572
41573
41574
41575
41576
41577
41578
41579
41580
41581
41582
41583
41584
41585
41586
41587
41588
41589
41590
41591
41592
41593
41594
41595
41596
41597
41598
41599
41600
41601
41602
41603
41604
41605
41606
41607
41608
41609
41610
41611
41612
41613
41614
41615
41616
41617
41618
41619
41620
41621
41622
41623
41624
41625
41626
41627
41628
41629
41630
41631
41632
41633
41634
41635
41636
41637
41638
41639
41640
41641
41642
41643
41644
41645
41646
41647
41648
41649
41650
41651
41652
41653
41654
41655
41656
41657
41658
41659
41660
41661
41662
41663
41664
41665
41666
41667
41668
41669
41670
41671
41672
41673
41674
41675
41676
41677
41678
41679
41680
41681
41682
41683
41684
41685
41686
41687
41688
41689
41690
41691
41692
41693
41694
41695
41696
41697
41698
41699
41700
41701
41702
41703
41704
41705
41706
41707
41708
41709
41710
41711
41712
41713
41714
41715
41716
41717
41718
41719
41720
41721
41722
41723
41724
41725
41726
41727
41728
41729
41730
41731
41732
41733
41734
41735
41736
41737
41738
41739
41740
41741
41742
41743
41744
41745
41746
41747
41748
41749
41750
41751
41752
41753
41754
41755
41756
41757
41758
41759
41760
41761
41762
41763
41764
41765
41766
41767
41768
41769
41770
41771
41772
41773
41774
41775
41776
41777
41778
41779
41780
41781
41782
41783
41784
41785
41786
41787
41788
41789
41790
41791
41792
41793
41794
41795
41796
41797
41798
41799
41800
41801
41802
41803
41804
41805
41806
41807
41808
41809
41810
41811
41812
41813
41814
41815
41816
41817
41818
41819
41820
41821
41822
41823
41824
41825
41826
41827
41828
41829
41830
41831
41832
41833
41834
41835
41836
41837
41838
41839
41840
41841
41842
41843
41844
41845
41846
41847
41848
41849
41850
41851
41852
41853
41854
41855
41856
41857
41858
41859
41860
41861
41862
41863
41864
41865
41866
41867
41868
41869
41870
41871
41872
41873
41874
41875
41876
41877
41878
41879
41880
41881
41882
41883
41884
41885
41886
41887
41888
41889
41890
41891
41892
41893
41894
41895
41896
41897
41898
41899
41900
41901
41902
41903
41904
41905
41906
41907
41908
41909
41910
41911
41912
41913
41914
41915
41916
41917
41918
41919
41920
41921
41922
41923
41924
41925
41926
41927
41928
41929
41930
41931
41932
41933
41934
41935
41936
41937
41938
41939
41940
41941
41942
41943
41944
41945
41946
41947
41948
41949
41950
41951
41952
41953
41954
41955
41956
41957
41958
41959
41960
41961
41962
41963
41964
41965
41966
41967
41968
41969
41970
41971
41972
41973
41974
41975
41976
41977
41978
41979
41980
41981
41982
41983
41984
41985
41986
41987
41988
41989
41990
41991
41992
41993
41994
41995
41996
41997
41998
41999
42000
42001
42002
42003
42004
42005
42006
42007
42008
42009
42010
42011
42012
42013
42014
42015
42016
42017
42018
42019
42020
42021
42022
42023
42024
42025
42026
42027
42028
42029
42030
42031
42032
42033
42034
42035
42036
42037
42038
42039
42040
42041
42042
42043
42044
42045
42046
42047
42048
42049
42050
42051
42052
42053
42054
42055
42056
42057
42058
42059
42060
42061
42062
42063
42064
42065
42066
42067
42068
42069
42070
42071
42072
42073
42074
42075
42076
42077
42078
42079
42080
42081
42082
42083
42084
42085
42086
42087
42088
42089
42090
42091
42092
42093
42094
42095
42096
42097
42098
42099
42100
42101
42102
42103
42104
42105
42106
42107
42108
42109
42110
42111
42112
42113
42114
42115
42116
42117
42118
42119
42120
42121
42122
42123
42124
42125
42126
42127
42128
42129
42130
42131
42132
42133
42134
42135
42136
42137
42138
42139
42140
42141
42142
42143
42144
42145
42146
42147
42148
42149
42150
42151
42152
42153
42154
42155
42156
42157
42158
42159
42160
42161
42162
42163
42164
42165
42166
42167
42168
42169
42170
42171
42172
42173
42174
42175
42176
42177
42178
42179
42180
42181
42182
42183
42184
42185
42186
42187
42188
42189
42190
42191
42192
42193
42194
42195
42196
42197
42198
42199
42200
42201
42202
42203
42204
42205
42206
42207
42208
42209
42210
42211
42212
42213
42214
42215
42216
42217
42218
42219
42220
42221
42222
42223
42224
42225
42226
42227
42228
42229
42230
42231
42232
42233
42234
42235
42236
42237
42238
42239
42240
42241
42242
42243
42244
42245
42246
42247
42248
42249
42250
42251
42252
42253
42254
42255
42256
42257
42258
42259
42260
42261
42262
42263
42264
42265
42266
42267
42268
42269
42270
42271
42272
42273
42274
42275
42276
42277
42278
42279
42280
42281
42282
42283
42284
42285
42286
42287
42288
42289
42290
42291
42292
42293
42294
42295
42296
42297
42298
42299
42300
42301
42302
42303
42304
42305
42306
42307
42308
42309
42310
42311
42312
42313
42314
42315
42316
42317
42318
42319
42320
42321
42322
42323
42324
42325
42326
42327
42328
42329
42330
42331
42332
42333
42334
42335
42336
42337
42338
42339
42340
42341
42342
42343
42344
42345
42346
42347
42348
42349
42350
42351
42352
42353
42354
42355
42356
42357
42358
42359
42360
42361
42362
42363
42364
42365
42366
42367
42368
42369
42370
42371
42372
42373
42374
42375
42376
42377
42378
42379
42380
42381
42382
42383
42384
42385
42386
42387
42388
42389
42390
42391
42392
42393
42394
42395
42396
42397
42398
42399
42400
42401
42402
42403
42404
42405
42406
42407
42408
42409
42410
42411
42412
42413
42414
42415
42416
42417
42418
42419
42420
42421
42422
42423
42424
42425
42426
42427
42428
42429
42430
42431
42432
42433
42434
42435
42436
42437
42438
42439
42440
42441
42442
42443
42444
42445
42446
42447
42448
42449
42450
42451
42452
42453
42454
42455
42456
42457
42458
42459
42460
42461
42462
42463
42464
42465
42466
42467
42468
42469
42470
42471
42472
42473
42474
42475
42476
42477
42478
42479
42480
42481
42482
42483
42484
42485
42486
42487
42488
42489
42490
42491
42492
42493
42494
42495
42496
42497
42498
42499
42500
42501
42502
42503
42504
42505
42506
42507
42508
42509
42510
42511
42512
42513
42514
42515
42516
42517
42518
42519
42520
42521
42522
42523
42524
42525
42526
42527
42528
42529
42530
42531
42532
42533
42534
42535
42536
42537
42538
42539
42540
42541
42542
42543
42544
42545
42546
42547
42548
42549
42550
42551
42552
42553
42554
42555
42556
42557
42558
42559
42560
42561
42562
42563
42564
42565
42566
42567
42568
42569
42570
42571
42572
42573
42574
42575
42576
42577
42578
42579
42580
42581
42582
42583
42584
42585
42586
42587
42588
42589
42590
42591
42592
42593
42594
42595
42596
42597
42598
42599
42600
42601
42602
42603
42604
42605
42606
42607
42608
42609
42610
42611
42612
42613
42614
42615
42616
42617
42618
42619
42620
42621
42622
42623
42624
42625
42626
42627
42628
42629
42630
42631
42632
42633
42634
42635
42636
42637
42638
42639
42640
42641
42642
42643
42644
42645
42646
42647
42648
42649
42650
42651
42652
42653
42654
42655
42656
42657
42658
42659
42660
42661
42662
42663
42664
42665
42666
42667
42668
42669
42670
42671
42672
42673
42674
42675
42676
42677
42678
42679
42680
42681
42682
42683
42684
42685
42686
42687
42688
42689
42690
42691
42692
42693
42694
42695
42696
42697
42698
42699
42700
42701
42702
42703
42704
42705
42706
42707
42708
42709
42710
42711
42712
42713
42714
42715
42716
42717
42718
42719
42720
42721
42722
42723
42724
42725
42726
42727
42728
42729
42730
42731
42732
42733
42734
42735
42736
42737
42738
42739
42740
42741
42742
42743
42744
42745
42746
42747
42748
42749
42750
42751
42752
42753
42754
42755
42756
42757
42758
42759
42760
42761
42762
42763
42764
42765
42766
42767
42768
42769
42770
42771
42772
42773
42774
42775
42776
42777
42778
42779
42780
42781
42782
42783
42784
42785
42786
42787
42788
42789
42790
42791
42792
42793
42794
42795
42796
42797
42798
42799
42800
42801
42802
42803
42804
42805
42806
42807
42808
42809
42810
42811
42812
42813
42814
42815
42816
42817
42818
42819
42820
42821
42822
42823
42824
42825
42826
42827
42828
42829
42830
42831
42832
42833
42834
42835
42836
42837
42838
42839
42840
42841
42842
42843
42844
42845
42846
42847
42848
42849
42850
42851
42852
42853
42854
42855
42856
42857
42858
42859
42860
42861
42862
42863
42864
42865
42866
42867
42868
42869
42870
42871
42872
42873
42874
42875
42876
42877
42878
42879
42880
42881
42882
42883
42884
42885
42886
42887
42888
42889
42890
42891
42892
42893
42894
42895
42896
42897
42898
42899
42900
42901
42902
42903
42904
42905
42906
42907
42908
42909
42910
42911
42912
42913
42914
42915
42916
42917
42918
42919
42920
42921
42922
42923
42924
42925
42926
42927
42928
42929
42930
42931
42932
42933
42934
42935
42936
42937
42938
42939
42940
42941
42942
42943
42944
42945
42946
42947
42948
42949
42950
42951
42952
42953
42954
42955
42956
42957
42958
42959
42960
42961
42962
42963
42964
42965
42966
42967
42968
42969
42970
42971
42972
42973
42974
42975
42976
42977
42978
42979
42980
42981
42982
42983
42984
42985
42986
42987
42988
42989
42990
42991
42992
42993
42994
42995
42996
42997
42998
42999
43000
43001
43002
43003
43004
43005
43006
43007
43008
43009
43010
43011
43012
43013
43014
43015
43016
43017
43018
43019
43020
43021
43022
43023
43024
43025
43026
43027
43028
43029
43030
43031
43032
43033
43034
43035
43036
43037
43038
43039
43040
43041
43042
43043
43044
43045
43046
43047
43048
43049
43050
43051
43052
43053
43054
43055
43056
43057
43058
43059
43060
43061
43062
43063
43064
43065
43066
43067
43068
43069
43070
43071
43072
43073
43074
43075
43076
43077
43078
43079
43080
43081
43082
43083
43084
43085
43086
43087
43088
43089
43090
43091
43092
43093
43094
43095
43096
43097
43098
43099
43100
43101
43102
43103
43104
43105
43106
43107
43108
43109
43110
43111
43112
43113
43114
43115
43116
43117
43118
43119
43120
43121
43122
43123
43124
43125
43126
43127
43128
43129
43130
43131
43132
43133
43134
43135
43136
43137
43138
43139
43140
43141
43142
43143
43144
43145
43146
43147
43148
43149
43150
43151
43152
43153
43154
43155
43156
43157
43158
43159
43160
43161
43162
43163
43164
43165
43166
43167
43168
43169
43170
43171
43172
43173
43174
43175
43176
43177
43178
43179
43180
43181
43182
43183
43184
43185
43186
43187
43188
43189
43190
43191
43192
43193
43194
43195
43196
43197
43198
43199
43200
43201
43202
43203
43204
43205
43206
43207
43208
43209
43210
43211
43212
43213
43214
43215
43216
43217
43218
43219
43220
43221
43222
43223
43224
43225
43226
43227
43228
43229
43230
43231
43232
43233
43234
43235
43236
43237
43238
43239
43240
43241
43242
43243
43244
43245
43246
43247
43248
43249
43250
43251
43252
43253
43254
43255
43256
43257
43258
43259
43260
43261
43262
43263
43264
43265
43266
43267
43268
43269
43270
43271
43272
43273
43274
43275
43276
43277
43278
43279
43280
43281
43282
43283
43284
43285
43286
43287
43288
43289
43290
43291
43292
43293
43294
43295
43296
43297
43298
43299
43300
43301
43302
43303
43304
43305
43306
43307
43308
43309
43310
43311
43312
43313
43314
43315
43316
43317
43318
43319
43320
43321
43322
43323
43324
43325
43326
43327
43328
43329
43330
43331
43332
43333
43334
43335
43336
43337
43338
43339
43340
43341
43342
43343
43344
43345
43346
43347
43348
43349
43350
43351
43352
43353
43354
43355
43356
43357
43358
43359
43360
43361
43362
43363
43364
43365
43366
43367
43368
43369
43370
43371
43372
43373
43374
43375
43376
43377
43378
43379
43380
43381
43382
43383
43384
43385
43386
43387
43388
43389
43390
43391
43392
43393
43394
43395
43396
43397
43398
43399
43400
43401
43402
43403
43404
43405
43406
43407
43408
43409
43410
43411
43412
43413
43414
43415
43416
43417
43418
43419
43420
43421
43422
43423
43424
43425
43426
43427
43428
43429
43430
43431
43432
43433
43434
43435
43436
43437
43438
43439
43440
43441
43442
43443
43444
43445
43446
43447
43448
43449
43450
43451
43452
43453
43454
43455
43456
43457
43458
43459
43460
43461
43462
43463
43464
43465
43466
43467
43468
43469
43470
43471
43472
43473
43474
43475
43476
43477
43478
43479
43480
43481
43482
43483
43484
43485
43486
43487
43488
43489
43490
43491
43492
43493
43494
43495
43496
43497
43498
43499
43500
43501
43502
43503
43504
43505
43506
43507
43508
43509
43510
43511
43512
43513
43514
43515
43516
43517
43518
43519
43520
43521
43522
43523
43524
43525
43526
43527
43528
43529
43530
43531
43532
43533
43534
43535
43536
43537
43538
43539
43540
43541
43542
43543
43544
43545
43546
43547
43548
43549
43550
43551
43552
43553
43554
43555
43556
43557
43558
43559
43560
43561
43562
43563
43564
43565
43566
43567
43568
43569
43570
43571
43572
43573
43574
43575
43576
43577
43578
43579
43580
43581
43582
43583
43584
43585
43586
43587
43588
43589
43590
43591
43592
43593
43594
43595
43596
43597
43598
43599
43600
43601
43602
43603
43604
43605
43606
43607
43608
43609
43610
43611
43612
43613
43614
43615
43616
43617
43618
43619
43620
43621
43622
43623
43624
43625
43626
43627
43628
43629
43630
43631
43632
43633
43634
43635
43636
43637
43638
43639
43640
43641
43642
43643
43644
43645
43646
43647
43648
43649
43650
43651
43652
43653
43654
43655
43656
43657
43658
43659
43660
43661
43662
43663
43664
43665
43666
43667
43668
43669
43670
43671
43672
43673
43674
43675
43676
43677
43678
43679
43680
43681
43682
43683
43684
43685
43686
43687
43688
43689
43690
43691
43692
43693
43694
43695
43696
43697
43698
43699
43700
43701
43702
43703
43704
43705
43706
43707
43708
43709
43710
43711
43712
43713
43714
43715
43716
43717
43718
43719
43720
43721
43722
43723
43724
43725
43726
43727
43728
43729
43730
43731
43732
43733
43734
43735
43736
43737
43738
43739
43740
43741
43742
43743
43744
43745
43746
43747
43748
43749
43750
43751
43752
43753
43754
43755
43756
43757
43758
43759
43760
43761
43762
43763
43764
43765
43766
43767
43768
43769
43770
43771
43772
43773
43774
43775
43776
43777
43778
43779
43780
43781
43782
43783
43784
43785
43786
43787
43788
43789
43790
43791
43792
43793
43794
43795
43796
43797
43798
43799
43800
43801
43802
43803
43804
43805
43806
43807
43808
43809
43810
43811
43812
43813
43814
43815
43816
43817
43818
43819
43820
43821
43822
43823
43824
43825
43826
43827
43828
43829
43830
43831
43832
43833
43834
43835
43836
43837
43838
43839
43840
43841
43842
43843
43844
43845
43846
43847
43848
43849
43850
43851
43852
43853
43854
43855
43856
43857
43858
43859
43860
43861
43862
43863
43864
43865
43866
43867
43868
43869
43870
43871
43872
43873
43874
43875
43876
43877
43878
43879
43880
43881
43882
43883
43884
43885
43886
43887
43888
43889
43890
43891
43892
43893
43894
43895
43896
43897
43898
43899
43900
43901
43902
43903
43904
43905
43906
43907
43908
43909
43910
43911
43912
43913
43914
43915
43916
43917
43918
43919
43920
43921
43922
43923
43924
43925
43926
43927
43928
43929
43930
43931
43932
43933
43934
43935
43936
43937
43938
43939
43940
43941
43942
43943
43944
43945
43946
43947
43948
43949
43950
43951
43952
43953
43954
43955
43956
43957
43958
43959
43960
43961
43962
43963
43964
43965
43966
43967
43968
43969
43970
43971
43972
43973
43974
43975
43976
43977
43978
43979
43980
43981
43982
43983
43984
43985
43986
43987
43988
43989
43990
43991
43992
43993
43994
43995
43996
43997
43998
43999
44000
44001
44002
44003
44004
44005
44006
44007
44008
44009
44010
44011
44012
44013
44014
44015
44016
44017
44018
44019
44020
44021
44022
44023
44024
44025
44026
44027
44028
44029
44030
44031
44032
44033
44034
44035
44036
44037
44038
44039
44040
44041
44042
44043
44044
44045
44046
44047
44048
44049
44050
44051
44052
44053
44054
44055
44056
44057
44058
44059
44060
44061
44062
44063
44064
44065
44066
44067
44068
44069
44070
44071
44072
44073
44074
44075
44076
44077
44078
44079
44080
44081
44082
44083
44084
44085
44086
44087
44088
44089
44090
44091
44092
44093
44094
44095
44096
44097
44098
44099
44100
44101
44102
44103
44104
44105
44106
44107
44108
44109
44110
44111
44112
44113
44114
44115
44116
44117
44118
44119
44120
44121
44122
44123
44124
44125
44126
44127
44128
44129
44130
44131
44132
44133
44134
44135
44136
44137
44138
44139
44140
44141
44142
44143
44144
44145
44146
44147
44148
44149
44150
44151
44152
44153
44154
44155
44156
44157
44158
44159
44160
44161
44162
44163
44164
44165
44166
44167
44168
44169
44170
44171
44172
44173
44174
44175
44176
44177
44178
44179
44180
44181
44182
44183
44184
44185
44186
44187
44188
44189
44190
44191
44192
44193
44194
44195
44196
44197
44198
44199
44200
44201
44202
44203
44204
44205
44206
44207
44208
44209
44210
44211
44212
44213
44214
44215
44216
44217
44218
44219
44220
44221
44222
44223
44224
44225
44226
44227
44228
44229
44230
44231
44232
44233
44234
44235
44236
44237
44238
44239
44240
44241
44242
44243
44244
44245
44246
44247
44248
44249
44250
44251
44252
44253
44254
44255
44256
44257
44258
44259
44260
44261
44262
44263
44264
44265
44266
44267
44268
44269
44270
44271
44272
44273
44274
44275
44276
44277
44278
44279
44280
44281
44282
44283
44284
44285
44286
44287
44288
44289
44290
44291
44292
44293
44294
44295
44296
44297
44298
44299
44300
44301
44302
44303
44304
44305
44306
44307
44308
44309
44310
44311
44312
44313
44314
44315
44316
44317
44318
44319
44320
44321
44322
44323
44324
44325
44326
44327
44328
44329
44330
44331
44332
44333
44334
44335
44336
44337
44338
44339
44340
44341
44342
44343
44344
44345
44346
44347
44348
44349
44350
44351
44352
44353
44354
44355
44356
44357
44358
44359
44360
44361
44362
44363
44364
44365
44366
44367
44368
44369
44370
44371
44372
44373
44374
44375
44376
44377
44378
44379
44380
44381
44382
44383
44384
44385
44386
44387
44388
44389
44390
44391
44392
44393
44394
44395
44396
44397
44398
44399
44400
44401
44402
44403
44404
44405
44406
44407
44408
44409
44410
44411
44412
44413
44414
44415
44416
44417
44418
44419
44420
44421
44422
44423
44424
44425
44426
44427
44428
44429
44430
44431
44432
44433
44434
44435
44436
44437
44438
44439
44440
44441
44442
44443
44444
44445
44446
44447
44448
44449
44450
44451
44452
44453
44454
44455
44456
44457
44458
44459
44460
44461
44462
44463
44464
44465
44466
44467
44468
44469
44470
44471
44472
44473
44474
44475
44476
44477
44478
44479
44480
44481
44482
44483
44484
44485
44486
44487
44488
44489
44490
44491
44492
44493
44494
44495
44496
44497
44498
44499
44500
44501
44502
44503
44504
44505
44506
44507
44508
44509
44510
44511
44512
44513
44514
44515
44516
44517
44518
44519
44520
44521
44522
44523
44524
44525
44526
44527
44528
44529
44530
44531
44532
44533
44534
44535
44536
44537
44538
44539
44540
44541
44542
44543
44544
44545
44546
44547
44548
44549
44550
44551
44552
44553
44554
44555
44556
44557
44558
44559
44560
44561
44562
44563
44564
44565
44566
44567
44568
44569
44570
44571
44572
44573
44574
44575
44576
44577
44578
44579
44580
44581
44582
44583
44584
44585
44586
44587
44588
44589
44590
44591
44592
44593
44594
44595
44596
44597
44598
44599
44600
44601
44602
44603
44604
44605
44606
44607
44608
44609
44610
44611
44612
44613
44614
44615
44616
44617
44618
44619
44620
44621
44622
44623
44624
44625
44626
44627
44628
44629
44630
44631
44632
44633
44634
44635
44636
44637
44638
44639
44640
44641
44642
44643
44644
44645
44646
44647
44648
44649
44650
44651
44652
44653
44654
44655
44656
44657
44658
44659
44660
44661
44662
44663
44664
44665
44666
44667
44668
44669
44670
44671
44672
44673
44674
44675
44676
44677
44678
44679
44680
44681
44682
44683
44684
44685
44686
44687
44688
44689
44690
44691
44692
44693
44694
44695
44696
44697
44698
44699
44700
44701
44702
44703
44704
44705
44706
44707
44708
44709
44710
44711
44712
44713
44714
44715
44716
44717
44718
44719
44720
44721
44722
44723
44724
44725
44726
44727
44728
44729
44730
44731
44732
44733
44734
44735
44736
44737
44738
44739
44740
44741
44742
44743
44744
44745
44746
44747
44748
44749
44750
44751
44752
44753
44754
44755
44756
44757
44758
44759
44760
44761
44762
44763
44764
44765
44766
44767
44768
44769
44770
44771
44772
44773
44774
44775
44776
44777
44778
44779
44780
44781
44782
44783
44784
44785
44786
44787
44788
44789
44790
44791
44792
44793
44794
44795
44796
44797
44798
44799
44800
44801
44802
44803
44804
44805
44806
44807
44808
44809
44810
44811
44812
44813
44814
44815
44816
44817
44818
44819
44820
44821
44822
44823
44824
44825
44826
44827
44828
44829
44830
44831
44832
44833
44834
44835
44836
44837
44838
44839
44840
44841
44842
44843
44844
44845
44846
44847
44848
44849
44850
44851
44852
44853
44854
44855
44856
44857
44858
44859
44860
44861
44862
44863
44864
44865
44866
44867
44868
44869
44870
44871
44872
44873
44874
44875
44876
44877
44878
44879
44880
44881
44882
44883
44884
44885
44886
44887
44888
44889
44890
44891
44892
44893
44894
44895
44896
44897
44898
44899
44900
44901
44902
44903
44904
44905
44906
44907
44908
44909
44910
44911
44912
44913
44914
44915
44916
44917
44918
44919
44920
44921
44922
44923
44924
44925
44926
44927
44928
44929
44930
44931
44932
44933
44934
44935
44936
44937
44938
44939
44940
44941
44942
44943
44944
44945
44946
44947
44948
44949
44950
44951
44952
44953
44954
44955
44956
44957
44958
44959
44960
44961
44962
44963
44964
44965
44966
44967
44968
44969
44970
44971
44972
44973
44974
44975
44976
44977
44978
44979
44980
44981
44982
44983
44984
44985
44986
44987
44988
44989
44990
44991
44992
44993
44994
44995
44996
44997
44998
44999
45000
45001
45002
45003
45004
45005
45006
45007
45008
45009
45010
45011
45012
45013
45014
45015
45016
45017
45018
45019
45020
45021
45022
45023
45024
45025
45026
45027
45028
45029
45030
45031
45032
45033
45034
45035
45036
45037
45038
45039
45040
45041
45042
45043
45044
45045
45046
45047
45048
45049
45050
45051
45052
45053
45054
45055
45056
45057
45058
45059
45060
45061
45062
45063
45064
45065
45066
45067
45068
45069
45070
45071
45072
45073
45074
45075
45076
45077
45078
45079
45080
45081
45082
45083
45084
45085
45086
45087
45088
45089
45090
45091
45092
45093
45094
45095
45096
45097
45098
45099
45100
45101
45102
45103
45104
45105
45106
45107
45108
45109
45110
45111
45112
45113
45114
45115
45116
45117
45118
45119
45120
45121
45122
45123
45124
45125
45126
45127
45128
45129
45130
45131
45132
45133
45134
45135
45136
45137
45138
45139
45140
45141
45142
45143
45144
45145
45146
45147
45148
45149
45150
45151
45152
45153
45154
45155
45156
45157
45158
45159
45160
45161
45162
45163
45164
45165
45166
45167
45168
45169
45170
45171
45172
45173
45174
45175
45176
45177
45178
45179
45180
45181
45182
45183
45184
45185
45186
45187
45188
45189
45190
45191
45192
45193
45194
45195
45196
45197
45198
45199
45200
45201
45202
45203
45204
45205
45206
45207
45208
45209
45210
45211
45212
45213
45214
45215
45216
45217
45218
45219
45220
45221
45222
45223
45224
45225
45226
45227
45228
45229
45230
45231
45232
45233
45234
45235
45236
45237
45238
45239
45240
45241
45242
45243
45244
45245
45246
45247
45248
45249
45250
45251
45252
45253
45254
45255
45256
45257
45258
45259
45260
45261
45262
45263
45264
45265
45266
45267
45268
45269
45270
45271
45272
45273
45274
45275
45276
45277
45278
45279
45280
45281
45282
45283
45284
45285
45286
45287
45288
45289
45290
45291
45292
45293
45294
45295
45296
45297
45298
45299
45300
45301
45302
45303
45304
45305
45306
45307
45308
45309
45310
45311
45312
45313
45314
45315
45316
45317
45318
45319
45320
45321
45322
45323
45324
45325
45326
45327
45328
45329
45330
45331
45332
45333
45334
45335
45336
45337
45338
45339
45340
45341
45342
45343
45344
45345
45346
45347
45348
45349
45350
45351
45352
45353
45354
45355
45356
45357
45358
45359
45360
45361
45362
45363
45364
45365
45366
45367
45368
45369
45370
45371
45372
45373
45374
45375
45376
45377
45378
45379
45380
45381
45382
45383
45384
45385
45386
45387
45388
45389
45390
45391
45392
45393
45394
45395
45396
45397
45398
45399
45400
45401
45402
45403
45404
45405
45406
45407
45408
45409
45410
45411
45412
45413
45414
45415
45416
45417
45418
45419
45420
45421
45422
45423
45424
45425
45426
45427
45428
45429
45430
45431
45432
45433
45434
45435
45436
45437
45438
45439
45440
45441
45442
45443
45444
45445
45446
45447
45448
45449
45450
45451
45452
45453
45454
45455
45456
45457
45458
45459
45460
45461
45462
45463
45464
45465
45466
45467
45468
45469
45470
45471
45472
45473
45474
45475
45476
45477
45478
45479
45480
45481
45482
45483
45484
45485
45486
45487
45488
45489
45490
45491
45492
45493
45494
45495
45496
45497
45498
45499
45500
45501
45502
45503
45504
45505
45506
45507
45508
45509
45510
45511
45512
45513
45514
45515
45516
45517
45518
45519
45520
45521
45522
45523
45524
45525
45526
45527
45528
45529
45530
45531
45532
45533
45534
45535
45536
45537
45538
45539
45540
45541
45542
45543
45544
45545
45546
45547
45548
45549
45550
45551
45552
45553
45554
45555
45556
45557
45558
45559
45560
45561
45562
45563
45564
45565
45566
45567
45568
45569
45570
45571
45572
45573
45574
45575
45576
45577
45578
45579
45580
45581
45582
45583
45584
45585
45586
45587
45588
45589
45590
45591
45592
45593
45594
45595
45596
45597
45598
45599
45600
45601
45602
45603
45604
45605
45606
45607
45608
45609
45610
45611
45612
45613
45614
45615
45616
45617
45618
45619
45620
45621
45622
45623
45624
45625
45626
45627
45628
45629
45630
45631
45632
45633
45634
45635
45636
45637
45638
45639
45640
45641
45642
45643
45644
45645
45646
45647
45648
45649
45650
45651
45652
45653
45654
45655
45656
45657
45658
45659
45660
45661
45662
45663
45664
45665
45666
45667
45668
45669
45670
45671
45672
45673
45674
45675
45676
45677
45678
45679
45680
45681
45682
45683
45684
45685
45686
45687
45688
45689
45690
45691
45692
45693
45694
45695
45696
45697
45698
45699
45700
45701
45702
45703
45704
45705
45706
45707
45708
45709
45710
45711
45712
45713
45714
45715
45716
45717
45718
45719
45720
45721
45722
45723
45724
45725
45726
45727
45728
45729
45730
45731
45732
45733
45734
45735
45736
45737
45738
45739
45740
45741
45742
45743
45744
45745
45746
45747
45748
45749
45750
45751
45752
45753
45754
45755
45756
45757
45758
45759
45760
45761
45762
45763
45764
45765
45766
45767
45768
45769
45770
45771
45772
45773
45774
45775
45776
45777
45778
45779
45780
45781
45782
45783
45784
45785
45786
45787
45788
45789
45790
45791
45792
45793
45794
45795
45796
45797
45798
45799
45800
45801
45802
45803
45804
45805
45806
45807
45808
45809
45810
45811
45812
45813
45814
45815
45816
45817
45818
45819
45820
45821
45822
45823
45824
45825
45826
45827
45828
45829
45830
45831
45832
45833
45834
45835
45836
45837
45838
45839
45840
45841
45842
45843
45844
45845
45846
45847
45848
45849
45850
45851
45852
45853
45854
45855
45856
45857
45858
45859
45860
45861
45862
45863
45864
45865
45866
45867
45868
45869
45870
45871
45872
45873
45874
45875
45876
45877
45878
45879
45880
45881
45882
45883
45884
45885
45886
45887
45888
45889
45890
45891
45892
45893
45894
45895
45896
45897
45898
45899
45900
45901
45902
45903
45904
45905
45906
45907
45908
45909
45910
45911
45912
45913
45914
45915
45916
45917
45918
45919
45920
45921
45922
45923
45924
45925
45926
45927
45928
45929
45930
45931
45932
45933
45934
45935
45936
45937
45938
45939
45940
45941
45942
45943
45944
45945
45946
45947
45948
45949
45950
45951
45952
45953
45954
45955
45956
45957
45958
45959
45960
45961
45962
45963
45964
45965
45966
45967
45968
45969
45970
45971
45972
45973
45974
45975
45976
45977
45978
45979
45980
45981
45982
45983
45984
45985
45986
45987
45988
45989
45990
45991
45992
45993
45994
45995
45996
45997
45998
45999
46000
46001
46002
46003
46004
46005
46006
46007
46008
46009
46010
46011
46012
46013
46014
46015
46016
46017
46018
46019
46020
46021
46022
46023
46024
46025
46026
46027
46028
46029
46030
46031
46032
46033
46034
46035
46036
46037
46038
46039
46040
46041
46042
46043
46044
46045
46046
46047
46048
46049
46050
46051
46052
46053
46054
46055
46056
46057
46058
46059
46060
46061
46062
46063
46064
46065
46066
46067
46068
46069
46070
46071
46072
46073
46074
46075
46076
46077
46078
46079
46080
46081
46082
46083
46084
46085
46086
46087
46088
46089
46090
46091
46092
46093
46094
46095
46096
46097
46098
46099
46100
46101
46102
46103
46104
46105
46106
46107
46108
46109
46110
46111
46112
46113
46114
46115
46116
46117
46118
46119
46120
46121
46122
46123
46124
46125
46126
46127
46128
46129
46130
46131
46132
46133
46134
46135
46136
46137
46138
46139
46140
46141
46142
46143
46144
46145
46146
46147
46148
46149
46150
46151
46152
46153
46154
46155
46156
46157
46158
46159
46160
46161
46162
46163
46164
46165
46166
46167
46168
46169
46170
46171
46172
46173
46174
46175
46176
46177
46178
46179
46180
46181
46182
46183
46184
46185
46186
46187
46188
46189
46190
46191
46192
46193
46194
46195
46196
46197
46198
46199
46200
46201
46202
46203
46204
46205
46206
46207
46208
46209
46210
46211
46212
46213
46214
46215
46216
46217
46218
46219
46220
46221
46222
46223
46224
46225
46226
46227
46228
46229
46230
46231
46232
46233
46234
46235
46236
46237
46238
46239
46240
46241
46242
46243
46244
46245
46246
46247
46248
46249
46250
46251
46252
46253
46254
46255
46256
46257
46258
46259
46260
46261
46262
46263
46264
46265
46266
46267
46268
46269
46270
46271
46272
46273
46274
46275
46276
46277
46278
46279
46280
46281
46282
46283
46284
46285
46286
46287
46288
46289
46290
46291
46292
46293
46294
46295
46296
46297
46298
46299
46300
46301
46302
46303
46304
46305
46306
46307
46308
46309
46310
46311
46312
46313
46314
46315
46316
46317
46318
46319
46320
46321
46322
46323
46324
46325
46326
46327
46328
46329
46330
46331
46332
46333
46334
46335
46336
46337
46338
46339
46340
46341
46342
46343
46344
46345
46346
46347
46348
46349
46350
46351
46352
46353
46354
46355
46356
46357
46358
46359
46360
46361
46362
46363
46364
46365
46366
46367
46368
46369
46370
46371
46372
46373
46374
46375
46376
46377
46378
46379
46380
46381
46382
46383
46384
46385
46386
46387
46388
46389
46390
46391
46392
46393
46394
46395
46396
46397
46398
46399
46400
46401
46402
46403
46404
46405
46406
46407
46408
46409
46410
46411
46412
46413
46414
46415
46416
46417
46418
46419
46420
46421
46422
46423
46424
46425
46426
46427
46428
46429
46430
46431
46432
46433
46434
46435
46436
46437
46438
46439
46440
46441
46442
46443
46444
46445
46446
46447
46448
46449
46450
46451
46452
46453
46454
46455
46456
46457
46458
46459
46460
46461
46462
46463
46464
46465
46466
46467
46468
46469
46470
46471
46472
46473
46474
46475
46476
46477
46478
46479
46480
46481
46482
46483
46484
46485
46486
46487
46488
46489
46490
46491
46492
46493
46494
46495
46496
46497
46498
46499
46500
46501
46502
46503
46504
46505
46506
46507
46508
46509
46510
46511
46512
46513
46514
46515
46516
46517
46518
46519
46520
46521
46522
46523
46524
46525
46526
46527
46528
46529
46530
46531
46532
46533
46534
46535
46536
46537
46538
46539
46540
46541
46542
46543
46544
46545
46546
46547
46548
46549
46550
46551
46552
46553
46554
46555
46556
46557
46558
46559
46560
46561
46562
46563
46564
46565
46566
46567
46568
46569
46570
46571
46572
46573
46574
46575
46576
46577
46578
46579
46580
46581
46582
46583
46584
46585
46586
46587
46588
46589
46590
46591
46592
46593
46594
46595
46596
46597
46598
46599
46600
46601
46602
46603
46604
46605
46606
46607
46608
46609
46610
46611
46612
46613
46614
46615
46616
46617
46618
46619
46620
46621
46622
46623
46624
46625
46626
46627
46628
46629
46630
46631
46632
46633
46634
46635
46636
46637
46638
46639
46640
46641
46642
46643
46644
46645
46646
46647
46648
46649
46650
46651
46652
46653
46654
46655
46656
46657
46658
46659
46660
46661
46662
46663
46664
46665
46666
46667
46668
46669
46670
46671
46672
46673
46674
46675
46676
46677
46678
46679
46680
46681
46682
46683
46684
46685
46686
46687
46688
46689
46690
46691
46692
46693
46694
46695
46696
46697
46698
46699
46700
46701
46702
46703
46704
46705
46706
46707
46708
46709
46710
46711
46712
46713
46714
46715
46716
46717
46718
46719
46720
46721
46722
46723
46724
46725
46726
46727
46728
46729
46730
46731
46732
46733
46734
46735
46736
46737
46738
46739
46740
46741
46742
46743
46744
46745
46746
46747
46748
46749
46750
46751
46752
46753
46754
46755
46756
46757
46758
46759
46760
46761
46762
46763
46764
46765
46766
46767
46768
46769
46770
46771
46772
46773
46774
46775
46776
46777
46778
46779
46780
46781
46782
46783
46784
46785
46786
46787
46788
46789
46790
46791
46792
46793
46794
46795
46796
46797
46798
46799
46800
46801
46802
46803
46804
46805
46806
46807
46808
46809
46810
46811
46812
46813
46814
46815
46816
46817
46818
46819
46820
46821
46822
46823
46824
46825
46826
46827
46828
46829
46830
46831
46832
46833
46834
46835
46836
46837
46838
46839
46840
46841
46842
46843
46844
46845
46846
46847
46848
46849
46850
46851
46852
46853
46854
46855
46856
46857
46858
46859
46860
46861
46862
46863
46864
46865
46866
46867
46868
46869
46870
46871
46872
46873
46874
46875
46876
46877
46878
46879
46880
46881
46882
46883
46884
46885
46886
46887
46888
46889
46890
46891
46892
46893
46894
46895
46896
46897
46898
46899
46900
46901
46902
46903
46904
46905
46906
46907
46908
46909
46910
46911
46912
46913
46914
46915
46916
46917
46918
46919
46920
46921
46922
46923
46924
46925
46926
46927
46928
46929
46930
46931
46932
46933
46934
46935
46936
46937
46938
46939
46940
46941
46942
46943
46944
46945
46946
46947
46948
46949
46950
46951
46952
46953
46954
46955
46956
46957
46958
46959
46960
46961
46962
46963
46964
46965
46966
46967
46968
46969
46970
46971
46972
46973
46974
46975
46976
46977
46978
46979
46980
46981
46982
46983
46984
46985
46986
46987
46988
46989
46990
46991
46992
46993
46994
46995
46996
46997
46998
46999
47000
47001
47002
47003
47004
47005
47006
47007
47008
47009
47010
47011
47012
47013
47014
47015
47016
47017
47018
47019
47020
47021
47022
47023
47024
47025
47026
47027
47028
47029
47030
47031
47032
47033
47034
47035
47036
47037
47038
47039
47040
47041
47042
47043
47044
47045
47046
47047
47048
47049
47050
47051
47052
47053
47054
47055
47056
47057
47058
47059
47060
47061
47062
47063
47064
47065
47066
47067
47068
47069
47070
47071
47072
47073
47074
47075
47076
47077
47078
47079
47080
47081
47082
47083
47084
47085
47086
47087
47088
47089
47090
47091
47092
47093
47094
47095
47096
47097
47098
47099
47100
47101
47102
47103
47104
47105
47106
47107
47108
47109
47110
47111
47112
47113
47114
47115
47116
47117
47118
47119
47120
47121
47122
47123
47124
47125
47126
47127
47128
47129
47130
47131
47132
47133
47134
47135
47136
47137
47138
47139
47140
47141
47142
47143
47144
47145
47146
47147
47148
47149
47150
47151
47152
47153
47154
47155
47156
47157
47158
47159
47160
47161
47162
47163
47164
47165
47166
47167
47168
47169
47170
47171
47172
47173
47174
47175
47176
47177
47178
47179
47180
47181
47182
47183
47184
47185
47186
47187
47188
47189
47190
47191
47192
47193
47194
47195
47196
47197
47198
47199
47200
47201
47202
47203
47204
47205
47206
47207
47208
47209
47210
47211
47212
47213
47214
47215
47216
47217
47218
47219
47220
47221
47222
47223
47224
47225
47226
47227
47228
47229
47230
47231
47232
47233
47234
47235
47236
47237
47238
47239
47240
47241
47242
47243
47244
47245
47246
47247
47248
47249
47250
47251
47252
47253
47254
47255
47256
47257
47258
47259
47260
47261
47262
47263
47264
47265
47266
47267
47268
47269
47270
47271
47272
47273
47274
47275
47276
47277
47278
47279
47280
47281
47282
47283
47284
47285
47286
47287
47288
47289
47290
47291
47292
47293
47294
47295
47296
47297
47298
47299
47300
47301
47302
47303
47304
47305
47306
47307
47308
47309
47310
47311
47312
47313
47314
47315
47316
47317
47318
47319
47320
47321
47322
47323
47324
47325
47326
47327
47328
47329
47330
47331
47332
47333
47334
47335
47336
47337
47338
47339
47340
47341
47342
47343
47344
47345
47346
47347
47348
47349
47350
47351
47352
47353
47354
47355
47356
47357
47358
47359
47360
47361
47362
47363
47364
47365
47366
47367
47368
47369
47370
47371
47372
47373
47374
47375
47376
47377
47378
47379
47380
47381
47382
47383
47384
47385
47386
47387
47388
47389
47390
47391
47392
47393
47394
47395
47396
47397
47398
47399
47400
47401
47402
47403
47404
47405
47406
47407
47408
47409
47410
47411
47412
47413
47414
47415
47416
47417
47418
47419
47420
47421
47422
47423
47424
47425
47426
47427
47428
47429
47430
47431
47432
47433
47434
47435
47436
47437
47438
47439
47440
47441
47442
47443
47444
47445
47446
47447
47448
47449
47450
47451
47452
47453
47454
47455
47456
47457
47458
47459
47460
47461
47462
47463
47464
47465
47466
47467
47468
47469
47470
47471
47472
47473
47474
47475
47476
47477
47478
47479
47480
47481
47482
47483
47484
47485
47486
47487
47488
47489
47490
47491
47492
47493
47494
47495
47496
47497
47498
47499
47500
47501
47502
47503
47504
47505
47506
47507
47508
47509
47510
47511
47512
47513
47514
47515
47516
47517
47518
47519
47520
47521
47522
47523
47524
47525
47526
47527
47528
47529
47530
47531
47532
47533
47534
47535
47536
47537
47538
47539
47540
47541
47542
47543
47544
47545
47546
47547
47548
47549
47550
47551
47552
47553
47554
47555
47556
47557
47558
47559
47560
47561
47562
47563
47564
47565
47566
47567
47568
47569
47570
47571
47572
47573
47574
47575
47576
47577
47578
47579
47580
47581
47582
47583
47584
47585
47586
47587
47588
47589
47590
47591
47592
47593
47594
47595
47596
47597
47598
47599
47600
47601
47602
47603
47604
47605
47606
47607
47608
47609
47610
47611
47612
47613
47614
47615
47616
47617
47618
47619
47620
47621
47622
47623
47624
47625
47626
47627
47628
47629
47630
47631
47632
47633
47634
47635
47636
47637
47638
47639
47640
47641
47642
47643
47644
47645
47646
47647
47648
47649
47650
47651
47652
47653
47654
47655
47656
47657
47658
47659
47660
47661
47662
47663
47664
47665
47666
47667
47668
47669
47670
47671
47672
47673
47674
47675
47676
47677
47678
47679
47680
47681
47682
47683
47684
47685
47686
47687
47688
47689
47690
47691
47692
47693
47694
47695
47696
47697
47698
47699
47700
47701
47702
47703
47704
47705
47706
47707
47708
47709
47710
47711
47712
47713
47714
47715
47716
47717
47718
47719
47720
47721
47722
47723
47724
47725
47726
47727
47728
47729
47730
47731
47732
47733
47734
47735
47736
47737
47738
47739
47740
47741
47742
47743
47744
47745
47746
47747
47748
47749
47750
47751
47752
47753
47754
47755
47756
47757
47758
47759
47760
47761
47762
47763
47764
47765
47766
47767
47768
47769
47770
47771
47772
47773
47774
47775
47776
47777
47778
47779
47780
47781
47782
47783
47784
47785
47786
47787
47788
47789
47790
47791
47792
47793
47794
47795
47796
47797
47798
47799
47800
47801
47802
47803
47804
47805
47806
47807
47808
47809
47810
47811
47812
47813
47814
47815
47816
47817
47818
47819
47820
47821
47822
47823
47824
47825
47826
47827
47828
47829
47830
47831
47832
47833
47834
47835
47836
47837
47838
47839
47840
47841
47842
47843
47844
47845
47846
47847
47848
47849
47850
47851
47852
47853
47854
47855
47856
47857
47858
47859
47860
47861
47862
47863
47864
47865
47866
47867
47868
47869
47870
47871
47872
47873
47874
47875
47876
47877
47878
47879
47880
47881
47882
47883
47884
47885
47886
47887
47888
47889
47890
47891
47892
47893
47894
47895
47896
47897
47898
47899
47900
47901
47902
47903
47904
47905
47906
47907
47908
47909
47910
47911
47912
47913
47914
47915
47916
47917
47918
47919
47920
47921
47922
47923
47924
47925
47926
47927
47928
47929
47930
47931
47932
47933
47934
47935
47936
47937
47938
47939
47940
47941
47942
47943
47944
47945
47946
47947
47948
47949
47950
47951
47952
47953
47954
47955
47956
47957
47958
47959
47960
47961
47962
47963
47964
47965
47966
47967
47968
47969
47970
47971
47972
47973
47974
47975
47976
47977
47978
47979
47980
47981
47982
47983
47984
47985
47986
47987
47988
47989
47990
47991
47992
47993
47994
47995
47996
47997
47998
47999
48000
48001
48002
48003
48004
48005
48006
48007
48008
48009
48010
48011
48012
48013
48014
48015
48016
48017
48018
48019
48020
48021
48022
48023
48024
48025
48026
48027
48028
48029
48030
48031
48032
48033
48034
48035
48036
48037
48038
48039
48040
48041
48042
48043
48044
48045
48046
48047
48048
48049
48050
48051
48052
48053
48054
48055
48056
48057
48058
48059
48060
48061
48062
48063
48064
48065
48066
48067
48068
48069
48070
48071
48072
48073
48074
48075
48076
48077
48078
48079
48080
48081
48082
48083
48084
48085
48086
48087
48088
48089
48090
48091
48092
48093
48094
48095
48096
48097
48098
48099
48100
48101
48102
48103
48104
48105
48106
48107
48108
48109
48110
48111
48112
48113
48114
48115
48116
48117
48118
48119
48120
48121
48122
48123
48124
48125
48126
48127
48128
48129
48130
48131
48132
48133
48134
48135
48136
48137
48138
48139
48140
48141
48142
48143
48144
48145
48146
48147
48148
48149
48150
48151
48152
48153
48154
48155
48156
48157
48158
48159
48160
48161
48162
48163
48164
48165
48166
48167
48168
48169
48170
48171
48172
48173
48174
48175
48176
48177
48178
48179
48180
48181
48182
48183
48184
48185
48186
48187
48188
48189
48190
48191
48192
48193
48194
48195
48196
48197
48198
48199
48200
48201
48202
48203
48204
48205
48206
48207
48208
48209
48210
48211
48212
48213
48214
48215
48216
48217
48218
48219
48220
48221
48222
48223
48224
48225
48226
48227
48228
48229
48230
48231
48232
48233
48234
48235
48236
48237
48238
48239
48240
48241
48242
48243
48244
48245
48246
48247
48248
48249
48250
48251
48252
48253
48254
48255
48256
48257
48258
48259
48260
48261
48262
48263
48264
48265
48266
48267
48268
48269
48270
48271
48272
48273
48274
48275
48276
48277
48278
48279
48280
48281
48282
48283
48284
48285
48286
48287
48288
48289
48290
48291
48292
48293
48294
48295
48296
48297
48298
48299
48300
48301
48302
48303
48304
48305
48306
48307
48308
48309
48310
48311
48312
48313
48314
48315
48316
48317
48318
48319
48320
48321
48322
48323
48324
48325
48326
48327
48328
48329
48330
48331
48332
48333
48334
48335
48336
48337
48338
48339
48340
48341
48342
48343
48344
48345
48346
48347
48348
48349
48350
48351
48352
48353
48354
48355
48356
48357
48358
48359
48360
48361
48362
48363
48364
48365
48366
48367
48368
48369
48370
48371
48372
48373
48374
48375
48376
48377
48378
48379
48380
48381
48382
48383
48384
48385
48386
48387
48388
48389
48390
48391
48392
48393
48394
48395
48396
48397
48398
48399
48400
48401
48402
48403
48404
48405
48406
48407
48408
48409
48410
48411
48412
48413
48414
48415
48416
48417
48418
48419
48420
48421
48422
48423
48424
48425
48426
48427
48428
48429
48430
48431
48432
48433
48434
48435
48436
48437
48438
48439
48440
48441
48442
48443
48444
48445
48446
48447
48448
48449
48450
48451
48452
48453
48454
48455
48456
48457
48458
48459
48460
48461
48462
48463
48464
48465
48466
48467
48468
48469
48470
48471
48472
48473
48474
48475
48476
48477
48478
48479
48480
48481
48482
48483
48484
48485
48486
48487
48488
48489
48490
48491
48492
48493
48494
48495
48496
48497
48498
48499
48500
48501
48502
48503
48504
48505
48506
48507
48508
48509
48510
48511
48512
48513
48514
48515
48516
48517
48518
48519
48520
48521
48522
48523
48524
48525
48526
48527
48528
48529
48530
48531
48532
48533
48534
48535
48536
48537
48538
48539
48540
48541
48542
48543
48544
48545
48546
48547
48548
48549
48550
48551
48552
48553
48554
48555
48556
48557
48558
48559
48560
48561
48562
48563
48564
48565
48566
48567
48568
48569
48570
48571
48572
48573
48574
48575
48576
48577
48578
48579
48580
48581
48582
48583
48584
48585
48586
48587
48588
48589
48590
48591
48592
48593
48594
48595
48596
48597
48598
48599
48600
48601
48602
48603
48604
48605
48606
48607
48608
48609
48610
48611
48612
48613
48614
48615
48616
48617
48618
48619
48620
48621
48622
48623
48624
48625
48626
48627
48628
48629
48630
48631
48632
48633
48634
48635
48636
48637
48638
48639
48640
48641
48642
48643
48644
48645
48646
48647
48648
48649
48650
48651
48652
48653
48654
48655
48656
48657
48658
48659
48660
48661
48662
48663
48664
48665
48666
48667
48668
48669
48670
48671
48672
48673
48674
48675
48676
48677
48678
48679
48680
48681
48682
48683
48684
48685
48686
48687
48688
48689
48690
48691
48692
48693
48694
48695
48696
48697
48698
48699
48700
48701
48702
48703
48704
48705
48706
48707
48708
48709
48710
48711
48712
48713
48714
48715
48716
48717
48718
48719
48720
48721
48722
48723
48724
48725
48726
48727
48728
48729
48730
48731
48732
48733
48734
48735
48736
48737
48738
48739
48740
48741
48742
48743
48744
48745
48746
48747
48748
48749
48750
48751
48752
48753
48754
48755
48756
48757
48758
48759
48760
48761
48762
48763
48764
48765
48766
48767
48768
48769
48770
48771
48772
48773
48774
48775
48776
48777
48778
48779
48780
48781
48782
48783
48784
48785
48786
48787
48788
48789
48790
48791
48792
48793
48794
48795
48796
48797
48798
48799
48800
48801
48802
48803
48804
48805
48806
48807
48808
48809
48810
48811
48812
48813
48814
48815
48816
48817
48818
48819
48820
48821
48822
48823
48824
48825
48826
48827
48828
48829
48830
48831
48832
48833
48834
48835
48836
48837
48838
48839
48840
48841
48842
48843
48844
48845
48846
48847
48848
48849
48850
48851
48852
48853
48854
48855
48856
48857
48858
48859
48860
48861
48862
48863
48864
48865
48866
48867
48868
48869
48870
48871
48872
48873
48874
48875
48876
48877
48878
48879
48880
48881
48882
48883
48884
48885
48886
48887
48888
48889
48890
48891
48892
48893
48894
48895
48896
48897
48898
48899
48900
48901
48902
48903
48904
48905
48906
48907
48908
48909
48910
48911
48912
48913
48914
48915
48916
48917
48918
48919
48920
48921
48922
48923
48924
48925
48926
48927
48928
48929
48930
48931
48932
48933
48934
48935
48936
48937
48938
48939
48940
48941
48942
48943
48944
48945
48946
48947
48948
48949
48950
48951
48952
48953
48954
48955
48956
48957
48958
48959
48960
48961
48962
48963
48964
48965
48966
48967
48968
48969
48970
48971
48972
48973
48974
48975
48976
48977
48978
48979
48980
48981
48982
48983
48984
48985
48986
48987
48988
48989
48990
48991
48992
48993
48994
48995
48996
48997
48998
48999
49000
49001
49002
49003
49004
49005
49006
49007
49008
49009
49010
49011
49012
49013
49014
49015
49016
49017
49018
49019
49020
49021
49022
49023
49024
49025
49026
49027
49028
49029
49030
49031
49032
49033
49034
49035
49036
49037
49038
49039
49040
49041
49042
49043
49044
49045
49046
49047
49048
49049
49050
49051
49052
49053
49054
49055
49056
49057
49058
49059
49060
49061
49062
49063
49064
49065
49066
49067
49068
49069
49070
49071
49072
49073
49074
49075
49076
49077
49078
49079
49080
49081
49082
49083
49084
49085
49086
49087
49088
49089
49090
49091
49092
49093
49094
49095
49096
49097
49098
49099
49100
49101
49102
49103
49104
49105
49106
49107
49108
49109
49110
49111
49112
49113
49114
49115
49116
49117
49118
49119
49120
49121
49122
49123
49124
49125
49126
49127
49128
49129
49130
49131
49132
49133
49134
49135
49136
49137
49138
49139
49140
49141
49142
49143
49144
49145
49146
49147
49148
49149
49150
49151
49152
49153
49154
49155
49156
49157
49158
49159
49160
49161
49162
49163
49164
49165
49166
49167
49168
49169
49170
49171
49172
49173
49174
49175
49176
49177
49178
49179
49180
49181
49182
49183
49184
49185
49186
49187
49188
49189
49190
49191
49192
49193
49194
49195
49196
49197
49198
49199
49200
49201
49202
49203
49204
49205
49206
49207
49208
49209
49210
49211
49212
49213
49214
49215
49216
49217
49218
49219
49220
49221
49222
49223
49224
49225
49226
49227
49228
49229
49230
49231
49232
49233
49234
49235
49236
49237
49238
49239
49240
49241
49242
49243
49244
49245
49246
49247
49248
49249
49250
49251
49252
49253
49254
49255
49256
49257
49258
49259
49260
49261
49262
49263
49264
49265
49266
49267
49268
49269
49270
49271
49272
49273
49274
49275
49276
49277
49278
49279
49280
49281
49282
49283
49284
49285
49286
49287
49288
49289
49290
49291
49292
49293
49294
49295
49296
49297
49298
49299
49300
49301
49302
49303
49304
49305
49306
49307
49308
49309
49310
49311
49312
49313
49314
49315
49316
49317
49318
49319
49320
49321
49322
49323
49324
49325
49326
49327
49328
49329
49330
49331
49332
49333
49334
49335
49336
49337
49338
49339
49340
49341
49342
49343
49344
49345
49346
49347
49348
49349
49350
49351
49352
49353
49354
49355
49356
49357
49358
49359
49360
49361
49362
49363
49364
49365
49366
49367
49368
49369
49370
49371
49372
49373
49374
49375
49376
49377
49378
49379
49380
49381
49382
49383
49384
49385
49386
49387
49388
49389
49390
49391
49392
49393
49394
49395
49396
49397
49398
49399
49400
49401
49402
49403
49404
49405
49406
49407
49408
49409
49410
49411
49412
49413
49414
49415
49416
49417
49418
49419
49420
49421
49422
49423
49424
49425
49426
49427
49428
49429
49430
49431
49432
49433
49434
49435
49436
49437
49438
49439
49440
49441
49442
49443
49444
49445
49446
49447
49448
49449
49450
49451
49452
49453
49454
49455
49456
49457
49458
49459
49460
49461
49462
49463
49464
49465
49466
49467
49468
49469
49470
49471
49472
49473
49474
49475
49476
49477
49478
49479
49480
49481
49482
49483
49484
49485
49486
49487
49488
49489
49490
49491
49492
49493
49494
49495
49496
49497
49498
49499
49500
49501
49502
49503
49504
49505
49506
49507
49508
49509
49510
49511
49512
49513
49514
49515
49516
49517
49518
49519
49520
49521
49522
49523
49524
49525
49526
49527
49528
49529
49530
49531
49532
49533
49534
49535
49536
49537
49538
49539
49540
49541
49542
49543
49544
49545
49546
49547
49548
49549
49550
49551
49552
49553
49554
49555
49556
49557
49558
49559
49560
49561
49562
49563
49564
49565
49566
49567
49568
49569
49570
49571
49572
49573
49574
49575
49576
49577
49578
49579
49580
49581
49582
49583
49584
49585
49586
49587
49588
49589
49590
49591
49592
49593
49594
49595
49596
49597
49598
49599
49600
49601
49602
49603
49604
49605
49606
49607
49608
49609
49610
49611
49612
49613
49614
49615
49616
49617
49618
49619
49620
49621
49622
49623
49624
49625
49626
49627
49628
49629
49630
49631
49632
49633
49634
49635
49636
49637
49638
49639
49640
49641
49642
49643
49644
49645
49646
49647
49648
49649
49650
49651
49652
49653
49654
49655
49656
49657
49658
49659
49660
49661
49662
49663
49664
49665
49666
49667
49668
49669
49670
49671
49672
49673
49674
49675
49676
49677
49678
49679
49680
49681
49682
49683
49684
49685
49686
49687
49688
49689
49690
49691
49692
49693
49694
49695
49696
49697
49698
49699
49700
49701
49702
49703
49704
49705
49706
49707
49708
49709
49710
49711
49712
49713
49714
49715
49716
49717
49718
49719
49720
49721
49722
49723
49724
49725
49726
49727
49728
49729
49730
49731
49732
49733
49734
49735
49736
49737
49738
49739
49740
49741
49742
49743
49744
49745
49746
49747
49748
49749
49750
49751
49752
49753
49754
49755
49756
49757
49758
49759
49760
49761
49762
49763
49764
49765
49766
49767
49768
49769
49770
49771
49772
49773
49774
49775
49776
49777
49778
49779
49780
49781
49782
49783
49784
49785
49786
49787
49788
49789
49790
49791
49792
49793
49794
49795
49796
49797
49798
49799
49800
49801
49802
49803
49804
49805
49806
49807
49808
49809
49810
49811
49812
49813
49814
49815
49816
49817
49818
49819
49820
49821
49822
49823
49824
49825
49826
49827
49828
49829
49830
49831
49832
49833
49834
49835
49836
49837
49838
49839
49840
49841
49842
49843
49844
49845
49846
49847
49848
49849
49850
49851
49852
49853
49854
49855
49856
49857
49858
49859
49860
49861
49862
49863
49864
49865
49866
49867
49868
49869
49870
49871
49872
49873
49874
49875
49876
49877
49878
49879
49880
49881
49882
49883
49884
49885
49886
49887
49888
49889
49890
49891
49892
49893
49894
49895
49896
49897
49898
49899
49900
49901
49902
49903
49904
49905
49906
49907
49908
49909
49910
49911
49912
49913
49914
49915
49916
49917
49918
49919
49920
49921
49922
49923
49924
49925
49926
49927
49928
49929
49930
49931
49932
49933
49934
49935
49936
49937
49938
49939
49940
49941
49942
49943
49944
49945
49946
49947
49948
49949
49950
49951
49952
49953
49954
49955
49956
49957
49958
49959
49960
49961
49962
49963
49964
49965
49966
49967
49968
49969
49970
49971
49972
49973
49974
49975
49976
49977
49978
49979
49980
49981
49982
49983
49984
49985
49986
49987
49988
49989
49990
49991
49992
49993
49994
49995
49996
49997
49998
49999
50000
50001
50002
50003
50004
50005
50006
50007
50008
50009
50010
50011
50012
50013
50014
50015
50016
50017
50018
50019
50020
50021
50022
50023
50024
50025
50026
50027
50028
50029
50030
50031
50032
50033
50034
50035
50036
50037
50038
50039
50040
50041
50042
50043
50044
50045
50046
50047
50048
50049
50050
50051
50052
50053
50054
50055
50056
50057
50058
50059
50060
50061
50062
50063
50064
50065
50066
50067
50068
50069
50070
50071
50072
50073
50074
50075
50076
50077
50078
50079
50080
50081
50082
50083
50084
50085
50086
50087
50088
50089
50090
50091
50092
50093
50094
50095
50096
50097
50098
50099
50100
50101
50102
50103
50104
50105
50106
50107
50108
50109
50110
50111
50112
50113
50114
50115
50116
50117
50118
50119
50120
50121
50122
50123
50124
50125
50126
50127
50128
50129
50130
50131
50132
50133
50134
50135
50136
50137
50138
50139
50140
50141
50142
50143
50144
50145
50146
50147
50148
50149
50150
50151
50152
50153
50154
50155
50156
50157
50158
50159
50160
50161
50162
50163
50164
50165
50166
50167
50168
50169
50170
50171
50172
50173
50174
50175
50176
50177
50178
50179
50180
50181
50182
50183
50184
50185
50186
50187
50188
50189
50190
50191
50192
50193
50194
50195
50196
50197
50198
50199
50200
50201
50202
50203
50204
50205
50206
50207
50208
50209
50210
50211
50212
50213
50214
50215
50216
50217
50218
50219
50220
50221
50222
50223
50224
50225
50226
50227
50228
50229
50230
50231
50232
50233
50234
50235
50236
50237
50238
50239
50240
50241
50242
50243
50244
50245
50246
50247
50248
50249
50250
50251
50252
50253
50254
50255
50256
50257
50258
50259
50260
50261
50262
50263
50264
50265
50266
50267
50268
50269
50270
50271
50272
50273
50274
50275
50276
50277
50278
50279
50280
50281
50282
50283
50284
50285
50286
50287
50288
50289
50290
50291
50292
50293
50294
50295
50296
50297
50298
50299
50300
50301
50302
50303
50304
50305
50306
50307
50308
50309
50310
50311
50312
50313
50314
50315
50316
50317
50318
50319
50320
50321
50322
50323
50324
50325
50326
50327
50328
50329
50330
50331
50332
50333
50334
50335
50336
50337
50338
50339
50340
50341
50342
50343
50344
50345
50346
50347
50348
50349
50350
50351
50352
50353
50354
50355
50356
50357
50358
50359
50360
50361
50362
50363
50364
50365
50366
50367
50368
50369
50370
50371
50372
50373
50374
50375
50376
50377
50378
50379
50380
50381
50382
50383
50384
50385
50386
50387
50388
50389
50390
50391
50392
50393
50394
50395
50396
50397
50398
50399
50400
50401
50402
50403
50404
50405
50406
50407
50408
50409
50410
50411
50412
50413
50414
50415
50416
50417
50418
50419
50420
50421
50422
50423
50424
50425
50426
50427
50428
50429
50430
50431
50432
50433
50434
50435
50436
50437
50438
50439
50440
50441
50442
50443
50444
50445
50446
50447
50448
50449
50450
50451
50452
50453
50454
50455
50456
50457
50458
50459
50460
50461
50462
50463
50464
50465
50466
50467
50468
50469
50470
50471
50472
50473
50474
50475
50476
50477
50478
50479
50480
50481
50482
50483
50484
50485
50486
50487
50488
50489
50490
50491
50492
50493
50494
50495
50496
50497
50498
50499
50500
50501
50502
50503
50504
50505
50506
50507
50508
50509
50510
50511
50512
50513
50514
50515
50516
50517
50518
50519
50520
50521
50522
50523
50524
50525
50526
50527
50528
50529
50530
50531
50532
50533
50534
50535
50536
50537
50538
50539
50540
50541
50542
50543
50544
50545
50546
50547
50548
50549
50550
50551
50552
50553
50554
50555
50556
50557
50558
50559
50560
50561
50562
50563
50564
50565
50566
50567
50568
50569
50570
50571
50572
50573
50574
50575
50576
50577
50578
50579
50580
50581
50582
50583
50584
50585
50586
50587
50588
50589
50590
50591
50592
50593
50594
50595
50596
50597
50598
50599
50600
50601
50602
50603
50604
50605
50606
50607
50608
50609
50610
50611
50612
50613
50614
50615
50616
50617
50618
50619
50620
50621
50622
50623
50624
50625
50626
50627
50628
50629
50630
50631
50632
50633
50634
50635
50636
50637
50638
50639
50640
50641
50642
50643
50644
50645
50646
50647
50648
50649
50650
50651
50652
50653
50654
50655
50656
50657
50658
50659
50660
50661
50662
50663
50664
50665
50666
50667
50668
50669
50670
50671
50672
50673
50674
50675
50676
50677
50678
50679
50680
50681
50682
50683
50684
50685
50686
50687
50688
50689
50690
50691
50692
50693
50694
50695
50696
50697
50698
50699
50700
50701
50702
50703
50704
50705
50706
50707
50708
50709
50710
50711
50712
50713
50714
50715
50716
50717
50718
50719
50720
50721
50722
50723
50724
50725
50726
50727
50728
50729
50730
50731
50732
50733
50734
50735
50736
50737
50738
50739
50740
50741
50742
50743
50744
50745
50746
50747
50748
50749
50750
50751
50752
50753
50754
50755
50756
50757
50758
50759
50760
50761
50762
50763
50764
50765
50766
50767
50768
50769
50770
50771
50772
50773
50774
50775
50776
50777
50778
50779
50780
50781
50782
50783
50784
50785
50786
50787
50788
50789
50790
50791
50792
50793
50794
50795
50796
50797
50798
50799
50800
50801
50802
50803
50804
50805
50806
50807
50808
50809
50810
50811
50812
50813
50814
50815
50816
50817
50818
50819
50820
50821
50822
50823
50824
50825
50826
50827
50828
50829
50830
50831
50832
50833
50834
50835
50836
50837
50838
50839
50840
50841
50842
50843
50844
50845
50846
50847
50848
50849
50850
50851
50852
50853
50854
50855
50856
50857
50858
50859
50860
50861
50862
50863
50864
50865
50866
50867
50868
50869
50870
50871
50872
50873
50874
50875
50876
50877
50878
50879
50880
50881
50882
50883
50884
50885
50886
50887
50888
50889
50890
50891
50892
50893
50894
50895
50896
50897
50898
50899
50900
50901
50902
50903
50904
50905
50906
50907
50908
50909
50910
50911
50912
50913
50914
50915
50916
50917
50918
50919
50920
50921
50922
50923
50924
50925
50926
50927
50928
50929
50930
50931
50932
50933
50934
50935
50936
50937
50938
50939
50940
50941
50942
50943
50944
50945
50946
50947
50948
50949
50950
50951
50952
50953
50954
50955
50956
50957
50958
50959
50960
50961
50962
50963
50964
50965
50966
50967
50968
50969
50970
50971
50972
50973
50974
50975
50976
50977
50978
50979
50980
50981
50982
50983
50984
50985
50986
50987
50988
50989
50990
50991
50992
50993
50994
50995
50996
50997
50998
50999
51000
51001
51002
51003
51004
51005
51006
51007
51008
51009
51010
51011
51012
51013
51014
51015
51016
51017
51018
51019
51020
51021
51022
51023
51024
51025
51026
51027
51028
51029
51030
51031
51032
51033
51034
51035
51036
51037
51038
51039
51040
51041
51042
51043
51044
51045
51046
51047
51048
51049
51050
51051
51052
51053
51054
51055
51056
51057
51058
51059
51060
51061
51062
51063
51064
51065
51066
51067
51068
51069
51070
51071
51072
51073
51074
51075
51076
51077
51078
51079
51080
51081
51082
51083
51084
51085
51086
51087
51088
51089
51090
51091
51092
51093
51094
51095
51096
51097
51098
51099
51100
51101
51102
51103
51104
51105
51106
51107
51108
51109
51110
51111
51112
51113
51114
51115
51116
51117
51118
51119
51120
51121
51122
51123
51124
51125
51126
51127
51128
51129
51130
51131
51132
51133
51134
51135
51136
51137
51138
51139
51140
51141
51142
51143
51144
51145
51146
51147
51148
51149
51150
51151
51152
51153
51154
51155
51156
51157
51158
51159
51160
51161
51162
51163
51164
51165
51166
51167
51168
51169
51170
51171
51172
51173
51174
51175
51176
51177
51178
51179
51180
51181
51182
51183
51184
51185
51186
51187
51188
51189
51190
51191
51192
51193
51194
51195
51196
51197
51198
51199
51200
51201
51202
51203
51204
51205
51206
51207
51208
51209
51210
51211
51212
51213
51214
51215
51216
51217
51218
51219
51220
51221
51222
51223
51224
51225
51226
51227
51228
51229
51230
51231
51232
51233
51234
51235
51236
51237
51238
51239
51240
51241
51242
51243
51244
51245
51246
51247
51248
51249
51250
51251
51252
51253
51254
51255
51256
51257
51258
51259
51260
51261
51262
51263
51264
51265
51266
51267
51268
51269
51270
51271
51272
51273
51274
51275
51276
51277
51278
51279
51280
51281
51282
51283
51284
51285
51286
51287
51288
51289
51290
51291
51292
51293
51294
51295
51296
51297
51298
51299
51300
51301
51302
51303
51304
51305
51306
51307
51308
51309
51310
51311
51312
51313
51314
51315
51316
51317
51318
51319
51320
51321
51322
51323
51324
51325
51326
51327
51328
51329
51330
51331
51332
51333
51334
51335
51336
51337
51338
51339
51340
51341
51342
51343
51344
51345
51346
51347
51348
51349
51350
51351
51352
51353
51354
51355
51356
51357
51358
51359
51360
51361
51362
51363
51364
51365
51366
51367
51368
51369
51370
51371
51372
51373
51374
51375
51376
51377
51378
51379
51380
51381
51382
51383
51384
51385
51386
51387
51388
51389
51390
51391
51392
51393
51394
51395
51396
51397
51398
51399
51400
51401
51402
51403
51404
51405
51406
51407
51408
51409
51410
51411
51412
51413
51414
51415
51416
51417
51418
51419
51420
51421
51422
51423
51424
51425
51426
51427
51428
51429
51430
51431
51432
51433
51434
51435
51436
51437
51438
51439
51440
51441
51442
51443
51444
51445
51446
51447
51448
51449
51450
51451
51452
51453
51454
51455
51456
51457
51458
51459
51460
51461
51462
51463
51464
51465
51466
51467
51468
51469
51470
51471
51472
51473
51474
51475
51476
51477
51478
51479
51480
51481
51482
51483
51484
51485
51486
51487
51488
51489
51490
51491
51492
51493
51494
51495
51496
51497
51498
51499
51500
51501
51502
51503
51504
51505
51506
51507
51508
51509
51510
51511
51512
51513
51514
51515
51516
51517
51518
51519
51520
51521
51522
51523
51524
51525
51526
51527
51528
51529
51530
51531
51532
51533
51534
51535
51536
51537
51538
51539
51540
51541
51542
51543
51544
51545
51546
51547
51548
51549
51550
51551
51552
51553
51554
51555
51556
51557
51558
51559
51560
51561
51562
51563
51564
51565
51566
51567
51568
51569
51570
51571
51572
51573
51574
51575
51576
51577
51578
51579
51580
51581
51582
51583
51584
51585
51586
51587
51588
51589
51590
51591
51592
51593
51594
51595
51596
51597
51598
51599
51600
51601
51602
51603
51604
51605
51606
51607
51608
51609
51610
51611
51612
51613
51614
51615
51616
51617
51618
51619
51620
51621
51622
51623
51624
51625
51626
51627
51628
51629
51630
51631
51632
51633
51634
51635
51636
51637
51638
51639
51640
51641
51642
51643
51644
51645
51646
51647
51648
51649
51650
51651
51652
51653
51654
51655
51656
51657
51658
51659
51660
51661
51662
51663
51664
51665
51666
51667
51668
51669
51670
51671
51672
51673
51674
51675
51676
51677
51678
51679
51680
51681
51682
51683
51684
51685
51686
51687
51688
51689
51690
51691
51692
51693
51694
51695
51696
51697
51698
51699
51700
51701
51702
51703
51704
51705
51706
51707
51708
51709
51710
51711
51712
51713
51714
51715
51716
51717
51718
51719
51720
51721
51722
51723
51724
51725
51726
51727
51728
51729
51730
51731
51732
51733
51734
51735
51736
51737
51738
51739
51740
51741
51742
51743
51744
51745
51746
51747
51748
51749
51750
51751
51752
51753
51754
51755
51756
51757
51758
51759
51760
51761
51762
51763
51764
51765
51766
51767
51768
51769
51770
51771
51772
51773
51774
51775
51776
51777
51778
51779
51780
51781
51782
51783
51784
51785
51786
51787
51788
51789
51790
51791
51792
51793
51794
51795
51796
51797
51798
51799
51800
51801
51802
51803
51804
51805
51806
51807
51808
51809
51810
51811
51812
51813
51814
51815
51816
51817
51818
51819
51820
51821
51822
51823
51824
51825
51826
51827
51828
51829
51830
51831
51832
51833
51834
51835
51836
51837
51838
51839
51840
51841
51842
51843
51844
51845
51846
51847
51848
51849
51850
51851
51852
51853
51854
51855
51856
51857
51858
51859
51860
51861
51862
51863
51864
51865
51866
51867
51868
51869
51870
51871
51872
51873
51874
51875
51876
51877
51878
51879
51880
51881
51882
51883
51884
51885
51886
51887
51888
51889
51890
51891
51892
51893
51894
51895
51896
51897
51898
51899
51900
51901
51902
51903
51904
51905
51906
51907
51908
51909
51910
51911
51912
51913
51914
51915
51916
51917
51918
51919
51920
51921
51922
51923
51924
51925
51926
51927
51928
51929
51930
51931
51932
51933
51934
51935
51936
51937
51938
51939
51940
51941
51942
51943
51944
51945
51946
51947
51948
51949
51950
51951
51952
51953
51954
51955
51956
51957
51958
51959
51960
51961
51962
51963
51964
51965
51966
51967
51968
51969
51970
51971
51972
51973
51974
51975
51976
51977
51978
51979
51980
51981
51982
51983
51984
51985
51986
51987
51988
51989
51990
51991
51992
51993
51994
51995
51996
51997
51998
51999
52000
52001
52002
52003
52004
52005
52006
52007
52008
52009
52010
52011
52012
52013
52014
52015
52016
52017
52018
52019
52020
52021
52022
52023
52024
52025
52026
52027
52028
52029
52030
52031
52032
52033
52034
52035
52036
52037
52038
52039
52040
52041
52042
52043
52044
52045
52046
52047
52048
52049
52050
52051
52052
52053
52054
52055
52056
52057
52058
52059
52060
52061
52062
52063
52064
52065
52066
52067
52068
52069
52070
52071
52072
52073
52074
52075
52076
52077
52078
52079
52080
52081
52082
52083
52084
52085
52086
52087
52088
52089
52090
52091
52092
52093
52094
52095
52096
52097
52098
52099
52100
52101
52102
52103
52104
52105
52106
52107
52108
52109
52110
52111
52112
52113
52114
52115
52116
52117
52118
52119
52120
52121
52122
52123
52124
52125
52126
52127
52128
52129
52130
52131
52132
52133
52134
52135
52136
52137
52138
52139
52140
52141
52142
52143
52144
52145
52146
52147
52148
52149
52150
52151
52152
52153
52154
52155
52156
52157
52158
52159
52160
52161
52162
52163
52164
52165
52166
52167
52168
52169
52170
52171
52172
52173
52174
52175
52176
52177
52178
52179
52180
52181
52182
52183
52184
52185
52186
52187
52188
52189
52190
52191
52192
52193
52194
52195
52196
52197
52198
52199
52200
52201
52202
52203
52204
52205
52206
52207
52208
52209
52210
52211
52212
52213
52214
52215
52216
52217
52218
52219
52220
52221
52222
52223
52224
52225
52226
52227
52228
52229
52230
52231
52232
52233
52234
52235
52236
52237
52238
52239
52240
52241
52242
52243
52244
52245
52246
52247
52248
52249
52250
52251
52252
52253
52254
52255
52256
52257
52258
52259
52260
52261
52262
52263
52264
52265
52266
52267
52268
52269
52270
52271
52272
52273
52274
52275
52276
52277
52278
52279
52280
52281
52282
52283
52284
52285
52286
52287
52288
52289
52290
52291
52292
52293
52294
52295
52296
52297
52298
52299
52300
52301
52302
52303
52304
52305
52306
52307
52308
52309
52310
52311
52312
52313
52314
52315
52316
52317
52318
52319
52320
52321
52322
52323
52324
52325
52326
52327
52328
52329
52330
52331
52332
52333
52334
52335
52336
52337
52338
52339
52340
52341
52342
52343
52344
52345
52346
52347
52348
52349
52350
52351
52352
52353
52354
52355
52356
52357
52358
52359
52360
52361
52362
52363
52364
52365
52366
52367
52368
52369
52370
52371
52372
52373
52374
52375
52376
52377
52378
52379
52380
52381
52382
52383
52384
52385
52386
52387
52388
52389
52390
52391
52392
52393
52394
52395
52396
52397
52398
52399
52400
52401
52402
52403
52404
52405
52406
52407
52408
52409
52410
52411
52412
52413
52414
52415
52416
52417
52418
52419
52420
52421
52422
52423
52424
52425
52426
52427
52428
52429
52430
52431
52432
52433
52434
52435
52436
52437
52438
52439
52440
52441
52442
52443
52444
52445
52446
52447
52448
52449
52450
52451
52452
52453
52454
52455
52456
52457
52458
52459
52460
52461
52462
52463
52464
52465
52466
52467
52468
52469
52470
52471
52472
52473
52474
52475
52476
52477
52478
52479
52480
52481
52482
52483
52484
52485
52486
52487
52488
52489
52490
52491
52492
52493
52494
52495
52496
52497
52498
52499
52500
52501
52502
52503
52504
52505
52506
52507
52508
52509
52510
52511
52512
52513
52514
52515
52516
52517
52518
52519
52520
52521
52522
52523
52524
52525
52526
52527
52528
52529
52530
52531
52532
52533
52534
52535
52536
52537
52538
52539
52540
52541
52542
52543
52544
52545
52546
52547
52548
52549
52550
52551
52552
52553
52554
52555
52556
52557
52558
52559
52560
52561
52562
52563
52564
52565
52566
52567
52568
52569
52570
52571
52572
52573
52574
52575
52576
52577
52578
52579
52580
52581
52582
52583
52584
52585
52586
52587
52588
52589
52590
52591
52592
52593
52594
52595
52596
52597
52598
52599
52600
52601
52602
52603
52604
52605
52606
52607
52608
52609
52610
52611
52612
52613
52614
52615
52616
52617
52618
52619
52620
52621
52622
52623
52624
52625
52626
52627
52628
52629
52630
52631
52632
52633
52634
52635
52636
52637
52638
52639
52640
52641
52642
52643
52644
52645
52646
52647
52648
52649
52650
52651
52652
52653
52654
52655
52656
52657
52658
52659
52660
52661
52662
52663
52664
52665
52666
52667
52668
52669
52670
52671
52672
52673
52674
52675
52676
52677
52678
52679
52680
52681
52682
52683
52684
52685
52686
52687
52688
52689
52690
52691
52692
52693
52694
52695
52696
52697
52698
52699
52700
52701
52702
52703
52704
52705
52706
52707
52708
52709
52710
52711
52712
52713
52714
52715
52716
52717
52718
52719
52720
52721
52722
52723
52724
52725
52726
52727
52728
52729
52730
52731
52732
52733
52734
52735
52736
52737
52738
52739
52740
52741
52742
52743
52744
52745
52746
52747
52748
52749
52750
52751
52752
52753
52754
52755
52756
52757
52758
52759
52760
52761
52762
52763
52764
52765
52766
52767
52768
52769
52770
52771
52772
52773
52774
52775
52776
52777
52778
52779
52780
52781
52782
52783
52784
52785
52786
52787
52788
52789
52790
52791
52792
52793
52794
52795
52796
52797
52798
52799
52800
52801
52802
52803
52804
52805
52806
52807
52808
52809
52810
52811
52812
52813
52814
52815
52816
52817
52818
52819
52820
52821
52822
52823
52824
52825
52826
52827
52828
52829
52830
52831
52832
52833
52834
52835
52836
52837
52838
52839
52840
52841
52842
52843
52844
52845
52846
52847
52848
52849
52850
52851
52852
52853
52854
52855
52856
52857
52858
52859
52860
52861
52862
52863
52864
52865
52866
52867
52868
52869
52870
52871
52872
52873
52874
52875
52876
52877
52878
52879
52880
52881
52882
52883
52884
52885
52886
52887
52888
52889
52890
52891
52892
52893
52894
52895
52896
52897
52898
52899
52900
52901
52902
52903
52904
52905
52906
52907
52908
52909
52910
52911
52912
52913
52914
52915
52916
52917
52918
52919
52920
52921
52922
52923
52924
52925
52926
52927
52928
52929
52930
52931
52932
52933
52934
52935
52936
52937
52938
52939
52940
52941
52942
52943
52944
52945
52946
52947
52948
52949
52950
52951
52952
52953
52954
52955
52956
52957
52958
52959
52960
52961
52962
52963
52964
52965
52966
52967
52968
52969
52970
52971
52972
52973
52974
52975
52976
52977
52978
52979
52980
52981
52982
52983
52984
52985
52986
52987
52988
52989
52990
52991
52992
52993
52994
52995
52996
52997
52998
52999
53000
53001
53002
53003
53004
53005
53006
53007
53008
53009
53010
53011
53012
53013
53014
53015
53016
53017
53018
53019
53020
53021
53022
53023
53024
53025
53026
53027
53028
53029
53030
53031
53032
53033
53034
53035
53036
53037
53038
53039
53040
53041
53042
53043
53044
53045
53046
53047
53048
53049
53050
53051
53052
53053
53054
53055
53056
53057
53058
53059
53060
53061
53062
53063
53064
53065
53066
53067
53068
53069
53070
53071
53072
53073
53074
53075
53076
53077
53078
53079
53080
53081
53082
53083
53084
53085
53086
53087
53088
53089
53090
53091
53092
53093
53094
53095
53096
53097
53098
53099
53100
53101
53102
53103
53104
53105
53106
53107
53108
53109
53110
53111
53112
53113
53114
53115
53116
53117
53118
53119
53120
53121
53122
53123
53124
53125
53126
53127
53128
53129
53130
53131
53132
53133
53134
53135
53136
53137
53138
53139
53140
53141
53142
53143
53144
53145
53146
53147
53148
53149
53150
53151
53152
53153
53154
53155
53156
53157
53158
53159
53160
53161
53162
53163
53164
53165
53166
53167
53168
53169
53170
53171
53172
53173
53174
53175
53176
53177
53178
53179
53180
53181
53182
53183
53184
53185
53186
53187
53188
53189
53190
53191
53192
53193
53194
53195
53196
53197
53198
53199
53200
53201
53202
53203
53204
53205
53206
53207
53208
53209
53210
53211
53212
53213
53214
53215
53216
53217
53218
53219
53220
53221
53222
53223
53224
53225
53226
53227
53228
53229
53230
53231
53232
53233
53234
53235
53236
53237
53238
53239
53240
53241
53242
53243
53244
53245
53246
53247
53248
53249
53250
53251
53252
53253
53254
53255
53256
53257
53258
53259
53260
53261
53262
53263
53264
53265
53266
53267
53268
53269
53270
53271
53272
53273
53274
53275
53276
53277
53278
53279
53280
53281
53282
53283
53284
53285
53286
53287
53288
53289
53290
53291
53292
53293
53294
53295
53296
53297
53298
53299
53300
53301
53302
53303
53304
53305
53306
53307
53308
53309
53310
53311
53312
53313
53314
53315
53316
53317
53318
53319
53320
53321
53322
53323
53324
53325
53326
53327
53328
53329
53330
53331
53332
53333
53334
53335
53336
53337
53338
53339
53340
53341
53342
53343
53344
53345
53346
53347
53348
53349
53350
53351
53352
53353
53354
53355
53356
53357
53358
53359
53360
53361
53362
53363
53364
53365
53366
53367
53368
53369
53370
53371
53372
53373
53374
53375
53376
53377
53378
53379
53380
53381
53382
53383
53384
53385
53386
53387
53388
53389
53390
53391
53392
53393
53394
53395
53396
53397
53398
53399
53400
53401
53402
53403
53404
53405
53406
53407
53408
53409
53410
53411
53412
53413
53414
53415
53416
53417
53418
53419
53420
53421
53422
53423
53424
53425
53426
53427
53428
53429
53430
53431
53432
53433
53434
53435
53436
53437
53438
53439
53440
53441
53442
53443
53444
53445
53446
53447
53448
53449
53450
53451
53452
53453
53454
53455
53456
53457
53458
53459
53460
53461
53462
53463
53464
53465
53466
53467
53468
53469
53470
53471
53472
53473
53474
53475
53476
53477
53478
53479
53480
53481
53482
53483
53484
53485
53486
53487
53488
53489
53490
53491
53492
53493
53494
53495
53496
53497
53498
53499
53500
53501
53502
53503
53504
53505
53506
53507
53508
53509
53510
53511
53512
53513
53514
53515
53516
53517
53518
53519
53520
53521
53522
53523
53524
53525
53526
53527
53528
53529
53530
53531
53532
53533
53534
53535
53536
53537
53538
53539
53540
53541
53542
53543
53544
53545
53546
53547
53548
53549
53550
53551
53552
53553
53554
53555
53556
53557
53558
53559
53560
53561
53562
53563
53564
53565
53566
53567
53568
53569
53570
53571
53572
53573
53574
53575
53576
53577
53578
53579
53580
53581
53582
53583
53584
53585
53586
53587
53588
53589
53590
53591
53592
53593
53594
53595
53596
53597
53598
53599
53600
53601
53602
53603
53604
53605
53606
53607
53608
53609
53610
53611
53612
53613
53614
53615
53616
53617
53618
53619
53620
53621
53622
53623
53624
53625
53626
53627
53628
53629
53630
53631
53632
53633
53634
53635
53636
53637
53638
53639
53640
53641
53642
53643
53644
53645
53646
53647
53648
53649
53650
53651
53652
53653
53654
53655
53656
53657
53658
53659
53660
53661
53662
53663
53664
53665
53666
53667
53668
53669
53670
53671
53672
53673
53674
53675
53676
53677
53678
53679
53680
53681
53682
53683
53684
53685
53686
53687
53688
53689
53690
53691
53692
53693
53694
53695
53696
53697
53698
53699
53700
53701
53702
53703
53704
53705
53706
53707
53708
53709
53710
53711
53712
53713
53714
53715
53716
53717
53718
53719
53720
53721
53722
53723
53724
53725
53726
53727
53728
53729
53730
53731
53732
53733
53734
53735
53736
53737
53738
53739
53740
53741
53742
53743
53744
53745
53746
53747
53748
53749
53750
53751
53752
53753
53754
53755
53756
53757
53758
53759
53760
53761
53762
53763
53764
53765
53766
53767
53768
53769
53770
53771
53772
53773
53774
53775
53776
53777
53778
53779
53780
53781
53782
53783
53784
53785
53786
53787
53788
53789
53790
53791
53792
53793
53794
53795
53796
53797
53798
53799
53800
53801
53802
53803
53804
53805
53806
53807
53808
53809
53810
53811
53812
53813
53814
53815
53816
53817
53818
53819
53820
53821
53822
53823
53824
53825
53826
53827
53828
53829
53830
53831
53832
53833
53834
53835
53836
53837
53838
53839
53840
53841
53842
53843
53844
53845
53846
53847
53848
53849
53850
53851
53852
53853
53854
53855
53856
53857
53858
53859
53860
53861
53862
53863
53864
53865
53866
53867
53868
53869
53870
53871
53872
53873
53874
53875
53876
53877
53878
53879
53880
53881
53882
53883
53884
53885
53886
53887
53888
53889
53890
53891
53892
53893
53894
53895
53896
53897
53898
53899
53900
53901
53902
53903
53904
53905
53906
53907
53908
53909
53910
53911
53912
53913
53914
53915
53916
53917
53918
53919
53920
53921
53922
53923
53924
53925
53926
53927
53928
53929
53930
53931
53932
53933
53934
53935
53936
53937
53938
53939
53940
53941
53942
53943
53944
53945
53946
53947
53948
53949
53950
53951
53952
53953
53954
53955
53956
53957
53958
53959
53960
53961
53962
53963
53964
53965
53966
53967
53968
53969
53970
53971
53972
53973
53974
53975
53976
53977
53978
53979
53980
53981
53982
53983
53984
53985
53986
53987
53988
53989
53990
53991
53992
53993
53994
53995
53996
53997
53998
53999
54000
54001
54002
54003
54004
54005
54006
54007
54008
54009
54010
54011
54012
54013
54014
54015
54016
54017
54018
54019
54020
54021
54022
54023
54024
54025
54026
54027
54028
54029
54030
54031
54032
54033
54034
54035
54036
54037
54038
54039
54040
54041
54042
54043
54044
54045
54046
54047
54048
54049
54050
54051
54052
54053
54054
54055
54056
54057
54058
54059
54060
54061
54062
54063
54064
54065
54066
54067
54068
54069
54070
54071
54072
54073
54074
54075
54076
54077
54078
54079
54080
54081
54082
54083
54084
54085
54086
54087
54088
54089
54090
54091
54092
54093
54094
54095
54096
54097
54098
54099
54100
54101
54102
54103
54104
54105
54106
54107
54108
54109
54110
54111
54112
54113
54114
54115
54116
54117
54118
54119
54120
54121
54122
54123
54124
54125
54126
54127
54128
54129
54130
54131
54132
54133
54134
54135
54136
54137
54138
54139
54140
54141
54142
54143
54144
54145
54146
54147
54148
54149
54150
54151
54152
54153
54154
54155
54156
54157
54158
54159
54160
54161
54162
54163
54164
54165
54166
54167
54168
54169
54170
54171
54172
54173
54174
54175
54176
54177
54178
54179
54180
54181
54182
54183
54184
54185
54186
54187
54188
54189
54190
54191
54192
54193
54194
54195
54196
54197
54198
54199
54200
54201
54202
54203
54204
54205
54206
54207
54208
54209
54210
54211
54212
54213
54214
54215
54216
54217
54218
54219
54220
54221
54222
54223
54224
54225
54226
54227
54228
54229
54230
54231
54232
54233
54234
54235
54236
54237
54238
54239
54240
54241
54242
54243
54244
54245
54246
54247
54248
54249
54250
54251
54252
54253
54254
54255
54256
54257
54258
54259
54260
54261
54262
54263
54264
54265
54266
54267
54268
54269
54270
54271
54272
54273
54274
54275
54276
54277
54278
54279
54280
54281
54282
54283
54284
54285
54286
54287
54288
54289
54290
54291
54292
54293
54294
54295
54296
54297
54298
54299
54300
54301
54302
54303
54304
54305
54306
54307
54308
54309
54310
54311
54312
54313
54314
54315
54316
54317
54318
54319
54320
54321
54322
54323
54324
54325
54326
54327
54328
54329
54330
54331
54332
54333
54334
54335
54336
54337
54338
54339
54340
54341
54342
54343
54344
54345
54346
54347
54348
54349
54350
54351
54352
54353
54354
54355
54356
54357
54358
54359
54360
54361
54362
54363
54364
54365
54366
54367
54368
54369
54370
54371
54372
54373
54374
54375
54376
54377
54378
54379
54380
54381
54382
54383
54384
54385
54386
54387
54388
54389
54390
54391
54392
54393
54394
54395
54396
54397
54398
54399
54400
54401
54402
54403
54404
54405
54406
54407
54408
54409
54410
54411
54412
54413
54414
54415
54416
54417
54418
54419
54420
54421
54422
54423
54424
54425
54426
54427
54428
54429
54430
54431
54432
54433
54434
54435
54436
54437
54438
54439
54440
54441
54442
54443
54444
54445
54446
54447
54448
54449
54450
54451
54452
54453
54454
54455
54456
54457
54458
54459
54460
54461
54462
54463
54464
54465
54466
54467
54468
54469
54470
54471
54472
54473
54474
54475
54476
54477
54478
54479
54480
54481
54482
54483
54484
54485
54486
54487
54488
54489
54490
54491
54492
54493
54494
54495
54496
54497
54498
54499
54500
54501
54502
54503
54504
54505
54506
54507
54508
54509
54510
54511
54512
54513
54514
54515
54516
54517
54518
54519
54520
54521
54522
54523
54524
54525
54526
54527
54528
54529
54530
54531
54532
54533
54534
54535
54536
54537
54538
54539
54540
54541
54542
54543
54544
54545
54546
54547
54548
54549
54550
54551
54552
54553
54554
54555
54556
54557
54558
54559
54560
54561
54562
54563
54564
54565
54566
54567
54568
54569
54570
54571
54572
54573
54574
54575
54576
54577
54578
54579
54580
54581
54582
54583
54584
54585
54586
54587
54588
54589
54590
54591
54592
54593
54594
54595
54596
54597
54598
54599
54600
54601
54602
54603
54604
54605
54606
54607
54608
54609
54610
54611
54612
54613
54614
54615
54616
54617
54618
54619
54620
54621
54622
54623
54624
54625
54626
54627
54628
54629
54630
54631
54632
54633
54634
54635
54636
54637
54638
54639
54640
54641
54642
54643
54644
54645
54646
54647
54648
54649
54650
54651
54652
54653
54654
54655
54656
54657
54658
54659
54660
54661
54662
54663
54664
54665
54666
54667
54668
54669
54670
54671
54672
54673
54674
54675
54676
54677
54678
54679
54680
54681
54682
54683
54684
54685
54686
54687
54688
54689
54690
54691
54692
54693
54694
54695
54696
54697
54698
54699
54700
54701
54702
54703
54704
54705
54706
54707
54708
54709
54710
54711
54712
54713
54714
54715
54716
54717
54718
54719
54720
54721
54722
54723
54724
54725
54726
54727
54728
54729
54730
54731
54732
54733
54734
54735
54736
54737
54738
54739
54740
54741
54742
54743
54744
54745
54746
54747
54748
54749
54750
54751
54752
54753
54754
54755
54756
54757
54758
54759
54760
54761
54762
54763
54764
54765
54766
54767
54768
54769
54770
54771
54772
54773
54774
54775
54776
54777
54778
54779
54780
54781
54782
54783
54784
54785
54786
54787
54788
54789
54790
54791
54792
54793
54794
54795
54796
54797
54798
54799
54800
54801
54802
54803
54804
54805
54806
54807
54808
54809
54810
54811
54812
54813
54814
54815
54816
54817
54818
54819
54820
54821
54822
54823
54824
54825
54826
54827
54828
54829
54830
54831
54832
54833
54834
54835
54836
54837
54838
54839
54840
54841
54842
54843
54844
54845
54846
54847
54848
54849
54850
54851
54852
54853
54854
54855
54856
54857
54858
54859
54860
54861
54862
54863
54864
54865
54866
54867
54868
54869
54870
54871
54872
54873
54874
54875
54876
54877
54878
54879
54880
54881
54882
54883
54884
54885
54886
54887
54888
54889
54890
54891
54892
54893
54894
54895
54896
54897
54898
54899
54900
54901
54902
54903
54904
54905
54906
54907
54908
54909
54910
54911
54912
54913
54914
54915
54916
54917
54918
54919
54920
54921
54922
54923
54924
54925
54926
54927
54928
54929
54930
54931
54932
54933
54934
54935
54936
54937
54938
54939
54940
54941
54942
54943
54944
54945
54946
54947
54948
54949
54950
54951
54952
54953
54954
54955
54956
54957
54958
54959
54960
54961
54962
54963
54964
54965
54966
54967
54968
54969
54970
54971
54972
54973
54974
54975
54976
54977
54978
54979
54980
54981
54982
54983
54984
54985
54986
54987
54988
54989
54990
54991
54992
54993
54994
54995
54996
54997
54998
54999
55000
55001
55002
55003
55004
55005
55006
55007
55008
55009
55010
55011
55012
55013
55014
55015
55016
55017
55018
55019
55020
55021
55022
55023
55024
55025
55026
55027
55028
55029
55030
55031
55032
55033
55034
55035
55036
55037
55038
55039
55040
55041
55042
55043
55044
55045
55046
55047
55048
55049
55050
55051
55052
55053
55054
55055
55056
55057
55058
55059
55060
55061
55062
55063
55064
55065
55066
55067
55068
55069
55070
55071
55072
55073
55074
55075
55076
55077
55078
55079
55080
55081
55082
55083
55084
55085
55086
55087
55088
55089
55090
55091
55092
55093
55094
55095
55096
55097
55098
55099
55100
55101
55102
55103
55104
55105
55106
55107
55108
55109
55110
55111
55112
55113
55114
55115
55116
55117
55118
55119
55120
55121
55122
55123
55124
55125
55126
55127
55128
55129
55130
55131
55132
55133
55134
55135
55136
55137
55138
55139
55140
55141
55142
55143
55144
55145
55146
55147
55148
55149
55150
55151
55152
55153
55154
55155
55156
55157
55158
55159
55160
55161
55162
55163
55164
55165
55166
55167
55168
55169
55170
55171
55172
55173
55174
55175
55176
55177
55178
55179
55180
55181
55182
55183
55184
55185
55186
55187
55188
55189
55190
55191
55192
55193
55194
55195
55196
55197
55198
55199
55200
55201
55202
55203
55204
55205
55206
55207
55208
55209
55210
55211
55212
55213
55214
55215
55216
55217
55218
55219
55220
55221
55222
55223
55224
55225
55226
55227
55228
55229
55230
55231
55232
55233
55234
55235
55236
55237
55238
55239
55240
55241
55242
55243
55244
55245
55246
55247
55248
55249
55250
55251
55252
55253
55254
55255
55256
55257
55258
55259
55260
55261
55262
55263
55264
55265
55266
55267
55268
55269
55270
55271
55272
55273
55274
55275
55276
55277
55278
55279
55280
55281
55282
55283
55284
55285
55286
55287
55288
55289
55290
55291
55292
55293
55294
55295
55296
55297
55298
55299
55300
55301
55302
55303
55304
55305
55306
55307
55308
55309
55310
55311
55312
55313
55314
55315
55316
55317
55318
55319
55320
55321
55322
55323
55324
55325
55326
55327
55328
55329
55330
55331
55332
55333
55334
55335
55336
55337
55338
55339
55340
55341
55342
55343
55344
55345
55346
55347
55348
55349
55350
55351
55352
55353
55354
55355
55356
55357
55358
55359
55360
55361
55362
55363
55364
55365
55366
55367
55368
55369
55370
55371
55372
55373
55374
55375
55376
55377
55378
55379
55380
55381
55382
55383
55384
55385
55386
55387
55388
55389
55390
55391
55392
55393
55394
55395
55396
55397
55398
55399
55400
55401
55402
55403
55404
55405
55406
55407
55408
55409
55410
55411
55412
55413
55414
55415
55416
55417
55418
55419
55420
55421
55422
55423
55424
55425
55426
55427
55428
55429
55430
55431
55432
55433
55434
55435
55436
55437
55438
55439
55440
55441
55442
55443
55444
55445
55446
55447
55448
55449
55450
55451
55452
55453
55454
55455
55456
55457
55458
55459
55460
55461
55462
55463
55464
55465
55466
55467
55468
55469
55470
55471
55472
55473
55474
55475
55476
55477
55478
55479
55480
55481
55482
55483
55484
55485
55486
55487
55488
55489
55490
55491
55492
55493
55494
55495
55496
55497
55498
55499
55500
55501
55502
55503
55504
55505
55506
55507
55508
55509
55510
55511
55512
55513
55514
55515
55516
55517
55518
55519
55520
55521
55522
55523
55524
55525
55526
55527
55528
55529
55530
55531
55532
55533
55534
55535
55536
55537
55538
55539
55540
55541
55542
55543
55544
55545
55546
55547
55548
55549
55550
55551
55552
55553
55554
55555
55556
55557
55558
55559
55560
55561
55562
55563
55564
55565
55566
55567
55568
55569
55570
55571
55572
55573
55574
55575
55576
55577
55578
55579
55580
55581
55582
55583
55584
55585
55586
55587
55588
55589
55590
55591
55592
55593
55594
55595
55596
55597
55598
55599
55600
55601
55602
55603
55604
55605
55606
55607
55608
55609
55610
55611
55612
55613
55614
55615
55616
55617
55618
55619
55620
55621
55622
55623
55624
55625
55626
55627
55628
55629
55630
55631
55632
55633
55634
55635
55636
55637
55638
55639
55640
55641
55642
55643
55644
55645
55646
55647
55648
55649
55650
55651
55652
55653
55654
55655
55656
55657
55658
55659
55660
55661
55662
55663
55664
55665
55666
55667
55668
55669
55670
55671
55672
55673
55674
55675
55676
55677
55678
55679
55680
55681
55682
55683
55684
55685
55686
55687
55688
55689
55690
55691
55692
55693
55694
55695
55696
55697
55698
55699
55700
55701
55702
55703
55704
55705
55706
55707
55708
55709
55710
55711
55712
55713
55714
55715
55716
55717
55718
55719
55720
55721
55722
55723
55724
55725
55726
55727
55728
55729
55730
55731
55732
55733
55734
55735
55736
55737
55738
55739
55740
55741
55742
55743
55744
55745
55746
55747
55748
55749
55750
55751
55752
55753
55754
55755
55756
55757
55758
55759
55760
55761
55762
55763
55764
55765
55766
55767
55768
55769
55770
55771
55772
55773
55774
55775
55776
55777
55778
55779
55780
55781
55782
55783
55784
55785
55786
55787
55788
55789
55790
55791
55792
55793
55794
55795
55796
55797
55798
55799
55800
55801
55802
55803
55804
55805
55806
55807
55808
55809
55810
55811
55812
55813
55814
55815
55816
55817
55818
55819
55820
55821
55822
55823
55824
55825
55826
55827
55828
55829
55830
55831
55832
55833
55834
55835
55836
55837
55838
55839
55840
55841
55842
55843
55844
55845
55846
55847
55848
55849
55850
55851
55852
55853
55854
55855
55856
55857
55858
55859
55860
55861
55862
55863
55864
55865
55866
55867
55868
55869
55870
55871
55872
55873
55874
55875
55876
55877
55878
55879
55880
55881
55882
55883
55884
55885
55886
55887
55888
55889
55890
55891
55892
55893
55894
55895
55896
55897
55898
55899
55900
55901
55902
55903
55904
55905
55906
55907
55908
55909
55910
55911
55912
55913
55914
55915
55916
55917
55918
55919
55920
55921
55922
55923
55924
55925
55926
55927
55928
55929
55930
55931
55932
55933
55934
55935
55936
55937
55938
55939
55940
55941
55942
55943
55944
55945
55946
55947
55948
55949
55950
55951
55952
55953
55954
55955
55956
55957
55958
55959
55960
55961
55962
55963
55964
55965
55966
55967
55968
55969
55970
55971
55972
55973
55974
55975
55976
55977
55978
55979
55980
55981
55982
55983
55984
55985
55986
55987
55988
55989
55990
55991
55992
55993
55994
55995
55996
55997
55998
55999
56000
56001
56002
56003
56004
56005
56006
56007
56008
56009
56010
56011
56012
56013
56014
56015
56016
56017
56018
56019
56020
56021
56022
56023
56024
56025
56026
56027
56028
56029
56030
56031
56032
56033
56034
56035
56036
56037
56038
56039
56040
56041
56042
56043
56044
56045
56046
56047
56048
56049
56050
56051
56052
56053
56054
56055
56056
56057
56058
56059
56060
56061
56062
56063
56064
56065
56066
56067
56068
56069
56070
56071
56072
56073
56074
56075
56076
56077
56078
56079
56080
56081
56082
56083
56084
56085
56086
56087
56088
56089
56090
56091
56092
56093
56094
56095
56096
56097
56098
56099
56100
56101
56102
56103
56104
56105
56106
56107
56108
56109
56110
56111
56112
56113
56114
56115
56116
56117
56118
56119
56120
56121
56122
56123
56124
56125
56126
56127
56128
56129
56130
56131
56132
56133
56134
56135
56136
56137
56138
56139
56140
56141
56142
56143
56144
56145
56146
56147
56148
56149
56150
56151
56152
56153
56154
56155
56156
56157
56158
56159
56160
56161
56162
56163
56164
56165
56166
56167
56168
56169
56170
56171
56172
56173
56174
56175
56176
56177
56178
56179
56180
56181
56182
56183
56184
56185
56186
56187
56188
56189
56190
56191
56192
56193
56194
56195
56196
56197
56198
56199
56200
56201
56202
56203
56204
56205
56206
56207
56208
56209
56210
56211
56212
56213
56214
56215
56216
56217
56218
56219
56220
56221
56222
56223
56224
56225
56226
56227
56228
56229
56230
56231
56232
56233
56234
56235
56236
56237
56238
56239
56240
56241
56242
56243
56244
56245
56246
56247
56248
56249
56250
56251
56252
56253
56254
56255
56256
56257
56258
56259
56260
56261
56262
56263
56264
56265
56266
56267
56268
56269
56270
56271
56272
56273
56274
56275
56276
56277
56278
56279
56280
56281
56282
56283
56284
56285
56286
56287
56288
56289
56290
56291
56292
56293
56294
56295
56296
56297
56298
56299
56300
56301
56302
56303
56304
56305
56306
56307
56308
56309
56310
56311
56312
56313
56314
56315
56316
56317
56318
56319
56320
56321
56322
56323
56324
56325
56326
56327
56328
56329
56330
56331
56332
56333
56334
56335
56336
56337
56338
56339
56340
56341
56342
56343
56344
56345
56346
56347
56348
56349
56350
56351
56352
56353
56354
56355
56356
56357
56358
56359
56360
56361
56362
56363
56364
56365
56366
56367
56368
56369
56370
56371
56372
56373
56374
56375
56376
56377
56378
56379
56380
56381
56382
56383
56384
56385
56386
56387
56388
56389
56390
56391
56392
56393
56394
56395
56396
56397
56398
56399
56400
56401
56402
56403
56404
56405
56406
56407
56408
56409
56410
56411
56412
56413
56414
56415
56416
56417
56418
56419
56420
56421
56422
56423
56424
56425
56426
56427
56428
56429
56430
56431
56432
56433
56434
56435
56436
56437
56438
56439
56440
56441
56442
56443
56444
56445
56446
56447
56448
56449
56450
56451
56452
56453
56454
56455
56456
56457
56458
56459
56460
56461
56462
56463
56464
56465
56466
56467
56468
56469
56470
56471
56472
56473
56474
56475
56476
56477
56478
56479
56480
56481
56482
56483
56484
56485
56486
56487
56488
56489
56490
56491
56492
56493
56494
56495
56496
56497
56498
56499
56500
56501
56502
56503
56504
56505
56506
56507
56508
56509
56510
56511
56512
56513
56514
56515
56516
56517
56518
56519
56520
56521
56522
56523
56524
56525
56526
56527
56528
56529
56530
56531
56532
56533
56534
56535
56536
56537
56538
56539
56540
56541
56542
56543
56544
56545
56546
56547
56548
56549
56550
56551
56552
56553
56554
56555
56556
56557
56558
56559
56560
56561
56562
56563
56564
56565
56566
56567
56568
56569
56570
56571
56572
56573
56574
56575
56576
56577
56578
56579
56580
56581
56582
56583
56584
56585
56586
56587
56588
56589
56590
56591
56592
56593
56594
56595
56596
56597
56598
56599
56600
56601
56602
56603
56604
56605
56606
56607
56608
56609
56610
56611
56612
56613
56614
56615
56616
56617
56618
56619
56620
56621
56622
56623
56624
56625
56626
56627
56628
56629
56630
56631
56632
56633
56634
56635
56636
56637
56638
56639
56640
56641
56642
56643
56644
56645
56646
56647
56648
56649
56650
56651
56652
56653
56654
56655
56656
56657
56658
56659
56660
56661
56662
56663
56664
56665
56666
56667
56668
56669
56670
56671
56672
56673
56674
56675
56676
56677
56678
56679
56680
56681
56682
56683
56684
56685
56686
56687
56688
56689
56690
56691
56692
56693
56694
56695
56696
56697
56698
56699
56700
56701
56702
56703
56704
56705
56706
56707
56708
56709
56710
56711
56712
56713
56714
56715
56716
56717
56718
56719
56720
56721
56722
56723
56724
56725
56726
56727
56728
56729
56730
56731
56732
56733
56734
56735
56736
56737
56738
56739
56740
56741
56742
56743
56744
56745
56746
56747
56748
56749
56750
56751
56752
56753
56754
56755
56756
56757
56758
56759
56760
56761
56762
56763
56764
56765
56766
56767
56768
56769
56770
56771
56772
56773
56774
56775
56776
56777
56778
56779
56780
56781
56782
56783
56784
56785
56786
56787
56788
56789
56790
56791
56792
56793
56794
56795
56796
56797
56798
56799
56800
56801
56802
56803
56804
56805
56806
56807
56808
56809
56810
56811
56812
56813
56814
56815
56816
56817
56818
56819
56820
56821
56822
56823
56824
56825
56826
56827
56828
56829
56830
56831
56832
56833
56834
56835
56836
56837
56838
56839
56840
56841
56842
56843
56844
56845
56846
56847
56848
56849
56850
56851
56852
56853
--- linux/Makefile-otgorig	2006-09-20 16:02:57.347146612 +0200
+++ linux/Makefile	2006-09-20 16:03:35.280797278 +0200
@@ -181,6 +181,7 @@
 DRIVERS-$(CONFIG_HAMRADIO) += drivers/net/hamradio/hamradio.o
 DRIVERS-$(CONFIG_TC) += drivers/tc/tc.a
 DRIVERS-$(CONFIG_USB) += drivers/usb/usbdrv.o
+DRIVERS-$(CONFIG_OTG) += drivers/otg/otg_drv.o
 DRIVERS-$(CONFIG_USB_GADGET) += drivers/usb/gadget/built-in.o
 DRIVERS-y +=drivers/media/media.o
 DRIVERS-$(CONFIG_INPUT) += drivers/input/inputdrv.o
--- linux/arch/mips/config-shared.in-otgorig	2006-09-20 16:09:20.671834564 +0200
+++ linux/arch/mips/config-shared.in	2006-09-20 16:09:28.068156725 +0200
@@ -1009,6 +1009,7 @@
 endmenu
 
 source drivers/usb/Config.in
+source drivers/otg/Config.in
 
 source net/bluetooth/Config.in
 
--- linux/drivers/Makefile-otgorig	2006-09-20 16:09:47.100985766 +0200
+++ linux/drivers/Makefile	2006-09-20 16:10:25.730668535 +0200
@@ -6,7 +6,7 @@
 #
 
 
-mod-subdirs :=	dio hil mtd sbus video macintosh usb input telephony ide \
+mod-subdirs :=	dio hil mtd sbus video macintosh usb otg input telephony ide \
 		message/i2o message/fusion scsi md ieee1394 pnp isdn atm \
 		fc4 net/hamradio i2c acpi bluetooth usb/gadget sensors
 
@@ -28,6 +28,7 @@
 subdir-$(CONFIG_MAC)		+= macintosh
 subdir-$(CONFIG_PPC32)		+= macintosh
 subdir-$(CONFIG_USB)		+= usb
+subdir-$(CONFIG_OTG)		+= otg
 subdir-$(CONFIG_USB_GADGET)	+= usb/gadget
 subdir-$(CONFIG_INPUT)		+= input
 subdir-$(CONFIG_PHONE)		+= telephony
diff -uNr linux/drivers/no-otg/Config.in linux/drivers/otg/Config.in
--- linux/drivers/no-otg/Config.in	1970-01-01 01:00:00.000000000 +0100
+++ linux/drivers/otg/Config.in	2006-09-01 21:41:25.000000000 +0200
@@ -0,0 +1,103 @@
+#
+# USBOTG - Top level configuration of a USB Peripheral or USB On-The-Go Peripheral Device
+#
+# Copyright (c) 2004 Belcarra
+#
+#
+
+mainmenu_option next_comment
+
+    #
+    # Enable/Disable OTG Support
+    #
+    comment 'On-The-Go and USB Peripheral Support'
+    tristate 'Support for On-The-Go and USB Peripherals ' CONFIG_OTG
+
+    if [ "$CONFIG_OTG" != "n" ]; then
+        #
+        # Select appropriate hardware platform, each config file
+        # will only offer options if the kernel is configured for
+        # that specific architecture or platform. They should define
+        # the following to enable the further configuration in this
+        # file:
+        #       CONFIG_OTG_PLATFORM_OTG     offer OTG configuration
+        #
+        #       CONFIG_OTG_PLATFORM_USBD    offer USBD configuration and
+        #                               function selection
+        #       CONFIG_OTG_PLATFORM_HOST    offer HOST configuration
+        #
+        mainmenu_option next_comment
+            comment 'Select Hardware'
+        
+            # platform oriented configurations
+#            source drivers/otg/config/Config.in-mainstone
+#            source drivers/otg/config/Config.in-pcs-b780
+#            source drivers/otg/config/Config.in-pcs-p1
+#            source drivers/otg/config/Config.in-mx2ads
+#            source drivers/otg/config/Config.in-omap-h2
+            source drivers/otg/config/Config.in-db1550
+#            source drivers/otg/config/Config.in-mordor
+            
+            # architecture specific configurations
+            source drivers/otg/config/Config.in-au1x00
+#            source drivers/otg/config/Config.in-dbmx1
+#            source drivers/otg/config/Config.in-lh7a400
+#            source drivers/otg/config/Config.in-lubbock
+#            source drivers/otg/config/Config.in-smdk2500
+#            source drivers/otg/config/Config.in-strongarm
+#            source drivers/otg/config/Config.in-superh
+
+            # generic drivers
+            source drivers/otg/config/Config.in-isp1301
+	    source drivers/otg/config/Config.in-max3353e
+        endmenu
+
+        if [ "$CONFIG_OTG_PLATFORM_OTG" = "y" -o "$CONFIG_OTG_PLATFORM_USBD" = "y" ]; then
+
+            #
+            # Generic Options
+            #
+            mainmenu_option next_comment
+                comment      'General Support Options'
+                bool         'Enable High Speed Descriptors' CONFIG_OTG_HIGH_SPEED
+    #           bool         'Enable Root HUB Function' CONFIG_OTG_ROOT_HUB
+                bool         'OTG Fast Tracing' CONFIG_OTG_TRACE
+                tristate 'USB FUNCTION FS Module' CONFIG_OTG_PROCFSM 
+                bool         'Disable C99 initializers' CONFIG_OTG_NOC99
+            endmenu
+        
+            #
+            # Select USB Peripheral Function Drivers
+            #
+            mainmenu_option next_comment
+                comment      'Targeted Peripherals (USB Peripheral Function Drivers)'
+                source drivers/otg/functions/acm/Config.in
+                source drivers/otg/functions/mouse/Config.in
+                source drivers/otg/functions/network/Config.in
+                source drivers/otg/functions/msc/Config.in
+#                source drivers/otg/functions/test/Config.in
+            endmenu
+
+            mainmenu_option next_comment
+                comment      'Traditional Device Options'
+                bool         'Built-in Minimal USB Device' CONFIG_OTG_FW_MN
+                dep_bool         'Enable Auto-Start' CONFIG_OTG_TR_AUTO $CONFIG_OTG_MN
+            endmenu
+        fi
+        #dep_tristate 'Build OTG minihost core' CONFIG_OTG_HOSTCORE $CONFIG_OTG
+        if [ "$CONFIG_OTG_PLATFORM_HOST" != "n" ]; then
+            #
+            # Host configuration
+            #
+            mainmenu_option next_comment
+                comment      'Host configuration (OTG minihost core and HCD)'
+#                source drivers/otg/core/Config.in
+                source drivers/otg/ocd/Config.in
+#                source drivers/otg/classes/usblan/Config.in
+            endmenu
+        fi
+
+    fi
+
+endmenu
+
diff -uNr linux/drivers/no-otg/Config.in-orig linux/drivers/otg/Config.in-orig
--- linux/drivers/no-otg/Config.in-orig	1970-01-01 01:00:00.000000000 +0100
+++ linux/drivers/otg/Config.in-orig	2006-09-01 21:41:25.000000000 +0200
@@ -0,0 +1,73 @@
+#
+# OTG - configuration of a USB On-The-Go Device
+#
+# Copyright (c) 2004 Belcarra
+#
+# The otg_export script will delete all comments marked "(Testing)"
+#
+
+mainmenu_option next_comment
+
+comment 'OTG devices'
+
+tristate 'Support for USB On-The-Go Devices ' CONFIG_OTG
+
+if [ "$CONFIG_OTG" = "y" -o "$CONFIG_OTG" = "m" ]; then
+   comment ''
+   bool   '   Enable High Speed Descriptors' CONFIG_OTG_HIGH_SPEED
+   bool   '   Enable Root HUB Function' CONFIG_OTG_ROOT_HUB
+   comment ''
+
+   bool   '   Disable PCD' CONFIG_OTG_DISABLE_PCD
+   bool   '   Disable HCD' CONFIG_OTG_DISABLE_HCD
+
+   comment ''
+
+   bool   '  OTG Proc FS' CONFIG_OTG_PROCFS
+   tristate   '  OTG Proc FS Module' CONFIG_OTG_PROCFSM $CONFIG_OTG
+
+   comment 'On-The-Go Functions'
+
+   source drivers/otg/functions/network/Config.in
+   source drivers/otg/functions/acm/Config.in
+   source drivers/otg/functions/msc/Config.in
+
+   source drivers/otg/functions/isotest/Config.in
+   source drivers/otg/functions/mouse/Config.in
+
+   comment ''
+   comment 'On-The-Go Transceiver Controller Drivers (TCD)'
+
+   source drivers/otg/tcd/isp1301/Config.in
+
+   comment ''
+   comment 'On-The-Go Peripheral Controller Drivers (PCD)'
+
+   source drivers/otg/pcd/au1x00/Config.in
+   source drivers/otg/pcd/mx1/Config.in
+   source drivers/otg/pcd/mx2/Config.in
+   source drivers/otg/pcd/pxa/Config.in
+   source drivers/otg/pcd/sa1100/Config.in
+   source drivers/otg/pcd/lh7a400/Config.in
+   source drivers/otg/pcd/omap/Config.in
+   source drivers/otg/pcd/smdk2500/Config.in
+   source drivers/otg/pcd/superh/Config.in
+   source drivers/otg/pcd/sx2/Config.in
+   source drivers/otg/pcd/wmmx/Config.in
+
+   comment ''
+   comment 'On-The-Go Host Controller Drivers (HCD)'
+
+   source drivers/otg/hcd/omap/Config.in
+
+   comment ''
+   tristate '  OTG Fast Tracing' CONFIG_OTG_TRACE
+   comment ''
+
+   comment 'Non Current Bus Drivers (Testing)'
+   source drivers/otg/functions/pst/Config.in
+   source drivers/otg/functions/datalog/Config.in
+
+fi
+
+endmenu
diff -uNr linux/drivers/no-otg/FIX-MAKEFILES linux/drivers/otg/FIX-MAKEFILES
--- linux/drivers/no-otg/FIX-MAKEFILES	1970-01-01 01:00:00.000000000 +0100
+++ linux/drivers/otg/FIX-MAKEFILES	2006-09-01 21:41:25.000000000 +0200
@@ -0,0 +1,23 @@
+#!/bin/sh
+
+ARG=$1
+shift
+
+[ -z "${ARG}" ] && echo Bad args && exit 1
+
+#find . -name Makefile-${ARG} | while read i
+#do
+#        m=`expr $i : "\(.*\)-${ARG}"`
+#        ln -sfv $i $m
+#done
+
+find . -type d | while read d
+do
+        pushd $d > /dev/null
+        if [ -s Makefile-${ARG} ] ; then
+                echo -n `pwd` ": "
+                ln -sfv Makefile-${ARG} Makefile
+        fi
+        popd >/dev/null
+done
+
diff -uNr linux/drivers/no-otg/Kconfig linux/drivers/otg/Kconfig
--- linux/drivers/no-otg/Kconfig	1970-01-01 01:00:00.000000000 +0100
+++ linux/drivers/otg/Kconfig	2006-09-01 21:41:25.000000000 +0200
@@ -0,0 +1,115 @@
+menu "On-The-Go and USB Peripheral Support"
+
+    config OTG
+        tristate "Support for On-The-Go and USB Peripheral Support"
+        ---help---
+            Configure all or part of the Belcarra OTG Stack
+
+
+    menu "On-The-Go Support"
+        depends on OTG 
+
+        source "drivers/otg/config/Kconfig-scma11-evb"
+        #source "drivers/otg/config/Kconfig-omap-h2"
+
+    endmenu
+
+    menu "On-The-Go Support Configuration"
+        depends on OTG_PLATFORM_OTG
+
+        choice
+            depends on OTG && OTG_PLATFORM_OTG
+            prompt "On-The-Go Device Configuration"
+            config OTG_CFG_TR
+                bool "Traditional USB Peripheral"
+                ---help---
+                Compile as a Traditional USB Peripheral. 
+                On-The-Go support is enabled.
+            config OTG_CFG_HO
+                bool "Host Only"
+                ---help---
+                Compile the USB Host support without the USB Peripheral
+                support. This is generally only useful for testing the
+                USB Host support and Host Controller drivers.
+            config OTG_CFG_PO
+                bool "Peripheral Only"
+                ---help---
+                Compile as a On-The-Go Peripheral-Only device. This
+                is similiar to a Traditional USB Peripheral but enables
+                On-The-Go features such as SRP.
+            config OTG_CFG_DR
+                bool "Dual Role"
+                ---help---
+                Compile as an On-The-Go Dual-Role device.
+
+        endchoice
+    endmenu
+
+    menu "Targeted Peripheral List (USB Host Class Drivers)"
+        depends on OTG_PLATFORM_OTG
+        # souce "drivers/otg/xxxx"
+        #
+        #---help---
+        #A list of USB peripherals that this device
+        #can support when it is acting as a host.
+    endmenu
+
+    menu "General Support Options"
+
+        depends on OTG_PLATFORM_OTG|| OTG_PLATFORM_USBD
+
+        # This needs to be a specific defined variable that comes
+        # from Kconfig-platform file
+        #
+        #config usb
+        #    tristate 'OTG host core support (separate from native Linux host support)'
+            
+        config OTG_HIGH_SPEED
+            bool 'Enable high speed descriptors'
+            depends on OTG!=n
+
+        config OTG_TRACE
+            bool 'OTG Fast Tracing'
+            depends on OTG!=n
+            ---help---
+            This option implements register trace to support 
+            driver debugging.
+        
+        #config OTG_ROOT_HUB
+        #    bool 'Enable Root HUB Function'
+        #    depends on OTG!=n
+        
+        config OTG_PROCFS
+            bool 'OTG Proc FS'
+            depends on OTG!=n
+            ---help---
+            This option enables /proc/ support in various modules
+            Note: Some information previously exposed via the /proc
+            interface is now exposed via a different mechanism
+        
+        config OTG_PROCFSM
+            tristate 'OTG Proc FS Module'
+            depends on OTG != n
+            ---help---
+            Build in extra support to perform various operations
+            through the /proc filesystem.  Note: this module is
+            held over from the Device stack and its functions are
+            gradually being transferred to the OTG Admin API.
+        
+    endmenu
+
+
+    menu "Targeted Peripherals List (USB Peripheral Function Drivers)"
+        depends on OTG_PLATFORM_OTG || OTG_PLATFORM_USBD
+        #---help---
+        #A list of USB peripheral types that this device
+        #can emulate when it is acting as a peripheral.
+        source "drivers/otg/functions/acm/Kconfig"
+        source "drivers/otg/functions/mouse/Kconfig"
+        source "drivers/otg/functions/msc/Kconfig"
+        source "drivers/otg/functions/network/Kconfig"
+    endmenu
+
+
+endmenu
+
diff -uNr linux/drivers/no-otg/Makefile linux/drivers/otg/Makefile
--- linux/drivers/no-otg/Makefile	1970-01-01 01:00:00.000000000 +0100
+++ linux/drivers/otg/Makefile	2006-09-01 21:41:25.000000000 +0200
@@ -0,0 +1,132 @@
+#
+# Belcarra OTG - On-The-Go 
+#
+# Copyright (c) 2004 Belcarra Technologies Corp
+
+TOPDIR ?= ../../..
+
+# order here may be important (determines linking order, thus module init order)
+subdir-y 	:= otgcore functions ocd 
+subdir-m 	:= otgcore functions ocd 
+subdir-n 	:=
+subdir- 	:=
+# The target object and module list name.
+
+O_TARGET	:= otg_drv.o
+
+# Objects that export symbols.
+
+#export-objs	:= usbd.o usbd-bops.o usbd-fops.o usbd-pcd.o ep0.o hub.o
+
+# Multipart objects. (core layer)
+
+
+# Optional parts of multipart objects.
+
+# Object file lists.
+
+obj-y		:=
+obj-m		:=
+obj-n		:=
+obj-		:=
+
+# Each configuration option enables a list of files.
+
+ifeq ($(CONFIG_OTG),y)
+obj-y	+= otgcore/otgcore.o 
+endif
+
+
+# Object files in subdirectories (There has to be a better way to do this)
+
+#=== Function drivers
+f-obj-y		:=
+f-obj-m		:=
+f-obj-n		:=
+f-obj-		:=
+
+f-obj-$(CONFIG_OTG_ACM)		+= functions/function_target.o
+f-obj-$(CONFIG_OTG_ISOTEST)	+= functions/function_target.o
+f-obj-$(CONFIG_OTG_MSC)		+= functions/function_target.o
+f-obj-$(CONFIG_OTG_MOUSE)	+= functions/function_target.o
+f-obj-$(CONFIG_OTG_NETWORK)	+= functions/function_target.o
+f-obj-$(CONFIG_OTG_PST)		+= functions/function_target.o
+
+# Remove any duplicate entries in the list by sorting (since that drops dups)
+obj-y		+= $(sort $(f-obj-y))
+#obj-m		+= $(sort $(f-obj-m))
+obj-n		+= $(sort $(f-obj-n))
+obj-		+= $(sort $(f-obj-))
+
+#=== Peripheral controller drivers
+p-obj-y		:=
+p-obj-m		:=
+p-obj-n		:=
+p-obj-		:=
+
+p-obj-$(CONFIG_OTG_AU1X00)	+= ocd/ocd_target.o
+p-obj-$(CONFIG_OTG_AU1550_DB1550_TR) += ocd/ocd_target.o
+
+# Remove any duplicate entries in the list by sorting (since that drops dups)
+obj-y		+= $(sort $(p-obj-y))
+#obj-m		+= $(sort $(p-obj-m))
+obj-n		+= $(sort $(p-obj-n))
+obj-		+= $(sort $(p-obj-))
+
+#=== Host controller drivers
+h-obj-y		:=
+h-obj-m		:=
+h-obj-n		:=
+h-obj-		:=
+
+
+# Remove any duplicate entries in the list by sorting (since that drops dups)
+obj-y		+= $(sort $(h-obj-y))
+#obj-m		+= $(sort $(h-obj-m))
+obj-n		+= $(sort $(h-obj-n))
+obj-		+= $(sort $(h-obj-))
+
+
+#=== Class drivers
+class-obj-y		:=
+class-obj-m		:=
+class-obj-n		:=
+class-obj-		:=
+
+#class-obj-$(CONFIG_OTG_CLASS_USBLAN)		+= classes/class_target.o
+
+# Remove any duplicate entries in the list by sorting (since that drops dups)
+obj-y		+= $(sort $(class-obj-y))
+#obj-m		+= $(sort $(f-obj-m))
+#obj-n		+= $(sort $(f-obj-n))
+#obj-		+= $(sort $(f-obj-))
+
+
+
+# Extract lists of the multi-part drivers.
+# The 'int-*' lists are the intermediate files used to build the multi's.
+
+multi-y		:= $(filter $(list-multi), $(obj-y))
+multi-m		:= $(filter $(list-multi), $(obj-m))
+int-y		:= $(sort $(foreach m, $(multi-y), $($(basename $(m))-objs)))
+int-m		:= $(sort $(foreach m, $(multi-m), $($(basename $(m))-objs)))
+
+# Files that are both resident and modular: remove from modular.
+
+obj-m		:= $(filter-out $(obj-y), $(obj-m))
+int-m		:= $(filter-out $(int-y), $(int-m))
+
+# Translate to Rules.make lists.
+
+O_OBJS		:= $(filter-out $(export-objs), $(obj-y))
+OX_OBJS		:= $(filter     $(export-objs), $(obj-y))
+M_OBJS		:= $(sort $(filter-out $(export-objs), $(obj-m)))
+MX_OBJS		:= $(sort $(filter     $(export-objs), $(obj-m)))
+MI_OBJS		:= $(sort $(filter-out $(export-objs), $(int-m)))
+MIX_OBJS	:= $(sort $(filter     $(export-objs), $(int-m)))
+
+# The global Rules.make.
+
+include $(TOPDIR)/Rules.make
+EXTRA_CFLAGS += -Wno-format -Wall
+
diff -uNr linux/drivers/no-otg/Makefile-l26 linux/drivers/otg/Makefile-l26
--- linux/drivers/no-otg/Makefile-l26	1970-01-01 01:00:00.000000000 +0100
+++ linux/drivers/otg/Makefile-l26	2006-09-01 21:41:25.000000000 +0200
@@ -0,0 +1,7 @@
+#
+# Belcarra OTG - On-The-Go 
+#
+# Copyright (c) 2004 Belcarra Technologies Corp
+
+obj-y 		+= 
+obj-m	        += functions/ ocd/ otgcore/ core/
diff -uNr linux/drivers/no-otg/OWNER linux/drivers/otg/OWNER
--- linux/drivers/no-otg/OWNER	1970-01-01 01:00:00.000000000 +0100
+++ linux/drivers/otg/OWNER	2006-09-01 21:41:25.000000000 +0200
@@ -0,0 +1,20 @@
+OWNER: Belcarra Technologies Corp
+LICENSEE: Freescale
+
+THIS SOURCE CODE KIT IS SUPPLIED UNDER LICENSE
+
+Unless expressly modified elsewhere, the author of 
+each source file included herein retains ownership
+of the identified file notwithstanding release of
+a specific version under a Public License such as 
+the GNU General Public License.
+
+The line OWNER(entity) in a comment line at or near
+the top of a source file expressly asserts ownership
+of the file by that entity or person.
+
+In addition the presence of an OWNER.TXT or OWNER
+file in a directory asserts ownership of the contents
+of that directory and of the compilation thereof into
+the present and derivative kits.
+
diff -uNr linux/drivers/no-otg/README linux/drivers/otg/README
--- linux/drivers/no-otg/README	1970-01-01 01:00:00.000000000 +0100
+++ linux/drivers/otg/README	2006-09-01 21:41:25.000000000 +0200
@@ -0,0 +1,11 @@
+otg/README
+
+This is the top level of the OTG toolkit source tree.
+
+        classes         Host Class drivers
+        functions       Peripheral Function drivers
+        ocd             OTG Controller drivers
+        otgcore         OTG State Machine and USB Device stack
+        otghw           Hardware related include files
+        otg             Include files
+
diff -uNr linux/drivers/no-otg/config/Config.in-au1x00 linux/drivers/otg/config/Config.in-au1x00
--- linux/drivers/no-otg/config/Config.in-au1x00	1970-01-01 01:00:00.000000000 +0100
+++ linux/drivers/otg/config/Config.in-au1x00	2006-09-01 21:41:25.000000000 +0200
@@ -0,0 +1,29 @@
+#
+# Copyright (c) 2004 Belcarra
+#
+
+# Au1x00 on DB1100, PB1100 and PB1500
+
+if [ "$CONFIG_SOC_AU1X00" = "y" -o \
+    "$CONFIG_MIPS_AU1X00" = "y" -o \
+    "$CONFIG_CPU_AU1X00" = "y"  -o \
+    "$CONFIG_MIPS_AU1500" = "y" -o \
+    "$CONFIG_MIPS_AU1100" = "y" -o \
+    "$CONFIG_MIPS_AU1000" = "y" ]
+then	
+    mainmenu_option next_comment
+    comment 'AMD AU1X00 Bus Interface'
+    
+    dep_tristate 'DB1100/PB1100/PB1500 Development Boards Support' CONFIG_OTG_AU1X00 $CONFIG_OTG
+    
+    if [ "$CONFIG_OTG_AU1X00" != "n" ]; then
+        int 'AU1X00  System Clock' CONFIG_OTG_AU1X00_SCLOCK 400 
+        define_bool CONFIG_AU1000_USB_DEVICE n
+        define_bool CONFIG_AU1X00_USB_DEVICE y
+        define_bool CONFIG_OTG_PLATFORM_USBD y
+    #else
+    #    define_bool CONFIG_OTG_PLATFORM_USBD n
+    fi
+    endmenu
+fi
+
diff -uNr linux/drivers/no-otg/config/Config.in-db1550 linux/drivers/otg/config/Config.in-db1550
--- linux/drivers/no-otg/config/Config.in-db1550	1970-01-01 01:00:00.000000000 +0100
+++ linux/drivers/otg/config/Config.in-db1550	2006-09-01 21:41:25.000000000 +0200
@@ -0,0 +1,48 @@
+#
+# Copyright (c) 2004 Belcarra
+#
+
+if [ "$CONFIG_MIPS_DB1550" = "y" -o "$CONFIG_MIPS_MTX2" ]
+then	
+    mainmenu_option next_comment
+    comment 'DB1550 Development Board'
+    
+    dep_tristate 'DB1550 Development Boards Support' CONFIG_OTG_DB1550 $CONFIG_OTG
+
+    #define_tristate CONFIG_OTG_AU1550
+ 
+    if [ "$CONFIG_OTG_DB1550" != "n" ]; then
+        int 'AU1X00  System Clock' CONFIG_OTG_AU1X00_SCLOCK 400 
+        define_bool CONFIG_AU1000_USB_DEVICE n
+        define_bool CONFIG_AU1X00_USB_DEVICE n
+        define_bool CONFIG_OTG_PLATFORM_USBD y
+        define_tristate CONFIG_OTG_AU1550 $CONFIG_OTG_DB1550
+
+        choice 'Select DB1550 Standard B or Mini A-B Port' \
+              "Mini-B-J14 CONFIG_OTG_DB1550_J14 \
+               Mini-A-B-J15 CONFIG_OTG_DB1550_J15"  Mini-B-J14
+
+        if [ "$CONFIG_OTG_DB1550_J14" = "y" ]; then
+            define_bool CONFIG_OTG_PLATFORM_OTG n
+            define_bool CONFIG_OTG_PLATFORM_USBD y
+            define_bool CONFIG_OTG_MAX3353E n
+            define_tristate CONFIG_OTG_AU1550_DB1550_TR $CONFIG_OTG_DB1550
+
+        else 
+            if [ "$CONFIG_OTG_DB1550_J15" = "y" ]; then
+                define_bool CONFIG_OTG_PLATFORM_OTG y
+                define_bool CONFIG_OTG_PLATFORM_USBD n
+                define_bool CONFIG_OTG_MAX3353E y
+                define_tristate CONFIG_OTG_MAX3353E_DB1550 $CONFIG_OTG_DB1550
+                define_tristate CONFIG_OTG_AU1550_DB1550_DR $CONFIG_OTG_DB1550
+
+                # J15 is external OTG Transceiver Client
+                define_bool CONFIG_OTG_DB1550_HXOE n
+                define_bool CONFIG_OTG_DB1550_HXS y
+                define_int CONFIG_OTG_DB1550_SEOS 4
+            fi
+        fi
+    fi
+    endmenu
+fi
+
diff -uNr linux/drivers/no-otg/config/Config.in-dbmx1 linux/drivers/otg/config/Config.in-dbmx1
--- linux/drivers/no-otg/config/Config.in-dbmx1	1970-01-01 01:00:00.000000000 +0100
+++ linux/drivers/otg/config/Config.in-dbmx1	2006-09-01 21:41:25.000000000 +0200
@@ -0,0 +1,19 @@
+#
+# Copyright (c) 2004 Belcarra
+#
+
+# MX1ADS - Motorola MX1                                                            	
+
+if [ "$CONFIG_ARCH_MX1ADS" = "y" ]; then						
+
+    mainmenu_option next_comment
+    comment 'Motorola MX1 Bus Interface'
+    dep_tristate 'DBMX1 Developement Board Support' CONFIG_OTG_DBMX1 $CONFIG_OTG	
+
+    if [ "$CONFIG_OTG_DBMX1" != "n" ]; then
+        define_bool CONFIG_OTG_PLATFORM_USBD y
+    else
+        define_bool CONFIG_OTG_PLATFORM_USBD n
+    fi
+    endmenu
+fi											
diff -uNr linux/drivers/no-otg/config/Config.in-isp1301 linux/drivers/otg/config/Config.in-isp1301
--- linux/drivers/no-otg/config/Config.in-isp1301	1970-01-01 01:00:00.000000000 +0100
+++ linux/drivers/otg/config/Config.in-isp1301	2006-09-01 21:41:25.000000000 +0200
@@ -0,0 +1,25 @@
+
+#
+# Copyright (c) 2004 Belcarra
+#
+# ISP 1301 TCD
+
+#if [ "$CONFIG_OTG_ISP1301" = "y" ]; then
+#    mainmenu_option next_comment
+#    comment 'ISP 1301'
+#   
+#    #bool 'Proc FS debug' CONFIG_OTG_ISP1301_PROCFSX 
+#    #bool         'Enable High Speed Descriptors' CONFIG_OTG_HIGH_SPEEDX
+#    define_bool CONFIG_OTG_TEST y
+#
+#    endmenu
+#fi
+if [ "$CONFIG_OTG_ISP1301" = "y" ]; then
+
+    mainmenu_option next_comment
+    comment 'ISP 1301'
+    bool 'Proc FS debug' CONFIG_OTG_ISP1301_PROCFS
+
+    endmenu
+fi                                                                  
+
diff -uNr linux/drivers/no-otg/config/Config.in-max3353e linux/drivers/otg/config/Config.in-max3353e
--- linux/drivers/no-otg/config/Config.in-max3353e	1970-01-01 01:00:00.000000000 +0100
+++ linux/drivers/otg/config/Config.in-max3353e	2006-09-01 21:41:25.000000000 +0200
@@ -0,0 +1,25 @@
+
+#
+# Copyright (c) 2004 Belcarra
+#
+# MAX 3353E TCD
+
+#if [ "$CONFIG_OTG_MAX3353E" = "y" ]; then
+#    mainmenu_option next_comment
+#    comment 'MAX 3353E'
+#   
+#    #bool 'Proc FS debug' CONFIG_OTG_MAX3353E_PROCFSX 
+#    #bool         'Enable High Speed Descriptors' CONFIG_OTG_HIGH_SPEEDX
+#    define_bool CONFIG_OTG_TEST y
+#
+#    endmenu
+#fi
+if [ "$CONFIG_OTG_MAX3353E" = "y" ]; then
+
+    mainmenu_option next_comment
+    comment 'MAX 3353E'
+    bool 'Proc FS debug' CONFIG_OTG_MAX3353E_PROCFS
+
+    endmenu
+fi                                                                  
+
diff -uNr linux/drivers/no-otg/config/Config.in-mordor linux/drivers/otg/config/Config.in-mordor
--- linux/drivers/no-otg/config/Config.in-mordor	1970-01-01 01:00:00.000000000 +0100
+++ linux/drivers/otg/config/Config.in-mordor	2006-09-01 21:41:25.000000000 +0200
@@ -0,0 +1,28 @@
+#
+# Copyright (c) 2004 Belcarra
+#
+
+# Au1x00 on DB1100, PB1100 and PB1500
+
+if [ "$CONFIG_AMX_MORDOR" = "y"  ]
+then	
+    mainmenu_option next_comment
+    comment 'AMX Mordor Board'
+    
+    dep_tristate 'AMX Mordor Board Support' CONFIG_OTG_AU1550 $CONFIG_OTG
+    define_tristate CONFIG_OTG_BVD $CONFIG_OTG_AU1550  
+ 
+    if [ "$CONFIG_OTG_AU1550" != "n" ]; then
+        int 'AU1X00  System Clock' CONFIG_OTG_AU1X00_SCLOCK 400 
+        define_bool CONFIG_AU1000_USB_DEVICE n
+        define_bool CONFIG_AU1X00_USB_DEVICE n
+        define_bool CONFIG_OTG_PLATFORM_USBD y
+
+        define_bool CONFIG_OTG_PLATFORM_OTG n
+        define_bool CONFIG_OTG_PLATFORM_USBD y
+
+    else
+        define_bool CONFIG_OTG_PLATFORM_USBD n
+    fi
+    endmenu
+fi
diff -uNr linux/drivers/no-otg/config/Kconfig-omap-h2 linux/drivers/otg/config/Kconfig-omap-h2
--- linux/drivers/no-otg/config/Kconfig-omap-h2	1970-01-01 01:00:00.000000000 +0100
+++ linux/drivers/otg/config/Kconfig-omap-h2	2006-09-01 21:41:25.000000000 +0200
@@ -0,0 +1,87 @@
+#
+# Copyright (c) 2004 Belcarra
+#
+
+# OMAP - TI OMAP 1610
+#
+
+# CONFIG_OTG_PLATFORM_OTG           # Offer OTG Configuration
+
+# CONFIG_OTG_OMAP               # Make OMAP driver
+# CONFIG_OTG_ISP1301            # Make ISP1301 driver
+
+# CONFIG_OTG_ISP1301_OMAP_H2    # Compile ISP1301 OMAP H2 driver
+
+# CONFIG_OTG_OMAP_H2            # OMAP Helen 2 board
+# CONFIG_OTG_OMAP_H2_3_WIRE     # Use 3 Wire OTG Configuration
+# CONFIG_OTG_OMAP_H2_4_WIRE     # Use 4 Wire OTG Configuration
+# CONFIG_OTG_OMAP_H2_DR         # Compile as OTG Dual-role Device
+# CONFIG_OTG_OMAP_H2_TR         # Compile as Traditional USB Peripheral
+
+config OTG_OMAP_H2
+	tristate "OMAP 1610 H2 Development Board"
+	depends on OTG && ARCH_OMAP
+        ---help---
+        This implements On-The-Go USB Support for the Helen 2 OMAP 
+        Development Board.
+
+choice
+	prompt "Select Transceiver wire configuration"	
+	depends on OTG && ARCH_OMAP && OTG_OMAP_H2
+        config OTG_OMAP_H2_3_WIRE
+        	bool 'Enable Pin Group 1, 3 Wire Configuration'
+        	depends on OMAP_H2 !=n
+        	---help---
+                The H2 board ISP1301 can be configured in either 
+                the 3 wire or 4 wire configuration.
+                This enables the ISP1301 on the H2 board on Pin Group 1
+                using the 3-Wire OTG Transceiver Configuration. This
+                requires that the ISP1301 be configured for DAT_SE0.
+        
+        config OTG_OMAP_H2_4_WIRE
+        	bool 'Enable Pin Group 1, 4 Wire Configuration'
+        	depends on OMAP_H2 !=n
+        	---help---
+                The H2 board ISP1301 can be configured in either 
+                the 3 wire or 4 wire configuration.
+                This enables the ISP1301 on the H2 board on Pin Group 1
+                using the 3-Wire OTG Transceiver Configuration. This
+                requires that the ISP1301 be configured for VP_VM.
+
+endchoice
+
+config OTG_PLATFORM_OTG
+        bool 
+        default OTG_OMAP_H2
+
+config OTG_OMAP_H2_TR
+        tristate
+        depends on OTG_CFG_TR
+        default OTG_OMAP_H2
+
+config OTG_OMAP_H2_HO
+        tristate
+        depends on OTG_CFG_HO
+        default OTG_OMAP_H2
+
+config OTG_OMAP_H2_PO
+        tristate
+        depends on OTG_CFG_PO
+        default OTG_OMAP_H2
+
+config OTG_OMAP_H2_DR
+        tristate
+        depends on OTG_CFG_DR
+        default OTG_OMAP_H2
+
+
+config OTG_ISP1301
+        tristate
+        depends on OTG_OMAP_H2
+        default OTG_OMAP_H2
+
+config OTG_ISP1301_OMAP_H2
+        tristate
+        depends on OTG_OMAP_H2
+        default OTG_OMAP_H2
+
diff -uNr linux/drivers/no-otg/config/README-CONFIG.txt linux/drivers/otg/config/README-CONFIG.txt
--- linux/drivers/no-otg/config/README-CONFIG.txt	1970-01-01 01:00:00.000000000 +0100
+++ linux/drivers/otg/config/README-CONFIG.txt	2006-09-01 21:41:25.000000000 +0200
@@ -0,0 +1,106 @@
+OTG System Configuration                                        Stuart Lynne
+Belcarra                                        Thu Jan 13 15:45:19 PST 2005 
+
+This directory contains the linux system configurations to create
+architecture or board level drivers for On-The-Go or USB Device support.
+
+There are four generic configurations available
+
+    - tr - Traditional USB Peripheral    
+    - po - OTG Peripheral only
+    - dr - OTG Dual-Role device
+
+Traditional USB Peripheral    
+**************************
+
+Older types of systems supported USB peripherals as a separate module. It is
+not generally possible to configure these with On-The-Go support. In most
+cases the only customization available is for detection of the Vbus (cable
+attached) and control over the D-plus pullup resistor (soft-connect.)
+
+For most of these types of systems the generated driver will be of the form:
+
+    xxx_tr
+
+Where xxx is the system architecture:
+
+    au1x00
+    lh7a400
+    mx1
+    pxa
+    sa1100
+    smdk2500
+    superh
+
+
+OTG Modes
+*********
+
+Generally new systems that support On-The-Go have (at least) the following
+components:
+
+    - USB Peripheral
+    - USB Host
+    - OTG Transceiver
+    - Charge Pump (optional)
+
+In general the combination of the above is customized at the board or
+platform level, not the architecture (chip) level. There may be up to four
+different drivers implementing various combinations of the required support.
+
+Required support:
+
+    - pcd - Peripheral Controller Driver
+    - tcd - Transceiver Controller Driver
+    - hcd - Host Controller Driver
+    - ocd - OTG Controller Driver
+
+Typically the pcd and ocd drivers will be in a single module. This module
+will be named:
+
+    xxxx_ss
+
+Where xxxx is the platform, e.g.:
+
+    mainstone
+    mx1ads
+    omap-h2
+
+And ss is the type of OTG support being compiled:
+
+    - tr - traditonal usb
+    - ho - host only
+    - po - peripheral only
+    - dr - dual-role
+
+
+
+OTG Peripheral Only (po)
+************************
+
+This mode allows for implementing a restricted mode of On-The-Go support.
+The host driver module is not configured or available.
+
+
+OTG Dual-Role Device (dr)
+*************************
+
+This mode implements the full On-The-Go stack with both USB Device and Usb
+Host support.
+
+
+Configuration File
+******************
+
+Under linux, older systems (those that support only traditional devices) will
+have a generic Config.in / Kconfig file to generate the required driver.
+
+For newer systems that support OTG type configurations there should be a
+Configuration file for each major platform supported. This will specifically
+enable defines for all of the required drivers and their options. 
+
+Platform files may have several sub-types with appropriate configuration
+selectors.
+
+
+
diff -uNr linux/drivers/no-otg/dirs linux/drivers/otg/dirs
--- linux/drivers/no-otg/dirs	1970-01-01 01:00:00.000000000 +0100
+++ linux/drivers/otg/dirs	2006-09-01 21:41:25.000000000 +0200
@@ -0,0 +1,11 @@
+!if 0
+Copyright (c) 2004 Belcarra
+!endif
+
+DIRS= \
+        wince \
+        otgcore \
+        functions/mouse
+
+OPTIONAL_DIRS= \
+
diff -uNr linux/drivers/no-otg/functions/Makefile linux/drivers/otg/functions/Makefile
--- linux/drivers/no-otg/functions/Makefile	1970-01-01 01:00:00.000000000 +0100
+++ linux/drivers/otg/functions/Makefile	2006-09-01 21:41:25.000000000 +0200
@@ -0,0 +1,53 @@
+#
+# Belcarra OTG - On-The-Go 
+#
+# Copyright (c) 2004 Belcarra Technologies Corp
+
+TOPDIR ?= ../../../..
+
+subdir-y 	:= 
+subdir-m 	:=
+subdir-n 	:=
+subdir- 	:=
+
+# The target object and module list name.
+
+O_TARGET        := function_target.o
+
+# Function Drivers
+subdir-$(CONFIG_OTG_ACM) += acm
+subdir-$(CONFIG_OTG_ISOTEST) += isotest
+subdir-$(CONFIG_OTG_MSC) += msc
+subdir-$(CONFIG_OTG_MOUSE) += mouse
+subdir-$(CONFIG_OTG_NETWORK) += network
+
+# Object file lists.
+
+obj-y           :=
+obj-m           :=
+obj-n           :=
+obj-            :=
+
+# Function drivers
+ifeq ($(CONFIG_OTG_ACM),y)
+obj-y += acm/acm_fd_drv.o
+endif
+ifeq ($(CONFIG_OTG_MOUSE),y)
+obj-y += mouse/mouse_target.o
+endif
+ifeq ($(CONFIG_OTG_NETWORK),y)
+obj-y += network/network_target.o
+endif
+ifeq ($(CONFIG_OTG_MSC),y)
+obj-y += msc/msc_target.o
+endif
+ifeq ($(CONFIG_OTG_PST),y)
+obj-y += pst/pst_target.o
+endif
+ifeq ($(CONFIG_OTG_TEST),y)
+obj-y += test/test_target.o
+endif
+
+include $(TOPDIR)/Rules.make
+EXTRA_CFLAGS += -Wno-format -Wall
+
diff -uNr linux/drivers/no-otg/functions/Makefile-l26 linux/drivers/otg/functions/Makefile-l26
--- linux/drivers/no-otg/functions/Makefile-l26	1970-01-01 01:00:00.000000000 +0100
+++ linux/drivers/otg/functions/Makefile-l26	2006-09-01 21:41:25.000000000 +0200
@@ -0,0 +1,12 @@
+#
+# Belcarra OTG - On-The-Go 
+#
+# Copyright (c) 2004 Belcarra Technologies Corp
+
+EXTRA_CFLAGS += -Wno-format -Wall
+# Function Drivers
+obj-$(CONFIG_OTG_ACM) += acm/
+obj-$(CONFIG_OTG_MSC) += msc/
+obj-$(CONFIG_OTG_MOUSE) += mouse/
+obj-$(CONFIG_OTG_NETWORK) += network/
+
diff -uNr linux/drivers/no-otg/functions/acm/Config.in linux/drivers/otg/functions/acm/Config.in
--- linux/drivers/no-otg/functions/acm/Config.in	1970-01-01 01:00:00.000000000 +0100
+++ linux/drivers/otg/functions/acm/Config.in	2006-09-01 21:41:26.000000000 +0200
@@ -0,0 +1,28 @@
+#
+# CDC ACM Function Driver
+#
+# Copyright (C) 2003,2004 Belcarra
+#
+
+mainmenu_option next_comment
+comment "USB Peripheral Function Driver - CDC ACM"
+
+dep_tristate '  CDC ACM Function' CONFIG_OTG_ACM $CONFIG_OTG
+if [ "$CONFIG_OTG_ACM" != "n" ]; then
+   hex     'VendorID (hex value)' CONFIG_OTG_ACM_VENDORID "15ec"
+   hex     'ProductID (hex value)' CONFIG_OTG_ACM_PRODUCTID "f002"
+   hex     'bcdDevice (binary-coded decimal)' CONFIG_OTG_ACM_BCDDEVICE "0100"
+
+   string 'iManufacturer (string)' CONFIG_OTG_ACM_MANUFACTURER "Belcarra"
+   string 'iProduct (string)' CONFIG_OTG_ACM_PRODUCT_NAME "Belcarra ACM Device"
+
+   string 'iConfiguration (string)' CONFIG_OTG_ACM_DESC "Acm Cfg"
+   string 'Comm Interface iInterface (string)' CONFIG_OTG_ACM_COMM_INTF "Comm Intf"
+   string 'Data Interface iInterface (string)' CONFIG_OTG_ACM_DATA_INTF "Data Intf"
+
+   comment ''
+   #bool   'Communications Device'  CONFIG_OTG_ACM_COMM
+   #bool   'TTY Device'  CONFIG_OTG_ACM_TTY
+fi
+
+endmenu
diff -uNr linux/drivers/no-otg/functions/acm/Kconfig linux/drivers/otg/functions/acm/Kconfig
--- linux/drivers/no-otg/functions/acm/Kconfig	1970-01-01 01:00:00.000000000 +0100
+++ linux/drivers/otg/functions/acm/Kconfig	2006-09-01 21:41:25.000000000 +0200
@@ -0,0 +1,54 @@
+menu "OTG ACM Function"
+
+config OTG_ACM
+         tristate "  CDC ACM Function"
+         depends on OTG
+
+menu "OTG ACM function options"
+	depends on OTG && OTG_ACM
+
+config OTG_ACM_VENDORID
+        hex "VendorID (hex value)"
+	depends on OTG_ACM && OTG
+        default "0x15ec"
+
+config OTG_ACM_PRODUCTID
+	depends on OTG_ACM && OTG
+        hex "ProductID (hex value)"
+        default "0xe003"
+config OTG_ACM_BCDDEVICE
+	depends on OTG_ACM && OTG
+        hex "bcdDevice (binary-coded decimal)"
+        default "0x0100"
+
+config OTG_ACM_MANUFACTURER
+	depends on OTG_ACM && OTG
+        string "iManufacturer (string)"
+        default "Belcarra"
+
+config OTG_ACM_PRODUCT_NAME
+	depends on OTG_ACM && OTG
+        string "iProduct (string)"
+        default "Belcarra ACM Device"
+
+config OTG_ACM_DESC
+	depends on OTG_ACM && OTG
+        string "iConfiguration (string)"
+        default "Acm Cfg"
+
+config OTG_ACM_COMM_INTF
+	depends on OTG_ACM && OTG
+        string "Comm Interface iInterface (string)"
+        default "Comm Intf"
+
+config OTG_ACM_DATA_INTF
+	depends on OTG_ACM && OTG
+        string "Data Interface iInterface (string)"
+        default "Data Intf"
+
+config OTG_ACM_TRACE
+	depends on OTG_ACM && OTG
+        bool "  ACM Tracing"
+        default n
+endmenu
+endmenu
diff -uNr linux/drivers/no-otg/functions/acm/Makefile linux/drivers/otg/functions/acm/Makefile
--- linux/drivers/no-otg/functions/acm/Makefile	1970-01-01 01:00:00.000000000 +0100
+++ linux/drivers/otg/functions/acm/Makefile	2006-09-01 21:41:26.000000000 +0200
@@ -0,0 +1,80 @@
+#
+# Function driver for a CDC ACM USB Device
+#
+# Copyright (c) 2003 Belcarra
+
+# Multipart objects.
+
+O_TARGET	:= acm_fd_drv.o
+list-multi	:= acm_fd.o tty_fd.o
+
+#modem_fd-objs	:= acm-fd.o modem-l24-os.o modem.o
+#obex_fd-objs	:= acm-fd.o obex-l24-os.o obex.o
+tty_fd-objs	:= acm-fd.o tty-l24-os.o tty-fd.o
+
+# Objects that export symbols.
+#export-objs	:= acm-fd.o 
+
+# Object file lists.
+
+obj-y	:=
+obj-m	:=
+obj-n	:=
+obj-	:=
+
+# Each configuration option enables a list of files.
+
+#obj-$(CONFIG_OTG_ACM)   += modem_fd.o
+#obj-$(CONFIG_OTG_ACM)   += obex_fd.o
+obj-$(CONFIG_OTG_ACM)   += tty_fd.o
+
+# Extract lists of the multi-part drivers.
+# The 'int-*' lists are the intermediate files used to build the multi's.
+
+multi-y		:= $(filter $(list-multi), $(obj-y))
+multi-m		:= $(filter $(list-multi), $(obj-m))
+int-y		:= $(sort $(foreach m, $(multi-y), $($(basename $(m))-objs)))
+int-m		:= $(sort $(foreach m, $(multi-m), $($(basename $(m))-objs)))
+
+# Files that are both resident and modular: remove from modular.
+
+obj-m		:= $(filter-out $(obj-y), $(obj-m))
+int-m		:= $(filter-out $(int-y), $(int-m))
+
+# Translate to Rules.make lists.
+
+O_OBJS		:= $(filter-out $(export-objs), $(obj-y))
+OX_OBJS		:= $(filter     $(export-objs), $(obj-y))
+M_OBJS		:= $(sort $(filter-out $(export-objs), $(obj-m)))
+MX_OBJS		:= $(sort $(filter     $(export-objs), $(obj-m)))
+MI_OBJS		:= $(sort $(filter-out $(export-objs), $(int-m)))
+MIX_OBJS	:= $(sort $(filter     $(export-objs), $(int-m)))
+
+# The global Rules.make.
+
+ACMD=$(OTG)/functions/acm
+
+OTG_DIR=$(TOPDIR)/drivers/otg
+OTGCORE_DIR=$(OTG_DIR)/otgcore
+#USBDCORE_DIR=$(OTG_DIR)/usbdcore
+include $(TOPDIR)/Rules.make
+EXTRA_CFLAGS +=  -I$(OTG_DIR) -Wno-unused -Wno-format  -I$(OTGCORE_DIR)
+EXTRA_CFLAGS_nostdinc += -I$(OTG_DIR) -Wno-unused -Wno-format  -I$(OTGCORE_DIR)
+
+# Link rules for multi-part drivers.
+
+modem_fd.o: $(modem_fd-objs)
+	$(LD) -r -o $@ $(modem_fd-objs)
+
+obex_fd.o: $(obex_fd-objs)
+	$(LD) -r -o $@ $(obex_fd-objs)
+
+tty_fd.o: $(tty_fd-objs)
+	$(LD) -r -o $@ $(tty_fd-objs)
+
+# dependencies:
+
+#modem.o: $(USBDCORE_DIR)/usbd.h $(USBDCORE_DIR)/usbd-bus.h $(USBDCORE_DIR)/usbd-func.h
+#obex.o: $(USBDCORE_DIR)/usbd.h $(USBDCORE_DIR)/usbd-bus.h $(USBDCORE_DIR)/usbd-func.h
+#tty.o: $(USBDCORE_DIR)/usbd.h $(USBDCORE_DIR)/usbd-bus.h $(USBDCORE_DIR)/usbd-func.h
+
diff -uNr linux/drivers/no-otg/functions/acm/Makefile-l26 linux/drivers/otg/functions/acm/Makefile-l26
--- linux/drivers/no-otg/functions/acm/Makefile-l26	1970-01-01 01:00:00.000000000 +0100
+++ linux/drivers/otg/functions/acm/Makefile-l26	2006-09-01 21:41:26.000000000 +0200
@@ -0,0 +1,13 @@
+# Function driver for a CDC ACM OTG Device
+#
+# Copyright (c) 2004 Belcarra
+
+acm_fd-objs	:= acm-fd.o acm-l26-os.o
+
+obj-$(CONFIG_OTG_ACM) += acm_fd.o
+
+OTG=$(TOPDIR)/drivers/otg
+ACMD=$(OTG)/functions/acm
+USBDCORE_DIR=$(OTG)/usbdcore
+EXTRA_CFLAGS += -I$(ACMD) -I$(OTG) -Wno-unused -Wno-format  -I$(USBDCORE_DIR)
+EXTRA_CFLAGS_nostdinc += -I$(ACMD) -I$(OTG) -Wno-unused -Wno-format  -I$(USBDCORE_DIR)
diff -uNr linux/drivers/no-otg/functions/acm/OBEX-TODO.txt linux/drivers/otg/functions/acm/OBEX-TODO.txt
--- linux/drivers/no-otg/functions/acm/OBEX-TODO.txt	1970-01-01 01:00:00.000000000 +0100
+++ linux/drivers/otg/functions/acm/OBEX-TODO.txt	2006-09-01 21:41:26.000000000 +0200
@@ -0,0 +1,16 @@
+OBEX TODO List                                                  Stuart Lynne
+Belcarra                                        Tue Aug 24 21:36:09 PDT 2004 
+
+
+1. OBEX documentation
+
+
+2. define obex requirements
+
+        - similiar to acm
+        - uses comm interface for ?
+        - impelements data/nodata inteface
+
+        - socket family interface
+        - char device interface
+
diff -uNr linux/drivers/no-otg/functions/acm/TODO.txt linux/drivers/otg/functions/acm/TODO.txt
--- linux/drivers/no-otg/functions/acm/TODO.txt	1970-01-01 01:00:00.000000000 +0100
+++ linux/drivers/otg/functions/acm/TODO.txt	2006-09-01 21:41:26.000000000 +0200
@@ -0,0 +1,143 @@
+ACM TODO List                                                   Stuart Lynne
+Belcarra                                        Wed Sep 01 19:48:38 PDT 2004 
+
+
+1. The ACM driver needs to be expanded to allow it to build three ways:
+
+
+        - tty   equivalent of old acm, uses linux tty layer
+        - modem present two simple char devices for data and comm interfaces
+        - obex  single simple char device
+
+TTY 
+        - present a single char device with TTY line discipline
+        - TIOCM calls represent "state" of emulated serial port on this
+          side of null modem
+        - notifications and line state only affect far side of null modem
+
+MODEM 
+        - present two simple char device interfaces
+        - data char device will implement TIOCM ioctls
+        - comm char device will use encapsulated data over endpoint zero
+        - TIOCM calls represent state passed to/from host via notifications
+          and line state requests
+
+OBEX
+        - present a single simple char device interface
+        - no requirement for either TIOCM or TTY line discipline
+        - TIOCM calls not implemented
+
+
+
+2. The modem device must implement a virtual NULL modem and support the 
+following IOCTL's.
+
+
+
+Virtual NULL Modem
+
+Peripheral to Host via Serial State Notification (write ioctls)
+
+Application             TIOCM           Null Modem      ACM (DCE)       Notificaiton    Host (DTE)
+-------------------------------------------------------------------------------------------------------------
+Request to send         TIOCM_RTS       RTS ->  CTS     CTS             Not Available   Clear to Send (N/A)
+Data Terminal Ready     TIOCM_DTR       DTR ->  DSR     DSR             bTxCarrier      Data Set Ready
+                                        DTR ->  DCD     DCD             bRXCarrier      Carrier Detect
+Ring Indicator          TIOCM_OUT1      OUT1 -> RI      RI              bRingSignal     Ring Indicator
+Send Break              TIOCM_OUT2      OUT2 -> Break   Break           bBreak          Break Received
+-------------------------------------------------------------------------------------------------------------
+
+
+Host to Peripheral via Set Control Line State
+
+Host (DTE)              Line State      ACM (DCE)       Null Modem      TIOCM           Peripheral (DTE)
+-------------------------------------------------------------------------------------------------------------
+Data Terminal Ready     D0              DTR             DTR ->  DSR     TIOCM_DSR       Data Set Ready
+                                                        DTR ->  DCD     TIOCM_CAR       Carrier Detect
+Request To Send         D1              RTS             RTS ->  CTS     TIOCM_CTS       Clear to Send
+-------------------------------------------------------------------------------------------------------------
+
+
+3. The following DEVICE REQUESTS have to be implemented and where
+possible support added to hook the results to the upper layers
+and applications.
+
+        SetCommFeature 
+        GetCommFeature 
+        ClearCommFeature 
+
+        SendEncapsulatedCommand 
+        GetEncapsulatedResponse
+
+        SetLineCoding 
+        GetLineCoding
+
+        SetControlLineState
+
+        SendBreak
+
+4. The following NOTIFICATIONS need to be implemented and where 
+possible hooked to appropriate indications from upper layers
+and applications.
+
+
+        RESPONSE_AVAILABLE
+        NETWORK_CONNECTION
+        SERIAL_STATE
+
+
+
+5. ACM documentation needs to be updated to reflect implementation(s).
+
+
+6. POSIX IOCTLS
+        - TIOCM*
+        - hook into flow control or set from flow control as required
+        - baudrate and other device settings        
+        - CTS should disable receive urb
+
+7. COMM Interface 
+        - GET / SEND Encapsulated command
+        - response available notification
+        - char device interface
+
+8. Optional TTY
+        - register as TTY device only if requested
+        - register as simple char device otherwise
+
+9. select() 
+        - trackdown tty layer problem with select()
+        - sometimes fails under stress testing
+
+
+Notes..
+
+1. implementation of the comm and simple char device can probably share
+the same code base. These should queue received urbs. This will mean 
+a small change in the os between acm_recv_urb() and the os specific
+layer. 
+
+2. Now it is acm_os_recv_chars(), this must change to acm_os_recv_urbs().
+The acm_os_recv_urbs() function will be responsible for deallocing the
+urb when delivered.
+
+3. The various os specific functions will need to have a method to
+distinguish between the comm and data interfaces. 
+
+
+
+
+10. WMC  - Wireless Mobice Class
+
+        Call Management - bmCapabilities allow call management over data interface
+        ACM - bmCapabilities - 0x06, only SEND_BREAK, SET/GET LINE_CODING
+
+        NETWORK_CONNECTION, XXX_COMM_FEATURE not allowed
+
+        Call management over COMM interface optional
+
+        COMM_FEATURE - optional?
+
+
+
+
diff -uNr linux/drivers/no-otg/functions/acm/acm-fd.c linux/drivers/otg/functions/acm/acm-fd.c
--- linux/drivers/no-otg/functions/acm/acm-fd.c	1970-01-01 01:00:00.000000000 +0100
+++ linux/drivers/otg/functions/acm/acm-fd.c	2006-09-01 21:41:26.000000000 +0200
@@ -0,0 +1,1077 @@
+/*
+ * otg/functions/acm/acm-fd.c
+ *
+ *      Copyright (c) 2003, 2004 Belcarra
+ *
+ * By: 
+ *      Tom Rushworth <tbr@belcarra.com>, 
+ *      Stuart Lynne <sl@belcarra.com>, 
+ *      Bruce Balden <balden@belcarra.com>
+ */
+/*!
+ * @file otg/functions/acm/acm-fd.c
+ * @brief ACM Function Driver private defines
+ *
+ * An ACM (Abstract Control Model) driver is composed of several pieces:
+ *
+ *    1) An OS and function specific piece that handles creating and operating
+ *       a device for the given OS suitable for a specific function.
+ *
+ *    2) A set descriptors suitable for the function.
+ *
+ *    3) This acm-fd library which implements the interface to
+ *       the usb peripheral and otgcore stacks.
+ *
+ * If the USB piece interfaces with the host usbcore layer you get
+ * an ACM class driver.  If the USB piece interfaces with the otgcore
+ * layer you get an ACM function driver.
+ *
+ *
+ * @ingroup ACMFunction
+ */
+
+
+#include <otg/otg-compat.h>
+#include <otg/otg-module.h>
+
+#include <linux/init.h>
+#include <asm/uaccess.h>
+#include <linux/ctype.h>
+#include <linux/timer.h>
+#include <linux/interrupt.h>
+#include <asm/atomic.h>
+#include <linux/smp_lock.h>
+#include <linux/slab.h>
+
+#include <otg/usbp-chap9.h>
+#include <otg/usbp-func.h>
+
+#include <otg/otg-trace.h>
+#include <otg/otg-api.h>
+#include "acm.h"
+#include "acm-fd.h"
+#include "acm-os.h"
+
+
+static u32 max_queued_urbs;
+static u32 max_queued_bytes;
+
+// Define the low order 16 bits of an urb's memory address as it's ID for tracing.
+#define urbID(urb) (0xffff & (u32) (void *) urb)
+
+/* ******************************************************************************************* */
+
+STATIC int acm_send_int_notification(struct acm_private *acm, int , int );
+STATIC int acmfd_urb_sent_bulk (struct usbd_urb *urb, int rc);
+STATIC int acmfd_urb_sent_int (struct usbd_urb *urb, int rc);
+STATIC int acmfd_recv_urb (struct usbd_urb *urb, int rc);
+STATIC void acm_schedule_recv(struct acm_private *acm, int interface);
+
+/* ******************************************************************************************* */
+
+/*! acm_ready
+ * @param acm
+ */
+STATIC int acm_ready(struct acm_private *acm) 
+{
+        TRACE_MSG5(acm->trace_tag,"CONFIGURED: %x OPENED: %x THROTTLED: %x CARRIER: %x READY: %d", 
+                        acm->flags & ACM_CONFIGURED, acm->flags & ACM_OPENED,
+                        acm->flags & ACM_THROTTLED, acm->flags & ACM_CARRIER,
+                        (acm->flags & ACM_CONFIGURED) && (acm->flags & ACM_CARRIER) ? 1 : 0);
+
+        return (acm->flags & ACM_CONFIGURED) && 
+                (acm->flags & ACM_OPENED) &&
+                !(acm->flags & ACM_THROTTLED) &&
+                ((acm->flags & ACM_LOCAL) ? 1 : (acm->flags & ACM_CARRIER) ) 
+                ;
+
+}
+
+/*! acm_open
+ * @param acm
+ * @param interface
+ * @return int
+ */
+STATIC int acm_open(struct acm_private *acm, int interface) 
+{
+        TRACE_MSG0(acm->trace_tag,"OPEN");
+        acm->flags |= ACM_OPENED;
+        acm->flags &= ~ACM_THROTTLED;
+        acm->bmUARTState = CDC_UARTSTATE_BRXCARRIER_DCD | CDC_UARTSTATE_BTXCARRIER_DSR;
+        acm_schedule_recv(acm, interface);
+        TRACE_MSG1(acm->trace_tag,"bmUARTState: %04x", acm->bmUARTState);
+        acm_send_int_notification(acm, CDC_NOTIFICATION_SERIAL_STATE, acm->bmUARTState);
+        return 0;
+}
+
+/*! acm_flush
+ * @param acm
+ * @param interface
+ * @return number of urbs in queue
+ */
+STATIC void acm_flush(struct acm_private *acm, int interface) 
+{
+        struct usbd_function_instance *function = acm->function;
+        unsigned long flags;
+        TRACE_MSG0(acm->trace_tag,"FLUSH");
+        RETURN_UNLESS(function);
+        local_irq_save(flags);
+        acm->bytes_received = 0;
+        acm->bytes_forwarded = 0;
+        acm->bmUARTState = CDC_UARTSTATE_BRXCARRIER_DCD | CDC_UARTSTATE_BTXCARRIER_DSR;
+        TRACE_MSG1(acm->trace_tag,"bmUARTState: %04x", acm->bmUARTState);
+        // TBR: 20040705 use ...le32() not le16, spotted by Zhao Liang
+        acm->line_coding.dwDTERate = cpu_to_le32(0x1c200); // 115200
+        acm->line_coding.bDataBits = 0x08;
+
+        usbd_flush_endpoint_index(function, BULK_IN);
+        usbd_flush_endpoint_index(function, BULK_OUT);
+        local_irq_restore(flags);
+}
+
+/*! acm_close
+ * @param acm
+ * @param interface
+ * @return number of urbs in queue
+ */
+STATIC int acm_close(struct acm_private *acm, int interface) 
+{
+        TRACE_MSG0(acm->trace_tag,"CLOSE");
+        acm->flags &= ~ACM_OPENED;
+        acm->flags &= ~ACM_THROTTLED;
+        acm_flush(acm, interface);
+        acm->bmUARTState = 0;
+        TRACE_MSG1(acm->trace_tag,"bmUARTState: %04x", acm->bmUARTState);
+        acm_send_int_notification(acm, CDC_NOTIFICATION_SERIAL_STATE, acm->bmUARTState);
+        return 0;
+}
+
+
+/*! acmfd_start_recv_urbs
+ * @param acm
+ * @param interface
+ * @return number of urbs in queue
+ */
+STATIC int acmfd_start_recv_urbs(struct acm_private *acm, int interface) 
+{
+        /*
+         * Queue as many receive urbs as the OS layer has room for.  Return
+         * the number in the queue (may be more than we queue here).
+         */
+        struct usbd_function_instance *function = acm->function;
+        unsigned long flags;
+        int num_in_queue = 0;
+
+        switch (interface) {
+        case COMM_INTF:
+                return 0;
+        case DATA_INTF:
+                TRACE_MSG1(acm->trace_tag,"START RECV: acm: %x", (int)acm);
+                TRACE_MSG2(acm->trace_tag,"START RECV: connected: %x recv_urbs: %d", 
+                                acm->flags & ACM_CONFIGURED, acm->recv_urbs);
+
+                local_irq_save(flags);
+                if (acm_ready(acm)) {
+
+                        TRACE_MSG2(acm->trace_tag,"START RECV: throttled: %d space_avail: %d", acm->flags & ACM_THROTTLED, 
+                                        acm->function_services->recv_space_available(acm, DATA_INTF));
+
+                        while (((acm->recv_urbs + 1) * 64) < acm->function_services->recv_space_available(acm, DATA_INTF)) {
+                                struct usbd_urb *urb;
+
+                                BREAK_IF(!(urb = usbd_alloc_urb(function, BULK_OUT, usbd_endpoint_transferSize(function, 
+                                                                BULK_OUT, usbd_high_speed(function)), acmfd_recv_urb)));
+                                acm->recv_urbs++;
+                                urb->function_privdata = acm;
+                                TRACE_MSG3(acm->trace_tag,"START RECV: %d urb#%p privdata#%p",acm->recv_urbs,urb,acm);
+                                CONTINUE_UNLESS (usbd_start_out_urb(urb));
+                                acm->recv_urbs--;
+                                TRACE_MSG1(acm->trace_tag,"START RECV: %d", acm->recv_urbs);
+                                usbd_free_urb(urb);
+                                break;
+                        }
+                        if (!acm->recv_urbs)
+                                acm_schedule_recv(acm, interface);
+                }
+                /* There needs to be at least one recv urb queued in order
+                 * to keep driving the push to the OS layer, so return how
+                 * many there are in case the OS layer must try again later.
+                 */
+                num_in_queue = acm->recv_urbs;
+                local_irq_restore(flags);
+                break;
+        }
+        return(num_in_queue);
+}
+
+
+struct acm_work acm_work;
+
+/*! acmfd_start_recv
+ * @param data
+ */
+STATIC void acmfd_start_recv(void *data) 
+{
+        struct acm_work *work = data;
+        struct acm_private *acm;
+        unsigned long flags;
+        int interface;
+
+        local_irq_save(flags);
+        acm = work->acm;
+        interface = work->interface;
+        work->acm = NULL;
+        work->interface = -1;
+        acm->recv_tqueue.data = NULL;
+        local_irq_restore(flags);
+        acmfd_start_recv_urbs(acm, interface);
+}
+
+
+/*! acm_schedule_recv
+ * @param acm
+ * @param interface
+ */
+void acm_schedule_recv(struct acm_private *acm, int interface)
+{
+        unsigned long flags;
+        struct acm_work *work = &acm_work;
+        TRACE_MSG1(acm->trace_tag, "sync: %d", acm->recv_tqueue.sync);
+        local_irq_save(flags);
+
+        UNLESS (acm->recv_tqueue.sync && acm->recv_tqueue.data) {
+                work->acm = acm;
+                work->interface = interface;
+                acm->recv_tqueue.routine = &acmfd_start_recv;
+                acm->recv_tqueue.data = work;
+                #if defined(LINUX24)
+                queue_task(&acm->recv_tqueue, &tq_timer);
+                #else
+                SCHEDULE_WORK(acm->recv_tqueue);
+                #endif
+        }
+
+        local_irq_restore(flags);
+}
+
+/* Transmit INTERRUPT ************************************************************************** */
+
+/*! acm_send_int_notfication
+ * Generates a response urb on the notification (INTERRUPT) endpoint.
+ * CALLED from interrupt context.
+ * @return non-zero if error
+ */
+STATIC int acm_send_int_notification(struct acm_private *acm, int bnotification, int data)
+{
+        struct usbd_function_instance *function = acm->function;
+        struct usbd_urb *urb = NULL;
+        struct cdc_notification_descriptor *notification;
+        unsigned long flags;
+        int rc = 0;
+
+        TRACE_MSG4(acm->trace_tag,"used: %d MOD_IN_USE: %d configured: %d data: %04x",
+                        atomic_read(&acm->used), MOD_IN_USE, acm->flags & ACM_CONFIGURED, data);
+
+        RETURN_ZERO_UNLESS((acm->flags & ACM_CONFIGURED));
+
+        local_irq_save(flags);
+
+        do {
+                BREAK_IF(!(urb = usbd_alloc_urb(function, INT_IN,
+                                                sizeof(struct cdc_notification_descriptor),
+                                                acmfd_urb_sent_int))); 
+
+                urb->function_privdata = acm;
+
+                memset(urb->buffer, 0, urb->buffer_length);
+                urb->actual_length = sizeof(struct cdc_notification_descriptor);
+
+                /* fill in notification structure */
+                notification = (struct cdc_notification_descriptor *) urb->buffer;
+
+                notification->bmRequestType = USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE;
+                notification->bNotification = bnotification;
+
+                switch (bnotification) {
+                case CDC_NOTIFICATION_NETWORK_CONNECTION:
+                        notification->wValue = data;
+                        break;
+                case CDC_NOTIFICATION_SERIAL_STATE:
+                        notification->wLength = cpu_to_le16(2);
+                        *((unsigned short *)notification->data) = cpu_to_le16(data);
+                        break;
+                }
+
+                BREAK_IF(!(rc = usbd_start_in_urb (urb)));
+
+                urb->function_privdata = NULL;
+                usbd_free_urb (urb);
+
+                TRACE_MSG1(acm->trace_tag,"urbID#%04x --> 0",urbID(urb));
+
+        } while(0);
+
+        local_irq_restore(flags);
+        return(rc);
+}
+
+/* Callback functions for TX urb completion (function chosen when urb is allocated) ***********/
+
+/*! acmfd_urb_sent_bulk - called to indicate bulk URB transmit finished
+ * @param urb pointer to struct usbd_urb
+ * @param rc result
+ * @return non-zero for error
+ */
+STATIC int acmfd_urb_sent_bulk (struct usbd_urb *urb, int rc)
+{
+        struct acm_private *acm = urb->function_privdata;
+        struct usbd_function_instance *function;
+
+        TRACE_MSG2(acm->trace_tag,"entered urbID#%04x rc=%d",urbID(urb),rc);
+        TRACE_MSG3(acm->trace_tag,"used: %d MOD_IN_USE: %d configured: %d",
+                        atomic_read(&acm->used), MOD_IN_USE, acm->flags & ACM_CONFIGURED);
+
+        if (!urb || !(function = urb->function_instance)) {
+                TRACE_MSG1(acm->trace_tag,"urbID#%04x --> -EINVAL",urbID(urb));
+                return(-EINVAL);
+        }
+        TRACE_MSG1(acm->trace_tag,"IN length=%d",urb->actual_length);
+
+        atomic_sub(urb->actual_length, &acm->queued_bytes);
+        atomic_dec(&acm->queued_urbs);
+        urb->function_privdata = NULL;
+        usbd_free_urb (urb);
+        if (acm->flags & ACM_OPENED)
+                acm->function_services->schedule_wakeup_writers(acm);
+        TRACE_MSG1(acm->trace_tag,"urbID#%04x --> 0",urbID(urb));
+        return 0;
+}
+
+/*! acmfd_urb_sent_int - called to indicate int URB transmit finished
+ * @param urb pointer to struct usbd_urb
+ * @param rc result
+ * @return non-zero for error
+ */
+STATIC int acmfd_urb_sent_int (struct usbd_urb *urb, int rc)
+{
+        struct acm_private *acm = urb->function_privdata;
+        struct usbd_function_instance *function;
+
+        TRACE_MSG2(acm->trace_tag,"entered urbID#%04x rc=%d",urbID(urb),rc);
+        TRACE_MSG3(acm->trace_tag,"used: %d MOD_IN_USE: %d configured: %d",
+                        atomic_read(&acm->used), MOD_IN_USE, acm->flags & ACM_CONFIGURED);
+        if (!urb || !(function = urb->function_instance)) {
+                TRACE_MSG1(acm->trace_tag,"urbID#%04x --> -EINVAL",urbID(urb));
+                return(-EINVAL);
+        }
+        TRACE_MSG1(acm->trace_tag,"INT length=%d",urb->actual_length);
+
+        urb->function_privdata = NULL;
+        usbd_free_urb(urb);
+        TRACE_MSG1(acm->trace_tag,"urbID#%04x --> 0",urbID(urb));
+        return 0;
+}
+
+/*! acmfd_urb_sent_ep0 - called to indicate ep0 URB transmit finished
+ * @param urb pointer to struct usbd_urb
+ * @param rc result
+ * @return non-zero for error
+ */
+STATIC int acmfd_urb_sent_ep0 (struct usbd_urb *urb, int rc)
+{
+        struct acm_private *acm = urb->function_privdata;
+        struct usbd_function_instance *function;
+
+        TRACE_MSG2(acm->trace_tag,"entered urbID#%04x rc=%d",urbID(urb),rc);
+        TRACE_MSG3(acm->trace_tag,"used: %d MOD_IN_USE: %d configured: %d",
+                        atomic_read(&acm->used), MOD_IN_USE, acm->flags & ACM_CONFIGURED);
+
+        RETURN_EINVAL_IF (!urb || !(function = urb->function_instance));
+        
+        TRACE_MSG1(acm->trace_tag,"INT length=%d",urb->actual_length);
+
+        urb->function_privdata = NULL;
+        usbd_free_urb(urb);
+        return 0;
+}
+
+/* USB Device Functions ************************************************************************ */
+
+typedef enum mesg {
+        mesg_unknown,
+        mesg_configured,
+        mesg_reset,
+} mesg_t;
+mesg_t acm_last_mesg;
+
+char * acm_messages[3] = {
+        "",
+        "ACM Configured",
+        "ACM Reset",
+};
+
+
+/*! acmfd_check_mesg
+ * @param curr_mesg
+ */
+void acmfd_check_mesg(mesg_t curr_mesg)
+{
+        RETURN_UNLESS(acm_last_mesg != curr_mesg);
+        acm_last_mesg = curr_mesg;
+        otg_message(acm_messages[curr_mesg]);
+}
+
+
+/*! acmfd_event_handler - process a device event
+ * @param function
+ * @param event
+ * @param data
+ */
+void acmfd_event_handler (struct usbd_function_instance *function, usbd_device_event_t event, int data)
+{
+        struct acm_private *acm = function->privdata;
+        int i;
+
+        TRACE_MSG1(acm->trace_tag,"entered ev: %d",event);
+        TRACE_MSG3(acm->trace_tag,"used: %d MOD_IN_USE: %d ready: %d", atomic_read(&acm->used), MOD_IN_USE, acm_ready(acm));
+
+        switch (event) {
+
+        case DEVICE_CONFIGURED:
+                TRACE_MSG0(acm->trace_tag,"CONFIGURED");
+                acm->flags |= ACM_CONFIGURED;
+                acmfd_check_mesg(mesg_configured);
+                acmfd_start_recv_urbs(acm, DATA_INTF);
+                break;
+
+        case DEVICE_RESET:
+        case DEVICE_DE_CONFIGURED:
+                TRACE_MSG0(acm->trace_tag,"RESET");
+
+                /* if configured and open then schedule hangup
+                 */
+                if ((acm->flags & ACM_OPENED) && (acm->flags & ACM_CONFIGURED))
+                        acm->function_services->schedule_hangup(acm);
+
+                acm->flags &= ~(ACM_CONFIGURED | ACM_CARRIER);
+                acmfd_check_mesg(mesg_reset);
+                BREAK_IF(!acm->flags & ACM_CONFIGURED);
+                TRACE_MSG0(acm->trace_tag,"RESET continue");
+                // XXX flush
+                // Release any queued urbs
+                break;
+
+        default:
+                break;
+        }
+        TRACE_MSG0(acm->trace_tag,"exited");
+}
+
+/*! acm_write_room
+ * @param acm
+ * @param interface
+ * @return non-zero if error
+ */
+STATIC int acm_write_room(struct acm_private *acm, int interface)
+{
+        switch (interface) {
+        case COMM_INTF:
+                return 0;
+        case DATA_INTF:
+                return (!acm->function || max_queued_urbs <= atomic_read(&acm->queued_urbs) ||
+                                max_queued_bytes <= atomic_read(&acm->queued_bytes) ) ?  0 : acm->writesize ;
+        }
+        return 0;
+}
+
+/*! acm_chars_in_buffer
+ * @param acm
+ * @param interface
+ * @return non-zero if error
+ */
+STATIC int acm_chars_in_buffer(struct acm_private *acm, int interface)
+{
+        int rc;
+        switch (interface) {
+        case COMM_INTF:
+                return 0;
+        case DATA_INTF:
+                return atomic_read(&acm->queued_bytes);
+        }
+        return 0;
+}
+
+static int throttle_count = 0;
+static int unthrottle_count = 0;
+
+/*! acm_throttle
+ * @param acm
+ * @param interface
+ */
+STATIC void acm_throttle(struct acm_private *acm, int interface)
+{
+        switch (interface) {
+        case COMM_INTF:
+                break;
+        case DATA_INTF:
+                throttle_count += 1;
+                TRACE_MSG1(acm->trace_tag,"entered %d",throttle_count);
+                acm->flags |= ACM_THROTTLED;
+                TRACE_MSG1(acm->trace_tag,"exited %d",throttle_count);
+                break;
+        }
+}
+
+/*! acm_unthrottle
+ * @param acm
+ * @param interface
+ */
+STATIC void acm_unthrottle(struct acm_private *acm, int interface)
+{
+        switch (interface) {
+        case COMM_INTF:
+                break;
+        case DATA_INTF:
+                unthrottle_count += 1;
+                TRACE_MSG1(acm->trace_tag,"entered %d",unthrottle_count);
+                acm->flags &= ~ACM_THROTTLED;
+                TRACE_MSG1(acm->trace_tag,"exited %d",unthrottle_count);
+                UNLESS(acm->recv_urbs)
+                        acm_schedule_recv(acm, interface);
+                break;
+        }
+}
+
+/*! acm_xmit_chars
+ * @param acm
+ * @param interface
+ * @param count
+ * @param from_user
+ * @param buf
+ * @return number of bytes sent.
+ */
+STATIC int acm_xmit_chars(struct acm_private *acm, int interface, int count, int from_user, const unsigned char *buf)
+{
+        struct usbd_function_instance *function = acm->function;
+        struct usbd_urb *urb;
+
+        RETURN_ZERO_UNLESS((acm->flags & ACM_CONFIGURED) /*&& (acm->flags & ACM_CARRIER)*/);
+
+        TRACE_MSG2(acm->trace_tag, "count: %d from_user: %d", count, from_user);   
+
+        switch (interface) {
+        case COMM_INTF:
+                return 0;
+        case DATA_INTF:
+                // sanity check and are we connect
+                RETURN_ZERO_UNLESS(atomic_read(&acm->used));
+                TRACE_MSG0(acm->trace_tag,"used OK");
+                RETURN_ZERO_UNLESS (count);
+                RETURN_ZERO_UNLESS (acm->flags & ACM_CONFIGURED);
+                TRACE_MSG0(acm->trace_tag,"connected OK");  
+                RETURN_ZERO_IF(max_queued_urbs <= atomic_read(&acm->queued_urbs));
+                TRACE_MSG0(acm->trace_tag,"max_queued_urbs OK");
+
+                // allocate a write urb
+                count = MIN(count, acm->writesize);
+
+                RETURN_ZERO_UNLESS ((urb = usbd_alloc_urb (function, BULK_IN, count, acmfd_urb_sent_bulk)));
+
+                if (from_user)
+                        copy_from_user ((void *)urb->buffer, (void *)buf, count);
+                else
+                        memcpy ((void *)urb->buffer, (void *)buf, count);
+
+                urb->function_privdata = acm;
+                urb->actual_length = count;
+                atomic_add(count, &acm->queued_bytes);
+                atomic_inc(&acm->queued_urbs);
+                usbd_start_in_urb(urb);
+                TRACE_MSG2(acm->trace_tag,"urbID#%04x --> count: %d",urbID(urb),count);
+                return count;
+        default:
+                break;
+        }
+        return 0;
+}
+
+/*! acmfd_recv_urb
+ * @param urb
+ * @param rc
+ * @return non-zero if error
+ */
+STATIC int acmfd_recv_urb (struct usbd_urb *urb, int rc)
+{
+        /* Return 0 if urb has been accepted,
+         * return 1 and expect caller to deal with urb release otherwise. 
+         */
+        struct acm_private *acm = urb->function_privdata;
+        unsigned long flags;
+
+        local_irq_save(flags);
+        acm->recv_urbs--;               // this could probably be atomic operation....
+        local_irq_restore(flags);
+
+        if (RECV_CANCELLED == rc) {
+                TRACE_MSG1(acm->trace_tag,"cancelled URB=%p",urb);
+                return -EINVAL;
+        }
+        if (RECV_OK != rc) {
+                TRACE_MSG2(acm->trace_tag,"rejected URB=%p rc=%d",urb,rc);
+                usbd_free_urb(urb);
+                acm_schedule_recv(acm, DATA_INTF);
+                //acmfd_start_recv_urbs(acm, DATA_INTF);
+                return 0;
+        }
+
+        /* loopback mode
+         */
+        if (acm->flags & ACM_LOOPBACK) 
+                acm_xmit_chars(acm, DATA_INTF, urb->actual_length, 0, urb->buffer);
+
+        /* acmfd_start_recv_urbs() will never queue more urbs than there is currently
+         * room in the upper layer buffer for. So we are guaranteed that any data actually
+         * received can be given to the upper layers without worrying if we will 
+         * actually have room. 
+         */
+        else 
+                if ((rc = acm->function_services->recv_chars(acm, DATA_INTF, urb->buffer, urb->actual_length))) 
+                        return(rc); // XXX
+
+
+        acm->bytes_received += urb->actual_length;
+        TRACE_MSG1(acm->trace_tag,"bytes_received: %d",acm->bytes_received);
+        usbd_free_urb(urb);
+        acm_schedule_recv(acm, DATA_INTF);
+        return 0;
+
+#if 0
+        // XXX it may be reasonable to schedule here....
+        UNLESS (acmfd_start_recv_urbs(acm, DATA_INTF)) {
+                /* If start recv returns zero it means that there are no-queued urbs,
+                 * and we should queue a work item to restart.
+                 */
+                acm_schedule_recv(acm, DATA_INTF);
+        }
+#endif
+        return 0;
+}
+
+/*! acmfd_line_coding_urb_received - callback for sent URB
+ *
+ * Handles notification that an urb has been sent (successfully or otherwise).
+ *
+ * @param urb
+ * @param urb_rc
+ * @return non-zero for failure.
+ */
+STATIC int acmfd_line_coding_urb_received (struct usbd_urb *urb, int urb_rc)
+{
+        struct acm_private *acm = urb->function_privdata;
+        TRACE_MSG2(acm->trace_tag,"urbID#%04x rc=%d",urbID(urb),urb_rc);
+
+        RETURN_EINVAL_IF (RECV_OK != urb_rc);
+        RETURN_EINVAL_IF (urb->actual_length < sizeof(struct cdc_acm_line_coding));
+
+        RETURN_EINVAL_UNLESS (memcpy(&acm->line_coding, urb->buffer, sizeof(struct cdc_acm_line_coding)));
+
+        // something changed, copy and notify
+        
+        memcpy(&acm->line_coding, urb->buffer, sizeof(struct cdc_acm_line_coding));
+
+        // XXX notify application if baudrate has changed
+
+        return -EINVAL;         // caller will de-allocate
+}
+
+/*! acmfd_device_request - called to indicate urb has been received
+ * @param function
+ * @param request
+ * @return non-zero if error
+ */
+int acmfd_device_request (struct usbd_function_instance *function, struct usbd_device_request *request)
+{
+        struct acm_private *acm = (struct acm_private *) (function->privdata);
+
+        TRACE_MSG3(acm->trace_tag,"used: %d MOD_IN_USE: %d configured: %d",
+                        atomic_read(&acm->used), MOD_IN_USE, acm->flags & ACM_CONFIGURED);
+
+        TRACE_SETUP(acm->trace_tag,request);
+
+        // verify that this is a usb class request per cdc-acm specification or a vendor request.
+        if (!(request->bmRequestType & (USB_REQ_TYPE_CLASS | USB_REQ_TYPE_VENDOR))) {
+                TRACE_MSG0(acm->trace_tag,"--> 0");
+                return(0);
+        }
+
+        // determine the request direction and process accordingly
+        switch (request->bmRequestType & (USB_REQ_DIRECTION_MASK | USB_REQ_TYPE_MASK)) {
+
+        case USB_REQ_HOST2DEVICE | USB_REQ_TYPE_CLASS:
+                switch (request->bRequest) {
+                case CDC_CLASS_REQUEST_SEND_ENCAPSULATED: break;
+                case CDC_CLASS_REQUEST_SET_COMM_FEATURE: break;
+                case CDC_CLASS_REQUEST_CLEAR_COMM_FEATURE: break;
+                case CDC_CLASS_REQUEST_SET_LINE_CODING:
+                        {
+                                struct usbd_urb *urb;
+                                int len = le16_to_cpu(request->wLength);
+                                TRACE_MSG1(acm->trace_tag,"SET_LINE_CODING wLength=%d",len);
+                                if (len <= 0) {
+                                        TRACE_MSG0(acm->trace_tag,"(len<=0)--> 0");
+                                        return(0);
+                                }
+
+                                /* Set up an ep0 recv urb for the rest of it. */
+                                UNLESS ((urb = usbd_alloc_urb_ep0(function, len, acmfd_line_coding_urb_received))) {
+                                        TRACE_MSG0(acm->trace_tag,"no mem for ep0 recv urb");
+                                        return(-ENOMEM);
+                                }
+                                urb->function_privdata = acm;
+                                if (usbd_start_out_urb(urb)) {
+                                        TRACE_MSG0(acm->trace_tag,"usbd_start_out_urb() failed");
+                                        usbd_free_urb(urb);          // de-alloc if error
+                                        TRACE_MSG0(acm->trace_tag,"--> -EINVAL");
+                                        return(-EINVAL);
+                                }
+                        }
+                        break;
+
+                case CDC_CLASS_REQUEST_SET_CONTROL_LINE_STATE:
+                        {
+                                unsigned int prev_bmLineState = acm->bmLineState;
+                                acm->bmLineState = le16_to_cpu(request->wValue);
+
+                                // schedule writers or hangup IFF open
+                                BREAK_IF(!acm->privdata);
+                                TRACE_MSG3(acm->trace_tag,"set control state, bmLineState: %04x previous: %04x changed: %04x",
+                                                acm->bmLineState, prev_bmLineState, acm->bmLineState ^ prev_bmLineState);
+
+                                // make sure there really is a state change
+                                if ((acm->bmLineState ^ prev_bmLineState) & CDC_LINESTATE_D0_DTR) {
+
+                                        TRACE_MSG1(acm->trace_tag,"DTR state changed -> %x",
+                                                        (acm->bmLineState & CDC_LINESTATE_D0_DTR));
+
+                                        if (acm->bmLineState & CDC_LINESTATE_D0_DTR) {
+                                                if (acm->flags & ACM_OPENED)
+                                                        acm->function_services->schedule_wakeup_writers(acm);
+                                                acm->flags |= ACM_CARRIER;
+                                                acm_schedule_recv(acm, DATA_INTF);
+                                        }
+                                        else {
+                                                if (acm->flags & ACM_OPENED)
+                                                        acm->function_services->schedule_hangup(acm);
+
+                                                acm->flags &= ~ACM_CARRIER;
+                                                acm_flush(acm, DATA_INTF);
+                                        }
+                                        
+                                        /* wake up blocked opens */
+                                        acm->function_services->wakeup_opens(acm);
+
+                                        /* wake up blocked ioctls */
+                                        acm->function_services->wakeup_state(acm);
+
+                                }
+
+
+
+                                /* send notification if we have DCD */
+                                TRACE_MSG2(acm->trace_tag,"bmUARTState: %04x privdata: %p sending (DCD|DSR) notification",
+                                                acm->bmUARTState, acm->privdata);
+                                
+                                acm_send_int_notification(acm, CDC_NOTIFICATION_SERIAL_STATE, acm->bmUARTState);
+                        }
+                        break;
+
+                case CDC_CLASS_REQUEST_SEND_BREAK: break;
+                default: break;
+                }
+                TRACE_MSG0(acm->trace_tag,"--> 0");
+                return 0;
+
+        case USB_REQ_DEVICE2HOST | USB_REQ_TYPE_CLASS:
+                switch (request->bRequest) {
+                case CDC_CLASS_REQUEST_GET_ENCAPSULATED: break;
+                case CDC_CLASS_REQUEST_GET_COMM_FEATURE: break;
+                case CDC_CLASS_REQUEST_GET_LINE_CODING: 
+                        {
+                                struct usbd_urb *urb;
+                                RETURN_ENOMEM_IF (!(urb = usbd_alloc_urb_ep0(function, sizeof(struct cdc_acm_line_coding), 
+                                                                acmfd_urb_sent_ep0)));
+                                urb->function_privdata = acm;
+
+                                memcpy(urb->buffer, &acm->line_coding, sizeof(struct cdc_acm_line_coding));
+
+                                urb->actual_length = sizeof(struct cdc_acm_line_coding);
+
+                                TRACE_MSG1(acm->trace_tag,"sending line coding urb: %p",(u32)(void*)urb);
+                                RETURN_ZERO_UNLESS(usbd_start_in_urb(urb));
+                                usbd_free_urb(urb);
+                                TRACE_MSG0(acm->trace_tag,"(send failed)--> -EINVAL");
+                        }
+                        return -EINVAL;
+                default: break;
+                }
+                TRACE_MSG0(acm->trace_tag,"--> 0");
+                return 0;
+
+        case USB_REQ_HOST2DEVICE | USB_REQ_TYPE_VENDOR: break;
+        case USB_REQ_DEVICE2HOST | USB_REQ_TYPE_VENDOR: break;
+
+        default: break;
+        }
+        TRACE_MSG0(acm->trace_tag,"--> 0");
+        return 0;
+}
+
+
+/*! acmfd_function_enable
+ * @param function
+ * @return non-zero if error
+ */
+STATIC int acmfd_function_enable (struct usbd_function_instance *function)
+{
+        struct acm_private *acm = (struct acm_private *) (function->privdata);
+        
+        TRACE_MSG0(acm->trace_tag,"entered");
+        TRACE_MSG1(acm->trace_tag,"INC: %d", MOD_IN_USE);
+        acm->function_services->enable(function);
+        acm->writesize = usbd_endpoint_wMaxPacketSize(function, BULK_OUT, 0) * 24;
+
+        TRACE_MSG0(acm->trace_tag,"-> 0");
+        return 0;
+}
+
+/*! acmfd_function_disable
+ * @param function
+ */
+STATIC void acmfd_function_disable (struct usbd_function_instance *function)
+{
+        struct acm_private *acm = (struct acm_private *) (function->privdata);
+        
+        TRACE_MSG0(acm->trace_tag,"entered");
+        acm->writesize = 0;
+        acm->function_services->disable(function);
+        TRACE_MSG1(acm->trace_tag,"DEC: %d", MOD_IN_USE);
+        TRACE_MSG0(acm->trace_tag,"exited");
+}
+
+
+/*! function_ops 
+ */
+struct usbd_function_operations function_ops = {
+        event_handler: acmfd_event_handler,
+        device_request: acmfd_device_request,
+        function_enable: acmfd_function_enable,
+        function_disable: acmfd_function_disable,
+};
+
+/*! acm_fd_init
+ * @param acm
+ * @param usbd_module_info
+ * @param vendor_id
+ * @param product_id
+ * @param wmax_urbs
+ * @param wmax_bytes
+ * @return non-zero if error
+ */
+STATIC int acm_fd_init(struct acm_private *acm, char *usbd_module_info, u32 vendor_id, u32 product_id,
+                            u32 wmax_urbs, u32 wmax_bytes)
+{
+        
+        TRACE_MSG0(acm->trace_tag,"entered");
+
+        if (vendor_id) 
+                acm->function_driver->idVendor = cpu_to_le16(vendor_id);
+        if (product_id) 
+                acm->function_driver->idProduct = cpu_to_le16(product_id);
+        max_queued_urbs = wmax_urbs;
+        max_queued_bytes = wmax_bytes;
+
+        THROW_IF (NULL == (acm->function = usbd_register_function (acm->function_driver, "acm-fd", acm)), error);
+
+        CATCH(error) {
+                printk(KERN_ERR"%s: ERROR\n", __FUNCTION__);
+
+                if (acm->function) {
+                        usbd_deregister_function (acm->function);
+                        acm->function = NULL;
+                }
+                TRACE_MSG0(acm->trace_tag,"--> -EINVAL");
+                return -EINVAL;
+        }
+        TRACE_MSG0(acm->trace_tag,"--> 0");
+        return 0;
+}
+
+
+
+/*! acm_wait_task
+ * @param acm
+ * @param queue
+ */
+void acm_wait_task(struct acm_private *acm, WORK_ITEM *queue)
+{
+        TRACE_MSG1(acm->trace_tag,"entered data=%p",queue->data);
+        RETURN_IF(!queue->data);
+        queue->data = NULL;
+#if defined(LINUX24)
+        while (queue->sync) {
+                TRACE_MSG1(acm->trace_tag,"waiting for queue: %p",queue);
+                schedule_timeout(HZ);
+        }
+#else
+        while(PENDING_WORK_ITEM((*queue))){
+                TRACE_MSG1(acm->trace_tag,"waiting for queue: %p",queue);
+                SCHEDULE_TIMEOUT(1);  // 1 second delay 
+        }
+#endif
+        TRACE_MSG0(acm->trace_tag,"exited");
+}
+
+
+/*! acm_get_dtr
+ * Get DTR status.
+ */
+int acm_get_dtr(struct acm_private *acm)
+{
+        return (acm->bmLineState & CDC_LINESTATE_D0_DTR) ? 1 : 0;
+}
+
+/*! acm_get_dsr
+ * Get DSR status
+ */
+int acm_get_dsr(struct acm_private *acm)
+{
+        return (acm->bmUARTState & CDC_UARTSTATE_BTXCARRIER_DSR) ? 1 : 0;
+}
+
+/*! acm_get_dcd
+ * Get DCD status
+ */
+int acm_get_dcd(struct acm_private *acm)
+{
+        return (acm->bmUARTState & CDC_UARTSTATE_BRXCARRIER_DCD) ? 1 : 0;
+}
+
+/*! acm_set_dsr
+ * Set DSR status
+ * @param acm
+ * @param value
+ */
+void acm_set_dsr(struct acm_private *acm, int value)
+{
+        acm->bmUARTState &= ~CDC_UARTSTATE_BTXCARRIER_DSR;
+        acm->bmUARTState |= value ? CDC_UARTSTATE_BTXCARRIER_DSR : 0;
+        acm_send_int_notification(acm, CDC_NOTIFICATION_SERIAL_STATE, acm->bmUARTState);
+}
+
+/*! acm_set_dcd
+ * Set DCD status
+ * @param acm
+ * @param value
+ */
+void acm_set_dcd(struct acm_private *acm, int value)
+{
+        acm->bmUARTState &= ~CDC_UARTSTATE_BRXCARRIER_DCD;
+        acm->bmUARTState |= value ? CDC_UARTSTATE_BRXCARRIER_DCD : 0;
+        acm_send_int_notification(acm, CDC_NOTIFICATION_SERIAL_STATE, acm->bmUARTState);
+}
+
+/*! acm_ring
+ * Indicate Ring signal to host.
+ */
+void acm_ring(struct acm_private *acm)
+{
+        acm->bmUARTState |= CDC_UARTSTATE_BRINGSIGNAL;
+        acm_send_int_notification(acm, CDC_NOTIFICATION_SERIAL_STATE, acm->bmUARTState);
+        acm->bmUARTState &= ~CDC_UARTSTATE_BRINGSIGNAL;
+}
+
+/*! acm_send_break
+ * Indicate Break signal to host.
+ */
+void acm_send_break(struct acm_private *acm)
+{
+        acm->bmUARTState |= CDC_UARTSTATE_BBREAK;
+        acm_send_int_notification(acm, CDC_NOTIFICATION_SERIAL_STATE, acm->bmUARTState);
+        acm->bmUARTState &= ~CDC_UARTSTATE_BBREAK;
+}
+
+/*! acm_overrun
+ * Indicate Overrun signal to host.
+ */
+void acm_overrun(struct acm_private *acm)
+{
+        acm->bmUARTState |= CDC_UARTSTATE_BOVERRUN;
+        acm_send_int_notification(acm, CDC_NOTIFICATION_SERIAL_STATE, acm->bmUARTState);
+        acm->bmUARTState &= ~CDC_UARTSTATE_BOVERRUN;
+}
+
+/*! acm_set_local
+ * Set LOCAL status
+ * @param acm
+ * @param value
+ */
+void acm_set_local(struct acm_private *acm, int value)
+{
+        acm->flags &= ~ACM_LOCAL;
+        acm->flags |= value ? ACM_LOCAL : 0;
+}
+
+/*! acm_set_loopback
+ * Set LOOPBACK status
+ * @param acm
+ * @param value
+ */
+void acm_set_loopback(struct acm_private *acm, int value)
+{
+        acm->flags &= ~ACM_LOOPBACK;
+        acm->flags |= value ? ACM_LOOPBACK : 0;
+}
+
+
+
+/*! acm_fd_term
+ * @param acm
+ */
+STATIC void acm_fd_exit(struct acm_private *acm)
+{
+        //acm_wait_task(acm->recv_tqueue);
+        RETURN_UNLESS (acm->function);
+        usbd_deregister_function (acm->function);
+        acm->function = NULL;
+}
+
+/*!
+ * Function table exported to the OS specific upper layer.
+ */
+struct acm_usb_services acm_fd_usb_ops = {
+        .fd_init = acm_fd_init,
+        .fd_exit = acm_fd_exit,
+        .xmit_chars = acm_xmit_chars,
+        .send_int_notification = acm_send_int_notification,
+        .throttle = acm_throttle,
+        .unthrottle = acm_unthrottle,
+        .write_room = acm_write_room,
+        .chars_in_buffer = acm_chars_in_buffer,
+        .wait_task = acm_wait_task,
+        .schedule_recv = acm_schedule_recv,
+        .open = acm_open, 
+        .close = acm_close, 
+        .flush = acm_flush, 
+        .ready = acm_ready, 
+        .get_dtr = acm_get_dtr,
+        .get_dsr = acm_get_dsr,
+        .get_dcd = acm_get_dcd,
+        .set_dsr = acm_set_dsr,
+        .set_dcd = acm_set_dcd,
+        .ring = acm_ring,
+        .send_break = acm_send_break,
+        .overrun = acm_overrun,
+        .set_local = acm_set_local,
+        .set_loopback = acm_set_loopback,
+};
+
diff -uNr linux/drivers/no-otg/functions/acm/acm-fd.h linux/drivers/otg/functions/acm/acm-fd.h
--- linux/drivers/no-otg/functions/acm/acm-fd.h	1970-01-01 01:00:00.000000000 +0100
+++ linux/drivers/otg/functions/acm/acm-fd.h	2006-09-01 21:41:26.000000000 +0200
@@ -0,0 +1,242 @@
+/* 
+ * otg/functions/acm/acm-fd.h
+ *
+ *      Copyright (c) 2003, 2004 Belcarra
+ *
+ * By: 
+ *      Tom Rushworth <tbr@belcarra.com>, 
+ *      Stuart Lynne <sl@belcarra.com>, 
+ *      Bruce Balden <balden@belcarra.com>
+ *
+ */
+/*!
+ * @file otg/functions/acm/acm-fd.h
+ * @brief ACM Function Driver private defines
+ *
+ * The top and bottom halves of the driver comunication via these structures.
+ *
+ * @ingroup ACMFunction
+ */
+
+#ifndef ACM_FD_H
+#define ACM_FD_H 1
+
+typedef void (*cpy_fn)(u8 *dst, u8 *src, int len);
+
+/*! acm_usb_services
+ * Services that the acm_fd library provides.
+ */
+struct acm_usb_services {
+
+        /*! fd_init - called to initialize the acm_fd library
+         * This call initialized the acm_fd library and registers the function driver 
+         * with the USB Peripheral Stack.
+         */
+        int (*fd_init)(struct acm_private *acm, char *inf, u32 vendor_id, u32 product_id, u32 wmax_urbs, u32 wmax_bytes);
+
+        /*! fd_exit - called before exiting
+         * This call will cause the function driver to be de-registered.
+         */
+        void (*fd_exit)(struct acm_private *acm);
+
+        /*! open - device open
+         * This is called the device is opened (first open only if non-exclusive opens allowed).
+         */
+        int (*open)(struct acm_private *acm, int interface);
+
+        /*! close - Device close
+         * This is called when the device closed (last close only if non-exclusive opens allowed.)
+         */
+        int (*close)(struct acm_private *acm, int interface);
+
+
+        /*! flush - flush data urbs.
+         * Cancel outstanding data urbs.
+         */
+        void (*flush)(struct acm_private *acm, int interface);
+
+        /*! schedule_recv - queue as many data receive urbs as possible
+         * This will schedule a bottom half hander that will will start as
+         * many receive data urbs as are allowed given the amount of room
+         * available in the upper layer. If no urbs are queued by the
+         * bottom half handler it will re-schedule itself.
+         */
+        void (*schedule_recv)(struct acm_private *acm, int interface);
+
+        /*! throttle - set throttle flag for specified interface
+         * Receive urbs will not be queued when throttled.
+         */
+        void (*throttle)(struct acm_private *acm, int interface);
+
+        /*! unthrottle - reset throttle flag for specified interface
+         * Receive urbs are allowed to be queued. If no urbs are queued a
+         * bottom half handler will be scheduled to queue them.
+         */
+        void (*unthrottle)(struct acm_private *acm, int interface);
+
+
+        /*! xmit_chars - send data via specified interface
+         * This will start a transmit urb to send the specified data. The
+         * number of characters sent will be returned.
+         */
+        int (*xmit_chars)(struct acm_private *acm, int interface, int count, int from_user, const unsigned char *buf);
+
+        /*! write_room
+         * Return amount of data that could be queued for sending.
+         */
+        int (*write_room)(struct acm_private *acm, int interface);
+
+        /*! chars_in_buffer
+         * Return number of chars in xmit buffer.
+         */
+        int (*chars_in_buffer)(struct acm_private *acm, int interface);
+
+
+        /*! send_int_notification - send notification via interrupt endpoint
+         *  This can be used to queue network, serial state change notifications.
+         */
+        int (*send_int_notification)(struct acm_private *acm, int bnotification, int data);
+
+        /*! wait_task - wait for task to complete.
+        */
+        void (*wait_task)(struct acm_private *acm, WORK_ITEM *queue);
+
+        /*! ready - return true if connected and carrier
+        */
+        int (*ready)(struct acm_private *acm);
+
+        /*! get_dtr
+         * Get DTR status.
+         */
+        int (*get_dtr)(struct acm_private *acm);
+
+        /*! get_dsr
+         * Get DSR status
+         */
+        int (*get_dsr)(struct acm_private *acm);
+
+        /*! get_dcd
+         * Get DCD status
+         */
+        int (*get_dcd)(struct acm_private *acm);
+
+        /*! set_dsr
+         * Set DSR status
+         */
+        void (*set_dsr)(struct acm_private *acm, int value);
+
+        /*! set_dcd
+         * Set DCD status
+         */
+        void (*set_dcd)(struct acm_private *acm, int value);
+
+        /*! ring
+         * Indicate Ring signal to host.
+         */
+        void (*ring)(struct acm_private *acm);
+
+        /*! send_break
+         * Indicate Break signal to host.
+         */
+        void (*send_break)(struct acm_private *acm);
+
+        /*! overrun
+         * Indicate Overrun signal to host.
+         */
+        void (*overrun)(struct acm_private *acm);
+
+
+        /*! set_local
+         * Set LOCAL status
+         */
+        void (*set_local)(struct acm_private *acm, int value);
+
+        /*! set_loopback - set loopback mode
+         * Sets LOOP flag, data received from the host will be immediately
+         * returned without passing to the upper layer.
+         */
+        void (*set_loopback)(struct acm_private *acm, int value);
+};
+
+/*! acm_function_services
+ * Services that the top level driver provides to the lower library.
+ */
+struct acm_function_services {
+        /*! enable
+         * Enable the function driver.
+         */
+        void (*enable)(struct usbd_function_instance *function);
+
+        /*! disable
+         * Disable the function driver.
+         */
+        void (*disable)(struct usbd_function_instance *function);
+
+        /*! wakeup_opens
+         * Wakeup processes waiting for DTR.
+         */
+        void (*wakeup_opens)(struct acm_private *acm);
+
+        /*! wakeup_opens
+         * Wakeup processes waiting for state change.
+         */
+        void (*wakeup_state)(struct acm_private *acm);
+
+        /*! wakeup writers
+         * Wakeup pending writes.
+         */
+        void (*schedule_wakeup_writers)(struct acm_private *acm);
+
+        /*! recv_space_available
+         * Check for amount of receive space that is available, controls
+         * amount of receive urbs that will be queued.
+         */
+        int (*recv_space_available)(struct acm_private *acm, int interface);
+
+        /*! recv_chars
+         * Process chars received on specified interface.
+         */
+        int (*recv_chars)(struct acm_private *acm, int interface, u8 *cp, int n);
+
+
+        /*! schedule_hangup
+         * Schedule a work item that will perform a hangup.
+         */
+        void (*schedule_hangup)(struct acm_private *acm);
+
+        /*! comm_feature
+         * Tell function that comm feature has changed.
+         */
+        void (*comm_feature)(struct acm_private *acm);
+
+        /*! line_coding
+         * Tell function that line coding has changed.
+         */
+        void (*line_coding)(struct acm_private *acm);
+
+        /*! control_state
+         * Tell function that control state has changed.
+         */
+        void (*control_state)(struct acm_private *acm);
+
+        /*! send_break
+         * Tell function to send a break signal.
+         */
+        void (*send_break)(struct acm_private *acm);
+
+};
+
+extern struct acm_usb_services acm_fd_usb_ops;
+extern struct usbd_function_operations function_ops;
+
+
+/*
+ * otg-trace tag.
+ */
+extern otg_tag_t acm_fd_trace_tag;
+
+#define MAX_QUEUED_BYTES        256
+#define MAX_QUEUED_URBS         10     // Max for write
+
+
+#endif
diff -uNr linux/drivers/no-otg/functions/acm/acm-os.h linux/drivers/otg/functions/acm/acm-os.h
--- linux/drivers/no-otg/functions/acm/acm-os.h	1970-01-01 01:00:00.000000000 +0100
+++ linux/drivers/otg/functions/acm/acm-os.h	2006-09-01 21:41:26.000000000 +0200
@@ -0,0 +1,74 @@
+/*
+ * otg/functions/acm/acm-os.h
+ *
+ *      Copyright (c) 2003, 2004 Belcarra
+ *
+ * By: 
+ *      Tom Rushworth <tbr@belcarra.com>, 
+ *      Stuart Lynne <sl@belcarra.com>, 
+ *      Bruce Balden <balden@belcarra.com>
+ *
+ *
+ */
+/*!
+ * @file otg/functions/acm/acm-os.h
+ * @brief ACM Function Driver private defines
+ *
+ * An ACM (Abstract Control Model) driver is composed of two pieces:
+ *    1) An OS specific piece that handles creating and operating
+ *       a serial device for the given OS.
+ *    2) A USB specific piece that interfaces either with the host
+ *       usbcore layer, or with the otgcore layer.
+ *
+ * If the USB piece interfaces with the host usbcore layer you get
+ * an ACM class driver.  If the USB piece interfaces with the otgcore
+ * layer you get an ACM function driver.
+ *
+ * This file describes the functions exported by the various acm-*-os.c
+ * files (implementing (1)) for use in acm-fd.c (2).
+ *
+ * @ingroup ACMFunction
+ */
+
+#ifndef ACM_OS_H
+#define ACM_OS_H 1
+
+/*
+ * acm_os_recv_space_available - return the number of bytes of data
+ *                               the OS specific piece can accept without
+ *                               dropping some.
+ */
+extern int acm_os_recv_space_available(struct acm_private *acm);
+
+/*
+ * acm_os_recv_chars - receive n bytes starting at cp.  This will never be
+ *                     more than the last call to acm_os_recv_space_available().
+ *                     This will be called from interrupt context.
+ */
+extern int acm_os_recv_chars(struct acm_private *acm, u8 *cp, int n);
+
+/*
+ * acm_os_wakeup_writers - wakeup any blocked writers.  This is called
+ *                         from interrupt context, so may need to queue
+ *                         the actual wakeup in a "bottom half".
+ */
+extern void acm_os_wakeup_writers(struct acm_private *acm);
+
+/*
+ * acm_os_hangup - send a hangup to any readers or writers.  This can be
+ *                 called from interrupt context, so may need to queue
+ *                 the actual hangup in a "bottom half".
+ */
+extern void acm_os_hangup(struct acm_private *acm);
+
+/*
+ * acm_os_wakeup_opens - wakeup any blocked opens.  This is called
+ *                       from interrupt context, so may need to queue
+ *                       the actual wakeup in a "bottom half".
+ */
+extern void acm_os_wakeup_opens(struct acm_private *acm);
+
+extern void acm_os_enable(struct usbd_function_instance *function);
+extern void acm_os_disable(struct usbd_function_instance *function);
+
+#endif
diff -uNr linux/drivers/no-otg/functions/acm/acm.h linux/drivers/otg/functions/acm/acm.h
--- linux/drivers/no-otg/functions/acm/acm.h	1970-01-01 01:00:00.000000000 +0100
+++ linux/drivers/otg/functions/acm/acm.h	2006-09-01 21:41:26.000000000 +0200
@@ -0,0 +1,113 @@
+/*
+ * otg/functions/acm/acm.h
+ *
+ *      Copyright (c) 2003, 2004 Belcarra
+ *
+ * By: 
+ *      Stuart Lynne <sl@belcarra.com>, 
+ *      Tom Rushworth <tbr@belcarra.com>, 
+ *      Bruce Balden <balden@belcarra.com>
+ *
+ */
+
+/*!
+ * @defgroup ACMFunction ACM
+ * @ingroup functiongroup 
+ */
+
+/*!
+ * @file otg/functions/acm/acm.h
+ * @brief ACM Function Driver private defines
+ *
+ * This is an ACM Function Driver. The upper edge is exposed
+ * to the hosting OS as a Posix type character device. The lower
+ * edge implements the USB Device Stack API.
+ *
+ * This driver implements the CDC ACM driver model and uses the CDC ACM
+ * protocols. 
+ *
+ * Note that it appears to be impossible to determine the end of a receive
+ * bulk transfer larger than wMaxPacketsize. The host is free to send
+ * wMaxPacketsize chars in a single transfer. This means that we cannot
+ * queue receive urbs larger than wMaxPacketsize (typically 64 bytes.)
+ *
+ * This does not however prevent queuing transmit urbs with larger amounts
+ * of data. It is interpreted at the receiving (host) end as a series of
+ * wMaxPacketsize transfers but because there is no interpretation to the
+ * amounts of data sent it does affect anything if we treat the data as a
+ * single larger transfer.
+ *
+ * @ingroup ACMFunction
+ */
+
+
+// Endpoint indexes in acm_endpoint_requests[] and the endpoint map.
+#define BULK_OUT        0x00
+#define BULK_IN         0x01
+#define INT_IN          0x02
+#define ENDPOINTS       0x03
+
+#define COMM_INTF       0x00
+#define DATA_INTF       0x01
+
+
+
+// Most hosts don't care about BMAXPOWER, but the UUT tests want it to be 1
+#define BMAXPOWER   1
+#define BMATTRIBUTE 0
+
+#define STATIC
+//#define STATIC static
+//
+
+/*! @name ACM Flags
+ * @{
+ */
+#define ACM_OPENED      (1 << 0)        /*!< upper layer has opened the device port */
+#define ACM_CONFIGURED  (1 << 1)        /*!< device is configured */
+#define ACM_THROTTLED   (1 << 2)        /*!< upper layer doesn't want to receive data  */
+#define ACM_CARRIER     (1 << 3)        /*!< host has set DTR, i.e. host has opened com device */
+#define ACM_LOOPBACK    (1 << 4)        /*!< upper layer wants local loopback */
+#define ACM_LOCAL       (1 << 5)        /*!< upper layer specified LOCAL mode */
+
+/*! @} */
+
+/*! struct acm_private
+ */
+struct acm_private {
+        struct usbd_function_instance *function;
+        struct usbd_function_driver *function_driver;
+        struct acm_function_services *function_services;
+        otg_tag_t trace_tag;
+
+        u32 flags;
+
+        int recv_urbs;
+        atomic_t used;
+        atomic_t queued_bytes;
+        atomic_t queued_urbs;
+        unsigned int writesize;                 // packetsize * 4
+
+        /*TBR debug receive flow control */
+        unsigned long bytes_received;
+        unsigned long bytes_forwarded;
+        /*TBR end debug */
+
+        struct cdc_acm_line_coding line_coding; // C.f. Table 50 - [GS]ET_LINE_CODING Device Request (to/from host)
+        cdc_acm_bmUARTState bmUARTState;        // C.f. Table 51 - SET_CONTROL_LINE_STATE Device Request (from host)
+        cdc_acm_bmLineState bmLineState;        // C.f. Table 69 - SERIAL_STATE Notification (to host)
+
+
+
+        struct tq_struct recv_tqueue;
+
+        void *privdata;
+
+};
+
+
+struct acm_work {
+        struct acm_private *acm;
+        int interface;
+};
+
diff -uNr linux/drivers/no-otg/functions/acm/modem-l24-os.c linux/drivers/otg/functions/acm/modem-l24-os.c
--- linux/drivers/no-otg/functions/acm/modem-l24-os.c	1970-01-01 01:00:00.000000000 +0100
+++ linux/drivers/otg/functions/acm/modem-l24-os.c	2006-09-01 21:41:26.000000000 +0200
@@ -0,0 +1,194 @@
+/*
+ * otg/functions/acm/modem-l24-os.c
+ *
+ *      Copyright (c) 2003, 2004 Belcarra
+ *
+ * By: 
+ *      Tom Rushworth <tbr@belcarra.com>, 
+ *      Stuart Lynne <sl@belcarra.com>, 
+ *      Bruce Balden <balden@belcarra.com>
+ *
+ */
+/*!
+ * @file otg/functions/acm/modem-l24-os.c
+ * @brief ACM Function Driver private defines
+ *
+ * An ACM (Abstract Control Model) driver is composed of two pieces:
+ *    1) An OS specific piece that handles creating and operating
+ *       a serial device for the given OS.
+ *    2) A USB specific piece that interfaces either with the host
+ *       usbcore layer, or with the otgcore layer.
+ *
+ * If the USB piece interfaces with the host usbcore layer you get
+ * an ACM class driver.  If the USB piece interfaces with the otgcore
+ * layer you get an ACM function driver.
+ *
+ * This file is the OS specific piece that interfaces with Linux
+ * (kernel versions 2.4.*).  It creates two simple characters devices.
+ * One for the data transport and one for the communications interface.
+ *
+ * @ingroup ACMFunction
+ */
+
+
+//#include <otg/osversion.h>
+#include <otg/otg-compat.h>
+#include <otg/otg-module.h>
+
+MOD_AUTHOR ("tbr@belcarra.com, sl@belcarra.com");
+
+MOD_DESCRIPTION ("Belcarra CDC-ACM Function");
+EMBED_LICENSE();
+
+
+#include <linux/init.h>
+#include <asm/uaccess.h>
+#include <linux/ctype.h>
+#include <linux/timer.h>
+#include <linux/interrupt.h>
+#include <asm/atomic.h>
+#include <linux/tty.h>
+#include <linux/tty_driver.h>
+#include <linux/tty_flip.h>
+#include <linux/smp_lock.h>
+#include <linux/slab.h>
+
+#include <otg/usbp-chap9.h>
+#include <otg/usbp-func.h>
+
+#include <linux/capability.h>
+#include <otg/otg-trace.h>
+#include "acm.h"
+#include "acm-fd.h"
+#include "acm-os.h"
+
+// May need an ifdef here to pick up the acm_cl_usb_ops in the future.
+static struct acm_usb_services *usb_ops = &acm_fd_usb_ops;
+
+#define MDM mdm_fd_trace_tag
+otg_tag_t mdm_fd_trace_tag;
+
+/* Module Parameters ************************************************************************* */
+
+static u32 vendor_id;
+static u32 product_id;
+static u32 max_queued_urbs = MAX_QUEUED_URBS;
+static u32 max_queued_bytes = MAX_QUEUED_BYTES;
+
+MOD_PARM (vendor_id, "i");
+MOD_PARM (product_id, "i");
+MOD_PARM (max_queued_urbs, "i");
+MOD_PARM (max_queued_bytes, "i");
+
+MOD_PARM_DESC (vendor_id, "Device Vendor ID");
+MOD_PARM_DESC (product_id, "Device Product ID");
+MOD_PARM_DESC (max_queued_urbs, "Maximum TX Queued Urbs");
+MOD_PARM_DESC (max_queued_bytes, "Maximum TX Queued Bytes");
+
+
+/* ACM ***************************************************************************************** */
+
+#define ACM_TTY_MAJOR   166
+#define ACM_TTY_MINOR   0
+#define ACM_TTY_MINORS  1
+
+static struct acm_private acm_private;
+
+/* USB Simple Char Device ********************************************************************** */
+
+
+/* USB Comm Char Device ************************************************************************ */
+
+
+/* USB Module init/exit ************************************************************************ */
+/*
+ * acm_modinit - module init
+ *
+ */
+STATIC int acm_modinit (void)
+{
+        int i;
+        printk (KERN_INFO "Copyright (c) 2003-2004 sl@belcarra.com, tbr@belcarra.com\n");
+        MDM = otg_trace_obtain_tag();
+
+        // initialize private structure
+        //acm_private.tty_driver = &acm_tty_driver;
+        //acm_private.wqueue.routine = acm_wakeup_writers;
+        //acm_private.wqueue.data = &acm_private;
+        //acm_private.hqueue.routine = acm_hangup;
+        //acm_private.hqueue.data = &acm_private;
+        //acm_private.recv_tqueue.routine = &acm_start_recv;
+        //acm_private.recv_tqueue.data = &acm_private;
+
+        //init_waitqueue_head(&acm_private.open_wait);
+
+
+        // register as usb function driver
+
+        THROW_IF (usb_ops->initialize_usb_part(&acm_private,"acm_fd",vendor_id,product_id,
+                                               max_queued_urbs,max_queued_bytes ), error);
+
+        CATCH(error) {
+                printk(KERN_ERR"%s: ERROR\n", __FUNCTION__);
+
+                TRACE_MSG0(MDM,"--> -EINVAL");
+                return -EINVAL;
+        }
+        TRACE_MSG0(MDM,"--> 0");
+        return 0;
+}
+
+void acm_wait_task(WORK_ITEM *queue)
+{
+        TRACE_MSG1(MDM,"entered data=%p",queue->data);
+        RETURN_IF(!queue->data);
+        queue->data = NULL;
+#if defined(LINUX24)
+        while (queue->sync) {
+                TRACE_MSG1(MDM,"waiting for queue: %p",queue);
+                schedule_timeout(HZ);
+        }
+#else
+        while(PENDING_WORK_ITEM((*queue))){
+                TRACE_MSG1(MDM,"waiting for queue: %p",queue);
+                SCHEDULE_TIMEOUT(1);  // 1 second delay 
+        }
+#endif
+        TRACE_MSG0(MDM,"exited");
+}
+
+/* acm_modexit - module cleanup
+ */
+STATIC void acm_modexit (void)
+{
+        unsigned long flags;
+        struct usbd_urb *urb;
+        TRACE_MSG0(MDM,"entered");
+
+        // Wake up any pending opens after setting the exiting flag.
+        local_irq_save(flags);
+        acm_private.exiting = 1;
+        //if (acm_private.open_wait_count > 0) {
+        //        wake_up_interruptible(&acm_private.open_wait);
+        //}
+        local_irq_restore(flags);
+
+        // verify no tasks are running
+        //acm_wait_task(&acm_private.wqueue);
+        //acm_wait_task(&acm_private.hqueue);
+
+//#if defined(LINUX24)
+//        run_task_queue(&tq_timer);
+//#else 
+//        blk_run_queues();
+//#endif
+ 
+        //acm_wait_task(&acm_private.recv_tqueue);
+
+        usb_ops->terminate_usb_part(&acm_private);
+        otg_trace_invalidate_tag(MDM);
+}
+
+
+module_init (acm_modinit);
+module_exit (acm_modexit);
diff -uNr linux/drivers/no-otg/functions/acm/modem.c linux/drivers/otg/functions/acm/modem.c
--- linux/drivers/no-otg/functions/acm/modem.c	1970-01-01 01:00:00.000000000 +0100
+++ linux/drivers/otg/functions/acm/modem.c	2006-09-01 21:41:26.000000000 +0200
@@ -0,0 +1,263 @@
+/*
+ * otg/functions/acm/modem.c
+ *
+ *      Copyright (c) 2003, 2004 Belcarra
+ *
+ * By: 
+ *      Tom Rushworth <tbr@belcarra.com>, 
+ *      Stuart Lynne <sl@belcarra.com>, 
+ *      Bruce Balden <balden@belcarra.com>
+ */
+/*!
+ * @file otg/functions/acm/modem.c
+ * @brief ACM-MODEM Descriptor Set
+ *
+ * This file is the USB specific piece that interfaces with the otgcore layer.
+ * If you're looking for the USB specific piece that interfaces with the
+ * host usbcore layer, that's in otg/functions/acm/acm-cl.c
+ *
+ * Note: this function driver requires the following endpoints:
+ *
+ *      BULK-IN
+ *      BULK-OUT
+ *      INTERRUPT-IN
+ *
+ * This function driver cannot be used on devices (such as the StrongArm
+ * SA1100) that do not have an interrupt endpoint.
+ *
+ * @ingroup MODEMFunction
+ */
+
+
+#include <otg/otg-compat.h>
+#include <otg/otg-module.h>
+
+#include <linux/init.h>
+#include <asm/uaccess.h>
+#include <linux/ctype.h>
+#include <linux/timer.h>
+#include <linux/interrupt.h>
+#include <asm/atomic.h>
+#include <linux/smp_lock.h>
+#include <linux/slab.h>
+
+#include <otg/usbp-chap9.h>
+#include <otg/usbp-func.h>
+
+#include <otg/otg-trace.h>
+#include "acm.h"
+#include "acm-fd.h"
+#include "acm-os.h"
+
+
+
+/*
+ * CDC ACM Configuration
+ *
+ * Endpoint, Class, Interface, Configuration and Device descriptors/descriptions
+ */
+
+/*! BULK OUT Data Endpoint Descriptor
+ */
+static u8 mdn_alt_1[] = { 0x07, USB_DT_ENDPOINT, USB_DIR_OUT, BULK,      0, 0x00, 0x00, };
+
+/*! BULK In Data Endpoint Descriptor
+ */
+static u8 mdn_alt_2[] = { 0x07, USB_DT_ENDPOINT, USB_DIR_IN,  BULK,     0,  0x00, 0x00, };
+
+/*! Data Endpoint Descriptor List
+ */
+static struct usbd_endpoint_descriptor *mdn_alt_endpoints[] = { 
+        (struct usbd_endpoint_descriptor *) mdn_alt_1, 
+        (struct usbd_endpoint_descriptor *) mdn_alt_2, };
+
+/*! Data Endpoint Index List
+ */
+u8 mdn_alt_indexes[] = { BULK_OUT, BULK_IN, };
+
+/*! INTERRUPT In Comm Endpoint Descriptor
+ */
+static u8 mdn_comm_1[] = { 0x07, USB_DT_ENDPOINT, USB_DIR_IN,  INTERRUPT, 0, 0x00, 0x0a, };
+
+/*! Comm Endpoint Descriptor List
+ */
+static struct usbd_endpoint_descriptor *mdn_comm_endpoints[] = { (struct usbd_endpoint_descriptor *) mdn_comm_1 };
+
+
+/*! Comm Endpoint Index List
+ */
+u8 mdn_comm_indexes[] = { INT_IN, };
+
+/*! @{
+ * Class Descriptors
+ */
+static u8 cdc_class_1[] = { 0x05, CS_INTERFACE, USB_ST_HEADER, 0x01, 0x01, /* CLASS_BDC_VERSION, CLASS_BDC_VERSION */ };
+static u8 cdc_class_2[] = { 0x05, CS_INTERFACE, USB_ST_CMF, 0x03, 0x01, /* bMasterInterface: 0, bSlaveInterface: 1 */ };
+static u8 cdc_class_3[] = { 0x05, CS_INTERFACE, USB_ST_UF, 0x00, 0x01, /* bMasterInterface: 0, bSlaveInterface: 1 */ };
+/*! @} */
+
+/*! ACMF Class Descriptor
+ * ACMF - c.f. Table 28
+ * currenty set to 0x2 - Support Set_Line_Coding etc, 
+ *
+ * XXX Should we also set 0x4 - Supports Network_Notification?
+ */
+static u8 cdc_class_4[] = { 0x04, CS_INTERFACE, USB_ST_ACMF, 0x02, };
+
+static struct usbd_generic_class_descriptor *cdc_comm_class_descriptors[] = 
+        { (struct usbd_generic_class_descriptor *) cdc_class_1, 
+        (struct usbd_generic_class_descriptor *) cdc_class_2, 
+        (struct usbd_generic_class_descriptor *) cdc_class_3, 
+        (struct usbd_generic_class_descriptor *) cdc_class_4, };
+
+
+
+/* Alternate Descriptors */
+// First two bytes are identical in all: bLength, bDescriptorType (0x09 0x04)
+
+/*! Comm Alternate Descriptor
+ */
+static u8 cdc_comm_alternate_descriptor[sizeof(struct usbd_interface_descriptor)] = {
+        0x09, USB_DT_INTERFACE, COMM_INTF, 0x00, 0x00, // bInterfaceNumber, bAlternateSetting, bNumEndpoints
+        COMMUNICATIONS_INTERFACE_CLASS, COMMUNICATIONS_ACM_SUBCLASS, 0x01, 0x00, };
+
+/*! Data Alternate Descriptor
+ */
+static u8 cdc_data_alternate_descriptor[sizeof(struct usbd_interface_descriptor)] = {
+        0x09, USB_DT_INTERFACE, DATA_INTF, 0x00, 0x00, // bInterfaceNumber, bAlternateSetting, bNumEndpoints
+        DATA_INTERFACE_CLASS, COMMUNICATIONS_NO_SUBCLASS, COMMUNICATIONS_NO_PROTOCOL, 0x00, };
+
+
+
+/*! Comm alternate descriptions 
+ */
+static struct usbd_alternate_description cdc_comm_alternate_descriptions[] = {
+      { iInterface: CONFIG_OTG_ACM_COMM_INTF,
+              interface_descriptor: (struct usbd_interface_descriptor *)&cdc_comm_alternate_descriptor,
+              classes:sizeof (cdc_comm_class_descriptors) / sizeof (struct usbd_generic_class_descriptor *),
+              class_list: cdc_comm_class_descriptors,
+              endpoint_list: mdn_comm_endpoints,
+              endpoints:sizeof (mdn_comm_endpoints) / sizeof(struct usbd_endpoint_descriptor *),
+              endpoint_indexes: mdn_comm_indexes,
+      }, };
+
+/*! Data alternate descriptions 
+ */
+static struct usbd_alternate_description cdc_data_alternate_descriptions[] = {
+      { iInterface: CONFIG_OTG_ACM_DATA_INTF,
+              interface_descriptor: (struct usbd_interface_descriptor *)&cdc_data_alternate_descriptor,
+              endpoint_list: mdn_alt_endpoints,
+              endpoints:sizeof (mdn_alt_endpoints) / sizeof(struct usbd_endpoint_descriptor *),
+              endpoint_indexes: mdn_alt_indexes,
+              }, };
+
+
+/*! Interface Descriptions List */
+static struct usbd_interface_description cdc_interfaces[] = {
+      { 
+                alternates:sizeof (cdc_comm_alternate_descriptions) / sizeof (struct usbd_alternate_description),
+                alternate_list:cdc_comm_alternate_descriptions,
+      },
+
+      { 
+                alternates:sizeof (cdc_data_alternate_descriptions) / sizeof (struct usbd_alternate_description),
+                alternate_list:cdc_data_alternate_descriptions,
+      },
+};
+
+
+/*! Configuration Descriptor 
+ */
+static u8 cdc_configuration_descriptor[sizeof(struct usbd_configuration_descriptor)] = {
+        0x09, USB_DT_CONFIGURATION, 0x00, 0x00, sizeof (cdc_interfaces) / sizeof (struct usbd_interface_description),
+        0x01, 0x00, BMATTRIBUTE, BMAXPOWER, };
+
+/*! Configuration Description 
+ */
+struct usbd_configuration_description mdn_description[] = {
+      { iConfiguration: CONFIG_OTG_ACM_DESC,
+              configuration_descriptor: (struct usbd_configuration_descriptor *)cdc_configuration_descriptor,
+      }, };
+
+/*! Device Descriptor 
+ */
+static struct usbd_device_descriptor mdn_device_descriptor = {
+        bLength: sizeof(struct usbd_device_descriptor),
+        bDescriptorType: USB_DT_DEVICE,
+        bcdUSB: __constant_cpu_to_le16(USB_BCD_VERSION),
+        bDeviceClass: COMMUNICATIONS_DEVICE_CLASS,
+        bDeviceSubClass: 0x02,
+        bDeviceProtocol: 0x00,
+        bMaxPacketSize0: 0x00,
+        idVendor: __constant_cpu_to_le16(CONFIG_OTG_ACM_VENDORID),
+        idProduct: __constant_cpu_to_le16(CONFIG_OTG_ACM_PRODUCTID),
+        bcdDevice: __constant_cpu_to_le16(CONFIG_OTG_ACM_BCDDEVICE),
+};
+
+#ifdef CONFIG_OTG_HIGH_SPEED
+/*! High Speed Device Qualifier Descriptor 
+ */
+static struct usbd_device_qualifier_descriptor mdn_device_qualifier_descriptor = {
+        bLength: sizeof(struct usbd_device_qualifier_descriptor),
+        bDescriptorType: USB_DT_DEVICE_QUALIFIER,
+        bcdUSB: __constant_cpu_to_le16(USB_BCD_VERSION),
+        bDeviceClass: COMMUNICATIONS_DEVICE_CLASS,
+        bDeviceSubClass: 0x02,
+        bDeviceProtocol: 0x00,
+        bMaxPacketSize0: 0x00,
+};
+#endif /* CONFIG_OTG_HIGH_SPEED */
+
+
+/*! Endpoint Request List
+ */
+static struct usbd_endpoint_request mdn_endpoint_requests[ENDPOINTS+1] = {
+        { 1, 1, 0, USB_DIR_OUT | USB_ENDPOINT_BULK, 64, 512, },
+        { 1, 1, 0, USB_DIR_IN | USB_ENDPOINT_BULK, 64 /* * 4 */, 512, },
+        { 1, 0, 0, USB_DIR_IN | USB_ENDPOINT_INTERRUPT, 16, 64, },
+        { 0, },
+};
+
+/*! OTG Descriptor
+ */
+static struct usbd_otg_descriptor mdn_otg_descriptor = {
+        bLength : sizeof(struct usbd_otg_descriptor),
+        bDescriptorType: USB_DT_OTG,
+        bmAttributes: 0,
+};
+
+/*! Device Description
+ */
+struct usbd_device_description mdn_device_description = {
+        device_descriptor: &mdn_device_descriptor,
+#ifdef CONFIG_OTG_HIGH_SPEED
+        device_qualifier_descriptor: &mdn_device_qualifier_descriptor,
+#endif /* CONFIG_OTG_HIGH_SPEED */
+        otg_descriptor: &mdn_otg_descriptor,
+        iManufacturer: CONFIG_OTG_ACM_MANUFACTURER,
+        iProduct: CONFIG_OTG_ACM_PRODUCT_NAME,
+#if !defined(CONFIG_OTG_NO_SERIAL_NUMBER) && defined(CONFIG_OTG_SERIAL_NUMBER_STR)
+        iSerialNumber: CONFIG_OTG_SERIAL_NUMBER_STR, 
+#endif
+};
+
+
+
+
+/*! function_driver 
+ */
+struct usbd_function_driver modem_function_driver = {
+        name: "ACM-MDM",
+        fops:&function_ops,
+        device_description:&mdn_device_description,
+        bNumConfigurations:sizeof (mdn_description) / sizeof (struct usbd_configuration_description),
+        configuration_description:mdn_description,
+        idVendor: __constant_cpu_to_le16(CONFIG_OTG_ACM_VENDORID),
+        idProduct: __constant_cpu_to_le16(CONFIG_OTG_ACM_PRODUCTID),
+        bcdDevice: __constant_cpu_to_le16(CONFIG_OTG_ACM_BCDDEVICE),
+        bNumInterfaces:sizeof (cdc_interfaces) / sizeof (struct usbd_interface_description),
+        interface_list:cdc_interfaces,
+        endpointsRequested: ENDPOINTS,
+        requestedEndpoints: mdn_endpoint_requests,
+};
+
diff -uNr linux/drivers/no-otg/functions/acm/modem.h linux/drivers/otg/functions/acm/modem.h
--- linux/drivers/no-otg/functions/acm/modem.h	1970-01-01 01:00:00.000000000 +0100
+++ linux/drivers/otg/functions/acm/modem.h	2006-09-01 21:41:26.000000000 +0200
@@ -0,0 +1,118 @@
+/*
+ * otg/functions/acm/modem.h
+ *
+ *      Copyright (c) 2003, 2004 Belcarra
+ *
+ * By: 
+ *      Stuart Lynne <sl@belcarra.com>, 
+ *      Tom Rushworth <tbr@belcarra.com>, 
+ *      Bruce Balden <balden@belcarra.com>
+ *
+ */
+/*!
+ * @defgroup MODEMFunction ACM-Modem 
+ * @ingroup functiongroup
+ */
+/*!
+ * @file otg/functions/acm/modem.h
+ * @brief ACM Function Driver private defines
+ *
+ *
+ * This is an ACM Function Driver. The upper edge is exposed
+ * to the hosting OS as a Posix type character device. The lower
+ * edge implements the USB Device Stack API.
+ *
+ * These are emulated and set by the modem driver as appropriate
+ * to model a virutal serial port. The
+ *
+ *      TIOCM_RNG       RNG (Ring)                              not used
+ *      TIOCM_LE        DSR (Data Set Ready / Line Enable)      
+ *      TIOCM_DSR       DSR (Data Set Ready)
+ *      TIOCM_CAR       DCD (Data Carrier Detect)
+ *      TIOCM_CTS       CTS (Clear to Send)
+ *      TIOCM_CD        TIOCM_CAR
+ *      TIOCM_RI        TIOCM_RNG
+ *
+ * These are set by the application:
+ *
+ *      TIOCM_DTR       DTR (Data Terminal Ready)
+ *      TIOCM_RTS       RTS (Request to Send)
+ *
+ *      TIOCM_LOOP      Set into loopback mode
+ *      TIOCM_OUT1      Not used.
+ *      TIOCM_OUT2      Not used.
+ *
+ *
+ * The following File and  termio c_cflags are used:
+ *
+ *      O_NONBLOCK      don't block in modem_open()
+ *      O_EXCL          don't allow more than one open
+ *
+ *      CLOCAL          ignore DTR status
+ *      CBAUD           send break if set to B0
+ *
+ * Virtual NULL Modem
+ *
+ * Peripheral to Host via Serial State Notification (write ioctls)
+ *
+ * Application             TIOCM           Null Modem      ACM (DCE)       Notificaiton    Host (DTE)
+ * -------------------------------------------------------------------------------------------------------------
+ * Request to send         TIOCM_RTS       RTS ->  CTS     CTS             Not Available   Clear to Send (N/A)
+ * Data Terminal Ready     TIOCM_DTR       DTR ->  DSR     DSR             bTxCarrier      Data Set Ready
+ *                                         DTR ->  DCD     DCD             bRXCarrier      Carrier Detect
+ * Ring Indicator          TIOCM_OUT1      OUT1 -> RI      RI              bRingSignal     Ring Indicator
+ * Overrun                 TIOCM_OUT2      OUT2 -> Overrun Overrun         bOverrun        Overrun
+ * Send Break              B0              B0   -> Break   Break           bBreak          Break Received
+ * -------------------------------------------------------------------------------------------------------------
+ *
+ *
+ * Host to Peripheral via Set Control Line State
+ *
+ * Host (DTE)              Line State      ACM (DCE)       Null Modem      TIOCM           Peripheral (DTE)
+ * -------------------------------------------------------------------------------------------------------------
+ * Data Terminal Ready     D0              DTR             DTR ->  DSR     TIOCM_DSR       Data Set Ready
+ *                                                         DTR ->  DCD     TIOCM_CAR       Carrier Detect
+ * Request To Send         D1              RTS             RTS ->  CTS     TIOCM_CTS       Clear to Send
+ * -------------------------------------------------------------------------------------------------------------
+ *
+ *      
+ * @ingroup MODEMFunction
+ */
+
+
+extern struct usbd_function_driver modem_function_driver;
+
+//#define MODEM_OPENED      (1 << 0)                /*!< OPENED flag */
+#define MODEMFD_CLOCAL      (1 << 1)                /*!< CLOCAL flag */
+//#define MODEMFD_LOOPBACK    (1 << 2)                /*!< LOOPBACK flag */
+#define MODEMFD_EXCLUSIVE   (1 << 3)                /*!< EXCLUSIVE flag */
+#define MODEMFD_THROTTLED   (1 << 4)                /*!< THROTTLED flag */
+
+/*! struct modem_private
+ */
+struct modem_private {
+
+        struct modem_driver *modem_driver;          /*!< modem structure */
+        int modem_driver_registered;              /*!< non-zero if modem_driver registered */
+        int usb_driver_registered;              /*!< non-zero if usb function registered */
+
+        struct modem_struct *modem;                 /*!< non-null if modem open */
+        struct tq_struct wqueue;                /*!< task queue for writer wakeup  */
+        struct tq_struct hqueue;                /*!< task queue for hangup */
+
+        u32 flags;                              /*!< flags */
+
+        u32 tiocm;                              /*!< tiocm settings */
+
+        struct serial_struct serial_struct;     /*!< serial structure used for TIOCSSERIAL and TIOCGSERIAL */
+
+        wait_queue_head_t tiocm_wait;
+        u32 tiocgicount;
+
+        u32 c_cflag;
+
+        wait_queue_head_t open_wait;            // wait queue for blocking open
+        int exiting;                            // True if module exiting
+};
+
+
diff -uNr linux/drivers/no-otg/functions/acm/obex-l24-os.c linux/drivers/otg/functions/acm/obex-l24-os.c
--- linux/drivers/no-otg/functions/acm/obex-l24-os.c	1970-01-01 01:00:00.000000000 +0100
+++ linux/drivers/otg/functions/acm/obex-l24-os.c	2006-09-01 21:41:26.000000000 +0200
@@ -0,0 +1,194 @@
+/*
+ * otg/functions/acm/obex-l24-os.c
+ *
+ *      Copyright (c) 2003, 2004 Belcarra
+ *
+ * By: 
+ *      Tom Rushworth <tbr@belcarra.com>, 
+ *      Stuart Lynne <sl@belcarra.com>, 
+ *      Bruce Balden <balden@belcarra.com>
+ *
+ */
+/*!
+ * @file otg/functions/acm/obex-l24-os.c
+ * @brief ACM Function Driver private defines
+ *
+ * An ACM (Abstract Control Model) driver is composed of two pieces:
+ *    1) An OS specific piece that handles creating and operating
+ *       a serial device for the given OS.
+ *    2) A USB specific piece that interfaces either with the host
+ *       usbcore layer, or with the otgcore layer.
+ *
+ * If the USB piece interfaces with the host usbcore layer you get
+ * an ACM class driver.  If the USB piece interfaces with the otgcore
+ * layer you get an ACM function driver.
+ *
+ * This file is the OS specific piece that interfaces with Linux
+ * (kernel versions 2.4.*).  It creates two simple characters devices.
+ * One for the data transport and one for the communications interface.
+ *
+ * @ingroup ACMFunction
+ */
+
+
+//#include <otg/osversion.h>
+#include <otg/otg-compat.h>
+#include <otg/otg-module.h>
+
+MOD_AUTHOR ("tbr@belcarra.com, sl@belcarra.com");
+
+MOD_DESCRIPTION ("Belcarra CDC-ACM Function");
+EMBED_LICENSE();
+
+
+#include <linux/init.h>
+#include <asm/uaccess.h>
+#include <linux/ctype.h>
+#include <linux/timer.h>
+#include <linux/interrupt.h>
+#include <asm/atomic.h>
+#include <linux/tty.h>
+#include <linux/tty_driver.h>
+#include <linux/tty_flip.h>
+#include <linux/smp_lock.h>
+#include <linux/slab.h>
+
+#include <otg/usbp-chap9.h>
+#include <otg/usbp-func.h>
+
+#include <linux/capability.h>
+#include <otg/otg-trace.h>
+#include "acm.h"
+#include "acm-fd.h"
+#include "acm-os.h"
+
+// May need an ifdef here to pick up the acm_cl_usb_ops in the future.
+static struct acm_usb_services *usb_ops = &acm_fd_usb_ops;
+
+#define OBX obx_fd_trace_tag
+otg_tag_t obx_fd_trace_tag;
+
+/* Module Parameters ************************************************************************* */
+
+static u32 vendor_id;
+static u32 product_id;
+static u32 max_queued_urbs = MAX_QUEUED_URBS;
+static u32 max_queued_bytes = MAX_QUEUED_BYTES;
+
+MOD_PARM (vendor_id, "i");
+MOD_PARM (product_id, "i");
+MOD_PARM (max_queued_urbs, "i");
+MOD_PARM (max_queued_bytes, "i");
+
+MOD_PARM_DESC (vendor_id, "Device Vendor ID");
+MOD_PARM_DESC (product_id, "Device Product ID");
+MOD_PARM_DESC (max_queued_urbs, "Maximum TX Queued Urbs");
+MOD_PARM_DESC (max_queued_bytes, "Maximum TX Queued Bytes");
+
+
+/* ACM ***************************************************************************************** */
+
+#define ACM_TTY_MAJOR   166
+#define ACM_TTY_MINOR   0
+#define ACM_TTY_MINORS  1
+
+static struct acm_private acm_private;
+
+/* USB Simple Char Device ********************************************************************** */
+
+
+/* USB Comm Char Device ************************************************************************ */
+
+
+/* USB Module init/exit ************************************************************************ */
+/*
+ * acm_modinit - module init
+ *
+ */
+STATIC int acm_modinit (void)
+{
+        int i;
+        printk (KERN_INFO "Copyright (c) 2003-2004 sl@belcarra.com, tbr@belcarra.com\n");
+        OBX = otg_trace_obtain_tag();
+
+        // initialize private structure
+        //acm_private.tty_driver = &acm_tty_driver;
+        //acm_private.wqueue.routine = acm_wakeup_writers;
+        //acm_private.wqueue.data = &acm_private;
+        //acm_private.hqueue.routine = acm_hangup;
+        //acm_private.hqueue.data = &acm_private;
+        //acm_private.recv_tqueue.routine = &acm_start_recv;
+        //acm_private.recv_tqueue.data = &acm_private;
+
+        //init_waitqueue_head(&acm_private.open_wait);
+
+
+        // register as usb function driver
+
+        THROW_IF (usb_ops->initialize_usb_part(&acm_private,"acm_fd",vendor_id,product_id,
+                                               max_queued_urbs,max_queued_bytes ), error);
+
+        CATCH(error) {
+                printk(KERN_ERR"%s: ERROR\n", __FUNCTION__);
+
+                TRACE_MSG0(OBX,"--> -EINVAL");
+                return -EINVAL;
+        }
+        TRACE_MSG0(OBX,"--> 0");
+        return 0;
+}
+
+void acm_wait_task(WORK_ITEM *queue)
+{
+        TRACE_MSG1(OBX,"entered data=%p",queue->data);
+        RETURN_IF(!queue->data);
+        queue->data = NULL;
+#if defined(LINUX24)
+        while (queue->sync) {
+                TRACE_MSG1(OBX,"waiting for queue: %p",queue);
+                schedule_timeout(HZ);
+        }
+#else
+        while(PENDING_WORK_ITEM((*queue))){
+                TRACE_MSG1(OBX,"waiting for queue: %p",queue);
+                SCHEDULE_TIMEOUT(1);  // 1 second delay 
+        }
+#endif
+        TRACE_MSG0(OBX,"exited");
+}
+
+/* acm_modexit - module cleanup
+ */
+STATIC void acm_modexit (void)
+{
+        unsigned long flags;
+        struct usbd_urb *urb;
+        TRACE_MSG0(OBX,"entered");
+
+        // Wake up any pending opens after setting the exiting flag.
+        local_irq_save(flags);
+        acm_private.exiting = 1;
+        //if (acm_private.open_wait_count > 0) {
+        //        wake_up_interruptible(&acm_private.open_wait);
+        //}
+        local_irq_restore(flags);
+
+        // verify no tasks are running
+        //acm_wait_task(&acm_private.wqueue);
+        //acm_wait_task(&acm_private.hqueue);
+
+//#if defined(LINUX24)
+//        run_task_queue(&tq_timer);
+//#else 
+//        blk_run_queues();
+//#endif
+ 
+        //acm_wait_task(&acm_private.recv_tqueue);
+
+        usb_ops->terminate_usb_part(&acm_private);
+        otg_trace_invalidate_tag(OBX);
+}
+
+
+module_init (acm_modinit);
+module_exit (acm_modexit);
diff -uNr linux/drivers/no-otg/functions/acm/obex.c linux/drivers/otg/functions/acm/obex.c
--- linux/drivers/no-otg/functions/acm/obex.c	1970-01-01 01:00:00.000000000 +0100
+++ linux/drivers/otg/functions/acm/obex.c	2006-09-01 21:41:26.000000000 +0200
@@ -0,0 +1,263 @@
+/*
+ * otg/functions/acm/obex.c
+ *
+ *      Copyright (c) 2003, 2004 Belcarra
+ *
+ * By: 
+ *      Tom Rushworth <tbr@belcarra.com>, 
+ *      Stuart Lynne <sl@belcarra.com>, 
+ *      Bruce Balden <balden@belcarra.com>
+ */
+/*!
+ * @file otg/functions/acm/obex.c
+ * @brief ACM-OBEX Descriptor Set
+ *
+ * This file is the USB specific piece that interfaces with the otgcore layer.
+ * If you're looking for the USB specific piece that interfaces with the
+ * host usbcore layer, that's in otg/functions/acm/acm-cl.c
+ *
+ * Note: this function driver requires the following endpoints:
+ *
+ *      BULK-IN
+ *      BULK-OUT
+ *      INTERRUPT-IN
+ *
+ * This function driver cannot be used on devices (such as the StrongArm
+ * SA1100) that do not have an interrupt endpoint.
+ *
+ * @ingroup OBEXFunction
+ */
+
+
+#include <otg/otg-compat.h>
+#include <otg/otg-module.h>
+
+#include <linux/init.h>
+#include <asm/uaccess.h>
+#include <linux/ctype.h>
+#include <linux/timer.h>
+#include <linux/interrupt.h>
+#include <asm/atomic.h>
+#include <linux/smp_lock.h>
+#include <linux/slab.h>
+
+#include <otg/usbp-chap9.h>
+#include <otg/usbp-func.h>
+
+#include <otg/otg-trace.h>
+#include "acm.h"
+#include "acm-fd.h"
+#include "acm-os.h"
+
+
+
+/*
+ * CDC ACM Configuration
+ *
+ * Endpoint, Class, Interface, Configuration and Device descriptors/descriptions
+ */
+
+/*! BULK OUT Data Endpoint Descriptor
+ */
+static u8 obx_alt_1[] = { 0x07, USB_DT_ENDPOINT, USB_DIR_OUT, BULK,      0, 0x00, 0x00, };
+
+/*! BULK In Data Endpoint Descriptor
+ */
+static u8 obx_alt_2[] = { 0x07, USB_DT_ENDPOINT, USB_DIR_IN,  BULK,     0,  0x00, 0x00, };
+
+/*! Data Endpoint Descriptor List
+ */
+static struct usbd_endpoint_descriptor *obx_alt_endpoints[] = { 
+        (struct usbd_endpoint_descriptor *) obx_alt_1, 
+        (struct usbd_endpoint_descriptor *) obx_alt_2, };
+
+/*! Data Endpoint Index List
+ */
+u8 obx_alt_indexes[] = { BULK_OUT, BULK_IN, };
+
+/*! INTERRUPT In Comm Endpoint Descriptor
+ */
+static u8 obx_comm_1[] = { 0x07, USB_DT_ENDPOINT, USB_DIR_IN,  INTERRUPT, 0, 0x00, 0x0a, };
+
+/*! Comm Endpoint Descriptor List
+ */
+static struct usbd_endpoint_descriptor *obx_comm_endpoints[] = { (struct usbd_endpoint_descriptor *) obx_comm_1 };
+
+
+/*! Comm Endpoint Index List
+ */
+u8 obx_comm_indexes[] = { INT_IN, };
+
+/*! @{
+ * Class Descriptors
+ */
+static u8 cdc_class_1[] = { 0x05, CS_INTERFACE, USB_ST_HEADER, 0x01, 0x01, /* CLASS_BDC_VERSION, CLASS_BDC_VERSION */ };
+static u8 cdc_class_2[] = { 0x05, CS_INTERFACE, USB_ST_CMF, 0x03, 0x01, /* bMasterInterface: 0, bSlaveInterface: 1 */ };
+static u8 cdc_class_3[] = { 0x05, CS_INTERFACE, USB_ST_UF, 0x00, 0x01, /* bMasterInterface: 0, bSlaveInterface: 1 */ };
+/*! @} */
+
+/*! ACMF Class Descriptor
+ * ACMF - c.f. Table 28
+ * currenty set to 0x2 - Support Set_Line_Coding etc, 
+ *
+ * XXX Should we also set 0x4 - Supports Network_Notification?
+ */
+static u8 cdc_class_4[] = { 0x04, CS_INTERFACE, USB_ST_ACMF, 0x02, };
+
+static struct usbd_generic_class_descriptor *cdc_comm_class_descriptors[] = 
+        { (struct usbd_generic_class_descriptor *) cdc_class_1, 
+        (struct usbd_generic_class_descriptor *) cdc_class_2, 
+        (struct usbd_generic_class_descriptor *) cdc_class_3, 
+        (struct usbd_generic_class_descriptor *) cdc_class_4, };
+
+
+
+/* Alternate Descriptors */
+// First two bytes are identical in all: bLength, bDescriptorType (0x09 0x04)
+
+/*! Comm Alternate Descriptor
+ */
+static u8 cdc_comm_alternate_descriptor[sizeof(struct usbd_interface_descriptor)] = {
+        0x09, USB_DT_INTERFACE, COMM_INTF, 0x00, 0x00, // bInterfaceNumber, bAlternateSetting, bNumEndpoints
+        COMMUNICATIONS_INTERFACE_CLASS, COMMUNICATIONS_ACM_SUBCLASS, 0x01, 0x00, };
+
+/*! Data Alternate Descriptor
+ */
+static u8 cdc_data_alternate_descriptor[sizeof(struct usbd_interface_descriptor)] = {
+        0x09, USB_DT_INTERFACE, DATA_INTF, 0x00, 0x00, // bInterfaceNumber, bAlternateSetting, bNumEndpoints
+        DATA_INTERFACE_CLASS, COMMUNICATIONS_NO_SUBCLASS, COMMUNICATIONS_NO_PROTOCOL, 0x00, };
+
+
+
+/*! Comm alternate descriptions 
+ */
+static struct usbd_alternate_description cdc_comm_alternate_descriptions[] = {
+      { iInterface: CONFIG_OTG_ACM_COMM_INTF,
+              interface_descriptor: (struct usbd_interface_descriptor *)&cdc_comm_alternate_descriptor,
+              classes:sizeof (cdc_comm_class_descriptors) / sizeof (struct usbd_generic_class_descriptor *),
+              class_list: cdc_comm_class_descriptors,
+              endpoint_list: obx_comm_endpoints,
+              endpoints:sizeof (obx_comm_endpoints) / sizeof(struct usbd_endpoint_descriptor *),
+              endpoint_indexes: obx_comm_indexes,
+      }, };
+
+/*! Data alternate descriptions 
+ */
+static struct usbd_alternate_description cdc_data_alternate_descriptions[] = {
+      { iInterface: CONFIG_OTG_ACM_DATA_INTF,
+              interface_descriptor: (struct usbd_interface_descriptor *)&cdc_data_alternate_descriptor,
+              endpoint_list: obx_alt_endpoints,
+              endpoints:sizeof (obx_alt_endpoints) / sizeof(struct usbd_endpoint_descriptor *),
+              endpoint_indexes: obx_alt_indexes,
+              }, };
+
+
+/*! Interface Descriptions List */
+static struct usbd_interface_description cdc_interfaces[] = {
+      { 
+                alternates:sizeof (cdc_comm_alternate_descriptions) / sizeof (struct usbd_alternate_description),
+                alternate_list:cdc_comm_alternate_descriptions,
+      },
+
+      { 
+                alternates:sizeof (cdc_data_alternate_descriptions) / sizeof (struct usbd_alternate_description),
+                alternate_list:cdc_data_alternate_descriptions,
+      },
+
+};
+
+
+/*! Configuration Descriptor 
+ */
+static u8 cdc_configuration_descriptor[sizeof(struct usbd_configuration_descriptor)] = {
+        0x09, USB_DT_CONFIGURATION, 0x00, 0x00, sizeof (cdc_interfaces) / sizeof (struct usbd_interface_description),
+        0x01, 0x00, BMATTRIBUTE, BMAXPOWER, };
+
+/*! Configuration Description 
+ */
+struct usbd_configuration_description obx_description[] = {
+      { iConfiguration: CONFIG_OTG_ACM_DESC,
+              configuration_descriptor: (struct usbd_configuration_descriptor *)cdc_configuration_descriptor,
+      }, };
+
+/*! Device Descriptor 
+ */
+static struct usbd_device_descriptor obx_device_descriptor = {
+        bLength: sizeof(struct usbd_device_descriptor),
+        bDescriptorType: USB_DT_DEVICE,
+        bcdUSB: __constant_cpu_to_le16(USB_BCD_VERSION),
+        bDeviceClass: COMMUNICATIONS_DEVICE_CLASS,
+        bDeviceSubClass: 0x02,
+        bDeviceProtocol: 0x00,
+        bMaxPacketSize0: 0x00,
+        idVendor: __constant_cpu_to_le16(CONFIG_OTG_ACM_VENDORID),
+        idProduct: __constant_cpu_to_le16(CONFIG_OTG_ACM_PRODUCTID),
+        bcdDevice: __constant_cpu_to_le16(CONFIG_OTG_ACM_BCDDEVICE),
+};
+
+#ifdef CONFIG_OTG_HIGH_SPEED
+/*! High Speed Device Qualifier Descriptor 
+ */
+static struct usbd_device_qualifier_descriptor obx_device_qualifier_descriptor = {
+        bLength: sizeof(struct usbd_device_qualifier_descriptor),
+        bDescriptorType: USB_DT_DEVICE_QUALIFIER,
+        bcdUSB: __constant_cpu_to_le16(USB_BCD_VERSION),
+        bDeviceClass: COMMUNICATIONS_DEVICE_CLASS,
+        bDeviceSubClass: 0x02,
+        bDeviceProtocol: 0x00,
+        bMaxPacketSize0: 0x00,
+};
+#endif /* CONFIG_OTG_HIGH_SPEED */
+
+
+/*! Endpoint Request List
+ */
+static struct usbd_endpoint_request obx_endpoint_requests[ENDPOINTS+1] = {
+        { 1, 1, 0, USB_DIR_OUT | USB_ENDPOINT_BULK, 64, 512, },
+        { 1, 1, 0, USB_DIR_IN | USB_ENDPOINT_BULK, 64 /* * 4 */, 512, },
+        { 1, 0, 0, USB_DIR_IN | USB_ENDPOINT_INTERRUPT, 16, 64, },
+        { 0, },
+};
+
+/*! OTG Descriptor
+ */
+static struct usbd_otg_descriptor obx_otg_descriptor = {
+        bLength : sizeof(struct usbd_otg_descriptor),
+        bDescriptorType: USB_DT_OTG,
+        bmAttributes: 0,
+};
+
+/*! Device Description
+ */
+struct usbd_device_description obx_device_description = {
+        device_descriptor: &obx_device_descriptor,
+#ifdef CONFIG_OTG_HIGH_SPEED
+        device_qualifier_descriptor: &obx_device_qualifier_descriptor,
+#endif /* CONFIG_OTG_HIGH_SPEED */
+        otg_descriptor: &obx_otg_descriptor,
+        iManufacturer: CONFIG_OTG_ACM_MANUFACTURER,
+        iProduct: CONFIG_OTG_ACM_PRODUCT_NAME,
+#if !defined(CONFIG_OTG_NO_SERIAL_NUMBER) && defined(CONFIG_OTG_SERIAL_NUMBER_STR)
+        iSerialNumber: CONFIG_OTG_SERIAL_NUMBER_STR, 
+#endif
+};
+
+
+
+/*! function_driver 
+ */
+struct usbd_function_driver obex_function_driver = {
+        name: "ACM-OBX",
+        fops:&function_ops,
+        device_description:&obx_device_description,
+        bNumConfigurations:sizeof (obx_description) / sizeof (struct usbd_configuration_description),
+        configuration_description:obx_description,
+        idVendor: __constant_cpu_to_le16(CONFIG_OTG_ACM_VENDORID),
+        idProduct: __constant_cpu_to_le16(CONFIG_OTG_ACM_PRODUCTID),
+        bcdDevice: __constant_cpu_to_le16(CONFIG_OTG_ACM_BCDDEVICE),
+        bNumInterfaces:sizeof (cdc_interfaces) / sizeof (struct usbd_interface_description),
+        interface_list:cdc_interfaces,
+        endpointsRequested: ENDPOINTS,
+        requestedEndpoints: obx_endpoint_requests,
+};
+
diff -uNr linux/drivers/no-otg/functions/acm/obex.h linux/drivers/otg/functions/acm/obex.h
--- linux/drivers/no-otg/functions/acm/obex.h	1970-01-01 01:00:00.000000000 +0100
+++ linux/drivers/otg/functions/acm/obex.h	2006-09-01 21:41:26.000000000 +0200
@@ -0,0 +1,66 @@
+/*
+ * otg/functions/acm/obex.h
+ *
+ *      Copyright (c) 2003, 2004 Belcarra
+ *
+ * By: 
+ *      Stuart Lynne <sl@belcarra.com>, 
+ *      Tom Rushworth <tbr@belcarra.com>, 
+ *      Bruce Balden <balden@belcarra.com>
+ *
+ */
+/*!
+ * @defgroup OBEXFunction ACM-OBEX 
+ * @ingroup functiongroup 
+ */
+/*!
+ * @file otg/functions/acm/obex.h
+ * @brief ACM Function Driver private defines
+ *
+ *
+ * This is an ACM Function Driver. The upper edge is exposed
+ * to the hosting OS as a Posix type character device. The lower
+ * edge implements the USB Device Stack API.
+ *
+ *
+ *      
+ * @ingroup OBEXFunction
+ */
+
+
+extern struct usbd_function_driver tty_function_driver;
+
+//#define TTY_OPENED      (1 << 0)                /*!< OPENED flag */
+#define TTYFD_CLOCAL      (1 << 1)                /*!< CLOCAL flag */
+//#define TTYFD_LOOPBACK    (1 << 2)                /*!< LOOPBACK flag */
+#define TTYFD_EXCLUSIVE   (1 << 3)                /*!< EXCLUSIVE flag */
+#define TTYFD_THROTTLED   (1 << 4)                /*!< THROTTLED flag */
+
+/*! struct obex_private
+ */
+struct obex_private {
+
+        struct tty_driver *tty_driver;          /*!< tty structure */
+        int tty_driver_registered;              /*!< non-zero if tty_driver registered */
+        int usb_driver_registered;              /*!< non-zero if usb function registered */
+
+        struct tty_struct *tty;                 /*!< non-null if tty open */
+        struct tq_struct wqueue;                /*!< task queue for writer wakeup  */
+        struct tq_struct hqueue;                /*!< task queue for hangup */
+
+        u32 flags;                              /*!< flags */
+
+        u32 tiocm;                              /*!< tiocm settings */
+
+        struct serial_struct serial_struct;     /*!< serial structure used for TIOCSSERIAL and TIOCGSERIAL */
+
+        wait_queue_head_t tiocm_wait;
+        u32 tiocgicount;
+
+        u32 c_cflag;
+
+        wait_queue_head_t open_wait;            // wait queue for blocking open
+        int exiting;                            // True if module exiting
+};
+
+
diff -uNr linux/drivers/no-otg/functions/acm/tty-fd.c linux/drivers/otg/functions/acm/tty-fd.c
--- linux/drivers/no-otg/functions/acm/tty-fd.c	1970-01-01 01:00:00.000000000 +0100
+++ linux/drivers/otg/functions/acm/tty-fd.c	2006-09-01 21:41:26.000000000 +0200
@@ -0,0 +1,263 @@
+/*
+ * otg/functions/acm/tty-fd.c
+ *
+ *      Copyright (c) 2003, 2004 Belcarra
+ *
+ * By: 
+ *      Tom Rushworth <tbr@belcarra.com>, 
+ *      Stuart Lynne <sl@belcarra.com>, 
+ *      Bruce Balden <balden@belcarra.com>
+ */
+/*!
+ * @file otg/functions/acm/tty-fd.c
+ * @brief ACM-TTY Descriptor Set
+ *
+ * This file is the USB specific piece that interfaces with the otgcore layer.
+ * If you're looking for the USB specific piece that interfaces with the
+ * host usbcore layer, that's in otg/functions/acm/acm-cl.c
+ *
+ * Note: this function driver requires the following endpoints:
+ *
+ *      BULK-IN
+ *      BULK-OUT
+ *      INTERRUPT-IN
+ *
+ * This function driver cannot be used on devices (such as the StrongArm
+ * SA1100) that do not have an interrupt endpoint.
+ *
+ * @ingroup TTYFunction
+ */
+
+
+#include <otg/otg-compat.h>
+#include <otg/otg-module.h>
+
+#include <linux/init.h>
+#include <asm/uaccess.h>
+#include <linux/ctype.h>
+#include <linux/timer.h>
+#include <linux/interrupt.h>
+#include <asm/atomic.h>
+#include <linux/smp_lock.h>
+#include <linux/slab.h>
+
+#include <otg/usbp-chap9.h>
+#include <otg/usbp-func.h>
+
+#include <otg/otg-trace.h>
+#include "acm.h"
+#include "acm-fd.h"
+#include "acm-os.h"
+
+
+
+/*
+ * CDC ACM Configuration
+ *
+ * Endpoint, Class, Interface, Configuration and Device descriptors/descriptions
+ */
+
+/*! BULK OUT Data Endpoint Descriptor
+ */
+static u8 tty_alt_1[] = { 0x07, USB_DT_ENDPOINT, USB_DIR_OUT, BULK,      0, 0x00, 0x00, };
+
+/*! BULK In Data Endpoint Descriptor
+ */
+static u8 tty_alt_2[] = { 0x07, USB_DT_ENDPOINT, USB_DIR_IN,  BULK,     0,  0x00, 0x00, };
+
+/*! Data Endpoint Descriptor List
+ */
+static struct usbd_endpoint_descriptor *tty_alt_endpoints[] = { 
+        (struct usbd_endpoint_descriptor *) tty_alt_1, 
+        (struct usbd_endpoint_descriptor *) tty_alt_2, };
+
+/*! Data Endpoint Index List
+ */
+u8 tty_alt_indexes[] = { BULK_OUT, BULK_IN, };
+
+/*! INTERRUPT In Comm Endpoint Descriptor
+ */
+static u8 tty_comm_1[] = { 0x07, USB_DT_ENDPOINT, USB_DIR_IN,  INTERRUPT, 0, 0x00, 0x0a, };
+
+/*! Comm Endpoint Descriptor List
+ */
+static struct usbd_endpoint_descriptor *tty_comm_endpoints[] = { (struct usbd_endpoint_descriptor *) tty_comm_1 };
+
+
+/*! Comm Endpoint Index List
+ */
+u8 tty_comm_indexes[] = { INT_IN, };
+
+/*! @{
+ * Class Descriptors
+ */
+static u8 cdc_class_1[] = { 0x05, CS_INTERFACE, USB_ST_HEADER, 0x01, 0x01, /* CLASS_BDC_VERSION, CLASS_BDC_VERSION */ };
+static u8 cdc_class_2[] = { 0x05, CS_INTERFACE, USB_ST_CMF, 0x03, 0x01, /* bMasterInterface: 0, bSlaveInterface: 1 */ };
+static u8 cdc_class_3[] = { 0x05, CS_INTERFACE, USB_ST_UF, 0x00, 0x01, /* bMasterInterface: 0, bSlaveInterface: 1 */ };
+/*! @} */
+
+/*! ACMF Class Descriptor
+ * ACMF - c.f. Table 28
+ * currenty set to 0x2 - Support Set_Line_Coding etc, 
+ *
+ * XXX Should we also set 0x4 - Supports Network_Notification?
+ */
+static u8 cdc_class_4[] = { 0x04, CS_INTERFACE, USB_ST_ACMF, 0x02, };
+
+static struct usbd_generic_class_descriptor *cdc_comm_class_descriptors[] = 
+        { (struct usbd_generic_class_descriptor *) cdc_class_1, 
+        (struct usbd_generic_class_descriptor *) cdc_class_2, 
+        (struct usbd_generic_class_descriptor *) cdc_class_3, 
+        (struct usbd_generic_class_descriptor *) cdc_class_4, };
+
+
+
+/* Alternate Descriptors */
+// First two bytes are identical in all: bLength, bDescriptorType (0x09 0x04)
+
+/*! Comm Alternate Descriptor
+ */
+static u8 cdc_comm_alternate_descriptor[sizeof(struct usbd_interface_descriptor)] = {
+        0x09, USB_DT_INTERFACE, COMM_INTF, 0x00, 0x00, // bInterfaceNumber, bAlternateSetting, bNumEndpoints
+        COMMUNICATIONS_INTERFACE_CLASS, COMMUNICATIONS_ACM_SUBCLASS, 0x01, 0x00, };
+
+/*! Data Alternate Descriptor
+ */
+static u8 cdc_data_alternate_descriptor[sizeof(struct usbd_interface_descriptor)] = {
+        0x09, USB_DT_INTERFACE, DATA_INTF, 0x00, 0x00, // bInterfaceNumber, bAlternateSetting, bNumEndpoints
+        DATA_INTERFACE_CLASS, COMMUNICATIONS_NO_SUBCLASS, COMMUNICATIONS_NO_PROTOCOL, 0x00, };
+
+
+
+/*! Comm alternate descriptions 
+ */
+static struct usbd_alternate_description cdc_comm_alternate_descriptions[] = {
+      { iInterface: CONFIG_OTG_ACM_COMM_INTF,
+              interface_descriptor: (struct usbd_interface_descriptor *)&cdc_comm_alternate_descriptor,
+              classes:sizeof (cdc_comm_class_descriptors) / sizeof (struct usbd_generic_class_descriptor *),
+              class_list: cdc_comm_class_descriptors,
+              endpoint_list: tty_comm_endpoints,
+              endpoints:sizeof (tty_comm_endpoints) / sizeof(struct usbd_endpoint_descriptor *),
+              endpoint_indexes: tty_comm_indexes,
+      }, };
+
+/*! Data alternate descriptions 
+ */
+static struct usbd_alternate_description cdc_data_alternate_descriptions[] = {
+      { iInterface: CONFIG_OTG_ACM_DATA_INTF,
+              interface_descriptor: (struct usbd_interface_descriptor *)&cdc_data_alternate_descriptor,
+              endpoint_list: tty_alt_endpoints,
+              endpoints:sizeof (tty_alt_endpoints) / sizeof(struct usbd_endpoint_descriptor *),
+              endpoint_indexes: tty_alt_indexes,
+              }, };
+
+
+/*! Interface Descriptions List */
+static struct usbd_interface_description cdc_interfaces[] = {
+      { 
+                alternates:sizeof (cdc_comm_alternate_descriptions) / sizeof (struct usbd_alternate_description),
+                alternate_list:cdc_comm_alternate_descriptions,
+      },
+
+      { 
+                alternates:sizeof (cdc_data_alternate_descriptions) / sizeof (struct usbd_alternate_description),
+                alternate_list:cdc_data_alternate_descriptions,
+      },
+
+};
+
+
+/*! Configuration Descriptor 
+ */
+static u8 cdc_configuration_descriptor[sizeof(struct usbd_configuration_descriptor)] = {
+        0x09, USB_DT_CONFIGURATION, 0x00, 0x00, sizeof (cdc_interfaces) / sizeof (struct usbd_interface_description),
+        0x01, 0x00, BMATTRIBUTE, BMAXPOWER, };
+
+/*! Configuration Description 
+ */
+struct usbd_configuration_description tty_description[] = {
+      { iConfiguration: CONFIG_OTG_ACM_DESC,
+              configuration_descriptor: (struct usbd_configuration_descriptor *)cdc_configuration_descriptor,
+      }, };
+
+/*! Device Descriptor 
+ */
+static struct usbd_device_descriptor tty_device_descriptor = {
+        bLength: sizeof(struct usbd_device_descriptor),
+        bDescriptorType: USB_DT_DEVICE,
+        bcdUSB: __constant_cpu_to_le16(USB_BCD_VERSION),
+        bDeviceClass: COMMUNICATIONS_DEVICE_CLASS,
+        bDeviceSubClass: 0x02,
+        bDeviceProtocol: 0x00,
+        bMaxPacketSize0: 0x00,
+        idVendor: __constant_cpu_to_le16(CONFIG_OTG_ACM_VENDORID),
+        idProduct: __constant_cpu_to_le16(CONFIG_OTG_ACM_PRODUCTID),
+        bcdDevice: __constant_cpu_to_le16(CONFIG_OTG_ACM_BCDDEVICE),
+};
+
+#ifdef CONFIG_OTG_HIGH_SPEED
+/*! High Speed Device Qualifier Descriptor 
+ */
+static struct usbd_device_qualifier_descriptor tty_device_qualifier_descriptor = {
+        bLength: sizeof(struct usbd_device_qualifier_descriptor),
+        bDescriptorType: USB_DT_DEVICE_QUALIFIER,
+        bcdUSB: __constant_cpu_to_le16(USB_BCD_VERSION),
+        bDeviceClass: COMMUNICATIONS_DEVICE_CLASS,
+        bDeviceSubClass: 0x02,
+        bDeviceProtocol: 0x00,
+        bMaxPacketSize0: 0x00,
+};
+#endif /* CONFIG_OTG_HIGH_SPEED */
+
+
+/*! Endpoint Request List
+ */
+static struct usbd_endpoint_request tty_endpoint_requests[ENDPOINTS+1] = {
+        { 1, 1, 0, USB_DIR_OUT | USB_ENDPOINT_BULK, 64, 512, },
+        { 1, 1, 0, USB_DIR_IN | USB_ENDPOINT_BULK, 64 /* * 4 */, 512, },
+        { 1, 0, 0, USB_DIR_IN | USB_ENDPOINT_INTERRUPT, 16, 64, },
+        { 0, },
+};
+
+/*! OTG Descriptor
+ */
+static struct usbd_otg_descriptor tty_otg_descriptor = {
+        bLength : sizeof(struct usbd_otg_descriptor),
+        bDescriptorType: USB_DT_OTG,
+        bmAttributes: 0,
+};
+
+/*! Device Description
+ */
+struct usbd_device_description tty_device_description = {
+        device_descriptor: &tty_device_descriptor,
+#ifdef CONFIG_OTG_HIGH_SPEED
+        device_qualifier_descriptor: &tty_device_qualifier_descriptor,
+#endif /* CONFIG_OTG_HIGH_SPEED */
+        otg_descriptor: &tty_otg_descriptor,
+        iManufacturer: CONFIG_OTG_ACM_MANUFACTURER,
+        iProduct: CONFIG_OTG_ACM_PRODUCT_NAME,
+#if !defined(CONFIG_OTG_NO_SERIAL_NUMBER) && defined(CONFIG_OTG_SERIAL_NUMBER_STR)
+        iSerialNumber: CONFIG_OTG_SERIAL_NUMBER_STR, 
+#endif
+};
+
+/*! function_driver 
+ */
+struct usbd_function_driver tty_function_driver = {
+        name: "ACM-TTY",
+        fops:&function_ops,
+        device_description:&tty_device_description,
+        bNumConfigurations:sizeof (tty_description) / sizeof (struct usbd_configuration_description),
+        configuration_description:tty_description,
+        idVendor: __constant_cpu_to_le16(CONFIG_OTG_ACM_VENDORID),
+        idProduct: __constant_cpu_to_le16(CONFIG_OTG_ACM_PRODUCTID),
+        bcdDevice: __constant_cpu_to_le16(CONFIG_OTG_ACM_BCDDEVICE),
+        bNumInterfaces:sizeof (cdc_interfaces) / sizeof (struct usbd_interface_description),
+        interface_list:cdc_interfaces,
+        endpointsRequested: ENDPOINTS,
+        requestedEndpoints: tty_endpoint_requests,
+};
+
+
+
diff -uNr linux/drivers/no-otg/functions/acm/tty-if.c linux/drivers/otg/functions/acm/tty-if.c
--- linux/drivers/no-otg/functions/acm/tty-if.c	1970-01-01 01:00:00.000000000 +0100
+++ linux/drivers/otg/functions/acm/tty-if.c	2006-09-01 21:41:26.000000000 +0200
@@ -0,0 +1,263 @@
+/*
+ * otg/functions/acm/tty-if.c
+ *
+ *      Copyright (c) 2003, 2004 Belcarra
+ *
+ * By: 
+ *      Tom Rushworth <tbr@belcarra.com>, 
+ *      Stuart Lynne <sl@belcarra.com>, 
+ *      Bruce Balden <balden@belcarra.com>
+ */
+/*!
+ * @file otg/functions/acm/tty-if.c
+ * @brief ACM-TTY Descriptor Set
+ *
+ * This file is the USB specific piece that interfaces with the otgcore layer.
+ * If you're looking for the USB specific piece that interfaces with the
+ * host usbcore layer, that's in otg/functions/acm/acm-cl.c
+ *
+ * Note: this function driver requires the following endpoints:
+ *
+ *      BULK-IN
+ *      BULK-OUT
+ *      INTERRUPT-IN
+ *
+ * This function driver cannot be used on devices (such as the StrongArm
+ * SA1100) that do not have an interrupt endpoint.
+ *
+ * @ingroup TTYFunction
+ */
+
+
+#include <otg/otg-compat.h>
+#include <otg/otg-module.h>
+
+#include <linux/init.h>
+#include <asm/uaccess.h>
+#include <linux/ctype.h>
+#include <linux/timer.h>
+#include <linux/interrupt.h>
+#include <asm/atomic.h>
+#include <linux/smp_lock.h>
+#include <linux/slab.h>
+
+#include <otg/usbp-chap9.h>
+#include <otg/usbp-func.h>
+
+#include <otg/otg-trace.h>
+#include "acm.h"
+#include "acm-fd.h"
+#include "acm-os.h"
+
+
+
+/*
+ * CDC ACM Configuration
+ *
+ * Endpoint, Class, Interface, Configuration and Device descriptors/descriptions
+ */
+
+/*! BULK OUT Data Endpoint Descriptor
+ */
+static u8 tty_alt_1[] = { 0x07, USB_DT_ENDPOINT, USB_DIR_OUT, BULK,      0, 0x00, 0x00, };
+
+/*! BULK In Data Endpoint Descriptor
+ */
+static u8 tty_alt_2[] = { 0x07, USB_DT_ENDPOINT, USB_DIR_IN,  BULK,     0,  0x00, 0x00, };
+
+/*! Data Endpoint Descriptor List
+ */
+static struct usbd_endpoint_descriptor *tty_alt_endpoints[] = { 
+        (struct usbd_endpoint_descriptor *) tty_alt_1, 
+        (struct usbd_endpoint_descriptor *) tty_alt_2, };
+
+/*! Data Endpoint Index List
+ */
+u8 tty_alt_indexes[] = { BULK_OUT, BULK_IN, };
+
+/*! INTERRUPT In Comm Endpoint Descriptor
+ */
+static u8 tty_comm_1[] = { 0x07, USB_DT_ENDPOINT, USB_DIR_IN,  INTERRUPT, 0, 0x00, 0x0a, };
+
+/*! Comm Endpoint Descriptor List
+ */
+static struct usbd_endpoint_descriptor *tty_comm_endpoints[] = { (struct usbd_endpoint_descriptor *) tty_comm_1 };
+
+
+/*! Comm Endpoint Index List
+ */
+u8 tty_comm_indexes[] = { INT_IN, };
+
+/*! @{
+ * Class Descriptors
+ */
+static u8 cdc_class_1[] = { 0x05, CS_INTERFACE, USB_ST_HEADER, 0x01, 0x01, /* CLASS_BDC_VERSION, CLASS_BDC_VERSION */ };
+static u8 cdc_class_2[] = { 0x05, CS_INTERFACE, USB_ST_CMF, 0x03, 0x01, /* bMasterInterface: 0, bSlaveInterface: 1 */ };
+static u8 cdc_class_3[] = { 0x05, CS_INTERFACE, USB_ST_UF, 0x00, 0x01, /* bMasterInterface: 0, bSlaveInterface: 1 */ };
+/*! @} */
+
+/*! ACMF Class Descriptor
+ * ACMF - c.f. Table 28
+ * currenty set to 0x2 - Support Set_Line_Coding etc, 
+ *
+ * XXX Should we also set 0x4 - Supports Network_Notification?
+ */
+static u8 cdc_class_4[] = { 0x04, CS_INTERFACE, USB_ST_ACMF, 0x02, };
+
+static struct usbd_generic_class_descriptor *cdc_comm_class_descriptors[] = 
+        { (struct usbd_generic_class_descriptor *) cdc_class_1, 
+        (struct usbd_generic_class_descriptor *) cdc_class_2, 
+        (struct usbd_generic_class_descriptor *) cdc_class_3, 
+        (struct usbd_generic_class_descriptor *) cdc_class_4, };
+
+
+
+/* Alternate Descriptors */
+// First two bytes are identical in all: bLength, bDescriptorType (0x09 0x04)
+
+/*! Comm Alternate Descriptor
+ */
+static u8 cdc_comm_alternate_descriptor[sizeof(struct usbd_interface_descriptor)] = {
+        0x09, USB_DT_INTERFACE, COMM_INTF, 0x00, 0x00, // bInterfaceNumber, bAlternateSetting, bNumEndpoints
+        COMMUNICATIONS_INTERFACE_CLASS, COMMUNICATIONS_ACM_SUBCLASS, 0x01, 0x00, };
+
+/*! Data Alternate Descriptor
+ */
+static u8 cdc_data_alternate_descriptor[sizeof(struct usbd_interface_descriptor)] = {
+        0x09, USB_DT_INTERFACE, DATA_INTF, 0x00, 0x00, // bInterfaceNumber, bAlternateSetting, bNumEndpoints
+        DATA_INTERFACE_CLASS, COMMUNICATIONS_NO_SUBCLASS, COMMUNICATIONS_NO_PROTOCOL, 0x00, };
+
+
+
+/*! Comm alternate descriptions 
+ */
+static struct usbd_alternate_description cdc_comm_alternate_descriptions[] = {
+      { iInterface: CONFIG_OTG_ACM_COMM_INTF,
+              interface_descriptor: (struct usbd_interface_descriptor *)&cdc_comm_alternate_descriptor,
+              classes:sizeof (cdc_comm_class_descriptors) / sizeof (struct usbd_generic_class_descriptor *),
+              class_list: cdc_comm_class_descriptors,
+              endpoint_list: tty_comm_endpoints,
+              endpoints:sizeof (tty_comm_endpoints) / sizeof(struct usbd_endpoint_descriptor *),
+              endpoint_indexes: tty_comm_indexes,
+      }, };
+
+/*! Data alternate descriptions 
+ */
+static struct usbd_alternate_description cdc_data_alternate_descriptions[] = {
+      { iInterface: CONFIG_OTG_ACM_DATA_INTF,
+              interface_descriptor: (struct usbd_interface_descriptor *)&cdc_data_alternate_descriptor,
+              endpoint_list: tty_alt_endpoints,
+              endpoints:sizeof (tty_alt_endpoints) / sizeof(struct usbd_endpoint_descriptor *),
+              endpoint_indexes: tty_alt_indexes,
+              }, };
+
+
+/*! Interface Descriptions List */
+static struct usbd_interface_description cdc_interfaces[] = {
+      { 
+                alternates:sizeof (cdc_comm_alternate_descriptions) / sizeof (struct usbd_alternate_description),
+                alternate_list:cdc_comm_alternate_descriptions,
+      },
+
+      { 
+                alternates:sizeof (cdc_data_alternate_descriptions) / sizeof (struct usbd_alternate_description),
+                alternate_list:cdc_data_alternate_descriptions,
+      },
+
+};
+
+
+/*! Configuration Descriptor 
+ */
+static u8 cdc_configuration_descriptor[sizeof(struct usbd_configuration_descriptor)] = {
+        0x09, USB_DT_CONFIGURATION, 0x00, 0x00, sizeof (cdc_interfaces) / sizeof (struct usbd_interface_description),
+        0x01, 0x00, BMATTRIBUTE, BMAXPOWER, };
+
+/*! Configuration Description 
+ */
+struct usbd_configuration_description tty_description[] = {
+      { iConfiguration: CONFIG_OTG_ACM_DESC,
+              configuration_descriptor: (struct usbd_configuration_descriptor *)cdc_configuration_descriptor,
+      }, };
+
+/*! Device Descriptor 
+ */
+static struct usbd_device_descriptor tty_device_descriptor = {
+        bLength: sizeof(struct usbd_device_descriptor),
+        bDescriptorType: USB_DT_DEVICE,
+        bcdUSB: __constant_cpu_to_le16(USB_BCD_VERSION),
+        bDeviceClass: COMMUNICATIONS_DEVICE_CLASS,
+        bDeviceSubClass: 0x02,
+        bDeviceProtocol: 0x00,
+        bMaxPacketSize0: 0x00,
+        idVendor: __constant_cpu_to_le16(CONFIG_OTG_ACM_VENDORID),
+        idProduct: __constant_cpu_to_le16(CONFIG_OTG_ACM_PRODUCTID),
+        bcdDevice: __constant_cpu_to_le16(CONFIG_OTG_ACM_BCDDEVICE),
+};
+
+#ifdef CONFIG_OTG_HIGH_SPEED
+/*! High Speed Device Qualifier Descriptor 
+ */
+static struct usbd_device_qualifier_descriptor tty_device_qualifier_descriptor = {
+        bLength: sizeof(struct usbd_device_qualifier_descriptor),
+        bDescriptorType: USB_DT_DEVICE_QUALIFIER,
+        bcdUSB: __constant_cpu_to_le16(USB_BCD_VERSION),
+        bDeviceClass: COMMUNICATIONS_DEVICE_CLASS,
+        bDeviceSubClass: 0x02,
+        bDeviceProtocol: 0x00,
+        bMaxPacketSize0: 0x00,
+};
+#endif /* CONFIG_OTG_HIGH_SPEED */
+
+
+/*! Endpoint Request List
+ */
+static struct usbd_endpoint_request tty_endpoint_requests[ENDPOINTS+1] = {
+        { 1, 1, 0, USB_DIR_OUT | USB_ENDPOINT_BULK, 64, 512, },
+        { 1, 1, 0, USB_DIR_IN | USB_ENDPOINT_BULK, 64 /* * 4 */, 512, },
+        { 1, 0, 0, USB_DIR_IN | USB_ENDPOINT_INTERRUPT, 16, 64, },
+        { 0, },
+};
+
+/*! OTG Descriptor
+ */
+static struct usbd_otg_descriptor tty_otg_descriptor = {
+        bLength : sizeof(struct usbd_otg_descriptor),
+        bDescriptorType: USB_DT_OTG,
+        bmAttributes: 0,
+};
+
+/*! Device Description
+ */
+struct usbd_device_description tty_device_description = {
+        device_descriptor: &tty_device_descriptor,
+#ifdef CONFIG_OTG_HIGH_SPEED
+        device_qualifier_descriptor: &tty_device_qualifier_descriptor,
+#endif /* CONFIG_OTG_HIGH_SPEED */
+        otg_descriptor: &tty_otg_descriptor,
+        iManufacturer: CONFIG_OTG_ACM_MANUFACTURER,
+        iProduct: CONFIG_OTG_ACM_PRODUCT_NAME,
+#if !defined(CONFIG_OTG_NO_SERIAL_NUMBER) && defined(CONFIG_OTG_SERIAL_NUMBER_STR)
+        iSerialNumber: CONFIG_OTG_SERIAL_NUMBER_STR, 
+#endif
+};
+
+/*! function_driver 
+ */
+struct usbd_function_driver tty_function_driver = {
+        name: "ACM-TTY",
+        fops:&function_ops,
+        device_description:&tty_device_description,
+        bNumConfigurations:sizeof (tty_description) / sizeof (struct usbd_configuration_description),
+        configuration_description:tty_description,
+        idVendor: __constant_cpu_to_le16(CONFIG_OTG_ACM_VENDORID),
+        idProduct: __constant_cpu_to_le16(CONFIG_OTG_ACM_PRODUCTID),
+        bcdDevice: __constant_cpu_to_le16(CONFIG_OTG_ACM_BCDDEVICE),
+        bNumInterfaces:sizeof (cdc_interfaces) / sizeof (struct usbd_interface_description),
+        interface_list:cdc_interfaces,
+        endpointsRequested: ENDPOINTS,
+        requestedEndpoints: tty_endpoint_requests,
+};
+
+
+
diff -uNr linux/drivers/no-otg/functions/acm/tty-l24-os.c linux/drivers/otg/functions/acm/tty-l24-os.c
--- linux/drivers/no-otg/functions/acm/tty-l24-os.c	1970-01-01 01:00:00.000000000 +0100
+++ linux/drivers/otg/functions/acm/tty-l24-os.c	2006-09-01 21:41:26.000000000 +0200
@@ -0,0 +1,1291 @@
+/*
+ * otg/functions/acm/tty-l24-os.c
+ *
+ *      Copyright (c) 2003, 2004 Belcarra
+ *
+ * By:
+ *      Tom Rushworth <tbr@belcarra.com>,
+ *      Stuart Lynne <sl@belcarra.com>,
+ *      Bruce Balden <balden@belcarra.com>
+ *
+ */
+
+/*!
+ * @file otg/functions/acm/tty-l24-os.c
+ * @brief ACM Function Driver private defines
+ *
+ * An ACM (Abstract Control Model) driver is composed of two pieces:
+ *
+ *    1) An OS specific piece that handles creating and operating
+ *       a serial device for the given OS.
+ *
+ *    2) A USB specific piece that interfaces either with the host
+ *       usbcore layer, or with the otgcore layer.
+ *
+ * If the USB piece interfaces with the host usbcore layer you get an ACM
+ * class driver.  If the USB piece interfaces with the otgcore layer you get
+ * an ACM function driver.
+ *
+ * This file is the OS specific piece that interfaces with Linux (kernel
+ * versions 2.4.*).  It talks to the bottom edge of the Linux tty layer.
+ *
+ * See the file acm/tty.c for USB descriptor definitions used for this
+ * function.
+ *
+ * @ingroup TTYFunction
+ */
+
+
+//#include <otg/osversion.h>
+#include <otg/otg-compat.h>
+#include <otg/otg-module.h>
+
+MOD_AUTHOR ("tbr@belcarra.com, sl@belcarra.com");
+
+MOD_DESCRIPTION ("Belcarra TTY Function");
+EMBED_LICENSE();
+
+#include <linux/init.h>
+#include <asm/uaccess.h>
+#include <linux/ctype.h>
+#include <linux/timer.h>
+#include <linux/interrupt.h>
+#include <asm/atomic.h>
+#include <linux/tty.h>
+#include <linux/tty_driver.h>
+#include <linux/tty_flip.h>
+#include <linux/smp_lock.h>
+#include <linux/slab.h>
+#include <linux/serial.h>
+
+#include <otg/usbp-chap9.h>
+#include <otg/usbp-func.h>
+
+#include <linux/capability.h>
+#include "tty.h"
+#include <otg/otg-trace.h>
+#include "acm.h"
+#include "acm-fd.h"
+#include "acm-os.h"
+
+static struct acm_usb_services *usb_ops = &acm_fd_usb_ops;
+
+#define TTY tty_fd_trace_tag
+otg_tag_t tty_fd_trace_tag;
+
+/* Module Parameters ************************************************************************* */
+
+static u32 vendor_id;
+static u32 product_id;
+static u32 max_queued_urbs = MAX_QUEUED_URBS;
+static u32 max_queued_bytes = MAX_QUEUED_BYTES;
+
+MOD_PARM (vendor_id, "i");
+MOD_PARM (product_id, "i");
+MOD_PARM (max_queued_urbs, "i");
+MOD_PARM (max_queued_bytes, "i");
+
+MOD_PARM_DESC (vendor_id, "Device Vendor ID");
+MOD_PARM_DESC (product_id, "Device Product ID");
+MOD_PARM_DESC (max_queued_urbs, "Maximum TX Queued Urbs");
+MOD_PARM_DESC (max_queued_bytes, "Maximum TX Queued Bytes");
+
+
+/* ACM ***************************************************************************************** */
+
+#define ACM_TTY_MAJOR   166
+#define ACM_TTY_MINOR   0
+#define ACM_TTY_MINORS  1
+
+extern struct acm_private acmfd_private;
+extern struct tty_private ttyfd_private;
+
+
+/* ******************************************************************************************* */
+
+#define TTY_OVERFLOW_SIZE       512
+
+u8 tty_overflow_buffer[TTY_OVERFLOW_SIZE];
+int tty_overflow_used;
+
+/* ******************************************************************************************* */
+
+/*! ttyfd_schedule
+ *
+ * Schedule a bottom half handler work item.
+ *
+ * @param queue
+ */
+STATIC void ttyfd_schedule(WORK_ITEM *queue)
+{
+        RETURN_IF(NO_WORK_DATA((*queue)) || PENDING_WORK_ITEM((*queue)));
+#if defined(LINUX24)
+        queue_task(queue, &tq_immediate);
+        mark_bh(IMMEDIATE_BH);
+        TRACE_MSG1(TTY,"task %p scheduled and marked",queue);
+#else
+        SCHEDULE_WORK((*queue));
+        TRACE_MSG1(TTY,"task %p scheduled",queue);
+#endif
+}
+
+
+/*! ttyfd_block_until_ready
+ *
+ * Called from tty_open to implement blocking open. Wait for DTR indication
+ * from acm-fd.
+ *
+ * Called by TTY layer to open device.
+ *
+ * @param tty
+ * @param filp
+ * @param acm
+ * @returns non-zero for error
+ */
+STATIC int ttyfd_block_until_ready(struct tty_struct *tty, struct file *filp, struct acm_private *acm)
+{
+        struct tty_private *tty_private = (struct tty_private *) acm->privdata;
+        unsigned long flags;
+        int rc = 0;
+
+        local_irq_save(flags);
+        //TRACE_MSG1(TTY,"open_wait_count: %d --> at entry", tty_private->open_wait_count);
+        //tty_private->open_wait_count += 1;
+        for (;;) {
+                /* check if the file has been closed */
+                if (tty_hung_up_p(filp)) {
+                        TRACE_MSG0(TTY,"tty_hung_up_p()");
+                        rc = -ERESTARTSYS;
+                        break;
+                }
+                /* check for pending signals */
+                if (signal_pending(current)) {
+                        TRACE_MSG0(TTY,"signal_pending()");
+                        rc = -ERESTARTSYS;
+                        break;
+                }
+                /* check if the module is unloading */
+                if (tty_private->exiting) {
+                        TRACE_MSG0(TTY,"module exiting()");
+                        rc = -ENODEV;
+                        break;
+                }
+                /* check for what we want, do we have DTR?
+                 */
+                if (acm->bmLineState & CDC_LINESTATE_D0_DTR) {
+                        // OK, there's somebody on the other end, let's go...
+                        TRACE_MSG0(TTY,"found DTR");
+                        rc = 0;
+                        break;
+                }
+                //TRACE_MSG1(TTY,"open_wait_count: %d sleeping...", tty_private->open_wait_count);
+
+                /* restore irqs */
+                local_irq_restore(flags);
+
+                /* sleep */
+                interruptible_sleep_on(&tty_private->open_wait);
+
+                /* lock irqs again */
+                local_irq_save(flags);
+
+                //TRACE_MSG1(TTY,"open_wait_count: %d got WAKEUP", tty_private->open_wait_count);
+        }
+        //tty_private->open_wait_count -= 1;
+        //TRACE_MSG1(TTY,"open_wait_count: %d <-- at exit", tty_private->open_wait_count);
+        local_irq_restore(flags);
+        return(rc);
+}
+
+/*! ttyfd_call_hangup
+ *
+ * Bottom half handler to safely send hangup signal to process group.
+ *
+ * @param private - point to acm_private dat structure
+ */
+STATIC void ttyfd_call_hangup(void *private)
+{
+        struct acm_private *acm = (struct acm_private *) private;
+        struct tty_private *tty_private = (struct tty_private *) acm->privdata;
+
+        struct tty_struct *tty = (NULL == acm) ? NULL : tty_private->tty;
+
+        TRACE_MSG5(acm->trace_tag,"used: %d MOD_IN_USE: %d configured: %d c_cflag: %04x clocal: %d",
+                        atomic_read(&acm->used), MOD_IN_USE, acm->flags & ACM_CONFIGURED,
+                        tty_private->c_cflag, tty_private->c_cflag & CLOCAL);
+
+
+        if (tty && !(tty_private->c_cflag & CLOCAL))
+                tty_hangup(tty);
+
+        wake_up_interruptible(&tty_private->open_wait);
+
+        TRACE_MSG0(TTY,"exited");
+}
+
+/*! ttyfd_schedule_hangup
+ *
+ * Schedule hangup bottom half handler.
+ *
+ * @param acm - point to acm_private dat structure
+ */
+void ttyfd_schedule_hangup(struct acm_private *acm)
+{
+        struct tty_private *tty_private = (struct tty_private *) acm->privdata;
+
+        TRACE_MSG0(TTY, "entered");
+        ttyfd_schedule(&tty_private->hqueue);
+}
+
+
+/*! tty_open
+ *
+ * Called by TTY layer to open device.
+ *
+ * @param tty
+ * @param filp
+ * @returns non-zero for error
+ */
+STATIC int tty_open(struct tty_struct *tty, struct file *filp)
+{
+        struct acm_private *acm = &acmfd_private;
+        struct tty_private *tty_private = (struct tty_private *) acm->privdata;
+        int used;
+        int nonblocking;
+        int rc = 0;
+        unsigned long flags;
+
+        TRACE_MSG3(acm->trace_tag,"used: %d MOD_IN_USE: %d configured: %d",
+                        atomic_read(&acm->used), MOD_IN_USE, acm->flags & ACM_CONFIGURED);
+
+        TRACE_MSG2(TTY,"f_flags: %08x NONBLOCK: %x",
+                        filp->f_flags, filp->f_flags & O_NONBLOCK);
+
+        nonblocking = filp->f_flags & O_NONBLOCK;
+
+        /* Lock and increment used counter, save current value.
+         * Check for exclusive open at the same time.
+         */
+        local_irq_save(flags);
+
+        /* The value of acm->used controls MOD_{INC/DEC}_USE_COUNT, so
+         * it has to be incremented unconditionally, and no early return
+         * made until after USE_COUNT has been adjusted to match.
+         */
+        used = atomic_post_inc(&acm->used);
+#ifdef MCEL
+        filp->f_flags |= O_EXCL;  // QQQ Can we persuade MCEL to add this to their app?
+        if (nonblocking && !(acm->connected)) {
+                // QQQ Is MCEL actually using this "feature"?  (See below for printk)
+                rc = -EINVAL;
+        }
+        else
+#endif
+#if 0
+        if (filp->f_flags & O_EXCL) {
+                /* This is intended to be an exclusive open, so
+                 * make sure no one has the device open already, and
+                 * set the exclusive flag so no one can open it later.
+                 */
+                if (used > 0) {
+                        // Someone already has it.
+                        rc = -EBUSY;
+                }
+                else {
+                        tty_private->flags |= TTYFD_EXCLUSIVE;
+                        set_bit(TTY_EXCLUSIVE, &tty->flags);
+                }
+        }
+#if defined(LINUX24)
+        else if ((tty_private->flags & TTYFD_EXCLUSIVE) && !suser())
+#else
+	if ((tty_private->flags & TTYFD_EXCLUSIVE) && !capable(CAP_SYS_TTY_CONFIG))
+#endif
+        {
+                // Only the superuser can do a normal open of an O_EXCL tty
+                rc = -EBUSY;
+        }
+#endif
+        local_irq_restore(flags);
+
+        // OK, now it's safe to make an early return if we are failing.
+        if (rc) {
+#ifdef MCEL
+                // This can dissappear when the "feature" above does.
+                if (-EINVAL == rc)
+                        //printk(KERN_INFO "\nusb cable not connected!\n");
+#endif
+                return(rc);
+        }
+        local_irq_save(flags);
+
+
+        /* To truly emulate the old dual-device approach of having a non-blocking
+         * device (e.g cu0) and a blocking device (e.g. tty0) we would need to
+         * track blocking and non-blocking opens separately.  We don't.  This
+         * may lead to funny behavior in the multiple open case.
+         */
+        UNLESS (used) {
+                MOD_INC_USE_COUNT;
+                TRACE_MSG1(TTY,"INC: %d", MOD_IN_USE);
+                // First open.
+                TRACE_MSG2(TTY, "FIRST OPEN nonblocking: %x exclusive: %x", nonblocking, tty_private->flags & TTYFD_EXCLUSIVE);
+                tty->driver_data = acm;
+                tty_private->tty = tty;
+                tty->low_latency = 1;
+                usb_ops->open(acm, DATA_INTF);
+                usb_ops->set_local(acm, 1);
+        }
+        local_irq_restore(flags);
+
+
+        /* All done if configured
+         */
+        RETURN_ZERO_UNLESS(usb_ops->ready(acm));
+
+        /* All done if non blocking open
+         */
+        RETURN_ZERO_IF(nonblocking);
+
+        /* Block open - wait until ready
+         */
+        TRACE_MSG0(TTY, "BLOCKING - wait until ready");
+        rc = ttyfd_block_until_ready(tty,filp,acm);
+
+        /* The tty layer calls tty_close() even if this open fails,
+         * so any cleanup (rc != 0) will be done there.
+         */
+        TRACE_MSG3(acm->trace_tag,"used: %d MOD_IN_USE: %d configured: %d",
+                        atomic_read(&acm->used), MOD_IN_USE, acm->flags & ACM_CONFIGURED);
+        return(rc);
+}
+
+/*! ttyfd_wakeup_opens
+ *
+ * Called by acm_fd to wakeup processes blocked in open waiting for DTR.
+ *
+ * @param acm - pointer to acm private data structure
+ */
+void ttyfd_wakeup_opens(struct acm_private *acm)
+{
+        struct tty_private *tty_private = (struct tty_private *) acm->privdata;
+        TRACE_MSG0(TTY, "entered");
+        //if (tty_private->open_wait_count > 0)
+        wake_up_interruptible(&tty_private->open_wait);
+}
+
+/*! tty_close
+ *
+ * Called by TTY layer to close device.
+ *
+ * @param tty
+ * @param filp
+ * @returns non-zero for error
+ */
+STATIC void tty_close(struct tty_struct *tty, struct file *filp)
+{
+        struct acm_private *acm = tty->driver_data;
+        struct tty_private *tty_private = (struct tty_private *) acm->privdata;
+        struct usbd_function_instance *function;
+        int used;
+
+        TRACE_MSG0(TTY, "entered");
+
+        //UNLESS (acm) {
+        //        printk(KERN_INFO"%s: ACM null\n", __FUNCTION__);
+        //        return;
+        //}
+
+        usb_ops->close(acm, DATA_INTF);
+        tty_private = (struct tty_private *) acm->privdata;
+        function = acm->function;
+        TRACE_MSG3(acm->trace_tag,"used: %d MOD_IN_USE: %d configured: %d",
+                        atomic_read(&acm->used), MOD_IN_USE, acm->flags & ACM_CONFIGURED);
+
+        // lock and decrement used counter, save result
+        used = atomic_pre_dec(&acm->used);
+
+        // finished unless this is the last close
+
+        RETURN_IF (used > 0);
+
+        TRACE_MSG0(TTY,"FINAL CLOSE");
+
+        tty_private->tty = NULL;
+
+        /* This should never happen if this is the last close,
+         * but it can't hurt to check.
+         */
+        //if (tty_private->open_wait_count)
+        wake_up_interruptible(&tty_private->open_wait);
+
+        if (tty_private->flags & TTYFD_EXCLUSIVE) {
+                tty_private->flags &= ~TTYFD_EXCLUSIVE;
+                if (tty)
+                        clear_bit(TTYFD_EXCLUSIVE, &tty->flags);
+        }
+
+        _MOD_DEC_USE_COUNT;
+        TRACE_MSG1(TTY,"DEC: %d", MOD_IN_USE);
+        TRACE_MSG1(TTY,"LAST CLOSE r-f=%d",(acm->bytes_received-acm->bytes_forwarded));
+
+        MOD_DEC_USE_COUNT;
+
+        TRACE_MSG0(TTY,"FINISHED");
+}
+
+/* Transmit Function - called by tty layer ****************************************************** */
+
+int ttyfd_recv_space_available(struct acm_private *acm, int interface);
+int ttyfd_recv_chars(struct acm_private *acm, int interface, u8 *cp, int n);
+
+#define LOOP_BUF 16
+/*! ttyfd_loop_xmit_chars
+ *
+ * Implement data loop back, send xmit data back as received data.
+ *
+ * @param acm - pointer to acm private data structure
+ * @param interface - data interface to send on
+ * @param count - number of bytes to send
+ * @param from_user - true if from user memory space
+ * @param buf - pointer to data to send
+ * @return count - number of bytes actually sent.
+ */
+STATIC int ttyfd_loop_xmit_chars(struct acm_private *acm, int interface, int count, int from_user, const unsigned char *buf)
+{
+        int received = 0;
+        while (count > 0) {
+                u8 copybuf[LOOP_BUF];
+                int i;
+                int space = ttyfd_recv_space_available(acm, DATA_INTF);
+                int recv = MIN (space, LOOP_BUF);
+                recv = MIN(recv, count);
+
+                //TRACE_MSG7(TTY, "buf: %8x buf+received: %8x received: %d from_user: %d count: %d space: %d recv: %d\n",
+                //                buf, buf + received, received, from_user, count, space, recv);
+
+                BREAK_UNLESS(recv);
+
+                if (from_user)
+                        copy_from_user ((void *)copybuf, (void*)(buf + received), recv);
+                else
+                        memcpy ((void *)copybuf, (void*)(buf + received), recv);
+
+                count -= recv;
+                received += recv;
+                TRACE_MSG3(TTY, "count: %d recv: %d received: %d", count, recv, received);
+                ttyfd_recv_chars(acm, DATA_INTF, copybuf, recv);
+        }
+        return received;
+}
+
+/*! ttyfd_overflow_send
+ *
+ * Check if there is data in overflow buffer and send if neccessary.
+ *
+ * @param acm - pointer to acm private data structure
+ * @param interface - data interface to send on
+ * @param force - force the send
+ */
+void ttyfd_overflow_send(struct acm_private *acm, int interface, int force)
+{
+        int chars_in_buffer;
+        int count;
+
+        unsigned long flags;
+        local_irq_save(flags);
+
+        chars_in_buffer = usb_ops->chars_in_buffer(acm, DATA_INTF);
+
+        TRACE_MSG3(TTY, "overflow_used: %d chars_in_buffer: %d force: %d", tty_overflow_used, chars_in_buffer, force);
+
+        if (force || !chars_in_buffer || tty_overflow_used > 200) {
+
+                count = usb_ops->xmit_chars(acm, DATA_INTF, tty_overflow_used, 0, tty_overflow_buffer);
+
+                TRACE_MSG2(TTY, "overflow_used: %d count: %d", tty_overflow_used, count);
+
+                tty_overflow_used = 0;
+
+        }
+        local_irq_restore(flags);
+}
+
+
+/*! ttyfd_overflow_xmit_chars
+ *
+ * Implement overflow buffer. The TTY layer implements echo with single
+ * character writes which can quickly exhaust the urbs allowed for sending.
+ * This results in data being lost.
+ *
+ * This function is called to accumulate small amounts of data when there is
+ * already an bulk in urb in the queue.
+ *
+ * The urb sent function calls wakeup writers which will call
+ * ttyfd_overflow_send which will send the data.
+ *
+ * @param acm - pointer to acm private data structure
+ * @param interface - data interface to send on
+ * @param count - number of bytes to send
+ * @param from_user - true if from user memory space
+ * @param buf - pointer to data to send
+ * @return count - number of bytes actually sent.
+ */
+STATIC int ttyfd_overflow_xmit_chars(struct acm_private *acm, int interface, int count, int from_user, const unsigned char *buf)
+{
+        unsigned long flags;
+        int overflow_room;
+        int write_room = usb_ops->write_room(acm, DATA_INTF);
+
+        TRACE_MSG2(TTY,"-> count: %d from_usr: %d", count, from_user);
+        local_irq_save(flags);
+
+        if ((overflow_room = TTY_OVERFLOW_SIZE - tty_overflow_used) > 0) {
+
+                count = MIN(overflow_room, count);
+                if (from_user)
+                        copy_from_user ((void *)tty_overflow_buffer + tty_overflow_used, (void*)buf, count);
+                else
+                        memcpy ((void *)tty_overflow_buffer + tty_overflow_used, (void*)buf, count);
+
+                tty_overflow_used += count;
+        }
+        else
+                count = 0;
+
+        local_irq_restore(flags);
+
+        /* start sending from overflow if neccessary
+         */
+        ttyfd_overflow_send(acm, interface, 0);
+
+        return count;
+}
+
+/*! tty_chars_in_buffer
+ *
+ * Called by TTY layer to determine amount of pending write data.
+ *
+ * @param tty - pointer to tty data structure
+ * @return amount of pending write data
+ */
+STATIC int tty_chars_in_buffer(struct tty_struct *tty)
+{
+        struct acm_private *acm = tty->driver_data;
+        struct tty_private *tty_private = (struct tty_private *) acm->privdata;
+        int rc;
+
+        //TRACE_MSG0(TTY, "entered");
+
+        rc = usb_ops->chars_in_buffer(acm, DATA_INTF);
+        TRACE_MSG1(TTY, "chars in buffer: %d", rc);
+        return rc;
+        //return(usb_ops->chars_in_buffer(acm, DATA_INTF));
+}
+
+/*! tty_write
+ *
+ * Called by TTY layer to write data.
+ * @param tty - pointer to tty data structure
+ * @param from_user - true if from user memory space
+ * @param buf - pointer to data to send
+ * @param  count - number of bytes want to send.
+ * @return count - number of bytes actually sent.
+ */
+STATIC int tty_write(struct tty_struct *tty, int from_user, const unsigned char *buf, int count)
+{
+        struct acm_private *acm = tty->driver_data;
+        struct tty_private *tty_private = (struct tty_private *) acm->privdata;
+
+        TRACE_MSG3(TTY,"-> count: %d loopback: %d from_usr: %d", count, tty_private->tiocm & TIOCM_LOOP, from_user);
+
+        /* loopback mode
+         */
+        if (tty_private->tiocm & TIOCM_LOOP)
+                return ttyfd_loop_xmit_chars(acm, DATA_INTF, count, from_user, buf);
+
+        /* overflow mode
+         */
+        if (tty_overflow_used || (tty_chars_in_buffer(tty_private->tty) && (count < 64)))
+                return ttyfd_overflow_xmit_chars(acm, DATA_INTF, count, from_user, buf);
+
+        /* straight through mode
+         */
+        return usb_ops->xmit_chars(acm, DATA_INTF, count, from_user, buf);
+
+}
+
+/*! tty_write_room
+ *
+ * Called by TTY layer get amount of write room available.
+ *
+ * @param tty - pointer to tty data structure
+ * @return amount of write room available
+ */
+STATIC int tty_write_room(struct tty_struct *tty)
+{
+        struct acm_private *acm = tty->driver_data;
+        struct tty_private *tty_private = (struct tty_private *) acm->privdata;
+        int rc;
+
+        TRACE_MSG0(TTY, "entered");
+        /* loopback mode
+         */
+        if (tty_private->tiocm & TIOCM_LOOP)
+                return ttyfd_recv_space_available(acm, DATA_INTF);
+
+        rc = usb_ops->write_room(acm, DATA_INTF) + (TTY_OVERFLOW_SIZE - tty_overflow_used);
+        TRACE_MSG1(TTY, "write room: %d", rc);
+
+        return rc;
+
+
+        //return(usb_ops->write_room(acm, DATA_INTF));
+}
+
+static int throttle_count = 0;
+static int unthrottle_count = 0;
+
+/*! tty_throttle
+ *
+ * Called by TTY layer to throttle (do not allow received data.)
+ *
+ * @return amount of write room available
+ */
+STATIC void tty_throttle(struct tty_struct *tty)
+{
+        struct acm_private *acm = tty->driver_data;
+        struct tty_private *tty_private = (struct tty_private *) acm->privdata;
+
+        TRACE_MSG0(TTY, "entered");
+        //tty_private->flags |= TTYFD_THROTTLED;
+        usb_ops->throttle(acm, DATA_INTF);
+}
+
+/*! tty_unthrottle
+ *
+ * Called by TTY layer to unthrottle (allow received data.)
+ *
+ * @return amount of write room available
+ */
+STATIC void tty_unthrottle(struct tty_struct *tty)
+{
+        struct acm_private *acm = tty->driver_data;
+        struct tty_private *tty_private = (struct tty_private *) acm->privdata;
+
+        TRACE_MSG0(TTY, "entered");
+        //tty_private->flags &= ~TTYFD_THROTTLED;
+        usb_ops->unthrottle(acm, DATA_INTF);
+
+        /* This function is called while the TTY_DONT_FLIP flag is still
+         * set, so there is no point trying to push the flip buffer.  Just
+         * try to queue some recv urbs, and keep trying until we do manage
+         * to get some queued.
+         */
+        tty_schedule_flip(tty);
+}
+
+/* ********************************************************************************************** */
+
+/*! ttyfd_tiocm
+ * @param acm
+ * @param tiocm
+ * @return - computed tiocm value
+ */
+unsigned int ttyfd_tiocm(struct acm_private *acm, unsigned int tiocm)
+{
+        tiocm &= ~ ( TIOCM_DTR | TIOCM_DSR | TIOCM_CAR);
+
+        return tiocm |
+                usb_ops->get_dtr(acm) ? TIOCM_DTR : 0 |
+                usb_ops->get_dsr(acm) ? TIOCM_DSR : 0 |
+                usb_ops->get_dcd(acm) ? TIOCM_CAR : 0 ;
+}
+
+/*! tty_ioctl
+ *
+ * Used by TTY layer to execute IOCTL command.
+ *
+ * Called by TTY layer to open device.
+ *
+ * Unhandled commands must return -ENOIOCTLCMD for correct operation.
+ *
+ * @param tty
+ * @param file
+ * @param cmd
+ * @param arg
+ * @returns non-zero for error
+ */
+STATIC int acm_tty_ioctl(struct tty_struct *tty, struct file *file, unsigned int cmd, unsigned long arg)
+{
+        struct acm_private *acm = tty->driver_data;
+        struct tty_private *tty_private = (struct tty_private *) acm->privdata;
+        unsigned int mask;
+        unsigned int newctrl;
+        unsigned int tiocm;
+        unsigned int saved_tiocm;
+        unsigned int changed_tiocm;
+        int rc;
+
+        //TRACE_MSG3(TTY,"entered: %8x dir: %x size: %x", cmd, _IOC_DIR(cmd), _IOC_SIZE(cmd));
+
+        switch(cmd) {
+
+        case TIOCMGET:
+                TRACE_MSG1(TTY, "TIOCMGET: tiocm: %08x", tiocm);
+                tty_private->tiocm = ttyfd_tiocm(acm, tiocm);
+                RETURN_EINVAL_IF (copy_to_user((void *)arg, &tty_private->tiocm, sizeof(int)));
+                return 0;
+
+        case TIOCMBIS:
+        case TIOCMBIC:
+        case TIOCMSET:
+                TRACE_MSG0(TTY, "TIOCMXXX: copying tiocm arguement");
+
+                TRACE_MSG1(TTY, "access: %d", access_ok(VERIFY_READ, (void *)arg, sizeof(int)));
+                RETURN_EINVAL_UNLESS(access_ok(VERIFY_READ, (void *)arg, sizeof(int)));
+
+
+                //RETURN_EINVAL_IF (copy_from_user(&tiocm, (void *)arg, sizeof(int)));
+                rc = copy_from_user((void *)&tiocm, (void *)arg, sizeof(int));
+                TRACE_MSG2(TTY, "copy_from_user: %d tiocm: %08x", rc, tiocm);
+
+                TRACE_MSG2(TTY, "tiocm: %08x mask: %08x", tiocm,
+                                TIOCM_RTS | TIOCM_DTR | TIOCM_OUT1 | TIOCM_OUT2 | TIOCM_LOOP);
+
+                tiocm &= TIOCM_RTS | TIOCM_DTR | TIOCM_OUT1 | TIOCM_OUT2 | TIOCM_LOOP;
+                saved_tiocm = tty_private->tiocm;
+
+                switch (cmd) {
+                case TIOCMBIS:
+                        TRACE_MSG1(TTY, "TIOCMBIS: tiocm: %08x", tiocm);
+                        tty_private->tiocm |= tiocm;      /* turn on flags set in tiocm */
+                        break;
+                case TIOCMBIC:
+                        TRACE_MSG1(TTY, "TIOCMBIC: tiocm: %08x", tiocm);
+                        tty_private->tiocm &= ~tiocm;     /* turn off flags set in tiocm */
+                        break;
+                case TIOCMSET:
+                        TRACE_MSG1(TTY, "TIOCMSET: tiocm: %08x", tiocm);
+                        tty_private->tiocm = tiocm;       /* set all flags as in tiocm */
+                        break;
+                }
+                /* make changes
+                 */
+                changed_tiocm = saved_tiocm ^ tty_private->tiocm;
+                TRACE_MSG4(TTY, "TIOCMSET: tiocm: %08x saved: %08x set: %08x changed: %d",
+                                tiocm, saved_tiocm, tty_private->tiocm, changed_tiocm);
+
+                if (changed_tiocm) {
+                        /* DTR -> (DSR/DCD) */
+                        tiocm = tty_private->tiocm;
+                        if (changed_tiocm & TIOCM_DTR) {
+                                usb_ops->set_dsr(acm, tiocm & TIOCM_DTR);
+                                usb_ops->set_dcd(acm, tiocm & TIOCM_DTR);
+                        }
+                        /* OUT1 -> Ring */
+                        if (changed_tiocm & TIOCM_OUT1)
+                                usb_ops->ring(acm);
+                        /* OUT2 -> Overrun */
+                        if (changed_tiocm & TIOCM_OUT2)
+                                usb_ops->overrun(acm);
+                        /* LOOPBACK */
+                        if (changed_tiocm & TIOCM_OUT2)
+                                usb_ops->set_loopback(acm, tiocm & TIOCM_LOOP);
+                }
+
+                return 0;
+
+        case TIOCGSERIAL:
+                TRACE_MSG0(TTY, "TIOCGSERIAL");
+                RETURN_EINVAL_IF (copy_to_user((void *)arg, &tty_private->serial_struct, sizeof(struct serial_struct)));
+                return 0;
+
+        case TIOCSSERIAL:
+                TRACE_MSG0(TTY, "TIOCSSERIAL");
+                RETURN_EFAULT_IF (copy_from_user(&tty_private->serial_struct, (void *)arg, sizeof(struct serial_struct)));
+                return 0;
+
+        case TIOCSERCONFIG:
+        case TIOCSERGETLSR: /* Get line status register */
+        case TIOCSERGSTRUCT:
+                TRACE_MSG0(TTY, "TIOCSER*");
+                return -EINVAL;
+
+                /*
+                 * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
+                 * - mask passed in arg for lines of interest
+                 *   (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
+                 * Caller should use TIOCGICOUNT to see which one it was
+                 */
+        case TIOCMIWAIT:
+                TRACE_MSG0(TTY, "TIOCGMIWAIT");
+                while (1) {
+
+                        saved_tiocm = tty_private->tiocm;
+
+                        interruptible_sleep_on(&tty_private->tiocm_wait);
+
+                        /* see if a signal did it */
+                        if (signal_pending(current))
+                                return -ERESTARTSYS;
+
+                        tty_private->tiocm = ttyfd_tiocm(acm, tiocm);
+                        changed_tiocm = saved_tiocm ^ tty_private->tiocm;
+                        RETURN_ZERO_IF ( (changed_tiocm | TIOCM_CAR) | (changed_tiocm | TIOCM_DSR) );
+                        /* loop */
+                }
+                /* NOTREACHED */
+
+                /*
+                 * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
+                 * Return: write counters to the user passed counter struct
+                 * NB: both 1->0 and 0->1 transitions are counted except for
+                 *     RI where only 0->1 is counted.
+                 */
+        case TIOCGICOUNT:
+                TRACE_MSG0(TTY, "TIOCGICOUNT");
+                if (copy_to_user((void *)arg, &tty_private->tiocgicount, sizeof(int)))
+                        return -EFAULT;
+                return 0;
+
+        case TCSETS:
+                TRACE_MSG1(TTY, "TCSETS: tiocm: %08x", tiocm);
+                return -ENOIOCTLCMD;
+        case TCFLSH:
+                TRACE_MSG1(TTY, "TCFLSH: tiocm: %08x", tiocm);
+                return -ENOIOCTLCMD;
+
+        case TCGETS:
+                TRACE_MSG1(TTY, "TCGETS: tiocm: %08x", tiocm);
+                return -ENOIOCTLCMD;
+
+        default:
+                TRACE_MSG1(TTY, "unknown cmd: %08x", cmd);
+                return -ENOIOCTLCMD;
+        }
+        return -ENOIOCTLCMD;
+}
+
+/*! ttyfd_wakeup_state
+ *
+ * Called by acm_fd to wakeup processes blocked waiting for state change
+ *
+ * @param acm - pointer to acm private data structure
+ */
+void ttyfd_wakeup_state(struct acm_private *acm)
+{
+        struct tty_private *tty_private = (struct tty_private *) acm->privdata;
+        TRACE_MSG0(TTY, "entered");
+        wake_up_interruptible(&tty_private->tiocm_wait);
+}
+
+
+/*! tty_set_termios
+ *
+ * Used by TTY layer to set termios structure according to current status.
+ *
+ * @param tty - pointer to acm private data structure
+ * @param termios_old - termios structure
+ */
+STATIC void tty_set_termios(struct tty_struct *tty, struct termios *termios_old)
+{
+        struct acm_private *acm = tty->driver_data;
+        struct tty_private *tty_private = (struct tty_private *) acm->privdata;
+
+        unsigned int c_cflag = tty->termios->c_cflag;
+
+        /* see if CLOCAL has changed */
+        if ((termios_old->c_cflag ^ tty->termios->c_cflag ) & CLOCAL)
+                usb_ops->set_local(acm, tty->termios->c_cflag & CLOCAL);
+
+        /* save cflags
+         */
+        tty_private->c_cflag = c_cflag;
+
+        /* send break?
+         */
+        if ((termios_old->c_cflag & CBAUD) && !(c_cflag & CBAUD))
+                usb_ops->send_break(acm);
+}
+
+/* ********************************************************************************************* */
+/*! ttyfd_wakeup_writers
+ *
+ * Bottom half handler to wakeup pending writers.
+ *
+ * @param data - pointer to acm private data structure
+ */
+STATIC void ttyfd_wakeup_writers(void *data)
+{
+        struct acm_private *acm = (struct acm_private *) data;
+        struct tty_private *tty_private = (struct tty_private *) acm->privdata;
+        struct tty_struct *tty = tty_private->tty;
+        unsigned long flags;
+
+        TRACE_MSG2(TTY,"used: %d MOD_IN_USE: %d", atomic_read(&acm->used), MOD_IN_USE);
+
+        RETURN_UNLESS(usb_ops->ready(acm));
+        RETURN_UNLESS(atomic_read(&acm->used));
+        RETURN_UNLESS(tty);
+
+        /* start sending from overflow buffer if necessary
+         */
+        ttyfd_overflow_send(acm, DATA_INTF, 0);
+
+        if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) && tty->ldisc.write_wakeup)
+                (tty->ldisc.write_wakeup)(tty);
+
+        wake_up_interruptible(&tty->write_wait);
+}
+
+/*! ttyfd_schedule_wakeup_writers
+ *
+ * Called by acm-fd to schedule a wakeup writes bottom half handler.
+ *
+ * @param acm - pointer to acm private data structure
+ */
+void ttyfd_schedule_wakeup_writers(struct acm_private *acm)
+{
+        struct tty_private *tty_private = (struct tty_private *) acm->privdata;
+
+        TRACE_MSG2(TTY,"used: %d MOD_IN_USE: %d", atomic_read(&acm->used), MOD_IN_USE);
+        ttyfd_schedule(&tty_private->wqueue);
+}
+
+/* ********************************************************************************************* */
+
+/*! ttyfd_recv_space_available
+ *
+ * Used by acm-fd to determine receive space available. This will determine
+ * how many receive urbs can be queued as there are never more receive urbs
+ * pending than there is currently room for data to be received.
+ *
+ * @param acm - pointer to acm private data structure
+ * @param interface - data interface to send on
+ * @return count - number of bytes available
+ */
+int ttyfd_recv_space_available(struct acm_private *acm, int interface)
+{
+        struct tty_private *tty_private;
+        struct tty_struct *tty;
+        int rc;
+
+        TRACE_MSG0(TTY, "entered");
+        RETURN_ZERO_UNLESS(acm);
+
+        tty_private = (struct tty_private *) acm->privdata;
+
+        RETURN_ZERO_UNLESS(tty_private);
+
+        RETURN_ZERO_UNLESS(tty_private->tty);
+
+        switch (interface) {
+        case COMM_INTF:
+                return 0;
+        case DATA_INTF:
+                tty = tty_private->tty;
+                rc = TTY_FLIPBUF_SIZE - tty->flip.count;
+                TRACE_MSG1(TTY, "recv space available: %d", rc);
+                return rc;
+                return(TTY_FLIPBUF_SIZE - tty->flip.count);
+        }
+        return 0;
+}
+
+/*! ttyfd_recv_chars
+ *
+ * Called by acm-fd when data has been received. This will
+ * receive n bytes starting at cp.  This will never be
+ * more than the last call to ttyfd_recv_space_available().
+ * This will be called from interrupt context.
+ *
+ * @param acm - pointer to acm private data structure
+ * @param interface - data interface to send on
+ * @param n - number of bytes to send
+ * @param cp - pointer to data to send
+ * @return non-zero if error
+ */
+int ttyfd_recv_chars(struct acm_private *acm, int interface, u8 *cp, int n)
+{
+        struct tty_private *tty_private = (struct tty_private *) acm->privdata;
+        struct tty_struct *tty = tty_private->tty;
+        unsigned long flags;
+
+        /* acm_start_recv_urbs() will never queue more urbs than there is currently
+         * room in the upper layer buffer for. So we are guaranteed that any data actually
+         * received can be given to the upper layers without worrying if we will
+         * actually have room.
+         *
+         * XXX I think that if the tty layer does get behind and we reach a point where
+         * there are no outstanding receive urbs, then we will also have been throttled
+         * so we will have an oppourtunity to queue more receive urbs when we get unthrottled.
+         *
+         * XXX If the above assumption is not true that a timer will be needed to periodically
+         * check if we can restart.
+         *
+         * YYY What can happen is that the tty_flip_buffer_push() call can fail for
+         * a variety of reasons (locked out while a read call is in progress, ldisc
+         * buffer is full, and probably others).  We need to ensure that we keep
+         * trying to push the flip buffer until it does go, and there is room for
+         * more receive urbs.  acm_start_recv_urbs() will queue a task to push and
+         * try again if it can't queue any now.
+         *
+         * ZZZ Yet another failure mode is to call tty_flip_buffer_push() too many
+         * times while throttled.  This will result in the ldisc layer silently
+         * dropping data inside the n_tty_receive_buf() routine.  (Acutal numbers
+         * for the 2.4.20 kernel this was discovered on are 128 bytes left in the
+         * ldisc buffer when throttle is called, and upto 7 64 byte urbs outstanding.
+         * The urbs will fit into the flip buffer, but NOT the ldisc buffer.)  This
+         * means we must not call tty_flip_buffer_push() when throttled.  We will
+         * count on the tty_unthrottle() call to kick off the final push.
+         */
+
+        {
+                int i;
+                local_irq_save(flags);
+                TRACE_MSG2(TTY, "recv: %d buffer: %x", n, cp);
+
+                for (i = 0; i < n; i += 8) {
+                        TRACE_MSG8(TTY, "%02x %02x %02x %02x %02x %02x %02x %02x", 
+                                        cp[i + 0], cp[i + 1], cp[i + 2], cp[i + 3], 
+                                        cp[i + 4], cp[i + 5], cp[i + 6], cp[i + 7]
+                                  );
+                }
+
+                local_irq_restore(flags);
+        }
+        switch (interface) {
+        case COMM_INTF:
+                return 0;
+        case DATA_INTF:
+
+                RETURN_EINVAL_UNLESS(tty);
+
+#define WORD_COPY 1
+#if defined(WORD_COPY)
+                local_irq_save(flags);
+                memcpy(tty->flip.char_buf_ptr,cp,n);
+                memset(tty->flip.flag_buf_ptr,TTY_NORMAL,n);
+                tty->flip.count += n;
+                tty->flip.char_buf_ptr += n;
+                tty->flip.flag_buf_ptr += n;
+                acm->bytes_forwarded += n;
+                local_irq_restore(flags);
+#else
+                int i;
+                for (i = 0; i < n; i++) {
+                        /* tty_flip_char() has no lock out from any calls
+                         * to tty_flip_buffer_push() that may have been queued
+                         * by acm_unthrottle() or acm_start_recv_urbs(), so... 
+                         */
+                        local_irq_save(flags);
+                        tty_insert_flip_char(tty, *cp++, TTY_NORMAL);
+                        local_irq_restore(flags);
+                }
+                acm->bytes_forwarded += n;
+#endif
+
+                UNLESS (test_bit(TTY_THROTTLED, &tty->flags)) 
+                        tty_flip_buffer_push(tty);
+
+                //UNLESS (tty_private->flags & TTYFD_THROTTLED) 
+                //        tty_flip_buffer_push(tty);
+
+                return 0;
+        }
+        return 0;
+}
+
+/* ********************************************************************************************* */
+
+/*! ttyfd_enable
+ *
+ * Called by acm-fd when the function driver is enabled.
+ */
+void ttyfd_enable(struct usbd_function_instance *function)
+{
+        //function->privdata = &acm_private;
+        //acm_private.function = function;
+}
+
+/*! ttyfd_enable
+ *
+ * Called by acm-fd when the function driver is disabled.
+ */
+void ttyfd_disable(struct usbd_function_instance *function)
+{
+        //function->privdata = NULL;
+        //acm_private.function = NULL;
+}
+
+/* ************************************************************************** */
+
+#if defined(LINUX24)
+static int tty_refcount;
+#else
+//Maintained inside tty_driver in LINUX 2.6
+#endif
+static struct tty_struct *tty_table[ACM_TTY_MINORS];
+static struct termios *tty_termios[ACM_TTY_MINORS];
+static struct termios *tty_termios_locked[ACM_TTY_MINORS];
+
+/*! tty_driver
+ */
+static struct tty_driver tty_driver = {
+        .magic =                 TTY_DRIVER_MAGIC,
+        .type =                  TTY_DRIVER_TYPE_SERIAL,
+        .subtype =               SERIAL_TYPE_NORMAL,
+        .flags =                 TTY_DRIVER_REAL_RAW | TTY_DRIVER_NO_DEVFS,
+        .driver_name =           "acm-CDC",
+        .name =                  "usb/acm/%d",
+        .major =                 ACM_TTY_MAJOR,
+        .num =                   ACM_TTY_MINORS,
+        .minor_start =           0, 
+
+        .open =                  tty_open,
+        .close =                 tty_close,
+        .write =                 tty_write,
+        .write_room =            tty_write_room,
+        .ioctl =                 acm_tty_ioctl,
+        .throttle =              tty_throttle,
+        .unthrottle =            tty_unthrottle,
+        .chars_in_buffer =       tty_chars_in_buffer,
+        .set_termios =           tty_set_termios,
+
+#if defined(LINUX24)
+        .refcount =              &tty_refcount,
+#else
+        .refcount =              0,
+#endif
+        .table =                 tty_table,
+        .termios =               tty_termios,
+        .termios_locked =        tty_termios_locked,
+};      
+
+/* USB Module init/exit ************************************************************************ */
+
+struct tty_private ttyfd_private = {
+        .tty_driver = &tty_driver,
+        //.wqueue.routine = ttyfd_wakeup_writers,
+        //.wqueue.data = &acmfd_private,
+        .wqueue = {
+                .routine = ttyfd_wakeup_writers,
+                .data = &acmfd_private,
+        },
+        //.hqueue.routine = ttyfd_call_hangup,
+        //.hqueue.data = &acmfd_private,
+        .hqueue = {
+                .routine = ttyfd_call_hangup,
+                .data = &acmfd_private,
+        },
+};
+
+
+/*! tty_function_services
+ * 
+ * This structure contains the list of services 
+ */
+struct acm_function_services tty_function_services = {
+        .schedule_wakeup_writers = ttyfd_schedule_wakeup_writers,
+        .recv_space_available = ttyfd_recv_space_available,
+        .recv_chars = ttyfd_recv_chars,
+        .enable = ttyfd_enable,
+        .disable = ttyfd_disable,
+        .schedule_hangup = ttyfd_schedule_hangup,
+        .wakeup_opens = ttyfd_wakeup_opens,
+        .wakeup_state = ttyfd_wakeup_state,
+};
+
+struct acm_private acmfd_private = {
+        .function_driver = &tty_function_driver,
+        .function_services = &tty_function_services,
+        .privdata = &ttyfd_private,
+        // TBR: 20040705 use ...le32() not le16, spotted by Zhao Liang
+        //.line_coding.dwDTERate = __constant_cpu_to_le32(0x1c200), // 115200
+        //.line_coding.bDataBits = 0x08,
+        .line_coding = {
+                .dwDTERate = __constant_cpu_to_le32(0x1c200), // 115200
+                .bDataBits = 0x08,
+        },
+};
+
+
+/*! ttyfd_modinit - module init
+ *
+ * This is called immediately after the module is loaded or during
+ * the kernel driver initialization if linked into the kernel.
+ *
+ */
+STATIC int ttyfd_modinit (void)
+{
+        int i;
+
+        /* initialize private structures */
+        acmfd_private.trace_tag = TTY = otg_trace_obtain_tag();
+        init_waitqueue_head(&ttyfd_private.open_wait);
+        init_waitqueue_head(&ttyfd_private.tiocm_wait);
+
+        /* update init_termios and register as tty driver */
+        tty_driver.init_termios = tty_std_termios;
+        tty_driver.init_termios.c_cflag = B115200 | CS8 | CREAD | HUPCL | CLOCAL;
+        tty_driver.init_termios.c_lflag &= ~(ECHO | ICANON);
+        THROW_IF(tty_register_driver(&tty_driver), error);
+        tty_register_devfs(&tty_driver, 0, ACM_TTY_MINOR);
+        ttyfd_private.tty_driver_registered++;
+
+        /* register as usb function driver via acm-fd */
+        THROW_IF (usb_ops->fd_init(&acmfd_private, "acm_fd", vendor_id, product_id, max_queued_urbs,max_queued_bytes ), error);
+
+        CATCH(error) {
+                printk(KERN_ERR"%s: ERROR\n", __FUNCTION__);
+                if (ttyfd_private.tty_driver_registered) {
+                        tty_unregister_driver(&tty_driver);
+                        ttyfd_private.tty_driver_registered = 0;
+                }
+                return -EINVAL;
+        }
+        return 0;
+}
+
+
+/*! ttyfd_modexit - module cleanup
+ *
+ * This is called prior to the module being unloaded.
+ */
+STATIC void ttyfd_modexit (void)
+{
+        struct acm_private *acm = &acmfd_private;
+        struct tty_private *tty_private = (struct tty_private *) acm->privdata;
+        unsigned long flags;
+        struct usbd_urb *urb;
+
+        /* Wake up any pending opens after setting the exiting flag. */
+        local_irq_save(flags);
+        ttyfd_private.exiting = 1;
+        //if (ttyfd_private.open_wait_count > 0) 
+        wake_up_interruptible(&ttyfd_private.open_wait);
+        local_irq_restore(flags);
+
+        /* verify no tasks are running */
+        usb_ops->wait_task(acm, &acm->recv_tqueue);
+        usb_ops->wait_task(acm, &ttyfd_private.wqueue);
+        usb_ops->wait_task(acm, &ttyfd_private.hqueue);
+
+#if defined(LINUX24)
+        run_task_queue(&tq_timer);
+#else 
+        blk_run_queues();
+#endif
+        /* de-register as tty  and usb drivers */
+        if (ttyfd_private.tty_driver_registered) 
+                tty_unregister_driver(&tty_driver);
+
+        /* de-register as function driver via acm-fd */
+        usb_ops->fd_exit(&acmfd_private);
+        otg_trace_invalidate_tag(TTY);
+}
+
+module_init (ttyfd_modinit);
+module_exit (ttyfd_modexit);
+
diff -uNr linux/drivers/no-otg/functions/acm/tty-os.h linux/drivers/otg/functions/acm/tty-os.h
--- linux/drivers/no-otg/functions/acm/tty-os.h	1970-01-01 01:00:00.000000000 +0100
+++ linux/drivers/otg/functions/acm/tty-os.h	2006-09-01 21:41:26.000000000 +0200
@@ -0,0 +1,74 @@
+/*
+ * otg/functions/acm/tty-os.h
+ *
+ *      Copyright (c) 2003, 2004 Belcarra
+ *
+ * By: 
+ *      Tom Rushworth <tbr@belcarra.com>, 
+ *      Stuart Lynne <sl@belcarra.com>, 
+ *      Bruce Balden <balden@belcarra.com>
+ *
+ *
+ */
+/*!
+ * @file otg/functions/acm/tty-os.h
+ * @brief ACM Function Driver private defines
+ *
+ * An ACM (Abstract Control Model) driver is composed of two pieces:
+ *    1) An OS specific piece that handles creating and operating
+ *       a serial device for the given OS.
+ *    2) A USB specific piece that interfaces either with the host
+ *       usbcore layer, or with the otgcore layer.
+ *
+ * If the USB piece interfaces with the host usbcore layer you get
+ * an ACM class driver.  If the USB piece interfaces with the otgcore
+ * layer you get an ACM function driver.
+ *
+ * This file describes the functions exported by the various acm-*-os.c
+ * files (implementing (1)) for use in acm-fd.c (2).
+ *
+ * @ingroup TTYFunction
+ */
+
+#ifndef ACM_OS_H
+#define ACM_OS_H 1
+
+/*
+ * acm_os_recv_space_available - return the number of bytes of data
+ *                               the OS specific piece can accept without
+ *                               dropping some.
+ */
+extern int acm_os_recv_space_available(struct acm_private *acm);
+
+/*
+ * acm_os_recv_chars - receive n bytes starting at cp.  This will never be
+ *                     more than the last call to acm_os_recv_space_available().
+ *                     This will be called from interrupt context.
+ */
+extern int acm_os_recv_chars(struct acm_private *acm, u8 *cp, int n);
+
+/*
+ * acm_os_wakeup_writers - wakeup any blocked writers.  This is called
+ *                         from interrupt context, so may need to queue
+ *                         the actual wakeup in a "bottom half".
+ */
+extern void acm_os_wakeup_writers(struct acm_private *acm);
+
+/*
+ * acm_os_hangup - send a hangup to any readers or writers.  This can be
+ *                 called from interrupt context, so may need to queue
+ *                 the actual hangup in a "bottom half".
+ */
+extern void acm_os_hangup(struct acm_private *acm);
+
+/*
+ * acm_os_wakeup_opens - wakeup any blocked opens.  This is called
+ *                       from interrupt context, so may need to queue
+ *                       the actual wakeup in a "bottom half".
+ */
+extern void acm_os_wakeup_opens(struct acm_private *acm);
+
+extern void acm_os_enable(struct usbd_function_instance *function);
+extern void acm_os_disable(struct usbd_function_instance *function);
+
+#endif
diff -uNr linux/drivers/no-otg/functions/acm/tty.h linux/drivers/otg/functions/acm/tty.h
--- linux/drivers/no-otg/functions/acm/tty.h	1970-01-01 01:00:00.000000000 +0100
+++ linux/drivers/otg/functions/acm/tty.h	2006-09-01 21:41:26.000000000 +0200
@@ -0,0 +1,117 @@
+/*
+ * otg/functions/acm/tty.h
+ *
+ *      Copyright (c) 2003, 2004 Belcarra
+ *
+ * By: 
+ *      Stuart Lynne <sl@belcarra.com>, 
+ *      Tom Rushworth <tbr@belcarra.com>, 
+ *      Bruce Balden <balden@belcarra.com>
+ *
+ */
+/*!
+ * @defgroup TTYFunction ACM-TTY 
+ * @ingroup functiongroup
+ */
+/*!
+ * @file otg/functions/acm/tty.h
+ * @brief ACM Function Driver private defines
+ *
+ *
+ * This is an ACM Function Driver. The upper edge is exposed
+ * to the hosting OS as a Posix type character device. The lower
+ * edge implements the USB Device Stack API.
+ *
+ * These are emulated and set by the tty driver as appropriate
+ * to model a virutal serial port. The
+ *
+ *      TIOCM_RNG       RNG (Ring)                              not used
+ *      TIOCM_LE        DSR (Data Set Ready / Line Enable)      
+ *      TIOCM_DSR       DSR (Data Set Ready)
+ *      TIOCM_CAR       DCD (Data Carrier Detect)
+ *      TIOCM_CTS       CTS (Clear to Send)
+ *      TIOCM_CD        TIOCM_CAR
+ *      TIOCM_RI        TIOCM_RNG
+ *
+ * These are set by the application:
+ *
+ *      TIOCM_DTR       DTR (Data Terminal Ready)
+ *      TIOCM_RTS       RTS (Request to Send)
+ *
+ *      TIOCM_LOOP      Set into loopback mode
+ *      TIOCM_OUT1      Not used.
+ *      TIOCM_OUT2      Not used.
+ *
+ *
+ * The following File and  termio c_cflags are used:
+ *
+ *      O_NONBLOCK      don't block in tty_open()
+ *      O_EXCL          don't allow more than one open
+ *
+ *      CLOCAL          ignore DTR status
+ *      CBAUD           send break if set to B0
+ *
+ * Virtual NULL Modem
+ *
+ * Peripheral to Host via Serial State Notification (write ioctls)
+ *
+ * Application             TIOCM           Null Modem      ACM (DCE)       Notificaiton    Host (DTE)
+ * -------------------------------------------------------------------------------------------------------------
+ * Request to send         TIOCM_RTS       RTS ->  CTS     CTS             Not Available   Clear to Send (N/A)
+ * Data Terminal Ready     TIOCM_DTR       DTR ->  DSR     DSR             bTxCarrier      Data Set Ready
+ *                                         DTR ->  DCD     DCD             bRXCarrier      Carrier Detect
+ * Ring Indicator          TIOCM_OUT1      OUT1 -> RI      RI              bRingSignal     Ring Indicator
+ * Overrun                 TIOCM_OUT2      OUT2 -> Overrun Overrun         bOverrun        Overrun
+ * Send Break              B0              B0   -> Break   Break           bBreak          Break Received
+ * -------------------------------------------------------------------------------------------------------------
+ *
+ *
+ * Host to Peripheral via Set Control Line State
+ *
+ * Host (DTE)              Line State      ACM (DCE)       Null Modem      TIOCM           Peripheral (DTE)
+ * -------------------------------------------------------------------------------------------------------------
+ * Data Terminal Ready     D0              DTR             DTR ->  DSR     TIOCM_DSR       Data Set Ready
+ *                                                         DTR ->  DCD     TIOCM_CAR       Carrier Detect
+ * Request To Send         D1              RTS             RTS ->  CTS     TIOCM_CTS       Clear to Send
+ * -------------------------------------------------------------------------------------------------------------
+ *
+ *      
+ * @ingroup TTYFunction
+ */
+
+extern struct usbd_function_driver tty_function_driver;
+
+//#define TTY_OPENED      (1 << 0)                /*! OPENED flag */
+#define TTYFD_CLOCAL      (1 << 1)                /*! CLOCAL flag */
+//#define TTYFD_LOOPBACK    (1 << 2)               /*! LOOPBACK flag */
+#define TTYFD_EXCLUSIVE   (1 << 3)                /*! EXCLUSIVE flag */
+#define TTYFD_THROTTLED   (1 << 4)                /*! THROTTLED flag */
+
+/*! @struct tty_private
+ */
+struct tty_private {
+
+        struct tty_driver *tty_driver;          /*!< tty structure */
+        int tty_driver_registered;              /*!< non-zero if tty_driver registered */
+        int usb_driver_registered;              /*!< non-zero if usb function registered */
+
+        struct tty_struct *tty;                 /*!< non-null if tty open */
+        struct tq_struct wqueue;                /*!< task queue for writer wakeup  */
+        struct tq_struct hqueue;                /*!< task queue for hangup */
+
+        u32 flags;                              /*!< flags */
+
+        u32 tiocm;                              /*!< tiocm settings */
+
+        struct serial_struct serial_struct;     /*!< serial structure used for TIOCSSERIAL and TIOCGSERIAL */
+
+        wait_queue_head_t tiocm_wait;
+        u32 tiocgicount;
+
+        u32 c_cflag;
+
+        wait_queue_head_t open_wait;            /*! wait queue for blocking open*/
+        int exiting;                            /*! True if module exiting */
+};
+
+
diff -uNr linux/drivers/no-otg/functions/isotest/Config.in linux/drivers/otg/functions/isotest/Config.in
--- linux/drivers/no-otg/functions/isotest/Config.in	1970-01-01 01:00:00.000000000 +0100
+++ linux/drivers/otg/functions/isotest/Config.in	2006-09-01 21:41:26.000000000 +0200
@@ -0,0 +1,28 @@
+#
+# Loop Function
+#
+# Copyright (C) 2003-2004 Belcarra
+# Enhanced Jan 2004 to provide greater runtime selection
+# of xmit buffer patterns  from the device to the host
+#
+
+mainmenu_option next_comment
+comment "ISO Test Function"
+
+dep_tristate '  Loop Function Driver' CONFIG_OTG_ISOTEST $CONFIG_OTG
+
+if [ "$CONFIG_OTG_ISOTEST" = "y" -o "$CONFIG_OTG_ISOTEST" = "m" ]; then
+
+   hex     ' idVendor (hex value)' CONFIG_OTG_ISOTEST_VENDORID "15ec"
+   hex     ' idProduct (hex value)' CONFIG_OTG_ISOTEST_PRODUCTID "f004"
+   hex     ' bcdDevice (binary-coded decimal)' CONFIG_OTG_ISOTEST_BCDDEVICE "0100"
+   string ' iManufacturer (string)' CONFIG_OTG_ISOTEST_MANUFACTURER ""
+   string ' iProduct (string)' CONFIG_OTG_ISOTEST_PRODUCT_NAME ""
+
+   string ' iConfiguration (string)' CONFIG_OTG_ISOTEST_DESC "ISO Test Cfg"
+   string ' Data Interface iInterface (string)' CONFIG_OTG_ISOTEST_DATA_INTF "ISO Data Intf"
+   #bool	  ' Runtime pattern buffer selection ' CONFIG_OTG_ISOTEST_XMIT_PATTERN "y"
+
+fi
+
+endmenu
diff -uNr linux/drivers/no-otg/functions/isotest/Makefile linux/drivers/otg/functions/isotest/Makefile
--- linux/drivers/no-otg/functions/isotest/Makefile	1970-01-01 01:00:00.000000000 +0100
+++ linux/drivers/otg/functions/isotest/Makefile	2006-09-01 21:41:26.000000000 +0200
@@ -0,0 +1,73 @@
+#
+# Function driver ISO Test Device
+#
+# Copyright (c) 2003 Belcarra
+
+# Multipart objects.
+
+O_TARGET	:= isotest_fd_drv.o 
+list-multi	:= isotest_fd.o isotest.o
+
+isotest_fd-objs	:= iso.o test.o fermat.o
+isotest-objs	:= host.o test.o
+
+# Objects that export symbols.
+export-objs	:= iso.o
+
+# Object file lists.
+
+obj-y	:=
+obj-m	:=
+obj-n	:=
+obj-	:=
+
+# Each configuration option enables a list of files.
+
+obj-$(CONFIG_OTG_ISOTEST)   += isotest_fd.o isotest.o
+
+# Extract lists of the multi-part drivers.
+# The 'int-*' lists are the intermediate files used to build the multi's.
+
+multi-y		:= $(filter $(list-multi), $(obj-y))
+multi-m		:= $(filter $(list-multi), $(obj-m))
+int-y		:= $(sort $(foreach m, $(multi-y), $($(basename $(m))-objs)))
+int-m		:= $(sort $(foreach m, $(multi-m), $($(basename $(m))-objs)))
+
+# Files that are both resident and modular: remove from modular.
+
+obj-m		:= $(filter-out $(obj-y), $(obj-m))
+int-m		:= $(filter-out $(int-y), $(int-m))
+
+# Translate to Rules.make lists.
+
+O_OBJS		:= $(filter-out $(export-objs), $(obj-y))
+OX_OBJS		:= $(filter     $(export-objs), $(obj-y))
+M_OBJS		:= $(sort $(filter-out $(export-objs), $(obj-m)))
+MX_OBJS		:= $(sort $(filter     $(export-objs), $(obj-m)))
+MI_OBJS		:= $(sort $(filter-out $(export-objs), $(int-m)))
+MIX_OBJS	:= $(sort $(filter     $(export-objs), $(int-m)))
+
+# The global Rules.make.
+
+OTG=$(TOPDIR)/drivers/otg
+ISOD=$(OTG)/functions/isotest
+USBDCORE_DIR=$(OTG)/usbdcore
+include $(TOPDIR)/Rules.make
+EXTRA_CFLAGS += -I$(ISOD) -I$(OTG) -Wno-unused -Wno-format  
+EXTRA_CFLAGS_nostdinc += -I$(ISOD) -I$(OTG) -Wno-unused -Wno-format  
+
+# Link rules for multi-part drivers.
+
+isotest_fd.o: $(isotest_fd-objs)
+	$(LD) -r -o $@ $(isotest_fd-objs)
+
+isotest.o: $(isotest-objs)
+	$(LD) -r -o $@ $(isotest-objs)
+
+# dependencies:
+
+isotest.o: $(USBDCORE_DIR)/usbd.h $(USBDCORE_DIR)/usbd-bus.h $(USBDCORE_DIR)/usbd-func.h test.h
+host.o: host.c test.h
+
+test.o:test.h
+
diff -uNr linux/drivers/no-otg/functions/isotest/fermat.c linux/drivers/otg/functions/isotest/fermat.c
--- linux/drivers/no-otg/functions/isotest/fermat.c	1970-01-01 01:00:00.000000000 +0100
+++ linux/drivers/otg/functions/isotest/fermat.c	2006-09-01 21:41:26.000000000 +0200
@@ -0,0 +1,132 @@
+/*
+ * otg/network_fd/fermat.c - Network Function Driver
+ *
+ *      Copyright (c) 2003, 2004 Belcarra
+ *
+ * By: 
+ *      Bruce Balden <balden@belcarra.com>
+ *
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ */
+
+#include <linux/config.h>
+#include <linux/module.h>
+#include <linux/kernel.h>
+#include <linux/version.h>
+
+#ifdef CONFIG_OTG_ISOTEST_MODULE
+
+#include "fermat.h"
+
+#ifndef ISO_FERMAT_DEFINED
+typedef unsigned char BYTE;
+typedef struct fermat {
+        int length;
+        BYTE power[256];
+} FERMAT;
+#endif
+
+
+static int fermat_setup(FERMAT *p, int seed){
+        int i = 0;
+        unsigned long x,y;
+        y = 1;
+        do{
+                x = y;
+                p->power[i] = ( x == 256 ? 0 : x);
+                y = ( seed * x ) % 257;
+                i += 1;
+        }while( y != 1);
+        p->length = i;
+        return i;
+}
+
+static void fermat_xform(FERMAT *p, BYTE *data, int length){
+        BYTE *pw = p->power;
+        int   i, j;
+        BYTE * q ;
+        for(i = 0, j=0, q = data; i < length; i++, j++, q++){
+                if(j>=p->length){
+                        j = 0;
+                }
+                *q ^= pw[j];
+        }
+}
+
+static FERMAT default_fermat;
+static const int primitive_root = 5;
+void fermat_init(){
+        (void) fermat_setup(&default_fermat, primitive_root); 
+}
+
+// Here are the public official versions.
+// Change the primitive_root above to another primitive root
+// if you need better scatter. Possible values are 3 and 7
+
+
+void fermat_encode(BYTE *data, int length){
+        fermat_xform(&default_fermat, data, length);
+}
+
+void fermat_decode(BYTE *data, int length){
+        fermat_xform(&default_fermat, data, length);
+}
+
+
+// Note: the seed must be a "primitive root" of 257. This means that
+// the return value of the setup routine must be 256 (otherwise the
+// seed is not a primitive root.  The routine will still work fine
+// but will be less pseudo-random.
+
+#undef TEST 
+#if TEST
+#include <stdio.h>
+#include <memory.h>
+
+// Use FERMAT in two ways: to encode, and to generate test data.
+
+main(){
+        //Note 3, 5, and 7 are primitive roots of 257
+        // 11 is not a primitive root
+        FERMAT three, five, seven;
+
+        FERMAT three2;
+        printf("Cycle lengths: 3,5,7 %d %d %d \n", 
+                        fermat_setup(&three, 3), 
+                        fermat_setup(&five, 5), 
+                        fermat_setup(&seven, 7));
+        three2=three; // Copy data from three
+        fermat_xform(&three,three2.power,three2.length);
+        fermat_xform(&five,three2.power,three2.length);
+        fermat_xform(&seven,three2.power,three2.length);
+        fermat_xform(&seven,three2.power,three2.length);
+        fermat_xform(&five,three2.power,three2.length);
+        fermat_xform(&three,three2.power,three2.length);
+
+        //At this stage, three2 and three should be identical
+        if(memcpy(&three,&three2,sizeof(FERMAT))){
+                printf("Decoded intact\n");
+        }
+
+        fermat_init();
+        fermat_encode(three2.power,256);
+
+}
+#endif
+
+#endif /* CONFIG_OTG_ISOTEST */
+
diff -uNr linux/drivers/no-otg/functions/isotest/fermat.h linux/drivers/otg/functions/isotest/fermat.h
--- linux/drivers/no-otg/functions/isotest/fermat.h	1970-01-01 01:00:00.000000000 +0100
+++ linux/drivers/otg/functions/isotest/fermat.h	2006-09-01 21:41:26.000000000 +0200
@@ -0,0 +1,38 @@
+/*
+ * otg/network_fd/fermat.h - Network Function Driver
+ *
+ *      Copyright (c) 2003, 2004 Belcarra
+ *
+ * By: 
+ *      Bruce Balden <balden@belcarra.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ *
+ */
+
+#ifndef ISO_FERMAT_DEFINED
+#define ISO_FERMAT_DEFINED 1
+typedef unsigned char BYTE;
+typedef struct fermat {
+        int length;
+        BYTE power[256];
+} FERMAT;
+
+void fermat_init(void);
+void fermat_encode(BYTE *data, int length);
+void fermat_decode(BYTE *data, int length);
+#endif
+
diff -uNr linux/drivers/no-otg/functions/isotest/host.c linux/drivers/otg/functions/isotest/host.c
--- linux/drivers/no-otg/functions/isotest/host.c	1970-01-01 01:00:00.000000000 +0100
+++ linux/drivers/otg/functions/isotest/host.c	2006-09-01 21:41:27.000000000 +0200
@@ -0,0 +1,840 @@
+/*
+ * otg/isotest_fd/host.c
+ *
+ *      Copyright (c) 2003, 2004 Belcarra
+ *
+ * USB ISO Test
+ *
+ * 
+ *      Copyright (c) 2003, 2004 sl@belcarra.com
+ *
+ */
+
+//#include <linux/config.h>
+#include <linux/module.h>
+#include <linux/version.h>
+
+#include <linux/kernel.h>
+//#include <linux/sched.h>
+#include <linux/signal.h>
+#include <linux/errno.h>
+//#include <linux/poll.h>
+#include <linux/init.h>
+#include <linux/slab.h>
+#include <linux/fcntl.h>
+#include <linux/module.h>
+#include <linux/spinlock.h>
+#include <linux/list.h>
+#include <linux/smp_lock.h>
+#include <linux/usb.h>
+#include <linux/interrupt.h>
+#include <linux/pci.h>
+#include <linux/delay.h>
+#include <linux/proc_fs.h>
+
+#include <linux/vmalloc.h>
+
+#include <asm/atomic.h>
+#include <asm/io.h>
+
+
+#if defined(CONFIG_ARCH_SA1100) || defined (CONFIG_ARCH_PXA)
+#include <asm/dma.h>
+#include <asm/mach/dma.h>
+#include <asm/irq.h>
+#include <asm/system.h>
+#include <asm/hardware.h>
+#include <asm/types.h>
+#endif
+
+#if defined(CONFIG_MIPS_AU1000) || defined(CONFIG_MIPS_PB1500) || defined(CONFIG_MIPS_PB1100)
+#include <asm/au1000.h>
+#include <asm/au1000_dma.h>
+#include <asm/mipsregs.h>
+#endif
+
+#if defined(CONFIG_ARCH_SAMSUNG)
+#include <asm/arch/timers.h>
+#include <asm/arch/hardware.h>
+#endif
+
+#include "test.h"
+
+
+/* Use our own dbg macro */
+#undef dbg
+#define dbg(format, arg...) do { if (debug) printk(KERN_DEBUG __FILE__ ": " format "\n" , ## arg); } while (0)
+
+#define MIN(a,b) (((a) < (b))?(a):(b))
+#define MAX(a,b) (((a) > (b))?(a):(b))
+
+#define THROW(x) goto x
+#define CATCH(x) while(0) x:
+#define THROW_IF(e, x) if (e) { goto x; }
+#define BREAK_IF(x) if (x) { break; }
+#define CONTINUE_IF(x) if (x) { continue; }
+#define RETURN_IF(y) if (y) { return; }
+#define RETURN_ZERO_IF(y) if (y) { return 0; }
+#define RETURN_NULL_IF(y) if (y) { return NULL; }
+
+
+/* Version Information */
+#define DRIVER_VERSION "v0.9"
+#define DRIVER_AUTHOR "sl@belcarra.com"
+#define DRIVER_DESC "USB ISO Test"
+
+/* Define these values to match your device */
+
+#ifdef CONFIG_OTG_ISOTEST_VENDORID
+        #undef USB_ISOTEST_VENDOR_ID
+        #define USB_ISOTEST_VENDOR_ID   CONFIG_OTG_ISOTEST_VENDORID
+#else
+        #define USB_ISOTEST_VENDOR_ID   0xfff0
+#endif
+
+#ifdef CONFIG_OTG_ISOTEST_PRODUCTID
+        #undef USB_ISOTEST_PRODUCT_ID
+        #define USB_ISOTEST_PRODUCT_ID  CONFIG_OTG_ISOTEST_PRODUCTID
+#else
+        #define USB_ISOTEST_PRODUCT_ID  0xfff1
+#endif
+
+/* Module paramaters */
+//MODULE_PARM(debug, "i");
+//MODULE_PARM_DESC(debug, "Debug enabled or not");
+
+static int send;
+MODULE_PARM(send, "i");
+MODULE_PARM_DESC(send, "send test");
+
+static int recv;
+MODULE_PARM(recv, "i");
+MODULE_PARM_DESC(recv, "recv test");
+
+static u32    vendor_id;         // no default
+static u32    product_id;        // no default
+
+
+MODULE_PARM_DESC(vendor_id, "User specified USB idVendor");
+MODULE_PARM_DESC(product_id, "User specified USB idProduct");
+MODULE_PARM(vendor_id, "i");
+MODULE_PARM(product_id, "i");
+
+
+/* table of devices that work with this driver */
+static struct usb_device_id isotest_table [] = {
+        { USB_DEVICE(USB_ISOTEST_VENDOR_ID, USB_ISOTEST_PRODUCT_ID) },
+        { },                            /* extra entry */
+        { },                            /* Terminating entry */
+};
+
+MODULE_DEVICE_TABLE (usb, isotest_table);
+
+/* ********************************************************************************************* */
+
+
+/* ********************************************************************************************* */
+/*
+struct iso_test_data {
+
+        // sender info
+        u32     sender_id;
+        time_t  send_time;
+        u32     send_crc;
+
+        // loop info
+        u32     recv_id;
+        time_t  recv_time;
+        u32     recv_crc;
+
+        // payload
+        u32     size;
+        u8      data[0];
+};
+*/
+
+#define IN_URBS 10
+#define OUT_URBS 10
+
+
+
+
+/* Structure to hold all of our device specific stuff */
+struct usb_isotest {
+        struct usb_device *     udev;           /* save off the usb device pointer */
+        struct usb_interface *  interface;      /* the interface for this device */
+
+        u8                    closing;
+        u8                    num_interrupt_in;       /* number of interrupt in endpoints we have */
+        u8                    num_iso_in;             /* number of iso in endpoints we have */
+        u8                    num_iso_out;            /* number of iso out endpoints we have */
+
+        int                     iso_in_size;            /* the size of the receive buffer */
+        struct urb *            iso_in_urbs[IN_URBS];   /* the urb used to send data */
+        u8                    iso_in_endpointAddr;    /* the address of the iso in endpoint */
+        int                     in_urbs;
+
+        int                     iso_out_size;           /* the size of the send buffer */
+        struct urb *            iso_out_urbs[OUT_URBS]; /* the urb used to send data */
+        u8                    iso_out_endpointAddr;   /* the address of the iso out endpoint */
+        int                     out_urbs;
+
+        struct semaphore        sem;                    /* locks this structure */
+        struct tq_struct        iso_bh;
+        wait_queue_head_t       iso_wq;
+
+        struct isotest_stats    stats;
+
+        int                     first;
+};
+
+
+/* local function prototypes */
+
+static void * isotest_probe     (struct usb_device *, unsigned int , const struct usb_device_id *);
+static void isotest_disconnect  (struct usb_device *, void *);
+
+
+void isotest_schedule_bh(struct usb_isotest *isotest);
+
+
+/* ISO OUT - Transmit ************************************************************************** */
+
+
+#if 1
+static void isotest_iso_out_free_urb(struct urb *urb)
+{
+        struct usb_isotest *isotest;
+        int i;
+        unsigned long   flags;
+
+        //printk(KERN_INFO"%s: urb: %p\n", __FUNCTION__, urb);
+
+        RETURN_IF(!urb);
+
+        if (urb->transfer_buffer) {
+                kfree(urb->transfer_buffer);
+        }
+
+        isotest = (struct usb_isotest *)urb->context;
+        usb_free_urb(urb);
+
+        RETURN_IF(!isotest);
+
+        local_irq_save (flags);
+        for (i = 0; i < OUT_URBS; i++) {
+                CONTINUE_IF(isotest->iso_out_urbs[i] != urb);
+                //printk(KERN_INFO"%s: zeroing %d urb: %p\n", __FUNCTION__, i, urb);
+                isotest->iso_out_urbs[i] = NULL;
+                break;
+        }
+        local_irq_restore (flags);
+}
+#endif
+
+static int iso_transfer_count;
+
+int iso_out_submit(struct usb_isotest *isotest, struct urb *urb) 
+{
+        int                     i;
+        int                     j;
+        int                     rc = 0;
+
+        //printk(KERN_INFO"%s: transfer: %d size: %d frames: %x\n", __FUNCTION__, 
+        //                iso_transfer_count, urb->transfer_buffer_length, urb->number_of_packets);
+
+        RETURN_ZERO_IF(isotest->closing);
+
+        urb->dev = isotest->udev;
+
+        iso_transfer_count++;
+
+        for (j = urb->transfer_buffer_length, i = 0; i < urb->number_of_packets; i++) {
+
+                int                     send = MIN(isotest->iso_out_size, j);
+
+                u8                      *cp = urb->transfer_buffer + (isotest->iso_out_size * i);
+
+                j -= send;
+
+                urb->iso_frame_desc[i].offset = i * isotest->iso_out_size;
+                urb->iso_frame_desc[i].length = send;
+
+                // iso_transfer_count
+                *cp++ = cpu_to_le16(iso_transfer_count) & 0xff;
+                *cp++ = (cpu_to_le16(iso_transfer_count) >> 8) & 0xff;
+                *cp++ = (cpu_to_le16(iso_transfer_count) >> 16) & 0xff;
+                *cp++ = (cpu_to_le16(iso_transfer_count) >> 24) & 0xff;
+
+                // iso transfer length
+                *cp++ = cpu_to_le16(urb->transfer_buffer_length) & 0xff;
+                *cp++ = (cpu_to_le16(urb->transfer_buffer_length) >> 8) & 0xff;
+
+                // iso frame size
+                *cp++ = cpu_to_le16(isotest->iso_out_size) & 0xff;
+                *cp++ = (cpu_to_le16(isotest->iso_out_size) >> 8) & 0xff;
+
+                // total frames
+                *cp++ = cpu_to_le16(urb->number_of_packets) & 0xff;
+                *cp++ = (cpu_to_le16(urb->number_of_packets) >> 8) & 0xff;
+
+                // this packet number
+                *cp++ = cpu_to_le16(i+1) & 0xff;
+                *cp++ = (cpu_to_le16(i+1) >> 8) & 0xff;
+
+        }
+
+        //printk(KERN_INFO"%s: submitting\n", __FUNCTION__);
+
+        RETURN_ZERO_IF(!(rc = usb_submit_urb(urb)));
+        printk(KERN_INFO"%s: FAILED rc: %x\n", __FUNCTION__, rc);
+
+        return rc;
+}
+
+void isotest_iso_out_complete (struct urb *urb)
+{
+        struct usb_isotest *isotest = (struct usb_isotest *)urb->context;
+        int rc;
+
+        //printk(KERN_INFO"%s: urb: %p\n", __FUNCTION__, urb);
+
+        RETURN_IF(!urb);
+
+        if (urb->status /* && (urb->status != -ENOENT) && (urb->status != -ECONNRESET)*/) {
+                //printk(KERN_INFO"%s: - nonzero write iso status received: %d\n", __FUNCTION__, urb->status);
+        }
+
+        iso_out_submit(isotest, urb);
+}
+
+
+#define ISO_SEND_TOTAL  1000
+#define ISO_SEND_TRANSFERS 1200
+
+static int out_count;
+
+struct urb *iso_out_start(struct usb_isotest *isotest)
+{
+        struct urb *            urb = NULL;
+        int                     rc = 0;
+        int                     i;
+        int                     j;
+
+
+        int                     size = ((ISO_SEND_TOTAL % isotest->iso_out_size) < 20) ? ISO_SEND_TOTAL + 20 : ISO_SEND_TOTAL;
+        int                     frames = (size / isotest->iso_out_size) + 1;
+
+        //RETURN_NULL_IF(out_count-- <= 0);
+
+        //printk(KERN_INFO"%s: %d %02x\n", __FUNCTION__, out_count, isotest->iso_out_endpointAddr);
+
+
+        //printk(KERN_INFO"%s: frames: %x packet: %d size: %d\n", __FUNCTION__, frames, isotest->iso_out_size, size);
+
+        // allocate urb and buffer, fill buffer with some data
+        THROW_IF(!(urb = usb_alloc_urb(frames + 1)), error);
+
+        THROW_IF (!(urb->transfer_buffer = kmalloc(size, GFP_ATOMIC)), error);
+
+        for (i = 0; i < size; i++) {
+                unsigned char *cp = urb->transfer_buffer + i;
+                *cp = i % 256;
+        }
+
+        //printk(KERN_INFO"%s: CCC\n", __FUNCTION__);
+
+        urb->hcpriv = NULL;
+        urb->context = isotest;
+        urb->transfer_flags = USB_ISO_ASAP;
+        urb->complete = isotest_iso_out_complete;
+        urb->pipe = usb_sndisocpipe(isotest->udev, isotest->iso_out_endpointAddr);
+
+        urb->transfer_buffer_length = size;
+
+        urb->number_of_packets = frames;
+
+        THROW_IF((rc = iso_out_submit(isotest, urb)), error);
+
+        //printk(KERN_INFO"%s: OK frames: %d\n", __FUNCTION__, frames);
+
+        CATCH(error) {
+                printk(KERN_INFO"%s: FAILED rc: %d\n", __FUNCTION__, rc);
+                //isotest_iso_out_free_urb(urb);
+                return NULL;
+        }
+        return urb;
+}
+
+/* ISO IN - Receive **************************************************************************** */
+
+static int in_count = 5;
+static int in_submitted;
+static int in_resubmitted;
+static long in_completed;
+static long in_total_received;
+
+#if 0
+static void isotest_iso_in_free_urb(struct urb *urb)
+{
+        struct usb_isotest *isotest;
+        int i;
+        unsigned long   flags;
+
+        //printk(KERN_INFO"%s: urb: %p\n", __FUNCTION__, urb);
+
+        RETURN_IF(!urb);
+
+        if (urb->transfer_buffer) {
+                kfree(urb->transfer_buffer);
+        }
+
+        isotest = (struct usb_isotest *)urb->context;
+        usb_free_urb(urb);
+
+        RETURN_IF(!isotest);
+
+        local_irq_save (flags);
+        for (i = 0; i < IN_URBS; i++) {
+                CONTINUE_IF(isotest->iso_in_urbs[i] != urb);
+                //printk(KERN_INFO"%s: clearing %d urb: %p\n", __FUNCTION__, i, urb);
+                isotest->iso_in_urbs[i] = NULL;
+                break;
+        }
+        local_irq_restore (flags);
+}
+#endif
+
+struct urb *iso_in_start(struct usb_isotest *isotest);
+
+int iso_in_submit(struct usb_isotest *isotest, struct urb *urb) 
+{
+        int                     i;
+        int                     j;
+        int                     rc = 0;
+
+        //printk(KERN_INFO"%s: %p\n", __FUNCTION__, urb->complete);
+        
+        RETURN_ZERO_IF(isotest->closing);
+
+        urb->dev = isotest->udev;
+        urb->actual_length = 0;
+
+        for (j = urb->transfer_buffer_length, i = 0; i < urb->number_of_packets; i++) {
+                
+                int                     send = MIN(isotest->iso_in_size, j);
+                j -= send;
+                urb->iso_frame_desc[i].offset = i * isotest->iso_in_size;
+                urb->iso_frame_desc[i].length = send;
+        }       
+
+        RETURN_ZERO_IF(!(rc = usb_submit_urb(urb)));
+        printk(KERN_INFO"%s: FAILED rc: %x\n", __FUNCTION__, rc);
+
+        //isotest_iso_in_free_urb(urb);
+        return rc;
+}
+
+
+/**
+ * isotest_iso_in_complete
+ */
+void isotest_iso_in_complete (struct urb *urb)
+{
+        struct usb_isotest *isotest = (struct usb_isotest *)urb->context;
+        int i;
+        int rc;
+        int status;
+
+        //printk(KERN_INFO"%s: urb: %p\n", __FUNCTION__, urb);
+
+        RETURN_IF(!urb);
+
+        if (isotest->closing) {
+                //printk(KERN_INFO"%s: urb: %p pre urbs: %d\n", __FUNCTION__, urb, isotest->in_urbs);
+                if (urb->transfer_buffer) {
+                        kfree(urb->transfer_buffer);
+                }
+                usb_free_urb(urb);
+                isotest->in_urbs--;
+                //printk(KERN_INFO"%s: urb: %p pre urbs: %d\n", __FUNCTION__, urb, isotest->in_urbs);
+                return;
+        }
+        status = urb->status;
+
+        if (status /* && (status != -ENOENT) && (status != -ECONNRESET)*/) {
+                //printk(KERN_INFO"%s: - urb: %p nonzero write iso status received: %x\n", __FUNCTION__, urb, status);
+        }
+
+        else if (urb->actual_length) {
+                //printk(KERN_INFO"%s: urb: %p lenght: %d\n", __FUNCTION__, urb, urb->actual_length);
+                in_completed++;
+                in_total_received += urb->actual_length;
+
+                //printk(KERN_INFO"%s: ", __FUNCTION__);
+                for (i = 0; i < urb->number_of_packets; i++) {
+
+                        iso_trace_recv_data(&isotest->stats, 
+                                        urb->transfer_buffer + urb->iso_frame_desc[i].offset,
+                                        urb->iso_frame_desc[i].actual_length, 0);
+
+                //        printk("%d:%d:%x ", i, 
+                //                        urb->iso_frame_desc[i].actual_length,
+                //                        urb->iso_frame_desc[i].status);
+
+
+                }       
+                //printk("\n");
+        }
+
+        THROW_IF((rc = iso_in_submit(isotest, urb)), error);
+
+        in_resubmitted++;
+
+        CATCH(error) {
+                printk(KERN_INFO"%s: FAILED rc: %x\n", __FUNCTION__, rc);
+        }
+}
+
+
+struct urb *iso_in_start(struct usb_isotest *isotest)
+{
+        struct urb *            urb = NULL;
+        int                     frames = (ISO_SEND_TOTAL + isotest->iso_in_size) / isotest->iso_in_size;
+        int                     iso_transfer_size;
+        int                     rc = 0;
+
+
+        //RETURN_NULL_IF(in_count-- <= 0);
+
+        //printk(KERN_INFO"%s: %d %02x\n", __FUNCTION__, in_count, isotest->iso_in_endpointAddr);
+
+        //iso_transfer_size = ((ISO_SEND_TOTAL % isotest->iso_in_size) > 20) ?
+        //        ISO_SEND_TOTAL :
+        //        ISO_SEND_TOTAL + (20 - (ISO_SEND_TOTAL % isotest->iso_in_size));
+
+        iso_transfer_size = frames * isotest->iso_in_size;
+
+        THROW_IF(!(urb = usb_alloc_urb(frames + 1)), error);
+        THROW_IF (!(urb->transfer_buffer = kmalloc(iso_transfer_size, GFP_ATOMIC)), error);
+        memset(urb->transfer_buffer, 0, isotest->iso_in_size);
+
+        urb->hcpriv = NULL;
+        urb->context = isotest;
+        urb->transfer_flags = USB_ISO_ASAP;
+        urb->complete = isotest_iso_in_complete;
+        urb->pipe = usb_rcvisocpipe(isotest->udev, isotest->iso_in_endpointAddr);
+
+        urb->transfer_buffer_length = iso_transfer_size;
+        urb->number_of_packets = frames;
+
+        THROW_IF((rc = iso_in_submit(isotest, urb)), error);
+        in_submitted++;
+        isotest->in_urbs++;
+        //printk(KERN_INFO"%s: new urbs: %d\n", __FUNCTION__, isotest->in_urbs);
+        return urb;
+
+        CATCH(error) {
+                printk(KERN_INFO"%s: FAILED rc: %x\n", __FUNCTION__, rc);
+                //isotest_iso_in_free_urb(urb);
+                return NULL;
+        }
+}
+
+
+
+
+/* ********************************************************************************************* */
+
+void isotest_schedule_bh(struct usb_isotest *isotest)
+{
+        unsigned long   flags;
+
+        //RETURN_IF(!isotest->iso_bh.data);
+
+        // schedule more data
+        local_irq_save (flags);
+        if (isotest->iso_bh.data && !isotest->iso_bh.sync) {
+                MOD_INC_USE_COUNT;
+                queue_task(&isotest->iso_bh, &tq_immediate);
+                mark_bh(IMMEDIATE_BH);
+        }
+        local_irq_restore (flags);
+}
+
+
+static void bottomhalf(void *data)
+{
+        int i;
+        unsigned long   flags;
+        struct usb_isotest *isotest = (struct usb_isotest *) data;
+
+        if (isotest->first) {
+                //sleep_on_timeout(&isotest->iso_wq, 200);
+                udelay(100);
+                isotest->first = 0;
+        }
+
+        THROW_IF(!isotest, error);
+
+        if (isotest->closing) {
+                struct urb *urb;
+                //printk(KERN_INFO"%s: closing\n", __FUNCTION__);
+
+                // unlink outstanding urbs, this has side-effect of calling completion routing
+                local_irq_save (flags);
+                for (i = 0; i < IN_URBS; i++) {
+                        CONTINUE_IF(!(urb = isotest->iso_in_urbs[i]));
+                        //printk(KERN_INFO"%s: unlinking: %d IN urb: %p\n", __FUNCTION__, i, urb);
+                        isotest->iso_in_urbs[i] = NULL;
+                        urb->transfer_flags |= USB_ASYNC_UNLINK;
+                        usb_unlink_urb(urb);
+                }
+                local_irq_restore (flags);
+
+                local_irq_save (flags);
+                for (i = 0; i < OUT_URBS; i++) {
+                        CONTINUE_IF(!(urb = isotest->iso_out_urbs[i]));
+                        //printk(KERN_INFO"%s: unlinking: %d OUT urb: %p\n", __FUNCTION__, i, urb);
+                        isotest->iso_out_urbs[i] = NULL;
+                        urb->transfer_flags |= USB_ASYNC_UNLINK;
+                        usb_unlink_urb(urb);
+                }
+                local_irq_restore (flags);
+
+                // tell disconnect that we are finished
+                isotest->iso_bh.data = NULL;
+
+        }
+        else {
+
+                //printk(KERN_INFO"%s: normal\n", __FUNCTION__);
+
+                if (send && out_count) {
+                        local_irq_save (flags);
+                        for (i = 0; i < OUT_URBS; i++) {
+                                CONTINUE_IF(isotest->iso_out_urbs[i]);
+                                isotest->iso_out_urbs[i] = iso_out_start(isotest);
+                                //printk(KERN_INFO"%s: starting: %d OUT urb: %p\n", __FUNCTION__, i, isotest->iso_out_urbs[i]);
+                        }
+                        local_irq_restore (flags);
+                }
+
+                if (recv && in_count) {
+                        local_irq_save (flags);
+                        for (i = 0; i < IN_URBS; i++) {
+                                CONTINUE_IF(isotest->iso_in_urbs[i]);
+                                isotest->iso_in_urbs[i] = iso_in_start(isotest);
+                                //printk(KERN_INFO"%s: starting: %d IN urb: %p\n", __FUNCTION__, i, isotest->iso_in_urbs[i]);
+                        }
+                        local_irq_restore (flags);
+                }
+        }
+
+        CATCH(error) {
+                printk(KERN_ERR"%s: isotest NULL\n", __FUNCTION__);
+        }
+        MOD_DEC_USE_COUNT;
+}
+
+/* ********************************************************************************************* */
+
+/* usb specific object needed to register this driver with the usb subsystem */
+static struct usb_driver isotest_driver = {
+        name:           "isotest",
+        probe:          isotest_probe,
+        disconnect:     isotest_disconnect,
+        id_table:       isotest_table,
+};
+
+/**
+ * isotest_probe
+ *
+ * Called by the usb core when a new device is connected that it thinks
+ * this driver might be interested in.
+ */
+static void * isotest_probe(struct usb_device *udev, unsigned int ifnum, const struct usb_device_id *id)
+{
+        struct usb_isotest *isotest = NULL;
+        struct usb_interface *interface;
+        struct usb_device_descriptor *device = &udev->descriptor;
+        struct usb_interface_descriptor *interface_descriptor;
+        int i;
+
+        printk(KERN_INFO"%s: %04x %04x\n", __FUNCTION__, device->idVendor, device->idProduct);
+
+        // See if the device offered us matches what we can accept 
+        //if ((device->idVendor != vendor_id) || (device->idProduct != product_id)) {
+        //        printk(KERN_INFO"%s: FAILED\n", __FUNCTION__);
+        // return NULL;
+        //}
+
+        // allocate memory for our device state and intialize it 
+        if (!(isotest = kmalloc (sizeof(struct usb_isotest), GFP_KERNEL))) {
+                printk(KERN_INFO"%s: Out of memory\n", __FUNCTION__);
+                return NULL;
+        }
+
+        memset (isotest, 0x00, sizeof (*isotest));
+        init_MUTEX (&isotest->sem);
+        init_waitqueue_head(&isotest->iso_wq);
+
+        isotest->udev = udev;
+        isotest->first = 1;
+        isotest->closing = 0;
+        isotest->in_urbs = 0;
+        isotest->interface = interface = &udev->actconfig->interface[ifnum];
+        isotest->iso_bh.routine = bottomhalf;
+        isotest->iso_bh.data = (void *)isotest;
+        interface_descriptor = &interface->altsetting[0];
+
+        // set up the endpoint information and check out the endpoints 
+
+        for (i = 0; i < interface_descriptor->bNumEndpoints; ++i) {
+
+                struct usb_endpoint_descriptor *endpoint = &interface_descriptor->endpoint[i];
+
+                //printk(KERN_INFO"%s: looking at %02x\n", __FUNCTION__, endpoint->bEndpointAddress);
+
+                if ((endpoint->bEndpointAddress & 0x80) && ((endpoint->bmAttributes & 3) == 0x01)) {
+
+                        //printk(KERN_INFO"%s: found ISO IN %02x\n", __FUNCTION__, endpoint->bEndpointAddress);
+                        isotest->iso_in_size = endpoint->wMaxPacketSize;
+                        isotest->iso_in_endpointAddr = endpoint->bEndpointAddress;
+                }
+
+                if (((endpoint->bEndpointAddress & 0x80) == 0x00) && ((endpoint->bmAttributes & 3) == 0x01)) {
+
+                        //printk(KERN_INFO"%s: found ISO OUT %02x\n", __FUNCTION__, endpoint->bEndpointAddress);
+
+                        isotest->iso_out_size = endpoint->wMaxPacketSize;
+                        isotest->iso_out_endpointAddr = endpoint->bEndpointAddress;
+                }
+        }
+
+        out_count = ISO_SEND_TRANSFERS;
+
+        // let the user know what node this device is now attached to 
+        //printk(KERN_INFO"%s: USB TEST device now attached to ISOTEST\n", __FUNCTION__);
+
+        isotest_schedule_bh(isotest);
+
+        return isotest;
+}
+
+/**
+ * isotest_disconnect
+ *
+ * Called by the usb core when the device is removed from the system.
+ */
+static void isotest_disconnect(struct usb_device *udev, void *ptr)
+{
+        struct usb_isotest *isotest;
+
+        printk(KERN_INFO"%s: in_submitted: %d in_resubmitted: %d in_completed: %ld in_total: %ld\n", 
+                        __FUNCTION__, in_submitted, in_resubmitted, in_completed, in_total_received);
+
+        RETURN_IF(!(isotest = (struct usb_isotest *)ptr));
+
+        // set flag to say we are closing
+        isotest->closing = 1;
+        isotest_schedule_bh(isotest);
+
+        while (isotest->iso_bh.data) {
+                isotest_schedule_bh(isotest);
+                printk(KERN_INFO"%s: waiting for bh\n", __FUNCTION__);
+                sleep_on_timeout(&isotest->iso_wq, 20);
+        }
+
+        while (isotest->in_urbs) {
+                printk(KERN_INFO"%s: waiting for urbs\n", __FUNCTION__);
+                sleep_on_timeout(&isotest->iso_wq, 20);
+        }
+
+        kfree(isotest);
+
+        printk(KERN_INFO"%s: USB ISOTEST now disconnected\n", __FUNCTION__);
+}
+
+
+/* ********************************************************************************************* */
+
+void iso_start_in(int count)
+{
+
+}
+
+void iso_start_out(int count)
+{
+
+}
+
+
+/**
+ * isotest_init
+ */
+static int isotest_init(void)
+{
+        int result;
+
+        printk(KERN_INFO"%s:\n", __FUNCTION__);
+
+        if (vendor_id && product_id) {
+                int i;
+                for (i = 0; i < (sizeof(isotest_table) / sizeof(struct usb_device_id) - 1); i++) {
+
+                        if ((isotest_table[i].idVendor == vendor_id) && isotest_table[i].idProduct == product_id ) {
+                                printk(KERN_INFO"%s: vendor_id: %04x product_id: %04x already in table\n",
+                                                __FUNCTION__, vendor_id, product_id);
+                                break;
+                        }
+                        printk(KERN_INFO"%s: vendor_id: %04x product_id: %04x\n",
+                                        __FUNCTION__, isotest_table[i].idVendor, isotest_table[i].idProduct);
+                }
+                if (!isotest_table[i].idVendor && !isotest_table[i].idProduct) {
+                        printk(KERN_INFO"%s: inserting vendor_id: %04x product_id: %04x into table\n",
+                                        __FUNCTION__, vendor_id, product_id);
+
+                        isotest_table[i].match_flags = USB_DEVICE_ID_MATCH_DEVICE;
+                        isotest_table[i].idVendor = vendor_id;
+                        isotest_table[i].idProduct = product_id;
+                        isotest_table[i].bDeviceClass = 0;
+                        isotest_table[i].bDeviceSubClass = 0;
+                }
+        }
+
+        iso_trace_init("isotest_host");
+
+        /* register this driver with the USB subsystem */
+        result = usb_register(&isotest_driver);
+        if (result < 0) {
+                printk(KERN_INFO"%s: usb_register failed for the "__FILE__" driver. Error number %d\n", KERN_INFO, result);
+                return -1;
+        }
+
+        printk(KERN_INFO "%s: " DRIVER_DESC " " DRIVER_VERSION "\n", __FUNCTION__);
+        return 0;
+}
+
+
+/**
+ * isotest_exit
+ */
+static void isotest_exit(void)
+{
+        /* deregister this driver with the USB subsystem */
+        usb_deregister(&isotest_driver);
+
+        iso_trace_exit("isotest_host");
+}
+
+
+module_init (isotest_init);
+module_exit (isotest_exit);
+
+MODULE_AUTHOR(DRIVER_AUTHOR);
+MODULE_DESCRIPTION(DRIVER_DESC);
+#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,17)
+MODULE_LICENSE("PRIVATE");
+#endif
+
diff -uNr linux/drivers/no-otg/functions/isotest/iso.c linux/drivers/otg/functions/isotest/iso.c
--- linux/drivers/no-otg/functions/isotest/iso.c	1970-01-01 01:00:00.000000000 +0100
+++ linux/drivers/otg/functions/isotest/iso.c	2006-09-01 21:41:27.000000000 +0200
@@ -0,0 +1,620 @@
+/*
+ * otg/isotest_fd/iso.c
+ *
+ *      Copyright (c) 2003, 2004 Belcarra
+ *
+ * By: 
+ *      Stuart Lynne <sl@belcarra.com>, 
+ *      Tom Rushworth <tbr@belcarra.com>, 
+ *      Bruce Balden <balden@belcarra.com>
+ *
+ *
+ */
+
+#include <linux/config.h>
+#include <linux/module.h>
+#include <linux/kernel.h>
+#include <linux/version.h>
+
+MODULE_AUTHOR ("sl@belcarra.com, tbr@belcarra.com");
+MODULE_DESCRIPTION ("USB Device Serial Function");
+
+#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,17)
+MODULE_LICENSE("GPL");
+#endif
+
+#include <linux/init.h>
+#include <linux/list.h>
+#include <asm/uaccess.h>
+#include <linux/slab.h>
+#include <linux/interrupt.h>
+
+#include <linux/smp_lock.h>
+#include <linux/ctype.h>
+#include <linux/timer.h>
+#include <linux/string.h>
+
+#include "usbp-chap9.h"
+#include <usbp-mem.h>
+#include <usbp-func.h>
+#include <usbp-admin.h>
+
+USBD_MODULE_INFO ("isotest_fd 2.0-beta");
+
+#include "test.h"
+#include "fermat.h"
+
+struct usb_isotest {
+        int interface;
+        struct usbd_function_instance *function;
+        rwlock_t rwlock;
+
+        int open;
+        int closing;
+        struct tq_struct iso_bh;
+        wait_queue_head_t       iso_wq;
+};
+
+#define ISO_OUT        0x00
+#define ISO_IN         0x01
+
+#define ENDPOINTS       0x02
+
+
+u8 isotest_requested_endpoints[ENDPOINTS+1] = {
+        USB_DIR_OUT | USB_ENDPOINT_ISOCHRONOUS,
+        USB_DIR_IN | USB_ENDPOINT_ISOCHRONOUS,
+        0,
+};
+
+#define ISO_OUT_PKTSIZE 90
+#define ISO_IN_PKTSIZE 90
+#define isotest_requested_transferSizes xfer_sizes
+
+u16 xfer_sizes[ENDPOINTS+1] = {
+        ISO_OUT_PKTSIZE,
+        ISO_IN_PKTSIZE,
+        0,
+};
+
+
+
+/* Module Parameters ************************************************************************* */
+// override vendor ID
+static u32 vendor_id;
+MODULE_PARM (vendor_id, "i");
+MODULE_PARM_DESC (vendor_id, "vendor id");
+
+// override product ID
+static u32 product_id;
+MODULE_PARM (product_id, "i");
+MODULE_PARM_DESC (product_id, "product id");
+
+MODULE_PARM (xfer_sizes, "3-3h");
+MODULE_PARM_DESC (xfer_sizes, "Requested transfer sizes for each endpoint; default 90 for iso in and out");
+
+// packet sizes
+static u32 in = ISO_IN_PKTSIZE;
+MODULE_PARM (in, "i");
+MODULE_PARM_DESC (in, "in size");
+
+static u32 out = ISO_OUT_PKTSIZE;
+MODULE_PARM (out, "i");
+MODULE_PARM_DESC (out, "out size");
+
+static int fermat=0;
+MODULE_PARM (fermat, "i");
+MODULE_PARM_DESC (fermat, "Apply randomization to buffer");
+
+static int custom=0;
+MODULE_PARM (custom, "i");
+MODULE_PARM_DESC (custom, "Supply custom pattern via xmit_pattern parameter");
+
+static int print_all=0;
+MODULE_PARM (print_all, "i");
+MODULE_PARM_DESC (print_all, "Print all buffers, not just the first");
+
+#define ZERO4 0,0,0,0
+#define ZERO16 ZERO4,ZERO4,ZERO4,ZERO4
+#define ZERO64 ZERO16,ZERO16,ZERO16,ZERO16
+static u8 xmit_pattern[256]={1,0xE,1,2,3,4,5,6,7,8,9,0xa,0xb,0xc,0xd,0xe, ZERO16,ZERO16,ZERO16,ZERO64,ZERO64,ZERO64};
+MODULE_PARM(xmit_pattern,"1-256b");
+MODULE_PARM_DESC(xmit_pattern, "pattern to be transmitted, count, size, payload; size is "
+                "the size of the payload following. count is the number of times to repeat the pattern (0=infinite)");
+static struct {
+        int     count;
+        int     size;
+        u8      *payload_start, *payload_end;
+        u8      *current;
+        int     total_size;
+        int     sent;
+} xps; // Transmit pattern state
+
+static void xmit_pattern_state_init(void);
+static u8 xmit_pattern_next(void);
+static void fill_xmit_buffer(u32 size, u8*buffer);
+
+static void xmit_pattern_state_init(){
+        xps.count = xmit_pattern[0];
+        xps.size = xmit_pattern[1];
+        xps.payload_start = xmit_pattern+2;
+        xps.payload_end = xps.payload_start + xps.size;
+        xps.current = xps.payload_start;
+        xps.sent = 0;
+        xps.total_size = xps.count * xps.size;
+}
+
+static u8 xmit_pattern_next(){
+
+        // current always points at the next character to send
+        u8      next_value = *xps.current++;
+
+        xps.sent += 1;
+
+        if (xps.current >= xps.payload_end)
+                xps.current = xps.payload_start; //Rewind buffer
+
+        // if Total pattern has been sent; rewind
+        if (xps.sent >= xps.total_size){
+                xmit_pattern_state_init();
+        }
+
+        return next_value;
+}
+
+static void fill_xmit_buffer(u32 size, u8 *buffer)
+{
+        u8 * limit = buffer + size;
+        xmit_pattern_state_init();
+        while(buffer < limit){
+                *buffer++ = xmit_pattern_next();
+        }
+}
+
+static void fill_xmit_buffer_default(u32 size, u8 * buffer){
+        int  j;
+        for(j = 0; j < size; j++){
+                buffer[j] = j & 0xff;
+        }
+}
+
+static void print_buffer(u32 size, u8 * buffer){
+        // Print up to 16 bytes of the buffer, first time called only
+        static int first = 1;
+        int n = ( size >=16 ? 16 : size);
+        int j;
+        if((first) || (print_all)){
+                char *prefix = first? "\n" : "";
+                printk(KERN_INFO "%sxmit buffer:", prefix);
+                for( j=0 ; j < n; j++){
+                        printk("%02x",buffer[j]);
+                }
+                printk("\n");
+                first = 0;
+        }
+}
+
+
+
+/* ************************************************************************** */
+
+static struct isotest_stats isotest_stats;
+static struct usb_isotest isotest;
+
+
+/* Classes descriptors
+ */
+
+static u8 isotest_ep_1[7] = { 0x07, USB_DT_ENDPOINT, 0, ISOCHRONOUS, ISO_OUT_PKTSIZE&0xff, (ISO_OUT_PKTSIZE>>8)&0xff, 0x00};
+static u8 isotest_ep_2[7] = { 0x07, USB_DT_ENDPOINT, 0, ISOCHRONOUS, ISO_IN_PKTSIZE&0xff, (ISO_IN_PKTSIZE>>8)&0xff, 0x00};
+
+static struct usbd_endpoint_descriptor *isotest_data_endpoints[] = { 
+        (struct usbd_endpoint_descriptor *) &isotest_ep_1, 
+        (struct usbd_endpoint_descriptor *) &isotest_ep_2 };
+
+u8 isotest_data_indexes [] = { ISO_OUT, ISO_IN, };
+
+
+/* Alternate descriptors
+ */
+static u8 isotest_data_alternate_descriptor[sizeof(struct usbd_interface_descriptor)] = {
+        0x09, USB_DT_INTERFACE, 0x00, 0x00, 0x01, // bInterfaceNumber, bAlternateSetting, bNumEndpoints
+        0x00, 0x00, 0x00, 0x00,
+};
+
+
+/* Alternate descriptions
+ */
+static struct usbd_alternate_description isotest_data_alternate_descriptions[] = {
+        { iInterface:CONFIG_OTG_ISOTEST_DATA_INTF,
+                interface_descriptor: (struct usbd_interface_descriptor *)&isotest_data_alternate_descriptor,
+                endpoints:sizeof (isotest_data_endpoints) / sizeof(u8 *),
+                endpoint_list: isotest_data_endpoints, 
+                endpoint_indexes: isotest_data_indexes,
+        },
+};
+
+
+/* Interface descriptions
+ */
+static struct usbd_interface_description isotest_interfaces[] = {
+        { alternates:sizeof (isotest_data_alternate_descriptions) / sizeof (struct usbd_alternate_description),
+alternate_list:isotest_data_alternate_descriptions,
+        },
+};
+
+
+/* Configuration descriptions
+ */
+static u8 isotest_configuration_descriptor[sizeof(struct usbd_configuration_descriptor)] = {
+        0x09, USB_DT_CONFIGURATION, 0x00, 0x00, // wLength
+        sizeof (isotest_interfaces) / sizeof (struct usbd_interface_description),
+        0x01, 0x00, // bConfigurationValue, iConfiguration
+        0, 0,
+};
+
+
+struct usbd_configuration_description isotest_description[] = {
+        { iConfiguration:CONFIG_OTG_ISOTEST_DESC,
+                configuration_descriptor: (struct usbd_configuration_descriptor *)isotest_configuration_descriptor,
+        },
+};
+
+/* Device Description
+ */
+static struct usbd_device_descriptor isotest_device_descriptor = {
+        bLength: sizeof(struct usbd_device_descriptor),
+        bDescriptorType: USB_DT_DEVICE,
+        bcdUSB: __constant_cpu_to_le16(USB_BCD_VERSION),
+        bDeviceClass: COMMUNICATIONS_DEVICE_CLASS,
+        bDeviceSubClass: 0x00,
+        bDeviceProtocol: 0x00,
+        bMaxPacketSize0: 0x00,
+        idVendor: __constant_cpu_to_le16(CONFIG_OTG_ISOTEST_VENDORID),
+        idProduct: __constant_cpu_to_le16(CONFIG_OTG_ISOTEST_PRODUCTID),
+        bcdDevice: __constant_cpu_to_le16(CONFIG_OTG_ISOTEST_BCDDEVICE),
+};
+
+#ifdef CONFIG_OTG_HIGH_SPEED
+static struct usbd_device_qualifier_descriptor isotest_device_qualifier_descriptor = {
+        bLength: sizeof(struct usbd_device_qualifier_descriptor),
+        bDescriptorType: USB_DT_DEVICE_QUALIFIER,
+        bcdUSB: __constant_cpu_to_le16(USB_BCD_VERSION),
+        bDeviceClass: COMMUNICATIONS_DEVICE_CLASS,
+        bDeviceSubClass: 0x00,
+        bDeviceProtocol: 0x00,
+        bMaxPacketSize0: 0x00,
+};
+#endif /* CONFIG_OTG_HIGH_SPEED */
+
+static struct usbd_endpoint_request iso_endpoint_requests[ENDPOINTS+1] = {
+        { 1, 0, 0, USB_DIR_OUT | USB_ENDPOINT_ISOCHRONOUS, ISO_OUT_PKTSIZE, ISO_OUT_PKTSIZE * 4, },
+        { 1, 0, 0, USB_DIR_IN | USB_ENDPOINT_ISOCHRONOUS, ISO_IN_PKTSIZE, ISO_OUT_PKTSIZE * 4, },
+        { 1, },
+};
+
+static struct usbd_otg_descriptor iso_otg_descriptor = {
+bLength : sizeof(struct usbd_otg_descriptor),
+bDescriptorType: USB_DT_OTG,
+bmAttributes: 0,
+};
+
+struct usbd_device_description isotest_device_description = {
+        device_descriptor: &isotest_device_descriptor,
+        #ifdef CONFIG_OTG_HIGH_SPEED
+        device_qualifier_descriptor: &isotest_device_qualifier_descriptor,
+        #endif /* CONFIG_OTG_HIGH_SPEED */
+        otg_descriptor: &iso_otg_descriptor,
+        iManufacturer: CONFIG_OTG_ISOTEST_MANUFACTURER,
+        iProduct: CONFIG_OTG_ISOTEST_PRODUCT_NAME,
+        #if !defined(CONFIG_OTG_NO_SERIAL_NUMBER) && defined(CONFIG_OTG_SERIAL_NUMBER_STR)
+        iSerialNumber: CONFIG_OTG_SERIAL_NUMBER_STR,
+        #endif
+};
+
+void iso_start_in(int count);
+void iso_start_out(int count);
+
+void schedule_bh(void) 
+{
+        unsigned long   flags;
+
+        //printk(KERN_INFO"%s: GET_USE_COUNT: %d\n", __FUNCTION__, GET_USE_COUNT(THIS_MODULE));
+
+        local_irq_save (flags);
+        if (!isotest.iso_bh.sync) {
+                MOD_INC_USE_COUNT;
+                queue_task(&isotest.iso_bh, &tq_immediate);
+                mark_bh(IMMEDIATE_BH);
+        }
+        local_irq_restore (flags);
+
+        //printk(KERN_INFO"%s: GET_USE_COUNT: %d\n", __FUNCTION__, GET_USE_COUNT(THIS_MODULE));
+}
+
+
+int isotest_urb_sent (struct usbd_urb *urb, int rc);
+int isotest_recv_urb (struct usbd_urb *urb, int rc);
+
+/* Transmit Function *************************************************************************** */
+
+static int out_count;
+static int in_count;
+static int send_size = 1000;
+static int iso_transfer_in_count;
+
+static int isotest_xmit_data (void)
+{
+        int i; 
+        int j; 
+        int frames;
+        int size = ((send_size % in) < 20) ? send_size + 20 : send_size;
+        struct usbd_urb *urb;
+        struct usbd_function_instance *function = isotest.function;
+
+        //printk(KERN_INFO"%s: open: %d in_count: %d\n", __FUNCTION__, isotest.open, in_count);
+
+        RETURN_ZERO_IF(!isotest.open);
+
+        RETURN_EINVAL_IF(!(urb = usbd_alloc_urb (isotest.function, ISO_IN, size, isotest_urb_sent)));
+
+        frames = (size / in) + 1;
+
+        iso_transfer_in_count++;
+
+        for (i = 0; i < frames; i++) {
+
+                u8 *cp = urb->buffer + (i * in);
+                if(custom){
+                        (void) fill_xmit_buffer(in, cp);
+                } else {
+                        (void) fill_xmit_buffer_default(in,cp);
+                }
+                if(fermat){
+                        fermat_encode(cp, in);
+                }
+                print_buffer(in, cp);
+
+                // iso_transfer_count
+                *cp++ = cpu_to_le16(iso_transfer_in_count) & 0xff;
+                *cp++ = (cpu_to_le16(iso_transfer_in_count) >> 8) & 0xff;
+                *cp++ = (cpu_to_le16(iso_transfer_in_count) >> 16) & 0xff;
+                *cp++ = (cpu_to_le16(iso_transfer_in_count) >> 24) & 0xff;
+
+                // iso transfer length
+                *cp++ = cpu_to_le16(size) & 0xff;
+                *cp++ = (cpu_to_le16(size) >> 8) & 0xff;
+
+                // iso frame size
+                *cp++ = cpu_to_le16(in) & 0xff;
+                *cp++ = (cpu_to_le16(in) >> 8) & 0xff;
+
+                // total frames
+                *cp++ = cpu_to_le16(frames) & 0xff;
+                *cp++ = (cpu_to_le16(frames) >> 8) & 0xff;
+
+                // this packet number
+                *cp++ = cpu_to_le16(i+1) & 0xff;
+                *cp++ = (cpu_to_le16(i+1) >> 8) & 0xff;
+
+        }
+        urb->actual_length = size;
+
+        // push it down into the usb-device layer
+        //printk(KERN_INFO"%s: sending: %p length: %d\n", __FUNCTION__, urb, urb->actual_length);
+        return usbd_start_in_urb (urb);
+}
+
+
+/* isotest_urb_sent - called to indicate URB transmit finished
+ * @urb: pointer to struct usbd_urb
+ * @rc: result
+ */
+int isotest_urb_sent (struct usbd_urb *urb, int rc)
+{
+        //printk(KERN_INFO"%s: sent: %p status: %d\n", __FUNCTION__, urb, urb->status);
+
+        usbd_free_urb (urb);
+
+        //printk(KERN_INFO"%s: calling schedule_bh\n", __FUNCTION__);
+        schedule_bh();
+        return 0;
+}
+
+
+/* USB Device Functions ************************************************************************ */
+
+/* isotest_event_handler - process a device event
+ *
+ */
+void isotest_event_handler (struct usbd_function_instance *function, usbd_device_event_t event, int data)
+{
+        int i;
+        switch (event) {
+
+        case DEVICE_RESET:
+        case DEVICE_DESTROY:
+                if (isotest.open) 
+                        usbd_disable_irq(NULL);
+
+                isotest.open = 0;
+                break;
+
+        case DEVICE_CONFIGURED:
+                isotest.open = 1;
+                for (i = 0; i < 2; i++) {
+                        struct usbd_urb *urb;
+                        BREAK_IF(!(urb = usbd_alloc_urb (function, ISO_OUT, 
+                                                        usbd_endpoint_transferSize(
+                                                                function, ISO_OUT,usbd_high_speed(function)), 
+                                                        isotest_recv_urb
+                                                        )));
+                        if (usbd_start_out_urb(urb)) 
+                                usbd_free_urb(urb);
+                }
+                iso_start_in(100);
+                break;
+
+        case DEVICE_BUS_INACTIVE:
+                break;
+
+        case DEVICE_BUS_ACTIVITY:
+                break;
+
+        default:
+                break;
+        }
+}
+
+
+/* isotest_recv_urb - called to indicate URB has been received
+ * @urb - pointer to struct usbd_urb
+ *
+ * Return non-zero if we failed and urb is still valid (not disposed)
+ */
+int isotest_recv_urb (struct usbd_urb *urb, int rc)
+{
+        //struct usbd_device_instance *device = urb->device;
+#if 0
+        printk(KERN_INFO"%s: urb: %p length: %d framenum: %04x %d\n", __FUNCTION__, 
+                        urb, urb->actual_length, urb->framenum, urb->framenum);
+#endif
+        iso_trace_recv_data(&isotest_stats, urb->buffer, urb->actual_length, 0);
+
+        // start_recv urb
+        return (usbd_start_out_urb (urb));
+}
+
+
+/* ********************************************************************************************* */
+
+void iso_start_in(int count)
+{
+        in_count = count;
+        //printk(KERN_INFO"%s: calling schedule_bh\n", __FUNCTION__);
+        schedule_bh();
+}
+
+void iso_start_out(int count)
+{
+        out_count = count;
+        //printk(KERN_INFO"%s: calling schedule_bh\n", __FUNCTION__);
+        schedule_bh();
+}
+
+static void
+bottomhalf(void *data)
+{       
+        //printk(KERN_INFO"%s: closing: %d\n", __FUNCTION__, isotest.closing);
+        if (isotest.closing) 
+                isotest.iso_bh.data = NULL;
+
+        else if (isotest.open) 
+                isotest_xmit_data ();
+
+        MOD_DEC_USE_COUNT;
+        //printk(KERN_INFO"%s: GET_USE_COUNT: %d\n", __FUNCTION__, GET_USE_COUNT(THIS_MODULE));
+}
+
+
+/* ********************************************************************************************* */
+
+static int isotest_function_enable (struct usbd_function_instance *function)
+{
+        //printk(KERN_INFO"%s:\n", __FUNCTION__);
+        MOD_INC_USE_COUNT;
+
+        //printk(KERN_INFO"%s: calling schedule_bh\n", __FUNCTION__);
+        isotest.closing = 0;
+        schedule_bh();
+        return 0;
+}
+
+static void isotest_function_disable (struct usbd_function_instance *function)
+{
+        isotest.closing = 1;
+        while (isotest.iso_bh.data) {
+                //printk(KERN_INFO"%s: calling schedule_bh\n", __FUNCTION__);
+                schedule_bh();
+                //printk(KERN_INFO"%s: waiting\n", __FUNCTION__);
+                sleep_on_timeout(&isotest.iso_wq, 20);
+        }
+        MOD_DEC_USE_COUNT;
+}
+
+
+struct usbd_function_operations function_ops = {
+        event_handler:isotest_event_handler,
+        function_enable: isotest_function_enable,
+        function_disable: isotest_function_disable,
+};
+
+struct usbd_function_driver function_driver = {
+        name:"ISO Test Function",
+        fops:&function_ops,
+        device_description:&isotest_device_description,
+        bNumConfigurations:sizeof (isotest_description) / sizeof (struct usbd_configuration_description),
+        configuration_description:isotest_description,
+        idVendor: __constant_cpu_to_le16(CONFIG_OTG_ISOTEST_VENDORID),
+        idProduct: __constant_cpu_to_le16(CONFIG_OTG_ISOTEST_PRODUCTID),
+        bcdDevice: __constant_cpu_to_le16(CONFIG_OTG_ISOTEST_BCDDEVICE),
+        bNumInterfaces:sizeof (isotest_interfaces) / sizeof (struct usbd_interface_description),
+        interface_list:isotest_interfaces,
+        endpointsRequested: ENDPOINTS,
+        requestedEndpoints: iso_endpoint_requests,
+};
+
+
+/*
+ * isotest_modinit - module init
+ *
+ */
+static int isotest_modinit (void)
+{
+        printk (KERN_INFO "vendor_id: %04x product_id: %04x in: %02x out: %02x\n", 
+                        vendor_id, product_id, in, out);
+
+        if (vendor_id) 
+                function_driver.idVendor = cpu_to_le16(vendor_id);
+
+        if (product_id) 
+                function_driver.idProduct = cpu_to_le16(product_id);
+
+
+        isotest_ep_1[4] = out&0xff;
+        isotest_ep_1[5] = (out>>8)&0xff;
+        isotest_ep_2[4] = in&0xff;
+        isotest_ep_2[5] = (in>>8)&0xff;
+
+        // XXX should this be the endpoint request structure?
+        iso_endpoint_requests[0].fs_requestedTransferSize = out;
+        iso_endpoint_requests[1].fs_requestedTransferSize = in;
+
+        iso_trace_init("isotest_fd");
+        if(fermat){
+                fermat_init();
+        }
+
+        isotest.iso_bh.routine = bottomhalf;
+        isotest.iso_bh.data = (void *)&isotest;
+        init_waitqueue_head(&isotest.iso_wq);
+
+        // register us with the usb device support layer
+        RETURN_EINVAL_IF (usbd_register_function (&function_driver, "isotest", NULL));
+
+        // return
+        return 0;
+}
+
+
+/* isotest_modexit - module cleanup
+ */
+static void isotest_modexit (void)
+{
+        usbd_deregister_function (&function_driver);
+        iso_trace_exit("isotest_fd");
+}
+
+module_init (isotest_modinit);
+module_exit (isotest_modexit);
+
diff -uNr linux/drivers/no-otg/functions/isotest/iso_fermat linux/drivers/otg/functions/isotest/iso_fermat
--- linux/drivers/no-otg/functions/isotest/iso_fermat	1970-01-01 01:00:00.000000000 +0100
+++ linux/drivers/otg/functions/isotest/iso_fermat	2006-09-01 21:41:26.000000000 +0200
@@ -0,0 +1,34 @@
+#!/bin/sh
+XFER=$1
+PATTERN=$2
+if [ -z "$PATTERN" ]
+then
+	PATTERN="0x1,0x8,1,2,3,4,5,6,7,8"
+fi
+echo "Using PATTERN='$PATTERN'"
+set -x
+
+insmod  /tmp/usbdcore.o 
+insmod /tmp/usbdprocfs.o
+
+insmod  /tmp/isotest_fd.o vendor_id=0xfff0 product_id=0xfff1 in=$XFER out=$XFER xmit_pattern="$PATTERN" print_all=0 fermat=1 custom=0
+
+insmod  /tmp/au1x00_bi.o 
+echo "enable" > /proc/usbd-switch
+
+
+set +x
+echo -n "INSERT CABLE"
+sleep 15
+echo -n ";  REMOVE CABLE AND PRESS RETURN"
+read junk
+set -x
+cp /proc/isotest_fd client
+cp /proc/isotest_host host
+echo "disable" > /proc/usbd-switch
+
+rmmod au1x00_bi
+rmmod isotest_fd
+rmmod usbdprocfs
+rmmod usbdcore
+
diff -uNr linux/drivers/no-otg/functions/isotest/iso_one linux/drivers/otg/functions/isotest/iso_one
--- linux/drivers/no-otg/functions/isotest/iso_one	1970-01-01 01:00:00.000000000 +0100
+++ linux/drivers/otg/functions/isotest/iso_one	2006-09-01 21:41:26.000000000 +0200
@@ -0,0 +1,34 @@
+#!/bin/sh
+XFER=$1
+PATTERN=$2
+if [ -z "$PATTERN" ]
+then
+	PATTERN="0x1,0x1,0xFF"
+fi
+echo "Using PATTERN='$PATTERN'"
+set -x
+
+insmod  /tmp/usbdcore.o 
+insmod /tmp/usbdprocfs.o
+
+insmod  /tmp/isotest_fd.o vendor_id=0xfff0 product_id=0xfff1 in=$XFER out=$XFER xmit_pattern="$PATTERN" print_all=1 fermat=0 custom=1
+
+insmod  /tmp/au1x00_bi.o 
+echo "enable" > /proc/usbd-switch
+
+
+set +x
+echo -n "INSERT CABLE"
+sleep 15
+echo -n ";  REMOVE CABLE AND PRESS RETURN"
+read junk
+set -x
+cp /proc/isotest_fd client
+cp /proc/isotest_host host
+echo "disable" > /proc/usbd-switch
+
+rmmod au1x00_bi
+rmmod isotest_fd
+rmmod usbdprocfs
+rmmod usbdcore
+
diff -uNr linux/drivers/no-otg/functions/isotest/iso_zero linux/drivers/otg/functions/isotest/iso_zero
--- linux/drivers/no-otg/functions/isotest/iso_zero	1970-01-01 01:00:00.000000000 +0100
+++ linux/drivers/otg/functions/isotest/iso_zero	2006-09-01 21:41:26.000000000 +0200
@@ -0,0 +1,34 @@
+#!/bin/sh
+XFER=$1
+PATTERN=$2
+if [ -z "$PATTERN" ]
+then
+	PATTERN="0x1,0x1,0x00"
+fi
+echo "Using PATTERN='$PATTERN'"
+set -x
+
+insmod  /tmp/usbdcore.o 
+insmod /tmp/usbdprocfs.o
+
+insmod  /tmp/isotest_fd.o vendor_id=0xfff0 product_id=0xfff1 in=$XFER out=$XFER xmit_pattern="$PATTERN" print_all=1 fermat=0 custom=1
+
+insmod  /tmp/au1x00_bi.o 
+echo "enable" > /proc/usbd-switch
+
+
+set +x
+echo -n "INSERT CABLE"
+sleep 15
+echo -n ";  REMOVE CABLE AND PRESS RETURN"
+read junk
+set -x
+cp /proc/isotest_fd client
+cp /proc/isotest_host host
+echo "disable" > /proc/usbd-switch
+
+rmmod au1x00_bi
+rmmod isotest_fd
+rmmod usbdprocfs
+rmmod usbdcore
+
diff -uNr linux/drivers/no-otg/functions/isotest/iso_zero_silent linux/drivers/otg/functions/isotest/iso_zero_silent
--- linux/drivers/no-otg/functions/isotest/iso_zero_silent	1970-01-01 01:00:00.000000000 +0100
+++ linux/drivers/otg/functions/isotest/iso_zero_silent	2006-09-01 21:41:26.000000000 +0200
@@ -0,0 +1,34 @@
+#!/bin/sh
+XFER=$1
+PATTERN=$2
+if [ -z "$PATTERN" ]
+then
+	PATTERN="0x1,0x1,0x00"
+fi
+echo "Using PATTERN='$PATTERN'"
+set -x
+
+insmod  /tmp/usbdcore.o 
+insmod /tmp/usbdprocfs.o
+
+insmod  /tmp/isotest_fd.o vendor_id=0xfff0 product_id=0xfff1 in=$XFER out=$XFER xmit_pattern="$PATTERN" print_all=0 fermat=0 custom=1
+
+insmod  /tmp/au1x00_bi.o 
+echo "enable" > /proc/usbd-switch
+
+
+set +x
+echo -n "INSERT CABLE"
+sleep 15
+echo -n ";  REMOVE CABLE AND PRESS RETURN"
+read junk
+set -x
+cp /proc/isotest_fd client
+cp /proc/isotest_host host
+echo "disable" > /proc/usbd-switch
+
+rmmod au1x00_bi
+rmmod isotest_fd
+rmmod usbdprocfs
+rmmod usbdcore
+
diff -uNr linux/drivers/no-otg/functions/isotest/test.c linux/drivers/otg/functions/isotest/test.c
--- linux/drivers/no-otg/functions/isotest/test.c	1970-01-01 01:00:00.000000000 +0100
+++ linux/drivers/otg/functions/isotest/test.c	2006-09-01 21:41:27.000000000 +0200
@@ -0,0 +1,456 @@
+/*
+ * otg/isotest_fd/test.c
+ *
+ *      Copyright (c) 2003, 2004 Belcarra
+ *
+ * By: 
+ *      Stuart Lynne <sl@belcarra.com>, 
+ *      Tom Rushworth <tbr@belcarra.com>, 
+ *
+ */
+
+#include <linux/config.h>
+#include <linux/module.h>
+#include <linux/version.h>
+
+#include <linux/kernel.h>
+//#include <linux/sched.h>
+#include <linux/signal.h>
+#include <linux/errno.h>
+//#include <linux/poll.h>
+#include <linux/init.h>
+
+#include <asm/system.h>
+#include <asm/atomic.h>
+//#include <linux/interrupt.h>
+//#include <linux/pci.h>
+#include <linux/init.h>
+#include <linux/delay.h>
+
+#include <linux/proc_fs.h>
+#include <linux/vmalloc.h>
+
+#include <asm/io.h>
+#include <asm/string.h>
+
+#include <linux/proc_fs.h>
+
+#include <linux/netdevice.h>
+#include <linux/cache.h>
+
+
+#if defined(CONFIG_ARCH_SA1100) || defined (CONFIG_ARCH_PXA)
+#include <asm/dma.h>
+#include <asm/mach/dma.h>
+#include <asm/irq.h>
+#include <asm/system.h>
+#include <asm/hardware.h>
+#include <asm/types.h>
+#endif
+
+#if defined(CONFIG_MIPS_AU1000) || defined(CONFIG_MIPS_PB1500) || defined(CONFIG_MIPS_PB1100)
+#include <asm/au1000.h>
+#include <asm/au1000_dma.h>
+#include <asm/mipsregs.h>
+#endif
+
+#if defined(CONFIG_ARCH_SAMSUNG)
+#include <asm/arch/timers.h>
+#include <asm/arch/hardware.h>
+#endif
+
+#include <asm/uaccess.h>
+#include <asm/io.h>
+#include <asm/pgtable.h>
+#include <asm/pgalloc.h>
+#include "test.h"
+
+
+int iso_trace_first;
+int iso_trace_next;
+iso_trace_t *iso_traces;
+
+
+/* ******************************************************************************************* */
+
+void dump_stats(struct isotest_stats *stats)
+{
+        if (stats->errors || ((stats->ok % 100) == 99)) {
+                TRACE_ISO(stats->iso_transfer_number, stats->total_frames,
+                                stats->received, stats->errors, stats->missed, stats->skipped, stats->ok);
+        }
+        if (stats->errors) {
+                memset(stats, 0, sizeof(struct isotest_stats));
+        }
+        else {
+                stats->ok++;
+                stats->iso_transfer_number++;
+                stats->last_packet = 0;
+        }
+}
+
+u16 getu16(u8 **cp)
+{
+        u16     val = 0;
+
+        val = *(*cp)++;
+        val |= *(*cp)++ << 8;
+
+        return val;
+}
+
+u16 getu32(u8 **cp)
+{
+        u32     val = 0;
+
+        val = *(*cp)++;
+        val |= *(*cp)++ << 8;
+        val |= *(*cp)++ << 16;
+        val |= *(*cp)++ << 24;
+
+        return val;
+}
+
+static int iso_in_received;
+
+void iso_trace_recv_data (struct isotest_stats *stats, u8 *cp, int length, int framenum)
+{
+        //struct usb_device_instance *device = urb->device;
+
+        u16     iso_transfer_number;
+        u16     total_size;
+        u16     packet_size;
+        u16     total_frames;
+        u16     this_packet;
+        u8      frames;
+        
+        
+        iso_transfer_number = getu32(&cp);
+        total_size = getu16(&cp);
+        packet_size = getu16(&cp);
+        total_frames = getu16(&cp);
+        this_packet = getu16(&cp);
+
+                
+        // did we miss the end of the last ISO transfer?
+        if (stats->iso_transfer_number && (stats->iso_transfer_number != iso_transfer_number)) {
+                stats->errors++;
+                stats->missed++;
+                //printk(KERN_INFO"%s: ERROR bad iso number: %x %x %x %x %x expecting: %x\n", __FUNCTION__, iso_transfer_number,
+                //                total_size, packet_size, total_frames, this_packet, stats->iso_transfer_number);
+                dump_stats(stats);
+                return;
+        }
+                
+        if (!stats->iso_transfer_number) {
+                stats->framenum = framenum;
+                stats->iso_transfer_number = iso_transfer_number;
+                stats->total_frames = total_frames;
+        }
+
+        // is this the packet we are expecting?
+        if (stats->last_packet && ((stats->last_packet + 1) != this_packet)) {
+                stats->errors++;
+                stats->missed++;
+                //printk(KERN_INFO"%s: ERROR bad packet: %x %x %x %x %x expecting: %x\n", __FUNCTION__, iso_transfer_number,
+                //                total_size, packet_size, total_frames, this_packet, stats->last_packet + 1);
+        }
+        
+        // did we miss a frame? 
+        frames = (stats->framenum < framenum) ?  (framenum - stats->framenum) : (stats->framenum - framenum);
+        
+        if (frames > 1) {
+                stats->errors++;
+                stats->skipped++;
+                //printk(KERN_INFO"%s: SKIPPED %x %x %x %x %x\n", __FUNCTION__, iso_transfer_number,
+                //                total_size, packet_size, total_frames, this_packet);
+        }
+
+        // update stats
+        stats->received++;
+        stats->last_packet = this_packet;
+
+        // last packet?
+        if (stats->total_frames == this_packet) 
+                dump_stats(stats);
+}
+
+
+
+/* Proc Filesystem *************************************************************************** */
+        
+/* *    
+ * iso_trace_proc_read - implement proc file system read.
+ * @file        
+ * @buf         
+ * @count
+ * @pos 
+ *      
+ * Standard proc file system read function.
+ */         
+static ssize_t iso_trace_proc_read (struct file *file, char *buf, size_t count, loff_t * pos)
+{                                  
+        unsigned long page;
+        int len = 0;
+        int index;
+        int oindex;
+        int previous;
+
+        MOD_INC_USE_COUNT;
+        //printk(KERN_INFO"%s: GET_USE_COUNT: %d\n", __FUNCTION__, GET_USE_COUNT(THIS_MODULE));
+        // get a page, max 4095 bytes of data...
+        if (!(page = get_free_page (GFP_KERNEL))) {
+                MOD_DEC_USE_COUNT;
+                //printk(KERN_INFO"%s: GET_USE_COUNT: %d\n", __FUNCTION__, GET_USE_COUNT(THIS_MODULE));
+                return -ENOMEM;
+        }
+
+        len = 0;
+        oindex = index = (*pos)++;
+
+        if (index == 0) {
+              //len += sprintf ((char *) page + len, "      uS              Exp  Rcv   Err  Miss  Skip  OK\n");
+              //len += sprintf ((char *) page + len, "      uS            Exp  Rcv   Err  Miss  Skip  Xfers\n");
+                len += sprintf ((char *) page + len, "      uS           Tr   Exp   Rcv   Err  Miss  Skip  Xfers\n");
+
+//      uS            Exp  Rcv   Err  Miss  Skip  Xfers
+//      uS          Tr     Exp  Rcv   Err  Miss  Skip  Xfers
+//        0  |      100     9   900     0     0     0    99
+
+        }       
+                         
+        index += iso_trace_first;
+        if (index >= TRACE_MAX) {
+                index -= TRACE_MAX;
+        }
+        previous = (index) ? (index - 1) : (TRACE_MAX - 1);
+
+        //printk(KERN_INFO"first: %d next: %d index: %d %d prev: %d\n", iso_trace_first, iso_trace_next, oindex, index, previous);
+
+        if (
+                        ((iso_trace_first < iso_trace_next) && (index >= iso_trace_first) && (index < iso_trace_next)) ||
+                        ((iso_trace_first > iso_trace_next) && ((index < iso_trace_next) || (index >= iso_trace_first)))
+           )
+        {
+
+#if defined(CONFIG_ARCH_SA1100) || defined (CONFIG_ARCH_PXA) || defined(CONFIG_MIPS_AU1000)  || defined(CONFIG_MIPS_PB1500) || defined(CONFIG_MIPS_PB1100)
+                u32 ticks = 0;
+#elif defined(CONFIG_ARCH_SAMSUNG)
+                u32 ticks = 0;
+#else
+                u64 jifs = 0;
+#endif
+                iso_trace_t *p = iso_traces + index;
+                //unsigned char *cp;
+                //int skip = 0;
+
+                if (oindex > 0) {
+                        iso_trace_t *o = iso_traces + previous;
+
+#if defined(CONFIG_ARCH_SA1100) || defined (CONFIG_ARCH_PXA)
+                        /*
+                         * oscr is 3.6864 Mhz free running counter, 
+                         *
+                         *      1/3.6864 = .2712
+                         *      60/221   = .2714
+                         *
+                         */
+                        if (o->ocsr) {
+                                ticks = (p->ocsr > o->ocsr) ? (p->ocsr - o->ocsr) : (o->ocsr - p->ocsr) ;
+                                ticks = (ticks * 60) / 221;
+                        }
+
+#elif defined(CONFIG_MIPS_AU1000)  || defined(CONFIG_MIPS_PB1500) || defined(CONFIG_MIPS_PB1100)
+                        /*
+                         * cp0_count is incrementing timer at system clock
+                         */
+                        if (o->cp0_count) {
+                                //ticks = (p->cp0_count > o->cp0_count) ? 
+                                //        (p->cp0_count - o->cp0_count) : (o->cp0_count - p->cp0_count) ;
+                                ticks = (p->cp0_count - o->cp0_count) ;
+                                ticks = ticks / CONFIG_OTG_AU1X00_SCLOCK;
+                        }
+
+#elif defined(CONFIG_ARCH_SAMSUNG)
+                        /*
+                         * tcnt1 is a count-down timer running at the system bus clock
+                         * The divisor must be set as a configuration value, typically 66 or 133.
+                         */
+                        if (o->tcnt1) {
+                                ticks = (p->tcnt1 < o->tcnt1) ?  (o->tcnt1 - p->tcnt1) : (p->tcnt1 - o->tcnt1) ;
+                                ticks /= CONFIG_OTG_SMDK2500_BCLOCK;
+                        }
+#else
+                        if (o->jiffies) {
+                                jifs = p->jiffies - iso_traces[previous].jiffies;
+                        }
+#endif
+
+                        //if (o->interrupts != p->interrupts) {
+                        //        skip++;
+                        //}
+                }
+                
+
+#if defined(CONFIG_ARCH_SA1100) || defined (CONFIG_ARCH_PXA) || defined(CONFIG_MIPS_AU1000) || defined(CONFIG_MIPS_PB1500) || defined(CONFIG_MIPS_PB1100)
+                len += sprintf ((char *) page + len, "%9d  ", ticks);
+#elif defined(CONFIG_ARCH_SAMSUNG)
+                //len += sprintf ((char *) page + len, "%8u ", p->jiffies);
+                //len += sprintf ((char *) page + len, "%8u ", p->tcnt0);
+                len += sprintf ((char *) page + len, "%8u ", p->tcnt1);
+                if (ticks > 1024*1024) {
+                        len += sprintf ((char *) page + len, "%8dM ", ticks>>20);
+                }
+                else {
+                        len += sprintf ((char *) page + len, "%8d  ", ticks);
+                }
+#else
+                if (jifs > 1024) {
+                        len += sprintf ((char *) page + len, "%4dK", (int)jifs>>20);
+                }
+                else {
+                        len += sprintf ((char *) page + len, "%4d  ", (int)jifs);
+                }
+#endif
+
+                len += sprintf ((char *) page + len, "| %8d", p->iso_transfer_number);
+                len += sprintf ((char *) page + len, "%6d", p->expected);
+                len += sprintf ((char *) page + len, "%6d", p->received);
+                len += sprintf ((char *) page + len, "%6d", p->errors);
+                len += sprintf ((char *) page + len, "%6d", p->missed);
+                len += sprintf ((char *) page + len, "%6d", p->skipped);
+                len += sprintf ((char *) page + len, "%6d", p->ok);
+                len += sprintf ((char *) page + len, "\n");
+        }
+
+#if 1
+        if ((len > count) ) {
+                printk(KERN_INFO"%s: ((len > count)  count=%d len=%d\n", __FUNCTION__, count, len);
+                len = -EINVAL;
+        } 
+
+#else
+        if ((len > count) || (len == 0)) {
+                printk(KERN_INFO"%s: ((len > count) || (len == 0)) count=%d len=%d\n", __FUNCTION__, count, len);
+                len = -EINVAL;
+        } 
+#endif
+        else if (len > 0 && copy_to_user (buf, (char *) page, len)) {
+                printk(KERN_INFO"%s: (len > 0 && copy_to_user (buf, (char *) page, len) \n", __FUNCTION__);
+                len = -EFAULT;
+        }
+        free_page (page);
+        MOD_DEC_USE_COUNT;
+        //printk(KERN_INFO"%s: GET_USE_COUNT: %d\n", __FUNCTION__, GET_USE_COUNT(THIS_MODULE));
+        return len;
+}
+
+void iso_start_in(int count);
+void iso_start_out(int count);
+
+/* *
+ * iso_trace_proc_write - implement proc file system write.
+ * @file
+ * @buf
+ * @count
+ * @pos
+ *
+ * Proc file system write function, used to signal monitor actions complete.
+ * (Hotplug script (or whatever) writes to the file to signal the completion
+ * of the script.)  An ugly hack.
+ */
+static ssize_t iso_trace_proc_write (struct file *file, const char *buf, size_t count, loff_t * pos)
+{
+        //struct usb_device_instance *device;
+        size_t n = count;
+        char command[64];
+        char *cp = command;
+        int i = 0;
+
+        MOD_INC_USE_COUNT;
+        //printk(KERN_INFO"%s: GET_USE_COUNT: %d\n", __FUNCTION__, GET_USE_COUNT(THIS_MODULE));
+        //printk(KERN_DEBUG "%s: count=%u\n",__FUNCTION__,count);
+        while ((n > 0) && (i < 64)) {
+                // Not too efficient, but it shouldn't matter
+                if (copy_from_user (cp++, buf + (count - n), 1)) {
+                        count = -EFAULT;
+                        break;
+                }
+                *cp = '\0';
+                i++;
+                n -= 1;
+                //printk(KERN_DEBUG "%s: %u/%u %02x\n",__FUNCTION__,count-n,count,c);
+        }
+        if (!strncmp (command, "in", 2)) {
+                iso_start_in (10);
+        } 
+        else if (!strncmp (command, "out", 3)) {
+                iso_start_out (10);
+        } 
+        // XXX need to be able to set serial number here
+        MOD_DEC_USE_COUNT;
+        //printk(KERN_INFO"%s: GET_USE_COUNT: %d\n", __FUNCTION__, GET_USE_COUNT(THIS_MODULE));
+        return (count);
+}
+
+static struct file_operations iso_trace_proc_operations_functions = {
+read:iso_trace_proc_read,
+write:iso_trace_proc_write,
+};
+
+#if defined(CONFIG_ARCH_SAMSUNG)
+#endif
+
+/**
+ * iso_trace_init
+ *
+ * Return non-zero if not successful.
+ */
+int iso_trace_init (char *name)
+{
+        printk(KERN_INFO"%s:\n", __FUNCTION__);
+        if (!(iso_traces = vmalloc(sizeof(iso_trace_t) * TRACE_MAX))) {
+                printk(KERN_ERR"%s: malloc failed %p %d\n", __FUNCTION__, iso_traces, sizeof(iso_trace_t) * TRACE_MAX);
+                return -EINVAL;
+        }
+        memset(iso_traces, 0, sizeof(iso_trace_t) * TRACE_MAX);
+
+        {
+                struct proc_dir_entry *p;
+
+                // create proc filesystem entries
+                if ((p = create_proc_entry (name, 0, 0)) == NULL) {
+                        printk(KERN_INFO"BITRACE PROC FS failed\n");
+                }
+                else {
+                        p->proc_fops = &iso_trace_proc_operations_functions;
+                }
+        }
+#if defined(CONFIG_ARCH_SAMSUNG)
+        *(volatile u32 *)TMOD |= 0x3 << 3;
+#endif
+        printk(KERN_INFO"%s: OK\n", __FUNCTION__);
+        return 0;
+}
+
+/**
+ * udc_release_io - release UDC io region
+ */
+void iso_trace_exit (char *name)
+{
+        {
+                unsigned long flags;
+                local_irq_save (flags);
+                remove_proc_entry (name, NULL);
+                if (iso_traces) {
+                        iso_trace_t *p = iso_traces;
+                        iso_traces = 0;
+                        vfree(p);
+                }
+                local_irq_restore (flags);
+        }
+}
+
+
+/* End of FILE */
+
diff -uNr linux/drivers/no-otg/functions/isotest/test.h linux/drivers/otg/functions/isotest/test.h
--- linux/drivers/no-otg/functions/isotest/test.h	1970-01-01 01:00:00.000000000 +0100
+++ linux/drivers/otg/functions/isotest/test.h	2006-09-01 21:41:27.000000000 +0200
@@ -0,0 +1,129 @@
+/*
+ * otg/isotest_fd/test.h
+ *
+ *      Copyright (c) 2003, 2004 Belcarra
+ *
+ * By: 
+ *      Stuart Lynne <sl@belcarra.com>, 
+ *      Bruce Balden <balden@belcarra.com>, 
+ *      Tom Rushworth <tbr@belcarra.com>, 
+ *
+ */
+
+#if defined(CONFIG_ARCH_SAMSUNG)
+#ifndef CONFIG_OTG_SMDK2500_BCLOCK 
+#define CONFIG_OTG_SMDK2500_BCLOCK 66
+#endif
+#endif
+
+#define NUSBD_FRAMENUM             0xB0200038
+
+
+struct isotest_stats {
+
+        u16     count;
+
+        u16     iso_transfer_number;
+        u16     total_size;
+        u16     packet_size;
+        u16     total_frames;
+        u16     last_packet;
+
+
+        u16     framenum;
+        u16     received;
+        u16     missed;
+        u16     skipped;
+        u16     errors;
+
+        u32     ok;
+
+};
+
+
+
+
+
+typedef struct iso_trace_types {
+#if defined(CONFIG_ARCH_SA1100) || defined (CONFIG_ARCH_PXA)
+        u32     ocsr;
+#elif defined(CONFIG_MIPS_AU1000) || defined(CONFIG_MIPS_PB1500) || defined(CONFIG_MIPS_PB1100)
+        u32     cp0_count;
+#elif defined(CONFIG_ARCH_SAMSUNG)
+        u32     tcnt1;
+#else
+        u64     jiffies;
+#endif
+        u16     iso_transfer_number;
+
+        u16     expected;
+        u16     received;
+        u16     errors;
+        u16     missed;
+        u16     skipped;
+
+        u32     ok;
+
+} iso_trace_t;
+
+
+#define TRACE_MAX       3000
+
+extern int iso_trace_first;
+extern int iso_trace_next;
+
+extern iso_trace_t *iso_traces;
+
+static __inline__ iso_trace_t *ISO_TRACE_NEXT(void)
+{
+        iso_trace_t *p;
+
+        p = iso_traces + iso_trace_next;
+
+#if defined(CONFIG_ARCH_SA1100) || defined (CONFIG_ARCH_PXA)
+        p->ocsr = OSCR;
+#elif defined(CONFIG_MIPS_AU1000) || defined(CONFIG_MIPS_PB1500) || defined(CONFIG_MIPS_PB1100)
+        p->cp0_count = read_c0_count();
+#elif defined(CONFIG_ARCH_SAMSUNG)
+        p->tcnt1 = *(volatile u32 *)TCNT1;
+#else
+        p->jiffies = jiffies;
+#endif
+//#if defined(CONFIG_MIPS_AU1000) || defined(CONFIG_MIPS_PB1500) || defined(CONFIG_MIPS_PB1100)
+//        p->iso_transfer_number = au_readl(NUSBD_FRAMENUM);
+//#endif
+
+        iso_trace_next++;
+        iso_trace_next = (iso_trace_next == TRACE_MAX) ? 0 : iso_trace_next;
+
+        if (iso_trace_next == iso_trace_first) {
+                iso_trace_first++;
+                iso_trace_first = (iso_trace_first == TRACE_MAX) ? 0 : iso_trace_first;
+        }
+
+        return p;
+}
+
+static __inline__ void TRACE_ISO(u16 iso_transfer_number, u16 expected, u16 received, u16 errors, u16 missed, u16 skipped, u32 ok)
+{
+        iso_trace_t *p = NULL;
+
+        //printk(KERN_INFO"%s: %d %d %d %d %d first: %d next: %d traces: %p\n", __FUNCTION__, 
+        //                expected, received, errors, missed, skipped, iso_trace_first, iso_trace_next, traces);
+
+        if (!iso_traces) return;
+
+        p = ISO_TRACE_NEXT();
+        p->iso_transfer_number = iso_transfer_number;
+        p->expected = expected;
+        p->received = received;
+        p->errors = errors;
+        p->missed = missed;
+        p->skipped = skipped;
+        p->ok = ok;
+}
+
+void iso_trace_recv_data (struct isotest_stats *stats, u8 *cp, int length, int framenum);
+int iso_trace_init (char *str);
+void iso_trace_exit (char *str);
+
diff -uNr linux/drivers/no-otg/functions/mouse/Config.in linux/drivers/otg/functions/mouse/Config.in
--- linux/drivers/no-otg/functions/mouse/Config.in	1970-01-01 01:00:00.000000000 +0100
+++ linux/drivers/otg/functions/mouse/Config.in	2006-09-01 21:41:27.000000000 +0200
@@ -0,0 +1,28 @@
+#
+# Mouse Function Driver
+#
+# Copyright (C) 2003 Belcarra
+#
+
+
+mainmenu_option next_comment
+
+comment "USB Peripheral Function Driver - Random Mouse"
+dep_tristate '  Mouse Function' CONFIG_OTG_MOUSE $CONFIG_OTG
+
+if [ "$CONFIG_OTG_MOUSE" != "n" ]; then
+   hex    'VendorID (hex value)' CONFIG_OTG_MOUSE_VENDORID "15ec"
+   hex    'ProductID (hex value)' CONFIG_OTG_MOUSE_PRODUCTID "f003"
+   hex    'bcdDevice (binary-coded decimal)' CONFIG_OTG_MOUSE_BCDDEVICE "0100"
+
+   string 'iManufacturer (string)' CONFIG_OTG_MOUSE_MANUFACTURER "Belcarra"
+   #string 'iProduct (string)' CONFIG_OTG_MOUSE_PRODUCT_NAME "Belcarra Mouse"
+
+   string 'iConfiguration (string)' CONFIG_OTG_MOUSE_DESC "Mouse Cfg"
+   string 'Comm Interface iInterface (string)' CONFIG_OTG_MOUSE_COMM_INTF "Comm Intf"
+   bool   'Mouse BH Test'  CONFIG_OTG_MOUSE_BH
+   int    'Number of packets (zero is continous ' CONFIG_OTG_MOUSE_PACKETS "10"
+   int    'Polling Interval ' CONFIG_OTG_MOUSE_INTERVAL "1"
+
+fi
+endmenu
diff -uNr linux/drivers/no-otg/functions/mouse/Kconfig linux/drivers/otg/functions/mouse/Kconfig
--- linux/drivers/no-otg/functions/mouse/Kconfig	1970-01-01 01:00:00.000000000 +0100
+++ linux/drivers/otg/functions/mouse/Kconfig	2006-09-01 21:41:27.000000000 +0200
@@ -0,0 +1,74 @@
+menu "OTG Random Mouse function"
+	depends on OTG
+
+config OTG_MOUSE
+        tristate "  Random Mouse Function"
+        depends on OTG
+	---help---
+        This implements a simple Mouse driver that sends HID packets with
+        small random movements. This is a good function for initial testing
+        of Peripheral Controller Drivers as it provides for a complicated
+        enumeration but a simple uni-directional (send Interrupt only) data
+        transport.
+
+menu "OTG Random Mouse function options"
+	depends on OTG && OTG_MOUSE
+
+config OTG_MOUSE_VENDORID
+        hex "VendorID (hex value)"
+        depends on OTG && OTG_MOUSE
+        default "0x15ec"
+
+config OTG_MOUSE_PRODUCTID
+        hex "ProductID (hex value)"
+        depends on OTG && OTG_MOUSE
+        default "0xf003"
+
+config OTG_MOUSE_BCDDEVICE
+        hex "bcdDevice (binary-coded decimal)"
+        depends on OTG && OTG_MOUSE
+        default "0x0100"
+
+config OTG_MOUSE_MANUFACTURER
+        string "iManufacturer (string)"
+        depends on OTG && OTG_MOUSE
+        default "Belcarra"
+
+config OTG_MOUSE_PRODUCT_NAME
+        string "iProduct (string)"
+        depends on OTG && OTG_MOUSE
+        default "Random Mouse Class - Interrupt"
+
+config OTG_MOUSE_COMM_INTF
+        string "MOUSE Bulk Only iInterface (string)"
+        depends on OTG && OTG_MOUSE
+        default "MOUSE Data Intf"
+
+config OTG_MOUSE_DESC
+        string "Data Interface iConfiguration (string)"
+        depends on OTG && OTG_MOUSE
+        default "MOUSE Configuration"
+
+config OTG_MOUSE_BH
+        bool "  MOUSE BH Test"
+        depends on OTG && OTG_MOUSE
+        default n
+	---help---
+        Implement the Mouse send packet in a bottom half handler,
+        this will delay responses to test the PCD works correctly
+        when the host polls before a send data urb is queued.
+
+
+config OTG_MOUSE_PACKETS
+        int "Number of packets (zero is continous)"
+        depends on OTG && OTG_MOUSE
+        default 10
+	---help---
+        Number of Mouse packets to send, will run
+        forever if set to zero.
+
+
+endmenu
+
+
+endmenu
diff -uNr linux/drivers/no-otg/functions/mouse/Makefile linux/drivers/otg/functions/mouse/Makefile
--- linux/drivers/no-otg/functions/mouse/Makefile	1970-01-01 01:00:00.000000000 +0100
+++ linux/drivers/otg/functions/mouse/Makefile	2006-09-01 21:41:27.000000000 +0200
@@ -0,0 +1,81 @@
+#
+# Function driver for a Mouse USB Device
+#
+# Copyright (c) 2003 Belcarra
+
+# Multipart objects.
+
+O_TARGET	:= mouse_target.o
+#list-multi	:= mouse_fd.o cmouse_fd.o
+list-multi     := mouse_fd.o
+
+mouse_fd-objs	:= mouse-fd.o mouse-l24.o
+#cmouse_fd-objs	:= mouse-cf.o mouse-if.o cmouse-l24.o
+#cmouse_fd-objs := mouse-if.o cmouse-l24.o
+
+# Objects that export symbols.
+#export-objs	:= mouse.o
+
+# Object file lists.
+
+obj-y	:=
+obj-m	:=
+obj-n	:=
+obj-	:=
+
+# Each configuration option enables a list of files.
+
+obj-$(CONFIG_OTG_MOUSE)   += mouse_fd.o
+#obj-$(CONFIG_OTG_MOUSE)   += cmouse_fd.o
+
+# Extract lists of the multi-part drivers.
+# The 'int-*' lists are the intermediate files used to build the multi's.
+
+multi-y		:= $(filter $(list-multi), $(obj-y))
+multi-m		:= $(filter $(list-multi), $(obj-m))
+int-y		:= $(sort $(foreach m, $(multi-y), $($(basename $(m))-objs)))
+int-m		:= $(sort $(foreach m, $(multi-m), $($(basename $(m))-objs)))
+
+# Files that are both resident and modular: remove from modular.
+
+obj-m		:= $(filter-out $(obj-y), $(obj-m))
+int-m		:= $(filter-out $(int-y), $(int-m))
+
+# Translate to Rules.make lists.
+
+O_OBJS		:= $(filter-out $(export-objs), $(obj-y))
+OX_OBJS		:= $(filter     $(export-objs), $(obj-y))
+M_OBJS		:= $(sort $(filter-out $(export-objs), $(obj-m)))
+MX_OBJS		:= $(sort $(filter     $(export-objs), $(obj-m)))
+MI_OBJS		:= $(sort $(filter-out $(export-objs), $(int-m)))
+MIX_OBJS	:= $(sort $(filter     $(export-objs), $(int-m)))
+
+# The global Rules.make.
+
+#MOUSED=$(OTG)/functions/mouse
+
+OTG_WARNINGS=-Wno-unused -Wno-format
+OTG=$(TOPDIR)/drivers/otg
+OTG_INCLUDES=-I$(OTG)
+OTG_EXTRA=$(OTG_INCLUDES) $(OTG_WARNINGS)
+OTGCORE_DIR=$(OTG)/otgcore
+##USBDCORE_DIR=$(OTG)/usbdcore
+include $(TOPDIR)/Rules.make
+#EXTRA_CFLAGS +=  -I$(OTG) -Wno-unused -Wno-format  -I$(USBDCORE_DIR) -I$(OTGCORE_DIR)
+#EXTRA_CFLAGS_nostdinc += -I$(OTG) -Wno-unused -Wno-format  -I$(USBDCORE_DIR) -I$(OTGCORE_DIR)
+EXTRA_CFLAGS += $(OTG_EXTRA) 
+EXTRA_CFLAGS_nostdinc += $(OTG_EXTRA)
+
+# Link rules for multi-part drivers.
+
+mouse_fd.o: $(mouse_fd-objs)
+	$(LD) -r -o $@ $(mouse_fd-objs)
+
+#cmouse_fd.o: $(cmouse_fd-objs)
+#	$(LD) -r -o $@ $(cmouse_fd-objs)
+
+# dependencies:
+
+#mouse.o: $(USBDCORE_DIR)/usbd.h $(USBDCORE_DIR)/usbd-bus.h $(USBDCORE_DIR)/usbd-func.h
+
+
diff -uNr linux/drivers/no-otg/functions/mouse/Makefile-l26 linux/drivers/otg/functions/mouse/Makefile-l26
--- linux/drivers/no-otg/functions/mouse/Makefile-l26	1970-01-01 01:00:00.000000000 +0100
+++ linux/drivers/otg/functions/mouse/Makefile-l26	2006-09-01 21:41:27.000000000 +0200
@@ -0,0 +1,14 @@
+# Function driver for a Random Mouse
+#
+# Copyright (c) 2004 Belcarra
+
+mouse_fd-objs	:= mouse-fd.o mouse-l24.o
+
+obj-$(CONFIG_OTG_MOUSE) += mouse_fd.o
+
+OTG=$(TOPDIR)/drivers/otg
+ACMD=$(OTG)/functions/mouse
+OTGCORE_DIR=$(OTG)/otgcore
+USBDCORE_DIR=$(OTG)/usbdcore
+EXTRA_CFLAGS += -I$(ACMD) -I$(OTG) -Wno-unused -Wno-format  -I$(USBDCORE_DIR) -I$(OTGCORE_DIR)
+EXTRA_CFLAGS_nostdinc += -I$(ACMD) -I$(OTG) -Wno-unused -Wno-format  -I$(USBDCORE_DIR) -I$(OTGCORE_DIR)
diff -uNr linux/drivers/no-otg/functions/mouse/cmouse-l24.c linux/drivers/otg/functions/mouse/cmouse-l24.c
--- linux/drivers/no-otg/functions/mouse/cmouse-l24.c	1970-01-01 01:00:00.000000000 +0100
+++ linux/drivers/otg/functions/mouse/cmouse-l24.c	2006-09-01 21:41:27.000000000 +0200
@@ -0,0 +1,199 @@
+/*
+ * otg/functions/mouse/cmouse-l24.c
+ *
+ *      Copyright (c) 2003, 2004 Belcarra
+ *
+ * By: 
+ *      Stuart Lynne <sl@belcarra.com>, 
+ *      Bruce Balden <balden@belcarra.com>
+ *
+ */
+/*!
+ * @file otg/functions/mouse/cmouse-l24.c
+ * @brief Mouse Function Driver Linux 2.4 initialization
+ *
+ *
+ * This file contains the Linux 2.4 module init and exit functions
+ * to load and unload the Random Mouse Function.
+ *
+ * Notes
+ *
+ * This driver does not export any upper edge functionality to
+ * for the user or applications to use. It simply sends a configurable
+ * number of data packets to the USB Host when configured. Each data
+ * packet contains the equivalent of a small mouse report which will
+ * cause the mouse cursor to move on the host.
+ *
+ * To use simply load with something like:
+ *
+ *      insmod cmouse_fd.o vendor_id=0xffff product_id=0xffff
+ *
+ * And attach to a Windows box. Windows should recognize as a mouse and
+ * immediately start receiving a stream of data. 
+ *
+ * To terminate simply unplug.
+ *
+ *
+ * 1. The mouse driver is product name is hard coded to a string that will
+ *    generate a string descriptor that is 32 bytes long. This will test
+ *    most UDC's ep0 ZLP handling as it is a multiple of the most common
+ *    UDC endpoint zero size. (An option can be added later to allow for
+ *    64 byte ep0 packetsize.)
+ *
+ * 2. The CONFIG_OTG_MOUSE_BH option can be enabled to delay the HID report 
+ *    to being generated by a bottom half handler. This will verify that 
+ *    the bus interface driver properly handles the case of a delayed
+ *    CONTROL READ. I.e. when the usbd_device_request() function returns
+ *    zero for successful completion but there is no tx_urb containing the
+ *    requested data. The bus interface driver must setup the conditions for
+ *    ACK'ing the SETUP packet but then NAK the IN data for endpoint zero
+ *    until the tx_urb is started later.
+ *
+ * 3. The CONFIG_OTG_MOUSE_PACKETS option is used to set the number of
+ *    mouse data reports to send. Set to zero for a continous stream
+ *    of data.
+ *
+ * @ingroup MouseFunction
+ */
+
+#include <otg/otg-compat.h>
+#include <otg/otg-module.h>
+
+#include <otg/usbp-chap9.h>
+#include <otg/usbp-func.h>
+#include <otg/otg-trace.h>
+
+#include "cmouse.h"
+
+
+/* Module Parameters ******************************************************** */
+/* ! 
+ * @name XXXXX MODULE Parameters
+ */
+/* ! @{ */
+
+MOD_AUTHOR ("sl@belcarra.com"); 
+EMBED_LICENSE();
+MOD_DESCRIPTION ("Belcarra Random Walk MOUSE Function");
+
+static u32 vendor_id;           /*!< override built-in vendor ID */
+static u32 product_id;          /*!< override built-in product ID */
+
+MOD_PARM (vendor_id, "i");
+MOD_PARM (product_id, "i");
+
+MOD_PARM_DESC (vendor_id, "Device Vendor ID");
+MOD_PARM_DESC (product_id, "Device Product ID");
+
+otg_tag_t MOUSE;
+/* ! *} */
+
+/* USB Module init/exit ***************************************************** */
+
+struct mouse_private mouse_private;
+
+char *mouse_interface_list[2] = {
+        "mouse-random-interface",
+        NULL,
+};
+
+/*! 
+ * mouse_modinit() - module init
+ *
+ * This is called by the Linux kernel; either when the module is loaded
+ * if compiled as a module, or during the system intialization if the 
+ * driver is linked into the kernel.
+ *
+ * This function will parse module parameters if required and then register
+ * the mouse driver with the USB Device software.
+ *
+ * If the CONFIG_OTG_MOUSE_BH option is enabled it will also setup the mouse
+ * bottom half handler.
+ *
+ */
+static int mouse_modinit (void)
+{
+        struct usbd_function_instance *mfi;
+        printk (KERN_INFO "%s: vendor_id: %04x product_id: %04x\n", __FUNCTION__, vendor_id, product_id);
+
+        #if !defined(OTG_C99)
+        mouse_global_init();
+        mouse_ops_init();
+        #endif /* defined(OTG_C99) */
+        
+        MOUSE = otg_trace_obtain_tag();
+        TRACE_MSG2(MOUSE, "vendor_id: %04x product_id: %04x",vendor_id, product_id);
+
+        if (vendor_id) 
+                mouse_composite_driver.idVendor = cpu_to_le16(vendor_id);
+        if (product_id) 
+                mouse_composite_driver.idProduct = cpu_to_le16(product_id);
+
+        mouse_hid.wItemLength = cpu_to_le16(0x34);      // XXX mips compiler bug.....
+
+        // register as usb function driver
+        TRACE_MSG2(MOUSE, "%s %d", mouse_interface_driver.name, mouse_interface_driver.endpointsRequested);
+        THROW_UNLESS ((mouse_private.interface = 
+                                usbd_register_interface (&mouse_interface_driver, "mouse-random-interface", NULL)), error);
+
+        TRACE_MSG2(MOUSE, "%s %d", mouse_interface_driver.name, mouse_interface_driver.endpointsRequested);
+
+        THROW_UNLESS ((mouse_private.composite = 
+                                usbd_register_composite (&mouse_composite_driver, "mouse-random-cf", 
+                                        mouse_interface_list, NULL)), error);
+
+        #ifdef CONFIG_OTG_MOUSE_BH
+        mouse_private.notification_bh.routine = mouse_hid_bh;
+        mouse_private.notification_bh.data = NULL;
+        #endif
+        CATCH(error) {
+                if (mouse_private.interface) {
+                        usbd_deregister_function (mouse_private.interface);
+                        mouse_private.interface = NULL;
+                }
+                if (mouse_private.composite) {
+                        usbd_deregister_function (mouse_private.composite);
+                        mouse_private.composite = NULL;
+                }
+                otg_trace_invalidate_tag(MOUSE);
+                return -EINVAL;
+        }
+        return 0;
+}
+
+module_init (mouse_modinit);
+
+#if OTG_EPILOGUE
+/*! 
+ * mouse_modexit() - module init
+ *
+ * This is called by the Linux kernel; when the module is being unloaded 
+ * if compiled as a module. This function is never called if the 
+ * driver is linked into the kernel.
+ *
+ * @param void
+ * @return void
+ */
+static void mouse_modexit (void)
+{
+        #ifdef CONFIG_OTG_MOUSE_BH
+        while (PENDING_WORK_ITEM(mouse_private.notification_bh)) {
+                printk(KERN_ERR"%s: waiting for bh\n", __FUNCTION__);
+                schedule_timeout(10 * HZ);
+        }
+        #endif
+        if (mouse_private.interface) {
+                usbd_deregister_function (mouse_private.interface);
+                mouse_private.interface = NULL;
+        }
+        if (mouse_private.composite) {
+                usbd_deregister_function (mouse_private.composite);
+                mouse_private.composite = NULL;
+        }
+
+        otg_trace_invalidate_tag(MOUSE);
+}
+
+module_exit (mouse_modexit);
+#endif
+
diff -uNr linux/drivers/no-otg/functions/mouse/cmouse.h linux/drivers/otg/functions/mouse/cmouse.h
--- linux/drivers/no-otg/functions/mouse/cmouse.h	1970-01-01 01:00:00.000000000 +0100
+++ linux/drivers/otg/functions/mouse/cmouse.h	2006-09-01 21:41:27.000000000 +0200
@@ -0,0 +1,64 @@
+/*
+ * otg/functions/mouse/mouse.h
+ *
+ *      Copyright (c) 2003, 2004 Belcarra
+ *
+ * By: 
+ *      Stuart Lynne <sl@belcarra.com>, 
+ *      Tom Rushworth <tbr@belcarra.com>, 
+ *      Bruce Balden <balden@belcarra.com>
+ *
+ */
+
+/*!
+ * @defgroup MouseFunction Random Mouse
+ * @ingroup functiongroup
+ */
+/*!
+ * @file otg/functions/mouse/mouse.h
+ * @brief Mouse Function Driver private defines
+ *
+ *
+ * This is a test USB HID Function Driver designed to test and
+ * verify that INTERRUPT IN endpoints work properly.
+ *
+ *
+ * @ingroup MouseFunction
+ */
+
+
+/*!
+ * The mouse_private structure is used to collect all of the mouse driver 
+ * global variables into one place.
+ */
+struct mouse_private {
+        struct usbd_function_instance *interface; /*!< function instance for this module */
+        struct usbd_function_instance *composite; /*!< function instance for this module */
+
+#ifdef CONFIG_OTG_MOUSE_BH
+        struct WORK_STRUCT notification_bh;
+#endif /* CONFIG_OTG_MOUSE_BH */
+        int usb_driver_registered;              /*!< non-zero if usb function registered */
+        unsigned char connected;                /*!< non-zero if connected to host (configured) */
+        unsigned int writesize;                 /*!< packetsize * 4 */
+        struct usbd_urb *tx_urb;                /*!< saved copy of current tx urb */
+        int wLength;                            
+        int x;
+        int y;
+        int last_x;
+        int last_y;
+        int n;
+};
+
+extern struct mouse_private mouse_private;
+extern struct usbd_function_driver mouse_interface_driver;
+extern struct usbd_function_driver mouse_composite_driver;
+extern struct hid_descriptor mouse_hid;
+
+#ifndef OTG_C99
+extern void mouse_global_init(void);
+#endif /* OTG_C99 */
+
+#define MOUSE mouse_trace_tag
+extern otg_tag_t MOUSE;
+
diff -uNr linux/drivers/no-otg/functions/mouse/getmouse.c linux/drivers/otg/functions/mouse/getmouse.c
--- linux/drivers/no-otg/functions/mouse/getmouse.c	1970-01-01 01:00:00.000000000 +0100
+++ linux/drivers/otg/functions/mouse/getmouse.c	2006-09-01 21:41:27.000000000 +0200
@@ -0,0 +1,107 @@
+/*
+ *
+ *
+ * 3.2. Non-Canonical Input Processing
+ * 
+ * In non-canonical input processing mode, input is not assembled into lines and
+ * input processing (erase, kill, delete, etc.) does not occur. Two parameters
+ * control the behavior of this mode: c_cc[VTIME] sets the character timer, and
+ * c_cc[VMIN] sets the minimum number of characters to receive before satisfying
+ * the read.
+ * 
+ * If MIN > 0 and TIME = 0, MIN sets the number of characters to receive before
+ * the read is satisfied. As TIME is zero, the timer is not used.
+ * 
+ * If MIN = 0 and TIME > 0, TIME serves as a timeout value. The read will be
+ * satisfied if a single character is read, or TIME is exceeded (t = TIME *0.1
+ * s). If TIME is exceeded, no character will be returned.
+ * 
+ * If MIN > 0 and TIME > 0, TIME serves as an inter-character timer. The read
+ * will be satisfied if MIN characters are received, or the time between two
+ * characters exceeds TIME. The timer is restarted every time a character is
+ * received and only becomes active after the first character has been received.
+ * 
+ * If MIN = 0 and TIME = 0, read will be satisfied immediately. The number of
+ * characters currently available, or the number of characters requested will be
+ * returned. According to Antonino (see contributions), you could issue a fcntl
+ * (fd, F_SETFL, FNDELAY); before reading to get the same result.
+ * 
+ * By modifying newtio.c_cc[VTIME] and newtio.c_cc[VMIN] all modes described
+ * above can be tested.
+ * 
+ */
+
+#include <sys/types.h>                                                    
+#include <sys/stat.h>                                                     
+#include <fcntl.h>                                                        
+#include <termios.h>                                                      
+#include <stdio.h>                                                        
+
+#define BAUDRATE B1200                                                   
+#define MODEMDEVICE "/dev/ttyS0"                                          
+#define _POSIX_SOURCE 1 /* POSIX compliant source */                      
+#define FALSE 0                                                           
+#define TRUE 1                                                            
+
+volatile int STOP=FALSE;                                                  
+
+main()                                                                    
+{                                                                         
+        int fd,c, res;                                                          
+        struct termios oldtio,newtio;                                           
+        unsigned char buf[255];                                                          
+
+        int bytes;
+        unsigned char mouse[3];
+
+        fd = open(MODEMDEVICE, O_RDWR | O_NOCTTY );                             
+        if (fd <0) {perror(MODEMDEVICE); exit(-1); }                            
+
+        tcgetattr(fd,&oldtio); /* save current port settings */                 
+
+        bzero(&newtio, sizeof(newtio));                                         
+        newtio.c_cflag = BAUDRATE | CRTSCTS | CS8 | CLOCAL | CREAD;             
+        newtio.c_iflag = IGNPAR;                                                
+        newtio.c_oflag = 0;                                                     
+
+        /* set input mode (non-canonical, no echo,...) */                       
+        newtio.c_lflag = 0;                                                     
+
+        newtio.c_cc[VTIME]    = 0;   /* inter-character timer unused */         
+        newtio.c_cc[VMIN]     = 1;   /* blocking read until 5 chars received */ 
+
+        tcflush(fd, TCIFLUSH);                                                  
+        tcsetattr(fd,TCSANOW,&newtio);                                          
+
+
+        bytes = 0;
+        while (STOP==FALSE) {       /* loop for input */                        
+                unsigned char c;
+
+                res = read(fd,buf,1);   /* returns after 5 chars have been input */ 
+                buf[res]=0;               /* so we can printf... */                   
+                //fprintf(stderr, ":%02x:%d\n", buf[0], res);                                         
+                //if (buf[0]=='z') STOP=TRUE;                                           
+
+                c = buf[0];
+
+                if ( c & 0x40 ) {
+                        bytes = 1;
+                        mouse[0] = c;
+                }
+                else if (bytes == 1) {
+                        bytes = 2;
+                        mouse[1] = c;
+                }
+                else if (bytes == 2) {
+                        bytes = 0;
+                        mouse[2] = c;
+                        fprintf(stderr, "%02x %02x %02x\n", mouse[0], mouse[1], mouse[2]);
+                        //printf("%c%c%c", mouse[0], mouse[1], mouse[2]);
+                }
+        }                                                                       
+        tcsetattr(fd,TCSANOW,&oldtio);                                          
+}                                                                         
+
+
+/* End of FILE */
diff -uNr linux/drivers/no-otg/functions/mouse/mouse-ce42.c linux/drivers/otg/functions/mouse/mouse-ce42.c
--- linux/drivers/no-otg/functions/mouse/mouse-ce42.c	1970-01-01 01:00:00.000000000 +0100
+++ linux/drivers/otg/functions/mouse/mouse-ce42.c	2006-09-01 21:41:27.000000000 +0200
@@ -0,0 +1,89 @@
+/*
+ * otg/functions/mouse/mouse-ce42.c
+ *
+ *      Copyright (c) 2003, 2004 Belcarra
+ *
+ * By: 
+ *      Chris Lynne <sl@belcarra.com>, 
+ *      Stuart Lynne <sl@belcarra.com>, 
+ *
+ * This is a test USB HID Function Driver designed to test and
+ * verify that INTERRUPT IN endpoints work properly.
+ *
+ * This emulates a simple USB mouse and generates a constant stream
+ * of small random mouse movements.
+ *
+ * To use simply load with something like:
+ *
+ *      insmod mouse_fd.o vendor_id=0xffff product_id=0xffff
+ *
+ * And attach to a Windows box. Windows should recognize as a mouse and
+ * immediately start receiving a stream of data. 
+ *
+ * To terminate simply unplug.
+ *
+ * The mouse driver has several other characteristics to allow testing of
+ * other features of the bus interface driver:
+ *
+ *      - ep0 ZLP handling
+ *
+ *      - ep0 delayed CONTROL READ
+ *
+ *
+ * Notes
+ *
+ * 1. The mouse driver is product name is hard coded to a string that will
+ *    generate a string descriptor that is 32 bytes long. This will test
+ *    most UDC's ep0 ZLP handling as it is a multiple of the most common
+ *    UDC endpoint zero size. (An option can be added later to allow for
+ *    64 byte ep0 packetsize.)
+ *
+ * 2. The CONFIG_OTG_MOUSE_BH option can be enabled to delay the HID report 
+ *    to being generated by a bottom half handler. This will verify that 
+ *    the bus interface driver properly handles the case of a delayed
+ *    CONTROL READ. I.e. when the usbd_device_requesdevice_requesdevice_request
+ *    zero for successful completion but there is no tx_urb containing the
+ *    requested data. The bus interface driver must setup the conditions for
+ *    ACK'ing the SETUP packet but then NAK the IN data for endpoint zero
+ *    until the tx_urb is started later.
+ */
+
+
+
+#include "otg/usbp-chap9.h"
+#include <otg/usbp-mem.h>
+#include <otg/usbp-func.h>
+
+/*
+ * mouse_modinit - module init
+ *
+ */
+static int mouse_modinit (void)
+{
+
+        // register as usb function driver
+        THROW_IF (NULL == (mouse_private.function = usbd_register_function (&mouse_function_driver, "mouse", NULL)), error);
+#ifdef CONFIG_OTG_MOUSE_BH
+        mouse_private.notification_bh.routine = mouse_hid_bh;
+        mouse_private.notification_bh.data = NULL;
+#endif
+        CATCH(error) {
+                if (mouse_private.function) {
+                        usbd_deregister_function (mouse_private.function);
+                        mouse_private.function = NULL;
+                }
+                return -EINVAL;
+        }
+        return 0;
+}
+
+/* mouse_modexit - module cleanup
+ */
+static void mouse_modexit (void)
+{
+        if (mouse_private.function) {
+                usbd_deregister_function (mouse_private.function);
+                mouse_private.function = NULL;
+        }
+}
+
diff -uNr linux/drivers/no-otg/functions/mouse/mouse-fd.c linux/drivers/otg/functions/mouse/mouse-fd.c
--- linux/drivers/no-otg/functions/mouse/mouse-fd.c	1970-01-01 01:00:00.000000000 +0100
+++ linux/drivers/otg/functions/mouse/mouse-fd.c	2006-09-01 21:41:27.000000000 +0200
@@ -0,0 +1,826 @@
+/*
+ * otg/functions/mouse/mouse-fd.c
+ *
+ *      Copyright (c) 2003, 2004 Belcarra
+ *
+ * By: 
+ *      Stuart Lynne <sl@belcarra.com>, 
+ *      Bruce Balden <balden@belcarra.com>
+ *
+ */
+/*!
+ * @file otg/functions/mouse/mouse-fd.c
+ * @brief Mouse Function Driver protocol implementation.
+ *
+ * This file implements the Mouse HID protocols and will generate
+ * random mouse data on the INTERRUPT endpoint.
+ *
+ * The primary purpose of this driver is to demonstrate how to
+ * implement a simple function driver and to provide a simple
+ * uni-directional driver for testing USB Device peripheral drivers.
+ *
+ * The mouse driver has several other characteristics to allow testing of
+ * other features of USB Device peripheral drivers:
+ *
+ *      - ep0 ZLP handling
+ *
+ *      - ep0 delayed CONTROL READ
+ *
+ * To verify that ep0 ZLP processing is being handling correctly this
+ * driver has a hard coded Product name that is returned as a 32 byte
+ * string. This forces most any implementations that have an endpoint
+ * zero packetsize of 8, 16 or 32 to send a Zero Length Packet to 
+ * terminate the string transfer when the Product name is requested
+ * by the host.
+ *
+ * The delayed CONTROL READ test verifies that that USB Device peripheral
+ * drivers will correctly NAK until data is provide for device requests.
+ * This is done by (optionally) delaying the HID report via a bottom half
+ * handler. The device request returns normally and the peripheral driver
+ * must properly recognize that while the device request had a non-zero
+ * wLength field, there is currently no queued urb. 
+ *
+ * When the bottom half handler is scheduled the HID report urb will be
+ * queued on endpoint zero and then returned to the host.
+ *
+ *
+ * @ingroup MouseFunction
+ */
+
+#include <linux/config.h>
+#include <otg/otg-compat.h>
+
+#include <otg/usbp-chap9.h>
+#include <otg/usbp-func.h>
+#include <otg/otg-trace.h>
+#include <otg/otg-api.h>
+
+#include "mouse.h"
+
+struct mouse_private mouse_private;
+
+/*
+ * ep0 testing.... ensure that this is exactly 16 bytes
+ */
+#undef CONFIG_OTG_MOUSE_PRODUCT_NAME
+#define CONFIG_OTG_MOUSE_PRODUCT_NAME "Belcarra  Mouse"
+
+/*! 
+ * @name Mouse Descriptors
+ *
+ * MouseHIDReport
+ * MOUSE Configuration
+ *
+ * Endpoint, Class, Interface, Configuration and Device descriptors/descriptions
+ */
+
+/*! 
+ * Mouse Descriptors 
+ * @{ 
+ */ 
+
+#define BULK_INT        0x00    /*!< Interrupt endpoint number */
+#define ENDPOINTS       0x01    /*!< Number of endpoints required */
+
+/*! List of required endpoint numbers
+ */
+static u8 mouse_indexes[] = { BULK_INT, };
+
+/*! List of requested endpoints
+ */
+static struct usbd_endpoint_request mouse_endpoint_requests[ENDPOINTS+1] = {
+        { 1, 0, 0, USB_DIR_IN | USB_ENDPOINT_INTERRUPT, 16, 64, },
+        { 0, },
+};
+
+/*! This is the HID report returned for the HID Device Request.
+ *  There is no pre-defined descriptor type for this.
+ */
+static char MouseHIDReport[52] = {
+    0x05, 0x01,                    /*!< USAGE_PAGE (Generic Desktop)            */
+    0x09, 0x02,                    /*!< USAGE (Mouse)                           */
+    0xa1, 0x01,                    /*!< COLLECTION (Application)                */
+    0x09, 0x01,                    /*!<   USAGE (Pointer)                       */
+    0xa1, 0x00,                    /*!<   COLLECTION (Physical)                 */
+    0x05, 0x09,                    /*!<     USAGE_PAGE (Button)                 */
+    0x19, 0x01,                    /*!<     USAGE_MINIMUM (Button 1)            */
+    0x29, 0x03,                    /*!<     USAGE_MAXIMUM (Button 3)            */
+    0x15, 0x00,                    /*!<     LOGICAL_MINIMUM (0)                 */
+    0x25, 0x01,                    /*!<     LOGICAL_MAXIMUM (1)                 */
+    0x95, 0x03,                    /*!<     REPORT_COUNT (3)                    */
+    0x75, 0x01,                    /*!<     REPORT_SIZE (1)                     */
+    0x81, 0x02,                    /*!<     INPUT (Data,Var,Abs)                */
+    0x95, 0x01,                    /*!<     REPORT_COUNT (1)                    */
+    0x75, 0x05,                    /*!<     REPORT_SIZE (5)                     */
+    0x81, 0x03,                    /*!<     INPUT (Cnst,Var,Abs)                */
+    0x05, 0x01,                    /*!<     USAGE_PAGE (Generic Desktop)        */
+    0x09, 0x30,                    /*!<     USAGE (X)                           */
+    0x09, 0x31,                    /*!<     USAGE (Y)                           */
+    0x09, 0x38,                    /*!<     USAGE (WHEEL)                       */
+    0x15, 0x81,                    /*!<     LOGICAL_MINIMUM (-127)              */
+    0x25, 0x7f,                    /*!<     LOGICAL_MAXIMUM (127)               */
+    0x75, 0x08,                    /*!<     REPORT_SIZE (8)                     */
+    0x95, 0x03,                    /*!<     REPORT_COUNT (3)                    */
+    0x81, 0x06,                    /*!<     INPUT (Data,Var,Rel)                */
+    0xc0,                          /*!<   END_COLLECTION                        */
+    0xc0                           /*!< END_COLLECTION                        */
+};
+
+#if !defined(OTG_C99)
+/*! This is the interrupt endpoint descriptor.
+ */
+static struct usbd_endpoint_descriptor mouse_data;
+
+/*! The list of endpoint descriptors 
+ */
+static struct usbd_endpoint_descriptor *mouse_default[] = { &mouse_data, };
+
+
+/*! This is the HID desccriptor.
+ */
+struct hid_descriptor mouse_hid;
+
+/*! List of class descriptors 
+ */
+static struct usbd_generic_class_descriptor *mouse_hid_descriptors[] = { 
+        (struct usbd_generic_class_descriptor *)&mouse_hid, };
+
+
+/*! Data Interface Alternate description
+ */
+static struct usbd_interface_descriptor  mouse_data_alternate_descriptor;
+
+/*! Interface Descriptions
+ */
+static struct usbd_alternate_description mouse_data_alternate_descriptions[1];
+
+/*! List of Interface description(s)
+ */
+static struct usbd_interface_description mouse_interfaces[1];
+
+/*! Configuration description(s)
+ */
+static struct usbd_configuration_descriptor  mouse_configuration_descriptor;
+
+/*! Mouse Configuration Description
+ */
+struct usbd_configuration_description mouse_configuration_description[1];
+
+/*! Device Description
+ */
+static struct usbd_device_descriptor mouse_device_descriptor;
+
+#ifdef CONFIG_OTG_HIGH_SPEED
+/*! High Speed Device Description 
+ */
+static struct usbd_device_qualifier_descriptor mouse_device_qualifier_descriptor;
+#endif /* CONFIG_OTG_HIGH_SPEED */
+
+/*! OTG Descriptor
+ */
+static struct usbd_otg_descriptor mouse_otg_descriptor;
+
+/*! Device Description
+ */
+struct usbd_device_description mouse_device_description;
+
+void mouse_global_init(void)
+{
+
+        /*! This is the interrupt endpoint descriptor.
+         */
+        ZERO(mouse_data);
+        mouse_data.bLength = 0x07;
+        mouse_data.bDescriptorType = USB_DT_ENDPOINT;
+        mouse_data.bEndpointAddress = USB_DIR_IN;
+        mouse_data.bmAttributes = INTERRUPT;
+        mouse_data.wMaxPacketSize = __constant_cpu_to_le16(0x10);
+#if defined (CONFIG_OTG_MOUSE_INTERVAL)
+        mouse_data.bInterval = CONFIG_OTG_MOUSE_INTERVAL;
+#else
+        mouse_data.bInterval = 0x01;
+#endif /* defined (CONFIG_OTG_MOUSE_INTERVAL) */
+
+
+        /*! This is the HID desccriptor.
+         */
+        ZERO(mouse_hid);
+        mouse_hid.bLength = 0x09; 
+        mouse_hid.bDescriptorType = 0x21; 
+        mouse_hid.bcdHID = __constant_cpu_to_le16(0x110); 
+        mouse_hid.bCountryCode = 0x00; 
+        mouse_hid.bNumDescriptors = 0x01; 
+        mouse_hid.bReportType = 0x22; 
+        mouse_hid.wItemLength = __constant_cpu_to_le16(0x34); 
+
+
+        /*! Data Interface Alternate description
+         */
+        ZERO(mouse_data_alternate_descriptor);
+        mouse_data_alternate_descriptor.bLength = 0x09;
+        mouse_data_alternate_descriptor.bDescriptorType = USB_DT_INTERFACE;
+        mouse_data_alternate_descriptor.bInterfaceNumber = 0x00;
+        mouse_data_alternate_descriptor.bAlternateSetting = 0x00;
+        mouse_data_alternate_descriptor.bNumEndpoints = sizeof (mouse_default) / sizeof(struct usbd_endpoint_descriptor);
+        mouse_data_alternate_descriptor.bInterfaceClass = 0x03;
+        mouse_data_alternate_descriptor.bInterfaceSubClass = 0x01;
+        mouse_data_alternate_descriptor.bInterfaceProtocol = 0x02;
+        mouse_data_alternate_descriptor.iInterface = 0x00;
+
+        /*! Interface Descriptions
+         */
+        ZERO(mouse_data_alternate_descriptions);
+        mouse_data_alternate_descriptions[0].iInterface = "Random Mouse Interface - Interrupt";
+        mouse_data_alternate_descriptions[0].interface_descriptor =  &mouse_data_alternate_descriptor;
+        mouse_data_alternate_descriptions[0].classes = 
+                sizeof (mouse_hid_descriptors) / sizeof (struct usbd_generic_class_descriptor *);
+        mouse_data_alternate_descriptions[0].class_list = mouse_hid_descriptors;
+        mouse_data_alternate_descriptions[0].endpoints = sizeof (mouse_default) / sizeof(struct usbd_endpoint_descriptor *);
+        mouse_data_alternate_descriptions[0].endpoint_list = mouse_default;
+        mouse_data_alternate_descriptions[0].endpoint_indexes = mouse_indexes;
+
+        /*! List of Interface description(s)
+         */
+        ZERO(mouse_interfaces);
+        mouse_interfaces[0].alternates = sizeof (mouse_data_alternate_descriptions) / sizeof (struct usbd_alternate_description);
+        mouse_interfaces[0].alternate_list = mouse_data_alternate_descriptions;
+
+
+        /*! Configuration description(s)
+         */
+        ZERO(mouse_configuration_descriptor);
+        mouse_configuration_descriptor.bLength = 0x09;
+        mouse_configuration_descriptor.bDescriptorType = USB_DT_CONFIGURATION;
+        mouse_configuration_descriptor.wTotalLength = 0x00;
+        mouse_configuration_descriptor.bNumInterfaces = sizeof (mouse_interfaces) / sizeof (struct usbd_interface_description);
+        mouse_configuration_descriptor.bConfigurationValue = 0x01;
+        mouse_configuration_descriptor.iConfiguration = 0x00;
+        mouse_configuration_descriptor.bmAttributes = 0;
+        mouse_configuration_descriptor.bMaxPower = 0;
+
+        /*! Mouse Configuration Description
+         */
+        ZERO(mouse_configuration_description);
+        mouse_configuration_description[0].configuration_descriptor = &mouse_configuration_descriptor;
+        mouse_configuration_description[0].iConfiguration = "USB Random Mouse Configuration";
+        mouse_configuration_description[0].bNumInterfaces = 
+                sizeof (mouse_interfaces) / sizeof (struct usbd_interface_description);
+        mouse_configuration_description[0].interface_list = mouse_interfaces;
+
+        /*! Device Description
+         */
+        ZERO(mouse_device_descriptor);
+        mouse_device_descriptor.bLength = sizeof(struct usbd_device_descriptor);
+        mouse_device_descriptor.bDescriptorType = USB_DT_DEVICE;
+        mouse_device_descriptor.bcdUSB = __constant_cpu_to_le16(USB_BCD_VERSION);
+        mouse_device_descriptor.bDeviceClass = 0x00;
+        mouse_device_descriptor.bDeviceSubClass = 0x00;
+        mouse_device_descriptor.bDeviceProtocol = 0x00;
+        mouse_device_descriptor.bMaxPacketSize0 = 0x00;
+        mouse_device_descriptor.idVendor = __constant_cpu_to_le16(CONFIG_OTG_MOUSE_VENDORID);
+        mouse_device_descriptor.idProduct = __constant_cpu_to_le16(CONFIG_OTG_MOUSE_PRODUCTID);
+        mouse_device_descriptor.bcdDevice = __constant_cpu_to_le16(CONFIG_OTG_MOUSE_BCDDEVICE);
+
+#ifdef CONFIG_OTG_HIGH_SPEED
+        /*! High Speed Device Description 
+         */
+        ZERO(mouse_device_qualifier_descriptor);
+        mouse_device_qualifier_descriptor.bLength = sizeof(struct usbd_device_qualifier_descriptor);
+        mouse_device_qualifier_descriptor.bDescriptorType = USB_DT_DEVICE_QUALIFIER;
+        mouse_device_qualifier_descriptor.bcdUSB = __constant_cpu_to_le16(USB_BCD_VERSION);
+        mouse_device_qualifier_descriptor.bDeviceClass = 0x00;
+        mouse_device_qualifier_descriptor.bDeviceSubClass = 0x00;
+        mouse_device_qualifier_descriptor.bDeviceProtocol = 0x00;
+        mouse_device_qualifier_descriptor.bMaxPacketSize0 = 0x00;
+#endif /* CONFIG_OTG_HIGH_SPEED */
+
+        /*! OTG Descriptor
+         */
+        ZERO(mouse_otg_descriptor);
+        mouse_otg_descriptor.bLength = sizeof(struct usbd_otg_descriptor);
+        mouse_otg_descriptor.bDescriptorType = USB_DT_OTG;
+        mouse_otg_descriptor.bmAttributes = 0;
+
+        /*! Device Description
+         */
+        ZERO(mouse_device_description);
+        mouse_device_description.device_descriptor = &mouse_device_descriptor;
+#ifdef CONFIG_OTG_HIGH_SPEED
+        mouse_device_description.device_qualifier_descriptor = &mouse_device_qualifier_descriptor;
+#endif /* CONFIG_OTG_HIGH_SPEED */
+        mouse_device_description.otg_descriptor = &mouse_otg_descriptor;
+        mouse_device_description.iManufacturer = CONFIG_OTG_MOUSE_MANUFACTURER;
+        mouse_device_description.iProduct = CONFIG_OTG_MOUSE_PRODUCT_NAME;
+#if !defined(CONFIG_OTG_NO_SERIAL_NUMBER) && defined(CONFIG_OTG_SERIAL_NUMBER_STR)
+        mouse_device_description.iSerialNumber = CONFIG_OTG_SERIAL_NUMBER_STR; 
+#endif
+}
+
+#else /* defined(OTG_C99) */
+
+/*! This is the interrupt endpoint descriptor.
+ */
+static struct usbd_endpoint_descriptor mouse_data = {
+        .bLength = 0x07, 
+        .bDescriptorType = USB_DT_ENDPOINT, 
+        .bEndpointAddress = USB_DIR_IN, 
+        .bmAttributes = INTERRUPT, 
+        .wMaxPacketSize = __constant_cpu_to_le16(0x10), 
+#if defined (CONFIG_OTG_MOUSE_INTERVAL)
+        .bInterval = CONFIG_OTG_MOUSE_INTERVAL, 
+#else
+        .bInterval = 0x01, 
+#endif /* defined (CONFIG_OTG_MOUSE_INTERVAL) */
+};
+
+/*! The list of endpoint descriptors 
+ */
+static struct usbd_endpoint_descriptor *mouse_default[] = { &mouse_data, };
+
+/*! This is the HID desccriptor.
+ */
+struct hid_descriptor mouse_hid = {
+        .bLength = 0x09, 
+        .bDescriptorType = 0x21, 
+        .bcdHID = __constant_cpu_to_le16(0x110), 
+        .bCountryCode = 0x00, 
+        .bNumDescriptors = 0x01, 
+        .bReportType = 0x22, 
+        .wItemLength = __constant_cpu_to_le16(0x34), 
+};
+
+/*! List of class descriptors 
+ */
+static struct usbd_generic_class_descriptor *mouse_hid_descriptors[] = { 
+        (struct usbd_generic_class_descriptor *)&mouse_hid, };
+
+
+/*! Data Interface Alternate description
+ */
+static struct usbd_interface_descriptor  mouse_data_alternate_descriptor = {
+        .bLength = 0x09, 
+        .bDescriptorType = USB_DT_INTERFACE, 
+        .bInterfaceNumber = 0x00, 
+        .bAlternateSetting = 0x00, 
+        .bNumEndpoints = sizeof (mouse_default) / sizeof(struct usbd_endpoint_descriptor), 
+        .bInterfaceClass = 0x03, 
+        .bInterfaceSubClass = 0x01, 
+        .bInterfaceProtocol = 0x02, 
+        .iInterface = 0x00,
+};
+
+/*! Interface Descriptions
+ */
+static struct usbd_alternate_description mouse_data_alternate_descriptions[] = {
+        { .iInterface = "Random Mouse Interface - Interrupt",
+                .interface_descriptor =  &mouse_data_alternate_descriptor,
+                .classes = sizeof (mouse_hid_descriptors) / sizeof (struct usbd_generic_class_descriptor *),
+                .class_list =  mouse_hid_descriptors,
+                .endpoints = sizeof (mouse_default) / sizeof(struct usbd_endpoint_descriptor *),
+                .endpoint_list =  mouse_default,
+                .endpoint_indexes =  mouse_indexes,
+        },
+};
+
+/*! List of Interface description(s)
+ */
+static struct usbd_interface_description mouse_interfaces[] = {
+      { .alternates = sizeof (mouse_data_alternate_descriptions) / sizeof (struct usbd_alternate_description),
+                .alternate_list = mouse_data_alternate_descriptions,},
+};
+
+
+/*! Configuration description(s)
+ */
+static struct usbd_configuration_descriptor  mouse_configuration_descriptor = {
+        .bLength = 0x09, 
+        .bDescriptorType = USB_DT_CONFIGURATION, 
+        .wTotalLength = 0x00, 
+        .bNumInterfaces = sizeof (mouse_interfaces) / sizeof (struct usbd_interface_description),
+        .bConfigurationValue = 0x01, 
+        .iConfiguration = 0x00, 
+        .bmAttributes = 0, 
+        .bMaxPower = 0,
+};
+
+/*! Mouse Configuration Description
+ */
+struct usbd_configuration_description mouse_configuration_description[] = {
+      { .configuration_descriptor = &mouse_configuration_descriptor,
+              .iConfiguration = "USB Random Mouse Configuration",
+      },
+};
+
+/*! Device Description
+ */
+static struct usbd_device_descriptor mouse_device_descriptor = {
+        .bLength = sizeof(struct usbd_device_descriptor),
+        .bDescriptorType = USB_DT_DEVICE,
+        .bcdUSB = __constant_cpu_to_le16(USB_BCD_VERSION),
+        .bDeviceClass = 0x00,
+        .bDeviceSubClass = 0x00,
+        .bDeviceProtocol = 0x00,
+        .bMaxPacketSize0 = 0x00,
+        .idVendor = __constant_cpu_to_le16(CONFIG_OTG_MOUSE_VENDORID),
+        .idProduct = __constant_cpu_to_le16(CONFIG_OTG_MOUSE_PRODUCTID),
+        .bcdDevice = __constant_cpu_to_le16(CONFIG_OTG_MOUSE_BCDDEVICE),
+};
+
+#ifdef CONFIG_OTG_HIGH_SPEED
+/*! High Speed Device Description 
+ */
+static struct usbd_device_qualifier_descriptor mouse_device_qualifier_descriptor = {
+        .bLength = sizeof(struct usbd_device_qualifier_descriptor),
+        .bDescriptorType = USB_DT_DEVICE_QUALIFIER,
+        .bcdUSB = __constant_cpu_to_le16(USB_BCD_VERSION),
+        .bDeviceClass = 0x00,
+        .bDeviceSubClass = 0x00,
+        .bDeviceProtocol = 0x00,
+        .bMaxPacketSize0 = 0x00,
+};
+#endif /* CONFIG_OTG_HIGH_SPEED */
+
+/*! OTG Descriptor
+ */
+static struct usbd_otg_descriptor mouse_otg_descriptor = {
+        .bLength = sizeof(struct usbd_otg_descriptor),
+        .bDescriptorType = USB_DT_OTG,
+        .bmAttributes = 0,
+};
+
+/*! Device Description
+ */
+struct usbd_device_description mouse_device_description = {
+        .device_descriptor = &mouse_device_descriptor,
+#ifdef CONFIG_OTG_HIGH_SPEED
+        .device_qualifier_descriptor = &mouse_device_qualifier_descriptor,
+#endif /* CONFIG_OTG_HIGH_SPEED */
+        .otg_descriptor = &mouse_otg_descriptor,
+        .iManufacturer = CONFIG_OTG_MOUSE_MANUFACTURER,
+        .iProduct = CONFIG_OTG_MOUSE_PRODUCT_NAME,
+#if !defined(CONFIG_OTG_NO_SERIAL_NUMBER) && defined(CONFIG_OTG_SERIAL_NUMBER_STR)
+        .iSerialNumber = CONFIG_OTG_SERIAL_NUMBER_STR, 
+#endif
+};
+#endif /* defined(OTG_C99) */
+
+
+/*! @} */ 
+
+static int mouse_count;
+
+/* MOUSE ***************************************************************************************** */
+/* Transmit Function *************************************************************************** */
+
+int get_xy[8] = {
+         0,  0,  1,  1, 
+         0,  0, -1, -1, 
+};
+
+/*!
+ * mouse_send() - send a mouse data urb with random data
+ * @param function
+ * @return void
+ */
+void mouse_send(struct usbd_function_instance *function)
+{
+        struct mouse_private *mouse = &mouse_private;
+        int new_x = 0;
+        int new_y = 0;
+        u8 random;
+
+        memset(mouse->tx_urb->buffer, 0, 4);
+        if (!mouse->n) {
+
+                get_random_bytes(&random, 1);
+
+                mouse->last_x = MAX(-4, MIN(4, mouse->last_x + get_xy[random & 0x7]));
+                mouse->last_y = MAX(-4, MIN(4, mouse->last_y + get_xy[(random >> 3) & 0x7]));
+                mouse->n = (random>>6) & 0x3;
+
+                new_x = mouse->x + mouse->last_x;
+                new_y = mouse->y + mouse->last_y;
+
+                mouse->tx_urb->buffer[1] = mouse->last_x;
+                mouse->x = new_x;
+                mouse->tx_urb->buffer[2] = mouse->last_y;
+                mouse->y = new_y;
+                mouse->tx_urb->actual_length = 4;
+
+        }
+        else if ((mouse->n)&1) {
+                mouse->n--;
+        } 
+        else {
+                mouse->n--;
+                mouse->tx_urb->buffer[1] = mouse->last_x;
+                mouse->x = new_x;
+                mouse->tx_urb->buffer[2] = mouse->last_y;
+                mouse->y = new_y;
+        }
+        usbd_start_in_urb(mouse->tx_urb);
+}
+
+/*! 
+ * mouse_urb_sent() - called to indicate URB transmit finished
+ * This function is the callback function for sent urbs.
+ * It simply queues up another urb until the packets to be sent
+ * configuration parameter is reached (or forever if zero.)
+ * @param urb The urb to be sent.
+ * @param rc result
+ * @return int Return non-zero for failure.
+ */
+int mouse_urb_sent (struct usbd_urb *urb, int rc)
+{
+        struct mouse_private *mouse = &mouse_private;
+        struct usbd_function_instance *function = mouse->function;
+
+        RETURN_ZERO_IF(usbd_get_device_status(function) == USBD_CLOSING);
+        RETURN_ZERO_IF(usbd_get_device_status(function) != USBD_OK);
+        RETURN_ZERO_IF(usbd_get_device_state(function) != STATE_CONFIGURED);
+        TRACE_MSG1(MOUSE, "mouse_count: %d", mouse_count);
+        #ifdef CONFIG_OTG_MOUSE_PACKETS
+        RETURN_ZERO_IF (CONFIG_OTG_MOUSE_PACKETS && (mouse_count++ > CONFIG_OTG_MOUSE_PACKETS));
+        #endif /* CONFIG_OTG_MOUSE_PACKETS */
+        mouse_send(function);                                                   // re-send
+        return 0;
+}
+
+/* USB Device Functions ************************************************************************ */
+
+typedef enum mesg {
+        mesg_unknown,
+        mesg_configured,
+        mesg_reset,
+} mesg_t;
+mesg_t mouse_last_mesg;
+
+char * mouse_messages[3] = {
+        "",
+        "Mouse Configured",
+        "Mouse Reset",
+};
+
+void mouse_check_mesg(mesg_t curr_mesg)
+{
+        RETURN_UNLESS(mouse_last_mesg != curr_mesg);
+        mouse_last_mesg = curr_mesg;
+        otg_message(mouse_messages[curr_mesg]);
+}
+
+/*! 
+ * mouse_event_handler() - process a device event
+ * This function is called to process USB Device Events.
+ *
+ * The DEVICE_CONFIGURED event causes the mouse_send() to be called
+ * to start the data flow.
+ *
+ * The DEVICE_RESET or DEVICE_DE_CONFIGURED events cause the outstanding
+ * transmit urb to be cancelled.
+ *
+ * @param function the function instance 
+ * @param event the event 
+ * @param data
+ * @return void
+ *
+ */
+void mouse_event_handler (struct usbd_function_instance *function, usbd_device_event_t event, int data)
+{
+        struct mouse_private *mouse = &mouse_private;
+
+        switch (event) {
+        case DEVICE_CONFIGURED:
+                TRACE_MSG0(MOUSE, "Mouse Configured");
+                mouse_check_mesg(mesg_configured);
+                mouse_count = 0;
+                mouse->connected = 1;
+                if (!(mouse->tx_urb = usbd_alloc_urb (function, BULK_INT, 4, mouse_urb_sent))) 
+                        printk(KERN_INFO"%s: alloc failed\n", __FUNCTION__);
+                mouse_send(function);                                           // start sending
+                break;
+
+        case DEVICE_RESET:
+        case DEVICE_DE_CONFIGURED:
+                TRACE_MSG0(MOUSE, "Mouse De-Configured");
+                mouse_check_mesg(mesg_reset);
+                BREAK_IF(!mouse->connected);
+                mouse->connected = 0;
+                if (mouse->tx_urb) {
+                        usbd_free_urb (mouse->tx_urb);
+                        mouse->tx_urb = NULL;
+                }
+                break;
+        default: 
+                break;
+        }
+}
+
+/*! copy_report()
+ * This function copies the Mouse HID report into the provided URB.
+ * @param urb Destination
+ * @param data Source
+ * @param size Amount of data to copy.
+ * @param max_buf Size of buffer
+ * @return Non-zero if error.
+ *
+ */
+static int copy_report (struct usbd_urb *urb, void *data, int size, int max_buf)
+{
+        int available;
+        int length;
+
+        RETURN_EINVAL_IF (!urb);
+        RETURN_EINVAL_IF (!data);
+        RETURN_EINVAL_IF (!(length = size));
+        RETURN_EINVAL_IF ((available = max_buf - urb->actual_length) <= 0);
+
+        length = (length < available) ? length : available;
+        memcpy (urb->buffer + urb->actual_length, data, length);
+        urb->actual_length += length;
+        return 0;
+}
+
+/*! 
+ * mouse_send_hid() - send an EP0 urb containing HID report
+ * This is called to send the Mouse HID report.
+ */
+static int mouse_send_hid (void *data)
+{
+        struct mouse_private *mouse = &mouse_private;
+        struct usbd_function_instance *function = mouse->function;
+        struct usbd_urb *urb = usbd_alloc_urb_ep0(function, mouse->wLength, NULL);
+        int wMaxPacketSize = usbd_endpoint_zero_wMaxPacketSize(urb->function_instance, 0);
+
+        TRACE_MSG1(MOUSE, "Send Hid wLength: %d", mouse->wLength);
+        RETURN_EINVAL_IF (copy_report(urb, MouseHIDReport, sizeof(MouseHIDReport), mouse->wLength));
+        RETURN_EINVAL_UNLESS (wMaxPacketSize);
+
+        if (!(urb->actual_length % wMaxPacketSize) && (urb->actual_length < mouse->wLength)) 
+                urb->flags |= USBD_URB_SENDZLP;
+
+        RETURN_ZERO_IF(!usbd_start_in_urb(urb));
+        usbd_free_urb(urb);
+        return -EINVAL;
+}
+
+#ifdef CONFIG_OTG_MOUSE_BH
+/*! 
+ * mouse_hid_bh() - Bottom half handler to send a HID report
+ * @param data
+ */
+static void mouse_hid_bh (void *data)
+{
+        struct usbd_function_instance *function = mouse_private.function;
+        mouse_send_hid(function);
+}
+
+/*! mouse_schedule_bh - schedule a call for mouse_hid_bh
+ * @param void
+ */
+static int mouse_schedule_bh (void)
+{
+        TRACE_MSG0(MOUSE, "Scheduling");
+        return (!schedule_task (&mouse_private.notification_bh)) ? EINVAL : 0;
+}
+#endif /* CONFIG_OTG_MOUSE_BH */
+
+/*! 
+ * mouse_device_request - called to indicate urb has been received
+ * @param function
+ * @param request
+ */
+int mouse_device_request (struct usbd_function_instance *function, struct usbd_device_request *request)
+{
+        /* verify that this is a usb class request per cdc-mouse specification or a vendor request.
+         * determine the request direction and process accordingly
+         */
+
+        switch (request->bmRequestType & (USB_REQ_DIRECTION_MASK | USB_REQ_TYPE_MASK)) {
+
+        case USB_REQ_HOST2DEVICE:
+        case USB_REQ_HOST2DEVICE | USB_REQ_TYPE_CLASS:
+        case USB_REQ_HOST2DEVICE | USB_REQ_TYPE_VENDOR: 
+                return 0;
+
+        case USB_REQ_DEVICE2HOST :
+        case USB_REQ_DEVICE2HOST | USB_REQ_TYPE_CLASS:
+        case USB_REQ_DEVICE2HOST | USB_REQ_TYPE_VENDOR:
+
+                switch (request->bRequest) {
+                case USB_REQ_GET_DESCRIPTOR:
+                        switch (le16_to_cpu(request->wValue)>>8) {
+                        case HID_REPORT:
+                                mouse_private.wLength = request->wLength;
+                                #ifdef CONFIG_OTG_MOUSE_BH
+                                return mouse_schedule_bh();
+                                #else
+                                return mouse_send_hid(function);
+                                #endif
+                        }
+                default: break;
+                }
+                break;
+
+        default:
+                break;
+        }
+        return -EINVAL;
+}
+
+
+/*! mouse_function_enable - called by USB Device Core to enable the driver
+ * @param function The function instance for this driver to use.
+ * @return non-zero if error.
+ */
+static int mouse_function_enable (struct usbd_function_instance *function)
+{
+        struct mouse_private *mouse = &mouse_private;
+        mouse->function = function;
+        mouse->n = 0;
+        mouse->x = 0;
+        mouse->y = 0;
+        mouse->last_x = 0;
+        mouse->last_y = 0;
+        mouse->writesize = usbd_endpoint_wMaxPacketSize(function, BULK_INT, 0);
+        return 0;
+}
+
+/*! mouse_function_disable - called by the USB Device Core to disable the driver
+ * @param function The function instance for this driver
+ */
+static void mouse_function_disable (struct usbd_function_instance *function)
+{               
+        struct mouse_private *mouse = &mouse_private;
+        mouse->function = NULL;
+        mouse->writesize = 0;
+        mouse->function = NULL;
+}
+
+/* ********************************************************************************************* */
+#if !defined(OTG_C99)
+/*! function_ops - operations table for the USB Device Core
+ */
+static struct usbd_function_operations mouse_function_ops;
+
+/*! mouse_function_driver - USB Device Core function driver definition
+ */
+struct usbd_function_driver mouse_function_driver;
+
+void mouse_ops_init(void)
+{
+        /*! function_ops - operations table for the USB Device Core
+         */
+        ZERO(mouse_function_ops);
+        mouse_function_ops.event_handler = mouse_event_handler;              /*! called for each USB Device Event */
+        mouse_function_ops.device_request = mouse_device_request;          /*! called for each received device request */
+        mouse_function_ops.function_enable = mouse_function_enable;         /*! called to enable the function driver */
+        mouse_function_ops.function_disable = mouse_function_disable;       /*! called to disable the function driver */
+
+        /*! function_driver - USB Device Core function driver definition
+         */
+        ZERO(mouse_function_driver);
+        mouse_function_driver.name = "mouse-random";                            /*! driver name */
+        mouse_function_driver.fops = &mouse_function_ops;                             /*! operations table */
+        mouse_function_driver.device_description = &mouse_device_description;   /*! mouse device description */
+        mouse_function_driver.bNumConfigurations = 
+                sizeof (mouse_configuration_description) / sizeof (struct usbd_configuration_description);
+        mouse_function_driver.configuration_description = 
+                mouse_configuration_description;    /*! mouse configuration description */
+        mouse_function_driver.idVendor = __constant_cpu_to_le16(CONFIG_OTG_MOUSE_VENDORID);
+        mouse_function_driver.idProduct = __constant_cpu_to_le16(CONFIG_OTG_MOUSE_PRODUCTID);
+        mouse_function_driver.bcdDevice = __constant_cpu_to_le16(CONFIG_OTG_MOUSE_BCDDEVICE);
+
+
+        mouse_function_driver.bNumInterfaces = sizeof (mouse_interfaces) / sizeof (struct usbd_interface_description);
+        mouse_function_driver.interface_list = mouse_interfaces;
+        mouse_function_driver.endpointsRequested = ENDPOINTS;
+        mouse_function_driver.requestedEndpoints = mouse_endpoint_requests;
+}
+#else /* defined(OTG_C99) */
+/*! function_ops - operations table for the USB Device Core
+ */
+static struct usbd_function_operations mouse_function_ops = {
+        .event_handler = mouse_event_handler,                     /*!< called for each USB Device Event */
+        .device_request = mouse_device_request,           /*!< called for each received device request */
+        .function_enable = mouse_function_enable,         /*!< called to enable the function driver */
+        .function_disable = mouse_function_disable,       /*!< called to disable the function driver */
+};
+
+/*! mouse_function_driver - USB Device Core function driver definition
+ */
+struct usbd_function_driver mouse_function_driver = {
+        .name = "mouse-random",                            /*!< driver name */
+        .fops = &mouse_function_ops,                             /*!< operations table */
+        .device_description = &mouse_device_description,   /*!< mouse device description */
+        .bNumConfigurations = sizeof (mouse_configuration_description) / sizeof (struct usbd_configuration_description),
+        .configuration_description = mouse_configuration_description,    /*!< mouse configuration description */
+        .idVendor = __constant_cpu_to_le16(CONFIG_OTG_MOUSE_VENDORID),
+        .idProduct = __constant_cpu_to_le16(CONFIG_OTG_MOUSE_PRODUCTID),
+        .bcdDevice = __constant_cpu_to_le16(CONFIG_OTG_MOUSE_BCDDEVICE),
+        .bNumInterfaces = sizeof (mouse_interfaces) / sizeof (struct usbd_interface_description),
+        .interface_list = mouse_interfaces,
+        .endpointsRequested = ENDPOINTS,
+        .requestedEndpoints = mouse_endpoint_requests,
+};
+#endif /* defined(OTG_C99) */
+
diff -uNr linux/drivers/no-otg/functions/mouse/mouse-if.c linux/drivers/otg/functions/mouse/mouse-if.c
--- linux/drivers/no-otg/functions/mouse/mouse-if.c	1970-01-01 01:00:00.000000000 +0100
+++ linux/drivers/otg/functions/mouse/mouse-if.c	2006-09-01 21:41:27.000000000 +0200
@@ -0,0 +1,499 @@
+/*
+ * otg/functions/mouse/mouse-if.c
+ *
+ *      Copyright (c) 2003, 2004 Belcarra
+ *
+ * By: 
+ *      Stuart Lynne <sl@belcarra.com>, 
+ *      Bruce Balden <balden@belcarra.com>
+ *
+ */
+/*!
+ * @file otg/functions/mouse/mouse-if.c
+ * @brief Mouse Interface Function Driver protocol implementation.
+ *
+ * This file implements the Mouse HID protocols and will generate
+ * random mouse data on the INTERRUPT endpoint.
+ *
+ * The primary purpose of this driver is to demonstrate how to
+ * implement a simple function driver and to provide a simple
+ * uni-directional driver for testing USB Device peripheral drivers.
+ *
+ * The mouse driver has several other characteristics to allow testing of
+ * other features of USB Device peripheral drivers:
+ *
+ *      - ep0 ZLP handling
+ *
+ *      - ep0 delayed CONTROL READ
+ *
+ * To verify that ep0 ZLP processing is being handling correctly this
+ * driver has a hard coded Product name that is returned as a 32 byte
+ * string. This forces most any implementations that have an endpoint
+ * zero packetsize of 8, 16 or 32 to send a Zero Length Packet to 
+ * terminate the string transfer when the Product name is requested
+ * by the host.
+ *
+ * The delayed CONTROL READ test verifies that that USB Device peripheral
+ * drivers will correctly NAK until data is provide for device requests.
+ * This is done by (optionally) delaying the HID report via a bottom half
+ * handler. The device request returns normally and the peripheral driver
+ * must properly recognize that while the device request had a non-zero
+ * wLength field, there is currently no queued urb. 
+ *
+ * When the bottom half handler is scheduled the HID report urb will be
+ * queued on endpoint zero and then returned to the host.
+ *
+ *
+ * @ingroup MouseFunction
+ */
+
+#include <linux/config.h>
+#include <otg/otg-compat.h>
+
+#include <otg/usbp-chap9.h>
+#include <otg/usbp-func.h>
+#include <otg/otg-trace.h>
+#include <otg/otg-api.h>
+
+#include "mouse.h"
+
+
+/*
+ * ep0 testing.... ensure that this is exactly 16 bytes
+ */
+#undef CONFIG_OTG_MOUSE_PRODUCT_NAME
+#define CONFIG_OTG_MOUSE_PRODUCT_NAME "Belcarra  Mouse"
+
+/*! 
+ * @name Mouse Descriptors
+ *
+ * MouseHIDReport
+ * MOUSE Configuration
+ *
+ * Endpoint, Class, Interface, Configuration and Device descriptors/descriptions
+ */
+
+/*! 
+ * Mouse Descriptors 
+ * @{ 
+ */ 
+
+#define BULK_INT        0x00    /*!< Interrupt endpoint number */
+#define ENDPOINTS       0x01    /*!< Number of endpoints required */
+
+/*! List of required endpoint numbers
+ */
+static u8 mouse_indexes[] = { BULK_INT, };
+
+/*! List of requested endpoints
+ */
+static struct usbd_endpoint_request mouse_endpoint_requests[ENDPOINTS+1] = {
+        { 1, 0, 0, USB_DIR_IN | USB_ENDPOINT_INTERRUPT, 16, 64, },
+        { 0, },
+};
+
+#if !defined(OTG_C99)
+/*! This is the interrupt endpoint descriptor.
+ */
+static struct usbd_endpoint_descriptor mouse_data;
+
+/*! The list of endpoint descriptors 
+ */
+static struct usbd_endpoint_descriptor *mouse_default[] = { &mouse_data, };
+
+
+/*! List of class descriptors 
+ */
+static struct usbd_generic_class_descriptor *mouse_hid_descriptors[] = { 
+        (struct usbd_generic_class_descriptor *)&mouse_hid, };
+
+
+/*! Data Interface Alternate description
+ */
+static struct usbd_interface_descriptor  mouse_data_alternate_descriptor;
+
+/*! Interface Descriptions
+ */
+static struct usbd_alternate_description mouse_data_alternate_descriptions[1];
+
+/*! List of Interface description(s)
+ */
+static struct usbd_interface_description mouse_interfaces[1];
+
+
+void mouse_if_global_init(void)
+{
+
+        /*! This is the interrupt endpoint descriptor.
+         */
+        ZERO(mouse_data);
+        mouse_data.bLength = 0x07;
+        mouse_data.bDescriptorType = USB_DT_ENDPOINT;
+        mouse_data.bEndpointAddress = USB_DIR_IN;
+        mouse_data.bmAttributes = INTERRUPT;
+        mouse_data.wMaxPacketSize = __constant_cpu_to_le16(0x10);
+#if defined (CONFIG_OTG_MOUSE_INTERVAL)
+        mouse_data.bInterval = CONFIG_OTG_MOUSE_INTERVAL;
+#else
+        mouse_data.bInterval = 0x01;
+#endif /* defined (CONFIG_OTG_MOUSE_INTERVAL) */
+
+
+        /*! Data Interface Alternate description
+         */
+        ZERO(mouse_data_alternate_descriptor);
+        mouse_data_alternate_descriptor.bLength = 0x09;
+        mouse_data_alternate_descriptor.bDescriptorType = USB_DT_INTERFACE;
+        mouse_data_alternate_descriptor.bInterfaceNumber = 0x00;
+        mouse_data_alternate_descriptor.bAlternateSetting = 0x00;
+        mouse_data_alternate_descriptor.bNumEndpoints = sizeof (mouse_default) / sizeof(struct usbd_endpoint_descriptor);
+        mouse_data_alternate_descriptor.bInterfaceClass = 0x03;
+        mouse_data_alternate_descriptor.bInterfaceSubClass = 0x01;
+        mouse_data_alternate_descriptor.bInterfaceProtocol = 0x02;
+        mouse_data_alternate_descriptor.iInterface = 0x00;
+
+        /*! Interface Descriptions
+         */
+        ZERO(mouse_data_alternate_descriptions);
+        mouse_data_alternate_descriptions[0].iInterface = "Random Mouse Interface - Interrupt";
+        mouse_data_alternate_descriptions[0].interface_descriptor =  &mouse_data_alternate_descriptor;
+        mouse_data_alternate_descriptions[0].classes = 
+                sizeof (mouse_hid_descriptors) / sizeof (struct usbd_generic_class_descriptor *);
+        mouse_data_alternate_descriptions[0].class_list = mouse_hid_descriptors;
+        mouse_data_alternate_descriptions[0].endpoints = sizeof (mouse_default) / sizeof(struct usbd_endpoint_descriptor *);
+        mouse_data_alternate_descriptions[0].endpoint_list = mouse_default;
+        mouse_data_alternate_descriptions[0].endpoint_indexes = mouse_indexes;
+
+        /*! List of Interface description(s)
+         */
+        ZERO(mouse_interfaces);
+        mouse_interfaces[0].alternates = sizeof (mouse_data_alternate_descriptions) / sizeof (struct usbd_alternate_description);
+        mouse_interfaces[0].alternate_list = mouse_data_alternate_descriptions;
+
+
+#else /* defined(OTG_C99) */
+
+/*! This is the interrupt endpoint descriptor.
+ */
+static struct usbd_endpoint_descriptor mouse_data = {
+        .bLength = 0x07, 
+        .bDescriptorType = USB_DT_ENDPOINT, 
+        .bEndpointAddress = USB_DIR_IN, 
+        .bmAttributes = INTERRUPT, 
+        .wMaxPacketSize = __constant_cpu_to_le16(0x10), 
+#if defined (CONFIG_OTG_MOUSE_INTERVAL)
+        .bInterval = CONFIG_OTG_MOUSE_INTERVAL, 
+#else
+        .bInterval = 0x01, 
+#endif /* defined (CONFIG_OTG_MOUSE_INTERVAL) */
+};
+
+/*! The list of endpoint descriptors 
+ */
+static struct usbd_endpoint_descriptor *mouse_default[] = { &mouse_data, };
+
+
+/*! List of class descriptors 
+ */
+static struct usbd_generic_class_descriptor *mouse_hid_descriptors[] = { 
+        (struct usbd_generic_class_descriptor *)&mouse_hid, };
+
+
+/*! Data Interface Alternate description
+ */
+static struct usbd_interface_descriptor  mouse_data_alternate_descriptor = {
+        .bLength = 0x09, 
+        .bDescriptorType = USB_DT_INTERFACE, 
+        .bInterfaceNumber = 0x00, 
+        .bAlternateSetting = 0x00, 
+        .bNumEndpoints = sizeof (mouse_default) / sizeof(struct usbd_endpoint_descriptor), 
+        .bInterfaceClass = 0x03, 
+        .bInterfaceSubClass = 0x01, 
+        .bInterfaceProtocol = 0x02, 
+        .iInterface = 0x00,
+};
+
+/*! Interface Descriptions
+ */
+static struct usbd_alternate_description mouse_data_alternate_descriptions[] = {
+        { .iInterface = "Random Mouse Interface - Interrupt",
+                .interface_descriptor =  &mouse_data_alternate_descriptor,
+                .classes = sizeof (mouse_hid_descriptors) / sizeof (struct usbd_generic_class_descriptor *),
+                .class_list =  mouse_hid_descriptors,
+                .endpoints = sizeof (mouse_default) / sizeof(struct usbd_endpoint_descriptor *),
+                .endpoint_list =  mouse_default,
+                .endpoint_indexes =  mouse_indexes,
+        },
+};
+
+/*! List of Interface description(s)
+ */
+static struct usbd_interface_description mouse_interfaces[] = {
+      { .alternates = sizeof (mouse_data_alternate_descriptions) / sizeof (struct usbd_alternate_description),
+                .alternate_list = mouse_data_alternate_descriptions,},
+};
+
+#endif /* defined(OTG_C99) */
+
+/*! @} */ 
+
+static int mouse_count;
+
+/* MOUSE ***************************************************************************************** */
+/* Transmit Function *************************************************************************** */
+
+int get_xy[8] = {
+         0,  0,  1,  1, 
+         0,  0, -1, -1, 
+};
+
+/*!
+ * mouse_send() - send a mouse data urb with random data
+ * @param function
+ * @return void
+ */
+void mouse_send(struct usbd_function_instance *function)
+{
+        struct mouse_private *mouse = &mouse_private;
+        int new_x = 0;
+        int new_y = 0;
+        u8 random;
+
+        memset(mouse->tx_urb->buffer, 0, 4);
+        if (!mouse->n) {
+
+                get_random_bytes(&random, 1);
+
+                mouse->last_x = MAX(-4, MIN(4, mouse->last_x + get_xy[random & 0x7]));
+                mouse->last_y = MAX(-4, MIN(4, mouse->last_y + get_xy[(random >> 3) & 0x7]));
+                mouse->n = (random>>6) & 0x3;
+
+                new_x = mouse->x + mouse->last_x;
+                new_y = mouse->y + mouse->last_y;
+
+                mouse->tx_urb->buffer[1] = mouse->last_x;
+                mouse->x = new_x;
+                mouse->tx_urb->buffer[2] = mouse->last_y;
+                mouse->y = new_y;
+                mouse->tx_urb->actual_length = 4;
+
+        }
+        else if ((mouse->n)&1) {
+                mouse->n--;
+        } 
+        else {
+                mouse->n--;
+                mouse->tx_urb->buffer[1] = mouse->last_x;
+                mouse->x = new_x;
+                mouse->tx_urb->buffer[2] = mouse->last_y;
+                mouse->y = new_y;
+        }
+        usbd_start_in_urb(mouse->tx_urb);
+}
+
+/*! 
+ * mouse_urb_sent() - called to indicate URB transmit finished
+ * This function is the callback function for sent urbs.
+ * It simply queues up another urb until the packets to be sent
+ * configuration parameter is reached (or forever if zero.)
+ * @param urb The urb to be sent.
+ * @param rc result
+ * @return int Return non-zero for failure.
+ */
+int mouse_urb_sent (struct usbd_urb *urb, int rc)
+{
+        struct mouse_private *mouse = &mouse_private;
+        struct usbd_function_instance *function = mouse->function;
+
+        RETURN_ZERO_IF(usbd_get_device_status(function) == USBD_CLOSING);
+        RETURN_ZERO_IF(usbd_get_device_status(function) != USBD_OK);
+        RETURN_ZERO_IF(usbd_get_device_state(function) != STATE_CONFIGURED);
+        TRACE_MSG1(MOUSE, "mouse_count: %d", mouse_count);
+        #ifdef CONFIG_OTG_MOUSE_PACKETS
+        RETURN_ZERO_IF (CONFIG_OTG_MOUSE_PACKETS && (mouse_count++ > CONFIG_OTG_MOUSE_PACKETS));
+        #endif /* CONFIG_OTG_MOUSE_PACKETS */
+        mouse_send(function);                                                   // re-send
+        return 0;
+}
+
+/* USB Device Functions ************************************************************************ */
+
+typedef enum mesg {
+        mesg_unknown,
+        mesg_configured,
+        mesg_reset,
+} mesg_t;
+static mesg_t mouse_last_mesg;
+
+static char * mouse_messages[3] = {
+        "",
+        "Mouse-if Configured",
+        "Mouse-if Reset",
+};
+
+static void mouse_check_mesg(mesg_t curr_mesg)
+{
+        RETURN_UNLESS(mouse_last_mesg != curr_mesg);
+        mouse_last_mesg = curr_mesg;
+        otg_message(mouse_messages[curr_mesg]);
+}
+
+/*! 
+ * mouse_event_handler() - process a device event
+ * This function is called to process USB Device Events.
+ *
+ * The DEVICE_CONFIGURED event causes the mouse_send() to be called
+ * to start the data flow.
+ *
+ * The DEVICE_RESET or DEVICE_DE_CONFIGURED events cause the outstanding
+ * transmit urb to be cancelled.
+ *
+ * @param function the function instance 
+ * @param event the event 
+ * @param data
+ * @return void
+ *
+ */
+static void mouse_event_handler (struct usbd_function_instance *function, usbd_device_event_t event, int data)
+{
+        struct mouse_private *mouse = &mouse_private;
+
+        switch (event) {
+        case DEVICE_CONFIGURED:
+                TRACE_MSG0(MOUSE, "Mouse-if Configured");
+                mouse_check_mesg(mesg_configured);
+                mouse_count = 0;
+                mouse->connected = 1;
+                if (!(mouse->tx_urb = usbd_alloc_urb (function, BULK_INT, 4, mouse_urb_sent))) 
+                        printk(KERN_INFO"%s: alloc failed\n", __FUNCTION__);
+                mouse_send(function);                                           // start sending
+                break;
+
+        case DEVICE_RESET:
+        case DEVICE_DE_CONFIGURED:
+                TRACE_MSG0(MOUSE, "Mouse-if De-Configured");
+                mouse_check_mesg(mesg_reset);
+                BREAK_IF(!mouse->connected);
+                mouse->connected = 0;
+                if (mouse->tx_urb) {
+                        usbd_free_urb (mouse->tx_urb);
+                        mouse->tx_urb = NULL;
+                }
+                break;
+        default: 
+                break;
+        }
+}
+
+#ifdef CONFIG_OTG_MOUSE_BH
+/*! 
+ * mouse_hid_bh() - Bottom half handler to send a HID report
+ * @param data
+ */
+static void mouse_hid_bh (void *data)
+{
+        struct usbd_function_instance *function = mouse_private.function;
+        mouse_send_hid(function);
+}
+
+/*! mouse_schedule_bh - schedule a call for mouse_hid_bh
+ * @param void
+ */
+static int mouse_schedule_bh (void)
+{
+        TRACE_MSG0(MOUSE, "Scheduling");
+        return (!schedule_task (&mouse_private.notification_bh)) ? EINVAL : 0;
+}
+#endif /* CONFIG_OTG_MOUSE_BH */
+
+/*! 
+ * mouse_device_request - called to indicate urb has been received
+ * @param function
+ * @param request
+ */
+static int mouse_device_request (struct usbd_function_instance *function, struct usbd_device_request *request)
+{
+        return -EINVAL;
+}
+
+
+/*! mouse_function_enable - called by USB Device Core to enable the driver
+ * @param function The function instance for this driver to use.
+ * @return non-zero if error.
+ */
+static int mouse_function_enable (struct usbd_function_instance *function)
+{
+        struct mouse_private *mouse = &mouse_private;
+        mouse->function = function;
+        mouse->n = 0;
+        mouse->x = 0;
+        mouse->y = 0;
+        mouse->last_x = 0;
+        mouse->last_y = 0;
+        mouse->writesize = usbd_endpoint_wMaxPacketSize(function, BULK_INT, 0);
+        return 0;
+}
+
+/*! mouse_function_disable - called by the USB Device Core to disable the driver
+ * @param function The function instance for this driver
+ */
+static void mouse_function_disable (struct usbd_function_instance *function)
+{               
+        struct mouse_private *mouse = &mouse_private;
+        mouse->function = NULL;
+        mouse->writesize = 0;
+        mouse->function = NULL;
+}
+
+/* ********************************************************************************************* */
+#if !defined(OTG_C99)
+/*! function_ops - operations table for the USB Device Core
+ */
+static struct usbd_function_operations mouse_function_ops;
+
+/*! mouse_interfacedriver - USB Device Core function driver definition
+ */
+struct usbd_function_driver mouse_interfacedriver;
+
+void mouse_if_ops_init(void)
+{
+        /*! function_ops - operations table for the USB Device Core
+         */
+        ZERO(mouse_function_ops);
+        mouse_function_ops.event_handler = mouse_event_handler;              /*! called for each USB Device Event */
+        mouse_function_ops.device_request = mouse_device_request;          /*! called for each received device request */
+        mouse_function_ops.function_enable = mouse_function_enable;         /*! called to enable the function driver */
+        mouse_function_ops.function_disable = mouse_function_disable;       /*! called to disable the function driver */
+
+        /*! function_driver - USB Device Core function driver definition
+         */
+        ZERO(mouse_interfacedriver);
+        mouse_interface_driver.name = "mouse-random-if-driver-noc99";                            /*! driver name */
+        mouse_interface_driver.fops = &mouse_function_ops;                             /*! operations table */
+
+        mouse_function_driver.bNumInterfaces = sizeof (mouse_interfaces) / sizeof (struct usbd_interface_description);
+        mouse_function_driver.interface_list = mouse_interfaces;
+        mouse_interface_driver.endpointsRequested = ENDPOINTS;
+        mouse_interface_driver.requestedEndpoints = mouse_endpoint_requests;
+
+}
+#else /* defined(OTG_C99) */
+/*! function_ops - operations table for the USB Device Core
+ */
+static struct usbd_function_operations mouse_function_ops = {
+        .event_handler = mouse_event_handler,                     /*!< called for each USB Device Event */
+        .device_request = mouse_device_request,           /*!< called for each received device request */
+        .function_enable = mouse_function_enable,         /*!< called to enable the function driver */
+        .function_disable = mouse_function_disable,       /*!< called to disable the function driver */
+};
+
+/*! mouse_interface_driver - USB Device Core function driver definition
+ */
+struct usbd_function_driver mouse_interface_driver = {
+        .name = "mouse-random-if-driver",                        /*!< driver name */
+        .fops = &mouse_function_ops,                             /*!< operations table */
+        .endpointsRequested = ENDPOINTS,
+        .requestedEndpoints = mouse_endpoint_requests,
+};
+#endif /* defined(OTG_C99) */
+
diff -uNr linux/drivers/no-otg/functions/mouse/mouse-l24.c linux/drivers/otg/functions/mouse/mouse-l24.c
--- linux/drivers/no-otg/functions/mouse/mouse-l24.c	1970-01-01 01:00:00.000000000 +0100
+++ linux/drivers/otg/functions/mouse/mouse-l24.c	2006-09-01 21:41:27.000000000 +0200
@@ -0,0 +1,175 @@
+/*
+ * otg/functions/mouse/mouse-l24.c
+ *
+ *      Copyright (c) 2003, 2004 Belcarra
+ *
+ * By: 
+ *      Stuart Lynne <sl@belcarra.com>, 
+ *      Bruce Balden <balden@belcarra.com>
+ *
+ */
+/*!
+ * @file otg/functions/mouse/mouse-l24.c
+ * @brief Mouse Function Driver Linux 2.4 initialization
+ *
+ *
+ * This file contains the Linux 2.4 module init and exit functions
+ * to load and unload the Random Mouse Function.
+ *
+ * Notes
+ *
+ * This driver does not export any upper edge functionality to
+ * for the user or applications to use. It simply sends a configurable
+ * number of data packets to the USB Host when configured. Each data
+ * packet contains the equivalent of a small mouse report which will
+ * cause the mouse cursor to move on the host.
+ *
+ * To use simply load with something like:
+ *
+ *      insmod mouse_fd.o vendor_id=0xffff product_id=0xffff
+ *
+ * And attach to a Windows box. Windows should recognize as a mouse and
+ * immediately start receiving a stream of data. 
+ *
+ * To terminate simply unplug.
+ *
+ *
+ * 1. The mouse driver is product name is hard coded to a string that will
+ *    generate a string descriptor that is 32 bytes long. This will test
+ *    most UDC's ep0 ZLP handling as it is a multiple of the most common
+ *    UDC endpoint zero size. (An option can be added later to allow for
+ *    64 byte ep0 packetsize.)
+ *
+ * 2. The CONFIG_OTG_MOUSE_BH option can be enabled to delay the HID report 
+ *    to being generated by a bottom half handler. This will verify that 
+ *    the bus interface driver properly handles the case of a delayed
+ *    CONTROL READ. I.e. when the usbd_device_request() function returns
+ *    zero for successful completion but there is no tx_urb containing the
+ *    requested data. The bus interface driver must setup the conditions for
+ *    ACK'ing the SETUP packet but then NAK the IN data for endpoint zero
+ *    until the tx_urb is started later.
+ *
+ * 3. The CONFIG_OTG_MOUSE_PACKETS option is used to set the number of
+ *    mouse data reports to send. Set to zero for a continous stream
+ *    of data.
+ *
+ * @ingroup MouseFunction
+ */
+
+#include <otg/otg-compat.h>
+#include <otg/otg-module.h>
+
+#include <otg/usbp-chap9.h>
+#include <otg/usbp-func.h>
+#include <otg/otg-trace.h>
+
+#include "mouse.h"
+
+
+/* Module Parameters ******************************************************** */
+/* ! 
+ * @name XXXXX MODULE Parameters
+ */
+/* ! @{ */
+
+MOD_AUTHOR ("sl@belcarra.com, tbr@belcarra.com"); 
+EMBED_LICENSE();
+MOD_DESCRIPTION ("Belcarra Random Walk MOUSE Function");
+
+static u32 vendor_id;           /*!< override built-in vendor ID */
+static u32 product_id;          /*!< override built-in product ID */
+
+MOD_PARM (vendor_id, "i");
+MOD_PARM (product_id, "i");
+
+MOD_PARM_DESC (vendor_id, "Device Vendor ID");
+MOD_PARM_DESC (product_id, "Device Product ID");
+
+otg_tag_t MOUSE;
+/* ! *} */
+
+/* USB Module init/exit ***************************************************** */
+
+/*! 
+ * mouse_modinit() - module init
+ *
+ * This is called by the Linux kernel; either when the module is loaded
+ * if compiled as a module, or during the system intialization if the 
+ * driver is linked into the kernel.
+ *
+ * This function will parse module parameters if required and then register
+ * the mouse driver with the USB Device software.
+ *
+ * If the CONFIG_OTG_MOUSE_BH option is enabled it will also setup the mouse
+ * bottom half handler.
+ *
+ */
+static int mouse_modinit (void)
+{
+        struct usbd_function_instance *mfi;
+        printk (KERN_INFO "%s: vendor_id: %04x product_id: %04x\n", __FUNCTION__, vendor_id, product_id);
+
+        #if !defined(OTG_C99)
+        mouse_global_init();
+        mouse_ops_init();
+        #endif /* defined(OTG_C99) */
+        
+        MOUSE = otg_trace_obtain_tag();
+        TRACE_MSG2(MOUSE, "vendor_id: %04x product_id: %04x",vendor_id, product_id);
+
+        if (vendor_id) 
+                mouse_function_driver.idVendor = cpu_to_le16(vendor_id);
+        if (product_id) 
+                mouse_function_driver.idProduct = cpu_to_le16(product_id);
+
+        mouse_hid.wItemLength = cpu_to_le16(0x34);      // XXX mips compiler bug.....
+
+        // register as usb function driver
+        THROW_UNLESS ((mouse_private.function = usbd_register_function (&mouse_function_driver, "mouse",  NULL)), error);
+        #ifdef CONFIG_OTG_MOUSE_BH
+        mouse_private.notification_bh.routine = mouse_hid_bh;
+        mouse_private.notification_bh.data = NULL;
+        #endif
+        CATCH(error) {
+                if (mouse_private.function) {
+                        usbd_deregister_function (mouse_private.function);
+                        mouse_private.function = NULL;
+                }
+                otg_trace_invalidate_tag(MOUSE);
+                return -EINVAL;
+        }
+        return 0;
+}
+
+module_init (mouse_modinit);
+
+#if OTG_EPILOGUE
+/*! 
+ * mouse_modexit() - module init
+ *
+ * This is called by the Linux kernel; when the module is being unloaded 
+ * if compiled as a module. This function is never called if the 
+ * driver is linked into the kernel.
+ *
+ * @param void
+ * @return void
+ */
+static void mouse_modexit (void)
+{
+        #ifdef CONFIG_OTG_MOUSE_BH
+        while (PENDING_WORK_ITEM(mouse_private.notification_bh)) {
+                printk(KERN_ERR"%s: waiting for bh\n", __FUNCTION__);
+                schedule_timeout(10 * HZ);
+        }
+        #endif
+        if (mouse_private.function) {
+                usbd_deregister_function (mouse_private.function);
+                mouse_private.function = NULL;
+        }
+
+        otg_trace_invalidate_tag(MOUSE);
+}
+
+module_exit (mouse_modexit);
+#endif
+
diff -uNr linux/drivers/no-otg/functions/mouse/mouse.h linux/drivers/otg/functions/mouse/mouse.h
--- linux/drivers/no-otg/functions/mouse/mouse.h	1970-01-01 01:00:00.000000000 +0100
+++ linux/drivers/otg/functions/mouse/mouse.h	2006-09-01 21:41:27.000000000 +0200
@@ -0,0 +1,63 @@
+/*
+ * otg/functions/mouse/mouse.h
+ *
+ *      Copyright (c) 2003, 2004 Belcarra
+ *
+ * By: 
+ *      Stuart Lynne <sl@belcarra.com>, 
+ *      Tom Rushworth <tbr@belcarra.com>, 
+ *      Bruce Balden <balden@belcarra.com>
+ *
+ */
+
+/*!
+ * @defgroup MouseFunction Random Mouse
+ * @ingroup functiongroup
+ */
+/*!
+ * @file otg/functions/mouse/mouse.h
+ * @brief Mouse Function Driver private defines
+ *
+ *
+ * This is a test USB HID Function Driver designed to test and
+ * verify that INTERRUPT IN endpoints work properly.
+ *
+ *
+ * @ingroup MouseFunction
+ */
+
+
+/*!
+ * The mouse_private structure is used to collect all of the mouse driver 
+ * global variables into one place.
+ */
+struct mouse_private {
+        struct usbd_function_instance *function; /*!< function instance for this module */
+#ifdef CONFIG_OTG_MOUSE_BH
+        struct WORK_STRUCT notification_bh;
+#endif /* CONFIG_OTG_MOUSE_BH */
+        int usb_driver_registered;              /*!< non-zero if usb function registered */
+        unsigned char connected;                /*!< non-zero if connected to host (configured) */
+        unsigned int writesize;                 /*!< packetsize * 4 */
+        struct usbd_urb *tx_urb;                /*!< saved copy of current tx urb */
+        int wLength;                            
+        int x;
+        int y;
+        int last_x;
+        int last_y;
+        int n;
+};
+
+extern struct mouse_private mouse_private;
+extern struct usbd_function_driver mouse_function_driver;
+extern struct usbd_function_driver mouse_interface_driver;
+extern struct usbd_function_driver mouse_composite_driver;
+extern struct hid_descriptor mouse_hid;
+
+#ifndef OTG_C99
+extern void mouse_global_init(void);
+#endif /* OTG_C99 */
+
+#define MOUSE mouse_trace_tag
+extern otg_tag_t MOUSE;
+
diff -uNr linux/drivers/no-otg/functions/msc/Config.in linux/drivers/otg/functions/msc/Config.in
--- linux/drivers/no-otg/functions/msc/Config.in	1970-01-01 01:00:00.000000000 +0100
+++ linux/drivers/otg/functions/msc/Config.in	2006-09-01 21:41:27.000000000 +0200
@@ -0,0 +1,25 @@
+#
+# Mass Storage Class Function Drivers
+#
+# Copyright (C) 2003 Belcarra
+#
+
+
+mainmenu_option next_comment
+
+comment "Mass Storage Function "
+dep_tristate '  Mass Storage Function' CONFIG_OTG_MSC $CONFIG_OTG
+
+if [ "$CONFIG_OTG_MSC" = "y" -o "$CONFIG_OTG_MSC" = "m" ]; then
+   hex     'VendorID (hex value)' CONFIG_OTG_MSC_VENDORID "15ec"
+   hex     'ProductID (hex value)' CONFIG_OTG_MSC_PRODUCTID "f006"
+   hex     'bcdDevice (binary-coded decimal)' CONFIG_OTG_MSC_BCDDEVICE "0100"
+
+   string  'iManufacturer (string)' CONFIG_OTG_MSC_MANUFACTURER "Belcarra"
+   string  'iProduct (string)' CONFIG_OTG_MSC_PRODUCT_NAME "Mass Storage Class - Bulk Only"
+   string  'MSC Bulk Only iInterface (string)' CONFIG_OTG_MSC_INTF "MSC BO Data Intf"
+   string  'Data Interface iConfiguration (string)' CONFIG_OTG_MSC_DESC "MSC BO Configuration"	
+
+fi
+
+endmenu
diff -uNr linux/drivers/no-otg/functions/msc/Kconfig linux/drivers/otg/functions/msc/Kconfig
--- linux/drivers/no-otg/functions/msc/Kconfig	1970-01-01 01:00:00.000000000 +0100
+++ linux/drivers/otg/functions/msc/Kconfig	2006-09-01 21:41:27.000000000 +0200
@@ -0,0 +1,52 @@
+menu "OTG Mass Storage function"
+	depends on OTG
+
+config OTG_MSC
+        tristate "  Mass Storage Function"
+        depends on OTG
+
+menu "OTG Mass Storage function options"
+	depends on OTG && OTG_MSC
+
+config OTG_MSC_VENDORID
+        hex "VendorID (hex value)"
+        depends on OTG && OTG_MSC
+        default "Ox15ec"
+
+config OTG_MSC_PRODUCTID
+        hex "ProductID (hex value)"
+        depends on OTG && OTG_MSC
+        default "Oxf006"
+
+config OTG_MSC_BCDDEVICE
+        hex "bcdDevice (binary-coded decimal)"
+        depends on OTG && OTG_MSC
+        default "Ox0100"
+
+config OTG_MSC_MANUFACTURER
+        string "iManufacturer (string)"
+        depends on OTG && OTG_MSC
+        default "Belcarra"
+
+config OTG_MSC_PRODUCT_NAME
+        string "iProduct (string)"
+        depends on OTG && OTG_MSC
+        default "Mass Storage Class - Bulk Only"
+
+config OTG_MSC_INTF
+        string "MSC Bulk Only iInterface (string)"
+        depends on OTG && OTG_MSC
+        default "MSC BO Data Intf"
+
+config OTG_MSC_DESC
+        string "Data Interface iConfiguration (string)"
+        depends on OTG && OTG_MSC
+        default "MSC BO Configuration"
+
+config OTG_MSC_REGISTER_TRACE
+        bool "  MSC Tracing"
+        depends on OTG && OTG_MSC
+        default n
+endmenu
+
+endmenu
diff -uNr linux/drivers/no-otg/functions/msc/Makefile linux/drivers/otg/functions/msc/Makefile
--- linux/drivers/no-otg/functions/msc/Makefile	1970-01-01 01:00:00.000000000 +0100
+++ linux/drivers/otg/functions/msc/Makefile	2006-09-01 21:41:27.000000000 +0200
@@ -0,0 +1,68 @@
+#
+# Function driver for a Mass Storage USB Device
+#
+# Copyright (c) 2003 Belcarra
+
+# Multipart objects.
+
+O_TARGET	:= msc_target.o
+list-multi	:= msc_fd.o 
+
+#msc_fd-objs	:= msc.o crc.o 
+msc_fd-objs	:= msc-fd.o crc.o msc-linux.o msc-bo.o msc-io-l24.o
+
+# Objects that export symbols.
+#export-objs	:= msc.o 
+export-objs	:= msc-fd.o msc-linux.o msc-bo.o
+
+# Object file lists.
+
+obj-y	:=
+obj-m	:=
+obj-n	:=
+obj-	:=
+
+# Each configuration option enables a list of files.
+
+obj-$(CONFIG_OTG_MSC)   += msc_fd.o
+
+# Extract lists of the multi-part drivers.
+# The 'int-*' lists are the intermediate files used to build the multi's.
+
+multi-y		:= $(filter $(list-multi), $(obj-y))
+multi-m		:= $(filter $(list-multi), $(obj-m))
+int-y		:= $(sort $(foreach m, $(multi-y), $($(basename $(m))-objs)))
+int-m		:= $(sort $(foreach m, $(multi-m), $($(basename $(m))-objs)))
+
+# Files that are both resident and modular: remove from modular.
+
+obj-m		:= $(filter-out $(obj-y), $(obj-m))
+int-m		:= $(filter-out $(int-y), $(int-m))
+
+# Translate to Rules.make lists.
+
+O_OBJS		:= $(filter-out $(export-objs), $(obj-y))
+OX_OBJS		:= $(filter     $(export-objs), $(obj-y))
+M_OBJS		:= $(sort $(filter-out $(export-objs), $(obj-m)))
+MX_OBJS		:= $(sort $(filter     $(export-objs), $(obj-m)))
+MI_OBJS		:= $(sort $(filter-out $(export-objs), $(int-m)))
+MIX_OBJS	:= $(sort $(filter     $(export-objs), $(int-m)))
+
+# The global Rules.make.
+
+MSCD=$(OTG)/functions/msc
+
+OTG=$(TOPDIR)/drivers/otg
+include $(TOPDIR)/Rules.make
+EXTRA_CFLAGS +=  -I$(OTG) -Wno-unused -Wno-format  
+EXTRA_CFLAGS_nostdinc += -I$(OTG) -Wno-unused -Wno-format 
+
+# Link rules for multi-part drivers.
+
+msc_fd.o: $(msc_fd-objs)
+	$(LD) -r -o $@ $(msc_fd-objs)
+
+# dependencies:
+
+mass.o: $(USBDCORE_DIR)/usbd.h $(USBDCORE_DIR)/usbd-bus.h $(USBDCORE_DIR)/usbd-func.h
+
diff -uNr linux/drivers/no-otg/functions/msc/TODO.txt linux/drivers/otg/functions/msc/TODO.txt
--- linux/drivers/no-otg/functions/msc/TODO.txt	1970-01-01 01:00:00.000000000 +0100
+++ linux/drivers/otg/functions/msc/TODO.txt	2006-09-01 21:41:27.000000000 +0200
@@ -0,0 +1,118 @@
+MSC TODO List                                                   Stuart Lynne
+Belcarra                                        Mon Oct 25 00:51:12 PDT 2004 
+
+
+1. MSC documentation
+
+        - requirements and specifications
+        - comparison to mass storage specification, bulk-only and rbc
+        - implementation specifics
+        - rbc/spc commands
+        - sense keys
+        - control and usage
+
+2. PSC requirements
+
+        - notification to PSM layer for addroot and deleteroot
+
+
+3. expunge non Belcarra code and header files
+
+
+4. Insertion emulation
+
+        i. If not connected,
+                - do bdget(),
+                - do blkdev_get()
+                - set flags appropriately.
+
+        ii. If connected,
+                - ensure that there are no active requests (sleep if neccessary)
+                - do bdget()
+                - do blkdev_get()
+                - ensure that overlapped requests from the host while
+                  doing the open will not cause problems
+
+5. Removal emulation
+
+        i. If not connected
+                - reset flags appropriately
+
+        ii. If connected,
+                - wait for any active requests to complete (sleep if necessary),
+                - reset flags
+                - ensure that overlapped requests from host while
+                  resetting the flags will not cause problems
+                - wait for acknowledgement from host
+
+
+Note that all of the above must take place in the thread of execution of
+the ioctl call (i.e. a normal user thread or process.)
+
+The ioctl should not complete until all of the above is complete OR
+cannot complete and you return an error.
+
+
+6. ioctls
+
+Finish ioctl implementation.
+
+        start           start using block device (major, minor specified)
+        stop            stop using block device
+        status          return current connection status
+        connect         wait for connection
+        disconnect      wait for dis-connection
+        connected       return zero if connected, error otherwise
+        disconnected    return zero if dis-connected, error otherwise
+
+
+7. tests
+
+Get the tests in the scripts directory working. 
+
+These should verify the following:
+
+        1. connect/disconnect while started/stopped
+        2. start/stop while connected/disconnected
+       
+
+Or as two matrixes:
+
+                        Connected       Disconnected
+        Start
+        Stop
+
+
+                        Started         Stopped
+        Connect
+        DisConnect
+
+
+All valid combinations of starting, stopping, connecting and disconnecting
+must be verified.
+                
+
+
+8. Windows
+
+Get Windows test applicatoin working.
+
+9. Compatibility
+
+Test against:
+
+        WinXP
+        Win2k
+        Linux 2.4
+        (Linux 2.6)
+        (WinME)
+
+
+
+10. code review
+
+        - review other drivers to verify we have implemented all required 
+        rbc/spc-2 commands and sense codes
+
+11. prevent removal
+
diff -uNr linux/drivers/no-otg/functions/msc/crc.c linux/drivers/otg/functions/msc/crc.c
--- linux/drivers/no-otg/functions/msc/crc.c	1970-01-01 01:00:00.000000000 +0100
+++ linux/drivers/otg/functions/msc/crc.c	2006-09-01 21:41:27.000000000 +0200
@@ -0,0 +1,74 @@
+/*
+ * otg/msc_fd/crc.c - crc table
+ *
+ *      Copyright (c) 2003, 2004 Belcarra
+ *
+ * By: 
+ *      Chris Lynne <cl@belcarra.com>
+ *      Stuart Lynne <sl@belcarra.com>
+ *      Bruce Balden <balden@belcarra.com>
+ *
+ */
+
+
+#include <linux/config.h>
+#include <linux/module.h>
+#include <linux/kernel.h>
+#include <linux/version.h>
+
+
+#include <linux/init.h>
+#include <asm/uaccess.h>
+#include <linux/ctype.h>
+#include <linux/timer.h>
+#include <linux/interrupt.h>
+#include <asm/atomic.h>
+#include <linux/mm.h>
+#include <linux/slab.h>
+
+#include <otg/otg-compat.h>
+
+//#include <usbd-chap9.h>
+//#include <usbd-mem.h>
+//#include <usbd.h>
+//#include <usbd-func.h>
+
+#include "crc.h"
+
+unsigned int *msc_crc32_table;
+
+/**
+ * Generate the crc32 table
+ *
+ * return non-zero if malloc fails
+ */
+int make_crc_table(void)
+{
+        unsigned int n;
+        if (msc_crc32_table) return 0;
+        if (!(msc_crc32_table = (unsigned int *)ckmalloc(256*4, GFP_KERNEL))) return -EINVAL;
+        for (n = 0; n < 256; n++) {
+                int k;
+                unsigned int c = n;
+                for (k = 0; k < 8; k++) {
+                        c =  (c & 1) ? (CRC32_POLY ^ (c >> 1)) : (c >> 1);
+                }
+                msc_crc32_table[n] = c;
+        }
+        return 0;
+}
+
+void free_crc_table(void)
+{
+        if (msc_crc32_table) {
+                lkfree(msc_crc32_table);
+                msc_crc32_table = NULL;
+        }
+}
+
+unsigned int crc32_compute(unsigned char *src, int len, unsigned int val)
+{
+        for (; len-- > 0; val = COMPUTE_FCS (val, *src++));
+        return val;
+}
+
diff -uNr linux/drivers/no-otg/functions/msc/crc.h linux/drivers/otg/functions/msc/crc.h
--- linux/drivers/no-otg/functions/msc/crc.h	1970-01-01 01:00:00.000000000 +0100
+++ linux/drivers/otg/functions/msc/crc.h	2006-09-01 21:41:27.000000000 +0200
@@ -0,0 +1,29 @@
+/*
+ * otg/msc_fd/crc.c - crc table
+ *
+ *      Copyright (c) 2003, 2004 Belcarra
+ *
+ * By: 
+ *      Chris Lynne <cl@belcarra.com>
+ *      Stuart Lynne <sl@belcarra.com>
+ *      Bruce Balden <balden@belcarra.com>
+ *      Ted Powell <ted@belcarra.com>
+ *
+ *
+ */
+
+
+extern unsigned int *msc_crc32_table;
+
+#define CRC32_INIT   0xffffffff      // Initial FCS value
+#define CRC32_GOOD   0xdebb20e3      // Good final FCS value
+
+#define CRC32_POLY   0xedb88320      // Polynomial for table generation
+        
+#define COMPUTE_FCS(val, c) (((val) >> 8) ^ msc_crc32_table[((val) ^ (c)) & 0xff])
+
+int make_crc_table(void);
+void free_crc_table(void);
+unsigned int crc32_compute(unsigned char *src, int len, unsigned int val);
+
+
diff -uNr linux/drivers/no-otg/functions/msc/msc-bo.c linux/drivers/otg/functions/msc/msc-bo.c
--- linux/drivers/no-otg/functions/msc/msc-bo.c	1970-01-01 01:00:00.000000000 +0100
+++ linux/drivers/otg/functions/msc/msc-bo.c	2006-09-01 21:41:27.000000000 +0200
@@ -0,0 +1,176 @@
+/*
+ * otg/function/msc/msc-bo.c
+ *
+ *      Copyright (c) 2003, 2004 Belcarra
+ *
+ * By: 
+ *      Stuart Lynne <sl@belcarra.com>, 
+ *      Tom Rushworth <tbr@belcarra.com>, 
+ *      Bruce Balden <balden@belcarra.com>
+ *
+ */
+/*!
+ * @file otg/functions/msc/msc-bo.c
+ * @brief Mass Storage Driver private defines
+ *
+ * This is a Mass Storage Class Function that uses the Bulk Only protocol.
+ *
+ *
+ * @ingroup MSCFunction
+ */
+
+#include <otg/otg-compat.h>
+#include <otg/usbp-chap9.h>
+#include <otg/usbp-func.h>
+#include <otg/otg-trace.h>
+
+#include <linux/config.h>
+#include <linux/module.h>
+#include <linux/kernel.h>
+#include <linux/version.h>
+
+#include <linux/init.h>
+#include <asm/uaccess.h>
+#include <linux/ctype.h>
+#include <linux/timer.h>
+#include <linux/interrupt.h>
+#include <asm/atomic.h>
+#include <linux/random.h>
+#include <linux/slab.h>
+
+#include <linux/blkdev.h>
+
+
+#include "msc-scsi.h"
+#include "msc.h"
+#include "msc-fd.h"
+#include "crc.h"
+
+//#include "rbc.h"
+
+/*!
+ * Mass Storage Class - Bulk Only
+ *
+ * Endpoint, Class, Interface, Configuration and Device descriptors/descriptions
+ */
+
+static u8 msc_data_1[] = { 0x07, USB_DT_ENDPOINT, USB_DIR_OUT, BULK,     0, 0x00, 0x00,  };
+static u8 msc_data_2[] = { 0x07, USB_DT_ENDPOINT, USB_DIR_IN,  BULK,     0, 0x00, 0x00, };
+static struct usbd_endpoint_descriptor *msc_default[] = { 
+        (struct usbd_endpoint_descriptor *) msc_data_1, 
+        (struct usbd_endpoint_descriptor *) msc_data_2, };
+u8 msc_indexes[] = { BULK_OUT, BULK_IN, };
+
+
+/*! Endpoint, Interface, Configuration and Device descriptions and descriptors
+ */
+static u8 msc_data_alternate_descriptor[sizeof(struct usbd_interface_descriptor)] = {
+        0x09, USB_DT_INTERFACE, 
+        0x00, 0x00, // bInterfaceNumber, bAlternateSetting
+        sizeof (msc_default) / sizeof(struct usbd_endpoint_descriptor), // bNumEndpoints
+        MASS_STORAGE_CLASS,
+        MASS_STORAGE_SUBCLASS_SCSI,
+        MASS_STORAGE_PROTO_BULK_ONLY,
+        0x00,
+};
+
+static struct usbd_alternate_description msc_data_alternate_descriptions[] = {
+        { iInterface: CONFIG_OTG_MSC_INTF,
+                interface_descriptor: (struct usbd_interface_descriptor *)&msc_data_alternate_descriptor,
+                endpoints:sizeof (msc_default) / sizeof(struct usbd_endpoint_descriptor *),
+                endpoint_list: msc_default,
+                endpoint_indexes: msc_indexes,
+                },
+};
+
+
+struct usbd_interface_description msc_interfaces[] = {
+        { alternates:sizeof (msc_data_alternate_descriptions) / sizeof (struct usbd_alternate_description),
+                alternate_list:msc_data_alternate_descriptions,},
+};
+
+u8 msc_configuration_descriptor[sizeof(struct usbd_configuration_descriptor)] = {
+        0x09, USB_DT_CONFIGURATION, 0x00, 0x00, // wLength
+        sizeof (msc_interfaces) / sizeof (struct usbd_interface_description),
+        0x01, 0x00, // bConfigurationValue, iConfiguration
+        0, 0,
+};
+
+struct usbd_configuration_description msc_description[] = {
+        { iConfiguration: CONFIG_OTG_MSC_DESC,
+                configuration_descriptor: (struct usbd_configuration_descriptor *)msc_configuration_descriptor,
+        },
+};
+
+
+static struct usbd_device_descriptor msc_device_descriptor = {
+        bLength: sizeof(struct usbd_device_descriptor),
+        bDescriptorType: USB_DT_DEVICE,
+        bcdUSB: __constant_cpu_to_le16(USB_BCD_VERSION),
+        bDeviceClass: 0x00,
+        bDeviceSubClass: 0x02,
+        bDeviceProtocol: 0x00,
+        bMaxPacketSize0: 0x00,
+        idVendor: __constant_cpu_to_le16(CONFIG_OTG_MSC_VENDORID),
+        idProduct: __constant_cpu_to_le16(CONFIG_OTG_MSC_PRODUCTID),
+        bcdDevice: __constant_cpu_to_le16(CONFIG_OTG_MSC_BCDDEVICE),
+};
+
+#ifdef CONFIG_OTG_HIGH_SPEED
+static struct usbd_device_qualifier_descriptor msc_device_qualifier_descriptor = {
+        bLength: sizeof(struct usbd_device_qualifier_descriptor),
+        bDescriptorType: USB_DT_DEVICE_QUALIFIER,
+        bcdUSB: __constant_cpu_to_le16(USB_BCD_VERSION),
+        bDeviceClass: 0x00,
+        bDeviceSubClass: 0x02,
+        bDeviceProtocol: 0x00,
+        bMaxPacketSize0: 0x00,
+};
+#endif /* CONFIG_OTG_HIGH_SPEED */
+
+
+
+
+static struct usbd_endpoint_request msc_endpoint_requests[ENDPOINTS+1] = {
+        { 1, 0, 0, USB_DIR_OUT | USB_ENDPOINT_BULK, PAGE_SIZE, PAGE_SIZE, },
+        { 1, 0, 0, USB_DIR_IN | USB_ENDPOINT_BULK, PAGE_SIZE, PAGE_SIZE, },
+        { 0, },
+};
+
+struct usbd_otg_descriptor msc_otg_descriptor = {
+        bLength : sizeof(struct usbd_otg_descriptor),
+        bDescriptorType: USB_DT_OTG,
+        bmAttributes: 0,
+};
+
+struct usbd_device_description msc_device_description = {
+        device_descriptor: &msc_device_descriptor,
+#ifdef CONFIG_OTG_HIGH_SPEED
+        device_qualifier_descriptor: &msc_device_qualifier_descriptor,
+#endif /* CONFIG_OTG_HIGH_SPEED */
+        otg_descriptor: &msc_otg_descriptor,
+        iManufacturer: CONFIG_OTG_MSC_MANUFACTURER,
+        iProduct: CONFIG_OTG_MSC_PRODUCT_NAME,
+#if !defined(CONFIG_OTG_NO_SERIAL_NUMBER) && defined(CONFIG_OTG_SERIAL_NUMBER_STR)
+        iSerialNumber: CONFIG_OTG_SERIAL_NUMBER_STR, 
+#endif
+};
+
+
+extern struct usbd_function_operations msc_function_ops;
+
+struct usbd_function_driver msc_function_driver = {
+        name: "msc-bulkonly",
+        fops:&msc_function_ops,
+        device_description:&msc_device_description,
+        bNumConfigurations:sizeof (msc_description) / sizeof (struct usbd_configuration_description),
+        configuration_description:msc_description,
+        idVendor: __constant_cpu_to_le16(CONFIG_OTG_MSC_VENDORID),
+        idProduct: __constant_cpu_to_le16(CONFIG_OTG_MSC_PRODUCTID),
+        bcdDevice: __constant_cpu_to_le16(CONFIG_OTG_MSC_BCDDEVICE),
+        bNumInterfaces:sizeof (msc_interfaces) / sizeof (struct usbd_interface_description),
+        interface_list:msc_interfaces,
+        endpointsRequested: ENDPOINTS,
+        requestedEndpoints: msc_endpoint_requests,
+};
+
diff -uNr linux/drivers/no-otg/functions/msc/msc-fd.c linux/drivers/otg/functions/msc/msc-fd.c
--- linux/drivers/no-otg/functions/msc/msc-fd.c	1970-01-01 01:00:00.000000000 +0100
+++ linux/drivers/otg/functions/msc/msc-fd.c	2006-09-01 21:41:27.000000000 +0200
@@ -0,0 +1,1233 @@
+/*
+ * otg/function/msc/msc.-fd.cc
+ *
+ *      Copyright (c) 2003, 2004 Belcarra
+ *
+ * By:
+ *      Stuart Lynne <sl@belcarra.com>,
+ *      Tom Rushworth <tbr@belcarra.com>,
+ *      Bruce Balden <balden@belcarra.com>
+ *
+ */
+
+/*!
+ * @file otg/functions/msc/msc-fd.c
+ * @brief Mass Storage Driver private defines
+ *
+ * This is a Mass Storage Class Function that uses the Bulk Only protocol.
+ *
+ *
+ * Notes:
+ *
+ * 1. Currently we only support the Bulk Only model. Microsoft states that
+ * further support for the mass storage driver will only be done for devices
+ * that conform to the Bulk Only model.
+ *
+ * 2. Multiple LUN's are not supported, but in theory they could be.
+ *
+ * 3. Error handling should be done with STALL but using ZLP seems to also
+ * work. ZLP is usually easier to implement (except possibly on the SA1100.)
+ * We may need to make STALL an option if we find devices (perhaps SA1100)
+ * that cannot reliaby send a ZLP on BULK-IN endpoint.
+ *
+ *
+ * 4. WinXP will match the following:
+ *
+ *      USB: Class_08&SubClass_02&Prot_50
+ *      USB: Class_08&SubClass_05&Prot_50
+ *      USB: Class_08&SubClass_06&Prot_50
+ *
+ * SubClass 02 is MMC or SFF8020I
+ * SubClass 05 is SFF or SFF8070I
+ * SubClass 06 is SCSI
+ *
+ * From the Windows USB Storage FAQ:
+ *
+ *      RBC not supported
+ *
+ *      SubClass = 6 (SCSI)
+ *              CDBs SHOULD NOT be padded to 12 bytes
+ *              ModeSense/ModeSelect SHOULD NOT be translated from 1ah/15h to 5ah/55h
+ *              should be used for FLASH
+ *
+ *      SubClass !=6
+ *              CDBs SHOULD be padded to 12 bytes
+ *              ModeSense/ModeSelect SHOULD be translated from 1ah/15h to 5ah/55h
+ *
+ * We are using the former, SubClass = 6, and implement the required SCSI operations.
+ *
+ * 
+ * TODO
+ *
+ *
+ * 
+ * TODO FIXME Bus Interface Notes
+ *
+ * 1. The bus interface driver must correctly implement NAK'ing if not rcv_urb is
+ * present (see au1x00.c or wmmx.c for examples.)
+ *
+ * 2. The bus interface driver must implement USBD_URB_SENDZLP flag (see au1x00.c 
+ * for example.)
+ *
+ * @ingroup MSCFunction
+ */
+
+
+#include <otg/otg-compat.h>
+#include <otg/usbp-chap9.h>
+#include <otg/usbp-func.h>
+#include <otg/otg-trace.h>
+
+#include <linux/config.h>
+#include <linux/module.h>
+#include <linux/kernel.h>
+#include <linux/version.h>
+
+#include <linux/init.h>
+#include <asm/uaccess.h>
+#include <linux/ctype.h>
+#include <linux/timer.h>
+#include <linux/interrupt.h>
+#include <asm/atomic.h>
+#include <linux/random.h>
+#include <linux/slab.h>
+
+#include <linux/blkdev.h>
+
+
+#include "msc-scsi.h"
+#include "msc.h"
+#include "msc-fd.h"
+#include "crc.h"
+
+
+/* Module Parameters ************************************************************************* */
+
+
+#define DEVICE_EJECTED          0x0001          // MEDIA_EJECTED
+#define DEVICE_INSERTED         0x0002          // MEDIA_INSERT
+
+#define DEVICE_OPEN             0x0004          // WR_PROTECT_OFF
+#define DEVICE_WRITE_PROTECTED  0x0008          // WR_PROTECT_ON
+
+#define DEVICE_CHANGE_ON        0x0010          // MEDIA_CHANGE_ON
+
+#define DEVICE_PREVENT_REMOVAL  0x0020
+
+
+#define DEF_NUMBER_OF_HEADS     0x10
+#define DEF_SECTORS_PER_TRACK   0x20
+
+
+
+/* MSC ******************************************************************************************** */
+
+extern struct msc_private msc_private;
+
+int msc_urb_sent (struct usbd_urb *tx_urb, int rc);
+static int msc_recv_urb (struct usbd_urb *urb, int rc);
+
+otg_tag_t msc_fd_trace_tag;
+
+/* Sense Key *********************************************************************************** */
+
+/*! set_sense_data - set sensedata in msc private struture
+ * @param msc
+ * @param sensedata
+ * @param info
+ *
+ * This saves sense data. Sense data indicates what type of error
+ * has occurred and will be returned to the host when a request sense
+ * command is sent.
+ */
+void set_sense_data(struct msc_private *msc, u32 sensedata, u32 info)
+{
+        TRACE_SENSE(sensedata, info);
+        msc->sensedata = sensedata;
+        msc->info = info;
+}
+
+/* Check blockdev ****************************************************************************** */
+
+/*! msc_check_blockdev_name - check current status of the block device
+ *
+ * Check if the block device is operational, either generate a failed CSW 
+ * or a ZLP if not ready.
+ *
+ * Returns non-zero if the block device is not available for I/O operations
+ * and a failed CSW has already been sent.
+ */
+int msc_check_blockdev_name(struct msc_private *msc, int zlp, char *name)
+{
+        if (msc->block_dev_state & DEVICE_EJECTED) {
+                TRACE_MSG0(MSC,"CHECK BLOCKDEV DEVICE_EJECTED");
+                ((SENDZLP == zlp) ? msc_dispatch_query_urb_zlp : msc_start_sending_csw_failed)
+                        (msc, SCSI_SENSEKEY_MEDIA_NOT_PRESENT, msc->lba, USB_MSC_FAILED);
+                return -EINVAL;
+        }
+        if (msc->block_dev_state & DEVICE_CHANGE_ON) {
+                msc->block_dev_state &= ~DEVICE_CHANGE_ON;
+                TRACE_MSG0(MSC,"CHECK BLOCKDEV DEVICE_CHANGE_ON");
+                ((SENDZLP == zlp) ? msc_dispatch_query_urb_zlp : msc_start_sending_csw_failed)
+                        (msc, SCSI_SENSEKEY_NOT_READY_TO_READY_CHANGE, msc->lba, USB_MSC_FAILED);
+                return -EINVAL;
+        }
+        //TRACE_MSG0(MSC,"CHECK BLOCKDEV DEVICE_INSERTED");
+        return 0;
+}
+
+/* Generic start recv urb and send csw ********************************************************* */
+
+/*! msc_start_recv - queue a receive urb
+ *
+ * Ensure that size is a multiple of the endpoint packetsize.
+ *
+ * Returns non-zero if there is an error in the USB layer.
+ */
+int msc_start_recv_urb(struct usbd_function_instance *function, struct msc_private *msc, int size)
+{
+        struct usbd_urb *rcv_urb = NULL;
+        int wMaxPacketSize = usbd_endpoint_wMaxPacketSize(function, BULK_OUT, usbd_high_speed(function));
+        if ((size % wMaxPacketSize)) 
+                size = ((size + wMaxPacketSize) / wMaxPacketSize) * wMaxPacketSize;
+
+        RETURN_EINVAL_UNLESS((rcv_urb = usbd_alloc_urb (function, BULK_OUT, size, msc_recv_urb)));
+        rcv_urb->function_privdata = msc;
+        msc->rcv_urb_finished = NULL;
+        RETURN_ZERO_UNLESS(usbd_start_out_urb(rcv_urb));
+        TRACE_MSG0(MSC,"START RECV URB ERROR"); 
+        usbd_free_urb(rcv_urb);
+        return -EINVAL;
+}
+
+/*! msc_start_sending - start sending a new data or csw urb
+ *
+ * Generate a CSW (Command Status Wrapper) to send to the the host.
+ *
+ * Returns non-zero if there is an error in the USB layer.
+ */
+int msc_start_sending_csw(struct usbd_function_instance *function, struct msc_private *msc, u8 status)
+{
+        COMMAND_STATUS_WRAPPER *csw;
+        int length = sizeof(COMMAND_STATUS_WRAPPER);
+        struct usbd_urb *tx_urb;
+
+        //TRACE_MSG1(MSC,"START SENDING CSW %08x", status); 
+
+        msc->command_state = MSC_STATUS;
+
+        RETURN_EINVAL_UNLESS((tx_urb = usbd_alloc_urb (function, BULK_IN, length, msc_urb_sent)));
+
+        tx_urb->actual_length = length;
+        tx_urb->function_privdata = msc;
+
+        // fill in CSW and queue the urb
+        csw = (COMMAND_STATUS_WRAPPER *) tx_urb->buffer;
+        csw->dCSWSignature = CSW_SIGNATURE;
+        csw->dCSWTag = msc->command.dCBWTag;
+        csw->dCSWDataResidue = msc->command.dCBWDataTransferLength - msc->data_transferred_in_bytes;
+        csw->bCSWStatus = status;
+
+        TRACE_MSG2(MSC,"START SENDING CSW status: %02x data residue: %d", status, csw->dCSWDataResidue); 
+
+        RETURN_ZERO_UNLESS(usbd_start_in_urb (tx_urb));
+        TRACE_MSG0(MSC,"START SENDING CSW FAILED");
+        usbd_free_urb (tx_urb);
+        return -EINVAL;
+}
+
+/*! msc_start_sending_csw_failed - starting sending a CSW showing failure
+ *
+ * Sets sensedata and generates a CSW with status set to USB_MSC_FAILED.
+ *
+ * Returns non-zero if there is an error in the USB layer.
+ */
+int msc_start_sending_csw_failed(struct msc_private *msc, u32 sensedata, u32 info, int status)
+{
+        TRACE_MSG2(MSC, "sensedata: %x status: %x", sensedata, status);
+        set_sense_data(msc, sensedata, info);
+        return msc_start_sending_csw(msc->function, msc, status);
+}
+
+
+/* ********************************************************************************************* */
+
+/*! msc_alloc_urb - allocate an urb for returning a query
+ *
+ * Returns NULL if there is an error in the USB layer.
+ */
+struct usbd_urb * msc_alloc_urb(struct msc_private *msc, int length)
+{
+        struct usbd_function_instance *function;
+        struct usbd_urb *urb;
+
+        THROW_IF(!(function = msc->function), error);
+        THROW_IF(!(urb = usbd_alloc_urb (function, BULK_IN, length, msc_urb_sent)), error);
+        return urb;
+        CATCH(error) {
+                msc->command_state = MSC_READY;
+                return NULL;
+        }
+}
+
+/*! msc_dispatch_query_urb - dispatch an urb containing query data
+ *
+ * Returns non-zero if there is an error in the USB layer.
+ */
+int msc_dispatch_query_urb(struct usbd_urb *urb, struct msc_private *msc, int length, int status)
+{
+        int rc;
+        unsigned long flags;
+
+        TRACE_MSG1(MSC,"DISPATCH URB len: %d", length);
+
+        // save information in msc and urb
+        //
+        urb->function_privdata = msc;
+        urb->actual_length = msc->TransferLength_in_bytes = length;
+
+        // dispatch urb
+        local_irq_save(flags);
+        if ((rc = usbd_start_in_urb (urb))) {
+
+                TRACE_MSG0(MSC,"DISPATCH URB FAILED");
+                usbd_free_urb (urb);
+
+                // stall?
+                msc->command_state = MSC_READY;
+                local_irq_restore(flags);
+                return -EINVAL;
+        }
+        msc->command_state = MSC_QUERY;
+        msc->status = status;
+        local_irq_restore(flags);
+        return 0;
+}
+
+/*! msc_dispatch_query_urb_zlp - send a ZLP
+ *
+ * Returns non-zero if there is an error in the USB layer.
+ */
+int msc_dispatch_query_urb_zlp(struct msc_private *msc, u32 sensedata, u32 info, int status)
+{
+        struct usbd_urb *urb;
+        TRACE_MSG2(MSC, "sensedata: %x status: %x", sensedata, status);
+        RETURN_EINVAL_IF(!(urb = msc_alloc_urb(msc, 1))); 
+        set_sense_data(msc, sensedata, info);
+        urb->flags |= USBD_URB_SENDZLP;
+        return msc_dispatch_query_urb(urb, msc, 0, status);
+}
+
+/* READ 10 COMMAND - read and send data to the host ******************************************** */
+extern int msc_scsi_read_10(struct msc_private *msc, char *name, int op);
+extern int msc_in_read_10_urb_sent(struct usbd_urb *tx_urb, struct msc_private *msc);
+
+/* WRITE 10 - receive data from host and write to block device ********************************* */
+extern int msc_scsi_write_10(struct msc_private *msc, char *name, int op);
+extern void msc_recv_out_blocks(struct usbd_urb *rcv_urb, struct msc_private *msc);
+
+/* SCSI Commands ******************************************************************************* */
+
+/*! msc_scsi_inquiry - process an inquiry
+ *
+ * Used by:
+ *      win2k
+ *
+ * Returns non-zero if there is an error in the USB layer.
+ */
+int msc_scsi_inquiry(struct msc_private *msc, char *name, int op) 
+{
+        SCSI_INQUIRY_COMMAND *command = (SCSI_INQUIRY_COMMAND *)&msc->command.CBWCB;
+        SCSI_INQUIRY_DATA *data;
+        struct usbd_urb *urb;
+        int length = sizeof(SCSI_INQUIRY_DATA);
+
+        /*
+         * C.f. SPC2 7.3 INQUIRY command
+         * C.f. Table 46 - Standard INQUIRY data format
+         *
+         * C.f. Table 47 - Peripheral Qualifier
+         *
+         * 000b The specified peripheral device type is currently connected to this
+         *      logical unit.....
+         * 001b The device server is capable of of supporting the peripheral device
+         *      type on this logical unit. However, the physical device is not currently 
+         *      connected to this logical unit.....
+         * 010b Reserved
+         * 011b The device server is not capable of supporting a physical device on
+         *      this logical unit....
+         *      
+         */
+
+        TRACE_MSG4(MSC,"INQUIRY EnableVPD: %02x LogicalUnitNumber: %02x PageCode: %02x AllocLen: %02x",
+                        command->EnableVPD, command->LogicalUnitNumber, command->PageCode, command->AllocationLength);
+
+        // XXX THROW_IF(msc->command_state != MSC_READY, error);
+
+        RETURN_EINVAL_IF(!(urb = msc_alloc_urb(msc, length)));
+
+        data = (SCSI_INQUIRY_DATA *)urb->buffer;
+        data->PeripheralQaulifier = msc->block_dev_state & (DEVICE_EJECTED | DEVICE_CHANGE_ON) ? 0x1 : 0;
+        data->PeripheralDeviceType = 0x00;
+        data->RMB = 0x1;
+        data->ResponseDataFormat = 0x1;
+        data->AdditionalLength = 0x1f;
+        
+        strncpy(data->VendorInformation, CONFIG_OTG_MSC_MANUFACTURER, strlen(CONFIG_OTG_MSC_MANUFACTURER));
+        strncpy(data->ProductIdentification, CONFIG_OTG_MSC_PRODUCT_NAME, strlen(CONFIG_OTG_MSC_PRODUCT_NAME));
+
+        return msc_dispatch_query_urb(urb, msc, length, USB_MSC_PASSED);
+}
+
+/*! msc_scsi_read_format_capacity - process a query
+ *
+ * Used by:
+ *      win2k
+ *
+ * Returns non-zero if there is an error in the USB layer.
+ */
+int msc_scsi_read_format_capacity(struct msc_private *msc, char *name, int op) 
+{
+        SCSI_READ_FORMAT_CAPACITY_DATA *data;
+        struct usbd_urb *urb;
+        int length = sizeof(SCSI_READ_FORMAT_CAPACITY_DATA);
+        u32 block_num = msc->capacity;
+        u32 block_len;
+
+        RETURN_EINVAL_IF(!(urb = msc_alloc_urb(msc, length))); 
+
+        data = (SCSI_READ_FORMAT_CAPACITY_DATA *) urb->buffer;
+
+        data->CapacityListHeader.CapacityListLength = sizeof(data->CurrentMaximumCapacityDescriptor);
+
+        data->CurrentMaximumCapacityDescriptor.NumberofBlocks = block_num;
+        data->CurrentMaximumCapacityDescriptor.DescriptorCode = 0x03;
+        memcpy(data->CurrentMaximumCapacityDescriptor.BlockLength + 1, &block_len, sizeof(block_len));
+
+        return msc_dispatch_query_urb(urb, msc, length, USB_MSC_PASSED);
+}
+
+/*! msc_read_capacity - process a read_capacity command
+ *
+ * Used by:
+ *      win2k
+ *
+ * Returns non-zero if there is an error in the USB layer.
+ */
+int msc_scsi_read_capacity(struct msc_private *msc, char *name, int op) 
+{
+        SCSI_READ_CAPACITY_COMMAND *command = (SCSI_READ_CAPACITY_COMMAND *)&msc->command.CBWCB;
+        SCSI_READ_CAPACITY_DATA *data;
+        struct usbd_urb *urb;
+        int length = 8;
+        u32 lba;
+
+        /*
+         * C.f. RBC 5.3
+         */
+        lba = be32_to_cpu(command->LogicalBlockAddress);
+
+        TRACE_MSG1(MSC,"READ CAPACITY LBA: %d", lba);
+
+        if ((command->PMI > 1) || (!command->PMI && lba)) {
+                TRACE_MSG1(MSC,"READ CAPACITY PMI: %d", command->PMI);
+                return msc_start_sending_csw_failed (msc, SCSI_SENSEKEY_INVALID_FIELD_IN_CDB, lba, USB_MSC_FAILED);
+        }
+
+        // alloc urb
+        RETURN_EINVAL_IF(!(urb = msc_alloc_urb(msc, length)));
+
+        data = (SCSI_READ_CAPACITY_DATA *) urb->buffer;
+
+        data->LastLogicalBlockAddress = cpu_to_be32(msc->capacity);
+        data->BlockLengthInBytes = cpu_to_be32(msc->block_size);
+
+        TRACE_MSG2(MSC,"RECV READ CAPACITY lba: %x block_size: %x", 
+                        be32_to_cpu(data->LastLogicalBlockAddress), be32_to_cpu(data->BlockLengthInBytes));
+
+        return msc_dispatch_query_urb(urb, msc, length, USB_MSC_PASSED);
+}
+
+
+/*! msc_scsi_request_sense - process a request_sense command
+ *
+ * Returns non-zero if there is an error in the USB layer.
+ */
+int msc_scsi_request_sense(struct msc_private *msc, char *name, int op)
+{
+        SCSI_REQUEST_SENSE_COMMAND*    command = (SCSI_REQUEST_SENSE_COMMAND *)&msc->command.CBWCB;
+        SCSI_REQUEST_SENSE_DATA *data;
+
+        /*
+         * C.f. SPC2 7.20 REQUEST SENSE command
+         */
+
+        struct usbd_urb *urb;
+        int length = sizeof(SCSI_REQUEST_SENSE_DATA);
+
+        // alloc urb
+        RETURN_EINVAL_IF(!(urb = msc_alloc_urb(msc, length)));
+
+        data = (SCSI_REQUEST_SENSE_DATA *) urb->buffer;
+        memset(command, 0x0, length);
+        data->ErrorCode = SCSI_ERROR_CURRENT;
+        data->SenseKey = msc->sensedata >> 16;
+        data->AdditionalSenseLength = 0xa; /* XXX is this needed */
+        data->AdditionalSenseCode = msc->sensedata >> 8;
+        data->AdditionalSenseCodeQualifier = msc->sensedata;
+        data->Valid = 1;
+
+        set_sense_data(msc, SCSI_SENSEKEY_NO_SENSE, 0);
+
+        return msc_dispatch_query_urb(urb, msc, length, USB_MSC_PASSED);
+}
+
+/*! msc_scsi_mode_sense - process a request_sense command
+ *
+ * Used by:
+ *      win2k
+ *
+ * Returns non-zero if there is an error in the USB layer.
+ */
+int msc_scsi_mode_sense(struct msc_private *msc, char *name, int op)
+{
+        SCSI_MODE_SENSE_COMMAND *command = (SCSI_MODE_SENSE_COMMAND *)&msc->command.CBWCB;
+        SCSI_MODE_SENSE_DATA *data;
+        int length = sizeof(SCSI_MODE_SENSE_DATA);
+
+        struct usbd_urb *urb;
+        u8 *cp;
+
+        TRACE_MSG4(MSC,"MODE SENSE dbd: %02x PageControl: %02x PageCode: %02x Alloc: %02x", 
+                        command->DBD, command->PageControl, command->PageCode, 0);
+        length = 8;
+
+        // alloc urb
+        RETURN_EINVAL_IF(!(urb = msc_alloc_urb(msc, length)));
+
+        cp = (u8 *) urb->buffer;
+        memset(cp, 0x0, length);
+
+        cp[0] = 0;
+        cp[1] = 0;
+        cp[2] = 0;              // 0x80 is writeprotect
+        cp[3] = 0x08;
+        cp[4] = 0;
+        cp[5] = 0;
+        cp[6] = 0;
+        cp[7] = 0;
+
+        return msc_dispatch_query_urb(urb, msc, length, USB_MSC_PASSED);
+}
+
+/*! msc_scsi_mode_sense - process a request_sense command
+ *
+ * Used by:
+ *      win2k
+ *
+ * XXX this doesn't work, need to re-implement and add these pages.
+ *
+ * Returns non-zero if there is an error in the USB layer.
+ */
+int new_msc_scsi_mode_sense(struct msc_private *msc, char *name, int op)
+{
+
+        /*
+         * C.f. SPC2 7.8.1 MODE SENSE(6) command
+         */
+
+        static READ_WRITE_ERROR_RECOVERY_PAGE           page_01 = {
+                PageCode:0x01,
+                PageLength:0x0A,
+                ReadRetryCount:0x03,
+                WriteRetryCount:0x80,
+        };
+        static FLEXIBLE_DISK_PAGE                       page_05 = {
+                PageCode:0x05,
+                PageLength:0x1E,
+                TransferRate:__constant_cpu_to_be16(0xFA00),
+                NumberofHeads:0xA0,
+                SectorsperTrack:0x00,
+                DataBytesperSector:__constant_cpu_to_be16(0x0002),
+                NumberofCylinders:__constant_cpu_to_be16(0x0000),
+                MotorOnDelay:0x05,
+                MotorOffDelay:0x1E,
+                MediumRotationRate:__constant_cpu_to_be16(0x6801),
+        };
+        static REMOVABLE_BLOCK_ACCESS_CAPABILITIES_PAGE page_1b = {
+                PageCode:0x1B,
+                PageLength:0x0A,
+                TLUN:0x01,
+        };
+        static TIMER_AND_PROTECT_PAGE                   page_1c = {
+                PageCode:0x1c,
+                PageLength:0x06,
+                InactivityTimeMultiplier:0x0A,
+        };
+
+        SCSI_MODE_SENSE_COMMAND *command = (SCSI_MODE_SENSE_COMMAND *)&msc->command.CBWCB;
+        SCSI_MODE_SENSE_DATA *data;
+        struct usbd_urb *urb;
+        int length = sizeof(SCSI_MODE_SENSE_DATA);
+
+
+        TRACE_MSG4(MSC,"MODE SENSE dbd: %02x PageControl: %02x PageCode: %02x Alloc: %02x", 
+                        command->DBD, command->PageControl, command->PageCode, 0);
+
+
+        if (msc->block_dev_state & DEVICE_EJECTED) {
+                u16 sector   = htons((unsigned short)msc->block_size);
+                u16 cylinder = 0;
+                page_05.NumberofHeads = 0;
+                page_05.SectorsperTrack = 0;
+                memcpy(&page_05.DataBytesperSector, &sector, sizeof(sector));
+                memcpy(&page_05.NumberofCylinders, &cylinder, sizeof(cylinder));
+        }
+        else {
+                u16 sector   = htons((unsigned short)msc->block_size);
+                u32 size = DEF_NUMBER_OF_HEADS * DEF_SECTORS_PER_TRACK * sector;
+                u16 cylinder = htons(msc->capacity / size);
+                page_05.NumberofHeads = DEF_NUMBER_OF_HEADS;
+                page_05.SectorsperTrack = DEF_SECTORS_PER_TRACK;
+                memcpy(&page_05.DataBytesperSector, &sector, sizeof(sector));
+                memcpy(&page_05.NumberofCylinders, &cylinder, sizeof(cylinder));
+        }
+
+        if (SCSI_MODEPAGE_CONTROL_CURRENT != command->PageControl) {
+                TRACE_MSG2(MSC,"MODE SENSE - requeested other than CONTROL_CURRENT: %d %d", 
+                                command->PageControl, command->PageCode);
+                return msc_start_sending_csw_failed (msc, SCSI_SENSEKEY_INVALID_FIELD_IN_CDB, msc->lba, USB_MSC_FAILED);
+        }
+
+        RETURN_EINVAL_IF(!(urb = msc_alloc_urb(msc, length)));
+        data = (SCSI_MODE_SENSE_DATA *) urb->buffer;
+
+        data->ModeParameterHeader.WriteProtect = msc->block_dev_state & DEVICE_WRITE_PROTECTED ? 1 : 0;
+
+        switch (command->PageCode) {
+        case SCSI_MODEPAGE_ERROR_RECOVERY:
+                TRACE_MSG0(MSC, "MODEPAGE ERROR_RECOVERY");
+                length = sizeof(MODE_PARAMETER_HEADER) + sizeof(page_01);
+                data->ModeParameterHeader.ModeDataLength = length - 1;
+                memcpy(&data->ModePages, &page_01, sizeof(page_01));
+                break;
+        case SCSI_MODEPAGE_FLEXIBLE_DISK_PAGE:
+                TRACE_MSG0(MSC, "MODEPAGE FLEXIBLE_DISK_PAGE");
+                length = sizeof(MODE_PARAMETER_HEADER) + sizeof(page_05);
+                data->ModeParameterHeader.ModeDataLength = length - 1;
+                memcpy(&data->ModePages, &page_05, sizeof(page_05));
+                break;
+        case SCSI_MODEPAGE_REMOVABLE_BLOCK_ACCESS:
+                TRACE_MSG0(MSC, "MODEPAGE REMOVABLE_BLOCK_ACCESS");
+                length = sizeof(MODE_PARAMETER_HEADER) + sizeof(page_1b);
+                data->ModeParameterHeader.ModeDataLength = length - 1;
+                memcpy(&data->ModePages, &page_1b, sizeof(page_1b));
+                break;
+        case SCSI_MODEPAGE_INFORMATION_EXCEPTIONS:
+                TRACE_MSG0(MSC, "MODEPAGE INFORMATION_EXCEPTIONS");
+                length = sizeof(MODE_PARAMETER_HEADER) + sizeof(page_1c);
+                data->ModeParameterHeader.ModeDataLength = length - 1;
+                memcpy(&data->ModePages, &page_1c, sizeof(page_1c));
+                break;
+        case SCSI_MODEPAGE_ALL_SUPPORTED:
+                TRACE_MSG0(MSC, "MODEPAGE ALL_SUPPORTED");
+                length = sizeof(MODE_PARAMETER_HEADER) + sizeof(MODE_ALL_PAGES);
+                data->ModeParameterHeader.ModeDataLength = length - 1;
+                memcpy(&data->ModePages.ModeAllPages.ReadWriteErrorRecoveryPage, &page_01, sizeof(page_01));
+                memcpy(&data->ModePages.ModeAllPages.FlexibleDiskPage, &page_05, sizeof(page_05));
+                memcpy(&data->ModePages.ModeAllPages.RemovableBlockAccessCapabilitiesPage, &page_1b, sizeof(page_1b));
+                memcpy(&data->ModePages.ModeAllPages.TimerAndProtectPage, &page_1c, sizeof(page_1c));
+                break;
+        case SCSI_MODEPAGE_CACHING:
+                // see file_storage.c for an example if we want to support this
+                TRACE_MSG0(MSC, "MODEPAGE CACHING (not supported)");
+                break;
+        }
+
+        TRACE_MSG2(MSC,"LENGTH: %d %d", msc->command.dCBWDataTransferLength, length);
+
+        /*
+         * XXX verify that length is <= 256 bytes, return CHECK_CONDITION if it is
+         */
+        length = MIN(msc->command.dCBWDataTransferLength, length);
+
+        return msc_dispatch_query_urb(urb, msc, length, USB_MSC_PASSED);
+}
+
+
+/*! msc_scsi_test_unit_ready - process a request_sense command
+ *
+ * Used by:
+ *      win2k
+ *
+ * Returns non-zero if there is an error in the USB layer.
+ */
+int msc_scsi_test_unit_ready(struct msc_private *msc, char *name, int op)
+{
+        return msc_start_sending_csw(msc->function, msc, USB_MSC_PASSED);
+}
+
+
+/*! msc_scsi_prevent_allow - process a request_sense command
+ *
+ * Used by:
+ *      win2k
+ *
+ * Returns non-zero if there is an error in the USB layer.
+ */
+int msc_scsi_prevent_allow(struct msc_private *msc, char *name, int op)
+{
+        SCSI_PREVENT_ALLOW_MEDIA_REMOVAL_COMMAND* command = (SCSI_PREVENT_ALLOW_MEDIA_REMOVAL_COMMAND*)&msc->command.CBWCB;
+
+        /*
+         * C.f. SPC2 7.12 Table 78 PREVENT ALLOW MEDIA REMOVAL Prevent Field
+         *
+         * 00b  Medium removal shall be allowed from both the data transport
+         *      element and the attached medium changer (if any).
+         * 01b  Medium removal shall be prohibited from the data transport
+         *      element but allowed from the attached medium changer (if any).
+         * 10b  Medium removal shall be allowed for the data transport element
+         *      but prohibited for the attached medium changer.
+         * 11b  Medium remval shall be prohibited from both the data transport
+         *      element and the attached medium changer
+         *
+         * Prevention shall terminate after 00b or 10b, after a SYNC CACHE or hard reset.
+         */
+
+        // XXX TODO 
+        // this is from storageproto.c, shouldn't we implement something?
+        if (command->Prevent) 
+                return msc_start_sending_csw_failed (msc, SCSI_SENSEKEY_INVALID_FIELD_IN_CDB, msc->lba, USB_MSC_FAILED);
+
+        return msc_start_sending_csw(msc->function, msc, USB_MSC_PASSED);
+}
+
+
+/*! msc_scsi_start_stop - process a request_sense command
+ *
+ * C.f. RBC 5.4 and 5.4.2
+ *
+ * Used by:
+ *      win2k
+ *
+ * Returns non-zero if there is an error in the USB layer.
+ */
+int msc_scsi_start_stop(struct msc_private *msc, char *name, int op)
+{
+        SCSI_START_STOP_COMMAND* command = (SCSI_START_STOP_COMMAND*)&msc->command.CBWCB;
+
+        TRACE_MSG4(MSC,"START STOP: Immed: %d Power: %x LoEj: %d Start: %d", 
+                        command->IMMED, command->PowerConditions, command->LoEj, command->Start);
+        /*
+         * C.f. 5.4 
+         *
+         * IMMED - if set return status immediately after command validation, otherwise
+         * return status as soon operation is completed.
+         *
+         * C.f. 5.4.1 Table 8 POWER CONDITIONS
+         *
+         * 0 - M - no change in power condition
+         * 1 - M - place device in active condition
+         * 2 - M - place device in idle condition
+         * 3 - M - place device in Standby condition
+         * 4 -   - reserved
+         * 5 - M - place device in Sleep condition
+         * 6 -   - reserved
+         * 7 - 0 - Device Control
+         *
+         * C.f. 5.4.2 Table 9 START STOP control bit definitions
+         *
+         * Power        Load/Eject      Start   
+         * 1-7          x               x               LoEj and Start Ignored
+         * 0            0               0               Stop the medium
+         * 0            0               1               Make the medium ready
+         * 0            1               0               Unload the medium
+         * 0            1               1               Load the medium
+         *
+         */
+        // XXX TODO 
+        // this is from storageproto.c, shouldn't we implement something?
+        
+        if (command->Start && command->LoEj) 
+                return msc_start_sending_csw_failed (msc, SCSI_SENSEKEY_INVALID_FIELD_IN_CDB, msc->lba, USB_MSC_FAILED);
+
+        return msc_start_sending_csw(msc->function, msc, USB_MSC_PASSED);
+}
+
+/*! msc_scsi_verify - process a verify command
+ *
+ * Used by:
+ *      win2k
+ *
+ * Returns non-zero if there is an error in the USB layer.
+ */
+int msc_scsi_verify(struct msc_private *msc, char *name, int op)
+{
+        SCSI_VERIFY_COMMAND *command = (SCSI_VERIFY_COMMAND *)&msc->command.CBWCB; 
+
+        /*
+         * C.f. RBC 5.7 VERIFY command
+         */
+        // XXX This actually should use the read_10 function and when
+        // finished reading simply send the following
+        return msc_start_sending_csw(msc->function, msc, USB_MSC_PASSED);
+}
+
+
+/*! msc_scsi_mode_select - process a select command
+ *
+ * Returns non-zero if there is an error in the USB layer.
+ */
+int msc_scsi_mode_select(struct msc_private *msc, char *name, int op)
+{
+        //SCSI_MODE_SELECT_COMMAND *command = (SCSI_MODE_SELECT_COMMAND *)&msc->command.CBWCB; 
+       
+        /*
+         * C.f. SPC2 7.6 MODE SELECT(6) command
+         */
+
+        // if less than correct amount of data return USB_MSC_PHASE_ERROR - see MV
+        //
+        return msc_start_sending_csw(msc->function, msc, USB_MSC_PASSED);
+}
+
+/*! msc_private_pcs - process a private command
+ *
+ * Returns non-zero if there is an error in the USB layer.
+ */
+int msc_cmd_private_pcs(struct msc_private *msc, char *name, int op)
+{
+        return msc_start_sending_csw_failed (msc, SCSI_SENSEKEY_INVALID_COMMAND, msc->lba, USB_MSC_FAILED);
+}
+
+/*! msc_cmd_unknown - process an unknown command
+ *
+ * Returns non-zero if there is an error in the USB layer.
+ */
+int msc_cmd_unknown(struct msc_private *msc, char *name, int op)
+{
+        return msc_start_sending_csw_failed (msc, SCSI_SENSEKEY_INVALID_COMMAND, msc->lba, USB_MSC_FAILED);
+}
+
+struct rbc_dispatch {
+        u8      op;
+        char    *name;
+        int     (*rbc_command) (struct msc_private *, char *, int op);
+        int     device_check;
+};
+
+/*! Command cross reference 
+ *
+ * This is the list of commands observed from each host OS. It is necessarily
+ * incomplete in that we not have reached some condition necessary to have
+ * other commands used.
+ *                                      Win2k   WinXP
+ * SCSI_TEST_UNIT_READY                 yes     yes
+ * SCSI_READ_10                         yes     yes
+ * SCSI_WRITE_10                        yes     yes
+ * SCSI_READ_CAPACITY                   yes     yes
+ * SCSI_VERIFY                          yes
+ * SCSI_INQUIRY                         yes     yes
+ * SCSI_MODE_SENSE                      yes
+ * SCSI_READ_FORMAT_CAPACITY            yes     yes
+ * SCSI_REQUEST_SENSE                   
+ * SCSI_PREVENT_ALLOW_MEDIA_REMOVAL    
+ * SCSI_START_STOP                      
+ * SCSI_MODE_SELECT                     
+ * SCSI_FORMAT_UNIT                     
+ *
+ */
+
+struct rbc_dispatch rbc_dispatch_table[] = {
+        { SCSI_TEST_UNIT_READY,         "SCSI_TEST_UNIT_READY",         msc_scsi_test_unit_ready,               NOZLP },    
+        { SCSI_READ_10,                 "SCSI_READ_10",                 msc_scsi_read_10,                       SENDZLP },
+        { SCSI_WRITE_10,                "SCSI_WRITE_10",                msc_scsi_write_10,                      NOZLP },
+        { SCSI_READ_CAPACITY,           "SCSI_READ_CAPACITY",           msc_scsi_read_capacity,                 SENDZLP },
+        { SCSI_VERIFY,                  "SCSI_VERIFY",                  msc_scsi_verify,                        NOCHK },
+        { SCSI_INQUIRY,                 "SCSI_INQUIRY",                 msc_scsi_inquiry,                       NOCHK },
+        { SCSI_MODE_SENSE,              "SCSI_MODE_SENSE",              msc_scsi_mode_sense,                    NOCHK },
+        { SCSI_READ_FORMAT_CAPACITY,    "SCSI_READ_FORMAT_CAPACITY",    msc_scsi_read_format_capacity,          SENDZLP },
+        { SCSI_REQUEST_SENSE,           "SCSI_REQUEST_SENSE",           msc_scsi_request_sense,                 NOCHK },
+        { SCSI_PREVENT_ALLOW_MEDIA_REMOVAL, "SCSI_PREVENT_ALLOW_MEDIA_REMOVAL", msc_scsi_prevent_allow,         NOZLP }, 
+        { SCSI_START_STOP,              "SCSI_START_STOP",              msc_scsi_start_stop,                    NOZLP },
+        { SCSI_MODE_SELECT,             "SCSI_MODE_SELECT",             msc_scsi_mode_select,                   NOCHK },
+        { SCSI_FORMAT_UNIT,             "SCSI_FORMAT_UNIT",             msc_cmd_unknown,                        NOCHK },
+
+        { SCSI_READ_6,                  "SCSI_READ_6",                  msc_cmd_unknown,                        NOCHK },
+        { SCSI_WRITE_6,                 "SCSI_WRITE_6",                 msc_cmd_unknown,                        NOCHK },
+        { SCSI_RESERVE,                 "SCSI_RESERVE",                 msc_cmd_unknown,                        NOCHK },
+        { SCSI_RELEASE,                 "SCSI_RELEASE",                 msc_cmd_unknown,                        NOCHK },
+        { SCSI_SEND_DIAGNOSTIC,         "SCSI_SEND_DIAGNOSTIC",         msc_cmd_unknown,                        NOCHK },
+        { SCSI_SYNCHRONIZE_CACHE,       "SCSI_SYNCHRONIZE_CACHE",       msc_cmd_unknown,                        NOCHK },
+        { SCSI_MODE_SENSE_10,           "SCSI_MODE_SENSE_10",           msc_cmd_unknown,                        NOCHK },
+        { SCSI_REZERO_UNIT,             "SCSI_REZERO_UNIT",             msc_cmd_unknown,                        NOCHK },
+        { SCSI_SEEK_10,                 "SCSI_SEEK_10",                 msc_cmd_unknown,                        NOCHK },
+        { SCSI_WRITE_AND_VERIFY,        "SCSI_WRITE_AND_VERIFY",        msc_cmd_unknown,                        NOCHK },
+        { SCSI_WRITE_12,                "SCSI_WRITE_12",                msc_cmd_unknown,                        NOCHK },
+        { SCSI_READ_12,                 "SCSI_READ_12",                 msc_cmd_unknown,                        NOCHK },
+
+        { SCSI_PRIVATE_PCS,             "SCSI_PRIVATE_PCS",             msc_cmd_private_pcs,                    NOCHK },
+
+        { 0xff,                         "SCSI_UNKNOWN",                 msc_cmd_unknown,                        NOCHK },
+};
+
+
+/*! msc_recv_command - process a new CBW
+ *
+ * Return non-zero if urb was not disposed of.
+ */
+void msc_recv_command(struct usbd_urb *urb, struct msc_private *msc)
+{
+        COMMAND_BLOCK_WRAPPER *command = (COMMAND_BLOCK_WRAPPER *)urb->buffer;
+        u8 op = command->CBWCB[0];
+        struct rbc_dispatch *dispatch;
+
+        /*
+         * c.f. section 6.2 - Valid and Meaningful CBW
+         * c.f. section 6.2.1 - Valid CBW
+         *
+         * The CBW was received after the device had sent a CSW or after a
+         * reset XXX check that we only set MSC_READY after reset or sending
+         * CSW.
+         *
+         * The CBW is 31 (1Fh) bytes in length and the bCBWSignature is
+         * equal to 43425355h.
+         */
+        THROW_IF(31 != urb->actual_length, error);
+        THROW_IF(CBW_SIGNATURE != le32_to_cpu(command->dCBWSignature), error);
+        
+        /*
+         * c.f. section 6.2.2 - Meaningful CBW
+         *
+         * no reserved bits are set
+         * the bCBWLUN contains a valid LUN supported by the device
+         * both bCBWCBlength and the content of the CBWCB are in accordance with bInterfaceSubClass
+         */
+
+        // XXX checklun etc
+
+        /* 
+         * Success 
+         */
+        memcpy(&msc->command, command, sizeof(COMMAND_BLOCK_WRAPPER));
+        msc->data_transferred_in_bytes = msc->TransferLength_in_blocks = msc->TransferLength_in_bytes = 0;
+
+        TRACE_TAG(command->dCBWTag, urb->framenum);
+        usbd_free_urb(urb);
+        urb = NULL;
+
+        /*
+         * Search using the opcode to find the dispatch function to use and
+         * call it.
+         */
+        for (dispatch = rbc_dispatch_table; dispatch->op != 0xff; dispatch++) {
+                CONTINUE_UNLESS ((dispatch->op == op));
+
+                TRACE_CBW(dispatch->name, dispatch->op, dispatch->device_check);
+                TRACE_RECV(&(command->CBWCB[1]));
+
+                /* Depending on the command we may need to check if the device is available
+                 * and either fail or send a ZLP if it is not
+                 */
+                if (dispatch->device_check) 
+                        RETURN_IF (msc_check_blockdev_name(msc, dispatch->device_check, dispatch->name));  
+
+                /* Call the specific function that implements the specified command
+                 */
+                if (dispatch->rbc_command(msc, dispatch->name, op)) 
+                        TRACE_MSG0(MSC,"COMMAND ERROR");
+                return;
+        }
+
+        /* FALL THROUGH if no match is found */
+
+        CATCH(error) {
+                TRACE_MSG0(MSC,"RECV CBW ERROR");
+                if (urb)
+                        usbd_free_urb(urb);
+
+                /* XXX which of these do we stall?
+                 */
+                usbd_halt_endpoint(urb->function_instance, BULK_IN);
+                usbd_halt_endpoint(urb->function_instance, BULK_OUT);
+        }
+        msc_cmd_unknown(msc, "CMD_UNKNOWN", op);
+}
+
+
+/* Sent Function - process a sent urb ********************************************************** */
+
+/*! msc_urb_sent - called to indicate URB transmit finished
+ * @param tx_urb: pointer to struct usbd_urb
+ * @param rc: result
+ *
+ * This is called when an urb is sent. Depending on current state
+ * it may:
+ *
+ *      - continue sending data
+ *      - send a CSW
+ *      - start a recv for a CBW
+ *
+ * This is called from BOTTOM HALF context.
+ *
+ * @return non-zero if urb was not disposed of.
+ */
+int msc_urb_sent (struct usbd_urb *tx_urb, int rc)
+{
+        struct usbd_function_instance *function;
+        struct msc_private *msc = &msc_private;
+
+        RETURN_EINVAL_IF(!(function = tx_urb->function_instance));
+        RETURN_EINVAL_IF(usbd_get_device_status(function) == USBD_CLOSING);
+        RETURN_EINVAL_IF(usbd_get_device_state(function) != STATE_CONFIGURED);
+
+        switch (msc->command_state) {
+        case MSC_DATA_IN_READ:
+        case MSC_DATA_IN_READ_FINISHED:
+                TRACE_MSG0(MSC,"URB SENT READ");
+                return msc_in_read_10_urb_sent(tx_urb, msc);
+
+        case MSC_QUERY:
+                // finished, send CSW
+                TRACE_MSG0(MSC,"URB SENT QUERY");
+                msc_start_sending_csw(msc->function, msc, USB_MSC_PASSED);
+                break;
+
+        case MSC_STATUS:
+        default:
+                // sent a CSW need to receive the next CBW
+                TRACE_MSG0(MSC,"URB SENT STATUS");
+                msc->command_state = MSC_READY;
+                msc_start_recv_urb(msc->function, msc, sizeof(COMMAND_BLOCK_WRAPPER));
+                break;
+        }
+        usbd_free_urb (tx_urb);
+        return 0;
+}
+
+
+/* Receive Function - receiving an urb ********************************************************* */
+
+/*! msc_recv_urb - process a received urb
+ *
+ * Return non-zero if urb was not disposed of.
+ */
+static int msc_recv_urb (struct usbd_urb *rcv_urb, int rc)
+{
+        struct msc_private *msc = &msc_private;
+
+        RETURN_EINVAL_IF(!msc->connected);
+
+        //TRACE_MSG2(MSC, "RECV URB length: %d state: %d", rcv_urb->actual_length, msc->command_state);
+
+        switch(msc->command_state) {
+
+                // ready to start a new transaction
+        case MSC_READY:
+                msc_recv_command(rcv_urb, msc);
+                return 0;
+
+                // we think we are receiving data
+        case MSC_DATA_OUT_WRITE:
+        case MSC_DATA_OUT_WRITE_FINISHED:
+                msc_recv_out_blocks(rcv_urb, msc);
+                return 0;
+
+                // we think we are sending data
+        case MSC_DATA_IN_READ:
+        case MSC_DATA_IN_READ_FINISHED:
+                msc_start_sending_csw_failed (msc, SCSI_SENSEKEY_INVALID_COMMAND, msc->lba, USB_MSC_FAILED);
+                break;
+
+                // we think we are sending status
+        case MSC_STATUS:
+                msc_start_sending_csw_failed (msc, SCSI_SENSEKEY_INVALID_COMMAND, msc->lba, USB_MSC_FAILED);
+                break;
+
+                // we don't think
+        case MSC_UNKNOWN:
+        default:
+                TRACE_MSG0(MSC,"RECV URB ERROR");
+                usbd_halt_endpoint(rcv_urb->function_instance, BULK_OUT);
+        }
+        // let caller dispose of urb
+        return -EINVAL;
+}
+
+/* USB Device Functions ************************************************************************ */
+
+
+static void msc_device_request (struct usbd_function_instance *function, struct usbd_device_request *request)
+{
+        TRACE_MSG0(MSC,"--");
+}
+
+static void msc_set_configuration (struct usbd_function_instance *function, int wValue)
+{
+        TRACE_MSG1(MSC,"wValue: %02x", wValue);
+
+}
+
+static void msc_set_interface (struct usbd_function_instance *function, int wIndex, int wValue)
+{
+        TRACE_MSG2(MSC,"wIndex: %02x wValue: %02x", wIndex, wValue);
+
+}
+
+static void msc_endpoint_cleared (struct usbd_function_instance *function, int bEndpointAddress)
+{
+        TRACE_MSG1(MSC,"bEndpointAddress: %02x", bEndpointAddress);
+
+}
+
+/* USB Device Functions ************************************************************************ */
+/*! msc_event_handler - process a device event
+ *
+ * This function is called when an USBD event occurs.
+ *
+ * This is called from INTERRUPT context.
+ */
+void msc_event_handler (struct usbd_function_instance *function, usbd_device_event_t event, int data)
+{
+        unsigned long flags;
+        struct msc_private *msc = &msc_private;
+        int connected;
+        switch (event) {
+        case DEVICE_CONFIGURED:
+                TRACE_MSG0(MSC,"EVENT CONFIGURED");
+                msc->connected = 1;
+                msc->command_state = MSC_READY;
+                msc_start_recv_urb(function, msc, sizeof(COMMAND_BLOCK_WRAPPER));
+                #if 0
+                local_irq_save(flags);
+                if (msc->io_state & MSC_IOCTL_WAITING) {
+                        msc->io_state &= ~MSC_IOCTL_WAITING;
+                        TRACE_MSG0(MSC, "WAKEUP");
+                }
+                local_irq_restore(flags);
+                #endif
+                wake_up_interruptible(&msc->ioctl_wq);
+                break;
+
+        case DEVICE_BUS_INACTIVE:
+        case DEVICE_RESET:
+        case DEVICE_DE_CONFIGURED:
+                TRACE_MSG2(MSC,"EVENT RESET: connected %d msc->io_state", msc->connected, msc->io_state);
+                connected = msc->connected;
+                msc->connected = 0;
+                #if 0
+                local_irq_save(flags);
+                if (msc->io_state & MSC_IOCTL_WAITING) {
+                        msc->io_state &= ~MSC_IOCTL_WAITING;
+                        TRACE_MSG0(MSC, "WAKEUP");
+                        wake_up_interruptible(&msc->ioctl_wq);
+                }
+                local_irq_restore(flags);
+                #endif
+                wake_up_interruptible(&msc->ioctl_wq);
+                BREAK_UNLESS(connected);
+
+                // XXX we should have a semaphore to protect this 
+                BREAK_UNLESS (msc->rcv_urb_finished);
+                usbd_free_urb (msc->rcv_urb_finished);
+                msc->rcv_urb_finished = NULL;
+                break;
+
+        default: 
+                TRACE_MSG0(MSC,"EVENT IGNORED");
+                break;
+        }
+}
+
+/*! msc_device_request - called to indicate urb has been received
+ *
+ * This function is called when a SETUP packet has been received that
+ * should be handled by the function driver. It will not be called to
+ * process the standard chapter nine defined requests.
+ *
+ * Return non-zero for failure.
+ */
+int msc_device_handler (struct usbd_function_instance *function, struct usbd_device_request *request)
+{
+        struct msc_private *msc = &msc_private;
+        struct usbd_urb *urb;
+
+        TRACE_MSG0(MSC,"RECV SETUP");
+
+        // verify that this is a usb class request per cdc-acm specification or a vendor request.
+        RETURN_ZERO_IF (!(request->bmRequestType & (USB_REQ_TYPE_CLASS | USB_REQ_TYPE_VENDOR)));
+       
+        // determine the request direction and process accordingly
+        switch (request->bmRequestType & (USB_REQ_DIRECTION_MASK | USB_REQ_TYPE_MASK)) {
+
+        case USB_REQ_HOST2DEVICE | USB_REQ_TYPE_CLASS:
+                switch (request->bRequest) {
+                case MSC_BULKONLY_RESET:
+                        // XXX TODO FIXME
+                        return 0;
+                }
+
+        case USB_REQ_DEVICE2HOST | USB_REQ_TYPE_CLASS:
+                switch (request->bRequest) {
+                case MSC_BULKONLY_GETMAXLUN: 
+                        RETURN_EINVAL_IF(!(urb = usbd_alloc_urb_ep0(function, 1, NULL)));
+                        urb->buffer[0] = 0;
+                        urb->actual_length = 1;
+                        RETURN_ZERO_IF(!usbd_start_in_urb(urb));
+                        usbd_free_urb(urb);
+                        return -EINVAL;
+                }
+        default: 
+                break;
+        }
+        return -EINVAL;
+}
+
+/*! msc_function_enable - this is called by the USBD core layer 
+ *
+ * This is called to initialize the function when a bus interface driver
+ * is loaded.
+ */
+static int msc_function_enable (struct usbd_function_instance *function)
+{
+        struct msc_private *msc = &msc_private;
+
+        MOD_INC_USE_COUNT;
+
+        // XXX TODO need to verify that serial number is minimum of 12 
+        
+        msc->function = function;
+        msc->command_state = MSC_READY;
+
+        return 0;
+}
+
+/*! msc_function_disable - this is called by the USBD core layer 
+ *
+ * This is called to close the function when a bus interface driver
+ * is unloaded.
+ */
+static void msc_function_disable (struct usbd_function_instance *function)
+{               
+        struct msc_private *msc = &msc_private;
+
+        TRACE_MSG0(MSC,"FUNCTION EXIT");
+
+        msc->function = NULL;
+
+        MOD_DEC_USE_COUNT;
+}
+
+/* ********************************************************************************************* */
+struct usbd_function_operations msc_function_ops = {
+        event_handler: msc_event_handler,
+        device_request: msc_device_request,
+        function_enable: msc_function_enable,
+        function_disable: msc_function_disable,
+        endpoint_cleared: msc_device_request,
+        endpoint_cleared: msc_set_configuration,
+        endpoint_cleared: msc_set_interface,
+        endpoint_cleared: msc_endpoint_cleared,
+};
+
+/* ********************************************************************************************* */
diff -uNr linux/drivers/no-otg/functions/msc/msc-fd.h linux/drivers/otg/functions/msc/msc-fd.h
--- linux/drivers/no-otg/functions/msc/msc-fd.h	1970-01-01 01:00:00.000000000 +0100
+++ linux/drivers/otg/functions/msc/msc-fd.h	2006-09-01 21:41:27.000000000 +0200
@@ -0,0 +1,64 @@
+/*
+ * otg/msc_fd/msc.h - Mass Storage Class
+ *
+ *      Copyright (c) 2003, 2004 Belcarra
+ *
+ * By: 
+ *      Stuart Lynne <sl@belcarra.com>
+ *      Bruce Balden <balden@belcarra.com>
+ *
+ */
+
+#ifndef MSC_FD_H
+#define MSC_FD_H 1
+
+extern int msc_dispatch_query_urb(struct usbd_urb *urb, struct msc_private *msc, int length, int status);
+extern int msc_start_sending_csw(struct usbd_function_instance *function, struct msc_private *msc, u8 status);
+extern int msc_dispatch_query_urb_zlp(struct msc_private *msc, u32 sensedata, u32 info, int status);
+extern int msc_start_sending_csw_failed(struct msc_private *msc, u32 sensedata, u32 info, int status);
+extern int msc_start_recv_urb(struct usbd_function_instance *function, struct msc_private *msc, int size);
+
+#define NOCHK 0
+#define NOZLP 1
+#define SENDZLP 2
+
+#if 1
+static __inline__ void TRACE_SENSE(unsigned int sense, unsigned int info)
+{
+        TRACE_MSG2(MSC, "-->             SENSE: %06x INFO: %08x", sense, info);
+}
+static __inline__ void TRACE_RLBA(unsigned int lba, unsigned int crc)
+{
+        TRACE_MSG2(MSC, "<--             rlba [%8x %08x]", lba, crc);
+}
+static __inline__ void TRACE_SLBA(unsigned int lba, unsigned int crc)
+{
+        TRACE_MSG2(MSC, "-->             slba [%8x %08x]", lba, crc);
+}
+static __inline__ void TRACE_TLBA(unsigned int lba, unsigned int crc)
+{
+        TRACE_MSG2(MSC, "-->             tlba [%8x %08x]", lba, crc);
+}
+static __inline__ void TRACE_TAG(unsigned int tag, unsigned int frame)
+{
+        TRACE_MSG2(MSC, "-->             TAG: %8x FRAME: %03x", tag, frame);
+}
+static __inline__ void TRACE_CBW(char *msg, int val, int check)
+{
+        TRACE_MSG3(MSC,  " -->            %s %02x check: %d", msg, val, check);
+}
+static __inline__ void TRACE_RECV(unsigned char *cp)
+{
+        TRACE_MSG8(MSC, "<--             recv [%02x %02x %02x %02x %02x %02x %02x %02x]",
+                        cp[0], cp[1], cp[2], cp[3], cp[4], cp[5], cp[6], cp[7]);
+}
+static __inline__ void TRACE_SENT(unsigned char *cp)
+{
+        TRACE_MSG8(MSC, "-->             sent [%02x %02x %02x %02x %02x %02x %02x %02x]",
+                        cp[0], cp[1], cp[2], cp[3], cp[4], cp[5], cp[6], cp[7]);
+}
+
+#endif
+
+
+#endif /* MSC_H */
diff -uNr linux/drivers/no-otg/functions/msc/msc-io-l24.c linux/drivers/otg/functions/msc/msc-io-l24.c
--- linux/drivers/no-otg/functions/msc/msc-io-l24.c	1970-01-01 01:00:00.000000000 +0100
+++ linux/drivers/otg/functions/msc/msc-io-l24.c	2006-09-01 21:41:28.000000000 +0200
@@ -0,0 +1,272 @@
+/*
+ * otg/function/msc/msc-io-l24.c - MSC IO
+ *
+ *      Copyright (c) 2004 Belcarra
+ *
+ * By: 
+ *      Stuart Lynne <sl@lbelcarra.com>, 
+ *      Tom Rushworth <tbr@belcarra.com>, 
+ *      Bruce Balden <balden@belcarra.com>
+ *
+ */
+/*!
+ * @file otg/functions/msc/msc-io-l24.c
+ * @brief 
+ *
+ * NOTES
+ *
+ * TODO
+ *
+ * 1. implement prevent removal command.
+ *
+ * @ingroup MSCFunction
+ */
+
+#include <otg/otg-compat.h>
+#include <otg/otg-module.h>
+
+#include <otg/usbp-chap9.h>
+#include <otg/usbp-func.h>
+#include <otg/otg-trace.h>
+#include <otg/otg-api.h>
+
+#include <linux/poll.h>
+#include <linux/sched.h>
+#include <linux/devfs_fs_kernel.h>
+
+#include "msc-scsi.h"
+#include "msc.h"
+#include "msc-fd.h"
+#include "crc.h"
+#include "msc-io.h"
+
+extern void msc_open_blockdev (struct msc_private *msc);
+extern void msc_close_blockdev (struct msc_private *msc);
+extern int msc_connection_blockdev (struct msc_private *msc); 
+extern struct msc_private msc_private;
+
+#define DEVICE_EJECTED	0x0001		//MEDIA_EJECTED
+#define DEVICE_INSERTED	0x0002		//MEDIA_INSERT
+#define DEVICE_MOUNTED  0x0001		//DEVICE_MOUNTED
+#define DEVICE_UNMOUNTED 0x0002		//DEVICE_UNMOUNTED
+
+extern u32 major;
+extern u32 minor;
+#if 0
+void msc_io_wait(struct msc_private *msc, u32 flag)
+{
+	unsigned long flags;
+        msc->io_state |= MSC_IOCTL_WAITING | flag;
+        TRACE_MSG1(MSC, "SLEEPING io_state: %x", msc->io_state);
+        interruptible_sleep_on(&msc->ioctl_wq);
+}
+
+void msc_io_wakup(struct msc_private *msc)
+{
+	unsigned long flags;
+        local_irq_save(flags);
+        if (msc->io_state & MSC_IOCTL_WAITING) {
+                msc->io_state &= ~MSC_IOCTL_WAITING;
+                TRACE_MSG0(MSC, "WAKEUP");
+                wake_up_interruptible(&msc->ioctl_wq);
+        }
+        local_irq_restore(flags);
+}
+#endif
+/*! msc_io_ioctl_internal
+ */
+int msc_io_ioctl_internal(unsigned int cmd, unsigned long arg)
+{
+        int i;
+        int len;
+        int flag;
+
+        static char func_buf[32];
+        struct otgmsc_mount mount;
+	struct msc_private *msc = &msc_private;
+	unsigned long flags;
+
+        TRACE_MSG2(MSC, "cmd: %d connect: %d", cmd, msc->connected);
+        memset(&mount, 0, sizeof(mount));
+        switch (cmd) {
+
+                /* Mount - make the specified major/minof device available for use
+                 * by the USB Host, this does not require an active connection.
+                 */
+	case OTGMSC_START:
+		TRACE_MSG0(MSC, "Mounting the device");
+		RETURN_EINVAL_IF(copy_from_user(&mount, (void *)arg, _IOC_SIZE(cmd)));
+                
+		TRACE_MSG3(MSC, "major=%d minor=%d state=%d", mount.major, mount.minor, msc->block_dev_state);
+		major = mount.major;
+		minor = mount.minor;
+		
+		//msc->io_state = MSC_INACTIVE;
+		msc_open_blockdev (msc);
+                RETURN_EAGAIN_UNLESS(msc->command_state == MSC_READY);
+
+		mount.status = (msc->block_dev_state == DEVICE_INSERTED) ? DEVICE_MOUNTED : DEVICE_UNMOUNTED;
+
+		TRACE_MSG1(MSC, "Device is mounted status: %d", mount.status);
+
+                // XXX Need to copy result back to user space
+                RETURN_EINVAL_IF (copy_to_user((void *)arg, &mount, sizeof(mount)));
+		TRACE_MSG0(MSC,  "Device mounted");
+		return 0; 
+
+                /* Umount - make the currently mounted device unavailable to the USB Host,
+                 * if there is pending block i/o block until it has finished.
+                 * Note that if the driver is unloaded the waiting ioctl process 
+                 * must be woken up and informed with error code.
+                 */
+	case OTGMSC_STOP:
+
+		TRACE_MSG0(MSC,  "Unmounting the device");
+                
+		RETURN_EINVAL_IF (copy_from_user (&mount, (void *)arg, _IOC_SIZE(cmd)));
+
+                if (msc->command_state != MSC_READY) {
+                        TRACE_MSG0(MSC, "SLEEPING");
+                        interruptible_sleep_on(&msc->ioctl_wq);
+                        TRACE_MSG0(MSC, "AWAKE");
+                }
+
+                RETURN_EAGAIN_UNLESS(msc->command_state == MSC_INACTIVE);
+
+                // XXX Need to copy result back to user space
+		msc->major = mount.major;
+		msc->minor = mount.minor;
+		msc_close_blockdev(msc);
+		mount.status = DEVICE_UNMOUNTED;
+                RETURN_EINVAL_IF (copy_to_user((void *)arg, &mount, sizeof(mount)));
+		TRACE_MSG0(MSC,  "Device unmounted");
+		return 0;
+
+
+                /* Status - return the current mount status.
+                 */
+        case OTGMSC_STATUS:
+		TRACE_MSG0(MSC, "Mount status");
+                RETURN_EINVAL_IF (copy_from_user (&mount, (void *)arg, _IOC_SIZE(cmd)));
+		if (msc->block_dev_state == DEVICE_EJECTED)
+			mount.status = DEVICE_UNMOUNTED;
+		else
+			mount.status = DEVICE_MOUNTED;
+
+                // XXX Need to copy result back to user space
+                RETURN_EINVAL_IF (copy_to_user((void *)arg, &mount, sizeof(mount)));
+		return 0;
+                
+                /* Wait_Connect - if not already connected wait until connected,
+                 * Note that if the driver is unloaded the waiting ioctl process 
+                 * must be woken up and informed with error code.
+                 */
+        case OTGMSC_WAIT_CONNECT:
+		TRACE_MSG1(MSC, "Wait for connect: connected: %d", msc->connected);
+                local_irq_save(flags);
+
+                if (msc->connected == 0) {
+                        TRACE_MSG0(MSC, "SLEEPING");
+			interruptible_sleep_on (&msc->ioctl_wq);
+                        TRACE_MSG0(MSC, "AWAKE");
+                }
+                RETURN_EAGAIN_UNLESS(msc->connected);
+
+		RETURN_EINVAL_IF (copy_from_user (&mount, (void *)arg, _IOC_SIZE(cmd)));
+		RETURN_EINVAL_IF (copy_to_user((void *)arg, &mount, sizeof(mount)));
+		TRACE_MSG0(MSC,  "Device connected");
+		return 0;
+                
+                /* Wait_DisConnect - if not already disconnected wait until disconnected,
+                 * Note that if the driver is unloaded the waiting ioctl process 
+                 * must be woken up and informed with error code.
+                 */
+        case OTGMSC_WAIT_DISCONNECT:
+		TRACE_MSG1(MSC, "Wait for disconnect: connected: %d", msc->connected);
+
+                if (msc->connected == 1) {
+                        TRACE_MSG0(MSC, "SLEEPING");
+			interruptible_sleep_on (&msc->ioctl_wq);
+                        TRACE_MSG0(MSC, "AWAKE");
+                }
+                RETURN_EAGAIN_IF(msc->connected);
+		RETURN_EINVAL_IF (copy_from_user (&mount, (void *)arg, _IOC_SIZE(cmd)));
+		RETURN_EINVAL_IF (copy_to_user((void *)arg, &mount, sizeof(mount)));
+		TRACE_MSG0(MSC,  "Device disconnected");
+		return 0; 
+                
+        default:
+		TRACE_MSG1(MSC,  "Unknown command: %x", cmd);
+		TRACE_MSG1(MSC,  "OTGMSC_START: %x", OTGMSC_START);
+		TRACE_MSG1(MSC,  "OTGMSC_WRITEPROTECT: %x", OTGMSC_WRITEPROTECT);
+		TRACE_MSG1(MSC,  "OTGMSC_STOP: %x", OTGMSC_STOP);
+		TRACE_MSG1(MSC,  "OTGMSC_STATUS: %x", OTGMSC_STATUS);
+		TRACE_MSG1(MSC,  "OTGMSC_WAIT_CONNECT: %x", OTGMSC_WAIT_CONNECT);
+		TRACE_MSG1(MSC,  "OTGMSC_WAIT_DISCONNECT: %x", OTGMSC_WAIT_DISCONNECT);
+                return -EINVAL;
+        }
+        return 0;
+}
+
+
+
+/*! msc_io_ioctl
+ */
+int msc_io_ioctl(struct inode *inode, struct file *filp, unsigned int cmd, unsigned long arg)
+{
+        int i;
+        int len;
+        int flag;
+
+        //printk(KERN_INFO"%s: cmd: %08x arg: %08x\n", __FUNCTION__, cmd, arg);
+        TRACE_MSG6(MSC, "cmd: %08x arg: %08x type: %02d nr: %02d dir: %02d size: %02d", 
+                        cmd, arg, _IOC_TYPE(cmd), _IOC_NR(cmd), _IOC_DIR(cmd), _IOC_SIZE(cmd));
+        
+        RETURN_EINVAL_UNLESS (_IOC_TYPE(cmd) == OTGMSC_MAGIC);
+        RETURN_EINVAL_UNLESS (_IOC_NR(cmd) <= OTGMSC_MAXNR);
+
+        RETURN_EFAULT_IF((_IOC_DIR(cmd) == _IOC_READ) && !access_ok(VERIFY_WRITE, (void *)arg, _IOC_SIZE(cmd)));
+        RETURN_EFAULT_IF((_IOC_DIR(cmd) == _IOC_WRITE) && !access_ok(VERIFY_READ, (void *)arg, _IOC_SIZE(cmd)));
+
+        return msc_io_ioctl_internal(cmd, arg);
+}
+
+
+
+int msc_io_proc_ioctl(struct inode *inode, struct file *filp, unsigned int cmd, unsigned long arg)
+{
+        return msc_io_ioctl(inode, filp, cmd, arg);
+}
+
+
+static struct file_operations msc_io_proc_switch_functions = {
+        ioctl:msc_io_proc_ioctl,
+};
+
+
+/* msc_io_init_l24 - initialize
+ */
+int msc_io_init_l24(void)
+{
+        struct proc_dir_entry *message = NULL;
+
+        THROW_IF (!(message = create_proc_entry ("msc_io", 0666, 0)), error);
+        message->proc_fops = &msc_io_proc_switch_functions;
+        CATCH(error) {
+                printk(KERN_ERR"%s: creating /proc/msc_io failed\n", __FUNCTION__);
+                if (message)
+                        remove_proc_entry("msc_io", NULL);
+                return -EINVAL;
+        }
+        return 0;
+}
+
+/* msc_io_exit_l24 - exit
+ */
+void msc_io_exit_l24(void)
+{
+        remove_proc_entry("msc_io", NULL);
+}
+
+
+
diff -uNr linux/drivers/no-otg/functions/msc/msc-io.h linux/drivers/otg/functions/msc/msc-io.h
--- linux/drivers/no-otg/functions/msc/msc-io.h	1970-01-01 01:00:00.000000000 +0100
+++ linux/drivers/otg/functions/msc/msc-io.h	2006-09-01 21:41:28.000000000 +0200
@@ -0,0 +1,57 @@
+/*
+ * otg/functions/msc/msc-io.h - Mass Storage Class
+ *
+ *      Copyright (c) 2003, 2004 Belcarra
+ *
+ * By: 
+ *      Stuart Lynne <sl@belcarra.com>
+ *      Bruce Balden <balden@belcarra.com>
+ *
+ */
+/*!
+ * @file otg/functions/msc/msc-io.h
+ * @brief Mass Storage Driver private defines
+ *
+ * OTGMSC_START                 - make specified major/minor device available to USB Host
+ * OTGMSC_WRITEPROTECT          - set or reset write protect flag
+ *
+ * OTGMSC_STOP                  - remove access to current device, block until pending I/O finished
+ * OTGMSC_STATUS                - remove current USB connection status (connected or disconnected)
+ * OTGMSC_WAIT_CONNECT          - wait until device is connected (may return immediately if already connected)
+ * OTGMSC_WAIT_DISCONNECT       - wait until device is disconnected (may return immediately if already disconnected)
+ *
+ * @ingroup MSCFunction
+ */
+
+//#ifndef MSC_H
+//#define MSC_H 1
+
+#define MSC_IO "/proc/msc_io"
+
+struct otgmsc_mount {
+        int     major;
+        int     minor;
+        int     lun;
+        int     writeprotect;
+        int     result;
+        int     status;
+};
+
+#define OTGMSC_MAGIC    'M'
+#define OTGMSC_MAXNR	10
+
+#define OTGMSC_START            _IOWR(OTGMSC_MAGIC, 1, struct otgmsc_mount)
+#define OTGMSC_WRITEPROTECT     _IOWR(OTGMSC_MAGIC, 2, struct otgmsc_mount)
+
+#define OTGMSC_STOP             _IOR(OTGMSC_MAGIC, 1, struct otgmsc_mount)
+#define OTGMSC_STATUS           _IOR(OTGMSC_MAGIC, 2, struct otgmsc_mount)
+#define OTGMSC_WAIT_CONNECT     _IOR(OTGMSC_MAGIC, 3, struct otgmsc_mount)
+#define OTGMSC_WAIT_DISCONNECT  _IOR(OTGMSC_MAGIC, 4, struct otgmsc_mount)
+
+
+#define MSC_CONNECTED           0x01
+#define MSC_DISCONNECTED        0x02
+#define MSC_WRITEPROTECTED      0x04
+
+//#endif /* MSC_H */
+
diff -uNr linux/drivers/no-otg/functions/msc/msc-linux.c linux/drivers/otg/functions/msc/msc-linux.c
--- linux/drivers/no-otg/functions/msc/msc-linux.c	1970-01-01 01:00:00.000000000 +0100
+++ linux/drivers/otg/functions/msc/msc-linux.c	2006-09-01 21:41:28.000000000 +0200
@@ -0,0 +1,935 @@
+/*
+ * otg/function/msc/msc-linux.c
+ *
+ *      Copyright (c) 2003, 2004 Belcarra
+ *
+ * By: 
+ *      Stuart Lynne <sl@belcarra.com>, 
+ *      Tom Rushworth <tbr@belcarra.com>, 
+ *      Bruce Balden <balden@belcarra.com>
+ *
+ *
+ */
+
+/*!
+ * @file otg/functions/msc/msc-linux.c
+ * @brief Mass Storage Driver private defines
+ *
+ *
+ * This is a Mass Storage Class Function that uses the Bulk Only protocol.
+ *
+ * To use simply load with something like:
+ *
+ *      insmod msc_fd.o vendor_id=0xffff product_id=0xffff
+ *
+ * Notes:
+ *
+ * 1. Currently block I/O is done a page at a time. I have not determined if
+ * it is possible to dispatch a multiple page generic request. It should at
+ * least be possible to queue page requests. 
+ *
+ * 2. Currently for READ operations we have a maximum of one outstanding
+ * read block I/O and send urb. These are allowed to overlap so that we can
+ * continue to read data while sending data to the host.
+ *
+ * 3. Currently for WRITE operations we have a maximum of one outstanding
+ * recv urb and one outstanding write block I/O. These are allowed to
+ * overlap so that we can continue to receive data from the host while
+ * waiting for writing to complete.
+ *
+ * 4. It would be possible to allow multiple writes to be pending, to the
+ * limit of the page cache, if desired. 
+ *
+ * 5. It should be possible to allow multiple outstanding reads to be
+ * pending, to the limit of the page cache, but this potentially could
+ * require dealing with out of order completions of the reads. Essentially a
+ * list of completed buffer heads would be required to hold any completed
+ * buffer heads that cannot be sent prior to another, earlier request being
+ * completed. 
+ *
+ * 6. Currently ioctl calls are available to start and stop device i/o.
+ *
+ * 7. The driver can optionally generate trace messages showing each sectors 
+ * CRC as read or written with LBA. These can be compared by user programs to
+ * ensure that the correct data was read and/or written.
+ *
+ * 
+ * TODO
+ *
+ * 1. error handling for block io, e.g. what if using with removable block
+ * device (like CF card) and it is removed.
+ *
+ * 2. Currently we memcpy() data from between the urb buffer and buffer
+ * head page. It should be possible to simply use the page buffer for the
+ * urb.
+ *
+ * 3. Should we offer using fileio as an option? This would allow direct access
+ * to a block device image stored in a normal file or direct access to (for example)
+ * ram disks. It would require implementing a separate file I/O kernel thread to
+ * do the actual I/O.
+ *
+ * 4. It may be interesting to support use of SCSI block device and pass the
+ * scsi commands directly to that. This would allow vendor commands for real
+ * devices to be passed through and executed with results being properly
+ * returned to the host. [This is the intended design for the mass storage
+ * specification.]
+ *
+ * 
+ * TODO FIXME Bus Interface Notes
+ *
+ *
+ * @ingroup MSCFunction
+ */
+
+
+#include <otg/otg-compat.h>
+#include <otg/usbp-chap9.h>
+#include <otg/usbp-func.h>
+#include <otg/otg-trace.h>
+
+#include <linux/config.h>
+#include <linux/module.h>
+#include <linux/kernel.h>
+#include <linux/version.h>
+
+#include <linux/init.h>
+#include <asm/uaccess.h>
+#include <linux/ctype.h>
+#include <linux/timer.h>
+#include <linux/interrupt.h>
+#include <asm/atomic.h>
+#include <linux/random.h>
+#include <linux/slab.h>
+
+#include <linux/blkdev.h>
+
+
+#include "msc-scsi.h"
+#include "msc.h"
+#include "msc-fd.h"
+#ifdef CONFIG_OTG_MSC_BLOCK_TRACE
+#include "crc.h"
+#endif /* CONFIG_OTG_MSC_BLOCK_TRACE */
+#include "msc-io.h"
+
+
+//#include "rbc.h"
+
+
+/* Module Parameters ************************************************************************* */
+
+u32 vendor_id;
+u32 product_id;
+
+u32 major;
+u32 minor;
+
+MODULE_PARM (vendor_id, "i");
+MODULE_PARM (product_id, "i");
+MODULE_PARM (major, "i");
+MODULE_PARM (minor, "i");
+
+MODULE_PARM_DESC (vendor_id, "Device Vendor ID");
+MODULE_PARM_DESC (product_id, "Device Product ID");
+MODULE_PARM_DESC (major, "Device Major");
+MODULE_PARM_DESC (minor, "Device Minor");
+
+
+#define DEVICE_EJECTED          0x0001          // MEDIA_EJECTED
+#define DEVICE_INSERTED         0x0002          // MEDIA_INSERT
+
+#define DEVICE_OPEN             0x0004          // WR_PROTECT_OFF
+#define DEVICE_WRITE_PROTECTED  0x0008          // WR_PROTECT_ON
+
+#define DEVICE_CHANGE_ON        0x0010          // MEDIA_CHANGE_ON
+
+#define DEVICE_PREVENT_REMOVAL  0x0020
+
+
+#define DEF_NUMBER_OF_HEADS     0x10
+#define DEF_SECTORS_PER_TRACK   0x20
+
+
+DECLARE_MUTEX(msc_sem);
+
+
+/* MSC ******************************************************************************************** */
+
+struct msc_private msc_private;
+
+int msc_urb_sent (struct usbd_urb *tx_urb, int rc);
+
+
+/* Block Device ******************************************************************************** */
+
+/*! msc_open_blockdev - open the block device specified in msc->major, msc->minor
+ *
+ * Sets appropriate fields to show current status of block device.
+ *
+ * XXX TODO - this needs to be tested against RO and absent devices.
+ *
+ */
+void msc_open_blockdev(struct msc_private *msc)
+{
+        int rc;
+
+        down(&msc_sem);
+        msc->block_dev_state = DEVICE_EJECTED;
+
+        TRACE_MSG2(MSC, "OPEN BLOCKDEV: Major: %x Minor: %x", major, minor);
+
+        /*
+         * Check device information and verify access to the block device.
+         */
+        THROW_IF (!major, ejected);
+
+        msc->dev = MKDEV(major, minor);
+
+        THROW_IF (!(msc->bdev = bdget(kdev_t_to_nr(msc->dev))), ejected);
+
+        if ((rc = blkdev_get(msc->bdev, FMODE_READ | FMODE_WRITE, 0, BDEV_RAW))) {
+
+                TRACE_MSG0(MSC,"OPEN BLOCKDEV: cannot open RW");
+                THROW_IF ((rc = blkdev_get(msc->bdev, FMODE_READ, 0, BDEV_RAW)), ejected);
+                msc->block_dev_state |= DEVICE_WRITE_PROTECTED;
+        }
+
+        msc->io_state = MSC_INACTIVE;
+        msc->block_dev_state &= ~DEVICE_EJECTED;
+        msc->block_dev_state |= DEVICE_INSERTED | DEVICE_CHANGE_ON;
+
+        TRACE_MSG1(MSC,"OPEN BLOCKDEV: opened block_dev_state: %x", msc->block_dev_state);
+
+        /*
+         * Note that capacity must return the LBA of the last addressable block
+         * c.f. RBC 4.4, RBC 5.3 and notes below RBC Table 6
+         *
+         * The blk_size array contains the number of addressable blocks or N, 
+         * capacity is therefore N-1.
+         */
+         
+        msc->capacity = (blk_size[MAJOR(msc_private.dev)][MINOR(msc_private.dev)] << 1) - 1;
+        msc->block_size = get_hardsect_size(msc->dev);
+        msc->max_blocks = PAGE_SIZE / msc->block_size;
+
+        TRACE_MSG2(MSC,"blk_size: %x %d", 
+                        blk_size[MAJOR(msc_private.dev)][MINOR(msc_private.dev)] << 1,
+                        blk_size[MAJOR(msc_private.dev)][MINOR(msc_private.dev)] << 1); 
+
+        TRACE_MSG2(MSC,"capacity: %x %d", msc->capacity, msc->capacity); 
+        TRACE_MSG2(MSC,"block_size: %x %d", msc->block_size, msc->block_size); 
+        TRACE_MSG2(MSC,"max_blocks: %x %d", msc->max_blocks, msc->max_blocks); 
+
+        /* setup generic buffer_head
+         * XXX do we need two pages? it should be possible to have a single page
+         * for both read and write, in fact do we need a read_bh and write_bh?
+         * XXX ensure the page (or pages) get deallocated
+         */
+        msc->write_pending = msc->read_pending = 0;
+        msc->write_bh.b_rdev = msc->read_bh.b_rdev = msc->dev;
+        msc->write_bh.b_private = msc->read_bh.b_private = msc;
+        msc->read_bh.b_page = alloc_page(GFP_NOIO);     // XXX ensure that this gets de-allocated
+        msc->write_bh.b_page = alloc_page(GFP_NOIO);    // XXX ensure that this gets de-allocated
+        msc->read_bh.b_data = page_address(msc->read_bh.b_page);
+        msc->write_bh.b_data = page_address(msc->write_bh.b_page);
+
+        CATCH(ejected) {
+                TRACE_MSG1(MSC,"OPEN BLOCKDEV: EJECTED block_dev_state: %x", msc->block_dev_state);
+                printk(KERN_INFO"%s: Cannot get device %d %d\n", __FUNCTION__, major, minor); 
+        }
+        up(&msc_sem);
+}
+
+/*! msc_close_blockdev - close the device for host
+ */
+
+void msc_close_blockdev(struct msc_private *msc)
+{
+
+        RETURN_IF(msc->block_dev_state == DEVICE_EJECTED);
+
+        down(&msc_sem);
+	msc->block_dev_state = DEVICE_EJECTED;
+	if (msc->bdev)
+		blkdev_put(msc->bdev, BDEV_RAW);
+
+        // XXX this should be a wait for read_bh/write_bh to go to NULL
+        //while (msc->read_bh.b_data || msc->write_bh.b_data) {
+	while (msc->read_pending || msc->write_pending) {
+                printk(KERN_INFO"%s: sleeping on read or write bh\n", __FUNCTION__);
+                sleep_on_timeout(&msc->msc_wq, 20);
+        }
+        __free_page((void *)&msc->read_bh.b_page);
+        msc->read_bh.b_page = NULL;
+        __free_page((void *)&msc->write_bh.b_page);
+        msc->write_bh.b_page = NULL;
+        up(&msc_sem);
+}
+
+
+/* READ 10 COMMAND - read and send data to the host ******************************************** */
+
+int msc_start_reading_block_data(struct usbd_function_instance *function, struct msc_private *msc);
+void msc_block_read_finished(struct buffer_head *bh, int flag);
+
+
+/*! msc_scsi_read_10 - process a read(10) command
+ *
+ * We have received a READ(10) CBW, if transfer length is non-zero
+ * initiate a generic block i/o otherwise send a CSW.
+ *
+ * Returns non-zero if there is an error in the USB layer.
+ */
+int msc_scsi_read_10(struct msc_private *msc, char *name, int op)
+{
+        SCSI_READ_10_COMMAND *command = (SCSI_READ_10_COMMAND *)&msc->command.CBWCB;
+
+        /*
+         * save the CBW information and setup for transmitting data read from the block device
+         */
+        msc->lba = be32_to_cpu(command->LogicalBlockAddress);
+        msc->TransferLength_in_blocks = be16_to_cpu(command->TransferLength);
+        msc->TransferLength_in_bytes = msc->TransferLength_in_blocks * msc->block_size;
+        msc->command_state = MSC_DATA_IN_READ;
+        msc->io_state = MSC_INACTIVE;
+
+        /*
+         * Start reading blocks to send or simply send the CSW if the host
+         * didn't actually ask for a non-zero length.
+         */
+        return (msc->TransferLength_in_blocks) ?  msc_start_reading_block_data(msc->function, msc) :
+                msc_start_sending_csw(msc->function, msc, USB_MSC_PASSED);
+}
+
+
+/*! msc_start_reading_block_data - start reading data
+ *
+ * Generate a generic block io request to read some data to transmit.
+ *
+ * This function is initially called by msc_scsi_read_10() but can also be called
+ * by msc_urb_sent() if the amount of data requested was too large.
+ *
+ * The function msc_block_read_finished() will be called to actually send the data
+ * to the host when the I/O request is complete.
+ *
+ */
+int msc_start_reading_block_data(struct usbd_function_instance *function, struct msc_private *msc)
+{
+        int TransferLength_in_blocks = MIN(msc->max_blocks, msc->TransferLength_in_blocks);
+        unsigned long flags;
+        
+        TRACE_MSG3(MSC,"START READING BLOCK DATA lba:    %x blocks: %d %d ", 
+                        msc->lba, msc->TransferLength_in_blocks, TransferLength_in_blocks); 
+
+        /* ensure that device state is ok
+         */
+        if (msc->block_dev_state != DEVICE_INSERTED) {
+                TRACE_MSG0(MSC,"START READ MEDIA NOT PRESENT");
+                return msc_start_sending_csw_failed (msc, SCSI_SENSEKEY_MEDIA_NOT_PRESENT, msc->lba, USB_MSC_FAILED);
+        }
+
+        // XXX an ioctl has requested that pending io be aborted
+        local_irq_save(flags);
+        if (msc->io_state & MSC_ABORT_IO) {
+                msc->io_state &= ~MSC_ABORT_IO;
+                TRACE_MSG0(MSC,"BLOCK READ ABORTED"); 
+                local_irq_restore(flags);
+                return msc_start_sending_csw_failed (msc, SCSI_SENSEKEY_UNRECOVERED_READ_ERROR, msc->lba, USB_MSC_FAILED);
+        }
+        local_irq_restore(flags);
+
+        /* sanity check lba against capacity 
+         */
+        if (msc->lba >= msc->capacity) {
+                TRACE_MSG2(MSC, "START READ LBA out of range: lba: %d capacity: %d", msc->lba, msc->capacity);
+                return msc_start_sending_csw_failed (msc, SCSI_SENSEKEY_BLOCK_ADDRESS_OUT_OF_RANGE, msc->lba, USB_MSC_FAILED);
+        }
+
+        /* setup buffer head - msc_block_read_finished() will be called when block i/o is finished
+         */
+        msc->read_bh.b_end_io = msc_block_read_finished;
+        msc->read_bh.b_size = TransferLength_in_blocks * msc->block_size;
+        msc->read_bh.b_rsector = msc->lba;
+        msc->read_bh.b_state = (1UL << BH_Mapped) | (1UL << BH_Lock);
+        msc->io_state |= MSC_BLOCKIO_PENDING;
+	msc->read_pending=1;
+	
+        memset(msc->read_bh.b_data, 0x0, msc->read_bh.b_size);
+
+        generic_make_request(READ, &msc->read_bh);
+        generic_unplug_device(blk_get_queue(msc->read_bh.b_rdev));
+        return 0;
+}
+
+
+/*! msc_block_read_finished - called by generic request 
+ *
+ * Called when block i/o read is complete, send the data to the host if possible.
+ *
+ * The function msc_urb_sent() will be called when the data is sent to
+ * either send additional data or the CSW as appropriate.
+ *
+ * If more data is required then call msc_start_reading_block_data() to
+ * issue another block i/o to get more data.
+ *
+ * These means that there can be two outstanding actions when this
+ * function completes:
+ *
+ *      1. a transmit urb may be pending, sending the most recently
+ *         read data to the host
+ *      2. another block i/o may be pending to read additional data
+ *
+ * This leads to a race condition, if the block i/o finished before the urb
+ * transmit, then we must simply exit. The msc_in_read_10_urb_sent()
+ * function will ensure that this is restarted.
+ */
+void msc_block_read_finished(struct buffer_head *bh, int uptodate)
+{
+        //struct msc_private *msc = bh->b_private;
+        //int TransferLength_in_blocks = bh->b_size / msc->block_size;
+        
+        struct msc_private *msc;
+        int TransferLength_in_blocks;
+
+        struct usbd_function_instance *function;
+        struct usbd_urb *tx_urb;
+        unsigned long flags;
+        int rc;
+#ifdef CONFIG_OTG_MSC_BLOCK_TRACE
+        u32 crc;
+#endif /* CONFIG_OTG_MSC_BLOCK_TRACE */
+        int i;
+
+        msc = bh->b_private;
+        TransferLength_in_blocks = bh->b_size / msc->block_size;
+
+        TRACE_MSG1(MSC,"BLOCK READ FINISHED size: %x", bh->b_size); 
+
+        #if 0
+        local_irq_save(flags);
+        if (msc->io_state & MSC_IOCTL_WAITING) {
+                wake_up_interruptible(&msc->ioctl_wq);
+                msc->io_state &= ~MSC_IOCTL_WAITING;
+        }
+        local_irq_restore(flags);
+        #endif
+
+        wake_up_interruptible(&msc->ioctl_wq);
+        
+        /*
+         * Race condition here, if we have not finished sending the
+         * previous tx_urb then we want to simply exit and let
+         * msc_in_read_10_urb_sent() call us again.
+         *
+         * Ensure that we do not reset BLOCKIO flags if SEND PENDING and
+         * that we do reset BLOCKIO if not SEND PENDING
+         */
+        {
+                unsigned long flags;
+                local_irq_save(flags);
+                if (msc->io_state & MSC_SEND_PENDING) {
+                        TRACE_MSG0(MSC,"BLOCK READ SEND PENDING");
+                        msc->io_state |= MSC_BLOCKIO_FINISHED;
+                        msc->uptodate = uptodate;
+                        local_irq_restore(flags);
+                        return;
+                }
+                msc->io_state &= ~(MSC_BLOCKIO_PENDING | MSC_BLOCKIO_FINISHED);
+
+                local_irq_restore(flags);
+        }
+
+        /* verify that the I/O completed
+         */
+        if (1 != uptodate) {
+                TRACE_MSG0(MSC,"BLOCK READ FAILED"); 
+                msc_start_sending_csw_failed (msc, SCSI_SENSEKEY_UNRECOVERED_READ_ERROR, msc->lba, USB_MSC_FAILED);
+                return;
+        }
+
+#ifdef CONFIG_OTG_MSC_BLOCK_TRACE
+        /* debug output trace - dump rlba and computed crc
+         */
+        for (i = 0; i < bh->b_size / msc->block_size; i++) {
+                crc = crc32_compute(bh->b_data + (i * msc->block_size), msc->block_size, CRC32_INIT);
+                TRACE_RLBA(bh->b_rsector + i, crc);
+        }
+#endif /* CONFIG_OTG_MSC_BLOCK_TRACE */
+        /* allocate a tx_urb and copy into it
+         */
+        THROW_IF(!(function = msc->function), error);
+        THROW_IF(!(tx_urb = usbd_alloc_urb (function, BULK_IN, bh->b_size, msc_urb_sent)), error);
+        TRACE_MSG1(MSC,"BLOCK READ FINISHED urb: %p", (int)tx_urb);
+
+        tx_urb->function_privdata = msc;
+        tx_urb->actual_length = tx_urb->buffer_length = bh->b_size;
+        memcpy(tx_urb->buffer, bh->b_data, bh->b_size);
+
+	msc->read_pending=0;
+
+        {
+                unsigned long flags;
+                local_irq_save(flags);
+
+                msc->io_state |= MSC_SEND_PENDING;
+                msc->TransferLength_in_bytes -= bh->b_size;
+                msc->TransferLength_in_blocks -= TransferLength_in_blocks; 
+                msc->lba += TransferLength_in_blocks;
+
+                if (!msc->TransferLength_in_blocks) {
+                        TRACE_MSG0(MSC,"BLOCK READ FINISHED - IO FINISHED");
+                        // set flag so that CSW can be sent
+                        msc->io_state |= MSC_DATA_IN_READ_FINISHED;
+                }
+                local_irq_restore(flags);
+        }
+
+        /* dispatch urb - msc_urb_sent() will be called when urb is finished
+         */
+        if ((rc = usbd_start_in_urb (tx_urb))) {
+                TRACE_MSG0(MSC,"BLOCK READ FINISHED FAILED"); 
+                usbd_free_urb (tx_urb);
+                THROW(error);
+        }
+
+        /* if more data is required then call msc_start_reading_block_data() to
+         * issue another block i/o to get more data.
+         */
+        if (!(msc->io_state & MSC_DATA_IN_READ_FINISHED)) {
+                TRACE_MSG0(MSC,"BLOCK READ SEND RESTARTING");
+                msc_start_reading_block_data(msc->function, msc);
+        }
+
+        CATCH(error) {
+                TRACE_MSG0(MSC,"BLOCK READ FINISHED ERROR"); 
+                // stall?
+        }
+}
+
+/*! msc_in_read_10_urb_sent - called by msc_urb_sent when state is MSC_DATA_IN_READ
+ *
+ * This will process a read_10 urb that has been sent. Specifically if any previous
+ * read_10 block I/O has finished we recall the msc_block_read_finished() function
+ * to transmit another read_10 urb. 
+ *
+ * If there is no other pending read_10 to do we create and send a CSW.
+ *
+ * If there is more I/O to do or there is already an outstanding I/O we simply
+ * return after freeing the URB.
+ *
+ * Return non-zero if urb was not disposed of.
+ */
+int msc_in_read_10_urb_sent(struct usbd_urb *tx_urb, struct msc_private *msc)
+{
+        unsigned long flags;
+
+        TRACE_MSG0(MSC,"URB SENT DATA IN");
+
+        /*
+         * Potential race condition here, we may need to restart blockio.
+         */
+        local_irq_save(flags);
+
+        msc->io_state &= ~MSC_SEND_PENDING;
+        msc->data_transferred_in_bytes += tx_urb->actual_length;
+
+        TRACE_MSG1(MSC,"URB SENT DATA IN data transferred: %d", msc->data_transferred_in_bytes);
+
+        if (!tx_urb->actual_length) 
+                msc->TransferLength_in_blocks = 0;
+
+        /* XXX We should be checking urb status
+         */
+
+        usbd_free_urb (tx_urb);
+
+        /*
+         * Check to see if we need to send CSW.
+         */
+        if (MSC_DATA_IN_READ_FINISHED & msc->io_state) {
+                TRACE_MSG0(MSC,"URB SENT DATA IN - FINISHED SENDING CSW");
+                local_irq_restore(flags);
+                return msc_start_sending_csw(msc->function, msc, USB_MSC_PASSED);
+        }
+        /*
+         * Check to see if there is a block read needs to be finished.
+         */
+        if (MSC_BLOCKIO_FINISHED & msc->io_state) {
+                TRACE_MSG0(MSC,"URB SENT DATA IN - RESTART");
+                local_irq_restore(flags);
+                // XXX uptodate?
+                msc_block_read_finished(&msc->read_bh, msc->uptodate);
+                return 0;
+        }
+        local_irq_restore(flags);
+        return 0;
+}
+
+
+/* WRITE 10 - receive data from host and write to block device ********************************* */
+
+int msc_start_receiving_data(struct usbd_function_instance *function, struct msc_private *msc);
+void msc_data_written(struct buffer_head *bh, int flag);
+void msc_data_test(struct buffer_head *bh, int flag);
+
+/*! msc_scsi_write_10 - process a write command
+ *
+ * Call either msc_start_receiving_data() or msc_start_sending_csw() as appropriate.
+ * Normally msc_start_receiving_data() is called and it will use msc_start_recv_urb() 
+ * to setup a receive urb of the appropriate size. 
+ *
+ * Returns non-zero if there is an error in the USB layer.
+ */
+int msc_scsi_write_10(struct msc_private *msc, char *name, int op)
+{
+        SCSI_WRITE_10_COMMAND *command = (SCSI_WRITE_10_COMMAND *)&msc->command.CBWCB;
+
+        /* save the CBW and setup for receiving data to be written to the block device
+         */
+        msc->lba = be32_to_cpu(command->LogicalBlockAddress);
+        msc->TransferLength_in_blocks = be16_to_cpu(command->TransferLength);
+        msc->TransferLength_in_bytes = msc->TransferLength_in_blocks * msc->block_size;
+
+        msc->command_state = MSC_DATA_OUT_WRITE;
+        msc->io_state = MSC_INACTIVE;
+
+        TRACE_MSG1(MSC,"RECV WRITE lba: %x", msc->lba); 
+        TRACE_MSG1(MSC,"RECV WRITE blocks: %d", msc->TransferLength_in_blocks); 
+
+        return (msc->TransferLength_in_blocks) ?
+                msc_start_receiving_data(msc->function, msc) :
+                msc_start_sending_csw(msc->function, msc, USB_MSC_PASSED);
+}
+
+
+/*! msc_start_receiving_data - called to initiate an urb to receive WRITE(10) data
+ *
+ * Initiate a receive urb to receive upto PAGE_SIZE WRITE(10) data. The
+ * msc_recv_urb() function will call msc_recv_out_blocks() to actually
+ * write the data. 
+ *
+ * This is called from msc_scsi_write_10() to initiate the WRITE(10) and
+ * called from msc_data_written() to start the next page sized receive
+ * urb.
+ */
+int msc_start_receiving_data(struct usbd_function_instance *function, struct msc_private *msc)
+{
+        /*
+         * Calculating the length is most of the work we do :-)
+         */
+        int TransferLength_in_blocks = MIN(msc->max_blocks, msc->TransferLength_in_blocks);
+
+        TRACE_MSG1(MSC,"START RECEIVING DATA lba: %x", msc->lba); 
+        TRACE_MSG1(MSC,"START RECEIVING DATA blocks: %d", msc->TransferLength_in_blocks);
+        TRACE_MSG1(MSC,"START RECEIVING DATA blocks: %d", TransferLength_in_blocks);
+        TRACE_MSG1(MSC,"START RECEIVING DATA bytes: %d", TransferLength_in_blocks * msc->block_size);
+
+        THROW_IF(msc->command_state != MSC_DATA_OUT_WRITE, error);
+        THROW_IF(!msc->TransferLength_in_blocks, error);
+
+        msc->io_state |= MSC_RECV_PENDING;
+
+        return msc_start_recv_urb(function, msc, TransferLength_in_blocks * msc->block_size);
+
+        CATCH(error) {
+                TRACE_MSG0(MSC,"START RECEIVING DATA ERROR");
+                return -EINVAL;
+        }
+}
+
+
+/*! msc_recv_out_blocks - process received WRITE(10) data by writing to block device
+ *
+ * We get here indirectly from msc_recv_urb() when state is MSC_DATA_OUT_WRITE.
+ *
+ * Dealloc the urb and call Initiate the generic request to write the
+ * received data. The b_end_io function msc_data_written() will send the
+ * CSW.
+ *
+ */
+void msc_recv_out_blocks(struct usbd_urb *rcv_urb, struct msc_private *msc)
+{
+        int TransferLength_in_bytes = rcv_urb->actual_length;
+        int TransferLength_in_blocks = TransferLength_in_bytes / msc->block_size;
+#ifdef CONFIG_OTG_MSC_BLOCK_TRACE
+        u32 crc;
+#endif /* CONFIG_OTG_MSC_BLOCK_TRACE */
+        int i;
+
+        TRACE_MSG1(MSC,"RECV OUT            bytes: %d", TransferLength_in_bytes);
+        TRACE_MSG1(MSC,"RECV OUT          iostate: %x", msc->io_state);
+        TRACE_MSG1(MSC,"RECV OUT data transferred: %d", msc->data_transferred_in_bytes);
+
+        /*
+         * Race condition here, we may get to here before the previous block
+         * write completed. If so just exit and the msc_data_written()
+         * function will recall us.
+         */
+        {
+                unsigned long flags;
+                local_irq_save(flags);
+                if (msc->io_state & MSC_BLOCKIO_PENDING) {
+                        TRACE_MSG0(MSC,"RECV OUT  BLOCKIO PENDING");
+                        msc->io_state |= MSC_RECV_FINISHED;
+                        msc->rcv_urb_finished = rcv_urb;
+                        local_irq_restore(flags);
+                        return;
+                }
+                msc->io_state &= ~(MSC_RECV_PENDING |MSC_RECV_FINISHED);
+                local_irq_restore(flags);
+        }
+
+        msc->data_transferred_in_bytes += rcv_urb->actual_length;
+
+#ifdef CONFIG_OTG_MSC_BLOCK_TRACE
+        for (i = 0; i < TransferLength_in_blocks; i++) {
+                crc = crc32_compute(rcv_urb->buffer + (i * msc->block_size), msc->block_size, CRC32_INIT);
+                TRACE_SLBA(msc->lba + i, crc);
+        }
+#endif /* CONFIG_OTG_MSC_BLOCK_TRACE */
+        
+	msc->write_pending=1;
+
+        memcpy(msc->write_bh.b_data, rcv_urb->buffer, rcv_urb->actual_length);
+
+        usbd_free_urb(rcv_urb);
+
+        /* ensure media state is ok
+         */
+        if (msc->block_dev_state != DEVICE_INSERTED) {
+                TRACE_MSG0(MSC,"START READ MEDIA NOT PRESENT");
+                msc_start_sending_csw_failed (msc, SCSI_SENSEKEY_MEDIA_NOT_PRESENT, msc->lba, USB_MSC_FAILED);
+                return;
+        }
+
+        // XXX an ioctl has requested that pending io be aborted
+        if (msc->io_state & MSC_ABORT_IO) {
+                unsigned long flags;
+                local_irq_save(flags);
+                msc->io_state &= ~MSC_ABORT_IO;
+                TRACE_MSG0(MSC,"BLOCK READ ABORTED"); 
+                local_irq_restore(flags);
+                msc_start_sending_csw_failed (msc, SCSI_SENSEKEY_UNRECOVERED_READ_ERROR, msc->lba, USB_MSC_FAILED);
+                return;
+        }
+
+        /* sanity check lba against capacity
+         */
+        if (msc->lba >= msc->capacity) {
+                TRACE_MSG2(MSC, "START READ LBA out of range: lba: %d capacity: %d", msc->lba, msc->capacity);
+                msc_start_sending_csw_failed (msc, SCSI_SENSEKEY_BLOCK_ADDRESS_OUT_OF_RANGE, msc->lba, USB_MSC_FAILED);
+                return;
+        }
+
+        /* additional sanity check for lba - ensure non-zero
+         */
+        if (!msc->TransferLength_in_blocks) {
+                TRACE_MSG0(MSC,"START READ LBA TransferLength_in_blocks zero:");
+                msc_start_sending_csw_failed (msc, SCSI_SENSEKEY_BLOCK_ADDRESS_OUT_OF_RANGE, msc->lba, USB_MSC_FAILED);
+                return;
+        }
+        
+        /* XXX additional sanity check required here - verify that the transfer
+         * size agrees with what we where expecting, specifically I think
+         * we need to verify that we have received a multiple of the block
+         * size and either a full bulk transfer (so more will come) or
+         * the actual exact amount that the host said it would send.
+         * An error should generate a CSW with a USB_MSC_PHASE_ERROR
+         */
+
+
+        TRACE_MSG1(MSC,"RECV OUT              lba: %x", msc->lba); 
+        TRACE_MSG1(MSC,"RECV OUT   blocks    left: %d", msc->TransferLength_in_blocks); 
+        TRACE_MSG1(MSC,"RECV OUT   blocks current: %d", TransferLength_in_blocks); 
+
+        /* Initiate writing the data - msc_data_written() will be called
+         * when finished.
+         */
+        msc->write_bh.b_end_io = msc_data_written;
+
+        /* set address in buffer head
+         */
+        msc->write_bh.b_size = TransferLength_in_bytes;
+        msc->write_bh.b_rsector = msc->lba;
+
+        /* decrement counters and increment address
+         */
+        msc->TransferLength_in_bytes -= TransferLength_in_bytes;
+        msc->TransferLength_in_blocks -= TransferLength_in_blocks;
+        msc->lba += TransferLength_in_blocks;
+        msc->write_bh.b_state = (1UL << BH_Mapped) | (1UL << BH_Lock);
+        msc->io_state |= MSC_BLOCKIO_PENDING;
+
+
+        /* Check current status of TransferLength - if non-zero then we need
+         * to queue another urb to receive more data.
+         */
+        if (!msc->TransferLength_in_blocks) 
+                msc->command_state = MSC_DATA_OUT_WRITE_FINISHED;
+        else 
+                msc_start_receiving_data(msc->function, msc);
+
+        /* initiate the block i/o request
+         */
+        generic_make_request(WRITE, &msc->write_bh);
+        generic_unplug_device(blk_get_queue(msc->write_bh.b_rdev));
+}
+
+
+/*! msc_data_written - called by generic request 
+ *
+ * Called when current block i/o read is finished, restart block i/o or send the CSW.
+ *
+ */
+void msc_data_written(struct buffer_head *bh, int uptodate)
+{
+        struct msc_private *msc = bh->b_private;
+        unsigned long flags;
+        u8 io_state;
+
+        TRACE_MSG4(MSC,"DATA WRITTEN uptodate: %x b_state: %x state: %x io: %x", 
+                        uptodate, bh->b_state, msc->command_state, msc->io_state); 
+
+        TRACE_MSG1(MSC,"DATA WRITTEN lba: %x", msc->lba); 
+
+        local_irq_save(flags);
+        io_state = msc->io_state &= ~MSC_BLOCKIO_PENDING;
+	msc->write_pending=0;
+
+        local_irq_restore(flags);
+       
+        // XXX an ioctl has waited for io to complete, need to wake it up
+        #if 0
+        local_irq_save(flags);
+        if (msc->io_state & MSC_IOCTL_WAITING) {
+                wake_up_interruptible(&msc->ioctl_wq);
+                msc->io_state &= ~MSC_IOCTL_WAITING;
+        }
+        local_irq_restore(flags);
+        #endif
+
+        wake_up_interruptible(&msc->ioctl_wq);
+
+        /*
+         * verify that the I/O completed
+         */
+        if (1 != uptodate) {
+                TRACE_MSG0(MSC,"BLOCK READ FAILED"); 
+                msc_start_sending_csw_failed (msc, SCSI_SENSEKEY_UNRECOVERED_READ_ERROR, msc->lba, USB_MSC_FAILED);
+                // XXX CHECKME
+                if (msc->rcv_urb_finished) {
+                        usbd_free_urb(msc->rcv_urb_finished);
+                        msc->rcv_urb_finished = NULL;
+                }
+                return;
+        }
+
+        /*
+         * If there was a rcv_urb that was not processed then we need
+         * to process it now. This is done by simply restarting msc_recv_out()
+         */
+        if ((io_state & MSC_RECV_FINISHED) && msc->rcv_urb_finished) {
+                struct usbd_urb *rcv_urb = msc->rcv_urb_finished;
+                msc->rcv_urb_finished = NULL;
+                TRACE_MSG0(MSC,"DATA WRITTEN RECV FINISHED");
+                msc_recv_out_blocks(rcv_urb, msc);
+        }
+        /*
+         * DATA_IN mode and no more data to write
+         */
+        if (MSC_DATA_OUT_WRITE_FINISHED == msc->command_state) {
+                // finished, send CSW
+                TRACE_MSG0(MSC,"DATA WRITTEN send CSW");
+                msc_start_sending_csw(msc->function, msc, USB_MSC_PASSED);
+        }
+}
+
+extern struct usbd_function_operations msc_function_ops;
+extern struct usbd_function_driver msc_function_driver;
+
+
+/* USB Module init/exit ************************************************************************ */
+
+int msc_io_init_l24(void);
+void msc_io_exit_l24(void);
+
+/*! msc_modinit - module init
+ *
+ */
+static int msc_modinit (void)
+{
+        int rc;
+        struct msc_private *msc = &msc_private;
+
+        printk(KERN_INFO "Copyright (c) 2004 Belcarra Technologies; www.belcarra.com; sl@belcarra.com\n");
+        printk (KERN_INFO "%s vendor_id: %04x product_id: %04x major: %d minor: %d\n", __FUNCTION__, 
+                        vendor_id, product_id, major, minor);
+
+
+        MSC = otg_trace_obtain_tag();
+
+        if (vendor_id) 
+                msc_function_driver.idVendor = cpu_to_le16(vendor_id);
+        if (product_id) 
+                msc_function_driver.idProduct = cpu_to_le16(product_id);
+
+        init_waitqueue_head(&msc->msc_wq);
+        init_waitqueue_head(&msc->ioctl_wq);
+
+        msc->block_dev_state = DEVICE_EJECTED;
+
+
+        msc->command_state = MSC_READY;
+        msc->io_state = MSC_INACTIVE;
+
+#ifdef CONFIG_OTG_MSC_BLOCK_TRACE
+        make_crc_table();
+#endif /* CONFIG_OTG_MSC_BLOCK_TRACE */
+
+        TRACE_MSG3(MSC,"PAGE_SHIFT: %x PAGE_SIZE: %x %d", PAGE_SHIFT, PAGE_SIZE, PAGE_SIZE); 
+
+        THROW_IF(msc_io_init_l24(), error);
+
+        /* register as usb function driver
+         */
+        THROW_UNLESS ((msc->function = usbd_register_function (&msc_function_driver, "msc", NULL)), error);
+        msc->usb_driver_registered++;
+
+        CATCH(error) {
+                if (msc->usb_driver_registered) {
+                        usbd_deregister_function (msc->function);
+                        msc->usb_driver_registered = 0;
+                }
+                otg_trace_invalidate_tag(MSC);
+                return -EINVAL;
+        }
+        return 0;
+}
+
+/*! msc_modexit - module cleanup
+ */
+static void msc_modexit (void)
+{
+        struct msc_private *msc = &msc_private;
+
+        /* destroy control io interface
+         */
+        msc_io_exit_l24();
+
+        /* flush io and free page buffers
+         */
+        msc_close_blockdev(msc);
+
+        if (msc->usb_driver_registered) 
+                usbd_deregister_function (msc->function);
+
+#ifdef CONFIG_OTG_MSC_BLOCK_TRACE
+        free_crc_table();
+#endif /* CONFIG_OTG_MSC_BLOCK_TRACE */
+        otg_trace_invalidate_tag(MSC);
+}
+
+
+module_init (msc_modinit);
+module_exit (msc_modexit);
+
diff -uNr linux/drivers/no-otg/functions/msc/msc-original.c linux/drivers/otg/functions/msc/msc-original.c
--- linux/drivers/no-otg/functions/msc/msc-original.c	1970-01-01 01:00:00.000000000 +0100
+++ linux/drivers/otg/functions/msc/msc-original.c	2006-09-01 21:41:28.000000000 +0200
@@ -0,0 +1,2209 @@
+/*
+ * otg/msc_fd/msc.c
+ *
+ *      Copyright (c) 2003, 2004 Belcarra
+ *
+ * By: 
+ *      Stuart Lynne <sl@belcarra.com>, 
+ *      Tom Rushworth <tbr@belcarra.com>, 
+ *      Bruce Balden <balden@belcarra.com>
+ *
+ *
+ * This is a Mass Storage Class Function that uses the Bulk Only protocol.
+ *
+ * To use simply load with something like:
+ *
+ *      insmod msc_fd.o vendor_id=0xffff product_id=0xffff
+ *
+ * Notes:
+ *
+ * 1. Currently block I/O is done a page at a time. I have not determined if
+ * it is possible to dispatch a multiple page generic request. It should at
+ * least be possible to queue page requests. 
+ *
+ * 2. Currently for READ operations we have a maximum of one outstanding
+ * read block I/O and send urb. These are allowed to overlap so that we can
+ * continue to read data while sending data to the host.
+ *
+ * 3. Currently for WRITE operations we have a maximum of one outstanding
+ * recv urb and one outstanding write block I/O. These are allowed to
+ * overlap so that we can continue to receive data from the host while
+ * waiting for writing to complete.
+ *
+ * 4. It would be possible to allow multiple writes to be pending, to the
+ * limit of the page cache, if desired. 
+ *
+ * 5. It should be possible to allow multiple outstanding reads to be
+ * pending, to the limit of the page cache, but this potentially could
+ * require dealing with out of order completions of the reads. Essentially a
+ * list of completed buffer heads would be required to hold any completed
+ * buffer heads that cannot be sent prior to another, earlier request being
+ * completed. 
+ *
+ * 6. Currently we only support the Bulk Only model. Microsoft states that
+ * further support for the mass storage driver will only be done for devices
+ * that conform to the Bulk Only model.
+ *
+ * 7. Multiple LUN's are not supported, but in theory they could be.
+ *
+ * 8. It should be possible to use the removalble disk model to allow for a
+ * hotplug script to umount locally and signal that the block device is now
+ * available. An insertion event would notify the host that it can now use
+ * the device. Similarily applications could notify us to send a removal
+ * event to the host so that the device can be mounted locally.
+ *
+ * 9. Error handling should be done with STALL but using ZLP seems to also
+ * work. ZLP is usually easier to implement (except possibly on the SA1100.)
+ * We may need to make STALL an option if we find devices (perhaps SA1100)
+ * that cannot reliaby send a ZLP on BULK-IN endpoint.
+ *
+ *
+ * 10. WinXP will match the following:
+ *
+ *      USB\Class_08&SubClass_02&Prot_50
+ *      USB\Class_08&SubClass_05&Prot_50
+ *      USB\Class_08&SubClass_06&Prot_50
+ *
+ * SubClass 02 is MMC or SFF8020I
+ * SubClass 05 is SFF or SFF8070I
+ * SubClass 06 is SCSI
+ *
+ * From the Windows USB Storage FAQ:
+ *
+ *      RBC not supported
+ *
+ *      SubClass = 6 (SCSI)
+ *              CDBs SHOULD NOT be padded to 12 bytes
+ *              ModeSense/ModeSelect SHOULD NOT be translated from 1ah/15h to 5ah/55h
+ *              should be used for FLASH
+ *
+ *      SubClass !=6
+ *              CDBs SHOULD be padded to 12 bytes
+ *              ModeSense/ModeSelect SHOULD be translated from 1ah/15h to 5ah/55h
+ *
+ * We are using the former, SubClass = 6, and implement the required SCSI operations.
+ *
+ * 
+ * TODO
+ *
+ * 1. error handling for block io, e.g. what if using with removable block
+ * device (like CF card) and it is removed.
+ *
+ * 2. Currently we memcpy() data from between the urb buffer and buffer
+ * head page. It should be possible to simply use the page buffer for the
+ * urb.
+ *
+ * 3. Should we offer using fileio as an option? This would allow direct access
+ * to a block device image stored in a normal file or direct access to (for example)
+ * ram disks. It would require implementing a separate file I/O kernel thread to
+ * do the actual I/O.
+ *
+ * 
+ * TODO FIXME Bus Interface Notes
+ *
+ * 1. The bus interface driver must correctly implement NAK'ing if not rcv_urb is
+ * present (see au1x00.c or wmmx.c for examples.)
+ *
+ * 2. The bus interface driver must implement USBD_URB_SENDZLP flag (see au1x00.c 
+ * for example.)
+ *
+ */
+
+#if 1
+#include <otg/otg-compat.h>
+#include <otg/usbp-chap9.h>
+#include <otg/usbp-func.h>
+#include <otg/otg-trace.h>
+#include <otg/otg-mesg.h>
+
+#include <linux/config.h>
+#include <linux/module.h>
+#include <linux/kernel.h>
+#include <linux/version.h>
+
+#include <linux/init.h>
+#include <asm/uaccess.h>
+#include <linux/ctype.h>
+#include <linux/timer.h>
+#include <linux/interrupt.h>
+#include <asm/atomic.h>
+#include <linux/random.h>
+#include <linux/slab.h>
+
+#include <linux/blkdev.h>
+
+
+#include "msc-scsi.h"
+#include "msc.h"
+#include "crc.h"
+
+static __inline__ void TRACE_SENSE(unsigned int sense, unsigned int info)
+{
+        TRACE_MSG2(MSC, "-->             SENSE: %06x INFO: %08x", sense, info);
+}
+static __inline__ void TRACE_RLBA(unsigned int lba, unsigned int crc)
+{
+        TRACE_MSG2(MSC, "<--             rlba [%8x %08x]", lba, crc);
+}
+static __inline__ void TRACE_SLBA(unsigned int lba, unsigned int crc)
+{
+        TRACE_MSG2(MSC, "-->             slba [%8x %08x]", lba, crc);
+}
+static __inline__ void TRACE_TLBA(unsigned int lba, unsigned int crc)
+{
+        TRACE_MSG2(MSC, "-->             tlba [%8x %08x]", lba, crc);
+}
+static __inline__ void TRACE_TAG(unsigned int tag, unsigned int frame)
+{
+        TRACE_MSG2(MSC, "-->             TAG: %8x FRAME: %03x", tag, frame);
+}
+static __inline__ void TRACE_CBW(char *msg, int val)
+{
+        TRACE_MSG2(MSC,  " -->                  %s %02x", msg, val);
+}
+static __inline__ void TRACE_RECV(unsigned char *cp)
+{
+        TRACE_MSG8(MSC, "<--             recv [%02x %02x %02x %02x %02x %02x %02x %02x]",
+                        cp[0], cp[1], cp[2], cp[3], cp[4], cp[5], cp[6], cp[7]);
+}
+static __inline__ void TRACE_SENT(unsigned char *cp)
+{
+        TRACE_MSG8(MSC, "-->             sent [%02x %02x %02x %02x %02x %02x %02x %02x]",
+                        cp[0], cp[1], cp[2], cp[3], cp[4], cp[5], cp[6], cp[7]);
+}
+
+
+#else
+
+#include <linux/config.h>
+#include <linux/module.h>
+#include <linux/kernel.h>
+#include <linux/version.h>
+
+MODULE_AUTHOR ("sl@belcarra.com, tbr@belcarra.com");
+MODULE_LICENSE("PROPRIETARY");
+MODULE_DESCRIPTION ("Belcarra Mass Storage Class Bulk Only Function");
+
+#include <linux/init.h>
+#include <asm/uaccess.h>
+#include <linux/ctype.h>
+#include <linux/timer.h>
+#include <linux/interrupt.h>
+#include <asm/atomic.h>
+#include <linux/random.h>
+#include <linux/slab.h>
+
+#include <linux/blkdev.h>
+
+#include "usbp-chap9.h"
+#include <usbp-mem.h>
+#include <otg-compat.h>
+#include <usbp-func.h>
+
+USBD_MODULE_INFO ("msc_fd 2.0-beta");
+
+#include "msc-scsi.h"
+#include "msc.h"
+
+#include "trace.h"
+#include "crc.h"
+#endif
+
+//#include "rbc.h"
+
+
+/* Module Parameters ************************************************************************* */
+
+static u32 vendor_id;
+static u32 product_id;
+
+static u32 major;
+static u32 minor;
+
+MODULE_PARM (vendor_id, "i");
+MODULE_PARM (product_id, "i");
+MODULE_PARM (major, "i");
+MODULE_PARM (minor, "i");
+
+MODULE_PARM_DESC (vendor_id, "Device Vendor ID");
+MODULE_PARM_DESC (product_id, "Device Product ID");
+MODULE_PARM_DESC (major, "Device Major");
+MODULE_PARM_DESC (minor, "Device Minor");
+
+/*
+ * MSC Configuration
+ *
+ * Endpoint, Class, Interface, Configuration and Device descriptors/descriptions
+ */
+
+#define BULK_OUT        0x00
+#define BULK_IN         0x01
+#define ENDPOINTS       0x02
+
+/*
+ * Mass Storage Class - Bulk Only
+ *
+ * Endpoint, Class, Interface, Configuration and Device descriptors/descriptions
+ */
+
+static u8 msc_data_1[] = { 0x07, USB_DT_ENDPOINT, USB_DIR_OUT, BULK,     0, 0x00, 0x00,  };
+static u8 msc_data_2[] = { 0x07, USB_DT_ENDPOINT, USB_DIR_IN,  BULK,     0, 0x00, 0x00, };
+static struct usbd_endpoint_descriptor *msc_default[] = { 
+        (struct usbd_endpoint_descriptor *) msc_data_1, 
+        (struct usbd_endpoint_descriptor *) msc_data_2, };
+u8 msc_indexes[] = { BULK_OUT, BULK_IN, };
+
+
+/* Endpoint, Interface, Configuration and Device descriptions and descriptors
+ */
+static u8 msc_data_alternate_descriptor[sizeof(struct usbd_interface_descriptor)] = {
+        0x09, USB_DT_INTERFACE, 
+        0x00, 0x00, // bInterfaceNumber, bAlternateSetting
+        sizeof (msc_default) / sizeof(struct usbd_endpoint_descriptor), // bNumEndpoints
+        MASS_STORAGE_CLASS,
+        MASS_STORAGE_SUBCLASS_SCSI,
+        MASS_STORAGE_PROTO_BULK_ONLY,
+        0x00,
+};
+
+static struct usbd_alternate_description msc_data_alternate_descriptions[] = {
+        { iInterface: CONFIG_OTG_MSC_INTF,
+                interface_descriptor: (struct usbd_interface_descriptor *)&msc_data_alternate_descriptor,
+                endpoints:sizeof (msc_default) / sizeof(struct usbd_endpoint_descriptor *),
+                endpoint_list: msc_default,
+                endpoint_indexes: msc_indexes,
+                },
+};
+
+
+struct usbd_interface_description msc_interfaces[] = {
+        { alternates:sizeof (msc_data_alternate_descriptions) / sizeof (struct usbd_alternate_description),
+                alternate_list:msc_data_alternate_descriptions,},
+};
+
+u8 msc_configuration_descriptor[sizeof(struct usbd_configuration_descriptor)] = {
+        0x09, USB_DT_CONFIGURATION, 0x00, 0x00, // wLength
+        sizeof (msc_interfaces) / sizeof (struct usbd_interface_description),
+        0x01, 0x00, // bConfigurationValue, iConfiguration
+        0, 0,
+};
+
+struct usbd_configuration_description msc_description[] = {
+        { iConfiguration: CONFIG_OTG_MSC_DESC,
+                configuration_descriptor: (struct usbd_configuration_descriptor *)msc_configuration_descriptor,
+        },
+};
+
+
+static struct usbd_device_descriptor msc_device_descriptor = {
+        bLength: sizeof(struct usbd_device_descriptor),
+        bDescriptorType: USB_DT_DEVICE,
+        bcdUSB: __constant_cpu_to_le16(USB_BCD_VERSION),
+        bDeviceClass: 0x00,
+        bDeviceSubClass: 0x02,
+        bDeviceProtocol: 0x00,
+        bMaxPacketSize0: 0x00,
+        idVendor: __constant_cpu_to_le16(CONFIG_OTG_MSC_VENDORID),
+        idProduct: __constant_cpu_to_le16(CONFIG_OTG_MSC_PRODUCTID),
+        bcdDevice: __constant_cpu_to_le16(CONFIG_OTG_MSC_BCDDEVICE),
+};
+
+#ifdef CONFIG_OTG_HIGH_SPEED
+static struct usbd_device_qualifier_descriptor msc_device_qualifier_descriptor = {
+        bLength: sizeof(struct usbd_device_qualifier_descriptor),
+        bDescriptorType: USB_DT_DEVICE_QUALIFIER,
+        bcdUSB: __constant_cpu_to_le16(USB_BCD_VERSION),
+        bDeviceClass: 0x00,
+        bDeviceSubClass: 0x02,
+        bDeviceProtocol: 0x00,
+        bMaxPacketSize0: 0x00,
+};
+#endif /* CONFIG_OTG_HIGH_SPEED */
+
+
+
+
+static struct usbd_endpoint_request msc_endpoint_requests[ENDPOINTS+1] = {
+        { 1, 0, 0, USB_DIR_OUT | USB_ENDPOINT_BULK, PAGE_SIZE, PAGE_SIZE, },
+        { 1, 0, 0, USB_DIR_IN | USB_ENDPOINT_BULK, PAGE_SIZE, PAGE_SIZE, },
+        { 0, },
+};
+
+struct usbd_otg_descriptor msc_otg_descriptor = {
+        bLength : sizeof(struct usbd_otg_descriptor),
+        bDescriptorType: USB_DT_OTG,
+        bmAttributes: 0,
+};
+
+struct usbd_device_description msc_device_description = {
+        device_descriptor: &msc_device_descriptor,
+#ifdef CONFIG_OTG_HIGH_SPEED
+        device_qualifier_descriptor: &msc_device_qualifier_descriptor,
+#endif /* CONFIG_OTG_HIGH_SPEED */
+        otg_descriptor: &msc_otg_descriptor,
+        iManufacturer: CONFIG_OTG_MSC_MANUFACTURER,
+        iProduct: CONFIG_OTG_MSC_PRODUCT_NAME,
+#if !defined(CONFIG_OTG_NO_SERIAL_NUMBER) && defined(CONFIG_OTG_SERIAL_NUMBER_STR)
+        iSerialNumber: CONFIG_OTG_SERIAL_NUMBER_STR, 
+#endif
+};
+
+
+#define DEVICE_EJECTED          0x0001          // MEDIA_EJECTED
+#define DEVICE_INSERTED         0x0002          // MEDIA_INSERT
+
+#define DEVICE_OPEN             0x0004          // WR_PROTECT_OFF
+#define DEVICE_WRITE_PROTECTED  0x0008          // WR_PROTECT_ON
+
+#define DEVICE_CHANGE_ON        0x0010          // MEDIA_CHANGE_ON
+
+#define DEVICE_PREVENT_REMOVAL  0x0020
+
+
+#define DEF_NUMBER_OF_HEADS     0x10
+#define DEF_SECTORS_PER_TRACK   0x20
+
+
+
+/* MSC ******************************************************************************************** */
+
+static struct msc_private msc_private;
+int msc_interrupts;
+
+int msc_urb_sent (struct usbd_urb *tx_urb, int rc);
+static int msc_recv_urb (struct usbd_urb *urb, int rc);
+
+
+/* Sense Key *********************************************************************************** */
+
+void set_sense_data(struct msc_private *msc, u32 sensedata, u32 info)
+{
+        TRACE_SENSE(sensedata, info);
+        msc->sensedata = sensedata;
+        msc->info = info;
+}
+
+
+static int msc_reset(void)
+{
+        struct msc_private *msc = &msc_private;
+        msc->device_state = MSC_DEVICE_DN;
+        msc->command_state = MSC_READY;
+        msc->io_state = MSC_INACTIVE;
+        msc->block_dev_state = 0;
+        msc->sensedata = 0;
+        msc->info = 0;
+        return 0;
+}
+
+
+/* Generic start recv urb and send csw ********************************************************* */
+
+/* msc_start_recv - queue a receive urb
+ *
+ * Returns non-zero if there is an error in the USB layer.
+ */
+int msc_start_recv_urb(struct usbd_function_instance *function, struct msc_private *msc, int size)
+{
+        struct usbd_urb *rcv_urb = NULL;
+        int wMaxPacketSize;
+
+        TRACE_MSG1(MSC, "START RECV URB size: %d", size); 
+
+        // ensure that we are a multiple of the endpoint packetsize
+        wMaxPacketSize = usbd_endpoint_wMaxPacketSize(function, BULK_OUT, usbd_high_speed(function));
+        if ((size % wMaxPacketSize)) {
+                size += wMaxPacketSize;
+                size = (size / wMaxPacketSize) * wMaxPacketSize;
+        }
+
+        // allocate urb and queue it
+        THROW_IF(!(rcv_urb = usbd_alloc_urb (function, BULK_OUT, size, msc_recv_urb)), error);
+        TRACE_MSG1(MSC, "START RECV urb: %p", (int)rcv_urb);
+
+        rcv_urb->function_privdata = msc;
+        msc->rcv_urb_finished = NULL;
+        THROW_IF(usbd_start_out_urb(rcv_urb), error);
+        return 0;
+
+        CATCH(error) {
+                TRACE_MSG0(MSC,"START RECV URB ERROR"); 
+                if (rcv_urb)
+                        usbd_free_urb(rcv_urb);
+                return -EINVAL;
+        }
+}
+
+/* msc_start_sending - start sending a new data or csw urb
+ *
+ * Generate a CSW (Command Status Wrapper) to send to the the host.
+ *
+ * Returns non-zero if there is an error in the USB layer.
+ */
+int msc_start_sending_csw(struct usbd_function_instance *function, struct msc_private *msc, u8 status)
+{
+        COMMAND_STATUS_WRAPPER *csw;
+        int length = sizeof(COMMAND_STATUS_WRAPPER);
+        struct usbd_urb *tx_urb;
+
+        TRACE_MSG1(MSC,"START SENDING CSW %08x", status); 
+
+        msc->command_state = MSC_STATUS;
+
+        THROW_IF(!(tx_urb = usbd_alloc_urb (function, BULK_IN, length, msc_urb_sent)), error);
+        TRACE_MSG1(MSC,"START sending csw urb: %p", (int)tx_urb);
+        tx_urb->actual_length = length;
+        tx_urb->function_privdata = msc;
+
+        // fill in CSW and queue the urb
+        csw = (COMMAND_STATUS_WRAPPER *) tx_urb->buffer;
+        csw->dCSWSignature = CSW_SIGNATURE;
+        csw->dCSWTag = msc->command.dCBWTag;
+        csw->dCSWDataResidue = msc->command.dCBWDataTransferLength - msc->data_transferred_in_bytes;
+        csw->bCSWStatus = status;
+
+        TRACE_MSG1(MSC,"START SENDING CSW data residue: %d", csw->dCSWDataResidue); 
+
+        THROW_IF (usbd_start_in_urb (tx_urb), error);
+
+        CATCH(error) {
+                TRACE_MSG0(MSC,"START SENDING CSW FAILED");
+                if (tx_urb) usbd_free_urb (tx_urb);
+                return -EINVAL;
+        }
+        return 0;
+}
+
+/* msc_start_sending_csw_failed - starting sending a CSW showing failure
+ *
+ * Sets sensedata and generates a CSW with status set to USB_MSC_FAILED.
+ *
+ * Returns non-zero if there is an error in the USB layer.
+ */
+int msc_start_sending_csw_failed(struct msc_private *msc, u32 sensedata, u32 info, int status)
+{
+        set_sense_data(msc, sensedata, info);
+        return msc_start_sending_csw(msc->function, msc, status);
+}
+
+
+/* ********************************************************************************************* */
+
+/* msc_alloc_urb - allocate an urb for returning a query
+ *
+ * Returns NULL if there is an error in the USB layer.
+ */
+struct usbd_urb * msc_alloc_urb(struct msc_private *msc, int length)
+{
+        struct usbd_function_instance *function;
+        struct usbd_urb *urb;
+
+        THROW_IF(!(function = msc->function), error);
+        THROW_IF(!(urb = usbd_alloc_urb (function, BULK_IN, length, msc_urb_sent)), error);
+        return urb;
+
+        CATCH(error) {
+                msc->command_state = MSC_READY;
+                return NULL;
+        }
+}
+
+
+/* msc_dispatch_query_urb - dispatch an urb containing query data
+ *
+ * Returns non-zero if there is an error in the USB layer.
+ */
+int msc_dispatch_query_urb(struct usbd_urb *urb, struct msc_private *msc, int length, int status)
+{
+        int rc;
+
+        TRACE_MSG1(MSC,"DISPATCH URB len: %d", length);
+
+        // save information in msc and urb
+        //
+        urb->function_privdata = msc;
+        urb->actual_length = msc->TransferLength_in_bytes = length;
+
+        // dispatch urb
+        {
+                unsigned long flags;
+                local_irq_save(flags);
+                if ((rc = usbd_start_in_urb (urb))) {
+
+                        TRACE_MSG0(MSC,"DISPATCH URB FAILED");
+                        usbd_free_urb (urb);
+
+                        // stall?
+                        msc->command_state = MSC_READY;
+                        local_irq_restore(flags);
+                        return -EINVAL;
+                }
+                msc->command_state = MSC_QUERY;
+                msc->status = status;
+                local_irq_restore(flags);
+        }
+        return 0;
+}
+
+
+/* msc_dispatch_query_urb_zlp - send a ZLP
+ *
+ * Returns non-zero if there is an error in the USB layer.
+ */
+int msc_dispatch_query_urb_zlp(struct msc_private *msc, u32 sensedata, u32 info, int status)
+{
+        struct usbd_urb *urb;
+        RETURN_EINVAL_IF(!(urb = msc_alloc_urb(msc, 1))); 
+        set_sense_data(msc, sensedata, info);
+        urb->flags |= USBD_URB_SENDZLP;
+        return msc_dispatch_query_urb(urb, msc, 0, status);
+}
+
+
+
+/* Block Device ******************************************************************************** */
+
+/* msc_open_blockdev - open the block device specified in msc->major, msc->minor
+ *
+ * Sets appropriate fields to show current status of block device.
+ *
+ * XXX TODO - this needs to be tested against RO and absent devices.
+ *
+ */
+void msc_open_blockdev(struct msc_private *msc)
+{
+        int rc;
+        msc->block_dev_state = DEVICE_EJECTED;
+
+        TRACE_MSG2(MSC, "OPEN BLOCKDEV: Major: %x Minor: %x", major, minor);
+        //printk(KERN_INFO"%s: major: %d minor: %d\n", __FUNCTION__, major, minor);
+
+        /*
+         * Check device information and verify access to the block device.
+         */
+        THROW_IF (!major, ejected);
+
+        msc->dev = MKDEV(major, minor);
+
+        THROW_IF (!(msc->bdev = bdget(kdev_t_to_nr(msc->dev))), ejected);
+
+        if ((rc = blkdev_get(msc->bdev, FMODE_READ | FMODE_WRITE, 0, BDEV_RAW))) {
+
+                TRACE_MSG0(MSC,"OPEN BLOCKDEV: cannot open RW");
+                //printk(KERN_INFO"%s: cannot open RW\n", __FUNCTION__);
+                THROW_IF ((rc = blkdev_get(msc->bdev, FMODE_READ, 0, BDEV_RAW)), ejected);
+                msc->block_dev_state |= DEVICE_WRITE_PROTECTED;
+        }
+
+        msc->block_dev_state &= ~DEVICE_EJECTED;
+        msc->block_dev_state |= DEVICE_INSERTED;
+
+        TRACE_MSG1(MSC,"OPEN BLOCKDEV: opened block_dev_state: %x", msc->block_dev_state);
+
+        /*
+         * Note that capacity must return the LBA of the last addressable block
+         * c.f. RBC 4.4, RBC 5.3 and notes below RBC Table 6
+         *
+         * The blk_size array contains the number of addressable blocks or N, 
+         * capacity is therefore N-1.
+         */
+         
+        msc->capacity = (blk_size[MAJOR(msc_private.dev)][MINOR(msc_private.dev)] << 1) - 1;
+        msc->block_size = get_hardsect_size(msc->dev);
+        msc->max_blocks = PAGE_SIZE / msc->block_size;
+
+        TRACE_MSG1(MSC,"blk_size: %x", blk_size[MAJOR(msc_private.dev)][MINOR(msc_private.dev)] << 1); 
+        TRACE_MSG1(MSC,"blk_size: %d", blk_size[MAJOR(msc_private.dev)][MINOR(msc_private.dev)] << 1); 
+
+        TRACE_MSG1(MSC,"capacity: %x", msc->capacity); 
+        TRACE_MSG1(MSC,"capacity: %d", msc->capacity); 
+
+        TRACE_MSG1(MSC,"block_size: %x", msc->block_size); 
+        TRACE_MSG1(MSC,"block_size: %d", msc->block_size); 
+
+        TRACE_MSG1(MSC,"max_blocks: %d", msc->max_blocks); 
+
+        printk(KERN_INFO"%s: finis\n", __FUNCTION__);
+
+        CATCH(ejected) {
+                TRACE_MSG1(MSC,"OPEN BLOCKDEV: EJECTED block_dev_state: %x", msc->block_dev_state);
+                printk(KERN_INFO"%s: cannot open R)\n", __FUNCTION__);
+                printk(KERN_INFO"%s: Cannot get device %d %d\n", __FUNCTION__, major, minor); 
+        }
+}
+
+/* msc_check_blockdev - check current status of the block device
+ *
+ * Check if the block device is operational, generate a failed CSW if not.
+ *
+ * Returns non-zero if the block device is not available for I/O operations
+ * and a failed CSW has already been sent.
+ */
+int msc_check_blockdev(struct msc_private *msc, int zlp)
+{
+        if (msc->block_dev_state & DEVICE_EJECTED) {
+                TRACE_MSG0(MSC,"CHECK BLOCKDEV DEVICE_EJECTED");
+
+                (zlp ? msc_dispatch_query_urb_zlp : msc_start_sending_csw_failed) 
+                        (msc, SCSI_SENSEKEY_MEDIUM_NOT_PRESENT, msc->lba, USB_MSC_FAILED);
+
+                return -EINVAL;
+        }
+        if (msc->block_dev_state & DEVICE_CHANGE_ON) {
+                TRACE_MSG0(MSC,"CHECK BLOCKDEV DEVICE_CHANGE_ON");
+                (zlp ? msc_dispatch_query_urb_zlp : msc_start_sending_csw_failed) 
+                        (msc, SCSI_SENSEKEY_NOT_READY_TO_CHANGE, msc->lba, USB_MSC_FAILED);
+                return -EINVAL;
+        }
+        return 0;
+}
+
+
+/* READ 10 COMMAND - read and send data to the host ******************************************** */
+
+int msc_start_reading_block_data(struct usbd_function_instance *function, struct msc_private *msc);
+void msc_block_read_finished(struct buffer_head *bh, int flag);
+
+
+/* msc_scsi_read_10 - process a read(10) command
+ *
+ * We have received a READ(10) CBW, if transfer length is non-zero
+ * initiate a generic block i/o otherwise send a CSW.
+ *
+ * Returns non-zero if there is an error in the USB layer.
+ */
+int msc_scsi_read_10(struct msc_private *msc, char *name, int op)
+{
+        SCSI_READ_10_COMMAND *command = (SCSI_READ_10_COMMAND *)&msc->command.CBWCB;
+
+        RETURN_ZERO_IF (msc_check_blockdev(msc, 1));
+
+        /*
+         * save the CBW information and setup for transmitting data read from the block device
+         */
+        msc->lba = be32_to_cpu(command->LogicalBlockAddress);
+        msc->TransferLength_in_blocks = be16_to_cpu(command->TransferLength);
+        msc->TransferLength_in_bytes = msc->TransferLength_in_blocks * msc->block_size;
+        msc->command_state = MSC_DATA_IN_READ;
+        msc->io_state = MSC_INACTIVE;
+
+        /*
+         * Start reading blocks to send or simply send the CSW if the host
+         * didn't actually ask for a non-zero length.
+         */
+        return (msc->TransferLength_in_blocks) ?  msc_start_reading_block_data(msc->function, msc) :
+                msc_start_sending_csw(msc->function, msc, USB_MSC_PASSED);
+}
+
+
+/* msc_start_reading_block_data - start reading data
+ *
+ * Generate a generic block io request to read some data to transmit.
+ *
+ * This function is initially called by msc_scsi_read_10() but can also be called
+ * by msc_urb_sent() if the amount of data requested was too large.
+ *
+ * The function msc_block_read_finished() will be called to actually send the data
+ * to the host when the I/O request is complete.
+ *
+ */
+int msc_start_reading_block_data(struct usbd_function_instance *function, struct msc_private *msc)
+{
+        int TransferLength_in_blocks = MIN(msc->max_blocks, msc->TransferLength_in_blocks);
+        
+        TRACE_MSG1(MSC,"START READING BLOCK DATA lba:    %x", msc->lba); 
+        TRACE_MSG1(MSC,"START READING BLOCK DATA blocks: %d", msc->TransferLength_in_blocks); 
+        TRACE_MSG1(MSC,"START READING BLOCK DATA blocks: %d", TransferLength_in_blocks); 
+
+        if (msc->block_dev_state != DEVICE_INSERTED) {
+                TRACE_MSG0(MSC,"START READ MEDIUM NOT PRESENT");
+                return msc_start_sending_csw_failed (msc, SCSI_SENSEKEY_MEDIUM_NOT_PRESENT, msc->lba, USB_MSC_FAILED);
+        }
+
+        if (msc->lba >= msc->capacity) {
+                TRACE_MSG2(MSC, "START READ LBA out of range: lba: %d capacity: %d", msc->lba, msc->capacity);
+                return msc_start_sending_csw_failed (msc, SCSI_SENSEKEY_LOGICAL_BLOCK_OUT_OF_RANGE, msc->lba, USB_MSC_FAILED);
+        }
+
+        // setup buffer head
+        msc->read_bh.b_size = TransferLength_in_blocks * msc->block_size;
+        msc->read_bh.b_end_io = msc_block_read_finished;
+        msc->read_bh.b_rsector = msc->lba;
+        msc->read_bh.b_state = (1UL << BH_Mapped) | (1UL << BH_Lock);
+        msc->io_state |= MSC_BLOCKIO_PENDING;
+        msc->read_bh.b_data = page_address(msc->read_bh.b_page);
+        memset(msc->read_bh.b_data, 0x0, msc->read_bh.b_size);
+
+        generic_make_request(READ, &msc->read_bh);
+        generic_unplug_device(blk_get_queue(msc->read_bh.b_rdev));
+
+        return 0;
+}
+
+
+/* msc_block_read_finished - called by generic request 
+ *
+ * Called when block i/o read is complete, send the data to the host if possible.
+ *
+ * The function msc_urb_sent() will be called when the data is sent to
+ * either send additional data or the CSW as appropriate.
+ *
+ */
+void msc_block_read_finished(struct buffer_head *bh, int uptodate)
+{
+        struct msc_private *msc = bh->b_private;
+        int TransferLength_in_blocks = bh->b_size / msc->block_size;
+        struct usbd_function_instance *function;
+        struct usbd_urb *tx_urb;
+        int rc;
+        u32 crc;
+        int i;
+
+        msc_interrupts++;
+        TRACE_MSG1(MSC,"BLOCK READ FINISHED size: %x", bh->b_size); 
+
+        /*
+         * Race condition here, if we have not finished sending the
+         * previous tx_urb then we want to simply exit and let
+         * msc_in_read_10_urb_sent() call us again.
+         *
+         * Ensure that we do not reset BLOCKIO flags if SEND PENDING and
+         * that we do reset BLOCKIO if not SEND PENDING
+         */
+        {
+                unsigned long flags;
+                local_irq_save(flags);
+                if (msc->io_state & MSC_SEND_PENDING) {
+                        TRACE_MSG0(MSC,"BLOCK READ SEND PENDING");
+                        msc->io_state |= MSC_BLOCKIO_FINISHED;
+                        msc->uptodate = uptodate;
+                        local_irq_restore(flags);
+                        return;
+                }
+                msc->io_state &= ~(MSC_BLOCKIO_PENDING | MSC_BLOCKIO_FINISHED);
+                local_irq_restore(flags);
+        }
+
+        // verify that the I/O completed
+        //
+        if (1 != uptodate) {
+                TRACE_MSG0(MSC,"BLOCK READ FAILED"); 
+                msc_start_sending_csw_failed (msc, SCSI_SENSEKEY_UNRECOVERED_READ_ERROR, msc->lba, USB_MSC_FAILED);
+                return;
+        }
+
+        // debug output trace
+        //
+        for (i = 0; i < bh->b_size / msc->block_size; i++) {
+                crc = crc32_compute(bh->b_data + (i * msc->block_size), msc->block_size, CRC32_INIT);
+                TRACE_RLBA(bh->b_rsector + i, crc);
+        }
+
+        // allocate a tx_urb and copy into it
+        //
+        THROW_IF(!(function = msc->function), error);
+        THROW_IF(!(tx_urb = usbd_alloc_urb (function, BULK_IN, bh->b_size, msc_urb_sent)), error);
+        TRACE_MSG1(MSC,"BLOCK READ FINISHED urb: %p", (int)tx_urb);
+
+        tx_urb->function_privdata = msc;
+        tx_urb->actual_length = tx_urb->buffer_length = bh->b_size;
+        memcpy(tx_urb->buffer, bh->b_data, bh->b_size);
+
+        msc->read_bh.b_data = NULL;
+        {
+                unsigned long flags;
+                local_irq_save(flags);
+
+                msc->io_state |= MSC_SEND_PENDING;
+                msc->TransferLength_in_bytes -= bh->b_size;
+                msc->TransferLength_in_blocks -= TransferLength_in_blocks; 
+                msc->lba += TransferLength_in_blocks;
+
+                if (!msc->TransferLength_in_blocks) {
+                        TRACE_MSG0(MSC,"BLOCK READ FINISHED - IO FINISHED");
+                        // set flag so that CSW can be sent
+                        msc->io_state |= MSC_DATA_IN_READ_FINISHED;
+                }
+                local_irq_restore(flags);
+        }
+
+        // dispatch urb
+        if ((rc = usbd_start_in_urb (tx_urb))) {
+                TRACE_MSG0(MSC,"BLOCK READ FINISHED FAILED"); 
+                usbd_free_urb (tx_urb);
+                THROW(error);
+        }
+        if (!(msc->io_state & MSC_DATA_IN_READ_FINISHED)) {
+                TRACE_MSG0(MSC,"BLOCK READ SEND RESTARTING");
+                msc_start_reading_block_data(msc->function, msc);
+                return;
+        }
+
+        CATCH(error) {
+                TRACE_MSG0(MSC,"BLOCK READ FINISHED ERROR"); 
+                // stall?
+        }
+}
+
+/* msc_in_read_10_urb_sent - called by msc_urb_sent when state is MSC_DATA_IN_READ
+ *
+ * This will process a read_10 urb that has been sent. Specifically if any previous
+ * read_10 block I/O has finished we recall the msc_block_read_finished() function
+ * to transmit another read_10 urb. 
+ *
+ * If there is no other pending read_10 to do we create and send a CSW.
+ *
+ * If there is more I/O to do or there is already an outstanding I/O we simply
+ * return after freeing the URB.
+ *
+ * Return non-zero if urb was not disposed of.
+ */
+int msc_in_read_10_urb_sent(struct usbd_urb *tx_urb, struct msc_private *msc)
+{
+        unsigned long flags;
+
+        TRACE_MSG0(MSC,"URB SENT DATA IN");
+
+        /*
+         * Potential race condition here, we may need to restart blockio.
+         */
+
+        local_irq_save(flags);
+
+        msc->io_state &= ~MSC_SEND_PENDING;
+        msc->data_transferred_in_bytes += tx_urb->actual_length;
+
+        TRACE_MSG1(MSC,"URB SENT DATA IN data transferred: %d", msc->data_transferred_in_bytes);
+
+        if (!tx_urb->actual_length) 
+                msc->TransferLength_in_blocks = 0;
+
+        usbd_free_urb (tx_urb);
+
+        /*
+         * Check to see if we need to send CSW.
+         */
+        if (MSC_DATA_IN_READ_FINISHED & msc->io_state) {
+                TRACE_MSG0(MSC,"URB SENT DATA IN - FINISHED SENDING CSW");
+                local_irq_restore(flags);
+                return msc_start_sending_csw(msc->function, msc, USB_MSC_PASSED);
+        }
+        /*
+         * Check to see if there is a block read needs to be finished.
+         */
+        if (MSC_BLOCKIO_FINISHED & msc->io_state) {
+                TRACE_MSG0(MSC,"URB SENT DATA IN - RESTART");
+                local_irq_restore(flags);
+                // XXX uptodate?
+                msc_block_read_finished(&msc->read_bh, msc->uptodate);
+                return 0;
+        }
+        local_irq_restore(flags);
+        return 0;
+}
+
+
+/* WRITE 10 - receive data from host and write to block device ********************************* */
+
+int msc_start_receiving_data(struct usbd_function_instance *function, struct msc_private *msc);
+void msc_data_written(struct buffer_head *bh, int flag);
+void msc_data_test(struct buffer_head *bh, int flag);
+
+/* msc_scsi_write_10 - process a write command
+ *
+ * Call either msc_start_receiving_data() or msc_start_sending_csw() as appropriate.
+ * Normally msc_start_receiving_data() is called and it will use msc_start_recv_urb() 
+ * to setup a receive urb of the appropriate size. 
+ *
+ * Returns non-zero if there is an error in the USB layer.
+ */
+int msc_scsi_write_10(struct msc_private *msc, char *name, int op)
+{
+        SCSI_WRITE_10_COMMAND *command = (SCSI_WRITE_10_COMMAND *)&msc->command.CBWCB;
+
+        RETURN_ZERO_IF (msc_check_blockdev(msc, 0));
+
+        // save the CBW and setup for receiving data to be written to the block device
+        //
+        msc->lba = be32_to_cpu(command->LogicalBlockAddress);
+        msc->TransferLength_in_blocks = be16_to_cpu(command->TransferLength);
+        msc->TransferLength_in_bytes = msc->TransferLength_in_blocks * msc->block_size;
+
+        msc->command_state = MSC_DATA_OUT_WRITE;
+        msc->io_state = MSC_INACTIVE;
+
+        TRACE_MSG1(MSC,"RECV WRITE lba: %x", msc->lba); 
+        TRACE_MSG1(MSC,"RECV WRITE blocks: %d", msc->TransferLength_in_blocks); 
+
+        return (msc->TransferLength_in_blocks) ?
+                msc_start_receiving_data(msc->function, msc) :
+                msc_start_sending_csw(msc->function, msc, USB_MSC_PASSED);
+}
+
+
+/* msc_start_receiving_data - called to initiate an urb to receive WRITE(10) data
+ *
+ * Initiate a receive urb to receive upto PAGE_SIZE WRITE(10) data. The
+ * msc_recv_urb() function will call msc_recv_out_blocks() to actually
+ * write the data. 
+ *
+ * This is called from msc_scsi_write_10() to initiate the WRITE(10) and
+ * called from msc_data_written() to start the next page sized receive
+ * urb.
+ *
+ */
+int msc_start_receiving_data(struct usbd_function_instance *function, struct msc_private *msc)
+{
+        /*
+         * Calculating the length is most of the work we do :-)
+         */
+        int TransferLength_in_blocks = MIN(msc->max_blocks, msc->TransferLength_in_blocks);
+
+        TRACE_MSG1(MSC,"START RECEIVING DATA lba: %x", msc->lba); 
+        TRACE_MSG1(MSC,"START RECEIVING DATA blocks: %d", msc->TransferLength_in_blocks);
+        TRACE_MSG1(MSC,"START RECEIVING DATA blocks: %d", TransferLength_in_blocks);
+        TRACE_MSG1(MSC,"START RECEIVING DATA bytes: %d", TransferLength_in_blocks * msc->block_size);
+
+        THROW_IF(msc->command_state != MSC_DATA_OUT_WRITE, error);
+        THROW_IF(!msc->TransferLength_in_blocks, error);
+
+        msc->io_state |= MSC_RECV_PENDING;
+
+        return msc_start_recv_urb(function, msc, TransferLength_in_blocks * msc->block_size);
+
+        CATCH(error) {
+                TRACE_MSG0(MSC,"START RECEIVING DATA ERROR");
+                return -EINVAL;
+        }
+}
+
+
+/* msc_recv_out_blocks - process received WRITE(10) data by writing to block device
+ *
+ * We get here indirectly from msc_recv_urb() when state is MSC_DATA_OUT_WRITE.
+ *
+ * Dealloc the urb and call Initiate the generic request to write the
+ * received data. The b_end_io function msc_data_written() will send the
+ * CSW.
+ *
+ */
+void msc_recv_out_blocks(struct usbd_urb *rcv_urb, struct msc_private *msc)
+{
+        int TransferLength_in_bytes = rcv_urb->actual_length;
+        int TransferLength_in_blocks = TransferLength_in_bytes / msc->block_size;
+        u32 crc;
+        int i;
+
+        TRACE_MSG1(MSC,"RECV OUT            bytes: %d", TransferLength_in_bytes);
+        TRACE_MSG1(MSC,"RECV OUT          iostate: %x", msc->io_state);
+        TRACE_MSG1(MSC,"RECV OUT data transferred: %d", msc->data_transferred_in_bytes);
+
+        /*
+         * Race condition here, we may get to here before the previous block
+         * write completed. If so just exit and the msc_data_written()
+         * function will recall us.
+         */
+        {
+                unsigned long flags;
+                local_irq_save(flags);
+                if (msc->io_state & MSC_BLOCKIO_PENDING) {
+                        TRACE_MSG0(MSC,"RECV OUT  BLOCKIO PENDING");
+                        msc->io_state |= MSC_RECV_FINISHED;
+                        msc->rcv_urb_finished = rcv_urb;
+                        local_irq_restore(flags);
+                        return;
+                }
+                msc->io_state &= ~(MSC_RECV_PENDING |MSC_RECV_FINISHED);
+                local_irq_restore(flags);
+        }
+
+        msc->data_transferred_in_bytes += rcv_urb->actual_length;
+
+        for (i = 0; i < TransferLength_in_blocks; i++) {
+                crc = crc32_compute(rcv_urb->buffer + (i * msc->block_size), msc->block_size, CRC32_INIT);
+                TRACE_SLBA(msc->lba + i, crc);
+        }
+        
+        msc->write_bh.b_data = page_address(msc->write_bh.b_page);
+        memcpy(msc->write_bh.b_data, rcv_urb->buffer, rcv_urb->actual_length);
+
+        usbd_free_urb(rcv_urb);
+
+        if (msc->block_dev_state != DEVICE_INSERTED) {
+                TRACE_MSG0(MSC,"START READ MEDIUM NOT PRESENT");
+                msc_start_sending_csw_failed (msc, SCSI_SENSEKEY_MEDIUM_NOT_PRESENT, msc->lba, USB_MSC_FAILED);
+                return;
+        }
+
+        if (msc->lba >= msc->capacity) {
+                TRACE_MSG2(MSC, "START READ LBA out of range: lba: %d capacity: %d", msc->lba, msc->capacity);
+                msc_start_sending_csw_failed (msc, SCSI_SENSEKEY_LOGICAL_BLOCK_OUT_OF_RANGE, msc->lba, USB_MSC_FAILED);
+                return;
+        }
+
+        if (!msc->TransferLength_in_blocks) {
+                TRACE_MSG0(MSC,"START READ LBA TransferLength_in_blocks zero:");
+                msc_start_sending_csw_failed (msc, SCSI_SENSEKEY_LOGICAL_BLOCK_OUT_OF_RANGE, msc->lba, USB_MSC_FAILED);
+                return;
+        }
+
+        // Initiate writing the data
+
+        TRACE_MSG1(MSC,"RECV OUT              lba: %x", msc->lba); 
+        TRACE_MSG1(MSC,"RECV OUT   blocks    left: %d", msc->TransferLength_in_blocks); 
+        TRACE_MSG1(MSC,"RECV OUT   blocks current: %d", TransferLength_in_blocks); 
+
+
+        // set address in buffer head
+        msc->write_bh.b_size = TransferLength_in_bytes;
+        msc->write_bh.b_rsector = msc->lba;
+
+        // decrement counters and increment address
+        msc->TransferLength_in_bytes -= TransferLength_in_bytes;
+        msc->TransferLength_in_blocks -= TransferLength_in_blocks;
+        msc->lba += TransferLength_in_blocks;
+        msc->write_bh.b_state = (1UL << BH_Mapped) | (1UL << BH_Lock);
+        msc->write_bh.b_end_io = msc_data_written;
+        msc->io_state |= MSC_BLOCKIO_PENDING;
+
+
+        // start new urb here
+        if (!msc->TransferLength_in_blocks) 
+                msc->command_state = MSC_DATA_OUT_WRITE_FINISHED;
+        else 
+                msc_start_receiving_data(msc->function, msc);
+
+        generic_make_request(WRITE, &msc->write_bh);
+        generic_unplug_device(blk_get_queue(msc->write_bh.b_rdev));
+}
+
+
+/* msc_data_written - called by generic request 
+ *
+ * Called when current block i/o read is finished, restart block i/o or send the CSW.
+ *
+ */
+void msc_data_written(struct buffer_head *bh, int uptodate)
+{
+        struct msc_private *msc = bh->b_private;
+        unsigned long flags;
+        u8 io_state;
+
+        msc_interrupts++;
+
+        TRACE_MSG4(MSC,"DATA WRITTEN uptodate: %x b_state: %x state: %x io: %x", 
+                        uptodate, bh->b_state, msc->command_state, msc->io_state); 
+
+        TRACE_MSG1(MSC,"DATA WRITTEN lba: %x", msc->lba); 
+
+        local_irq_save(flags);
+        io_state = msc->io_state &= ~MSC_BLOCKIO_PENDING;
+        msc->write_bh.b_data = NULL;
+        local_irq_restore(flags);
+
+        /*
+         * verify that the I/O completed
+         */
+        if (1 != uptodate) {
+                TRACE_MSG0(MSC,"BLOCK READ FAILED"); 
+                msc_start_sending_csw_failed (msc, SCSI_SENSEKEY_UNRECOVERED_READ_ERROR, msc->lba, USB_MSC_FAILED);
+                // XXX CHECKME
+                if (msc->rcv_urb_finished) {
+                        usbd_free_urb(msc->rcv_urb_finished);
+                        msc->rcv_urb_finished = NULL;
+                }
+                return;
+        }
+        /*
+         * If there was a rcv_urb that was not processed then we need
+         * to process it now. This is done by simply restarting msc_recv_out()
+         */
+        if ((io_state & MSC_RECV_FINISHED) && msc->rcv_urb_finished) {
+                struct usbd_urb *rcv_urb = msc->rcv_urb_finished;
+                msc->rcv_urb_finished = NULL;
+                TRACE_MSG0(MSC,"DATA WRITTEN RECV FINISHED");
+                msc_recv_out_blocks(rcv_urb, msc);
+        }
+        /*
+         * DATA_IN mode and no more data to write
+         */
+        if (MSC_DATA_OUT_WRITE_FINISHED == msc->command_state) {
+                // finished, send CSW
+                TRACE_MSG0(MSC,"DATA WRITTEN send CSW");
+                msc_start_sending_csw(msc->function, msc, USB_MSC_PASSED);
+        }
+}
+
+
+/* SCSI Commands ******************************************************************************* */
+
+/* msc_scsi_inquiry - process an inquiry
+ *
+ * Used by:
+ *      win2k
+ *
+ * Returns non-zero if there is an error in the USB layer.
+ */
+int msc_scsi_inquiry(struct msc_private *msc, char *name, int op) 
+{
+        SCSI_INQUIRY_COMMAND *command = (SCSI_INQUIRY_COMMAND *)&msc->command.CBWCB;
+        SCSI_INQUIRY_DATA *data;
+        struct usbd_urb *urb;
+        int length = sizeof(SCSI_INQUIRY_DATA);
+
+        /*
+         * C.f. SPC2 7.3 INQUIRY command
+         * C.f. Table 46 - Standard INQUIRY data format
+         *
+         * C.f. Table 47 - Peripheral Qualifier
+         *
+         * 000b The specified peripheral device type is currently connected to this
+         *      logical unit.....
+         * 001b The device server is capable of of supporting the peripheral device
+         *      type on this logical unit. However, the physical device is not currently 
+         *      connected to this logical unit.....
+         * 010b Reserved
+         * 011b The device server is not capable of supporting a physical device on
+         *      this logical unit....
+         *      
+         */
+
+        TRACE_MSG4(MSC,"INQUIRY EnableVPD: %02x LogicalUnitNumber: %02x PageCode: %02x AllocLen: %02x",
+                        command->EnableVPD, command->LogicalUnitNumber, command->PageCode, command->AllocationLength);
+
+        // XXX THROW_IF(msc->command_state != MSC_READY, error);
+
+        RETURN_EINVAL_IF(!(urb = msc_alloc_urb(msc, length)));
+
+        data = (SCSI_INQUIRY_DATA *)urb->buffer;
+        data->PeripheralQaulifier = msc->block_dev_state & (DEVICE_EJECTED | DEVICE_CHANGE_ON) ? 0x1 : 0;
+        data->PeripheralDeviceType = 0x00;
+        data->RMB = 0x1;
+        data->ResponseDataFormat = 0x1;
+        data->AdditionalLength = 0x1f;
+        
+        strncpy(data->VendorInformation, CONFIG_OTG_MSC_MANUFACTURER, strlen(CONFIG_OTG_MSC_MANUFACTURER));
+        strncpy(data->ProductIdentification, CONFIG_OTG_MSC_PRODUCT_NAME, strlen(CONFIG_OTG_MSC_PRODUCT_NAME));
+
+        return msc_dispatch_query_urb(urb, msc, length, USB_MSC_PASSED);
+}
+
+
+/* old_msc_scsi_read_format_capacity - process a query
+ *
+ * Used by:
+ *      win2k
+ *
+ * Returns non-zero if there is an error in the USB layer.
+ */
+int old_msc_scsi_read_format_capacity(struct msc_private *msc, char *name, int op) 
+{
+        // send ZLP then CSW
+        return msc_dispatch_query_urb_zlp(msc, 0, 0, USB_MSC_PASSED);
+}
+
+/* msc_scsi_read_format_capacity - process a query
+ *
+ * Used by:
+ *      win2k
+ *
+ * Returns non-zero if there is an error in the USB layer.
+ */
+int msc_scsi_read_format_capacity(struct msc_private *msc, char *name, int op) 
+{
+        SCSI_READ_FORMAT_CAPACITY_DATA *data;
+        struct usbd_urb *urb;
+        int length = sizeof(SCSI_READ_FORMAT_CAPACITY_DATA);
+        u32 block_num = msc->capacity;
+        u32 block_len;
+
+
+
+        /*
+         * If we don't have a valid block device then simply
+         * send a ZLP.
+         *
+         * XXX VERIFY this is correct thing to do for this situation.
+         */
+        //if (msc->block_dev_state & (DEVICE_EJECTED | DEVICE_CHANGE_ON)) 
+        //        return msc_dispatch_query_urb_zlp(msc);
+        RETURN_ZERO_IF (msc_check_blockdev(msc, 1));
+
+        RETURN_EINVAL_IF(!(urb = msc_alloc_urb(msc, length))); 
+
+        data = (SCSI_READ_FORMAT_CAPACITY_DATA *) urb->buffer;
+
+        data->CapacityListHeader.CapacityListLength = sizeof(data->CurrentMaximumCapacityDescriptor);
+
+        data->CurrentMaximumCapacityDescriptor.NumberofBlocks = block_num;
+        data->CurrentMaximumCapacityDescriptor.DescriptorCode = 0x03;
+        memcpy(data->CurrentMaximumCapacityDescriptor.BlockLength + 1, &block_len, sizeof(block_len));
+
+        return msc_dispatch_query_urb(urb, msc, length, USB_MSC_PASSED);
+}
+
+/* msc_read_capacity - process a read_capacity command
+ *
+ * Used by:
+ *      win2k
+ *
+ * Returns non-zero if there is an error in the USB layer.
+ */
+int msc_scsi_read_capacity(struct msc_private *msc, char *name, int op) 
+{
+        SCSI_READ_CAPACITY_COMMAND *command = (SCSI_READ_CAPACITY_COMMAND *)&msc->command.CBWCB;
+        SCSI_READ_CAPACITY_DATA *data;
+        struct usbd_urb *urb;
+        int length = 8;
+        u32 lba;
+
+        /*
+         * C.f. RBC 5.3
+         */
+
+        /*
+         * If we don't have a valid block device then simply
+         * send a ZLP.
+         *
+         * XXX VERIFY this is correct thing to do for this situation.
+         */
+        //if (msc->block_dev_state & (DEVICE_EJECTED | DEVICE_CHANGE_ON)) 
+        //        return msc_dispatch_query_urb_zlp(msc);
+
+        RETURN_ZERO_IF (msc_check_blockdev(msc, 1));
+
+        //memcpy(&lba, &command->LogicalBlockAddress, 4);
+        //lba = be32_to_cpu(lba);
+
+        lba = be32_to_cpu(command->LogicalBlockAddress);
+
+        TRACE_MSG1(MSC,"READ CAPACITY LBA: %d", lba);
+
+        if ((command->PMI > 1) || (!command->PMI && lba)) {
+                TRACE_MSG1(MSC,"READ CAPACITY PMI: %d", command->PMI);
+                return msc_start_sending_csw_failed (msc, SCSI_SENSEKEY_INVALID_FIELD_IN_CDB, lba, USB_MSC_FAILED);
+        }
+
+        // alloc urb
+        RETURN_EINVAL_IF(!(urb = msc_alloc_urb(msc, length)));
+
+        data = (SCSI_READ_CAPACITY_DATA *) urb->buffer;
+
+        data->LastLogicalBlockAddress = cpu_to_be32(msc->capacity);
+        data->BlockLengthInBytes = cpu_to_be32(msc->block_size);
+
+        TRACE_MSG1(MSC,"RECV READ CAPACITY lba: %x", be32_to_cpu(data->LastLogicalBlockAddress));
+        TRACE_MSG1(MSC,"RECV READ CAPACITY block_size: %x", be32_to_cpu(data->BlockLengthInBytes));
+
+        return msc_dispatch_query_urb(urb, msc, length, USB_MSC_PASSED);
+}
+
+
+/* msc_scsi_request_sense - process a request_sense command
+ *
+ * Returns non-zero if there is an error in the USB layer.
+ */
+int msc_scsi_request_sense(struct msc_private *msc, char *name, int op)
+{
+        SCSI_REQUEST_SENSE_COMMAND*    command = (SCSI_REQUEST_SENSE_COMMAND *)&msc->command.CBWCB;
+        SCSI_REQUEST_SENSE_DATA *data;
+
+        /*
+         * C.f. SPC2 7.20 REQUEST SENSE command
+         */
+
+        struct usbd_urb *urb;
+        int length = sizeof(SCSI_REQUEST_SENSE_DATA);
+
+        // alloc urb
+        RETURN_EINVAL_IF(!(urb = msc_alloc_urb(msc, length)));
+
+        data = (SCSI_REQUEST_SENSE_DATA *) urb->buffer;
+        memset(command, 0x0, length);
+        data->ErrorCode = SCSI_ERROR_CURRENT;
+        data->SenseKey = msc->sensedata >> 16;
+        data->AdditionalSenseCode = msc->sensedata >> 8;
+        data->AdditionalSenseCodeQualifier = msc->sensedata;
+        data->Valid = 1;
+
+        set_sense_data(msc, SCSI_SENSEKEY_NO_SENSE, 0);
+
+        return msc_dispatch_query_urb(urb, msc, length, USB_MSC_PASSED);
+}
+
+/* msc_scsi_mode_sense - process a request_sense command
+ *
+ * Used by:
+ *      win2k
+ *
+ * Returns non-zero if there is an error in the USB layer.
+ */
+int old_msc_scsi_mode_sense(struct msc_private *msc, char *name, int op)
+{
+        SCSI_MODE_SENSE_COMMAND *command = (SCSI_MODE_SENSE_COMMAND *)&msc->command.CBWCB;
+        SCSI_MODE_SENSE_DATA *data;
+        int length = sizeof(SCSI_MODE_SENSE_DATA);
+
+        struct usbd_urb *urb;
+        u8 *cp;
+
+
+        TRACE_MSG4(MSC,"MODE SENSE dbd: %02x PageControl: %02x PageCode: %02x Alloc: %02x", 
+                        command->DBD, 
+                        command->PageControl, 
+                        command->PageCode, 
+                        0);
+
+        length = 8;
+
+        // alloc urb
+        RETURN_EINVAL_IF(!(urb = msc_alloc_urb(msc, length)));
+
+        cp = (u8 *) urb->buffer;
+        memset(cp, 0x0, length);
+
+        cp[0] = 0;
+        cp[1] = 0;
+        cp[2] = 0;              // 0x80 is writeprotect
+        cp[3] = 0x08;
+        cp[4] = 0;
+        cp[5] = 0;
+        cp[6] = 0;
+        cp[7] = 0;
+
+        return msc_dispatch_query_urb(urb, msc, length, USB_MSC_PASSED);
+}
+
+/* msc_scsi_mode_sense - process a request_sense command
+ *
+ * Used by:
+ *      win2k
+ *
+ * Returns non-zero if there is an error in the USB layer.
+ */
+int msc_scsi_mode_sense(struct msc_private *msc, char *name, int op)
+{
+
+        /*
+         * C.f. SPC2 7.8.1 MODE SENSE(6) command
+         */
+
+        static READ_WRITE_ERROR_RECOVERY_PAGE           page_01 = {
+                PageCode:0x01,
+                PageLength:0x0A,
+                ReadRetryCount:0x03,
+                WriteRetryCount:0x80,
+        };
+        static FLEXIBLE_DISK_PAGE                       page_05 = {
+                PageCode:0x05,
+                PageLength:0x1E,
+                TransferRate:__constant_cpu_to_be16(0xFA00),
+                NumberofHeads:0xA0,
+                SectorsperTrack:0x00,
+                DataBytesperSector:__constant_cpu_to_be16(0x0002),
+                NumberofCylinders:__constant_cpu_to_be16(0x0000),
+                MotorOnDelay:0x05,
+                MotorOffDelay:0x1E,
+                MediumRotationRate:__constant_cpu_to_be16(0x6801),
+        };
+        static REMOVABLE_BLOCK_ACCESS_CAPABILITIES_PAGE page_1b = {
+                PageCode:0x1B,
+                PageLength:0x0A,
+                TLUN:0x01,
+        };
+        static TIMER_AND_PROTECT_PAGE                   page_1c = {
+                PageCode:0x1c,
+                PageLength:0x06,
+                InactivityTimeMultiplier:0x0A,
+        };
+
+        SCSI_MODE_SENSE_COMMAND *command = (SCSI_MODE_SENSE_COMMAND *)&msc->command.CBWCB;
+        SCSI_MODE_SENSE_DATA *data;
+        struct usbd_urb *urb;
+        int length = sizeof(SCSI_MODE_SENSE_DATA);
+
+        TRACE_MSG4(MSC,"MODE SENSE dbd: %02x PageControl: %02x PageCode: %02x Alloc: %02x", 
+                        command->DBD, 
+                        command->PageControl, 
+                        command->PageCode, 
+                        0);
+
+
+        if (msc->block_dev_state & DEVICE_EJECTED) {
+                u16 sector   = htons((unsigned short)msc->block_size);
+                u16 cylinder = 0;
+                page_05.NumberofHeads = 0;
+                page_05.SectorsperTrack = 0;
+                memcpy(&page_05.DataBytesperSector, &sector, sizeof(sector));
+                memcpy(&page_05.NumberofCylinders, &cylinder, sizeof(cylinder));
+        }
+        else {
+                u16 sector   = htons((unsigned short)msc->block_size);
+                u32 size = DEF_NUMBER_OF_HEADS * DEF_SECTORS_PER_TRACK * sector;
+                u16 cylinder = htons(msc->capacity / size);
+                page_05.NumberofHeads = DEF_NUMBER_OF_HEADS;
+                page_05.SectorsperTrack = DEF_SECTORS_PER_TRACK;
+                memcpy(&page_05.DataBytesperSector, &sector, sizeof(sector));
+                memcpy(&page_05.NumberofCylinders, &cylinder, sizeof(cylinder));
+
+        }
+
+
+        if (SCSI_MODEPAGE_CONTROL_CURRENT != command->PageControl) {
+                TRACE_MSG2(MSC,"MODE SENSE - requeested other than CONTROL_CURRENT: %d %d", 
+                                command->PageControl, command->PageCode);
+                return msc_start_sending_csw_failed (msc, SCSI_SENSEKEY_INVALID_FIELD_IN_CDB, msc->lba, USB_MSC_FAILED);
+        }
+
+        RETURN_EINVAL_IF(!(urb = msc_alloc_urb(msc, length)));
+        data = (SCSI_MODE_SENSE_DATA *) urb->buffer;
+
+        data->ModeParameterHeader.WriteProtect = msc->block_dev_state & DEVICE_WRITE_PROTECTED ? 1 : 0;
+
+        switch (command->PageCode) {
+        case SCSI_MODEPAGE_ERROR_RECOVERY:
+                length = sizeof(MODE_PARAMETER_HEADER) + sizeof(page_01);
+                data->ModeParameterHeader.ModeDataLength = length - 1;
+                memcpy(&data->ModePages, &page_01, sizeof(page_01));
+                break;
+        case SCSI_MODEPAGE_FLEXIBLE_DISK_PAGE:
+                length = sizeof(MODE_PARAMETER_HEADER) + sizeof(page_05);
+                data->ModeParameterHeader.ModeDataLength = length - 1;
+                memcpy(&data->ModePages, &page_05, sizeof(page_05));
+                break;
+        case SCSI_MODEPAGE_REMOVABLE_BLOCK_ACCESS:
+                length = sizeof(MODE_PARAMETER_HEADER) + sizeof(page_1b);
+                data->ModeParameterHeader.ModeDataLength = length - 1;
+                memcpy(&data->ModePages, &page_1b, sizeof(page_1b));
+                break;
+        case SCSI_MODEPAGE_INFORMATION_EXCEPTIONS:
+                length = sizeof(MODE_PARAMETER_HEADER) + sizeof(page_1c);
+                data->ModeParameterHeader.ModeDataLength = length - 1;
+                memcpy(&data->ModePages, &page_1c, sizeof(page_1c));
+                break;
+        case SCSI_MODEPAGE_ALL_SUPPORTED:
+                length = sizeof(MODE_PARAMETER_HEADER) + sizeof(MODE_ALL_PAGES);
+                data->ModeParameterHeader.ModeDataLength = length - 1;
+                memcpy(&data->ModePages.ModeAllPages.ReadWriteErrorRecoveryPage, &page_01, sizeof(page_01));
+                memcpy(&data->ModePages.ModeAllPages.FlexibleDiskPage, &page_05, sizeof(page_05));
+                memcpy(&data->ModePages.ModeAllPages.RemovableBlockAccessCapabilitiesPage, &page_1b, sizeof(page_1b));
+                memcpy(&data->ModePages.ModeAllPages.TimerAndProtectPage, &page_1c, sizeof(page_1c));
+                break;
+        case SCSI_MODEPAGE_CACHING:
+                // see file_storage.c for an example if we want to support this
+                break;
+        }
+
+        TRACE_MSG2(MSC,"LENGTH: %d %d", msc->command.dCBWDataTransferLength, length);
+
+        /*
+         * XXX verify that length is <= 256 bytes, return CHECK_CONDITION if it is
+         */
+        length = MIN(msc->command.dCBWDataTransferLength, length);
+
+        return msc_dispatch_query_urb(urb, msc, length, USB_MSC_PASSED);
+}
+
+
+/* msc_scsi_test_unit_ready - process a request_sense command
+ *
+ * Used by:
+ *      win2k
+ *
+ * Returns non-zero if there is an error in the USB layer.
+ */
+int msc_scsi_test_unit_ready(struct msc_private *msc, char *name, int op)
+{
+        /*
+         * C.f. SPC2 7.25 TEST UNIT READY command
+         *
+         * Return GOOD, illegal request for bad LUN or NOT READY
+         *
+         */
+        RETURN_EINVAL_IF (msc_check_blockdev(msc, 0));
+        return msc_start_sending_csw(msc->function, msc, USB_MSC_PASSED);
+}
+
+
+/* msc_scsi_prevent_allow - process a request_sense command
+ *
+ * Used by:
+ *      win2k
+ *
+ * Returns non-zero if there is an error in the USB layer.
+ */
+int msc_scsi_prevent_allow(struct msc_private *msc, char *name, int op)
+{
+        SCSI_PREVENT_ALLOW_MEDIUM_REMOVAL_COMMAND* command = (SCSI_PREVENT_ALLOW_MEDIUM_REMOVAL_COMMAND*)&msc->command.CBWCB;
+
+        /*
+         * C.f. SPC2 7.12 Table 78 PREVENT ALLOW MEDIUM REMOVAL Prevent Field
+         *
+         * 00b  Medium removal shall be allowed from both the data transport
+         *      element and the attached medium changer (if any).
+         * 01b  Medium removal shall be prohibited from the data transport
+         *      element but allowed from the attached medium changer (if any).
+         * 10b  Medium removal shall be allowed for the data transport element
+         *      but prohibited for the attached medium changer.
+         * 11b  Medium remval shall be prohibited from both the data transport
+         *      element and the attached medium changer
+         *
+         * Prevention shall terminate after 00b or 10b, after a SYNC CACHE or hard reset.
+         */
+
+        TRACE_MSG1(MSC,"PREVENT: %d", command->Prevent);
+
+        RETURN_EINVAL_IF (msc_check_blockdev(msc, 0));
+
+
+        // XXX TODO 
+        // this is from storageproto.c, shouldn't we implement something?
+        if (command->Prevent) 
+                return msc_start_sending_csw_failed (msc, SCSI_SENSEKEY_INVALID_FIELD, msc->lba, USB_MSC_FAILED);
+
+        return msc_start_sending_csw(msc->function, msc, USB_MSC_PASSED);
+}
+
+
+/* msc_scsi_start_stop - process a request_sense command
+ *
+ * C.f. RBC 5.4 and 5.4.2
+ *
+ * Used by:
+ *      win2k
+ *
+ * Returns non-zero if there is an error in the USB layer.
+ */
+int msc_scsi_start_stop(struct msc_private *msc, char *name, int op)
+{
+        SCSI_START_STOP_COMMAND* command = (SCSI_START_STOP_COMMAND*)&msc->command.CBWCB;
+
+        TRACE_MSG4(MSC,"START STOP: Immed: %d Power: %x LoEj: %d Start: %d", 
+                        command->IMMED, command->PowerConditions, command->LoEj, command->Start);
+
+        /*
+         * C.f. 5.4 
+         *
+         * IMMED - if set return status immediately after command validation, otherwise
+         * return status as soon operation is completed.
+         *
+         * C.f. 5.4.1 Table 8 POWER CONDITIONS
+         *
+         * 0 - M - no change in power condition
+         * 1 - M - place device in active condition
+         * 2 - M - place device in idle condition
+         * 3 - M - place device in Standby condition
+         * 4 -   - reserved
+         * 5 - M - place device in Sleep condition
+         * 6 -   - reserved
+         * 7 - 0 - Device Control
+         *
+         * C.f. 5.4.2 Table 9 START STOP control bit definitions
+         *
+         * Power        Load/Eject      Start   
+         * 1-7          x               x               LoEj and Start Ignored
+         * 0            0               0               Stop the medium
+         * 0            0               1               Make the medium ready
+         * 0            1               0               Unload the medium
+         * 0            1               1               Load the medium
+         *
+         */
+         
+
+        RETURN_EINVAL_IF (msc_check_blockdev(msc, 0));
+        
+        // XXX TODO 
+        // this is from storageproto.c, shouldn't we implement something?
+        
+        if (command->Start && command->LoEj) 
+                return msc_start_sending_csw_failed (msc, SCSI_SENSEKEY_INVALID_FIELD, msc->lba, USB_MSC_FAILED);
+
+        return msc_start_sending_csw(msc->function, msc, USB_MSC_PASSED);
+}
+
+
+/* msc_scsi_verify - process a verify command
+ *
+ * Used by:
+ *      win2k
+ *
+ * Returns non-zero if there is an error in the USB layer.
+ */
+int msc_scsi_verify(struct msc_private *msc, char *name, int op)
+{
+        SCSI_VERIFY_COMMAND *command = (SCSI_VERIFY_COMMAND *)&msc->command.CBWCB; 
+        /*
+         * C.f. RBC 5.7 VERIFY command
+         */
+        // XXX This actually should use the read_10 function and when
+        // finished reading simply send the following
+        return msc_start_sending_csw(msc->function, msc, USB_MSC_PASSED);
+}
+
+
+/* msc_scsi_mode_select - process a select command
+ *
+ * Returns non-zero if there is an error in the USB layer.
+ */
+int msc_scsi_mode_select(struct msc_private *msc, char *name, int op)
+{
+        //SCSI_MODE_SELECT_COMMAND *command = (SCSI_MODE_SELECT_COMMAND *)&msc->command.CBWCB; 
+       
+        /*
+         * C.f. SPC2 7.6 MODE SELECT(6) command
+         */
+
+        // if less than correct amount of data return USB_MSC_PHASE_ERROR - see MV
+        //
+        return msc_start_sending_csw(msc->function, msc, USB_MSC_PASSED);
+}
+
+
+/* msc_cmd_unknown - process an unknown command
+ *
+ *
+ * Returns non-zero if there is an error in the USB layer.
+ */
+int msc_cmd_unknown(struct msc_private *msc, char *name, int op)
+{
+        /*
+        struct usbd_urb *urb;
+        RETURN_EINVAL_IF(!(urb = msc_alloc_urb(msc, 1)));         // XXX +1 ?
+        urb->flags |= USBD_URB_SENDZLP;
+        return msc_dispatch_query_urb(urb, msc, 0);
+        */
+
+        printk(KERN_ERR"%s: NOT IMPLEMENTED %s opcode: %02x\n", __FUNCTION__, name, op);
+
+        // should we send ZLP for unknow commands?
+        // return msc_dispatch_query_urb_zlp(msc);
+
+        // or failed
+        return msc_start_sending_csw_failed (msc, SCSI_SENSEKEY_INVALID_COMMAND, msc->lba, USB_MSC_FAILED);
+}
+
+
+struct rbc_dispatch {
+        u8      op;
+        char    *name;
+        int     (*rbc_command) (struct msc_private *, char *, int op);
+};
+
+/* Command cross reference 
+ *
+ * This is the list of commands observed from each host OS. It is necessarily
+ * incomplete in that we not have reached some condition necessary to have
+ * other commands used.
+ *                                      Win2k   WinXP
+ * SCSI_TEST_UNIT_READY                 yes     yes
+ * SCSI_READ_10                         yes     yes
+ * SCSI_WRITE_10                        yes     yes
+ * SCSI_READ_CAPACITY                   yes     yes
+ * SCSI_VERIFY                          yes
+ * SCSI_INQUIRY                         yes     yes
+ * SCSI_MODE_SENSE                      yes
+ * SCSI_READ_FORMAT_CAPACITY            yes     yes
+ * SCSI_REQUEST_SENSE                   
+ * SCSI_PREVENT_ALLOW_MEDIUM_REMOVAL    
+ * SCSI_START_STOP                      
+ * SCSI_MODE_SELECT                     
+ * SCSI_FORMAT_UNIT                     
+ *
+ */
+
+struct rbc_dispatch rbc_dispatch_table[] = {
+        { SCSI_TEST_UNIT_READY,         "SCSI_TEST_UNIT_READY",         msc_scsi_test_unit_ready },          //
+        { SCSI_READ_10,                 "SCSI_READ_10",                 msc_scsi_read_10 },                  //
+        { SCSI_WRITE_10,                "SCSI_WRITE_10",                msc_scsi_write_10 },                 //
+        { SCSI_READ_CAPACITY,           "SCSI_READ_CAPACITY",           msc_scsi_read_capacity },            //
+        { SCSI_VERIFY,                  "SCSI_VERIFY",                  msc_scsi_verify },                   //
+        { SCSI_INQUIRY,                 "SCSI_INQUIRY",                 msc_scsi_inquiry },                  //
+        { SCSI_MODE_SENSE,              "SCSI_MODE_SENSE",              msc_scsi_mode_sense },               //
+        { SCSI_READ_FORMAT_CAPACITY,    "SCSI_READ_FORMAT_CAPACITY",    msc_scsi_read_format_capacity },     //
+        { SCSI_REQUEST_SENSE,           "SCSI_REQUEST_SENSE",           msc_scsi_request_sense },            //
+        { SCSI_PREVENT_ALLOW_MEDIUM_REMOVAL, "SCSI_PREVENT_ALLOW_MEDIUM_REMOVAL", msc_scsi_prevent_allow }, //
+        { SCSI_START_STOP,              "SCSI_START_STOP",              msc_scsi_start_stop },               //
+        { SCSI_MODE_SELECT,             "SCSI_MODE_SELECT",             msc_scsi_mode_select },
+        { SCSI_FORMAT_UNIT,             "SCSI_FORMAT_UNIT",             msc_cmd_unknown },
+
+        { SCSI_READ_6,                  "SCSI_READ_6",                  msc_cmd_unknown },
+        { SCSI_WRITE_6,                 "SCSI_WRITE_6",                 msc_cmd_unknown },
+        { SCSI_RESERVE,                 "SCSI_RESERVE",                 msc_cmd_unknown },
+        { SCSI_RELEASE,                 "SCSI_RELEASE",                 msc_cmd_unknown },
+        { SCSI_SEND_DIAGNOSTIC,         "SCSI_SEND_DIAGNOSTIC",         msc_cmd_unknown },
+        { SCSI_SYNCHRONIZE_CACHE,       "SCSI_SYNCHRONIZE_CACHE",       msc_cmd_unknown },
+        { SCSI_MODE_SENSE_10,           "SCSI_MODE_SENSE_10",           msc_cmd_unknown },
+        { SCSI_REZERO_UNIT,             "SCSI_REZERO_UNIT",             msc_cmd_unknown },
+        { SCSI_SEEK_10,                 "SCSI_SEEK_10",                 msc_cmd_unknown },
+        { SCSI_WRITE_AND_VERIFY,        "SCSI_WRITE_AND_VERIFY",        msc_cmd_unknown },
+        { SCSI_WRITE_12,                "SCSI_WRITE_12",                msc_cmd_unknown },
+        { SCSI_READ_12,                 "SCSI_READ_12",                 msc_cmd_unknown },
+
+        { 0xff,                         "SCSI_UNKNOWN",                      msc_cmd_unknown },
+};
+
+
+/* msc_recv_command - process a new CBW
+ *
+ * Return non-zero if urb was not disposed of.
+ */
+int msc_recv_command(struct usbd_urb *urb, struct msc_private *msc)
+{
+        COMMAND_BLOCK_WRAPPER *command = (COMMAND_BLOCK_WRAPPER *)urb->buffer;
+        u8 op = command->CBWCB[0];
+        struct rbc_dispatch *dispatch;
+
+        /*
+         * c.f. section 6.2 - Valid and Meaningful CBW
+         * c.f. section 6.2.1 - Valid CBW
+         *
+         * The CBW was received after the device had sent a CSW or after a
+         * reset XXX check that we only set MSC_READY after reset or sending
+         * CSW.
+         *
+         * The CBW is 31 (1Fh) bytes in length and the bCBWSignature is
+         * equal to 43425355h.
+         */
+        THROW_IF(31 != urb->actual_length, error);
+        THROW_IF(CBW_SIGNATURE != le32_to_cpu(command->dCBWSignature), error);
+        
+        /*
+         * c.f. section 6.2.2 - Meaningful CBW
+         *
+         * no reserved bits are set
+         * the bCBWLUN contains a valid LUN supported by the device
+         * both bCBWCBlength and the content of the CBWCB are in accordance with bInterfaceSubClass
+         */
+
+        // XXX checklun etc
+
+        /* 
+         * Success 
+         */
+        memcpy(&msc->command, command, sizeof(COMMAND_BLOCK_WRAPPER));
+        msc->data_transferred_in_bytes = msc->TransferLength_in_blocks = msc->TransferLength_in_bytes = 0;
+
+        TRACE_TAG(command->dCBWTag, urb->framenum);
+        usbd_free_urb(urb);
+
+        /*
+         * Search using the opcode to find the dispatch function to use and
+         * call it.
+         */
+        for (dispatch = rbc_dispatch_table; dispatch->op != 0xff; dispatch++) {
+                if ((dispatch->op == op)) {
+                        TRACE_CBW(dispatch->name, dispatch->op);
+                        TRACE_RECV(&(command->CBWCB[1]));
+                        if (dispatch->rbc_command(msc, dispatch->name, op)) 
+                                TRACE_MSG0(MSC,"COMMAND ERROR");
+                        return 0;
+                }
+        }
+        /* FALL THROUGH if no match is found */
+
+        THROW_IF(msc_cmd_unknown(msc, "CMD_UNKNOWN", op), error);
+        return 0;
+
+        CATCH(error) {
+                TRACE_MSG0(MSC,"RECV CBW ERROR");
+        }
+        
+        /*
+         * default behaviour is to stall or send ZLP on BULK-IN endpoint
+         */
+        TRACE_CBW("UNKNOWN COMMAND", op);
+
+        /*
+         * We cannot return error as we have deallocated urb and there is no
+         * other sensible course of action that the usbdcore layer can take
+         * for us.
+         */
+        return 0;
+}
+
+
+/* Sent Function - process a sent urb ********************************************************** */
+
+/* msc_urb_sent - called to indicate URB transmit finished
+ * @urb: pointer to struct usbd_urb
+ * @rc: result
+ *
+ * This is called when an urb is sent. Depending on current state
+ * it may:
+ *
+ *      - continue sending data
+ *      - send a CSW
+ *      - start a recv for a CBW
+ *
+ * This is called from BOTTOM HALF context.
+ *
+ * Return non-zero if urb was not disposed of.
+ */
+int msc_urb_sent (struct usbd_urb *tx_urb, int rc)
+{
+        struct usbd_function_instance *function;
+        struct msc_private *msc = &msc_private;
+
+        msc_interrupts++;
+
+        TRACE_MSG1(MSC,"URB SENT tx_urb: %p", (int)tx_urb);
+        TRACE_MSG1(MSC,"URB SENT function: %p", (int)tx_urb->function_instance);
+
+        RETURN_EINVAL_IF(!(function = tx_urb->function_instance)); 
+
+        TRACE_MSG1(MSC,"URB SENT status: %p", usbd_get_device_status(function));
+        RETURN_EINVAL_IF(usbd_get_device_status(function) == USBD_CLOSING);
+
+        TRACE_MSG1(MSC,"URB SENT state: %p", usbd_get_device_state(function));
+        RETURN_EINVAL_IF(usbd_get_device_state(function) != STATE_CONFIGURED);
+
+        switch (msc->command_state) {
+        case MSC_DATA_IN_READ:
+        case MSC_DATA_IN_READ_FINISHED:
+                TRACE_MSG0(MSC,"URB SENT READ");
+                return msc_in_read_10_urb_sent(tx_urb, msc);
+        case MSC_QUERY:
+                // finished, send CSW
+                TRACE_MSG0(MSC,"URB SENT QUERY");
+                msc_start_sending_csw(msc->function, msc, USB_MSC_PASSED);
+                break;
+        case MSC_STATUS:
+        default:
+                // sent a CSW need to receive the next CBW
+                TRACE_MSG0(MSC,"URB SENT STATUS");
+                msc->command_state = MSC_READY;
+                msc_start_recv_urb(msc->function, msc, sizeof(COMMAND_BLOCK_WRAPPER));
+                break;
+        }
+        usbd_free_urb (tx_urb);
+        return 0;
+}
+
+
+/* Receive Function - receiving an urb ********************************************************* */
+
+/* msc_recv_urb - process a received urb
+ *
+ * Return non-zero if urb was not disposed of.
+ */
+static int msc_recv_urb (struct usbd_urb *rcv_urb, int rc)
+{
+        struct msc_private *msc = &msc_private;
+
+        msc_interrupts++;
+
+        RETURN_EINVAL_IF(!msc->connected);
+
+        TRACE_MSG2(MSC, "RECV URB length: %d state: %d", rcv_urb->actual_length, msc->command_state);
+
+        switch(msc->command_state) {
+
+                // ready to start a new transaction
+        case MSC_READY:
+                return msc_recv_command(rcv_urb, msc);
+
+                // we think we are receiving data 
+        case MSC_DATA_OUT_WRITE:
+        case MSC_DATA_OUT_WRITE_FINISHED:
+                msc_recv_out_blocks(rcv_urb, msc);
+                return 0;
+
+                // we think we are sending data
+        case MSC_DATA_IN_READ:
+        case MSC_DATA_IN_READ_FINISHED:
+                msc_start_sending_csw_failed (msc, SCSI_SENSEKEY_INVALID_COMMAND, msc->lba, USB_MSC_FAILED);
+                break;
+
+                // we think we are sending status
+        case MSC_STATUS:
+                msc_start_sending_csw_failed (msc, SCSI_SENSEKEY_INVALID_COMMAND, msc->lba, USB_MSC_FAILED);
+                break;
+
+                // we don't think
+        case MSC_UNKNOWN:
+        default:
+                TRACE_MSG0(MSC,"RECV URB ERROR");
+        }
+        // let caller dispose of urb
+        return -EINVAL;
+}
+
+/* USB Device Functions ************************************************************************ */
+
+/* msc_event_handler - process a device event
+ *
+ * This function is called when an USBD event occurs. 
+ *
+ * This is called from INTERRUPT context.
+ */
+void msc_event_handler (struct usbd_function_instance *function, usbd_device_event_t event, int data)
+{
+        struct msc_private *msc = &msc_private;
+
+        msc_interrupts++;
+
+        TRACE_MSG1(MSC,"EVENT IRQ %d", event);
+
+        switch (event) {
+        case DEVICE_CONFIGURED:
+                TRACE_MSG0(MSC,"EVENT CONFIGURED");
+                msc->connected = 1;
+                msc->command_state = MSC_READY;
+                msc_start_recv_urb(function, msc, sizeof(COMMAND_BLOCK_WRAPPER));
+                break;
+
+        case DEVICE_BUS_INACTIVE:
+        case DEVICE_RESET:
+        case DEVICE_DE_CONFIGURED:
+                TRACE_MSG0(MSC,"EVENT RESET");
+                BREAK_IF(!msc->connected);
+                msc->connected = 0;
+                if (msc->rcv_urb_finished) {
+                        usbd_free_urb (msc->rcv_urb_finished);
+                        msc->rcv_urb_finished = NULL;
+                }
+                break;
+
+        default: 
+                TRACE_MSG0(MSC,"EVENT IGNORED");
+                break;
+        }
+}
+
+
+/* msc_device_request - called to indicate urb has been received
+ *
+ * This function is called when a SETUP packet has been received that
+ * should be handled by the function driver. It will not be called to
+ * process the standard chapter nine defined requests.
+ *
+ * Return non-zero for failure.
+ */
+int msc_device_request (struct usbd_function_instance *function, struct usbd_device_request *request)
+{
+        struct msc_private *msc = &msc_private;
+        struct usbd_urb *urb;
+
+        TRACE_MSG0(MSC,"RECV SETUP");
+        msc_interrupts++;
+
+        // verify that this is a usb class request per cdc-acm specification or a vendor request.
+        RETURN_ZERO_IF (!(request->bmRequestType & (USB_REQ_TYPE_CLASS | USB_REQ_TYPE_VENDOR)));
+       
+        // determine the request direction and process accordingly
+        switch (request->bmRequestType & (USB_REQ_DIRECTION_MASK | USB_REQ_TYPE_MASK)) {
+
+        case USB_REQ_HOST2DEVICE | USB_REQ_TYPE_CLASS:
+                switch (request->bRequest) {
+                case MSC_BULKONLY_RESET:
+                        // XXX TODO FIXME
+                        return 0;
+                }
+
+        case USB_REQ_DEVICE2HOST | USB_REQ_TYPE_CLASS:
+                switch (request->bRequest) {
+                case MSC_BULKONLY_GETMAXLUN: 
+                        RETURN_EINVAL_IF(!(urb = usbd_alloc_urb_ep0(function, 1, NULL)));
+                        urb->buffer[0] = 0;
+                        urb->actual_length = 1;
+                        RETURN_ZERO_IF(!usbd_start_in_urb(urb));
+                        usbd_free_urb(urb);
+                        return -EINVAL;
+                }
+        default: 
+                break;
+        }
+        return -EINVAL;
+}
+
+/* msc_function_enable - this is called by the USBD core layer 
+ *
+ * This is called to initialize the function when a bus interface driver
+ * is loaded.
+ */
+static int msc_function_enable (struct usbd_function_instance *function)
+{
+        struct msc_private *msc = &msc_private;
+
+        MOD_INC_USE_COUNT;
+
+        // XXX TODO need to verify that serial number is minimum of 12 
+        
+        msc_interrupts++;
+
+        msc->function = function;
+        msc->command_state = MSC_READY;
+
+        //msc_open_blockdev(msc);
+        return 0;
+}
+
+/* msc_function_disable - this is called by the USBD core layer 
+ *
+ * This is called to close the function when a bus interface driver
+ * is unloaded.
+ */
+static void msc_function_disable (struct usbd_function_instance *function)
+{               
+        struct msc_private *msc = &msc_private;
+
+        msc_interrupts++;
+
+        TRACE_MSG0(MSC,"FUNCTION EXIT");
+
+        msc->function = NULL;
+
+        MOD_DEC_USE_COUNT;
+}
+
+static struct usbd_function_operations function_ops = {
+        event_handler: msc_event_handler,
+        device_request: msc_device_request,
+        function_enable: msc_function_enable,
+        function_disable: msc_function_disable,
+};
+
+static struct usbd_function_driver function_driver = {
+        name: "msc-bulkonly",
+        fops:&function_ops,
+        device_description:&msc_device_description,
+        bNumConfigurations:sizeof (msc_description) / sizeof (struct usbd_configuration_description),
+        configuration_description:msc_description,
+        idVendor: __constant_cpu_to_le16(CONFIG_OTG_MSC_VENDORID),
+        idProduct: __constant_cpu_to_le16(CONFIG_OTG_MSC_PRODUCTID),
+        bcdDevice: __constant_cpu_to_le16(CONFIG_OTG_MSC_BCDDEVICE),
+        bNumInterfaces:sizeof (msc_interfaces) / sizeof (struct usbd_interface_description),
+        interface_list:msc_interfaces,
+        endpointsRequested: ENDPOINTS,
+        requestedEndpoints: msc_endpoint_requests,
+};
+
+
+/* USB Module init/exit ************************************************************************ */
+
+/*
+ * msc_modinit - module init
+ *
+ */
+static int msc_modinit (void)
+{
+        int rc;
+        struct msc_private *msc = &msc_private;
+
+        printk(KERN_INFO "Copyright (c) 2004 Belcarra Technologies; www.belcarra.com; sl@belcarra.com\n");
+        printk (KERN_INFO "%s vendor_id: %04x product_id: %04x major: %d minor: %d\n", __FUNCTION__, 
+                        vendor_id, product_id, major, minor);
+
+        if (vendor_id) 
+                function_driver.idVendor = cpu_to_le16(vendor_id);
+        if (product_id) 
+                function_driver.idProduct = cpu_to_le16(product_id);
+
+        init_waitqueue_head(&msc->msc_wq);
+#if 0
+        /*
+         * Check device information and verify access to the block device.
+         */
+        RETURN_EINVAL_IF(!major && !minor);
+        msc->dev = MKDEV(major, minor);
+        if(!(msc->bdev = bdget(kdev_t_to_nr(msc->dev)))) {
+                printk(KERN_INFO"%s: Cannot find device %d %d\n", __FUNCTION__, major, minor); 
+                return -EINVAL;
+        }
+        if ((rc = blkdev_get(msc->bdev, FMODE_READ | FMODE_WRITE, 0, BDEV_RAW))) {
+                printk(KERN_INFO"%s: Cannot get device %d %d\n", __FUNCTION__, major, minor); 
+                return -EINVAL;
+        }
+        
+        /*
+         * Note that capacity must return the LBA of the last addressable block
+         * c.f. RBC 4.4, RBC 5.3 and notes below RBC Table 6
+         *
+         * The blk_size array contains the number of addressable blocks or N, 
+         * capacity is therefore N-1.
+         */
+         
+        msc->capacity = (blk_size[MAJOR(msc_private.dev)][MINOR(msc_private.dev)] << 1) - 1;
+        msc->block_size = get_hardsect_size(msc->dev);
+        msc->max_blocks = PAGE_SIZE / msc->block_size;
+#endif
+
+        msc->block_dev_state = DEVICE_EJECTED;
+
+        msc_open_blockdev(msc);
+
+        /*
+         * setup generic buffer_head
+         */
+
+        msc->read_bh.b_rdev = msc->dev;
+        msc->read_bh.b_private = msc;
+        msc->read_bh.b_page = alloc_page(GFP_NOIO);
+
+        msc->write_bh.b_rdev = msc->dev;
+        msc->write_bh.b_private = msc;
+        msc->write_bh.b_page = alloc_page(GFP_NOIO);
+
+        msc->command_state = MSC_READY;
+        msc->io_state = MSC_INACTIVE;
+
+        msc_trace_init("msctrace", &msc_private);
+        make_crc_table();
+
+        TRACE_MSG1(MSC,"PAGE_SHIFT: %x", PAGE_SHIFT); 
+        TRACE_MSG1(MSC,"PAGE_SIZE: %x", PAGE_SIZE); 
+        TRACE_MSG1(MSC,"PAGE_SIZE: %d", PAGE_SIZE); 
+
+        // register as usb function driver
+        THROW_IF (usbd_register_function (&function_driver, NULL), error);
+        msc->usb_driver_registered++;
+
+        CATCH(error) {
+                if (msc->usb_driver_registered) {
+                        usbd_deregister_function (&function_driver);
+                        msc->usb_driver_registered = 0;
+                }
+                return -EINVAL;
+        }
+        return 0;
+}
+
+/* msc_modexit - module cleanup
+ */
+static void msc_modexit (void)
+{
+        struct msc_private *msc = &msc_private;
+
+        if (msc->bdev) {
+                blkdev_put(msc->bdev, BDEV_RAW);
+        }
+        // XXX this should be a wait for read_bh/write_bh to go to NULL
+        while (msc->read_bh.b_data) {
+                printk(KERN_INFO"%s: sleeping on read bh\n", __FUNCTION__);
+                sleep_on_timeout(&msc->msc_wq, 20);
+        }
+
+        while (msc->write_bh.b_data) {
+                printk(KERN_INFO"%s: sleeping on read bh\n", __FUNCTION__);
+                sleep_on_timeout(&msc->msc_wq, 20);
+        }
+
+        printk(KERN_INFO"%s: freeing read bh\n", __FUNCTION__);
+        __free_page((void *)&msc->read_bh.b_page);
+        msc->read_bh.b_page = NULL;
+
+        printk(KERN_INFO"%s: freeing write bh\n", __FUNCTION__);
+        __free_page((void *)&msc->write_bh.b_page);
+        msc->write_bh.b_page = NULL;
+
+        if (msc->usb_driver_registered) {
+                usbd_deregister_function (&function_driver);
+        }
+
+        msc_trace_exit("msctrace");
+        free_crc_table();
+}
+
+
+module_init (msc_modinit);
+module_exit (msc_modexit);
diff -uNr linux/drivers/no-otg/functions/msc/msc-scsi.h linux/drivers/otg/functions/msc/msc-scsi.h
--- linux/drivers/no-otg/functions/msc/msc-scsi.h	1970-01-01 01:00:00.000000000 +0100
+++ linux/drivers/otg/functions/msc/msc-scsi.h	2006-09-01 21:41:28.000000000 +0200
@@ -0,0 +1,795 @@
+/*
+ * otg/msc_fd/msc_scsi.h - mass storage protocol library header
+ *
+ *      Copyright(c) 2004, Belcarra
+ *
+ * Adapated from work:
+ *
+ *      Copyright (c) 2003 Lineo Solutions, Inc.
+ *
+ * Written by Shunnosuke kabata
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ */
+
+
+/*
+ *
+ * Documents
+ *      Universal Serial Bus Mass Storage Class - Specification Overview
+ *      Universal Serial Bus Mass Storage Class - Bulk-Only Transport
+ *      T10/1240-D - Information technology - Reduced Block Commands
+ *
+ * Notes
+ *
+ * 1. Reduced Block Command set C.f. Table 2 RBC
+ *
+ *                                              Command Support
+ *      Name                            OpCode  Fixed   Removable       Reference
+ *      Format Unit                     04h     O       O               RBC
+ *      Inquiry                         12h     M       M               SPC-2
+ *      Mode Select (6)                 15h     M       M               SPC-2
+ *      Mode Sense (6)                  1Ah     M       M               SPC-2
+ *      Perisstent Reserve In           5Eh     O       O               SPC-2
+ *      Persistent Reserve Out          5Fh     O       O               SPC-2
+ *      Prevent/Allow Medium Removal    1Eh     N/A     M               SPC-2
+ *      Read (10)                       28h     M       M               RBC
+ *      Read Capacity                   25h     M       M               RBC
+ *      Reelase (6)                     17h     O       O               SPC-2
+ *      Request Sense                   03h     O       O               SPC-2
+ *      Reserve (6)                     16h     O       O               SPC-2
+ *      Start Stop Unit                 1Bh     M       M               RBC
+ *      Synchronize Cache               35h     O       O               RBC
+ *      Test Unit Ready                 00h     M       M               SPC-2
+ *      Verify (10)                     2Fh     M       M               RBC
+ *      Write (10)                      2Ah     M       M               RBC
+ *      Write Buffer                    3Bh     M       O               SPC-2
+ *
+ * 2. Other commands seen?
+ *                                                      Sh      MV      FS
+ *      SCSI_REZERO_UNIT                     0x01       no
+ *      SCSI_READ_6                          0x08                       yes
+ *      SCSI_WRITE_6                         0x0a                       yes
+ *      SCSI_SEND_DIAGNOSTIC                 0x1d       no              yes
+ *      SCSI_READ_FORMAT_CAPACITY            0x23       yes             yes
+ *      SCSI_WRITE_AND_VERIFY                0x2e       no
+ *      SCSI_SEEK_10                         0x2b       no
+ *      SCSI_MODE_SELECT_10                  0x55       no      yes     yes
+ *      SCSI_READ_12                         0xa8       no              yes
+ *      SCSI_WRITE_12                        0xaa       no              yes
+ *
+ *
+ * 3. Status - C.f. Chapter 5 - RBC
+ *
+ *      Check Condition                 02h
+ *
+ *
+ * 4. Sense Keys - C.f. Chapter 5 - RBC
+ *      
+ *      Not Ready                       02h
+ *      Media Error                     03h
+ *      Illegal Request                 05h
+ *      Unit Attention                  06h
+ *
+ * 5. ASC/ASCQ - C.f. Chapter 5 - RBC
+ *
+ *      Logical Unit Not Ready                                          04h
+ *      Logical Unit Not Ready, Format in Progress                      04h,04h
+ *      Invalid Command Operation Code                                  20h
+ *      Invalide Field in CDB                                           24h
+ *      Format Command Failed                                           31h,01h
+ *      Status Notification / Power Management Class Event              38h,02h
+ *      Status Notification / Media Class Event                         38h,04h
+ *      Status Notification / Device Busy Class Event                   38h,06h
+ *      Low Power Condition Active                                      5Eh,00
+ *      Power Condition Change to Active                                5Eh,41h
+ *      Power Condition Change to Idle                                  5Eh,42h
+ *      Power Condition Change to Standby                               5Eh,43h
+ *      Power Condition Change to Sleep                                 5Eh,45h
+ *      Power Condition Change to Device Control                        5Eh,47h
+ *
+ * 6. ASCQ - C.f. Chapter 5 - RBC
+ *
+ *
+ *
+ * 7. Sense Keys C.f. Chapter 5 - RBC
+ *
+ * Command                      Status                  Sense Key      ASC/ASCQ
+ *
+ * 5.1 Format Unit
+ *   Format progress            Check Condition         Not Ready,      Logical Unit Note Ready, Format in Progress
+ *   Sueccsful completion       Check Condition         Unit Attention, Status Notification / Media Class Event
+ *   Failure                    Check Condition         Media error,    Format Command Failed
+ *
+ * 5.2 Read (10)
+ *
+ * 5.3 Read Capacity
+ *   No Media                   Check Condition         Not Ready,      Logical Unit Not Ready
+ *
+ * 5.4 Start Stop Unit
+ *   Power Consumption          Check Condition         Illegal Request,Low Power Condition Active
+ *
+ * 5.5 Synchronize Cache        Check Condition         Illegal Request,Invalid Command Operation Code
+ *
+ * 5.6 Write (10
+ *
+ * 5.7 Verify
+ *
+ * 5.8 Mode
+ * 
+ * 6.1 Inquiry
+ *
+ * 6.2 Mode Select (6)
+ *   Cannot Save                Check Condition         Illegal Request,Invalid Field in CDB
+ *
+ * 6.3 Mode Sense (6)
+ *
+ * 6.4 Prevent Allow Medium Removal
+ *
+ * 6.5 Request Sense
+ *
+ * 6.6 Test Unit Ready
+ *
+ * 6.7 Write Buffer
+ *
+ * 7.1 Unit Attention
+ *   Power Contition change     Check Condition         Unit Attention, Power Condition Change Notification
+ *
+ * 7.4.1 Event Status Sense
+ *
+ *   Power Management Event     Check Condition         Unit Attention, Event Status Notification / Power Managment
+ *   Media Class Event          Check Condition         Unit Attention, Event Status Notification / Media Class
+ *   Device Busy Event          Check Condition         Unit Attention, Event Status Notification / Device Busy Class
+ *
+ * 7.4.6 Removable Medium Device Initial Response
+ *   Ready                      Check Condition         Unit Attention, Event Status Notification / Media Class Event 
+ *   Power                      Check Condition         Unit Attention, Event Status Notification / Power Management Class Event
+ */
+
+#ifndef _MSCSCSIPROTO_H_
+#define _MSCSCSIPROTO_H_
+
+
+/*
+ * Class Specific Requests - C.f. MSC BO Chapter 3
+ */
+
+#define MSC_BULKONLY_RESET      0xff
+#define MSC_BULKONLY_GETMAXLUN  0xfe
+
+
+/*
+ * Class Code
+ */
+
+#define MASS_STORAGE_CLASS                  0x08
+
+/*
+ * MSC - Specification Overview
+ *
+ * SubClass Codes - C.f MSC Table 2.1
+ */
+
+#define MASS_STORAGE_SUBCLASS_RBC           0x01
+#define MASS_STORAGE_SUBCLASS_SFF8020I      0x02
+#define MASS_STORAGE_SUBCLASS_QIC157        0x03
+#define MASS_STORAGE_SUBCLASS_UFI           0x04
+#define MASS_STORAGE_SUBCLASS_SFF8070I      0x05
+#define MASS_STORAGE_SUBCLASS_SCSI          0x06
+
+/*
+ * Protocol - C.f MSC Table 3.1
+ */
+
+#define MASS_STORAGE_PROTO_CBI_WITH_COMP    0x00
+#define MASS_STORAGE_PROTO_CBI_NO_COMP      0x01
+#define MASS_STORAGE_PROTO_BULK_ONLY        0x50
+
+/*
+ * SCSI Command
+*/
+
+#define SCSI_TEST_UNIT_READY                    0x00
+#define SCSI_REQUEST_SENSE                      0x03
+#define SCSI_FORMAT_UNIT                        0x04
+#define SCSI_INQUIRY                            0x12
+#define SCSI_MODE_SELECT                        0x15    // aka MODE_SELECT_6
+#define SCSI_MODE_SENSE                         0x1a    // aka MODE_SENSE_6
+#define SCSI_START_STOP                         0x1b
+#define SCSI_PREVENT_ALLOW_MEDIA_REMOVAL        0x1e
+#define SCSI_READ_FORMAT_CAPACITY               0x23
+#define SCSI_READ_CAPACITY                      0x25
+#define SCSI_READ_10                            0x28
+#define SCSI_WRITE_10                           0x2a
+#define SCSI_VERIFY                             0x2f
+
+#define SCSI_READ_6                             0x08
+#define SCSI_WRITE_6                            0x0a
+#define SCSI_RESERVE                            0x16
+#define SCSI_RELEASE                            0x17
+#define SCSI_SEND_DIAGNOSTIC                    0x1d
+#define SCSI_SYNCHRONIZE_CACHE                  0x35
+#define SCSI_MODE_SENSE_10                      0x5a
+
+#define SCSI_REZERO_UNIT                        0x01
+#define SCSI_REASSIGN_BLOCKS                    0x07
+#define SCSI_COPY                               0x18
+#define SCSI_RECEIVE_DIAGNOSTIC_RESULTS         0x1c
+#define SCSI_WRITE_AND_VERIFY                   0x2e
+#define SCSI_PREFETCH                           0x34
+#define SCSI_READ_DEFECT_DATA                   0x37
+#define SCSI_COMPARE                            0x39
+#define SCSI_COPY_AND_VERIFY                    0x3a
+#define SCSI_WRITE_BUFFER                       0x3b
+#define SCSI_READ_BUFFER                        0x3c
+#define SCSI_READ_LONG                          0x3e
+#define SCSI_WRITE_LONG                         0x3f
+#define SCSI_CHANGE_DEFINITION                  0x40
+#define SCSI_WRITE_SAME                         0x41
+#define SCSI_LOG_SELECT                         0x4c
+#define SCSI_LOG_SENSE                          0x4d
+#define SCSI_XD_WRITE                           0x50
+#define SCSI_XP_WRITE                           0x51
+#define SCSI_XD_READ                            0x52
+#define SCSI_MODE_SELECT_10                     0x55
+#define SCSI_RESERVE_10                         0x56
+#define SCSI_RELEASE_10                         0x57
+#define SCSI_MODE_SELECT_10                     0x55
+#define SCSI_XD_WRITE_EXTENDED                  0x80
+#define SCSI_REBUILD                            0x81
+#define SCSI_REGENERATE                         0x82
+
+#define SCSI_SEEK_10                            0x2b
+#define SCSI_WRITE_AND_VERIFY                   0x2e
+#define SCSI_WRITE_12                           0xaa
+#define SCSI_READ_12                            0xa8
+
+/*
+ * Private
+ */
+#define SCSI_PRIVATE_PCS                        0xff
+
+/*
+ * SCSI Command Parameter
+ */
+
+#define CBW_SIGNATURE           0x43425355  /* USBC */
+#define CSW_SIGNATURE           0x53425355  /* USBS */
+
+#define PRODUCT_REVISION_LEVEL  "1.00"
+
+/*
+ * Command Block Status Values - C.f MSC BO Table 5.3
+ */
+
+#define USB_MSC_PASSED  0x00            // good
+#define USB_MSC_FAILED  0x01            // bad
+#define USB_MSC_PHASE_ERROR     0x02    // we want to be reset
+
+
+
+
+/*
+ * SCSI Sense 
+ *      SenseKey
+ *      AdditionalSenseCode
+ *      SenseCodeQualifier
+ */
+
+#define SCSI_ERROR_CURRENT                      0x70
+#define SCSI_ERROR_DEFERRED                     0x07
+
+/*
+ *  SCSI Sense Keys
+ */
+
+#define SK_NO_SENSE            0x00
+#define SK_RECOVERED_ERROR     0x01
+#define SK_NOT_READY           0x02
+#define SK_MEDIA_ERROR         0x03
+#define SK_HARDWARE_ERROR      0x04
+#define SK_ILLEGAL_REQUEST     0x05
+#define SK_UNIT_ATTENTION      0x06
+#define SK_DATA_PROTECT        0x07
+#define SK_BLANK_CHECK         0x08
+#define SK_COPY_ABORTED        0x0a
+#define SK_ABORTED_COMMAND     0x0b
+#define SK_VOLUME_OVERFLOW     0x0d
+#define SK_MISCOMPARE          0x0e
+
+/*
+ * 5. ASC/ASCQ - C.f. Chapter 5 - RBC
+ */
+
+#define SK(SenseKey,ASC,ASCQ) ((SenseKey<<16) | (ASC << 8) | (ASCQ))
+
+#define SCSI_SENSEKEY_NO_SENSE                          SK(SK_NO_SENSE,         0x00,0x00)      // 0x000000
+
+#define SCSI_FAILURE_PREDICTION_THRESHOLD_EXCEEDED      SK(SK_RECOVERED_ERROR,  0x5d,0x00)      // 0x015d00
+
+#define SCSI_SENSEKEY_LOGICAL_UNIT_NOT_READY            SK(SK_NOT_READY,        0x04,0x00)      // 0x020400
+#define SCSI_SENSEKEY_FORMAT_IN_PROGRESS                SK(SK_NOT_READY,        0x04,0x04)      // 0x020404
+#define SCSI_SENSEKEY_MEDIA_NOT_PRESENT                 SK(SK_NOT_READY,        0x3a,0x00)      // 0x023a00
+
+#define SCSI_SENSEKEY_WRITE_ERROR                       SK(SK_MEDIA_ERROR,      0x0c,0x02)      // 0x030c02
+#define SCSI_SENSEKEY_UNRECOVERED_READ_ERROR            SK(SK_MEDIA_ERROR,      0x11,0x00)      // 0x031100
+#define SCSI_FORMAT_COMMAND_FAILED                      SK(SK_MEDIA_ERROR,      0x31,0x01)      // 0x033101
+
+#define SCSI_SENSEKEY_COMMUNICATION_FAILURE             SK(SK_HARDWARE_ERROR,   0x08,0x00)       // 0x040800
+
+#define SCSI_SENSEKEY_INVALID_COMMAND                   SK(SK_ILLEGAL_REQUEST,  0x20,0x00)      // 0x052000
+#define SCSI_SENSEKEY_BLOCK_ADDRESS_OUT_OF_RANGE        SK(SK_ILLEGAL_REQUEST,  0x21,0x00)      // 0x052100
+#define SCSI_SENSEKEY_INVALID_FIELD_IN_CDB              SK(SK_ILLEGAL_REQUEST,  0x24,0x00)      // 0x052400
+#define SCSI_SENSEKEY_LOGICAL_UNIT_NOT_SUPPORTED        SK(SK_ILLEGAL_REQUEST,  0x25,0x00)      // 0x052500
+#define SCSI_SENSEKEY_SAVING_PARAMETERS_NOT_SUPPORTED   SK(SK_ILLEGAL_REQUEST,  0x39,0x00)      // 0x053900
+#define SCSI_MEDIA_REMOVAL_PREVENTED                    SK(SK_ILLEGAL_REQUEST,  0x53,0x02)      // 0x055302
+
+#define SCSI_SENSEKEY_NOT_READY_TO_READY_CHANGE         SK(SK_UNIT_ATTENTION,   0x28,0x00)      // 0x062800
+#define SCSI_SENSEKEY_RESET_OCCURRED                    SK(SK_UNIT_ATTENTION,   0x29,0x00)      // 0x062900
+
+#define SCSI_SENSEKEY_STATUS_NOTIFICATION_POWER_CLASS   SK(SK_UNIT_ATTENTION,   0x38,0x02)      // 0x063802
+#define SCSI_SENSEKEY_STATUS_NOTIFICATION_MEDIA_CLASS   SK(SK_UNIT_ATTENTION,   0x38,0x04)      // 0x063804
+#define SCSI_SENSEKEY_STATUS_NOTIFICATION_DEVICE_BUSY   SK(SK_UNIT_ATTENTION,   0x38,0x06)      // 0x063806
+
+#define SCSI_SENSEKEY_LOW_POWER_CONDITION_ACTIVE        SK(SK_UNIT_ATTENTION,   0x5e,0x00)      // 0x065e00
+#define SCSI_SENSEKEY_POWER_CONDITION_CHANGE_TO_ACTIVE  SK(SK_UNIT_ATTENTION,   0x5e,0x41)      // 0x065e41
+#define SCSI_SENSEKEY_POWER_CONDITION_CHANGE_TO_IDLE    SK(SK_UNIT_ATTENTION,   0x5e,0x42)      // 0x065e42
+#define SCSI_SENSEKEY_POWER_CONDITION_CHANGE_TO_STANDBY SK(SK_UNIT_ATTENTION,   0x5e,0x43)      // 0x065e43
+#define SCSI_SENSEKEY_POWER_CONDITION_CHANGE_TO_SLEEP   SK(SK_UNIT_ATTENTION,   0x5e,0x45)      // 0x065e45
+#define SCSI_SENSEKEY_POWER_CONDITION_CHANGE_TO_DEVICE  SK(SK_UNIT_ATTENTION,   0x5e,0x47)      // 0x065e47
+
+
+#define SCSI_SENSEKEY_WRITE_PROTECTED                   SK(SK_DATA_PROTECT,     0x27,0x00)      // 0x072700
+
+
+/*
+ * Mode Page Code and Page Control
+ */
+#define SCSI_MODEPAGE_CONTROL_CURRENT           0x0
+#define SCSI_MODEPAGE_CONTROL_CHANGEABLE        0x1
+#define SCSI_MODEPAGE_CONTROL_DEFAULT           0x2
+#define SCSI_MODEPAGE_CONTROL_SAVED             0x3
+
+#define SCSI_MODEPAGE_UNIT_ATTENTION            0x00
+#define SCSI_MODEPAGE_ERROR_RECOVERY            0x01
+#define SCSI_MODEPAGE_DISCONNNECT_RECONNECT     0x02
+#define SCSI_MODEPAGE_FORMAT                    0x03
+#define SCSI_MODEPAGE_RIGID_DRIVE_GEOMETRY      0x04
+#define SCSI_MODEPAGE_FLEXIBLE_DISK_PAGE        0x05    // check
+#define SCSI_MODEPAGE_VERIFY_ERROR_RECOVERY     0x07
+#define SCSI_MODEPAGE_CACHING                   0x08
+#define SCSI_MODEPAGE_CONTROL_MODE              0x0a
+#define SCSI_MODEPAGE_NOTCH_AND_PARTITION       0x0c
+#define SCSI_MODEPAGE_POWER_CONDITION           0x0d
+#define SCSI_MODEPAGE_XOR                       0x10
+#define SCSI_MODEPAGE_CONTROL_MODE_ALIAS        0x1a
+#define SCSI_MODEPAGE_REMOVABLE_BLOCK_ACCESS    0x1b// check
+#define SCSI_MODEPAGE_INFORMATION_EXCEPTIONS    0x1c
+#define SCSI_MODEPAGE_ALL_SUPPORTED             0x3f
+
+
+
+/*
+ * Command Block Wrapper / Command Status Wrapper
+ */
+
+/*
+ * Command Block Wrapper
+ */
+typedef struct{
+        unsigned long   dCBWSignature;
+        unsigned long   dCBWTag;
+        unsigned long   dCBWDataTransferLength;
+        u8   bmCBWFlags;
+        u8   bCBWLUN:4,
+             Reserved:4;
+        u8   bCBWCBLength:5,
+             Reserved2:3;
+        u8   CBWCB[16];
+} __attribute__((packed)) COMMAND_BLOCK_WRAPPER;
+
+/*
+ * Command Status Wrapper
+ */
+typedef struct{
+        unsigned long   dCSWSignature;
+        unsigned long   dCSWTag;
+        unsigned long   dCSWDataResidue;
+        u8   bCSWStatus;
+} __attribute__((packed)) COMMAND_STATUS_WRAPPER;
+
+/*
+ * SCSI Command
+ */
+
+/*
+ * INQUIRY
+ */
+
+typedef struct{
+        u8   OperationCode;
+        u8   EnableVPD:1,
+             Reserved1:4,
+             LogicalUnitNumber:3;
+        u8   PageCode;
+        u8   Reserved2;
+        u8   AllocationLength;
+        u8   Reserved3;
+        u8   Reserved4;
+        u8   Reserved5;
+        u8   Reserved6;
+        u8   Reserved7;
+        u8   Reserved8;
+        u8   Reserved9;
+} __attribute__((packed)) SCSI_INQUIRY_COMMAND;
+
+typedef struct{
+        u8   PeripheralDeviceType:5,
+             PeripheralQaulifier:3;
+        u8   Reserved2:7,
+             RMB:1;
+        u8   ANSIVersion:3,
+             ECMAVersion:3,
+             ISOVersion:2;
+        u8   ResponseDataFormat:4,
+             Reserved3:4;
+        u8   AdditionalLength;
+        u8   Reserved4;
+        u8   Reserved5;
+        u8   Reserved6;
+        u8   VendorInformation[8];
+        u8   ProductIdentification[16];
+        u8   ProductRevisionLevel[4];
+} __attribute__((packed)) SCSI_INQUIRY_DATA;
+
+/*
+ * READ FORMAT CAPACITY
+ */
+
+typedef struct{
+        u8   OperationCode;
+        u8   Reserved1:5,
+             LogicalUnitNumber:3;
+        u8   Reserved2;
+        u8   Reserved3;
+        u8   Reserved4;
+        u8   Reserved5;
+        u8   Reserved6;
+        u16  AllocationLength;
+        u8   Reserved7;
+        u8   Reserved8;
+        u8   Reserved9;
+} __attribute__((packed)) SCSI_READ_FORMAT_CAPACITY_COMMAND;
+
+typedef struct{
+        struct{
+                u8   Reserved1;
+                u8   Reserved2;
+                u8   Reserved3;
+                u8   CapacityListLength;
+        } __attribute__((packed)) CapacityListHeader;
+
+        struct{
+                u32  NumberofBlocks;
+                u8   DescriptorCode:2,
+                     Reserved1:6;
+                u8   BlockLength[3];
+        } __attribute__((packed)) CurrentMaximumCapacityDescriptor;
+
+} __attribute__((packed)) SCSI_READ_FORMAT_CAPACITY_DATA;
+
+/*
+ * READ FORMAT CAPACITY
+ */
+
+typedef struct{
+        u8   OperationCode;
+        u8   RelAdr:1, 
+             Reserved1:4, 
+             LogicalUnitNumber:3;
+        u32  LogicalBlockAddress;
+        u8   Reserved2;
+        u8   Reserved3;
+        u8   PMI:1, 
+             Reserved4:7;
+        u8   Reserved5;
+        u8   Reserved6;
+        u8   Reserved7;
+} __attribute__((packed)) SCSI_READ_CAPACITY_COMMAND;
+
+typedef struct{
+        u32   LastLogicalBlockAddress;
+        u32   BlockLengthInBytes;
+} __attribute__((packed)) SCSI_READ_CAPACITY_DATA;
+
+/*
+ * REQUEST SENSE
+ */
+
+typedef struct{
+        u8   OperationCode;
+        u8   Reserved1:5, 
+             LogicalUnitNumber:3;
+        u8   Reserved2;
+        u8   Reserved3;
+        u8   AllocationLength;
+        u8   Reserved4;
+        u8   Reserved5;
+        u8   Reserved6;
+        u8   Reserved7;
+        u8   Reserved8;
+        u8   Reserved9;
+        u8   Reserved10;
+} __attribute__((packed)) SCSI_REQUEST_SENSE_COMMAND;
+
+typedef struct{
+        u8   ErrorCode:7, 
+             Valid:1;
+        u8   Reserved1;
+        u8   SenseKey:4, 
+             Reserved2:4;
+        u32  Information;
+        u8   AdditionalSenseLength;
+        u8   Reserved3[4];
+        u8   AdditionalSenseCode;
+        u8   AdditionalSenseCodeQualifier;
+        u8   Reserved4;
+        u8   Reserved5[3];
+} __attribute__((packed)) SCSI_REQUEST_SENSE_DATA;
+
+/*
+ * READ(10)
+ */
+
+typedef struct{
+        u8   OperationCode;
+        u8   RelAdr:1, 
+             Reserved1:2, 
+             FUA:1, 
+             DPO:1, 
+             LogicalUnitNumber:3;
+        u32  LogicalBlockAddress;
+        u8   Reserved2;
+        u16  TransferLength;
+        u8   Reserved3;
+        u8   Reserved4;
+        u8   Reserved5;
+} __attribute__((packed)) SCSI_READ_10_COMMAND;
+
+/*
+ * MODE SENSE
+ */
+typedef struct{
+        u8   OperationCode;
+        u8   Reserved1:3, 
+             DBD:1, 
+             Reserved2:1, 
+             LogicalUnitNumber:3; 
+        u8   PageCode:6, 
+             PageControl:2;          // PC
+        u8   Reserved3;
+        u8   Reserved4;
+        u8   Reserved5;
+        u8   Reserved6;
+        u16   ParameterListLength;
+        u8   Reserved7;
+        u8   Reserved8;
+        u8   Reserved9;
+} __attribute__((packed)) SCSI_MODE_SENSE_COMMAND;
+
+typedef struct{
+        u8   ModeDataLength;
+        u8   MediumTypeCode;
+        u8   Reserved1:4,
+             DPOFUA:1,
+             Reserved2:2,
+             WriteProtect:1;
+        u8   Reserved3;
+} __attribute__((packed)) MODE_PARAMETER_HEADER;
+
+typedef struct{
+        u8   PageCode:6,
+             Reserved1:1,
+             PS:1;
+        u8   PageLength;
+        u8   DCR:1,
+             Reserved2:1,
+             PER:1,
+             Reserved3:1,
+             RC:1,
+             Reserved4:1,
+             Reserved5:1,
+             AWRE:1;
+        u8   ReadRetryCount;
+        u8   Reserved6[4];
+        u8   WriteRetryCount;
+        u8   Reserved7[3];
+} __attribute__((packed)) READ_WRITE_ERROR_RECOVERY_PAGE;
+
+typedef struct{
+        u8   PageCode:6,
+             Reserved1:1,
+             PS:1;
+        u8   PageLength;
+        u16  TransferRate;
+        u8   NumberofHeads;
+        u8   SectorsperTrack;
+        u16  DataBytesperSector;
+        u16  NumberofCylinders;
+        u8   Reserved2[9];
+        u8   MotorOnDelay;
+        u8   MotorOffDelay;
+        u8   Reserved3[7];
+        u16  MediumRotationRate;
+        u8   Reserved4;
+        u8   Reserved5;
+} __attribute__((packed)) FLEXIBLE_DISK_PAGE;
+
+typedef struct{
+        u8   PageCode:6,
+             Reserved1:1,
+             PS:1;
+        u8   PageLength;
+        u8   Reserved2:6,
+             SRFP:1,
+             SFLP:1;
+        u8   TLUN:3,
+             Reserved3:3,
+             SML:1,
+             NCD:1;
+        u8   Reserved4[8];
+} __attribute__((packed)) REMOVABLE_BLOCK_ACCESS_CAPABILITIES_PAGE;
+
+typedef struct{
+        u8   PageCode:6,
+             Reserved1:1,
+             PS:1;
+        u8   PageLength;
+        u8   Reserved2;
+        u8   InactivityTimeMultiplier:4,
+             Reserved3:4;
+        u8   SWPP:1,
+             DISP:1,
+             Reserved4:6;
+        u8   Reserved5;
+        u8   Reserved6;
+        u8   Reserved7;
+} __attribute__((packed)) TIMER_AND_PROTECT_PAGE;
+
+typedef struct{
+        READ_WRITE_ERROR_RECOVERY_PAGE              ReadWriteErrorRecoveryPage;
+        FLEXIBLE_DISK_PAGE                          FlexibleDiskPage;
+        REMOVABLE_BLOCK_ACCESS_CAPABILITIES_PAGE    RemovableBlockAccessCapabilitiesPage;
+        TIMER_AND_PROTECT_PAGE                      TimerAndProtectPage;
+} __attribute__((packed)) MODE_ALL_PAGES;
+
+typedef struct{
+        MODE_PARAMETER_HEADER   ModeParameterHeader;
+        union{
+            READ_WRITE_ERROR_RECOVERY_PAGE              ReadWriteErrorRecoveryPage;
+            FLEXIBLE_DISK_PAGE                          FlexibleDiskPage;
+            REMOVABLE_BLOCK_ACCESS_CAPABILITIES_PAGE    RemovableBlockAccessCapabilitiesPage;
+            TIMER_AND_PROTECT_PAGE                      TimerAndProtectPage;
+            MODE_ALL_PAGES                              ModeAllPages;
+        } __attribute__((packed)) ModePages;
+} __attribute__((packed)) SCSI_MODE_SENSE_DATA;
+
+/*
+ * TEST UNIT READY
+ */
+
+typedef struct{
+        u8   OperationCode;
+        u8   Reserved1:5,
+             LogicalUnitNumber:3;
+        u8   Reserved2;
+        u8   Reserved3;
+        u8   Reserved4;
+        u8   Reserved5;
+        u8   Reserved6;
+        u8   Reserved7;
+        u8   Reserved8;
+        u8   Reserved9;
+        u8   Reserved10;
+        u8   Reserved11;
+} __attribute__((packed)) SCSI_TEST_UNIT_READY_COMMAND;
+
+/*
+ * PREVENT-ALLOW MEDIA REMOVAL
+ */
+
+typedef struct{
+        u8   OperationCode;
+        u8   Reserved1:5,
+             LogicalUnitNumber:3;
+        u8   Reserved2;
+        u8   Reserved3;
+        u8   Prevent:2,
+             Reserved4:6;
+        u8   Reserved5;
+        u8   Reserved6;
+        u8   Reserved7;
+        u8   Reserved8;
+        u8   Reserved9;
+        u8   Reserved10;
+        u8   Reserved11;
+} __attribute__((packed)) SCSI_PREVENT_ALLOW_MEDIA_REMOVAL_COMMAND;
+
+/*
+ * START-STOP UNIT
+ */
+
+typedef struct{
+        u8   OperationCode;
+        u8   IMMED:1,
+             Reserved1:4,
+             LogicalUnitNumber:3;
+        u8   Reserved2;
+        u8   Reserved3;
+        u8   Start:1,
+             LoEj:1,
+             Reserved4:2,
+             PowerConditions:4;
+        u8   Reserved5;
+        u8   Reserved6;
+        u8   Reserved7;
+        u8   Reserved8;
+        u8   Reserved9;
+        u8   Reserved10;
+        u8   Reserved11;
+} __attribute__((packed)) SCSI_START_STOP_COMMAND;
+
+/*
+ * WRITE(10)
+ */
+
+typedef struct{
+        u8   OperationCode;
+        u8   RelAdr:1,
+             Reserved1:2,
+             FUA:1,
+             DPO:1,
+             LogicalUnitNumber:3;
+        u32  LogicalBlockAddress;
+        u8   Reserved2;
+        u16  TransferLength;
+        u8   Reserved3;
+        u8   Reserved4;
+        u8   Reserved5;
+} __attribute__((packed)) SCSI_WRITE_10_COMMAND;
+
+/*
+ * VERIFY
+ */
+
+typedef struct{
+        u8   OperationCode;
+        u8   RelAdr:1,
+             ByteChk:1,
+             Reserved1:1,
+             Reserved2:1,
+             DPO:1,
+             LogicalUnitNumber:3;
+        u32  LogicalBlockAddress;
+        u8   Reserved3;
+        u16  VerificationLength;
+        u8   Reserved4;
+        u8   Reserved5;
+        u8   Reserved6;
+} __attribute__((packed)) SCSI_VERIFY_COMMAND;
+
+#endif  /* _MSCSCSIPROTO_H_ */
+
diff -uNr linux/drivers/no-otg/functions/msc/msc.h linux/drivers/otg/functions/msc/msc.h
--- linux/drivers/no-otg/functions/msc/msc.h	1970-01-01 01:00:00.000000000 +0100
+++ linux/drivers/otg/functions/msc/msc.h	2006-09-01 21:41:28.000000000 +0200
@@ -0,0 +1,141 @@
+/*
+ * otg/function/msc/msc.h - Mass Storage Class
+ *
+ *      Copyright (c) 2003, 2004 Belcarra
+ *
+ * By: 
+ *      Stuart Lynne <sl@belcarra.com>
+ *      Bruce Balden <balden@belcarra.com>
+ *
+ */
+/*!
+ * @defgroup MSCFunction Mass Storage 
+ * @ingroup functiongroup
+ */
+/*!
+ * @file otg/functions/msc/msc.h
+ * @brief Mass Storage Driver private defines
+ *
+ *
+ * @ingroup MSCFunction
+ */
+
+#ifndef MSC_H
+#define MSC_H 1
+
+extern otg_tag_t msc_fd_trace_tag;
+#define MSC msc_fd_trace_tag
+
+/*
+ * Command/Data/Status Flow
+ * C.f. 5 - Figure 1 
+ */
+
+typedef enum msc_state {
+        MSC_READY,
+        MSC_DATA_OUT_WRITE,
+        MSC_DATA_OUT_WRITE_FINISHED,
+        MSC_DATA_IN_READ,
+        MSC_DATA_IN_READ_FINISHED,
+        MSC_STATUS,
+        MSC_QUERY,
+        MSC_PHASE_ERROR,
+        MSC_UNKNOWN
+} msc_state_t;
+
+
+/*
+ * Device Transfer state
+ * C.F. Table 6.1
+ */
+
+typedef enum msc_device_state {
+        MSC_DEVICE_DN,                  // The device intends to transfer no data
+        MSC_DEVICE_DI,                  // The device intends to send data to the host
+        MSC_DEVICE_DO,                  // The device intents to received data from the host
+} msc_device_state_t;
+
+#define MSC_INACTIVE            0x0000
+#define MSC_BLOCKIO_PENDING     0x0001
+#define MSC_BLOCKIO_FINISHED    0x0002
+#define MSC_RECV_PENDING        0x0010
+#define MSC_RECV_FINISHED       0x0020
+#define MSC_SEND_PENDING        0x0040
+#define MSC_SEND_FINISHED       0x0080
+#define MSC_IOCTL_WAITING       0x0100          // there is an ioctl call waiting for I/O completion
+#define MSC_ABORT_IO            0x0200          // please abort current i/o
+
+#if 0
+struct SPC_inquiry_cdb {
+        u8            OperationCode; /* 12H */
+        u8            EnableVPD:1;
+        u8            CmdSupportData:1;
+        u8            Reserved0:6;
+        u8            PageCode;
+        u8            Reserved1;
+        u8            AllocationLen;
+        u8            Control;
+} __attribute__((packed));
+#endif
+
+struct msc_private {
+        struct usbd_function_instance *function;
+
+        int                     usb_driver_registered;  // non-zero if usb function registered
+        unsigned char           connected;              // non-zero if connected to host (configured)
+
+        struct usbd_urb         *rcv_urb_finished;
+
+        struct buffer_head      read_bh;
+        struct buffer_head      write_bh;
+
+	u16                     read_pending;
+	u16                     write_pending;
+
+        msc_device_state_t      device_state;
+        msc_state_t             command_state;          // current command state
+        u16                     io_state;               // current IO state
+
+        COMMAND_BLOCK_WRAPPER   command;
+
+        u32                     lba;                    // next lba to read/write from
+        u32                     transfer_blocks;
+        u32                     TransferLength_in_blocks;       // amount of transfer remaining
+        u32                     TransferLength_in_bytes;        // amount of transfer remaining
+        u32                     data_transferred_in_bytes;       // amount of data actually transferred
+
+        int                     major;
+        int                     minor;
+        kdev_t                  dev;
+        struct block_device    *bdev;
+        u32                     block_size;
+        u32                     capacity;                      
+        u32                     max_blocks;                      
+
+        int                     uptodate;
+
+        wait_queue_head_t       msc_wq;
+        wait_queue_head_t       ioctl_wq;
+
+        u32                     status;
+        u32                     block_dev_state;
+        u32                     sensedata;
+        u32                     info;
+};
+
+
+/*
+ * MSC Configuration
+ *
+ * Endpoint, Class, Interface, Configuration and Device descriptors/descriptions
+ */
+
+#define BULK_OUT        0x00
+#define BULK_IN         0x01
+#define ENDPOINTS       0x02
+
+extern struct usbd_function_operations function_ops;
+extern struct usbd_function_driver function_driver;
+
+
+#endif
diff -uNr linux/drivers/no-otg/functions/msc/scripts/test1 linux/drivers/otg/functions/msc/scripts/test1
--- linux/drivers/no-otg/functions/msc/scripts/test1	1970-01-01 01:00:00.000000000 +0100
+++ linux/drivers/otg/functions/msc/scripts/test1	2006-09-01 21:41:28.000000000 +0200
@@ -0,0 +1,37 @@
+#!/bin/sh
+#
+# Basic Start/Stop test 
+#  
+#
+# 1. Wait for connection
+# 2. loop making block device available for 10 second intervals 
+# 3. exit if disconnected
+#
+
+MAJOR=7
+MINOR=0
+
+TIMEOUT=5
+
+set -x
+
+# wait for connection
+msc_check connect
+
+while sleep 1
+do
+
+        # make block device available
+        msc_check start $MAJOR $MINOR
+        
+        sleep $TIMEOUT
+
+        # make block device un-available
+        msc_check stop 
+
+        sleep $TIMEOUT
+
+        msc_check connected || break
+
+done
+
diff -uNr linux/drivers/no-otg/functions/msc/scripts/test2 linux/drivers/otg/functions/msc/scripts/test2
--- linux/drivers/no-otg/functions/msc/scripts/test2	1970-01-01 01:00:00.000000000 +0100
+++ linux/drivers/otg/functions/msc/scripts/test2	2006-09-01 21:41:28.000000000 +0200
@@ -0,0 +1,35 @@
+#!/bin/sh
+#
+# Basic Start/Stop test on connect/disconnect.
+#
+# Loop
+#       1. wait for connection
+#       2. making block device available for 10 seconds
+#       3. wait for disconnect 
+#       4. continue
+#
+
+MAJOR=7
+MINOR=0
+
+TIMEOUT=5
+
+set -x
+
+while sleep 1
+do
+        # wait for connection
+        msc_check connect
+
+        # make block device available
+        msc_check start $MAJOR $MINOR
+        
+        sleep $TIMEOUT
+
+        # make block device un-available
+        msc_check stop 
+
+        msc_check disconnect
+
+done
+
diff -uNr linux/drivers/no-otg/functions/msc/scripts/test3 linux/drivers/otg/functions/msc/scripts/test3
--- linux/drivers/no-otg/functions/msc/scripts/test3	1970-01-01 01:00:00.000000000 +0100
+++ linux/drivers/otg/functions/msc/scripts/test3	2006-09-01 21:41:28.000000000 +0200
@@ -0,0 +1,46 @@
+#!/bin/sh
+#
+# Force connect/disconnect with start/stop.
+#
+# 1. loop making block device available for 10 second intervals 
+# 2. exit if disconnected
+# 3. start
+# 4. stop
+# 5. force disconnect
+#
+
+MAJOR=7
+MINOR=0
+
+TIMEOUT=5
+
+set -x
+
+while sleep 1
+do
+
+        # bus request
+        otgd bus_req
+
+        # wait for connection
+        msc_check connect
+
+        # make block device available
+        msc_check start $MAJOR $MINOR
+        
+        sleep $TIMEOUT
+
+        # make block device un-available
+        msc_check stop 
+
+        sleep $TIMEOUT
+
+        # bus request/
+        otgd bus_req/
+
+        msc_check disconnect 
+
+        sleep $TIMEOUT
+
+done
+
diff -uNr linux/drivers/no-otg/functions/msc/scripts/test4 linux/drivers/otg/functions/msc/scripts/test4
--- linux/drivers/no-otg/functions/msc/scripts/test4	1970-01-01 01:00:00.000000000 +0100
+++ linux/drivers/otg/functions/msc/scripts/test4	2006-09-01 21:41:28.000000000 +0200
@@ -0,0 +1,47 @@
+#!/bin/sh
+
+# Force connect/disconnect without start/stop.
+#
+#
+# 1. loop making block device available for 10 second intervals 
+# 2. exit if disconnected
+# 3. force disconnect
+# 4. force connect
+#
+
+MAJOR=7
+MINOR=0
+
+TIMEOUT=5
+
+set -x
+
+# make block device available
+msc_check start $MAJOR $MINOR
+
+
+while sleep 1
+do
+
+        # bus request
+        otgd bus_req
+
+        # wait for connection
+        msc_check connect
+
+        sleep $TIMEOUT
+
+        sleep $TIMEOUT
+
+        # bus request/
+        otgd bus_req/
+
+        msc_check disconnect 
+
+        sleep $TIMEOUT
+
+done
+
+# make block device un-available
+msc_check stop 
+
diff -uNr linux/drivers/no-otg/functions/msc/scripts/test5 linux/drivers/otg/functions/msc/scripts/test5
--- linux/drivers/no-otg/functions/msc/scripts/test5	1970-01-01 01:00:00.000000000 +0100
+++ linux/drivers/otg/functions/msc/scripts/test5	2006-09-01 21:41:28.000000000 +0200
@@ -0,0 +1,47 @@
+#!/bin/sh
+#
+# Force connect/disconnect with start/stop.
+#
+# 1. loop making block device available for 10 second intervals 
+# 2. exit if disconnected
+# 3. start
+# 4. force disconnect
+# 5. stop
+# 6. force connect
+#
+
+MAJOR=7
+MINOR=0
+
+TIMEOUT=5
+
+set -x
+
+while sleep 1
+do
+
+        # bus request
+        otgd bus_req
+
+        # wait for connection
+        msc_check connect
+
+        # make block device available
+        msc_check start $MAJOR $MINOR
+        
+        sleep $TIMEOUT
+
+        # bus request/
+        otgd bus_req/
+
+        # make block device un-available
+        msc_check stop 
+
+        sleep $TIMEOUT
+
+        msc_check disconnect 
+
+        sleep $TIMEOUT
+
+done
+
diff -uNr linux/drivers/no-otg/functions/msc/trace.c linux/drivers/otg/functions/msc/trace.c
--- linux/drivers/no-otg/functions/msc/trace.c	1970-01-01 01:00:00.000000000 +0100
+++ linux/drivers/otg/functions/msc/trace.c	2006-09-01 21:41:28.000000000 +0200
@@ -0,0 +1,340 @@
+/*
+ * otg/msc_fd/trace.c
+ *
+ *      Copyright (c) 2004 Belcarra
+ *
+ * Adapted from earlier work:
+ *      Copyright (c) 2002, 2003 Belcarra
+ *      Copyright (c) 2002 Lineo
+ *
+ * By: 
+ *      Stuart Lynne <sl@belcarra.com>, 
+ *      Tom Rushworth <tbr@belcarra.com>, 
+ *
+ */
+
+#include <linux/config.h>
+#include <linux/module.h>
+#include <linux/version.h>
+
+#include <linux/kernel.h>
+#include <linux/slab.h>
+#include <linux/interrupt.h>
+#include <linux/pci.h>
+#include <linux/init.h>
+#include <linux/delay.h>
+
+#include <linux/proc_fs.h>
+#include <linux/vmalloc.h>
+
+#include <asm/atomic.h>
+#include <asm/io.h>
+
+#include <linux/proc_fs.h>
+
+#include <linux/netdevice.h>
+#include <linux/pci.h>
+#include <linux/cache.h>
+
+
+
+#include <asm/uaccess.h>
+#include <asm/io.h>
+#include <asm/pgtable.h>
+#include <asm/pgalloc.h>
+
+#include <usbp-chap9.h>
+#include <usbp-mem.h>
+#include <usbp-func.h>
+#include "msc-scsi.h"
+#include "msc.h"
+#include "trace.h"
+
+
+static struct msc_private *msc_private;
+int msc_trace_first;
+int msc_trace_next;
+msc_trace_t *msc_traces;
+
+extern int msc_interrupts;
+
+
+#if defined(CONFIG_OTG_MSC_REGISTER_TRACE) && defined(CONFIG_PROC_FS)
+
+msc_trace_t *MSC_TRACE_NEXT(msc_trace_types_t msc_trace_type)
+{
+        msc_trace_t *p;
+
+        p = msc_traces + msc_trace_next;
+
+        if (msc_private) {
+                p->ticks = usbd_ticks(msc_private->function);
+                p->sofs = usbd_framenum(msc_private->function);
+        }
+        p->interrupts = msc_interrupts;
+        p->msc_trace_type = msc_trace_type;
+
+        msc_trace_next++;
+        msc_trace_next = (msc_trace_next == TRACE_MAX) ? 0 : msc_trace_next;
+
+        if (msc_trace_next == msc_trace_first) {
+                msc_trace_first++;
+                msc_trace_first = (msc_trace_first == TRACE_MAX) ? 0 : msc_trace_first;
+        }
+
+        return p;
+}
+
+/* Proc Filesystem *************************************************************************** */
+        
+/* *    
+ * msc_trace_proc_read - implement proc file system read.
+ * @file        
+ * @buf         
+ * @count
+ * @pos 
+ *      
+ * Standard proc file system read function.
+ */         
+static ssize_t msc_trace_proc_read (struct file *file, char *buf, size_t count, loff_t * pos)
+{                                  
+        unsigned long page;
+        int len = 0;
+        int index;
+        int oindex;
+        int previous;
+
+        MOD_INC_USE_COUNT;
+        // get a page, max 4095 bytes of data...
+        if (!(page = get_free_page (GFP_KERNEL))) {
+                MOD_DEC_USE_COUNT;
+                return -ENOMEM;
+        }
+
+        len = 0;
+        oindex = index = (*pos)++;
+
+        if (index == 0) 
+                len += sprintf ((char *) page + len, " Index     Ints     Ticks\n");
+               
+                         
+        index += msc_trace_first;
+        if (index >= TRACE_MAX) 
+                index -= TRACE_MAX;
+        
+        previous = (index) ? (index - 1) : (TRACE_MAX - 1);
+
+
+        if (
+                        ((msc_trace_first < msc_trace_next) && (index >= msc_trace_first) && (index < msc_trace_next)) ||
+                        ((msc_trace_first > msc_trace_next) && ((index < msc_trace_next) || (index >= msc_trace_first)))
+           )
+        {
+
+                u64 ticks = 0;
+
+                msc_trace_t *p = msc_traces + index;
+                unsigned char *cp;
+                unsigned int *ip;
+                int skip = 0;
+
+                if (oindex > 0) {
+                        msc_trace_t *o = msc_traces + previous;
+
+                        if (o->ticks) 
+                                ticks = (p->ticks > o->ticks) ? (p->ticks - o->ticks) : (o->ticks - p->ticks) ;
+
+                        if (o->interrupts != p->interrupts) 
+                                skip++;
+                        
+                }
+                
+                //printk(KERN_INFO"index: %d interrupts: %d\n", index, p->interrupts);
+                len += sprintf ((char *) page + len, "%s%6d %8d ", skip?"\n":"", index, p->interrupts);
+
+                if (ticks > 1024*1024) 
+                        len += sprintf ((char *) page + len, "%8dM ", ticks>>20);
+                else 
+                        len += sprintf ((char *) page + len, "%8d  ", ticks);
+                
+                len += sprintf ((char *) page + len, "%6d  ", (int)p->sofs);
+
+                switch (p->msc_trace_type) {
+                case msc_trace_msg:
+                        len += sprintf ((char *) page + len, " --                   ");
+                        len += sprintf ((char *) page + len, p->trace.msg.msg);
+                        break;
+
+                case msc_trace_w:
+                        len += sprintf ((char *) page + len, " -->                  ");
+                        len += sprintf ((char *) page + len, "[%8x] W %s", p->trace.msg32.val, p->trace.msg32.msg);
+                        break;
+
+                case msc_trace_r:
+                        len += sprintf ((char *) page + len, "<--                   ");
+                        len += sprintf ((char *) page + len, "[%8x] R %s", p->trace.msg32.val, p->trace.msg32.msg);
+                        break;
+
+                case msc_trace_msg32:
+                        len += sprintf ((char *) page + len, " --                   ");
+                        len += sprintf ((char *) page + len, p->trace.msg32.msg, p->trace.msg32.val);
+                        break;
+
+                case msc_trace_msg16:
+                        len += sprintf ((char *) page + len, " --                   ");
+                        len += sprintf ((char *) page + len, p->trace.msg16.msg, p->trace.msg16.val0, p->trace.msg16.val1);
+                        break;
+
+                case msc_trace_msg8:
+                        len += sprintf ((char *) page + len, " --                   ");
+                        len += sprintf ((char *) page + len, p->trace.msg8.msg, 
+                                        p->trace.msg8.val0, p->trace.msg8.val1, p->trace.msg8.val2, p->trace.msg8.val3);
+                        break;
+
+                case msc_trace_setup:
+                        cp = (unsigned char *)&p->trace.setup;
+                        len += sprintf ((char *) page + len, 
+                                        " --           request [%02x %02x %02x %02x %02x %02x %02x %02x]", 
+                                        cp[0], cp[1], cp[2], cp[3], cp[4], cp[5], cp[6], cp[7]);
+                        break;
+
+                case msc_trace_recv:
+                case msc_trace_sent:
+                        cp = (unsigned char *)&p->trace.sent;
+                        len += sprintf ((char *) page + len, 
+                                        "%s             %s [%02x %02x %02x %02x %02x %02x %02x %02x]", 
+                                        ( p->msc_trace_type == msc_trace_recv)?"<-- ":" -->",
+                                        ( p->msc_trace_type == msc_trace_recv)?"recv":"sent",
+                                        cp[0], cp[1], cp[2], cp[3], cp[4], cp[5], cp[6], cp[7]);
+                        break;
+                case msc_trace_rlba:
+                        ip = (unsigned int *)&p->trace.ints;
+                        len += sprintf ((char *) page + len, 
+                                        "%s             %s [%8x %08x]", 
+                                        "<-- ", "rlba", ip[0], ip[1]);
+                        break;
+                case msc_trace_slba:
+                case msc_trace_tlba:
+                        ip = (unsigned int *)&p->trace.ints;
+                        len += sprintf ((char *) page + len, 
+                                        "%s             %s [%8x %08x]", " -->",
+                                        ( p->msc_trace_type == msc_trace_tlba)?"tlba":"slba",
+                                        ip[0], ip[1]);
+                        break;
+
+                case msc_trace_tag:
+                        ip = (unsigned int *)&p->trace.ints;
+                        len += sprintf ((char *) page + len, 
+                                        "%s             TAG: %8x FRAME: %03x", " -->",
+                                        ip[0], ip[1]);
+                        break;
+
+                case msc_trace_sense:
+                        ip = (unsigned int *)&p->trace.ints;
+                        len += sprintf ((char *) page + len, 
+                                        "%s             SENSE: %06x INFO: %08x", " -->",
+                                        ip[0], ip[1]);
+                        break;
+
+                case msc_trace_cbw:
+                        len += sprintf ((char *) page + len, " -->                  ");
+                        len += sprintf ((char *) page + len, "%s %02x", p->trace.msg32.msg, p->trace.msg32.val);
+                        break;
+                }
+                len += sprintf ((char *) page + len, "\n");
+        }
+
+        if ((len > count) || (len == 0))
+                len = -EINVAL;
+        else if (len > 0 && copy_to_user (buf, (char *) page, len)) 
+                len = -EFAULT;
+        
+        free_page (page);
+        MOD_DEC_USE_COUNT;
+        return len;
+}
+
+/* *
+ * msc_trace_proc_write - implement proc file system write.
+ * @file
+ * @buf
+ * @count
+ * @pos
+ *
+ * Proc file system write function, used to signal monitor actions complete.
+ * (Hotplug script (or whatever) writes to the file to signal the completion
+ * of the script.)  An ugly hack.
+ */
+static ssize_t msc_trace_proc_write (struct file *file, const char *buf, size_t count, loff_t * pos)
+{
+        return count;
+}
+
+static struct file_operations msc_trace_proc_operations_functions = {
+        read:msc_trace_proc_read,
+        write:msc_trace_proc_write,
+};
+
+
+/**
+ * msc_trace_init 
+ *
+ * Return non-zero if not successful.
+ */
+int msc_trace_init (char *name, struct msc_private *msc)
+{
+        printk(KERN_INFO"%s:\n", __FUNCTION__);
+        if (!(msc_traces = vmalloc(sizeof(msc_trace_t) * TRACE_MAX))) {
+                printk(KERN_ERR"%s: malloc failed %p %d\n", __FUNCTION__, msc_traces, sizeof(msc_trace_t) * TRACE_MAX);
+                return -EINVAL;
+        }
+        memset(msc_traces, 0, sizeof(msc_trace_t) * TRACE_MAX);
+
+        {
+                struct proc_dir_entry *p;
+
+                // create proc filesystem entries
+                if ((p = create_proc_entry (name, 0, 0)) == NULL) {
+                        printk(KERN_INFO"BITRACE PROC FS failed\n");
+                }
+                else {
+                        p->proc_fops = &msc_trace_proc_operations_functions;
+                }
+        }
+        printk(KERN_INFO"%s: OK\n", __FUNCTION__);
+        msc_private = msc;
+        return 0;
+}
+
+/**
+ * udc_release_io - release UDC io region
+ */
+void msc_trace_exit (char *name)
+{
+        msc_private = NULL;
+        {
+                unsigned long flags;
+                local_irq_save (flags);
+                remove_proc_entry (name, NULL);
+                if (msc_traces) {
+                        msc_trace_t *p = msc_traces;
+                        msc_traces = NULL;
+                        vfree(p);
+                }
+                local_irq_restore (flags);
+        }
+}
+
+
+#else
+int msc_trace_init (void)
+{
+        return 0;
+}
+
+void msc_trace_exit (char *) 
+{
+        return;
+}
+#endif
+
diff -uNr linux/drivers/no-otg/functions/msc/trace.h linux/drivers/otg/functions/msc/trace.h
--- linux/drivers/no-otg/functions/msc/trace.h	1970-01-01 01:00:00.000000000 +0100
+++ linux/drivers/otg/functions/msc/trace.h	2006-09-01 21:41:28.000000000 +0200
@@ -0,0 +1,303 @@
+/*
+ * otg/msc_fd/trace.h
+ *
+ *      Copyright (c) 2004 Belcarra
+ *
+ * Adapted from earlier work:
+ *      Copyright (c) 2002, 2003 Belcarra
+ *      Copyright (c) 2002 Lineo
+ *
+ * By: 
+ *      Stuart Lynne <sl@belcarra.com>, 
+ *      Tom Rushworth <tbr@belcarra.com>, 
+ *
+ *
+ */
+
+
+typedef enum msc_trace_int_types {
+        msc_trace_int_udc, msc_trace_int_ep0, msc_trace_int_in, msc_trace_int_out, msc_trace_int_int
+} msc_trace_int_types_t;
+
+typedef struct msc_trace_int {
+        u32     last;
+        u32     total;
+        u32     samples;
+} msc_trace_int_t;
+
+
+typedef enum msc_trace_types {
+        msc_trace_setup, msc_trace_msg, msc_trace_msg32, msc_trace_msg16, 
+        msc_trace_msg8, msc_trace_recv, msc_trace_sent, msc_trace_w, msc_trace_r, 
+        msc_trace_rlba, msc_trace_slba, msc_trace_tlba, msc_trace_tag, 
+        msc_trace_sense, msc_trace_cbw
+} msc_trace_types_t;
+
+
+typedef struct msc_trace_regs32 {
+        u32     reg;
+        char *  msg;
+} msc_trace_regs32_t;
+
+
+typedef struct msc_trace_msg {
+        char    *msg;
+} msc_trace_msg_t;
+
+typedef struct msc_trace_msg32 {
+        u32      val;
+        char    *msg;
+} msc_trace_msg32_t;
+
+typedef struct msc_trace_msg16 {
+        u16     val0;
+        u16     val1;
+        char    *msg;
+} msc_trace_msg16_t;
+
+typedef struct msc_trace_msg8 {
+        u8      val0;
+        u8      val1;
+        u8      val2;
+        u8      val3;
+        char    *msg;
+} msc_trace_msg8_t;
+
+
+typedef struct trace {
+        msc_trace_types_t        msc_trace_type;
+        char    *function;
+        u32     interrupts;
+        u64     ticks;
+        u64     sofs;
+
+        union {
+                msc_trace_msg_t        msg;
+                msc_trace_msg8_t       msg8;
+                msc_trace_msg16_t      msg16;
+                msc_trace_msg32_t      msg32;
+
+                struct usbd_device_request       setup;
+                unsigned char      recv[8];
+                unsigned char      sent[8];
+
+                unsigned int       ints[2];
+
+        } trace;
+
+} msc_trace_t;
+
+
+#define TRACE_MAX       30000
+
+extern int msc_trace_first;
+extern int msc_trace_next;
+
+extern msc_trace_int_t *msc_trace_ints;
+extern msc_trace_t *msc_traces;
+
+#ifdef CONFIG_OTG_MSC_REGISTER_TRACE
+
+msc_trace_t *MSC_TRACE_NEXT(msc_trace_types_t msc_trace_type);
+
+static __inline__ void TRACE_SETUP(struct usbd_device_request *setup)
+{
+        if (msc_traces) {
+                msc_trace_t *p = MSC_TRACE_NEXT(msc_trace_setup);
+                p->msc_trace_type = msc_trace_setup;
+                memcpy(&p->trace.setup, setup, sizeof(struct usbd_device_request));
+        }
+}
+
+static __inline__ void TRACE_MSG(char *msg)
+{
+        if (msc_traces) {
+                msc_trace_t *p = MSC_TRACE_NEXT(msc_trace_msg);
+                p->trace.msg.msg = msg;
+        }
+}
+
+static __inline__ void TRACE_W(char *msg, u32 val)
+{
+        if (msc_traces) {
+                msc_trace_t *p = MSC_TRACE_NEXT(msc_trace_w);
+                p->trace.msg32.val = val;
+                p->trace.msg32.msg = msg;
+        }
+}
+
+static __inline__ void TRACE_R(char *msg, u32 val)
+{
+        if (msc_traces) {
+                msc_trace_t *p = MSC_TRACE_NEXT(msc_trace_r);
+                p->trace.msg32.val = val;
+                p->trace.msg32.msg = msg;
+        }
+}
+
+static __inline__ void TRACE_MSG32(char *msg, u32 val)
+{
+        if (msc_traces) {
+                msc_trace_t *p = MSC_TRACE_NEXT(msc_trace_msg32);
+                p->trace.msg32.val = val;
+                p->trace.msg32.msg = msg;
+        }
+}
+
+static __inline__ void TRACE_MSG16(char *msg, u16 val0, u16 val1)
+{
+        if (msc_traces) {
+                msc_trace_t *p = MSC_TRACE_NEXT(msc_trace_msg16);
+                p->trace.msg16.val0 = val0;
+                p->trace.msg16.val1 = val1;
+                p->trace.msg16.msg = msg;
+        }
+}
+
+static __inline__ void TRACE_MSG8(char *msg, u8 val0, u8 val1, u8 val2, u8 val3)
+{
+        if (msc_traces) {
+                msc_trace_t *p = MSC_TRACE_NEXT(msc_trace_msg8);
+                p->trace.msg8.val0 = val0;
+                p->trace.msg8.val1 = val1;
+                p->trace.msg8.val2 = val2;
+                p->trace.msg8.val3 = val3;
+                p->trace.msg8.msg = msg;
+        }
+}
+
+static __inline__ void TRACE_RECV(unsigned char *cp)
+{
+        if (msc_traces) {
+                msc_trace_t *p = MSC_TRACE_NEXT(msc_trace_recv);
+                memcpy(&p->trace.recv, cp, 8);
+        }
+}
+
+static __inline__ void TRACE_RECVN(unsigned char *cp, int bytes)
+{
+        if (msc_traces) {
+                msc_trace_t *p = MSC_TRACE_NEXT(msc_trace_recv);
+                memset(&p->trace.recv, 0, 8);
+                memcpy(&p->trace.recv, cp, bytes);
+        }
+}
+
+static __inline__ void TRACE_SENT(unsigned char *cp)
+{
+        if (msc_traces) {
+                msc_trace_t *p = MSC_TRACE_NEXT(msc_trace_sent);
+                memcpy(&p->trace.sent, cp, 8);
+        }
+}
+
+
+static __inline__ void TRACE_RLBA(unsigned int lba, unsigned int crc)
+{
+        if (msc_traces) {
+                msc_trace_t *p = MSC_TRACE_NEXT(msc_trace_rlba);
+                p->trace.ints[0] = lba;
+                p->trace.ints[1] = crc;
+        }
+}
+
+static __inline__ void TRACE_SLBA(unsigned int lba, unsigned int crc)
+{
+        if (msc_traces) {
+                msc_trace_t *p = MSC_TRACE_NEXT(msc_trace_slba);
+                p->trace.ints[0] = lba;
+                p->trace.ints[1] = crc;
+        }
+}
+
+static __inline__ void TRACE_TLBA(unsigned int lba, unsigned int crc)
+{
+        if (msc_traces) {
+                msc_trace_t *p = MSC_TRACE_NEXT(msc_trace_tlba);
+                p->trace.ints[0] = lba;
+                p->trace.ints[1] = crc;
+        }
+}
+
+static __inline__ void TRACE_TAG(unsigned int tag, unsigned int frame)
+{
+        if (msc_traces) {
+                msc_trace_t *p = MSC_TRACE_NEXT(msc_trace_tag);
+                p->trace.ints[0] = tag;
+                p->trace.ints[1] = frame;
+        }
+}
+
+static __inline__ void TRACE_SENSE(unsigned int sense, unsigned int info)
+{
+        if (msc_traces) {
+                msc_trace_t *p = MSC_TRACE_NEXT(msc_trace_sense);
+                p->trace.ints[0] = sense;
+                p->trace.ints[1] = info;
+        }
+}
+
+static __inline__ void TRACE_CBW(char *msg, int val)
+{
+        if (msc_traces) {
+                msc_trace_t *p = MSC_TRACE_NEXT(msc_trace_cbw);
+                p->trace.msg32.val = val;
+                p->trace.msg32.msg = msg;
+        }
+}
+
+#else
+
+static __inline__ void TRACE_SETUP(struct usbd_device_request *setup)
+{
+}
+
+static __inline__ void TRACE_IRQS(u32 cr, u32 sr)
+{
+}
+
+static __inline__ void TRACE_RECV(unsigned char *cp)
+{
+}
+
+static __inline__ void TRACE_SENT(unsigned char *cp)
+{
+}
+
+static __inline__ void TRACE_W(char *msg, u32 val)
+{
+}
+
+static __inline__ void TRACE_R(char *msg, u32 val)
+{
+}
+
+static __inline__ void TRACE_MSG(char *msg)
+{
+}
+
+static __inline__ void TRACE_MSG32(char *msg, u32 val)
+{
+}
+
+static __inline__ void TRACE_MSG16(char *msg, u16 val0, u16 val1)
+{
+}
+
+static __inline__ void TRACE_TLBA(char *msg, u16 val0, u16 val1)
+{
+}
+
+static __inline__ void TRACE_TAG(char *msg, u16 val0, u16 val1)
+{
+}
+
+static __inline__ void TRACE_SENSE(char *msg, u8 val0, u8 val1, u8 val2, u8 vale)
+{
+}
+
+#endif
+
+int msc_trace_init (char *str, struct msc_private *);
+void msc_trace_exit (char *str);
+
diff -uNr linux/drivers/no-otg/functions/network/Config.in linux/drivers/otg/functions/network/Config.in
--- linux/drivers/no-otg/functions/network/Config.in	1970-01-01 01:00:00.000000000 +0100
+++ linux/drivers/otg/functions/network/Config.in	2006-09-01 21:41:28.000000000 +0200
@@ -0,0 +1,70 @@
+#
+# Network Function
+#
+# Copyright (C) 2002-2004 Belcarra
+#
+
+mainmenu_option next_comment
+comment "USB Peripheral Function Driver - Network"
+
+dep_tristate '  Network Function Driver' CONFIG_OTG_NETWORK $CONFIG_OTG
+
+if [ "$CONFIG_OTG_NETWORK" != "n" ]; then
+    
+    hex     'VendorID (hex value)'             CONFIG_OTG_NETWORK_VENDORID "15ec"
+    hex     'ProductID (hex value)'            CONFIG_OTG_NETWORK_PRODUCTID "e003"
+    hex     'bcdDevice (binary-coded decimal)' CONFIG_OTG_NETWORK_BCDDEVICE "0100"
+    
+    string 'iManufacturer (string)' CONFIG_OTG_NETWORK_MANUFACTURER "Belcarra"
+    string 'iProduct (string)'      CONFIG_OTG_NETWORK_PRODUCT_NAME "Belcarra BLAN Device"
+    
+    choice "Mode" "\
+            MDLM-BLAN-Networking-for-personal-devices  CONFIG_OTG_NETWORK_BLAN \
+            MDLM-SAFE-Networking-for-Bridge-Routers CONFIG_OTG_NETWORK_SAFE \
+            CDC-Networking-for-Bridge-Routers CONFIG_OTG_NETWORK_CDC \
+            Failsafe-BASIC-Networking-testing-only CONFIG_OTG_NETWORK_BASIC \
+            Failsafe-BASIC2-Networking-testing-only CONFIG_OTG_NETWORK_BASIC2 \
+            Experimental-Ethernet-Mode-Networking-testing-only CONFIG_OTG_NETWORK_EEM \
+            " MDLM-BLAN-Networking-for-personal-devices
+    
+    if [ "$CONFIG_OTG_NETWORK_BLAN" = "y" ]; then
+        source drivers/otg/functions/network/Config.in-blan
+    fi
+
+    if [ "$CONFIG_OTG_NETWORK_SAFE" = "y" ]; then
+        source drivers/otg/functions/network/Config.in-safe
+    fi
+
+    if [ "$CONFIG_OTG_NETWORK_CDC" = "y" ]; then
+        source drivers/otg/functions/network/Config.in-cdc
+    fi
+
+    if [ "$CONFIG_OTG_NETWORK_BASIC" = "y" ]; then
+        source drivers/otg/functions/network/Config.in-basic
+    fi
+
+    if [ "$CONFIG_OTG_NETWORK_BASIC2" = "y" ]; then
+        source drivers/otg/functions/network/Config.in-basic2
+    fi
+    
+    if [ "$CONFIG_OTG_NETWORK_EEM" = "y" ]; then
+        source drivers/otg/functions/network/Config.in-eem
+    fi
+    
+    comment ''
+    comment 'Testing Options'
+    bool    'Start Single Urb Test' CONFIG_OTG_NETWORK_START_SINGLE
+    bool    'EP0 Test' CONFIG_OTG_NETWORK_EP0TEST
+    dep_bool    "Hotplug-config" CONFIG_OTG_NETWORK_HOTPLUG $CONFIG_HOTPLUG
+
+else
+    define_bool CONFIG_OTG_NETWORK_BLAN n
+    define_bool CONFIG_OTG_NETWORK_SAFE n
+    define_bool CONFIG_OTG_NETWORK_CDC n
+    define_bool CONFIG_OTG_NETWORK_BASIC n
+    define_bool CONFIG_OTG_NETWORK_BASIC2 n
+    define_bool CONFIG_OTG_NETWORK_EEM n
+fi
+
+endmenu
+
diff -uNr linux/drivers/no-otg/functions/network/Config.in-basic linux/drivers/otg/functions/network/Config.in-basic
--- linux/drivers/no-otg/functions/network/Config.in-basic	1970-01-01 01:00:00.000000000 +0100
+++ linux/drivers/otg/functions/network/Config.in-basic	2006-09-01 21:41:28.000000000 +0200
@@ -0,0 +1,17 @@
+#
+# Network Function
+#
+# Copyright (C) 2002-2003 Belcarra
+#
+
+mainmenu_option next_comment
+comment "BASIC Networking Configuration"
+
+    #bool " Failsafe BASIC Networking mode" CONFIG_OTG_NETWORK_BASIC
+    if [ "$CONFIG_OTG_NETWORK_BASIC" = "y" ]; then
+        comment 'BASIC Configuration'
+        string 'Data Interface iConfiguration (string)' CONFIG_OTG_NETWORK_BASIC_DESC "BASIC Net Cfg"
+        string 'Data Interface iInterface (string)'     CONFIG_OTG_NETWORK_BASIC_INTF "Data Intf"
+    fi
+
+endmenu
diff -uNr linux/drivers/no-otg/functions/network/Config.in-basic2 linux/drivers/otg/functions/network/Config.in-basic2
--- linux/drivers/no-otg/functions/network/Config.in-basic2	1970-01-01 01:00:00.000000000 +0100
+++ linux/drivers/otg/functions/network/Config.in-basic2	2006-09-01 21:41:28.000000000 +0200
@@ -0,0 +1,13 @@
+#
+# Network Function
+#
+# Copyright (C) 2002-2003 Belcarra
+#
+
+mainmenu_option next_comment
+    comment "BASIC2 Networking Configuration"
+    string 'Data Interface iConfiguration (string)' CONFIG_OTG_NETWORK_BASIC2_DESC "BASIC Net Cfg"
+    string 'Comm Interface iInterface (string)'     CONFIG_OTG_NETWORK_BASIC2_COMM_INTF "Comm Intf"
+    string 'Data Interface iInterface (string)'     CONFIG_OTG_NETWORK_BASIC2_DATA_INTF "Data Intf"
+    
+endmenu
diff -uNr linux/drivers/no-otg/functions/network/Config.in-blan linux/drivers/otg/functions/network/Config.in-blan
--- linux/drivers/no-otg/functions/network/Config.in-blan	1970-01-01 01:00:00.000000000 +0100
+++ linux/drivers/otg/functions/network/Config.in-blan	2006-09-01 21:41:28.000000000 +0200
@@ -0,0 +1,38 @@
+#
+# Network Function
+#
+# Copyright (C) 2002-2003 Belcarra
+#
+
+mainmenu_option next_comment
+    comment "MBLM-BLAN Networking Configuration"
+
+    string 'iConfiguration (string)' CONFIG_OTG_NETWORK_BLAN_DESC "BLAN Net Cfg"
+    string 'iInterface (string)'     CONFIG_OTG_NETWORK_BLAN_INTF "Comm/Data Intf"
+
+
+    comment 'Special Framing Options'
+    bool "CRC"  CONFIG_OTG_NETWORK_BLAN_CRC
+    if [ "$CONFIG_OTG_NETWORK_BLAN_CRC" = "y" ]; then
+
+        bool "Pad Before CRC (to wMaxPacketSize-1)"  CONFIG_OTG_NETWORK_BLAN_PADBEFORE
+        bool "Pad After CRC"  CONFIG_OTG_NETWORK_BLAN_PADAFTER
+        if [ "$CONFIG_OTG_NETWORK_BLAN" = "y" -a "$CONFIG_OTG_NETWORK_BLAN_PADAFTER" = "y" ]; then
+             int 'Pad multiple' CONFIG_OTG_NETWORK_BLAN_PADBYTES "8"
+        fi
+        bool "Fermat Randomizer"  CONFIG_OTG_NETWORK_BLAN_FERMAT
+    fi
+
+    comment 'USBLAN Options'
+    bool "Do not Set Time"  CONFIG_OTG_NETWORK_BLAN_DO_NOT_SETTIME
+    bool "Request Hostname"  CONFIG_OTG_NETWORK_BLAN_HOSTNAME
+    bool "Infrastructure Device"  CONFIG_OTG_NETWORK_BLAN_NONBRIDGED
+    bool "Data Notification OK"  CONFIG_OTG_NETWORK_BLAN_DATA_NOTIFY_OK
+    int  'Pad multiple' CONFIG_OTG_NETWORK_BLAN_PADBYTES "8"
+    int  'Polling Interval ' CONFIG_OTG_NETWORK_BLAN_INTERVAL "4"
+    bool "Set iProduct from IPADDR" CONFIG_OTG_NETWORK_BLAN_IPADDR
+    bool "Auto-config" CONFIG_OTG_NETWORK_BLAN_AUTO_CONFIG
+    comment ''
+
+
+endmenu
diff -uNr linux/drivers/no-otg/functions/network/Config.in-cdc linux/drivers/otg/functions/network/Config.in-cdc
--- linux/drivers/no-otg/functions/network/Config.in-cdc	1970-01-01 01:00:00.000000000 +0100
+++ linux/drivers/otg/functions/network/Config.in-cdc	2006-09-01 21:41:28.000000000 +0200
@@ -0,0 +1,16 @@
+#
+# Network Function
+#
+# Copyright (C) 2002-2003 Belcarra
+#
+
+mainmenu_option next_comment
+    comment "CDC Networking Configuration"
+
+    string 'iConfiguration (string)'            CONFIG_OTG_NETWORK_CDC_DESC "SAFE Net Cfg"
+    string 'Comm Interface iInterface (string)' CONFIG_OTG_NETWORK_CDC_COMM_INTF "Comm Intf"
+    string 'Data (diabled) iInterface (string)' CONFIG_OTG_NETWORK_CDC_NODATA_INTF "Data (Disabled) Intf"
+    string 'Data Interface iInterface (string)' CONFIG_OTG_NETWORK_CDC_DATA_INTF "Data Intf"
+      
+
+endmenu
diff -uNr linux/drivers/no-otg/functions/network/Config.in-eem linux/drivers/otg/functions/network/Config.in-eem
--- linux/drivers/no-otg/functions/network/Config.in-eem	1970-01-01 01:00:00.000000000 +0100
+++ linux/drivers/otg/functions/network/Config.in-eem	2006-09-01 21:41:28.000000000 +0200
@@ -0,0 +1,17 @@
+#
+# Network Function
+#
+# Copyright (C) 2002-2003 Belcarra
+#
+
+mainmenu_option next_comment
+comment "EEM Networking Configuration"
+
+    #bool " Experimental Ethernet Mode" CONFIG_OTG_NETWORK_EEM
+    if [ "$CONFIG_OTG_NETWORK_EEM" = "y" ]; then
+        comment 'EEM Configuration'
+        string 'Data Interface iConfiguration (string)' CONFIG_OTG_NETWORK_EEM_DESC "EEM Net Cfg"
+        string 'Data Interface iInterface (string)'     CONFIG_OTG_NETWORK_EEM_INTF "Data Intf"
+    fi
+
+endmenu
diff -uNr linux/drivers/no-otg/functions/network/Config.in-safe linux/drivers/otg/functions/network/Config.in-safe
--- linux/drivers/no-otg/functions/network/Config.in-safe	1970-01-01 01:00:00.000000000 +0100
+++ linux/drivers/otg/functions/network/Config.in-safe	2006-09-01 21:41:28.000000000 +0200
@@ -0,0 +1,21 @@
+#
+# Network Function
+#
+# Copyright (C) 2002-2003 Belcarra
+#
+
+mainmenu_option next_comment
+    comment "MDLM-SAFE Networking Configuration"
+
+    string 'Data Interface iConfiguration (string)' CONFIG_OTG_NETWORK_SAFE_DESC "SAFE Net Cfg"
+    string 'Data Interface iInterface (string)'     CONFIG_OTG_NETWORK_SAFE_INTF "Data Intf"
+    bool " Do not Set Time"  CONFIG_OTG_NETWORK_SAFE_DO_NOT_SETTIME
+    bool " CRC"  CONFIG_OTG_NETWORK_SAFE_CRC
+    if [ "$CONFIG_OTG_NETWORK_SAFE_CRC" = "y" ]; then
+        bool "Pad Before CRC (to wMaxPacketSize-1)"  CONFIG_OTG_NETWORK_SAFE_PADBEFORE
+    fi
+    bool "Infrastructure Device"  CONFIG_OTG_NETWORK_SAFE_NONBRIDGED
+    comment ''
+    
+
+endmenu
diff -uNr linux/drivers/no-otg/functions/network/Kconfig linux/drivers/otg/functions/network/Kconfig
--- linux/drivers/no-otg/functions/network/Kconfig	1970-01-01 01:00:00.000000000 +0100
+++ linux/drivers/otg/functions/network/Kconfig	2006-09-01 21:41:28.000000000 +0200
@@ -0,0 +1,131 @@
+menu "OTG Network Function"
+	depends on OTG
+
+config OTG_NETWORK
+        tristate "  Network Function Driver"
+        depends on OTG
+
+menu "OTG Network Function options"
+	depends on OTG_NETWORK
+
+config OTG_NETWORK_VENDORID
+        hex "VendorID (hex value)"
+        depends on OTG && OTG_NETWORK
+        default "0x15ec"
+
+config OTG_NETWORK_PRODUCTID
+        hex "ProductID (hex value)"
+        depends on OTG && OTG_NETWORK
+        default "0xe003"
+
+config OTG_NETWORK_BCDDEVICE
+        hex "bcdDevice (binary-coded decimal)"
+        depends on OTG && OTG_NETWORK
+        default "0x0100"
+
+config OTG_NETWORK_MANUFACTURER
+        string "iManufacturer (string)"
+        depends on OTG && OTG_NETWORK
+        default "Belcarra"
+
+config OTG_NETWORK_PRODUCT_NAME
+        string "iProduct (string)"
+        depends on OTG && OTG_NETWORK
+        default "Belcarra BLAN Device"
+
+config OTG_NETWORK_BLAN_DESC
+        string "    iConfiguration (string)"
+        depends on OTG && OTG_NETWORK
+        default "BLAN Net Cfg"
+
+config OTG_NETWORK_MANUFACTURER
+        string "iManufacturer (string)"
+        depends on OTG && OTG_NETWORK
+        default "Belcarra"
+
+config OTG_NETWORK_PRODUCT_NAME
+        string "iProduct (string)"
+        depends on OTG && OTG_NETWORK
+        default "Belcarra BLAN Device"
+
+config OTG_NETWORK_BLAN_DESC
+        string "    iConfiguration (string)"
+        depends on OTG && OTG_NETWORK
+        default "BLAN Net Cfg"
+
+config OTG_NETWORK_BLAN_INTF
+        string "    iInterface (string)"
+        depends on OTG && OTG_NETWORK
+        default "Comm/Data Intf"
+
+config OTG_NETWORK_BLAN_PADBYTES
+        int "        Pad multiple"
+        default "8"
+
+config OTG_NETWORK_SAFE_DESC
+        string "    Data Interface iConfiguration (string)"
+        depends on OTG && OTG_NETWORK
+        default "SAFE Net Cfg"
+
+config OTG_NETWORK_SAFE_INTF
+        string "    Data Interface iInterface (string)"
+        depends on OTG && OTG_NETWORK
+        default "Data Intf"
+
+config OTG_NETWORK_CDC_DESC
+        string "    iConfiguration (string)"
+        depends on OTG && OTG_NETWORK
+        default "SAFE Net Cfg"
+
+config OTG_NETWORK_CDC_COMM_INTF
+        string "    Comm Interface iInterface (string)"
+        depends on OTG && OTG_NETWORK
+        default "Comm Intf"
+
+config OTG_NETWORK_CDC_NODATA_INTF
+        string "    Data (diabled) iInterface (string)"
+        depends on OTG && OTG_NETWORK
+        default "Data (Disabled) Intf"
+
+config OTG_NETWORK_CDC_DATA_INTF
+        string "    Data Interface iInterface (string)"
+        depends on OTG && OTG_NETWORK
+        default "Data Intf"
+
+config OTG_NETWORK_BASIC_DESC
+        string "    Data Interface iConfiguration (string)"
+        depends on OTG && OTG_NETWORK
+        default "BASIC Net Cfg"
+
+config OTG_NETWORK_BASIC_INTF
+        string "    Data Interface iInterface (string)"
+        depends on OTG && OTG_NETWORK
+        default "Data Intf"
+
+config OTG_NETWORK_BASIC2_DESC
+        string "    Data Interface iConfiguration (string)"
+        depends on OTG && OTG_NETWORK
+        default "BASIC Net Cfg"
+
+config OTG_NETWORK_BASIC2_COMM_INTF
+        string "    Comm Interface iInterface (string)"
+        depends on OTG && OTG_NETWORK
+        default "Comm Intf"
+
+config OTG_NETWORK_BASIC2_DATA_INTF
+        string "    Data Interface iInterface (string)"
+        depends on OTG && OTG_NETWORK
+        default "Data Intf"
+
+config OTG_NETWORK_START_SINGLE
+        bool " Start Single Urb Test"
+        depends on OTG && OTG_NETWORK
+        default n
+
+config OTG_NETWORK_EP0TEST
+        bool " EP0 Test"
+        depends on OTG && OTG_NETWORK
+        default n
+
+endmenu
+endmenu
diff -uNr linux/drivers/no-otg/functions/network/Makefile linux/drivers/otg/functions/network/Makefile
--- linux/drivers/no-otg/functions/network/Makefile	1970-01-01 01:00:00.000000000 +0100
+++ linux/drivers/otg/functions/network/Makefile	2006-09-01 21:41:28.000000000 +0200
@@ -0,0 +1,70 @@
+#
+# Network Function Driver
+#
+# Copyright (C) 2002-2003 Belcarra
+
+
+O_TARGET	:= network_target.o
+list-multi	:= network_fd.o
+
+network_fd-objs	:= net-l24-os.o net-fd.o basic.o basic2.o blan.o cdc.o safe.o fermat.o
+
+# Objects that export symbols.
+export-objs	:= net-l24-os.o net-fd.o
+
+
+# Object file lists.
+
+obj-y		:=
+obj-m		:=
+obj-n		:=
+obj-		:=
+
+# Each configuration option enables a list of files.
+
+obj-$(CONFIG_OTG_NETWORK)   += network_fd.o
+
+# Extract lists of the multi-part drivers.
+# The 'int-*' lists are the intermediate files used to build the multi's.
+
+multi-y		:= $(filter $(list-multi), $(obj-y))
+multi-m		:= $(filter $(list-multi), $(obj-m))
+int-y		:= $(sort $(foreach m, $(multi-y), $($(basename $(m))-objs)))
+int-m		:= $(sort $(foreach m, $(multi-m), $($(basename $(m))-objs)))
+
+# Files that are both resident and modular: remove from modular.
+
+obj-m		:= $(filter-out $(obj-y), $(obj-m))
+int-m		:= $(filter-out $(int-y), $(int-m))
+
+# Translate to Rules.make lists.
+
+O_OBJS		:= $(filter-out $(export-objs), $(obj-y))
+OX_OBJS		:= $(filter     $(export-objs), $(obj-y))
+M_OBJS		:= $(sort $(filter-out $(export-objs), $(obj-m)))
+MX_OBJS		:= $(sort $(filter     $(export-objs), $(obj-m)))
+MI_OBJS		:= $(sort $(filter-out $(export-objs), $(int-m)))
+MIX_OBJS	:= $(sort $(filter     $(export-objs), $(int-m)))
+
+# The global Rules.make.
+
+
+OTG_DIR=$(TOPDIR)/drivers/otg
+NETWORK_DIR=$(OTG_DIR)/functions/network
+include $(TOPDIR)/Rules.make
+EXTRA_CFLAGS += -Wno-unused -Wstrict-prototypes -Wno-format
+EXTRA_CFLAGS += -I$(NETWORK_DIR) -I$(OTG_DIR) 
+EXTRA_CFLAGS_nostdinc += -Wstrict-prototypes -Wno-unused -Wno-format 
+EXTRA_CFLAGS_nostdinc += -I$(NETWORK_DIR) -I$(OTG_DIR) 
+
+# Link rules for multi-part drivers.
+
+network_fd.o: $(network_fd-objs)
+	$(LD) -r -o $@ $(network_fd-objs)
+
+# dependencies:
+
+net-fd.o: net-fd.h net-os.h network.h 
+net-l24-os.o: net-fd.h net-os.h network.h 
+
+
diff -uNr linux/drivers/no-otg/functions/network/Makefile-l26 linux/drivers/otg/functions/network/Makefile-l26
--- linux/drivers/no-otg/functions/network/Makefile-l26	1970-01-01 01:00:00.000000000 +0100
+++ linux/drivers/otg/functions/network/Makefile-l26	2006-09-01 21:41:28.000000000 +0200
@@ -0,0 +1,22 @@
+#
+# Network Function Driver
+#
+# Copyright (C) 2002-2004 Belcarra Technologies Corp
+
+
+OTG=$(TOPDIR)/drivers/otg
+NETWORKD=$(OTG)/functions/network
+OTGCORE_DIR=$(OTG)/otgcore
+USBDCORE_DIR=$(OTG)/usbdcore
+EXTRA_CFLAGS += -I$(NETWORKD) -I$(OTG) -Wno-unused -Wno-format  -I$(USBDCORE_DIR) -I$(OTGCORE_DIR)
+EXTRA_CFLAGS_nostdinc += -I$(NETWORKD) -I$(OTG) -Wno-unused -Wno-format  -I$(USBDCORE_DIR) -I$(OTGCORE_DIR)
+
+network_fd-objs := network.o basic.o basic2.o blan.o cdc.o safe.o fermat.o
+obj-$(CONFIG_OTG_NETWORK) += network_fd.o
+
+
+# Link rules for multi-part drivers.
+
+network_fd.o: $(network_fd-objs)
+	$(LD) -r -o $@ $(network_fd-objs)
+
diff -uNr linux/drivers/no-otg/functions/network/TODO.txt linux/drivers/otg/functions/network/TODO.txt
--- linux/drivers/no-otg/functions/network/TODO.txt	1970-01-01 01:00:00.000000000 +0100
+++ linux/drivers/otg/functions/network/TODO.txt	2006-09-01 21:41:28.000000000 +0200
@@ -0,0 +1,32 @@
+NETWORK TODO List                                               Stuart Lynne
+Belcarra                                        Tue Aug 24 21:36:09 PDT 2004 
+
+
+1. NETWORK documentation
+
+
+2. BLAN
+        - data notification
+        - timeout to resend data notification after XX mS
+
+3. CDC
+        - filters
+
+
+4. RNDIS
+        - should we add back in?
+
+
+5. NOTIFICATIONS
+
+        NETWORK_CONNECTION
+        CONNECTION_SPEED_CHANGE
+
+
+
+I suspect that we should make the data notification a runtime option, 
+the host driver would "enable" this after a certain number of devices 
+are plugged in (say 5 or more, some configurable number.) This would
+be done by sending a vendor device request to the peripheral to tell
+the network driver that it needs to use this mechanism.
+
diff -uNr linux/drivers/no-otg/functions/network/basic.c linux/drivers/otg/functions/network/basic.c
--- linux/drivers/no-otg/functions/network/basic.c	1970-01-01 01:00:00.000000000 +0100
+++ linux/drivers/otg/functions/network/basic.c	2006-09-01 21:41:28.000000000 +0200
@@ -0,0 +1,247 @@
+/*
+ * otg/functions/network/basic.c - Network Function Driver
+ *
+ *      Copyright (c) 2002, 2003, 2004 Belcarra
+ *
+ * By: 
+ *      Chris Lynne <cl@belcarra.com>
+ *      Stuart Lynne <sl@belcarra.com>
+ *      Bruce Balden <balden@belcarra.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ */
+/*!
+ * @file otg/functions/network/basic.c
+ * @brief This file implements the required descriptors to implement
+ * a basic network device with a single interface.
+ *
+ * The BASIC network driver implements a very simple descriptor set.
+ * A single interface with two BULK data endpoints and a optional
+ * INTERRUPT endpoint.
+ *
+ * @ingroup NetworkFunction
+ */
+
+
+#include <otg/otg-compat.h>
+#include <otg/otg-module.h>
+
+#include <linux/slab.h>
+#include <linux/interrupt.h>
+#include <linux/utsname.h>
+#include <linux/netdevice.h>
+
+#include <otg/usbp-chap9.h>
+#include <otg/usbp-func.h>
+
+#include "network.h"
+
+#define NTT network_fd_trace_tag
+
+#ifdef CONFIG_OTG_NETWORK_BASIC
+/* USB BASIC Configuration ******************************************************************** */
+
+/* BASIC Communication Interface Class descriptors
+ */
+/*! BULK OUT data endpoint descriptor
+ */
+static struct usbd_endpoint_descriptor basic_data_1 = { 
+    bLength:sizeof(struct usbd_endpoint_descriptor),
+    bDescriptorType:USB_DT_ENDPOINT,
+    bEndpointAddress:USB_DIR_OUT, 
+    bmAttributes: BULK,     
+    wMaxPacketSize: 0, 
+    bInterval: 0,
+};
+/*! BULK IN data endpoint descriptor
+ */
+static struct usbd_endpoint_descriptor basic_data_2 =// { 0x07, USB_DT_ENDPOINT, USB_DIR_IN,  BULK,     0, 0x00, 0x00, };
+{
+    bLength:sizeof(struct usbd_endpoint_descriptor),
+    bDescriptorType:USB_DT_ENDPOINT,
+    bEndpointAddress:USB_DIR_IN, 
+    bmAttributes: BULK,     
+    wMaxPacketSize: 0, 
+    bInterval: 0x0,
+};
+
+/*! INTERRUPT IN data endpoint descriptor
+ */
+static struct usbd_endpoint_descriptor basic_comm_1 = //{ 0x07, USB_DT_ENDPOINT, USB_DIR_IN,  INTERRUPT,0, 0x00, 0x0a, };
+{
+    bLength:sizeof(struct usbd_endpoint_descriptor),
+    bDescriptorType:USB_DT_ENDPOINT,
+    bEndpointAddress:USB_DIR_IN, 
+    bmAttributes: INTERRUPT,     
+    wMaxPacketSize: 0, 
+    bInterval: 0xa,
+};
+
+/*! Endpoint descriptor list
+ */
+static  struct usbd_endpoint_descriptor  *basic_default[] = { &basic_data_1, 
+    &basic_data_2, 
+    &basic_comm_1, };
+u8 basic_indexes[] = { BULK_OUT, BULK_IN, INT_IN, };
+
+/*! Data Interface Descriptor
+ */
+static struct usbd_interface_descriptor  basic_data_alternate_descriptor = {
+    bLength:sizeof(struct usbd_interface_descriptor),
+    bDescriptorType:USB_DT_INTERFACE,
+    bInterfaceNumber: 0x00, 
+    bAlternateSetting: 0x00, // bInterfaceNumber, bAlternateSetting
+    sizeof (basic_default) / sizeof(struct usbd_endpoint_descriptor *), // bNumEndpoints
+    bInterfaceClass:LINEO_CLASS, 
+    bInterfaceSubClass:LINEO_SUBCLASS_BASIC_NET, 
+    bInterfaceProtocol:LINEO_BASIC_NET_CRC, 
+    iInterface:0x00,
+};
+
+/*! Data Alternate Interface Description List
+ */
+static struct usbd_alternate_description basic_data_alternate_descriptions[] = {
+        { iInterface: CONFIG_OTG_NETWORK_BASIC_INTF,
+                interface_descriptor: (struct usbd_interface_descriptor *)&basic_data_alternate_descriptor,
+                endpoints:sizeof (basic_default) / sizeof(struct usbd_endpoint_descriptor *),
+                endpoint_list: basic_default,
+                endpoint_indexes: basic_indexes,
+                },
+};
+
+
+/* BASIC Data Interface Alternate descriptions and descriptors
+ */
+
+/* BASIC Interface descriptions and descriptors
+ */
+/*! Interface Description List
+ */
+struct usbd_interface_description basic_interfaces[] = {
+        { alternates:sizeof (basic_data_alternate_descriptions) / sizeof (struct usbd_alternate_description),
+                alternate_list:basic_data_alternate_descriptions,},
+};
+
+/* BASIC Configuration descriptions and descriptors
+ */
+/*! Configuration Descriptor 
+ */
+u8 basic_configuration_descriptor[sizeof(struct usbd_configuration_descriptor)] = {
+        0x09, USB_DT_CONFIGURATION, 0x00, 0x00, // wLength
+        sizeof (basic_interfaces) / sizeof (struct usbd_interface_description),
+        0x01, 0x00, // bConfigurationValue, iConfiguration
+        0, 0,
+};
+
+/*! Configuration Description List 
+ */
+struct usbd_configuration_description basic_description[] = {
+        { iConfiguration: CONFIG_OTG_NETWORK_BASIC_DESC,
+                configuration_descriptor: (struct usbd_configuration_descriptor *)basic_configuration_descriptor,
+        },
+
+};
+
+/* BASIC Device Description
+ */
+/*! Device Descriptor 
+ */
+static struct usbd_device_descriptor basic_device_descriptor = {
+        bLength: sizeof(struct usbd_device_descriptor),
+        bDescriptorType: USB_DT_DEVICE,
+        bcdUSB: __constant_cpu_to_le16(USB_BCD_VERSION),
+        bDeviceClass: COMMUNICATIONS_DEVICE_CLASS,
+        bDeviceSubClass: 0x02,
+        bDeviceProtocol: 0x00,
+        bMaxPacketSize0: 0x00,
+        idVendor: __constant_cpu_to_le16(CONFIG_OTG_NETWORK_VENDORID),
+        idProduct: __constant_cpu_to_le16(CONFIG_OTG_NETWORK_PRODUCTID),
+        bcdDevice: __constant_cpu_to_le16(CONFIG_OTG_NETWORK_BCDDEVICE),
+};
+
+#ifdef CONFIG_OTG_HIGH_SPEED
+/*! High Speed Device Qualifier Descriptor 
+ */
+static struct usbd_device_qualifier_descriptor basic_device_qualifier_descriptor = {
+        bLength: sizeof(struct usbd_device_qualifier_descriptor),
+        bDescriptorType: USB_DT_DEVICE_QUALIFIER,
+        bcdUSB: __constant_cpu_to_le16(USB_BCD_VERSION),
+        bDeviceClass: COMMUNICATIONS_DEVICE_CLASS,
+        bDeviceSubClass: 0x02,
+        bDeviceProtocol: 0x00,
+        bMaxPacketSize0: 0x00,
+};
+#endif /* CONFIG_OTG_HIGH_SPEED */
+
+/*! Endpoint Request List
+ */
+static struct usbd_endpoint_request basic_endpoint_requests[ENDPOINTS+1] = {
+        { 1, 0, 0, USB_DIR_OUT | USB_ENDPOINT_BULK, MAXFRAMESIZE + 48, MAXFRAMESIZE + 512, },
+        { 1, 0, 0, USB_DIR_IN | USB_ENDPOINT_BULK, MAXFRAMESIZE + 48, MAXFRAMESIZE + 512, },
+        { 1, 0, 0, USB_DIR_IN | USB_ENDPOINT_INTERRUPT | USB_ENDPOINT_OPT, 16, 64, },
+        { 0, },
+};
+
+/*! OTG Descriptor
+ */
+static struct usbd_otg_descriptor basic_otg_descriptor = {
+        bLength : sizeof(struct usbd_otg_descriptor),
+        bDescriptorType: USB_DT_OTG,
+        bmAttributes: 0,
+};
+
+/*! Device Description
+ */
+struct usbd_device_description basic_device_description = {
+        device_descriptor: &basic_device_descriptor,
+#ifdef CONFIG_OTG_HIGH_SPEED
+        device_qualifier_descriptor: &basic_device_qualifier_descriptor,
+#endif /* CONFIG_OTG_HIGH_SPEED */
+        otg_descriptor: &basic_otg_descriptor,
+        iManufacturer: CONFIG_OTG_NETWORK_MANUFACTURER,
+        iProduct: CONFIG_OTG_NETWORK_PRODUCT_NAME,
+#if !defined(CONFIG_OTG_NO_SERIAL_NUMBER) && defined(CONFIG_OTG_SERIAL_NUMBER_STR)
+        iSerialNumber:CONFIG_OTG_SERIAL_NUMBER_STR,
+#endif
+};
+
+
+/*! basic_init
+ * @param function The function instance
+ */
+void basic_init (struct usbd_function_instance *function)
+{
+        basic_data_alternate_descriptions[0].endpoints = Usb_network_private.have_interrupt ? 3 : 2;
+}
+
+/*! function driver description
+ */
+struct usbd_function_driver basic_function_driver = {
+        name: "network-BASIC",
+        fops: &net_fd_function_ops,
+        device_description: &basic_device_description,
+        bNumConfigurations: sizeof (basic_description) / sizeof (struct usbd_configuration_description),
+        configuration_description: basic_description,
+        idVendor: __constant_cpu_to_le16(CONFIG_OTG_NETWORK_VENDORID),
+        idProduct: __constant_cpu_to_le16(CONFIG_OTG_NETWORK_PRODUCTID),
+        bcdDevice: __constant_cpu_to_le16(CONFIG_OTG_NETWORK_BCDDEVICE),
+        bNumInterfaces:sizeof (basic_interfaces) / sizeof (struct usbd_interface_description),
+        interface_list:basic_interfaces,
+        endpointsRequested: ENDPOINTS,
+        requestedEndpoints: basic_endpoint_requests,
+};
+#endif                  /* CONFIG_OTG_NETWORK_BASIC */
+
diff -uNr linux/drivers/no-otg/functions/network/basic2.c linux/drivers/otg/functions/network/basic2.c
--- linux/drivers/no-otg/functions/network/basic2.c	1970-01-01 01:00:00.000000000 +0100
+++ linux/drivers/otg/functions/network/basic2.c	2006-09-01 21:41:28.000000000 +0200
@@ -0,0 +1,320 @@
+/*
+ * otg/functions/network/basic2.c - Network Function Driver
+ *
+ *      Copyright (c) 2002, 2003, 2004 Belcarra
+ *
+ * By: 
+ *      Chris Lynne <cl@belcarra.com>
+ *      Stuart Lynne <sl@belcarra.com>
+ *      Bruce Balden <balden@belcarra.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ */
+/*!
+ * @file otg/functions/network/basic2.c
+ * @brief This file implements the required descriptors to implement
+ * a basic network device with two interfaces.
+ *
+ * The BASIC2 network driver implements a very simple descriptor set.
+ * A data interface with two BULK data endpoints and comm interface with
+ * an INTERRUPT endpoint.
+ *
+ * @ingroup NetworkFunction
+ */
+
+
+#include <otg/otg-compat.h>
+#include <otg/otg-module.h>
+
+#include <linux/slab.h>
+#include <linux/interrupt.h>
+#include <linux/utsname.h>
+#include <linux/netdevice.h>
+
+#include <otg/usbp-chap9.h>
+#include <otg/otg-compat.h>
+#include <otg/usbp-func.h>
+
+#include "network.h"
+
+#define NTT network_fd_trace_tag
+
+#ifdef CONFIG_OTG_NETWORK_BASIC2
+/* USB BASIC Configuration ******************************************************************** */
+
+/*
+ * This provides a slight amplification of the basic configuration, it moves the
+ * interrupt endpoint (if available) to a separate interface, so that it is similiar
+ * to the cdc configuration
+ */
+
+/* BASIC Communication Interface Class descriptors
+ */
+//static u8 basic2_data_1[] = { 0x07, USB_DT_ENDPOINT, USB_DIR_OUT, BULK,     0, 0x00, 0x00,  };
+//static u8 basic2_data_2[] = { 0x07, USB_DT_ENDPOINT, USB_DIR_IN,  BULK,     0, 0x00, 0x00, };
+//static u8 basic2_comm_1[] = { 0x07, USB_DT_ENDPOINT, USB_DIR_IN,  INTERRUPT,0, 0x00, 0x0a, };
+
+/*! BULK OUT data endpoint descriptor
+ */
+static struct usbd_endpoint_descriptor basic2_data_1 = { 
+    bLength:sizeof(struct usbd_endpoint_descriptor),
+    bDescriptorType:USB_DT_ENDPOINT,
+    bEndpointAddress:USB_DIR_OUT, 
+    bmAttributes: BULK,     
+    wMaxPacketSize: 0, 
+    bInterval: 0,
+};
+/*! BULK IN data endpoint descriptor
+ */
+static struct usbd_endpoint_descriptor basic2_data_2 = {
+    bLength:sizeof(struct usbd_endpoint_descriptor),
+    bDescriptorType:USB_DT_ENDPOINT,
+    bEndpointAddress:USB_DIR_IN, 
+    bmAttributes: BULK,     
+    wMaxPacketSize: 0, 
+    bInterval: 0x0,
+};
+
+/*! INTERRUPT IN data endpoint descriptor
+ */
+static struct usbd_endpoint_descriptor basic2_comm_1 = {
+    bLength:sizeof(struct usbd_endpoint_descriptor),
+    bDescriptorType:USB_DT_ENDPOINT,
+    bEndpointAddress:USB_DIR_IN, 
+    bmAttributes: INTERRUPT,     
+    wMaxPacketSize: 0, 
+    bInterval: 0xa,
+};
+
+/*! Endpoint descriptor list
+ */
+static  struct usbd_endpoint_descriptor  *basic2_default[] = { 
+    &basic2_data_1, 
+    &basic2_data_2, 
+    &basic2_comm_1, };
+u8 basic2_indexes[] = { BULK_OUT, BULK_IN, INT_IN, };
+
+static struct usbd_endpoint_descriptor *basic2_comm_endpoints[] = { 
+        (struct usbd_endpoint_descriptor *) &basic2_comm_1, };
+
+/*! Endpoint descriptor list
+ */
+static struct usbd_endpoint_descriptor *basic2_data_endpoints[] = { 
+        (struct usbd_endpoint_descriptor *) &basic2_data_1, 
+        (struct usbd_endpoint_descriptor *) &basic2_data_2, };
+
+u8 basic2_comm_indexes[] = { INT_IN, };
+u8 basic2_data_indexes[] = { BULK_OUT, BULK_IN, };
+
+
+/* BASIC2 Data Interface Alternate endpoints
+ */
+
+
+#ifndef COMMUNICATIONS_NETWORK_SUBCLASS
+#define COMMUNICATIONS_NETWORK_SUBCLASS 0
+#endif
+
+#ifndef VENDOR_PROTOCOL
+#define VENDOR_PROTOCOL 0xFF
+#endif
+/*! Comm Interface Descriptor
+ */
+static struct usbd_interface_descriptor  basic2_comm_alternate_descriptor = {
+    bLength:sizeof(struct usbd_interface_descriptor),
+    bDescriptorType:USB_DT_INTERFACE,
+    bInterfaceNumber: 0x00, 
+    bAlternateSetting: 0x00, // bInterfaceNumber, bAlternateSetting
+    sizeof (basic2_default) / sizeof(struct usbd_endpoint_descriptor *), // bNumEndpoints
+    bInterfaceClass:COMMUNICATIONS_INTERFACE_CLASS,
+    bInterfaceSubClass:COMMUNICATIONS_NETWORK_SUBCLASS, 
+    bInterfaceProtocol:VENDOR_PROTOCOL,
+    iInterface:0x00,
+};
+
+/*! Data Interface Descriptor
+ */
+static struct usbd_interface_descriptor  basic2_data_alternate_descriptor = {
+    bLength:sizeof(struct usbd_interface_descriptor),
+    bDescriptorType:USB_DT_INTERFACE,
+    bInterfaceNumber: 0x01, 
+    bAlternateSetting: 0x00, // bInterfaceNumber, bAlternateSetting
+    sizeof (basic2_default) / sizeof(struct usbd_endpoint_descriptor *), // bNumEndpoints
+    bInterfaceClass:DATA_INTERFACE_CLASS,
+    bInterfaceSubClass:COMMUNICATIONS_NO_SUBCLASS,
+    bInterfaceProtocol:COMMUNICATIONS_NO_PROTOCOL, 
+    iInterface:0x00,
+};
+
+/*! Comm Alternate Interface Description List
+ */
+static struct usbd_alternate_description basic2_comm_alternate_descriptions[] = {
+      { iInterface: CONFIG_OTG_NETWORK_BASIC2_COMM_INTF,
+                interface_descriptor: (struct usbd_interface_descriptor *)&basic2_comm_alternate_descriptor,
+                endpoints:sizeof (basic2_comm_endpoints) / sizeof(struct usbd_endpoint_descriptor *),
+                endpoint_list:basic2_comm_endpoints,
+                endpoint_indexes:basic2_comm_indexes,
+                },
+};
+
+
+/*! Data Alternate Interface Description List
+ */
+static struct usbd_alternate_description basic2_data_alternate_descriptions[] = {
+      { iInterface: CONFIG_OTG_NETWORK_BASIC2_DATA_INTF,
+                interface_descriptor: (struct usbd_interface_descriptor *)&basic2_data_alternate_descriptor,
+                endpoints:sizeof (basic2_data_endpoints) / sizeof(struct usbd_endpoint_descriptor *),
+                endpoint_list: basic2_data_endpoints,
+                endpoint_indexes: basic2_data_indexes,
+        },
+};
+
+
+/* BASIC Data Interface Alternate descriptions and descriptors
+ */
+
+/* BASIC Interface descriptions and descriptors
+ */
+/*! Interface Description List
+ */
+static struct usbd_interface_description basic2_interfaces[] = {
+        { alternates:sizeof (basic2_comm_alternate_descriptions) / sizeof (struct usbd_alternate_description),
+                alternate_list:basic2_comm_alternate_descriptions,},
+
+        { alternates:sizeof (basic2_data_alternate_descriptions) / sizeof (struct usbd_alternate_description),
+                alternate_list:basic2_data_alternate_descriptions,},
+};
+
+
+/* BASIC Configuration descriptions and descriptors
+ */
+/*! Configuration Descriptor 
+ */
+u8 basic2_configuration_descriptor[sizeof(struct usbd_configuration_descriptor)] = {
+        0x09, USB_DT_CONFIGURATION, 0x00, 0x00, // wLength
+        sizeof (basic2_interfaces) / sizeof (struct usbd_interface_description),
+        0x01, 0x00, // bConfigurationValue, iConfiguration
+        0, 0,
+};
+
+/*! Configuration Description List 
+ */
+struct usbd_configuration_description basic2_description[] = {
+        { iConfiguration: CONFIG_OTG_NETWORK_BASIC2_DESC,
+                configuration_descriptor: (struct usbd_configuration_descriptor *)basic2_configuration_descriptor,
+        },
+
+};
+
+/* BASIC Device Description
+ */
+/*! Device Descriptor 
+ */
+static struct usbd_device_descriptor basic2_device_descriptor = {
+        bLength: sizeof(struct usbd_device_descriptor),
+        bDescriptorType: USB_DT_DEVICE,
+        bcdUSB: __constant_cpu_to_le16(USB_BCD_VERSION),
+        bDeviceClass: COMMUNICATIONS_DEVICE_CLASS,
+        bDeviceSubClass: 0x02,
+        bDeviceProtocol: 0x00,
+        bMaxPacketSize0: 0x00,
+        idVendor: __constant_cpu_to_le16(CONFIG_OTG_NETWORK_VENDORID),
+        idProduct: __constant_cpu_to_le16(CONFIG_OTG_NETWORK_PRODUCTID),
+        bcdDevice: __constant_cpu_to_le16(CONFIG_OTG_NETWORK_BCDDEVICE),
+};
+
+#ifdef CONFIG_OTG_HIGH_SPEED
+/*! High Speed Device Qualifier Descriptor 
+ */
+static struct usbd_device_qualifier_descriptor basic2_device_qualifier_descriptor = {
+        bLength: sizeof(struct usbd_device_qualifier_descriptor),
+        bDescriptorType: USB_DT_DEVICE_QUALIFIER,
+        bcdUSB: __constant_cpu_to_le16(USB_BCD_VERSION),
+        bDeviceClass: COMMUNICATIONS_DEVICE_CLASS,
+        bDeviceSubClass: 0x02,
+        bDeviceProtocol: 0x00,
+        bMaxPacketSize0: 0x00,
+};
+#endif /* CONFIG_OTG_HIGH_SPEED */
+
+/*! Endpoint Request List
+ */
+static struct usbd_endpoint_request basic2_endpoint_requests[ENDPOINTS+1] = {
+        { 1, 0, 0, USB_DIR_OUT | USB_ENDPOINT_BULK, MAXFRAMESIZE + 48, MAXFRAMESIZE + 512,  },
+        { 1, 0, 0, USB_DIR_IN | USB_ENDPOINT_BULK, MAXFRAMESIZE + 48, MAXFRAMESIZE + 512, },
+        { 1, 0, 0, USB_DIR_IN | USB_ENDPOINT_INTERRUPT | USB_ENDPOINT_OPT, 16, 64, },
+        { 0, },
+};
+
+/*! OTG Descriptor
+ */
+static struct usbd_otg_descriptor basic2_otg_descriptor = {
+        bLength : sizeof(struct usbd_otg_descriptor),
+        bDescriptorType: USB_DT_OTG,
+        bmAttributes: 0,
+};
+/*! Device Description
+ */
+struct usbd_device_description basic2_device_description = {
+        device_descriptor: &basic2_device_descriptor,
+#ifdef CONFIG_OTG_HIGH_SPEED
+        device_qualifier_descriptor: &basic2_device_qualifier_descriptor,
+#endif /* CONFIG_OTG_HIGH_SPEED */
+        otg_descriptor: &basic2_otg_descriptor,
+        iManufacturer: CONFIG_OTG_NETWORK_MANUFACTURER,
+        iProduct: CONFIG_OTG_NETWORK_PRODUCT_NAME,
+#if !defined(CONFIG_OTG_NO_SERIAL_NUMBER) && defined(CONFIG_OTG_SERIAL_NUMBER_STR)
+        iSerialNumber:CONFIG_OTG_SERIAL_NUMBER_STR,
+#endif
+};
+
+
+/*! basic2_init
+ * @param function The function instance
+ */
+void basic2_init (struct usbd_function_instance *function)
+{
+        TRACE_MSG0(NTT,"entered");
+
+        basic2_comm_alternate_descriptions[0].endpoints = Usb_network_private.have_interrupt ? 1 : 0;
+
+        TRACE_MSG2(NTT,"alternate: %p endpoints: %d",
+                        basic2_data_alternate_descriptions,
+                        basic2_data_alternate_descriptions->endpoints
+        );
+
+        TRACE_MSG0(NTT,"exited");
+}
+
+
+/*! function driver description
+ */
+struct usbd_function_driver basic2_function_driver = {
+        name: "network-BASIC2",
+        fops: &net_fd_function_ops,
+        device_description: &basic2_device_description,
+        bNumConfigurations: sizeof (basic2_description) / sizeof (struct usbd_configuration_description),
+        configuration_description: basic2_description,
+        idVendor: __constant_cpu_to_le16(CONFIG_OTG_NETWORK_VENDORID),
+        idProduct: __constant_cpu_to_le16(CONFIG_OTG_NETWORK_PRODUCTID),
+        bcdDevice: __constant_cpu_to_le16(CONFIG_OTG_NETWORK_BCDDEVICE),
+        bNumInterfaces:sizeof (basic2_interfaces) / sizeof (struct usbd_interface_description),
+        interface_list:basic2_interfaces,
+        endpointsRequested: ENDPOINTS,
+        requestedEndpoints: basic2_endpoint_requests,
+};
+#endif                          /* CONFIG_OTG_NETWORK_BASIC2 */
+
diff -uNr linux/drivers/no-otg/functions/network/blan.c linux/drivers/otg/functions/network/blan.c
--- linux/drivers/no-otg/functions/network/blan.c	1970-01-01 01:00:00.000000000 +0100
+++ linux/drivers/otg/functions/network/blan.c	2006-09-01 21:41:28.000000000 +0200
@@ -0,0 +1,403 @@
+/*
+ * otg/functions/network/blan.c - Network Function Driver
+ *
+ *      Copyright (c) 2002, 2003, 2004 Belcarra
+ *
+ * By: 
+ *      Chris Lynne <cl@belcarra.com>
+ *      Stuart Lynne <sl@belcarra.com>
+ *      Bruce Balden <balden@belcarra.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ */
+/*!
+ * @file otg/functions/network/blan.c
+ * @brief This file implements the required descriptors to implement
+ * a BLAN network device with a single interface.
+ *
+ * The BLAN network driver implements the BLAN protocol descriptors.
+ *
+ * The BLAN protocol is designed to support smart devices that want
+ * to create a virtual network between them host and other similiar
+ * devices.
+ *
+ * @ingroup NetworkFunction
+ */
+
+
+#include <otg/otg-compat.h>
+#include <otg/otg-module.h>
+#include <linux/config.h>
+#include <linux/module.h>
+#include <linux/kernel.h>
+#include <linux/version.h>
+
+#include <linux/slab.h>
+#include <linux/interrupt.h>
+#include <linux/utsname.h>
+#include <linux/netdevice.h>
+
+#include <otg/usbp-chap9.h>
+#include <otg/usbp-func.h>
+
+#include "network.h"
+
+#define NTT network_fd_trace_tag
+
+#ifdef CONFIG_OTG_NETWORK_BLAN
+/* USB BLAN  Configuration ******************************************************************** */
+
+/*
+ * BLAN Ethernet Configuration
+ */
+
+/* Communication Interface Class descriptors
+ */
+
+/*! BULK OUT data endpoint descriptor
+ */
+static struct usbd_endpoint_descriptor blan_data_1 = { 
+    .bLength = sizeof(struct usbd_endpoint_descriptor),
+    .bDescriptorType = USB_DT_ENDPOINT,
+    .bEndpointAddress = USB_DIR_OUT, 
+    .bmAttributes =  BULK,     
+    .wMaxPacketSize =  0, 
+    .bInterval =  0,
+};
+/*! BULK IN data endpoint descriptor
+ */
+static struct usbd_endpoint_descriptor blan_data_2 =// { 0x07, USB_DT_ENDPOINT, USB_DIR_IN,  BULK,     0, 0x00, 0x00, };
+{
+    .bLength = sizeof(struct usbd_endpoint_descriptor),
+    .bDescriptorType = USB_DT_ENDPOINT,
+    .bEndpointAddress = USB_DIR_IN, 
+    .bmAttributes =  BULK,     
+    .wMaxPacketSize =  0, 
+    .bInterval =  0x0,
+};
+
+/*! INTERRUPT IN data endpoint descriptor
+ */
+static struct usbd_endpoint_descriptor blan_comm_1 = //{ 0x07, USB_DT_ENDPOINT, USB_DIR_IN,  INTERRUPT,0, 0x00, 0x0a, };
+{
+        .bLength = sizeof(struct usbd_endpoint_descriptor),
+        .bDescriptorType = USB_DT_ENDPOINT,
+        .bEndpointAddress = USB_DIR_IN, 
+        .bmAttributes = INTERRUPT,     
+        .wMaxPacketSize = 0, 
+#if defined (CONFIG_OTG_NETWORK_BLAN_INTERVAL)
+        .bInterval = CONFIG_OTG_NETWORK_BLAN_INTERVAL, 
+#else
+        .bInterval = 0x08, 
+#endif /* defined (CONFIG_OTG_NETWORK_BLAN_INTERVAL) */
+};
+
+//static u8 blan_class_1[] = { 0x05, CS_INTERFACE, USB_ST_HEADER, 0x10, 0x01, /* CLASS_BDC_VERSION, CLASS_BDC_VERSION */ };
+static struct usbd_class_header_function_descriptor blan_class_1 = {
+        .bFunctionLength = 0x05, /* Length */
+        .bDescriptorType =  CS_INTERFACE, 
+        .bDescriptorSubtype = USB_ST_HEADER, 
+        .bcdCDC =  __constant_cpu_to_le16(0x0110) /*Version */
+};
+//static u8 blan_class_2[] = { 0x15, CS_INTERFACE, USB_ST_MDLM, 0x00, 0x01, /* bcdVersion, bcdVersion */
+//                0x74, 0xf0, 0x3d, 0xbd, 0x1e, 0xc1, 0x44, 0x70,  /* bGUID */
+static struct usbd_class_mdlm_descriptor blan_class_2 = {
+        .bFunctionLength = 0x15, 
+        .bDescriptorType = CS_INTERFACE, 
+        .bDescriptorSubtype = USB_ST_MDLM, 
+        .bcdVersion = __constant_cpu_to_le16(0x0100), 
+        .bGUID =  {
+            0x74, 0xf0, 0x3d, 0xbd, 0x1e, 0xc1, 0x44, 0x70,  /* bGUID */
+            0xa3, 0x67, 0x71, 0x34, 0xc9, 0xf5, 0x54, 0x37,  /* bGUID */ },
+};
+
+
+
+//static u8 blan_class_3[] = { 0x07, CS_INTERFACE, USB_ST_MDLMD, 0x01, 0x00, 0x00, 0x00, };
+static struct usbd_class_blan_descriptor blan_class_3 = { 
+        .bFunctionLength = 0x07, 
+        .bDescriptorType =  CS_INTERFACE, 
+        .bDescriptorSubtype =  USB_ST_MDLMD, 
+        .bGuidDescriptorType =  0x01,
+        .bmNetworkCapabilities =  0x00,
+        .bmDataCapabilities =  0x00,
+        .bPad =  0x00,
+};
+
+//static u8 blan_class_4[] = { 0x0d, CS_INTERFACE, USB_ST_ENF, 
+//      0x00, 0x00, 0x00, 0x00, 0x00, 0xea, 0x05, /* 1514 maximum frame size */
+//      0x00, 0x00, 0x00 , };
+static struct usbd_class_ethernet_networking_descriptor blan_class_4 = { 
+        .bFunctionLength = 0x0d, 
+        .bDescriptorType = CS_INTERFACE, 
+        .bDescriptorSubtype = USB_ST_ENF, 
+        .iMACAddress =  0x00, 
+        .bmEthernetStatistics =  0x00, 
+        .wMaxSegmentSize =  0x05ea, /* 1514 maximum frame size */
+        .wNumberMCFilters =   0x00,
+        .bNumberPowerFilters =  0x00 , 
+};
+
+//static u8 blan_class_5[] = { 0x07, CS_INTERFACE, USB_ST_NCT, 0x00, 0x00, 0x00, 0x00, };
+static struct usbd_class_network_channel_descriptor blan_class_5 = {
+        .bFunctionLength =  0x07,
+        .bDescriptorType =  CS_INTERFACE,
+        .bDescriptorSubtype = USB_ST_NCT,
+        .bEntityId =  0,
+        .iName =  0,
+        .bChannelIndex =  0,
+        .bPhysicalInterface =  0,
+};
+
+
+/*! Endpoint descriptor list
+ */
+static struct usbd_endpoint_descriptor *blan_alt_endpoints[] = { 
+        (struct usbd_endpoint_descriptor *) &blan_data_1, 
+        (struct usbd_endpoint_descriptor *) &blan_data_2, 
+        (struct usbd_endpoint_descriptor *) &blan_comm_1, };
+
+u8 blan_alt_indexes[] = { BULK_OUT, BULK_IN, INT_IN, };
+
+static struct usbd_generic_class_descriptor *blan_comm_class_descriptors[] = {
+        (struct usbd_generic_class_descriptor *) &blan_class_1, 
+        (struct usbd_generic_class_descriptor *) &blan_class_2, 
+        (struct usbd_generic_class_descriptor *) &blan_class_3, 
+        (struct usbd_generic_class_descriptor *) &blan_class_4, 
+        (struct usbd_generic_class_descriptor *) &blan_class_5, };
+
+
+/*! Data Interface Descriptor
+ */
+static struct usbd_interface_descriptor blan_alternate_descriptor = {
+        .bLength = 0x09, 
+        .bDescriptorType =  USB_DT_INTERFACE, 
+        .bInterfaceNumber = 0x00, 
+        .bAlternateSetting =  0x00, // bInterfaceNumber, bAlternateSetting
+        .bNumEndpoints =  sizeof (blan_alt_endpoints) / sizeof(struct usbd_endpoint_descriptor *), // bNumEndpoints
+        .bInterfaceClass = COMMUNICATIONS_INTERFACE_CLASS, 
+        .bInterfaceSubClass =  COMMUNICATIONS_MDLM_SUBCLASS, 
+        .bInterfaceProtocol = COMMUNICATIONS_NO_PROTOCOL, 
+        .iInterface =  0x00,
+};
+
+/*! Data Alternate Interface Description List
+ */
+static struct usbd_alternate_description blan_alternate_descriptions[] = {
+        { 
+                .iInterface =  CONFIG_OTG_NETWORK_BLAN_INTF,
+                .interface_descriptor =  (struct usbd_interface_descriptor *)&blan_alternate_descriptor,
+                .classes = sizeof (blan_comm_class_descriptors) / sizeof (struct usbd_generic_class_descriptor *),
+                .class_list =  blan_comm_class_descriptors,
+                .endpoints = sizeof (blan_alt_endpoints) / sizeof(struct usbd_endpoint_descriptor *),
+                .endpoint_list =  blan_alt_endpoints,
+                .endpoint_indexes =  blan_alt_indexes,
+        },
+};
+/* Interface descriptions and descriptors
+ */
+/*! Interface Description List
+ */
+static struct usbd_interface_description blan_interfaces[] = {
+        { 
+                .alternates =  sizeof (blan_alternate_descriptions) / sizeof (struct usbd_alternate_description),
+                .alternate_list =  blan_alternate_descriptions, 
+        },
+};
+
+
+/* Configuration descriptions and descriptors
+ */
+
+/*! Configuration Descriptor 
+ */
+static struct usbd_configuration_descriptor  blan_configuration_descriptor = {
+        .bLength =  0x09, 
+        .bDescriptorType =  USB_DT_CONFIGURATION, 
+        .wTotalLength = 0x00, // wLength
+        .bNumInterfaces =  sizeof (blan_interfaces) / sizeof (struct usbd_interface_description),
+        .bConfigurationValue = 0x01, 
+        .iConfiguration =  0x00, // bConfigurationValue, iConfiguration
+        .bmAttributes = 0, 
+        .bMaxPower = 0,
+};
+
+/*! Configuration Description List 
+ */
+struct usbd_configuration_description blan_description[] = {
+    { 
+        .iConfiguration =  CONFIG_OTG_NETWORK_BLAN_DESC,
+        .configuration_descriptor =  &blan_configuration_descriptor,
+    },
+};
+
+/* Device Description
+ */
+
+/*! Device Descriptor 
+ */
+static struct usbd_device_descriptor blan_device_descriptor = {
+        .bLength =  sizeof(struct usbd_device_descriptor),
+        .bDescriptorType =  USB_DT_DEVICE,
+        .bcdUSB =  __constant_cpu_to_le16(USB_BCD_VERSION),
+        .bDeviceClass =  COMMUNICATIONS_DEVICE_CLASS,
+        .bDeviceSubClass =  0x02,
+        .bDeviceProtocol =  0x00,
+        .bMaxPacketSize0 =  0x00,
+        .idVendor =  __constant_cpu_to_le16(CONFIG_OTG_NETWORK_VENDORID),
+        .idProduct =  __constant_cpu_to_le16(CONFIG_OTG_NETWORK_PRODUCTID),
+        .bcdDevice =  __constant_cpu_to_le16(CONFIG_OTG_NETWORK_BCDDEVICE),
+};
+
+#ifdef CONFIG_OTG_HIGH_SPEED
+/*! High Speed Device Qualifier Descriptor 
+ */
+static struct usbd_device_qualifier_descriptor blan_device_qualifier_descriptor = {
+        .bLength =  sizeof(struct usbd_device_qualifier_descriptor),
+        .bDescriptorType =  USB_DT_DEVICE_QUALIFIER,
+        .bcdUSB =  __constant_cpu_to_le16(USB_BCD_VERSION),
+        .bDeviceClass =  COMMUNICATIONS_DEVICE_CLASS,
+        .bDeviceSubClass =  0x02,
+        .bDeviceProtocol =  0x00,
+        .bMaxPacketSize0 =  0x00,
+};
+#endif /* CONFIG_OTG_HIGH_SPEED */
+
+/*! Endpoint Request List
+ */
+static struct usbd_endpoint_request blan_endpoint_requests[ENDPOINTS+1] = {
+        { 1, 0, 0, USB_DIR_OUT | USB_ENDPOINT_BULK, MAXFRAMESIZE + 48, MAXFRAMESIZE + 512,  },
+        { 1, 0, 0, USB_DIR_IN | USB_ENDPOINT_BULK, MAXFRAMESIZE + 48, MAXFRAMESIZE + 512,  },
+        { 1, 0, 0, USB_DIR_IN | USB_ENDPOINT_INTERRUPT | USB_ENDPOINT_OPT, 16, 64, },
+        { 0, },
+};
+
+/*! OTG Descriptor
+ */
+static struct usbd_otg_descriptor blan_otg_descriptor = {
+        .bLength  =  sizeof(struct usbd_otg_descriptor),
+        .bDescriptorType =  USB_DT_OTG,
+        .bmAttributes =  0,
+};
+
+/*! Device Description
+ */
+struct usbd_device_description blan_device_description = {
+        .device_descriptor =  &blan_device_descriptor,
+#ifdef CONFIG_OTG_HIGH_SPEED
+        .device_qualifier_descriptor =  &blan_device_qualifier_descriptor,
+#endif /* CONFIG_OTG_HIGH_SPEED */
+        .otg_descriptor =  &blan_otg_descriptor,
+        .iManufacturer =  CONFIG_OTG_NETWORK_MANUFACTURER,
+        .iProduct =  CONFIG_OTG_NETWORK_PRODUCT_NAME,
+#if !defined(CONFIG_OTG_NO_SERIAL_NUMBER) && defined(CONFIG_OTG_SERIAL_NUMBER_STR)
+        .iSerialNumber = CONFIG_OTG_SERIAL_NUMBER_STR,
+#endif
+};
+
+
+/*! blan_init
+ * @param function The function instance
+ */
+void blan_init (struct usbd_function_instance *function)
+{
+        struct usbd_class_ethernet_networking_descriptor *ethernet;
+        struct usbd_class_network_channel_descriptor *channel;
+
+        int len = 0;
+        char buf[255];
+
+        buf[0] = 0;
+
+        blan_alternate_descriptions[0].endpoints = Usb_network_private.have_interrupt ? 3 : 2;
+
+        // Update the iMACAddress field in the ethernet descriptor
+        {
+#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,17)
+                char address_str[14];
+                snprintf(address_str, 13, "%02x%02x%02x%02x%02x%02x",
+                                local_dev_addr[0], local_dev_addr[1], local_dev_addr[2], 
+                                local_dev_addr[3], local_dev_addr[4], local_dev_addr[5]);
+#else
+                char address_str[20];
+                sprintf(address_str, "%02x%02x%02x%02x%02x%02x",
+                                local_dev_addr[0], local_dev_addr[1], local_dev_addr[2], 
+                                local_dev_addr[3], local_dev_addr[4], local_dev_addr[5]);
+#endif
+                TRACE_MSG0(NTT,"alloc mac string");
+                if ((ethernet = &blan_class_4)) 
+                        ethernet->iMACAddress = usbd_alloc_string(address_str);
+                TRACE_MSG0(NTT,"alloc mac string done");
+        }
+        TRACE_MSG0(NTT,"alloc channel string");
+        if ((channel = &blan_class_5)) 
+                channel->iName = usbd_alloc_string(system_utsname.nodename);
+        TRACE_MSG0(NTT,"alloc channel string done");
+
+#ifdef CONFIG_OTG_NETWORK_BLAN_PADBYTES
+        blan_class_3.bPad = CONFIG_OTG_NETWORK_BLAN_PADBYTES;
+        len += sprintf(buf + len, "PADBYTES: %02x ", blan_class_3.bPad);
+#endif
+#ifdef CONFIG_OTG_NETWORK_BLAN_PADBEFORE
+        blan_class_3.bmDataCapabilities |= BMDATA_PADBEFORE;
+        len += sprintf(buf + len, "PADBEFORE: %02x ", blan_class_3.bmDataCapabilities);
+#endif
+#ifdef CONFIG_OTG_NETWORK_BLAN_PADAFTER
+        blan_class_3.bmDataCapabilities |= BMDATA_PADAFTER;
+        len += sprintf(buf + len, "PADAFTER: %02x ", blan_class_3.bmDataCapabilities);
+#endif
+#ifdef CONFIG_OTG_NETWORK_BLAN_CRC
+        blan_class_3.bmDataCapabilities |= BMDATA_CRC;
+        len += sprintf(buf + len, "CRC: %02x ", blan_class_3.bmDataCapabilities);
+#endif
+#ifdef CONFIG_OTG_NETWORK_BLAN_FERMAT
+        blan_class_3.bmDataCapabilities |= BMDATA_FERMAT;
+        len += sprintf(buf + len, "FERMAT: %02x ", blan_class_3.bmDataCapabilities);
+#endif
+#ifdef CONFIG_OTG_NETWORK_BLAN_HOSTNAME
+        blan_class_3.bmDataCapabilities |= BMDATA_HOSTNAME;
+        len += sprintf(buf + len, "HOSTNAME: %02x ", blan_class_3.bmDataCapabilities);
+#endif
+#ifdef CONFIG_OTG_NETWORK_BLAN_NONBRIDGED
+        blan_class_3.bmNetworkCapabilities |= BMNETWORK_NONBRIDGED;
+        len += sprintf(buf + len, "NONBRIDGE: %02x ", blan_class_3.bmNetworkCapabilities);
+#endif
+#ifdef CONFIG_OTG_NETWORK_BLAN_DATA_NOTIFY_OK
+        blan_class_3.bmNetworkCapabilities |= BMNETWORK_DATA_NOTIFY_OK;
+        len += sprintf(buf + len, "DATA NOTIFY: %02x ", blan_class_3.bmNetworkCapabilities);
+#endif
+        if (strlen(buf))
+                TRACE_MSG1(NTT,"%s", buf);
+}
+
+/*! function driver description
+ */
+struct usbd_function_driver blan_function_driver = {
+        .name =  "network-BLAN",
+        .fops =  &net_fd_function_ops,
+        .device_description =  &blan_device_description,
+        .bNumConfigurations =  sizeof (blan_description) / sizeof (struct usbd_configuration_description),
+        .configuration_description =  blan_description,
+        .idVendor =  __constant_cpu_to_le16(CONFIG_OTG_NETWORK_VENDORID),
+        .idProduct =  __constant_cpu_to_le16(CONFIG_OTG_NETWORK_PRODUCTID),
+        .bcdDevice =  __constant_cpu_to_le16(CONFIG_OTG_NETWORK_BCDDEVICE),
+        .bNumInterfaces = sizeof (blan_interfaces) / sizeof (struct usbd_interface_description),
+        .interface_list = blan_interfaces, 
+        .endpointsRequested =  ENDPOINTS,
+        .requestedEndpoints =  blan_endpoint_requests,
+};
+#endif                  /* CONFIG_OTG_NETWORK_BLAN */
+
diff -uNr linux/drivers/no-otg/functions/network/cdc.c linux/drivers/otg/functions/network/cdc.c
--- linux/drivers/no-otg/functions/network/cdc.c	1970-01-01 01:00:00.000000000 +0100
+++ linux/drivers/otg/functions/network/cdc.c	2006-09-01 21:41:28.000000000 +0200
@@ -0,0 +1,376 @@
+/*
+ * otg/functions/network/cdc.c - Network Function Driver
+ *
+ *      Copyright (c) 2002, 2003, 2004 Belcarra
+ *
+ * By: 
+ *      Chris Lynne <cl@belcarra.com>
+ *      Stuart Lynne <sl@belcarra.com>
+ *      Bruce Balden <balden@belcarra.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * The CDC network driver implements the standard CDC Ethernet descriptors.
+ *
+ * The CDC protocol is suitable for infrastructure devices that
+ * are implementing a bridged or routed connection betwen an
+ * external network and the USB Host.
+ *
+ */
+/*!
+ * @file otg/functions/network/cdc.c
+ * @brief This file implements the required descriptors to implement
+ * a CDC network device with two interfaces.
+ *
+ * @ingroup NetworkFunction
+ */
+
+#include <otg/otg-compat.h>
+#include <otg/otg-module.h>
+
+#include <linux/interrupt.h>
+#include <linux/utsname.h>
+#include <linux/netdevice.h>
+
+#include <otg/usbp-chap9.h>
+#include <otg/usbp-func.h>
+
+#include "network.h"
+
+#define NTT network_fd_trace_tag
+
+
+#ifdef CONFIG_OTG_NETWORK_CDC
+/* USB CDC Configuration ********************************************************************* */
+
+/* CDC Communication Interface Class descriptors
+ */
+/*! BULK OUT data endpoint descriptor
+ */
+static struct usbd_endpoint_descriptor cdc_data_1 = { 
+    bLength:          0x07, 
+    bDescriptorType:  USB_DT_ENDPOINT,
+    bEndpointAddress: USB_DIR_OUT,
+    bmAttributes:     BULK,
+    wMaxPacketSize:   __constant_cpu_to_le16(0x00),
+    bInterval:        0x00,
+};
+
+/*! BULK IN data endpoint descriptor
+ */
+static struct usbd_endpoint_descriptor cdc_data_2 = {
+    bLength:          0x07,
+    bDescriptorType:  USB_DT_ENDPOINT,
+    bEndpointAddress: USB_DIR_IN,
+    bmAttributes:     BULK,
+    wMaxPacketSize:   __constant_cpu_to_le16(0x00),
+    bInterval:        0x00,
+};
+
+/*! INTERRUPT IN data endpoint descriptor
+ */
+static struct usbd_endpoint_descriptor cdc_comm_1 = {
+    bLength:          0x07,
+    bDescriptorType:  USB_DT_ENDPOINT,
+    bEndpointAddress: USB_DIR_IN,
+    bmAttributes:     INTERRUPT,
+    wMaxPacketSize:   __constant_cpu_to_le16(0x00),
+    bInterval:        0x0a,
+};
+
+
+
+
+//static u8 cdc_class_1[] = { 0x05, CS_INTERFACE, USB_ST_HEADER, 0x10, 0x01, /* CLASS_BDC_VERSION, CLASS_BDC_VERSION */ };
+
+static struct usbd_class_header_function_descriptor cdc_class_1 = {
+    bFunctionLength:	0x05,
+    bDescriptorType:    CS_INTERFACE,
+    bDescriptorSubtype: USB_ST_HEADER,
+    bcdCDC:             __constant_cpu_to_le16(0x0110),
+};
+
+
+//static u8 cdc_class_2[] = { 
+//        0x0d, CS_INTERFACE, USB_ST_ENF, 0x00, 0x00, 0x00, 0x00, 0x00, 0xea, 0x05, /* 1514 maximum frame size */
+//        0x00, 0x00, 0x00 , };
+static struct usbd_class_ethernet_networking_descriptor cdc_class_2 = {
+    bFunctionLength:    0x0D,
+    bDescriptorType:    CS_INTERFACE,
+    bDescriptorSubtype: USB_ST_HEADER,
+    iMACAddress:        0x00,
+    bmEthernetStatistics: 0x00,
+    wMaxSegmentSize:    __constant_cpu_to_le16(0x05ea),
+    wNumberMCFilters:   0,
+    bNumberPowerFilters: 0,
+};
+
+//static u8 cdc_class_3[] = { 0x05, CS_INTERFACE, USB_ST_UF, 0x00, 0x01, /* bMasterInterface, bSlaveInterface */};
+
+static struct usbd_class_union_function_descriptor cdc_class_3 = 
+{
+     bFunctionLength:    0x05,
+     bDescriptorType:    CS_INTERFACE,
+     bDescriptorSubtype: USB_ST_UF,
+     bMasterInterface:   0x00,
+     bSlaveInterface0:	{1},
+      
+};
+
+static usbd_class_descriptor_t *cdc_comm_class_descriptors[] = { 
+        (struct usbd_generic_class_descriptor *) &cdc_class_1, 
+        (struct usbd_generic_class_descriptor *) &cdc_class_2, 
+        (struct usbd_generic_class_descriptor *) &cdc_class_3, 
+};
+
+/*! Endpoint descriptor list
+ */
+static  usbd_endpoint_descriptor_t  *cdc_data_endpoints[] = { 
+        (struct usbd_endpoint_descriptor *) &cdc_data_1, 
+        (struct usbd_endpoint_descriptor *) &cdc_data_2, 
+};
+static usbd_endpoint_descriptor_t  *cdc_comm_endpoints[] = { 
+        (usbd_endpoint_descriptor_t *) &cdc_comm_1, };
+
+u8 cdc_comm_indexes[] = { INT_IN, };
+u8 cdc_data_indexes[] = { BULK_OUT, BULK_IN, };
+
+
+/*! Comm Interface Descriptor
+ */
+static struct usbd_interface_descriptor cdc_comm_alternate_descriptor  = {
+        bLength: sizeof(struct usbd_interface_descriptor),
+        bDescriptorType: USB_DT_INTERFACE,
+        bInterfaceNumber: 0x00,
+        bAlternateSetting: 0x00,
+        bNumEndpoints: sizeof(cdc_comm_endpoints)/sizeof(usbd_endpoint_descriptor_t *),
+        bInterfaceClass: COMMUNICATIONS_INTERFACE_CLASS,
+        bInterfaceSubClass: COMMUNICATIONS_ENCM_SUBCLASS,
+        bInterfaceProtocol: COMMUNICATIONS_NO_PROTOCOL,
+        iInterface: 0x00
+};
+	
+/*! No Data Interface Descriptor
+ */
+static struct usbd_interface_descriptor  cdc_nodata_alternate_descriptor = {
+        bLength: sizeof(struct usbd_interface_descriptor),
+        bDescriptorType: USB_DT_INTERFACE,
+        bInterfaceNumber: 0x01,
+        bAlternateSetting: 0x00,
+        bNumEndpoints: 0,
+        bInterfaceClass: DATA_INTERFACE_CLASS,
+        bInterfaceSubClass: COMMUNICATIONS_NO_SUBCLASS,
+        bInterfaceProtocol: COMMUNICATIONS_NO_PROTOCOL,
+        iInterface: 0x00
+};
+
+/*! Data Interface Descriptor
+ */
+static struct usbd_interface_descriptor cdc_data_alternate_descriptor  = {
+        bLength: sizeof(struct usbd_interface_descriptor),
+        bDescriptorType: USB_DT_INTERFACE,
+        bInterfaceNumber: 0x01,
+        bAlternateSetting: 0x01,
+        bNumEndpoints: sizeof(cdc_comm_endpoints)/sizeof(usbd_endpoint_descriptor_t *),
+        bInterfaceClass: DATA_INTERFACE_CLASS,
+        bInterfaceSubClass: COMMUNICATIONS_NO_SUBCLASS,
+        bInterfaceProtocol: COMMUNICATIONS_NO_PROTOCOL,
+        iInterface: 0x00
+};
+
+/*! Comm Alternate Interface Description List
+ */
+static struct usbd_alternate_description cdc_comm_alternate_descriptions[] = {
+       {
+         iInterface: CONFIG_OTG_NETWORK_CDC_COMM_INTF,
+         interface_descriptor: &cdc_comm_alternate_descriptor,
+         classes:sizeof (cdc_comm_class_descriptors) / sizeof (struct usbd_generic_class_descriptor *),
+         class_list:    cdc_comm_class_descriptors,
+
+         endpoints:sizeof (cdc_comm_endpoints) / sizeof(struct usbd_endpoint_descriptor *),
+         endpoint_list:  cdc_comm_endpoints,
+         endpoint_indexes: cdc_comm_indexes,
+        },
+};
+
+
+/*! Data Alternate Interface Description List
+ */
+static struct usbd_alternate_description cdc_data_alternate_descriptions[] = {
+        { 
+                iInterface: CONFIG_OTG_NETWORK_CDC_NODATA_INTF,
+                interface_descriptor: (struct usbd_interface_descriptor *)&cdc_nodata_alternate_descriptor, 
+        },
+        { 
+                iInterface: CONFIG_OTG_NETWORK_CDC_DATA_INTF,
+                interface_descriptor: &cdc_data_alternate_descriptor,
+                endpoints:sizeof (cdc_data_endpoints) / sizeof(struct usbd_endpoint_descriptor *),
+                endpoint_list: cdc_data_endpoints,
+                endpoint_indexes: cdc_data_indexes,
+        },
+};
+
+/* Interface descriptions and descriptors
+ */
+/*! Interface Description List
+ */
+static struct usbd_interface_description cdc_interfaces[] = {
+        { 
+                alternates:sizeof (cdc_comm_alternate_descriptions) / sizeof (struct usbd_alternate_description),
+                alternate_list:cdc_comm_alternate_descriptions,
+        },
+        { 
+                alternates:sizeof (cdc_data_alternate_descriptions) / sizeof (struct usbd_alternate_description),
+                alternate_list:cdc_data_alternate_descriptions,
+        },
+};
+
+
+/* Configuration descriptions and descriptors
+ */
+
+/*! Configuration Descriptor 
+ */
+static struct usbd_configuration_descriptor cdc_configuration_descriptor = {
+        bLength: sizeof(struct usbd_configuration_descriptor),
+        bDescriptorType: USB_DT_CONFIGURATION,
+        wTotalLength:0,
+        bNumInterfaces:sizeof(cdc_interfaces)/sizeof(struct usbd_interface_description *),
+        bConfigurationValue: 0x01,
+        iConfiguration: 0x00,
+        bmAttributes:0,
+        bMaxPower:0,
+};
+
+/*! Configuration Description List 
+ */
+static struct usbd_configuration_description cdc_description[] = {
+        { iConfiguration: CONFIG_OTG_NETWORK_CDC_DESC,
+                configuration_descriptor: &cdc_configuration_descriptor,
+        },
+};
+
+
+
+/* Device Description
+ */
+
+/*! Device Descriptor 
+ */
+static struct usbd_device_descriptor cdc_device_descriptor = {
+        bLength: sizeof(struct usbd_device_descriptor),
+        bDescriptorType: USB_DT_DEVICE,
+        bcdUSB: __constant_cpu_to_le16(USB_BCD_VERSION),
+        bDeviceClass: COMMUNICATIONS_DEVICE_CLASS,
+        bDeviceSubClass: 0x02,
+        bDeviceProtocol: 0x00,
+        bMaxPacketSize0: 0x00,
+        idVendor: __constant_cpu_to_le16(CONFIG_OTG_NETWORK_VENDORID),
+        idProduct: __constant_cpu_to_le16(CONFIG_OTG_NETWORK_PRODUCTID),
+        bcdDevice: __constant_cpu_to_le16(CONFIG_OTG_NETWORK_BCDDEVICE),
+};
+
+#ifdef CONFIG_OTG_HIGH_SPEED
+/*! High Speed Device Qualifier Descriptor 
+ */
+static struct usbd_device_qualifier_descriptor cdc_device_qualifier_descriptor = {
+        bLength: sizeof(struct usbd_device_qualifier_descriptor),
+        bDescriptorType: USB_DT_DEVICE_QUALIFIER,
+        bcdUSB: __constant_cpu_to_le16(USB_BCD_VERSION),
+        bDeviceClass: COMMUNICATIONS_DEVICE_CLASS,
+        bDeviceSubClass: 0x02,
+        bDeviceProtocol: 0x00,
+        bMaxPacketSize0: 0x00,
+};
+#endif /* CONFIG_OTG_HIGH_SPEED */
+
+/*! Endpoint Request List
+ */
+static struct usbd_endpoint_request cdc_endpoint_requests[ENDPOINTS+1] = {
+        { 1, 1, 1, USB_DIR_OUT | USB_ENDPOINT_BULK, MAXFRAMESIZE + 48, MAXFRAMESIZE + 512,  },
+        { 1, 1, 1, USB_DIR_IN | USB_ENDPOINT_BULK, MAXFRAMESIZE + 48, MAXFRAMESIZE + 512,  },
+        { 1, 0, 0, USB_DIR_IN | USB_ENDPOINT_INTERRUPT | USB_ENDPOINT_OPT, 16, 64, },
+        { 0, },
+};
+
+/*! OTG Descriptor
+ */
+static struct usbd_otg_descriptor cdc_otg_descriptor = {
+        bLength : sizeof(struct usbd_otg_descriptor),
+        bDescriptorType: USB_DT_OTG,
+        bmAttributes: 0,
+};
+
+/*! Device Description
+ */
+struct usbd_device_description cdc_device_description = {
+        device_descriptor: &cdc_device_descriptor,
+#ifdef CONFIG_OTG_HIGH_SPEED
+        device_qualifier_descriptor: &cdc_device_qualifier_descriptor,
+#endif /* CONFIG_OTG_HIGH_SPEED */
+        otg_descriptor: &cdc_otg_descriptor,
+        iManufacturer: CONFIG_OTG_NETWORK_MANUFACTURER,
+        iProduct: CONFIG_OTG_NETWORK_PRODUCT_NAME,
+#if !defined(CONFIG_OTG_NO_SERIAL_NUMBER) && defined(CONFIG_OTG_SERIAL_NUMBER_STR)
+        iSerialNumber:CONFIG_OTG_SERIAL_NUMBER_STR,
+#endif
+};
+
+
+
+/*! cdc_init
+ * @param function The function instance
+ */
+void cdc_init (struct usbd_function_instance *function)
+{
+        struct usbd_class_ethernet_networking_descriptor *ethernet;
+
+        cdc_comm_alternate_descriptions[0].endpoints = Usb_network_private.have_interrupt ? 1 : 0;
+
+        // Update the iMACAddress field in the ethernet descriptor
+        {
+                char address_str[14];
+                snprintf(address_str, 13, "%02x%02x%02x%02x%02x%02x",
+                                remote_dev_addr[0], remote_dev_addr[1], remote_dev_addr[2], 
+                                remote_dev_addr[3], remote_dev_addr[4], remote_dev_addr[5]);
+
+                if ((ethernet = &cdc_class_2)) {
+                        if (ethernet->iMACAddress) {
+                                usbd_free_string_descriptor(ethernet->iMACAddress);
+                        }
+                        ethernet->iMACAddress = usbd_alloc_string(address_str);
+                }
+        }
+}
+
+
+/*! function driver description
+ */
+struct usbd_function_driver cdc_function_driver = {
+        name: "network-CDC",
+        fops: &net_fd_function_ops,
+        device_description: &cdc_device_description,
+        bNumConfigurations: sizeof (cdc_description) / sizeof (struct usbd_configuration_description),
+        configuration_description: cdc_description,
+        idVendor: __constant_cpu_to_le16(CONFIG_OTG_NETWORK_VENDORID),
+        idProduct: __constant_cpu_to_le16(CONFIG_OTG_NETWORK_PRODUCTID),
+        bcdDevice: __constant_cpu_to_le16(CONFIG_OTG_NETWORK_BCDDEVICE),
+        bNumInterfaces:sizeof (cdc_interfaces) / sizeof (struct usbd_interface_description),
+        interface_list:cdc_interfaces,
+        endpointsRequested: ENDPOINTS,
+        requestedEndpoints: cdc_endpoint_requests,
+};
+#endif                  /* CONFIG_OTG_NETWORK_CDC */
+
diff -uNr linux/drivers/no-otg/functions/network/eem.c linux/drivers/otg/functions/network/eem.c
--- linux/drivers/no-otg/functions/network/eem.c	1970-01-01 01:00:00.000000000 +0100
+++ linux/drivers/otg/functions/network/eem.c	2006-09-01 21:41:28.000000000 +0200
@@ -0,0 +1,247 @@
+/*
+ * otg/functions/network/eem.c - Network Function Driver
+ *
+ *      Copyright (c) 2002, 2003, 2004 Belcarra
+ *
+ * By: 
+ *      Chris Lynne <cl@belcarra.com>
+ *      Stuart Lynne <sl@belcarra.com>
+ *      Bruce Balden <balden@belcarra.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ */
+/*!
+ * @file otg/functions/network/eem.c
+ * @brief This file implements the required descriptors to implement
+ * a eem network device with a single interface.
+ *
+ * The BASIC network driver implements a very simple descriptor set.
+ * A single interface with two BULK data endpoints and a optional
+ * INTERRUPT endpoint.
+ *
+ * @ingroup NetworkFunction
+ */
+
+
+#include <otg/otg-compat.h>
+#include <otg/otg-module.h>
+
+#include <linux/slab.h>
+#include <linux/interrupt.h>
+#include <linux/utsname.h>
+#include <linux/netdevice.h>
+
+#include <otg/usbp-chap9.h>
+#include <otg/usbp-func.h>
+
+#include "network.h"
+
+#define NTT network_fd_trace_tag
+
+#ifdef CONFIG_OTG_NETWORK_BASIC
+/* USB BASIC Configuration ******************************************************************** */
+
+/* BASIC Communication Interface Class descriptors
+ */
+/*! BULK OUT data endpoint descriptor
+ */
+static struct usbd_endpoint_descriptor eem_data_1 = { 
+    bLength:sizeof(struct usbd_endpoint_descriptor),
+    bDescriptorType:USB_DT_ENDPOINT,
+    bEndpointAddress:USB_DIR_OUT, 
+    bmAttributes: BULK,     
+    wMaxPacketSize: 0, 
+    bInterval: 0,
+};
+/*! BULK IN data endpoint descriptor
+ */
+static struct usbd_endpoint_descriptor eem_data_2 =// { 0x07, USB_DT_ENDPOINT, USB_DIR_IN,  BULK,     0, 0x00, 0x00, };
+{
+    bLength:sizeof(struct usbd_endpoint_descriptor),
+    bDescriptorType:USB_DT_ENDPOINT,
+    bEndpointAddress:USB_DIR_IN, 
+    bmAttributes: BULK,     
+    wMaxPacketSize: 0, 
+    bInterval: 0x0,
+};
+
+/*! INTERRUPT IN data endpoint descriptor
+ */
+static struct usbd_endpoint_descriptor eem_comm_1 = //{ 0x07, USB_DT_ENDPOINT, USB_DIR_IN,  INTERRUPT,0, 0x00, 0x0a, };
+{
+    bLength:sizeof(struct usbd_endpoint_descriptor),
+    bDescriptorType:USB_DT_ENDPOINT,
+    bEndpointAddress:USB_DIR_IN, 
+    bmAttributes: INTERRUPT,     
+    wMaxPacketSize: 0, 
+    bInterval: 0xa,
+};
+
+/*! Endpoint descriptor list
+ */
+static  struct usbd_endpoint_descriptor  *eem_default[] = { &eem_data_1, 
+    &eem_data_2, 
+    &eem_comm_1, };
+u8 eem_indexes[] = { BULK_OUT, BULK_IN, INT_IN, };
+
+/*! Data Interface Descriptor
+ */
+static struct usbd_interface_descriptor  eem_data_alternate_descriptor = {
+    bLength:sizeof(struct usbd_interface_descriptor),
+    bDescriptorType:USB_DT_INTERFACE,
+    bInterfaceNumber: 0x00, 
+    bAlternateSetting: 0x00, // bInterfaceNumber, bAlternateSetting
+    sizeof (eem_default) / sizeof(struct usbd_endpoint_descriptor *), // bNumEndpoints
+    bInterfaceClass:LINEO_CLASS, 
+    bInterfaceSubClass:LINEO_SUBCLASS_BASIC_NET, 
+    bInterfaceProtocol:LINEO_BASIC_NET_CRC, 
+    iInterface:0x00,
+};
+
+/*! Data Alternate Interface Description List
+ */
+static struct usbd_alternate_description eem_data_alternate_descriptions[] = {
+        { iInterface: CONFIG_OTG_NETWORK_BASIC_INTF,
+                interface_descriptor: (struct usbd_interface_descriptor *)&eem_data_alternate_descriptor,
+                endpoints:sizeof (eem_default) / sizeof(struct usbd_endpoint_descriptor *),
+                endpoint_list: eem_default,
+                endpoint_indexes: eem_indexes,
+                },
+};
+
+
+/* BASIC Data Interface Alternate descriptions and descriptors
+ */
+
+/* BASIC Interface descriptions and descriptors
+ */
+/*! Interface Description List
+ */
+struct usbd_interface_description eem_interfaces[] = {
+        { alternates:sizeof (eem_data_alternate_descriptions) / sizeof (struct usbd_alternate_description),
+                alternate_list:eem_data_alternate_descriptions,},
+};
+
+/* BASIC Configuration descriptions and descriptors
+ */
+/*! Configuration Descriptor 
+ */
+u8 eem_configuration_descriptor[sizeof(struct usbd_configuration_descriptor)] = {
+        0x09, USB_DT_CONFIGURATION, 0x00, 0x00, // wLength
+        sizeof (eem_interfaces) / sizeof (struct usbd_interface_description),
+        0x01, 0x00, // bConfigurationValue, iConfiguration
+        0, 0,
+};
+
+/*! Configuration Description List 
+ */
+struct usbd_configuration_description eem_description[] = {
+        { iConfiguration: CONFIG_OTG_NETWORK_BASIC_DESC,
+                configuration_descriptor: (struct usbd_configuration_descriptor *)eem_configuration_descriptor,
+        },
+
+};
+
+/* BASIC Device Description
+ */
+/*! Device Descriptor 
+ */
+static struct usbd_device_descriptor eem_device_descriptor = {
+        bLength: sizeof(struct usbd_device_descriptor),
+        bDescriptorType: USB_DT_DEVICE,
+        bcdUSB: __constant_cpu_to_le16(USB_BCD_VERSION),
+        bDeviceClass: COMMUNICATIONS_DEVICE_CLASS,
+        bDeviceSubClass: 0x02,
+        bDeviceProtocol: 0x00,
+        bMaxPacketSize0: 0x00,
+        idVendor: __constant_cpu_to_le16(CONFIG_OTG_NETWORK_VENDORID),
+        idProduct: __constant_cpu_to_le16(CONFIG_OTG_NETWORK_PRODUCTID),
+        bcdDevice: __constant_cpu_to_le16(CONFIG_OTG_NETWORK_BCDDEVICE),
+};
+
+#ifdef CONFIG_OTG_HIGH_SPEED
+/*! High Speed Device Qualifier Descriptor 
+ */
+static struct usbd_device_qualifier_descriptor eem_device_qualifier_descriptor = {
+        bLength: sizeof(struct usbd_device_qualifier_descriptor),
+        bDescriptorType: USB_DT_DEVICE_QUALIFIER,
+        bcdUSB: __constant_cpu_to_le16(USB_BCD_VERSION),
+        bDeviceClass: COMMUNICATIONS_DEVICE_CLASS,
+        bDeviceSubClass: 0x02,
+        bDeviceProtocol: 0x00,
+        bMaxPacketSize0: 0x00,
+};
+#endif /* CONFIG_OTG_HIGH_SPEED */
+
+/*! Endpoint Request List
+ */
+static struct usbd_endpoint_request eem_endpoint_requests[ENDPOINTS+1] = {
+        { 1, 0, 0, USB_DIR_OUT | USB_ENDPOINT_BULK, MAXFRAMESIZE + 48, MAXFRAMESIZE + 512, },
+        { 1, 0, 0, USB_DIR_IN | USB_ENDPOINT_BULK, MAXFRAMESIZE + 48, MAXFRAMESIZE + 512, },
+        { 1, 0, 0, USB_DIR_IN | USB_ENDPOINT_INTERRUPT | USB_ENDPOINT_OPT, 16, 64, },
+        { 0, },
+};
+
+/*! OTG Descriptor
+ */
+static struct usbd_otg_descriptor eem_otg_descriptor = {
+        bLength : sizeof(struct usbd_otg_descriptor),
+        bDescriptorType: USB_DT_OTG,
+        bmAttributes: 0,
+};
+
+/*! Device Description
+ */
+struct usbd_device_description eem_device_description = {
+        device_descriptor: &eem_device_descriptor,
+#ifdef CONFIG_OTG_HIGH_SPEED
+        device_qualifier_descriptor: &eem_device_qualifier_descriptor,
+#endif /* CONFIG_OTG_HIGH_SPEED */
+        otg_descriptor: &eem_otg_descriptor,
+        iManufacturer: CONFIG_OTG_NETWORK_MANUFACTURER,
+        iProduct: CONFIG_OTG_NETWORK_PRODUCT_NAME,
+#if !defined(CONFIG_OTG_NO_SERIAL_NUMBER) && defined(CONFIG_OTG_SERIAL_NUMBER_STR)
+        iSerialNumber:CONFIG_OTG_SERIAL_NUMBER_STR,
+#endif
+};
+
+
+/*! eem_init
+ * @param function The function instance
+ */
+void eem_init (struct usbd_function_instance *function)
+{
+        eem_data_alternate_descriptions[0].endpoints = Usb_network_private.have_interrupt ? 3 : 2;
+}
+
+/*! function driver description
+ */
+struct usbd_function_driver eem_function_driver = {
+        name: "network-BASIC",
+        fops: &net_fd_function_ops,
+        device_description: &eem_device_description,
+        bNumConfigurations: sizeof (eem_description) / sizeof (struct usbd_configuration_description),
+        configuration_description: eem_description,
+        idVendor: __constant_cpu_to_le16(CONFIG_OTG_NETWORK_VENDORID),
+        idProduct: __constant_cpu_to_le16(CONFIG_OTG_NETWORK_PRODUCTID),
+        bcdDevice: __constant_cpu_to_le16(CONFIG_OTG_NETWORK_BCDDEVICE),
+        bNumInterfaces:sizeof (eem_interfaces) / sizeof (struct usbd_interface_description),
+        interface_list:eem_interfaces,
+        endpointsRequested: ENDPOINTS,
+        requestedEndpoints: eem_endpoint_requests,
+};
+#endif                  /* CONFIG_OTG_NETWORK_BASIC */
+
diff -uNr linux/drivers/no-otg/functions/network/fermat.c linux/drivers/otg/functions/network/fermat.c
--- linux/drivers/no-otg/functions/network/fermat.c	1970-01-01 01:00:00.000000000 +0100
+++ linux/drivers/otg/functions/network/fermat.c	2006-09-01 21:41:30.000000000 +0200
@@ -0,0 +1,140 @@
+/*
+ * otg/functions/network/fermat.c - Network Function Driver
+ *
+ *      Copyright (c) 2003, 2004 Belcarra
+ *
+ * By: 
+ *      Bruce Balden <balden@belcarra.com>
+ *
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ */
+/*!
+ * @file otg/functions/network/fermat.c
+ * @brief This implements a special data munging function that
+ * randomizes data. This is a very specific fix for a device
+ * that had trouble with runs of zeros.
+ *
+ * @ingroup NetworkFunction
+ */
+
+#include <linux/config.h>
+#include <linux/module.h>
+#include <linux/kernel.h>
+#include <linux/version.h>
+
+#ifdef CONFIG_OTG_NETWORK_BLAN_FERMAT
+
+#include "fermat.h"
+
+#ifndef FERMAT_DEFINED
+typedef unsigned char BYTE;
+typedef struct fermat {
+        int length;
+        BYTE power[256];
+} FERMAT;
+#endif
+
+
+static int fermat_setup(FERMAT *p, int seed){
+        int i = 0;
+        unsigned long x,y;
+        y = 1;
+        do{
+                x = y;
+                p->power[i] = ( x == 256 ? 0 : x);
+                y = ( seed * x ) % 257;
+                i += 1;
+        }while( y != 1);
+        p->length = i;
+        return i;
+}
+
+static void fermat_xform(FERMAT *p, BYTE *data, int length){
+        BYTE *pw = p->power;
+        int   i, j;
+        BYTE * q ;
+        for(i = 0, j=0, q = data; i < length; i++, j++, q++){
+                if(j>=p->length){
+                        j = 0;
+                }
+                *q ^= pw[j];
+        }
+}
+
+static FERMAT default_fermat;
+static const int primitive_root = 5;
+void fermat_init(){
+        (void) fermat_setup(&default_fermat, primitive_root); 
+}
+
+// Here are the public official versions.
+// Change the primitive_root above to another primitive root
+// if you need better scatter. Possible values are 3 and 7
+
+
+void fermat_encode(BYTE *data, int length){
+        fermat_xform(&default_fermat, data, length);
+}
+
+void fermat_decode(BYTE *data, int length){
+        fermat_xform(&default_fermat, data, length);
+}
+
+
+// Note: the seed must be a "primitive root" of 257. This means that
+// the return value of the setup routine must be 256 (otherwise the
+// seed is not a primitive root.  The routine will still work fine
+// but will be less pseudo-random.
+
+#undef TEST 
+#if TEST
+#include <stdio.h>
+#include <memory.h>
+
+// Use FERMAT in two ways: to encode, and to generate test data.
+
+main(){
+        //Note 3, 5, and 7 are primitive roots of 257
+        // 11 is not a primitive root
+        FERMAT three, five, seven;
+
+        FERMAT three2;
+        printf("Cycle lengths: 3,5,7 %d %d %d \n", 
+                        fermat_setup(&three, 3), 
+                        fermat_setup(&five, 5), 
+                        fermat_setup(&seven, 7));
+        three2=three; // Copy data from three
+        fermat_xform(&three,three2.power,three2.length);
+        fermat_xform(&five,three2.power,three2.length);
+        fermat_xform(&seven,three2.power,three2.length);
+        fermat_xform(&seven,three2.power,three2.length);
+        fermat_xform(&five,three2.power,three2.length);
+        fermat_xform(&three,three2.power,three2.length);
+
+        //At this stage, three2 and three should be identical
+        if(memcpy(&three,&three2,sizeof(FERMAT))){
+                printf("Decoded intact\n");
+        }
+
+        fermat_init();
+        fermat_encode(three2.power,256);
+
+}
+#endif
+
+#endif /* CONFIG_OTG_NETWORK_BLAN_FERMAT */
+
diff -uNr linux/drivers/no-otg/functions/network/fermat.h linux/drivers/otg/functions/network/fermat.h
--- linux/drivers/no-otg/functions/network/fermat.h	1970-01-01 01:00:00.000000000 +0100
+++ linux/drivers/otg/functions/network/fermat.h	2006-09-01 21:41:30.000000000 +0200
@@ -0,0 +1,46 @@
+/*
+ * otg/functions/network/fermat.h - Network Function Driver
+ *
+ *      Copyright (c) 2003, 2004 Belcarra
+ *
+ * By: 
+ *      Bruce Balden <balden@belcarra.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ *
+ */
+/*!
+ * @file otg/functions/network/fermat.h
+ * @brief Fermat related data structures.
+ *
+ *
+ * @ingroup NetworkFunction
+ */
+
+#ifndef FERMAT_DEFINED
+#define FERMAT_DEFINED 1
+typedef unsigned char BYTE;
+typedef struct fermat {
+        int length;
+        BYTE power[256];
+} FERMAT;
+
+void fermat_init(void);
+void fermat_encode(BYTE *data, int length);
+void fermat_decode(BYTE *data, int length);
+#endif
+
+
diff -uNr linux/drivers/no-otg/functions/network/net-fd.c linux/drivers/otg/functions/network/net-fd.c
--- linux/drivers/no-otg/functions/network/net-fd.c	1970-01-01 01:00:00.000000000 +0100
+++ linux/drivers/otg/functions/network/net-fd.c	2006-09-01 21:41:30.000000000 +0200
@@ -0,0 +1,1463 @@
+/*
+ * otg/functions/network/net-fd.c - Network Function Driver
+ *
+ *      Copyright (c) 2002, 2003, 2004 Belcarra
+ *
+ * By: 
+ *      Tom Rushworth <tbr@belcarra.com>
+ *      Chris Lynne <cl@belcarra.com>
+ *      Stuart Lynne <sl@belcarra.com>
+ *      Bruce Balden <balden@belcarra.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ */
+/*!
+ * @file otg/functions/network/net-fd.c
+ * @brief The lower edge (USB Device Function) implementation of
+ * the Network Function Driver. This performs the core protocol
+ * handling and data encpasulation.
+ *
+ * This implements the lower edge (USB Device) layer of the Network Function
+ * Driver. Specifically the data encapsulation, envent and protocol handlers.
+ *
+ *
+ *
+ * This network function driver intended to interoperate with 
+ * Belcarra's USBLAN Class drivers. 
+ *
+ * These are available for Windows, Linux and Mac OSX. For more 
+ * information and to download a copy for testing:
+ *
+ *      http://www.belcarra.com/usblan/
+ *
+ * When configured for CDC it can also work with any CDC Class driver.
+ *
+ *
+ * @ingroup NetworkFunction
+ */
+
+
+#include <otg/otg-compat.h>
+#include <otg/otg-module.h>
+//#include <linux/config.h>
+//#include <linux/module.h>
+#include <linux/list.h>
+#include <linux/netdevice.h>
+#include <linux/skbuff.h>
+#include <linux/etherdevice.h>
+#include <net/arp.h>
+#include <linux/rtnetlink.h>
+#include <linux/smp_lock.h>
+#include <linux/ctype.h>
+#include <linux/time.h>
+#include <linux/timer.h>
+#include <linux/string.h>
+#include <linux/atmdev.h>
+#include <linux/pkt_sched.h>
+#include <linux/random.h>
+#include <linux/utsname.h>
+
+#include <linux/ip.h>
+#include <linux/if_ether.h>
+#include <linux/in.h>
+#include <linux/inetdevice.h>
+
+#include <linux/kmod.h>
+
+#include <asm/uaccess.h>
+#include <asm/system.h>
+
+#include <otg/usbp-chap9.h>
+//#include <otg/usbd-mem.h>
+#include <otg/usbp-func.h>
+
+#include <otg/otg-trace.h>
+#include <otg/otg-api.h>
+
+#include "network.h"
+#include "net-fd.h"
+#include "net-os.h"
+#ifdef CONFIG_OTG_NETWORK_BLAN_FERMAT
+#include "fermat.h"
+#endif
+
+otg_tag_t network_fd_trace_tag;
+#define NTT network_fd_trace_tag
+
+#if defined(CONFIG_OTG_NETWORK_CDC)
+void cdc_init(struct usbd_function_instance *);
+extern struct usbd_function_driver cdc_function_driver;
+#endif
+
+#ifdef CONFIG_OTG_NETWORK_EEM
+void eem_init(struct usbd_function_instance *);
+extern struct usbd_function_driver eem_function_driver;
+#endif
+
+#ifdef CONFIG_OTG_NETWORK_BASIC
+void basic_init(struct usbd_function_instance *);
+extern struct usbd_function_driver basic_function_driver;
+#endif
+
+#ifdef CONFIG_OTG_NETWORK_BASIC2
+void basic2_init(struct usbd_function_instance *);
+extern struct usbd_function_driver basic2_function_driver;
+#endif
+
+#ifdef CONFIG_OTG_NETWORK_SAFE
+void safe_init(struct usbd_function_instance *);
+extern struct usbd_function_driver safe_function_driver;
+#endif
+
+#ifdef CONFIG_OTG_NETWORK_BLAN
+void blan_init(struct usbd_function_instance *);
+extern struct usbd_function_driver blan_function_driver;
+#endif
+
+static char ip_addr_str[20];
+u32 ip_addr;
+u32 router_ip;
+u32 network_mask;
+u32 dns_server_ip;
+
+//_________________________________________________________________________________________________
+
+/*
+ * If the following are defined we implement the crc32_copy routine using
+ * Duff's device. This will unroll the copy loop by either 4 or 8. Do not
+ * use these without profiling to test if it actually helps on any specific
+ * device.
+ */
+#undef CONFIG_OTG_NETWORK_CRC_DUFF4
+#undef CONFIG_OTG_NETWORK_CRC_DUFF8
+
+static u32 *network_crc32_table;
+
+#define CRC32_INIT   0xffffffff      // Initial FCS value
+#define CRC32_GOOD   0xdebb20e3      // Good final FCS value
+
+#define CRC32_POLY   0xedb88320      // Polynomial for table generation
+        
+#define COMPUTE_FCS(val, c) (((val) >> 8) ^ network_crc32_table[((val) ^ (c)) & 0xff])
+
+//_________________________________________________________________________________________________
+//                                      crc32_copy
+
+/*! make_crc_table
+ * Generate the crc32 table
+ *
+ * @return non-zero if malloc fails
+ */
+STATIC int make_crc_table(void)
+{
+        u32 n;
+        RETURN_ZERO_IF(network_crc32_table);
+        RETURN_ENOMEM_IF(!(network_crc32_table = (u32 *)ckmalloc(256*4, GFP_KERNEL)));
+        for (n = 0; n < 256; n++) {
+                int k;
+                u32 c = n;
+                for (k = 0; k < 8; k++) {
+                        c =  (c & 1) ? (CRC32_POLY ^ (c >> 1)) : (c >> 1);
+                }
+                network_crc32_table[n] = c;
+        }
+        return 0;
+}
+
+#if !defined(CONFIG_OTG_NETWORK_CRC_DUFF4) && !defined(CONFIG_OTG_NETWORK_CRC_DUFF8)
+/*! crc32_copy
+ * Copies a specified number of bytes, computing the 32-bit CRC FCS as it does so.
+ *
+ * @param dst   Pointer to the destination memory area.
+ * @param src   Pointer to the source memory area.
+ * @param len   Number of bytes to copy.
+ * @param val   Starting value for the CRC FCS.
+ *
+ * @return      Final value of the CRC FCS.
+ *
+ * @sa crc32_pad
+ */
+static u32 __inline__ crc32_copy (u8 *dst, u8 *src, int len, u32 val)
+{
+        for (; len-- > 0; val = COMPUTE_FCS (val, *dst++ = *src++));
+        return val;
+}
+
+#else /* DUFFn */
+
+/*! crc32_copy
+ * Copies a specified number of bytes, computing the 32-bit CRC FCS as it does so.
+ *
+ * @param dst   Pointer to the destination memory area.
+ * @param src   Pointer to the source memory area.
+ * @param len   Number of bytes to copy.
+ * @param val   Starting value for the CRC FCS.
+ *
+ * @return      Final value of the CRC FCS.
+ *
+ * @sa crc32_pad
+ */
+static u32 crc32_copy (u8 *dst, u8 *src, int len, u32 val)
+{
+#if defined(CONFIG_OTG_NETWORK_CRC_DUFF8)
+        int n = (len + 7) / 8;
+        switch (len % 8) 
+#elif defined(CONFIG_OTG_NETWORK_CRC_DUFF4)
+                int n = (len + 3) / 4;
+        switch (len % 4) 
+#endif
+        {
+        case 0: do {
+                        val = COMPUTE_FCS (val, *dst++ = *src++);
+#if defined(CONFIG_OTG_NETWORK_CRC_DUFF8)
+                case 7:
+                        val = COMPUTE_FCS (val, *dst++ = *src++);
+                case 6:
+                        val = COMPUTE_FCS (val, *dst++ = *src++);
+                case 5:
+                        val = COMPUTE_FCS (val, *dst++ = *src++);
+                case 4:
+                        val = COMPUTE_FCS (val, *dst++ = *src++);
+#endif
+                case 3:
+                        val = COMPUTE_FCS (val, *dst++ = *src++);
+                case 2:
+                        val = COMPUTE_FCS (val, *dst++ = *src++);
+                case 1:
+                        val = COMPUTE_FCS (val, *dst++ = *src++);
+                } while (--n > 0);
+        }
+        return val;
+}
+#endif /* DUFFn */
+
+
+//_________________________________________________________________________________________________
+//                                      crc32_pad
+
+/*! crc32_pad - pad and calculate crc32
+ *
+ * @return CRC FCS
+ */
+static u32 __inline__ crc32_pad (u8 *dst, int len, u32 val)
+{
+        for (; len-- > 0; val = COMPUTE_FCS (val, *dst++ = '\0'));
+        return val;
+}
+
+//_________________________________________________________________________________________________
+//                                      net_fd_send_int
+//
+
+/*! net_fd_urb_sent_int - callback for completed INT URB
+ *
+ * Handles notification that an urb has been sent (successfully or otherwise).
+ *
+ * @return non-zero for failure.
+ */
+STATIC int net_fd_urb_sent_int (struct usbd_urb *urb, int urb_rc)
+{
+        unsigned long flags;
+        int rc = -EINVAL;
+        struct usb_network_private *npd = urb->function_privdata;
+
+        TRACE_MSG3(NTT,"urb: %p npd: %p urb_rc: %d", urb, npd, urb_rc);
+
+        local_irq_save(flags);
+        npd->int_urb = NULL;
+        usbd_free_urb (urb);
+        local_irq_restore(flags);
+        return 0;
+}
+
+/*! net_fd_send_int_blan - send an interrupt notification response
+ *
+ * Generates a response urb on the notification (INTERRUPT) endpoint.
+ *
+ * This is called from either a scheduled task or from the process context
+ * that calls network_open() or network_close().
+ * This must be called with interrupts locked out as net_fd_event_handler can
+ * change the NETWORK_ATTACHED status
+ *
+ */
+STATIC void net_fd_send_int_blan(struct usb_network_private *npd, int connected, int data)
+{
+        struct usbd_urb *urb;
+        struct cdc_notification_descriptor *cdc;
+        int rc;
+        struct usbd_function_instance *function = (npd?npd->function:NULL);
+
+        TRACE_MSG2(NTT,"npd=%p function=%p",npd,function);
+
+        do {
+                BREAK_IF(!function);
+
+
+                BREAK_IF(npd->network_type != network_blan);
+                BREAK_IF(!npd->have_interrupt);
+
+                BREAK_IF(!(npd->flags & NETWORK_ATTACHED));
+
+                TRACE_MSG3(NTT,"connected: %d network: %d %d", connected, 
+                           npd->network_type, network_blan);
+
+                BREAK_IF(usbd_get_device_status(function) != USBD_OK);
+
+                //if (npd->int_urb) {
+                //        printk(KERN_INFO"%s: int_urb: %p\n", __FUNCTION__, npd->int_urb);
+                //        usbd_cancel_urb_irq(npd->int_urb);
+                //        npd->int_urb = NULL;
+                //}
+
+                BREAK_IF(!(urb = usbd_alloc_urb (function, INT_IN, 
+                                                sizeof(struct cdc_notification_descriptor), net_fd_urb_sent_int)));
+                
+                urb->actual_length = sizeof(struct cdc_notification_descriptor);
+                memset(urb->buffer, 0, sizeof(struct cdc_notification_descriptor));
+                urb->function_privdata = npd;
+
+                cdc = (struct cdc_notification_descriptor *)urb->buffer;
+
+                cdc->bmRequestType = 0xa1;
+
+                if (data) {
+                        cdc->bNotification = 0xf0;
+                        cdc->wValue = 1;
+                }
+                else {
+                        cdc->bNotification = 0x00;
+                        cdc->wValue = connected ? 0x01 : 0x00;
+                }
+                cdc->wIndex = 0x00; // XXX interface - check that this is correct
+
+
+                npd->int_urb = urb;
+                TRACE_MSG1(NTT,"int_urb: %p", urb);
+                BREAK_IF (!(rc = usbd_start_in_urb (urb)));
+
+                TRACE_MSG1(NTT,"usbd_start_in_urb failed err: %x", rc);
+                printk(KERN_ERR"%s: usbd_start_in_urb failed err: %x\n", __FUNCTION__, rc);
+                urb->function_privdata = NULL;
+                npd->int_urb = NULL;
+                usbd_free_urb (urb);
+
+        } while(0);
+}
+
+//_________________________________________________________________________________________________
+//                                      net_fd_start_xmit
+
+/*! net_fd_urb_sent_bulk - callback for completed BULK xmit URB
+ *
+ * Handles notification that an urb has been sent (successfully or otherwise).
+ *
+ * @param urb     Pointer to the urb that has been sent.
+ * @param urb_rc  Result code from the send operation.
+ *
+ * @return non-zero for failure.
+ */
+STATIC int net_fd_urb_sent_bulk (struct usbd_urb *urb, int urb_rc)
+{
+        unsigned long flags;
+        void *buff_ctx;
+        int rc = -EINVAL;
+
+        TRACE_MSG2(NTT,"urb: %p urb_rc: %d", urb, urb_rc);
+
+        local_irq_save(flags);
+        do {
+
+                BREAK_IF(!urb);
+                buff_ctx = urb->function_privdata;
+                TRACE_MSG2(NTT,"urb: %p buff_ctx: %p", urb, buff_ctx);
+                urb->function_privdata = NULL;
+
+                BREAK_IF(net_os_xmit_done(urb->function_instance,buff_ctx,urb_rc));
+
+                usbd_free_urb (urb);
+                rc = 0;
+
+        } while (0);
+        local_irq_restore(flags);
+        return rc;
+}
+
+/*! net_fd_start_xmit - start sending a buffer
+ *
+ * Called with net_os_mutex_enter()d.
+ *
+ * @return: 0 if all OK
+ *         -EINVAL, -EUNATCH, -ENOMEM
+ *         rc from usbd_start_in_urb() if that fails (is != 0, may be one of err values above) 
+ *         Note: -ECOMM is interpreted by calling routine as signal to leave IF stopped.
+ */
+STATIC int net_fd_start_xmit (struct usb_network_private *npd, u8 *buff, int len, void *buff_ctx)
+{
+        struct usbd_function_instance *function = (npd?npd->function:NULL);
+        struct usbd_urb *urb = NULL;
+        int rc;
+        int in_pkt_sz;
+        u8 *cp;
+        u32 crc;
+        TRACE_MSG2(NTT,"npd=%p function=%p",npd,function);
+
+#if 0
+        printk(KERN_INFO"%s: %s len: %d encap: %d\n", __FUNCTION__, net_device->name, len, npd->encapsulation);
+        printk(KERN_INFO"start_xmit: len: %x data: %p\n", len, buff);
+        {
+                u8 *cp = buff;
+                int i;
+                for (i = 0; i < len; i++) {
+                        if ((i%32) == 0) {
+                                printk("\ntx[%2x] ", i);
+                        }
+                        printk("%02x ", *cp++);
+                }
+                printk("\n");
+        }
+#endif
+
+        if (!(npd->flags & NETWORK_ATTACHED)) {
+                return -EUNATCH;
+        }
+        if (usbd_get_device_status(function) != USBD_OK) {
+                return -EINVAL;
+        }
+
+#if defined(CONFIG_OTG_NETWORK_CDC)
+        // verify interface is enabled - non-zero altsetting means data is enabled
+        if (!usbd_interface_AltSetting(function, DATA_INTF)) {
+                return -EINVAL;
+        }
+#endif
+        in_pkt_sz = usbd_endpoint_wMaxPacketSize(function, BULK_IN, usbd_high_speed(function));
+
+        if (npd->encapsulation != simple_crc) {
+                TRACE_MSG0(NTT,"unknown encapsulation");
+                printk(KERN_ERR"%s: unknown encapsulation\n", __FUNCTION__);
+                // Since we are now only really using one, just fix it.
+                npd->encapsulation = simple_crc;
+        }
+        //TRACE_MSG0(NTT,"SIMPLE_CRC");
+        // allocate urb 5 bytes larger than required
+        if (!(urb = usbd_alloc_urb (function, BULK_IN, len + 5 + 4 + in_pkt_sz, net_fd_urb_sent_bulk ))) {
+                u8 epa = usbd_endpoint_bEndpointAddress(function, BULK_IN, usbd_high_speed(function));
+                TRACE_MSG2(NTT,"urb alloc failed len: %d endpoint: %02x", len, epa);
+                printk(KERN_ERR"%s: urb alloc failed len: %d endpoint: %02x\n", __FUNCTION__, len, epa);
+                return -ENOMEM;
+        }
+
+        switch (npd->network_type) {
+        case network_eem:
+                cp = urb->buffer + 2;
+                urb->actual_length = len + 2;
+                break;
+        default:
+                cp = urb->buffer;
+                urb->actual_length = len;
+                break;
+        }
+        // copy and crc len bytes
+        crc = crc32_copy(cp, buff, len, CRC32_INIT);
+
+        switch (npd->network_type) {
+        case network_eem:
+                break;
+        default:
+                if ((urb->actual_length % in_pkt_sz) == (in_pkt_sz - 4)) {
+
+                        #undef CONFIG_OTG_NETWORK_PADBYTE
+                        #ifdef CONFIG_OTG_NETWORK_PADBYTE
+                        // add a pad byte if required to ensure a short packet, usbdnet driver
+                        // will correctly handle pad byte before or after CRC, but the MCCI driver
+                        // wants it before the CRC.
+                        crc = crc32_pad(urb->buffer + urb->actual_length, 1, crc);
+                        urb->actual_length++;
+                        #else /* CONFIG_OTG_NETWORK_PADBYTE */
+                        urb->flags |= USBD_URB_SENDZLP; 
+                        TRACE_MSG2(NTT,"setting ZLP: urb: %p flags: %x", urb, urb->flags);
+                        #endif /* CONFIG_OTG_NETWORK_PADBYTE */
+                }
+                break;
+        }
+        // munge and append crc
+        crc = ~crc;
+        urb->buffer[urb->actual_length++] = crc & 0xff;
+        urb->buffer[urb->actual_length++] = (crc >> 8) & 0xff;
+        urb->buffer[urb->actual_length++] = (crc >> 16) & 0xff;
+        urb->buffer[urb->actual_length++] = (crc >> 24) & 0xff;
+        // End of CRC processing
+        //
+        switch (npd->network_type) {
+        case network_eem:
+                break;
+        default:
+                break;
+        }
+
+        TRACE_MSG3(NTT,"urb=%p buff_ctx=%p priv=%p",urb,buff_ctx,urb->function_privdata);
+        urb->function_privdata = (void *) buff_ctx;
+#if 0
+        printk(KERN_INFO"start_xmit: len: %d : %d data: %p\n", skb->len, urb->actual_length, urb->buffer);
+        {
+                u8 *cp = urb->buffer;
+                int i;
+                for (i = 0; i < urb->actual_length; i++) {
+                        if ((i%32) == 0) {
+                                printk("\ntx[%2x] ", i);
+                        }
+                        printk("%02x ", *cp++);
+                }
+                printk("\n");
+        }
+#endif
+#if defined(CONFIG_OTG_NETWORK_BLAN_FERMAT)
+        if (npd->fermat) {
+                fermat_encode(urb->buffer, urb->actual_length);
+        }
+#endif
+        TRACE_MSG1(NTT,"sending urb: %p", urb);
+        if ((rc = usbd_start_in_urb (urb))) {
+
+                TRACE_MSG1(NTT,"FAILED: %d", rc);
+                printk(KERN_ERR"%s: FAILED: %d\n", __FUNCTION__, rc);
+                urb->function_privdata = NULL;
+                usbd_free_urb (urb);
+
+                return(rc);
+        }
+        #if 0
+        {
+                static int xmit_count = 0;
+                if (xmit_count++ == 100) {
+                        TRACE_MSG1(NTT, "halt test: %02x", BULK_IN);
+                        usbd_halt_endpoint(function, BULK_IN);
+                }
+        }
+        #endif
+
+        TRACE_MSG0(NTT,"OK");
+        return 0;
+}
+
+
+//_________________________________________________________________________________________________
+
+/*! net_fd_recv_urb - callback to process a received URB
+ *
+ * @return non-zero for failure.
+ */
+STATIC int net_fd_recv_urb(struct usbd_urb *urb, int rc)
+{
+        struct usbd_function_instance *function = urb->function_instance;
+        struct usb_network_private *npd = urb->function_privdata;
+        void *buff_ctx = NULL;
+        u8 *buff;
+        int crc_bad = 0;
+        int trim = 0;
+        int len;
+        int out_pkt_sz;
+        u32 crc;
+
+#if 0
+        printk(KERN_INFO"%s: urb: %p len: %d maxtransfer: %d encap: %d\n", __FUNCTION__, 
+                        urb, urb->actual_length, npd->maxtransfer, npd->encapsulation);
+
+        {
+                u8 *cp = urb->buffer;
+                int i;
+                for (i = 0; i < urb->actual_length; i++) {
+                        if ((i%32) == 0) {
+                                printk("\n[%2x] ", i);
+                        }
+                        printk("%02x ", *cp++);
+                }
+                printk("\n");
+        }
+#endif
+        if (!urb || !urb->function_instance || !npd || !npd->function ||
+            urb->function_instance != npd->function) {
+                TRACE_MSG4(NTT,"urb=%p npd=%p u->f=%p n->f=%p",
+                                 urb,(urb?npd:NULL),(urb?urb->function_instance:NULL),
+                                 ((urb&&npd)?npd->function:NULL));
+        }
+
+        THROW_IF(urb->status != RECV_OK, error);
+
+        out_pkt_sz = usbd_endpoint_wMaxPacketSize(function, BULK_OUT, usbd_high_speed(function));
+        // There is only one working encapsulation.
+        if (npd->encapsulation != simple_crc) {
+                 npd->encapsulation = simple_crc;
+        }
+
+        len = urb->actual_length;
+        trim = 0;
+        buff_ctx = net_os_alloc_buff(npd, &buff, len);
+        THROW_IF((NULL == buff_ctx), error);
+
+#if defined(CONFIG_OTG_NETWORK_BLAN_PADAFTER)
+        {
+                /* This version simply checks for a correct CRC along the 
+                 * entire packet. Some UDC's have trouble with some packet
+                 * sizes, this allows us to add pad bytes after the CRC.
+                 */
+
+                u8 *src = urb->buffer;
+                int copied;
+
+                // XXX this should work, but the MIPS optimizer seems to get it wrong....
+                //copied = (len < out_pkt_sz) ? 0 : ((len / out_pkt_sz) - 1) * out_pkt_sz;
+                        
+                if (len < out_pkt_sz*2) 
+                        copied = 0;
+                else {
+                        int pkts = ((len - out_pkt_sz) / out_pkt_sz);
+                        copied = (pkts - 1) * out_pkt_sz;
+                }
+
+                len -= copied;
+                crc = CRC32_INIT;
+                for (; copied-- > 0 ; crc = COMPUTE_FCS (crc, *buff++ = *src++));
+
+                for (; (len-- > 0) && (CRC32_GOOD != crc); crc = COMPUTE_FCS (crc, *buff++ = *src++));
+
+                trim = len + 4;
+
+                if (CRC32_GOOD != crc) {
+                        TRACE_MSG1(NTT,"AAA frame: %03x", urb->framenum);
+                        THROW_IF(npd->crc, crc_error);
+                }
+                else 
+                        npd->crc = 1;
+        }
+#else
+        /* 
+         * The CRC can be sent in two ways when the size of the transfer 
+         * ends up being a multiple of the packetsize:
+         *
+         *                                           |
+         *                <data> <CRC><CRC><CRC><CRC>|<???>     case 1
+         *                <data> <NUL><CRC><CRC><CRC>|<CRC>     case 2
+         *           <data> <NUL><CRC><CRC><CRC><CRC>|          case 3
+         *     <data> <NUL><CRC><CRC><CRC>|<CRC>     |          case 4
+         *                                           |
+         *        
+         * This complicates CRC checking, there are four scenarios:
+         *
+         *      1. length is 1 more than multiple of packetsize with a trailing byte
+         *      2. length is 1 more than multiple of packetsize 
+         *      3. length is multiple of packetsize
+         *      4. none of the above
+         *
+         * Finally, even though we always compute CRC, we do not actually throw
+         * things away until and unless we have previously seen a good CRC.
+         * This allows backwards compatibility with hosts that do not support
+         * adding a CRC to the frame.
+         *
+         */
+
+        // test if 1 more than packetsize multiple
+        if (1 == (len % out_pkt_sz)) {
+
+                // copy and CRC up to the packetsize boundary
+                crc = crc32_copy(buff, urb->buffer, len - 1, CRC32_INIT);
+                buff += len - 1;
+
+                // if the CRC is good then this is case 1
+                if (CRC32_GOOD != crc) {
+
+                        crc = crc32_copy(buff, urb->buffer + len - 1, 1, crc);
+                        buff += 1;
+
+                        if (CRC32_GOOD != crc) {
+                                //crc_errors[len%64]++;
+                                TRACE_MSG2(NTT,"A CRC error %08x %03x", crc, urb->framenum);
+                                THROW_IF(npd->crc, crc_error);
+                        }
+                        else 
+                                npd->crc = 1;
+                }
+                else 
+                        npd->crc = 1;
+        }
+        else {
+                crc = crc32_copy(buff, urb->buffer, len, CRC32_INIT);
+                buff += len;
+
+                if (CRC32_GOOD != crc) {
+                        //crc_errors[len%64]++;
+                        TRACE_MSG2(NTT,"B CRC error %08x %03x", crc, urb->framenum);
+                        THROW_IF(npd->crc, crc_error);
+                }
+                else 
+                        npd->crc = 1;
+        }
+        // trim IFF we are paying attention to crc
+        if (npd->crc) 
+                trim = 4;
+#endif
+        // catch a simple error, just increment missed error and general error
+        CATCH(error) {
+                TRACE_MSG4(NTT,"CATCH(error) urb: %p status: %d len: %d function: %p",
+                                urb, urb->status, urb->actual_length, function);
+                // catch a CRC error
+                CATCH(crc_error) {
+                        crc_bad = 1;
+#if 0
+                        printk(KERN_INFO"%s: urb: %p status: %d len: %d maxtransfer: %d encap: %d\n", __FUNCTION__, 
+                                        urb, urb->status, urb->actual_length, npd->maxtransfer, 
+                                        npd->encapsulation);
+
+                        {
+                                u8 *cp = urb->buffer;
+                                int i;
+                                for (i = 0; i < urb->actual_length; i++) {
+                                        if ((i%32) == 0) {
+                                                printk("\n[%2x] ", i);
+                                        }
+                                        printk("%02x ", *cp++);
+                                }
+                                printk("\n");
+                        }
+#endif
+                }
+        }
+        net_os_recv_buff(npd,buff_ctx,crc_bad,trim);
+        TRACE_MSG1(NTT,"restart: %p", urb);
+        return (usbd_start_out_urb (urb));
+}
+/*! net_fd_recv_urb_eem - callback to process a received URB
+ *
+ * @return non-zero for failure.
+ */
+STATIC int net_fd_recv_urb_eem(struct usbd_urb *urb, int rc)
+{
+        return 0;
+}
+
+//_________________________________________________________________________________________________
+//                                      net_fd_device_request
+//
+/*! net_fd_urb_received_ep0 - callback for sent URB
+ *
+ * Handles notification that an urb has been sent (successfully or otherwise).
+ *
+ * @return non-zero for failure.
+ */
+STATIC int net_fd_urb_received_ep0 (struct usbd_urb *urb, int urb_rc)
+{
+        TRACE_MSG2(NTT,"urb: %p status: %d", urb, urb->status);
+
+        printk(KERN_INFO"%s:\n", __FUNCTION__);
+        RETURN_EINVAL_IF (RECV_OK != urb->status);
+
+        // TRACE_MSG1(NTT,"%s", urb->buffer); // QQSV  is this really a NUL-terminated string???
+
+        printk(KERN_INFO"%s: ok\n", __FUNCTION__);
+        return -EINVAL;         // caller will de-allocate
+}
+
+/*! net_fd_device_request - process a received SETUP URB
+ *
+ * Processes a received setup packet and CONTROL WRITE data.
+ * Results for a CONTROL READ are placed in urb->buffer.
+ *
+ * @return non-zero for failure.
+ */
+STATIC int net_fd_device_request (struct usbd_function_instance *function, struct usbd_device_request *request)
+{
+        struct usb_network_private *npd = (struct usb_network_private *) (function->privdata);
+        struct usbd_urb *urb;
+        int index;
+
+        // Verify that this is a USB Class request per CDC specification or a vendor request.
+        RETURN_ZERO_IF (!(request->bmRequestType & (USB_REQ_TYPE_CLASS | USB_REQ_TYPE_VENDOR)));
+
+        // Determine the request direction and process accordingly
+        switch (request->bmRequestType & (USB_REQ_DIRECTION_MASK | USB_REQ_TYPE_MASK)) {
+
+        case USB_REQ_HOST2DEVICE | USB_REQ_TYPE_VENDOR:
+
+                switch (request->bRequest) {
+                case MCCI_ENABLE_CRC:
+                        if (make_crc_table()) 
+                                return -EINVAL;
+                        npd->encapsulation = simple_crc;
+                        return 0;
+
+                case BELCARRA_PING:
+                        TRACE_MSG1(NTT,"H2D VENDOR IP: %08x", ip_addr);
+                        if ((npd->network_type == network_blan)) 
+                                net_os_send_notification_later(npd);
+                        break;
+
+#if !defined(CONFIG_OTG_NETWORK_BLAN_DO_NOT_SETTIME) || !defined(CONFIG_OTG_NETWORK_SAFE_DO_NOT_SETTIME) 
+                case BELCARRA_SETTIME:
+                        {
+#if defined(LINUX24)
+                                struct timeval tv;
+#else
+				struct timespec tv;
+#endif
+				memset(&tv, 0, sizeof(tv));
+
+                                // wIndex and wLength contain RFC868 time - seconds since midnight 1 jan 1900
+
+                                tv.tv_sec = ntohl( request->wValue << 16 | request->wIndex);
+                      //        tv.tv_usec = 0;
+
+                                // convert to Unix time - seconds since midnight 1 jan 1970
+                                
+                                tv.tv_sec -= RFC868_OFFSET_TO_EPOCH; 
+
+                                TRACE_MSG1(NTT,"H2D VENDOR TIME: %08x", tv.tv_sec);
+
+                                // set the time
+                                do_settimeofday(&tv);
+                        } break;
+#endif
+                case BELCARRA_SETIP:
+                        ip_addr = ntohl( request->wValue << 16 | request->wIndex);
+                        // XXX need to get in correct order here
+                        npd->local_dev_addr[2] = (ip_addr >> 24) & 0xff;
+                        npd->local_dev_addr[3] = (ip_addr >> 16) & 0xff;
+                        npd->local_dev_addr[4] = (ip_addr >> 8) & 0xff;
+                        npd->local_dev_addr[5] = (ip_addr >> 0) & 0xff;
+#ifdef CONFIG_OTG_NETWORK_BLAN_IPADDR
+                        snprintf(ip_addr_str, sizeof(ip_addr_str), "%d.%d.%d.%d", 
+                                        (ip_addr >> 24) & 0xff, (ip_addr >> 16) & 0xff,
+                                        (ip_addr >> 8) & 0xff, (ip_addr) & 0xff);
+                        index = usbd_realloc_string(
+                                        npd->function_driver->device_description->device_descriptor->iProduct, ip_addr_str);
+#endif /* CONFIG_OTG_NETWORK_IPADDR */
+                        break;
+
+                case BELCARRA_SETMSK:
+                        network_mask = ntohl( request->wValue << 16 | request->wIndex);
+                        break;
+
+                case BELCARRA_SETROUTER:
+                        router_ip = ntohl( request->wValue << 16 | request->wIndex);
+                        break;
+
+                case BELCARRA_SETDNS:
+                        dns_server_ip = ntohl( request->wValue << 16 | request->wIndex);
+                        break;
+#ifdef CONFIG_OTG_NETWORK_BLAN_FERMAT
+                case BELCARRA_SETFERMAT:
+                        npd->fermat = 1;
+                        break;
+#endif
+#ifdef CONFIG_OTG_NETWORK_BLAN_HOSTNAME
+                case BELCARRA_HOSTNAME:
+                        TRACE_MSG0(NTT,"HOSTNAME");
+                        RETURN_EINVAL_IF(!(urb = usbd_alloc_urb_ep0(function, le16_to_cpu(request->wLength), 
+                                                        net_fd_urb_received_ep0) ));
+                        RETURN_ZERO_IF(!usbd_start_out_urb(urb));                  // return if no error
+                        usbd_free_urb(urb);                                  // de-alloc if error
+                        return -EINVAL;
+#endif
+#ifdef CONFIG_OTG_NETWORK_BLAN_DATA_NOTIFY_OK
+                case BELCARRA_DATA_NOTIFY:
+                        TRACE_MSG0(NTT,"DATA NOTIFY");
+                        npd->data_notify = 1;
+                        return -EINVAL;
+#endif
+                }
+                return 0;
+#if 0
+        case USB_REQ_DEVICE2HOST | USB_REQ_TYPE_VENDOR:
+                urb->actual_length = 0;
+                switch (request->bRequest) {
+                case BELCARRA_GETMAC:
+                        {
+                                // copy and free the original buffer
+                                memcpy(urb->buffer, npd->local_dev_addr, ETH_ALEN);
+                                urb->actual_length = ETH_ALEN;
+                                return 0;
+                        }
+                }
+#endif
+                return 0;
+        default:
+                break;
+        }
+        return -EINVAL;
+}
+//_________________________________________________________________________________________________
+
+#ifdef CONFIG_OTG_NETWORK_START_SINGLE
+#define NETWORK_START_URBS 1
+#else
+#define NETWORK_START_URBS 2
+#endif
+
+typedef enum mesg {
+        mesg_unknown,
+        mesg_configured,
+        mesg_reset,
+} mesg_t;
+mesg_t net_last_mesg;
+
+char * net_messages[3] = {
+        "",
+        "Network Configured",
+        "Network Reset",
+};
+
+/*! net_check_mesg
+ */
+void net_check_mesg(mesg_t curr_mesg)
+{
+        RETURN_UNLESS(net_last_mesg != curr_mesg);
+        net_last_mesg = curr_mesg;
+        otg_message(net_messages[curr_mesg]);
+}
+
+
+
+/*! net_fd_start_recv - start recv urb(s)
+ */
+STATIC void net_fd_start_recv(struct usbd_function_instance *function)
+{
+        struct usb_network_private *npd = (struct usb_network_private *) (function->privdata);
+        int i;
+        for (i = 0; i < NETWORK_START_URBS; i++) {
+                struct usbd_urb *urb;
+                BREAK_IF(!(urb = usbd_alloc_urb(function, BULK_OUT, 
+                                                usbd_endpoint_transferSize(function, BULK_OUT, usbd_high_speed(function)), 
+                                                net_fd_recv_urb)));
+                TRACE_MSG5(NTT,"i: %d urb=%p priv=%p npd=%p function=%p",
+                           i, urb, urb->function_privdata, npd, function);
+                
+                urb->function_privdata = npd;
+                if (usbd_start_out_urb(urb)) {
+                        urb->function_privdata = NULL;
+                        usbd_free_urb(urb);
+                }
+        }
+}
+
+/*! net_fd_start_recv_eem - start recv urb(s)
+ */
+STATIC void net_fd_start_recv_eem(struct usbd_function_instance *function)
+{
+}
+
+/*! net_fd_event_handler - Processes a USB event.
+ */
+STATIC void net_fd_event_handler (struct usbd_function_instance *function, usbd_device_event_t event, int data)
+{
+        struct usb_network_private *npd = (struct usb_network_private *) (function->privdata);
+
+        switch (event) {
+
+        case DEVICE_RESET:
+        case DEVICE_DESTROY:
+        case DEVICE_BUS_INACTIVE:
+        case DEVICE_DE_CONFIGURED:
+                TRACE_MSG1(NTT,"RESET/DESTROY/BUS_INACTIVE/DE_CONFIGURED %08x",ip_addr);
+                net_check_mesg(mesg_reset);
+                {
+                        // Return if argument is null.
+
+                        // XXX flush
+
+                        npd->flags &= ~NETWORK_ATTACHED;
+                        npd->int_urb = NULL;
+
+                        // Disable our net-device.
+                        // Apparently it doesn't matter if we should do this more than once.
+
+                        net_os_carrier_off(npd);
+
+                        // If we aren't already tearing things down, do it now.
+                        if (!(npd->flags & NETWORK_DESTROYING)) {
+                                npd->flags |= NETWORK_DESTROYING;
+                                //npd->device = NULL;
+                        }
+                }
+                npd->crc = 0;
+                break;
+
+        case DEVICE_CONFIGURED:
+        case DEVICE_BUS_ACTIVITY:
+                TRACE_MSG1(NTT,"CONFIGURED/BUS_ACTIVITY %08x",ip_addr);
+                net_check_mesg(mesg_configured);
+                npd->flags |= NETWORK_ATTACHED;
+                if ((npd->network_type == network_blan) && (npd->flags & NETWORK_OPEN)) 
+                        net_os_send_notification_later(npd);
+                net_os_carrier_on(npd);
+                (npd->network_type == network_eem) ?  net_fd_start_recv_eem : net_fd_start_recv(function);
+                break;
+
+        case DEVICE_SET_INTERFACE:
+                // XXX if CDC then we can check device->alternates[1] and see if we should
+                // enable/disable data flow.
+                // XXX verify ep0.c SET_CONFIGURATION and SET_INTERFACE implmentation are
+                // complete before using this
+                break;
+
+        default:
+                return;
+        }
+        // Let the OS layer know, if it's interested.
+        net_os_config(npd);
+        net_os_hotplug(npd);
+}
+
+
+/*! net_fd_endpoint_cleared - 
+ */
+STATIC void net_fd_endpoint_cleared (struct usbd_function_instance *function, int bEndpointAddress, int wIndex)
+{
+        TRACE_MSG2(NTT, "bEndpointAddress: %02x wIndex: %02x", bEndpointAddress, wIndex);
+}
+
+
+//_________________________________________________________________________________________________
+
+/*! net_fd_function_enable - enable the function driver
+ *
+ * Called for usbd_function_enable() from usbd_register_device()
+ */
+
+STATIC int net_fd_function_enable (struct usbd_function_instance *function)
+{
+        struct usb_network_private *npd;
+#if 0
+        /* This is the first time we've seen the function instance
+           (it's allocated by the usbdcore), so we need to let the OS
+           layer see it and initialize the privdata pointer. */
+        net_os_enable(function);
+        npd = (struct usb_network_private *) (function->privdata);
+        TRACE_MSG0(NTT,"semaphore DOWN");
+        net_os_mutex_enter(npd);
+#else
+        npd = (struct usb_network_private *) (function->privdata);
+        TRACE_MSG0(NTT,"semaphore DOWN");
+        net_os_mutex_enter(npd);
+        net_os_enable(function);
+#endif
+        _MOD_INC_USE_COUNT;  // QQQ Should this be _before_ the mutex_enter()?
+        TRACE_MSG1(NTT, "INC: %d", MOD_IN_USE);
+
+        // set the network device address from the local device address
+        // Now done in net_os_enable() above.
+        // memcpy(npd->net_dev->dev_addr, npd->local_dev_addr, ETH_ALEN);
+
+        npd->have_interrupt = usbd_endpoint_bEndpointAddress(function, INT_IN, usbd_high_speed(function)) ? 1 : 0;
+
+        npd->flags |= NETWORK_ENABLED;
+
+#if defined(CONFIG_OTG_NETWORK_CDC)
+        cdc_init(function);
+#endif                          /* CONFIG_OTG_NETWORK_CDC */
+
+#ifdef CONFIG_OTG_NETWORK_EEM
+        eem_init(function);
+#endif
+
+#ifdef CONFIG_OTG_NETWORK_BASIC
+        basic_init(function);
+#endif
+
+#ifdef CONFIG_OTG_NETWORK_BASIC2
+        basic2_init(function);
+#endif
+
+#ifdef CONFIG_OTG_NETWORK_SAFE
+        safe_init(function);
+#endif
+#ifdef CONFIG_OTG_NETWORK_BLAN
+        blan_init(function);
+#endif
+        net_os_mutex_exit(npd);
+        TRACE_MSG0(NTT,"semaphore UP");
+        return 0;
+}
+
+/*! net_fd_function_disable - disable the function driver
+ *
+ */
+STATIC void net_fd_function_disable (struct usbd_function_instance *function)
+{
+        struct usb_network_private *npd = (struct usb_network_private *) (function->privdata);
+        TRACE_MSG0(NTT,"semaphore DOWN");
+        net_os_mutex_enter(npd);
+        npd->flags &= ~NETWORK_ENABLED;
+        _MOD_DEC_USE_COUNT;  // QQQ Should this be _after_ the up()?
+        TRACE_MSG1(NTT, "DEC: %d", MOD_IN_USE);
+        net_os_mutex_exit(npd);
+        TRACE_MSG0(NTT,"semaphore UP");
+}
+
+/*! net_fd_function_ops - operations table for network function driver
+ */
+struct usbd_function_operations net_fd_function_ops = {
+        device_request: net_fd_device_request,
+        event_handler: net_fd_event_handler,
+        function_enable: net_fd_function_enable,
+        function_disable: net_fd_function_disable,
+        endpoint_cleared: net_fd_endpoint_cleared,
+};
+
+//______________________________________module_init and module_exit________________________________
+
+/*! hexdigit -
+ *
+ * Converts characters in [0-9A-F] to 0..15, characters in [a-f] to 42..47, and all others to 0.
+ */
+static u8 hexdigit (char c)
+{
+        return isxdigit (c) ? (isdigit (c) ? (c - '0') : (c - 'A' + 10)) : 0;
+}
+
+/*! set_address -
+ */
+static void set_address(char *mac_address_str, u8 *dev_addr)
+{
+        int i;
+        if (mac_address_str && strlen(mac_address_str)) {
+                for (i = 0; i < ETH_ALEN; i++) {
+                        dev_addr[i] = 
+                                hexdigit (mac_address_str[i * 2]) << 4 | 
+                                hexdigit (mac_address_str[i * 2 + 1]);
+                }
+        }
+        else {
+                get_random_bytes(dev_addr, ETH_ALEN);
+                dev_addr[0] = (dev_addr[0] & 0xfe) | 0x02;
+        }
+}
+
+/*! macstrtest -
+ */
+static int macstrtest(char *mac_address_str)
+{
+        int l = 0;
+
+        if (mac_address_str) {
+                l = strlen(mac_address_str);
+        }
+        return ((l != 0) && (l != 12));
+}
+
+/*! select_network_type
+ *
+ */
+STATIC network_type_t select_network_type(struct usb_network_params *p)
+{
+        /*
+         * Figure out what network mode to operate in.
+         */
+        network_type_t  network_type = network_unknown;
+        /*
+         * Step 1. Look to see if network_type is specified by something like module params.
+         */
+#if defined(CONFIG_OTG_NETWORK_CDC) 
+        p->cdc_capable = 1;
+        if (p->cdc) {
+                TRACE_MSG0(NTT,"cdc");
+                network_type = network_cdc;
+        }
+#endif
+#ifdef CONFIG_OTG_NETWORK_EEM
+        p->eem_capable = 1;
+        if (p->eem) {
+                THROW_IF (network_type != network_unknown, select_error);
+                TRACE_MSG0(NTT,"eem");
+                network_type = network_eem;
+        }
+#endif
+#ifdef CONFIG_OTG_NETWORK_BASIC
+        p->basic_capable = 1;
+        if (p->basic) {
+                THROW_IF (network_type != network_unknown, select_error);
+                TRACE_MSG0(NTT,"basic");
+                network_type = network_basic;
+        }
+#endif
+#ifdef CONFIG_OTG_NETWORK_BASIC2
+        p->basic2_capable = 1;
+        if (p->basic2) {
+                THROW_IF (network_type != network_unknown, select_error);
+                TRACE_MSG0(NTT,"basic2");
+                network_type = network_basic2;
+        }
+#endif
+#ifdef CONFIG_OTG_NETWORK_SAFE
+        p->safe_capable = 1;
+        if (p->safe) {
+                THROW_IF (network_type != network_unknown, select_error);
+                TRACE_MSG0(NTT,"safe");
+                network_type = network_safe;
+        }
+#endif
+#ifdef CONFIG_OTG_NETWORK_BLAN
+        p->blan_capable = 1;
+        if (p->blan) {
+                THROW_IF (network_type != network_unknown, select_error);
+                TRACE_MSG0(NTT,"blan");
+                network_type = network_blan;
+        }
+#endif
+
+        /*
+         * Step 2. If nothing was specified by parameter, default to
+         *         to the first available (i.e. compiled) of:
+         *            CDC, BASIC, BASIC2, SAFE, BLAN.
+         */
+#if defined(CONFIG_OTG_NETWORK_CDC)
+        if (network_type == network_unknown) {
+                TRACE_MSG0(NTT,"unknown => cdc");
+                network_type = network_cdc;
+        }
+#endif
+#if defined(CONFIG_OTG_NETWORK_EEM)
+        if (network_type == network_unknown) {
+                TRACE_MSG0(NTT,"unknown => eem");
+                network_type = network_eem;
+        }
+#endif
+#if defined(CONFIG_OTG_NETWORK_BASIC)
+        if (network_type == network_unknown) {
+                TRACE_MSG0(NTT,"unknown => basic");
+                network_type = network_basic;
+        }
+#endif
+#if defined(CONFIG_OTG_NETWORK_BASIC2)
+        if (network_type == network_unknown) {
+                TRACE_MSG0(NTT,"unknown => basic2");
+                network_type = network_basic2;
+        }
+#endif
+#if defined(CONFIG_OTG_NETWORK_SAFE)
+        if (network_type == network_unknown) {
+                TRACE_MSG0(NTT,"unknown => safe");
+                network_type = network_safe;
+        }
+#endif
+#if defined(CONFIG_OTG_NETWORK_BLAN)
+        if (network_type == network_unknown) {
+                TRACE_MSG0(NTT,"unknown => blan");
+                network_type = network_blan;
+        }
+#endif
+        CATCH(select_error) {
+                network_type == network_unknown;
+        }
+        return(network_type);
+}
+
+/*! select_descriptors
+ *
+ */
+STATIC struct usbd_function_driver * select_descriptors(network_type_t network_type)
+{
+        // select the function driver descriptors based on network_type
+
+        switch (network_type) {
+
+#if defined(CONFIG_OTG_NETWORK_CDC)
+        case network_cdc:
+                TRACE_MSG0(NTT,"fd=cdc");
+                return(&cdc_function_driver);
+#endif
+#if defined(CONFIG_OTG_NETWORK_EEM)
+        case network_eem:
+                TRACE_MSG0(NTT,"fd=eem");
+                return(&eem_function_driver);
+#endif
+#if defined(CONFIG_OTG_NETWORK_BASIC)
+        case network_basic:
+                TRACE_MSG0(NTT,"fd=basic");
+                return(&basic_function_driver);
+#endif
+#if defined(CONFIG_OTG_NETWORK_BASIC2)
+        case network_basic2:
+                TRACE_MSG0(NTT,"fd=basic2");
+                return(&basic2_function_driver);
+#endif
+#if defined(CONFIG_OTG_NETWORK_SAFE)
+        case network_safe:
+                TRACE_MSG1(NTT,"fd=safe bNumConfigurations: %d",
+                           safe_function_driver.bNumConfigurations);
+                return(&safe_function_driver);
+#endif
+#if defined(CONFIG_OTG_NETWORK_BLAN)
+        case network_blan:
+                TRACE_MSG1(NTT,"fd=blan bNumConfigurations: %d",
+                           blan_function_driver.bNumConfigurations);
+                return(&blan_function_driver);
+#endif
+        default:
+                break;
+        }
+        return(NULL);
+}
+
+/*! net_fd_init - function driver usb part intialization
+ *
+ * @return non-zero for failure.
+ */
+STATIC int net_fd_init(struct usb_network_private *npd, char *info_str)
+{
+        struct usb_network_params *p = npd->params;
+
+        printk(KERN_INFO "Copyright (c) 2002-2004 Belcarra Technologies; www.belcarra.com; sl@belcarra.com\n");
+        NTT = otg_trace_obtain_tag();
+
+        /*
+         * Figure out what network mode to operate in.
+         */
+        if (network_unknown == (npd->network_type = select_network_type(p)) ||
+            NULL == (npd->function_driver = select_descriptors(npd->network_type))) {
+                printk(KERN_INFO"configuration selection error");
+                otg_trace_invalidate_tag(NTT);
+                return -EINVAL;
+        }
+
+#ifdef CONFIG_OTG_NETWORK_ALLOW_SETID
+        TRACE_MSG2(NTT,"checking idVendor: %04x idProduct: %04x", p->vendor_id, p->product_id);
+
+        if (p->vendor_id) 
+                npd->function_driver->idVendor = cpu_to_le16(p->vendor_id);
+
+        if (p->product_id) 
+                npd->function_driver->idProduct = cpu_to_le16(p->product_id);
+#endif
+        printk(KERN_INFO "%s: %s vendor_id: %04x product_id: %04x\n",
+                __FUNCTION__, info_str,
+                le16_to_cpu(npd->function_driver->idVendor),
+                le16_to_cpu(npd->function_driver->idProduct));
+        TRACE_MSG3(NTT,"%s vendor_id: %04x product_id: %04x", info_str,
+                le16_to_cpu(npd->function_driver->idVendor),
+                le16_to_cpu(npd->function_driver->idProduct));
+
+#ifdef CONFIG_OTG_NETWORK_BLAN_FERMAT
+        TRACE_MSG0(NTT,"fermat");
+        fermat_init();
+#endif
+
+#ifdef CONFIG_OTG_NETWORK_EP0TEST
+        /*
+         * ep0test - test that bus interface can do ZLP on endpoint zero
+         *
+         * This will artificially force iProduct string descriptor to be
+         * exactly the same as the endpoint zero packetsize.  When the host
+         * requests this string it will request it not knowing the strength
+         * and will use a max length of 0xff. The bus interface driver must
+         * send a ZLP to terminate the transaction.
+         *
+         * The iProduct descriptor is used because both the Linux and
+         * Windows usb implmentations fetch this in a default enumeration. 
+         *
+         */
+        //UNLESS (ep0test) 
+        //        ep0test = usbd_endpoint_zero_wMaxPacketsize(function);
+
+        if (ep0test) {
+                switch (ep0test) {
+                case 8:  npd->function_driver->device_description->iProduct = "012"; break;
+                case 16: npd->function_driver->device_description->iProduct = "0123456"; break;
+                case 32: npd->function_driver->device_description->iProduct = "0123456789abcde"; break;
+                case 64: npd->function_driver->device_description->iProduct = "0123456789abcdef0123456789abcde"; break;
+                default: printk(KERN_ERR"%s: ep0test: bad value: %d, must be one of 8, 16, 32 or 64\n", 
+                                         __FUNCTION__, ep0test); return -EINVAL;
+                         break;
+                }
+                TRACE_MSG1(NTT,"ep0test: iProduct set to: %s",
+                           npd->function_driver->device_description->iProduct);
+                printk(KERN_INFO"%s: ep0test: iProduct set to: %s\n", __FUNCTION__, 
+                                npd->function_driver->device_description->iProduct);
+        }
+        else {
+                TRACE_MSG0(NTT,"ep0test: not set");
+                printk(KERN_INFO"%s: ep0test: not set\n", __FUNCTION__);
+        }
+#endif /* CONFIG_OTG_NETWORK_EP0TEST */
+
+
+#ifdef CONFIG_OTG_NETWORK_LOCAL_MACADDR
+        if (NULL == p->local_mac_address_str) {
+                /* There is a configured override, and it has NOT been
+                   overridden in turn by a module load time parameter, so use it. */
+                p->local_mac_address_str = CONFIG_OTG_NETWORK_LOCAL_MACADDR;
+        }
+#endif
+#ifdef CONFIG_OTG_NETWORK_REMOTE_MACADDR
+        if (NULL == p->remote_mac_address_str) {
+                /* There is a configured override, and it has NOT been
+                   overridden in turn by a module load time parameter, so use it. */
+                p->remote_mac_address_str = CONFIG_OTG_NETWORK_REMOTE_MACADDR;
+        }
+#endif
+        if ((macstrtest(p->local_mac_address_str) || macstrtest(p->remote_mac_address_str))) {
+                TRACE_MSG2(NTT,"bad size %s %s", p->local_mac_address_str, p->remote_mac_address_str);
+                otg_trace_invalidate_tag(NTT);
+                return -EINVAL;
+        }
+
+        set_address(p->local_mac_address_str, npd->local_dev_addr);
+        set_address(p->remote_mac_address_str, npd->remote_dev_addr);
+        npd->encapsulation = simple_crc;
+
+        if (make_crc_table() ||
+            NULL == (npd->function = usbd_register_function(npd->function_driver, "net-fd", NULL))) {
+                if (network_crc32_table) {
+                        lkfree(network_crc32_table);
+                        network_crc32_table = NULL;
+                }
+                otg_trace_invalidate_tag(NTT);
+                return -EINVAL;
+        }
+        npd->function->privdata = (void *) npd;
+
+        return 0;
+
+}
+
+//_____________________________________________________________________________
+
+/*! net_fd_term - driver exit
+ *
+ * Cleans up the module. Deregisters the function driver and destroys the network object.
+ */
+STATIC void net_fd_term(struct usb_network_private *npd)
+{
+        if (NULL != npd->function) {
+                usbd_deregister_function(npd->function);
+                npd->function = NULL;
+        }
+        if (network_crc32_table) {
+                lkfree(network_crc32_table);
+                network_crc32_table = NULL;
+        }
+        otg_trace_invalidate_tag(NTT);
+}
+
+//_________________________________________________________________________________________________
+
+
+/*! net_fd_usb_ops operations
+ */
+struct net_usb_services net_fd_usb_ops = {
+        .initialize_usb_part = net_fd_init,
+        .terminate_usb_part = net_fd_term,
+        .send_int_notification = net_fd_send_int_blan,
+        .start_xmit = net_fd_start_xmit,
+};
diff -uNr linux/drivers/no-otg/functions/network/net-fd.h linux/drivers/otg/functions/network/net-fd.h
--- linux/drivers/no-otg/functions/network/net-fd.h	1970-01-01 01:00:00.000000000 +0100
+++ linux/drivers/otg/functions/network/net-fd.h	2006-09-01 21:41:30.000000000 +0200
@@ -0,0 +1,48 @@
+/*
+ * otg/functions/network/net-fd.h - Network Function Driver
+ *
+ *      Copyright (c) 2002, 2003, 2004 Belcarra
+ *
+ * By: 
+ *      Tom Rushworth <tbr@belcarra.com>
+ *      Chris Lynne <cl@belcarra.com>
+ *      Stuart Lynne <sl@belcarra.com>
+ *      Bruce Balden <balden@belcarra.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ */
+/*!
+ * @file otg/functions/network/net-fd.h
+ * @brief These are the functions exported by the USB specific parts
+ * (i.e. net-fd.c and net-cl.c) for use in the OS specific
+ * layers (e.g. net-l24-os.c).
+ *
+ *
+ * @ingroup NetworkFunction
+ */
+#ifndef NET_FD_H
+#define NET_FD_H 1
+
+struct net_usb_services {
+        int (*initialize_usb_part)(struct usb_network_private *npd, char *info_str);
+        void (*terminate_usb_part)(struct usb_network_private *npd);
+        void (*send_int_notification)(struct usb_network_private *npd, int connected, int data);
+        int (*start_xmit)(struct usb_network_private *npd, __u8 *buff, int len, void *buff_ctx);
+};
+
+extern struct net_usb_services net_fd_usb_ops;
+
+#endif
diff -uNr linux/drivers/no-otg/functions/network/net-l24-os.c linux/drivers/otg/functions/network/net-l24-os.c
--- linux/drivers/no-otg/functions/network/net-l24-os.c	1970-01-01 01:00:00.000000000 +0100
+++ linux/drivers/otg/functions/network/net-l24-os.c	2006-09-01 21:41:30.000000000 +0200
@@ -0,0 +1,1395 @@
+/*
+ * otg/functions/network/net-l24-os.c - Network Function Driver
+ *
+ *      Copyright (c) 2002, 2003, 2004 Belcarra Technologies
+ *
+ * By: 
+ *      Tom Rushworth <tbr@belcarra.com>
+ *      Stuart Lynne <sl@belcarra.com>
+ *      Bruce Balden <balden@belcarra.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ */
+/*!
+ * @file otg/functions/network/net-l24-os.c
+ * @brief The Linux 2.4 OS specific upper edge (network interface) 
+ * implementation for the Network Function Driver.
+ *
+ * This file implements a standard Linux network driver interface and
+ * the standard Linux 2.4 module init and exit functions.
+ *
+ * If compiled into the kernel, this driver can be used with NFSROOT to
+ * provide the ROOT filesystem. Please note that the kernel NFSROOT support
+ * (circa 2.4.20) can have problems if there are multiple interfaces. So 
+ * it is best to ensure that there are no other network interfaces compiled
+ * in.
+ *
+ *
+ * @ingroup NetworkFunction
+ */
+
+
+/* OS-specific #includes */
+#include <linux/config.h>
+#include <linux/kernel.h>
+#include <linux/version.h>
+#include <linux/init.h>
+#include <linux/list.h>
+#include <linux/smp_lock.h>
+#include <linux/ctype.h>
+#include <linux/time.h>
+#include <linux/timer.h>
+#include <linux/string.h>
+#include <linux/random.h>
+#include <linux/utsname.h>
+#include <linux/kmod.h>
+#include <asm/uaccess.h>
+#include <asm/system.h>
+
+/* Networking #includes from OS */
+#include <linux/netdevice.h>
+#include <linux/skbuff.h>
+#include <linux/etherdevice.h>
+#include <net/arp.h>
+#include <linux/rtnetlink.h>
+#include <linux/atmdev.h>
+#include <linux/pkt_sched.h>
+#include <linux/ip.h>
+#include <linux/if_ether.h>
+#include <linux/in.h>
+#include <linux/inetdevice.h>
+
+
+
+/* Belcarra public interfaces */
+#include <otg/otg-compat.h>
+#include <otg/otg-module.h>
+#include <otg/usbp-chap9.h>
+//#include <otg/usbd-mem.h>
+#include <otg/usbp-func.h>
+
+/* Belcarra private interfaces SUBJECT TO CHANGE WITHOUT NOTICE */
+#include "network.h"
+#include "net-os.h"
+#include "net-fd.h"
+
+MOD_AUTHOR ("sl@belcarra.com, tbr@belcarra.com, balden@belcarra.com");
+
+EMBED_LICENSE();
+
+MOD_DESCRIPTION ("USB Network Function");
+
+
+
+EMBED_USBD_INFO ("network_fd 2.0-beta");
+
+
+#define NTT network_fd_trace_tag
+
+wait_queue_head_t usb_netif_wq;
+#ifdef CONFIG_OTG_NET_NFS_SUPPORT
+int usb_is_configured;
+#endif
+
+/* Module Parameters ************************************************************************* */
+
+STATIC struct usb_network_params params;
+
+#ifdef CONFIG_OTG_NETWORK_ALLOW_SETID
+// override vendor ID
+static int vendor_id;
+MOD_PARM (vendor_id, "i");
+MOD_PARM_DESC (vendor_id, "vendor id");
+
+// override product ID
+static int product_id;
+MOD_PARM (product_id, "i");
+MOD_PARM_DESC (product_id, "product id");
+#endif
+
+// override local mac address
+static char *local_mac_address_str;
+MOD_PARM (local_mac_address_str, "s");
+MOD_PARM_DESC (local_mac_address_str, "Local MAC");
+
+// override remote mac address
+static char *remote_mac_address_str;
+MOD_PARM (remote_mac_address_str, "s");
+MOD_PARM_DESC (remote_mac_address_str, "Remote MAC");
+
+#ifdef CONFIG_OTG_NETWORK_EP0TEST
+static int ep0test;
+MOD_PARM (ep0test, "i");
+MOD_PARM_DESC (ep0test, "Test EP0 String Handling - set to ep0 packetsize [8,16,32,64]");
+#endif
+
+static int zeroconf;
+MOD_PARM (zeroconf, "i");
+MOD_PARM_DESC (zeroconf, "Use usbz%d for network name");
+
+#if defined(CONFIG_OTG_NETWORK_CDC)
+static int cdc;
+MOD_PARM (cdc, "i");
+MOD_PARM_DESC (cdc, "Enable CDC mode");
+#endif
+
+#ifdef CONFIG_OTG_NETWORK_BASIC
+static int basic;
+MOD_PARM (basic, "i");
+MOD_PARM_DESC (basic, "Enable BASIC mode");
+#endif
+
+#ifdef CONFIG_OTG_NETWORK_BASIC2
+static int basic2;
+MOD_PARM (basic2, "i");
+MOD_PARM_DESC (basic2, "Enable BASIC2 mode");
+#endif
+
+#ifdef CONFIG_OTG_NETWORK_SAFE
+static int safe;
+MOD_PARM (safe, "i");
+MOD_PARM_DESC (safe, "Enable SAFE mode");
+#endif
+
+#ifdef CONFIG_OTG_NETWORK_BLAN
+static int blan;
+MOD_PARM (blan, "i");
+MOD_PARM_DESC (blan, "Enable BLAN mode");
+#endif
+
+/* End of Module Parameters ****************************************************************** */
+
+
+u8 local_dev_addr[ETH_ALEN];
+
+u8 remote_dev_addr[ETH_ALEN];
+static u8 zeros[ETH_ALEN];
+
+extern u32 ip_addr;
+extern u32 router_ip;
+extern u32 network_mask;
+extern u32 dns_server_ip;
+
+/* Prevent overlapping of bus administrative functions:
+ *
+ *      network_function_enable
+ *      network_function_disable
+ *      network_hard_start_xmit
+ */
+DECLARE_MUTEX(usbd_network_sem);
+
+
+struct net_device Network_net_device;
+struct usb_network_private Usb_network_private;
+
+void notification_schedule_bh (void);
+int network_urb_sent_bulk (struct usbd_urb *urb, int urb_rc);
+int network_urb_sent_int (struct usbd_urb *urb, int urb_rc);
+
+
+//_________________________________________________________________________________________________
+
+/*
+ * Synchronization
+ *
+ *
+ * Notification bottom half
+ *   
+ *  This is a scheduled task that will send an interrupt notification. Because it
+ *  is called from the task scheduler context it needs to verify that the device is
+ *  still usable.
+ *
+ *      static int network_send_int_blan(struct usbd_function_instance *, int )
+ *      static void notification_bh (void *)
+ *      void notification_schedule_bh (void)
+ *
+ *
+ * Netdevice functions
+ *
+ *   These are called by the Linux network layer. They must be protected by irq locks
+ *   if necessary to prevent interruption by IRQ level events.
+ *
+ *      int network_init (struct net_device *net_device)
+ *      void network_uninit (struct net_device *net_device)
+ *      int network_open (struct net_device *net_device)
+ *      int network_stop (struct net_device *net_device)
+ *      struct net_device_stats *network_get_stats (struct net_device *net_device)
+ *      int network_set_mac_addr (struct net_device *net_device, void *p)
+ *      void network_tx_timeout (struct net_device *net_device)
+ *      int network_set_config (struct net_device *net_device, struct ifmap *map)
+ *      int network_stop (struct net_device *net_device)
+ *      int network_hard_start_xmit (struct sk_buff *skb, struct net_device *net_device)
+ *      int network_do_ioctl (struct net_device *net_device, struct ifreq *rp, int cmd)
+ *
+ *
+ * Data bottom half functions
+ *
+ *  These are called from the bus bottom half handler. 
+ *
+ *      static int network_recv (struct usb_network_private *, struct net_device *, struct sk_buff *)
+ *      int network_recv_urb (struct usbd_urb *)
+ *      int network_urb_sent (struct usbd_urb *, int )
+ *      
+ *
+ * Hotplug bottom half:
+ *
+ *  This is a scheduled task that will send do a hotplug call. Because it is
+ *  called from the task scheduler context it needs to verify that the
+ *  device is still usable.
+ *
+ *      static int hotplug_attach (u32 ip, u32 mask, u32 router, int attach)
+ *      static void hotplug_bh (void *data)
+ *      void net_os_hotplug (void)
+ *
+ *
+ * Irq level functions:
+ *
+ *   These are called at interrupt time do process or respond to USB setup
+ *   commands. 
+ *
+ *      int network_device_request (struct usbd_device_request *)
+ *      void network_event_handler (struct usbd_function_instance *function, usbd_device_event_t event, int data)
+ *
+ *      
+ * Enable and disable functions:
+ *
+ *      void network_function_enable (struct usbd_function_instance *, struct usbd_function_instance *)
+ *      void network_function_disable (struct usbd_function_instance *function)
+ *
+ *
+ * Driver initialization and exit:
+ *
+ *      static  int network_create (struct usb_network_private *)
+ *      static  void network_destroy (struct usb_network_private *)
+ *
+ *      int network_modinit (void)
+ *      void network_modexit (void)
+ */
+
+
+//_______________________________USB part Functions_________________________________________
+
+/*
+ * net_os_mutex_enter - enter mutex region
+ */
+void net_os_mutex_enter(struct usb_network_private *npd)
+{
+        down(&usbd_network_sem);
+}
+
+/*
+ * net_os_mutex_exit - exit mutex region
+ */
+void net_os_mutex_exit(struct usb_network_private *npd)
+{
+        up(&usbd_network_sem);
+}
+
+/* notification_bh - Bottom half handler to send a notification status 
+ *
+ * Send a notification with open/close status
+ *
+ * It should not be possible for this to be called more than once at a time
+ * as it is only called via schedule_task() which protects against a second
+ * invocation.
+ */
+STATIC void notification_bh (void *data)
+{
+        struct usb_network_private *npd = (struct usb_network_private *) data;
+        unsigned long flags;
+        local_irq_save(flags);
+        (*(npd->fd_ops->send_int_notification))(npd, npd->flags & NETWORK_OPEN, 0);
+        local_irq_restore(flags);
+        //_MOD_DEC_USE_COUNT;
+        TRACE_MSG1(NTT, "DEC: %d", MOD_IN_USE);
+}
+
+/* notification_schedule_bh - schedule a call for notification_bh
+ */
+void net_os_send_notification_later(struct usb_network_private *npd)
+{
+#ifdef LINUX24
+        //LINUX 2.4 is the only supported OS to need the USE_COUNT
+        //_MOD_INC_USE_COUNT;
+        TRACE_MSG1(NTT, "INC: %d", MOD_IN_USE);
+        if (!SCHEDULE_WORK(npd->notification_bh)) {
+                //_MOD_DEC_USE_COUNT;
+                TRACE_MSG1(NTT, "DEC: %d", MOD_IN_USE);
+        }
+#else
+        SCHEDULE_WORK(npd->notification_bh);
+        //XXX No provision for failure of schedule ....
+#endif
+}
+
+/*! net_os_xmit_done - called from USB part when a transmit completes, good or bad.
+ *                    tx_rc is SEND_FINISHED_ERROR, SEND_CANCELLED or...
+ *                    buff_ctx is the pointer passed to fd_ops->start_xmit().
+ * @return non-zero only if network does not exist, ow 0.
+ */
+int net_os_xmit_done(struct usbd_function_instance *function, void *buff_ctx, int tx_rc)
+{
+        struct sk_buff *skb = (struct sk_buff *) buff_ctx;
+        struct usb_network_private *npd;
+        struct net_device *net_device;
+        int rc = 0;
+
+        npd = (function ? (struct usb_network_private *) (function->privdata) : NULL);
+        if (skb) {
+                if (NULL != npd && 0 == (rc = !(npd->flags & NETWORK_CREATED))) {
+                        switch (tx_rc) {
+                        case SEND_FINISHED_ERROR:
+                                npd->stats.tx_errors++;
+                                npd->stats.tx_dropped++;
+                                break;
+                        case SEND_CANCELLED:
+                                npd->stats.tx_errors++;
+                                npd->stats.tx_carrier_errors++;
+                                break;
+                        default:
+                                break;
+                        }
+
+                        npd->avg_queue_entries += npd->queued_entries;
+                        npd->queued_entries--;
+                        npd->samples++;
+                        npd->jiffies += jiffies - *(time_t *) (&skb->cb);
+                        npd->queued_bytes -= skb->len;
+                        if (NULL != (net_device = npd->net_dev) &&
+                            netif_queue_stopped(net_device)) {
+                                netif_wake_queue(net_device);
+                                npd->restarts++;
+                        }
+                }
+                dev_kfree_skb_any(skb);
+        }
+        return rc;
+}
+
+/*! net_os_alloc_buf
+ */
+void *net_os_alloc_buff(struct usb_network_private *npd, u8 **cp, int n)
+{
+        struct sk_buff *skb;
+        // allocate skb of appropriate length, reserve 2 to align ip
+        if (!(skb = dev_alloc_skb(n+2))) {
+                *cp = NULL;
+                return(NULL);
+        }
+        skb_reserve(skb, 2);
+        *cp = skb_put(skb, n);
+        return((void*) skb);
+}
+
+/*! network_recv - function to process an received data URB
+ *
+ * Passes received data to the network layer. Passes skb to network layer.
+ *
+ * @return non-zero for failure.
+ */
+STATIC __inline__ int network_recv (struct usb_network_private *npd, 
+                struct net_device *net_device, struct sk_buff *skb)
+{
+        int rc;
+#if 0
+        printk(KERN_INFO"%s: len: %x head: %p data: %p tail: %p\n", __FUNCTION__, 
+                        skb->len, skb->head, skb->data, skb->tail);
+        {
+                u8 *cp = skb->data;
+                int i;
+                for (i = 0; i < skb->len; i++) {
+                        if ((i%32) == 0) {
+                                printk("\nrx[%2x] ", i);
+                        }
+                        printk("%02x ", *cp++);
+                }
+                printk("\n");
+        }
+#endif
+
+        // refuse if no device present
+        if (!netif_device_present (net_device)) {
+                TRACE_MSG0(NTT,"device not present");
+                printk(KERN_INFO"%s: device not present\n", __FUNCTION__);
+                return -EINVAL;
+        }
+
+        // refuse if no carrier
+        if (!netif_carrier_ok (net_device)) {
+                TRACE_MSG0(NTT,"no carrier");
+                printk(KERN_INFO"%s: no carrier\n", __FUNCTION__);
+                return -EINVAL;
+        }
+
+        // refuse if the net device is down
+        if (!(net_device->flags & IFF_UP)) {
+                TRACE_MSG1(NTT,"not up net_dev->flags: %x", net_device->flags);
+                //printk(KERN_INFO"%s: not up net_dev->flags: %x\n", __FUNCTION__, net_device->flags);
+                //npd->stats.rx_dropped++;
+                return -EINVAL;
+        }
+
+        skb->dev = net_device;
+        skb->pkt_type = PACKET_HOST;
+        skb->protocol = eth_type_trans (skb, net_device);
+        skb->ip_summed = CHECKSUM_UNNECESSARY;
+
+        //TRACE_MSG4(NTT,"len: %x head: %p data: %p tail: %p",
+        //                skb->len, skb->head, skb->data, skb->tail);
+
+
+        // pass it up to kernel networking layer
+        if ((rc = netif_rx (skb))) {
+                TRACE_MSG1(NTT,"netif_rx rc: %d", rc);
+        }
+        // QQSV - should be else case for stats?
+        npd->stats.rx_bytes += skb->len;
+        npd->stats.rx_packets++;
+
+        return 0;
+}
+
+/*! net_os_recv_buff - forward a received URB, or clean up after a bad one.
+ *      buff_ctx == NULL --> just accumulate stats (count 1 bad buff)
+ *      crc_bad != 0     --> count a crc error and free buff_ctx/skb
+ *      trim             --> amount to trim from valid skb
+ */
+void net_os_recv_buff(struct usb_network_private *npd, void *buff_ctx, int crc_bad, int trim)
+{
+        struct sk_buff *skb = (struct sk_buff *) buff_ctx;
+
+        if (NULL == skb) {
+                /* Lower layer never got around to allocating an skb, but
+                   needs to count a packet it can't forward. */
+                npd->stats.rx_frame_errors++;
+                npd->stats.rx_errors++;
+        } else {
+                // There is an skb, either forward it or free it.
+                if (crc_bad) {
+                        npd->stats.rx_crc_errors++;
+                        npd->stats.rx_errors++;
+                } else if ((npd->flags & NETWORK_ATTACHED) && NULL != npd->net_dev) {
+                        // All good, forward it.
+                        if (trim) {
+                                skb_trim(skb, skb->len - trim);
+                        }
+                        // all done if network_recv() == 0, ow fall through to error handling
+                        RETURN_IF(!network_recv(npd, npd->net_dev, skb));
+                        // Network layer wouldn't take it.
+                        // rx_dropped gets counted below, any other counter we need?
+                }
+                // Something wrong, free the skb
+                dev_kfree_skb_any (skb);
+        }
+        // The received buffer didn't get forwarded, so...
+        npd->stats.rx_dropped++;
+}
+
+/*! net_os_enable
+ *
+ */
+void net_os_enable(struct usbd_function_instance *function)
+{
+        //struct usb_network_private *npd = &Usb_network_private;
+        struct usb_network_private *npd = (struct usb_network_private *) function->privdata;
+#if 0
+        /* This is the first time we've gotten access to the function instance,
+           and it's an allocated structure.  Fill in the links to our private data. */
+        function->privdata = (void *) npd;
+        npd->function = function;
+#endif
+        TRACE_MSG3(NTT,"npd=%p function=%p, f->p=%p",npd,function,function->privdata);
+
+        // set the network device address from the local device address
+        memcpy(npd->net_dev->dev_addr, npd->local_dev_addr, ETH_ALEN);
+}
+
+/*! net_os_disable
+ * 
+ */
+extern void net_os_disable(struct usbd_function_instance *function)
+{
+#if 0
+        struct usb_network_private *npd = (struct usb_network_private *) (function->privdata);
+        function->privdata = NULL;
+        npd->function = NULL;
+#endif
+}
+
+/*
+ *
+ */
+void net_os_carrier_on(struct usb_network_private *npd)
+{
+        netif_carrier_on(npd->net_dev);
+        netif_wake_queue(npd->net_dev);
+#ifdef CONFIG_OTG_NET_NFS_SUPPORT
+        if (!usb_is_configured) {
+                wake_up(&usb_netif_wq);
+                usb_is_configured = 1;
+        }
+#endif
+
+}
+
+/*
+ *
+ */
+void net_os_carrier_off(struct usb_network_private *npd)
+{
+        netif_stop_queue(npd->net_dev);
+        netif_carrier_off(npd->net_dev);
+}
+
+
+//_______________________________Network Layer Functions____________________________________
+
+/*
+ * In general because the functions are called from an independant layer it is necessary
+ * to verify that the device is still ok and to lock interrupts out to prevent in-advertant
+ * closures while in progress.
+ */
+
+/* network_init -
+ *
+ * Initializes the specified network device.
+ *
+ * Returns non-zero for failure.
+ */
+STATIC int network_init (struct net_device *net_device)
+{
+        TRACE_MSG0(NTT,"no-op");
+        return 0;
+}
+
+
+/* network_uninit -
+ *
+ * Uninitializes the specified network device.
+ */
+STATIC void network_uninit (struct net_device *net_device)
+{
+        TRACE_MSG0(NTT,"no-op");
+        return;
+}
+
+
+/* network_open -
+ *
+ * Opens the specified network device.
+ *
+ * Returns non-zero for failure.
+ */
+STATIC int network_open (struct net_device *net_device)
+{
+        struct usb_network_private *npd = (struct usb_network_private *)(net_device->priv);
+        unsigned long flags;
+
+        _MOD_INC_USE_COUNT;
+        TRACE_MSG1(NTT, "INC: %d", MOD_IN_USE);
+        npd->flags |= NETWORK_OPEN;
+        netif_wake_queue (net_device);
+
+        local_irq_save(flags);
+        (*(npd->fd_ops->send_int_notification))(npd, 1, 0);
+        local_irq_restore(flags);
+
+#ifdef CONFIG_OTG_NET_NFS_SUPPORT
+        if (!usb_is_configured) {
+                if (!in_interrupt()) {
+                        TRACE_MSG0(NTT,"Please replug USB cable and then ifconfig host interface.\n");
+                        printk(KERN_ERR"Please replug USB cable and then ifconfig host interface.\n");
+                        interruptible_sleep_on(&usb_netif_wq);
+                }
+                else {
+                        TRACE_MSG_0(NTT,"Warning! In interrupt\n");
+                        printk(KERN_ERR"Warning! In interrupt\n");
+                }
+        }
+#endif
+        return 0;
+}
+
+
+/* network_stop -
+ *
+ * Stops the specified network device.
+ *
+ * Returns non-zero for failure.
+ */
+STATIC int network_stop (struct net_device *net_device)
+{
+        struct usb_network_private *npd = (struct usb_network_private *)(net_device->priv);
+        unsigned long flags;
+
+        TRACE_MSG0(NTT,"-");
+
+        //netif_stop_queue (net_device);
+
+        npd->flags &= ~NETWORK_OPEN;
+
+        local_irq_save(flags);
+        (*(npd->fd_ops->send_int_notification))(npd, 0, 0);
+        local_irq_restore(flags);
+
+        _MOD_DEC_USE_COUNT;
+        TRACE_MSG1(NTT, "DEC: %d", MOD_IN_USE);
+        return 0;
+}
+
+
+/* network_get_stats -
+ *
+ * Gets statistics from the specified network device.
+ *
+ * Returns pointer to net_device_stats structure with the required information.
+ */
+STATIC struct net_device_stats *network_get_stats (struct net_device *net_device)
+{
+        struct usb_network_private *npd = (struct usb_network_private *)(net_device->priv);
+        return &npd->stats;
+}
+
+
+/* network_set_mac_addr
+ *
+ * Sets the MAC address of the specified network device. Fails if the device is in use.
+ *
+ * Returns non-zero for failure.
+ */
+STATIC int network_set_mac_addr (struct net_device *net_device, void *p)
+{
+        struct usb_network_private *npd = (struct usb_network_private *)(net_device->priv);
+        struct sockaddr *addr = p;
+        unsigned long flags;
+
+        TRACE_MSG0(NTT,"--");
+
+        RETURN_EBUSY_IF(netif_running (net_device));
+        local_irq_save(flags);
+        memcpy(net_device->dev_addr, addr->sa_data, net_device->addr_len);
+        memcpy(npd->local_dev_addr, npd->net_dev->dev_addr, ETH_ALEN);
+        local_irq_restore(flags);
+        return 0;
+}
+
+
+/* network_tx_timeout -
+ *
+ * Tells the specified network device that its current transmit attempt has timed out.
+ */
+STATIC void network_tx_timeout (struct net_device *net_device)
+{
+        struct usb_network_private *npd = (struct usb_network_private *)(net_device->priv);
+#if 0
+        //printk(KERN_ERR"%s:\n", __FUNCTION__);
+        npd->stats.tx_errors++;
+        npd->stats.tx_dropped++;
+        usbd_cancel_urb_irq (npd->bus, NULL);  // QQSV
+#endif
+#if 0
+        // XXX attempt to wakeup the host...
+        if ((npd->network_type == network_blan) && (npd->flags & NETWORK_OPEN)) {
+                notification_schedule_bh();
+        }
+#endif
+}
+
+
+/** network_set_config -
+ *
+ * Sets the specified network device's configuration. Fails if the device is in use.
+ *
+ * map           An ifmap structure containing configuration values.
+ *                      Those values which are non-zero/non-null update the corresponding fields
+ *                      in net_device.
+ *
+ * Returns non-zero for failure.
+ */
+STATIC int network_set_config (struct net_device *net_device, struct ifmap *map)
+{
+        RETURN_EBUSY_IF(netif_running (net_device));
+        if (map->base_addr) 
+                net_device->base_addr = map->base_addr;
+        if (map->mem_start) 
+                net_device->mem_start = map->mem_start;
+        if (map->irq) 
+                net_device->irq = map->irq;
+        return 0;
+}
+
+
+/* network_change_mtu -
+ *
+ * Sets the specified network device's MTU. Fails if the new value is larger and
+ * the device is in use.
+ *
+ * Returns non-zero for failure.
+ */
+STATIC int network_change_mtu (struct net_device *net_device, int mtu)
+{
+        struct usb_network_private *npd = (struct usb_network_private *)(net_device->priv);
+
+        TRACE_MSG0(NTT,"-");
+
+        RETURN_EBUSY_IF(netif_running (net_device));
+        RETURN_EBUSY_IF(mtu > npd->mtu);
+
+        npd->mtu = mtu;
+        return 0;
+}
+
+//_________________________________________________________________________________________________
+//                                      network_hard_start_xmit
+
+/*
+ * net_os_start_xmit - start sending an skb, with usbd_network_sem already held.
+ *
+ * Return non-zero (1) if busy. QQSV - this code always returns 0.
+ */
+STATIC __inline__ int net_os_start_xmit (struct sk_buff *skb, struct net_device *net_device)
+{
+        struct usb_network_private *npd = (struct usb_network_private *)(net_device->priv);
+        int rc;
+
+        if (!netif_carrier_ok(net_device)) {
+                dev_kfree_skb_any (skb);
+                npd->stats.tx_dropped++;
+                //netif_stop_queue(net_device); // XXX
+                // XXX this is what we should do, blows up on some 2.4.20 kernels
+                // return(NET_XMIT_DROP);
+                return(0);
+        }
+
+        // stop queue, it will be restarted only when we are ready for another skb
+        netif_stop_queue(net_device);
+        npd->stopped++;
+
+        // Set the timestamp for tx timeout
+        net_device->trans_start = jiffies;
+
+        // XXX request IN, should start a timer to resend this. 
+        if (npd->data_notify)
+                (*(npd->fd_ops->send_int_notification))(npd, 0, 1);
+
+        rc = (*(npd->fd_ops->start_xmit))(npd,skb->data,skb->len,(void*)skb);
+        switch (rc) {
+        case 0:
+                TRACE_MSG1(NTT,"OK: %d", rc);
+                // update some stats
+                npd->queued_entries++;
+                npd->queued_bytes += skb->len;
+                npd->stats.tx_packets++;
+                npd->stats.tx_bytes += skb->len;
+
+                // Should we restart network queue
+                if ((npd->queued_entries < npd->max_queue_entries) && 
+                                (npd->queued_bytes < npd->max_queue_bytes)) 
+                        netif_wake_queue (net_device);
+
+                break;
+
+        case -EINVAL:
+        case -EUNATCH:
+                TRACE_MSG1(NTT,"not attached, send failed: %d", rc);
+                printk(KERN_ERR"%s: not attached, send failed: %d\n", __FUNCTION__, rc);
+                npd->stats.tx_errors++;
+                npd->stats.tx_carrier_errors++;
+                netif_wake_queue(net_device);
+                break;
+
+        case -ENOMEM:
+                TRACE_MSG1(NTT,"no mem, send failed: %d", rc);
+                printk(KERN_ERR"%s: no mem, send failed: %d\n", __FUNCTION__, rc);
+                npd->stats.tx_errors++;
+                npd->stats.tx_fifo_errors++;
+                netif_wake_queue(net_device);
+                break;
+
+        case -ECOMM:
+                TRACE_MSG2(NTT,"comm failure, send failed: %d %p", rc, net_device);
+                printk(KERN_ERR"%s: comm failure, send failed: %d %p\n", __FUNCTION__, rc, net_device);
+                // Leave the IF queue stopped.
+                npd->stats.tx_dropped++;
+                break;
+
+        }
+        if (0 != rc) {
+                dev_kfree_skb_any (skb);
+                // XXX this is what we should do, blows up on some 2.4.20 kernels
+                // return(NET_XMIT_DROP);
+                return(0);
+        }
+
+        return 0;
+}
+
+/*
+ * network_hard_start_xmit - start sending an skb.  Called by the OS network layer.
+ *
+ * Return non-zero (1) if busy. QQSV - this code always returns 0.
+ */
+STATIC int network_hard_start_xmit (struct sk_buff *skb, struct net_device *net_device)
+{
+        int rc;
+        down(&usbd_network_sem);
+        rc = net_os_start_xmit(skb,net_device);
+        up(&usbd_network_sem);
+        return rc;
+}
+
+
+/* network_do_ioctl - perform an ioctl call 
+ *
+ * Carries out IOCTL commands for the specified network device.
+ *
+ * rp            Points to an ifreq structure containing the IOCTL parameter(s).
+ * cmd           The IOCTL command.
+ *
+ * Returns non-zero for failure.
+ */
+STATIC int network_do_ioctl (struct net_device *net_device, struct ifreq *rp, int cmd)
+{
+        return -ENOIOCTLCMD;
+}
+
+//_________________________________________________________________________________________________
+
+struct net_device Network_net_device = {
+        get_stats: network_get_stats,
+        tx_timeout: network_tx_timeout,
+        do_ioctl: network_do_ioctl,
+        set_config: network_set_config,
+        set_mac_address: network_set_mac_addr,
+        hard_start_xmit: network_hard_start_xmit,
+        change_mtu: network_change_mtu,
+        init: network_init,
+        uninit: network_uninit,
+        open: network_open,
+        stop: network_stop,
+        priv: &Usb_network_private,
+};
+
+//_________________________________________________________________________________________________
+
+#ifdef CONFIG_OTG_NETWORK_BLAN_AUTO_CONFIG
+/* sock_ioctl - perform an ioctl call to inet device
+ */
+int sock_ioctl(u32 cmd, struct ifreq *ifreq)
+{
+        int rc = 0;
+        mm_segment_t save_get_fs = get_fs();
+        TRACE_MSG1(NTT, "cmd: %x", cmd);
+        set_fs(get_ds());
+        rc = devinet_ioctl(cmd, ifreq);
+        set_fs(save_get_fs);
+        return rc;
+}
+
+/* sock_addr - setup a socket address for specified interface
+ */
+static int sock_addr(char * ifname, u32 cmd, u32 s_addr)
+{
+        struct ifreq ifreq;
+        struct sockaddr_in *sin = (void *) &(ifreq.ifr_ifru.ifru_addr);
+
+        TRACE_MSG2(NTT, "ifname: %s addr: %x", ifname, ntohl(s_addr));
+
+        memset(&ifreq, 0, sizeof(ifreq));
+        strcpy(ifreq.ifr_ifrn.ifrn_name, ifname); 
+
+        sin->sin_family = AF_INET;
+        sin->sin_addr.s_addr = s_addr;
+        
+        return sock_ioctl(cmd, &ifreq);
+}
+
+
+/* sock_flags - set flags for specified interface
+ */
+static int sock_flags(char * ifname, u16 oflags, u16 sflags, u16 rflags)
+{
+        int rc = 0;
+        struct ifreq ifreq;
+
+        TRACE_MSG4(NTT, "ifname: %s oflags: %x s_flags: %x r_flags: %x", ifname, oflags, sflags, rflags);
+
+        memset(&ifreq, 0, sizeof(ifreq));
+        strcpy(ifreq.ifr_ifrn.ifrn_name, ifname); 
+
+        oflags |= sflags;
+        oflags &= ~rflags;
+        ifreq.ifr_flags = oflags;
+
+        TRACE_MSG1(NTT, "-> ifr_flags: %x ", ifreq.ifr_flags);
+
+        THROW_IF ((rc = sock_ioctl(SIOCSIFFLAGS, &ifreq)), error);
+
+        TRACE_MSG1(NTT, "<- ifr_flags: %x ", ifreq.ifr_flags);
+
+        CATCH(error) {
+                TRACE_MSG1(NTT, "ifconfig: cannot get/set interface flags (%d)", rc);
+                return rc;
+        }
+        return rc;
+}
+
+/* network_attach - configure interface
+ *
+ * This will use socket calls to configure the interface to the supplied
+ * ip address and make it active.
+ */
+STATIC int network_attach(struct usb_network_private *npd, u32 ip, u32 mask, u32 router, int attach)
+{
+        int err = 0;
+
+        if (attach) {
+                TRACE_MSG5(NTT, "ATTACH npd: %x ip: %08x mask: %08x router: %08x attach: %d", npd, ip, mask, router, attach);
+                u16 oflags = (npd && npd->net_dev) ? npd->net_dev->flags : 0;
+
+                if (npd->net_dev)
+                        memcpy(npd->net_dev->dev_addr, npd->local_dev_addr, ETH_ALEN);
+
+                /* setup ip address, netwask, and broadcast address */
+                if (ip) {
+                        THROW_IF ((err = sock_addr("usbl0", SIOCSIFADDR, htonl(ip))), error);
+                        if (mask) {
+                                THROW_IF ((err = sock_addr("usbl0", SIOCSIFNETMASK, htonl(mask))), error);
+                                THROW_IF ((err = sock_addr("usbl0", SIOCSIFBRDADDR, htonl(ip | ~mask))), error);
+                        }
+                        /* bring the interface up */
+                        THROW_IF ((err = sock_flags("usbl0", oflags, IFF_UP, 0)), error);
+                }
+        }
+        else {
+                TRACE_MSG5(NTT, "DETACH npd: %x ip: %08x mask: %08x router: %08x attach: %d", npd, ip, mask, router, attach);
+                u16 oflags = (npd && npd->net_dev) ? npd->net_dev->flags : 0;
+                /* bring the interface down */
+                THROW_IF ((err = sock_flags("usbl0", oflags, 0, IFF_UP)), error);
+        }
+
+        CATCH(error) {
+                TRACE_MSG1(NTT, "ifconfig: cannot configure interface (%d)", err);
+                return err;
+        }
+        return 0;
+}
+
+/* network_config - configure network
+ *
+ * Check connected status and load/unload as appropriate.
+ *
+ */
+STATIC void config_bh (void *data)
+{
+        struct usb_network_private *npd = (struct usb_network_private *) data;
+        struct usbd_function_instance *function = npd->function;
+
+        TRACE_MSG5(NTT, "function: %x enabled: %x status: %x state: %x npd->config_status: %d", 
+                        function, 
+                        npd->flags & NETWORK_ENABLED,
+                        usbd_get_device_status(function),
+                        usbd_get_device_state(function),
+                        npd->config_status);
+
+        if ((npd->flags & NETWORK_ENABLED ) && (function && (USBD_OK == usbd_get_device_status(function)) 
+                         && (STATE_CONFIGURED == usbd_get_device_state(function))
+                         && ip_addr
+                         )) 
+        {
+                TRACE_MSG2(NTT,"BUS state: %d status: %d", usbd_get_device_state(function), usbd_get_device_status(function));
+                if (config_attached != npd->config_status) {
+                        TRACE_MSG0(NTT,"ATTACH");
+                        npd->config_status = config_attached;
+                        network_attach (npd, ip_addr, network_mask, router_ip, 1);
+                }
+                return;
+        }
+
+        if (config_detached != npd->config_status) {
+                TRACE_MSG0(NTT,"DETACH");
+                npd->config_status = config_detached;
+                network_attach (npd, ip_addr, network_mask, router_ip, 0);
+        }
+        //_MOD_DEC_USE_COUNT;
+        TRACE_MSG1(NTT, "DEC: %d", MOD_IN_USE);
+}
+
+/*
+ * net_os_config - schedule a call to config bottom half
+ */
+void net_os_config(struct usb_network_private *npd)
+{
+        //_MOD_INC_USE_COUNT;
+        TRACE_MSG1(NTT, "INC: %d", MOD_IN_USE);
+        if (!SCHEDULE_WORK(npd->config_bh)) {
+                //_MOD_DEC_USE_COUNT;
+                TRACE_MSG1(NTT, "DEC: %d", MOD_IN_USE);
+        }
+}
+#else /* CONFIG_OTG_NETWORK_BLAN_AUTO_CONFIG */
+void net_os_config(struct usb_network_private *npd)
+{
+        TRACE_MSG0(NTT, "NOT CONFIGURED");
+        // Do nothing, config not configured.
+}
+#endif /* CONFIG_OTG_NETWORK_BLAN_AUTO_CONFIG */
+//______________________________________ Hotplug Functions ________________________________________
+
+#ifdef CONFIG_OTG_NETWORK_HOTPLUG
+
+#define AGENT "network_fd"
+
+/* hotplug_attach - call hotplug 
+ */
+STATIC int hotplug_attach(struct usb_network_private *npd, u32 ip, u32 mask, u32 router, int attach)
+{
+        static int count = 0;
+        char *argv[3];
+        char *envp[10];
+        char ifname[20+12 + IFNAMSIZ];
+        int i;
+        char count_str[20];
+
+        RETURN_EINVAL_IF(!hotplug_path[0]);
+
+        argv[0] = hotplug_path;
+        argv[1] = AGENT;
+        argv[2] = 0;
+
+        sprintf (ifname, "INTERFACE=%s", npd->net_dev->name);
+        sprintf (count_str, "COUNT=%d", count++);
+
+        i = 0;
+        envp[i++] = "HOME=/";
+        envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
+        envp[i++] = ifname;
+
+
+        if (attach) {
+                unsigned char *cp;
+                char ip_str[20+32];
+                char mask_str[20+32];
+                char router_str[20+32];
+                u32 nh;
+
+                nh = htonl(ip);
+                cp = (unsigned char*) &nh;
+                sprintf (ip_str, "IP=%d.%d.%d.%d", cp[0], cp[1], cp[2], cp[3]);
+
+                nh = htonl(mask);
+                cp = (unsigned char*) &nh;
+                sprintf (mask_str, "MASK=%d.%d.%d.%d", cp[0], cp[1], cp[2], cp[3]);
+
+                nh = htonl(router);
+                cp = (unsigned char*) &nh;
+                sprintf (router_str, "ROUTER=%d.%d.%d.%d", cp[0], cp[1], cp[2], cp[3]);
+
+                //TRACE_MSG3(NTT, "attach %s %s %s", ifname, ip_str, count_str);
+
+                envp[i++] = "ACTION=attach";
+                envp[i++] = ip_str;
+                envp[i++] = mask_str;
+                envp[i++] = router_str;
+
+                TRACE_MSG3(NTT, "attach ip: %08x mask: %08x router: %08x", ip, mask, router);
+        }
+        else {
+                //TRACE_MSG2(NTT,"detach %s %s", ifname, count_str);
+                envp[i++] = "ACTION=detach";
+                TRACE_MSG3(NTT, "detach ip: %08x mask: %08x router: %08x", ip, mask, router);
+        }
+
+        envp[i++] = count_str;
+        envp[i++] = 0;
+
+        return call_usermodehelper (argv[0], argv, envp);
+}
+
+
+/* hotplug_bh - bottom half handler to call hotplug script to signal ATTACH or DETACH
+ *
+ * Check connected status and load/unload as appropriate.
+ *
+ * It should not be possible for this to be called more than once at a time
+ * as it is only called via schedule_task() which protects against a second
+ * invocation.
+ */
+STATIC void hotplug_bh (void *data)
+{
+        //struct usb_network_private *network_private = &Usb_network_private;
+        struct usb_network_private *npd = (struct usb_network_private *) data;
+        struct usbd_function_instance *function = NULL; 
+
+        UNLESS (npd) {
+              //_MOD_DEC_USE_COUNT;
+              TRACE_MSG1(NTT, "NPD NULL DEC: %d", MOD_IN_USE);
+              return;
+        }
+
+        function = npd->function;
+
+        TRACE_MSG2(NTT, "function: %x npd->hotplug_status: %d", function, npd->hotplug_status);
+
+
+        if ((npd->flags & NETWORK_ENABLED ) && (function && (USBD_OK == usbd_get_device_status(function)) 
+                         && (STATE_CONFIGURED == usbd_get_device_state(function)))) 
+        {
+                TRACE_MSG2(NTT,"BUS state: %d status: %d", usbd_get_device_state(function), usbd_get_device_status(function));
+                if (hotplug_attached != npd->hotplug_status) {
+                        TRACE_MSG0(NTT,"ATTACH");
+                        npd->hotplug_status = hotplug_attached;
+                        hotplug_attach (npd, ip_addr, network_mask, router_ip, 1);
+                }
+                return;
+        }
+
+        if (hotplug_detached != npd->hotplug_status) {
+                TRACE_MSG0(NTT,"DETACH");
+                npd->hotplug_status = hotplug_detached;
+                hotplug_attach (npd, ip_addr, network_mask, router_ip, 0);
+        }
+        //_MOD_DEC_USE_COUNT;
+        TRACE_MSG1(NTT, "DEC: %d", MOD_IN_USE);
+}
+
+/*
+ * net_os_hotplug - schedule a call to hotplug bottom half
+ */
+void net_os_hotplug(struct usb_network_private *npd)
+{
+        //_MOD_INC_USE_COUNT;
+        TRACE_MSG1(NTT, "INC: %d", MOD_IN_USE);
+        if (!SCHEDULE_WORK(npd->hotplug_bh)) {
+                //_MOD_DEC_USE_COUNT;
+                TRACE_MSG1(NTT, "DEC: %d", MOD_IN_USE);
+        }
+}
+#else
+void net_os_hotplug(struct usb_network_private *npd)
+{
+        TRACE_MSG0(NTT, "NOT CONFIGURED");
+        // Do nothing, hotplug not configured.
+}
+#endif                  /* CONFIG_OTG_NETWORK_HOTPLUG */
+
+//_________________________________________________________________________________________________
+
+/* network_create - create and initialize network private structure
+ *
+ * Returns non-zero for failure.
+ */
+STATIC int network_create(struct usb_network_private *npd)
+{
+        TRACE_MSG0(NTT,"entered");
+
+        // Set some fields to generic defaults and register the network device with the kernel networking code
+
+        memset(&npd->stats, 0, sizeof npd->stats);
+
+        ether_setup(npd->net_dev);
+        RETURN_EINVAL_IF (register_netdev(npd->net_dev));
+        npd->flags |= NETWORK_REGISTERED;
+
+        netif_stop_queue(npd->net_dev);
+        netif_carrier_off(npd->net_dev);
+        npd->net_dev->flags &= ~IFF_UP;
+
+        npd->flags |= NETWORK_CREATED;
+
+        npd->maxtransfer = MAXFRAMESIZE + 4 + 64;
+
+        TRACE_MSG0(NTT,"finis");
+        return 0;
+}
+
+/* network_destroy - destroy network private struture
+ *
+ * Destroys the network interface referenced by the global variable @c Network_net_device.
+ */
+STATIC void network_destroy(struct usb_network_private *npd)
+{
+        if (npd->flags & NETWORK_REGISTERED) {
+                netif_stop_queue(npd->net_dev);
+                netif_carrier_off(npd->net_dev);
+                unregister_netdev(npd->net_dev);
+        }
+        npd->flags = 0;
+}
+
+
+//______________________________________module_init and module_exit________________________________
+
+#ifdef CONFIG_OTG_NETWORK_BLAN_AUTO_CONFIG
+
+/*! network_proc_read_ip - implement proc file system read.
+ * Standard proc file system read function.
+ */
+static ssize_t network_proc_read_ip (struct file *file, char *buf, size_t count, loff_t * pos)
+{
+        unsigned long page;
+        int len = 0;
+        char *bp;
+        int index = (*pos)++;
+
+        RETURN_ZERO_IF(index);
+
+        // get a page, max 4095 bytes of data...
+        RETURN_ENOMEM_UNLESS ((page = GET_KERNEL_PAGE()));
+
+        bp = (char *) page;
+
+        len = sprintf(bp, "%d.%d.%d.%d\n", 
+                        (router_ip >> 24) & 0xff,
+                        (router_ip >> 16) & 0xff,
+                        (router_ip >> 8) & 0xff,
+                        (router_ip) & 0xff
+                        );
+
+        if (copy_to_user(buf, (char *) page, len)) 
+                len = -EFAULT;
+        
+        free_page (page);
+        return len;
+}
+
+static struct file_operations otg_message_proc_switch_functions = {
+      read: network_proc_read_ip,
+};
+
+#endif
+
+
+/* network_modinit - driver intialization
+ *
+ * Returns non-zero for failure.
+ */
+STATIC int network_modinit (void)
+{
+        // Pickup and link together the various global structs.
+        struct usb_network_private *npd = &Usb_network_private;
+        #ifdef CONFIG_PROC_FS
+        struct proc_dir_entry *p;
+        #endif /* CONFIG_PROC_FS */
+        npd->fd_ops = &net_fd_usb_ops;
+        npd->params = &params;
+        npd->net_dev = &Network_net_device;
+        npd->net_dev->priv = (void *) npd;
+#ifdef CONFIG_OTG_NETWORK_ALLOW_SETID
+        params.vendor_id = vendor_id;
+        params.product_id = product_id;
+#endif
+        params.local_mac_address_str = local_mac_address_str;
+        params.remote_mac_address_str = remote_mac_address_str;
+#ifdef CONFIG_OTG_NETWORK_EP0TEST
+        params.ep0test = ep0test;
+#endif
+        params.zeroconf = zeroconf;
+#if defined(CONFIG_OTG_NETWORK_CDC)
+        params.cdc = cdc;
+#endif
+#ifdef CONFIG_OTG_NETWORK_BASIC
+        params.basic = basic;
+#endif
+#ifdef CONFIG_OTG_NETWORK_BASIC2
+        params.basic2 = basic2;
+#endif
+#ifdef CONFIG_OTG_NETWORK_SAFE
+        params.safe = safe;
+#endif
+#ifdef CONFIG_OTG_NETWORK_BLAN
+        params.blan = blan;
+#endif
+        // That should be it for reference to globals except for network_modexit().
+
+        init_waitqueue_head(&usb_netif_wq);
+        PREPARE_WORK_ITEM(npd->notification_bh, notification_bh,npd);
+#ifdef CONFIG_OTG_NETWORK_BLAN_AUTO_CONFIG
+        PREPARE_WORK_ITEM(npd->config_bh, config_bh, npd);
+        #ifdef CONFIG_PROC_FS
+        if ((p = create_proc_entry ("network_ip", 0666, 0))) 
+                p->proc_fops = &otg_message_proc_switch_functions;
+        #endif /* CONFIG_PROC_FS */
+#endif /* CONFIG_OTG_NETWORK_BLAN_AUTO_CONFIG */
+#ifdef CONFIG_OTG_NETWORK_HOTPLUG
+        PREPARE_WORK_ITEM(npd->hotplug_bh, hotplug_bh, npd);
+#endif /* CONFIG_OTG_NETWORK_HOTPLUG */
+
+        if (0 != (*(npd->fd_ops->initialize_usb_part))(npd,"linux network_fd")) {
+                // If the usb part fails, there are no trace functions.
+                return -EINVAL;
+        }
+
+        memcpy(npd->net_dev->dev_addr, npd->local_dev_addr, ETH_ALEN);
+        /* QQSV - this is very strange, since npd->network type can have
+           a number of different values. setting zeroconf is not going to
+           be quite as obvious as it might at first seem. */
+        strncpy(npd->net_dev->name, ((npd->network_type == zeroconf) ? "usbz0" :
+                                     (network_blan ? "usbl0" : "usbb0")), 6);
+
+        if (network_create(npd)) {
+                (*(npd->fd_ops->terminate_usb_part))(npd);
+                return -EINVAL;
+        }
+
+        return 0;
+}
+
+//_____________________________________________________________________________
+
+/* network_modexit - driver exit
+ *
+ * Cleans up the module. Deregisters the function driver and destroys the network object.
+ */
+STATIC void network_modexit (void)
+{
+        struct usb_network_private *npd = &Usb_network_private;
+
+        #ifdef CONFIG_PROC_FS
+        remove_proc_entry("network_ip", NULL);
+        #endif /* CONFIG_PROC_FS */
+
+        while (PENDING_WORK_ITEM(npd->notification_bh)) {
+                // TRACE_MSG pointless here, module will be gon before we can look at it.
+                printk(KERN_ERR"%s: waiting for notificationhotplug bh\n", __FUNCTION__);
+                schedule_timeout(10 * HZ);
+        }
+#ifdef CONFIG_OTG_NETWORK_BLAN_AUTO_CONFIG
+        while (PENDING_WORK_ITEM(npd->config_bh)) {
+                printk(KERN_ERR"%s: waiting for config bh\n", __FUNCTION__);
+                schedule_timeout(10 * HZ);
+        }
+#endif /* CONFIG_OTG_NETWORK_BLAN_AUTO_CONFIG */
+
+#ifdef CONFIG_OTG_NETWORK_HOTPLUG
+        while (PENDING_WORK_ITEM(npd->hotplug_bh)) {
+                printk(KERN_ERR"%s: waiting for hotplug bh\n", __FUNCTION__);
+                schedule_timeout(10 * HZ);
+        }
+#endif
+        /* Which order to tear down the two parts?  No matter which goes first,
+           the other could still try to send data through the now-torn-down part,
+           so go with usb last, because it has the otg_trace cleanup. */
+        network_destroy(npd);
+        if (npd->fd_ops) 
+                (*(npd->fd_ops->terminate_usb_part))(npd);
+}
+
+//_________________________________________________________________________________________________
+
+module_init (network_modinit);
+module_exit (network_modexit);
+
diff -uNr linux/drivers/no-otg/functions/network/net-os.h linux/drivers/otg/functions/network/net-os.h
--- linux/drivers/no-otg/functions/network/net-os.h	1970-01-01 01:00:00.000000000 +0100
+++ linux/drivers/otg/functions/network/net-os.h	2006-09-01 21:41:30.000000000 +0200
@@ -0,0 +1,107 @@
+/*
+ * otg/functions/network/net-os.h
+ *
+ *      Copyright (c) 2003, 2004 Belcarra
+ *
+ * By: 
+ *      Tom Rushworth <tbr@belcarra.com>, 
+ *      Stuart Lynne <sl@belcarra.com>, 
+ *      Bruce Balden <balden@belcarra.com>
+ */
+
+/*!
+ * @file otg/functions/network/net-os.h
+ * @brief Structures and function definitions required for implementation
+ * of the OS specific upper edge (network interface) for the Network
+ * Function Driver.
+ *
+ * A USB network driver is composed of two pieces:
+ *    1) An OS specific piece that handles creating and operating
+ *       a network device for the given OS.
+ *    2) A USB specific piece that interfaces either with the host
+ *       usbcore layer, or with the device usbdcore layer.
+ *
+ * If the USB piece interfaces with the host usbcore layer you get
+ * a network class driver.  If the USB piece interfaces with the usbdcore
+ * layer you get a network function driver.
+ *
+ * This file describes the functions exported by the various net-*-os.c
+ * files (implementing (1)) for use in net-fd.c (2).
+ *
+ *
+ * @ingroup NetworkFunction
+ */
+
+#ifndef NET_OS_H
+#define NET_OS_H 1
+
+/*
+ * net_os_mutex_enter - enter mutex region
+ */
+extern void net_os_mutex_enter(struct usb_network_private *npd);
+
+/*
+ * net_os_mutex_exit - exit mutex region
+ */
+extern void net_os_mutex_exit(struct usb_network_private *npd);
+
+/*
+ * net_os_send_notification_later - schedule a callback to the USB part to send
+ *                                  an INT notification.
+ */
+extern void net_os_send_notification_later(struct usb_network_private *npd);
+
+/*
+ * net_os_xmit_done - called from USB part when a transmit completes, good or bad.
+ *                    tx_rc is SEND_FINISHED_ERROR, SEND_CANCELLED or...
+ *                    buff_ctx is the pointer passed to fd_ops->start_xmit().
+ * Return non-zero only if network does not exist, ow 0.
+ */
+extern int net_os_xmit_done(struct usbd_function_instance *function, void *buff_ctx, int tx_rc);
+
+/*
+ *
+ */
+extern void *net_os_alloc_buff(struct usb_network_private *npd, u8 **cp, int n);
+
+/*
+ * net_os_recv_buff - forward a received URB, or clean up after a bad one.
+ *      buff_ctx == NULL --> just accumulate stats (count 1 bad buff)
+ *      crc_bad != 0     --> count a crc error and free buff_ctx/skb
+ *      trim             --> amount to trim from valid buffer (i.e. shorten
+ *                           from amount requested in net_os_alloc_buff(..... n).
+ *                    This will be called from interrupt context.
+ */
+extern void net_os_recv_buff(struct usb_network_private *npd, void *buff_ctx, int crc_bad, int trim);
+
+/*
+ * net_os_config - schedule a network hotplug event if the OS supports hotplug.
+ */
+extern void net_os_config(struct usb_network_private *npd);
+
+/*
+ * net_os_hotplug - schedule a network hotplug event if the OS supports hotplug.
+ */
+extern void net_os_hotplug(struct usb_network_private *npd);
+
+/*
+ *
+ */
+extern void net_os_enable(struct usbd_function_instance *function);
+
+/*
+ *
+ */
+extern void net_os_disable(struct usbd_function_instance *function);
+
+/*
+ *
+ */
+extern void net_os_carrier_on(struct usb_network_private *npd);
+
+/*
+ *
+ */
+extern void net_os_carrier_off(struct usb_network_private *npd);
+
+#endif
diff -uNr linux/drivers/no-otg/functions/network/network.h linux/drivers/otg/functions/network/network.h
--- linux/drivers/no-otg/functions/network/network.h	1970-01-01 01:00:00.000000000 +0100
+++ linux/drivers/otg/functions/network/network.h	2006-09-01 21:41:30.000000000 +0200
@@ -0,0 +1,348 @@
+/*
+ * otg/functions/network/network.h - Network Function Driver
+ *
+ *      Copyright (c) 2002, 2003, 2004 Belcarra
+ *
+ * By: 
+ *      Tom Rushworth <tbr@belcarra.com>
+ *      Chris Lynne <cl@belcarra.com>
+ *      Stuart Lynne <sl@belcarra.com>
+ *      Bruce Balden <balden@belcarra.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ */
+/*!
+ * @defgroup NetworkFunction Network
+ * @ingroup functiongroup 
+ */
+/*!
+ * @file otg/functions/network/network.h
+ * @brief Network Function Driver private defines
+ *
+ * This defines the internal and private structures required
+ * by the Network Function Driver.
+ *
+ * @ingroup NetworkFunction
+ */
+
+/*! This is a test
+ */
+#ifndef NETWORK_FD_H
+#define NETWORK_FD_H 1
+
+#include <otg/otg-trace.h>
+
+extern otg_tag_t network_fd_trace_tag;
+
+// Some platforms need to get rid of "static" when using GDB or looking at ksyms
+//#define STATIC
+#define STATIC static
+
+typedef enum network_encapsulation {
+        simple_net, simple_crc, 
+} network_encapsulation_t;
+
+typedef enum network_config_status {
+        config_unkown,
+        config_attached,
+        config_detached
+} network_config_status_t;
+
+typedef enum network_hotplug_status {
+        hotplug_unkown,
+        hotplug_attached,
+        hotplug_detached
+} network_hotplug_status_t;
+
+typedef enum network_type {
+        network_unknown,
+        network_blan,
+        network_safe,
+        network_cdc,
+        network_basic,
+        network_basic2,
+        network_eem,
+} network_type_t;
+
+struct usb_network_params {
+        // enabling switchs
+        u32 cdc;
+        u32 basic;
+        u32 basic2;
+        u32 safe;
+        u32 blan;
+        // capability flags
+        u32 cdc_capable;
+        u32 basic_capable;
+        u32 basic2_capable;
+        u32 safe_capable;
+        u32 blan_capable;
+        // overrides
+        u32 vendor_id;
+        u32 product_id;
+        char *local_mac_address_str;
+        char *remote_mac_address_str;
+        // other switches
+        u32 ep0test;
+        u32 zeroconf;
+};
+
+#define CONFIG_OTG_NETWORK_ALLOW_SETID           1
+
+#define NETWORK_CREATED         0x01
+#define NETWORK_REGISTERED      0x02
+#define NETWORK_DESTROYING      0x04
+#define NETWORK_ENABLED         0x08
+#define NETWORK_ATTACHED        0x10 
+#define NETWORK_OPEN            0x20
+
+
+#define MCCI_ENABLE_CRC         0x03
+#define BELCARRA_GETMAC         0x04
+
+#define BELCARRA_SETTIME        0x04
+#define BELCARRA_SETIP          0x05
+#define BELCARRA_SETMSK         0x06
+#define BELCARRA_SETROUTER      0x07
+#define BELCARRA_SETDNS         0x08
+#define BELCARRA_PING           0x09
+#define BELCARRA_SETFERMAT      0x0a
+#define BELCARRA_HOSTNAME       0x0b
+
+
+// RFC868 - seconds from midnight on 1 Jan 1900 GMT to Midnight 1 Jan 1970 GMT
+#define RFC868_OFFSET_TO_EPOCH  0x83AA7E80
+
+struct usb_network_private {
+
+        struct net_device_stats stats;  /* network device statistics */
+
+        int flags;
+        struct net_device *net_dev;
+        struct usbd_function_instance *function;
+        struct usbd_function_driver *function_driver;
+        struct usb_network_params *params;
+        struct net_usb_services *fd_ops;
+        unsigned int maxtransfer;
+        rwlock_t rwlock;
+
+        network_config_status_t config_status;
+        network_hotplug_status_t hotplug_status;
+        network_type_t network_type;
+
+        int state;
+
+        int mtu; 
+        int crc;
+#if defined(CONFIG_OTG_NETWORK_BLAN_FERMAT)
+        int fermat;
+#endif
+
+        unsigned int stopped; 
+        unsigned int restarts;
+
+        unsigned int max_queue_entries;
+        unsigned int max_queue_bytes;
+
+        unsigned int queued_entries;
+        unsigned int queued_bytes;
+
+        time_t avg_queue_entries;
+
+        time_t jiffies;
+        unsigned long samples;
+
+        int have_interrupt;
+
+        int data_notify;
+
+        struct usbd_urb *int_urb;
+
+        network_encapsulation_t encapsulation;
+
+        struct WORK_ITEM notification_bh;
+#ifdef CONFIG_OTG_NETWORK_BLAN_AUTO_CONFIG
+        struct WORK_ITEM config_bh;
+#endif /* CONFIG_OTG_NETWORK_BLAN_AUTO_CONFIG */
+#ifdef CONFIG_HOTPLUG
+        struct WORK_ITEM hotplug_bh;
+#endif /* CONFIG_HOTPLUG */
+        __u8 local_dev_addr[ETH_ALEN];
+        __u8 remote_dev_addr[ETH_ALEN];
+};
+
+// XXX this needs to be co-ordinated with rndis.c maximum's
+#define MAXFRAMESIZE 2000
+
+#if !defined(CONFIG_OTG_MANUFACTURER)
+        #define CONFIG_OTG_MANUFACTURER                "Belcarra"
+#endif
+
+
+#if !defined(CONFIG_OTG_SERIAL_NUMBER_STR)
+        #define CONFIG_OTG_SERIAL_NUMBER_STR           ""
+#endif
+
+/*
+ * Lineo specific 
+ */
+
+#define VENDOR_SPECIFIC_CLASS           0xff
+#define VENDOR_SPECIFIC_SUBCLASS        0xff
+#define VENDOR_SPECIFIC_PROTOCOL        0xff
+
+/*
+ * Lineo Classes
+ */
+#define LINEO_CLASS                     0xff
+
+#define LINEO_SUBCLASS_BASIC_NET          0x01
+#define LINEO_SUBCLASS_BASIC_SERIAL       0x02
+
+/*
+ * Lineo Protocols
+ */
+#define LINEO_BASIC_NET_CRC             0x01
+#define LINEO_BASIC_NET_CRC_PADDED      0x02
+
+#define LINEO_BASIC_SERIAL_CRC          0x01
+#define LINEO_BASIC_SERIAL_CRC_PADDED   0x02
+
+
+/*
+ * endpoint and interface indexes
+ */
+#define BULK_OUT        0x00
+#define BULK_IN         0x01
+#define INT_IN          0x02
+#define ENDPOINTS       0x03
+
+#define COMM_INTF       0x00
+#define DATA_INTF       0x01
+
+
+/* bmDataCapabilities */
+#define BMDATA_CRC                      0x01
+#define BMDATA_PADBEFORE                0x02
+#define BMDATA_PADAFTER                 0x04
+#define BMDATA_FERMAT                   0x08
+#define BMDATA_HOSTNAME                 0x10
+
+/* bmNetworkCapabilities */
+#define BMNETWORK_SET_PACKET_OK         0x01
+#define BMNETWORK_NONBRIDGED            0x02
+#define BMNETWORK_DATA_NOTIFY_OK        0x04
+
+
+/*
+ * BLAN Data Plane
+ */
+//#define CONFIG_OTG_NETWORK_PADBYTES  8
+//#define CONFIG_OTG_NETWORK_PADAFTER  1
+//#undef CONFIG_OTG_NETWORK_PADBEFORE  
+//#define CONFIG_OTG_NETWORK_CRC       1
+
+
+extern __u8 network_requested_endpoints[ENDPOINTS+1];
+extern __u16 network_requested_transferSizes[ENDPOINTS+1];
+extern struct usb_network_private Usb_network_private;
+extern __u8 local_dev_addr[ETH_ALEN];
+extern __u8 remote_dev_addr[ETH_ALEN];
+
+extern struct usbd_function_operations net_fd_function_ops;
+
+struct usbd_class_safe_networking_mdlm_descriptor {
+        __u8 bFunctionLength;           // 0x06
+        __u8 bDescriptorType;           // 0x24
+        __u8 bDescriptorSubtype;        // 0x13
+        __u8 bGuidDescriptorType;       // 0x00
+        __u8 bmNetworkCapabilities;
+        __u8 bmDataCapabilities;
+} __attribute__ ((packed));
+
+struct usbd_class_blan_networking_mdlm_descriptor {
+        __u8 bFunctionLength;           // 0x07
+        __u8 bDescriptorType;           // 0x24
+        __u8 bDescriptorSubtype;        // 0x13
+        __u8 bGuidDescriptorType;       // 0x01
+        __u8 bmNetworkCapabilities;
+        __u8 bmDataCapabilities;
+        __u8 bPad;
+} __attribute__ ((packed));
+
+
+
+
+#define NETWORK_CREATED         0x01
+#define NETWORK_REGISTERED      0x02
+#define NETWORK_DESTROYING      0x04
+#define NETWORK_ENABLED         0x08
+#define NETWORK_ATTACHED        0x10
+#define NETWORK_OPEN            0x20
+
+
+#define MCCI_ENABLE_CRC         0x03
+#define BELCARRA_GETMAC         0x04
+
+#define BELCARRA_SETTIME        0x04
+#define BELCARRA_SETIP          0x05
+#define BELCARRA_SETMSK         0x06
+#define BELCARRA_SETROUTER      0x07
+#define BELCARRA_SETDNS         0x08
+#define BELCARRA_PING           0x09
+#define BELCARRA_SETFERMAT      0x0a
+#define BELCARRA_HOSTNAME       0x0b
+#define BELCARRA_DATA_NOTIFY    0x0c
+
+#define RFC868_OFFSET_TO_EPOCH  0x83AA7E80      // RFC868 - seconds from midnight on 1 Jan 1900 GMT to Midnight 1 Jan 1970 GMT
+
+
+extern __u32 *network_crc32_table;
+                        
+#define CRC32_INIT   0xffffffff      // Initial FCS value
+#define CRC32_GOOD   0xdebb20e3      // Good final FCS value
+
+#define CRC32_POLY   0xedb88320      // Polynomial for table generation
+        
+#define COMPUTE_FCS(val, c) (((val) >> 8) ^ network_crc32_table[((val) ^ (c)) & 0xff])
+
+
+#if 0
+#if !defined(CONFIG_OTG_NETWORK_CRC_DUFF4) && !defined(CONFIG_OTG_NETWORK_CRC_DUFF8)
+/**
+ * Copies a specified number of bytes, computing the 32-bit CRC FCS as it does so.
+ *
+ * dst   Pointer to the destination memory area.
+ * src   Pointer to the source memory area.
+ * len   Number of bytes to copy.
+ * val   Starting value for the CRC FCS.
+ *
+ * Returns      Final value of the CRC FCS.
+ *
+ * @sa crc32_pad
+ */
+static __u32 __inline__ crc32_copy (__u8 *dst, __u8 *src, int len, __u32 val)
+{
+        for (; len-- > 0; val = COMPUTE_FCS (val, *dst++ = *src++));
+        return val;
+}
+
+#endif /* DUFFn */
+#endif
+
+
+
+
+#endif /* NETWORK_FD_H */
diff -uNr linux/drivers/no-otg/functions/network/safe.c linux/drivers/otg/functions/network/safe.c
--- linux/drivers/no-otg/functions/network/safe.c	1970-01-01 01:00:00.000000000 +0100
+++ linux/drivers/otg/functions/network/safe.c	2006-09-01 21:41:30.000000000 +0200
@@ -0,0 +1,362 @@
+/*
+ * otg/functions/network/safe.c - Network Function Driver
+ *
+ *      Copyright (c) 2002, 2003, 2004 Belcarra
+ *
+ * By: 
+ *      Thomas Rushworth <tbr @belcarra.com>
+ *      Stuart Lynne <sl@belcarra.com>
+ *      Bruce Balden <balden@belcarra.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ *
+ * The SAFE network driver implements the SAFE protocol descriptors.
+ *
+ * The SAFE protocol is suitable for infrastructure devices that
+ * are implementing a bridged or routed connection betwen an
+ * external network and the USB Host.
+ *
+ *
+ */
+/*!
+ * @file otg/functions/network/safe.c
+ * @brief This file implements the required descriptors to implement
+ * a SAFE network device with one interface.
+ *
+ * @ingroup NetworkFunction
+ */
+
+#include <otg/otg-compat.h>
+#include <otg/otg-module.h>
+
+
+#include <linux/slab.h>
+#include <linux/interrupt.h>
+#include <linux/utsname.h>
+#include <linux/netdevice.h>
+
+#include <otg/usbp-chap9.h>
+#include <otg/usbp-func.h>
+
+#include "network.h"
+
+#define NTT network_fd_trace_tag
+
+#ifdef CONFIG_OTG_NETWORK_SAFE
+/* USB SAFE  Configuration ********************************************************** */
+
+/*
+ * SAFE Ethernet Configuration
+ */
+
+/* Communication Interface Class descriptors
+ */
+
+/*! BULK OUT data endpoint descriptor
+ */
+static struct usbd_endpoint_descriptor safe_data_1 = {
+    bLength: 0x07,
+    bDescriptorType: USB_DT_ENDPOINT,
+    bEndpointAddress: USB_DIR_OUT,
+    bmAttributes: BULK,     
+    wMaxPacketSize: __constant_cpu_to_le16(0), 
+    bInterval: 0,
+};
+/*! BULK IN data endpoint descriptor
+ */
+static struct usbd_endpoint_descriptor safe_data_2 =
+{
+    bLength:sizeof(struct usbd_endpoint_descriptor),
+    bDescriptorType:USB_DT_ENDPOINT,
+    bEndpointAddress:USB_DIR_IN, 
+    bmAttributes: BULK,     
+    wMaxPacketSize: __constant_cpu_to_le16(0), 
+    bInterval: 0x0,
+};
+
+/*! INTERRUPT IN data endpoint descriptor
+ */
+static struct usbd_endpoint_descriptor safe_comm_1 = 
+{
+    bLength:sizeof(struct usbd_endpoint_descriptor),
+    bDescriptorType:USB_DT_ENDPOINT,
+    bEndpointAddress:USB_DIR_IN, 
+    bmAttributes: INTERRUPT,     
+    wMaxPacketSize: __constant_cpu_to_le16(0), 
+    bInterval: 0xa,
+};
+
+#if 0
+static  struct usbd_endpoint_descriptor  *basic_default[] = { &basic_data_1, 
+    &basic_data_2, 
+    &basic_comm_1, };
+u8 basic_indexes[] = { BULK_OUT, BULK_IN, INT_IN, };
+#endif
+	
+#if 0
+static u8 safe_class_1[] = { 0x05, CS_INTERFACE, USB_ST_HEADER, 0x10, 0x01, /* CLASS_BDC_VERSION, CLASS_BDC_VERSION */ };
+static u8 safe_class_2[] = { 0x15, CS_INTERFACE, USB_ST_MDLM, 0x00, 0x01, /* bcdVersion, bcdVersion */
+
+                0x5d, 0x34, 0xcf, 0x66, 0x11, 0x18, 0x11, 0xd6,  /* bGUID */
+                0xa2, 0x1a, 0x00, 0x01, 0x02, 0xca, 0x9a, 0x7f,  /* bGUID */ };
+
+static u8 safe_class_3[] = { 0x06, CS_INTERFACE, USB_ST_MDLMD, 0x00, 0x00, 0x00,    /* bDetailData */ };
+
+static u8 safe_class_4[] = { 0x0d, CS_INTERFACE, USB_ST_ENF, 
+        0x00, 0x00, 0x00, 0x00, 0x00, 0xea, 0x05, /* 1514 maximum frame size */
+        0x00, 0x00, 0x00 , };
+#else 
+static  struct usbd_class_header_function_descriptor safe_class_1 = { 
+     bFunctionLength:0x05, 
+     bDescriptorType: CS_INTERFACE, 
+     bDescriptorSubtype: USB_ST_HEADER, 
+     bcdCDC: __constant_cpu_to_le16(0x0110), //0x10, 0x01, 
+};
+
+static struct usbd_class_mdlm_descriptor safe_class_2 = { 
+     bFunctionLength: 0x15, 
+     bDescriptorType: CS_INTERFACE, 
+     bDescriptorSubtype: USB_ST_MDLM, 
+     bcdVersion: __constant_cpu_to_le16( 0x0100), //0x00, 0x01, 
+     bGUID:  {
+                0x5d, 0x34, 0xcf, 0x66, 0x11, 0x18, 0x11, 0xd6,  /* bGUID */
+                0xa2, 0x1a, 0x00, 0x01, 0x02, 0xca, 0x9a, 0x7f,  /* bGUID */ },
+};
+
+static struct usbd_class_safe_descriptor safe_class_3 = { 
+     bFunctionLength: 0x06, 
+     bDescriptorType: CS_INTERFACE, 
+     bDescriptorSubtype: USB_ST_MDLMD, 
+     bGuidDescriptorType: 0x00, 
+     bmNetworkCapabilities: 0x00, 
+     bmDataCapabilities: 0x00,    /* bDetailData */ 
+};
+
+static struct usbd_class_ethernet_networking_descriptor safe_class_4 = { 
+     bFunctionLength: 0x0d, 
+     bDescriptorType: CS_INTERFACE, 
+     bDescriptorSubtype: USB_ST_ENF,
+     iMACAddress: 0x0,
+     bmEthernetStatistics: __constant_cpu_to_le32(0x00), 
+     wMaxSegmentSize: __constant_cpu_to_le16(0x5ea),
+     wNumberMCFilters: __constant_cpu_to_le16(0x00),
+     bNumberPowerFilters: 0,
+};
+
+#endif
+
+/*! Endpoint descriptor list
+ */
+static struct usbd_endpoint_descriptor  *safe_alt_endpoints[] = { 
+    &safe_data_1, &safe_data_2, &safe_comm_1, };
+
+static struct usbd_generic_class_descriptor  *safe_comm_class_descriptors[] = { 
+        (struct usbd_generic_class_descriptor *) &safe_class_1, 
+        (struct usbd_generic_class_descriptor *) &safe_class_2, 
+        (struct usbd_generic_class_descriptor *) &safe_class_3, 
+        (struct usbd_generic_class_descriptor *) &safe_class_4, };
+
+u8 safe_alt_indexes[] = { BULK_OUT, BULK_IN, INT_IN, };
+
+/*! Data Interface Descriptor
+ */
+static struct usbd_interface_descriptor safe_alternate_descriptor = {
+    bLength: 0x09,
+    bDescriptorType: USB_DT_INTERFACE,
+    bInterfaceNumber: 0x00,
+    bAlternateSetting: 0x00,
+    bNumEndpoints: sizeof(safe_alt_endpoints)/sizeof(struct usbd_endpoint_descriptor *),
+    bInterfaceClass: COMMUNICATIONS_INTERFACE_CLASS,
+    bInterfaceSubClass: COMMUNICATIONS_MDLM_SUBCLASS,
+    bInterfaceProtocol: COMMUNICATIONS_NO_PROTOCOL,
+    iInterface: 0x00,
+};
+
+/*! Data Alternate Interface Description List
+ */
+static struct usbd_alternate_description safe_alternate_descriptions[] = {
+    { 
+        iInterface: CONFIG_OTG_NETWORK_SAFE_INTF,
+        interface_descriptor: &safe_alternate_descriptor,
+        classes: sizeof (safe_comm_class_descriptors) / sizeof (struct usbd_generic_class_descriptor *),
+        class_list: safe_comm_class_descriptors,
+        endpoints:sizeof (safe_alt_endpoints) / sizeof(struct usbd_endpoint_descriptor *),
+        endpoint_list: safe_alt_endpoints,
+        endpoint_indexes: safe_alt_indexes,
+    },
+};
+
+/* Interface descriptions and descriptors
+ */
+/*! Interface Description List
+ */
+static struct usbd_interface_description safe_interfaces[] = {
+    { 
+        alternates:sizeof (safe_alternate_descriptions) / sizeof (struct usbd_alternate_description),
+        alternate_list:safe_alternate_descriptions, },
+};
+
+
+/* Configuration descriptions and descriptors
+ */
+
+/*! Configuration Descriptor 
+ */
+static struct usbd_configuration_descriptor  safe_configuration_descriptor = {
+    bLength: 0x09, 
+    bDescriptorType: USB_DT_CONFIGURATION, 
+    wTotalLength: __constant_cpu_to_le16(0x0000), //0x00, 0x00, // wLength
+    bNumInterfaces:sizeof (safe_interfaces) / sizeof (struct usbd_interface_description),
+    bConfigurationValue:0x01, 
+    iConfiguration: 0x00, // bConfigurationValue, iConfiguration
+    bmAttributes: 0, 
+    bMaxPower:0,
+};
+
+/*! Configuration Description List 
+ */
+struct usbd_configuration_description safe_description[] = {
+    { 
+        iConfiguration: CONFIG_OTG_NETWORK_SAFE_DESC,
+        configuration_descriptor: &safe_configuration_descriptor,
+    },
+};
+
+/* Device Description
+ */
+/*! Device Descriptor 
+ */
+static struct usbd_device_descriptor safe_device_descriptor = {
+        bLength: sizeof(struct usbd_device_descriptor),
+        bDescriptorType: USB_DT_DEVICE,
+        bcdUSB: __constant_cpu_to_le16(USB_BCD_VERSION),
+        bDeviceClass: COMMUNICATIONS_DEVICE_CLASS,
+        bDeviceSubClass: 0x02,
+        bDeviceProtocol: 0x00,
+        bMaxPacketSize0: 0x00,
+        idVendor: __constant_cpu_to_le16(CONFIG_OTG_NETWORK_VENDORID),
+        idProduct: __constant_cpu_to_le16(CONFIG_OTG_NETWORK_PRODUCTID),
+        bcdDevice: __constant_cpu_to_le16(CONFIG_OTG_NETWORK_BCDDEVICE),
+};
+
+#ifdef CONFIG_OTG_HIGH_SPEED
+/*! High Speed Device Qualifier Descriptor 
+ */
+static struct usbd_device_qualifier_descriptor safe_device_qualifier_descriptor = {
+        bLength: sizeof(struct usbd_device_qualifier_descriptor),
+        bDescriptorType: USB_DT_DEVICE_QUALIFIER,
+        bcdUSB: __constant_cpu_to_le16(USB_BCD_VERSION),
+        bDeviceClass: COMMUNICATIONS_DEVICE_CLASS,
+        bDeviceSubClass: 0x02,
+        bDeviceProtocol: 0x00,
+        bMaxPacketSize0: 0x00,
+};
+#endif /* CONFIG_OTG_HIGH_SPEED */
+
+/*! Endpoint Request List
+ */
+static struct usbd_endpoint_request safe_endpoint_requests[ENDPOINTS+1] = {
+        { 1, 0, 0, USB_DIR_OUT | USB_ENDPOINT_BULK, MAXFRAMESIZE + 48, MAXFRAMESIZE + 512,  },
+        { 1, 0, 0, USB_DIR_IN | USB_ENDPOINT_BULK, MAXFRAMESIZE + 48, MAXFRAMESIZE + 512,  },
+        { 1, 0, 0, USB_DIR_IN | USB_ENDPOINT_INTERRUPT | USB_ENDPOINT_OPT, 16, 64, },
+        { 0, },
+};
+
+/*! OTG Descriptor
+ */
+static struct usbd_otg_descriptor safe_otg_descriptor = {
+        bLength : sizeof(struct usbd_otg_descriptor),
+        bDescriptorType: USB_DT_OTG,
+        bmAttributes: 0,
+};
+
+/*! Device Description
+ */
+struct usbd_device_description safe_device_description = {
+        device_descriptor: &safe_device_descriptor,
+#ifdef CONFIG_OTG_HIGH_SPEED
+        device_qualifier_descriptor: &safe_device_qualifier_descriptor,
+#endif /* CONFIG_OTG_HIGH_SPEED */
+        otg_descriptor: &safe_otg_descriptor,
+        iManufacturer: CONFIG_OTG_NETWORK_MANUFACTURER,
+        iProduct: CONFIG_OTG_NETWORK_PRODUCT_NAME,
+#if !defined(CONFIG_OTG_NO_SERIAL_NUMBER) && defined(CONFIG_OTG_SERIAL_NUMBER_STR)
+        iSerialNumber:CONFIG_OTG_SERIAL_NUMBER_STR,
+#endif
+};
+
+/*! safe_init
+ * @param function The function instance
+ */
+void safe_init (struct usbd_function_instance *function)
+{
+        struct usbd_class_ethernet_networking_descriptor *ethernet;
+        int len = 0;
+        char buf[255];
+
+        buf[0] = 0;
+
+        safe_alternate_descriptions[0].endpoints = Usb_network_private.have_interrupt ? 3 : 2;
+
+        // Update the iMACAddress field in the ethernet descriptor
+        {
+                char address_str[14];
+                snprintf(address_str, 13, "%02x%02x%02x%02x%02x%02x",
+                                remote_dev_addr[0], remote_dev_addr[1], remote_dev_addr[2], 
+                                remote_dev_addr[3], remote_dev_addr[4], remote_dev_addr[5]);
+                if ((ethernet = &safe_class_4)) {
+                        if (ethernet->iMACAddress) {
+                                usbd_free_string_descriptor(ethernet->iMACAddress);
+                        }
+                        ethernet->iMACAddress = usbd_alloc_string(address_str);
+                }
+        }
+
+#ifdef CONFIG_OTG_NETWORK_SAFE_PADBEFORE
+        safe_class_3.bmNetworkCapabilities |= BMDATA_PADBEFORE;
+        len += sprintf(buf + len, "PADBEFORE: %02x ", safe_class_3.bmNetworkCapabilities);
+#endif
+#ifdef CONFIG_OTG_NETWORK_SAFE_CRC
+        safe_class_3.bmNetworkCapabilities |= BMDATA_CRC;
+        len += sprintf(buf + len, "CRC: %02x ", safe_class_3.bmNetworkCapabilities);
+#endif
+#ifdef CONFIG_OTG_NETWORK_SAFE_NONBRIDGED
+        safe_class_3.bmNetworkCapabilities |= BMNETWORK_NONBRIDGED;
+        len += sprintf(buf + len, "NONBRIDGE: %02x ",safe_class_3.bmDataCapabilities);
+#endif
+        if (strlen(buf))
+                TRACE_MSG1(NTT,"%s", buf);
+
+}
+
+/*! function driver description
+ */
+struct usbd_function_driver safe_function_driver = {
+        name: "network-SAFE",
+        fops: &net_fd_function_ops,
+        device_description: &safe_device_description,
+        bNumConfigurations: sizeof (safe_description) / sizeof (struct usbd_configuration_description),
+        configuration_description: safe_description,
+        idVendor: __constant_cpu_to_le16(CONFIG_OTG_NETWORK_VENDORID),
+        idProduct: __constant_cpu_to_le16(CONFIG_OTG_NETWORK_PRODUCTID),
+        bcdDevice: __constant_cpu_to_le16(CONFIG_OTG_NETWORK_BCDDEVICE),
+        bNumInterfaces:sizeof (safe_interfaces) / sizeof (struct usbd_interface_description),
+        interface_list:safe_interfaces, 
+        endpointsRequested: ENDPOINTS,
+        requestedEndpoints: safe_endpoint_requests,
+};
+#endif                  /* CONFIG_OTG_NETWORK_SAFE */
+
diff -uNr linux/drivers/no-otg/ocd/Config.in linux/drivers/otg/ocd/Config.in
--- linux/drivers/no-otg/ocd/Config.in	1970-01-01 01:00:00.000000000 +0100
+++ linux/drivers/otg/ocd/Config.in	2006-09-01 21:41:31.000000000 +0200
@@ -0,0 +1,15 @@
+
+#
+# Copyright (c) 2004 Belcarra
+#
+# 
+
+   mainmenu_option next_comment
+   comment 'OTG Host Controller Driver (HCD)'
+   dep_tristate '  USB Host Controller Driver' CONFIG_OTG_HCD $CONFIG_OTG_HOSTCORE
+   hex     'VendorID (hex value)'             CONFIG_OTG_ROOTHUB_VENDORID "15ec"
+   hex     'ProductID (hex value)'            CONFIG_OTG_ROOTHUB_PRODUCTID "f010"
+   hex     'ProductID (hex value)'            CONFIG_OTG_ROOTHUB_BCDDEVICE "0100"
+
+
+   endmenu
diff -uNr linux/drivers/no-otg/ocd/Makefile linux/drivers/otg/ocd/Makefile
--- linux/drivers/no-otg/ocd/Makefile	1970-01-01 01:00:00.000000000 +0100
+++ linux/drivers/otg/ocd/Makefile	2006-09-01 21:41:30.000000000 +0200
@@ -0,0 +1,44 @@
+#
+# Belcarra OTG - On-The-Go 
+#
+# Copyright (c) 2004 Belcarra Technologies Corp
+
+TOPDIR ?= ../../../..
+
+subdir-y 	:= 
+subdir-m 	:=
+subdir-n 	:=
+subdir- 	:=
+
+# The target object and module list name.
+
+O_TARGET        := ocd_target.o
+
+subdir-$(CONFIG_OTG_AU1550) += au1x00
+subdir-$(CONFIG_OTG_AU1X00) += au1x00
+subdir-$(CONFIG_OTG_ISP1301_OMAP_H2) += isp1301
+subdir-$(CONFIG_OTG_ISP1301_MAINSTONE) += isp1301
+
+subdir-$(CONFIG_OTG_MAX3353E_DB1550) += max3353e
+
+# Object file lists.
+
+obj-y           :=
+obj-m           :=
+obj-n           :=
+obj-            :=
+
+# Bus Interface Drivers
+ocd-obj-$(CONFIG_OTG_AU1550) += au1x00/au1x00_pcd_drv.o
+ocd-obj-$(CONFIG_OTG_AU1X00) += au1x00/au1x00_pcd_drv.o
+
+# Platform 
+ocd-obj-$(CONFIG_OTG_ISP1301_OMAP_H2) += isp1301/isp1301_target.o
+ocd-obj-$(CONFIG_OTG_ISP1301_MAINSTONE) += isp1301/isp1301_target.o
+
+ocd-obj-$(CONFIG_OTG_MAX3353E_DB1550) += max3353e/max3353e_target.o
+
+obj-y += $(ocd-obj-y)  
+
+include $(TOPDIR)/Rules.make
+EXTRA_CFLAGS += -Wno-format -Wall
diff -uNr linux/drivers/no-otg/ocd/Makefile-l26 linux/drivers/otg/ocd/Makefile-l26
--- linux/drivers/no-otg/ocd/Makefile-l26	1970-01-01 01:00:00.000000000 +0100
+++ linux/drivers/otg/ocd/Makefile-l26	2006-09-01 21:41:30.000000000 +0200
@@ -0,0 +1,29 @@
+#
+# Belcarra OTG - On-The-Go 
+#
+# Copyright (c) 2004 Belcarra Technologies Corp
+
+EXTRA_CFLAGS += -Wno-format -Wall
+
+# Bus Interface Drivers
+#subdir-$(CONFIG_OTG_AU1X00) += au1x00
+#subdir-$(CONFIG_OTG_LH7A400) += lh7a400
+#subdir-$(CONFIG_OTG_MX1) += mx1
+#subdir-$(CONFIG_OTG_MX2) += mx2
+#subdir-$(CONFIG_OTG_NC2280) += nc2280
+#subdir-$(CONFIG_OTG_PXA) += pxa
+#subdir-$(CONFIG_OTG_SA1100) += sa1100
+#subdir-$(CONFIG_OTG_SMDK2500) += smdk2500
+#subdir-$(CONFIG_OTG_SUPERH) += superh
+#subdir-$(CONFIG_OTG_SX2) += sx2
+#subdir-$(CONFIG_OTG_WMMX) += wmmx
+
+obj-m += scma11-evb/
+obj-m += omap/
+obj-m += isp1301/
+
+obj-y += scma11-evb/
+obj-y += omap/
+obj-y += isp1301/
+
+
diff -uNr linux/drivers/no-otg/ocd/README linux/drivers/otg/ocd/README
--- linux/drivers/no-otg/ocd/README	1970-01-01 01:00:00.000000000 +0100
+++ linux/drivers/otg/ocd/README	2006-09-01 21:41:30.000000000 +0200
@@ -0,0 +1,54 @@
+otg/pcd
+
+
+Linux Build support:
+
+        Makefile
+        Makefile-l24
+        Makefile-l26
+
+        README
+
+PCD support files:
+
+        otg-pcd/pcd.c
+        otg-pcd/pcd-init-l24.c
+        otg-pcd/pcd-ocd-init-l24.c
+        otg-pcd/tr-init-l24.c
+
+TCD support files:
+
+        otg-tcd/tcd.c
+        otg-tcd/tcd-init-l24.c
+
+HCD support files;
+
+        otg-hcd/hcd-init-l24.c
+        otg-hcd/hcd-l26.c
+        otg-hcd/hcd-rh.c
+        otg-hcd/hcd.c
+
+Architecture Specific Drivers:
+
+        lh7a400
+        mx1
+        nc2280
+        pxa
+        sa1100
+        smdk2500
+        superh
+        sx2
+
+Device Specific Drivers:
+        
+        atlas
+        isp1301
+        max3353e
+
+Platform Specific Drivers
+
+        au1x00
+        bvd
+        mx2
+        omap
+
diff -uNr linux/drivers/no-otg/ocd/au1x00/Makefile linux/drivers/otg/ocd/au1x00/Makefile
--- linux/drivers/no-otg/ocd/au1x00/Makefile	1970-01-01 01:00:00.000000000 +0100
+++ linux/drivers/otg/ocd/au1x00/Makefile	2006-09-01 21:41:31.000000000 +0200
@@ -0,0 +1,111 @@
+#
+# Makefile for the kernel USBD (device not host) drivers.
+#
+# Copyright (c) 2002 Belcarra
+
+# Subdirs.
+# This is a bit complex, because some subdirs are for
+# proprietary code, and are simply not present in a
+# general distribution.
+
+# The all-CAPS *_DIRS get nuked in the new versions
+# of Rules.make, so use only the subdir-* methods.
+subdir-y 	:=
+subdir-m 	:=
+subdir-n 	:=
+subdir- 	:=
+
+# The target object and module list name.
+
+O_TARGET	:= au1x00_pcd_drv.o
+
+# Objects that export symbols.
+
+export-objs	:= 
+
+# Multipart objects.
+
+#au1x00_otg-objs	:= au1x00.o usbd-bi.o trace.o
+db1550_tr-objs	:= tr-init-l24.o au1550.o pcd.o 
+db1550_dr-objs	:= tr-init-l24.o au1550.o pcd.o 
+#au1550_tr-objs	:= tr-init-l24.o au1550.o pcd.o 
+au1x00_tr-objs	:= tr-init-l24.o au1x00.o pcd.o 
+
+# Optional parts of multipart objects.
+
+# Object file lists.
+
+#obj-y		:= usbd-bi.o 
+obj-y		:=
+obj-m		:=
+obj-n		:=
+obj-		:=
+
+# Each configuration option enables a list of files.
+
+obj-$(CONFIG_OTG_AU1550_DB1500_TR)	+= db1550_tr.o
+obj-$(CONFIG_OTG_AU1550_DB1550_TR)	+= db1550_tr.o
+obj-$(CONFIG_OTG_AU1550_DB1500_DR)	+= db1550_dr.o
+
+#obj-$(CONFIG_OTG_AU1550)		+= au1550_tr.o
+obj-$(CONFIG_OTG_AU1X00)		+= au1x00_tr.o
+
+# Object files in subdirectories
+
+
+# Extract lists of the multi-part drivers.
+# The 'int-*' lists are the intermediate files used to build the multi's.
+
+multi-y		:= $(filter $(list-multi), $(obj-y))
+multi-m		:= $(filter $(list-multi), $(obj-m))
+int-y		:= $(sort $(foreach m, $(multi-y), $($(basename $(m))-objs)))
+int-m		:= $(sort $(foreach m, $(multi-m), $($(basename $(m))-objs)))
+
+# Files that are both resident and modular: remove from modular.
+
+obj-m		:= $(filter-out $(obj-y), $(obj-m))
+int-m		:= $(filter-out $(int-y), $(int-m))
+
+# Translate to Rules.make lists.
+
+O_OBJS		:= $(filter-out $(export-objs), $(obj-y))
+OX_OBJS		:= $(filter     $(export-objs), $(obj-y))
+M_OBJS		:= $(sort $(filter-out $(export-objs), $(obj-m)))
+MX_OBJS		:= $(sort $(filter     $(export-objs), $(obj-m)))
+MI_OBJS		:= $(sort $(filter-out $(export-objs), $(int-m)))
+MIX_OBJS	:= $(sort $(filter     $(export-objs), $(int-m)))
+
+# The global Rules.make.
+
+include $(TOPDIR)/Rules.make
+
+OTG=$(TOPDIR)/drivers/otg
+OCD_DIR=$(OTG)/ocd
+INCLUDE_DIRS =          -I$(OTG) 
+EXTRA_CFLAGS +=          -Wno-missing-prototypes -Wno-unused -Wno-format ${INCLUDE_DIRS}
+EXTRA_CFLAGS_nostdinc += -Wno-missing-prototypes -Wno-unused -Wno-format ${INCLUDE_DIRS}
+
+I2C=$(OTG)/ocd/otg-i2c
+PCD=$(OTG)/ocd/otg-pcd
+vpath %.c $(USBDCORE_DIR) $(OCD_DIR) $(PCD) $(I2C)
+
+
+# Link rules for multi-part drivers.
+
+db1550_tr.o: $(db1550_tr-objs)
+	$(LD) -r -o $@ $(db1550_tr-objs)
+
+db1550_dr.o: $(db1550_tr-objs)
+	$(LD) -r -o $@ $(au1550_tr-objs)
+
+#au1550_tr.o: $(au1550_tr-objs)
+#	$(LD) -r -o $@ $(au1550_tr-objs)
+
+au1x00_tr.o: $(au1x00_tr-objs)
+	$(LD) -r -o $@ $(au1x00_tr-objs)
+
+# dependencies:
+
+# local
+
+
diff -uNr linux/drivers/no-otg/ocd/au1x00/NOTES.txt linux/drivers/otg/ocd/au1x00/NOTES.txt
--- linux/drivers/no-otg/ocd/au1x00/NOTES.txt	1970-01-01 01:00:00.000000000 +0100
+++ linux/drivers/otg/ocd/au1x00/NOTES.txt	2006-09-01 21:41:31.000000000 +0200
@@ -0,0 +1,62 @@
+
+Notes
+
+1. EP0 - packetsize of 8 does not work, this means that PIO cannot be used for IN (transmit).
+DMA must be used. Only 16, 32 and 64 are usable.
+
+Update: even though we couldn't program the endpoint size to 8, we just set it to 8 in the
+device descriptor and things work ok. This works and we don't need or use DMA for either
+direction for EP0.
+
+2. For each BULK transfer the first N packets must be sent with the full packetsize and the
+last must be sent as a short packet. The packetsize must be set before enabling the DMA.
+
+3. For IN endpoints an interrupt will be generated for EVERY IN packet including ones that
+will be NAK'd. To reduce overhead the interrupt should only be enabled when there is data
+being sent and we need to know when it has been sent.
+
+Update: for new silicon this is not true, the UDC will interrupt less often and will NAK
+until the interrupt is cleared.
+
+4. The AU1x00 auto-acks many setup commands and is very sensitive to latency of the irq
+handler processing the interrupt and clearing the ep0 fifo. Like the pxa we need to spool the
+requests and process later when we receive a request for data.  There is a special test in
+the bulk data receive handler to check if there are spooled requests to process.
+
+Update: this has been mostly mitigated by reducing all upper layer processing in recv_setup,
+event_irq etc so that they complete in less than a milli-second. This simplifies things and
+we can now pass UUT tests.
+
+5. There is a time sensitive problem in au_in_epn(). Without a udelay(8) sending large
+packets will eventually stall interrupts.
+
+Update: this is not required for new silicon.
+
+6. When receiving data (Bulk OUT) the UDC will not start NAKing until and unless the receive
+FIFO is full. This means that there is no reliable way to determine the end of one packet and
+the beginning of the next if there is any undue latency in handling the interrupt for the
+first packet.
+
+The Belcarra Windows and Macintosh network class drivers have an option that can be used
+to pad all outgoing packets to a multiple of 8 bytes. This helps eliminate this problem.
+
+Update: this is not a problem with new silicon, it NAK's until the interrupt is cleared.
+
+8. The original AU1x00 UDC generated an interrupt for EVERY IN packet. If we do not have
+any data we need to disable the interrupt for the endpoint to eliminate the overhead
+for processing them. The interrupt must be re-enabled when DMA is finished and we
+actually want to know that the endpoint has finished.
+
+Leaving the interrupt enabled also interfers with the DMA process. It is not apparant
+why.
+
+Update: This has been fixed on the new silicon but it doesn't hurt to continue to do this.
+
+9. The DMA functions replace the equivalent routines in au1100_dma.h except that they pass
+the actual struct dma_chan pointer instead of the channel number.  The bounds checking and
+table lookup to derive this information amounts to a substantial increase in code size and
+latency which can simply be avoided by using the structure address directly and/or doing the
+equivalent i/o directly.
+
+10. The Au1x00 does not support REMOTE WAKEUP
+
diff -uNr linux/drivers/no-otg/ocd/au1x00/au1550.c linux/drivers/otg/ocd/au1x00/au1550.c
--- linux/drivers/no-otg/ocd/au1x00/au1550.c	1970-01-01 01:00:00.000000000 +0100
+++ linux/drivers/otg/ocd/au1x00/au1550.c	2006-09-01 21:41:31.000000000 +0200
@@ -0,0 +1,1451 @@
+/*
+ * otg/ocd/au1x00/au1x00.c -- USB Device Controller driver. 
+ *
+ *      Copyright (c) 2004 Belcarra
+ *
+ * By: 
+ *      Stuart Lynne <sl@belcarra.com>, 
+ *      Tom Rushworth <tbr@belcarra.com>, 
+ *
+ */
+/*!
+ * @file otg/ocd/au1x00/au1550.c
+ * @brief AU1X00 Header
+ *
+ * This file contains the private defines and structures for the AU1X00  
+ * Drivers. 
+ *
+ * @ingroup AU1X00
+ */
+
+
+#include <otg/pcd-include.h>
+#include "au1x00.h"
+
+#include <asm/io.h>
+#include <asm/au1000.h>
+#include <asm/au1000_dma.h>
+
+#if defined(CONFIG_SOC_AU1550)
+#include <asm/au1xxx_dbdma.h>
+#else
+#error "Wrong AU1X00_MDA"
+#endif
+
+#include <asm/mipsregs.h>
+
+#ifdef CONFIG_MIPS_FREEHAND
+#include <linux/i2c.h>
+#include <linux/sensors.h>      /* for reading serial number */
+#endif
+
+
+#undef CHECK_LATENCY
+#undef RECORD_LATENCY
+#undef MAX_INTR_LOOP_STATS   //10
+#if defined(MAX_INTR_LOOP_STATS)
+static u32 interrupt_loop_stats[MAX_INTR_LOOP_STATS+1];
+#endif
+#ifdef  RECORD_LATENCY
+#define CP0_COUNTS 50
+u32 cp0_counts[CP0_COUNTS];
+u32 cp0_record;
+#endif
+u32 cp0_count;
+unsigned int udc_saw_bus_activity;
+int au_halt_dma_expired;
+u32 au1x00_inten;
+
+u8 au1x00_config_bulk[25] = {
+        0x04, (USB_ENDPOINT_CONTROL << 4) | (EP0_PACKETSIZE & 0x380) >> 7,                            // 1
+        (EP0_PACKETSIZE & 0x7F) << 1, 0x00, 0x00,
+        0x24, (USB_ENDPOINT_BULK << 4) | USB_DIR_IN | (MAX_EPN_PACKET_SIZE & 0x380) >> 7,             // 6
+        (MAX_EPN_PACKET_SIZE & 0x7F) << 1, 0x00, 0x02, 
+        0x34, (USB_ENDPOINT_BULK << 4) | USB_DIR_IN | (MAX_EPN_PACKET_SIZE & 0x380) >> 7,             // 11
+        (MAX_EPN_PACKET_SIZE & 0x7F) << 1, 0x00, 0x03, 
+        0x44, (USB_ENDPOINT_BULK << 4) | USB_DIR_OUT | (MAX_EPN_PACKET_SIZE & 0x380) >> 7,            // 16
+        (MAX_EPN_PACKET_SIZE & 0x7F) << 1, 0x00, 0x04,
+        0x54, (USB_ENDPOINT_BULK << 4) | USB_DIR_OUT | (MAX_EPN_PACKET_SIZE & 0x380) >> 7,            // 21
+        (MAX_EPN_PACKET_SIZE & 0x7F) << 1, 0x00, 0x05,
+};
+
+/* ********************************************************************************************* */
+/* Note - The endpoints names are confusing. To simplify mapping of the interrupt request lines
+ * we use a numbering of the physical endpoints of 0-5, which also matches the fifo numbering
+ * (see the config block).
+ *
+ *   Physical  FIFO    Name    Direction   Logical
+ *      0       0       EP0     OUT         0
+ *      1       1       EP0     IN          0
+ *      2       2       EP1     IN          81
+ *      3       3       EP2     IN          82
+ *      4       4       EP3     OUT         3
+ *      5       5       EP4     OUT         4
+ *
+ * The ep_regs array maps a physical endpoint number to the registers required to access the udc
+ * for that endpoint. Note that ep0 is always accessed via 0.  The epl2p array maps the logical
+ * addresses back to the physical number.  The epp2l array maps the physical number to the
+ * logical endpoint address
+ */
+struct ep_regs ep_regs[6] = {
+
+        { rd: USBD_EP0RD, rds: USBD_EP0RDSTAT, /*rx_id: DMA_ID_USBDEV_EP0_RX,*/ cs: USBD_EP0CS, rx_str: "EP0 OUT RD",
+          wr: USBD_EP0WR, wrs: USBD_EP0WRSTAT, tx_id: DSCR_CMD0_USBDEV_TX0,                 tx_str: "EP0 IN WR",},
+
+        { rds: 0, wrs: 0, indma: -1, outdma: -1, },     // ep0
+        { wr: NUSBD_EP1WR, wrs: NUSBD_EP1WRSTAT, tx_id: DSCR_CMD0_USBDEV_TX1, cs: NUSBD_EP1CS, tx_str: "EP1 IN WR",},
+        { wr: NUSBD_EP2WR, wrs: NUSBD_EP2WRSTAT, tx_id: DSCR_CMD0_USBDEV_TX2, cs: NUSBD_EP2CS, tx_str: "EP2 IN WR",},
+
+        { rd: NUSBD_EP3RD, rds: NUSBD_EP3RDSTAT, rx_id: DSCR_CMD0_USBDEV_RX3, cs: NUSBD_EP3CS, rx_str: "EP3 OUT RD",},
+        { rd: NUSBD_EP4RD, rds: NUSBD_EP4RDSTAT, rx_id: DSCR_CMD0_USBDEV_RX4, cs: NUSBD_EP4CS, rx_str: "EP4 OUT RD",},
+};
+u8 epl2p[6] = { 0x00, 0x02, 0x03, 0x04, 0x05, 0x00, };        // map logical to physical
+u8 epp2l[6] = { 0x00, 0x00, 0x81, 0x82, 0x03, 0x04, };        // map physical to logical
+
+static __inline__ void au_fifo_read(struct ep_regs *ep, unsigned char * cp, int bytes)
+{
+        u32 rd = ep->rd;
+        for (; bytes--; *cp++ = au_readl(rd));
+}
+
+static __inline__ void au_fifo_write(int ep, unsigned char * cp, int bytes)
+{
+        u32 wr = ep_regs[ep].wr;
+        for (; bytes--; au_writel(*cp++, wr));
+        ep_regs[ep].last = bytes;
+}
+
+void __inline__ au_inten(u32 inten, char *msg)
+{
+        TRACE_MSG2(PCD, "%s %08x", msg, inten);
+        au1x00_inten = inten;
+        au_writel(au1x00_inten, USBD_INTEN);
+}
+
+void __inline__ au_epn_interrupt_enable(int epn)
+{
+        au_inten(au1x00_inten | (1 << epn), "epn enable");
+}
+
+void __inline__ au_epn_interrupt_disable(int epn)
+{
+        au_inten(au1x00_inten & ~(1 << epn), "epn disable");
+        au_writel((1 << epn) , USBD_INTSTAT);
+}
+
+static void __inline__ send_zlp(unsigned char epn)
+{
+        au_writel(USBDEV_FSTAT_FLUSH | USBDEV_FSTAT_UF | USBDEV_FSTAT_OF, ep_regs[epn].wrs);
+        au_writel(0 << 1, ep_regs[epn].cs);
+        au_writel(0, ep_regs[epn].wr);
+}
+/* ********************************************************************************************* */
+#define AU_DMA_HALT_POLL 0x1000
+
+#define         DDMA_DESCCMD_ARB                        (1<<15)
+#define         DDMA_DESCCMD_DW                         (16)
+#define         DDMA_DESCCMD_DW_N(n)            ((n&3)<<DDMA_DESCCMD_DW)
+ #define                DDMA_DESCCMD_DW_BYTE    DDMA_DESCCMD_DW_N(0)
+ #define                DDMA_DESCCMD_DW_HWORD   DDMA_DESCCMD_DW_N(1)
+ #define                DDMA_DESCCMD_DW_WORD    DDMA_DESCCMD_DW_N(2)
+#define         DDMA_DESCCMD_SW                         (18)
+#define         DDMA_DESCCMD_SW_N(n)            ((n&3)<<DDMA_DESCCMD_SW)
+ #define                DDMA_DESCCMD_SW_BYTE    DDMA_DESCCMD_SW_N(0)
+ #define                DDMA_DESCCMD_SW_HWORD   DDMA_DESCCMD_SW_N(1)
+ #define                DDMA_DESCCMD_SW_WORD    DDMA_DESCCMD_SW_N(2)
+
+#define         DDMA_DESCCMD_DID                        (20)
+#define         DDMA_DESCCMD_DID_N(n)           ((n&0x1F)<<DDMA_DESCCMD_DID)
+#define         DDMA_DESCCMD_SID                        (25)
+#define         DDMA_DESCCMD_SID_N(n)           ((n&0x1F)<<DDMA_DESCCMD_SID)
+
+/*      
+ *      Bit definitions for source stride/block 1 dimensional
+ */
+#define         DDMA_DESCSRC_STRIDE_SS                          (0)
+#define         DDMA_DESCSRC_STRIDE_SS_N(n)                     ((n&0x3fff)<<DDMA_DESCSRC_STRIDE_SS)
+#define         DDMA_DESCSRC_STRIDE_SB                          (14)
+#define         DDMA_DESCSRC_STRIDE_SB_N(n)                     ((n&0x3Fff)<<DDMA_DESCSRC_STRIDE_SB)
+#define         DDMA_DESCSRC_STRIDE_SAM                         (28)
+#define         DDMA_DESCSRC_STRIDE_SAM_N(n)            ((n&3)<<DDMA_DESCSRC_STRIDE_SAM)
+ #define                DDMA_DESCSRC_STRIDE_SAM_INC             DDMA_DESCSRC_STRIDE_SAM_N(0)
+ #define        DDMA_DESCSRC_STRIDE_SAM_DEC             DDMA_DESCSRC_STRIDE_SAM_N(1)
+ #define                DDMA_DESCSRC_STRIDE_SAM_STATIC  DDMA_DESCSRC_STRIDE_SAM_N(2)
+ #define                DDMA_DESCSRC_STRIDE_SAM_BURST   DDMA_DESCSRC_STRIDE_SAM_N(3)
+#define         DDMA_DESCSRC_STRIDE_STS                         (30)
+#define         DDMA_DESCSRC_STRIDE_STS_N(n)            ((n&3)<<DDMA_DESCSRC_STRIDE_STS)
+ #define                DDMA_DESCSRC_STRIDE_STS_1               DDMA_DESCSRC_STRIDE_STS_N(0)
+ #define                DDMA_DESCSRC_STRIDE_STS_2               DDMA_DESCSRC_STRIDE_STS_N(1)
+ #define                DDMA_DESCSRC_STRIDE_STS_4               DDMA_DESCSRC_STRIDE_STS_N(2)
+ #define                DDMA_DESCSRC_STRIDE_STS_8               DDMA_DESCSRC_STRIDE_STS_N(3)
+                                        
+
+/*
+ *      Bit definitions for dest         stride/block 1 dimensional 
+ */
+#define         DDMA_DESCDST_STRIDE_DS                          (0)
+#define         DDMA_DESCDST_STRIDE_DS_N(n)                     ((n&0x3fff)<<DDMA_DESCDST_STRIDE_DS)
+#define         DDMA_DESCDST_STRIDE_DB                          (14)
+#define         DDMA_DESCDST_STRIDE_DB_N(n)                     ((n&0x3Fff)<<DDMA_DESCDST_STRIDE_DB)
+#define         DDMA_DESCDST_STRIDE_DAM                         (28)
+#define         DDMA_DESCDST_STRIDE_DAM_N(n)            ((n&3)<<DDMA_DESCDST_STRIDE_DAM)
+ #define                DDMA_DESCDST_STRIDE_DAM_INC             DDMA_DESCDST_STRIDE_DAM_N(0)
+ #define        DDMA_DESCDST_STRIDE_DAM_DEC             DDMA_DESCDST_STRIDE_DAM_N(1)                                               #define                DDMA_DESCDST_STRIDE_DAM_STATIC  DDMA_DESCDST_STRIDE_DAM_N(2)
+ #define                DDMA_DESCDST_STRIDE_DAM_BURST   DDMA_DESCDST_STRIDE_DAM_N(3)
+#define         DDMA_DESCDST_STRIDE_DTS                         (30)
+#define         DDMA_DESCDST_STRIDE_DTS_N(n)            ((n&3)<<DDMA_DESCDST_STRIDE_DTS)
+ #define                DDMA_DESCDST_STRIDE_DTS_1               DDMA_DESCDST_STRIDE_DTS_N(0)
+ #define                DDMA_DESCDST_STRIDE_DTS_2               DDMA_DESCDST_STRIDE_DTS_N(1)
+ #define                DDMA_DESCDST_STRIDE_DTS_4               DDMA_DESCDST_STRIDE_DTS_N(2)
+ #define                DDMA_DESCDST_STRIDE_DTS_8               DDMA_DESCDST_STRIDE_DTS_N(3)
+
+#ifndef CONFIG_MIPS_MTX2
+typedef struct dbdma_device_table {
+        u32             dev_flags;
+        u32             dev_tsize;
+        u32             dev_devwidth;
+        u32             dev_physaddr;           /* If FIFO */
+        u32             dev_intlevel;
+        u32             dev_intpolarity;
+} dbdev_tab_t;
+
+typedef struct dbdma_chan_config {
+        u32                     chan_flags;
+        u32                     chan_index;
+        dbdev_tab_t             *chan_src;
+        dbdev_tab_t             *chan_dest;
+        au1x_dma_chan_t         *chan_ptr;
+        au1x_ddma_desc_t        *chan_desc_base;
+        au1x_ddma_desc_t        *get_ptr, *put_ptr, *cur_ptr;
+        void                    *chan_callparam;
+        void (*chan_callback)(int, void *, struct pt_regs *);
+} chan_tab_t;
+#endif
+static void __inline__ au_halt_dma(u32 chan)
+{
+        chan_tab_t                      *ctp = *((chan_tab_t **)chan);
+        volatile au1x_dma_chan_t        *cp = ctp->chan_ptr;;
+        au1x_ddma_desc_t                *dp;
+
+	au1xxx_dbdma_stop(chan);  
+        au_halt_dma_expired++;
+
+        dp = ctp->cur_ptr;              // ensure that descriptor is stopped
+        dp->dscr_cmd0 &= ~DSCR_CMD0_V;
+
+}
+
+static void __inline__ au_start_dma_in(int indma, u32 tx_id, __u8 *bp, int len)
+{
+        chan_tab_t                      *ctp = *((chan_tab_t **)indma);
+        au1x_ddma_desc_t                *dp = ctp->cur_ptr; 
+        volatile au1x_dma_chan_t        *cp = ctp->chan_ptr;;
+
+        TRACE_MSG8(PCD, "%08x %08x cmd: %08x %04x src: %08x %08x dst: %08x %08x MEM2USB", 
+                        indma, dp, dp->dscr_cmd0, dp->dscr_cmd1,
+                        dp->dscr_source0, dp->dscr_source1, dp->dscr_dest0, dp->dscr_dest1);
+
+        dp->dscr_source0 = virt_to_phys(bp);
+        dp->dscr_source1 = DDMA_DESCSRC_STRIDE_STS_4 | DDMA_DESCSRC_STRIDE_SAM_INC;
+
+        dp->dscr_cmd1 = len;
+        dp->dscr_cmd0  = 
+                DSCR_CMD0_SID(DSCR_CMD0_ALWAYS) |
+                DSCR_CMD0_DID(indma) 
+                ;
+        TRACE_MSG1(PCD, "cmd0: %08x SID/DID", dp->dscr_cmd0);
+
+        dp->dscr_cmd0  |= 
+                DDMA_DESCCMD_SW_BYTE |
+                DDMA_DESCCMD_DW_BYTE 
+                ;
+        TRACE_MSG1(PCD, "cmd0: %08x SW/DW", dp->dscr_cmd0);
+
+        dp->dscr_cmd0  = DSCR_CMD0_V |
+                DSCR_CMD0_SID(DSCR_CMD0_ALWAYS) |
+                DSCR_CMD0_DID(tx_id) |
+                DDMA_DESCCMD_SW_BYTE |
+                DDMA_DESCCMD_DW_BYTE |
+                DSCR_CMD0_ARB |
+                DSCR_CMD0_DN |
+                DSCR_CMD0_CV 
+                ;
+
+        dp->dscr_dest1 = DDMA_DESCDST_STRIDE_DTS_4 | DDMA_DESCSRC_STRIDE_SAM_STATIC;
+                
+        ctp->cur_ptr = dp;
+        cp->ddma_desptr = virt_to_phys(ctp->cur_ptr);
+        cp->ddma_cfg |= DDMA_CFG_EN;    /* Enable channel */
+        au_sync();
+        cp->ddma_dbell = 0xffffffff;    /* Make it go */
+        au_sync();
+
+        TRACE_MSG8(PCD, "%08x %08x cmd: %08x %04x src: %08x %08x dst: %08x %08x MEM2USB start", 
+                        indma, dp, dp->dscr_cmd0, dp->dscr_cmd1,
+                        dp->dscr_source0, dp->dscr_source1, dp->dscr_dest0, dp->dscr_dest1);
+}
+
+static void __inline__ au_start_dma_out(unsigned int outdma, u32 rx_id, __u8 *bp, int wMaxPacketSize)
+{
+        chan_tab_t                      *ctp = *((chan_tab_t **)outdma);
+        au1x_ddma_desc_t                *dp = ctp->cur_ptr; 
+        volatile au1x_dma_chan_t        *cp = ctp->chan_ptr;;
+
+        
+        dma_cache_inv((unsigned long) bp, wMaxPacketSize);
+
+        TRACE_MSG8(PCD, "%08x %08x cmd: %08x %04x src: %08x %08x dst: %08x %08x USB2MEM", 
+                        outdma, dp, dp->dscr_cmd0, dp->dscr_cmd1,
+                        dp->dscr_source0, dp->dscr_source1, dp->dscr_dest0, dp->dscr_dest1);
+
+        // if (!au1xxx_dbdma_put_dest(outdma, bp, wMaxPacketSize)) 
+        
+        dp->dscr_dest0 = virt_to_phys(bp);
+        dp->dscr_dest1 = DDMA_DESCDST_STRIDE_DTS_4 | DDMA_DESCDST_STRIDE_DAM_INC;
+
+        dp->dscr_cmd1 = wMaxPacketSize;
+        dp->dscr_cmd0  = DSCR_CMD0_V |
+                DSCR_CMD0_SID(rx_id) |
+                DSCR_CMD0_DID(DSCR_CMD0_ALWAYS) |
+                DDMA_DESCCMD_SW_BYTE |
+                DDMA_DESCCMD_DW_BYTE |
+                DSCR_CMD0_ARB |
+                DSCR_CMD0_SN |
+                DSCR_CMD0_CV 
+                ;
+
+        dp->dscr_source1 = DDMA_DESCSRC_STRIDE_STS_4 | DDMA_DESCSRC_STRIDE_SAM_STATIC;
+
+        ctp->cur_ptr = dp;
+        cp->ddma_desptr = virt_to_phys(ctp->cur_ptr);
+        cp->ddma_cfg |= DDMA_CFG_EN;    /* Enable channel */
+        au_sync();
+        cp->ddma_dbell = 0xffffffff;    /* Make it go */
+        au_sync();
+
+        TRACE_MSG8(PCD, "%08x %08x cmd: %08x %04x src: %08x %08x dst: %08x %08x USB2MEM start", 
+                        outdma, dp, dp->dscr_cmd0, dp->dscr_cmd1,
+                        dp->dscr_source0, dp->dscr_source1, dp->dscr_dest0, dp->dscr_dest1);
+}
+
+/* ********************************************************************************************* */
+static struct usbd_urb *au_rcv_complete_ep0_irq(struct usbd_endpoint_instance *endpoint, int len, int urb_bad)
+{
+        struct usbd_urb *rcv_urb = endpoint->rcv_urb;
+        if (rcv_urb && !urb_bad) {
+                int i;
+                u8 *cp = rcv_urb->buffer + rcv_urb->actual_length;
+                for (i = 0 ; i < len; i+= 8)
+                        TRACE_MSG8(PCD, "ARCV  %02x %02x %02x %02x %02x %02x %02x %02x",
+                                        cp[i + 0], cp[i + 1], cp[i + 2], cp[i + 3],
+                                        cp[i + 4], cp[i + 5], cp[i + 6], cp[i + 7]
+                                  );
+
+        }
+        return pcd_rcv_complete_irq(endpoint, len, urb_bad);
+}
+
+/* au_start_in_ep0 - start transmit
+ */
+static void au_start_in_ep0 (struct usbd_endpoint_instance *endpoint, struct ep_regs *ep)
+{
+	struct usbd_urb *urb = endpoint->tx_urb;
+        int last = ep->last = endpoint->last = MIN (urb->actual_length - endpoint->sent, endpoint->wMaxPacketSize);
+        TRACE_MSG2(PCD, "START IN EP0 SENT: %d SENDING: %d", endpoint->sent, last);
+        RETURN_IF ((urb->actual_length - endpoint->sent) <= 0);
+        au_writel(last << 1, ep->cs); // XXX
+        au_fifo_write(0, urb->buffer + endpoint->sent, last);
+        endpoint->last = last;
+}
+
+/* au_in_ep0 - called to service an endpoint zero IN interrupt, data sent
+ */
+static void au_in_ep0(struct usbd_endpoint_instance *endpoint, struct ep_regs *ep)
+{
+        u32 cs;
+        struct usbd_urb *tx_urb;
+        int last;
+        TRACE_MSG1(PCD, "EP0 IN: tx_urb: %p", (int)endpoint->tx_urb);
+        if ((cs = au_readl(ep->cs)) & USBDEV_CS_STALL) {                // clear stall if present
+                TRACE_MSG1(PCD, "CLEAR STALL %d", 0);
+                cs &= ~USBDEV_CS_STALL;
+                au_writel(cs, ep->cs);
+                return;
+        }
+        if (!(tx_urb = pcd_tx_complete_irq(endpoint, 0))) {               // wait for setup if no more data
+                endpoint->state = WAIT_FOR_SETUP;
+                return;
+        }
+        TRACE_MSG4(PCD, "EP0 IN actual: %d last: %d sent: %d flags: %x", endpoint->tx_urb->actual_length, 
+                        endpoint->last, endpoint->sent, endpoint->tx_urb->flags);
+        if (pcd_tx_sendzlp(endpoint)) {                          // check if tx_urb we have is finished
+                TRACE_MSG0(PCD, "EP0 IN BULK - sending ZLP");
+                tx_urb->flags &= ~USBD_URB_SENDZLP;
+                send_zlp(0);
+                pcd_tx_complete_irq(endpoint, 0);
+                return;
+        }
+        if (tx_urb->actual_length > endpoint->sent) {
+                if ((tx_urb->actual_length - endpoint->sent) < endpoint->wMaxPacketSize) 
+                        TRACE_MSG1(PCD, "EP0 IN starting short packet %d", tx_urb->actual_length - endpoint->sent);
+                else 
+                        TRACE_MSG1(PCD, "EP0 IN LEFT TO SEND %d", tx_urb->actual_length - endpoint->sent);
+                au_start_in_ep0(endpoint, ep);
+        }
+}
+
+/* au_out_ep0 - called to service an endpoint zero OUT interrupt, data received
+ */
+static void au_out_ep0(struct usbd_endpoint_instance *endpoint, struct ep_regs *ep)
+{
+        struct usbd_device_request request;
+        int i;
+        u32 cs;
+        u32 bytes;
+        TRACE_MSG0(PCD, "EP0 OUT");
+        cs = au_readl(ep->cs);                                  // check if host aborted transfer and flush the write fifo
+        bytes = au_readl(ep->rds) & USBDEV_FSTAT_FCNT_MASK;
+        if (endpoint->state == DATA_STATE_RECV) {
+                struct usbd_urb *rcv_urb = pcd_rcv_next_irq(endpoint);
+                TRACE_MSG1(PCD, "EP0 OUT: RECV: rcv_urb: %x", (int) rcv_urb);
+                if (rcv_urb) {
+                        au_fifo_read(ep, rcv_urb->buffer + rcv_urb->actual_length, bytes);
+                        if (au_rcv_complete_ep0_irq(endpoint, bytes, 0)) 
+                                return;
+                        au_writel(USBDEV_FSTAT_FLUSH | USBDEV_FSTAT_UF | USBDEV_FSTAT_OF, ep->rds);
+                        endpoint->state = WAIT_FOR_SETUP;
+                        send_zlp(0);
+                        return;
+                }
+                endpoint->state = WAIT_FOR_SETUP;
+        }
+        pcd_tx_cancelled_irq(endpoint);
+        pcd_rcv_cancelled_irq(endpoint);
+        au_fifo_read(ep, (u8 *)&request, bytes);
+        au_writel(USBDEV_FSTAT_FLUSH | USBDEV_FSTAT_UF | USBDEV_FSTAT_OF, ep->wrs);
+        if (bytes != 8) {
+                TRACE_MSG1(PCD, "ERROR SETUP SET not eight bytes: %d", bytes);
+                au_writel(USBDEV_FSTAT_FLUSH | USBDEV_FSTAT_UF | USBDEV_FSTAT_OF, ep->wrs);
+                return;
+        }
+        TRACE_MSG4(PCD, "SETUP bmRequestType: %02x bRequest %02x state: %d status: %d", request.bmRequestType, 
+                        request.bRequest, pcd_instance->bus->device_state, pcd_instance->bus->status);
+        switch (request.bRequest) {                     // we need to simply ignore any of these
+        case USB_REQ_SET_ADDRESS:                       // Fake a bus reset IFF not state default and then process normally
+                BREAK_IF (pcd_instance->bus->device_state == STATE_DEFAULT);
+                udc_saw_bus_activity = 0;
+                usbd_bus_event_handler_irq (pcd_instance->bus, DEVICE_RESET, 0);
+                //usbd_bus_event_handler_irq (pcd_instance->bus, DEVICE_ADDRESS_ASSIGNED, 0);
+                break;
+        case USB_REQ_GET_DESCRIPTOR:                    // Fake a bus reset IFF suspended and then process normally
+                BREAK_IF (STATE_SUSPENDED != pcd_instance->bus->device_state);
+                udc_saw_bus_activity = 0;
+                usbd_bus_event_handler_irq (pcd_instance->bus, DEVICE_RESET, 0);
+                //usbd_bus_event_handler_irq (pcd_instance->bus, DEVICE_ADDRESS_ASSIGNED, 0);
+                break;
+        }
+        if (pcd_recv_setup_irq(pcd_instance, &request)) {
+                TRACE_MSG1(PCD, "ep0 STALL %d", cs);
+                au_writel(USBDEV_CS_STALL, USBD_EP0CS);
+                return;
+        }
+        if (((request.bmRequestType & USB_REQ_DIRECTION_MASK) == USB_REQ_HOST2DEVICE) && le16_to_cpu (request.wLength)) {
+                TRACE_MSG1(PCD, "ep0 Class H2D request %04x", le16_to_cpu(request.wLength));
+                endpoint->state = DATA_STATE_RECV;
+                return;
+        }                               
+        if ((request.bmRequestType & USB_REQ_DIRECTION_MASK) == USB_REQ_HOST2DEVICE) {
+                TRACE_MSG1(PCD, "ep0 Class H2D request %04x", le16_to_cpu(request.wLength));
+                if ((request.bmRequestType & ~USB_REQ_DIRECTION_MASK)) {
+                        TRACE_MSG1(PCD, "ep0 Class or Vendor, send ZLP %d", cs);
+                        send_zlp(0);
+                        return;
+                }
+        }
+        TRACE_MSG1(PCD, "ep0 Class D2H request %04x", le16_to_cpu(request.wLength));
+}
+/* ********************************************************************************************* */
+/* au_start_in_bulk - start transmit
+ * The au1x00 will start to send when the first byte is loaded into the FIFO, either by
+ * DMA or PIO. The packetsize must be set first.
+ */
+static void au_start_in_bulk (unsigned int epn, struct usbd_endpoint_instance *endpoint, struct ep_regs *ep)
+{
+        struct usbd_urb *urb = endpoint->tx_urb;
+        u8 *bp = urb->buffer + endpoint->sent;
+        int last;
+        int i;
+        u32 *lp;
+        TRACE_MSG1(PCD, "START IN BULK %d", epn);
+        RETURN_IF (!urb || (( (urb->actual_length - endpoint->sent) == 0) && !(urb->flags & USBD_URB_SENDZLP)));
+        last = ep->last = endpoint->last = MIN (urb->actual_length - endpoint->sent, endpoint->wMaxPacketSize);
+        TRACE_MSG2(PCD, "START IN BULK sent: %d last:%d", endpoint->sent, last);
+        au_writel(USBDEV_FSTAT_FLUSH | USBDEV_FSTAT_UF | USBDEV_FSTAT_OF, ep->wrs); // XXX
+        if (!last) {
+                if (endpoint->tx_urb->flags & USBD_URB_SENDZLP) {
+                        TRACE_MSG0(PCD, "START IN BULK - zending ZLP");
+                        send_zlp(epn);
+                        endpoint->tx_urb->flags &= ~USBD_URB_SENDZLP;
+                }
+                return;
+        }
+        else if (8 >= last) {
+                au_writel(last << 1, ep->cs);
+                au_fifo_write(epn, bp, last);
+                return;
+        }
+
+        dma_cache_wback_inv((unsigned long) bp, last); 
+        //au_writel(last << 1, ep->cs);
+        au_start_dma_in(ep->indma, ep->tx_id, bp, last);
+        au_writel(last << 1, ep->cs);
+}
+
+static void au_in_bulk(unsigned int epn, struct ep_regs *ep, struct usbd_endpoint_instance *endpoint)
+{
+        struct usbd_urb *tx_urb;
+        int rc = 0;
+        u32 cs = au_readl(ep->cs);
+        u32 wrs = au_readl(ep->wrs);
+        u32 residue = au1xxx_get_dma_residue(ep->indma);
+        TRACE_MSG3(PCD, "BULK IN EPN - cs: %x wrs: %x residue:%d", cs, wrs, residue);
+        if (wrs) {                                                              // check for underflow or overflow
+                rc = 1;
+                if (wrs & USBDEV_FSTAT_UF) {
+                        TRACE_MSG2(PCD, "BULK IN EPN - UF epn %d wrs: %x", epn, wrs);
+                        rc = 1;                                                 // set rc to indicate an error
+                }
+                if (wrs & USBDEV_FSTAT_OF) 
+                        TRACE_MSG2(PCD, "BULK IN EPN - OF epn %d wrs: %x", epn, wrs);
+                au_writel(USBDEV_FSTAT_FLUSH | USBDEV_FSTAT_UF | USBDEV_FSTAT_OF, ep->wrs);     // flush the fifo 
+        }
+        if (cs & USBDEV_CS_NAK) {
+                RETURN_IF (ep->last && ((wrs&0x1f) == ep->last));
+                rc = 1;
+        }
+        if (cs & USBDEV_CS_STALL) {                                             // clear stall if present
+                TRACE_MSG1(PCD, "BULK IN EPN - CLEAR STALL %d", epn);
+                cs &= ~USBDEV_CS_STALL;
+                au_writel(cs, ep->cs);
+                return;
+        }
+        TRACE_MSG4(PCD, "BULK IN EPN epn: %d rc: %d last: %d sent: %d", epn, rc, endpoint->last, endpoint->sent);
+
+        if ((tx_urb = pcd_tx_complete_irq(endpoint, rc))) {
+                if ((tx_urb->actual_length > endpoint->sent) || (endpoint->tx_urb->flags & USBD_URB_SENDZLP)) {
+                        au_start_in_bulk(epl2p[endpoint->bEndpointAddress&0xf], endpoint, ep);
+                        TRACE_MSG1(PCD, "BULK IN EPN - LEFT TO SEND %d", tx_urb->actual_length - endpoint->sent);
+                        return;
+                }
+        }
+        au_epn_interrupt_disable(epn);                                         // disable interrupts
+}
+
+/* au_start_in_iso - start transmit
+ * The au1x00 will start to send when the first byte is loaded into the FIFO, either by DMA or
+ * PIO. The packetsize must be set first.
+ */
+static void au_start_in_iso (unsigned int epn, struct usbd_endpoint_instance *endpoint, struct ep_regs *ep)
+{
+        struct usbd_urb *urb = endpoint->tx_urb;
+        unsigned char *bp = urb->buffer + endpoint->sent;
+        int last;
+        TRACE_MSG2(PCD, "START IN ISO actual: %d sent: %d", urb->actual_length, endpoint->sent);
+        RETURN_IF ((urb->actual_length - endpoint->sent) == 0);
+        last = ep->last = endpoint->last = MIN (urb->actual_length - endpoint->sent, endpoint->wMaxPacketSize);
+        TRACE_MSG2(PCD, "START IN ISO last: %d packetSize: %d", last, endpoint->wMaxPacketSize);
+        au_writel(USBDEV_FSTAT_FLUSH | USBDEV_FSTAT_UF | USBDEV_FSTAT_OF, ep->wrs); // XXX
+        au_epn_interrupt_enable (epn);
+        dma_cache_wback_inv((unsigned long) bp, last);
+        au_writel(last << 1, ep->cs);
+        au_start_dma_in(ep->indma, ep->tx_id, bp, last);
+}
+
+static void au_in_iso(unsigned int epn, struct ep_regs *ep, struct usbd_endpoint_instance *endpoint)
+{
+        struct usbd_urb *tx_urb = pcd_tx_complete_irq(endpoint, 0);
+        u32 cs = au_readl(ep->cs);
+        u32 wrs = au_readl(ep->wrs);
+        TRACE_MSG2(PCD, "ISO IN EPN - cs: %x wrs: %x", cs, wrs);
+        TRACE_MSG4(PCD, "ISO IN EPN epn: %d last: %d sent: %d", epn, endpoint->last, endpoint->sent, 0);
+        ep->last = 0;
+        if ((tx_urb = endpoint->tx_urb) && (tx_urb->actual_length > endpoint->sent)) {
+                au_start_in_iso(epl2p[endpoint->bEndpointAddress&0xf], endpoint, ep);
+                /* XXX magic delay - without this large packets will eventually stall the transmit
+                 * XXX and all traffic in both directions will stop. 
+                 */
+                TRACE_MSG1(PCD, "ISO IN EPN - LEFT TO SEND %d", tx_urb->actual_length - endpoint->sent);
+                return;
+        }
+
+        TRACE_MSG0(PCD, "ISO IN EPN - nothing to send");
+        au_epn_interrupt_disable(epn);                                 // disable interrupts
+}
+/* ********************************************************************************************* */
+
+static struct usbd_urb *au_rcv_complete_epn_irq(struct usbd_endpoint_instance *endpoint, int len, int urb_bad)
+{
+        return pcd_rcv_complete_irq(endpoint, len, urb_bad);
+}
+
+
+static void au_start_out_bulk(unsigned int epn, struct usbd_endpoint_instance *endpoint, struct ep_regs *ep)
+{
+        TRACE_MSG2(PCD, "START OUT BULK %d actual: %d pcktSize: %d", epn, endpoint->wMaxPacketSize);
+        if (!endpoint->rcv_urb) {
+                TRACE_MSG0(PCD, "START OUT BULK DISABLE");
+                au_epn_interrupt_disable(epn);
+                return;
+        }
+        if (endpoint->rcv_error) {
+                TRACE_MSG0(PCD, "START OUT BULK reseting rcv_error");
+                endpoint->rcv_error = 0;
+        }
+        au_start_dma_out(ep->outdma, ep->rx_id, 
+                        endpoint->rcv_urb->buffer + endpoint->rcv_urb->actual_length, endpoint->wMaxPacketSize);
+}
+
+static void au_out_bulk(unsigned int epn, struct ep_regs *ep, struct usbd_endpoint_instance *endpoint)
+{
+        int bytes = 0;
+        int residue = 0;
+        u32 *lp = NULL;
+
+        struct usbd_urb *urb;
+        struct usbd_urb *completed_urb = NULL;
+        u32 cs;
+        u32 rds;
+        u32 nrds;
+        int i;
+
+        cs = au_readl(ep->cs);
+        rds = au_readl(ep->rds);
+
+        // XXX it appears that this is zero if 64 bytes where received!!!
+        
+        // get dest ring buffer
+        au1xxx_dbdma_get_dest(ep->outdma, (void **)&lp, &bytes);
+
+        au_halt_dma(ep->outdma);
+        residue = endpoint->wMaxPacketSize - au1xxx_get_dma_residue(ep->outdma);
+
+        TRACE_MSG5(PCD, "BULK OUT CS: %04x RD: %04x bytes: %d residue: %d DMA %s", 
+                        cs, rds, bytes, residue, lp ? "FINISHED" : "ACTIVE");
+
+        /* check for NAK
+         */
+        if (cs & USBDEV_CS_NAK) {
+                TRACE_MSG0(PCD, "NAK bytes: 0");
+                bytes = 0;
+        }
+
+        /* with dbdma residue will either be zero or non-zero and less than packetsize, if
+         * zero then a complete transfer of packetsize was performed.
+         */
+        else if (!residue && !rds) {
+                TRACE_MSG0(PCD, "BULK OUT CS: forcing bytes: 64");
+                bytes = endpoint->wMaxPacketSize;
+        }
+        else {
+                TRACE_MSG0(PCD, "BULK OUT CS: using residue bytes: 64");
+                bytes = residue;
+        }
+
+
+        if (!(urb = pcd_rcv_next_irq(endpoint))) {
+                TRACE_MSG0(PCD, "BULK OUT EPN - no rcv_urb");
+                au_epn_interrupt_disable(epn);
+                return;
+        }
+
+        TRACE_MSG4(PCD, "BULK OUT CS: %04x RD: %04x actual: %d DMA bytes: %d", cs, rds, urb->actual_length, bytes);
+
+        if (bytes)
+                dma_cache_inv((u32) urb->buffer + urb->actual_length, bytes);
+
+        /* The original AU1X00 UDC design will continue to receive data as long as there is room
+         * in the FIFO. We cannot tell when we are at the end of a packet and/or have the start
+         * of a new one.
+         *
+         * There are only two scenarios that are guaranteed (almost) to be correct:
+         *
+         *      64 bytes of data from DMA, empty fifo, continue Bulk OUT < 60 bytes of data and
+         *      < 4 bytes in fifo, end Bulk OUT.
+         *
+         * There may be a third scenario that is ok:
+         *
+         *      0 bytes dma, 0 bytes in fifo, NAK
+         *
+         * Everything else is an error. In all cases we assume that it is safer to drop data
+         * than to accept it in error. This allows CRC or size protected encapsulations to
+         * notice bulk transfers received with errors.
+         *
+         * In general none of the policies or strategies are able to cope with all errors
+         * without missing errors and dropping good data.  The intent is to minimize the amount
+         * of potentially bad data getting to the function driver while minimizing the amount of
+         * good data that is dropped.
+         *
+         * The new silicon mitigates this problem for non control endpoints because it will NAK
+         * additional data until the interrupt service flag is reset.
+         * 
+         * Start with generic error tests, OF, UF or NAK indicate an error we cannot recover
+         * from, start flushing until end of current bulk transfer (wait for a short packet)
+         *
+         * XXX The following needs to be tested and streamlined for au1550, much of the extra
+         * testing done for older silicon cab probably be eliminated. For example the extra read 
+         * of nrds to check for overrun in the FIFO should not be required.
+         *
+         */
+        if (rds & (USBDEV_FSTAT_OF | USBDEV_FSTAT_UF)) {
+                TRACE_MSG2(PCD, "BULK OUT FLUSHING %d length: %d", bytes, urb->actual_length);
+                THROW(start_flushing);
+        }
+        rds = rds & USBDEV_FSTAT_FCNT_MASK;
+        nrds = au_readl(ep->rds);
+
+        /* full size packet received, check that we are not flushing and that the FIFO
+         * does not have any data. If there is data in the FIFO we may not be able to
+         * restart DMA in time, so start flushing 
+         */
+        if (endpoint->wMaxPacketSize == bytes) {
+                TRACE_MSG2(PCD, "BULK OUT 64 BYTES %d length: %d", bytes, urb->actual_length);
+                if (endpoint->rcv_error) {
+                        TRACE_MSG4(PCD, "FULL PACKET bytes: %d rds: %d nrds: %d cp0: %d CONTINUE FLUSHING", 
+                                        bytes, rds, nrds, cp0_count);
+                        THROW(start_flushing);
+                }
+                if ((nrds > 6)  && (nrds < 8) ) {
+                        TRACE_MSG4(PCD, "FIFO not empty bytes: %d rds: %d nrds: %d cp0: %d START FLUSHING nrds > 6 < 8", 
+                                        bytes, rds, nrds, cp0_count);
+                        THROW(start_flushing);
+                }
+                if (!urb->actual_length) 
+                        TRACE_MSG4(PCD, "PACKET ok bytes: %d rds: %d nrds: %d cp0: %d ACCEPTING 64 bytes", 
+                                        bytes, rds, nrds, cp0_count);
+                au_writel(USBDEV_FSTAT_FLUSH | USBDEV_FSTAT_UF | USBDEV_FSTAT_OF, ep->rds);
+                au_rcv_complete_epn_irq(endpoint, endpoint->wMaxPacketSize, 0);
+                if (cs & USBDEV_CS_NAK) 
+                        if (nrds && (nrds < 8) ) {
+                                TRACE_MSG4(PCD, "NAK bytes: %d rds: %d nrds: %d cp0: %d START FLUSHING CS_NAK", 
+                                                bytes, rds, nrds, cp0_count);
+                                THROW(start_flushing);
+                        }
+        }
+        /* a nak'd packet may be ok to ignore IFF the FIFO is empty(?) or completely full.
+         */
+        else if ((cs & USBDEV_CS_NAK) && (!nrds || (nrds == 8)) ) {
+                TRACE_MSG2(PCD, "BULK OUT  NAK BYTES %d length: %d", bytes, urb->actual_length);
+                if (endpoint->rcv_error) {
+                        TRACE_MSG4(PCD, "NAK bytes: %d rds: %d nrds: %d cp0: %d CONTINUE FLUSHING", 
+                                        bytes, rds, nrds, cp0_count);
+                        THROW(start_flushing);
+                }
+                au_writel(USBDEV_FSTAT_FLUSH | USBDEV_FSTAT_UF | USBDEV_FSTAT_OF, ep->rds);
+        }
+        /* short packet by DMA, additional data for this packet in FIFO, more than 3
+         * bytes is probably an error.
+         */
+        else {
+                TRACE_MSG2(PCD, "BULK OUT < 64 BYTES %d length: %d", bytes, urb->actual_length);
+                if ((cs & USBDEV_CS_NAK) && nrds && (nrds < 8) ) {
+                        TRACE_MSG4(PCD, "BULK OUT NAK bytes: %d rds: %d nrds: %d cp0: %d START FLUSHING", 
+                                        bytes, rds, nrds, cp0_count);
+                        THROW(start_flushing);
+                }
+                if (nrds > 4) {
+                        TRACE_MSG4(PCD, "BULK OUT SHORT PACKET by DMA full FIFO bytes: %d rds: %d nrds: %d cp0: %d START FLUSH", 
+                                        bytes, rds, nrds, cp0_count);
+                        THROW(start_flushing);
+                }
+                TRACE_MSG4(PCD, "BULK OUT SHORT bytes: %d rds: %d nrds: %d cp0: %d reading fifo", bytes, rds, nrds, cp0_count);
+                au_fifo_read(ep, urb->buffer + urb->actual_length + bytes, nrds);
+                bytes += nrds;
+                TRACE_MSG1(PCD, "BULK OUT < 64 BYTES %d", bytes);
+                au_writel(USBDEV_FSTAT_FLUSH | USBDEV_FSTAT_UF | USBDEV_FSTAT_OF, ep->rds);
+                if (!endpoint->rcv_error) {
+                        TRACE_MSG0(PCD, "BULK OUT COMPLETED URB");
+                        au_rcv_complete_epn_irq(endpoint, bytes, 0);
+                }
+                else {
+                        TRACE_MSG0(PCD, "BULK OUT - FLUSHING URB - reseting rcv_error");
+                        endpoint->rcv_error = 0;
+                }
+        }
+        CATCH(start_flushing) {
+                TRACE_MSG0(PCD, "BULK OUT - START FLUSHING URB");
+                endpoint->rcv_error = 1;
+                endpoint->rcv_urb->actual_length = 0;
+                au_writel(USBDEV_FSTAT_FLUSH | USBDEV_FSTAT_UF | USBDEV_FSTAT_OF, ep->rds);
+                bytes = 0;
+        }
+        TRACE_MSG0(PCD, "BULK OUT - RESTARTING");
+        au_start_out_bulk(epn, endpoint, ep);
+}
+
+static void au_start_out_iso(unsigned int epn, struct usbd_endpoint_instance *endpoint, struct ep_regs *ep)
+{
+        RETURN_IF(!endpoint->rcv_urb);
+        au_start_dma_out(ep->outdma, ep->rx_id, 
+                        endpoint->rcv_urb->buffer + endpoint->rcv_urb->actual_length, endpoint->wMaxPacketSize);
+}
+
+static void au_out_iso(unsigned int epn, struct ep_regs *ep, struct usbd_endpoint_instance *endpoint)
+{
+        int bytes = 0;
+        struct usbd_urb *urb;
+        u32 cs;
+        u32 rds;
+        au_halt_dma(ep->outdma);
+        cs = au_readl(ep->cs);
+        rds = au_readl(ep->rds);
+        if (!endpoint) {
+                au_writel(USBDEV_FSTAT_FLUSH | USBDEV_FSTAT_UF | USBDEV_FSTAT_OF, ep->rds);
+                return;
+        }
+        if (!(urb = endpoint->rcv_urb)) {
+                TRACE_MSG2(PCD, "ISO OUT EPN - rcv_urb was NULL bytes: %d rds: %d", bytes, rds);
+                au_rcv_complete_epn_irq(endpoint, bytes, 1);
+                TRACE_MSG0(PCD, "ISO OUT setting rcv_error");
+        }
+        bytes = endpoint->wMaxPacketSize - au1xxx_get_dma_residue(ep->outdma);
+        rds = rds & USBDEV_FSTAT_FCNT_MASK;
+        au_fifo_read(ep, urb->buffer + urb->actual_length + bytes, rds);
+        bytes += rds;
+        au_writel(USBDEV_FSTAT_FLUSH | USBDEV_FSTAT_UF | USBDEV_FSTAT_OF, ep->rds);
+        au_rcv_complete_epn_irq(endpoint, bytes, 0);
+        au_start_out_iso(epn, endpoint, ep);
+}
+/* ********************************************************************************************* */
+/* au_tx_dma_done - TX DMA interrupt handler
+ */
+static void au_tx_dma_done(int irq, void *dev_id, struct pt_regs *regs)
+{
+        int epn = (int) dev_id;
+        struct usbd_endpoint_instance *endpoint = pcd_instance->bus->endpoint_array + epn;
+        struct ep_regs *ep = &ep_regs[epn];
+        int residue;
+        ocd_ops.interrupts++;
+        RETURN_IF (!epn);
+        au_halt_dma(ep->indma);
+        ep->last = 0;
+}
+
+/* au_int_req - usb interrupt handler
+ */
+static void au_int_req (int irq, void *dev_id, struct pt_regs *regs)
+{
+        u32 intstat;
+        struct ep_regs *ep;
+#if defined(MAX_INTR_LOOP_STATS)
+        u32 loopcount = 0;
+#endif
+#ifdef  RECORD_LATENCY
+        cp0_count = (read_c0_count(CP0_COUNT) - cp0_record) >> 9;
+        if (cp0_count < CP0_COUNTS) 
+                cp0_counts[cp0_count]++;
+#endif
+#ifdef  CHECK_LATENCY
+        u32 cp0_count = read_c0_count(CP0_COUNT);
+#endif
+        ocd_ops.interrupts++;
+        TRACE_MSG2(PCD, "INT - device: %d status: %d", pcd_instance->bus->device_state, pcd_instance->bus->status);
+#if 0
+        if (ocd_ops.interrupts > 2000) {
+                TRACE_MSG0(PCD, "UDC_INT call udc_disable_interrupts");
+                au_inten(0, "DISABLING");
+                return;
+        }
+#endif
+        while (( intstat = au_readl(USBD_INTSTAT) & au1x00_inten)) {    // read and reset interrupt status register
+
+                int epn;
+#if 1                
+                for (epn = 2; epn < 6; epn++) {
+                        CONTINUE_IF (!(intstat & (1 << epn)));
+#else
+                // NOT TESTED
+                u32 local_intstat = intstat;
+                TRACE_MSG2(PCD, "INTSTAT: %04x %04x", intstat, au_readl(USBD_INTEN));
+                while (local_intstat & 0x3f) {
+                        int epn = 31 - au_clz(local_intstat);
+                        local_intstat &= ~(1<<epn);
+#endif
+                        if (udc_saw_bus_activity) {
+                                udc_saw_bus_activity = 0;
+                                usbd_bus_event_handler_irq (pcd_instance->bus, DEVICE_BUS_ACTIVITY, 0);
+                                au_inten(0x0033/*|USBDEV_INT_SOF*/, "int SOF");                    
+                        }
+                        ep = &ep_regs[epn];
+                        switch(ep->eptype) {
+                        case USB_DIR_IN | USB_ENDPOINT_BULK:
+                        case USB_DIR_IN | USB_ENDPOINT_INTERRUPT:
+                                //TRACE_MSG2(PCD, "BULK IN %d %02x", epn, ep->eptype);
+                                au_in_bulk(epn, ep, pcd_instance->bus->endpoint_array + epn);
+                                break;
+                        case USB_DIR_IN | USB_ENDPOINT_ISOCHRONOUS:
+                                //TRACE_MSG2(PCD, "ISO IN %d %02x", epn, ep->eptype);
+                                au_in_iso(epn, ep, pcd_instance->bus->endpoint_array + epn);
+                                break;
+                        case USB_DIR_OUT | USB_ENDPOINT_BULK:
+                        case USB_DIR_OUT | USB_ENDPOINT_INTERRUPT:
+                                //TRACE_MSG2(PCD, "BULK OUT %d %02x", epn, ep->eptype);
+                                au_out_bulk(epn, ep, pcd_instance->bus->endpoint_array + epn);
+                                break;
+                        case USB_DIR_OUT | USB_ENDPOINT_ISOCHRONOUS:
+                                //TRACE_MSG2(PCD, "ISO OUT %d %04d", epn, au_readl(NUSBD_FRAMENUM));
+                                au_out_iso(epn, ep, pcd_instance->bus->endpoint_array + epn);
+                                break;
+                        }
+                }
+                au_writel(intstat, USBD_INTSTAT);               // Only clear the interrupt(s) AFTER servicing OUT
+                /* even though we disable the bulk-in interrupt (endpoint 2) prior to enabling
+                 * DMA we always see one additional interrupt that is a NAK on that endpoint.
+                 */
+                CONTINUE_IF(!(intstat & au1x00_inten));
+                /* handle control endpoint and suspend interrupt
+                 */
+                if (intstat & ( ((1 << 0) | (1 << 1) | (1 << 3) | (1 << 5) | USBDEV_INT_SOF))) {
+                        if (intstat & (1 << 0)) 
+                                au_out_ep0(pcd_instance->bus->endpoint_array + 0, &ep_regs[0]);
+                        if (intstat & (1 << 1)) 
+                                au_in_ep0(pcd_instance->bus->endpoint_array + 0, &ep_regs[0]);
+                        if (intstat & USBDEV_INT_SOF) 
+                                if (USBD_SUSPENDED == pcd_instance->bus->status) {
+                                        TRACE_MSG0(PCD, "SUS - ACTIVITY");
+                                        udc_saw_bus_activity++;
+                                }
+                }
+#if defined(MAX_INTR_LOOP_STATS)
+                loopcount += 1;                         // Gather stats on how many times this loop is performed. 
+#endif
+        }
+#if defined(MAX_INTR_LOOP_STATS)
+        interrupt_loop_stats[MIN(loopcount, MAX_INTR_LOOP_STATS)]++;
+#endif
+#if defined(CHECK_LATENCY)
+        TRACE_MSG1(PCD, "USB IRQ - %d", read_c0_count(CP0_COUNT) - cp0_count);
+#endif
+#if defined(RECORD_LATENCY)
+        cp0_record = read_c0_count(CP0_COUNT);
+#endif
+}
+
+/* au_int_sus -suspend interrupt handler
+ */
+static void au_int_sus (int irq, void *dev_id, struct pt_regs *regs)
+{
+        ocd_ops.interrupts++;
+        TRACE_MSG2(PCD, "SUS - INACTIVE device: %d status: %d", pcd_instance->bus->device_state, pcd_instance->bus->status);
+        switch(pcd_instance->bus->status) {
+        case USBD_OPENING:
+        case USBD_OK:
+                au_inten(0x0033, "suspend");
+                usbd_bus_event_handler_irq (pcd_instance->bus, DEVICE_BUS_INACTIVE, 0);
+                break;
+        default:
+                break;
+        }
+}
+/* ********************************************************************************************* */
+/* 
+ * au_connect - enable pullup resistor
+ * Turn on the USB connection by enabling the pullup resistor.
+ *
+ * au_disconnect - disable pullup resistor
+ * Turn off the USB connection by disabling the pullup resistor.
+ */
+
+#if defined(CONFIG_MIPS_FREEHAND_NATIVE)
+#warning "Old code for Freehand"
+void au_connect (struct pcd_instance *pcd)
+{
+        au1000gpio_set(GPIO01);
+}
+
+void au_disconnect (struct pcd_instance *pcd)
+{               
+        au1000gpio_clear(GPIO01);
+}
+#elif defined(CONFIG_AMX_MORDOR)
+void au_connect (struct pcd_instance *pcd)
+{
+        au_writel(0x00000008, SYS_OUTPUTSET);
+}
+
+void au_disconnect (struct pcd_instance *pcd)
+{               
+        au_writel(0x00000008, SYS_OUTPUTCLR);
+}
+#elif defined(CONFIG_MIPS_DB1550) || defined(CONFIG_MIPS_MTX2)
+void au_connect (struct pcd_instance *pcd)
+{
+	au_writel(au_readl(GPIO2_DIR) | 0x100, GPIO2_DIR);
+	au_writel(0x01000100, GPIO2_OUTPUT);
+	au_sync();
+}
+void au_disconnect (struct pcd_instance *pcd)
+{
+	au_writel(au_readl(GPIO2_DIR) | 0x100, GPIO2_DIR);
+	au_writel(0x01000000, GPIO2_OUTPUT);
+	au_sync();
+}
+#else
+#warning "Please define au_connect / au_disconnect"
+void au_connect (struct pcd_instance *pcd)
+{
+}
+void au_disconnect (struct pcd_instance *pcd)
+{
+}
+#endif
+
+#undef read_c0_COUNT
+#ifndef read_c0_count
+#define read_c0_count()  read_32bit_cp0_register(CP0_COUNT)
+#endif
+/* ********************************************************************************************* */
+/* au_ticks - get current ticks
+* */
+u64 au_ticks (void)
+{       
+        return read_c0_count();
+}       
+
+/* au_elapsed - return micro-seconds between two tick values
+ */
+u64 au_elapsed(u64 *t1, u64 *t2)
+{
+        u64 ticks = (*t1 > *t2) ? (*t1 - *t2) : (*t2 - *t1);
+        ticks = (u32)((u32) ticks / (u32) CONFIG_OTG_AU1X00_SCLOCK);
+        return ticks;
+}
+
+/* au_framenum - get current framenum
+ */             
+u16 au_framenum (void)
+{               
+        return au_readl(NUSBD_FRAMENUM);
+}               
+
+
+/* ********************************************************************************************* */
+/* au_start_endpoint_in - start transmit
+ */
+void au_start_endpoint_in(struct pcd_instance *pcd, struct usbd_endpoint_instance *endpoint)
+{
+        int epn = epl2p[endpoint->bEndpointAddress&0xf];
+        struct ep_regs *ep = &ep_regs[epn];
+        switch(endpoint->bmAttributes & USB_ENDPOINT_MASK) {
+        case USB_ENDPOINT_CONTROL:
+                au_in_ep0(endpoint, ep);
+                break;
+        case USB_ENDPOINT_BULK:
+        case USB_ENDPOINT_INTERRUPT:
+                au_start_in_bulk(epn, endpoint, ep);
+                break;
+        case USB_ENDPOINT_ISOCHRONOUS:
+                au_start_in_iso(epn, endpoint, ep);
+                break;
+        }
+        au_epn_interrupt_enable(epn);
+}
+
+/* au_start_endpoint_out - start receive
+ */
+void au_start_endpoint_out(struct pcd_instance *pcd, struct usbd_endpoint_instance *endpoint)
+{
+        int epn = epl2p[endpoint->bEndpointAddress&0xf];
+        struct ep_regs *ep = &ep_regs[epn];
+        switch(endpoint->bmAttributes & USB_ENDPOINT_MASK) {
+        case USB_ENDPOINT_CONTROL:
+                break;
+        case USB_ENDPOINT_BULK:
+        case USB_ENDPOINT_INTERRUPT:
+                au_start_out_bulk(epn, endpoint, ep);
+                au_epn_interrupt_enable(epn);
+                break;
+        case USB_ENDPOINT_ISOCHRONOUS:
+                au_start_out_iso(epn, endpoint, ep);
+                break;
+        }
+}
+
+/* 
+ * au_pcd_endpoint_halted() - is endpoint halted
+ */
+static int
+au_pcd_endpoint_halted (struct pcd_instance *pcd, struct usbd_endpoint_instance *endpoint)
+{
+int cs;
+        int epn = epl2p[endpoint->bEndpointAddress&0xf];
+        int dir = (endpoint->bEndpointAddress & 0x80) ? 1 : 0;
+        struct ep_regs *ep = &ep_regs[epn];
+
+        TRACE_MSG2(PCD, "epn: %02x dir: %x", epn, dir);
+        return ((cs = au_readl(ep->cs)) & USBDEV_CS_STALL) ? TRUE : FALSE;
+}
+
+/* au_pcd_halt_endpoint - halt endpoint
+ */     
+int au_pcd_halt_endpoint(struct pcd_instance *pcd, struct usbd_endpoint_instance *endpoint, int flag)
+{
+        int epn = epl2p[endpoint->bEndpointAddress&0xf];
+        int dir = (endpoint->bEndpointAddress & 0x80) ? 1 : 0;
+        struct ep_regs *ep = &ep_regs[epn];
+
+        TRACE_MSG3(PCD, "epn: %02x dir: %x flag: %d", epn, dir, flag);
+
+        au_writel(au_readl(ep->cs) & USBDEV_CS_STALL, ep->cs);
+        return 0;
+}       
+
+
+
+void au_cancel_in_irq(struct pcd_instance *pcd, struct usbd_urb *urb)
+{
+        int epn = epl2p[urb->endpoint->bEndpointAddress&0xf];
+        struct ep_regs *ep = &ep_regs[epn];
+        au_in_bulk(epn, ep, urb->endpoint);
+}
+
+void au_cancel_out_irq(struct pcd_instance *pcd, struct usbd_urb *urb)
+{
+        int epn = epl2p[urb->endpoint->bEndpointAddress&0xf];
+        struct ep_regs *ep = &ep_regs[epn];
+        au_out_bulk(epn, ep, urb->endpoint);
+}
+
+/* au_serial_init - set a serial number if available
+ */
+int au_serial_init (struct pcd_instance *pcd)
+{
+#if defined(CONFIG_MIPS_FREEHAND)
+        int length;
+        long data[16];          /* yeah a hack, but we KNOW it's 16 */
+        struct i2c_client *client;
+        char chData[16];
+        int i;
+
+        if (!(client = getFreeHandEepromClient())) {
+                printk(KERN_INFO"eeprom not ready when au_serial_init called\n");
+                return -EINVAL;
+        }
+        eeprom_contents(client, SENSORS_PROC_REAL_READ, EEPROM_SYSCTL1, &length, (long *)data);
+
+        /* serial number is first 9 longs. But each long is just an ASCII char
+         * convert this to a string and then extract a 4 byte value from it
+         */
+        for (i = 0; i < 9; chData[i] = (char)data[i], i++);     /* trunc, is ok */
+        chData[9] = 0;                                          /* terminate string */
+        printk(KERN_INFO"%s: %s\n", __FUNCTION__, chData);
+        pcd_instance->bus->serial_number_str = lstrdup(chData);
+        return 0;
+#else
+        return -EINVAL;
+#endif
+}
+
+
+/* au_start - start session
+ */
+void au_start (struct pcd_instance *pcd)
+{
+        au_inten(0x0033/*|USBDEV_INT_SOF*/, "START");                                // Only enable receive interrupts. 
+}
+
+/* au_stop - stop session
+ */
+void au_stop (struct pcd_instance *pcd)
+{
+        //au_inten(0, "STOP");
+}
+
+int au_assign_endpoint( u8 physicalEndpoint, int used[6], struct usbd_endpoint_map *endpoint_map, u8 bmAttributes,
+                u16 wMaxPacketSize, u16 transferSize) 
+{
+        struct ep_regs *ep = &ep_regs[physicalEndpoint]; 
+        RETURN_EINVAL_IF(used[physicalEndpoint]);
+        endpoint_map->bEndpointAddress[0] = epp2l[physicalEndpoint];
+        endpoint_map->physicalEndpoint[0] = physicalEndpoint;
+        endpoint_map->wMaxPacketSize[0] = wMaxPacketSize;
+        endpoint_map->transferSize[0] = transferSize;
+        endpoint_map->bmAttributes[0] = bmAttributes;
+        used[physicalEndpoint]++;
+        ep->eptype = bmAttributes & 0x83;
+        return 0;
+}
+
+int au_request_endpoints(struct pcd_instance *pcd, struct usbd_endpoint_map *endpoint_map_array, int endpointsRequested, 
+                struct usbd_endpoint_request *requestedEndpoints)
+{
+        struct usbd_device_description *device_description;
+        int i, j;
+        int used[UDC_MAX_ENDPOINTS];
+        memset(used, 0, sizeof(used));
+        for (j = 0, i = 0; i < endpointsRequested; i++, j++) {
+                struct usbd_endpoint_map *endpoint_map = endpoint_map_array + i;
+                u8 bmAttributes = requestedEndpoints[i].bmAttributes;
+                u16 transferSize = requestedEndpoints[i].fs_requestedTransferSize;
+                RETURN_EINVAL_UNLESS(j < UDC_MAX_ENDPOINTS);
+                switch(bmAttributes) {
+                case USB_DIR_OUT | USB_ENDPOINT_BULK:
+                case USB_DIR_OUT | USB_ENDPOINT_INTERRUPT:
+                case USB_DIR_OUT | USB_ENDPOINT_INTERRUPT | USB_ENDPOINT_OPT:
+                        CONTINUE_IF(!au_assign_endpoint(4, used, endpoint_map, bmAttributes, 0x40, transferSize));
+                        CONTINUE_IF(!au_assign_endpoint(5, used, endpoint_map, bmAttributes, 0x40, transferSize));
+                        break;
+                case USB_DIR_OUT | USB_ENDPOINT_ISOCHRONOUS:
+                        CONTINUE_IF(!au_assign_endpoint(4, used, endpoint_map, bmAttributes, transferSize, transferSize));
+                        CONTINUE_IF(!au_assign_endpoint(5, used, endpoint_map, bmAttributes, transferSize, transferSize));
+                        break;
+                case USB_DIR_IN | USB_ENDPOINT_BULK:
+                case USB_DIR_IN | USB_ENDPOINT_INTERRUPT:
+                case USB_DIR_IN | USB_ENDPOINT_INTERRUPT | USB_ENDPOINT_OPT:
+                        CONTINUE_IF(!au_assign_endpoint(2, used, endpoint_map, bmAttributes, 0x40, transferSize));
+                        CONTINUE_IF(!au_assign_endpoint(3, used, endpoint_map, bmAttributes, 0x40, transferSize));
+                        break;
+                case USB_DIR_IN | USB_ENDPOINT_ISOCHRONOUS:
+                        CONTINUE_IF(!au_assign_endpoint(2, used, endpoint_map, bmAttributes, transferSize, transferSize));
+                        CONTINUE_IF(!au_assign_endpoint(3, used, endpoint_map, bmAttributes, transferSize, transferSize));
+                        break;
+                }
+                CONTINUE_IF(bmAttributes & USB_ENDPOINT_OPT);
+                return -EINVAL;
+        }
+        return 0;
+}
+
+struct usbd_endpoint_map *au_endpoint_map_array;
+int au_endpointsRequested;
+
+int au_set_endpoints2(struct pcd_instance *pcd, int endpointsRequested, struct usbd_endpoint_map *endpoint_map_array);
+int au_set_endpoints(struct pcd_instance *pcd, int endpointsRequested, struct usbd_endpoint_map *endpoint_map_array)
+{
+	au_endpointsRequested = endpointsRequested;
+	au_endpoint_map_array = endpoint_map_array;
+	return au_set_endpoints2(pcd, au_endpointsRequested, au_endpoint_map_array);
+}
+
+int au_set_endpoints2(struct pcd_instance *pcd, int endpointsRequested, struct usbd_endpoint_map *endpoint_map_array)
+{
+        int i, j;
+        u8 config[25];
+        u8 *cp;
+        memcpy(config, au1x00_config_bulk, sizeof(config));
+        for (i = 0; i < endpointsRequested; i++) {
+                struct usbd_endpoint_map *endpoint_map = endpoint_map_array + i;
+                int epreq = endpoint_map->bmAttributes[0];
+                int eptype = epreq & USB_ENDPOINT_MASK;
+                int epdir = epreq & USB_ENDPOINT_DIR_MASK ? 0x8 : 0;
+                int epsize = endpoint_map->wMaxPacketSize[0];
+                int epaddr = epp2l[endpoint_map->physicalEndpoint[0]];
+                CONTINUE_IF(!endpoint_map->physicalEndpoint[0] || (endpoint_map->physicalEndpoint[0] > 5));
+                cp = config + ((endpoint_map->physicalEndpoint[0] - 1) * 5);
+                cp[0] = (epaddr & 0xf) << 4 | 0x4;
+                cp[1] = (eptype << 4) | epdir | (epsize & 0x380) >> 7;
+                cp[2] = (epsize & 0x7F) << 1;
+        } 
+        TRACE_MSG0(PCD, "Disable interrupts");
+        au_inten(0, "SET ENDPOINTS");                                                            // disable interrupts
+        au_writel(0x0002, USBD_ENABLE);                                         // reset controller
+        udelay(100);
+        au_writel(0x0003, USBD_ENABLE);                                         // enable controller
+        udelay(100);
+        for (cp = config, i = 0; i < 25; i++, au_writel(*cp++, USBD_CONFIG));   // feed the config into the UDC
+        return 0;
+}
+
+/* au_loc_conn - used to enable or disable peripheral connecting to bus
+ *
+ */
+void au_loc_conn(struct otg_instance *otg, u8 flag)
+{
+        struct pcd_instance *pcd = (struct pcd_instance *)otg->pcd;
+        TRACE_MSG0(PCD, "--");
+        TRACE_MSG1(PCD, "PCD: %x", (int)pcd);
+        switch (flag) {
+        case SET:
+                TRACE_MSG0(PCD, "OUTPUT: OCD_LOC_CONN_SET - Enable DP PULLUP");
+		au_set_endpoints2(pcd, au_endpointsRequested, au_endpoint_map_array);
+		au_inten(0x0033/*|USBDEV_INT_SOF*/, "START");                    // Only enable receive interrupts. 
+                au_connect(pcd);
+                break;
+
+        case RESET:
+                TRACE_MSG0(PCD, "OUTPUT: OCD_LOC_CONN_RESET - Disable DP PULLUP");
+                au_disconnect(pcd);
+		au_inten(0, "RESET ENDPOINTS");                                         // disable interrupts
+		au_writel(0x0000, USBD_ENABLE);                                         // reset controller
+                break;
+        }
+}
+
+
+/* ********************************************************************************************* */
+
+
+static u32 request_dma(int ep, int src, int dest, char *str, void dma_done(int , void *, struct pt_regs *))
+{
+        u32 dma;
+        
+        chan_tab_t              *ctp;
+        volatile au1x_dma_chan_t *cp;
+        au1x_ddma_desc_t        *dp;
+
+
+	if ((dma = au1xxx_dbdma_chan_alloc(src, dest, dma_done, (void *)ep)) < 0) {
+                printk(KERN_INFO"request_io[%d] dma: %x src: %d dest: %d %s FAILED\n", ep, dma, src, dest, str);
+                return -1;
+        }
+
+        if(au1xxx_dbdma_ring_alloc(dma, 1) == 0) {
+                printk(KERN_INFO"request_io[%d] Ring Buffer Allocation for  dma: %x src: %d dest: %d %s FAILED\n", 
+                                ep, dma, src, dest, str);
+                return -1;
+	}
+
+        ctp = *((chan_tab_t **)dma);
+        cp = ctp->chan_ptr;;
+        dp = ctp->put_ptr; 
+        dp->dscr_cmd0 &= ~DSCR_CMD0_V;
+
+        TRACE_MSG8(PCD, "%08x %08x cmd: %08x %04x src: %08x %04x dst: %08x %08x DBDMA", 
+                        dma, dp, dp->dscr_cmd0, dp->dscr_cmd1,
+                        dp->dscr_source0, dp->dscr_source1, dp->dscr_dest0, dp->dscr_dest1);
+
+#if 0
+        // XXX copy static components from au_start_out
+        // transmit - mem to usb
+        if (src == DBDMA_MEM_CHAN) {
+                TRACE_MSG8(PCD, "%08x %08x cmd: %08x %04x src: %08x %04x dst: %08x %08x MEM2USB", 
+                                dma, dp, dp->dscr_cmd0, dp->dscr_cmd1,
+                                dp->dscr_source0, dp->dscr_source1, dp->dscr_dest0, dp->dscr_dest1);
+        }
+#endif
+#if 0
+        // XXX copy static components from au_start_in
+        // receive - usb to mem
+        if (dest == DBDMA_MEM_CHAN) {
+                TRACE_MSG8(PCD, "%08x %08x cmd: %08x %04x src: %08x %08x dst: %08x %08x USB2MEM", 
+                                dma, dp, dp->dscr_cmd0, dp->dscr_cmd1,
+                                dp->dscr_source0, dp->dscr_source1, dp->dscr_dest0, dp->dscr_dest1);
+        }
+#endif
+        TRACE_MSG5(PCD, "request_io[%d] dma: %x src: %d dest: %d %s SUCCEDED", ep, dma, src, dest, str);
+        return dma;
+}
+
+/* au_mod_init 
+ */
+int au_mod_init (void)
+{
+        int dev = request_irq (AU1000_USB_DEV_REQ_INT, au_int_req, SA_INTERRUPT, UDC_NAME "UDC Req", NULL);
+        int sus = request_irq (AU1000_USB_DEV_SUS_INT, au_int_sus, SA_INTERRUPT, UDC_NAME "UDC Sus", NULL);
+        u32 cp0_prid = read_c0_prid();
+        int i;
+
+
+        if (dev || sus) {                                               // check if either request irq failed
+                if (!dev) free_irq (AU1000_USB_DEV_REQ_INT, NULL);      // free irqs that might have
+                if (!sus) free_irq (AU1000_USB_DEV_SUS_INT, NULL);      // been successfully allocated
+                return -EINVAL;
+        }
+        for (i = 0; i < 6; i++) {
+                ep_regs[i].indma = ep_regs[i].tx_id ? 
+                        request_dma(i, DBDMA_MEM_CHAN, ep_regs[i].tx_id, ep_regs[i].tx_str, au_tx_dma_done) : -1;
+                ep_regs[i].outdma = ep_regs[i].rx_id ? 
+                        request_dma(i, ep_regs[i].rx_id, DBDMA_MEM_CHAN, ep_regs[i].rx_str, NULL) : -1;
+        }
+        switch (cp0_prid & CP0_PRID_SOC_MASK) {
+        case CP0_PRID_AU1000:
+        case CP0_PRID_AU1100:
+        case CP0_PRID_AU1500:
+                return -EINVAL;
+        case CP0_PRID_AU1550:
+                TRACE_MSG1(PCD, "AU1550 cp0_prid: %08x\n", cp0_prid);
+                printk(KERN_INFO"%s: AU1550 cp0_prid: %08x\n", __FUNCTION__, cp0_prid);
+                break;
+        default:
+                printk(KERN_INFO"%s: UNKNOWN CPU cp0_prid: %08x UNKNOWN UDC\n", __FUNCTION__, cp0_prid);
+                break;
+        }
+        return 0;
+}
+
+/* au_mod_exit  
+ */
+void au_mod_exit (void)
+{
+        int j;
+        au_writel(0x0000, USBD_ENABLE);
+        for (j = 0; j < 6; j++) {
+                struct ep_regs *ep = &ep_regs[j]; 
+                if (ep->indma != -1) {
+                        au1xxx_dbdma_chan_free(ep->indma);
+                        ep->indma = -1;
+                }
+                if (ep->outdma != -1) {
+                        au1xxx_dbdma_chan_free(ep->outdma);
+                        ep->outdma = -1;
+                }
+        }
+        free_irq (AU1000_USB_DEV_REQ_INT, NULL);
+        free_irq (AU1000_USB_DEV_SUS_INT, NULL);
+#if defined(MAX_INTR_LOOP_STATS)
+        {
+                u32 lc;
+                for (lc = 0; lc <= MAX_INTR_LOOP_STATS; lc++) 
+                        if (interrupt_loop_stats[lc]) 
+                                printk(KERN_ERR "%s: interrupt loopcount[%02u] %9u\n", __FUNCTION__, lc,interrupt_loop_stats[lc]);
+                printk(KERN_INFO"%s: halt_dma_expired: %d\n", __FUNCTION__, au_halt_dma_expired);
+        }
+#endif
+#ifdef RECORD_LATENCY
+        {
+                int i;
+                for (i = 0; i < CP0_COUNTS; i++) 
+                        if (cp0_counts[i]) 
+                                printk(KERN_INFO"%s: cp0_counts[%d] %d\n", __FUNCTION__, i, cp0_counts[i]);
+        }
+#endif
+}
+
+/* ********************************************************************************************* */
+struct usbd_pcd_ops usbd_pcd_ops = {
+        max_endpoints:  UDC_MAX_ENDPOINTS,
+        ep0_packetsize:  EP0_PACKETSIZE,
+        name:  UDC_NAME,
+        start: au_start,
+        stop: au_stop,
+        start_endpoint_in: au_start_endpoint_in,
+        start_endpoint_out: au_start_endpoint_out,
+        request_endpoints: au_request_endpoints,
+        set_endpoints: au_set_endpoints,
+        cancel_in_irq: au_cancel_in_irq,
+        cancel_out_irq: au_cancel_out_irq,
+        serial_init: au_serial_init,
+        halt_endpoint: au_pcd_halt_endpoint,
+        endpoint_halted: au_pcd_endpoint_halted,
+};
+
+struct pcd_ops pcd_ops = {
+        pcd_en_func: pcd_en_func,
+        mod_init: au_mod_init,
+        mod_exit: au_mod_exit,
+        framenum: au_framenum,
+};
+
+struct ocd_ops ocd_ops = {
+	#if defined(CONFIG_OTG_TR_AUTO)
+        capabilities: OCD_CAPABILITIES_TR | OCD_CAPABILITIES_AUTO,
+	#else 
+        capabilities: OCD_CAPABILITIES_TR,
+        #endif
+        ticks: au_ticks,
+        elapsed: au_elapsed,
+};
+
+struct tcd_ops tcd_ops = {
+	dp_pullup_func: au_loc_conn,
+};
+
diff -uNr linux/drivers/no-otg/ocd/au1x00/au1x00.c linux/drivers/otg/ocd/au1x00/au1x00.c
--- linux/drivers/no-otg/ocd/au1x00/au1x00.c	1970-01-01 01:00:00.000000000 +0100
+++ linux/drivers/otg/ocd/au1x00/au1x00.c	2006-09-01 21:41:31.000000000 +0200
@@ -0,0 +1,1293 @@
+/*
+ * otg/ocd/au1x00/au1x00.c -- USB Device Controller driver. 
+ *
+ *      Copyright (c) 2004 Belcarra
+ *
+ * By: 
+ *      Stuart Lynne <sl@belcarra.com>, 
+ *      Tom Rushworth <tbr@belcarra.com>, 
+ *
+ */
+/*!
+ * @file otg/ocd/au1x00/au1x00.c
+ * @brief AU1X00 Header
+ *
+ * This file contains the private defines and structures for the AU1X00  
+ * Drivers. 
+ *
+ * @ingroup AU1X00
+ */
+
+
+#include <otg/pcd-include.h>
+#include "au1x00.h"
+
+#include <asm/io.h>
+#include <asm/au1000.h>
+#include <asm/au1000_dma.h>
+
+#if defined(CONFIG_SOC_AU1550)
+#define DBDMA 
+#endif
+#ifdef DBDMA
+#warning "Using AU1XXX_DBMDA"
+#include <asm/au1xxx_dbdma.h>
+#else /* DBDMA */
+#warning "Using AU1X00_MDA"
+#endif /* DBDMA */
+
+#include <asm/mipsregs.h>
+
+
+#ifdef CONFIG_MIPS_FREEHAND
+#include <linux/i2c.h>
+#include <linux/sensors.h>      /* for reading serial number */
+#endif
+
+
+#undef CHECK_LATENCY
+#undef RECORD_LATENCY
+#undef MAX_INTR_LOOP_STATS   //10
+#if defined(MAX_INTR_LOOP_STATS)
+static u32 interrupt_loop_stats[MAX_INTR_LOOP_STATS+1];
+#endif
+#ifdef  RECORD_LATENCY
+#define CP0_COUNTS 50
+u32 cp0_counts[CP0_COUNTS];
+u32 cp0_record;
+#endif
+u32 cp0_count;
+unsigned int udc_saw_bus_activity;
+int au_halt_dma_expired;
+u32 au1x00_new_silicon;
+u32 au1x00_inten;
+
+u8 au1x00_config_bulk[25] = {
+        0x04, (USB_ENDPOINT_CONTROL << 4) | (EP0_PACKETSIZE & 0x380) >> 7,                            // 1
+        (EP0_PACKETSIZE & 0x7F) << 1, 0x00, 0x00,
+        0x24, (USB_ENDPOINT_BULK << 4) | USB_DIR_IN | (MAX_EPN_PACKET_SIZE & 0x380) >> 7,             // 6
+        (MAX_EPN_PACKET_SIZE & 0x7F) << 1, 0x00, 0x02, 
+        0x34, (USB_ENDPOINT_BULK << 4) | USB_DIR_IN | (MAX_EPN_PACKET_SIZE & 0x380) >> 7,             // 11
+        (MAX_EPN_PACKET_SIZE & 0x7F) << 1, 0x00, 0x03, 
+        0x44, (USB_ENDPOINT_BULK << 4) | USB_DIR_OUT | (MAX_EPN_PACKET_SIZE & 0x380) >> 7,            // 16
+        (MAX_EPN_PACKET_SIZE & 0x7F) << 1, 0x00, 0x04,
+        0x54, (USB_ENDPOINT_BULK << 4) | USB_DIR_OUT | (MAX_EPN_PACKET_SIZE & 0x380) >> 7,            // 21
+        (MAX_EPN_PACKET_SIZE & 0x7F) << 1, 0x00, 0x05,
+};
+
+/* ********************************************************************************************* */
+/* Note - The endpoints names are confusing. To simplify mapping of the interrupt request lines
+ * we use a numbering of the physical endpoints of 0-5, which also matches the fifo numbering
+ * (see the config block).
+ *
+ *   Physical  FIFO    Name    Direction   Logical
+ *      0       0       EP0     OUT         0
+ *      1       1       EP0     IN          0
+ *      2       2       EP1     IN          81
+ *      3       3       EP2     IN          82
+ *      4       4       EP3     OUT         3
+ *      5       5       EP4     OUT         4
+ *
+ * The ep_regs array maps a physical endpoint number to the registers required to access the udc
+ * for that endpoint. Note that ep0 is always accessed via 0.  The epl2p array maps the logical
+ * addresses back to the physical number.  The epp2l array maps the physical number to the
+ * logical endpoint address
+ */
+#ifdef DBDMA
+struct ep_regs ep_regs[6] = {
+
+        { rd: USBD_EP0RD, rds: USBD_EP0RDSTAT, /*rx_id: DMA_ID_USBDEV_EP0_RX,*/ cs: USBD_EP0CS, rx_str: "EP0 OUT RD",
+          wr: USBD_EP0WR, wrs: USBD_EP0WRSTAT, tx_id: DSCR_CMD0_USBDEV_TX0,                 tx_str: "EP0 IN WR",},
+
+        { rds: 0, wrs: 0, indma: -1, outdma: -1, },
+        { wr: NUSBD_EP1WR, wrs: NUSBD_EP1WRSTAT, tx_id: DSCR_CMD0_USBDEV_TX1, cs: NUSBD_EP1CS, tx_str: "EP1 IN WR",},
+        { wr: NUSBD_EP2WR, wrs: NUSBD_EP2WRSTAT, tx_id: DSCR_CMD0_USBDEV_TX2, cs: NUSBD_EP2CS, tx_str: "EP2 IN WR",},
+
+        { rd: NUSBD_EP3RD, rds: NUSBD_EP3RDSTAT, rx_id: DSCR_CMD0_USBDEV_RX3, cs: NUSBD_EP3CS, rx_str: "EP3 OUT RD",},
+        { rd: NUSBD_EP4RD, rds: NUSBD_EP4RDSTAT, rx_id: DSCR_CMD0_USBDEV_RX4, cs: NUSBD_EP4CS, rx_str: "EP4 OUT RD",},
+};
+#else /*DBMA */
+struct ep_regs ep_regs[6] = {
+        { rd: USBD_EP0RD, rds: USBD_EP0RDSTAT, /*rx_id: DMA_ID_USBDEV_EP0_RX,*/ cs: USBD_EP0CS, rx_str: "EP0 OUT RD", 
+          wr: USBD_EP0WR, wrs: USBD_EP0WRSTAT, tx_id: DMA_ID_USBDEV_EP0_TX,                 tx_str: "EP0 IN WR",},  
+        { rds: 0, wrs: 0, indma: -1, outdma: -1, },
+        { wr: NUSBD_EP1WR, wrs: NUSBD_EP1WRSTAT, tx_id: NDMA_ID_USBDEV_EP1_TX, cs: NUSBD_EP1CS, tx_str: "EP1 IN WR",}, 
+        { wr: NUSBD_EP2WR, wrs: NUSBD_EP2WRSTAT, tx_id: NDMA_ID_USBDEV_EP2_TX, cs: NUSBD_EP2CS, tx_str: "EP2 IN WR",}, 
+
+        { rd: NUSBD_EP3RD, rds: NUSBD_EP3RDSTAT, rx_id: NDMA_ID_USBDEV_EP3_RX, cs: NUSBD_EP3CS, rx_str: "EP3 OUT RD",},
+        { rd: NUSBD_EP4RD, rds: NUSBD_EP4RDSTAT, rx_id: NDMA_ID_USBDEV_EP4_RX, cs: NUSBD_EP4CS, rx_str: "EP4 OUT RD",},
+};
+#endif /*DBMA */
+u8 epl2p[6] = { 0x00, 0x02, 0x03, 0x04, 0x05, 0x00, };        // map logical to physical
+u8 epp2l[6] = { 0x00, 0x00, 0x81, 0x82, 0x03, 0x04, };        // map physical to logical
+
+static __inline__ void au_fifo_read(struct ep_regs *ep, unsigned char * cp, int bytes)
+{
+        u32 rd = ep->rd;
+        for (; bytes--; *cp++ = au_readl(rd));
+}
+
+static __inline__ void au_fifo_write(int ep, unsigned char * cp, int bytes)
+{
+        u32 wr = ep_regs[ep].wr;
+        for (; bytes--; au_writel(*cp++, wr));
+        ep_regs[ep].last = bytes;
+}
+
+void __inline__ au_inten(u32 inten)
+{
+        au1x00_inten = inten;
+        au_writel(au1x00_inten, USBD_INTEN);
+}
+
+void __inline__ au_epn_interrupt_enable(int epn)
+{
+        au_inten(au1x00_inten | (1 << epn));
+}
+
+void __inline__ au_epn_interrupt_disable(int epn)
+{
+        au_inten(au1x00_inten & ~(1 << epn));
+        au_writel((1 << epn) , USBD_INTSTAT);
+}
+
+static void __inline__ send_zlp(unsigned char epn)
+{
+        au_writel(USBDEV_FSTAT_FLUSH | USBDEV_FSTAT_UF | USBDEV_FSTAT_OF, ep_regs[epn].wrs);
+        au_writel(0 << 1, ep_regs[epn].cs);
+        au_writel(0, ep_regs[epn].wr);
+}
+/* ********************************************************************************************* */
+#define AU_DMA_HALT_POLL 0x1000
+#ifdef DBDMA
+static void __inline__ au_halt_dma(int chan)
+{
+	au1xxx_dbdma_stop(chan);  
+        au_halt_dma_expired++;
+}
+
+static int __inline__ au_get_dma_residue(int chan)
+{
+	return au1xxx_get_dma_residue(chan);
+}
+
+static void __inline__ au_start_dma_in(int indma, __u8 *bp, int len)
+{
+        //printk("\n----------------------STARTING DMA IN dma:%X buffer:%p len:%d---------------\n", indma, bp, len);
+        //		au1xxx_dbdma_stop(indma);
+        au1xxx_dbdma_put_source(indma, bp, len);
+        au1xxx_dbdma_start(indma);
+}
+
+static void __inline__ au_start_dma_out(unsigned int outdma, __u8 *bp, int packetSize)
+{
+        //printk("\n----------------------STARTING DMA OUT dma:%X buffer:%p len:%d---------------\n", outdma, bp, packetSize);
+        au1xxx_dbdma_put_dest(outdma, bp, packetSize);
+        au1xxx_dbdma_start(outdma);
+}
+
+static u32 __inline__ au_get_dma_chan(u32 dma)
+{
+        return dma;
+}
+#else /* DBDMA */
+static void __inline__ au_halt_dma(struct dma_chan *chan)
+{
+        int i;
+        au_writel(DMA_GO, chan->io + DMA_MODE_CLEAR);
+        for (i = 0; i < AU_DMA_HALT_POLL; i++) 
+                RETURN_IF (au_readl(chan->io + DMA_MODE_READ) & DMA_HALT);
+        au_halt_dma_expired++;
+}
+
+static int __inline__ au_get_dma_residue(struct dma_chan *chan)
+{
+        int curBufCntReg = (au_readl(chan->io + DMA_MODE_READ) & DMA_AB) ? DMA_BUFFER1_COUNT : DMA_BUFFER0_COUNT;
+        return au_readl(chan->io + curBufCntReg) & DMA_COUNT_MASK;
+}
+
+static void __inline__ au_start_dma(struct dma_chan *chan, u8 *bp, int len)
+{
+        if (au_readl(chan->io + DMA_MODE_READ) & DMA_AB) {
+                au_writel(DMA_D1, chan->io + DMA_MODE_CLEAR);
+                au_writel(len & DMA_COUNT_MASK, chan->io + DMA_BUFFER1_COUNT);
+                au_writel(0, chan->io + DMA_BUFFER0_COUNT);
+                au_writel(virt_to_phys(bp), chan->io + DMA_BUFFER1_START);
+                au_writel(DMA_BE1, chan->io + DMA_MODE_SET);
+        }
+        else {
+                au_writel(DMA_D0, chan->io + DMA_MODE_CLEAR);
+                au_writel(len & DMA_COUNT_MASK, chan->io + DMA_BUFFER0_COUNT);
+                au_writel(0, chan->io + DMA_BUFFER1_COUNT);
+                au_writel(virt_to_phys(bp), chan->io + DMA_BUFFER0_START);
+                au_writel(DMA_BE0, chan->io + DMA_MODE_SET);
+        }
+        au_writel(DMA_GO, chan->io + DMA_MODE_SET);
+}
+
+static void __inline__ au_start_dma_in(int indma, u8 *bp, int len)
+{
+        au_start_dma(get_dma_chan(indma), bp, len);
+}
+
+static void __inline__ au_start_dma_out(int outdma, u8 *bp, int wMaxPacketSize)
+{
+        dma_cache_inv((unsigned long) bp, wMaxPacketSize);
+        au_start_dma(get_dma_chan(outdma), bp, wMaxPacketSize);
+}
+
+static __inline__ struct dma_chan * au_get_dma_chan(u32 dma)
+{
+        return get_dma_chan(dma);
+}
+#endif /* DBDMA */
+static struct usbd_urb *au_rcv_complete_irq(struct usbd_endpoint_instance *endpoint, int len, int urb_bad)
+{
+        return pcd_rcv_complete_irq(endpoint, len, urb_bad);
+}
+/* ********************************************************************************************* */
+/* au_start_in_ep0 - start transmit
+ */
+static void au_start_in_ep0 (struct usbd_endpoint_instance *endpoint, struct ep_regs *ep)
+{
+	struct usbd_urb *urb = endpoint->tx_urb;
+        int last = ep->last = endpoint->last = MIN (urb->actual_length - endpoint->sent, endpoint->wMaxPacketSize);
+        TRACE_MSG2(PCD, "START IN EP0 SENT: %d SENDING: %d", endpoint->sent, last);
+        RETURN_IF ((urb->actual_length - endpoint->sent) <= 0);
+        au_writel(last << 1, ep->cs); // XXX
+        au_fifo_write(0, urb->buffer + endpoint->sent, last);
+        endpoint->last = last;
+}
+
+/* au_in_ep0 - called to service an endpoint zero IN interrupt, data sent
+ */
+static void au_in_ep0(struct usbd_endpoint_instance *endpoint, struct ep_regs *ep)
+{
+        u32 cs;
+        struct usbd_urb *tx_urb;
+        int last;
+        TRACE_MSG1(PCD, "EP0 IN: tx_urb: %p", (int)endpoint->tx_urb);
+        if ((cs = au_readl(ep->cs)) & USBDEV_CS_STALL) {                // clear stall if present
+                TRACE_MSG1(PCD, "CLEAR STALL %d", 0);
+                cs &= ~USBDEV_CS_STALL;
+                au_writel(cs, ep->cs);
+                return;
+        }
+        if (!(tx_urb = pcd_tx_complete_irq(endpoint, 0))) {               // wait for setup if no more data
+                endpoint->state = WAIT_FOR_SETUP;
+                return;
+        }
+        TRACE_MSG4(PCD, "EP0 IN actual: %d last: %d sent: %d flags: %x", endpoint->tx_urb->actual_length, 
+                        endpoint->last, endpoint->sent, endpoint->tx_urb->flags);
+        if (pcd_tx_sendzlp(endpoint)) {                          // check if tx_urb we have is finished
+                TRACE_MSG0(PCD, "EP0 IN BULK - sending ZLP");
+                tx_urb->flags &= ~USBD_URB_SENDZLP;
+                send_zlp(0);
+                pcd_tx_complete_irq(endpoint, 0);
+                return;
+        }
+        if (tx_urb->actual_length > endpoint->sent) {
+                if ((tx_urb->actual_length - endpoint->sent) < endpoint->wMaxPacketSize) 
+                        TRACE_MSG1(PCD, "EP0 IN starting short packet %d", tx_urb->actual_length - endpoint->sent);
+                else 
+                        TRACE_MSG1(PCD, "EP0 IN LEFT TO SEND %d", tx_urb->actual_length - endpoint->sent);
+                au_start_in_ep0(endpoint, ep);
+        }
+}
+
+/* au_out_ep0 - called to service an endpoint zero OUT interrupt, data received
+ */
+static void au_out_ep0(struct usbd_endpoint_instance *endpoint, struct ep_regs *ep)
+{
+        struct usbd_device_request request;
+        int i;
+        u32 cs;
+        u32 bytes;
+        TRACE_MSG0(PCD, "EP0 OUT");
+        cs = au_readl(ep->cs);                                  // check if host aborted transfer and flush the write fifo
+        bytes = au_readl(ep->rds) & USBDEV_FSTAT_FCNT_MASK;
+        if (endpoint->state == DATA_STATE_RECV) {
+                struct usbd_urb *rcv_urb = pcd_rcv_next_irq(endpoint);
+                TRACE_MSG1(PCD, "EP0 OUT: RECV: rcv_urb: %x", (int) rcv_urb);
+                if (rcv_urb) {
+                        au_fifo_read(ep, rcv_urb->buffer + rcv_urb->actual_length, bytes);
+                        if (au_rcv_complete_irq(endpoint, bytes, 0)) 
+                                return;
+                        au_writel(USBDEV_FSTAT_FLUSH | USBDEV_FSTAT_UF | USBDEV_FSTAT_OF, ep->rds);
+                        endpoint->state = WAIT_FOR_SETUP;
+                        send_zlp(0);
+                        return;
+                }
+                endpoint->state = WAIT_FOR_SETUP;
+        }
+        pcd_tx_cancelled_irq(endpoint);
+        pcd_rcv_cancelled_irq(endpoint);
+        au_fifo_read(ep, (u8 *)&request, bytes);
+        au_writel(USBDEV_FSTAT_FLUSH | USBDEV_FSTAT_UF | USBDEV_FSTAT_OF, ep->wrs);
+        if (bytes != 8) {
+                TRACE_MSG1(PCD, "ERROR SETUP SET not eight bytes: %d", bytes);
+                au_writel(USBDEV_FSTAT_FLUSH | USBDEV_FSTAT_UF | USBDEV_FSTAT_OF, ep->wrs);
+                return;
+        }
+        TRACE_MSG4(PCD, "SETUP bmRequestType: %02x bRequest %02x state: %d status: %d", request.bmRequestType, 
+                        request.bRequest, pcd_instance->bus->device_state, pcd_instance->bus->status);
+        switch (request.bRequest) {                     // we need to simply ignore any of these
+        case USB_REQ_SET_ADDRESS:                       // Fake a bus reset IFF not state default and then process normally
+                BREAK_IF (pcd_instance->bus->device_state == STATE_DEFAULT);
+                udc_saw_bus_activity = 0;
+                usbd_bus_event_handler_irq (pcd_instance->bus, DEVICE_RESET, 0);
+                usbd_bus_event_handler_irq (pcd_instance->bus, DEVICE_ADDRESS_ASSIGNED, 0);
+                break;
+        case USB_REQ_GET_DESCRIPTOR:                    // Fake a bus reset IFF suspended and then process normally
+                BREAK_IF (STATE_SUSPENDED != pcd_instance->bus->device_state);
+                udc_saw_bus_activity = 0;
+                usbd_bus_event_handler_irq (pcd_instance->bus, DEVICE_RESET, 0);
+                usbd_bus_event_handler_irq (pcd_instance->bus, DEVICE_ADDRESS_ASSIGNED, 0);
+                break;
+        }
+        if (pcd_recv_setup_irq(pcd_instance, &request)) {
+                TRACE_MSG1(PCD, "ep0 STALL %d", cs);
+                au_writel(USBDEV_CS_STALL, USBD_EP0CS);
+                return;
+        }
+        if (((request.bmRequestType & USB_REQ_DIRECTION_MASK) == USB_REQ_HOST2DEVICE) && le16_to_cpu (request.wLength)) {
+                TRACE_MSG1(PCD, "ep0 Class H2D request %04x", le16_to_cpu(request.wLength));
+                endpoint->state = DATA_STATE_RECV;
+                return;
+        }                               
+        if ((request.bmRequestType & USB_REQ_DIRECTION_MASK) == USB_REQ_HOST2DEVICE) {
+                TRACE_MSG1(PCD, "ep0 Class H2D request %04x", le16_to_cpu(request.wLength));
+                if ((request.bmRequestType & ~USB_REQ_DIRECTION_MASK)) {
+                        TRACE_MSG1(PCD, "ep0 Class or Vendor, send ZLP %d", cs);
+                        send_zlp(0);
+                        return;
+                }
+        }
+        TRACE_MSG1(PCD, "ep0 Class D2H request %04x", le16_to_cpu(request.wLength));
+}
+/* ********************************************************************************************* */
+/* au_start_in_bulk - start transmit
+ * The au1x00 will start to send when the first byte is loaded into the FIFO, either by
+ * DMA or PIO. The packetsize must be set first.
+ */
+static void au_start_in_bulk (unsigned int epn, struct usbd_endpoint_instance *endpoint, struct ep_regs *ep)
+{
+        struct usbd_urb *urb = endpoint->tx_urb;
+        unsigned char *bp = urb->buffer + endpoint->sent;
+        int last;
+        TRACE_MSG1(PCD, "START IN BULK %d", epn);
+        RETURN_IF (!urb || (( (urb->actual_length - endpoint->sent) == 0) && !(urb->flags & USBD_URB_SENDZLP)));
+        last = ep->last = endpoint->last = MIN (urb->actual_length - endpoint->sent, endpoint->wMaxPacketSize);
+        TRACE_MSG2(PCD, "START IN BULK sent: %d last:%d", endpoint->sent, last);
+        au_writel(USBDEV_FSTAT_FLUSH | USBDEV_FSTAT_UF | USBDEV_FSTAT_OF, ep->wrs); // XXX
+        if (!last) {
+                if (endpoint->tx_urb->flags & USBD_URB_SENDZLP) {
+                        TRACE_MSG0(PCD, "START IN BULK - zending ZLP");
+                        send_zlp(epn);
+                        endpoint->tx_urb->flags &= ~USBD_URB_SENDZLP;
+                }
+                return;
+        }
+        else if (au1x00_new_silicon  && (8 >= last)) {
+                au_writel(last << 1, ep->cs);
+                au_fifo_write(epn, bp, last);
+                return;
+        }
+        if (!au1x00_new_silicon) 
+                au_epn_interrupt_disable(epn);
+        dma_cache_wback_inv((unsigned long) bp, last); 
+        au_writel(last << 1, ep->cs);
+        au_start_dma_in(ep->indma, bp, last);
+}
+
+static void au_in_bulk(unsigned int epn, struct ep_regs *ep, struct usbd_endpoint_instance *endpoint)
+{
+        struct usbd_urb *tx_urb;
+        int rc = 0;
+        u32 cs = au_readl(ep->cs);
+        u32 wrs = au_readl(ep->wrs);
+        TRACE_MSG2(PCD, "BULK IN EPN - cs: %x wrs: %x", cs, wrs);
+        if (!au1x00_new_silicon) 
+                if (epn && (ep->last > 8)) {
+                        TRACE_MSG2(PCD, "BULK IN EPN - DMA ACTIVE epn %d last %d", epn, ep->last);
+                        au_epn_interrupt_disable(epn);
+                        return;
+                }
+        if (wrs) {                                                              // check for underflow or overflow
+                rc = 1;
+                if (wrs & USBDEV_FSTAT_UF) {
+                        TRACE_MSG2(PCD, "BULK IN EPN - UF epn %d wrs: %x", epn, wrs);
+                        rc = 1;                                                 // set rc to indicate an error
+                }
+                if (wrs & USBDEV_FSTAT_OF) 
+                        TRACE_MSG2(PCD, "BULK IN EPN - OF epn %d wrs: %x", epn, wrs);
+                au_writel(USBDEV_FSTAT_FLUSH | USBDEV_FSTAT_UF | USBDEV_FSTAT_OF, ep->wrs);     // flush the fifo 
+        }
+        if (cs & USBDEV_CS_NAK) {
+                RETURN_IF (ep->last && ((wrs&0x1f) == ep->last));
+                rc = 1;
+        }
+        if (cs & USBDEV_CS_STALL) {                                             // clear stall if present
+                TRACE_MSG1(PCD, "BULK IN EPN - CLEAR STALL %d", epn);
+                cs &= ~USBDEV_CS_STALL;
+                au_writel(cs, ep->cs);
+                return;
+        }
+        TRACE_MSG4(PCD, "BULK IN EPN epn: %d rc: %d last: %d sent: %d", epn, rc, endpoint->last, endpoint->sent);
+        if ((tx_urb = pcd_tx_complete_irq(endpoint, rc))) {
+                if ((tx_urb->actual_length > endpoint->sent) || (endpoint->tx_urb->flags & USBD_URB_SENDZLP)) {
+                        au_start_in_bulk(epl2p[endpoint->bEndpointAddress&0xf], endpoint, ep);
+                        /* XXX magic delay - without this large packets will eventually stall the transmit
+                         * XXX and all traffic in both directions will stop. 
+                         */
+                        if (!au1x00_new_silicon) 
+                                udelay(8);
+                        TRACE_MSG1(PCD, "BULK IN EPN - LEFT TO SEND %d", tx_urb->actual_length - endpoint->sent);
+                        return;
+                }
+        }
+        au_epn_interrupt_disable(epn);                                         // disable interrupts
+}
+
+/* au_start_in_iso - start transmit
+ * The au1x00 will start to send when the first byte is loaded into the FIFO, either by DMA or
+ * PIO. The packetsize must be set first.
+ */
+static void au_start_in_iso (unsigned int epn, struct usbd_endpoint_instance *endpoint, struct ep_regs *ep)
+{
+        struct usbd_urb *urb = endpoint->tx_urb;
+        unsigned char *bp = urb->buffer + endpoint->sent;
+        int last;
+        TRACE_MSG2(PCD, "START IN ISO actual: %d sent: %d", urb->actual_length, endpoint->sent);
+        RETURN_IF ((urb->actual_length - endpoint->sent) == 0);
+        last = ep->last = endpoint->last = MIN (urb->actual_length - endpoint->sent, endpoint->wMaxPacketSize);
+        TRACE_MSG2(PCD, "START IN ISO last: %d packetSize: %d", last, endpoint->wMaxPacketSize);
+        au_writel(USBDEV_FSTAT_FLUSH | USBDEV_FSTAT_UF | USBDEV_FSTAT_OF, ep->wrs); // XXX
+        au1x00_new_silicon ? au_epn_interrupt_enable : au_epn_interrupt_disable (epn);
+        dma_cache_wback_inv((unsigned long) bp, last);
+        au_writel(last << 1, ep->cs);
+        au_start_dma_in(ep->indma, bp, last);
+}
+
+static void au_in_iso(unsigned int epn, struct ep_regs *ep, struct usbd_endpoint_instance *endpoint)
+{
+        struct usbd_urb *tx_urb = pcd_tx_complete_irq(endpoint, 0);
+        u32 cs = au_readl(ep->cs);
+        u32 wrs = au_readl(ep->wrs);
+        TRACE_MSG2(PCD, "ISO IN EPN - cs: %x wrs: %x", cs, wrs);
+        if (!au1x00_new_silicon) 
+                if (epn && (ep->last > 8)) {
+                        au_epn_interrupt_disable(epn);
+                        return;
+                }
+        TRACE_MSG4(PCD, "ISO IN EPN epn: %d last: %d sent: %d", epn, endpoint->last, endpoint->sent, 0);
+        ep->last = 0;
+        if ((tx_urb = endpoint->tx_urb) && (tx_urb->actual_length > endpoint->sent)) {
+                au_start_in_iso(epl2p[endpoint->bEndpointAddress&0xf], endpoint, ep);
+                /* XXX magic delay - without this large packets will eventually stall the transmit
+                 * XXX and all traffic in both directions will stop. 
+                 */
+                if (!au1x00_new_silicon) 
+                        udelay(8);
+                TRACE_MSG1(PCD, "ISO IN EPN - LEFT TO SEND %d", tx_urb->actual_length - endpoint->sent);
+                return;
+        }
+        else 
+                TRACE_MSG0(PCD, "ISO IN EPN - nothing to send");
+        au_epn_interrupt_disable(epn);                                 // disable interrupts
+}
+/* ********************************************************************************************* */
+
+static void au_start_out_bulk(unsigned int epn, struct usbd_endpoint_instance *endpoint, struct ep_regs *ep)
+{
+        TRACE_MSG2(PCD, "START OUT BULK %d %d", epn, endpoint->wMaxPacketSize);
+        if (!endpoint->rcv_urb) {
+                TRACE_MSG0(PCD, "START OUT BULK DISABLE");
+                au_epn_interrupt_disable(epn);
+                return;
+        }
+        if (endpoint->rcv_error) {
+                TRACE_MSG0(PCD, "START OUT BULK reseting rcv_error");
+                endpoint->rcv_error = 0;
+        }
+        au_start_dma_out(ep->outdma, endpoint->rcv_urb->buffer + endpoint->rcv_urb->actual_length, endpoint->wMaxPacketSize);
+}
+
+static void au_out_bulk(unsigned int epn, struct ep_regs *ep, struct usbd_endpoint_instance *endpoint)
+{
+        int bytes = 0;
+        struct usbd_urb *urb;
+        struct usbd_urb *completed_urb = NULL;
+        u32 cs;
+        u32 rds;
+        u32 nrds;
+        au_halt_dma(au_get_dma_chan(ep->outdma));
+        cs = au_readl(ep->cs);
+        rds = au_readl(ep->rds);
+        TRACE_MSG2(PCD, "BULK OUT CS: %04x RD: %04x", cs, rds);
+        bytes = endpoint->wMaxPacketSize - au_get_dma_residue(au_get_dma_chan(ep->outdma));
+        if (!(urb = pcd_rcv_next_irq(endpoint))) {
+                TRACE_MSG0(PCD, "BULK OUT EPN - no rcv_urb");
+                au_epn_interrupt_disable(epn);
+                return;
+        }
+        /* The original AU1X00 UDC design will continue to receive data as long as there is room
+         * in the FIFO. We cannot tell when we are at the end of a packet and/or have the start
+         * of a new one.
+         *
+         * There are only two scenarios that are guaranteed (almost) to be correct:
+         *
+         *      64 bytes of data from DMA, empty fifo, continue Bulk OUT < 60 bytes of data and
+         *      < 4 bytes in fifo, end Bulk OUT.
+         *
+         * There may be a third scenario that is ok:
+         *
+         *      0 bytes dma, 0 bytes in fifo, NAK
+         *
+         * Everything else is an error. In all cases we assume that it is safer to drop data
+         * than to accept it in error. This allows CRC or size protected encapsulations to
+         * notice bulk transfers received with errors.
+         *
+         * In general none of the policies or strategies are able to cope with all errors
+         * without missing errors and dropping good data.  The intent is to minimize the amount
+         * of potentially bad data getting to the function driver while minimizing the amount of
+         * good data that is dropped.
+         *
+         * The new silicon mitigates this problem for non control endpoints because it will NAK
+         * additional data until the interrupt service flag is reset.
+         * 
+         * Start with generic error tests, OF, UF or NAK indicate an error we cannot recover
+         * from, start flushing until end of current bulk transfer (wait for a short packet)
+         *
+         */
+        if (rds & (USBDEV_FSTAT_OF | USBDEV_FSTAT_UF)) {
+                TRACE_MSG2(PCD, "BULK OUT FLUSHING %d length: %d", bytes, urb->actual_length);
+                THROW(start_flushing);
+        }
+        rds = rds & USBDEV_FSTAT_FCNT_MASK;
+        nrds = au_readl(ep->rds);
+        if (64 == bytes) {
+                TRACE_MSG2(PCD, "BULK OUT 64 BYTES %d length: %d", bytes, urb->actual_length);
+                /* full size packet received, check that we are not flushing and that the FIFO
+                 * does not have any data. If there is data in the FIFO we may not be able to
+                 * restart DMA in time, so start flushing 
+                 */
+                if (endpoint->rcv_error) {
+                        TRACE_MSG4(PCD, "FULL PACKET bytes: %d rds: %d nrds: %d cp0: %d CONTINUE FLUSHING", 
+                                        bytes, rds, nrds, cp0_count);
+                        THROW(start_flushing);
+                }
+                if ((nrds > 6)  && (nrds < 8) ) {
+                        TRACE_MSG4(PCD, "FIFO not empty bytes: %d rds: %d nrds: %d cp0: %d START FLUSHING nrds > 6 < 8", 
+                                        bytes, rds, nrds, cp0_count);
+                        THROW(start_flushing);
+                }
+                if (!urb->actual_length) 
+                        TRACE_MSG4(PCD, "PACKET ok bytes: %d rds: %d nrds: %d cp0: %d ACCEPTING 64 bytes", 
+                                        bytes, rds, nrds, cp0_count);
+                au_writel(USBDEV_FSTAT_FLUSH | USBDEV_FSTAT_UF | USBDEV_FSTAT_OF, ep->rds);
+                au_rcv_complete_irq(endpoint, 64, 0);
+                if (cs & USBDEV_CS_NAK) 
+                        if (nrds && (nrds < 8) ) {
+                                TRACE_MSG4(PCD, "NAK bytes: %d rds: %d nrds: %d cp0: %d START FLUSHING CS_NAK", 
+                                                bytes, rds, nrds, cp0_count);
+                                THROW(start_flushing);
+                        }
+        }
+        else if ((cs & USBDEV_CS_NAK) && (!nrds || (nrds == 8)) ) {
+                TRACE_MSG2(PCD, "BULK OUT  NAK BYTES %d length: %d", bytes, urb->actual_length);
+                /* a nak'd packet may be ok to ignore IFF the FIFO is empty(?) or completely full.
+                 */
+                if (endpoint->rcv_error) {
+                        TRACE_MSG4(PCD, "NAK bytes: %d rds: %d nrds: %d cp0: %d CONTINUE FLUSHING", 
+                                        bytes, rds, nrds, cp0_count);
+                        THROW(start_flushing);
+                }
+                au_writel(USBDEV_FSTAT_FLUSH | USBDEV_FSTAT_UF | USBDEV_FSTAT_OF, ep->rds);
+        }
+        else {
+                TRACE_MSG2(PCD, "BULK OUT < 64 BYTES %d length: %d", bytes, urb->actual_length);
+                /* short packet by DMA, additional data for this packet in FIFO, more than 3
+                 * bytes is probably an error.
+                 */
+                if ((cs & USBDEV_CS_NAK) && nrds && (nrds < 8) ) {
+                        TRACE_MSG4(PCD, "BULK OUT NAK bytes: %d rds: %d nrds: %d cp0: %d START FLUSHING", 
+                                        bytes, rds, nrds, cp0_count);
+                        THROW(start_flushing);
+                }
+                if (nrds > 4) {
+                        TRACE_MSG4(PCD, "BULK OUT SHORT PACKET by DMA full FIFO bytes: %d rds: %d nrds: %d cp0: %d START FLUSHING", 
+                                        bytes, rds, nrds, cp0_count);
+                        THROW(start_flushing);
+                }
+                TRACE_MSG4(PCD, "BULK OUT SHORT bytes: %d rds: %d nrds: %d cp0: %d reading fifo", bytes, rds, nrds, cp0_count);
+                au_fifo_read(ep, urb->buffer + urb->actual_length + bytes, nrds);
+                bytes += nrds;
+                TRACE_MSG1(PCD, "BULK OUT < 64 BYTES %d", bytes);
+                au_writel(USBDEV_FSTAT_FLUSH | USBDEV_FSTAT_UF | USBDEV_FSTAT_OF, ep->rds);
+                if (!endpoint->rcv_error) {
+                        TRACE_MSG0(PCD, "BULK OUT COMPLETED URB");
+                        au_rcv_complete_irq(endpoint, bytes, 0);
+                }
+                else {
+                        TRACE_MSG0(PCD, "BULK OUT - FLUSHING URB - reseting rcv_error");
+                        endpoint->rcv_error = 0;
+                }
+        }
+        CATCH(start_flushing) {
+                TRACE_MSG0(PCD, "BULK OUT - START FLUSHING URB");
+                endpoint->rcv_error = 1;
+                endpoint->rcv_urb->actual_length = 0;
+                au_writel(USBDEV_FSTAT_FLUSH | USBDEV_FSTAT_UF | USBDEV_FSTAT_OF, ep->rds);
+                bytes = 0;
+        }
+        TRACE_MSG0(PCD, "BULK OUT - RESTARTING");
+        au_start_out_bulk(epn, endpoint, ep);
+}
+
+static void au_start_out_iso(unsigned int epn, struct usbd_endpoint_instance *endpoint, struct ep_regs *ep)
+{
+        int outdma = ep->outdma;
+        RETURN_IF(!endpoint->rcv_urb);
+        au_start_dma_out(outdma, endpoint->rcv_urb->buffer + endpoint->rcv_urb->actual_length, endpoint->wMaxPacketSize);
+}
+
+static void au_out_iso(unsigned int epn, struct ep_regs *ep, struct usbd_endpoint_instance *endpoint)
+{
+        int bytes = 0;
+        struct usbd_urb *urb;
+        u32 cs;
+        u32 rds;
+        au_halt_dma(au_get_dma_chan(ep->outdma));
+        cs = au_readl(ep->cs);
+        rds = au_readl(ep->rds);
+        if (!endpoint) {
+                au_writel(USBDEV_FSTAT_FLUSH | USBDEV_FSTAT_UF | USBDEV_FSTAT_OF, ep->rds);
+                return;
+        }
+        if (!(urb = endpoint->rcv_urb)) {
+                TRACE_MSG2(PCD, "ISO OUT EPN - rcv_urb was NULL bytes: %d rds: %d", bytes, rds);
+                au_rcv_complete_irq(endpoint, bytes, 1);
+                TRACE_MSG0(PCD, "ISO OUT setting rcv_error");
+        }
+        bytes = endpoint->wMaxPacketSize - au_get_dma_residue(au_get_dma_chan(ep->outdma));
+        rds = rds & USBDEV_FSTAT_FCNT_MASK;
+        au_fifo_read(ep, urb->buffer + urb->actual_length + bytes, rds);
+        bytes += rds;
+        au_writel(USBDEV_FSTAT_FLUSH | USBDEV_FSTAT_UF | USBDEV_FSTAT_OF, ep->rds);
+        au_rcv_complete_irq(endpoint, bytes, 0);
+        au_start_out_iso(epn, endpoint, ep);
+}
+/* ********************************************************************************************* */
+/* au_tx_dma_done - TX DMA interrupt handler
+ */
+static void au_tx_dma_done(int irq, void *dev_id, struct pt_regs *regs)
+{
+        int epn = (int) dev_id;
+        struct usbd_endpoint_instance *endpoint = pcd_instance->bus->endpoint_array + epn;
+        struct ep_regs *ep = &ep_regs[epn];
+        int residue;
+#if 0
+        int indma = irq - 6;
+        struct dma_chan *chan = get_dma_chan(indma);
+#endif
+#ifdef DBDMA
+#else /* DBDMA */
+        struct dma_chan *chan = get_dma_chan(ep->indma);
+        u32 mode = au_readl(chan->io + DMA_MODE_READ);
+        TRACE_MSG1(PCD, "TX DMA done: mode: %x", mode);
+        if (mode & DMA_D0) 
+                au_writel(DMA_D0, chan->io + DMA_MODE_CLEAR);
+        if (mode & DMA_D1) 
+                au_writel(DMA_D1, chan->io + DMA_MODE_CLEAR);
+#endif /* DBDMA */
+        ocd_ops.interrupts++;
+        RETURN_IF (!epn);
+        au_halt_dma(au_get_dma_chan(ep->indma));
+        residue = au_get_dma_residue(au_get_dma_chan(ep->indma));
+        TRACE_MSG2(PCD, "TX DMA IRQ - epn %d residue %d", epn, residue);
+        if (!au1x00_new_silicon) {
+                /* XXX magic delay - without this large packets will eventually stall the transmit
+                 * XXX and all traffic in both directions will stop. 
+                 */
+                udelay(8);
+                pcd_tx_complete_irq (endpoint, residue?1:0);
+                au_epn_interrupt_enable(epl2p[endpoint->bEndpointAddress&0xf]);
+        }
+        ep->last = 0;
+}
+
+/* au_int_req - usb interrupt handler
+ */
+static void au_int_req (int irq, void *dev_id, struct pt_regs *regs)
+{
+        u32 intstat;
+        struct ep_regs *ep;
+#if defined(MAX_INTR_LOOP_STATS)
+        u32 loopcount = 0;
+#endif
+#ifdef  RECORD_LATENCY
+        cp0_count = (read_c0_count(CP0_COUNT) - cp0_record) >> 9;
+        if (cp0_count < CP0_COUNTS) 
+                cp0_counts[cp0_count]++;
+#endif
+#ifdef  CHECK_LATENCY
+        u32 cp0_count = read_c0_count(CP0_COUNT);
+#endif
+        ocd_ops.interrupts++;
+#if 0
+        if (ocd_ops.interrupts > 2000) {
+                TRACE_MSG0(PCD, "UDC_INT call udc_disable_interrupts");
+                au_inten(0);
+                return;
+        }
+#endif
+        while (( intstat = au_readl(USBD_INTSTAT) & au1x00_inten)) {    // read and reset interrupt status register
+
+                int epn;
+#if 1                
+                for (epn = 2; epn < 6; epn++) {
+                        CONTINUE_IF (!(intstat & (1 << epn)));
+#else
+                // NOT TESTED
+                u32 local_intstat = intstat;
+                TRACE_MSG2(PCD, "INTSTAT: %04x %04x", intstat, au_readl(USBD_INTEN));
+                while (local_intstat & 0x3f) {
+                        int epn = 31 - au_clz(local_intstat);
+                        local_intstat &= ~(1<<epn);
+#endif
+                        if (udc_saw_bus_activity) {
+                                udc_saw_bus_activity = 0;
+                                usbd_bus_event_handler_irq (pcd_instance->bus, DEVICE_BUS_ACTIVITY, 0);
+                                au_inten(0x0033|USBDEV_INT_SOF);                    
+                        }
+                        ep = &ep_regs[epn];
+                        switch(ep->eptype) {
+                        case USB_DIR_IN | USB_ENDPOINT_BULK:
+                        case USB_DIR_IN | USB_ENDPOINT_INTERRUPT:
+                                //TRACE_MSG2(PCD, "BULK IN %d %02x", epn, ep->eptype);
+                                au_in_bulk(epn, ep, pcd_instance->bus->endpoint_array + epn);
+                                break;
+                        case USB_DIR_IN | USB_ENDPOINT_ISOCHRONOUS:
+                                //TRACE_MSG2(PCD, "ISO IN %d %02x", epn, ep->eptype);
+                                au_in_iso(epn, ep, pcd_instance->bus->endpoint_array + epn);
+                                break;
+                        case USB_DIR_OUT | USB_ENDPOINT_BULK:
+                        case USB_DIR_OUT | USB_ENDPOINT_INTERRUPT:
+                                //TRACE_MSG2(PCD, "BULK OUT %d %02x", epn, ep->eptype);
+                                au_out_bulk(epn, ep, pcd_instance->bus->endpoint_array + epn);
+                                break;
+                        case USB_DIR_OUT | USB_ENDPOINT_ISOCHRONOUS:
+                                //TRACE_MSG2(PCD, "ISO OUT %d %04d", epn, au_readl(NUSBD_FRAMENUM));
+                                au_out_iso(epn, ep, pcd_instance->bus->endpoint_array + epn);
+                                break;
+                        }
+                }
+                au_writel(intstat, USBD_INTSTAT);               // Only clear the interrupt(s) AFTER servicing OUT
+                /* even though we disable the bulk-in interrupt (endpoint 2) prior to enabling
+                 * DMA we always see one additional interrupt that is a NAK on that endpoint.
+                 */
+                CONTINUE_IF(!(intstat & au1x00_inten));
+                /* handle control endpoint and suspend interrupt
+                 */
+                if (intstat & ( ((1 << 0) | (1 << 1) | (1 << 3) | (1 << 5) | USBDEV_INT_SOF))) {
+                        if (intstat & (1 << 0)) 
+                                au_out_ep0(pcd_instance->bus->endpoint_array + 0, &ep_regs[0]);
+                        if (intstat & (1 << 1)) 
+                                au_in_ep0(pcd_instance->bus->endpoint_array + 0, &ep_regs[0]);
+                        if (intstat & USBDEV_INT_SOF) 
+                                if (USBD_SUSPENDED == pcd_instance->bus->status) {
+                                        TRACE_MSG0(PCD, "SUS - ACTIVITY");
+                                        udc_saw_bus_activity++;
+                                }
+                }
+#if defined(MAX_INTR_LOOP_STATS)
+                loopcount += 1;                         // Gather stats on how many times this loop is performed. 
+#endif
+        }
+#if defined(MAX_INTR_LOOP_STATS)
+        interrupt_loop_stats[MIN(loopcount, MAX_INTR_LOOP_STATS)]++;
+#endif
+#if defined(CHECK_LATENCY)
+        TRACE_MSG1(PCD, "USB IRQ - %d", read_c0_count(CP0_COUNT) - cp0_count);
+#endif
+#if defined(RECORD_LATENCY)
+        cp0_record = read_c0_count(CP0_COUNT);
+#endif
+}
+
+/* au_int_sus -suspend interrupt handler
+ */
+static void au_int_sus (int irq, void *dev_id, struct pt_regs *regs)
+{
+        ocd_ops.interrupts++;
+        TRACE_MSG2(PCD, "SUS - INACTIVE device: %d status: %d", pcd_instance->bus->device_state, pcd_instance->bus->status);
+        switch(pcd_instance->bus->status) {
+        case USBD_OPENING:
+        case USBD_OK:
+                au_inten(0x0033);
+                usbd_bus_event_handler_irq (pcd_instance->bus, DEVICE_BUS_INACTIVE, 0);
+                break;
+        default:
+                break;
+        }
+}
+/* ********************************************************************************************* */
+/* 
+ * au_connect - enable pullup resistor
+ * Turn on the USB connection by enabling the pullup resistor.
+ *
+ * au_disconnect - disable pullup resistor
+ * Turn off the USB connection by disabling the pullup resistor.
+ */
+
+#if defined(CONFIG_MIPS_PICOENGINE_MVCI)
+void au_connect (struct pcd_instance *pcd)
+{
+        extern void pico_mvci_set_usb_pullup(int);
+        pico_mvci_set_usb_pullup(1);
+}
+
+void au_disconnect (struct pcd_instance *pcd)
+{               
+        extern void pico_mvci_set_usb_pullup(int);
+        pico_mvci_set_usb_pullup(0);
+}
+
+#elif defined(CONFIG_MIPS_FREEHAND_NATIVE)
+void au_connect (struct pcd_instance *pcd)
+{
+        au1000gpio_set(GPIO01);
+}
+
+void au_disconnect (struct pcd_instance *pcd)
+{               
+        au1000gpio_clear(GPIO01);
+}
+#elif defined(CONFIG_AMX_MORDOR)
+void au_connect (struct pcd_instance *pcd)
+{
+        au_writel(0x00000008, SYS_OUTPUTSET);
+}
+
+void au_disconnect (struct pcd_instance *pcd)
+{               
+        au_writel(0x00000008, SYS_OUTPUTCLR);
+}
+#else
+void au_connect (struct pcd_instance *pcd)
+{
+}
+void au_disconnect (struct pcd_instance *pcd)
+{
+}
+#endif
+
+#if LINUX_VERSION_CODE < KERNEL_VERSION(2,4,19)
+#undef read_c0_prid
+#define read_c0_prid()  read_32bit_cp0_register(CP0_PRID)
+#endif
+#undef read_c0_COUNT
+#ifndef read_c0_count
+#define read_c0_count()  read_32bit_cp0_register(CP0_COUNT)
+#endif
+/* au_loc_conn - used to enable or disable peripheral connecting to bus
+ *
+ */
+void au_loc_conn(struct otg_instance *otg, u8 flag)
+{
+        struct pcd_instance *pcd = (struct pcd_instance *)otg->pcd;
+        TRACE_MSG0(PCD, "--");
+        TRACE_MSG1(PCD, "PCD: %x", (int)pcd);
+        switch (flag) {
+        case SET:
+                TRACE_MSG0(PCD, "OUTPUT: OCD_LOC_CONN_SET");
+                TRACE_MSG0(PCD, "OUTPUT: OCD_LOC_CONN_RESET - Enable DP PULLUP");
+                au_connect(pcd);
+                break;
+
+        case RESET:
+                TRACE_MSG0(PCD, "OUTPUT: OCD_LOC_CONN_RESET");
+                TRACE_MSG0(PCD, "OUTPUT: OCD_LOC_CONN_RESET - Disable DP PULLUP");
+                au_disconnect(pcd);
+                break;
+
+        case PULSE:
+                TRACE_MSG0(PCD, "OUTPUT: OCD_LOC_CONN_PULSE");
+                break;
+        }
+}
+
+/* ********************************************************************************************* */
+/* au_ticks - get current ticks
+* */
+u64 au_ticks (void)
+{       
+        return read_c0_count();
+}       
+
+/* au_elapsed - return micro-seconds between two tick values
+ */
+u64 au_elapsed(u64 *t1, u64 *t2)
+{
+        u64 ticks = (*t1 > *t2) ? (*t1 - *t2) : (*t2 - *t1);
+        ticks = (u32)((u32) ticks / (u32) CONFIG_OTG_AU1X00_SCLOCK);
+        return ticks;
+}
+
+/* au_framenum - get current framenum
+ */             
+u16 au_framenum (void)
+{               
+        return au_readl(NUSBD_FRAMENUM);
+}               
+
+
+/* ********************************************************************************************* */
+/* au_start_endpoint_in - start transmit
+ */
+void au_start_endpoint_in(struct pcd_instance *pcd, struct usbd_endpoint_instance *endpoint)
+{
+        int epn = epl2p[endpoint->bEndpointAddress&0xf];
+        struct ep_regs *ep = &ep_regs[epn];
+        switch(endpoint->bmAttributes & USB_ENDPOINT_MASK) {
+        case USB_ENDPOINT_CONTROL:
+                au_in_ep0(endpoint, ep);
+                break;
+        case USB_ENDPOINT_BULK:
+        case USB_ENDPOINT_INTERRUPT:
+                au_start_in_bulk(epn, endpoint, ep);
+                break;
+        case USB_ENDPOINT_ISOCHRONOUS:
+                au_start_in_iso(epn, endpoint, ep);
+                break;
+        }
+        if (au1x00_new_silicon) 
+                au_epn_interrupt_enable(epn);
+}
+
+/* au_start_endpoint_out - start receive
+ */
+void au_start_endpoint_out(struct pcd_instance *pcd, struct usbd_endpoint_instance *endpoint)
+{
+        int epn = epl2p[endpoint->bEndpointAddress&0xf];
+        struct ep_regs *ep = &ep_regs[epn];
+        switch(endpoint->bmAttributes & USB_ENDPOINT_MASK) {
+        case USB_ENDPOINT_CONTROL:
+                break;
+        case USB_ENDPOINT_BULK:
+        case USB_ENDPOINT_INTERRUPT:
+                au_start_out_bulk(epn, endpoint, ep);
+                au_epn_interrupt_enable(epn);
+                break;
+        case USB_ENDPOINT_ISOCHRONOUS:
+                au_start_out_iso(epn, endpoint, ep);
+                break;
+        }
+}
+
+void au_cancel_in_irq(struct pcd_instance *pcd, struct usbd_urb *urb)
+{
+        int epn = epl2p[urb->endpoint->bEndpointAddress&0xf];
+        struct ep_regs *ep = &ep_regs[epn];
+        au_in_bulk(epn, ep, urb->endpoint);
+}
+
+void au_cancel_out_irq(struct pcd_instance *pcd, struct usbd_urb *urb)
+{
+        int epn = epl2p[urb->endpoint->bEndpointAddress&0xf];
+        struct ep_regs *ep = &ep_regs[epn];
+        au_out_bulk(epn, ep, urb->endpoint);
+}
+
+/* au_serial_init - set a serial number if available
+ */
+int au_serial_init (struct pcd_instance *pcd)
+{
+#if defined(CONFIG_MIPS_FREEHAND)
+        int length;
+        long data[16];          /* yeah a hack, but we KNOW it's 16 */
+        struct i2c_client *client;
+        char chData[16];
+        int i;
+
+        if (!(client = getFreeHandEepromClient())) {
+                printk(KERN_INFO"eeprom not ready when au_serial_init called\n");
+                return -EINVAL;
+        }
+        eeprom_contents(client, SENSORS_PROC_REAL_READ, EEPROM_SYSCTL1, &length, (long *)data);
+
+        /* serial number is first 9 longs. But each long is just an ASCII char
+         * convert this to a string and then extract a 4 byte value from it
+         */
+        for (i = 0; i < 9; chData[i] = (char)data[i], i++);     /* trunc, is ok */
+        chData[9] = 0;                                          /* terminate string */
+        printk(KERN_INFO"%s: %s\n", __FUNCTION__, chData);
+        pcd_instance->bus->serial_number_str = lstrdup(chData);
+        return 0;
+#else
+        return -EINVAL;
+#endif
+}
+
+
+#ifdef DBDMA
+static int request_dma(int ep, int src, int dest, char *str, void dma_done(int , void *, struct pt_regs *))
+{
+        unsigned int dma;
+
+        if ((dma = au1xxx_dbdma_chan_alloc(src, dest, dma_done, (void *)ep)) < 0) {
+                printk(KERN_INFO"request_io[%d] dma: %X src: %x dest: %x %s FAILED\n", ep, dma, src, dest, str);
+                return -1;
+        }
+
+        au1xxx_dbdma_set_devwidth(dma, 16);
+
+        if(au1xxx_dbdma_ring_alloc(dma, 4) == 0) {
+                printk(KERN_INFO"Ring Buffer Allocation for  dma: %X src: %x dest: %x %s FAILED\n", ep, dma, src, dest, str);
+                return -1;
+	}
+        printk(KERN_INFO"request_io[%d] dma: %X src: %x dest: %x %s SUCCEDED\n", ep, dma, src, dest, str);
+        return dma;
+}
+#else /* DBDMA */
+static int request_dma(int ep, int id, char *str, void dma_done(int , void *, struct pt_regs *))
+{
+        int dma = request_au1000_dma(id, str, dma_done, SA_INTERRUPT, (void *)ep);
+        if (dma >= 0) return dma;
+        printk(KERN_INFO"request_io[%d] dma: %d id: %x %s FAILED\n", ep, dma, id, str);
+        return -1;
+}
+#endif /* DBDMA */
+
+/* au_start - start session
+ */
+void au_start (struct pcd_instance *pcd)
+{
+        au_inten(0x0033|USBDEV_INT_SOF);                                // Only enable receive interrupts. 
+}
+
+/* au_stop - stop session
+ */
+void au_stop (struct pcd_instance *pcd)
+{
+        au_inten(0);
+}
+
+int au_assign_endpoint( u8 physicalEndpoint, int used[6], struct usbd_endpoint_map *endpoint_map, u8 bmAttributes,
+                u16 wMaxPacketSize, u16 transferSize) 
+{
+        struct ep_regs *ep = &ep_regs[physicalEndpoint]; 
+        RETURN_EINVAL_IF(used[physicalEndpoint]);
+        endpoint_map->bEndpointAddress[0] = epp2l[physicalEndpoint];
+        endpoint_map->physicalEndpoint[0] = physicalEndpoint;
+        endpoint_map->wMaxPacketSize[0] = wMaxPacketSize;
+        endpoint_map->transferSize[0] = transferSize;
+        endpoint_map->bmAttributes[0] = bmAttributes;
+        used[physicalEndpoint]++;
+        ep->eptype = bmAttributes & 0x83;
+        return 0;
+}
+
+int au_request_endpoints(struct pcd_instance *pcd, struct usbd_endpoint_map *endpoint_map_array, int endpointsRequested, 
+                struct usbd_endpoint_request *requestedEndpoints)
+{
+        struct usbd_device_description *device_description;
+        int i, j;
+        int used[UDC_MAX_ENDPOINTS];
+        memset(used, 0, sizeof(used));
+        for (j = 0, i = 0; i < endpointsRequested; i++, j++) {
+                struct usbd_endpoint_map *endpoint_map = endpoint_map_array + i;
+                u8 bmAttributes = requestedEndpoints[i].bmAttributes;
+                u16 transferSize = requestedEndpoints[i].fs_requestedTransferSize;
+                RETURN_EINVAL_UNLESS(j < UDC_MAX_ENDPOINTS);
+                switch(bmAttributes) {
+                case USB_DIR_OUT | USB_ENDPOINT_BULK:
+                case USB_DIR_OUT | USB_ENDPOINT_INTERRUPT:
+                case USB_DIR_OUT | USB_ENDPOINT_INTERRUPT | USB_ENDPOINT_OPT:
+                        CONTINUE_IF(!au_assign_endpoint(4, used, endpoint_map, bmAttributes, 0x40, transferSize));
+                        CONTINUE_IF(!au_assign_endpoint(5, used, endpoint_map, bmAttributes, 0x40, transferSize));
+                        break;
+                case USB_DIR_OUT | USB_ENDPOINT_ISOCHRONOUS:
+                        CONTINUE_IF(!au_assign_endpoint(4, used, endpoint_map, bmAttributes, transferSize, transferSize));
+                        CONTINUE_IF(!au_assign_endpoint(5, used, endpoint_map, bmAttributes, transferSize, transferSize));
+                        break;
+                case USB_DIR_IN | USB_ENDPOINT_BULK:
+                case USB_DIR_IN | USB_ENDPOINT_INTERRUPT:
+                case USB_DIR_IN | USB_ENDPOINT_INTERRUPT | USB_ENDPOINT_OPT:
+                        CONTINUE_IF(!au_assign_endpoint(2, used, endpoint_map, bmAttributes, 0x40, transferSize));
+                        CONTINUE_IF(!au_assign_endpoint(3, used, endpoint_map, bmAttributes, 0x40, transferSize));
+                        break;
+                case USB_DIR_IN | USB_ENDPOINT_ISOCHRONOUS:
+                        CONTINUE_IF(!au_assign_endpoint(2, used, endpoint_map, bmAttributes, transferSize, transferSize));
+                        CONTINUE_IF(!au_assign_endpoint(3, used, endpoint_map, bmAttributes, transferSize, transferSize));
+                        break;
+                }
+                CONTINUE_IF(bmAttributes & USB_ENDPOINT_OPT);
+                return -EINVAL;
+        }
+        return 0;
+}
+
+int au_set_endpoints(struct pcd_instance *pcd, int endpointsRequested, struct usbd_endpoint_map *endpoint_map_array)
+{
+        int i, j;
+        u8 config[25];
+        u8 *cp;
+        memcpy(config, au1x00_config_bulk, sizeof(config));
+        for (i = 0; i < endpointsRequested; i++) {
+                struct usbd_endpoint_map *endpoint_map = endpoint_map_array + i;
+                int epreq = endpoint_map->bmAttributes[0];
+                int eptype = epreq & USB_ENDPOINT_MASK;
+                int epdir = epreq & USB_ENDPOINT_DIR_MASK ? 0x8 : 0;
+                int epsize = endpoint_map->wMaxPacketSize[0];
+                int epaddr = epp2l[endpoint_map->physicalEndpoint[0]];
+                CONTINUE_IF(!endpoint_map->physicalEndpoint[0] || (endpoint_map->physicalEndpoint[0] > 5));
+                cp = config + ((endpoint_map->physicalEndpoint[0] - 1) * 5);
+                cp[0] = (epaddr & 0xf) << 4 | 0x4;
+                cp[1] = (eptype << 4) | epdir | (epsize & 0x380) >> 7;
+                cp[2] = (epsize & 0x7F) << 1;
+        } 
+        au_inten(0);                                                            // disable interrupts
+        au_writel(0x0002, USBD_ENABLE);                                         // reset controller
+        udelay(100);
+        au_writel(0x0003, USBD_ENABLE);                                         // enable controller
+        udelay(100);
+        for (cp = config, i = 0; i < 25; i++, au_writel(*cp++, USBD_CONFIG));   // feed the config into the UDC
+        return 0;
+}
+/* ********************************************************************************************* */
+/* au_mod_init 
+ */
+int au_mod_init (void)
+{
+        int dev = request_irq (AU1000_USB_DEV_REQ_INT, au_int_req, SA_INTERRUPT, UDC_NAME "UDC Req", NULL);
+        int sus = request_irq (AU1000_USB_DEV_SUS_INT, au_int_sus, SA_INTERRUPT, UDC_NAME "UDC Sus", NULL);
+        u32 cp0_prid = read_c0_prid();
+        int i;
+
+
+        if (dev || sus) {                                               // check if either request irq failed
+                if (!dev) free_irq (AU1000_USB_DEV_REQ_INT, NULL);      // free irqs that might have
+                if (!sus) free_irq (AU1000_USB_DEV_SUS_INT, NULL);      // been successfully allocated
+                return -EINVAL;
+        }
+#ifdef DBDMA
+        for (i = 0; i < 6; i++) {
+                ep_regs[i].indma = ep_regs[i].tx_id ? 
+                        request_dma(i, DBDMA_MEM_CHAN, ep_regs[i].tx_id, ep_regs[i].tx_str, au_tx_dma_done) : -1;
+                ep_regs[i].outdma = ep_regs[i].rx_id ? 
+                        request_dma(i, ep_regs[i].rx_id, DBDMA_MEM_CHAN, ep_regs[i].rx_str, NULL) : -1;
+        }
+#else /*DBMA */
+        for (i = 0; i < 6; i++) {
+                ep_regs[i].indma = ep_regs[i].tx_id ?  request_dma(i, ep_regs[i].tx_id, ep_regs[i].tx_str, au_tx_dma_done) : -1;
+                ep_regs[i].outdma = ep_regs[i].rx_id ?  request_dma(i, ep_regs[i].rx_id, ep_regs[i].rx_str, NULL) : -1;
+        }
+#endif /*DBMA */
+        switch (cp0_prid & CP0_PRID_SOC_MASK) {
+        case CP0_PRID_AU1000:
+        case CP0_PRID_AU1100:
+                au1x00_new_silicon = (cp0_prid & CP0_PRID_REV_MASK) >= 4;
+                printk(KERN_INFO"%s: AU1100 cp0_prid: new: %d\n", __FUNCTION__, au1x00_new_silicon);
+                break;
+        case CP0_PRID_AU1500:
+                au1x00_new_silicon = (cp0_prid & CP0_PRID_REV_MASK) >= 2;
+                printk(KERN_INFO"%s: AU1500 cp0_prid: new: %d\n", __FUNCTION__, au1x00_new_silicon);
+                break;
+        case CP0_PRID_AU1550:
+                au1x00_new_silicon = (cp0_prid & CP0_PRID_REV_MASK) >= 0;
+                printk(KERN_INFO"%s: AU1550 cp0_prid: new: %d\n", __FUNCTION__, au1x00_new_silicon);
+                break;
+        default:
+                printk(KERN_INFO"%s: UNKNOWN CPU cp0_prid: %08x UNKNOWN UDC\n", __FUNCTION__, cp0_prid);
+                break;
+        }
+        return 0;
+}
+
+/* au_mod_exit  
+ */
+void au_mod_exit (void)
+{
+        int j;
+        au_writel(0x0000, USBD_ENABLE);
+        for (j = 0; j < 6; j++) {
+                struct ep_regs *ep = &ep_regs[j]; 
+#ifdef DBDMA
+                if (ep->indma != -1) {
+                        au1xxx_dbdma_chan_free(ep->indma);
+                        ep->indma = -1;
+                }
+                if (ep->outdma != -1) {
+                        au1xxx_dbdma_chan_free(ep->outdma);
+                        ep->outdma = -1;
+                }
+#else /*DBMA */
+                if (ep->indma != -1) {
+                        free_au1000_dma(ep->indma);
+                        ep->indma = -1;
+                }
+                if (ep->outdma != -1) {
+                        free_au1000_dma(ep->outdma);
+                        ep->outdma = -1;
+                }
+#endif /*DBMA */
+        }
+        free_irq (AU1000_USB_DEV_REQ_INT, NULL);
+        free_irq (AU1000_USB_DEV_SUS_INT, NULL);
+#if defined(MAX_INTR_LOOP_STATS)
+        {
+                u32 lc;
+                for (lc = 0; lc <= MAX_INTR_LOOP_STATS; lc++) 
+                        if (interrupt_loop_stats[lc]) 
+                                printk(KERN_ERR "%s: interrupt loopcount[%02u] %9u\n", __FUNCTION__, lc,interrupt_loop_stats[lc]);
+                printk(KERN_INFO"%s: halt_dma_expired: %d\n", __FUNCTION__, au_halt_dma_expired);
+        }
+#endif
+#ifdef RECORD_LATENCY
+        {
+                int i;
+                for (i = 0; i < CP0_COUNTS; i++) 
+                        if (cp0_counts[i]) 
+                                printk(KERN_INFO"%s: cp0_counts[%d] %d\n", __FUNCTION__, i, cp0_counts[i]);
+        }
+#endif
+}
+
+/* ********************************************************************************************* */
+struct usbd_pcd_ops usbd_pcd_ops = {
+        max_endpoints:  UDC_MAX_ENDPOINTS,
+        ep0_packetsize:  EP0_PACKETSIZE,
+        name:  UDC_NAME,
+        start: au_start,
+        stop: au_stop,
+        start_endpoint_in: au_start_endpoint_in,
+        start_endpoint_out: au_start_endpoint_out,
+        request_endpoints: au_request_endpoints,
+        set_endpoints: au_set_endpoints,
+        cancel_in_irq: au_cancel_in_irq,
+        cancel_out_irq: au_cancel_out_irq,
+        serial_init: au_serial_init,
+};
+
+struct pcd_ops pcd_ops = {
+        pcd_en_func: pcd_en_func,
+        mod_init: au_mod_init,
+        mod_exit: au_mod_exit,
+};
+
+struct ocd_ops ocd_ops = {
+	#if defined(CONFIG_OTG_TR_AUTO)
+        capabilities: OCD_CAPABILITIES_TR | OCD_CAPABILITIES_AUTO,
+	#else 
+        capabilities: OCD_CAPABILITIES_TR,
+        #endif
+        ticks: au_ticks,
+        elapsed: au_elapsed,
+};
+
+struct tcd_ops tcd_ops = {
+	dp_pullup_func: au_loc_conn,
+};
+
diff -uNr linux/drivers/no-otg/ocd/au1x00/au1x00.h linux/drivers/otg/ocd/au1x00/au1x00.h
--- linux/drivers/no-otg/ocd/au1x00/au1x00.h	1970-01-01 01:00:00.000000000 +0100
+++ linux/drivers/otg/ocd/au1x00/au1x00.h	2006-09-01 21:41:31.000000000 +0200
@@ -0,0 +1,112 @@
+/*
+ * otg/ocd/au1x00/au1100.h
+ *
+ *      Copyright (c) 2004 Belcarra
+ *
+ *
+ * By: 
+ *      Stuart Lynne <sl@belcarra.com>, 
+ *      Tom Rushworth <tbr@belcarra.com>, 
+ *      Bruce Balden <balden@belcarra.com>
+ */
+ /*!
+ * @defgroup AU1X00 AMD AU1X00
+ * @ingroup pcdgroup
+ */
+ 
+/*!
+ * @file otg/ocd/au1x00/au1x00.h
+ * @brief AU1X00 Header
+ *
+ * This file contains the private defines and structures for the AU1X00  
+ * Drivers. 
+ *
+ * @ingroup AU1X00
+ */
+
+
+
+/*
+ *
+ * au1100 
+ *
+ * The au1100 does not seem to work properly with an 8 byte
+ * packetsize. So 16, 32 or 64 are the only valid values.
+ *
+ * Unfortunately this means that a DMA channel is required for
+ * EP0 transmit.
+ */
+
+#define EP0_PACKETSIZE  0x8
+
+#define UDC_MAX_ENDPOINTS       6
+
+#define UDC_NAME        "AU1100"
+
+#define NUSBD_EP0RD                0xB0200000
+#define NUSBD_EP0WR                0xB0200004
+#define NUSBD_EP1WR                0xB0200008
+#define NUSBD_EP2WR                0xB020000C
+#define NUSBD_EP3RD                0xB0200010
+#define NUSBD_EP4RD                0xB0200014
+
+#define NUSBD_EP0CS                0xB0200024
+#define NUSBD_EP1CS                0xB0200028
+#define NUSBD_EP2CS                0xB020002C
+#define NUSBD_EP3CS                0xB0200030
+#define NUSBD_EP4CS                0xB0200034
+
+#define NUSBD_EP0RDSTAT            0xB0200040
+#define NUSBD_EP0WRSTAT            0xB0200044
+#define NUSBD_EP1WRSTAT            0xB0200048
+#define NUSBD_EP2WRSTAT            0xB020004C
+#define NUSBD_EP3RDSTAT            0xB0200050
+#define NUSBD_EP4RDSTAT            0xB0200054
+
+#define NUSBD_FRAMENUM             0xB0200038
+
+enum {
+        NDMA_ID_UART0_TX = 0,
+        NDMA_ID_UART0_RX,
+        NDMA_ID_GP04,
+        NDMA_ID_GP05,
+        NDMA_ID_AC97C_TX,
+        NDMA_ID_AC97C_RX,
+        NDMA_ID_UART3_TX,
+        NDMA_ID_UART3_RX,
+        NDMA_ID_USBDEV_EP0_RX,
+        NDMA_ID_USBDEV_EP0_TX,
+        NDMA_ID_USBDEV_EP1_TX,
+        NDMA_ID_USBDEV_EP2_TX,
+        NDMA_ID_USBDEV_EP3_RX,
+        NDMA_ID_USBDEV_EP4_RX,
+        NDMA_ID_I2S_TX,
+        NDMA_ID_I2S_RX,
+        NDMA_NUM_DEV
+};
+
+typedef struct ep_regs {
+        int rd;
+        int wr;
+        int cs;
+        int rds;
+        int wrs;
+        int rx_id;
+        int tx_id;
+        char * rx_str;
+        char * tx_str;
+        u32 indma;    
+        u32 outdma;
+        int last;
+        int eptype;
+} ep_regs_t;
+
+#define MAX_EPN_PACKET_SIZE 64
+#define CP0_PRID_SOC_MASK       0xff000000
+#define CP0_PRID_AU1000         0x00000000
+#define CP0_PRID_AU1500         0x01000000
+#define CP0_PRID_AU1100         0x02000000
+#define CP0_PRID_AU1550         0x03000000  /* AMX */
+
+#define CP0_PRID_REV_MASK       0x000000ff
+
diff -uNr linux/drivers/no-otg/ocd/fsotg/fs-hcd.c linux/drivers/otg/ocd/fsotg/fs-hcd.c
--- linux/drivers/no-otg/ocd/fsotg/fs-hcd.c	1970-01-01 01:00:00.000000000 +0100
+++ linux/drivers/otg/ocd/fsotg/fs-hcd.c	2006-09-01 21:41:31.000000000 +0200
@@ -0,0 +1,1527 @@
+/*
+ * otg/hcd/fsotg/fs-hcd.c - Freescale USBOTG aware Host Controller Driver (HCD)
+ *
+ *      Copyright (c) 2004 Belcarra Technologies
+ *
+ * By:
+ *      Stuart Lynne <sl@belcara.com>,
+ *      Tom Rushworth <tbr@belcara.com>,
+ *      Bruce Balden <balden@belcara.com>
+ */
+/*!
+ * @file otg/ocd/fsotg/fs-hcd.c
+ * @brief Freescale USB Host Controller Driver
+ *
+ * This is the hardware specific component of the HCD.  It knows nothing
+ * about URBs, and deals with "transfers".  This is not because it shouldn't
+ * know about them, but because URBs are generic, and so it seems reasonable
+ * to keep as much generic code in the hardware independent portion as
+ * possible.  As a result, the parameter list to hcd_hw_start_transfer()
+ * is really fat.
+ *
+ * This is not compiled as a self contained module, but is linked with
+ * the generic component hc_xfer.o.
+ *
+ * There is some processor specific code here to support the alternate processors
+ * that implement this type of USB support.
+ *
+ * There is no board or platform level code here.
+ *
+ * @ingroup FSOTG
+ * @{
+ */
+
+#include <linux/config.h>
+#include <linux/module.h>
+#include <linux/kernel.h>
+#include <linux/pci.h>    // For DMA directions
+//#include <linux/slab.h>
+#include <linux/errno.h>
+
+#include <linux/usb.h>
+#include <linux/delay.h>
+#include <asm/arch/mx2.h>
+
+#include <otg/otg-compat.h>
+
+#include "../core/hcd.h"
+
+#include <otg/usbp-chap9.h>
+#include <otg/otg-trace.h>
+#include <otg/otg-api.h>
+#include <otg/otg-utils.h>
+
+#include <otg/otg-hcd.h>
+#include <otg/hcd-l26.h>
+#include <otg/hcd-rh.h>
+#include <otg/hcd-hw.h>
+
+#include <otghw/fsotg-hardware.h>
+
+#ifndef CONFIG_OTG_ROOTHUB_VENDORID
+#define CONFIG_OTG_ROOTHUB_VENDORID 0x15ec
+#endif
+#ifndef CONFIG_OTG_ROOTHUB_PRODUCTID
+#define CONFIG_OTG_ROOTHUB_PRODUCTID 0xf010
+#endif
+#ifndef CONFIG_OTG_ROOTHUB_BCDDEVICE
+#define CONFIG_OTG_ROOTHUB_BCDDEVICE 0x0100
+#endif
+
+
+#define STATIC static
+extern void hcd_trace_mem(otg_tag_t tag, char *fn, char *msg, int len, volatile void *data);
+
+// Start of hardware specific components....
+
+/* ********************************************************************************************* */
+
+// Globals for debug
+static u32 monitoring_etd_mask = 0;  // 0 == no monitoring
+static u32 monitoring_etd_num;
+#define MAX_ETD_FRAMES 3
+static u32 current_etd_frame;
+static u32 done_flags[1+MAX_ETD_FRAMES];
+static u32 done_status[1+MAX_ETD_FRAMES];
+static endpoint_transfer_descriptor mon_etd[1+MAX_ETD_FRAMES];
+
+
+
+/*!
+ * @struct cc_name
+ * @brief Map CC values to names
+ */
+static char *cc_name[] = {
+        "no error", "CRC error", "bitstuff error", "data toggle error",
+        "stall", "device not responding", "PID failure" "reserved",
+        "data overrun", "data underrun", "ACK", "NAK", "buffer overrun",
+        "buffer underrun", "schedule overrun" "not accessed"
+};
+
+
+/*!
+ * comp_code_to_status() - map condition cod3 to errno value
+ * @param cc condition code
+ * @return errno value
+ */
+STATIC int comp_code_to_status(int cc)
+{
+        //
+        if (cc)
+                TRACE_MSG2(HCD,"NON-ZERO CC=%d %s", cc,cc_name[cc]);
+
+        switch (cc) {
+        case ETD_CC_NOERROR:           return 0;
+        case ETD_CC_CRCERR:            return -EILSEQ;
+        case ETD_CC_BITSTUFFERR:       return -EPROTO;
+        case ETD_CC_DATATOGERR:        return -EPROTO;  // I guess, nothing else looks better.
+        case ETD_CC_STALL:             return -EPIPE;
+        case ETD_CC_DEVNOTRESPONDING:  return -ETIMEDOUT;
+        case ETD_CC_PIDFAILURE:        return -EPROTO;
+        case ETD_CC_DATAOVERRUN:       return -EOVERFLOW;
+        case ETD_CC_DATAUNDERRUN:      return -EREMOTEIO;
+        case ETD_CC_ACK:               return -EPROTO;  // For lack of anything better
+        case ETD_CC_NAK:               return -EPROTO;  // For lack of anything better
+        case ETD_CC_BUFFEROVERRUN:     return -ECOMM;
+        case ETD_CC_BUFFERUNDERRUN:    return -ENOSR;
+        case ETD_CC_SCHEDOVERRUN:      return -ENOSPC;
+        case ETD_CC_NOTACCESSED:       return -EPROTO;  // For lack of anything better
+        default:                       return -EINVAL;
+        }
+}
+
+/*!
+ * rel_etd() - release etd
+ * {get,rel}_etd maintain a free list of ETDs by saving the index of the next free ETD in the
+ * etd->xbufsrtad field.  The list is terminated by 0x0000ffff.  List manipulation needs to be
+ * protected by irq_lock, since ETDs are released in the interrupt handler, but allocated at
+ * regular priority.
+ * @param fs_hcpriv
+ * @param etdn
+ */
+STATIC void rel_etd(fs_hcpriv *fs_hcpriv, int etdn)
+{
+        fs_wl(etd_word(etdn, 0), 0);
+        fs_wl(etd_word(etdn, 2), 0);
+        fs_wl(etd_word(etdn, 3), 0);
+        fs_wl(etd_word(etdn, 1), fs_hcpriv->free_etds);
+        fs_hcpriv->free_etds = etdn;
+}
+
+/*!
+ * get_etd() - get etd
+ * {get,rel}_etd maintain a free list of ETDs by saving the index of the next free ETD in the 
+ * etd->xbufsrtad field.  The list is terminated by 0x0000ffff.  List manipulation needs to be
+ * protected by irq_lock, since ETDs are released in the interrupt handler, but allocated at
+ * regular priority.
+ * @param fs_hcpriv
+ * @return int 
+ */
+STATIC int get_etd(fs_hcpriv *fs_hcpriv)
+{
+        // Return the index of an available ETD, or -1 if none are available.
+        unsigned long flags;
+        int etdn = -1;
+        local_irq_save(flags);
+        // Is list is empty?
+        if (0x0000ffff != (etdn = fs_hcpriv->free_etds)) 
+                fs_hcpriv->free_etds = fs_rl(etd_word(etdn, 1));
+        local_irq_restore(flags);
+        return etdn;
+}
+
+/* ********************************************************************************************* */
+/*
+ * For the generic transfer aware framework.
+ */
+
+// For debugging...
+static int num_urbs_started = 0;
+#define MAX_URBS_STARTED      0
+static char *dirpid_name[4] = {"SETUP","OUT","IN","WRONG"};
+//static char *direct_name[4] = {"TD00","OUT","IN","TD11"};
+static char *etd_urb_state_name[] = {"COMPLETED","SETUP_STATUS","SETUP_DATA","SETUP_PKT","BULK","INTERRUPT","ISOC","WRONG"};
+static char *format_name[4] = { "CONTROL", "ISO", "BULK", "INT", };
+
+
+/*!
+ * start_etd() - start an etd
+ * @param etdn
+ * @param len
+ * @param data
+ * @param out
+ */
+STATIC void start_etd(int etdn, int len, void *data, int out)
+{
+        if (len) {
+                consistent_sync(data,len, (out ? PCI_DMA_TODEVICE : PCI_DMA_FROMDEVICE));
+                fs_wl(OTG_DMA_ETD_MSA(etdn), virt_to_bus(data));
+        }
+        if (len || out) 
+                fs_wl(OTG_DMA_ETD_EN, ETD_MASK(etdn));
+        
+        fs_orl(OTG_HOST_IINT, ETD_MASK(etdn));                       // Don't wait until SOF, interrupt ASAP.
+        fs_orl(OTG_HOST_ETD_DONE, ETD_MASK(etdn));                   // Interrupt on ETD done (forget DMA interrupt).
+        fs_wl(OTG_HOST_ETD_EN, ETD_MASK(etdn));
+}
+
+/*!
+ * hcd_hw_start_transfer() - start a transfer
+ * Return 0..MAX_ACTIVE_XFERS-1 if the transfer is successfully started,
+ *       -1 if there is something wrong with the requested transfer,
+ *       -2 if there are no resources available at this time, but resubmission is OK. 
+ *
+ * @param bus_hcpriv    
+ * @param len           length of data region
+ * @param data          virtual address of data region
+ * @param toggle        toggle value to start the xfer with
+ * @param maxps         max packet size of endpoint
+ * @param slow          1 ==> slow speed, 0 ==> full speed  QQSV verify: (((urb->pipe) >> 26) & 1)
+ * @param endpoint      endpoint number
+ * @param address       USB device address
+ * @param pid           USB_PID_{OUT,IN,SETUP}
+ * @param format        PIPE_{CONTROL,BULK,INTERRUPT,ISOCHRONOUS}
+ * @param other         value depending on format
+ *
+ * PIPE_CONTROL:   other == address of setup packet.
+ * PIPE_BULK:      other == ZLP at end (T/F),
+ * PIPE_INTERRUPT: other == poll interval
+ */
+int hcd_hw_start_transfer(struct bus_hcpriv *bus_hcpriv, int len, void *data, int toggle, int maxps, 
+                int slow, int endpoint, int address, int pid, int format, u32 other)   
+{
+        fs_hcpriv *fs_hcpriv = (struct fs_hcpriv *) (bus_hcpriv->hw_hci);
+        fs_data_buff *x = NULL;
+        fs_data_buff *y = NULL;
+        int fmt = 0;
+        int out;
+        int etdn;
+        //int rc = 0;
+        int dir = 0;
+        endpoint_transfer_descriptor *sdp = NULL;
+        transfer_descriptor td;
+        int sdp_out = FALSE;
+
+        //TRACE_MSG8(HCD, "len: %d toggle: %d endpoint: %02x pid: %d format: %d address: %d %s %s",
+        //                len, toggle, endpoint, pid, format, address, dirpid_name[pid], format_name[format]);
+
+        // data address needs to be on a 32-bit boundary.
+        if (0x3 & (u32)data) {
+                printk(KERN_ERR "%s: invalid data alignment for urb, urb rejected.\n",__FUNCTION__);
+                TRACE_MSG1(HCD, "invalid data alignment for urb (%08x), urb rejected.",(u32)data);
+                return -1;
+        }
+        num_urbs_started += 1;
+        if ((MAX_URBS_STARTED > 0) && (num_urbs_started > MAX_URBS_STARTED)) {
+                // Can the urb, don't touch the HW
+                printk(KERN_ERR "%s: ignoring urb %d\n",__FUNCTION__,num_urbs_started);
+                return -1;
+        }
+        
+        /* Check pid, setup pid and out flags
+         */
+        switch (pid) {
+        case USB_PID_SETUP:
+                pid = ETD_DIRPID_SETUP;
+                out = TRUE;
+                break;
+        case USB_PID_OUT:
+                pid = ETD_DIRPID_OUT;
+                out = TRUE;
+                break;
+        case USB_PID_IN:
+                pid = ETD_DIRPID_IN;
+                out = FALSE;
+                break;
+        default:
+                TRACE_MSG1(HCD,"invalid PID %x",pid);
+                return -1;
+        }
+        switch (format) {
+        case PIPE_CONTROL:
+        case PIPE_BULK:
+        case PIPE_INTERRUPT:
+                td.w3.val = (ETD_SET_BUFSIZE(sizeof(fs_data_buff)-1) | ETD_SET_TOTBYECNT(len));
+                break;
+        default:
+                return -1;
+        }
+
+        /*
+         * 1. Allocate resources:
+         *    a. X,Y buffers
+         *    b. ETD (& matching DMA)
+         * 2. Set the registers
+         */
+        UNLESS ((x = get_data_buff(fs_hcpriv)) && (y = get_data_buff(fs_hcpriv)) && (0 <= (etdn = get_etd(fs_hcpriv)))) {
+                // Resource allocation failure, retry later is OK.
+                TRACE_MSG0(HCD,"resource allocation failure -> resubmit OK");
+                y = rel_data_buff(fs_hcpriv,y);
+                x = rel_data_buff(fs_hcpriv,x);
+                return -2;
+        }
+
+        // OK, we've got all we need.
+        td.w1.bufsrtad.y = data_buff_boff(y);
+        td.w1.bufsrtad.x = data_buff_boff(x);
+        
+        switch (format) {
+        case PIPE_CONTROL:
+                fmt = ETD_FORMAT_CONTROL;
+                dir = ETD_DIRECT_TD00;
+
+                /* build setup data/status phase ETD for use after completion of 8 byte setup packet
+                 */
+                fs_hcpriv->sdp_data[etdn] = data; // address of data phase buffer.
+                data = (void *) other;   // address of setup packet.
+                sdp = &fs_hcpriv->sdp_etd[etdn];
+                memset((void*)sdp, 0, sizeof(endpoint_transfer_descriptor));
+
+                /* toggle is constant 0 for setup packet, and 1 for data and status phases,
+                 * so toggle was overloaded to handle data phase direction.  If len == 0, there
+                 * is no data phase, so direction is IN. 
+                 */
+                sdp_out = (toggle && len > 0);
+                sdp->td.w1.val = td.w1.val;
+                sdp->td.w2.cb.flags = ETD_SET_DATATOGL((0x2|1)) |  // toggle is always 1, for data or status phases.
+                        ETD_SET_DELAYINT(0) |           // when done, interrupt ASAP (wait for 0 frames)
+                        ETD_SET_BUFROUND(1) |           // Allow short recv xfers
+                        ETD_SET_DIRPID((sdp_out?ETD_DIRPID_OUT:ETD_DIRPID_IN));
+                sdp->td.w2.cb.reserved = 0;
+                sdp->td.w2.cb.rtrydelay = 1;            // Number of frames to wait before retry on failure.
+                sdp->td.w3.val = ETD_SET_BUFSIZE(sizeof(fs_data_buff)-1) | ETD_SET_TOTBYECNT(len);
+
+                /* build SETUP ETD for initial 8 byte setup packet
+                 */
+                len = 8;
+                td.w2.cb.flags = ETD_SET_DATATOGL((0x2|0)) |  // starting toggle is here, not TOGCRY, and is always 0.
+                        ETD_SET_DELAYINT(0) |           // when done, interrupt ASAP (wait for 0 frames)
+                        ETD_SET_BUFROUND(1) |           // Allow short recv xfers
+                        ETD_SET_DIRPID(pid);
+                td.w2.cb.reserved = 0;
+                td.w2.cb.rtrydelay = 1;                 // Number of frames to wait before retry on failure.
+                td.w3.val = (ETD_SET_BUFSIZE(sizeof(fs_data_buff)-1) | ETD_SET_TOTBYECNT(len));
+
+                fs_hcpriv->etd_urb_state[etdn] = ETD_URB_SETUP_START;
+        
+                //TRACE_MSG3(HCD,"SETUP pkt, len=%d : %08x : %08x",len,*((u32*)(void*)data),*(1+(u32*)(void*)data));
+                
+                /* Complete the setup data phase ETD.
+                 */
+                sdp->epd = ETD_SET_MAXPKTSIZ(maxps) |
+                        ETD_SET_TOGCRY(0) |
+                        ETD_SET_FORMAT(ETD_FORMAT_BULK) |
+                        ETD_SET_SPEED((slow?ETD_SPEED_LOW:ETD_SPEED_FULL)) |
+                        ETD_SET_DIRECT(ETD_DIRECT_TD00) |
+                        ETD_SET_ENDPNT(endpoint) |
+                        ETD_SET_ADDRESS(address);
+       
+                break;
+
+        case PIPE_BULK:
+                fmt = ETD_FORMAT_BULK;
+                dir = ETD_DIRECT_TD00;
+
+                //TRACE_MSG1(HCD,"BULK, starting toggle %x",toggle);
+                td.w2.cb.flags = ETD_SET_DATATOGL((0x2|toggle)) |  // starting toggle is here, not TOGCRY
+                        ETD_SET_DELAYINT(0) |           // when done, interrupt ASAP (wait for 0 frames)
+                        ETD_SET_BUFROUND(1) |           // Allow short recv xfers
+                        ETD_SET_DIRPID(pid);
+                td.w2.cb.reserved = 0;
+                td.w2.cb.rtrydelay = 0;                 // Number of frames to wait before retry on failure.
+
+                /* Set the state to indicate if a trailing ZLP is required.
+                 */
+                fs_hcpriv->etd_urb_state[etdn] = ((other && out && 0 == 
+                                        (len % maxps)) ? ETD_URB_BULKWZLP_START : ETD_URB_BULK_START);
+                break;
+
+        case PIPE_INTERRUPT:
+                fmt = ETD_FORMAT_INTERRUPT;
+                dir = ETD_DIRECT_TD00;
+
+                //TRACE_MSG1(HCD,"INTERRUPT, starting toggle %x",toggle);
+                td.w2.intr.flags = ETD_SET_DATATOGL((0x2|toggle)) |  // starting toggle is here, not TOGCRY
+                        ETD_SET_DELAYINT(0) |           // when done, interrupt ASAP (wait for 0 frames)
+                        ETD_SET_BUFROUND(1) |           // Allow short recv xfers
+                        ETD_SET_DIRPID(pid);
+
+                td.w2.intr.relpolpos = (fs_rl(OTG_HOST_FRM_NUM) + 1) & 0xff;  // Start next frame
+
+                td.w2.intr.polinterv = other & 0xff;
+                fs_hcpriv->etd_urb_state[etdn] = ETD_URB_INTERRUPT_START;
+                break;
+
+        case PIPE_ISOCHRONOUS:
+                fmt = ETD_FORMAT_ISOC;
+                dir = out ? ETD_DIRECT_OUT : ETD_DIRECT_IN;
+                td.w2.isoc.startfrm = (fs_rl(OTG_HOST_FRM_NUM) + 1) & 0xffff;  // next frame
+                if (len > 1023) {
+                        // Two frames needed, send 1023 in the first, remainder in the second.
+                        td.w2.isoc.flags = ETD_SET_DELAYINT(0) | ETD_SET_AUTOISO(0) | ETD_SET_FRAMECNT(1);
+                        td.w3.isoc.pkt0 = ETD_SET_PKTLEN(1023);
+                        td.w3.isoc.pkt1 = ETD_SET_PKTLEN(len-1023);
+                } 
+                else {
+                        // Only one frame needed, send it all at once.
+                        td.w2.isoc.flags = ETD_SET_DELAYINT(0) | ETD_SET_AUTOISO(0) | ETD_SET_FRAMECNT(0);
+                        td.w3.isoc.pkt0 = ETD_SET_PKTLEN(len);
+                        td.w3.isoc.pkt1 = 0;
+                }
+                fs_hcpriv->etd_urb_state[etdn] = ETD_URB_ISOC_START;
+                break;
+        }
+
+        fs_wl(etd_word(etdn, 0), ETD_SET_MAXPKTSIZ(maxps) | ETD_SET_TOGCRY(0) | ETD_SET_FORMAT(fmt) |
+                        ETD_SET_SPEED((slow ? ETD_SPEED_LOW : ETD_SPEED_FULL)) |
+                        ETD_SET_DIRECT(dir) | ETD_SET_ENDPNT(endpoint) | ETD_SET_ADDRESS(address));
+
+        fs_wl(etd_word(etdn, 1), td.w1.val);
+        fs_wl(etd_word(etdn, 2), td.w2.val);
+        fs_wl(etd_word(etdn, 3), td.w3.val);
+
+        //TRACE_MSG5(HCD, "ETD[%02d] %08x %08x %08x %08x", etdn,
+        //                fs_rl(etd_word(etdn, 0)), fs_rl(etd_word(etdn, 1)),
+        //                fs_rl(etd_word(etdn, 2)), fs_rl(etd_word(etdn, 3)));
+
+        start_etd(etdn, len, data, out);
+        return etdn;
+}
+
+/*!
+ * finish_urb_irq() - called to complete transfer
+ * @param fs_hcpriv
+ * @param etdn
+ * @param killed
+ */
+STATIC void finish_urb_irq(fs_hcpriv *fs_hcpriv, int etdn, int killed)
+{
+        int cc = 0;
+        int next_toggle = 0;
+        int format = 0;
+        u32 remaining = 0;
+        fs_data_buff *x,*y;
+        endpoint_transfer_descriptor res;
+
+        TRACE_MSG5(HCD, "etdn=%d state=%d %s nus=%u %s", etdn, fs_hcpriv->etd_urb_state[etdn], 
+                        etd_urb_state_name[fs_hcpriv->etd_urb_state[etdn]], num_urbs_started, (killed?"KILLED":""));
+        
+        if (monitoring_etd_mask == ETD_MASK(etdn)) 
+                monitoring_etd_mask = 0;
+        
+
+        /* Disable/Abort DMA.  R1: sec 23.13.16 pg 23-103
+         * (supposed to auto-clear on DMA completion, but this shouldn't hurt). 
+         */
+        fs_wl(OTG_DMA_ETD_CH_CLR, ETD_MASK(etdn));
+
+        if (fs_rl(OTG_HOST_ETD_EN) & ETD_MASK(etdn)) 
+                fs_andl(OTG_HOST_ETD_EN, ~ETD_MASK(etdn));                // This register IS write 0 to disable.  Really.
+
+        if ( fs_rl(OTG_HOST_ETD_DONE) & ETD_MASK(etdn)) 
+                fs_andl(OTG_HOST_ETD_DONE, ~ETD_MASK(etdn));              // This register IS write 0 to disable.  Really.
+        
+                                                        // Hardware should have disabled ETD, but it doesn't hurt to check.
+        if (fs_rl(OTG_HOST_ETD_EN) & ETD_MASK(etdn)) 
+                fs_wl(OTG_HOST_ETD_EN, ETD_MASK(etdn));                   // This register IS write 1 to clear.  Really.
+
+        if (fs_rl(OTG_HOST_XINT_STAT) & ETD_MASK(etdn)) 
+                fs_wl(OTG_HOST_XINT_STAT, ETD_MASK(etdn));                // This register IS write 1 to clear.  Really.
+        
+        if (fs_rl(OTG_HOST_YINT_STAT) & ETD_MASK(etdn)) 
+                fs_wl(OTG_HOST_YINT_STAT, ETD_MASK(etdn));                // This register should be write 1 to clear.
+        
+
+        if (fs_rl(OTG_HOST_XYINT_STEN) & ETD_MASK(etdn)) 
+                fs_andl(OTG_HOST_XYINT_STEN, ~ETD_MASK(etdn));            // This register should be write 0 to disable.
+        
+        if (fs_rl(OTG_HOST_XFILL_STAT) & ETD_MASK(etdn)) 
+                fs_wl(OTG_HOST_XFILL_STAT, ETD_MASK(etdn));               // This register IS write 1 to clear.  Really.
+        
+        if (fs_rl(OTG_HOST_YFILL_STAT) & ETD_MASK(etdn)) 
+                fs_wl(OTG_HOST_YFILL_STAT, ETD_MASK(etdn));               // This register should be write 1 to clear.
+        
+
+        // Extract results
+        res.epd = fs_rl(etd_word(etdn, 0));
+        res.td.w1.val = fs_rl(etd_word(etdn, 1));
+        res.td.w2.val = fs_rl(etd_word(etdn, 2));
+        res.td.w3.val = fs_rl(etd_word(etdn, 3));
+
+        fs_wl(OTG_HOST_EP_DSTAT, ETD_MASK(etdn));
+
+        //TRACE_MSG1(HCD,"OTG_HOST_ETD_DONE: %08x", fs_rl(OTG_HOST_ETD_DONE));
+        
+        if ((ETD_URB_BULK_START == fs_hcpriv->etd_urb_state[etdn] || 
+                                (ETD_URB_BULKWZLP_START == fs_hcpriv->etd_urb_state[etdn])) &&
+                        (ETD_GET_DIRPID(res.td.w2.cb.flags) != ETD_DIRPID_IN)
+            ) 
+                TRACE_MSG4(HCD,"RES: epd: %08x w1: %08x w2: %08x w3: %08x", res.epd,res.td.w1.val,res.td.w2.val,res.td.w3.val);
+        
+        if (ETD_GET_HALTED(res.epd)) 
+                TRACE_MSG0(HCD,"endpoint HALTED");
+        
+        format = ETD_GET_FORMAT(res.epd);
+
+        switch (fs_hcpriv->etd_urb_state[etdn]) {
+        case ETD_URB_SETUP_START:
+                /* Just finished the setup packet.
+                 * Go to data or status phase of setup packet while we still have the ETD and data buffers allocated.
+                 */
+                if (!killed && ETD_CC_NOERROR == (cc = ETD_GET_COMPCODE(res.td.w2.cb.flags))) {
+
+                        endpoint_transfer_descriptor *sdp = &fs_hcpriv->sdp_etd[etdn];
+                        int len = ETD_GET_TOTBYECNT(sdp->td.w3.val);
+                        void *data = fs_hcpriv->sdp_data[etdn];
+                        fs_hcpriv->etd_urb_state[etdn] = ((len > 0) ? ETD_URB_SETUP_DATA : ETD_URB_SETUP_STATUS);
+
+                        // Copy into the active ETD and start it.
+                        fs_wl(etd_word(etdn, 0), sdp->epd);
+                        fs_wl(etd_word(etdn, 1), sdp->td.w1.val);
+                        fs_wl(etd_word(etdn, 2), sdp->td.w2.val);
+                        fs_wl(etd_word(etdn, 3), sdp->td.w3.val);
+                                                                                        // watch out for ETD_DIRPID_SETUP
+                        start_etd(etdn, len,data,(ETD_GET_DIRPID(sdp->td.w2.cb.flags)!=ETD_DIRPID_IN)); 
+                        TRACE_MSG1(HCD,"SETUP %s Phase started",((len > 0) ? "DATA" : "STATUS"));
+                        return;
+                }
+                /* Something is wrong, finish the URB as is.
+                 */
+                remaining = ETD_GET_TOTBYECNT(fs_rl(etd_word(etdn, 3)));
+                format = PIPE_CONTROL;
+                break;
+        
+        case ETD_URB_SETUP_DATA:
+                /* Go to status phase of setup packet while we still have the ETD and data buffers allocated.
+                 * Save the data phase length for after status.
+                 */
+                if (!killed && ETD_CC_NOERROR == (cc = ETD_GET_COMPCODE(res.td.w2.cb.flags))) {
+                        endpoint_transfer_descriptor *sdp = &fs_hcpriv->sdp_etd[etdn];
+                        fs_hcpriv->sdp_data[etdn] = (void *) ETD_GET_TOTBYECNT(res.td.w3.val); 
+                        fs_hcpriv->etd_urb_state[etdn] = ETD_URB_SETUP_STATUS;
+
+                        /* Rebuild data phase sdp for status phase with opposite direction, and 0 length. 
+                         */
+                        
+                        sdp->td.w2.cb.flags = ETD_FLIP_DIRPID(sdp->td.w2.cb.flags);
+                        sdp->td.w3.val = (ETD_SET_BUFSIZE(sizeof(fs_data_buff)-1) | ETD_SET_TOTBYECNT(0));
+
+                        /* Copy into the active ETD and start it.
+                         */
+                        fs_wl(etd_word(etdn, 0), sdp->epd);
+                        fs_wl(etd_word(etdn, 1), sdp->td.w1.val);
+                        fs_wl(etd_word(etdn, 2), sdp->td.w2.val);
+                        fs_wl(etd_word(etdn, 3), sdp->td.w3.val);
+                        start_etd(etdn, 0,NULL,(ETD_GET_DIRPID(sdp->td.w2.cb.flags)!=ETD_DIRPID_IN));
+                        TRACE_MSG0(HCD,"SETUP STATUS Phase started");
+                        return;
+                }
+                // Something is wrong, finish the URB as is.
+                remaining = ETD_GET_TOTBYECNT(res.td.w3.val);
+                format = PIPE_CONTROL;
+                break;
+
+        case ETD_URB_SETUP_STATUS:
+                cc = ETD_GET_COMPCODE(res.td.w2.cb.flags);
+                // Fetch saved length.
+                remaining = (u32) fs_hcpriv->sdp_data[etdn];
+                format = PIPE_CONTROL;
+                break;
+
+        case ETD_URB_BULKWZLP_START:
+                // Need a trailing ZLP.
+                next_toggle = ETD_GET_TOGCRY(res.epd);
+                if (!killed && ETD_CC_NOERROR == ETD_GET_COMPCODE(res.td.w2.cb.flags)) {
+                        // Since TOTBYECNT should be 0, re-enabling the same ETD ought to give a ZLP.
+                        fs_hcpriv->etd_urb_state[etdn] = ETD_URB_BULKWZLP;
+                                                                                // Save the data phase length for after ZLP.
+                        // fs_hcpriv->sdp_data[etdn] = (void *) ETD_GET_TOTBYECNT(res.td.w3.val); 
+                        // FIXME - verify toggle OK
+                        
+                        start_etd(etdn, 0,NULL,TRUE/*always OUT*/);
+
+                        TRACE_MSG0(HCD,"ZLP started");
+                        return;
+                }
+                // Something is wrong, finish the URB as is.
+                remaining = ETD_GET_TOTBYECNT(res.td.w3.val);
+                format = PIPE_BULK;
+                break;
+
+        case ETD_URB_BULKWZLP:
+                // Trailing ZLP now finished.
+                cc = ETD_GET_COMPCODE(res.td.w2.cb.flags);
+                next_toggle = ETD_GET_TOGCRY(res.epd);
+                // Fetch saved length (it was zero, or we wouldn't be here).
+                remaining = 0;
+                format = PIPE_BULK;
+                break;
+
+        case ETD_URB_BULK_START:
+                cc = ETD_GET_COMPCODE(res.td.w2.cb.flags);
+                next_toggle = ETD_GET_TOGCRY(res.epd);
+                remaining = ETD_GET_TOTBYECNT(res.td.w3.val);
+                format = PIPE_BULK;
+                break;
+
+        case ETD_URB_INTERRUPT_START:
+                cc = ETD_GET_COMPCODE(res.td.w2.intr.flags);
+                remaining = ETD_GET_TOTBYECNT(res.td.w3.val);
+                format = PIPE_INTERRUPT;
+                break;
+
+        case ETD_URB_ISOC_START:
+                // This probably needs more work, best guess only here.
+                remaining = ETD_GET_PKTLEN(res.td.w3.isoc.pkt0);
+                cc = ETD_GET_COMPCODE(res.td.w3.isoc.pkt0);
+                if (ETD_GET_FRAMECNT(res.td.w2.isoc.flags)) {
+                        // There were two packets in this transfer
+                        remaining += ETD_GET_PKTLEN(res.td.w3.isoc.pkt1);
+                }
+                format = PIPE_ISOCHRONOUS;
+        }
+
+        fs_hcpriv->etd_urb_state[etdn] = ETD_URB_COMPLETED;
+
+        /* Convert completion code to HW independent value.
+         */
+        if (killed) {
+                cc = -ENOENT;
+                remaining = 0;
+        }
+        else 
+                cc = comp_code_to_status(cc);
+        
+        /* Return ETD and X,Y buffers to free list.
+         */
+        y = data_buff_addr(res.td.w1.bufsrtad.y);
+        x = data_buff_addr(res.td.w1.bufsrtad.x);
+        y = rel_data_buff(fs_hcpriv,y);
+        x = rel_data_buff(fs_hcpriv,x);
+        rel_etd(fs_hcpriv, etdn);
+
+        /* Forward results to the layers above.
+         */
+        //TRACE_MSG4(HCD,"completing urb on ETD %d cc=%d remaining=%d next_toggle=%d", etdn, cc, remaining,next_toggle);
+        hcd_transfer_complete(fs_hcpriv->bus_hcpriv, etdn, format, cc, remaining, next_toggle);
+}
+
+/*!
+ * hcd_hw_unlink_urb() - unlink urb
+ * Called in irq_lock
+ * @param bus_hcpriv
+ * @param u
+ */
+int hcd_hw_unlink_urb(struct bus_hcpriv *bus_hcpriv, int u)
+{
+        unsigned long flags;
+        struct fs_hcpriv *fs_hcpriv;
+        RETURN_EINVAL_UNLESS ((fs_hcpriv = (struct fs_hcpriv *) bus_hcpriv->hw_hci));
+        local_irq_save(flags);
+        finish_urb_irq(fs_hcpriv, u, TRUE);
+        local_irq_restore(flags);
+        return 0;
+}
+
+/*!
+ * hcd_hw_frame_number() - return current frame number
+ * @param bus_hcpriv
+ * @return u32 ???
+ */
+u32 hcd_hw_frame_number(struct bus_hcpriv *bus_hcpriv)
+{
+        return(fs_rl(OTG_HOST_FRM_NUM));
+}
+
+/* ********************************************************************************************* */
+
+/*!
+ * MX21_PORT_CHANGED() - send otg event information to state machine
+ * @param name
+ * @param cs
+ * @param changed
+ * @param status
+ * @param set
+ * @param reset
+ */
+void MX21_PORT_CHANGED(char *name, u32 cs, u32 changed, u32 status, u64 set, u64 reset)
+{
+        RETURN_UNLESS(cs & changed);
+        TRACE_MSG2(HCD, "%s%s", name, (cs & (1 << status)) ? "" : "/");
+        if (cs & (1 << status) && set)
+                otg_event(hcd_instance->otg, set, HCD, name);
+        if (!(cs & (1 << status)) && reset)
+                otg_event(hcd_instance->otg, reset, HCD, name);
+}
+
+/*!
+ * update_shadow_rh() - Update shadow _status_ info (change info already updated).
+ * @param bus_hcpriv
+ * @param port_num - 1-N based port number
+ * @param cs
+ *
+ */
+STATIC void update_shadow_rh(struct bus_hcpriv *bus_hcpriv, int port_num, u32 cs)
+{
+        u32 msk_and = 0;  // bits to turn off
+        u32 msk_or = 0;   // bits to turn on
+
+        TRACE_MSG2(HCD,"port: %d cs: %08x", port_num, cs);
+
+        /* PORT_STATUS_PRTRSTSC (20) - PORT_RESET (4) changed
+         * PORT_RESET (4) status change, end of reset, PORT_ENABLE may (should) be on.
+         */
+        MX21_PORT_CHANGED("PORT_RESET", cs, PORT_STATUS_PRTRSTSC, PORT_RESET, 
+                        BUS_RESET, BUS_RESET_);
+
+        if (cs & (1 << C_PORT_RESET)) {
+                TRACE_MSG1(HCD,"port: %d PORT_RESET complete (s.b. enabled)", port_num);
+                if (cs & (1 << PORT_ENABLE)) {
+                        TRACE_MSG1(HCD,"port: %d enabled", port_num);
+                        //otg_write_info_message("port update C_PORT_RESET");
+                        msk_or |= (1 << PORT_ENABLE);
+                }
+                else {
+                        TRACE_MSG1(HCD,"port: %d NOT enabled!!!", port_num);
+                        //otg_write_info_message("update C_PORT_RESET/");
+                }
+
+                /* Clear the PORT_RESET bit too, since it's over.
+                 */
+                msk_and |= (1 << PORT_RESET);
+                bus_hcpriv->rh_hcpriv->hub_port_change_status |= (1 << port_num);
+        }
+
+        /* PORT_OVER_CURRENT (3) bit has changed (either direction).
+         * PORT_STATUS_OVRCURIC (19) - PORT_OVER_CURRENT (3) changed
+         */
+        MX21_PORT_CHANGED("PORT_OVER_CURRENT", cs, PORT_STATUS_OVRCURIC, PORT_OVER_CURRENT, (u64) 0, (u64) 0);
+        if (cs & (1 << C_PORT_OVER_CURRENT)) {
+                /* OVER_CURRENT has come on.
+                 */
+                if (cs & (1 << PORT_OVER_CURRENT)) {
+                        TRACE_MSG1(HCD,"port: %d over current ON", port_num);
+                        msk_or |= (1 << PORT_OVER_CURRENT);
+                } 
+                /* OVER_CURRENT has gone away.
+                 */
+                else {
+                        TRACE_MSG1(HCD,"port: %d over current OFF", port_num);
+                        msk_and |= (1 << PORT_OVER_CURRENT);
+                }
+                bus_hcpriv->rh_hcpriv->hub_port_change_status |= (1 << port_num);
+        }
+
+        /* PORT_SUSPEND (2) Resume sequence has completed....
+         * PORT_STATUS_PRTSTATSC (18) - PORT_SUSPEND (2) changed
+         */
+        MX21_PORT_CHANGED("PORT_SUSPEND", cs, PORT_STATUS_PRTSTATSC, PORT_SUSPEND, 
+                        BUS_SUSPENDED, BUS_SUSPENDED_);
+
+        if (cs & (1 << C_PORT_SUSPEND)) {
+                // FIXME - figure this out when we get to suspend/resume....
+
+        }
+
+
+        /* PORT_ENABLE (1) has been __cleared__ by some HW event
+         * PORT_STATUS_PRTENBLSC (17) - PORT_ENABLE (1) changed
+         */
+        MX21_PORT_CHANGED("PORT_ENABLE", cs, PORT_STATUS_PRTENBLSC, PORT_ENABLE, (u64) 0, (u64) 0);
+        if (cs & (1 << C_PORT_ENABLE)) {
+                //otg_write_info_message("update C_PORT_ENABLE");
+                TRACE_MSG1(HCD,"port: %d disabled (by HW)", port_num);
+                if (!(cs & (1 << PORT_ENABLE))) 
+                        msk_and |= ( 1 << PORT_ENABLE);
+                bus_hcpriv->rh_hcpriv->hub_port_change_status |= (1 << port_num);
+        }
+
+        /* PORT_CONNECTION (0) bit has changed (either direction).
+         * PORT_STATUS_CONNECTSC (16) - PORT_CONNECTION (0) changed
+         */
+        MX21_PORT_CHANGED("PORT_CONNECTION", cs, PORT_STATUS_CONNECTSC, PORT_CONNECTION, 
+                        HUB_PORT_CONNECT, HUB_PORT_CONNECT_ );
+
+        if (cs & (1 << C_PORT_CONNECTION)) {
+                /* CONNECT has come on.
+                 */
+                if (cs & (1 << PORT_CONNECTION)) {
+                        TRACE_MSG1(HCD,"port: %d new connection", port_num);
+                        msk_or |= (1 << PORT_CONNECTION);
+                        if (cs & (1 << PORT_LOW_SPEED)) {
+                                TRACE_MSG1(HCD,"port: %d low speed device", port_num);
+                                msk_or |= (1 << PORT_LOW_SPEED);
+                        }
+                        //otg_write_info_message("update PORT_CONNECTION");
+                        //printk(KERN_INFO"%s: AAA\n", __FUNCTION__);
+                        //otg_event(hcd_instance->otg, HUB_PORT_CONNECT, HCD, "HUB_PORT_CONNECT (mx21 hw)");
+                        //printk(KERN_INFO"%s: BBB\n", __FUNCTION__);
+                } 
+                /* CONNECT has gone away.
+                 */
+                else {
+                        TRACE_MSG1(HCD,"port: %d disconnection", port_num);
+                        //otg_write_info_message("update PORT_CONNECTION/");
+                        msk_and |= (1 << PORT_CONNECTION) | (1 << PORT_LOW_SPEED);
+                        //otg_event(hcd_instance->otg, HUB_PORT_CONNECT_, HCD, "HUB_PORT_CONNECT/ (mx21 hw)");
+                }
+                bus_hcpriv->rh_hcpriv->hub_port_change_status |= (1 << port_num);
+                //printk(KERN_INFO"%s: CCC\n", __FUNCTION__);
+        }
+
+        TRACE_MSG5(HCD, "port: %d shadow port_change_status: ((%08x & ~%04x) | %04x) hpcs: %04x",
+                        port_num, bus_hcpriv->rh_hcpriv->port_change_status[port_num - 1], 
+                        msk_and, msk_or, bus_hcpriv->rh_hcpriv->hub_port_change_status);
+}
+
+/* ********************************************************************************************* */
+static int num_host_interrupts = 0;
+#define MAX_HOST_INTERRUPTS  0
+static int num_psc_interrupts = 0;
+#define MAX_PSC_INTERRUPTS  0
+
+extern struct bus_hcpriv fs_bus_hcpriv;
+
+/*!
+ * hcd_hw_int_hndlr() - interrupt handler for hcd controller
+ * @param irq
+ * @param dev_id
+ * @param regs
+ */
+irqreturn_t hcd_hw_int_hndlr(int irq, void *dev_id, struct pt_regs *regs)
+{
+        struct bus_hcpriv *bus_hcpriv = (struct bus_hcpriv *) &fs_bus_hcpriv;
+        struct fs_hcpriv *fs_hcpriv = (struct fs_hcpriv *) bus_hcpriv->hw_hci;
+        u32 host_sint;                                  // C.f. 23.11.11 Host Interrupt Register
+
+        if (OTG_USBDMA == irq) {
+                // HOST DMA error interrupt
+                u32 err_stat = fs_rl(OTG_DMA_ETD_ERR);
+                TRACE_MSG1(HCD,"host DMA interrupt, error status %08x",err_stat);
+                if (0 != err_stat) {
+                        // Disable the matching DMA channels and clear the interrupt
+                        fs_wl(OTG_DMA_ETD_CH_CLR, err_stat);
+                        fs_wl(OTG_DMA_ETD_ERR, err_stat);
+                }
+                // local_irq_restore(flags);
+                return IRQ_HANDLED;
+        }
+
+        /* FIXME - should check fs_hcpriv->mm->otg.OTG_Module_Interrupt_Status & (0x1 << 3) | 0x1;
+         * for Host (async + regular) Interrupt - enable clock on async. 
+         */
+
+        num_host_interrupts += 1;
+        if (MAX_HOST_INTERRUPTS > 0 && num_host_interrupts > MAX_HOST_INTERRUPTS) {
+                // Host (async + regular) Interrupt Disable
+                
+                fs_wl(OTG_HOST_SINT_STEN, 0);
+                fs_andl(OTG_CORE_CINT_STEN, ~(/*(0x1 << 3) |*/ 0x1));  
+        }
+
+        while ((host_sint = fs_rl(OTG_HOST_SINT_STAT))) {
+
+                while (HOST_DONEINT & host_sint) {
+                        u32 etds_done;
+
+                        TRACE_MSG1(HCD,"HOST_DONEINT: %08x", fs_rl(OTG_HOST_EP_DSTAT));
+                        fs_wl(OTG_HOST_SINT_STAT, HOST_DONEINT);
+                        host_sint = fs_rl(OTG_HOST_SINT_STAT);
+                        while ((etds_done = fs_rl(OTG_HOST_EP_DSTAT))) 
+                                finish_urb_irq(fs_hcpriv, fls(etds_done) - 1, FALSE);
+                }
+
+                host_sint = fs_rl(OTG_HOST_SINT_STAT);
+
+                /* Start Of Frame.
+                 * Note: this interrupt seems to happen even if disabled when there is a frame number overflow.
+                 */
+                if (host_sint & HOST_SOFINT) {
+                        if (0 == (fs_hcpriv->sof_count & (4096 - 1))) {
+                                TRACE_MSG1(HCD,"SOF %08lx",fs_hcpriv->sof_count);
+                        }
+                        fs_hcpriv->sof_count += 1;
+                        if (monitoring_etd_mask != 0) {
+                                // This ETD needs to be watched....
+                                current_etd_frame += 1;
+                                
+                                done_flags[current_etd_frame] = fs_rl(OTG_HOST_ETD_EN);
+                                done_status[current_etd_frame] = fs_rl(OTG_HOST_EP_DSTAT);
+
+                                mon_etd[current_etd_frame].epd = fs_rl(etd_word(monitoring_etd_num, 0));
+                                mon_etd[current_etd_frame].td.w1.val = fs_rl(etd_word(monitoring_etd_num, 1));
+                                mon_etd[current_etd_frame].td.w2.val = fs_rl(etd_word(monitoring_etd_num, 2));
+                                mon_etd[current_etd_frame].td.w3.val = fs_rl(etd_word(monitoring_etd_num, 3));
+
+                                if (current_etd_frame >= MAX_ETD_FRAMES) {
+                                        // This has taken too long!
+                                        int i,u;
+                                        u = ((mon_etd[0].td.w3.val == mon_etd[1].td.w3.val) &&
+                                             (mon_etd[1].td.w3.val == mon_etd[2].td.w3.val) &&
+                                             (mon_etd[2].td.w3.val == mon_etd[3].td.w3.val));
+
+                                        TRACE_MSG5(HCD,"SOF: OUT urb TIMEOUT at %d/%d frames, mask: %08x num=%u u=%d",
+                                                  current_etd_frame,MAX_ETD_FRAMES,monitoring_etd_mask,monitoring_etd_num,u);
+
+                                        for (i = 0; i <= MAX_ETD_FRAMES; i++) 
+                                                TRACE_MSG7(HCD,"SOF: %d epd: %08x w1: %08x w2: %08x w3: %08x ef: %08x st: %08x",
+                                                                i, mon_etd[i]. epd, mon_etd[i]. td.w1.val, mon_etd[i].td.w2.val,
+                                                                mon_etd[i].td.w3.val, done_flags[i], done_status[i]);
+                                        // Disable monitoring by killing the urb.  FUTURE, look into restarting it.
+                                        finish_urb_irq(fs_hcpriv, monitoring_etd_num,TRUE);
+                                }
+                        }
+                }
+
+                /* Frame Number Overflow.
+                 */
+                if (host_sint & HOST_FMOFINT) {
+                        //TRACE_MSG0(HCD,"Frame Number Overflow");
+                        //printk(KERN_ERR "%s: Frame Number Overflow\n",__FUNCTION__);
+                }
+
+                /* Port Status Change.
+                 */
+                if (host_sint & HOST_PSCINT) {
+                        int port_num;
+                        num_psc_interrupts += 1;
+
+                        /* Clear the interrupt by clearing the port(s) in question.
+                         * Reflect any changes in the shadow values. 
+                         */
+                        for (port_num = 1; port_num <= bus_hcpriv->num_ports; port_num++) {
+
+                                u32 cs = fs_rl(fs_host_port_stat(port_num));  
+                                u32 cm = cs & 0xFFFF0000;
+
+                                CONTINUE_UNLESS (cm);
+                                fs_wl(fs_host_port_stat(port_num), cm);
+                                bus_hcpriv->rh_hcpriv->port_change_status[port_num - 1] |= cm;
+                                update_shadow_rh(bus_hcpriv, port_num, cs);
+                        }
+
+                        /* Tell the RH to scan for changes in shadow data. (schedule bottom half handler)
+                         */
+                        hcd_rh_int_hndlr(irq,bus_hcpriv,NULL);
+                }
+
+                /* Overrun
+                 */
+                if (host_sint & (HOST_SORINT | HOST_HERRINT)) {
+                        if (host_sint & HOST_SORINT) {
+                                TRACE_MSG0(HCD,"Scheduling Overrun");
+                                printk(KERN_ERR "%s: Scheduling Overrun\n",__FUNCTION__);
+                        }
+                        if (host_sint & HOST_HERRINT) {
+                                TRACE_MSG0(HCD,"Host Scheduling Error");
+                                printk(KERN_ERR "%s: Host Scheduling Error\n",__FUNCTION__);
+                        }
+                }
+                /* Resume Detected.
+                 */
+                if (host_sint & HOST_RESDETINT) {
+                        TRACE_MSG0(HCD,"Resume Detected");
+                }
+                /* Clear by writing back the ones we've checked for since the last read.
+                 */
+                fs_wl(OTG_HOST_SINT_STAT, host_sint & (HOST_RESDETINT | HOST_FMOFINT | HOST_SORINT | 
+                                HOST_HERRINT | HOST_PSCINT | HOST_SOFINT));
+        }
+        return IRQ_HANDLED;
+}
+
+/* ********************************************************************************************* */
+
+
+/*!
+ * hcd_hw_rh_hub_attributes() - get root hub attributes
+ * These provide access to the real root hub.
+ * @param bus_hcpriv
+ */
+u16 hcd_hw_rh_hub_attributes(struct bus_hcpriv *bus_hcpriv)
+{
+        // R1: sec 23.11.27
+        return 0xFFFF & (fs_rl(OTG_HOST_ROOTHUB_DESCA) >> 8);
+}
+
+/*!
+ * hcd_hw_rh_power_delay() - get power delay attributes
+ * These provide access to the real root hub.
+ * @param bus_hcpriv
+ */
+u8 hcd_hw_rh_power_delay(struct bus_hcpriv *bus_hcpriv)
+{
+        // R1: sec 23.11.27
+        return fs_rl(OTG_HOST_ROOTHUB_DESCA) >> 24;
+}
+
+/*!
+ * hcd_hw_rh_contr_current() - get contr current attributes
+ * These provide access to the real root hub.
+ * @param bus_hcpriv
+ */
+u8 hcd_hw_rh_hub_contr_current(struct bus_hcpriv *bus_hcpriv)
+{
+        return 0x0;
+}
+
+/*!
+ * hcd_hw_rh_DeviceRemovalbe() - get DeviceRemovalbe attributes
+ * These provide access to the real root hub.
+ * @param bus_hcpriv
+ */
+u8 hcd_hw_rh_DeviceRemovable(struct bus_hcpriv *bus_hcpriv)
+{
+        // USB2.0 pg 418 ==> bit 0 reserved, 1..7 for ports
+        // R1: sec 23.11.28
+        return 0xFF & fs_rl(OTG_HOST_ROOTHUB_DESCB);
+}
+
+/*!
+ * hcd_hw_rh_PortPwrCtrlMask() - get DeviceRemovalbe attributes
+ * These provide access to the real root hub.
+ * @param bus_hcpriv
+ */
+u8 hcd_hw_rh_PortPwrCtrlMask(struct bus_hcpriv *bus_hcpriv)
+{
+        // USB2.0 pg 418 ==> all 1s???  QQSV
+        // R1: sec 23.11.28
+        return 0xFF & fs_rl(OTG_HOST_ROOTHUB_DESCB);
+}
+
+
+/*!
+ * hcd_hw_rh_get_hub_change_status() - get change status
+ * These provide access to the real root hub.
+ * @param bus_hcpriv
+ */
+u32 hcd_hw_rh_get_hub_change_status(struct bus_hcpriv *bus_hcpriv)
+{
+        // R1: sec 23.11.29 pg 23-62, USB2.0 11.24.2.6 pg 425
+        return fs_rl(OTG_HOST_ROOTHUB_STATUS & (ROOTHUB_STATUS_LOCPWRSC | ROOTHUB_STATUS_DEVCONWUE |
+                                      ROOTHUB_STATUS_OVRCURI | ROOTHUB_STATUS_LOCPWRS));
+}
+
+/*!
+ * hcd_hw_rh_hub_feature() - get hub feature
+ * These provide access to the real root hub.
+ * @param bus_hcpriv
+ * @param feat_selector
+ * @param set_flag
+ */
+void hcd_hw_rh_hub_feature(struct bus_hcpriv *bus_hcpriv, int feat_selector, int set_flag)
+{
+        if (set_flag) {
+                // SET feature USB2.0 11.24.2.12 pg 434 and USB2.0 11.24.2 tbl 11-17 pg 421
+                // R1: sec 23.11.29 pg 23-62
+                switch (feat_selector) {
+                // case C_HUB_OVER_CURRENT: MX21 doesn't support SW setting the over current feature
+                // case C_HUB_LOCAL_POWER: MX21 doesn't support local power
+                default:
+                        // Error, but ignore.
+                        TRACE_MSG1(HCD,"SET invalid feature %d",feat_selector);
+                        break;
+                }
+        } 
+        else {
+                // CLEAR feature USB2.0 11.24.2.1 pg 422 and USB2.0 11.24.2 tbl 11-17 pg 421
+                // R1: sec 23.11.29 pg 23-62
+                switch (feat_selector) {
+                case C_HUB_OVER_CURRENT:
+                        fs_wl(OTG_HOST_ROOTHUB_STATUS, ROOTHUB_STATUS_OVRCURCHG);
+                        break;
+
+                // case C_HUB_LOCAL_POWER: MX21 doesn't support local power
+                default:
+                        // Error, but ignore.
+                        TRACE_MSG1(HCD,"CLEAR invalid feature %d",feat_selector);
+                        break;
+                }
+        }
+}
+
+/*!
+ * hcd_hw_rh_get_port_change_status() - get port change status
+ * R1: High 16 bits == change, low 16 == status 
+ * Change & Status bits in register match USB2.0 spec on (read).
+ * @param bus_hcpriv
+ * @param wIndex is 1-origin
+ */
+u32 hcd_hw_rh_get_port_change_status(struct bus_hcpriv *bus_hcpriv, int wIndex)
+{
+        u32 port_status = fs_rl(fs_host_port_stat(wIndex));
+
+        TRACE_MSG8(HCD,"port %d cs: %08lx %s %s %s %s %s %s", 
+                        wIndex, port_status,
+                        port_status & PORT_STATUS_CONNECTSC ? "CHANGE" : "", 
+                        port_status & PORT_STATUS_PRTPWRST ? "POWER" : "", 
+                        port_status & PORT_STATUS_PRTRSTST ? "RESET" : "", 
+                        port_status & PORT_STATUS_PRTSUSPST ? "SUSPEND" : "", 
+                        port_status & PORT_STATUS_PRTENABST ? "ENABLE" : "", 
+                        port_status & PORT_STATUS_CURCONST ? "CONNECT" : ""
+                        );
+
+        return fs_rl(fs_host_port_stat(wIndex));
+}
+
+extern char *port_feature_name[];
+
+/*!
+ * hcd_hw_rh_port_feature() - get rh port feature
+ * @param bus_hcpriv
+ * @param wValue is feature selector
+ * @param wIndex is port number (1-N)
+ * @param set_flag
+ */
+void hcd_hw_rh_port_feature(struct bus_hcpriv *bus_hcpriv, u16 wValue, u16 wIndex, int set_flag)
+{
+        char buf[64];
+        sprintf(buf, "port feature %s %s", port_feature_name[wValue], set_flag ? "SET" : "RESET");
+        //otg_write_info_message(buf);
+
+        TRACE_MSG4(HCD, "mx21 %s (%d) %s port_stat: %8x", port_feature_name[wValue], wValue, set_flag ? "SET" : "RESET",
+                        fs_rl(fs_host_port_stat(wIndex)));
+        if (set_flag) {
+                // SET feature
+                switch (wValue) {
+                case PORT_SUSPEND:
+                        otg_event(hcd_instance->otg, BUS_SUSPENDED, HCD, "HUB_PORT_SUSPEND (mx21 hw)");
+                        fs_wl(fs_host_port_stat(wIndex), 1 << wValue);
+                        break;
+                case PORT_RESET:
+                case PORT_POWER:
+                        fs_wl(fs_host_port_stat(wIndex), 1 << wValue);
+                        break;
+                default:
+                        // Error, but ignore.
+                        TRACE_MSG2(HCD,"SET port %d invalid feature %d", wIndex, wValue);
+                        break;
+                }
+        } 
+        else {
+                // CLEAR feature (valid features from USB2.0 11.24.2.2 pg 423).
+                switch (wValue) {
+                case PORT_SUSPEND:              // Cause a Host initiated resume, or no-op if already active.
+                        wValue++;               // yes, really. this clears PORT_SUSPEND 
+                        fs_wl(fs_host_port_stat(wIndex), 1 << wValue);
+                        otg_event(hcd_instance->otg, BUS_SUSPENDED_, HCD, "HUB_PORT_SUSPEND/ (mx21 hw)");
+                        break;
+
+                case PORT_POWER:                // Put port in powered-off state.
+                        wValue++;               // yes, really. this clears PORT_POWER 
+                        fs_wl(fs_host_port_stat(wIndex), 1 << wValue);
+                        break;
+
+                case PORT_ENABLE:               // Disable port.
+                        //wValue--;
+                        break;
+                default:
+                        break;
+                }
+                switch (wValue) {
+                case PORT_POWER:                // Put port in powered-off state.
+                case PORT_SUSPEND:              // Cause a Host initiated resume, or no-op if already active.
+                case PORT_ENABLE:               // Disable port.
+                case PORT_INDICATOR:            // ind_selector gives which indicator to clear
+                case C_PORT_SUSPEND:            // clear the PORT_SUSPEND change bit
+                case C_PORT_CONNECTION:         // clear the PORT_CONNECTION change bit
+                case C_PORT_RESET:              // clear the PORT_RESET change bit
+                case C_PORT_ENABLE:             // clear the PORT_ENABLE change bit
+                case C_PORT_OVER_CURRENT:       // clear the PORT_OVERCURRENT change bit
+                        fs_wl(fs_host_port_stat(wIndex), 1 << wValue);
+                        break;
+                default:
+                        // Error, but ignore.
+                        TRACE_MSG2(HCD,"CLEAR port %d invalid feature %d", wIndex, wValue);
+                        break;
+                }
+        }
+
+        TRACE_MSG3(HCD, "mx21 %s %s port_stat: %8x", port_feature_name[wValue], set_flag ? "SET" : "RESET",
+                        fs_rl(fs_host_port_stat(wIndex)));
+}
+
+/* ********************************************************************************************* */
+/* Hardware Initialization - this is used by the OTG state machine to 
+ * enable and disable the host controller hardware
+ */
+void rh_hcd_en_func(struct otg_instance *otg, u8 flag);
+
+extern void fs_host_clock_on(void);
+extern void fs_host_clock_off(void);
+
+/*!
+ * fs_hcd_en_func() - otg hcd enable output function
+ * @param otg
+ * @param flag
+ */
+void fs_hcd_en_func(struct otg_instance *otg, u8 flag)
+{
+        struct bus_hcpriv *bus_hcpriv = (struct bus_hcpriv *)(((struct hcd_instance *)(otg->hcd))->privdata);
+        fs_hcpriv *fs_hcpriv = bus_hcpriv->hw_hci;
+        int i;
+        u32 hwmode = fs_rl(OTG_CORE_HWMODE);
+        unsigned long flags;
+
+        /* puts OTG capable port into a state where host is enabled */
+
+        fs_andl(OTG_CORE_HWMODE, 0xffffff0f);                  // clear
+
+        switch (flag) {
+        case SET:
+                local_irq_save(flags);
+                printk(KERN_INFO"%s: SET\n", __FUNCTION__);
+                if (hwmode & MODULE_CRECFG_HOST) {
+                        printk(KERN_INFO"%s: warning FUNC STILL SET\n", __FUNCTION__);
+                }
+                //printk(KERN_INFO"%s: SET\n", __FUNCTION__);
+                fs_orl(OTG_CORE_HWMODE, MODULE_CRECFG_HOST);           // set to software hnp
+                local_irq_restore(flags);
+                break;
+        case RESET:
+                local_irq_save(flags);
+                printk(KERN_INFO"%s: RESET\n", __FUNCTION__);
+                fs_andl(OTG_CORE_HWMODE, ~MODULE_CRECFG_HOST);           // set to software hnp
+                local_irq_restore(flags);
+                break;
+        }
+
+        TRACE_MSG1(HCD, "HWMODE: %08x", fs_rl(OTG_CORE_HWMODE));
+}
+
+
+void rh_loc_sof_func(struct otg_instance *otg, u8 flag);
+void rh_loc_suspend_func(struct otg_instance *otg, u8 flag);
+
+/*!
+ * fs_loc_suspend() - otg loc suspend output function
+ * @param otg
+ * @param flag
+ */
+void fs_loc_suspend_func(struct otg_instance *otg, u8 flag)
+{
+        unsigned long flags;
+        switch (flag) {
+        case SET:
+                
+                fs_orl(OTG_HOST_CONTROL, HOST_CONTROL_HCUSBSTE_SUSPEND);
+                break;
+
+        case RESET:
+                break;
+        }
+
+        rh_loc_suspend_func(otg, flag);
+
+        switch (flag) {
+        case SET:
+                break;
+
+        case RESET:
+                local_irq_save(flags);
+                fs_andl(OTG_HOST_CONTROL, ~HOST_CONTROL_HCUSBSTE_SUSPEND);
+                fs_orl(OTG_HOST_CONTROL, HOST_CONTROL_HCUSBSTE_OPERATIONAL);
+                local_irq_restore(flags);
+                break;
+        }
+}
+
+/*!
+ * fs_loc_sof_func() - otg loc sof output function
+ * @param otg
+ * @param flag
+ */
+void fs_loc_sof_func(struct otg_instance *otg, u8 flag)
+{
+        struct bus_hcpriv *bus_hcpriv = (struct bus_hcpriv *)(((struct hcd_instance *)(otg->hcd))->privdata);
+        fs_hcpriv *fs_hcpriv = bus_hcpriv->hw_hci;
+        int i;  
+        u32 mask;
+        unsigned long flags;
+
+        switch (flag) {   
+        case SET:
+                //printk(KERN_INFO"%s: SET\n", __FUNCTION__);
+                //TRACE_MSG0(HCD, "-----------------------------------------");
+
+                local_irq_save(flags);
+
+		/* reset the host core
+		 */
+                fs_wl_clr(HCD, OTG_CORE_RST_CTRL, MODULE_RSTRH | MODULE_RSTHSIE | MODULE_RSTHC);
+                while (fs_rl(OTG_CORE_RST_CTRL));
+		
+                fs_host_clock_on();
+
+                for (i = 0; i < NUM_DATA_BUFFS; i++) 
+                        (void) rel_data_buff(fs_hcpriv, ((fs_data_buff *)OTG_DATA_BASE)+i);
+
+                for (i = 0; i < NUM_ETDS; i++) 
+                        rel_etd(fs_hcpriv,i);
+
+                fs_wl(OTG_HOST_CONTROL, HOST_CONTROL_HCRESET | HOST_CONTROL_RMTWUEN | 
+                                HOST_CONTROL_HCUSBSTE_RESET | HOST_CONTROL_CTLBLKSR_11);
+
+
+                //TRACE_MSG0(HCD, "HW HCD_ENABLE_SET");
+                fs_andl(OTG_CORE_HNP_CSTAT, ~(MODULE_MASTER | MODULE_SLAVE | MODULE_CMPEN | 
+                                        MODULE_BGEN | MODULE_SWAUTORST | MODULE_ABBUSREQ));
+                fs_rl(OTG_CORE_HNP_CSTAT); 
+
+                fs_orl(OTG_CORE_HNP_CSTAT, MODULE_MASTER | MODULE_CMPEN | MODULE_BGEN | MODULE_ABBUSREQ);
+                fs_orl(OTG_CORE_HNP_CSTAT, MODULE_ARMTHNPE | MODULE_BHNPEN); // XXX
+
+                fs_wl(OTG_HOST_CONTROL, HOST_CONTROL_HCUSBSTE_OPERATIONAL);
+
+                //hcd_hw_enable_interrupts(bus_hcpriv);
+
+                mask = (HOST_PSCINT_EN | HOST_FMOFINT_EN | HOST_HERRINT_EN | 
+                                HOST_RESDETINT_EN | /* HOST_SOFINT_EN | */ HOST_DONEINT_EN | HOST_SORINT_EN);
+
+                // R1: sec 23.11.15 pg 23-54
+                fs_rl(OTG_CORE_HNP_CSTAT); 
+                fs_rl(OTG_HOST_CONTROL);
+                fs_rl(OTG_HOST_ROOTHUB_STATUS);
+                fs_rl(OTG_HOST_PORT_STATUS_1);
+                fs_rl(OTG_HOST_PORT_STATUS_2);
+                fs_rl(OTG_HOST_PORT_STATUS_3);
+
+                fs_wl(OTG_HOST_SINT_STEN, mask);
+                fs_rl(OTG_HOST_SINT_STEN);
+                local_irq_restore(flags);
+        }
+
+        rh_loc_sof_func(otg, flag);
+
+        switch (flag) {   
+        case RESET:
+                local_irq_save(flags);
+
+                //printk(KERN_INFO"%s: RESET\n", __FUNCTION__);
+                //TRACE_MSG0(HCD, "HW HCD_ENABLE_RESET");
+
+                //hcd_hw_disable_interrupts(bus_hcpriv);
+                // R1: sec 23.11.15 pg 23-54
+                fs_wl(OTG_HOST_SINT_STAT, 0);
+
+                fs_andl(OTG_HOST_CONTROL, ~HOST_CONTROL_HCUSBSTE_OPERATIONAL);
+
+                // Shut down hardware if not already shut down....
+                //hcd_hw_disable_interrupts(bus_hcpriv);
+
+                fs_host_clock_off();
+                local_irq_restore(flags);
+                break;
+        }
+}
+/* ********************************************************************************************* */
+/* Module init
+ *
+ * Define the global data structures and call the initialization functions required
+ * to start the generic hcd layer and register with the otg core.
+ *
+ */
+
+void rh_suspend_func(struct otg_instance *otg, u8 on);
+void hcd_init_func (struct otg_instance *otg, u8 flag);
+int hcd_probe(struct usb_interface *intf, const struct usb_device_id *id);
+void hcd_disconnect(struct usb_interface *intf);
+
+int fs_mod_init(void);
+#ifdef MODULE
+void fs_mod_exit(void);
+#endif
+
+#if !defined(OTG_C99)
+
+static struct usb_driver mx2_usb_driver;
+fs_hcpriv priv_mx21;
+struct bus_hcpriv fs_bus_hcpriv;
+struct hcd_ops hcd_ops;
+
+
+/*!
+ * fs_hcd_global_init() - initialize global vars for non C99 systems
+ */
+void fs_hcd_global_init(void)
+{
+        ZERO(mx2_usb_driver);
+        mx2_usb_driver.owner = THIS_MODULE;
+        mx2_usb_driver.name = "MX21_HCD";
+        mx2_usb_driver.probe = hcd_probe;
+        mx2_usb_driver.disconnect = hcd_disconnect;
+
+        ZERO(priv_mx21);
+        priv_mx21.free_buffs = 0xffff;
+        priv_mx21.free_etds = 0x0000ffff;
+        priv_mx21.bus_hcpriv = &fs_bus_hcpriv;
+
+        ZERO(fs_bus_hcpriv);
+        fs_bus_hcpriv.hw_hci = &priv_mx21;
+        fs_bus_hcpriv.usb_driver = &mx2_usb_driver;
+        fs_bus_hcpriv.bus_device.driver = &mx2_usb_driver.driver;
+        fs_bus_hcpriv.bus_device.parent = NULL;
+        fs_bus_hcpriv.bus_device.bus = &usb_bus_type;    // struct bus_type *
+#if 0
+        strncpy(fs_bus_hcpriv.bus_device.name, "MX21 HOST", sizeof(fs_bus_hcpriv.bus_device.name));
+#endif
+        fs_bus_hcpriv.num_ports = 1;
+        fs_bus_hcpriv.otg_port = 1;
+        fs_bus_hcpriv.otg_capable_mask = (1 << 1);
+        fs_bus_hcpriv.rh_serial = "000001";
+        fs_bus_hcpriv.rh_product = "Virtual Root Hub";
+        fs_bus_hcpriv.rh_manufacturer = "Belcarra Technologies";
+        fs_bus_hcpriv.rh_vendorid = CONFIG_OTG_ROOTHUB_VENDORID;
+        fs_bus_hcpriv.rh_productid = CONFIG_OTG_ROOTHUB_PRODUCTID;
+        fs_bus_hcpriv.rh_bcddevice = CONFIG_OTG_ROOTHUB_BCDDEVICE;
+        fs_bus_hcpriv.rh_bmAttributes = 0x0;
+        fs_bus_hcpriv.rh_bMaxPower = 0;
+
+        fs_bus_hcpriv.max_active_transfers = NUM_ETDS;
+        fs_bus_hcpriv.max_active_urbs = NUM_ETDS;
+
+        ZERO(hcd_ops);
+        hcd_ops.name = "MX21 HCD";
+        hcd_ops.max_ports = 1;
+        hcd_ops.capabilities = 0;
+                                                        // module
+        hcd_ops.mod_init = fs_mod_init;               // called for module init
+#ifdef MODULE
+        hcd_ops.mod_exit = fs_mod_exit;               // called for module exit
+#endif
+                                                        // otg state machine
+        hcd_ops.hcd_init_func = hcd_init_func;         // initialize when otg enabled
+        hcd_ops.hcd_en_func = fs_hcd_en_func;         // setup hardware as host
+        hcd_ops.loc_suspend_func = rh_loc_suspend_func;        // enable port on hub
+        hcd_ops.loc_sof_func = fs_loc_sof_func;       // enable port on hub
+};
+
+#else /* !defined(OTG_C99) */
+
+static struct usb_driver mx2_usb_driver = {
+        .owner = THIS_MODULE,
+        .name = "MX21_HCD",
+        .probe = hcd_probe,
+        .disconnect = hcd_disconnect,
+};
+
+extern struct bus_hcpriv fs_bus_hcpriv;
+fs_hcpriv priv_mx21 = {
+        .free_buffs = 0xffff,
+        .free_etds = 0x0000ffff,
+        .bus_hcpriv = &fs_bus_hcpriv,
+};
+
+struct bus_hcpriv fs_bus_hcpriv = {
+
+        .hw_hci = &priv_mx21,
+        .usb_driver = &mx2_usb_driver,
+        .bus_device.driver = &mx2_usb_driver.driver,
+        .bus_device.parent = NULL,
+        .bus_device.bus = &usb_bus_type,    // struct bus_type *
+        .bus_device.name = "MX21 HOST",
+
+        .num_ports = 1,
+        .otg_port = 1,
+        .otg_capable_mask = (1 << 1),
+        .rh_serial = "000001",
+        .rh_product = "Virtual Root Hub",
+        .rh_manufacturer = "Belcarra Technologies",
+        .rh_vendorid = CONFIG_OTG_ROOTHUB_VENDORID,
+        .rh_productid = CONFIG_OTG_ROOTHUB_PRODUCTID,
+        .rh_bcddevice = CONFIG_OTG_ROOTHUB_BCDDEVICE,
+        .rh_bmAttributes = 0x0,
+        .rh_bMaxPower = 0,
+
+        .max_active_transfers = NUM_ETDS,
+        .max_active_urbs = NUM_ETDS,
+};
+
+struct hcd_ops hcd_ops = {
+
+        .name = "MX21 HCD",
+        .max_ports = 1,
+        .capabilities = 0,
+                                                // module
+        .mod_init = fs_mod_init,               // called for module init
+#ifdef MODULE
+        .mod_exit = fs_mod_exit,               // called for module exit
+#endif
+        .hcd_init_func = hcd_init_func,         // initialize when otg enabled
+        .hcd_en_func = fs_hcd_en_func,         // setup hardware as host
+        .loc_suspend_func = rh_loc_suspend_func,        // enable port on hub
+        .loc_sof_func = fs_loc_sof_func,        // enable port on hub
+};
+#endif /* !defined(OTG_C99) */
+
+
+extern int hcd_init(struct bus_hcpriv *bus_hcpriv, struct usb_driver *usb_driver);
+extern void hcd_exit(struct bus_hcpriv *bus_hcpriv, struct usb_driver *usb_driver);
+
+/*! 
+ * fs_mod_init() - initialize mx2 hcd
+ *
+ * This calls hcd_init() with the mx2 data structures.
+ */
+int fs_mod_init(void)
+{
+        RETURN_ENOMEM_IF(hcd_init(&fs_bus_hcpriv, &mx2_usb_driver));
+
+        hcd_instance->privdata = &fs_bus_hcpriv;
+        return 0;
+}
+
+#ifdef MODULE
+/*! 
+ * fs_mod_exit() - de-initialize mx2 hcd
+ *
+ * This calls hcd_exit() with the mx2 data structures.
+ */
+void fs_mod_exit(void)
+{
+        hcd_exit(&fs_bus_hcpriv, &mx2_usb_driver);
+}
+#endif
+
+/* @} */
+
diff -uNr linux/drivers/no-otg/ocd/fsotg/fs-ocd.c linux/drivers/otg/ocd/fsotg/fs-ocd.c
--- linux/drivers/no-otg/ocd/fsotg/fs-ocd.c	1970-01-01 01:00:00.000000000 +0100
+++ linux/drivers/otg/ocd/fsotg/fs-ocd.c	2006-09-01 21:41:31.000000000 +0200
@@ -0,0 +1,263 @@
+/*
+ * otg/ocd/fsotg/fs-ocd.c -- USB Device Controller driver 
+ *
+ *      Copyright (c) 2004 Belcarra
+ *
+ * By: 
+ *      Stuart Lynne <sl@lbelcarra.com>, 
+ *      Tom Rushworth <tbr@belcarra.com>, 
+ *      Bruce Balden <balden@belcarra.com>
+ *
+ */
+/*!
+ * @file otg/ocd/fsotg/fs-ocd.c
+ * @brief Freescale USB OTG Controller Driver
+ * This is the OTG Controller Driver. 
+ *
+ * This implements overall configuration and control of the Freescale OTG hardware
+ * and implements the main interrupt handler.
+ *
+ * There is some processor specific code here to support the alternate processors
+ * that implement this type of USB support.
+ *
+ * There is no board or platform level code here.
+ *
+ * @ingroup FSOTG
+ *
+ * @{
+ */
+
+#include <otg/pcd-include.h>
+#include <linux/pci.h>
+#include <asm/arch/mx2.h>
+#undef GPT_BASE_ADDR
+#include <asm/arch/gpt.h>
+#include <asm/arch/clk.h>
+
+#include <otghw/fsotg-hardware.h>
+
+
+/* ********************************************************************************************* */
+#define TIMEOUT_VALUE 1000
+/*!
+ * fs_func_clock_on() - enable function controller clock
+ */
+void fs_func_clock_on(void)
+{
+        u32 timeout = TIMEOUT_VALUE;
+        //TRACE_MSG0(OCD, "FUNC CLOCK ON:");
+        fs_orl(OTG_CORE_CLK_CTRL, MODULE_FUNC_CLK);
+        while(!( fs_rl(OTG_CORE_CLK_CTRL) & MODULE_FUNC_CLK)) { timeout--; if (!timeout) break; }
+
+        //fs_orl(OTG_CORE_CINT_STEN, MODULE_ASFCINT_EN |MODULE_FCINT_EN);
+}       
+
+/*!
+ * fs_host_clock_on() - enable host controller clock
+ */
+void fs_host_clock_on(void)
+{
+        u32 timeout = TIMEOUT_VALUE;
+        //TRACE_MSG0(OCD, "HOST CLOCK ON");
+        fs_orl(OTG_CORE_CLK_CTRL, MODULE_HOST_CLK);
+        while(!( fs_rl(OTG_CORE_CLK_CTRL) & MODULE_HOST_CLK)) { timeout--; if (!timeout) break; }
+}       
+
+/*!
+ * fs_func_clock_off() - disable function controller clock
+ */
+void fs_func_clock_off(void)
+{       
+        u32 timeout = TIMEOUT_VALUE;
+        //TRACE_MSG0(OCD, "FUNC CLOCK OFF");
+        fs_andl(OTG_CORE_CLK_CTRL, 0x0);
+        while((fs_rl(OTG_CORE_CLK_CTRL) & (MODULE_FUNC_CLK | MODULE_MAIN_CLK))) { timeout--; if (!timeout) break; }
+}       
+
+/*!
+ * fs_host_clock_off() - disable host controller clock
+ */
+void fs_host_clock_off(void)
+{       
+        u32 timeout = TIMEOUT_VALUE;
+        //TRACE_MSG0(OCD, "HOST CLOCK OFF");
+        fs_andl(OTG_CORE_CLK_CTRL, 0x0);
+        while((fs_rl(OTG_CORE_CLK_CTRL) & (MODULE_HOST_CLK | MODULE_MAIN_CLK))) { timeout--; if (!timeout) break; }
+}       
+
+/*!
+ * fs_main_clock_on() - enable main clock
+ */
+void fs_main_clock_on(void)
+{
+        u32 timeout = TIMEOUT_VALUE;
+        //TRACE_MSG0(OCD, "MAINFUNC CLOCK ON");
+        fs_orl(OTG_CORE_CLK_CTRL, MODULE_MAIN_CLK);
+        while(!( fs_rl(OTG_CORE_CLK_CTRL) & MODULE_MAIN_CLK)) { timeout--; if (!timeout) break; }
+}
+
+/*!
+ * fs_main_clock_off() - disable main clock
+ */
+void fs_main_clock_off(void)
+{
+        u32 timeout = TIMEOUT_VALUE;
+        //TRACE_MSG0(OCD, "MAIN CLOCK OFF");
+        fs_wl_set(OCD, OTG_CORE_CLK_CTRL, 0);
+        while((fs_rl(OTG_CORE_CLK_CTRL) & MODULE_MAIN_CLK)) { timeout--; if (!timeout) break; }
+}
+
+/* ********************************************************************************************* */
+
+/*!
+ * fs_disable_interrupts() - disable interrupts
+ */
+void fs_disable_interrupts (void)
+{
+        fs_wl(OTG_CORE_CINT_STEN, 0 );
+        fs_wl(OTG_CORE_HINT_STEN, 0 );
+        fs_wl(OTG_HOST_SINT_STEN, 0 );
+        fs_wl(OTG_HOST_XYINT_STEN, 0 );
+        fs_wl(OTG_HOST_ETD_EN, 0);
+        fs_wl(OTG_HOST_ETD_DONE, 0 );
+        fs_wl(OTG_FUNC_SINT_STEN, 0 );
+        fs_wl(OTG_FUNC_XYINT_STEN, 0 );
+        fs_wl(OTG_FUNC_EP_EN, 0 );
+        fs_wl(OTG_FUNC_EP_DEN, 0 );
+}
+
+/* ********************************************************************************************* */
+
+/* ********************************************************************************************* */
+/*!
+ * fs_init() - initial tcd setup
+ * Allocate interrupts and setup hardware.
+ */
+void fs_init (void)
+{
+        int timeout; 
+        unsigned long flags;
+
+        TRACE_MSG0(OCD, "MX2_MOD_OCD_INIT");
+        local_irq_save (flags);
+
+        fs_wl(OTG_SYS_CTRL, 0x0);
+
+        /* 2. Ensure hardware is reset and cleared
+         */
+        fs_clear_words((volatile u32 *)MX2_IO_ADDRESS(OTG_DMA_BASE), (32*16/4));
+        //fs_clear((void *)MX2_IO_ADDRESS(OTG_FUNC_BASE), 0x200);
+        fs_main_clock_off();
+
+        fs_wl_clr(OCD, OTG_CORE_RST_CTRL, MODULE_RSTI2C | 0x3f);
+        while (fs_rl(OTG_CORE_RST_CTRL));
+
+        /* 3. OTG Hardware Mode and clocks
+         * set to diff, diff and Configure the OTG to behave as function
+         */
+        TRACE_MSG0(OCD, "3. OTG Software Mode and clock");
+        fs_andl(OTG_CORE_HWMODE, 0xffffff0f);                  // clear
+        fs_orl(OTG_CORE_HWMODE, MODULE_CRECFG_SHNP);           // set to software hnp
+        fs_rl(OTG_CORE_HWMODE);
+
+        fs_andl(OTG_CORE_HNP_CSTAT, ~0x00000800);
+        fs_rl(OTG_CORE_HNP_CSTAT);
+
+        fs_wl_set(OCD, OTG_CORE_HNP_T3PCR, 0x00000000);
+        fs_rl(OTG_CORE_HNP_T3PCR);
+
+        TRACE_MSG0(OCD, "6. Enable ");
+        TRACE_MSG0(OCD, "enable core interrupts");
+
+        fs_wl(OTG_CORE_CINT_STEN, 0);
+        
+        fs_wl(OTG_CORE_CINT_STEN, MODULE_ASHNPINT_EN | 
+                MODULE_ASHCINT_EN | MODULE_HNPINT_EN | MODULE_HCINT);
+        
+        
+        TRACE_MSG0(OCD, "enable host interrupts");
+        fs_wl(OTG_CORE_HINT_STEN, HNP_I2COTGINT_EN | HNP_AWAITBTO_EN |  
+                HNP_AIDLEBDTO_EN | HNP_SRPSUCFAIL_EN | HNP_SRPINT_EN | HNP_VBUSERROR_EN |
+                HNP_ABSEVAILD_EN | HNP_ABUSVALID_EN | HNP_MASSLVCHG_EN | HNP_IDCHANGE_EN); 
+
+        TRACE_MSG0(OCD, "disable various host interrupts");
+        fs_wl(OTG_HOST_XYINT_STEN, 0);
+        fs_wl(OTG_HOST_ETD_EN, 0);
+        fs_wl(OTG_HOST_ETD_DONE, 0);
+        fs_wl(OTG_HOST_SINT_STEN, 0);
+
+        TRACE_MSG0(OCD, "disable various function interrupts");
+        fs_wl(OTG_FUNC_XYINT_STEN, 0); 
+        fs_wl(OTG_FUNC_EP_EN, 0); 
+        fs_wl(OTG_FUNC_EP_DEN, 0);
+
+        fs_wl(OTG_DMA_DINT_STEN, 0x3);
+        //fs_wb(I2C_MASTER_INT_REG_ADD, 0xf0);
+        //fs_wb(I2C_MASTER_INT_REG_ADD, 0x00);
+
+        // XXX note that newer designs than the mx21 will also need to check
+        // and/or set OTG_CORE_INTERRUPT_STEN
+
+
+        TRACE_MSG0(OCD, "--");
+        TRACE_MSG0(OCD, "8. Ready ");
+        TRACE_MSG0(OCD, "--");
+
+        local_irq_restore (flags);
+}
+
+extern void fs_stop_ep(int epn, int dir);
+
+/*!
+ * fs_exit() - de-initialize
+ */
+void fs_exit(void)
+{
+        int i;
+        unsigned long flags;
+        TRACE_MSG0(OCD, "MX2_MOD_OCD_EXIT");
+
+        fs_disable_interrupts();
+        //_reg_CRM_PCCR1 &= ~(1<<27);                                             // disable GPT3
+
+        local_irq_save (flags);
+        for (i = 0; i < 16; i++) {
+                fs_stop_ep(i, USB_DIR_IN);
+                fs_stop_ep(i, USB_DIR_OUT);
+        }
+        fs_clear_words((volatile u32 *)MX2_IO_ADDRESS(OTG_EP_BASE), (32*16/4));
+        //fs_clear((void *)MX2_IO_ADDRESS(OTG_EP_BASE), 0x200);
+        //fs_clear((volatile u32 *)MX2_IO_ADDRESS(OTG_DMA_BASE), 32*16);
+        //fs_clear((void *)MX2_IO_ADDRESS(OTG_FUNC_BASE), 0x200);
+        fs_wl( OTG_CORE_HWMODE, 0xa3);
+        fs_main_clock_off();
+        local_irq_restore (flags);
+}
+
+
+/*!
+ * fs_ocd_init() - used to initialize/enable or disable the tcd driver
+ * @param otg
+ * @param flag
+ */
+void fs_ocd_init(struct otg_instance *otg, u8 flag)
+{
+        TRACE_MSG0(OCD, "--");
+        switch (flag) {
+        case SET:
+                TRACE_MSG0(OCD, "MX2_OCD_EN SET");
+                fs_init();
+                otg_event(otg, TCD_OK | ID_FLOAT, OCD, "OCD_OK");
+                break;
+        case RESET:
+                fs_exit();
+                TRACE_MSG0(OCD, "MX2_OCD_EN RESET");
+                otg_event(otg, TCD_OK | ID_FLOAT, OCD, "OCD_OK");
+                break;
+        }
+}
+
+/* ********************************************************************************************* */
+
+/* @} */
+
diff -uNr linux/drivers/no-otg/ocd/fsotg/fs-pcd.c linux/drivers/otg/ocd/fsotg/fs-pcd.c
--- linux/drivers/no-otg/ocd/fsotg/fs-pcd.c	1970-01-01 01:00:00.000000000 +0100
+++ linux/drivers/otg/ocd/fsotg/fs-pcd.c	2006-09-01 21:41:31.000000000 +0200
@@ -0,0 +1,793 @@
+/*
+ * otg/ocd/fsotg/fs-pcd.c -- Freescale USBOTG Peripheral Controller driver
+ *
+ *      Copyright (c) 2004 Belcarra
+ *
+ * By:
+ *      Stuart Lynne <sl@lbelcarra.com>,
+ *      Tom Rushworth <tbr@belcarra.com>,
+ *      Bruce Balden <balden@belcarra.com>
+ *
+ */
+/*!
+ * @file otg/ocd/fsotg/fs-pcd.c
+ * @brief Freescale USB Peripheral Controller Driver
+ * This implements the Freescale USBOTG Peripheral Controller Driver.
+ *
+ * There is some processor specific code here to support the alternate processors
+ * that implement this type of USB support.
+ *
+ * There is no board or platform level code here.
+ *
+ * @ingroup FSOTG
+ *
+ * @{
+ */
+
+#include <otg/pcd-include.h>
+#include <linux/pci.h>
+#include <asm/arch/mx2.h>
+
+#include <otghw/fsotg-hardware.h>
+
+void fs_func_clock_on(void);                   /* defined in mx2-ocd.c */
+void fs_func_clock_off(void);                  /* defined in mx2-ocd.c */
+
+u8 fs_new_address, fs_usb_address;            /* used to save and set USB address */
+
+/* ********************************************************************************************* */
+/*!
+ * fs_ep_config() - configure and enable an endpoint to send or receive data
+ * First setup the endpoint descriptor words, and then enable the endpoint and done interrupt
+ * @param epn - endpoint number
+ * @param dir - direction IN or OUT
+ * @param ep_num -
+ * @param wMaxPacketSize
+ * @param format
+ * @param ttlbtecnt
+ * @param stall
+ */
+static void inline
+fs_ep_config(int epn, int dir, u32 ep_num, int wMaxPacketSize, int format, int ttlbtecnt, int stall)
+{
+        TRACE_MSG6(PCD, "epn: %02x dir: %02x sz: %d fmt: %x cnt: %d stall: %d",
+                        epn, dir, wMaxPacketSize, format, ttlbtecnt, stall);
+
+        fs_wl(ep_word(epn, dir, 0), ((stall ? 1 : 0) << 31) | (wMaxPacketSize << 16) | ((format & 3) << 14));
+        fs_wl(ep_word(epn, dir, 1), ttlbtecnt ?  (data_y_buf(epn, dir) << 16) | data_x_buf(epn, dir): 0);
+        fs_wl(ep_word(epn, dir, 3), ((wMaxPacketSize-1) << 21) | ttlbtecnt);
+        fs_orl(OTG_FUNC_IINT, ep_num);              // issue done status interrupts immediately
+        fs_orl(OTG_FUNC_EP_DEN, ep_num);            // enable endpoint done interrupt
+        fs_orl(OTG_FUNC_EP_EN, ep_num);             // enable the interrupt
+}
+
+/*!
+ * fs_ep_config_dma() - configure and enable an endpoint to send or receive data
+ * Check the RDY bit and clear if necessary, setup dma, do common setup and then set RDY.
+ * @param epn - endpoint number
+ * @param dir - direction IN or OUT
+ * @param wMaxPacketSize
+ * @param format
+ * @param ttlbtecnt
+ * @param bp
+ */
+static void inline
+fs_ep_config_dma(int epn, int dir, int wMaxPacketSize, int format, int ttlbtecnt, u8 *bp)
+{
+        u32 ep_num = ep_num_dir(epn, dir);
+        u32 dma_num = dma_num_dir(epn, dir);
+        TRACE_MSG2(PCD, "EP CONFIG DMA: epn: %02x num: %02x", epn, ep_num);
+        if (fs_rl(OTG_FUNC_EP_RDY) & ep_num) fs_wl_set(PCD, OTG_FUNC_EP_RDY, ep_num);
+        fs_wl(OTG_DMA_EPN_MSA(dma_num), (int)bp);
+        fs_wl_set(PCD, OTG_DMA_EP_EN, ep_num);
+        fs_ep_config(epn, dir, ep_num, wMaxPacketSize, format, ttlbtecnt, 0);
+        fs_wl_set(PCD, OTG_FUNC_EP_RDY, ep_num);           // toggle ready bit
+}
+
+/*!
+ * fs_ep_config_nodma() - configure and enable an endpoint to send or receive data
+ * Non-DMA setup, optional stall, ready etc.
+ * @param epn - endpoint number
+ * @param dir
+ * @param stall - stall endpoint
+ * @param wMaxPacketSize
+ * @param format
+ * @param ttlbtecnt
+ * @param rdy1
+ * @param rdy2
+ */
+static void
+fs_ep_config_nodma(int epn, int dir, int stall, int wMaxPacketSize, int format, int ttlbtecnt, int rdy1, int rdy2)
+{
+        u32 ep_num = ep_num_dir(epn, dir);
+        TRACE_MSG5(PCD, "EP CONFIG NODMA: epn: %02x num: %02x stall: %d r1: %d r2: %d", epn, ep_num, stall, rdy1, rdy2);
+        #if 1
+        // XXX This should work, but enumeratation fails after disable/enable
+        if (rdy1) {
+                TRACE_MSG3(PCD, "EP CONFIG NODMA: epn: %02x num: %02x checking RDY: %02x",
+                                epn, ep_num, rdy1 && fs_rl(OTG_FUNC_EP_RDY) & ep_num);
+                RETURN_IF (rdy1 && (fs_rl(OTG_FUNC_EP_RDY) & ep_num));
+        }
+        #endif
+        UNLESS(epn)
+                fs_orl(OTG_FUNC_XYINT_STEN, ep_num);        // enable x or y buffer interrupt
+
+        fs_ep_config(epn, dir, ep_num, wMaxPacketSize, format, ttlbtecnt, stall);
+        RETURN_UNLESS(rdy2);
+        UNLESS (fs_rl(OTG_FUNC_EP_RDY) & ep_num) fs_wl_set(PCD, OTG_FUNC_EP_RDY, ep_num);           // toggle ready bit
+}
+
+/*!
+ * fs_sendzlp() - start sending a buffer on a specified endpoint
+ * Toggle XFILL_STAT or YFILL_STAT bit, then call fs_ep_config_nodma().
+ * @param epn - endpoint number
+ * @param endpoint - endpoint instance
+ * @param wMaxPacketSize
+ */
+static void inline
+fs_sendzlp (int epn, struct usbd_endpoint_instance *endpoint, int wMaxPacketSize)
+{
+        u32 ep_num_in = ep_num_in(epn);
+        TRACE_MSG0(PCD, "ZLP");
+        if (!(fs_rl(OTG_FUNC_XFILL_STAT) & ep_num_in)) {
+                //TRACE_MSG0(PCD, "EMPTY Y BUFFER: ZLP");
+                fs_wl_set(PCD, OTG_FUNC_XFILL_STAT, ep_num_in);
+        }
+        else if (!(fs_rl(OTG_FUNC_YFILL_STAT) & ep_num_in)) {
+                //TRACE_MSG0(PCD, "EMPTY Y BUFFER: ZLP");
+                fs_wl_set(PCD, OTG_FUNC_YFILL_STAT, ep_num_in);
+        }
+        fs_ep_config_nodma(epn, USB_DIR_IN, 0, endpoint->wMaxPacketSize, endpoint->bmAttributes & 0x3, 0, 1, 1);
+}
+
+/*!
+ * fs_stop_ep() - stop endpoint
+ * @param epn - endpoint number
+ * @param dir - direction IN or OUT
+ */
+static void fs_stop_ep(int epn, int dir)
+{
+        int ep_num = ep_num_dir(epn, dir);
+
+        TRACE_MSG3(PCD, "STOP EP: %08x dir: %02x [%08x]", ep_num, dir, fs_rl(ep_word(epn, dir, 0)));
+
+        //fs_wl(ep_word(epn, dir, 0), ((stall ? 1 : 0) << 31) | (wMaxPacketSize << 16) | ((format & 3) << 14));
+
+        if (fs_rl(OTG_FUNC_EP_RDY) & ep_num)
+                fs_wl_clr(PCD, OTG_FUNC_EP_RDY, ep_num);
+        fs_andl(OTG_FUNC_XYINT_STEN, ~ep_num);
+        fs_andl(OTG_FUNC_IINT, ~ep_num);
+        if (fs_rl(OTG_FUNC_XINT_STAT) & ep_num) {
+                fs_wl_clr(PCD, OTG_FUNC_XINT_STAT, ep_num);
+                fs_wl_clr(PCD, OTG_FUNC_XFILL_STAT, ep_num);
+        }
+        else if (fs_rl(OTG_FUNC_YINT_STAT) & ep_num) {
+                fs_wl_clr(PCD, OTG_FUNC_YINT_STAT, ep_num);
+                fs_wl_clr(PCD, OTG_FUNC_YFILL_STAT, ep_num);
+        }
+}
+
+/* ********************************************************************************************* */
+/*!
+ * fs_pcd_setup_ep() - setup endpoint
+ * @param pcd - pcd instance
+ * @param epn - endpoint number
+ * @param endpoint - endpoint instance
+ */
+static void inline
+fs_pcd_setup_ep (struct pcd_instance *pcd, unsigned int epn, struct usbd_endpoint_instance *endpoint)
+{
+        TRACE_MSG2(PCD, "START EPN: config %d %02x", epn, endpoint->bEndpointAddress);
+        if (epn && endpoint->bEndpointAddress)
+                fs_ep_config_nodma(endpoint->bEndpointAddress & 0x7f, endpoint->bEndpointAddress & USB_DIR_IN, 0,
+                                endpoint->wMaxPacketSize, endpoint->bmAttributes&0x3, 0, 0, 0);
+}
+
+/*!
+ * fs_pcd_start_ep0_setup() - enable ep0 for receiving SETUP request
+ * Unless already ready, configure ep0 to receive.
+ * @param endpoint - endpoint instance
+ */
+static void inline
+fs_pcd_start_ep0_setup (struct usbd_endpoint_instance *endpoint)
+{
+        TRACE_MSG0(PCD, "START EP0: config");
+        fs_ep_config_nodma(0, USB_DIR_OUT, 0, endpoint->wMaxPacketSize, endpoint->bmAttributes&0x3,
+                        endpoint->wMaxPacketSize, 0, 1); // XXX rdy1 must be zero
+}
+/* ********************************************************************************************* */
+/*!
+ * fs_pcd_start_endpoint_out() - start receive
+ * @param pcd - pcd instance
+ * @param endpoint - endpoint number
+ */
+static void
+fs_pcd_start_endpoint_out(struct pcd_instance *pcd, struct usbd_endpoint_instance *endpoint)
+{
+        struct usbd_urb *rcv_urb = pcd_rcv_next_irq(endpoint);
+        int epn = endpoint->bEndpointAddress & 0x7f;
+        UNLESS (rcv_urb) {
+                UNLESS (!epn) fs_pcd_start_ep0_setup(endpoint);  // if EP0 then restart for receive setup
+                return;
+        }
+        rcv_urb->pcimap = (void *)pci_map_single (NULL, (void *) rcv_urb->buffer, rcv_urb->buffer_length, PCI_DMA_FROMDEVICE);
+        fs_ep_config_dma(epn, USB_DIR_OUT, endpoint->wMaxPacketSize, endpoint->bmAttributes & 0x3,
+                        rcv_urb->buffer_length, rcv_urb->pcimap);
+        TRACE_MSG3(PCD, "START OUT: %02x rcv_urb: %x length: %d", endpoint->bEndpointAddress, rcv_urb, rcv_urb->actual_length);
+}
+
+/*!
+ * fs_pcd_stop_out() - process interrupt for received buffer
+ * @param pcd - pcd instance
+ * @param epn - endpoint number
+ * @param endpoint - endpoint instance
+ */
+static void
+fs_pcd_stop_out (struct pcd_instance *pcd, int epn, struct usbd_endpoint_instance *endpoint)
+{
+        struct usbd_urb *rcv_urb = pcd_rcv_next_irq(endpoint);
+        u32 ep3 = fs_rl(ep_word(epn, USB_DIR_OUT, 3));
+        u32 ep_num_out = ep_num_out(epn);
+        fs_stop_ep(epn, USB_DIR_OUT);
+        RETURN_UNLESS (rcv_urb);
+        pcd_rcv_finished_irq (endpoint,rcv_urb->buffer_length - (ep3 & 0xffff), 0);      // XXX fix mask
+        if (!epn)
+                fs_sendzlp(epn, endpoint, endpoint->wMaxPacketSize);        // ZLP
+        else
+                fs_pcd_start_endpoint_out(pcd, endpoint);                        // restart
+}
+/* fs_pcd_cancel_out_irq - cancel OUT urb
+ */
+static void
+fs_pcd_cancel_out_irq (struct pcd_instance *pcd,struct usbd_urb *urb)
+{
+        struct usbd_endpoint_instance *endpoint = urb->endpoint;
+        int epn = endpoint->bEndpointAddress & 0x7f;
+        TRACE_MSG3(PCD, "urb: %x endpoint: %x epn: %d", urb, endpoint, epn);
+        fs_pcd_stop_out (pcd, epn, endpoint);
+}
+/* ********************************************************************************************* */
+/*!
+ * fs_pcd_start_endpoint_in() - start transmit
+ * Get next tx_urb (completing old one if there is one) and start sending it.
+ * @param pcd - pcd instance
+ * @param endpoint - endpoint instance
+ */
+static void
+fs_pcd_start_endpoint_in(struct pcd_instance *pcd, struct usbd_endpoint_instance *endpoint)
+{
+        struct usbd_urb *tx_urb = pcd_tx_complete_irq(endpoint, 0);
+        int epn = endpoint->bEndpointAddress & 0x7f;
+
+        UNLESS (tx_urb) {
+                TRACE_MSG1(PCD, "START IN: %02x finished", endpoint->bEndpointAddress);
+                UNLESS (epn) {                                  // if not EP0 we are done
+                        if (fs_new_address != fs_usb_address) {
+                                fs_usb_address = fs_new_address;
+                                //TRACE_MSG1(PCD, "START IN: SETTING ADDRESS %x", fs_usb_address);
+                                fs_wl(OTG_FUNC_DEV_ADDR, fs_usb_address);
+                        }
+                        fs_pcd_start_ep0_setup(endpoint);          // if EP0 then restart for receive setup
+                }
+                return;
+        }
+
+        if (pcd_tx_sendzlp(endpoint)) {                                                  // check if we need to send ZLP
+                TRACE_MSG1(PCD, "START IN: ZLP tx_urb: %x", tx_urb);
+                fs_sendzlp(epn, endpoint, endpoint->wMaxPacketSize);        // ZLP
+                return;
+        }
+        endpoint->last = tx_urb->actual_length;
+        consistent_sync (tx_urb->buffer, tx_urb->actual_length, PCI_DMA_BIDIRECTIONAL);
+        fs_ep_config_dma(epn, USB_DIR_IN, endpoint->wMaxPacketSize, endpoint->bmAttributes & 0x3,
+                        tx_urb->actual_length, tx_urb->buffer);
+
+        TRACE_MSG4(PCD, "START IN: %02x tx_urb: %x length: %d %s", 
+                        endpoint->bEndpointAddress, tx_urb, tx_urb->actual_length, 
+                        (tx_urb->flags & USBD_URB_SENDZLP) ? "ZLP": "");
+}
+
+/*!
+ * fs_pcd_stop_in() - process interrupt for transmitted buffer
+ * @param pcd - pcd instance
+ * @param epn -endpoint number
+ * @param endpoint - endpoint instance
+ */
+static void inline
+fs_pcd_stop_in (struct pcd_instance *pcd, int epn, struct usbd_endpoint_instance *endpoint)
+{
+        fs_stop_ep(epn, USB_DIR_IN);
+        fs_pcd_start_endpoint_in(pcd, endpoint);
+}
+/*!
+ * fs_pcd_cancel_in_irq() - cancel IN urb
+ */
+static void inline 
+fs_pcd_cancel_in_irq (struct pcd_instance *pcd,struct usbd_urb *urb)
+{
+        struct usbd_endpoint_instance *endpoint = urb->endpoint;
+        int epn = endpoint->bEndpointAddress & 0x7f;
+        TRACE_MSG3(PCD, "urb: %x endpoint: %x epn: %d", urb, endpoint, epn);
+        fs_pcd_stop_in (pcd, epn, endpoint);
+}
+
+/* ********************************************************************************************* */
+/*! 
+ * fs_pcd_stop_ep0_setup() - setup endpoint zero
+ * @param pcd - pcd instance
+ * @param endpoint - endpoint instance
+ */
+static void 
+fs_pcd_stop_ep0_setup (struct pcd_instance *pcd, struct usbd_endpoint_instance *endpoint)
+{
+        static struct usbd_device_request request;
+        u8 *cp = (u8 *) &request;
+        u32 ep0 = fs_rl(ep_word(0, USB_DIR_OUT, 0));
+        u32 ep3 = fs_rl(ep_word(0, USB_DIR_OUT, 3));
+        
+        fs_andl(OTG_FUNC_XYINT_STEN, ~ep_num_both(0));
+        fs_andl(OTG_FUNC_IINT, ~ep_num_both(0));
+        UNLESS (ep0 & EP0_SETUP) {                                      // check if SETUP flag set
+                fs_pcd_stop_out(pcd, 0, endpoint);
+                return;
+        }
+        TRACE_MSG1(PCD, "EP0 - SETUP: size: %x", ep3 & 0xffff);
+        pcd_tx_cancelled_irq(endpoint);
+        pcd_rcv_cancelled_irq(endpoint);
+        fs_orl(OTG_FUNC_EP_TOGGLE, 0x2);                             // XXX is this required?
+
+        /* get request from x or y buffer
+         */
+        if (fs_rl(OTG_FUNC_XINT_STAT) & ep_num_out(0)) {
+                TRACE_MSG0(PCD, "READ X SETUP");
+                fs_memcpy((u8 *)&request, (u8 *)data_x_address(0, USB_DIR_OUT), 8);
+                fs_wl_clr(PCD, OTG_FUNC_XINT_STAT, ep_num_out(0));
+                fs_wl_clr(PCD, OTG_FUNC_XFILL_STAT, ep_num_out(0));
+                fs_rl(OTG_FUNC_XFILL_STAT);
+        }
+        else if (fs_rl(OTG_FUNC_YINT_STAT) & ep_num_out(0)) {
+                TRACE_MSG0(PCD, "READ Y SETUP");
+                fs_memcpy((u8 *)&request, (u8 *)data_y_address(0, USB_DIR_OUT), 8);
+                fs_wl_clr(PCD, OTG_FUNC_YINT_STAT, ep_num_out(0));
+                fs_wl_clr(PCD, OTG_FUNC_YFILL_STAT, ep_num_out(0));
+                fs_rl(OTG_FUNC_YFILL_STAT);
+        }
+        else
+                TRACE_MSG0(PCD, "READ NO SETUP");                             // XXX what should we do here?
+        if (pcd_recv_setup_irq(pcd, &request)) {                              // process setup packet
+                TRACE_MSG0(PCD, "pcd_ep0: STALLING");
+                fs_rl(OTG_FUNC_XINT_STAT);
+                fs_ep_config_nodma(0, USB_DIR_IN, 1, endpoint->wMaxPacketSize, endpoint->bmAttributes & 0x3, 0, 1, 1);
+                return;
+        }
+        RETURN_IF (request.wLength);                            // fs_pcd_start_endpoint_in() will start xfer
+        if ((request.bmRequestType & USB_REQ_DIRECTION_MASK) == USB_REQ_HOST2DEVICE) {
+                //RETURN_IF (request.wLength);                            // fs_pcd_start_endpoint_in() will start xfer
+                fs_sendzlp(0, endpoint, endpoint->wMaxPacketSize);     // wLength zero, so send ZLP
+                return;
+        }
+        fs_pcd_start_ep0_setup(endpoint);                                  // restart endpoint 
+}
+/* ********************************************************************************************* */
+static int pcd_resetdet_count;
+/*! 
+ * fs_pcd_int_hndlr() - interrupt handler
+ * @param irq
+ * @param dev_id
+ * @param regs
+ * @return interrupt handled status
+ */
+static irqreturn_t 
+fs_pcd_int_hndlr (int irq, void *dev_id, struct pt_regs *regs)
+{
+	struct pcd_instance *pcd = pcd_instance;
+        u32 cmd_stat, sint_stat, ep_dstat, ep_stats, mask;
+
+        /* Reset and process any System interrupts
+         */
+        if ((sint_stat = fs_rl(OTG_FUNC_SINT_STAT))) 
+                fs_wl_clr(PCD, OTG_FUNC_SINT_STAT, sint_stat);
+
+        cmd_stat = fs_rl(OTG_FUNC_CMD_STAT);
+
+        TRACE_MSG2(PCD, "FUNC INT sint: %08x cmd: %08x", sint_stat, cmd_stat); 
+
+        if ((cmd_stat & COMMAND_RESETDET) || (sint_stat & SYSTEM_RESETINT)) {
+                TRACE_MSG1(PCD, "RESETDET: %08x", sint_stat);
+                usbd_bus_event_handler_irq (pcd->bus, DEVICE_RESET, 0);
+                fs_wl(OTG_FUNC_DEV_ADDR, 0);
+                fs_new_address = fs_usb_address = 0;
+                fs_wl(OTG_FUNC_XYINT_STEN, 0);
+                fs_wl(OTG_FUNC_IINT, 0);
+                //fs_wl(OTG_FUNC_EP_DEN, 0);  // XXX this fails, 
+                fs_wl(OTG_FUNC_EP_TOGGLE, 0);
+                fs_wl_clr(PCD, OTG_FUNC_EP_RDY, fs_rl(OTG_FUNC_EP_RDY));
+                fs_wl_clr(PCD, OTG_FUNC_EP_DSTAT, fs_rl(OTG_FUNC_EP_DSTAT));
+                fs_wl_clr(PCD, OTG_FUNC_XINT_STAT, fs_rl(OTG_FUNC_XINT_STAT));
+                fs_wl_clr(PCD, OTG_FUNC_YINT_STAT, fs_rl(OTG_FUNC_YINT_STAT));
+                if (pcd_resetdet_count++ > 10) {
+
+                        //printk(KERN_INFO"%s: SINT: %08x %08x CINT: %08x\n", __FUNCTION__,
+                        //                sint_stat, fs_rl(OTG_FUNC_SINT_STEN), fs_rl(OTG_CORE_CINT_STEN));
+                        fs_func_clock_on();
+                        fs_wl(OTG_FUNC_SINT_STEN, 0);
+                        fs_wl(OTG_CORE_CINT_STEN, 0);
+                        fs_wl(OTG_FUNC_XYINT_STEN, 0); 
+                        fs_wl(OTG_FUNC_EP_EN, 0); 
+                        fs_wl(OTG_FUNC_EP_DEN, 0);
+                        fs_func_clock_off();
+                        //printk(KERN_INFO"%s: SINT: %08x %08x CINT: %08x RESET\n", __FUNCTION__,
+                        //                sint_stat, fs_rl(OTG_FUNC_SINT_STEN), fs_rl(OTG_CORE_CINT_STEN));
+                }
+        }
+        if ((cmd_stat & COMMAND_RSMINPROG) || (sint_stat & SYSTEM_RSMFININT)) {
+                TRACE_MSG1(PCD, "RSMINPRO: %08x", sint_stat);
+                fs_wl_set(PCD, OTG_FUNC_SINT_STAT,SYSTEM_RSMFININT);
+                fs_func_clock_on();
+                usbd_bus_event_handler_irq (pcd->bus, DEVICE_BUS_ACTIVITY, 0);
+        }
+        if ((cmd_stat & COMMAND_SUPDET) || (sint_stat & SYSTEM_SUSPDETINT)) {
+                int timeout = 0;
+                TRACE_MSG1(PCD, "SUSPENDDET: %08x", sint_stat);
+                fs_wl_set(PCD, OTG_FUNC_SINT_STAT,SYSTEM_SUSPDETINT);
+                fs_func_clock_off();
+                usbd_bus_event_handler_irq (pcd->bus, DEVICE_BUS_INACTIVE, 0);
+        }
+        ep_dstat = fs_rl(OTG_FUNC_EP_DSTAT);
+        ep_stats = ep_dstat | fs_rl(OTG_FUNC_XINT_STAT) | fs_rl(OTG_FUNC_YINT_STAT);
+        if (ep_stats) {
+                int i;
+
+                pcd_resetdet_count = 0;
+
+                /* clear the done status flags
+                 */
+                TRACE_MSG1(PCD, "EP STATS: %08x", ep_stats);
+                fs_wl_clr(PCD, OTG_FUNC_EP_DSTAT, ep_dstat);
+
+                /* check ep0 first - special handling for control endpoint, need to check dstat, y/x stats
+                 */
+                if (ep_stats & EP_OUT) fs_pcd_stop_ep0_setup(pcd, pcd->bus->endpoint_array);
+                if (ep_stats & EP_IN) fs_pcd_stop_in(pcd, 0, pcd->bus->endpoint_array);
+
+                /* now check all of the data endpoints, only if in dstat
+                 */
+                for (i = 1, ep_dstat >>= 2; ep_dstat && (i < 16); i++, ep_dstat >>= 2) {
+                        if (ep_dstat & EP_OUT) fs_pcd_stop_out(pcd, i, pcd->bus->endpoint_array + (2 * i));
+                        if (ep_dstat & EP_IN) fs_pcd_stop_in(pcd, i, pcd->bus->endpoint_array + (2 * i) + 1);
+                }
+        }
+        return IRQ_HANDLED;
+}
+
+/* ********************************************************************************************* */
+/*!
+ * fs_pcd_set_address() - set the USB address for this device
+ * @param pcd
+ * @param address
+ */
+static void inline
+fs_pcd_set_address (struct pcd_instance *pcd, u8 address)
+{
+        fs_usb_address = 0;
+        fs_new_address = address;      /* this will be used in the interrupt handler */
+}
+
+/* ********************************************************************************************* */
+/*! 
+ * fs_pcd_en_func() - enable
+ * This is called to enable / disable the PCD and USBD stack.
+ * @param otg - otg instance
+ * @param flag - SET or RESET
+ */
+static void 
+fs_pcd_en_func (struct otg_instance *otg, u8 flag)
+{
+        unsigned long flags;
+        struct pcd_instance *pcd = otg->pcd;
+        struct usbd_bus_instance *bus = pcd->bus;
+        u32 hwmode = fs_rl(OTG_CORE_HWMODE);
+
+
+        switch (flag) {
+        case SET:
+                TRACE_MSG0(PCD, "PCD_EN: SET");
+                local_irq_save (flags);
+                fs_andl(OTG_CORE_HWMODE, 0xffffff0f);                  // clear
+
+                //printk(KERN_INFO"%s: SET\n", __FUNCTION__);
+                if (hwmode & MODULE_CRECFG_HOST) {
+                        printk(KERN_INFO"%s: warning HOST STILL SET\n", __FUNCTION__);
+                }
+                fs_orl(OTG_CORE_HWMODE, MODULE_CRECFG_FUNC);           // set to software hnp
+
+		/* reset the function core
+		 */
+                //fs_wl_clr(PCD, OTG_CORE_RST_CTRL, MODULE_RSTFSIE | MODULE_RSTFC);
+                //while (fs_rl(OTG_CORE_RST_CTRL));
+		
+                fs_func_clock_on();
+
+                fs_pcd_start_ep0_setup(pcd->bus->endpoint_array);
+
+                /* C.f. L3 12.1 - enable Slave, Band Gap enable, and Comparator enable.
+                 * C.f. SCM-11 version of the USBOTG specification, ABBusReq enables the charge pump
+                 */
+                fs_andl(OTG_CORE_HNP_CSTAT, ~(MODULE_MASTER | MODULE_SLAVE | 
+                                        MODULE_CMPEN | MODULE_BGEN | MODULE_SWAUTORST | MODULE_ABBUSREQ));
+
+                fs_orl(OTG_CORE_HNP_CSTAT, MODULE_SLAVE | MODULE_CMPEN | MODULE_BGEN | MODULE_SWAUTORST | MODULE_ABBUSREQ);
+                fs_rl(OTG_CORE_HNP_CSTAT);
+
+                fs_orl(OTG_FUNC_SINT_STEN, ( /*SYSTEM_DONEREGINTDS_EN | *//*SYSTEM_SOFDETINT_EN |*/ SYSTEM_DONEREGINT_EN |
+                                        SYSTEM_SUSPDETINT_EN | SYSTEM_RSMFININT_EN | SYSTEM_RESETINT_EN));
+
+                fs_orl(OTG_CORE_CINT_STEN, MODULE_FCINT_EN | MODULE_ASFCINT_EN);
+                //printk(KERN_INFO"%s: SINT: %08x CINT: %08x SET\n", __FUNCTION__,
+                //                fs_rl(OTG_FUNC_SINT_STEN), fs_rl(OTG_CORE_CINT_STEN));
+                local_irq_restore (flags);
+        
+                break;
+
+        case RESET:
+                TRACE_MSG0(PCD, "PCD_EN: RESET");
+                //printk(KERN_INFO"%s: RESET\n", __FUNCTION__);
+
+                //fs_andl(OTG_FUNC_SINT_STEN, ~( /*SYSTEM_DONEREGINTDS_EN | *//*SYSTEM_SOFDETINT_EN |*/ SYSTEM_DONEREGINT_EN |
+                //                        SYSTEM_SUSPDETINT_EN | SYSTEM_RSMFININT_EN | SYSTEM_RESETINT_EN));
+                
+                local_irq_save (flags);
+                fs_func_clock_on();
+                fs_andl(OTG_FUNC_SINT_STEN, 0);
+                fs_andl(OTG_CORE_CINT_STEN, ~(MODULE_FCINT_EN | MODULE_ASFCINT_EN));
+
+                fs_func_clock_off();
+                //printk(KERN_INFO"%s: SINT: %08x CINT: %08x RESET\n", __FUNCTION__,
+                //                fs_rl(OTG_FUNC_SINT_STEN), fs_rl(OTG_CORE_CINT_STEN));
+
+                /* reset the function core
+                 */
+                fs_wl_clr(PCD, OTG_CORE_RST_CTRL, MODULE_RSTFSIE | MODULE_RSTFC);
+                while (fs_rl(OTG_CORE_RST_CTRL));
+                fs_andl(OTG_CORE_HWMODE, ~MODULE_CRECFG_FUNC);           // set to software hnp
+                local_irq_restore (flags);
+
+                usbd_bus_event_handler_irq (bus, DEVICE_RESET, 0);
+                usbd_bus_event_handler_irq (bus, DEVICE_DESTROY, 0);
+
+                break;
+        }
+        TRACE_MSG1(PCD, "HWMODE: %08x", fs_rl(OTG_CORE_HWMODE));
+}
+
+/*! 
+ * fs_remote_wakeup() - perform remote wakeup.
+ * Initiate a remote wakeup to the host.
+ * @param otg - otg instance
+ * @param flag - SET or RESET
+ */
+static void 
+fs_remote_wakeup(struct otg_instance *otg, u8 flag)
+{
+        unsigned long flags;
+        TRACE_MSG0(PCD, "MX2_REMOTE_WAKEUP: ");
+        local_irq_save (flags);
+        fs_wl_set(PCD, OTG_FUNC_CMD_STAT, COMMAND_RSMINPROG);
+        fs_wl(OTG_FUNC_CMD_STAT, COMMAND_RSMINPROG);
+        local_irq_restore (flags);
+}               
+
+/* ********************************************************************************************* */
+/*! 
+ * fs_pcd_start() - start the UDC
+ * @param pcd
+ */
+static void 
+fs_pcd_start (struct pcd_instance *pcd)
+{
+        //TRACE_MSG0(PCD, "UDC START");
+        fs_rl(OTG_FUNC_SINT_STEN);
+        //fs_pcd_start_ep0_setup(pcd_instance->bus->endpoint_array);
+}
+
+/*! 
+ * fs_pcd_stop() - stop the UDC
+ * @param pcd
+ */
+static void 
+fs_pcd_stop (struct pcd_instance *pcd)
+{
+        TRACE_MSG0(PCD, "UDC STOP");
+}
+
+/*!
+ * pcd_assign_endpoint()
+ */
+static int 
+pcd_assign_endpoint(u8 physicalEndpoint, int used[UDC_MAX_ENDPOINTS], 
+                struct usbd_endpoint_map *endpoint_map, u8 bmAttributes, u16 wMaxPacketSize, u16 transferSize) 
+{
+        RETURN_EINVAL_IF( used[physicalEndpoint] );
+        endpoint_map->bEndpointAddress[0] = physicalEndpoint | (USB_DIR_IN & bmAttributes);
+        endpoint_map->physicalEndpoint[0] = (physicalEndpoint * 2) + ((USB_DIR_IN & bmAttributes) ? 1 : 0);
+        endpoint_map->wMaxPacketSize[0] = wMaxPacketSize;
+        endpoint_map->transferSize[0] = transferSize;
+        endpoint_map->bmAttributes[0] = bmAttributes;
+        used[physicalEndpoint]++;
+        TRACE_MSG4(PCD, "ASSIGN: index: %02x phys: %02x addr: %02x", physicalEndpoint, 
+                         endpoint_map->physicalEndpoint[0], endpoint_map->bEndpointAddress[0], 0);
+        return 0;
+}
+
+/*!
+ * fs_pcd_request_endpoints()
+ * @param pcd
+ * @param endpoint_map_array
+ * @param endpointsRequested
+ * @param requestedEndpoints
+ */
+static int 
+fs_pcd_request_endpoints(struct pcd_instance *pcd, struct usbd_endpoint_map *endpoint_map_array, int endpointsRequested, 
+                struct usbd_endpoint_request *requestedEndpoints)
+{
+        struct usbd_device_description *device_description;
+        int i, in, out;
+        int in_used[UDC_MAX_ENDPOINTS];
+        int out_used[UDC_MAX_ENDPOINTS];
+        memset(in_used, 0, sizeof(in_used));
+        memset(out_used, 0, sizeof(out_used));
+        TRACE_MSG1(PCD, "REQUEST ENDPOINTS: %d", endpointsRequested);
+        for (in = out = 1, i = 0; i < endpointsRequested; i++) {
+                struct usbd_endpoint_map *endpoint_map = endpoint_map_array + i;
+                u8 bmAttributes = requestedEndpoints[i].bmAttributes;
+                u16 transferSize = requestedEndpoints[i].fs_requestedTransferSize;
+                switch(bmAttributes) {
+                case USB_DIR_IN | USB_ENDPOINT_INTERRUPT:
+                case USB_DIR_IN | USB_ENDPOINT_INTERRUPT | USB_ENDPOINT_OPT:
+                case USB_DIR_IN | USB_ENDPOINT_BULK:
+                        CONTINUE_IF(!pcd_assign_endpoint(in++, in_used, endpoint_map, bmAttributes, 0x40, transferSize));
+                        THROW(error);
+
+                case USB_DIR_OUT | USB_ENDPOINT_BULK:
+                case USB_DIR_OUT | USB_ENDPOINT_INTERRUPT:
+                case USB_DIR_OUT | USB_ENDPOINT_INTERRUPT | USB_ENDPOINT_OPT:
+                        CONTINUE_IF(!pcd_assign_endpoint(out++, out_used, endpoint_map, bmAttributes, 0x40, transferSize));
+                        //CONTINUE_IF(!pcd_assign_endpoint(in++, out_used, endpoint_map, bmAttributes, 0x40, transferSize));
+                        THROW(error);
+
+                case USB_ENDPOINT_CONTROL:
+                        continue;
+                case USB_DIR_IN | USB_ENDPOINT_ISOCHRONOUS:
+                case USB_DIR_OUT | USB_ENDPOINT_ISOCHRONOUS:
+                        THROW(error);
+                }
+                THROW(error);
+                CATCH(error) {
+                        printk(KERN_ERR"%s: FAILED num: %d bmAttributes: %02x transferSize: %02x\n", 
+                                        __FUNCTION__, i, bmAttributes, transferSize);
+                        return -EINVAL;
+                }
+        }
+        return 0;
+}
+
+/*! 
+ * fs_pcd_set_endpoints() - setup the physical endpoints for the endpoint map
+ * @param pcd
+ * @param endpointsRequested
+ * @param endpoint_map_array
+ */
+static int 
+fs_pcd_set_endpoints (struct pcd_instance *pcd, int endpointsRequested, struct usbd_endpoint_map *endpoint_map_array)
+{
+        fs_clear_words((volatile u32 *)MX2_IO_ADDRESS(OTG_EP_BASE), (32*16/4));
+        //fs_clear((void *)MX2_IO_ADDRESS(OTG_EP_BASE), 0x200);
+        TRACE_MSG0(PCD, "Enable system control interrupts");
+        fs_wl(OTG_SYS_CTRL, SYS_CTRL_I2C_WU_INT_EN | SYS_CTRL_OTG_WU_INT_EN | SYS_CTRL_HOST_WU_INT_EN | SYS_CTRL_FNT_WU_INT_EN);
+        return 0;
+}
+
+/*! 
+ * fs_pcd_framenum() - get current framenum
+ */             
+static u16 
+fs_pcd_framenum (void)
+{               
+        return fs_rl(OTG_FUNC_FRM_NUM);
+}               
+
+/* 
+ * fs_endpoint_halted() - is endpoint halted
+ */
+static int 
+fs_endpoint_halted (struct pcd_instance *pcd, struct usbd_endpoint_instance *endpoint)
+{
+        int epn = endpoint->bEndpointAddress & 0x7f;
+        int dir = (endpoint->bEndpointAddress & 0x80) ? 1 : 0;
+
+        TRACE_MSG2(PCD, "epn: %02x dir: %x", epn, dir);
+        return fs_rl(ep_word(epn, dir, 0))  & (1 << 31) ? TRUE : FALSE;
+}
+
+/* fs_halt_endpoint - halt endpoint
+ */
+int fs_halt_endpoint(struct pcd_instance *pcd, struct usbd_endpoint_instance *endpoint, int flag)
+{
+        int epn = endpoint->bEndpointAddress & 0x7f;
+        int dir = (endpoint->bEndpointAddress & 0x80) ? 1 : 0;
+        TRACE_MSG3(PCD, "epn: %02x dir: %x flag: %d", epn, dir, flag);
+        fs_ep_config_nodma(epn, USB_DIR_IN, flag, endpoint->wMaxPacketSize, endpoint->bmAttributes & 0x3, 0, 0, 1);
+        return 0;
+}
+
+/* ********************************************************************************************* */
+
+#if !defined(OTG_C99)
+struct usbd_pcd_ops usbd_pcd_ops;
+struct pcd_ops pcd_ops;
+
+/*!
+ * fs_pcd_global_init() - initialize globals
+ */
+static void 
+fs_pcd_global_init(void)
+{
+        ZERO(usbd_pcd_ops);
+        usbd_pcd_ops.bmAttributes = USB_OTG_HNP_SUPPORTED | USB_OTG_SRP_SUPPORTED;
+        usbd_pcd_ops.max_endpoints =  UDC_MAX_ENDPOINTS;
+        usbd_pcd_ops.ep0_packetsize =  EP0_PACKETSIZE;
+        usbd_pcd_ops.capabilities = REMOTE_WAKEUP_SUPPORTED;
+        usbd_pcd_ops.name =  UDC_NAME;
+        usbd_pcd_ops.start = fs_pcd_start;
+        usbd_pcd_ops.stop = fs_pcd_stop;
+        usbd_pcd_ops.start_endpoint_in = fs_pcd_start_endpoint_in;
+        usbd_pcd_ops.start_endpoint_out = fs_pcd_start_endpoint_out;
+        usbd_pcd_ops.request_endpoints = fs_pcd_request_endpoints;
+        usbd_pcd_ops.set_endpoints = fs_pcd_set_endpoints;
+        usbd_pcd_ops.set_address = fs_pcd_set_address;
+        usbd_pcd_ops.setup_ep = fs_pcd_setup_ep;
+        usbd_pcd_ops.halt_endpoint = fs_halt_endpoint;
+        usbd_pcd_ops.endpoint_halted = fs_endpoint_halted;
+
+        usbd_pcd_ops.cancel_in_irq = fs_pcd_cancel_in_irq;
+        usbd_pcd_ops.cancel_out_irq = fs_pcd_cancel_out_irq;
+
+        ZERO(pcd_ops);
+        pcd_ops.pcd_en_func = fs_pcd_en_func;
+        pcd_ops.pcd_init_func = pcd_init_func;
+	pcd_ops.remote_wakeup_func = fs_remote_wakeup;
+	pcd_ops.framenum = fs_pcd_framenum;
+}
+#else /* !defined(OTG_C99) */
+static struct 
+usbd_pcd_ops usbd_pcd_ops = {
+        .bmAttributes = USB_OTG_HNP_SUPPORTED | USB_OTG_SRP_SUPPORTED,
+        .max_endpoints =  UDC_MAX_ENDPOINTS,
+        .ep0_packetsize =  EP0_PACKETSIZE,
+        .capabilities = REMOTE_WAKEUP_SUPPORTED,
+        .name =  UDC_NAME,
+        .start = fs_pcd_start,
+        .stop = fs_pcd_stop,
+        .start_endpoint_in = fs_pcd_start_endpoint_in,
+        .start_endpoint_out = fs_pcd_start_endpoint_out,
+        .request_endpoints = fs_pcd_request_endpoints,
+        .set_endpoints = fs_pcd_set_endpoints,
+        .set_address = fs_pcd_set_address,
+        .setup_ep = fs_pcd_setup_ep,
+        .halt_endpoint = fs_halt_endpoint,
+        .endpoint_halted = fs_endpoint_halted,
+
+        .cancel_in_irq = fs_pcd_cancel_in_irq,
+        .cancel_out_irq = fs_pcd_cancel_out_irq,
+};
+
+struct pcd_ops pcd_ops = {
+        .pcd_en_func = fs_pcd_en_func,
+        .pcd_init_func = pcd_init_func,
+	.remote_wakeup_func = fs_remote_wakeup,
+	.framenum = fs_pcd_framenum,
+};
+#endif /* !defined(OTG_C99) */
+
+/* @} */
+
diff -uNr linux/drivers/no-otg/ocd/gen/gen.c linux/drivers/otg/ocd/gen/gen.c
--- linux/drivers/no-otg/ocd/gen/gen.c	1970-01-01 01:00:00.000000000 +0100
+++ linux/drivers/otg/ocd/gen/gen.c	2006-09-01 21:41:31.000000000 +0200
@@ -0,0 +1,268 @@
+/*
+ * otg/ocd/gen/gen.c -- USB Device Controller driver 
+ *
+ *      Copyright (c) 2004 Belcarra
+ *
+ * By: 
+ *      Stuart Lynne <sl@lbelcarra.com>, 
+ *      Tom Rushworth <tbr@belcarra.com>, 
+ *      Bruce Balden <balden@belcarra.com>
+ *
+ */
+/*!
+ * @file otg/ocd/gen/gen.c
+ * @brief Sample driver
+ *
+ *
+ * @ingroup USBP
+ */
+
+#include <udc.h>
+#include <asm/hardware.h>
+#include "gen.h"
+
+
+static unsigned char usb_address;
+
+/* ********************************************************************************************* */
+/* udc_write_buffer - write a buffer to the udc fifo
+ */
+static void udc_write_buffer (unsigned char ep, unsigned char *b, unsigned char size)
+{
+}
+
+/* udc_read_buffer - fill a buffer from the udc fifo
+ */
+static void udc_read_buffer (unsigned char ep, unsigned char *b, unsigned char size)
+{
+}
+
+/* udc_out - process an OUT interrupt
+ */
+static void udc_out (struct usbd_endpoint_instance *endpoint)
+{
+}
+
+/* udc_in  - process an IN interrupt
+ */
+static void udc_in (struct usbd_endpoint_instance *endpoint)
+{
+}
+
+/* ********************************************************************************************* */
+/* udc_in_ep0 - start transmit of ep0 Control Read data
+ */
+static void udc_in_ep0 (struct usbd_endpoint_instance *endpoint)
+{
+        struct usbd_urb *tx_urb = pcd_tx_complete_irq (endpoint, 0);
+        if (!tx_urb || pcd_tx_sendzlp (endpoint)) {
+                udc_write_buffer (0, NULL, 0);
+                return;
+        }
+        TRACE_MSG16("EP0 IN: actual: %d sent: %d", endpoint->tx_urb->actual_length, endpoint->sent);
+        endpoint->last = MIN (tx_urb->actual_length - endpoint->sent, endpoint->wMaxPacketSize);
+        udc_write_buffer (0, tx_urb->buffer + endpoint->sent, endpoint->last);
+}
+
+/* udc_out_ep0 - start receive of ep0 Control Write data
+ */
+static void udc_out_ep0 (struct usbd_endpoint_instance *endpoint)
+{
+        struct usbd_urb *rcv_urb = pcd_rcv_next_irq (endpoint);
+        int size = 0; 
+        RETURN_IF(!rcv_urb);
+        TRACE_MSG8("OUT EP0: actual: %d buffer_length: %d size: %d", rcv_urb->actual_length, rcv_urb->buffer_length, size, 0);
+        udc_read_buffer (0, rcv_urb->buffer + rcv_urb->actual_length, size);
+        if (!pcd_rcv_complete_irq (endpoint, size, 0)) 
+                endpoint->state = WAIT_FOR_SETUP;
+}
+
+/* udc_ep0_setup - process a Control Setup command
+ */
+static void udc_ep0_setup (struct usbd_endpoint_instance *endpoint)
+{
+        static struct usbd_device_request request;
+        u8 *cp = (u8 *) &request;
+        int i;
+        TRACE_MSG32("EP0 SETUP: tx_urb: %p", (int)endpoint->tx_urb);
+        pcd_tx_cancelled_irq (endpoint);
+        pcd_rcv_cancelled_irq (endpoint);
+        if (pcd_recv_setup_irq (&request)) {              // process setup packet
+                TRACE_MSG("udc_ep0: setup failed");
+                //udc_stall_ep (0);
+                endpoint->state = WAIT_FOR_SETUP;
+                return;
+        }
+        if ((request.bmRequestType & USB_REQ_DIRECTION_MASK) == USB_REQ_HOST2DEVICE) {
+                TRACE_MSG32("udc_ep0: send zlp %d", le16_to_cpu (request.wLength));     // XXX 
+                //ctrl_outb (EP0s_RDFN, USBTRG);
+                RETURN_IF(pcd_rcv_next_irq (endpoint));
+                //and_b (~EP0o_TS, USBIER0);
+                return;
+        }
+}
+/* ********************************************************************************************* */
+/* udc_int_hndlr - interrupt handler
+ */
+static void udc_int_hndlr (int irq, void *dev_id, struct pt_regs *regs)
+{
+        trace_ops.interrupts++;
+#if 0
+        if (trace_ops.interrupts > 1000) { udc_disable_interrupts (); udc_disable (); TRACE_MSG("UDC DISABLE"); return; }
+#endif
+}
+/* ********************************************************************************************* */
+/* udc_start_endpoint_in - start transmit
+ */
+void udc_start_endpoint_in (struct usbd_endpoint_instance *endpoint)
+{               
+}
+
+/* udc_start_endpoint_out - start receive
+ */
+void udc_start_endpoint_out (struct usbd_endpoint_instance *endpoint)
+{
+}
+/* ********************************************************************************************* */
+/* udc_attached - is the USB cable attached
+ * Return non-zero if cable is attached.
+ */
+int udc_attached (void)
+{
+        return 0;
+}
+
+static int udc_connect_status;
+
+/* udc_connected - is the USB pullup enabled
+ * Return non-zero if pullup is enabled.
+ */
+int udc_connected (void)
+{
+        return udc_connect_status;
+}
+
+/* udc_connect - enable pullup resistor
+ */
+void udc_connect (void)
+{
+        udc_connect_status = 1;
+}
+
+/* udc_disconnect - disable pullup resistor
+ */
+void udc_disconnect (void)
+{
+        udc_connect_status = 0;
+}
+
+/* udc_start - start session
+ */
+void udc_start (void)
+{
+        // udc_enable_interrupts ();
+}
+
+/* udc_stop - stop session
+ */
+void udc_stop (void)
+{
+        // udc_disable_interrupts ();
+}
+
+/* udc_enable - enable the UDC
+ */
+void udc_enable (void)
+{
+}
+
+/* udc_disable - disable the UDC
+ */
+void udc_disable (void)
+{
+}
+
+/* udc_init - initialize
+ * Return non-zero if we cannot see device.
+ */
+int udc_init (void)
+{
+        return 0;
+}
+
+/* udc_exit - disable the UDC
+ */
+void udc_exit (void)
+{
+}
+
+/* ********************************************************************************************* */
+/* udc_assign_endpoint
+ */
+int udc_assign_endpoint (u8 physicalEndpoint, int used[6], struct usbd_endpoint_map *endpoint_map, u8 bmAttributes,
+                u16 wMaxPacketSize, u16 transferSize) 
+{
+        RETURN_EINVAL_IF(used[physicalEndpoint]);
+        endpoint_map->bEndpointAddress[0] = physicalEndpoint | (bmAttributes & USB_IN_DIR);
+        endpoint_map->physicalEndpoint[0] = physicalEndpoint;
+        endpoint_map->wMaxPacketSize[0] = wMaxPacketSize;
+        endpoint_map->transferSize[0] = transferSize;
+        endpoint_map->bmAttributes[0] = bmAttributes;
+        used[physicalEndpoint]++;
+        return 0;
+}
+
+/* udc_request_endpoints - process endpoint request list
+ */
+int udc_request_endpoints (struct usbd_endpoint_map *endpoint_map_array, int endpointsRequested, 
+                struct usbd_endpoint_request *requestedEndpoints)
+{
+        struct usbd_device_description *device_description;
+        int i, j;
+        int used[UDC_MAX_ENDPOINTS];
+        memset (used, 0, sizeof (used));
+        for (j = 0, i = 0; i < endpointsRequested; i++) {
+                struct usbd_endpoint_map *endpoint_map = endpoint_map_array + i;
+                u8 bmAttributes = requestedEndpoints[i].bmAttributes;
+                u16 transferSize = requestedEndpoints[i].fs_requestedTransferSize;
+                switch (bmAttributes) {
+                case USB_DIR_IN | USB_ENDPOINT_INTERRUPT:
+                case USB_DIR_IN | USB_ENDPOINT_INTERRUPT | USB_ENDPOINT_OPT:
+                case USB_DIR_IN | USB_ENDPOINT_BULK:
+                case USB_DIR_OUT | USB_ENDPOINT_BULK:
+                        BREAK_IF(!udc_assign_endpoint (j++, used, endpoint_map, bmAttributes, 0x40, transferSize));
+                        return -EINVAL;
+                case USB_ENDPOINT_CONTROL:
+                        continue;
+                case USB_DIR_OUT | USB_ENDPOINT_INTERRUPT:
+                case USB_DIR_OUT | USB_ENDPOINT_INTERRUPT | USB_ENDPOINT_OPT:
+                case USB_DIR_IN | USB_ENDPOINT_ISOCHRONOUS:
+                case USB_DIR_OUT | USB_ENDPOINT_ISOCHRONOUS:
+                        return -EINVAL;
+                }
+                return -EINVAL;
+                }
+                RETURN_EINVAL_UNLESS (j < UDC_MAX_ENDPOINTS);
+        }
+        return 0;
+}
+/* ********************************************************************************************* */
+struct udc_ops udc_ops = {
+        max_endpoints:  UDC_MAX_ENDPOINTS,
+        ep0_packetsize:  EP0_PACKETSIZE,
+        name:  UDC_NAME,
+        init: udc_init,
+        exit: udc_exit,
+        enable: udc_enable,
+        disable: udc_disable,
+        start: udc_start,
+        stop: udc_stop,
+        start_endpoint_in: udc_start_endpoint_in,
+        start_endpoint_out: udc_start_endpoint_out,
+        request_endpoints: udc_request_endpoints,
+        attached: udc_attached,
+        connected: udc_connected,
+        connect: udc_connect,
+        disconnect: udc_disconnect,
+};
+
diff -uNr linux/drivers/no-otg/ocd/gen/gen.h linux/drivers/otg/ocd/gen/gen.h
--- linux/drivers/no-otg/ocd/gen/gen.h	1970-01-01 01:00:00.000000000 +0100
+++ linux/drivers/otg/ocd/gen/gen.h	2006-09-01 21:41:31.000000000 +0200
@@ -0,0 +1,29 @@
+/*
+ * otg/ocd/gen/gen.h
+ *
+ *      Copyright (c) 2004 Belcarra
+ *
+ * By: 
+ *      Stuart Lynne <sl@belcarra.com>, 
+ *      Tom Rushworth <tbr@belcarra.com>, 
+ *      Bruce Balden <balden@belcarra.com>
+ *
+ */
+
+/*!
+ * @file otg/ocd/gen/gen.h
+ * @brief Private structures and defines for Intel PXA-270
+ *
+ * Notes
+ *
+ * 1. This is a minmal outline for a simple UDC driver.
+ *
+ *
+ * @ingroup BVD
+ */
+
+
+#define UDC_NAME                "GEN"
+#define UDC_MAX_ENDPOINTS       3
+#define EP0_PACKETSIZE          8
+
diff -uNr linux/drivers/no-otg/ocd/isp1301/Config.in linux/drivers/otg/ocd/isp1301/Config.in
--- linux/drivers/no-otg/ocd/isp1301/Config.in	1970-01-01 01:00:00.000000000 +0100
+++ linux/drivers/otg/ocd/isp1301/Config.in	2006-09-01 21:41:31.000000000 +0200
@@ -0,0 +1,17 @@
+
+#
+# Copyright (c) 2004 Belcarra
+#
+# ISP 1301 TCD
+
+if [ "$CONFIG_OTG_ISP1301" = "y" ]; then
+    mainmenu_option next_comment
+
+    comment 'ISP 1301 (i2c)'
+   
+        dep_bool 'Proc FS debug' CONFIG_OTG_ISP1301_PROCFS $CONFIG_OTG_ISP1301
+
+    endmenu
+else
+    define_bool CONFIG_OTG_ISP1301_PROCFS n
+fi
diff -uNr linux/drivers/no-otg/ocd/isp1301/Kconfig linux/drivers/otg/ocd/isp1301/Kconfig
--- linux/drivers/no-otg/ocd/isp1301/Kconfig	1970-01-01 01:00:00.000000000 +0100
+++ linux/drivers/otg/ocd/isp1301/Kconfig	2006-09-01 21:41:31.000000000 +0200
@@ -0,0 +1,15 @@
+
+#
+# Copyright (c) 2004 Belcarra
+#
+
+# OMAP 1610 H2 development board
+#
+config OTG_ISP1301_OMAP_H2
+	tristate "OMAP 1610 H2 Development board - ISP 1301 (i2c)"
+	depends on I2C && OTG && ARCH_OMAP && OMAP_H2
+
+config OTG_ISP1301_MX2ADS
+	tristate "MX2ADS Development board - ISP 1301 (i2c)"
+	depends on I2C && OTG && ARCH_MX2ADS 
+
diff -uNr linux/drivers/no-otg/ocd/isp1301/Makefile linux/drivers/otg/ocd/isp1301/Makefile
--- linux/drivers/no-otg/ocd/isp1301/Makefile	1970-01-01 01:00:00.000000000 +0100
+++ linux/drivers/otg/ocd/isp1301/Makefile	2006-09-01 21:41:31.000000000 +0200
@@ -0,0 +1,96 @@
+#
+# Makefile for the kernel USBD (device not host) drivers.
+#
+# Copyright (c) 2004 Belcarra
+
+# Subdirs.
+# This is a bit complex, because some subdirs are for
+# proprietary code, and are simply not present in a
+# general distribution.
+
+# The all-CAPS *_DIRS get nuked in the new versions
+# of Rules.make, so use only the subdir-* methods.
+subdir-y 	:=
+subdir-m 	:=
+subdir-n 	:=
+subdir- 	:=
+
+# The target object and module list name.
+
+O_TARGET	:= isp1301_target.o
+list-multi      := isp1301_tcd.o
+
+# Objects that export symbols.
+
+# Multipart objects.
+
+omap_h2_tcd-objs	:= tcd-init-l24.o tcd-omap-h2.o isp1301.o i2c-l26.o tcd.o isp1301-procfs.o
+mainstone_tcd-objs	:= tcd-init-l24.o tcd-mainstone.o tcd.o i2c-l26.o isp1301.o isp1301-procfs.o thread-l24.o
+
+# Optional parts of multipart objects.
+
+# Object file lists.
+
+obj-y		:=
+obj-m		:=
+obj-n		:=
+obj-		:=
+
+# Each configuration option enables a list of files.
+
+obj-$(CONFIG_OTG_ISP1301_OMAP_H2)	+= omap_h2_tcd.o
+obj-$(CONFIG_OTG_ISP1301_MAINSTONE)	+= mainstone_tcd.o
+
+# Object files in subdirectories
+
+
+# Extract lists of the multi-part drivers.
+# The 'int-*' lists are the intermediate files used to build the multi's.
+
+multi-y		:= $(filter $(list-multi), $(obj-y))
+multi-m		:= $(filter $(list-multi), $(obj-m))
+int-y		:= $(sort $(foreach m, $(multi-y), $($(basename $(m))-objs)))
+int-m		:= $(sort $(foreach m, $(multi-m), $($(basename $(m))-objs)))
+
+# Files that are both resident and modular: remove from modular.
+
+obj-m		:= $(filter-out $(obj-y), $(obj-m))
+int-m		:= $(filter-out $(int-y), $(int-m))
+
+# Translate to Rules.make lists.
+
+O_OBJS		:= $(filter-out $(export-objs), $(obj-y))
+OX_OBJS		:= $(filter     $(export-objs), $(obj-y))
+M_OBJS		:= $(sort $(filter-out $(export-objs), $(obj-m)))
+MX_OBJS		:= $(sort $(filter     $(export-objs), $(obj-m)))
+MI_OBJS		:= $(sort $(filter-out $(export-objs), $(int-m)))
+MIX_OBJS	:= $(sort $(filter     $(export-objs), $(int-m)))
+
+# The global Rules.make.
+
+DOT_DIR=$(TCD_DIR)/isp1301
+
+include $(TOPDIR)/Rules.make
+OTG=$(TOPDIR)/drivers/otg
+OCD_DIR=$(OTG)/ocd
+INCLUDE_DIRS =          -I$(OTG) 
+EXTRA_CFLAGS +=          -Wno-missing-prototypes -Wno-unused -Wno-format ${INCLUDE_DIRS}
+EXTRA_CFLAGS_nostdinc += -Wno-missing-prototypes -Wno-unused -Wno-format ${INCLUDE_DIRS}
+
+PCD=$(OTG)/ocd/otg-pcd
+vpath %.c $(USBDCORE_DIR) $(OCD_DIR) $(PCD)
+
+
+# Link rules for multi-part drivers.
+
+omap_h2_tcd.o: $(omap_h2_tcd-objs)
+	$(LD) -r -o $@ $(iomap_h2_tcd-objs)
+
+mainstone_tcd.o: $(mainstone_tcd-objs)
+	$(LD) -r -o $@ $(mainstone_tcd-objs)
+
+# dependencies:
+
+# local
+
+
diff -uNr linux/drivers/no-otg/ocd/isp1301/Makefile-l26 linux/drivers/otg/ocd/isp1301/Makefile-l26
--- linux/drivers/no-otg/ocd/isp1301/Makefile-l26	1970-01-01 01:00:00.000000000 +0100
+++ linux/drivers/otg/ocd/isp1301/Makefile-l26	2006-09-01 21:41:31.000000000 +0200
@@ -0,0 +1,26 @@
+#
+# Makefile for the kernel USBD (device not host) drivers.
+#
+# Copyright (c) 2004 Belcarra Technologies Corp
+
+DOT_DIR=$(TCD_DIR)/isp1301
+
+OTG=$(TOPDIR)/drivers/otg
+HCD_DIR=$(OTG)/hcd
+TCD_DIR=$(OTG)/tcd
+USBDCORE_DIR=$(OTG)/usbdcore
+OTGCORE_DIR=$(OTG)/otgcore
+INCLUDE_DIRS =          -I$(OTG) 
+EXTRA_CFLAGS +=          -Wno-missing-prototypes -Wno-unused -Wno-format ${INCLUDE_DIRS}
+EXTRA_CFLAGS_nostdinc += -Wno-missing-prototypes -Wno-unused -Wno-format ${INCLUDE_DIRS}
+
+vpath %.c $(USBDCORE_DIR) $(OCD_DIR) 
+
+
+# Link rules for multi-part drivers.
+
+omap_h2_tcd-objs	:= ../tcd-init-l24.o tcd-omap-h2.o isp1301.o i2c-l26.o ../tcd.o 
+obj-$(CONFIG_OTG_ISP1301_OMAP_H2)	+= omap_h2_tcd.o
+
+
+
diff -uNr linux/drivers/no-otg/ocd/isp1301/isp1301-procfs.c linux/drivers/otg/ocd/isp1301/isp1301-procfs.c
--- linux/drivers/no-otg/ocd/isp1301/isp1301-procfs.c	1970-01-01 01:00:00.000000000 +0100
+++ linux/drivers/otg/ocd/isp1301/isp1301-procfs.c	2006-09-01 21:41:31.000000000 +0200
@@ -0,0 +1,385 @@
+/*
+ * otg/ocd/isp1301/isp1301-procfs.c - USB Device Core Layer
+ *
+ *      Copyright (c) 2004 Belcarra
+ *
+ * By: 
+ *      Stuart Lynne <sl@belcarra.com>, 
+ *      Tom Rushworth <tbr@belcarra.com>, 
+ *      Bruce Balden <balden@belcarra.com>
+ *
+ *
+ */
+/*!
+ * @file otg/ocd/isp1301/isp1301-procfs.c
+ * @brief Implement /proc/isp1301 to dump ISP1301 registers.
+ *
+ *
+ * @ingroup ISP1301TCD
+ */
+
+
+#include <otg/pcd-include.h>
+#include "otghw/isp1301-hardware.h"
+#include <otghw/isp1301.h>
+
+#ifdef CONFIG_ARCH_MX2ADS
+#include <asm/arch/mx2.h>
+#define MX2_OTG_XCVR_DEVAD                      0x18
+#define MX2_SEQ_OP_REG                          0x19
+#define MX2_SEQ_RD_STARTAD                      0x1a
+#define MX2_I2C_OP_CTRL_REG                     0x1b
+#define MX2_SCLK_TO_SCL_HPER                    0x1e
+#define MX2_I2C_INTERRUPT_AND_CTRL              0x1f
+
+#define OTG_BASE_ADDR                           0x10024000
+//#define OTG_I2C_BASE                            (OTG_BASE_ADDR+0x100)
+
+#endif /* CONFIG_ARCH_MX2ADS */
+
+#ifdef CONFIG_OTG_ISP1301_PROCFS
+/* Proc Filesystem *************************************************************************** */
+
+extern struct isp1301_private isp1301_private;
+
+#define MAX_HISTORY     6
+struct reg_list {
+        u8 reg;
+        u8 size;
+        char *name;
+        u32 values[MAX_HISTORY];
+};
+
+#define REG(r, s)  {r, s, #r, }
+
+struct reg_list isp1301_prod_list[] = {
+        REG(ISP1301_VENDOR_ID, 2),
+        REG(ISP1301_PRODUCT_ID, 2),
+        REG(ISP1301_VERSION_ID, 2),
+        { 0, 1, NULL,}, 
+};
+struct reg_list isp1301_reg_list[] = {
+        REG(ISP1301_OTG_CONTROL_SET, 1),
+        REG(ISP1301_INTERRUPT_SOURCE, 1),
+        REG(ISP1301_INTERRUPT_LATCH_SET, 1),
+        REG(ISP1301_INTERRUPT_ENABLE_LOW_SET, 1),
+        REG(ISP1301_INTERRUPT_ENABLE_HIGH_SET, 1),
+        REG(ISP1301_MODE_CONTROL_1_SET, 1),
+        { 0, 1, NULL,}, 
+};
+struct reg_list isp1301_spec_list[] = {
+        REG(ISP1301_MODE_CONTROL_2_SET, 1),
+        REG(ISP1301_OTG_STATUS, 1),
+        { 0, 1, NULL,}, 
+};
+struct reg_list max3301e_spec_list[] = {
+        REG(MAX3301E_SPECIAL_FUNCTION_1_SET, 1),
+        REG(MAX3301E_SPECIAL_FUNCTION_2_SET, 1),
+        { 0, 1, NULL,}, 
+};
+#ifdef CONFIG_ARCH_MX2ADS
+struct reg_list mx21_spec_list[] = {
+        REG(MX2_OTG_XCVR_DEVAD, 1),
+        REG(MX2_SEQ_OP_REG, 1),
+        REG(MX2_SEQ_RD_STARTAD, 1),
+        REG(MX2_I2C_OP_CTRL_REG, 1),
+        REG(MX2_SCLK_TO_SCL_HPER, 1),
+        REG(MX2_I2C_INTERRUPT_AND_CTRL, 1),
+        { 0, 1, NULL,}, 
+};
+#endif /* CONFIG_ARCH_MX2ADS */
+
+void isp1301_update(struct reg_list *list)
+{
+        for (; list && list->name; list++) {
+                TRACE_MSG1(TCD, "list: %s", list->name);
+                memmove(list->values + 1, list->values, sizeof(list->values) - sizeof(u32));
+                switch(list->size) {
+                case 1: 
+                        list->values[0] = i2c_readb(list->reg); 
+                        break;
+                case 2: 
+                        list->values[0] = i2c_readw(list->reg); 
+                        break;
+                case 4: 
+                        list->values[0] = i2c_readl(list->reg); 
+                        break;
+                }
+        }
+}
+
+#ifdef CONFIG_ARCH_MX2ADS
+static u8 __inline__ mx2_rb(u32 port)
+{
+        return *(volatile u8 *) (MX2_IO_ADDRESS(port + OTG_I2C_BASE));
+}
+
+
+void mx21_update(struct reg_list *list)
+{
+        for (; list && list->name; list++) {
+                memmove(list->values + 1, list->values, sizeof(list->values) - sizeof(u32));
+                list->values[0] = mx2_rb(list->reg); 
+        }
+}
+
+#endif /* CONFIG_ARCH_MX2ADS */
+
+void isp1301_update_all(void)
+{
+        isp1301_update(isp1301_reg_list);
+        switch (isp1301_private.transceiver_map->transceiver_type) {
+        case isp1301:
+                isp1301_update(isp1301_spec_list);
+                break;
+        case max3301e:
+                isp1301_update(max3301e_spec_list);
+                break;
+        default:
+                break;
+        }
+#ifdef CONFIG_ARCH_MX2ADS
+        mx21_update(mx21_spec_list);
+#endif /* CONFIG_ARCH_MX2ADS */
+
+}
+
+
+
+/*
+ * dohex
+ *
+ */
+static void dohexdigit (char *cp, unsigned char val)
+{
+        if (val < 0xa) 
+                *cp = val + '0';
+        else if ((val >= 0x0a) && (val <= 0x0f)) 
+                *cp = val - 0x0a + 'a';
+}
+
+/*
+ * dohex
+ *
+ */
+static void dohexval (char *cp, unsigned char val)
+{
+        dohexdigit (cp++, val >> 4);
+        dohexdigit (cp++, val & 0xf);
+}
+
+int isp1301_dump(char *buf, char *name, char *fmt, struct reg_list *reg)
+{
+        int len = 0, i;
+        len += sprintf (buf + len, "%-20s  %-34s [%03x]:  ", name, reg->name, reg->reg);
+        len += sprintf (buf + len, fmt, reg->values[0]);
+        for (i = 1; i < MAX_HISTORY; i++) 
+                if (reg->values[i - 1] == reg->values[i]) 
+                        len += sprintf (buf + len, "         ");
+                else 
+                        len += sprintf (buf + len, fmt, reg->values[i]);
+        len += sprintf (buf + len, "\n");
+        return len;
+}
+
+int isp1301_dump_list(char * buf, char *name, struct reg_list *list)
+{
+        int len = 0;
+        for (; list && list->name; list++) 
+                switch(list->size) {
+                case 1: len += isp1301_dump(buf + len, name, "       %02x", list); break;
+                case 2: len += isp1301_dump(buf + len, name, "     %04x", list); break;
+                case 4: len += isp1301_dump(buf + len, name, " %08x", list); break;
+                }
+        return len;
+}
+
+char *isp1301_otg_control[8] = {
+        "DP_PULLUP",
+        "DM_PULLUP",
+        "DP_PULLDOWN",
+        "DM_PULLDOWN",
+        "ID_PULLDOWN",
+        "VBUS_DRV",
+        "VBUS_DISCHRG",
+        "VBUS_CHRG",
+};
+char *isp1301_interrupt_source[8] = {
+        "VBUS_VLD",
+        "SESS_VLD",
+        "DP_HI",
+        "ID_GND",
+        "DM_HI",
+        "ID_FLOAT",
+        "BDIS_ACON",
+        "CR_INT",
+};
+
+char *isp1301_mode_control_1[8] = {
+        "SPEED_REG",
+        "SUSPEND_REG",
+        "DAT_SE0",
+        "TRANSP_EN",
+        "BDIS_ACON_EN",
+        "OE_INT_EN",
+        "UART_EN",
+        NULL,
+};
+
+char *isp1301_otg_status[8] = {
+        NULL, NULL, NULL, NULL, NULL, NULL,
+        "B_SESS_END",
+        "B_SESS_VLD",
+};
+
+int isp1301_detailed(char *buf, char *name, u8 reg, char **detail)
+{
+        u8 val;
+        int i;
+        int len = 0;
+
+        val = i2c_readb(reg); 
+        for (i = 7; i >= 0; i--) {
+                
+                if (3 == (i % 4)) 
+                        len += sprintf (buf + len, "\n%-20s [%02d] ", name, reg);
+
+                if (detail[i])
+                        len += sprintf (buf + len, "%14s%s ", detail[i], (val & (1 << i)) ? " " : "/");
+                else
+                        len += sprintf (buf + len, "%14s%s ", "",  " ");
+        }
+        len += sprintf (buf + len, "\n", name);
+
+        return len;
+}
+
+int isp1301_dump_all(char *buf)
+{
+        int len = 0;
+        len += isp1301_dump_list(buf + len, "ISP1301 Standard", isp1301_reg_list);
+        switch (isp1301_private.transceiver_map->transceiver_type) {
+        case isp1301:
+                len += isp1301_dump_list(buf + len, "ISP1301 Extra", isp1301_spec_list);
+                break;
+        case max3301e:
+                len += isp1301_dump_list(buf + len, "MAX3301E Extra", max3301e_spec_list);
+                break;
+        default:
+                break;
+        }
+#ifdef CONFIG_ARCH_MX2ADS
+        len += isp1301_dump_list(buf + len, "MX21 ADS Extra", mx21_spec_list);
+#endif /* CONFIG_ARCH_MX2ADS */
+        return len;
+}
+
+int isp1301_dump_detail(char *buf)
+{
+        int len = 0;
+
+        len += isp1301_detailed(buf + len, "MODE CONTROL 1", ISP1301_MODE_CONTROL_1, isp1301_mode_control_1);
+        len += isp1301_detailed(buf + len, "INTERRUPT ENABLE", ISP1301_INTERRUPT_ENABLE_HIGH, isp1301_interrupt_source);
+
+        len += isp1301_detailed(buf + len, "OTG CONTROL", ISP1301_OTG_CONTROL_SET, isp1301_otg_control);
+        len += isp1301_detailed(buf + len, "INTERRUPT SOURCE", ISP1301_INTERRUPT_SOURCE, isp1301_interrupt_source);
+        len += isp1301_detailed(buf + len, "OTG STATUS", ISP1301_OTG_STATUS, isp1301_otg_status);
+        return len;
+}
+
+
+/*!
+ * isp1301_device_proc_read - implement proc file system read.
+ *
+ * Standard proc file system read function.
+ *
+ * We let upper layers iterate for us, *pos will indicate which device to return
+ * statistics for.
+ */
+static ssize_t isp1301_device_proc_read_functions (struct file *file, char *buf, size_t count, loff_t * pos)
+{
+        unsigned long page;
+        int len = 0;
+        int index;
+        int i;
+        u32 r;
+
+        int config_size;
+
+        // get a page, max 4095 bytes of data...
+        RETURN_EINVAL_UNLESS ((page = get_free_page (GFP_KERNEL)));
+
+        len = 0;
+        index = (*pos)++;
+
+        //printk(KERN_INFO"%s: index: %d\n", __FUNCTION__, index);
+        switch(index) {
+        case 0:
+                len += sprintf ((char *) page + len, "ISP1301 Transceiver Registers\n");
+
+                TRACE_MSG0(TCD, "UPDATING");
+                isp1301_update_all();
+                TRACE_MSG0(TCD, "UPDATE FINISHED");
+                len += sprintf ((char *) page + len , "Vendor: %04x Product: %04x Revision: %04x %s\n", 
+                                isp1301_private.vendor, isp1301_private.product,
+                                isp1301_private.revision, isp1301_private.transceiver_map->name
+                                );
+
+                len += isp1301_dump_all((char *) page + len);
+                len += sprintf ((char *) page + len, "\n");
+
+                break;
+
+        case 1:
+                len += sprintf ((char *) page + len, "\n--\n");
+                len += isp1301_dump_detail((char *) page + len);
+                len += sprintf ((char *) page + len, "\n");
+                break;
+
+        default:
+                break;
+
+        }
+
+        //printk(KERN_INFO"%s: len: %d\n", __FUNCTION__, len);
+
+        if (len > count) 
+                len = -EINVAL;
+        
+        else if ((len > 0) && copy_to_user (buf, (char *) page, len)) 
+                len = -EFAULT;
+        
+        //printk(KERN_INFO"%s: len: %d\n", __FUNCTION__, len);
+        free_page (page);
+        return len;
+}
+
+static struct file_operations isp1301_device_proc_operations_functions = {
+        read:isp1301_device_proc_read_functions,
+};
+
+/* Module init ******************************************************************************* */
+
+static int isp1301_procfs_init (void)
+{
+        struct proc_dir_entry *p;
+
+        // create proc filesystem entries
+        if ((p = create_proc_entry ("isp1301", 0, 0)) == NULL)
+                return -ENOMEM;
+
+        p->proc_fops = &isp1301_device_proc_operations_functions;
+
+        isp1301_update_all();
+
+        return 0;
+}
+static void isp1301_procfs_exit (void)
+{
+        remove_proc_entry ("isp1301", NULL);
+}
+#else
+static int isp1301_procfs_init (void) { return 0;
+}
+static void isp1301_procfs_exit (void) { }
+#endif
diff -uNr linux/drivers/no-otg/ocd/isp1301/isp1301.c linux/drivers/otg/ocd/isp1301/isp1301.c
--- linux/drivers/no-otg/ocd/isp1301/isp1301.c	1970-01-01 01:00:00.000000000 +0100
+++ linux/drivers/otg/ocd/isp1301/isp1301.c	2006-09-01 21:41:31.000000000 +0200
@@ -0,0 +1,923 @@
+/*
+ * otg/ocd/isp1301/isp1301.c -- USB Transceiver Controller driver
+ *
+ *      Copyright (c) 2004 Belcarra
+ *
+ * By:
+ *      Stuart Lynne <sl@lbelcarra.com>,
+ *      Tom Rushworth <tbr@belcarra.com>,
+ *      Bruce Balden <balden@belcarra.com>
+ *
+ */
+/*!
+ * @file otg/ocd/isp1301/isp1301.c
+ * @brief ISP1301 OTG Transceiver Driver.
+ *
+ * This is the generic ISP1301 TCD core support.
+ *
+ * Notes:
+ *
+ * 1. The ISP1301 can control the speed and suspend directly, would it be
+ * appropriate to allow state machine to control this.
+ *
+ * 2. The ISP1301 has auto connect feature, can this be used without change
+ * to state machine.
+ *
+ * 3. The ISP1301 can control the ADR/PSW pin to enable / disable external
+ * charge pump.
+ *
+ * @ingroup ISP1301TCD
+ */
+
+#include <otg/otg-compat.h>
+
+#include <asm/irq.h>
+#include <asm/system.h>
+#include <asm/io.h>
+#include <linux/i2c.h>
+
+#include <otg/usbp-chap9.h>
+#include <otg/usbp-bus.h>
+#include <otg/otg-trace.h>
+#include <otg/otg-api.h>
+#include <otg/otg-hcd.h>
+#include <otg/otg-tcd.h>
+#include <otg/otg-ocd.h>
+#include <otg/otg-pcd.h>
+
+#include <otghw/isp1301-hardware.h>
+#include <otghw/isp1301.h>
+
+#ifdef CONFIG_OMAP_H2
+#include <asm/arch/gpio.h>
+#include <asm/arch/mux.h>
+#endif /* CONFIG_OMAP_H2 */
+
+struct otg_transceiver_map isp1301_transceiver_map[] = {
+        { isp1301, 0x4cc, 0x1301, 0x00, "Phillips ISP1301", },
+        { max3301e, 0x6a0b, 0x0133, 0x00, "Maxim MAX3301E", },
+        { 0, 0, 0, 0, "Unknown Transceiver", },
+};
+
+struct isp1301_private isp1301_private;
+
+
+
+/* ********************************************************************************************* */
+#define TRACE_I2CB(t,r) TRACE_MSG3(t, "%-40s[%02x] %02x", #r, r, i2c_readb(r))
+#define TRACE_I2CW(t,r) TRACE_MSG3(t, "%-40s[%02x] %04x", #r, r, i2c_readw(r))
+#define TRACE_GPIO(t,b,r) TRACE_MSG2(t, "%-40s %04x", #r, readw(b + r))
+#define TRACE_REGL(t,r) TRACE_MSG2(t, "%-40s %08x", #r, readl(r))
+
+
+
+/*! isp1301_int_src - update interrupt source test mask
+ *
+ * This sets the current mask and updates the interrupt registers to match.
+ */
+void isp1301_int_src(u8 int_src)
+{
+        TRACE_MSG1(TCD, "setting int_src %02x", int_src);
+        isp1301_private.int_src = int_src;
+        i2c_writeb(ISP1301_INTERRUPT_ENABLE_LOW_CLR, ~int_src);
+        i2c_writeb(ISP1301_INTERRUPT_ENABLE_HIGH_CLR, ~int_src);
+        i2c_writeb(ISP1301_INTERRUPT_ENABLE_LOW_SET, int_src);
+        i2c_writeb(ISP1301_INTERRUPT_ENABLE_HIGH_SET, int_src);
+        i2c_writeb(ISP1301_INTERRUPT_LATCH_CLR, int_src);
+}
+
+/*! isp1301_int_src_set - add to the interrupt source mask
+ *
+ */
+void isp1301_int_src_set(u8 int_src)
+{
+        isp1301_int_src(int_src |= isp1301_private.int_src);
+}
+
+/*! isp1301_int_src_clr - remove from the interrup source mask
+ */
+void isp1301_int_src_clr(u8 int_src)
+{
+        isp1301_int_src(int_src = isp1301_private.int_src & ~int_src);
+}
+
+/* ********************************************************************************************* */
+
+typedef struct isp1301_test {
+        u32     input;
+        char   *name;
+        u8      mask;
+} isp1301_test_t;
+
+#define OV(i, m) {i, #i, m}
+
+struct isp1301_test isp1301_int_src_tests[9] = {
+
+        OV(VBUS_VLD, ISP1301_VBUS_VLD),
+        OV(A_SESS_VLD, ISP1301_SESS_VLD),
+        OV(DP_HIGH, ISP1301_DP_HI),
+        OV(ID_GND, ISP1301_ID_GND),
+        OV(DM_HIGH, ISP1301_DM_HI),
+        OV(ID_FLOAT, ISP1301_ID_FLOAT),
+        OV(BDIS_ACON, ISP1301_BDIS_ACON),
+        OV(CR_INT_DET, ISP1301_CR_INT),
+        {0, NULL, 0},
+};
+
+struct isp1301_test isp1301_otg_status_tests[3] = {
+
+        OV(B_SESS_END, ISP1301_B_SESS_END),
+        OV(B_SESS_VLD, ISP1301_B_SESS_VLD),
+        {0, NULL, 0},
+};
+
+/*! isp1301_event() - set input bit based on current settings and interrupt mask
+ */
+u64 isp1301_event(struct otg_instance *otg, struct isp1301_test *tests, int val, int testmask, char *msg)
+{
+        int i;
+        u64 inputs = 0;
+        TRACE_MSG3(TCD, "val: %02x mask: %02x %s", val, testmask, msg);
+        for (i = 0; tests[i].mask; i++) {
+                u64 input = tests[i].input;
+                u8 mask = tests[i].mask;
+                CONTINUE_UNLESS(mask & testmask);
+                inputs |= (val & mask) ? input : _NOT(input);
+                TRACE_MSG6(TCD, "%s%s %02x %08x inputs: %08x %08x", 
+                                tests[i].name, (val & mask) ? "" : "/", tests[i].mask, tests[i].input,
+                                (u32) (inputs >> 32), (u32)(inputs & 0xffffffff)
+                                );
+        }
+        return inputs;
+}
+
+
+
+int isp1301_bh_first;
+int isp1301_debounce;
+u64 isp1301_ticks;
+
+void isp1301_info(char *msg, u8 value)
+{
+        u64 new_ticks = otg_tmr_ticks();
+        u64 ticks = otg_tmr_elapsed(&new_ticks, &isp1301_ticks);
+
+        if (ticks < 10000)
+                TRACE_MSG3(PCD, "ISP1301 %d uS %s: %02x", (u32)(ticks & 0xffffffff), msg, value);
+        else if (ticks < 10000000)
+                TRACE_MSG3(PCD, "ISP1301 %d mS %s: %02x", (u32)((ticks >> 10) & 0xffffffff), msg, value);
+        else
+                TRACE_MSG3(PCD, "ISP1301 %d S  %s: %02x", (u32)((ticks >> 20) & 0xffffffff), msg, value);
+
+        isp1301_ticks = new_ticks;
+}
+
+
+void isp1301_trace(u8 int_src, u8 int_changed, char *msg)
+{
+
+        u64 new_ticks = otg_tmr_ticks();
+        u64 ticks = otg_tmr_elapsed(&new_ticks, &isp1301_ticks);
+
+        if (ticks < 10000)
+                TRACE_MSG4(PCD, "ISP1301 %d uS %s: src: %02x chng: %02x", 
+                                (u32)(ticks & 0xffffffff), msg, int_src, int_changed);
+        else if (ticks < 10000000)
+                TRACE_MSG4(PCD, "ISP1301 %d mS %s src: %02x chng: %02x", 
+                                (u32)((ticks >> 10) & 0xffffffff), msg, int_src, int_changed);
+        else
+                TRACE_MSG4(PCD, "ISP1301 %d S  %s src: %02x chng: %02x ", 
+                                (u32)((ticks >> 20) & 0xffffffff), msg, int_src, int_changed);
+
+        isp1301_ticks = new_ticks;
+}
+
+/*! isp1301_bh   
+ */             
+void isp1301_bh(void *arg)
+{               
+        u64 inputs = 0;
+        u8 int_lat, int_src1 = 0, int_src2 = 0, otg_status = 0, special_function_22 = 0;
+        static u8 int_src_saved, otg_status_saved, special_function_22_saved;
+
+        //TRACE_MSG0(TCD, "--");
+        /* read the interrupt latch and and clear latch and update otg  
+         */
+        if ((int_lat = i2c_readb(ISP1301_INTERRUPT_LATCH_SET))) {
+                //TRACE_MSG1(TCD, "CLEARING INT LAT: %02x", int_lat);
+                isp1301_info("INT LAT", int_lat);
+                i2c_writeb(ISP1301_INTERRUPT_LATCH_CLR, int_lat);
+        }
+
+        isp1301_info("INT_LAT", int_lat);
+
+        /* If int_lat shows a change or we are forcing an update, read interrupt source.
+         * Note that we mask DP_HI and DM_HI when LOC_CONN is set, we don't want them 
+         * when USB Bus is in use.
+         *
+         * Mask with bits we are currently interested in.
+         *
+         * Ensure that we have two matching reads to ensure that value is stable.
+         * Also ensure that interrupt source is non-zero.
+         */
+
+        int_src1 = i2c_readb(ISP1301_INTERRUPT_SOURCE);
+        isp1301_trace(int_src1, int_src_saved ^ int_src1, "INT_SRC_1");
+       
+        do {
+                int_src2 = i2c_readb(ISP1301_INTERRUPT_SOURCE);         // re-read interrupt source
+                BREAK_IF(int_src1 && (int_src1 & (ISP1301_ID_GND | ISP1301_ID_FLOAT)) && 
+                                (int_src1 == int_src2));           // finished if non-zero and same
+                isp1301_trace(int_src2, int_src_saved ^ int_src2, "INT_SRC_2");
+                int_src1 = int_src2;                                    // save most recent read value
+        } while (1);
+
+
+        #if 0
+        while ( (int_src1 != (int_src2 = i2c_readb(ISP1301_INTERRUPT_SOURCE) & isp1301_private.int_src))) {
+                isp1301_trace(int_src2, int_src_saved ^ int_src2, "INT_SRC_2");
+                int_src1 = int_src2;
+        } 
+        #endif
+
+        if (isp1301_bh_first || int_src_saved ^ (int_src1 & isp1301_private.int_src)) {
+                int_src_saved = int_src1;
+                inputs |= isp1301_event(tcd_instance->otg, isp1301_int_src_tests, int_src1, 
+                                isp1301_private.int_src, "ISP1301 INT SRC");
+                if (int_src1 & ISP1301_VBUS_VLD) {
+                        TRACE_MSG0(TCD, "VBUS_VLD, setting B_SESS_VLD");
+                        inputs |= B_SESS_VLD;
+                }
+                else if (!(int_src1 & ISP1301_SESS_VLD)) {
+                        TRACE_MSG0(TCD, "A_SESS_VLD/, setting B_SESS_VLD/");
+                        inputs |= B_SESS_VLD_;
+                }
+        }
+
+        /* further work if B-Device 
+         */
+        if ( !(int_src1 & ISP1301_ID_GND) || !(int_src_saved & ISP1301_ID_GND) ) {
+                /* read the otg_status register and update otg state machine
+                 *
+                 * XXX this needs to be conditional on Transceiver type. The
+                 * MAX3301E for example supports sess_end in a separate register.
+                 */
+                switch (isp1301_private.transceiver_map->transceiver_type) {
+                case isp1301:
+                        otg_status = i2c_readb(ISP1301_OTG_STATUS);
+                        isp1301_trace(otg_status, int_src_saved ^ int_src1, "OTG_STATUS");
+                        TRACE_MSG3(TCD, "otg_status_saved: %02x otg_status: %02x chng: %02x", 
+                                        otg_status_saved, otg_status, otg_status_saved ^ otg_status);
+
+                        if (isp1301_bh_first || (otg_status_saved ^ otg_status)) {
+                                inputs |= isp1301_event(tcd_instance->otg, isp1301_otg_status_tests, otg_status, 0xff,
+                                                "ISP1301 OTG STATUS");
+                                otg_status_saved = otg_status;
+                        }
+
+                        #if 0
+                        isp1301_info("OTG STATUS", otg_status);
+                        if (isp1301_bh_first) 
+                                otg_status_saved = ~otg_status;
+
+                        otg_status_saved = isp1301_update_otg_status( tcd_instance->otg, otg_status,
+                                        otg_status_saved ^ otg_status);
+                        #endif
+                        break;
+
+                        // XXX
+                case max3301e:
+                        special_function_22 = i2c_readb(MAX3301E_SPECIAL_FUNCTION_2_SET);
+                        break;
+                case unknown_transceiver:
+                        break;
+                }
+        }
+        if (inputs) otg_event(tcd_instance->otg, inputs, TCD, "ISP1301");
+        isp1301_bh_first = 0;
+}
+
+
+/*! isp1301_bh_wakeup - wakeup the isp1301 bottom half
+ */
+void isp1301_bh_wakeup(int first)
+{
+        //TRACE_MSG0(TCD, "--");
+        isp1301_bh_first |= first;
+        SCHEDULE_WORK(isp1301_private.bh);
+}
+
+
+/* ********************************************************************************************** */
+/*! isp1301_vbus - Do we have Vbus (cable attached?)
+ * Return non-zero if Vbus is detected.
+ *
+ */
+int isp1301_vbus (struct otg_instance *otg)
+{
+        return 0;
+}
+
+/*! isp1301_id - Do we have Vbus (cable attached?)
+ * Return non-zero if Vbus is detected.
+ *
+ */
+int isp1301_idvbus (struct otg_instance *otg)
+{
+        return 0;
+}
+
+
+/* ********************************************************************************************* */
+/*! isp1301_tcd_en() - used to enable 
+ *
+ */
+void isp1301_tcd_en(struct otg_instance *otg, u8 flag)
+{
+        switch (flag) {
+        case SET:
+                TRACE_MSG0(TCD, "SET");
+                break;
+        case PULSE:
+                TRACE_MSG0(TCD, "PULSE");
+                break;
+        case RESET:
+                TRACE_MSG0(TCD, "RESET");
+                break;
+        }
+        isp1301_bh_wakeup(TRUE);
+}
+
+
+/*! isp1301_chrg_vbus - used to enable or disable B-device Vbus charging
+ */
+void isp1301_chrg_vbus(struct otg_instance *otg, u8 flag)
+{
+        //TRACE_MSG0(TCD, "--");
+        switch (flag) {
+        case SET:
+                TRACE_MSG0(TCD, "CHRG_VBUS_SET");
+                i2c_writeb(ISP1301_OTG_CONTROL_SET, ISP1301_VBUS_CHRG);
+                break;
+        case RESET:
+                TRACE_MSG0(TCD, "CHRG_VBUS_RESET");
+                i2c_writeb(ISP1301_OTG_CONTROL_CLR, ISP1301_VBUS_RESET);
+                break;
+        case PULSE:
+                break;
+        }
+}
+
+/*! isp1301_drv_vbus - used to enable or disable A-device driving Vbus 
+ */
+void isp1301_drv_vbus(struct otg_instance *otg, u8 flag)
+{
+        //TRACE_MSG0(TCD, "--");
+        switch (flag) {
+        case SET:
+                TRACE_MSG0(TCD, "DRV_VBUS_SET");
+                i2c_writeb(ISP1301_OTG_CONTROL_SET, ISP1301_VBUS_DRV);
+                break;
+        case RESET:
+                TRACE_MSG0(TCD, "DRV_VBUS_RESET");
+                i2c_writeb(ISP1301_OTG_CONTROL_CLR, ISP1301_VBUS_RESET);
+                break;
+        }
+}
+
+/*! isp1301_dischrg_vbus - used to enable Vbus discharge
+ */
+void isp1301_dischrg_vbus(struct otg_instance *otg, u8 flag)
+{
+        struct tcd_instance *tcd = otg->tcd;
+        //TRACE_MSG0(TCD, "--");
+        switch (flag) {
+        case SET:
+                TRACE_MSG0(TCD, "OUTPUT: TCD_DISCHRG_VBUS_SET");
+                i2c_writeb(ISP1301_OTG_CONTROL_SET, ISP1301_VBUS_DISCHRG);
+                break;
+        case RESET:
+                TRACE_MSG0(TCD, "OUTPUT: TCD_DISCHRG_VBUS_RESET");
+                i2c_writeb(ISP1301_OTG_CONTROL_CLR, ISP1301_VBUS_RESET);
+                break;
+        }
+}
+
+/*! isp1301_mx21_vbus_drain - used to enable Vbus discharge
+ */
+void isp1301_mx21_vbus_drain_func(struct otg_instance *otg, u8 flag)
+{
+        struct tcd_instance *tcd = otg->tcd;
+        //TRACE_MSG0(TCD, "--");
+        switch (flag) {
+        case SET:
+                TRACE_MSG0(TCD, "OUTPUT: TCD_DISCHRG_VBUS_SET");
+                i2c_writeb(ISP1301_OTG_CONTROL_SET, ISP1301_VBUS_DISCHRG);
+                break;
+        case RESET:
+                TRACE_MSG0(TCD, "OUTPUT: TCD_DISCHRG_VBUS_RESET");
+                i2c_writeb(ISP1301_OTG_CONTROL_CLR, ISP1301_VBUS_RESET);
+                break;
+        }
+}
+
+/*! isp1301_dp_pullup_func - used to enable or disable peripheral connecting to bus
+ *
+ * C.f. 5.1.6, 5.1.7, 5.2.4 and 5.2.5
+ *
+ *                              host    peripheral
+ *              d+ pull-up      clr     set
+ *              d+ pull-down    set     clr
+ *
+ *              d- pull-up      clr     clr
+ *              d- pull-down    set     set
+ *
+ */
+void isp1301_dp_pullup_func(struct otg_instance *otg, u8 flag)
+{
+        struct tcd_instance *tcd = (struct tcd_instance *)otg->tcd;
+        //TRACE_MSG0(TCD, "--");
+        switch (flag) {
+        case SET:
+                isp1301_private.flags |= ISP1301_LOC_CONN;
+                TRACE_MSG0(TCD, "ISP1301_LOC_CONN SET - Set DP PULLUP");
+                i2c_writeb(ISP1301_OTG_CONTROL_SET, ISP1301_DP_PULLUP);
+                break;
+
+        case RESET:
+                isp1301_private.flags &= ~ISP1301_LOC_CONN;
+                TRACE_MSG0(TCD, "ISP1301_LOC_CONN RESET - Clr DP PULLUP");
+                i2c_writeb(ISP1301_OTG_CONTROL_CLR, ISP1301_DP_PULLUP);
+                break;
+        }
+}
+
+/*! isp1301_dm_pullup_func - used to enable or disable peripheral connecting to bus
+ *
+ */
+void isp1301_dm_pullup_func(struct otg_instance *otg, u8 flag)
+{
+        struct tcd_instance *tcd = (struct tcd_instance *)otg->tcd;
+        //TRACE_MSG0(TCD, "--");
+        switch (flag) {
+        case SET:
+                isp1301_private.flags |= ISP1301_LOC_CONN;
+                TRACE_MSG0(TCD, "ISP1301_LOC_CONN SET - Set DM PULLUP");
+                i2c_writeb(ISP1301_OTG_CONTROL_SET, ISP1301_DM_PULLUP);
+                break;
+
+        case RESET:
+                isp1301_private.flags &= ~ISP1301_LOC_CONN;
+                TRACE_MSG0(TCD, "ISP1301_LOC_CONN RESET - Clr DM PULLUP");
+                i2c_writeb(ISP1301_OTG_CONTROL_CLR, ISP1301_DM_PULLUP);
+                break;
+        }
+}
+
+/*! isp1301_dp_pulldown_func - used to enable or disable peripheral connecting to bus
+ *
+ * C.f. 5.1.6, 5.1.7, 5.2.4 and 5.2.5
+ *
+ *                              host    peripheral
+ *              d+ pull-up      clr     set
+ *              d+ pull-down    set     clr
+ *
+ *              d- pull-up      clr     clr
+ *              d- pull-down    set     set
+ *
+ */
+void isp1301_dp_pulldown_func(struct otg_instance *otg, u8 flag)
+{
+        struct tcd_instance *tcd = (struct tcd_instance *)otg->tcd;
+        //TRACE_MSG0(TCD, "--");
+        switch (flag) {
+        case SET:
+                isp1301_private.flags |= ISP1301_LOC_CONN;
+                TRACE_MSG0(TCD, "ISP1301_LOC_CONN SET - Set DP PULLUP");
+                i2c_writeb(ISP1301_OTG_CONTROL_SET, ISP1301_DP_PULLDOWN);
+                break;
+
+        case RESET:
+                isp1301_private.flags &= ~ISP1301_LOC_CONN;
+                TRACE_MSG0(TCD, "ISP1301_LOC_CONN RESET - Clr DP PULLUP");
+                i2c_writeb(ISP1301_OTG_CONTROL_CLR, ISP1301_DP_PULLDOWN);
+                break;
+        }
+}
+
+/*! isp1301_dm_pulldown_func - used to enable or disable peripheral connecting to bus
+ *
+ */
+void isp1301_dm_pulldown_func(struct otg_instance *otg, u8 flag)
+{
+        struct tcd_instance *tcd = (struct tcd_instance *)otg->tcd;
+        //TRACE_MSG0(TCD, "--");
+        switch (flag) {
+        case SET:
+                isp1301_private.flags |= ISP1301_LOC_CONN;
+                TRACE_MSG0(TCD, "ISP1301_LOC_CONN SET - Set DM PULLUP");
+                i2c_writeb(ISP1301_OTG_CONTROL_SET, ISP1301_DM_PULLDOWN);
+                break;
+
+        case RESET:
+                isp1301_private.flags &= ~ISP1301_LOC_CONN;
+                TRACE_MSG0(TCD, "ISP1301_LOC_CONN RESET - Clr DM PULLUP");
+                i2c_writeb(ISP1301_OTG_CONTROL_CLR, ISP1301_DM_PULLDOWN);
+                break;
+        }
+}
+
+/*! isp1301_peripheral_host_func - used to enable or disable peripheral connecting to bus
+ *
+ * A-Device             D+ pulldown     D- pulldown
+ *      idle            set             set
+ *      host            set             set
+ *      peripheral      reset           set
+ *
+ * B-Device
+ *      idle            set             set
+ *      host            set             set
+ *      peripheral      reset           set
+ */
+void isp1301_peripheral_host_func(struct otg_instance *otg, u8 flag)
+{
+        struct tcd_instance *tcd = (struct tcd_instance *)otg->tcd;
+        //TRACE_MSG0(TCD, "--");
+        switch (flag) {
+        case SET:       // peripheral
+                TRACE_MSG0(TCD, "SET - CLR DP PULLDOWN");
+                i2c_writeb(ISP1301_OTG_CONTROL_CLR, ISP1301_DP_PULLDOWN);
+                break;
+
+        case RESET:     // host
+                TRACE_MSG0(TCD, "RESET - SET DM PULLDOWN");
+                i2c_writeb(ISP1301_OTG_CONTROL_SET, ISP1301_DP_PULLDOWN);
+                break;
+        }
+}
+
+/*! isp1301_dm_det_func - used to enable or disable D- detect
+ *
+ */
+void isp1301_dm_det_func(struct otg_instance *otg, u8 flag)
+{
+        struct tcd_instance *tcd = (struct tcd_instance *)otg->tcd;
+        //TRACE_MSG0(TCD, "--");
+        switch (flag) {
+        case SET:
+                TRACE_MSG0(TCD, "setting DM_HI detect");
+                isp1301_int_src_set(ISP1301_DM_HI);
+                isp1301_bh_wakeup(TRUE);
+                isp1301_debounce = 0;
+                break;
+
+        case RESET:
+                TRACE_MSG0(TCD, "reseting DM_HI detect");
+                isp1301_int_src_clr(ISP1301_DM_HI);
+                isp1301_bh_wakeup(TRUE);
+                isp1301_debounce = 1;
+                break;
+        }
+}
+
+/*! isp1301_dp_det_func - used to enable or disable D+ detect
+ *
+ */
+void isp1301_dp_det_func(struct otg_instance *otg, u8 flag)
+{
+        struct tcd_instance *tcd = (struct tcd_instance *)otg->tcd;
+        //TRACE_MSG0(TCD, "--");
+        switch (flag) {
+        case SET:
+                TRACE_MSG0(TCD, "setting DP_HI detect");
+                isp1301_int_src_set(ISP1301_DP_HI);
+                isp1301_bh_wakeup(TRUE);
+                break;
+
+        case RESET:
+                TRACE_MSG0(TCD, "reseting DP_HI detect");
+                isp1301_int_src_clr(ISP1301_DP_HI);
+                isp1301_bh_wakeup(TRUE);
+                break;
+        }
+}
+
+/*! isp1301_cr_det_func - used to enable or disable D+ detect
+ *
+ */
+void isp1301_cr_det_func(struct otg_instance *otg, u8 flag)
+{
+        struct tcd_instance *tcd = (struct tcd_instance *)otg->tcd;
+        //TRACE_MSG0(TCD, "--");
+        switch (flag) {
+        case SET:
+                TRACE_MSG0(TCD, "setting CR_INT detect");
+                isp1301_int_src_set(ISP1301_CR_INT);
+                isp1301_bh_wakeup(TRUE);
+                break;
+
+        case RESET:
+                TRACE_MSG0(TCD, "reseting CR_INT detect");
+                isp1301_int_src_clr(ISP1301_CR_INT);
+                isp1301_bh_wakeup(TRUE);
+                break;
+        }
+}
+
+/*! isp1301_bdis_acon_func - used to enable or disable auto a-connect
+ *
+ */
+void isp1301_bdis_acon_func(struct otg_instance *otg, u8 flag)
+{
+        struct tcd_instance *tcd = (struct tcd_instance *)otg->tcd;
+        //TRACE_MSG0(TCD, "--");
+        switch (flag) {
+        case SET:
+                TRACE_MSG0(TCD, "setting BDIS ACON");
+                isp1301_int_src_set(ISP1301_BDIS_ACON);
+                i2c_writeb(ISP1301_MODE_CONTROL_1_SET, ISP1301_BDIS_ACON_EN);
+                break;
+
+        case RESET:
+                TRACE_MSG0(TCD, "reseting BDIS ACON");
+                i2c_writeb(ISP1301_MODE_CONTROL_1_CLR, ISP1301_BDIS_ACON_EN);
+                isp1301_int_src_clr(ISP1301_BDIS_ACON);
+                break;
+        }
+}
+
+/*! isp1301_id_pulldown_func - used to enable or disable ID pulldown
+ *
+ */
+void isp1301_id_pulldown_func(struct otg_instance *otg, u8 flag)
+{
+        struct tcd_instance *tcd = (struct tcd_instance *)otg->tcd;
+        //TRACE_MSG0(TCD, "--");
+        switch (flag) {
+        case SET:
+                TRACE_MSG0(TCD, "setting ID PULLDOWN");
+                i2c_writeb(ISP1301_OTG_CONTROL_SET, ISP1301_ID_PULLDOWN);
+                break;
+
+        case RESET:
+                TRACE_MSG0(TCD, "reseting ID PULLDOWN");
+                i2c_writeb(ISP1301_OTG_CONTROL_CLR, ISP1301_ID_PULLDOWN);
+                break;
+        }
+}
+
+/*! isp1301_audio_func - used to enable or disable Carkit Interrupt
+ *
+ */
+void isp1301_audio_func(struct otg_instance *otg, u8 flag)
+{
+        struct tcd_instance *tcd = (struct tcd_instance *)otg->tcd;
+        //TRACE_MSG0(TCD, "--");
+        switch (flag) {
+        case SET:
+                TRACE_MSG0(TCD, "SET AUDIO_EN");
+                //isp1301_int_src_set(ISP1301_CR_INT);
+                i2c_writeb(ISP1301_MODE_CONTROL_2_SET, ISP1301_AUDIO_EN);
+                break;
+
+        case RESET:
+                TRACE_MSG0(TCD, "RESET AUDIO_EN");
+                i2c_writeb(ISP1301_MODE_CONTROL_2_CLR, ISP1301_AUDIO_EN);
+                //isp1301_int_src_clr(ISP1301_CR_INT);
+                break;
+        }
+}
+
+/*! isp1301_uart_func - used to enable or disable transparent uart mode
+ *
+ */
+void isp1301_uart_func(struct otg_instance *otg, u8 flag)
+{
+        struct tcd_instance *tcd = (struct tcd_instance *)otg->tcd;
+        //TRACE_MSG0(TCD, "--");
+        switch (flag) {
+        case SET:
+                TRACE_MSG0(TCD, "setting UART_EN");
+                i2c_writeb(ISP1301_MODE_CONTROL_1_SET, ISP1301_UART_EN);
+                /* XXX enable uart */
+                break;
+
+        case RESET:
+                TRACE_MSG0(TCD, "reseting UART_EN");
+                i2c_writeb(ISP1301_MODE_CONTROL_1_CLR, ISP1301_UART_EN);
+                /* XXX disable uart */
+                break;
+        }
+}
+
+/*! isp1301_mono_func - used to enable or disable mono audio connection
+ *
+ */
+void isp1301_mono_func(struct otg_instance *otg, u8 flag)
+{
+        struct tcd_instance *tcd = (struct tcd_instance *)otg->tcd;
+        //TRACE_MSG0(TCD, "--");
+        switch (flag) {
+        case SET:
+                TRACE_MSG0(TCD, "setting MONO");
+                /* XXX enable mono output */
+                break;
+
+        case RESET:
+                TRACE_MSG0(TCD, "reseting MONO");
+                /* XXX disable mono output */
+                break;
+        }
+}
+
+
+/* ********************************************************************************************* */
+extern int isp1301_procfs_init (void);
+extern void isp1301_procfs_exit (void);
+
+extern void mx2_isp1301_bh(void *arg);
+
+/*! isp1301_mod_init
+ */
+int isp1301_mod_init(WORK_PROC bh_proc)
+//int isp1301_mod_init(void)
+{
+        /* for interrupt bottom half handler
+         */
+        //PREPARE_WORK_ITEM(isp1301_private.bh, &isp1301_bh, NULL);
+
+        UNLESS(bh_proc)
+                bh_proc = &isp1301_bh;
+
+        PREPARE_WORK_ITEM(isp1301_private.bh, bh_proc, NULL);
+
+        //else
+        //        PREPARE_WORK_ITEM(isp1301_private.bh, &isp1301_bh, NULL);
+
+        /* setup isp1301, enable interrupts, clear latch
+         * XXX when and how do we do DM_HI and DP_HI, when ID is present?
+         */
+
+        isp1301_int_src(ISP1301_ID_FLOAT | ISP1301_DM_HI | ISP1301_ID_GND | ISP1301_DP_HI | ISP1301_SESS_VLD | ISP1301_VBUS_VLD);
+        
+        //isp1301_int_src(ISP1301_INT_SRC);
+        //isp1301_int_src(ISP1301_NO_SE1_INT);
+        //isp1301_int_src_clr(ISP1301_CR_INT);
+
+        //i2c_writeb(ISP1301_OTG_CONTROL_SET, ISP1301_DM_PULLDOWN | ISP1301_DP_PULLDOWN);
+
+#ifdef CONFIG_OTG_ISP1301_PROCFS
+        isp1301_procfs_init ();
+#endif /* CONFIG_OTG_ISP1301_PROCFS */
+
+        return 0;
+}
+
+/*! isp1301_mod_exit
+ */
+void isp1301_mod_exit(void)
+{
+        i2c_writeb(ISP1301_INTERRUPT_ENABLE_LOW_CLR, 0xff);
+        i2c_writeb(ISP1301_INTERRUPT_ENABLE_HIGH_CLR, 0xff);
+        //i2c_close();
+#ifdef CONFIG_OTG_ISP1301_PROCFS
+        isp1301_procfs_exit ();
+#endif /* CONFIG_OTG_ISP1301_PROCFS */
+
+        while (PENDING_WORK_ITEM(isp1301_private.bh)) {
+                printk(KERN_ERR"%s: waiting for bh\n", __FUNCTION__);
+                schedule_timeout(10 * HZ);
+        }
+
+}
+
+/* ********************************************************************************************* */
+/*! isp1301_configure - configure the ISP1301 for this host
+ * @param tx_mode - the type of connection between the host USB and the ISP1301
+ * @param spd_ctrl - suspend control method
+ */
+void  isp1301_configure(tx_mode_t tx_mode, spd_ctrl_t spd_ctrl)
+{
+        u16 vendor = 0, product = 0, revision = 0;
+        u8 mode1, mode2, control;
+
+        struct otg_transceiver_map *map = isp1301_transceiver_map;
+
+
+#if defined(CONFIG_OTG_ISP1301_MX2ADS) || defined(CONFIG_OTG_ISP1301_MX2ADS_MODULE)
+        u32 vp;
+        TRACE_MSG0(TCD, "1. Read Transceiver ID's with long read");
+        vp = i2c_readl(ISP1301_VENDOR_ID);
+        isp1301_private.vendor = vp & 0xffff;
+        isp1301_private.product = vp >> 16;
+        isp1301_private.revision = i2c_readw(ISP1301_VERSION_ID);
+        mode1 = i2c_readb(ISP1301_MODE_CONTROL_1_SET);
+        mode2 = i2c_readb(ISP1301_MODE_CONTROL_2_SET);
+         
+        TRACE_MSG5(TCD, "2. OTG Transceiver: vendor: %04x product: %04x revision: %04x mode1: %02x mode2: %02x",
+                        vendor, product, revision, mode1, mode2); 
+
+#else /* defined(CONFIG_OTG_ISP1301_MX2ADS) */
+
+        isp1301_private.vendor = i2c_readw(ISP1301_VENDOR_ID);
+        isp1301_private.product = i2c_readw(ISP1301_PRODUCT_ID);
+        isp1301_private.revision = i2c_readw(ISP1301_VERSION_ID);
+        mode1 = i2c_readb(ISP1301_MODE_CONTROL_1_SET);
+        mode2 = i2c_readb(ISP1301_MODE_CONTROL_2_SET);
+
+        TRACE_MSG5(TCD, "2. OTG Transceiver: vendor: %04x product: %04x revision: %04x mode1: %02x mode2: %02x",
+                        isp1301_private.vendor, isp1301_private.product, isp1301_private.revision, mode1, mode2); 
+
+#endif /* defined(CONFIG_OTG_ISP1301_MX2ADS) */
+
+        for ( ; map && map->transceiver_type != unknown_transceiver; map++) {
+                CONTINUE_UNLESS ((isp1301_private.vendor == map->vendor) && (isp1301_private.product == map->product));
+                TRACE_MSG1(TCD, "Found Transceiver: %s", map->name);
+                isp1301_private.transceiver_map = map;
+                break;
+        }
+        UNLESS (isp1301_private.transceiver_map) 
+                isp1301_private.transceiver_map = isp1301_transceiver_map;
+
+        TRACE_MSG0(TCD, "3. Disable All Transceiver Control Register 1 ");
+        i2c_writeb(ISP1301_OTG_CONTROL_CLR, 0xff);                                    // clear
+         
+        TRACE_MSG0(TCD, "4. Enable D-/D+ Pulldowns in Transceiver Control Register 1 ");
+
+        i2c_writeb(ISP1301_OTG_CONTROL_SET, ISP1301_DM_PULLDOWN | ISP1301_DP_PULLDOWN);
+        i2c_writeb(ISP1301_OTG_CONTROL_CLR, (u8) ISP1301_DM_PULLUP | ISP1301_DP_PULLUP);
+
+
+        TRACE_MSG0(TCD, "5. Clear latch and enable interrupts");
+        i2c_writeb(ISP1301_INTERRUPT_LATCH_CLR, 0xff);
+        //i2c_writeb(ISP1301_INTERRUPT_ENABLE_LOW_CLR, 0xeb);
+        //i2c_writeb(ISP1301_INTERRUPT_ENABLE_HIGH_CLR, 0xeb);
+        isp1301_int_src_set(0xeb);
+        
+        /* The PSW_OE enables the ADR/PSW pin for output driving either high
+         * or low depending on the address.
+         *
+         * ADR          ADR_REG Address         PSW_OE=0        PSW_OE=1
+         *
+         *      low     0       0x2c            LOW             HIGH
+         *
+         *      high    1       0x2d            HIGH            LOW
+         *
+         *
+         * The i2c address by tying the ADR/PSW pin either high or low.
+         * Setting the PSW_OE drives the ADR/PSW into the opposite of the
+         * default wiring.
+         *
+         * On the MX21ADS the ADR/PSW pin is wired high which enables the
+         * charge pump. So enabling the PSW_OE is required to disable Vbus
+         * generation on the Charge Pump.
+         *
+         * The MAX3355E will only enable Vbus when this signal is high AND
+         * the ID signal is low. So it may be safe to leave enabled when
+         * operating as a B-device (ID floating.)
+         */
+        
+        //i2c_writeb(ISP1301_MODE_CONTROL_2_SET, ISP1301_PSW_OE);              // PSW_OE - OFFVBUS low
+
+
+        TRACE_MSG1(TCD, "6. tx_mode: %02x", tx_mode);
+        switch (tx_mode) {
+        case vp_vm_bidirectional:
+                break;
+        case dat_se0_unidirectional:
+                i2c_writeb(ISP1301_MODE_CONTROL_2_CLR, ISP1301_BI_DI);
+        case dat_se0_bidirectional:
+                i2c_writeb(ISP1301_MODE_CONTROL_1_SET, ISP1301_DAT_SE0);
+                break;
+        }
+        TRACE_MSG0(TCD, "6. tx_mode done");
+
+        TRACE_MSG1(TCD, "7. spd_ctrl: %02x", spd_ctrl);
+        switch (spd_ctrl) {
+        case spd_susp_pins:
+                i2c_writeb(ISP1301_MODE_CONTROL_2_CLR, ISP1301_SPD_SUSP_CTRL);
+                break;
+        case spd_susp_reg:
+                i2c_writeb(ISP1301_MODE_CONTROL_2_SET, ISP1301_SPD_SUSP_CTRL);
+                break;
+        }
+
+        //i2c_writeb(ISP1301_MODE_CONTROL_2_SET, ISP1301_PSW_OE);              // PSW_OE - OFFVBUS low
+
+        TRACE_MSG0(TCD, "7. spd_ctrl done");
+
+        TRACE_MSG1(TCD, "8. transceiver_type: %02x", isp1301_private.transceiver_map);
+
+        TRACE_MSG1(TCD, "8. transceiver_type: %02x", isp1301_private.transceiver_map->transceiver_type);
+        switch (isp1301_private.transceiver_map->transceiver_type) {
+        case isp1301:
+                break;
+        case max3301e:
+                i2c_writeb(MAX3301E_SPECIAL_FUNCTION_1_SET, MAX3301E_SESS_END);
+                break;
+        case unknown_transceiver:
+                break;
+        }
+        TRACE_MSG0(TCD, "8. transceiver_type done");
+}
+
diff -uNr linux/drivers/no-otg/ocd/isp1301/tcd-mainstone.c linux/drivers/otg/ocd/isp1301/tcd-mainstone.c
--- linux/drivers/no-otg/ocd/isp1301/tcd-mainstone.c	1970-01-01 01:00:00.000000000 +0100
+++ linux/drivers/otg/ocd/isp1301/tcd-mainstone.c	2006-09-01 21:41:31.000000000 +0200
@@ -0,0 +1,177 @@
+/*
+ * otg/ocd/mx2/tcd-mainstone.c -- Mainstone ISP1301 Transceiver Controller driver 
+ *
+ *      Copyright (c) 2004 Belcarra
+ *
+ * By: 
+ *      Stuart Lynne <sl@lbelcarra.com>, 
+ *      Tom Rushworth <tbr@belcarra.com>, 
+ *      Bruce Balden <balden@belcarra.com>
+ *
+ */
+/*!
+ * @file otg/ocd/isp1301/tcd-mainstone.c
+ * @brief Mainstone ISP1301 driver.
+ *
+ * This implements the ISP1301 Transceiver Controller Driver for the Mainstone
+ * using the USBOTG I2C controller for access.
+ *
+ * Notes
+ *
+ * 1. The interrupt source is handled by the OCD driver so we just publish
+ * an interrupt handler for it to call.
+ *
+ *
+ * @ingroup ISP1301TCD
+ */
+
+#include <otg/pcd-include.h>
+#include <linux/pci.h>
+#include <asm/arch/irq.h>
+#include <asm/arch/irqs.h>
+#include <otghw/isp1301-hardware.h>
+#include "mx2.h"
+#include "otghw/bvd-hardware.h"
+#include <otghw/isp1301.h>
+
+/* ********************************************************************************************* */
+/* ********************************************************************************************* */
+
+
+/* ********************************************************************************************* */
+
+
+/*! isp1301_int_hndlr 
+ */
+static irqreturn_t isp1301_int_hndlr (int irq, void *dev_id, struct pt_regs *regs)
+{
+        TRACE_MSG0(TCD, "--");
+
+        SCHEDULE_WORK(isp1301_private.bh);
+
+        return IRQ_HANDLED;
+}
+
+
+/* ********************************************************************************************* */
+
+/*! mainstone_tcd_init - used to initialize/enable or disable the tcd driver
+ */
+void mainstone_tcd_init(struct otg_instance *otg, u8 flag)
+{
+        TRACE_MSG0(TCD, "--");
+        switch (flag) {
+        case SET:
+                TRACE_MSG0(TCD, "MAINSTONE_TCD_EN SET");
+                otg_event(otg, TCD_OK, TCD, "TCD_OK");
+                isp1301_thread_wakeup(1, 1);
+                break;
+        case RESET:
+                TRACE_MSG0(TCD, "MAINSTONE_TCD_EN RESET");
+                isp1301_thread_wakeup(0, 0);
+                otg_event(otg, TCD_OK, TCD, "TCD_OK");
+                break;
+        }
+}
+
+
+struct tcd_ops tcd_ops;
+struct tcd_instance *mainstone_tcd_instance;
+//wait_queue_head_t mx2_i2c_wait;                                         // wait structure for i2c i/o interrupt
+
+#define ADAPTOR_NAME "PXA-I2C-Adapter"
+
+/*! mainstone_mod_init
+ */
+int mainstone_mod_init(void)
+{
+        struct otg_instance *otg;
+        int i2c = i2c_configure(ADAPTOR_NAME, ISP1301_I2C_ADDR_HIGH);
+        int irq = request_irq (GPIOX_TO_IRQ(35), isp1301_int_hndlr, SA_INTERRUPT, "ISP1301", NULL);
+
+        TRACE_MSG0(TCD, "--");
+
+        THROW_IF(i2c, error);
+
+        TRACE_MSG0(TCD, "1. Enable Mainstone external OTG Transceiver!");
+
+        TRACE_MSG1(TCD, "MST_MSCWR2: %08x", MST_MSCWR2);
+        MST_MSCWR2 |= MSCWR2_USB_OTG_SEL | MSCWR2_USB_OTG_RST;
+        TRACE_MSG1(TCD, "MST_MSCWR2: %08x", MST_MSCWR2);
+
+
+        TRACE_MSG0(TCD, "2. configure i2c driver!");
+        isp1301_configure(dat_se0_bidirectional, spd_susp_reg);
+
+        TRACE_MSG0(TCD, "3. configure isp1301!");
+        isp1301_mod_init();
+
+        TRACE_MSG0(TCD, "4. set ops");
+        THROW_UNLESS(ocd_instance && (otg = ocd_instance->otg), error);
+        THROW_UNLESS(mainstone_tcd_instance = otg_set_tcd_ops(&tcd_ops), error);
+
+        /* Success!
+         */
+        TRACE_MSG0(TCD, "5. Success!");
+
+        otg_init(otg);
+        //otg_start(otg, ocd_ops.capabilities & OCD_CAPABILITIES_AUTO);
+
+        // XXX TESTING
+        //isp1301_dp_pullup_func(otg, 1);
+
+        /* XXX start thread to monitor for changes - not needed once gpio interrupt implmented
+         */
+        isp1301_thread_init(isp1301_bh);
+
+        /* error handling
+         */
+        CATCH(error) {
+                //if (irq) free_irq(GPIO_2_80_TO_IRQ(35), NULL);
+                if (irq) free_irq(GPIOX_TO_IRQ(35), NULL);
+                if (i2c) i2c_close();
+        }
+        return 0;
+}
+
+/*! mainstone_mod_exit
+ */
+void mainstone_mod_exit(void)
+{
+        //free_irq(GPIO_2_80_TO_IRQ(35), NULL);
+        free_irq(GPIOX_TO_IRQ(35), NULL);
+
+        /* XXX stop thread - not needed once gpio interrupt implmented
+         */
+        isp1301_thread_exit(NULL);
+
+        isp1301_mod_exit();
+        TRACE_MSG1(TCD, "MST_MSCWR2: %08x", MST_MSCWR2);
+        MST_MSCWR2 &= ~(MSCWR2_USB_OTG_SEL | MSCWR2_USB_OTG_RST);
+        TRACE_MSG1(TCD, "MST_MSCWR2: %08x", MST_MSCWR2);
+        //isp1301_thread_exit();
+        i2c_close();
+
+}
+
+/* ********************************************************************************************* */
+
+struct tcd_ops tcd_ops = {
+
+        vbus: isp1301_vbus,
+
+        mod_init: mainstone_mod_init,
+        mod_exit: mainstone_mod_exit,
+
+        tcd_init_func: mainstone_tcd_init,
+
+        dischrg_vbus_func: isp1301_dischrg_vbus,
+        chrg_vbus_func: isp1301_chrg_vbus,
+        drv_vbus_func: isp1301_drv_vbus,
+        dp_pullup_func: isp1301_dp_pullup_func,
+        dm_pullup_func: isp1301_dm_pullup_func,
+        dm_det_func: isp1301_dm_det_func,
+        dp_det_func: isp1301_dp_det_func,
+
+};
+
diff -uNr linux/drivers/no-otg/ocd/isp1301/tcd-omap-h2.c linux/drivers/otg/ocd/isp1301/tcd-omap-h2.c
--- linux/drivers/no-otg/ocd/isp1301/tcd-omap-h2.c	1970-01-01 01:00:00.000000000 +0100
+++ linux/drivers/otg/ocd/isp1301/tcd-omap-h2.c	2006-09-01 21:41:31.000000000 +0200
@@ -0,0 +1,180 @@
+/*
+ * otg/ocd/isp1301/tcd-omap-h2.c -- USB Transceiver Controller driver 
+ *
+ *      Copyright (c) 2004 Belcarra
+ *
+ * By: 
+ *      Stuart Lynne <sl@lbelcarra.com>, 
+ *      Tom Rushworth <tbr@belcarra.com>, 
+ *      Bruce Balden <balden@belcarra.com>
+ *
+ */
+/*!
+ * @file otg/ocd/isp1301/tcd-omap-h2.c
+ * @brief OMAP H2 ISP1301 driver.
+ *
+ *
+ * @ingroup ISP1301TCD
+ */
+
+#include <otg/pcd-include.h>
+#include <linux/pci.h>
+#include <asm/arch/irq.h>
+#include <asm/arch/irqs.h>
+#include <otghw/isp1301-hardware.h>
+#include "otghw/bvd-hardware.h"
+#include <otghw/isp1301.h>
+
+#include <asm/irq.h>
+#include <asm/system.h>
+#include <asm/io.h>
+#include <linux/i2c.h>
+
+#include <asm/arch/gpio.h>
+#include <asm/arch/mux.h>
+//struct isp1301_private isp1301_private;
+
+/* ********************************************************************************************* */
+
+
+static irqreturn_t isp1301_int_hndlr (int irq, void *dev_id, struct pt_regs *regs)
+{
+        TRACE_MSG0(TCD, "--");
+
+        SCHEDULE_WORK(isp1301_private.bh);
+
+        return IRQ_HANDLED;
+}
+
+
+/* ********************************************************************************************* */
+
+struct tcd_ops tcd_ops;
+
+#define ADAPTOR_NAME "OMAP I2C"
+
+/*! omap_mod_init
+ */
+int omap_mod_init(void)
+{
+        int i2c = i2c_configure(ADAPTOR_NAME, ISP1301_I2C_ADDR_HIGH);
+
+        int gpio = omap_request_gpio (ISP1301_GPIO);
+        int irq = 0;
+
+        TRACE_MSG0(TCD, "--");
+
+        /* did we get gpio and i2c?
+         */
+        THROW_IF(gpio || i2c, error);
+
+        /* for interrupt bottom half handler
+         */
+        // moved isp1301.c - PREPARE_WORK_ITEM(isp1301_private.bh, &isp1301_bh, NULL);
+
+        /* 
+         * See H2 C.f. Figure 17 USBOTG Interface Connection
+         * GPIO2 M14
+         * setup gpio 2 and request interrupt for it
+         */
+        omap_set_gpio_direction(ISP1301_GPIO, 1);
+        //omap_cfg_reg(M14_1510_GPIO2);
+        omap_set_gpio_edge_ctrl(ISP1301_GPIO, OMAP_GPIO_FALLING_EDGE);
+        irq = request_irq (OMAP_GPIO_IRQ(ISP1301_GPIO), isp1301_int_hndlr, SA_INTERRUPT, "ISP1301", NULL);
+        THROW_IF(irq, error);
+
+        #ifdef CONFIG_OTG_OMAP_H2_3_WIRE
+
+        /* OTG on Pin Group 1 Using 3-Wire OTG Transceiver C.f. Figure 15-59
+         * See also H2 C.f. Figure 17 USBOTG Interface Connection
+         * DAT_SE0 mode
+         * Set DAT_SE0, SUSPEND_REG and BI_DI
+         */
+        //i2c_writeb(ISP1301_MODE_CONTROL_1_SET, ISP1301_DAT_SE0 | ISP1301_SUSPEND_REG);
+        //i2c_writeb(ISP1301_MODE_CONTROL_1_CLR, ~(ISP1301_DAT_SE0 | ISP1301_SUSPEND_REG));
+        //i2c_writeb(ISP1301_MODE_CONTROL_2_SET, ISP1301_BI_DI);
+        //i2c_writeb(ISP1301_MODE_CONTROL_2_CLR, ~ISP1301_BI_DI);
+
+        isp1301_configure(dat_se0_bidirectional, spd_susp_pins);
+
+        #elif CONFIG_OTG_OMAP_H2_4_WIRE
+
+        /* OTG on Pin Group 1 Using 4-Wire OTG Transceiver C.f. Figure 15-60
+         * See also H2 C.f. Figure 17 USBOTG Interface Connection
+         * VP_VM mode
+         * Reset DAT_SE0 
+         * Set SUSPEND_REG and BI_DI
+         */
+        //i2c_writeb(ISP1301_MODE_CONTROL_1_SET, ISP1301_SUSPEND_REG);
+        //i2c_writeb(ISP1301_MODE_CONTROL_1_CLR, ~ISP1301_SUSPEND_REG);
+        //i2c_writeb(ISP1301_MODE_CONTROL_2_SET, ISP1301_BI_DI);
+        //i2c_writeb(ISP1301_MODE_CONTROL_2_CLR, ~ISP1301_BI_DI);
+
+        isp1301_configure(vp_vm_bidirectional);
+
+        #else
+        #error "ISP1301 Connection style not selected!"
+        #endif /* CONFIG_OTG_OMAP_H2_4_WIRE */
+
+        isp1301_mod_init();
+
+        i2c_writeb(ISP1301_OTG_CONTROL_SET, ISP1301_DM_PULLDOWN | ISP1301_DP_PULLDOWN);
+
+        /* Success!
+         */
+
+        TRACE_MSG0(TCD, "5. Success!");
+
+        otg_init(otg);
+
+        /* error handling
+         */
+        CATCH(error) {
+                if (irq) free_irq(OMAP_GPIO_IRQ(ISP1301_GPIO), NULL);
+                if (gpio) omap_free_gpio(ISP1301_GPIO);
+                if (i2c) i2c_close();
+        }
+        return 0;
+}
+
+/*! omap_mod_exit
+ */
+void omap_mod_exit(void)
+{
+	#if 0
+	// moved to isp1301.c
+        while (PENDING_WORK_ITEM(isp1301_private.bh)) {
+                printk(KERN_ERR"%s: waiting for bh\n", __FUNCTION__);
+                schedule_timeout(10 * HZ);
+        }
+	#endif
+
+        isp1301_mod_exit();
+
+        i2c_writeb(ISP1301_INTERRUPT_ENABLE_LOW_CLR, 0xff);
+        i2c_writeb(ISP1301_INTERRUPT_ENABLE_HIGH_CLR, 0xff);
+        i2c_close();
+        free_irq(OMAP_GPIO_IRQ(ISP1301_GPIO), NULL);
+        omap_free_gpio(ISP1301_GPIO);
+}
+
+/* ********************************************************************************************* */
+
+struct tcd_ops tcd_ops = {
+
+        //initial_state: tr_init,
+        vbus: isp1301_vbus,
+
+        mod_init: omap_mod_init,
+        mod_exit: omap_mod_exit,
+
+        dischrg_vbus_func: isp1301_dischrg_vbus,
+        chrg_vbus_func: isp1301_chrg_vbus,
+        drv_vbus_func: isp1301_drv_vbus,
+        dp_pullup_func: isp1301_dp_pullup_func,
+        dm_pullup_func: isp1301_dm_pullup_func,
+        dm_det_func: isp1301_dm_det_func,
+        dp_det_func: isp1301_dp_det_func,
+
+};
+
diff -uNr linux/drivers/no-otg/ocd/isp1301/thread-l24.c linux/drivers/otg/ocd/isp1301/thread-l24.c
--- linux/drivers/no-otg/ocd/isp1301/thread-l24.c	1970-01-01 01:00:00.000000000 +0100
+++ linux/drivers/otg/ocd/isp1301/thread-l24.c	2006-09-01 21:41:31.000000000 +0200
@@ -0,0 +1,144 @@
+/*
+ * otg/ocd/isp1301/thread-l24.c -- MX21ADS ISP1301 Transceiver Controller driver 
+ *
+ *      Copyright (c) 2004 Belcarra
+ *
+ * By: 
+ *      Stuart Lynne <sl@lbelcarra.com>, 
+ *      Tom Rushworth <tbr@belcarra.com>, 
+ *      Bruce Balden <balden@belcarra.com>
+ *
+ */
+/*!
+ * @file otg/ocd/isp1301/thread-l24.c
+ * @brief Implement a kernel thread for checking ISP1301.
+ *
+ *
+ * @ingroup ISP1301TCD
+ */
+
+#include <otg/pcd-include.h>
+#include <otg/otg-module.h>
+#include <linux/pci.h>
+//#include <asm/arch/mx2.h>
+#include <otghw/isp1301-hardware.h>
+//#include "mx2.h"
+//#include "otghw/mx2-hardware.h"
+
+
+/* isp1301_bh
+ */
+extern void isp1301_bh(void *arg);
+
+/* ********************************************************************************************* */
+/* ********************************************************************************************* */
+
+/* I2C Thread
+ * The mx21 USBOTG module does not seem to be able to keep uptodate with the OTG Transceiver
+ * registers. This kernel thread will iterated endlessly reading the appropriate registers.
+ *
+ * If this is successful we can assume that the USBOTG module will correctly monitor the appropriate
+ * registers and allow us to follow them via the normal USBOTG interrupts and registers.
+ *
+ * Otherwise we can monitor here and issue the otg events directly.
+ */
+//#define ISP1301_THREAD_TIMEOUT_EN (10)
+#define ISP1301_THREAD_TIMEOUT_EN (1*HZ)
+#define ISP1301_THREAD_TIMEOUT_DIS (1*HZ)
+
+int isp1301_terminate;
+int mx2_pid;
+wait_queue_head_t isp1301_wait;
+static DECLARE_COMPLETION(mx2_exited);
+int isp1301_en;
+
+void (*isp1301_bh_proc)(void *);
+
+/*! isp1301_thread - implement a kernel task to monitor isp1301 for changes
+ */
+int isp1301_thread(void *data)
+{
+        u8 int_src_saved = 0, otg_status_saved = 0, int_src, otg_status;
+        TRACE_MSG0(TCD, "--");
+	daemonize();
+	reparent_to_init();
+	sprintf(current->comm, "isp1301_thread");
+                
+        /* loop until told to terminate
+         */
+	while (!isp1301_terminate) {
+
+                #if 0
+                // XXX TESTING
+                static int count = 0;
+                count++;
+                if (!(count % 5))
+                        isp1301_dp_pullup_func(tcd_instance->otg, !(count % 10)? 1 : 2);
+                #endif
+
+                /* sleep for a while if not enabled
+                 */
+                if (!isp1301_en) {
+                        TRACE_MSG1(TCD, "WAITING: %d", ISP1301_THREAD_TIMEOUT_DIS);
+                        interruptible_sleep_on_timeout(&isp1301_wait, ISP1301_THREAD_TIMEOUT_DIS);
+                        continue;
+                }
+
+                /* update 1301
+                 */
+                if (isp1301_bh_proc)
+                        isp1301_bh_proc(NULL);
+                
+                //TRACE_MSG1(TCD, "SLEEPING: %d", ISP1301_THREAD_TIMEOUT_EN);
+                interruptible_sleep_on_timeout(&isp1301_wait, ISP1301_THREAD_TIMEOUT_EN);
+                //TRACE_MSG0(TCD, "RUNNING");
+	}
+	TRACE_MSG0(TCD, "Exiting");
+	complete_and_exit(&mx2_exited, 0);
+}
+
+extern int isp1301_bh_first;
+/*! isp1301_thread_wakeup - wakeup the isp1301 task
+ */
+void isp1301_thread_wakeup(int enabled, int first)
+{
+        TRACE_MSG0(TCD, "--");
+        isp1301_bh_first |= first;
+        isp1301_en = enabled;
+	wake_up(&isp1301_wait);
+}
+
+/* ********************************************************************************************* */
+
+/*! isp1301_thread_init - initial tcd setup
+ * Allocate interrupts and setup hardware.
+ */
+int isp1301_thread_init (void (bh_proc)(void *))
+{
+	isp1301_terminate = 0;
+        TRACE_MSG0(TCD, "--");
+        isp1301_bh_proc = bh_proc;
+	init_waitqueue_head(&isp1301_wait);
+	THROW_IF((mx2_pid = kernel_thread(&isp1301_thread, NULL, CLONE_FS | CLONE_FILES | CLONE_SIGHAND)) < 0, error);
+        isp1301_thread_wakeup(0, 0);
+
+        CATCH(error) {
+                return -EINVAL;
+        }
+        return 0;
+}
+
+/*! isp1301_thread_exit - de-initialize
+ */
+void isp1301_thread_exit (wait_queue_head_t *wait_queue)
+{
+        TRACE_MSG0(TCD, "MX2_MOD_TCD_EXIT - terminating");
+        isp1301_bh_proc = NULL;
+	isp1301_terminate = 1;
+        if (wait_queue)
+                wake_up_interruptible(wait_queue);                              // wakeup waiting bottom half handler
+        isp1301_thread_wakeup(0, 0);
+	wait_for_completion(&mx2_exited);
+        TRACE_MSG0(TCD, "MX2_MOD_TCD_EXIT - terminated");
+}
+
diff -uNr linux/drivers/no-otg/ocd/max3353e/Config.in linux/drivers/otg/ocd/max3353e/Config.in
--- linux/drivers/no-otg/ocd/max3353e/Config.in	1970-01-01 01:00:00.000000000 +0100
+++ linux/drivers/otg/ocd/max3353e/Config.in	2006-09-01 21:41:32.000000000 +0200
@@ -0,0 +1,19 @@
+
+#
+# @(#) balden@belcarra.com|otg/ocd/max3353e/Config.in|20060831021117|26677
+# Copyright (c) 2004 Belcarra
+# Copyright (c) 2005-2006 Belcarra Technologies 2005 Corp
+#
+# MAX 3353E TCD
+
+if [ "$CONFIG_OTG_MAX3353E" = "y" ]; then
+    mainmenu_option next_comment
+
+    comment 'MAX 3353E (i2c)'
+
+        dep_bool 'Proc FS debug' CONFIG_OTG_MAX3353E_PROCFS $CONFIG_OTG_MAX3353E
+
+    endmenu
+else
+    define_bool CONFIG_OTG_MAX3353E_PROCFS n
+fi
diff -uNr linux/drivers/no-otg/ocd/max3353e/Makefile linux/drivers/otg/ocd/max3353e/Makefile
--- linux/drivers/no-otg/ocd/max3353e/Makefile	1970-01-01 01:00:00.000000000 +0100
+++ linux/drivers/otg/ocd/max3353e/Makefile	2006-09-01 21:41:32.000000000 +0200
@@ -0,0 +1,100 @@
+#
+# Makefile for the kernel USBD (device not host) drivers.
+# @(#) balden@belcarra.com|otg/ocd/max3353e/Makefile-l24|20060831021117|12633
+#
+# Copyright (c) 2004 Belcarra
+# Copyright (c) 2005-2006 Belcarra Technologies 2005 Corp
+
+# Subdirs.
+# This is a bit complex, because some subdirs are for
+# proprietary code, and are simply not present in a
+# general distribution.
+
+# The all-CAPS *_DIRS get nuked in the new versions
+# of Rules.make, so use only the subdir-* methods.
+subdir-y 	:=
+subdir-m 	:=
+subdir-n 	:=
+subdir- 	:=
+
+# The target object and module list name.
+
+O_TARGET	:= max3353e_target.o
+list-multi      := max3353e_tcd.o
+
+# Objects that export symbols.
+
+export-objs	:= i2c-max3353e.o max3353e.o
+
+# Multipart objects.
+
+#db1550_tcd-objs	:= tcd-init-l24.o tcd-omap-h2.o max3353e.o i2c-l26.o tcd.o max3353e-procfs.o
+#db1550_tcd-objs	:= tcd-init-l24.o tcd-db1550.o tcd-mx2ads.o max3353e.o i2c-l26.o tcd.o max3353e-procfs.o
+db1550_tcd-objs		:= tcd-init-l24.o max3353e.o tcd-db1550.o i2c-max3353e.o
+# Optional parts of multipart objects.
+
+# Object file lists.
+
+obj-y		:=
+obj-m		:=
+obj-n		:=
+obj-		:=
+
+# Each configuration option enables a list of files.
+
+obj-$(CONFIG_OTG_MAX3353E_DB1550)	+= db1550_tcd.o
+
+
+# Object files in subdirectories
+
+
+# Extract lists of the multi-part drivers.
+# The 'int-*' lists are the intermediate files used to build the multi's.
+
+multi-y		:= $(filter $(list-multi), $(obj-y))
+multi-m		:= $(filter $(list-multi), $(obj-m))
+int-y		:= $(sort $(foreach m, $(multi-y), $($(basename $(m))-objs)))
+int-m		:= $(sort $(foreach m, $(multi-m), $($(basename $(m))-objs)))
+
+# Files that are both resident and modular: remove from modular.
+
+obj-m		:= $(filter-out $(obj-y), $(obj-m))
+int-m		:= $(filter-out $(int-y), $(int-m))
+
+# Translate to Rules.make lists.
+
+O_OBJS		:= $(filter-out $(export-objs), $(obj-y))
+OX_OBJS		:= $(filter     $(export-objs), $(obj-y))
+M_OBJS		:= $(sort $(filter-out $(export-objs), $(obj-m)))
+MX_OBJS		:= $(sort $(filter     $(export-objs), $(obj-m)))
+MI_OBJS		:= $(sort $(filter-out $(export-objs), $(int-m)))
+MIX_OBJS	:= $(sort $(filter     $(export-objs), $(int-m)))
+
+# The global Rules.make.
+
+DOT_DIR=$(TCD_DIR)/max3353e
+
+include $(TOPDIR)/Rules.make
+OTG=$(TOPDIR)/drivers/otg
+OCD_DIR=$(OTG)ocd/
+OTGLIB=$(OTG)/ocd/otglib
+INCLUDE_DIRS =          -I$(OTG)
+EXTRA_CFLAGS +=          -Wno-missing-prototypes -Wno-unused -Wno-format ${INCLUDE_DIRS}
+EXTRA_CFLAGS_nostdinc += -Wno-missing-prototypes -Wno-unused -Wno-format ${INCLUDE_DIRS}
+
+I2C=$(OTG)/ocd/otg-i2c
+PCD=$(OTG)/ocd/otg-pcd
+TCD=$(OTG)/ocd/otg-tcd
+vpath %.c $(USBDCORE_DIR) $(OCD_DIR) $(PCD) $(TCD) $(OTGLIB)
+
+
+# Link rules for multi-part drivers.
+
+db1550_tcd.o: $(db1550_tcd-objs)
+	$(LD) -r -o $@ $(db1550_tcd-objs)
+
+# dependencies:
+
+# local
+
+
diff -uNr linux/drivers/no-otg/ocd/max3353e/Makefile-l26 linux/drivers/otg/ocd/max3353e/Makefile-l26
--- linux/drivers/no-otg/ocd/max3353e/Makefile-l26	1970-01-01 01:00:00.000000000 +0100
+++ linux/drivers/otg/ocd/max3353e/Makefile-l26	2006-09-01 21:41:32.000000000 +0200
@@ -0,0 +1,28 @@
+#
+# Makefile for the kernel USBD (device not host) drivers.
+# @(#) balden@belcarra.com|otg/ocd/max3353e/Makefile-l26|20060831021117|56045
+#
+# Copyright (c) 2004 Belcarra Technologies Corp
+# Copyright (c) 2005-2006 Belcarra Technologies 2005 Corp
+
+DOT_DIR=$(TCD_DIR)/max3353e
+
+OTG=$(TOPDIR)/drivers/otg
+HCD_DIR=$(OTG)/hcd
+TCD_DIR=$(OTG)/tcd
+USBDCORE_DIR=$(OTG)/usbdcore
+OTGCORE_DIR=$(OTG)/otgcore
+INCLUDE_DIRS =          -I$(OTG)
+EXTRA_CFLAGS +=          -Wno-missing-prototypes -Wno-unused -Wno-format ${INCLUDE_DIRS}
+EXTRA_CFLAGS_nostdinc += -Wno-missing-prototypes -Wno-unused -Wno-format ${INCLUDE_DIRS}
+
+vpath %.c $(USBDCORE_DIR) $(OCD_DIR)
+
+
+# Link rules for multi-part drivers.
+
+db1550_tcd-objs	:= ../tcd-init-l24.o max3353e.o i2c-l26.o ../tcd.o
+obj-$(CONFIG_OTG_MAX3353E_DB1550)	+= db1550_tcd.o
+
+
+
diff -uNr linux/drivers/no-otg/ocd/max3353e/README linux/drivers/otg/ocd/max3353e/README
--- linux/drivers/no-otg/ocd/max3353e/README	1970-01-01 01:00:00.000000000 +0100
+++ linux/drivers/otg/ocd/max3353e/README	2006-09-01 21:41:32.000000000 +0200
@@ -0,0 +1,9 @@
+Some notes for using max3353 tranciever in DB1550 development board
+1- Patch kernel with AMD DB1550 i2c driver.
+2- Patch arch/mips/au1000/db1x00/irqmap.c
+#if defined(CONFIG_MIPS_DB1550) || defined(CONFIG_MIPS_MTX2)
+        { AU1000_GPIO_3, INTC_INT_LOW_LEVEL, 0 }, // PCMCIA Card 0 IRQ#
+        { AU1000_GPIO_5, INTC_INT_LOW_LEVEL, 0 }, // PCMCIA Card 1 IRQ#
+        { AU1500_GPIO_207, INTC_INT_FALL_EDGE, 0 }, // OTG tranciever
+#else
+			
diff -uNr linux/drivers/no-otg/ocd/max3353e/i2c-max3353e.c linux/drivers/otg/ocd/max3353e/i2c-max3353e.c
--- linux/drivers/no-otg/ocd/max3353e/i2c-max3353e.c	1970-01-01 01:00:00.000000000 +0100
+++ linux/drivers/otg/ocd/max3353e/i2c-max3353e.c	2006-09-01 21:41:32.000000000 +0200
@@ -0,0 +1,178 @@
+/* @(#) balden@belcarra.com|otg/ocd/max3353e/i2c-max3353e.c|20060831021117|42179
+ *	Copyright (c) 2005-2006 Belcarra Technologies 2005 Corp
+ */
+#include <otg/otg-compat.h>
+
+#include <asm/irq.h>
+#include <asm/system.h>
+#include <asm/io.h>
+#include <linux/i2c.h>
+
+#include <otg/pcd-include.h>
+#include <linux/pci.h>
+#include <otghw/max3353e-hardware.h>
+//#include "isp1301.h"
+
+/*
+ * N.B. i2c functions must not be called from interrupt handlers
+ */
+
+static struct file *i2c_file;
+static struct i2c_client *db1550_i2c_client;
+static int initstate_i2c;
+static int initstate_region;
+#define MAX_I2C 16
+
+#ifdef CONFIG_OMAP_H2
+#define ADAPTOR_NAME "OMAP I2C"
+#endif /* CONFIG_OMAP_H2 */
+#ifdef CONFIG_ARCH_MAINSTONE
+#define ADAPTOR_NAME "PXA-I2C-Adapter"
+#endif /* CONFIG_ARCH_MAINSTONE */
+#ifdef CONFIG_OTG_DB1550_J15
+#define ADAPTOR_NAME "pb1550 adapter"
+#endif /* CONFIG_OTG_DB1550_J15 */
+
+void i2c_writeb(u8 subaddr, u8 buf)
+{
+        char tmpbuf[2];
+        int ret;
+
+        tmpbuf[0] = subaddr;    /*register number */
+        tmpbuf[1] = buf;        /*register data */
+
+        ret = i2c_master_send(db1550_i2c_client, &tmpbuf[0], 2);
+        while (ret != 2){
+                ret = i2c_master_send(db1550_i2c_client, &tmpbuf[0], 2);
+        }
+        TRACE_MSG1(TCD, "I2C write %d\n", ret);
+
+}
+
+void i2c_close()
+{
+        if (initstate_i2c)
+                filp_close(i2c_file, NULL);
+        initstate_i2c = 0;
+
+}
+
+int i2c_configure (char *name, int addr)
+{
+        char filename[20];
+        int tmp;
+
+        RETURN_ZERO_IF(initstate_i2c);
+
+        /*find the I2C driver we need
+         */
+        for (tmp = 0; tmp < MAX_I2C; tmp++) {
+
+                sprintf(filename, "/dev/i2c-%d", tmp);
+
+                printk(KERN_INFO"%s: %s\n", __FUNCTION__, filename);
+
+                UNLESS (IS_ERR(i2c_file = filp_open(filename, O_RDWR, 0))) {
+
+                        //printk(KERN_INFO"%s: %s found\n", __FUNCTION__, filename);
+
+                        /*found some driver */
+                        db1550_i2c_client = (struct i2c_client *)i2c_file->private_data;
+
+                        printk(KERN_INFO"%s: found %s\n", __FUNCTION__, db1550_i2c_client->adapter->name);
+                        if (strlen(db1550_i2c_client->adapter->name) >= 8) {
+                                if (!strncmp(db1550_i2c_client->adapter->name, name, strlen(name)))
+                                        break;  /*we found our driver! */
+                        }
+                        db1550_i2c_client = NULL;
+                        filp_close(i2c_file, NULL);
+                }
+                printk(KERN_INFO"%s: %s %d\n", __FUNCTION__, filename, i2c_file);
+        }
+        if (tmp == MAX_I2C) {                           // Nothing found
+                printk(KERN_ERR"%s: cannot find I2C driver", __FUNCTION__);
+                return -ENODEV;
+        }
+        db1550_i2c_client->addr = addr;
+        initstate_i2c = 1;
+        return 0;
+}
+
+/*! i2c_readb
+ * Read byte from i2c device
+ * @param subaddr
+ */
+u8 i2c_readb(u8 subaddr)
+{
+int ret;
+
+        u8 buf = 0;
+        ret = i2c_master_send(db1550_i2c_client, &subaddr, 1);
+        while (ret != 1){
+//              printk(KERN_INFO"send return %d\n", ret);
+                ret = i2c_master_send(db1550_i2c_client, &subaddr, 1);
+        }
+        //      printk(KERN_INFO"send return %d\n", ret);
+        ret = i2c_master_recv(db1550_i2c_client, &buf, 1);
+        while (ret != 1){
+                //      printk(KERN_INFO"rec return %d with %02x\n", ret, buf);
+                ret = i2c_master_send(db1550_i2c_client, &subaddr, 1);
+                while (ret != 1){
+                        //      printk(KERN_INFO"send return %d\n", ret);
+                        ret = i2c_master_send(db1550_i2c_client, &subaddr, 1);
+                }
+                ret = i2c_master_recv(db1550_i2c_client, &buf, 1);
+        }
+        //      printk(KERN_INFO"rec return %d with %02x\n", ret, buf);
+        TRACE_MSG2(TCD, "addr: %02x buf:  %02x", subaddr, buf);
+        return buf;
+}
+
+
+/*! i2c_readl
+ * Read long from i2c device
+ * @param subaddr
+ */
+u32 i2c_readl(u8 subaddr)
+{
+        int ret;
+        //        u32 buf = 0;
+        u8 buf[5];
+        ret = i2c_master_send(db1550_i2c_client, &subaddr, 1);
+        while (ret != 1){
+                printk(KERN_INFO"send return %d\n", ret);
+                ret = i2c_master_send(db1550_i2c_client, &subaddr, 1);
+        }
+        printk(KERN_INFO"send return %d\n", ret);
+        ret = i2c_master_recv(db1550_i2c_client, buf, 4);
+        while (ret != 4){
+                printk(KERN_INFO"rec return %d with %02x\n", ret, buf);
+                ret = i2c_master_send(db1550_i2c_client, &subaddr, 1);
+                while (ret != 1){
+                        printk(KERN_INFO"send return %d\n", ret);
+                        ret = i2c_master_send(db1550_i2c_client, &subaddr, 1);
+                }
+                ret = i2c_master_recv(db1550_i2c_client, buf, 4);
+        }
+        printk(KERN_INFO"rec return %d with %02x %02x %02x %02x\n", ret, buf[0], buf[1], buf[2], buf[3]);
+        TRACE_MSG2(TCD, "addr: %02x buf:  %08x", subaddr, buf);
+        return buf;
+}
+
+int i2c_readw (int addr)
+{
+        int data;
+
+        data = 0;
+
+        return data;
+}
+
+
+EXPORT_SYMBOL(i2c_writeb);
+EXPORT_SYMBOL(i2c_readw);
+EXPORT_SYMBOL(i2c_readb);
+EXPORT_SYMBOL(i2c_readl);
+EXPORT_SYMBOL(i2c_configure);
+EXPORT_SYMBOL(i2c_close);
+
diff -uNr linux/drivers/no-otg/ocd/max3353e/max3353-procfs.c linux/drivers/otg/ocd/max3353e/max3353-procfs.c
--- linux/drivers/no-otg/ocd/max3353e/max3353-procfs.c	1970-01-01 01:00:00.000000000 +0100
+++ linux/drivers/otg/ocd/max3353e/max3353-procfs.c	2006-09-01 21:41:32.000000000 +0200
@@ -0,0 +1,303 @@
+/*
+ * otg/ocd/max3353e/max3353-procfs.c - USB Device Core Layer
+ * @(#) balden@belcarra.com|otg/ocd/max3353e/max3353-procfs.c|20060831021117|57336
+ *
+ *      Copyright (c) 2004-2005 Belcarra
+ *	Copyright (c) 2005-2006 Belcarra Technologies 2005 Corp
+ *
+ * By:
+ *      Stuart Lynne <sl@belcarra.com>,
+ *      Shahrad Payandeh <sp@belcarra.com>,
+ *      Bruce Balden <balden@belcarra.com>
+ *
+ *
+ */
+/*!
+ * @file otg/ocd/max3353e/max3353-procfs.c
+ * @brief Implement /proc/max3353 to dump MAX3353 registers.
+ *
+ *
+ * @ingroup MAX3353TCD
+ */
+
+
+#include <otg/pcd-include.h>
+#include "otghw/max3353-hardware.h"
+#include "max3353.h"
+
+#ifdef CONFIG_ARCH_MX2ADS
+#include <asm/arch/mx2.h>
+#define MX2_OTG_XCVR_DEVAD                      0x18
+#define MX2_SEQ_OP_REG                          0x19
+#define MX2_SEQ_RD_STARTAD                      0x1a
+#define MX2_I2C_OP_CTRL_REG                     0x1b
+#define MX2_SCLK_TO_SCL_HPER                    0x1e
+#define MX2_I2C_INTERRUPT_AND_CTRL              0x1f
+
+#define OTG_BASE_ADDR                           0x10024000
+//#define OTG_I2C_BASE                            (OTG_BASE_ADDR+0x100)
+
+#endif /* CONFIG_ARCH_MX2ADS */
+
+#ifdef CONFIG_OTG_MAX3353_PROCFS
+/* Proc Filesystem *************************************************************************** */
+
+extern struct max3353_private max3353_private;
+
+#define MAX_HISTORY     6
+struct reg_list {
+        u8 reg;
+        u8 size;
+        char *name;
+        u32 values[MAX_HISTORY];
+};
+
+#define REG(r, s)  {r, s, #r, }
+
+struct reg_list max3353_prod_list[] = {
+        REG(MAX3353_VENDOR_ID, 2),
+        REG(MAX3353_PRODUCT_ID, 2),
+        REG(MAX3353_VERSION_ID, 2),
+        { 0, 1, NULL,},
+};
+struct reg_list max3353_reg_list[] = {
+        REG(MAX3353_OTG_CONTROL_SET, 1),
+        REG(MAX3353_INTERRUPT_SOURCE, 1),
+        REG(MAX3353_INTERRUPT_LATCH_SET, 1),
+        REG(MAX3353_INTERRUPT_ENABLE_LOW_SET, 1),
+        REG(MAX3353_INTERRUPT_ENABLE_HIGH_SET, 1),
+        REG(MAX3353_MODE_CONTROL_1_SET, 1),
+        { 0, 1, NULL,},
+};
+struct reg_list max3353_spec_list[] = {
+        REG(MAX3353_MODE_CONTROL_2_SET, 1),
+        REG(MAX3353_OTG_STATUS, 1),
+        { 0, 1, NULL,},
+};
+struct reg_list max3301e_spec_list[] = {
+        REG(MAX3301E_SPECIAL_FUNCTION_1_SET, 1),
+        REG(MAX3301E_SPECIAL_FUNCTION_2_SET, 1),
+        { 0, 1, NULL,},
+};
+#ifdef CONFIG_ARCH_MX2ADS
+struct reg_list mx21_spec_list[] = {
+        REG(MX2_OTG_XCVR_DEVAD, 1),
+        REG(MX2_SEQ_OP_REG, 1),
+        REG(MX2_SEQ_RD_STARTAD, 1),
+        REG(MX2_I2C_OP_CTRL_REG, 1),
+        REG(MX2_SCLK_TO_SCL_HPER, 1),
+        REG(MX2_I2C_INTERRUPT_AND_CTRL, 1),
+        { 0, 1, NULL,},
+};
+#endif /* CONFIG_ARCH_MX2ADS */
+
+void max3353_update(struct reg_list *list)
+{
+        for (; list && list->name; list++) {
+                memmove(list->values + 1, list->values, sizeof(list->values) - sizeof(u32));
+                switch(list->size) {
+                case 1:
+                        list->values[0] = i2c_readb(list->reg);
+                        break;
+                case 2:
+                        list->values[0] = i2c_readw(list->reg);
+                        break;
+                case 4:
+                        list->values[0] = i2c_readl(list->reg);
+                        break;
+                }
+        }
+}
+
+#ifdef CONFIG_ARCH_MX2ADS
+static u8 __inline__ mx2_rb(u32 port)
+{
+        return *(volatile u8 *) (MX2_IO_ADDRESS(port + OTG_I2C_BASE));
+}
+
+
+void mx21_update(struct reg_list *list)
+{
+        for (; list && list->name; list++) {
+                memmove(list->values + 1, list->values, sizeof(list->values) - sizeof(u32));
+                list->values[0] = mx2_rb(list->reg);
+        }
+}
+
+#endif /* CONFIG_ARCH_MX2ADS */
+
+void max3353_update_all(void)
+{
+        max3353_update(max3353_reg_list);
+        switch (max3353_private.transceiver_map->transceiver_type) {
+        case max3353:
+                max3353_update(max3353_spec_list);
+                break;
+        case max3301e:
+                max3353_update(max3301e_spec_list);
+                break;
+        default:
+                break;
+        }
+#ifdef CONFIG_ARCH_MX2ADS
+        mx21_update(mx21_spec_list);
+#endif /* CONFIG_ARCH_MX2ADS */
+}
+
+
+
+/*
+ * dohex
+ *
+ */
+static void dohexdigit (char *cp, unsigned char val)
+{
+        if (val < 0xa)
+                *cp = val + '0';
+        else if ((val >= 0x0a) && (val <= 0x0f))
+                *cp = val - 0x0a + 'a';
+}
+
+/*
+ * dohex
+ *
+ */
+static void dohexval (char *cp, unsigned char val)
+{
+        dohexdigit (cp++, val >> 4);
+        dohexdigit (cp++, val & 0xf);
+}
+
+int max3353_dump(char *buf, char *name, char *fmt, struct reg_list *reg)
+{
+        int len = 0, i;
+        len += sprintf (buf + len, "%-20s  %-34s [%03x]:  ", name, reg->name, reg->reg);
+        len += sprintf (buf + len, fmt, reg->values[0]);
+        for (i = 1; i < MAX_HISTORY; i++)
+                if (reg->values[i - 1] == reg->values[i])
+                        len += sprintf (buf + len, "         ");
+                else
+                        len += sprintf (buf + len, fmt, reg->values[i]);
+        len += sprintf (buf + len, "\n");
+        return len;
+}
+
+int max3353_dump_list(char * buf, char *name, struct reg_list *list)
+{
+        int len = 0;
+        for (; list && list->name; list++)
+                switch(list->size) {
+                case 1: len += max3353_dump(buf + len, name, "       %02x", list); break;
+                case 2: len += max3353_dump(buf + len, name, "     %04x", list); break;
+                case 4: len += max3353_dump(buf + len, name, " %08x", list); break;
+                }
+        return len;
+}
+
+int max3353_dump_all(char *buf)
+{
+        int len = 0;
+        len += max3353_dump_list(buf + len, "MAX3353 Standard", max3353_reg_list);
+        switch (max3353_private.transceiver_map->transceiver_type) {
+        case max3353:
+                len += max3353_dump_list(buf + len, "MAX3353 Extra", max3353_spec_list);
+                break;
+        case max3301e:
+                len += max3353_dump_list(buf + len, "MAX3301E Extra", max3301e_spec_list);
+                break;
+        default:
+                break;
+        }
+#ifdef CONFIG_ARCH_MX2ADS
+        len += max3353_dump_list(buf + len, "MX21 ADS Extra", mx21_spec_list);
+#endif /* CONFIG_ARCH_MX2ADS */
+        return len;
+}
+
+
+
+/*!
+ * max3353_device_proc_read - implement proc file system read.
+ *
+ * Standard proc file system read function.
+ *
+ * We let upper layers iterate for us, *pos will indicate which device to return
+ * statistics for.
+ */
+static ssize_t max3353_device_proc_read_functions (struct file *file, char *buf, size_t count, loff_t * pos)
+{
+        unsigned long page;
+        int len = 0;
+        int index;
+        int i;
+        u32 r;
+
+        int config_size;
+
+        // get a page, max 4095 bytes of data...
+        RETURN_EINVAL_UNLESS ((page = get_free_page (GFP_KERNEL)));
+
+        len = 0;
+        index = (*pos)++;
+
+        switch(index) {
+        case 0:
+                len += sprintf ((char *) page + len, "MAX3353 Transceiver Registers\n");
+
+                max3353_update_all();
+                len += sprintf ((char *) page + len , "Vendor: %04x Product: %04x Revision: %04x %s\n",
+                                max3353_private.vendor, max3353_private.product,
+                                max3353_private.revision, max3353_private.transceiver_map->name
+                                );
+
+                len += max3353_dump_all((char *) page + len);
+                len += sprintf ((char *) page + len, "\n");
+
+                break;
+
+        case 1:
+                break;
+
+        }
+
+        if (len > count) {
+                len = -EINVAL;
+        }
+        else if ((len > 0) && copy_to_user (buf, (char *) page, len)) {
+                len = -EFAULT;
+        }
+        else {
+        }
+        free_page (page);
+        return len;
+}
+
+static struct file_operations max3353_device_proc_operations_functions = {
+        read:max3353_device_proc_read_functions,
+};
+
+/* Module init ******************************************************************************* */
+
+static int max3353_procfs_init (void)
+{
+        struct proc_dir_entry *p;
+
+        // create proc filesystem entries
+        if ((p = create_proc_entry ("max3353", 0, 0)) == NULL)
+                return -ENOMEM;
+
+        p->proc_fops = &max3353_device_proc_operations_functions;
+
+        max3353_update_all();
+
+        return 0;
+}
+static void max3353_procfs_exit (void)
+{
+        remove_proc_entry ("max3353", NULL);
+}
+#else
+static int max3353_procfs_init (void) { return 0;
+}
+static void max3353_procfs_exit (void) { }
+#endif
diff -uNr linux/drivers/no-otg/ocd/max3353e/max3353.c linux/drivers/otg/ocd/max3353e/max3353.c
--- linux/drivers/no-otg/ocd/max3353e/max3353.c	1970-01-01 01:00:00.000000000 +0100
+++ linux/drivers/otg/ocd/max3353e/max3353.c	2006-09-01 21:41:32.000000000 +0200
@@ -0,0 +1,706 @@
+/*
+ * otg/ocd/max3353e/max3353.c -- USB Transceiver Controller driver
+ * @(#) balden@belcarra.com|otg/ocd/max3353e/max3353.c|20060831021117|27226
+ *
+ *      Copyright (c) 2003-2005 Belcarra
+ *	Copyright (c) 2005-2006 Belcarra Technologies 2005 Corp
+ *
+ * By:
+ *      Stuart Lynne <sl@lbelcarra.com>,
+ *      Shahrad Payandeh <sp@belcarra.com>,
+ *      Bruce Balden <balden@belcarra.com>
+ *
+ */
+/*!
+ * @file otg/ocd/max3353e/max3353.c
+ * @brief MAX3353 OTG Transceiver Driver.
+ *
+ * This is the generic MAX3353 TCD core support.
+ *
+ * Notes:
+ *
+ * 1. The MAX3353 can control the speed and suspend directly, would it be
+ * appropriate to allow state machine to control this.
+ *
+ * 2. The MAX3353 has auto connect feature, can this be used without change
+ * to state machine.
+ *
+ * 3. The MAX3353 can control the ADR/PSW pin to enable / disable external
+ * charge pump.
+ *
+ * @ingroup MAX3353TCD
+ */
+
+#include <otg/otg-compat.h>
+
+#include <asm/irq.h>
+#include <asm/system.h>
+#include <asm/io.h>
+#include <linux/i2c.h>
+
+#include <otg/usbp-chap9.h>
+#include <otg/usbp-bus.h>
+#include <otg/otg-trace.h>
+#include <otg/otg-api.h>
+#include <otg/otg-hcd.h>
+#include <otg/otg-tcd.h>
+#include <otg/otg-ocd.h>
+#include <otg/otg-pcd.h>
+
+#include <otghw/max3353-hardware.h>
+#include "max3353.h"
+
+
+struct otg_transceiver_map max3353_transceiver_map[] = {
+        { max3353, 0x4cc, 0x1301, 0x00, "Phillips MAX3353", },
+        { max3301e, 0x6a0b, 0x0133, 0x00, "Maxim MAX3301E", },
+        { 0, 0, 0, 0, "Unknown Transceiver", },
+};
+
+struct max3353_private max3353_private;
+
+
+
+/* ********************************************************************************************* */
+#define TRACE_I2CB(t,r) TRACE_MSG3(t, "%-40s[%02x] %02x", #r, r, i2c_readb(r))
+#define TRACE_I2CW(t,r) TRACE_MSG3(t, "%-40s[%02x] %04x", #r, r, i2c_readw(r))
+#define TRACE_GPIO(t,b,r) TRACE_MSG2(t, "%-40s %04x", #r, readw(b + r))
+#define TRACE_REGL(t,r) TRACE_MSG2(t, "%-40s %08x", #r, readl(r))
+
+
+
+/*! max3353_int_src - update interrupt source test mask
+ *
+ * This sets the current mask and updates the interrupt registers to match.
+ */
+void max3353_int_src(u8 int_src)
+{
+        TRACE_MSG1(TCD, "setting int_src %02x", int_src);
+        max3353_private.int_src = int_src;
+        i2c_writeb(MAX3353_INTERRUPT_ENABLE_LOW_CLR, ~int_src);
+        i2c_writeb(MAX3353_INTERRUPT_ENABLE_HIGH_CLR, ~int_src);
+        i2c_writeb(MAX3353_INTERRUPT_ENABLE_LOW_SET, int_src);
+        i2c_writeb(MAX3353_INTERRUPT_ENABLE_HIGH_SET, int_src);
+        i2c_writeb(MAX3353_INTERRUPT_LATCH_CLR, int_src);
+}
+
+/*! max3353_int_src_set - add to the interrupt source mask
+ *
+ */
+void max3353_int_src_set(u8 int_src)
+{
+        max3353_int_src(int_src |= max3353_private.int_src);
+}
+
+/*! max3353_int_src_clr - remove from the interrup source mask
+ */
+void max3353_int_src_clr(u8 int_src)
+{
+        max3353_int_src(int_src = max3353_private.int_src & ~int_src);
+}
+
+/* ********************************************************************************************* */
+
+
+/*! max3353_event_set_irq
+ */
+void max3353_event_set_irq(struct otg_instance *otg, int changed, int flag, u32 input, otg_tag_t tag, char *mset, char *mreset)
+{
+        TRACE_MSG5(TCD, "changed: %x flag: %x input: %08x %s %s", changed, flag, input,
+                        flag ? mset: mreset,
+                        changed ? "": "(ignored)");
+
+        otg_event_set_irq(otg, changed, flag, input, tag, flag ? mset : mreset);
+}
+
+/*! max3353_update - called from mx2_i2c_xcvr_bh to find changes in the max3353 Interrupt Source Register
+ */
+u8 max3353_update_int_src(struct otg_instance *otg, u8 int_src, u8 changed)
+{
+        UNLESS (changed) return int_src;
+        TRACE_MSG2(TCD, "CHANGED INTERRUPT_SOURCE: %02x (%02x) ", int_src, changed);
+
+        /* Core events
+         */
+        max3353_event_set_irq(otg, MAX3353_ID_GND & changed, MAX3353_ID_GND & int_src,
+                        ID_GND, TCD, "ID_GND", "ID_GND/");
+        max3353_event_set_irq(otg, MAX3353_ID_FLOAT & changed, MAX3353_ID_FLOAT & int_src,
+                        ID_FLOAT, TCD, "ID_FLOAT", "ID_FLOAT/");
+        max3353_event_set_irq(otg, MAX3353_VBUS_VLD & changed, MAX3353_VBUS_VLD & int_src,
+                        VBUS_VLD, TCD, "VBUS_VLD", "VBUS_VLD/");
+        max3353_event_set_irq(otg, MAX3353_SESS_VLD & changed, MAX3353_SESS_VLD & int_src,
+                        A_SESS_VLD, TCD, "A_SESS_VLD", "A_SESS_VLD/");
+
+        max3353_event_set_irq(otg, MAX3353_DM_HI & changed, MAX3353_DM_HI & int_src,
+                        DM_HIGH, TCD, "DM_HIGH", "DM_HIGH/");
+
+        max3353_event_set_irq(otg, MAX3353_DP_HI & changed, MAX3353_DP_HI & int_src,
+                        DP_HIGH, TCD, "DP_HIGH", "DP_HIGH/");
+
+        //max3353_event_set_irq(otg, MAX3353_SE1 & changed, ((MAX3353_SE1 & int_src) == 0), SE0_DET, TCD, "SE0", "SE0/");
+
+        /* carkit
+         */
+        max3353_event_set_irq(otg, MAX3353_CR_INT & changed, !(MAX3353_CR_INT & int_src),
+                        CR_INT_DET, TCD, "CR_INT", "CR_INT/");
+
+        //TRACE_MSG3(TCD, "MAX3353_SE1: %02x & int_src: %02x = %02x", MAX3353_SE1, int_src, MAX3353_SE1 & int_src);
+        //max3353_event_set_irq(otg, MAX3353_SE1 & changed, ((MAX3353_SE1 & int_src) == MAX3353_SE1),
+        //                SE1_DET, TCD, "SE1", "SE1/");
+
+
+        /* A-Device
+         */
+        max3353_event_set_irq(otg, MAX3353_BDIS_ACON & changed, !(MAX3353_BDIS_ACON & int_src),
+                        BDIS_ACON, TCD, "BDIS_ACON", "BDIS_ACON/" );
+
+        return int_src;
+}
+
+/*! max3353_update - called from mx2_i2c_xcvr_bh to find changes in the max3353 OTG Status Register
+ */
+u8 max3353_update_otg_status(struct otg_instance *otg, u8 otg_status, u8 changed)
+{
+        UNLESS (changed) return otg_status;
+        TRACE_MSG2(TCD, "CHANGED OTG_STATUS_REG: %02x (%02x) ", otg_status, changed);
+
+        max3353_event_set_irq(otg, MAX3353_B_SESS_VLD & changed, MAX3353_B_SESS_VLD & otg_status,
+                        B_SESS_VLD, TCD, "B_SESS_VLD", "B_SESS_VLD/");
+        max3353_event_set_irq(otg, MAX3353_B_SESS_END & changed, MAX3353_B_SESS_END & otg_status,
+                        B_SESS_END, TCD, "B_SESS_END", "B_SESS_END/");
+
+        //otg_b_sess_vld(otg, first || (MAX3353_B_SESS_VLD & changed), MAX3353_B_SESS_VLD & otg_status, MAX3353_NAME );
+        return otg_status;
+}
+
+int max3353_bh_first;
+
+
+/*! max3353_bh
+ */
+void max3353_bh(void *arg)
+{
+        u8 int_lat, int_src = 0, otg_status = 0, special_function_22 = 0;
+        static u8 int_src_saved, otg_status_saved, special_function_22_saved;
+
+        //TRACE_MSG0(TCD, "--");
+        /* read the interrupt latch and and clear latch and update otg
+         */
+        if ((int_lat = i2c_readb(MAX3353_INTERRUPT_LATCH_SET))) {
+                //TRACE_MSG1(TCD, "CLEARING INT LAT: %02x", int_lat);
+                i2c_writeb(MAX3353_INTERRUPT_LATCH_CLR, int_lat);
+        }
+
+        /* If int_lat shows a change or we are forcing an update, read interrupt source.
+         * Note that we mask DP_HI and DM_HI when LOC_CONN is set, we don't want them
+         * when USB Bus is in use.
+         *
+         * Mask with bits we are currently interested in.
+         */
+        int_src = i2c_readb(MAX3353_INTERRUPT_SOURCE) & max3353_private.int_src;
+
+        UNLESS ( max3353_private.flags & MAX3353_LOC_CONN) {
+        }
+
+
+        /* further work if B-Device and VBUS_VLD
+         */
+        if ( (!(int_src & MAX3353_ID_GND) && (int_src & MAX3353_VBUS_VLD)) ||
+                        (!(int_src_saved & MAX3353_ID_GND) && (int_src_saved & MAX3353_VBUS_VLD)) )
+        {
+                /* read the otg_status register and update otg state machine
+                 *
+                 * XXX this needs to be conditional on Transceiver type. The
+                 * MAX3301E for example supports sess_end in a separate register.
+                 */
+                switch (max3353_private.transceiver_map->transceiver_type) {
+                case max3353:
+                        otg_status = i2c_readb(MAX3353_OTG_STATUS);
+                        if (max3353_bh_first)
+                                otg_status_saved = ~otg_status;
+                        otg_status_saved = max3353_update_otg_status(tcd_instance->otg, otg_status,
+                                        otg_status_saved ^ otg_status);
+                        break;
+                case max3301e:
+                        // XXX
+                        special_function_22 = i2c_readb(MAX3301E_SPECIAL_FUNCTION_2_SET);
+                        break;
+                case unknown_transceiver:
+                        break;
+                }
+        }
+        if (max3353_bh_first)
+                int_src_saved =  ~int_src;
+
+        int_src_saved = max3353_update_int_src(tcd_instance->otg, int_src, int_src_saved ^ int_src);
+
+        max3353_bh_first = 0;
+}
+
+/* ********************************************************************************************** */
+/*! max3353_vbus - Do we have Vbus (cable attached?)
+ * Return non-zero if Vbus is detected.
+ *
+ */
+int max3353_vbus (struct otg_instance *otg)
+{
+        return 0;
+}
+
+/*! max3353_id - Do we have Vbus (cable attached?)
+ * Return non-zero if Vbus is detected.
+ *
+ */
+int max3353_idvbus (struct otg_instance *otg)
+{
+        return 0;
+}
+
+
+/* ********************************************************************************************* */
+
+/*! max3353_chrg_vbus - used to enable or disable B-device Vbus charging
+ */
+void max3353_chrg_vbus(struct otg_instance *otg, u8 flag)
+{
+        TRACE_MSG0(TCD, "--");
+        switch (flag) {
+        case SET:
+                TRACE_MSG0(TCD, "CHRG_VBUS_SET");
+                i2c_writeb(MAX3353_OTG_CONTROL_SET, (u8) MAX3353_VBUS_CHRG);
+                break;
+        case RESET:
+                TRACE_MSG0(TCD, "CHRG_VBUS_RESET");
+                i2c_writeb(MAX3353_OTG_CONTROL_CLR, (u8) MAX3353_VBUS_CHRG);
+                break;
+        case PULSE:
+                break;
+        }
+}
+
+/*! max3353_drv_vbus - used to enable or disable A-device driving Vbus
+ */
+void max3353_drv_vbus(struct otg_instance *otg, u8 flag)
+{
+        TRACE_MSG0(TCD, "--");
+        switch (flag) {
+        case SET:
+                TRACE_MSG0(TCD, "DRV_VBUS_SET");
+                i2c_writeb(MAX3353_OTG_CONTROL_SET, (u8) MAX3353_VBUS_DRV);
+                break;
+        case RESET:
+                TRACE_MSG0(TCD, "DRV_VBUS_RESET");
+                i2c_writeb(MAX3353_OTG_CONTROL_CLR, (u8) MAX3353_VBUS_DRV);
+                break;
+        }
+}
+
+/*! max3353_dischrg_vbus - used to enable Vbus discharge
+ */
+void max3353_dischrg_vbus(struct otg_instance *otg, u8 flag)
+{
+        struct tcd_instance *tcd = otg->tcd;
+        TRACE_MSG0(TCD, "--");
+        switch (flag) {
+        case SET:
+                TRACE_MSG0(TCD, "OUTPUT: TCD_DISCHRG_VBUS_SET");
+                i2c_writeb(MAX3353_OTG_CONTROL_SET, MAX3353_VBUS_DISCHRG);
+                break;
+        case RESET:
+                TRACE_MSG0(TCD, "OUTPUT: TCD_DISCHRG_VBUS_RESET");
+                i2c_writeb(MAX3353_OTG_CONTROL_CLR, MAX3353_VBUS_DISCHRG);
+                break;
+        }
+}
+
+/*! max3353_dp_pullup_func - used to enable or disable peripheral connecting to bus
+ *
+ * C.f. 5.1.6, 5.1.7, 5.2.4 and 5.2.5
+ *
+ *                              host    peripheral
+ *              d+ pull-up      clr     set
+ *              d+ pull-down    set     clr
+ *
+ *              d- pull-up      clr     clr
+ *              d- pull-down    set     set
+ *
+ */
+void max3353_dp_pullup_func(struct otg_instance *otg, u8 flag)
+{
+        struct tcd_instance *tcd = (struct tcd_instance *)otg->tcd;
+        TRACE_MSG0(TCD, "--");
+        switch (flag) {
+        case SET:
+		printk(KERN_INFO"%s: --\n", __FUNCTION__);
+                max3353_private.flags |= MAX3353_LOC_CONN;
+                TRACE_MSG0(TCD, "MAX3353_LOC_CONN SET - Clr DP PULLDOWN");
+                //i2c_writeb(MAX3353_OTG_CONTROL_CLR, (u8) MAX3353_DP_PULLDOWN);
+                //i2c_writeb(MAX3353_OTG_CONTROL_CLR, (u8) (MAX3353_DP_PULLDOWN | MAX3353_DM_PULLDOWN) );
+                TRACE_MSG0(TCD, "MAX3353_LOC_CONN SET - Set DP PULLUP");
+                i2c_writeb(MAX3353_OTG_CONTROL_SET, (u8) MAX3353_DP_PULLUP);
+                break;
+
+        case RESET:
+                max3353_private.flags &= ~MAX3353_LOC_CONN;
+                TRACE_MSG0(TCD, "MAX3353_LOC_CONN RESET - Clr DP PULLUP");
+                i2c_writeb(MAX3353_OTG_CONTROL_CLR, (u8)MAX3353_DP_PULLUP);
+                TRACE_MSG0(TCD, "MAX3353_LOC_CONN SET - Set DP PULLDOWN");
+                //i2c_writeb(MAX3353_OTG_CONTROL_SET, (u8)MAX3353_DP_PULLDOWN);
+                //i2c_writeb(MAX3353_OTG_CONTROL_SET, (u8) (MAX3353_DP_PULLDOWN | MAX3353_DM_PULLDOWN) );
+                break;
+
+        case PULSE:
+                TRACE_MSG0(TCD, "MAX3353_LOC_CONN PULSE");
+                break;
+        }
+}
+
+/*! max3353_dm_pullup_func - used to enable or disable peripheral connecting to bus
+ *
+ */
+void max3353_dm_pullup_func(struct otg_instance *otg, u8 flag)
+{
+        struct tcd_instance *tcd = (struct tcd_instance *)otg->tcd;
+        TRACE_MSG0(TCD, "--");
+        switch (flag) {
+        case SET:
+                max3353_private.flags |= MAX3353_LOC_CONN;
+                TRACE_MSG0(TCD, "MAX3353_LOC_CONN SET - Clr DM PULLDOWN");
+                //i2c_writeb(MAX3353_OTG_CONTROL_CLR, (u8) MAX3353_DM_PULLDOWN);
+                TRACE_MSG0(TCD, "MAX3353_LOC_CONN SET - Set DM PULLUP");
+                i2c_writeb(MAX3353_OTG_CONTROL_SET, (u8) MAX3353_DM_PULLUP);
+                break;
+
+        case RESET:
+                max3353_private.flags &= ~MAX3353_LOC_CONN;
+                TRACE_MSG0(TCD, "MAX3353_LOC_CONN RESET - Clr DM PULLUP");
+                i2c_writeb(MAX3353_OTG_CONTROL_CLR, (u8)MAX3353_DM_PULLUP);
+                TRACE_MSG0(TCD, "MAX3353_LOC_CONN SET - Set DM PULLDOWN");
+                //i2c_writeb(MAX3353_OTG_CONTROL_SET, (u8)MAX3353_DM_PULLDOWN);
+                break;
+
+        case PULSE:
+                TRACE_MSG0(TCD, "MAX3353_LOC_CONN PULSE");
+                break;
+        }
+}
+
+/*! max3353_dm_det_func - used to enable or disable D- detect
+ *
+ */
+void max3353_dm_det_func(struct otg_instance *otg, u8 flag)
+{
+        struct tcd_instance *tcd = (struct tcd_instance *)otg->tcd;
+        TRACE_MSG0(TCD, "--");
+        switch (flag) {
+        case SET:
+                TRACE_MSG0(TCD, "setting DM_HI detect");
+                max3353_int_src_set(MAX3353_DM_HI);
+                max3353_bh_first = 1;
+                break;
+
+        case RESET:
+                TRACE_MSG0(TCD, "reseting DM_HI detect");
+                max3353_int_src_clr(MAX3353_DM_HI);
+                break;
+        }
+}
+
+/*! max3353_dp_det_func - used to enable or disable D+ detect
+ *
+ */
+void max3353_dp_det_func(struct otg_instance *otg, u8 flag)
+{
+        struct tcd_instance *tcd = (struct tcd_instance *)otg->tcd;
+        TRACE_MSG0(TCD, "--");
+        switch (flag) {
+        case SET:
+                TRACE_MSG0(TCD, "setting DP_HI detect");
+                max3353_int_src_set(MAX3353_DP_HI);
+                max3353_bh_first = 1;
+                break;
+
+        case RESET:
+                TRACE_MSG0(TCD, "reseting DP_HI detect");
+                max3353_int_src_clr(MAX3353_DP_HI);
+                break;
+        }
+}
+
+/*! max3353_bdis_acon_func - used to enable or disable auto a-connect
+ *
+ */
+void max3353_bdis_acon_func(struct otg_instance *otg, u8 flag)
+{
+        struct tcd_instance *tcd = (struct tcd_instance *)otg->tcd;
+        TRACE_MSG0(TCD, "--");
+        switch (flag) {
+        case SET:
+                TRACE_MSG0(TCD, "setting BDIS ACON");
+                i2c_writeb(MAX3353_OTG_CONTROL_SET, MAX3353_ID_PULLDOWN);
+                break;
+
+        case RESET:
+                TRACE_MSG0(TCD, "reseting BDIS ACON");
+                i2c_writeb(MAX3353_OTG_CONTROL_CLR, MAX3353_ID_PULLDOWN);
+                break;
+        }
+}
+
+/*! max3353_id_pulldown_func - used to enable or disable ID pulldown
+ *
+ */
+void max3353_id_pulldown_func(struct otg_instance *otg, u8 flag)
+{
+        struct tcd_instance *tcd = (struct tcd_instance *)otg->tcd;
+        TRACE_MSG0(TCD, "--");
+        switch (flag) {
+        case SET:
+                TRACE_MSG0(TCD, "setting ID PULLDOWN");
+                i2c_writeb(MAX3353_MODE_CONTROL_1_SET, MAX3353_BDIS_ACON_EN);
+                break;
+
+        case RESET:
+                TRACE_MSG0(TCD, "reseting ID PULLDOWN");
+                i2c_writeb(MAX3353_MODE_CONTROL_1_CLR, MAX3353_BDIS_ACON_EN);
+                break;
+        }
+}
+
+/*! max3353_audio_func - used to enable or disable Carkit Interrupt
+ *
+ */
+void max3353_audio_func(struct otg_instance *otg, u8 flag)
+{
+        struct tcd_instance *tcd = (struct tcd_instance *)otg->tcd;
+        TRACE_MSG0(TCD, "--");
+        switch (flag) {
+        case SET:
+                TRACE_MSG0(TCD, "setting CARKIT INT");
+                i2c_writeb(MAX3353_MODE_CONTROL_2_SET, MAX3353_AUDIO_EN);
+                break;
+
+        case RESET:
+                TRACE_MSG0(TCD, "reseting CARKIT INT");
+                i2c_writeb(MAX3353_MODE_CONTROL_2_CLR, MAX3353_AUDIO_EN);
+                break;
+        }
+}
+
+/*! max3353_uart_func - used to enable or disable transparent uart mode
+ *
+ */
+void max3353_uart_func(struct otg_instance *otg, u8 flag)
+{
+        struct tcd_instance *tcd = (struct tcd_instance *)otg->tcd;
+        TRACE_MSG0(TCD, "--");
+        switch (flag) {
+        case SET:
+                TRACE_MSG0(TCD, "setting UART");
+                i2c_writeb(MAX3353_MODE_CONTROL_1_SET, MAX3353_UART_EN);
+                /* XXX enable uart */
+                break;
+
+        case RESET:
+                MAX3353SG0(TCD, "reseting UART");
+                i2c_writeb(MAX3353_MODE_CONTROL_1_CLR, MAX3353_UART_EN);
+                /* XXX disable uart */
+                break;
+        }
+}
+
+/*! max3353_mono_func - used to enable or disable mono audio connection
+ *
+ */
+void max3353_mono_func(struct otg_instance *otg, u8 flag)
+{
+        struct tcd_instance *tcd = (struct tcd_instance *)otg->tcd;
+        TRACE_MSG0(TCD, "--");
+        switch (flag) {
+        case SET:
+                TRACE_MSG0(TCD, "setting MONO");
+                /* XXX enable mono output */
+                break;
+
+        case RESET:
+                TRACE_MSG0(TCD, "reseting MONO");
+                /* XXX disable mono output */
+                break;
+        }
+}
+
+
+/* ********************************************************************************************* */
+extern int max3353_procfs_init (void);
+extern void max3353_procfs_exit (void);
+
+/*! max3353_mod_init
+ */
+int max3353_mod_init(void)
+{
+        /* for interrupt bottom half handler
+         */
+        PREPARE_WORK_ITEM(max3353_private.bh, &max3353_bh, NULL);
+
+        /* setup max3353, enable interrupts, clear latch
+         * XXX when and how do we do DM_HI and DP_HI, when ID is present?
+         */
+        max3353_int_src(MAX3353_INT_SRC);
+        //max3353_int_src(MAX3353_NO_SE1_INT);
+
+        i2c_writeb(MAX3353_OTG_CONTROL_SET, MAX3353_DM_PULLDOWN | MAX3353_DP_PULLDOWN);
+
+#ifdef CONFIG_OTG_MAX3353_PROCFS
+        max3353_procfs_init ();
+#endif /* CONFIG_OTG_MAX3353_PROCFS */
+
+        return 0;
+}
+
+/*! max3353_mod_exit
+ */
+void max3353_mod_exit(void)
+{
+        i2c_writeb(MAX3353_INTERRUPT_ENABLE_LOW_CLR, 0xff);
+        i2c_writeb(MAX3353_INTERRUPT_ENABLE_HIGH_CLR, 0xff);
+        //i2c_close();
+#ifdef CONFIG_OTG_MAX3353_PROCFS
+        max3353_procfs_exit ();
+#endif /* CONFIG_OTG_MAX3353_PROCFS */
+
+        while (PENDING_WORK_ITEM(max3353_private.bh)) {
+                printk(KERN_ERR"%s: waiting for bh\n", __FUNCTION__);
+                schedule_timeout(10 * HZ);
+        }
+
+}
+
+/* ********************************************************************************************* */
+/*! max3353_configure - configure the MAX3353 for this host
+ * @param tx_mode - the type of connection between the host USB and the MAX3353
+ * @param spd_ctrl - suspend control method
+ */
+void  max3353_configure(tx_mode_t tx_mode, spd_ctrl_t spd_ctrl)
+{
+        u16 vendor = 0, product = 0, revision = 0;
+        u8 mode1, mode2, control;
+
+        struct otg_transceiver_map *map = max3353_transceiver_map;
+
+
+#if defined(CONFIG_OTG_MAX3353_MX2ADS) || defined(CONFIG_OTG_MAX3353_MX2ADS_MODULE)
+        u32 vp;
+        MAX3353SG0(TCD, "1. Read Transceiver ID's with long read");
+        vp = i2c_readl(MAX3353_VENDOR_ID);
+        max3353_private.vendor = vp & 0xffff;
+        max3353_private.product = vp >> 16;
+        max3353_private.revision = i2c_readw(MAX3353_VERSION_ID);
+        mode1 = i2c_readb(MAX3353_MODE_CONTROL_1_SET);
+        mode2 = i2c_readb(MAX3353_MODE_CONTROL_2_SET);
+
+        TRACE_MSG5(TCD, "2. OTG Transceiver: vendor: %04x product: %04x revision: %04x mode1: %02x mode2: %02x",
+                        vendor, product, revision, mode1, mode2);
+
+#else /* defined(CONFIG_OTG_MAX3353_MX2ADS) */
+
+        max3353_private.vendor = i2c_readw(MAX3353_VENDOR_ID);
+        max3353_private.product = i2c_readw(MAX3353_PRODUCT_ID);
+        max3353_private.revision = i2c_readw(MAX3353_VERSION_ID);
+        mode1 = i2c_readb(MAX3353_MODE_CONTROL_1_SET);
+        mode2 = i2c_readb(MAX3353_MODE_CONTROL_2_SET);
+
+        TRACE_MSG5(TCD, "2. OTG Transceiver: vendor: %04x product: %04x revision: %04x mode1: %02x mode2: %02x",
+                        max3353_private.vendor, max3353_private.product, max3353_private.revision, mode1, mode2);
+
+#endif /* defined(CONFIG_OTG_MAX3353_MX2ADS) */
+
+        for ( ; map && map->transceiver_type != unknown_transceiver; map++) {
+                CONTINUE_UNLESS ((max3353_private.vendor == map->vendor) && (max3353_private.product == map->product));
+                TRACE_MSG1(TCD, "Found Transceiver: %s", map->name);
+                max3353_private.transceiver_map = map;
+                break;
+        }
+        UNLESS (max3353_private.transceiver_map)
+                max3353_private.transceiver_map = max3353_transceiver_map;
+
+        TRACE_MSG0(TCD, "3. Disable All Transceiver Control Register 1 ");
+        i2c_writeb(MAX3353_OTG_CONTROL_CLR, 0xff);                                    // clear
+
+        TRACE_MSG0(TCD, "4. Enable D-/D+ Pulldowns in Transceiver Control Register 1 ");
+
+        i2c_writeb(MAX3353_OTG_CONTROL_SET, MAX3353_DM_PULLDOWN | MAX3353_DP_PULLDOWN);
+        i2c_writeb(MAX3353_OTG_CONTROL_CLR, (u8) MAX3353_DM_PULLUP | MAX3353_DP_PULLUP);
+
+
+        TRACE_MSG0(TCD, "5. Clear latch and enable interrupts");
+        i2c_writeb(MAX3353_INTERRUPT_LATCH_CLR, 0xff);
+        i2c_writeb(MAX3353_INTERRUPT_ENABLE_LOW_CLR, 0xeb);
+        i2c_writeb(MAX3353_INTERRUPT_ENABLE_HIGH_CLR, 0xeb);
+
+        /* The PSW_OE enables the ADR/PSW pin for output driving either high
+         * or low depending on the address.
+         *
+         * ADR          ADR_REG Address         PSW_OE=0        PSW_OE=1
+         *
+         *      low     0       0x2c            LOW             HIGH
+         *
+         *      high    1       0x2d            HIGH            LOW
+         *
+         *
+         * The i2c address by tying the ADR/PSW pin either high or low.
+         * Setting the PSW_OE drives the ADR/PSW into the opposite of the
+         * default wiring.
+         *
+         * On the MX21ADS the ADR/PSW pin is wired high which enables the
+         * charge pump. So enabling the PSW_OE is required to disable Vbus
+         * generation on the Charge Pump.
+         *
+         * The MAX3355E will only enable Vbus when this signal is high AND
+         * the ID signal is low. So it may be safe to leave enabled when
+         * operating as a B-device (ID floating.)
+         */
+
+        //i2c_writeb(MAX3353_MODE_CONTROL_2_SET, MAX3353_PSW_OE);              // PSW_OE - OFFVBUS low
+
+
+        TRACE_MSG1(TCD, "6. tx_mode: %02x", tx_mode);
+        switch (tx_mode) {
+        case vp_vm_bidirectional:
+                break;
+        case dat_se0_unidirectional:
+                i2c_writeb(MAX3353_MODE_CONTROL_2_CLR, MAX3353_BI_DI);
+        MAX3353t_se0_bidirectional:
+                i2c_writeb(MAX3353_MODE_CONTROL_1_SET, MAX3353_DAT_SE0);
+                break;
+        }
+        TRACE_MSG0(TCD, "6. tx_mode done");
+
+        TRACE_MSG1(TCD, "7. spd_ctrl: %02x", spd_ctrl);
+        switch (spd_ctrl) {
+        case spd_susp_pins:
+                i2c_writeb(MAX3353_MODE_CONTROL_2_CLR, MAX3353_SPD_SUSP_CTRL);
+                break;
+        case spd_susp_reg:
+                i2c_writeb(MAX3353_MODE_CONTROL_2_SET, MAX3353_SPD_SUSP_CTRL);
+                break;
+        }
+
+        //i2c_writeb(MAX3353_MODE_CONTROL_2_SET, MAX3353_PSW_OE);              // PSW_OE - OFFVBUS low
+
+        TRACE_MSG0(TCD, "7. spd_ctrl done");
+
+        TRACE_MSG1(TCD, "8. transceiver_type: %02x", max3353_private.transceiver_map);
+
+        TRACE_MSG1(TCD, "8. transceiver_type: %02x", max3353_private.transceiver_map->transceiver_type);
+        switch (max3353_private.transceiver_map->transceiver_type) {
+        case max3353:
+                break;
+        case max3301e:
+                i2c_writeb(MAX3301E_SPECIAL_FUNCTION_1_SET, MAX3301E_SESS_END);
+                break;
+        case unknown_transceiver:
+                break;
+        }
+        TRACE_MSG0(TCD, "8. transceiver_type done");
+}
+
diff -uNr linux/drivers/no-otg/ocd/max3353e/max3353.h linux/drivers/otg/ocd/max3353e/max3353.h
--- linux/drivers/no-otg/ocd/max3353e/max3353.h	1970-01-01 01:00:00.000000000 +0100
+++ linux/drivers/otg/ocd/max3353e/max3353.h	2006-09-01 21:41:32.000000000 +0200
@@ -0,0 +1,132 @@
+/*
+ * otg/ocd/max3353/max3353.h -- USB Transceiver Controller driver
+ * @(#) balden@belcarra.com|otg/ocd/max3353e/max3353.h|20060831021117|56821
+ *
+ *      Copyright (c) 2004-2005 Belcarra
+ *	Copyright (c) 2005-2006 Belcarra Technologies 2005 Corp
+ *
+ * By:
+ *      Stuart Lynne <sl@lbelcarra.com>,
+ *      Bruce Balden <balden@belcarra.com>
+ *
+ */
+
+
+/*!
+ * @file otg/ocd/max3353e/max3353.h
+ * @brief Private structures and defines for MAX3353 Transciever Controller Driver.
+ *
+ * This file contains the private defines and structures for the MAX3353 Transceiver
+ * Driver.
+ *
+ * @ingroup MAX3353TCD
+ */
+
+
+#ifdef CONFIG_XXXXX
+#define MAX3353_GPIO     (2)
+#define TCD_MAX3353_I2C_ADDR  MAX3353_I2C_ADDR_HIGH;
+#endif
+
+#define MAX3353_NAME    "max3353"
+
+#define MAX3353_LOC_CONN 0x01
+
+#define MAX3353_INT_SRC         0xeb
+
+//#define MAX3353_ALL_INT_SRC     0xff
+//#define MAX3353_NO_SE1_INT    0xeb
+//#define MAX3353_NO_SE1_INT    0xfb              /* do not pay attention to DP_HI */
+
+typedef enum otg_transceiver {
+        unknown_transceiver,
+        max3353,
+        max3301e
+} otg_transceiver_t;
+
+struct otg_transceiver_map {
+        otg_transceiver_t transceiver_type;
+        u16 vendor;
+        u16 product;
+        u16 revision;
+        char *name;
+};
+
+struct max3353_private {
+        WORK_STRUCT bh;
+        u16 vendor;
+        u16 product;
+        u16 revision;
+        u32 flags;
+        u8  int_src;
+        struct otg_transceiver_map *transceiver_map;
+};
+
+/*! tx_mode
+ * This defines the various ways that the MAX3353 can be
+ * wired into the host USB.
+ */
+typedef enum tx_mode {
+        dat_se0_bidirectional,          /*!< 3-Wire */
+        vp_vm_bidirectional,            /*!< 4-Wire */
+        dat_se0_unidirectional          /*!< 6-Wire */
+} tx_mode_t;
+
+/*! spd_ctrl
+ * This defines how the speed and suspend pins are controlled
+ * for this host.
+ */
+typedef enum spd_ctrl {
+        spd_susp_pins,                  /*!< controlled by SPEED and SUSPEND pins */
+        spd_susp_reg,                   /*!< controled by SPEED_REG and SUSPEND_REG in Mode Control 1 register */
+} spd_ctrl_t;
+
+
+extern struct tcd_ops tcd_ops;
+extern struct max3353_private max3353_private;
+extern struct tcd_instance *tcd_instance;
+
+/* max3353.c */
+extern int max3353_mod_init(void);
+extern void max3353_mod_exit(void);
+extern void max3353_chrg_vbus(struct otg_instance *, u8 );
+extern void max3353_drv_vbus(struct otg_instance *, u8 );
+extern void max3353_dischrg_vbus(struct otg_instance *, u8 );
+extern void max3353_dp_pullup_func(struct otg_instance *, u8 );
+extern void max3353_dm_pullup_func(struct otg_instance *, u8 );
+extern void max3353_dm_det_func(struct otg_instance *, u8 );
+extern void max3353_dp_det_func(struct otg_instance *, u8 );
+extern void max3353_bdis_acon_func(struct otg_instance *otg, u8 flag);
+extern void max3353_id_pulldown_func(struct otg_instance *otg, u8 flag);
+extern void max3353_audio_func(struct otg_instance *otg, u8 flag);
+extern void max3353_uart_func(struct otg_instance *otg, u8 flag);
+extern void max3353_mono_func(struct otg_instance *otg, u8 flag);
+
+extern void max3353_otg_timer(struct otg_instance *, u8 );
+extern void max3353_bh(void *);
+extern int max3353_id (struct otg_instance *);
+extern int max3353_vbus (struct otg_instance *);
+extern void  max3353_configure(tx_mode_t, spd_ctrl_t );
+
+/* thread */
+extern int max3353_thread_init (void (bh_proc)(void *));
+extern void max3353_thread_exit (wait_queue_head_t *);
+extern void max3353_thread_wakeup(int enabled, int first);
+
+
+
+/* i2c-xxx.c */
+int  i2c_configure(char *name, int addr);
+extern void  i2c_close(void);
+extern u8 i2c_readb(u8 );
+extern u16 i2c_readw(u8 );
+extern u32 i2c_readl(u8 );
+extern int  i2c_writeb(u8 , u8 );
+
+
+/* ********************************************************************************************* */
+#define TRACE_I2CB(t,r) TRACE_MSG3(t, "%-40s[%02x] %02x", #r, r, i2c_readb(r))
+#define TRACE_I2CW(t,r) TRACE_MSG3(t, "%-40s[%02x] %04x", #r, r, i2c_readw(r))
+#define TRACE_GPIO(t,b,r) TRACE_MSG2(t, "%-40s %04x", #r, readw(b + r))
+#define TRACE_REGL(t,r) TRACE_MSG2(t, "%-40s %08x", #r, readl(r))
+
diff -uNr linux/drivers/no-otg/ocd/max3353e/max3353e-procfs.c linux/drivers/otg/ocd/max3353e/max3353e-procfs.c
--- linux/drivers/no-otg/ocd/max3353e/max3353e-procfs.c	1970-01-01 01:00:00.000000000 +0100
+++ linux/drivers/otg/ocd/max3353e/max3353e-procfs.c	2006-09-01 21:41:32.000000000 +0200
@@ -0,0 +1,303 @@
+/*
+ * otg/ocd/max3353/max3353-procfs.c - USB Device Core Layer
+ * @(#) balden@belcarra.com|otg/ocd/max3353e/max3353e-procfs.c|20060831021117|57336
+ *
+ *      Copyright (c) 2004-2005 Belcarra
+ *	Copyright (c) 2005-2006 Belcarra Technologies 2005 Corp
+ *
+ * By:
+ *      Stuart Lynne <sl@belcarra.com>,
+ *      Shahrad Payandeh <sp@belcarra.com>,
+ *      Bruce Balden <balden@belcarra.com>
+ *
+ *
+ */
+/*!
+ * @file otg/ocd/max3353e/max3353e-procfs.c
+ * @brief Implement /proc/max3353 to dump MAX3353 registers.
+ *
+ *
+ * @ingroup MAX3353TCD
+ */
+
+
+#include <otg/pcd-include.h>
+#include "otghw/max3353-hardware.h"
+#include "max3353.h"
+
+#ifdef CONFIG_ARCH_MX2ADS
+#include <asm/arch/mx2.h>
+#define MX2_OTG_XCVR_DEVAD                      0x18
+#define MX2_SEQ_OP_REG                          0x19
+#define MX2_SEQ_RD_STARTAD                      0x1a
+#define MX2_I2C_OP_CTRL_REG                     0x1b
+#define MX2_SCLK_TO_SCL_HPER                    0x1e
+#define MX2_I2C_INTERRUPT_AND_CTRL              0x1f
+
+#define OTG_BASE_ADDR                           0x10024000
+//#define OTG_I2C_BASE                            (OTG_BASE_ADDR+0x100)
+
+#endif /* CONFIG_ARCH_MX2ADS */
+
+#ifdef CONFIG_OTG_MAX3353_PROCFS
+/* Proc Filesystem *************************************************************************** */
+
+extern struct max3353_private max3353_private;
+
+#define MAX_HISTORY     6
+struct reg_list {
+        u8 reg;
+        u8 size;
+        char *name;
+        u32 values[MAX_HISTORY];
+};
+
+#define REG(r, s)  {r, s, #r, }
+
+struct reg_list max3353_prod_list[] = {
+        REG(MAX3353_VENDOR_ID, 2),
+        REG(MAX3353_PRODUCT_ID, 2),
+        REG(MAX3353_VERSION_ID, 2),
+        { 0, 1, NULL,},
+};
+struct reg_list max3353_reg_list[] = {
+        REG(MAX3353_OTG_CONTROL_SET, 1),
+        REG(MAX3353_INTERRUPT_SOURCE, 1),
+        REG(MAX3353_INTERRUPT_LATCH_SET, 1),
+        REG(MAX3353_INTERRUPT_ENABLE_LOW_SET, 1),
+        REG(MAX3353_INTERRUPT_ENABLE_HIGH_SET, 1),
+        REG(MAX3353_MODE_CONTROL_1_SET, 1),
+        { 0, 1, NULL,},
+};
+struct reg_list max3353_spec_list[] = {
+        REG(MAX3353_MODE_CONTROL_2_SET, 1),
+        REG(MAX3353_OTG_STATUS, 1),
+        { 0, 1, NULL,},
+};
+struct reg_list max3301e_spec_list[] = {
+        REG(MAX3301E_SPECIAL_FUNCTION_1_SET, 1),
+        REG(MAX3301E_SPECIAL_FUNCTION_2_SET, 1),
+        { 0, 1, NULL,},
+};
+#ifdef CONFIG_ARCH_MX2ADS
+struct reg_list mx21_spec_list[] = {
+        REG(MX2_OTG_XCVR_DEVAD, 1),
+        REG(MX2_SEQ_OP_REG, 1),
+        REG(MX2_SEQ_RD_STARTAD, 1),
+        REG(MX2_I2C_OP_CTRL_REG, 1),
+        REG(MX2_SCLK_TO_SCL_HPER, 1),
+        REG(MX2_I2C_INTERRUPT_AND_CTRL, 1),
+        { 0, 1, NULL,},
+};
+#endif /* CONFIG_ARCH_MX2ADS */
+
+void max3353_update(struct reg_list *list)
+{
+        for (; list && list->name; list++) {
+                memmove(list->values + 1, list->values, sizeof(list->values) - sizeof(u32));
+                switch(list->size) {
+                case 1:
+                        list->values[0] = i2c_readb(list->reg);
+                        break;
+                case 2:
+                        list->values[0] = i2c_readw(list->reg);
+                        break;
+                case 4:
+                        list->values[0] = i2c_readl(list->reg);
+                        break;
+                }
+        }
+}
+
+#ifdef CONFIG_ARCH_MX2ADS
+static u8 __inline__ mx2_rb(u32 port)
+{
+        return *(volatile u8 *) (MX2_IO_ADDRESS(port + OTG_I2C_BASE));
+}
+
+
+void mx21_update(struct reg_list *list)
+{
+        for (; list && list->name; list++) {
+                memmove(list->values + 1, list->values, sizeof(list->values) - sizeof(u32));
+                list->values[0] = mx2_rb(list->reg);
+        }
+}
+
+#endif /* CONFIG_ARCH_MX2ADS */
+
+void max3353_update_all(void)
+{
+        max3353_update(max3353_reg_list);
+        switch (max3353_private.transceiver_map->transceiver_type) {
+        case max3353:
+                max3353_update(max3353_spec_list);
+                break;
+        case max3301e:
+                max3353_update(max3301e_spec_list);
+                break;
+        default:
+                break;
+        }
+#ifdef CONFIG_ARCH_MX2ADS
+        mx21_update(mx21_spec_list);
+#endif /* CONFIG_ARCH_MX2ADS */
+}
+
+
+
+/*
+ * dohex
+ *
+ */
+static void dohexdigit (char *cp, unsigned char val)
+{
+        if (val < 0xa)
+                *cp = val + '0';
+        else if ((val >= 0x0a) && (val <= 0x0f))
+                *cp = val - 0x0a + 'a';
+}
+
+/*
+ * dohex
+ *
+ */
+static void dohexval (char *cp, unsigned char val)
+{
+        dohexdigit (cp++, val >> 4);
+        dohexdigit (cp++, val & 0xf);
+}
+
+int max3353_dump(char *buf, char *name, char *fmt, struct reg_list *reg)
+{
+        int len = 0, i;
+        len += sprintf (buf + len, "%-20s  %-34s [%03x]:  ", name, reg->name, reg->reg);
+        len += sprintf (buf + len, fmt, reg->values[0]);
+        for (i = 1; i < MAX_HISTORY; i++)
+                if (reg->values[i - 1] == reg->values[i])
+                        len += sprintf (buf + len, "         ");
+                else
+                        len += sprintf (buf + len, fmt, reg->values[i]);
+        len += sprintf (buf + len, "\n");
+        return len;
+}
+
+int max3353_dump_list(char * buf, char *name, struct reg_list *list)
+{
+        int len = 0;
+        for (; list && list->name; list++)
+                switch(list->size) {
+                case 1: len += max3353_dump(buf + len, name, "       %02x", list); break;
+                case 2: len += max3353_dump(buf + len, name, "     %04x", list); break;
+                case 4: len += max3353_dump(buf + len, name, " %08x", list); break;
+                }
+        return len;
+}
+
+int max3353_dump_all(char *buf)
+{
+        int len = 0;
+        len += max3353_dump_list(buf + len, "MAX3353 Standard", max3353_reg_list);
+        switch (max3353_private.transceiver_map->transceiver_type) {
+        case max3353:
+                len += max3353_dump_list(buf + len, "MAX3353 Extra", max3353_spec_list);
+                break;
+        case max3301e:
+                len += max3353_dump_list(buf + len, "MAX3301E Extra", max3301e_spec_list);
+                break;
+        default:
+                break;
+        }
+#ifdef CONFIG_ARCH_MX2ADS
+        len += max3353_dump_list(buf + len, "MX21 ADS Extra", mx21_spec_list);
+#endif /* CONFIG_ARCH_MX2ADS */
+        return len;
+}
+
+
+
+/*!
+ * max3353_device_proc_read - implement proc file system read.
+ *
+ * Standard proc file system read function.
+ *
+ * We let upper layers iterate for us, *pos will indicate which device to return
+ * statistics for.
+ */
+static ssize_t max3353_device_proc_read_functions (struct file *file, char *buf, size_t count, loff_t * pos)
+{
+        unsigned long page;
+        int len = 0;
+        int index;
+        int i;
+        u32 r;
+
+        int config_size;
+
+        // get a page, max 4095 bytes of data...
+        RETURN_EINVAL_UNLESS ((page = get_free_page (GFP_KERNEL)));
+
+        len = 0;
+        index = (*pos)++;
+
+        switch(index) {
+        case 0:
+                len += sprintf ((char *) page + len, "MAX3353 Transceiver Registers\n");
+
+                max3353_update_all();
+                len += sprintf ((char *) page + len , "Vendor: %04x Product: %04x Revision: %04x %s\n",
+                                max3353_private.vendor, max3353_private.product,
+                                max3353_private.revision, max3353_private.transceiver_map->name
+                                );
+
+                len += max3353_dump_all((char *) page + len);
+                len += sprintf ((char *) page + len, "\n");
+
+                break;
+
+        case 1:
+                break;
+
+        }
+
+        if (len > count) {
+                len = -EINVAL;
+        }
+        else if ((len > 0) && copy_to_user (buf, (char *) page, len)) {
+                len = -EFAULT;
+        }
+        else {
+        }
+        free_page (page);
+        return len;
+}
+
+static struct file_operations max3353_device_proc_operations_functions = {
+        read:max3353_device_proc_read_functions,
+};
+
+/* Module init ******************************************************************************* */
+
+static int max3353_procfs_init (void)
+{
+        struct proc_dir_entry *p;
+
+        // create proc filesystem entries
+        if ((p = create_proc_entry ("max3353", 0, 0)) == NULL)
+                return -ENOMEM;
+
+        p->proc_fops = &max3353_device_proc_operations_functions;
+
+        max3353_update_all();
+
+        return 0;
+}
+static void max3353_procfs_exit (void)
+{
+        remove_proc_entry ("max3353", NULL);
+}
+#else
+static int max3353_procfs_init (void) { return 0;
+}
+static void max3353_procfs_exit (void) { }
+#endif
diff -uNr linux/drivers/no-otg/ocd/max3353e/max3353e.c linux/drivers/otg/ocd/max3353e/max3353e.c
--- linux/drivers/no-otg/ocd/max3353e/max3353e.c	1970-01-01 01:00:00.000000000 +0100
+++ linux/drivers/otg/ocd/max3353e/max3353e.c	2006-09-01 21:41:32.000000000 +0200
@@ -0,0 +1,657 @@
+/*
+ * otg/ocd/max3353/max3353.c -- USB Transceiver Controller driver
+ * @(#) balden@belcarra.com|otg/ocd/max3353e/max3353e.c|20060831021117|08740
+ *
+ *      Copyright (c) 2004-2005 Belcarra
+ *	Copyright (c) 2005-2006 Belcarra Technologies 2005 Corp
+ *
+ * By:
+ *      Stuart Lynne <sl@lbelcarra.com>,
+ *      Shahrad Payandeh <sp@belcarra.com>,
+ *      Bruce Balden <balden@belcarra.com>
+ *
+ */
+/*!
+ * @file otg/ocd/max3353e/max3353e.c
+ * @brief MAX3353 OTG Transceiver Driver.
+ *
+ * This is the generic MAX3353 TCD core support.
+ *
+ * Notes:
+ *
+ * 1. The MAX3353 can control the speed and suspend directly, would it be
+ * appropriate to allow state machine to control this.
+ *
+ * 2. The MAX3353 has auto connect feature, can this be used without change
+ * to state machine.
+ *
+ * 3. The MAX3353 can control the ADR/PSW pin to enable / disable external
+ * charge pump.
+ *
+ * @ingroup MAX3353TCD
+ */
+
+#include <otg/otg-compat.h>
+
+#include <asm/irq.h>
+#include <asm/system.h>
+#include <asm/io.h>
+#include <asm/au1000.h>
+#include <linux/i2c.h>
+
+#include <otg/usbp-chap9.h>
+#include <otg/usbp-func.h>
+#include <otg/usbp-bus.h>
+#include <otg/otg-trace.h>
+#include <otg/otg-api.h>
+#include <otg/otg-hcd.h>
+#include <otg/otg-tcd.h>
+#include <otg/otg-ocd.h>
+#include <otg/otg-pcd.h>
+
+#include <otghw/max3353e-hardware.h>
+#include "max3353e.h"
+
+
+struct otg_transceiver_map max3353_transceiver_map[] = {
+        { max3353, 0x4cc, 0x1301, 0x00, "Phillips MAX3353", },
+        { max3301e, 0x6a0b, 0x0133, 0x00, "Maxim MAX3301E", },
+        { 0, 0, 0, 0, "Unknown Transceiver", },
+};
+
+struct max3353_private max3353_private;
+
+
+
+/* ********************************************************************************************* */
+#define TRACE_I2CB(t,r) TRACE_MSG3(t, "%-40s[%02x] %02x", #r, r, i2c_readb(r))
+#define TRACE_I2CW(t,r) TRACE_MSG3(t, "%-40s[%02x] %04x", #r, r, i2c_readw(r))
+#define TRACE_GPIO(t,b,r) TRACE_MSG2(t, "%-40s %04x", #r, readw(b + r))
+#define TRACE_REGL(t,r) TRACE_MSG2(t, "%-40s %08x", #r, readl(r))
+
+
+
+/*! max3353_int_src - update interrupt source test mask
+ *
+ * This sets the current mask and updates the interrupt registers to match.
+ */
+void max3353_int_src(u8 int_src)
+{
+        TRACE_MSG1(TCD, "setting int_src %02x", int_src);
+        max3353_private.int_src = int_src;
+        i2c_writeb(MAX3353E_INTERRUPT_MASK, int_src);
+        i2c_writeb(MAX3353E_INTERRUPT_EDGE, 3); //detect on positive edge
+        i2c_writeb(MAX3353E_INTERRUPT_LATCH, 0);
+}
+
+/*! max3353_int_src_set - add to the interrupt source mask
+ *
+ */
+void max3353_int_src_set(u8 int_src)
+{
+        max3353_int_src(int_src |= max3353_private.int_src);
+}
+
+/*! max3353_int_src_clr - remove from the interrup source mask
+ */
+void max3353_int_src_clr(u8 int_src)
+{
+        max3353_int_src(int_src = max3353_private.int_src & ~int_src);
+}
+
+/* ********************************************************************************************* */
+/* ********************************************************************************************* */
+
+
+
+typedef struct max3353_test {
+        u32     input;
+        char   *name;
+        u8      mask;
+} max3353_test_t;
+
+#define OV(i, m) {i, #i, m}
+#define MAX3353E_B_SESS_END 1<<4
+#define MAX3353E_B_SESS_VLD 1<<6
+#define MAX3353E_A_SESS_VLD 1<<5
+
+
+struct max3353_test max3353_int_src_tests[5] = {
+        OV(VBUS_VLD, MAX3353E_VBUS_VALID),
+        OV(ID_GND, MAX3353E_ID_GND),
+        OV(ID_FLOAT, MAX3353E_ID_FLOAT),
+        OV(BDIS_ACON, MAX3353E_A_HNP),
+        {0, NULL, 0},
+};
+
+struct max3353_test max3353_otg_status_tests[4] = {
+        OV(B_SESS_END, MAX3353E_B_SESS_END),
+        OV(B_SESS_VLD, MAX3353E_B_SESS_VLD),
+        OV(A_SESS_VLD, MAX3353E_A_SESS_VLD),
+        {0, NULL, 0},
+};
+
+u64 max3353_event(struct otg_instance *otg, struct max3353_test *tests, int val, char *msg)
+{
+        int i;
+        u64 inputs = 0;
+        TRACE_MSG2(TCD, "%02x %s", val, msg);
+        for (i = 0; tests[i].mask; i++) {
+                u64 input = tests[i].input;
+                u8 mask = tests[i].mask;
+                inputs |= (val & mask) ? input : _NOT(input);
+                TRACE_MSG5(TCD, "%d - %s%s %02x %08x", i, tests[i].name, (val & mask) ? "" : "/", tests[i].mask, tests[i].input);
+        }
+        return inputs;
+}
+
+/*! max3353_event_set_irq
+ */
+void max3353_event_set_irq(struct otg_instance *otg, int changed, int flag, u32 input, otg_tag_t tag, char *mset, char *mreset)
+{
+        TRACE_MSG5(TCD, "changed: %x flag: %x input: %08x %s %s", changed, flag, input,
+                        flag ? mset: mreset,
+                        changed ? "": "(ignored)");
+
+        otg_event_set_irq(otg, changed, flag, input, tag, flag ? mset : mreset);
+}
+
+/*! max3353_update - called from mx2_i2c_xcvr_bh to find changes in the max3353 Interrupt Source Register
+ */
+u8 max3353_update_int_src(struct otg_instance *otg, u8 int_src, u8 changed)
+{
+        UNLESS (changed) return int_src;
+        TRACE_MSG2(TCD, "CHANGED INTERRUPT_SOURCE: %02x (%02x) ", int_src, changed);
+
+        /* Core events
+         */
+        max3353_event_set_irq(otg, MAX3353E_ID_GND & changed, MAX3353E_ID_GND & int_src,
+                        ID_GND, TCD, "ID_GND", "ID_GND/");
+        max3353_event_set_irq(otg, MAX3353E_ID_FLOAT & changed, MAX3353E_ID_FLOAT & int_src,
+                        ID_FLOAT, TCD, "ID_FLOAT", "ID_FLOAT/");
+        max3353_event_set_irq(otg, MAX3353E_VBUS_VALID & changed, MAX3353E_VBUS_VALID & int_src,
+                        VBUS_VLD, TCD, "VBUS_VLD", "VBUS_VLD/");
+        max3353_event_set_irq(otg, MAX3353E_SESSION_END & changed, MAX3353E_SESSION_END & int_src,
+                        A_SESS_VLD, TCD, "A_SESS_VLD", "A_SESS_VLD/");
+
+
+        return int_src;
+}
+
+/*! max3353_update - called from mx2_i2c_xcvr_bh to find changes in the max3353 OTG Status Register
+ */
+u8 max3353_update_otg_status(struct otg_instance *otg, u8 otg_status, u8 changed)
+{
+        UNLESS (changed) return otg_status;
+        TRACE_MSG2(TCD, "CHANGED OTG_STATUS_REG: %02x (%02x) ", otg_status, changed);
+
+        max3353_event_set_irq(otg, MAX3353E_SESSION_VALID_EN & changed, MAX3353E_SESSION_VALID_EN & otg_status,
+                        B_SESS_VLD, TCD, "B_SESS_VLD", "B_SESS_VLD/");
+        max3353_event_set_irq(otg, MAX3353E_SESSION_END & changed, MAX3353E_SESSION_END & otg_status,
+                        B_SESS_END, TCD, "B_SESS_END", "B_SESS_END/");
+
+        return otg_status;
+}
+
+
+int max3353_bh_first;
+
+/*! max3353_bh
+ */
+void max3353_bh(void *arg)
+{
+
+        u64 inputs = 0;
+        u8 int_lat,status_reg,otg_reg;
+        u8 reg1,reg2;
+
+        au_writel(0x40000000, IC1_MASKCLR);     //disable interrupt
+        au_writel(0x40000000, IC1_FALLINGCLR);
+        au_writel(0x40000000, IC1_RISINGCLR);
+        TRACE_MSG0(TCD, "--MAX3353 ISR start");
+//        printk(KERN_INFO"ISR Start .......\n");
+
+        /* read the interrupt latch and and clear latch and update otg
+         */
+        if ((int_lat = i2c_readb(MAX3353E_INTERRUPT_LATCH))) {
+                TRACE_MSG1(TCD, "INT LAT: %02x", int_lat);
+                i2c_writeb(MAX3353E_INTERRUPT_LATCH, 0);
+        }
+
+        status_reg = i2c_readb(MAX3353E_STATUS_REGISTER);
+        inputs |= max3353_event(tcd_instance->otg, max3353_int_src_tests, status_reg, "MAX3353 STAT SRC");
+        TRACE_MSG1(TCD, "inputs after applying status register %02x\n", inputs);
+
+
+        otg_reg = 0;
+
+        if (status_reg & MAX3353E_SESSION_VALID_EN) {   //1.4,,4.5
+                TRACE_MSG0(TCD, "Setting B_SESS_VLD");
+                otg_reg |= MAX3353E_B_SESS_VLD;
+        }
+        if (status_reg & MAX3353E_SESSION_END) {
+                TRACE_MSG0(TCD, "Setting B_SESS_END");
+                otg_reg |= MAX3353E_B_SESS_END;
+        }
+        if (!(status_reg & MAX3353E_SESSION_END)) {     //0.8<<1.4
+                if (!(status_reg & MAX3353E_SESSION_VALID_EN)){
+                        TRACE_MSG0(TCD, "Setting A_SESS_VLD");
+                        otg_reg |= MAX3353E_A_SESS_VLD;
+                }
+        }
+
+
+
+        inputs |= max3353_event(tcd_instance->otg, max3353_otg_status_tests, otg_reg, "MAX3353 OTG SRC");
+        TRACE_MSG1(TCD, "inputs after applying otg register %02x\n", inputs);
+
+
+
+        TRACE_MSG0(TCD, "--MAX3353 ISR end");
+        if (inputs) otg_event(tcd_instance->otg, inputs, TCD, "MAX3353");
+        au_writel(0x40000000, IC1_MASKSET);     //enable interrupt
+}
+
+
+
+/* ********************************************************************************************** */
+/*! max3353_vbus - Do we have Vbus (cable attached?)
+ * Return non-zero if Vbus is detected.
+ *
+ */
+int max3353_vbus (struct otg_instance *otg)
+{
+        return 0;
+}
+
+/*! max3353_id - Do we have Vbus (cable attached?)
+ * Return non-zero if Vbus is detected.
+ *
+ */
+int max3353_idvbus (struct otg_instance *otg)
+{
+        return 0;
+}
+
+
+/* ********************************************************************************************* */
+
+/*! max3353_chrg_vbus - used to enable or disable B-device Vbus charging
+ */
+void max3353_chrg_vbus(struct otg_instance *otg, u8 flag)
+{
+        u8 reg2;
+
+        TRACE_MSG0(TCD, "--");
+        reg2 = i2c_readb(MAX3353E_CONTROL_REGISTER_2);
+        TRACE_MSG1(TCD, "Control 2 - Begin = %02x", reg2);
+        switch (flag) {
+        case SET:
+                TRACE_MSG0(TCD, "CHRG_VBUS_SET");
+                reg2 |= MAX3353E_VBUS_CHG2;
+                i2c_writeb(MAX3353E_CONTROL_REGISTER_2, (u8) reg2);
+                break;
+        case RESET:
+                TRACE_MSG0(TCD, "CHRG_VBUS_RESET");
+                reg2 &= (~MAX3353E_VBUS_CHG2);
+                i2c_writeb(MAX3353E_CONTROL_REGISTER_2, (u8) reg2);
+                break;
+        case PULSE:
+                break;
+        }
+        TRACE_MSG1(TCD, "Control 2 - End = %02x", reg2);
+}
+
+/*! max3353_drv_vbus - used to enable or disable A-device driving Vbus
+ */
+void max3353_drv_vbus(struct otg_instance *otg, u8 flag)
+{
+u8 reg2;
+
+        TRACE_MSG0(TCD, "--");
+        reg2 = i2c_readb(MAX3353E_CONTROL_REGISTER_2);
+        TRACE_MSG1(TCD, "Control 2 - Begin = %02x", reg2);
+        switch (flag) {
+        case SET:
+                TRACE_MSG0(TCD, "DRV_VBUS_SET");
+                reg2 |= MAX3353E_VBUS_DRV;
+                i2c_writeb(MAX3353E_CONTROL_REGISTER_2, (u8) reg2);
+                break;
+        case RESET:
+                TRACE_MSG0(TCD, "DRV_VBUS_RESET");
+                reg2 &= (~MAX3353E_VBUS_DRV);
+                i2c_writeb(MAX3353E_CONTROL_REGISTER_2, (u8) reg2);
+                break;
+        case PULSE:
+                break;
+        }
+        TRACE_MSG1(TCD, "Control 2 - End = %02x", reg2);
+}
+
+/*! max3353_dischrg_vbus - used to enable Vbus discharge
+ */
+void max3353_dischrg_vbus(struct otg_instance *otg, u8 flag)
+{
+        u8 reg2;
+
+        TRACE_MSG0(TCD, "--");
+        reg2 = i2c_readb(MAX3353E_CONTROL_REGISTER_2);
+        TRACE_MSG1(TCD, "Control 2 - Begin = %02x", reg2);
+        switch (flag) {
+        case SET:
+                TRACE_MSG0(TCD, "DISCHG_VBUS_SET");
+                reg2 |= MAX3353E_VBUS_DISCHG;
+                i2c_writeb(MAX3353E_CONTROL_REGISTER_2, (u8) reg2);
+                break;
+        case RESET:
+                TRACE_MSG0(TCD, "DISCHG_VBUS_RESET");
+                reg2 &= (~MAX3353E_VBUS_DISCHG);
+                i2c_writeb(MAX3353E_CONTROL_REGISTER_2, (u8) reg2);
+                break;
+        case PULSE:
+                break;
+        }
+        TRACE_MSG1(TCD, "Control 2 - End = %02x", reg2);
+}
+
+/*! max3353_dp_pullup_func - used to enable or disable peripheral connecting to bus
+ *
+ * C.f. 5.1.6, 5.1.7, 5.2.4 and 5.2.5
+ *
+ *                              host    peripheral
+ *              d+ pull-up      clr     set
+ *              d+ pull-down    set     clr
+ *
+ *              d- pull-up      clr     clr
+ *              d- pull-down    set     set
+ *
+ */
+void max3353_dp_pullup_func(struct otg_instance *otg, u8 flag)
+{
+        struct tcd_instance *tcd = (struct tcd_instance *)otg->tcd;
+        TRACE_MSG0(TCD, "-- ");
+        switch (flag) {
+        case SET:
+		printk(KERN_INFO"%s: --\n", __FUNCTION__);
+                max3353_private.flags = i2c_readb(MAX3353E_CONTROL_REGISTER_1);
+                TRACE_MSG0(TCD, "Clr DP PULLDOWN");
+                max3353_private.flags &= (~MAX3353E_DP_PULLDOWN);
+                TRACE_MSG0(TCD, "Set DP PULLUP");
+                max3353_private.flags |= (MAX3353E_DP_PULLUP);
+                i2c_writeb(MAX3353E_CONTROL_REGISTER_1, (u8) max3353_private.flags);
+                break;
+
+        case RESET:
+                max3353_private.flags = i2c_readb(MAX3353E_CONTROL_REGISTER_1);
+                TRACE_MSG0(TCD, "Clr DP PULLUP");
+                max3353_private.flags &= (~MAX3353E_DP_PULLUP);
+                TRACE_MSG0(TCD, "Set DP PULLDOWN");
+                max3353_private.flags |= (MAX3353E_DP_PULLDOWN);
+                i2c_writeb(MAX3353E_CONTROL_REGISTER_1, (u8) max3353_private.flags);
+                break;
+
+        case PULSE:
+                TRACE_MSG0(TCD, "MAX3353_LOC_CONN PULSE");
+                break;
+        }
+        int temp = 0;
+        temp = i2c_readb(MAX3353E_CONTROL_REGISTER_1);
+        TRACE_MSG1(TCD, "Control Register 1: %02x", temp);
+
+}
+
+/*! max3353_dm_pullup_func - used to enable or disable peripheral connecting to bus
+ *
+ */
+void max3353_dm_pullup_func(struct otg_instance *otg, u8 flag)
+{
+        struct tcd_instance *tcd = (struct tcd_instance *)otg->tcd;
+        TRACE_MSG0(TCD, "--");
+        switch (flag) {
+        case SET:
+                max3353_private.flags = i2c_readb(MAX3353E_CONTROL_REGISTER_1);
+                TRACE_MSG0(TCD, "Clr DM PULLDOWN");
+                max3353_private.flags &= (~MAX3353E_DM_PULLDOWN);
+                i2c_writeb(MAX3353E_CONTROL_REGISTER_1, (u8) max3353_private.flags);
+                TRACE_MSG0(TCD, "Set DM PULLUP");
+                max3353_private.flags |= (MAX3353E_DM_PULLUP);
+                i2c_writeb(MAX3353E_CONTROL_REGISTER_1, (u8) max3353_private.flags);
+                break;
+
+        case RESET:
+                max3353_private.flags = i2c_readb(MAX3353E_CONTROL_REGISTER_1);
+                TRACE_MSG0(TCD, "Clr DM PULLUP");
+                max3353_private.flags &= (~MAX3353E_DM_PULLUP);
+                i2c_writeb(MAX3353E_CONTROL_REGISTER_1, (u8) max3353_private.flags);
+                TRACE_MSG0(TCD, "Set DM PULLDOWN");
+                max3353_private.flags |= (MAX3353E_DM_PULLDOWN);
+                i2c_writeb(MAX3353E_CONTROL_REGISTER_1, (u8) max3353_private.flags);
+                break;
+
+        case PULSE:
+                TRACE_MSG0(TCD, "MAX3353_LOC_CONN PULSE");
+                break;
+        }
+        int temp = 0;
+        temp = i2c_readb(MAX3353E_CONTROL_REGISTER_1);
+        TRACE_MSG1(TCD, "Control Register 1 %02x", temp);
+}
+
+/*! max3353_dm_det_func - used to enable or disable D- detect
+ *
+ */
+void max3353_dm_det_func(struct otg_instance *otg, u8 flag)
+{
+        return;
+}
+
+/*! max3353_dp_det_func - used to enable or disable D+ detect
+ *
+ */
+void max3353_dp_det_func(struct otg_instance *otg, u8 flag)
+{
+        return;
+}
+
+/*! max3353_bdis_acon_func - used to enable or disable auto a-connect
+ *
+ */
+void max3353_bdis_acon_func(struct otg_instance *otg, u8 flag)
+{
+        return;
+}
+
+/*! max3353_id_pulldown_func - used to enable or disable ID pulldown
+ *
+ */
+void max3353_id_pulldown_func(struct otg_instance *otg, u8 flag)
+{
+        return;
+}
+
+/*! max3353_audio_func - used to enable or disable Carkit Interrupt
+ *
+ */
+void max3353_audio_func(struct otg_instance *otg, u8 flag)
+{
+        return;
+}
+
+/*! max3353_uart_func - used to enable or disable transparent uart mode
+ *
+ */
+void max3353_uart_func(struct otg_instance *otg, u8 flag)
+{
+        return;
+}
+
+/*! max3353_mono_func - used to enable or disable mono audio connection
+ *
+ */
+void max3353_mono_func(struct otg_instance *otg, u8 flag)
+{
+        return;
+}
+
+
+/* ********************************************************************************************* */
+extern int max3353_procfs_init (void);
+extern void max3353_procfs_exit (void);
+
+/*! max3353_mod_init
+ */
+int max3353_mod_init(void)
+{
+        int ret;
+
+        TRACE_MSG0(TCD, "Initilize");
+        /* for interrupt bottom half handler
+         */
+        PREPARE_WORK_ITEM(max3353_private.bh, &max3353_bh, NULL);
+
+        /* setup max3353, enable interrupts, clear latch
+         * XXX when and how do we do DM_HI and DP_HI, when ID is present?
+         */
+        max3353_int_src_set(0x1f);
+        //max3353_int_src(MAX3353_NO_SE1_INT);
+        /*Turn on the max3353 and discharge the bus*/
+        i2c_writeb(MAX3353E_CONTROL_REGISTER_2, 0x0);
+        i2c_writeb(MAX3353E_CONTROL_REGISTER_1, 0x0);
+        int status = i2c_readb(MAX3353E_STATUS_REGISTER);
+        if ((status & 0x1) == 0){
+                i2c_writeb(MAX3353E_CONTROL_REGISTER_2, 0x10);
+        }
+        i2c_writeb(MAX3353E_CONTROL_REGISTER_2, 0x0);
+        i2c_writeb(MAX3353E_CONTROL_REGISTER_1, MAX3353E_DM_PULLDOWN | MAX3353E_DP_PULLDOWN);
+
+#ifdef CONFIG_OTG_MAX3353_PROCFS
+        TRACE_MSG0(TCD, "MAX3353 Procfs");
+        max3353_procfs_init ();
+#endif /* CONFIG_OTG_MAX3353_PROCFS */
+
+        au_sync();
+
+        //      au_writel(0x1, IC1_TESTBIT);    //disable test bit for low leve interrupt
+        //All this done when define type of interrupt in irqmap.c
+        /*au_writel(0x40000000, IC1_CFG0CLR); //low level activation
+                au_writel(0x40000000, IC1_CFG1SET);
+                au_writel(0x40000000, IC1_CFG2CLR);
+                au_writel(0x40000000, IC1_MASKSET);//enable interrupt
+         */
+        //au_writel(0x40000000, IC1_SRCCLR);//set test bit as a source
+        au_writel(0x40000000, IC1_SRCSET);//set source to original
+        //au_writel(0x0, IC1_TESTBIT);
+        //au_writel(0x1, IC1_TESTBIT);
+
+#if 0
+        ret = au_readl(IC0_CFG0RD);
+        printk (KERN_INFO"IC0 Reg 0 : %08x\n", ret);
+        ret = au_readl(IC0_CFG1RD);
+        printk (KERN_INFO"IC0 Reg 1 : %08x\n", ret);
+        ret = au_readl(IC0_CFG2RD);
+        printk (KERN_INFO"IC0 Reg 2 : %08x\n", ret);
+        ret = au_readl(IC0_SRCRD);
+        printk (KERN_INFO"IC0 Source : %08x\n", ret);
+        ret = au_readl(IC0_ASSIGNRD);
+        printk (KERN_INFO"IC0 Assign : %08x\n", ret);
+        ret = au_readl(IC0_WAKERD);
+        printk (KERN_INFO"IC0 Wakeup : %08x\n", ret);
+        ret = au_readl(IC0_MASKRD);
+        printk (KERN_INFO"IC0 Mask : %08x\n", ret);
+
+        ret = au_readl(IC1_CFG0RD);
+        printk (KERN_INFO"IC1 Reg 0 : %08x\n", ret);
+        ret = au_readl(IC1_CFG1RD);
+        printk (KERN_INFO"IC1 Reg 1 : %08x\n", ret);
+        ret = au_readl(IC1_CFG2RD);
+        printk (KERN_INFO"IC1 Reg 2 : %08x\n", ret);
+        ret = au_readl(IC1_SRCRD);
+        printk (KERN_INFO"IC1 Source : %08x\n", ret);
+        ret = au_readl(IC1_ASSIGNRD);
+        printk (KERN_INFO"IC1 Assign : %08x\n", ret);
+        ret = au_readl(IC1_WAKERD);
+        printk (KERN_INFO"IC1 Wakeup : %08x\n", ret);
+        ret = au_readl(IC1_MASKRD);
+        printk (KERN_INFO"IC1 Mask : %08x\n", ret);
+
+#endif
+
+        return 0;
+}
+
+/*! max3353_mod_exit
+ */
+void max3353_mod_exit(void)
+{
+        i2c_writeb(MAX3353E_INTERRUPT_MASK, 0x0);
+        //i2c_close();
+#ifdef CONFIG_OTG_MAX3353_PROCFS
+        max3353_procfs_exit ();
+#endif /* CONFIG_OTG_MAX3353_PROCFS */
+
+        while (PENDING_WORK_ITEM(max3353_private.bh)) {
+                printk(KERN_ERR"%s: waiting for bh\n", __FUNCTION__);
+                schedule_timeout(10 * HZ);
+        }
+
+}
+
+/* ********************************************************************************************* */
+/*! max3353_configure - configure the MAX3353 for this host
+ * @param tx_mode - the type of connection between the host USB and the MAX3353
+ * @param spd_ctrl - suspend control method
+ */
+void  max3353_configure(tx_mode_t tx_mode, spd_ctrl_t spd_ctrl)
+{
+        u16 vendor = 0, product = 0, revision = 0;
+        u8 mode1, mode2, control;
+
+        struct otg_transceiver_map *map = max3353_transceiver_map;
+        TRACE_MSG0(TCD, "Configure");
+
+        //#if defined(CONFIG_OTG_MAX3353_MX2ADS) || defined(CONFIG_OTG_MAX3353_MX2ADS_MODULE)
+        u32 vp;
+        u8 vb=0;
+        TRACE_MSG0(TCD, "1. Read Transceiver ID's with long read");
+        vb = i2c_readb(MAX3353E_MANUFACTURER_REGISTER_0);
+        max3353_private.vendor = vb;
+        vb = i2c_readb(MAX3353E_MANUFACTURER_REGISTER_1);
+        max3353_private.vendor += vb<<8;
+        vb = i2c_readb(MAX3353E_MANUFACTURER_REGISTER_2);
+        max3353_private.vendor += vb<<16;
+        vb = i2c_readb(MAX3353E_MANUFACTURER_REGISTER_3);
+        max3353_private.vendor += vb<<24;
+
+        vb = i2c_readb(MAX3353E_PRODUCT_ID_REGISTER_0);
+        max3353_private.product = vb;
+        vb = i2c_readb(MAX3353E_PRODUCT_ID_REGISTER_1);
+        max3353_private.product += vb<<8;
+        vb = i2c_readb(MAX3353E_PRODUCT_ID_REGISTER_2);
+        max3353_private.product += vb<<16;
+        vb = i2c_readb(MAX3353E_PRODUCT_ID_REGISTER_3);
+        max3353_private.product += vb<<24;
+
+        mode1 = i2c_readb(MAX3353E_CONTROL_REGISTER_1);
+        mode2 = i2c_readb(MAX3353E_CONTROL_REGISTER_2);
+
+        TRACE_MSG4(TCD, "2. OTG Transceiver: vendor: %08x product: %08x mode1: %02x mode2: %02x",
+                        max3353_private.vendor, max3353_private.product, mode1, mode2);
+
+        TRACE_MSG0(TCD, "3. Disable All Transceiver Control Register 1 ");
+        i2c_writeb(MAX3353E_CONTROL_REGISTER_1, 0x0);                                    // clear
+        i2c_writeb(MAX3353E_CONTROL_REGISTER_2, 0x0);                                    // clear
+
+        TRACE_MSG0(TCD, "4. Enable D-/D+ Pulldowns in Transceiver Control Register 1 ");
+
+        i2c_writeb(MAX3353E_CONTROL_REGISTER_1, MAX3353E_DM_PULLDOWN | MAX3353E_DP_PULLDOWN);
+        max3353_private.flags = MAX3353E_DM_PULLDOWN | MAX3353E_DP_PULLDOWN;
+
+        TRACE_MSG0(TCD, "5. Clear latch and enable interrupts");
+        i2c_writeb(MAX3353E_INTERRUPT_LATCH, 0x0);
+        max3353_int_src_set(0x1f);
+
+
+
+}
+
+
diff -uNr linux/drivers/no-otg/ocd/max3353e/max3353e.h linux/drivers/otg/ocd/max3353e/max3353e.h
--- linux/drivers/no-otg/ocd/max3353e/max3353e.h	1970-01-01 01:00:00.000000000 +0100
+++ linux/drivers/otg/ocd/max3353e/max3353e.h	2006-09-01 21:41:32.000000000 +0200
@@ -0,0 +1,134 @@
+/*
+ * otg/ocd/max3353/max3353.h -- USB Transceiver Controller driver
+ * @(#) balden@belcarra.com|otg/ocd/max3353e/max3353e.h|20060831021118|62926
+ *
+ *      Copyright (c) 2004-2005 Belcarra
+ *	Copyright (c) 2005-2006 Belcarra Technologies 2005 Corp
+ *
+ * By:
+ *      Stuart Lynne <sl@lbelcarra.com>,
+ *      Bruce Balden <balden@belcarra.com>
+ *
+ */
+/*!
+ * @defgroup MAX3353TCD TCD - Maxim MAX3353
+ * @ingroup platformgroup
+ */
+/*!
+ * @file otg/ocd/max3353e/max3353e.h
+ * @brief Private structures and defines for MAX3353 Transciever Controller Driver.
+ *
+ * This file contains the private defines and structures for the MAX3353 Transceiver
+ * Driver.
+ *
+ * @ingroup MAX3353TCD
+ */
+
+
+#ifdef CONFIG_XXXXX
+#define MAX3353_GPIO            (2)
+#define TCD_MAX3353_I2C_ADDR    MAX3353_I2C_ADDR_HIGH;
+#endif
+
+#define MAX3353_NAME    "max3353"
+
+#define MAX3353_LOC_CONN 0x01
+
+#define MAX3353_INT_SRC         0xeb
+
+//#define MAX3353_ALL_INT_SRC     0xff
+//#define MAX3353_NO_SE1_INT    0xeb
+//#define MAX3353_NO_SE1_INT    0xfb              /* do not pay attention to DP_HI */
+
+typedef enum otg_transceiver {
+        unknown_transceiver,
+        max3353,
+        max3301e
+} otg_transceiver_t;
+
+struct otg_transceiver_map {
+        otg_transceiver_t transceiver_type;
+        u16 vendor;
+        u16 product;
+        u16 revision;
+        char *name;
+};
+
+struct max3353_private {
+        WORK_STRUCT bh;
+        u16 vendor;
+        u16 product;
+        u16 revision;
+        u32 flags;
+        u8  int_src;
+        struct otg_transceiver_map *transceiver_map;
+};
+
+/*! tx_mode
+ * This defines the various ways that the MAX3353 can be
+ * wired into the host USB.
+ */
+typedef enum tx_mode {
+        dat_se0_bidirectional,          /*!< 3-Wire */
+        vp_vm_bidirectional,            /*!< 4-Wire */
+        dat_se0_unidirectional          /*!< 6-Wire */
+} tx_mode_t;
+
+/*! spd_ctrl
+ * This defines how the speed and suspend pins are controlled
+ * for this host.
+ */
+typedef enum spd_ctrl {
+        spd_susp_pins,                  /*!< controlled by SPEED and SUSPEND pins */
+        spd_susp_reg,                   /*!< controled by SPEED_REG and SUSPEND_REG in Mode Control 1 register */
+} spd_ctrl_t;
+
+
+extern struct tcd_ops tcd_ops;
+extern struct max3353_private max3353_private;
+extern struct tcd_instance *tcd_instance;
+
+/* max3353.c */
+extern int max3353_mod_init(void);
+extern void max3353_mod_exit(void);
+extern void max3353_chrg_vbus(struct otg_instance *, u8 );
+extern void max3353_drv_vbus(struct otg_instance *, u8 );
+extern void max3353_dischrg_vbus(struct otg_instance *, u8 );
+extern void max3353_dp_pullup_func(struct otg_instance *, u8 );
+extern void max3353_dm_pullup_func(struct otg_instance *, u8 );
+extern void max3353_dm_det_func(struct otg_instance *, u8 );
+extern void max3353_dp_det_func(struct otg_instance *, u8 );
+extern void max3353_bdis_acon_func(struct otg_instance *otg, u8 flag);
+extern void max3353_id_pulldown_func(struct otg_instance *otg, u8 flag);
+extern void max3353_audio_func(struct otg_instance *otg, u8 flag);
+extern void max3353_uart_func(struct otg_instance *otg, u8 flag);
+extern void max3353_mono_func(struct otg_instance *otg, u8 flag);
+
+extern void max3353_otg_timer(struct otg_instance *, u8 );
+extern void max3353_bh(void *);
+extern int max3353_id (struct otg_instance *);
+extern int max3353_vbus (struct otg_instance *);
+extern void  max3353_configure(tx_mode_t, spd_ctrl_t );
+
+/* thread */
+extern int max3353_thread_init (void (bh_proc)(void *));
+extern void max3353_thread_exit (wait_queue_head_t *);
+extern void max3353_thread_wakeup(int enabled, int first);
+
+
+
+/* i2c-xxx.c */
+int  i2c_configure(char *name, int addr);
+extern void  i2c_close(void);
+extern u8 i2c_readb(u8 );
+extern u16 i2c_readw(u8 );
+extern u32 i2c_readl(u8 );
+extern int  i2c_writeb(u8 , u8 );
+
+
+/* ********************************************************************************************* */
+#define TRACE_I2CB(t,r) TRACE_MSG3(t, "%-40s[%02x] %02x", #r, r, i2c_readb(r))
+#define TRACE_I2CW(t,r) TRACE_MSG3(t, "%-40s[%02x] %04x", #r, r, i2c_readw(r))
+#define TRACE_GPIO(t,b,r) TRACE_MSG2(t, "%-40s %04x", #r, readw(b + r))
+#define TRACE_REGL(t,r) TRACE_MSG2(t, "%-40s %08x", #r, readl(r))
+
diff -uNr linux/drivers/no-otg/ocd/max3353e/tcd-db1550.c linux/drivers/otg/ocd/max3353e/tcd-db1550.c
--- linux/drivers/no-otg/ocd/max3353e/tcd-db1550.c	1970-01-01 01:00:00.000000000 +0100
+++ linux/drivers/otg/ocd/max3353e/tcd-db1550.c	2006-09-01 21:41:32.000000000 +0200
@@ -0,0 +1,227 @@
+/*
+ * otg/ocd/max3353e/tcd-db1550.c -- USB Transceiver Controller driver
+ * @(#) balden@belcarra.com|otg/ocd/max3353e/tcd-db1550.c|20060831021118|37213
+ *
+ *      Copyright (c) 2004-2005 Belcarra
+ *	Copyright (c) 2005-2006 Belcarra Technologies 2005 Corp
+ *
+ * By:
+ *      Stuart Lynne <sl@lbelcarra.com>,
+ *      Shahrad Payandeh <sp@belcarra.com>,
+ *      Bruce Balden <balden@belcarra.com>
+ *
+ */
+/*!
+ * @file otg/ocd/max3353e/tcd-db1550.c
+ * @brief DB1550 MAX3353 driver.
+ *
+ *
+ * @ingroup MAX3353TCD
+ */
+
+#include <otg/pcd-include.h>
+#include <linux/pci.h>
+#include <otghw/max3353e-hardware.h>
+#include "otghw/bvd-hardware.h"
+#include "max3353e.h"
+#include <asm/au1000.h>
+#include <linux/i2c.h>
+#include <linux/i2c-dev.h>
+
+struct ocd_instance *ocd_instance;
+otg_tag_t OCD;
+
+#if 0
+#include <otg/usbp-chap9.h>
+#include <otg/usbp-func.h>
+#include <otg/usbp-bus.h>
+#include <otg/otg-trace.h>
+#include <otg/hcd.h>
+#include <otg/tcd.h>
+#include <otg/ocd.h>
+#include <otg/pcd.h>
+
+#include <otghw/isp1301-hardware.h>
+#include "isp1301.h"
+
+#endif
+/*SP
+#include <asm/arch/gpio.h>
+#include <asm/arch/mux.h>
+*/
+//struct isp1301_private isp1301_private;
+
+#ifdef CONFIG_OMAP_H2
+#define ADAPTOR_NAME "OMAP I2C"
+#endif /* CONFIG_OMAP_H2 */
+#ifdef CONFIG_ARCH_MAINSTONE
+#define ADAPTOR_NAME "PXA-I2C-Adapter"
+#endif /* CONFIG_ARCH_MAINSTONE */
+#ifdef CONFIG_OTG_DB1550_J15
+#define ADAPTOR_NAME "pb1550 adapter"
+#endif /* CONFIG_OTG_DB1550_J15 */
+
+/* ********************************************************************************************* */
+
+
+static irqreturn_t max3353_int_hndlr (int irq, void *dev_id, struct pt_regs *regs)
+{
+        TRACE_MSG0(TCD, "--");
+
+        SCHEDULE_WORK(max3353_private.bh);
+
+        return IRQ_HANDLED;
+}
+
+
+/* ********************************************************************************************* */
+
+struct ocd_ops ocd_ops;
+struct tcd_instance *db1550_tcd_instance;
+
+//#define ADAPTOR_NAME "pb1550 adapter"
+
+/*! tcd_mod_init
+ */
+int tcd_mod_init(void)
+{
+
+        struct otg_instance *otg;
+
+        int i2c = i2c_configure(ADAPTOR_NAME, MAX3353E_I2C_ADDR_HIGH);
+        int irq = request_irq (AU1500_GPIO_207, max3353_int_hndlr, SA_INTERRUPT, "MAX3353", NULL);
+        THROW_IF(irq, error);
+
+        TRACE_MSG0(TCD, "--");
+
+        THROW_IF(i2c, error);
+
+        TRACE_MSG0(TCD, "1. Enable db1550 external OTG Transceiver!");
+
+
+        TRACE_MSG0(TCD, "2. configure i2c driver!");
+        max3353_configure(dat_se0_bidirectional, spd_susp_reg);
+
+        TRACE_MSG0(TCD, "3. configure max3353!");
+        max3353_mod_init();
+
+        TRACE_MSG0(TCD, "4. set ops");
+	THROW_UNLESS(ocd_instance = otg_set_ocd_ops(&ocd_ops), error);
+        THROW_UNLESS(ocd_instance && (otg = ocd_instance->otg), error);
+        THROW_UNLESS(db1550_tcd_instance = otg_set_tcd_ops(&tcd_ops), error);
+
+#if 0	//test for making pulse on DP
+	for (;;){
+                max3353_private.flags = i2c_readb(MAX3353E_CONTROL_REGISTER_1);
+                TRACE_MSG0(TCD, "Clr DP PULLDOWN");
+                max3353_private.flags &= (~MAX3353E_DP_PULLDOWN);
+                TRACE_MSG0(TCD, "Set DP PULLUP");
+                max3353_private.flags |= (MAX3353E_DP_PULLUP);
+                i2c_writeb(MAX3353E_CONTROL_REGISTER_1, (u8) max3353_private.flags);
+	}
+#endif	
+												
+	
+        /* Success!
+         */
+
+        TRACE_MSG0(TCD, "5. Success!");
+
+        otg_init(otg);
+
+	//In device mode only this part makes a virtual interrupt, while the cable is
+	//and modules are loaded.
+	au_writel (0x40000000, IC1_SRCCLR);
+	au_writel (0x0, IC1_TESTBIT);
+	au_writel (0x1, IC1_TESTBIT);
+	au_writel (0x0, IC1_TESTBIT);
+	au_writel (0x40000000, IC1_SRCSET);
+
+	
+        /* error handling
+         */
+        CATCH(error) {
+		printk(KERN_INFO"Error in tcd initilization\n");
+                if (irq) free_irq(AU1500_GPIO_207, NULL);
+                if (i2c) i2c_close();
+		return -EINVAL;
+        }
+        return 0;
+}
+
+/*! tcd_mod_exit
+ */
+void tcd_mod_exit(void)
+{
+        struct otg_instance *otg = tcd_instance->otg;
+        TRACE_MSG0(TCD, "Exit.");
+
+        otg_event(otg, enable_otg_, OCD, "enable_otg_");
+        //if (otg)
+        //        otg_exit(otg);
+        
+        db1550_tcd_instance = otg_set_tcd_ops(NULL);
+        free_irq(AU1500_GPIO_207, NULL);
+        max3353_mod_exit();
+
+        i2c_writeb(MAX3353E_INTERRUPT_MASK, 0xff);
+        i2c_close();
+
+        if (otg)
+                otg_exit(otg);
+}
+/* ********************************************************************************************* */
+struct tcd_ops tcd_ops = {
+
+//        vbus: max3353_vbus,
+
+        mod_init: tcd_mod_init,
+        mod_exit: tcd_mod_exit,
+
+        dischrg_vbus_func: max3353_dischrg_vbus,
+        chrg_vbus_func: max3353_chrg_vbus,
+        drv_vbus_func: max3353_drv_vbus,
+        dp_pullup_func: max3353_dp_pullup_func,
+        dm_pullup_func: max3353_dm_pullup_func,
+
+};
+
+#if 0
+
+struct tcd_ops tcd_ops;
+
+
+void db1550_tcd_global_init(void)
+{
+	ZERO(tcd_ops);
+
+	tcd_ops.id = max3353_id;
+	tcd_ops.vbus = max3353_vbus;
+        tcd_ops.chrg_vbus_func = max3353_chrg_vbus;
+        tcd_ops.dischrg_vbus_func = max3353_dischrg_vbus;
+        tcd_ops.dp_pullup_func = max3353_dp_pullup_func;
+        tcd_ops.dm_pullup_func = max3353_dm_pullup_func;
+        tcd_ops.dp_pulldown_func = max3353_dp_pulldown_func;
+        tcd_ops.dm_pulldown_func = max3353_dm_pulldown_func;
+        tcd_ops.overcurrent_func = NULL;
+        tcd_ops.dm_det_func = max3353_dm_det_func;
+        tcd_ops.dp_det_func = max3353_dp_det_func;
+        tcd_ops.cr_det_func = max3353_cr_det_func;
+
+	tcd_ops.peripheral_host_func = max3353_peripheral_host_func;
+
+        tcd_ops.id_pulldown_func = max3353_id_pulldown_func;
+        tcd_ops.audio_func = max3353_audio_func;
+        tcd_ops.uart_func = max3353_uart_func;
+        tcd_ops.mono_func = max3353_mono_func;
+
+        tcd_ops.mod_init = tcd_mod_init;
+        tcd_ops.mod_exit = tcd_mod_exit;
+}
+
+	
+#endif
+	
+		
+	
+
diff -uNr linux/drivers/no-otg/ocd/max3353e/tcd-mx2ads.c linux/drivers/otg/ocd/max3353e/tcd-mx2ads.c
--- linux/drivers/no-otg/ocd/max3353e/tcd-mx2ads.c	1970-01-01 01:00:00.000000000 +0100
+++ linux/drivers/otg/ocd/max3353e/tcd-mx2ads.c	2006-09-01 21:41:32.000000000 +0200
@@ -0,0 +1,541 @@
+/*
+ * otg/ocd/max3353e/tcd-mx2ads.c -- MX21ADS ISP1301 Transceiver Controller driver
+ * @(#) balden@belcarra.com|otg/ocd/max3353e/tcd-mx2ads.c|20060831021118|46922
+ *
+ *      Copyright (c) 2004-2005 Belcarra
+ *	Copyright (c) 2005-2006 Belcarra Technologies 2005 Corp
+ *
+ * By:
+ *      Stuart Lynne <sl@lbelcarra.com>,
+ *      Shahrad Payandeh <sp@belcarra.com>,
+ *      Bruce Balden <balden@belcarra.com>
+ *
+ */
+/*!
+ * @file otg/ocd/max3353e/tcd-mx2ads.c
+
+ * @brief MX2ADS ISP1301 driver.
+ *
+ * This implements the ISP1301 Transceiver Controller Driver for the mx21
+ * using the USBOTG I2C controller for access.
+ *
+ * Notes
+ *
+ * 1. There are two interrupt sources, the I2C IO completion type generated when either of NOACK
+ * or RWREADY is set and the XCVR activity type generated when there is a OTG transceiver
+ * generated interrupt.
+ *
+ * It is important to only enable a single source at a time. By default we leave the XCVR
+ * interrupt enabled for normal operation and only enable the I2C IO type when we are actively
+ * waiting for an I2C IO operation to complete.
+ *
+ * 2. The USBOTG module can also interact with the OTG Transceiver, attempting to do register
+ * reads for various conditions (such as XCVR interrupts.) These interfere and cause problems so
+ * we keep the the I2C Controller interface in software mode at all times.
+ *
+ * 3. Because we must wait several hundred micro-seconds for I/O operations to complete, the i2c
+ * implementation is only usable from non-interrupt contexts. This means that all work that
+ * needs to be done needs to be pushed to a bottom half handler. A generic i2cWrite_irq() is
+ * provided where simple writes are all that are required.
+ *
+ * 4. The handling of the ISC_INTERRUPT_AND_CTRL register is complicated because the top nibble
+ * is r/w controlling the interrupt source enables while the bottom nibble is write 1 to clear
+ * to reset two of the three interrupt source bits (NOACK and RWREADY).
+ *
+ *
+ * TODO
+ *
+ * 1. The MX2ADS seems to have a hardware problem, when operating as a B-Device, Vbus does not
+ * drop to zero when disconnected (unplugged.) A 22kohm resister across C133 is suggested.
+ * Alternately I am just enabling VBUS_DISCHRG in LOC_CONN when we enable DP_PULLUP. Hopefully
+ * this will not be required in production code.
+ *
+ * @ingroup ISP1301TCD
+ */
+
+#include <otg/pcd-include.h>
+#include <linux/pci.h>
+#include <asm/arch/mx2.h>
+#include <otghw/isp1301-hardware.h>
+#include "mx2.h"
+#include <otghw/mx2-hardware.h>
+#include "isp1301.h"
+//#include <otg/otg-ops.h>
+
+/* ********************************************************************************************* */
+/* ********************************************************************************************* */
+
+/*! mx2_urb
+ */
+u8 mx2_urbv(u32 port)
+{
+        u8 val =  mx2_rb(port);
+        TRACE_MSG2(TCD, "PORT: %08x %02x", port, val);
+        return val;
+}
+
+/* ********************************************************************************************* */
+/* ********************************************************************************************* */
+/* I2C Bottom Half I/O
+ *
+ * These are the I2C I/O routines that can be used from any non-interrupt
+ * context, typically from a bottom half handler.
+ *
+ * There is an interrupt safe, delayed i2cWrite function, it queues the
+ * writes and schedules work via a bottom half handler that can safely call
+ * the i2cWrite function.
+ */
+#define MX2_I2C_TIMEOUT         1
+#define I2C_READ_MASK           0x80
+#define I2C_MAX_WRITE           100
+
+DECLARE_MUTEX(i2c_sem);                                                 // semaphore to prevent overlapped i2c i/o
+wait_queue_head_t mx2_i2c_wait;                                         // wait structure for i2c i/o interrupt
+struct WORK_STRUCT mx2_i2c_io_work_struct;                              // work structure for queued i2c writes
+
+int i2c_Write_Queued;
+u8 i2c_Write_Data[I2C_MAX_WRITE];
+u32 i2c_Write_Addr[I2C_MAX_WRITE];
+char * i2c_Write_Msg[I2C_MAX_WRITE];
+
+/*! i2c_read - read from otg transceiver
+ * This programs the I2C controller to do a read, waits for the completion
+ * interrupt, cleans up and returns the value for the specified register.
+ */
+void i2c_read(u32 port, int ops)
+{
+        u8 i2c_interrupt_and_ctrl;
+        u8 ctrl_reg;
+        int count;
+        RETURN_IF(i2c_Write_Queued < 0);
+        down(&i2c_sem);                                                 // pass through semaphore
+#if 1
+        if ((ctrl_reg = mx2_rb(MX2_I2C_OP_CTRL_REG)) & 0x4) {
+                TRACE_MSG1(TCD, "bad ctrl_reg: %02x", ctrl_reg);
+        }
+        for (count = 0; (count < 100) && (ctrl_reg = mx2_rb(MX2_I2C_OP_CTRL_REG)) & 0x80; count++) {
+                TRACE_MSG1(TCD, "busy ctrl_reg: %02x", ctrl_reg);
+        }
+#endif
+        mx2_orb(MX2_I2C_INTERRUPT_AND_CTRL, 0x6);                   // reset and disable interrupts
+        mx2_wb(MX2_SEQ_OP_REG, ops);                                    // set number of sequential operations to 1
+        mx2_wb(MX2_SEQ_RD_STARTAD, port);                               // sequential read start address
+        mx2_wb(MX2_OTG_XCVR_DEVAD, I2C_READ_MASK | MX2_I2C_DEV_ADDR);   // receiver address
+        mx2_orb(MX2_I2C_INTERRUPT_AND_CTRL, I2C_NOACK_EN | I2C_RWREADY_EN); // enable i2x I/O interrupt
+        interruptible_sleep_on_timeout(&mx2_i2c_wait, MX2_I2C_TIMEOUT); // wait for signal from i2c io interrupt handler
+        up(&i2c_sem);
+}
+
+/*! i2c_readb - read a byte from otg transceiver
+ * This programs the I2C controller to do a single byte read, waits for the
+ * completion interrupt, cleans up and returns the value for the specified
+ * register.
+ */
+u8 i2c_readb(u8 port)
+{
+        u8 data;
+        u32 addr = OTG_I2C_BASE + port;
+        i2c_read(addr, 1);
+        data =  mx2_rb(addr);                                          // read the register
+        //TRACE_MSG2(TCD, "port: %08x data: %02x", port, data);
+        return data;
+}
+
+/*! i2c_readw - read two bytes from otg transceiver
+ * This programs the I2C controller to do a two byte read, waits for the
+ * completion interrupt, cleans up and returns the value for the specified
+ * registers.
+ */
+u16 i2c_readw(u8 port)
+{
+        u16 data;
+        u32 addr = OTG_I2C_BASE + port;
+        i2c_read(addr, 2);
+        data =  mx2_rb(addr) | (mx2_rb(addr + 1) << 8);               // read the register
+        //TRACE_MSG2(TCD, "port: %08x data: %04x", port, data);
+        return data;
+}
+
+/*! i2c_readl - read two bytes from otg transceiver
+ * This programs the I2C controller to do a two byte read, waits for the
+ * completion interrupt, cleans up and returns the value for the specified
+ * registers.
+ */
+u32 i2c_readl(u8 port)
+{
+        u32 data;
+        u32 addr = OTG_I2C_BASE + port;
+        i2c_read(addr, 2);
+        data = mx2_rb(addr) | (mx2_rb(addr + 1) << 8) | (mx2_rb(addr + 2) << 16) | (mx2_rb(addr + 3) << 24);
+        //TRACE_MSG2(TCD, "port: %08x data: %04x", port, data);
+        return data;
+}
+
+/*! i2cWrite - write a byte to otg transceiver
+ * This writes the data to the shadow register, programs the I2C controller
+ * to do a single byte write, waits for the completion interrupt, cleans up
+ * and exits.
+ */
+void i2cWrite(u32 port, u8 data, char *msg)
+{
+        u8 i2c_interrupt_and_ctrl;
+        u8 test;
+        u8 ctrl_reg;
+        int count;
+        //TRACE_FMSG3(TCD, msg, "i2cWrite: port: %s (%08x) data: %02x", mx2_ureg_name(port), port, data);
+        TRACE_FMSG2(TCD, msg, "i2cWrite: (%08x) data: %02x", port, data);
+
+        //test = i2c_readb(port & 0xfffffffe);
+        //TRACE_MSG2(TCD, "i2cWrite: %02x before %02x", port, test);
+
+        down(&i2c_sem);                                                 // pass through semaphore
+
+#if 1
+        if ((ctrl_reg = mx2_rb(MX2_I2C_OP_CTRL_REG)) & 0x4) {
+                TRACE_MSG1(TCD, "bad ctrl_reg: %02x", ctrl_reg);
+        }
+        for (count = 0; (count < 10) && (ctrl_reg = mx2_rb(MX2_I2C_OP_CTRL_REG)) & 0x80; count++) {
+                TRACE_MSG1(TCD, "busy ctrl_reg: %02x", ctrl_reg);
+        }
+#endif
+        mx2_orb(MX2_I2C_INTERRUPT_AND_CTRL, 0x6);                   // reset and disable interrupts
+        mx2_wb(port, data);                                           // write the register
+        mx2_wb(MX2_SEQ_OP_REG, 1);                                      // set number of sequential operations to 1
+        mx2_wb(MX2_SEQ_RD_STARTAD, port);                               // sequential read start address
+        mx2_wb(MX2_OTG_XCVR_DEVAD, MX2_I2C_DEV_ADDR);                   // receiver address
+        mx2_orb(MX2_I2C_INTERRUPT_AND_CTRL, I2C_NOACK_EN | I2C_RWREADY_EN); // enable i2x I/O interrupt
+        interruptible_sleep_on_timeout(&mx2_i2c_wait, MX2_I2C_TIMEOUT); // wait for signal from i2c io interrupt handler
+        up(&i2c_sem);
+
+        //test = i2c_readb(port & 0xfffffffe);
+        //TRACE_MSG2(TCD, "i2cWrite: %02x after %02x", port, test);
+}
+
+/*! mx2_i2c_io_bh - bottom half handler to use i2cWrite on queued data
+ */
+void mx2_i2c_io_bh(void *arg)
+{
+        while (i2c_Write_Queued > 0) {
+                u8 write = i2c_Write_Data[0];
+                u32 port = i2c_Write_Addr[0];
+                char *msg = i2c_Write_Msg[0];
+                u8 data;
+                unsigned long flags;                                    // atomic update of counter and saved values
+                local_irq_save (flags);
+                i2c_Write_Queued--;
+                memcpy(i2c_Write_Data, i2c_Write_Data + 1, sizeof(i2c_Write_Data) - 1);
+                memcpy(i2c_Write_Addr, i2c_Write_Addr + 1, sizeof(i2c_Write_Addr) - 4);
+                memcpy(i2c_Write_Msg, i2c_Write_Msg + 1, sizeof(i2c_Write_Msg) - 4);
+                local_irq_restore (flags);
+                i2cWrite(port, write, msg);                             // perform write
+        }
+}
+
+/*! i2cWrite_irq - queue data for i2cWrite bottom half handler
+ */
+void i2cWrite_irq(u32 port, u8 data, char *msg)
+{
+        RETURN_IF(i2c_Write_Queued < 0);
+        if ((i2c_Write_Queued == I2C_MAX_WRITE)) {
+                TRACE_MSG3(TCD, "TOO MANY QUEUED CANNOT WRITE port: %08x data: %02x active: %d", port, data, i2c_Write_Queued);
+                return;
+        }
+        i2c_Write_Addr[i2c_Write_Queued] = port;
+        i2c_Write_Data[i2c_Write_Queued] = data;
+        i2c_Write_Msg[i2c_Write_Queued] = msg;
+        i2c_Write_Queued++;
+        TRACE_FMSG3(TCD, msg, "i2cWrite_irq: port: %08x data: %02x active: %d", port, data, i2c_Write_Queued);
+        SCHEDULE_WORK(mx2_i2c_io_work_struct);
+}
+
+/*! i2c_writeb
+ */
+int i2c_writeb(u8 subaddr, u8 buf)
+{
+        i2cWrite_irq(OTG_I2C_BASE + subaddr, buf, "i2c_writeb");
+        return 0;
+}
+
+/* ********************************************************************************************* */
+/* ********************************************************************************************* */
+/*! mx2ads_bh
+ */
+void mx2ads_bh(void *arg)
+{
+        isp1301_bh(NULL);
+        //mx2_bh(NULL);
+
+        /* re-enable the OTG XCVRINT
+         */
+        mx2_orb(MX2_I2C_INTERRUPT_AND_CTRL, I2C_OTGXCVRINT_EN);
+}
+
+
+
+/* ********************************************************************************************* */
+/* ********************************************************************************************* */
+/* I2C I/O and XCVR Interrupt Handler
+ */
+
+/*! mx2_i2c_int_hndlr
+ * This is called from the main mx21ads interrupt handler to handle the
+ * i2c i/o interrupt. There are two main interrupt sources:
+ *
+ */
+irqreturn_t mx2_i2c_int_hndlr (int irq, void *dev_id, struct pt_regs *regs)
+{
+        //TRACE_MSG0(TCD, "MX2ADS I2C INTERRUPT: WAKEUP");
+        wake_up_interruptible(&mx2_i2c_wait);                                   // wakeup waiting bottom half handler
+        return IRQ_HANDLED;
+}
+
+/*! mx2_tcd_int_hndlr
+ * This is called from the main mx21ads interrupt handler to handle the
+ * i2c i/o interrupt. There are two main interrupt sources:
+ */
+irqreturn_t mx2_tcd_int_hndlr (int irq, void *dev_id, struct pt_regs *regs)
+{
+        //TRACE_MSG0(TCD, "MX2ADS TCD INTERRUPT: SCHEDULE WORK");
+        isp1301_thread_wakeup(1, 0);                                                       // wakeup kernel thread
+        return IRQ_HANDLED;
+}
+
+/* ********************************************************************************************* */
+/* ********************************************************************************************* */
+
+/*! mx2_tcd_init - used to initialize/enable or disable the tcd driver
+ */
+void mx2_tcd_init(struct otg_instance *otg, u8 flag)
+{
+        TRACE_MSG0(TCD, "--");
+        switch (flag) {
+        case SET:
+                TRACE_MSG0(TCD, "MX2_TCD_EN SET");
+                otg_event(otg, TCD_OK | ID_FLOAT, TCD, "TCD_OK");
+                //mx2_i2c_xcvr_schedule(1);
+                isp1301_thread_wakeup(1, 1);
+                break;
+        case RESET:
+                TRACE_MSG0(TCD, "MX2_TCD_EN RESET");
+                isp1301_thread_wakeup(0, 0);
+                otg_event(otg, TCD_OK, TCD, "TCD_OK");
+                break;
+        }
+}
+
+/*! mx2_dp_pullup_func - used to enable or disable peripheral connecting to bus
+ */
+void mx2_dp_pullup_func(struct otg_instance *otg, u8 flag)
+{
+        struct tcd_instance *tcd = (struct tcd_instance *)otg->tcd;
+        TRACE_MSG0(TCD, "--");
+        switch (flag) {
+        case SET:
+                TRACE_MSG0(TCD, "MX2_LOC_CONN SET");
+
+                TRACE_MSG0(TCD, "MX2_LOC_CONN SET - enable function interrupts");
+                mx2_wl(OTG_FUNC_SINT_STEN, /*SYSTEM_DONEREGINTDS_EN | *//*SYSTEM_SOFDETINT_EN |*/ SYSTEM_DONEREGINT_EN |
+                                SYSTEM_SUSPDETINT_EN | SYSTEM_RSMFININT_EN | SYSTEM_RESETINT_EN);
+
+                TRACE_MSG0(TCD, "MX2_LOC_CONN SET - Set VBUS_DISCHRG");
+                i2c_writeb(ISP1301_OTG_CONTROL_SET, (u8) ISP1301_VBUS_DISCHRG);
+                break;
+        }
+
+        /* call generic dp pullup function
+         */
+        isp1301_dp_pullup_func(otg, flag);
+
+        switch (flag) {
+        case RESET:
+                TRACE_MSG0(TCD, "MX2_LOC_CONN SET - Clr VBUS_DISCHRG");
+                i2c_writeb(ISP1301_OTG_CONTROL_CLR, (u8) ISP1301_VBUS_DISCHRG);
+                break;
+        }
+
+}
+
+/*! mx2_drv_vbus - used to enable or disable A-device driving Vbus
+ */
+void mx2_drv_vbus(struct otg_instance *otg, u8 flag)
+{
+        TRACE_MSG0(TCD, "--");
+        switch (flag) {
+        case SET:
+                TRACE_MSG0(TCD, "DRV_VBUS_SET");
+                i2c_writeb(ISP1301_OTG_CONTROL_CLR, (u8) ISP1301_VBUS_DRV);
+                break;
+        }
+
+        /* call generic drv vbus function
+         */
+        isp1301_drv_vbus(otg, flag);
+
+        switch (flag) {
+        case RESET:
+                TRACE_MSG0(TCD, "DRV_VBUS_RESET");
+                i2c_writeb(ISP1301_OTG_CONTROL_SET, (u8) ISP1301_VBUS_DRV);
+                break;
+        }
+}
+
+
+
+/*! mx2_id - return true if id is present
+ */
+int mx2_id(struct otg_instance *otg)
+{
+        /* this will force the bottom half handler to read the interrupt
+         * source register and generate an otg event
+         */
+        TRACE_MSG0(TCD, "ID: 0");
+        //mx2_i2c_xcvr_schedule(1);
+        isp1301_thread_wakeup(1, 1);
+        return 0;
+}
+
+/*! mx2_vbus - return true if Vbus is present
+ */
+int mx2_vbus(struct otg_instance *otg)
+{
+        /* this will force the bottom half handler to read the interrupt
+         * source register and generate an otg event
+         */
+        TRACE_MSG0(TCD, "VBUS: 0");
+        //mx2_i2c_xcvr_schedule(1);
+        isp1301_thread_wakeup(1, 1);
+        return 0;
+}
+
+/* ********************************************************************************************* */
+struct tcd_ops *otg_tcd_ops;
+struct tcd_instance *mx2ads_tcd_instance;
+
+/*! mx2_tcd_mod_init - initial tcd setup
+ * Allocate interrupts and setup hardware.
+ * This is called from mx2-ocd.c
+ */
+int mx2_tcd_mod_init (void)
+{
+        struct otg_instance *otg;
+
+        printk(KERN_INFO"%s\n", __FUNCTION__);
+
+        TRACE_MSG0(TCD, "0. Basic Setup");
+
+
+        init_waitqueue_head(&mx2_i2c_wait);
+        PREPARE_WORK_ITEM(mx2_i2c_io_work_struct, &mx2_i2c_io_bh, NULL);
+        //PREPARE_WORK_ITEM(mx2_i2c_xcvr_work_struct, &mx2_i2c_xcvr_bh, NULL);
+
+        isp1301_thread_init(mx2ads_bh);
+
+
+        TRACE_MSG1(TCD, "i2c_int_hndlr: %x", otg_tcd_ops);
+        TRACE_MSG2(TCD, "i2c_int_hndlr: %x %x", &tcd_ops, tcd_ops.i2c_int_hndlr);
+        TRACE_MSG2(TCD, "tcd_int_hndlr: %x %x", &tcd_ops, tcd_ops.tcd_int_hndlr);
+
+        TRACE_MSG0(TCD, "4. Enable Interrupts");
+
+        mx2_wb(MX2_I2C_OP_CTRL_REG, 0x04);                            // Software mode, output enabled
+
+        mx2_urbv(MX2_OTG_XCVR_DEVAD);
+        mx2_urbv(MX2_SEQ_OP_REG);
+        mx2_urbv(MX2_SEQ_RD_STARTAD);
+        mx2_urbv(MX2_I2C_OP_CTRL_REG);
+        mx2_urbv(MX2_SCLK_TO_SCL_HPER);
+        mx2_urbv(MX2_I2C_INTERRUPT_AND_CTRL);
+
+        mx2_wb(MX2_I2C_OP_CTRL_REG, 0x01);                            // Software mode, output enabled
+
+
+
+        mx2_wb(MX2_I2C_INTERRUPT_AND_CTRL, I2C_OTGXCVRINT_EN | I2C_NOACK_EN | I2C_RWREADY_EN);
+
+        isp1301_configure(vp_vm_bidirectional, spd_susp_pins);
+        isp1301_mod_init();
+
+        THROW_UNLESS(tcd_instance && (otg = tcd_instance->otg), error);
+        THROW_UNLESS(mx2ads_tcd_instance = otg_set_tcd_ops(&tcd_ops), error);
+
+        /* Success!
+         */
+
+        TRACE_MSG0(TCD, "5. Success!");
+
+        otg_init(otg);
+        //otg_start(otg, ocd_ops.capabilities & OCD_CAPABILITIES_AUTO);
+
+        /*
+        if ((ocd_ops.capabilities & OCD_CAPABILITIES_TR) && (ocd_ops.capabilities & OCD_CAPABILITIES_AUTO)) {
+                otg_event_irq(otg, enable_otg, TCD, "enable_otg");
+                otg_event_irq(otg, bus_req, TCD, "bus_req");
+        }
+        */
+
+        CATCH(error) {
+                printk(KERN_INFO"%s: failed\n", __FUNCTION__);
+                return -EINVAL;
+        }
+
+        TRACE_MSG0(TCD, "MX2_MOD_TCD_INIT FINISHED");
+        return 0;
+}
+
+/*! mx2_tcd_mod_exit - de-initialize
+ * This is called from mx2-ocd.c
+ */
+void mx2_tcd_mod_exit (void)
+{
+        struct otg_instance *otg = tcd_instance->otg;
+        TRACE_MSG0(TCD, "MX2_MOD_TCD_EXIT");
+
+        otg_event(otg, enable_otg_, OCD, "enable_otg_");
+        //if (otg)
+        //        otg_exit(otg);
+
+        mx2ads_tcd_instance = otg_set_tcd_ops(NULL);
+
+        i2c_Write_Queued = -1;
+        wake_up_interruptible(&mx2_i2c_wait);                                   // wakeup waiting bottom half handler
+
+        while (PENDING_WORK_ITEM(mx2_i2c_io_work_struct) /*|| PENDING_WORK_ITEM(mx2_i2c_xcvr_work_struct)*/ ) {
+                printk(KERN_ERR"%s: waiting for bh\n", __FUNCTION__);
+                schedule_timeout(10 * HZ);
+        }
+        isp1301_thread_exit(&mx2_i2c_wait);
+        isp1301_mod_exit();
+
+        if (otg)
+                otg_exit(otg);
+}
+
+/* ********************************************************************************************* */
+struct tcd_ops tcd_ops = {
+        id: mx2_id,
+        vbus: mx2_vbus,
+
+        tcd_init_func: mx2_tcd_init,
+        tcd_en_func: NULL,
+        chrg_vbus_func: isp1301_chrg_vbus,
+        drv_vbus_func: isp1301_drv_vbus,
+        dischrg_vbus_func: isp1301_dischrg_vbus,
+        dp_pullup_func: mx2_dp_pullup_func,
+        dm_pullup_func: isp1301_dm_pullup_func,
+
+        overcurrent_func: NULL,
+        dm_det_func: isp1301_dm_det_func,
+        dp_det_func: isp1301_dp_det_func,
+        charge_pump_func: NULL,
+        bdis_acon_func: isp1301_bdis_acon_func,
+        id_pulldown_func: isp1301_id_pulldown_func,
+        audio_func: isp1301_audio_func,
+        uart_func: isp1301_uart_func,
+        mono_func: isp1301_mono_func,
+
+        i2c_int_hndlr: mx2_i2c_int_hndlr,
+        tcd_int_hndlr: mx2_tcd_int_hndlr,
+        mod_init: mx2_tcd_mod_init,
+        mod_exit: mx2_tcd_mod_exit,
+};
+
diff -uNr linux/drivers/no-otg/ocd/otg-hcd/hcd-init-l24.c linux/drivers/otg/ocd/otg-hcd/hcd-init-l24.c
--- linux/drivers/no-otg/ocd/otg-hcd/hcd-init-l24.c	1970-01-01 01:00:00.000000000 +0100
+++ linux/drivers/otg/ocd/otg-hcd/hcd-init-l24.c	2006-09-01 21:41:32.000000000 +0200
@@ -0,0 +1,89 @@
+/*
+ * otg/ocd/otg-hcd/hcd-l24-init.c - OTG Host Controller Driver Module Initialization
+ *
+ *      Copyright (c) 2004 Belcarra
+ *
+ * By: 
+ *      Stuart Lynne <sl@belcara.com>, 
+ *      Tom Rushworth <tbr@belcara.com>, 
+ *      Bruce Balden <balden@belcara.com>
+ *
+ */
+/*!
+ * @file otg/ocd/otg-hcd/hcd-init-l24.c
+ * @brief 
+ *
+ * This file initializes the low level hardware drivers.
+ *
+ * This is the linux 2.4 version.
+ *
+ * @ingroup OTGHCD
+ */
+
+#include <linux/config.h>
+#include <linux/module.h>
+
+#include <otg/otg-compat.h>
+
+#include <otg/otg-compat.h>
+#include <otg/usbp-chap9.h>
+#include <otg/otg-trace.h>
+#include <otg/otg-api.h>
+#include <otg/otg-utils.h>
+#include <otg/otg-hcd.h>
+
+#ifdef MODULE
+#if LINUX_VERSION_CODE >= KERNEL_VERSION (2,4,17)
+MODULE_LICENSE ("GPL");
+#endif
+MODULE_PARM (serial_number_str, "s");
+MODULE_PARM_DESC (serial_number_str, "Serial Number");
+MODULE_AUTHOR ("sl@belcarra.com, tbr@belcarra.com");
+MODULE_DESCRIPTION ("USB On-The-Go HCD");
+#endif
+char *serial_number_str;
+
+otg_tag_t HCD;
+extern struct hcd_ops hcd_ops;
+struct hcd_instance *hcd_instance;
+
+/* ************************************************************************************* */
+
+/* hcd_modexit - This is *only* used for drivers compiled and used as a module.
+ */
+static void hcd_modexit (void)
+{
+        printk(KERN_INFO"%s\n", __FUNCTION__);
+        if (hcd_ops.mod_exit) hcd_ops.mod_exit();
+        hcd_instance = otg_set_hcd_ops(NULL);
+        otg_trace_invalidate_tag(HCD);
+}
+
+/* otg_modinit - linux module initialization
+ *
+ * This needs to initialize the hcd, pcd and tcd drivers. This includes tcd and possibly hcd
+ * for some architectures.
+ *
+ */
+static int hcd_modinit (void)
+{
+        printk(KERN_INFO"%s\n", __FUNCTION__);
+        HCD = otg_trace_obtain_tag();
+
+        THROW_UNLESS(hcd_instance = otg_set_hcd_ops(&hcd_ops), error);
+        THROW_IF((hcd_ops.mod_init) ? hcd_ops.mod_init() : 0, error);
+
+        CATCH(error) {
+                hcd_modexit();
+                return -EINVAL;
+        }
+        return 0;
+}
+
+module_init (hcd_modinit);
+#ifdef MODULE
+module_exit (hcd_modexit);
+#endif
+
+
+
diff -uNr linux/drivers/no-otg/ocd/otg-hcd/hcd-l26.c linux/drivers/otg/ocd/otg-hcd/hcd-l26.c
--- linux/drivers/no-otg/ocd/otg-hcd/hcd-l26.c	1970-01-01 01:00:00.000000000 +0100
+++ linux/drivers/otg/ocd/otg-hcd/hcd-l26.c	2006-09-01 21:41:32.000000000 +0200
@@ -0,0 +1,806 @@
+/*
+ * otg/ocd/otg-hcd/hcd-l26.c - Generic transfer level USBOTG aware Host Controller Driver (HCD)
+ *
+ *      Copyright (c) 2004 Belcarra Technologies
+ *
+ * By:
+ *      Tom Rushworth <tbr@belcara.com>,
+ *      Stuart Lynne <sl@belcara.com>,
+ *      Bruce Balden <balden@belcara.com>
+ *
+ */
+/*!
+ * @file otg/ocd/otg-hcd/hcd-l26.c
+ * @brief Linux Generic Host Controller Driver
+ *
+ * @ingroup OTGHCD
+ */
+
+#include <linux/config.h>
+#include <linux/module.h>
+#include <linux/kernel.h>
+#include <linux/slab.h>
+#include <linux/init.h>
+#include <linux/usb.h> 
+#include <asm/io.h> 
+
+
+#include <otg/otg-compat.h>
+
+#include "../core/hcd.h"
+
+#include <otg/usbp-chap9.h>
+#include <otg/otg-trace.h>
+#include <otg/otg-api.h>
+#include <otg/otg-hcd.h>
+
+#include <otg/hcd-l26.h>
+#include <otg/hcd-rh.h>
+#include <otg/hcd-hw.h>
+
+int hcd_probe(struct usb_interface *intf, const struct usb_device_id *id)
+{
+        return 0;
+}
+
+void hcd_disconnect(struct usb_interface *intf)
+{
+}
+
+#define STATIC static
+
+STATIC __inline__ struct urb *grab_active_urb(struct bus_hcpriv *bus_hcpriv, int u)
+{
+        unsigned long flags;
+        struct urb *urb;
+        // Paranoia.
+        RETURN_NULL_UNLESS(bus_hcpriv && bus_hcpriv->active_urbs && (u < bus_hcpriv->max_active_urbs));
+        local_irq_save(flags);
+        urb = bus_hcpriv->active_urbs[u];
+        bus_hcpriv->active_urbs[u] = NULL;
+        local_irq_restore(flags);
+        return urb;
+}
+
+#define OUT(o,e)((o && e) ? 1 : 0)
+
+STATIC void hcd_queue_urb(struct dev_hcpriv *dev_hcpriv, struct urb *urb, int endpoint, int out)
+{
+        unsigned long flags;
+        // Paranoia.
+        RETURN_UNLESS(dev_hcpriv && urb && (endpoint < 16));
+        local_irq_save(flags);
+        dev_hcpriv->num_urbs_both[OUT(out, endpoint)][endpoint] += 1;
+        list_add_tail(&urb->urb_list, &dev_hcpriv->queued_urbs_both[OUT(out, endpoint)][endpoint]);
+        TRACE_MSG4(HCD,"urb: %08x ep: %d n: %u %s", (u32)(void*)urb, 
+                        endpoint, dev_hcpriv->num_urbs_both[OUT(out, endpoint)][endpoint], "IN");
+        local_irq_restore(flags);
+}
+
+STATIC struct urb *hcd_next_urb(struct dev_hcpriv *dev_hcpriv, int endpoint, int out)
+{
+        // MUST BE CALLED in_irq()!!
+        struct urb *urb;
+        struct list_head *epq;
+        // Paranoia.
+        RETURN_NULL_UNLESS(dev_hcpriv && (endpoint < 16));
+        epq = &dev_hcpriv->queued_urbs_both[OUT(out, endpoint)][endpoint];
+        RETURN_NULL_IF(list_empty(epq));
+        urb = list_entry(epq->next, struct urb, urb_list);
+        list_del_init(&urb->urb_list);
+        dev_hcpriv->num_urbs_both[OUT(out, endpoint)][endpoint] -= 1;
+        return urb;
+}
+
+STATIC void hcd_finish_urb(struct urb *urb, int reason)
+{
+        /* Call the upper layer completion routine.  Decrement the ref count
+         * (release our interest in this urb).
+         */
+        urb->status = reason;
+        if (urb->complete) 
+                urb->complete(urb, NULL);
+        usb_put_urb(urb);
+}
+
+STATIC int hcd_submit_to_hw(struct bus_hcpriv *bus_hcpriv, struct urb *urb, int out)
+{
+        // Pass the urb down to the hardware.
+        int rc;
+        void *data;
+        int dev_addr;
+        int endpoint;
+        int format;
+        int len;
+        int pid;
+        int toggle;
+        u32 other;
+        unsigned long flags;
+
+        struct usb_device *dev;
+        struct usb_bus *bus;
+        struct dev_hcpriv *dev_hcpriv;
+        u8 *cp;
+        
+        RETURN_EINVAL_UNLESS((dev = urb->dev));
+        RETURN_EINVAL_UNLESS((bus = dev->bus));
+        RETURN_EINVAL_UNLESS((dev_hcpriv = (struct dev_hcpriv *) (dev->hcpriv)));
+
+        dev_addr = usb_pipedevice(urb->pipe);
+        endpoint = usb_pipeendpoint(urb->pipe);
+
+        TRACE_MSG4(HCD,"urb: %08lx ep: %d %s len: %d", (u32)(void*)urb, endpoint, (out?"OUT":"IN"), urb->actual_length);
+        
+        if (out) {
+#if 0
+                int i;
+                u8 *cp = urb->transfer_buffer;
+
+                TRACE_MSG1(HCD, "NEXT TX: length: %d", urb->actual_length);
+
+                for (i = 0; i < urb->actual_length;  i+= 8)
+
+                        TRACE_MSG8(HCD, "SENT  %02x %02x %02x %02x %02x %02x %02x %02x",
+                                        cp[i + 0], cp[i + 1], cp[i + 2], cp[i + 3],
+                                        cp[i + 4], cp[i + 5], cp[i + 6], cp[i + 7]
+                                  );
+#endif
+        }
+
+        if (usb_endpoint_halted(urb->dev, endpoint, out)) {
+                TRACE_MSG4(HCD,"urb for halted endpoint urb: %08lx dev: %d ep: %d dir: %c", 
+                                urb, dev_addr, endpoint, (out?'O':'I'));
+                urb->actual_length = 0;
+                hcd_finish_urb(urb,-EPIPE);
+                return -EINVAL;
+        }
+
+        data = urb->transfer_buffer;
+        len = urb->transfer_buffer_length;
+        format = usb_pipetype(urb->pipe);
+        other = 0;
+
+        switch (format) {
+        case PIPE_CONTROL:
+                pid = USB_PID_SETUP;
+                // overload toggle with data phase direction (setup is always toggle 0)
+                // toggle == TRUE --> OUT  (HOST2DEVICE --> OUT)
+                
+                toggle = ((((struct usbd_device_request *) urb->setup_packet)->bmRequestType & USB_REQ_DIRECTION_MASK) == 
+                                USB_REQ_HOST2DEVICE);
+
+                other = (u32) (void *) urb->setup_packet;
+
+                // XXX send set feature otg enable IFF set configuration and
+                // to first device and previously otg descriptor was seen.
+                break;
+
+        case PIPE_BULK:
+                pid = (out ? USB_PID_OUT : USB_PID_IN);
+                toggle = usb_gettoggle(urb->dev,endpoint,out);
+                other = (urb->transfer_flags & URB_ZERO_PACKET);
+                break;
+
+        case PIPE_INTERRUPT:
+                pid = (out ? USB_PID_OUT : USB_PID_IN);
+                toggle = usb_gettoggle(urb->dev,endpoint,out);
+                other = urb->interval;
+                break;
+
+        case PIPE_ISOCHRONOUS:
+                pid = (out ? USB_PID_OUT : USB_PID_IN);
+                toggle = 0;
+                other = urb->interval;
+                break;
+
+        default:
+                pid = 0;
+                toggle = 0;
+                break;
+        }
+
+        /* We need an irq lock here in order to make sure the urb gets saved in
+           bus_hcpriv->active_urbs[] before any possible completion interrupt. */
+        local_irq_save(flags);
+        rc = hcd_hw_start_transfer(bus_hcpriv, len, data, toggle, usb_maxpacket(urb->dev, urb->pipe, out),
+                                   (((urb->pipe) >> 26) & 1), endpoint,dev_addr, pid, format,other);
+
+        if (rc >= 0 && rc < bus_hcpriv->max_active_urbs) {
+
+                if (!bus_hcpriv->active_urbs[rc]) 
+                        bus_hcpriv->active_urbs[rc] = urb;
+                 
+                /* Oops, total screw up, we shouldn't ever see this.
+                 * (The lower layer just told us it started a second urb in
+                 * an already busy slot.) 
+                 */
+                else 
+                        TRACE_MSG3(HCD,"ERROR - act: %08x urb: %08x slot: %d",
+                                        (u32)(void*)(bus_hcpriv->active_urbs[rc]), (u32)(void*)urb, rc);
+        }
+        local_irq_restore(flags);
+        if (rc < 0) {
+                // Resubmit is OK...
+                if (rc == -2) {
+                        TRACE_MSG1(HCD,"no ETDs for urb: %08lx",urb);
+                        return 1;
+                }
+
+                // Complete the urb as invalid.
+                urb->actual_length = 0;
+                hcd_finish_urb(urb,-EINVAL);
+                // And signal to try again.
+                return -1;
+        }
+        return 0;
+}
+
+STATIC void hcd_queue_start(struct bus_hcpriv *bus_hcpriv, struct dev_hcpriv *dev_hcpriv, int endpoint, int out)
+{
+        unsigned long flags;
+        struct urb *urb = NULL;
+        int rc;
+
+        TRACE_MSG2(HCD, "endpoint: %2x %s", endpoint, out ? "OUT" : "IN");
+
+        // Paranoia.
+        RETURN_UNLESS(dev_hcpriv && endpoint < 16);
+        do {
+                local_irq_save(flags);
+                if (dev_hcpriv->epq_state_both[OUT(out, endpoint)][endpoint] == EPQ_RUNNING) {
+                        // There's already an urb for this device/endpoint in the HW, don't start another.
+                        urb = NULL;
+                } 
+                else if (!(urb = hcd_next_urb(dev_hcpriv, endpoint, out))) {
+                        dev_hcpriv->epq_state_both[OUT(out, endpoint)][endpoint] = EPQ_EMPTY;
+                } 
+                else {
+                        // There is an urb to go, set the state to show the HW is (will be) busy.
+                        dev_hcpriv->epq_state_both[OUT(out, endpoint)][endpoint] = EPQ_RUNNING;
+                }
+                local_irq_restore(flags);
+                if (!urb) {
+                        // This endpoint is already active in the HW, or doesn't need to be.
+                        rc = 0;
+                } 
+                else {
+                        // Try to pass the urb down to the HW.
+                        rc =  hcd_submit_to_hw(bus_hcpriv, urb, out);
+                }
+        } while (rc < 0);
+        if (rc > 0) {
+                // We've run out of HW resources, try again later.
+                local_irq_save(flags);
+                list_add(&urb->urb_list, &dev_hcpriv->queued_urbs_both[OUT(out, endpoint)][endpoint]);
+                dev_hcpriv->num_urbs_both[OUT(out, endpoint)][endpoint] += 1;
+                dev_hcpriv->epq_state_both[OUT(out, endpoint)][endpoint] = EPQ_WAITING;
+                TRACE_MSG3(HCD,"urb: %08x ep: %d n: %u WAITING IN", 
+                                (u32)(void*)urb, endpoint, dev_hcpriv->num_urbs_both[OUT(out, endpoint)][endpoint]);
+                local_irq_restore(flags);
+                /* There should be no need to queue a task to try later, as the HW
+                 * should have lots of pending urbs, and urb completion will lead
+                 * to a search for new work.  There is a small chance that _all_ the
+                 * pending urbs will complete between the call to hcd_submit_to_hw(),
+                 * but it's unlikely. 
+                 */
+        }
+}
+
+
+/* ********************************************************************************************** */
+/* Functions provided to the HW dependent layer (below this one).
+ */
+void hcd_transfer_complete(struct bus_hcpriv *bus_hcpriv, int transfer_id, int format, int cc, u32 remaining, int next_toggle)
+{
+        struct urb *urb = grab_active_urb(bus_hcpriv, transfer_id);
+        struct usb_device *dev;
+        struct usb_bus *bus;
+        struct dev_hcpriv *dev_hcpriv;
+        int out;
+
+        int endpoint;
+        u8 *cp;
+
+        UNLESS (urb) {
+                TRACE_MSG1(HCD,"active_urb[%d] NULL",transfer_id);
+                return;
+        }
+
+        RETURN_UNLESS((dev = urb->dev));
+        RETURN_UNLESS((bus = dev->bus));
+        RETURN_UNLESS((dev_hcpriv = (struct dev_hcpriv *) (dev->hcpriv)));
+
+        out = usb_pipeout(urb->pipe);
+
+        TRACE_MSG5(HCD,"id: %d status: %d tlen: %d rem: %d buffer: %p", 
+                        transfer_id, cc, urb->transfer_buffer_length, remaining, urb->transfer_buffer);
+        //if (out) 
+        if (urb->transfer_buffer) {
+#if 0
+                int i;
+                u8 *cp = urb->transfer_buffer;
+
+                TRACE_MSG1(HCD, "NEXT TX: length: %d", urb->actual_length);
+
+                for (i = 0; i < urb->actual_length;  i+= 8)
+
+                        TRACE_MSG8(HCD, "RECV  %02x %02x %02x %02x %02x %02x %02x %02x",
+                                        cp[i + 0], cp[i + 1], cp[i + 2], cp[i + 3],
+                                        cp[i + 4], cp[i + 5], cp[i + 6], cp[i + 7]
+                                  );
+#endif
+        }
+
+        if (-ENOENT == cc) {
+                // Cancelled.
+                urb->actual_length = 0;
+        } 
+        else {
+                switch (format) {
+                case PIPE_CONTROL:
+                        urb->actual_length = urb->transfer_buffer_length - remaining;
+
+                        if ((cp = urb->setup_packet))
+                                TRACE_MSG8(HCD, "SETUP  %02x %02x %02x %02x %02x %02x %02x %02x",
+                                                cp[0], cp[1], cp[2], cp[3], cp[4], cp[5], cp[6], cp[7]);
+
+                        if ((dev == bus_hcpriv->first_dev) && !cc && urb->setup_packet) {
+                                struct usb_ctrlrequest *request = (struct usb_ctrlrequest*) urb->setup_packet;
+                                switch (request->bRequest) {
+                                case USB_REQ_SET_ADDRESS:
+                                        otg_event(hcd_instance->otg, ADDRESSED, HCD, "HCD COMPLETE ADDRESSED");
+                                        break;
+                                case USB_REQ_SET_CONFIGURATION:
+                                        otg_event(hcd_instance->otg, CONFIGURED, HCD, "HCD COMPLETE CONFIGURED");
+                                        break;
+                                // XXX check for GET_CONFIGURATION and otg descriptor here
+                                }
+                        }
+                        break;
+
+                case PIPE_BULK:
+                case PIPE_INTERRUPT:
+                        if (0 == cc) {
+                                // QQSV needed for BULK, what about interrupt? 
+                                TRACE_MSG4(HCD,"dev: %d ep: %d dir: %d TOGGLE: %d",urb->dev->devnum,
+                                                              usb_pipeendpoint(urb->pipe),usb_pipeout(urb->pipe),next_toggle);
+                                usb_settoggle(urb->dev,usb_pipeendpoint(urb->pipe),usb_pipeout(urb->pipe),next_toggle);
+                        }
+                        urb->actual_length = urb->transfer_buffer_length - remaining;
+                        //hcd_trace_mem(HCD,__FUNCTION__,"BULK/INTERRUPT urb",urb->actual_length,urb->transfer_buffer);
+                        break;
+
+                case PIPE_ISOCHRONOUS:
+                        urb->actual_length = urb->transfer_buffer_length - remaining;  // QQSV
+                        break;
+
+                default: // Paranoia.
+                        urb->actual_length = 0;
+                        break;
+                }
+        }
+        endpoint = usb_pipeendpoint(urb->pipe);
+        hcd_finish_urb(urb, cc);
+
+        // Start the next urb in the queue for this device/endpoint.
+                dev_hcpriv->epq_state_both[OUT(out, endpoint)][endpoint] = EPQ_WAITING;
+
+        TRACE_MSG2(HCD,"queue_start  ep: %d %s >>>", endpoint, out ? "OUT" : "IN");
+        hcd_queue_start(bus_hcpriv, dev_hcpriv, endpoint, out);
+
+        //TRACE_MSG1(HCD,"queue_start  ep: %d <<<",endpoint);
+        // FIXME - in the future, need to kick off a BH to search other EPs/devs for waiting urbs
+        // if (dev_hcpriv->epq_state[endpoint] == EPQ_EMPTY) {
+        //          /* This queue is empty, so its HW resources are now available,
+        //             kick off BH to search for other EP's and other devs that are waiting. */
+        //          _________
+        // }
+}
+
+/* ********************************************************************************************** */
+/* Functions provided to the USB core layer (above this one).
+ */
+
+/* Allocate space for some USB function plugged into this HC or any hub below it.
+ * Return 0 on success, error code (e.g. -ENOMEM) on failure. 
+ */
+STATIC int hcd_allocate(struct usb_device *dev)
+{
+        struct usb_bus *bus;
+        struct bus_hcpriv *bus_hcpriv;
+        struct dev_hcpriv *dev_hcpriv;
+        int endpoint;
+
+        TRACE_MSG1(HCD,"entered devnum: %d", dev->devnum);
+
+        RETURN_ENOMEM_UNLESS((bus = dev->bus));
+        RETURN_ENOMEM_UNLESS((bus_hcpriv = (struct bus_hcpriv *) bus->hcpriv));
+
+        RETURN_ENOMEM_UNLESS(dev_hcpriv = (struct dev_hcpriv *) kmalloc(sizeof(struct dev_hcpriv), GFP_KERNEL));
+
+        memset(dev_hcpriv, 0, sizeof(struct dev_hcpriv));
+        for (endpoint = 0; endpoint < 16; endpoint++) {
+                INIT_LIST_HEAD(&dev_hcpriv->queued_urbs_both[0][endpoint]);
+                INIT_LIST_HEAD(&dev_hcpriv->queued_urbs_both[1][endpoint]);
+        }
+
+        dev->hcpriv = dev_hcpriv;
+        dev_hcpriv->dev = dev;
+
+        UNLESS (bus_hcpriv->roothub_dev) 
+                bus_hcpriv->roothub_dev = dev;
+        
+        else UNLESS (bus_hcpriv->first_dev) 
+                bus_hcpriv->first_dev = dev;
+
+        return 0;
+}
+
+
+STATIC int hcd_deallocate(struct usb_device *dev)
+{
+        struct usb_bus *bus = dev->bus;
+        struct bus_hcpriv *bus_hcpriv = (struct bus_hcpriv *) bus->hcpriv;
+        struct dev_hcpriv *dev_hcpriv;
+
+        if (dev == bus_hcpriv->first_dev) 
+                bus_hcpriv->first_dev = NULL;
+        
+        if (dev == bus_hcpriv->roothub_dev) 
+                bus_hcpriv->roothub_dev = NULL;
+
+        if ((dev_hcpriv = (struct dev_hcpriv *) (dev->hcpriv))) {
+                dev_hcpriv->dev = NULL;
+                kfree(dev_hcpriv);
+                dev->hcpriv = NULL;
+        }
+        return 0;
+}
+
+
+STATIC void *hcd_buffer_alloc(struct usb_bus *usb_bus, size_t size, int mem_flags, dma_addr_t *dma)
+{
+        return consistent_alloc(0, size, dma);
+}
+
+STATIC void hcd_buffer_free(struct usb_bus *usb_bus, size_t size, void *addr, dma_addr_t dma)
+{
+        RETURN_UNLESS(addr);
+        consistent_free(addr,size,dma);
+}
+
+STATIC int frame_number(struct usb_device *usb_dev)
+{
+        u32 n = hcd_hw_frame_number((struct bus_hcpriv *)(usb_dev->bus->hcpriv));
+        TRACE_MSG1(HCD,"-->: %08lx",n);
+        return n;
+}
+
+STATIC int submit_urb(struct urb *urb, int mem_flags)
+{
+        struct bus_hcpriv *bus_hcpriv;
+        struct dev_hcpriv *dev_hcpriv;
+        int dev_addr;
+        int endpoint;
+        int out;
+
+        RETURN_EINVAL_UNLESS (urb && urb->dev && urb->dev->bus);
+        RETURN_EINVAL_UNLESS ((bus_hcpriv = (struct bus_hcpriv *) (urb->dev->bus->hcpriv)));
+        RETURN_EINVAL_UNLESS ((dev_hcpriv = (struct dev_hcpriv *) (urb->dev->hcpriv)));
+        RETURN_EINVAL_IF (bus_hcpriv->terminating);
+
+        dev_addr = usb_pipedevice(urb->pipe);
+        endpoint = usb_pipeendpoint(urb->pipe);
+        out = usb_pipeout(urb->pipe);
+
+        RETURN_EPIPE_IF (usb_endpoint_halted(urb->dev, endpoint, out));
+
+        /* Divert to root hub if appropriate, (note: NO ref count change).
+         */
+        if (urb->dev == bus_hcpriv->roothub_dev) {
+                TRACE_MSG4(HCD,"root hub --> urb: %08lx dev: %d ep: %d dir: %s", urb, dev_addr, endpoint, (out ? "OUT" : "IN"));
+                return hcd_rh_submit_urb(bus_hcpriv, urb, mem_flags);
+        }
+
+        /* Increase ref count so we own a piece of the URB.  Queue the urb,
+         * then make sure the queue is running.
+         */
+        TRACE_MSG4(HCD,"device --> urb: %08lx dev: %d ep: %d dir: %S", urb, dev_addr, endpoint, (out ? "OUT" : "IN"));
+        hcd_queue_urb(dev_hcpriv, usb_get_urb(urb), endpoint, out);
+        hcd_queue_start(bus_hcpriv, dev_hcpriv, endpoint, out);
+        return 0;
+}
+
+STATIC int unlink_urb(struct urb *urb)
+{
+        int u;
+        unsigned long flags;
+        struct bus_hcpriv *bus_hcpriv = NULL;
+        struct dev_hcpriv *dev_hcpriv = NULL;
+        int dev_addr;
+        int endpoint;
+        struct list_head *epq;
+        int out;
+        
+        RETURN_EINVAL_UNLESS (urb && urb->dev && urb->dev->bus);
+        RETURN_EINVAL_UNLESS ((dev_hcpriv = (struct dev_hcpriv *) (urb->dev->hcpriv)));
+        RETURN_EINVAL_UNLESS ((bus_hcpriv = (struct bus_hcpriv *) (urb->dev->bus->hcpriv)));
+
+        out = usb_pipeout(urb->pipe);
+        
+        dev_addr = usb_pipedevice(urb->pipe);
+
+        /* Does it belong to the root hub?
+         */
+        if (urb->dev == bus_hcpriv->roothub_dev) {
+                TRACE_MSG1(HCD,"root hub --> urb: %08lx",urb);
+                return hcd_rh_unlink_urb(bus_hcpriv,urb);
+        }
+
+        /* Look to see if it is in a device/endpoint queue.
+         */
+        endpoint = usb_pipeendpoint(urb->pipe);
+
+        epq = &dev_hcpriv->queued_urbs_both[OUT(out, endpoint)][endpoint];
+
+        /* Searching a list while in_irq - yuck.  For the time being,
+         * this just assumes that the list is short.  In the future,
+         * it may be possible to keep a small amount of state in the
+         * urb itself to indicate if it is in this list, so that all
+         * we need to do in_irq is check the state and delete from the
+         * list if the state indicates we should.  The state info will
+         * only needed while this HC "owns" the urb. 
+         */
+        local_irq_save(flags);
+
+        if (EPQ_EMPTY != dev_hcpriv->epq_state_both[OUT(out, endpoint)][endpoint]) {
+                // There are queued urbs, search to see if this is one of them.
+                struct urb *qe;
+                list_for_each_entry(qe, epq, urb_list) {
+                        if (qe == urb) {
+                                // Found it.  It hasn't been sent to the HW yet, so it can be finished here.
+                                list_del_init(&urb->urb_list);
+                                dev_hcpriv->num_urbs_both[OUT(out, endpoint)][endpoint] -= 1;
+                                if (list_empty(epq)) 
+                                        dev_hcpriv->epq_state_both[OUT(out, endpoint)][endpoint] = EPQ_EMPTY;
+
+                                TRACE_MSG3(HCD,"urb: %08x ep: %d n: %u UNLINKED IN",
+                                                (u32)(void*)urb, endpoint, 
+                                                dev_hcpriv->num_urbs_both[OUT(out, endpoint)][endpoint]);
+                                hcd_finish_urb(urb,-ENOENT);
+                                local_irq_restore(flags);
+                                return 0;
+                        }
+                }
+        }
+
+        local_irq_restore(flags);
+
+        /* Look to see if this is one of the active urbs.
+         */
+        if (bus_hcpriv->active_urbs) {
+
+                for (u = 0; u < bus_hcpriv->max_active_urbs; u++) {
+                        local_irq_save(flags);
+                        /* This is the one we want, unlink from the HW.
+                         * HW layer does the completion through hcd_transfer_complete()
+                         * in this file, which releases bus_hcpriv->active_urbs[u]. 
+                         */
+                        if (urb == bus_hcpriv->active_urbs[u]) {
+                                int out;
+                                out = usb_pipeout(urb->pipe);
+                                u = hcd_hw_unlink_urb(bus_hcpriv, u);
+
+                                TRACE_MSG4(HCD,"urb: %08x ep: %d n: %u UNLINKED from HW %s", (u32)(void*)urb, endpoint, 
+                                                dev_hcpriv->num_urbs_both[OUT(out, endpoint)][endpoint], out ? "OUT" : "IN");
+
+                                local_irq_restore(flags);
+                                // FUTURE - think about starting a search for new work for the released HW slot.
+                                return u;
+                        }
+                        local_irq_restore(flags);
+                }
+        }
+        // The urb isn't ours, or completed while we were looking for it.
+        TRACE_MSG1(HCD,"Already completed, or someone else's urb: %08lx", urb);
+        return -EINVAL;
+}
+
+/* ********************************************************************************************** */
+
+// void disable(struct usb_device *dev, int bEndpointAddress);
+
+static struct usb_operations operations = {
+        .allocate          = hcd_allocate,
+        .deallocate        = hcd_deallocate,
+        .get_frame_number  = frame_number,
+        .submit_urb        = submit_urb,
+        .unlink_urb        = unlink_urb,
+        .buffer_alloc      = hcd_buffer_alloc, /* allocate dma-consistent buffer for URB_DMA_NOMAPPING */
+        .buffer_free       = hcd_buffer_free,
+        .disable           = NULL
+};
+
+
+/* ********************************************************************************************** */
+
+/* Called as a "bottom-half" to do usbcore registration once all the HW is usable.
+ */
+STATIC void hcd_bh_init(void *arg)
+{
+        struct hcd_instance *hcd = (struct hcd_instance *) arg;
+        otg_event(hcd->otg, HCD_OK, HCD, "HCD_INIT SET HCD_OK");
+        TRACE_MSG0(HCD,"finished");
+}
+
+STATIC void hcd_bh_exit(void *arg)
+{
+        struct hcd_instance *hcd = (struct hcd_instance *) arg;
+        otg_event(hcd->otg, HCD_OK, HCD, "HCD_INIT RESET (EXIT) HCD_OK");
+}
+
+
+/*! hcd_init_func - per host controller common initialization
+ *
+ * This is called to initialize / de-initialize the HCD, all except the last
+ * stage of registering the root hub, because that needs to wait until rh_hcd_en_func()
+ *
+ * We start work items to do this.
+ *
+ */
+void hcd_init_func (struct otg_instance *otg, u8 flag)
+{
+        struct hcd_instance *hcd = otg->hcd;
+
+        switch (flag) {
+        case SET:
+                // Schedule BH for hcd_bh_init...
+                TRACE_MSG0(HCD, "HCD_INIT: SET");
+                PREPARE_WORK_ITEM(hcd->bh, hcd_bh_init, hcd);
+                SCHEDULE_WORK(hcd->bh);
+                TRACE_MSG0(HCD, "hcd_bh_init() schedule finished");
+                break;
+
+        case RESET:
+                TRACE_MSG0(HCD, "HCD_INIT: RESET");
+                PREPARE_WORK_ITEM(hcd->bh, hcd_bh_exit, hcd);
+                SCHEDULE_WORK(hcd->bh);
+                break;
+        }
+}
+
+
+/* ********************************************************************************************** */
+
+STATIC void release_hw_rh(struct bus_hcpriv *bus_hcpriv)
+{
+        RETURN_UNLESS (bus_hcpriv);
+
+        hcd_rh_exit(bus_hcpriv);
+
+        // Complete (cancel) any active urbs.
+        if (bus_hcpriv->active_urbs) {
+                int u;
+                for (u = 0; u < bus_hcpriv->max_active_urbs; u++) {
+                        struct urb *urb;
+                        if ((urb = grab_active_urb(bus_hcpriv,u))) {
+                                urb->actual_length = 0;
+                                hcd_finish_urb(urb,-ENOENT); // USB_ST_URB_KILLED
+                        }
+                }
+        }
+
+        // Release any resources from the layer above.
+        if (bus_hcpriv->usb_bus) {
+                usb_deregister_bus(bus_hcpriv->usb_bus);
+                bus_hcpriv->usb_bus = NULL;
+        }
+
+        // Lastly, release this layer's info.
+        if (bus_hcpriv->active_urbs) {
+                kfree(bus_hcpriv->active_urbs);
+                bus_hcpriv->active_urbs = NULL;
+        }
+}
+
+/* ********************************************************************************************** */
+/*
+ * Note that initialization and cleanup happen in two stages:
+ * 1. Allocation of the basic struct bus_hcpriv, the first level of LDM/USB driver registration, and OTG operations
+ * 2. Everything else - completion of data allocation, USBcore registration, HC HW init, root hub enumeration - when
+ *    called back by OTG (after any magic required to access the HCD hardware.)
+ * Only stage 1 is done on module entry.
+ */
+
+int __init hcd_init(struct bus_hcpriv *bus_hcpriv, struct usb_driver *usb_driver)
+{
+        struct usb_bus *usb_bus = NULL;
+        int usb_registered = 0;
+        int device_registered = 0;
+        int bus_registered = 0;
+        int rc = 0;
+        struct urb **active_urbs = NULL;
+
+        /* fixup bus_hcpriv structure
+         */
+        bus_hcpriv->usb_driver = usb_driver;
+        bus_hcpriv->bus_device.driver = &usb_driver->driver;
+        bus_hcpriv->max_active_urbs = bus_hcpriv->max_active_transfers;
+        usb_driver->probe = hcd_probe;
+
+        UNLESS (hcd_ops.hcd_init_func) hcd_ops.hcd_init_func = hcd_init_func;
+
+        /* allocate urb array 
+         */
+        THROW_UNLESS ((active_urbs = (struct urb **) kmalloc(bus_hcpriv->max_active_urbs*sizeof(struct urb *),GFP_KERNEL)), 
+                        error);
+
+        bus_hcpriv->active_urbs = active_urbs;
+        memset(bus_hcpriv->active_urbs,0,bus_hcpriv->max_active_urbs*sizeof(struct urb *));
+        TRACE_MSG1(HCD,"active_urbs ok, max active URBs: %d",bus_hcpriv->max_active_urbs);
+
+        /* register with usb core
+         */
+        THROW_UNLESS(usb_registered = usb_register(usb_driver) ? 0 : 1, error);
+        TRACE_MSG0(HCD,"usb_register ok");
+
+        /* We need a bus before we can initialize the root hub.
+         */
+        THROW_UNLESS ((usb_bus = usb_alloc_bus(&operations)), error);
+        bus_hcpriv->usb_bus = usb_bus; 
+        TRACE_MSG0(HCD,"usb bus allocated");
+
+        /* register bus
+         */
+        snprintf(bus_hcpriv->bus_device.bus_id, BUS_ID_SIZE, "bus_hcpriv%d", usb_bus->busnum);
+        snprintf(bus_hcpriv->bus_device.name, DEVICE_NAME_SIZE,"%s%d", bus_hcpriv->usb_driver->name, usb_bus->busnum); 
+        usb_bus->hcpriv = (void *) bus_hcpriv;
+        usb_bus->controller = &bus_hcpriv->bus_device;
+        THROW_UNLESS ((bus_registered = usb_register_bus(usb_bus)) ? 0 : 1, error);
+        TRACE_MSG0(HCD,"usb bus registered");
+
+        /* All set for the host controller, initialize the virtual root hub.
+         */
+        THROW_IF (hcd_rh_init(bus_hcpriv), error);
+        TRACE_MSG0(HCD,"root hub initialized");
+
+        /* register device
+         */
+        //THROW_IF ((device_registered = device_register(&bus_hcpriv->bus_device) ? 0 : 1), error);
+        if (device_register(&bus_hcpriv->bus_device)) {
+                printk(KERN_ERR"%s: device_register not ok\n", __FUNCTION__);
+        }
+        
+        /* Everybody's ready, enable the interrupts.
+         */
+        
+        /* Kick off the root hub.
+         */
+        THROW_IF ((rc = usb_register_root_hub(bus_hcpriv->rh_hcpriv->dev, &(bus_hcpriv->bus_device))), error); 
+
+        return 0;
+
+        CATCH(error) {
+
+                if (device_registered) device_unregister(&bus_hcpriv->bus_device);
+                if (usb_bus) {
+                        if (bus_registered && usb_bus) usb_deregister_bus(usb_bus);
+                        kfree(usb_bus);
+                }
+                if (usb_registered) usb_deregister(usb_driver);
+                if (active_urbs) kfree(active_urbs);
+                return -EINVAL;
+        }
+}
+
+#ifdef MODULE
+void hcd_exit(struct bus_hcpriv *bus_hcpriv, struct usb_driver *usb_driver)
+{
+        release_hw_rh(bus_hcpriv);
+
+        usb_deregister_bus(bus_hcpriv->usb_bus);
+        device_unregister(&bus_hcpriv->bus_device);
+        usb_deregister(usb_driver);
+}
+#endif
+
diff -uNr linux/drivers/no-otg/ocd/otg-hcd/hcd-rh.c linux/drivers/otg/ocd/otg-hcd/hcd-rh.c
--- linux/drivers/no-otg/ocd/otg-hcd/hcd-rh.c	1970-01-01 01:00:00.000000000 +0100
+++ linux/drivers/otg/ocd/otg-hcd/hcd-rh.c	2006-09-01 21:41:32.000000000 +0200
@@ -0,0 +1,1068 @@
+/*
+ * otg/ocd/otg-hcd/hcd-rh.c - Generic transfer level USBOTG aware
+ *                        virtual root hub _FUNCTION_ driver.
+ *
+ *      Copyright (c) 2004 Belcarra Technologies
+ *
+ * By:
+ *      Tom Rushworth <tbr@belcara.com>,
+ *      Stuart Lynne <sl@belcara.com>,
+ *      Bruce Balden <balden@belcara.com>
+ */
+/*!
+ * @file otg/ocd/otg-hcd/hcd-rh.c
+ * @brief Root Hub for Generic Host Controller Driver
+ *
+ *
+ *
+ * @ingroup OTGHCD
+ */
+
+#include <linux/config.h>
+#include <linux/module.h>
+#include <linux/kernel.h>
+#include <linux/slab.h>
+#include <linux/errno.h>
+#include <linux/usb.h>
+
+#include <otg/otg-compat.h>
+#include "../core/hcd.h"
+
+#include <otg/usbp-chap9.h>
+#include <otg/otg-trace.h>
+#include <otg/otg-api.h>
+#include <otg/otg-utils.h>
+#include <otg/otg-hcd.h>
+
+#include <otg/hcd-l26.h>
+#include <otg/hcd-rh.h>
+#include <otg/hcd-hw.h>
+
+#define STATIC static
+
+/* NOTE: This code assumes the root hub has < 8 ports.  If it has
+         more some of the descriptors and other structures will have to
+         be re-sized. */
+
+STATIC u8 prepare_str(char *ascii, u8 *str, int str_size)
+{
+        int c;
+        u8 *up = str;
+        u8 *ep = (up + str_size) - 1;
+        while ((c = *ascii++) && up < ep) {
+                *up++ = (c & 0x7F);
+                *up++ = 0;
+        }
+        return (u8)(up - str);
+}
+
+STATIC int hcd_rh_string(struct bus_hcpriv *bus_hcpriv, int strno, u8 *buff, int buff_len)
+{
+        // Provide default strings, but let the HW component override them.
+        char *str;
+        switch (strno) {
+        case 0: // Language ID
+                *buff++ = 4;
+                *buff++ = USB_DT_STRING;
+                *buff++ = 0;
+                *buff++ = 0;
+                return 4;
+        case XHC_RH_STRING_CONFIGURATION:
+                str = "Root hub";
+                break;
+        case XHC_RH_STRING_INTERFACE:
+                str = "Root hub - Status";
+                break;
+        case XHC_RH_STRING_SERIAL:
+                str = bus_hcpriv->rh_serial ? bus_hcpriv->rh_serial : "000001";
+                break;
+        case XHC_RH_STRING_PRODUCT:
+                str = bus_hcpriv->rh_product ? bus_hcpriv->rh_product : "Virtual Root Hub";
+                break;
+        case XHC_RH_STRING_MANUFACTURER:
+                str = bus_hcpriv->rh_manufacturer ? bus_hcpriv->rh_manufacturer : "Belcarra Technologies";
+                break;
+        default:
+                str = "????";
+        }
+
+        buff[0] = 2 + prepare_str(str,buff+2,buff_len-2);
+        buff[1] = USB_DT_STRING;
+        return buff[0];
+}
+
+
+/* ********************************************************************************************** */
+
+
+static char *hub_feature_name[] = {
+        "C_HUB_LOCAL_POWER",
+        "C_HUB_OVER_POWER",
+};
+
+STATIC void hcd_rh_hub_feature(struct bus_hcpriv *bus_hcpriv, u16 wValue, int set_flag)
+{
+        unsigned long flags;
+        TRACE_MSG3(HCD,"%s feature %d %s",(set_flag?"SET":"CLR"), wValue, hub_feature_name[wValue]);
+        local_irq_save(flags);
+        if (set_flag) {
+                // SET feature USB2.0 11.24.2.12 pg 434 and USB2.0 11.24.2 tbl 11-17 pg 421
+                // R1: sec 23.11.29 pg 23-62
+                switch (wValue) {
+                // case C_HUB_OVER_CURRENT: ____ FIXME ----
+                // case C_HUB_LOCAL_POWER: ____ FIXME ----
+                default:
+                        // Error, but ignore.
+                        TRACE_MSG1(HCD,"SET invalid FEATURE %d", wValue);
+                        break;
+                }
+        } 
+        else {
+                // CLEAR feature USB2.0 11.24.2.1 pg 422 and USB2.0 11.24.2 tbl 11-17 pg 421
+                // R1: sec 23.11.29 pg 23-62
+                switch (wValue) {
+                case C_HUB_OVER_CURRENT:
+                        bus_hcpriv->rh_hcpriv->hub_change_status &= ~(1 << wValue);
+                        TRACE_MSG1(HCD,"CLR C_HUB_OVER_CURRENT hub_change_status: %08x", bus_hcpriv->rh_hcpriv->hub_change_status);
+                        break;
+                // case C_HUB_LOCAL_POWER: ____ FIXME ----
+                default:
+                        // Error, but ignore.
+                        TRACE_MSG1(HCD,"CLR invalid FEATURE %d", wValue);
+                        break;
+                }
+        }
+        local_irq_restore(flags);
+        hcd_hw_rh_hub_feature(bus_hcpriv, wValue, set_flag);
+}
+
+/* ********************************************************************************************** */
+
+char *port_feature_name[] = {
+        "PORT_CONNECTION",     "PORT_ENABLE",         "PORT_SUSPEND",        "FEATURE 0x03",        
+        "PORT_RESET",          "FEATURE 0x05",        "FEATURE 0x06",        "FEATURE 0x07",        
+        "PORT_POWER",          "PORT_LOW_SPEED",      "FEATURE 0x0a",        "FEATURE 0x0b",        
+        "FEATURE 0x0c",        "FEATURE 0x0d",        "FEATURE 0x0e",        "FEATURE 0x0f",        
+        "C_PORT_CONNECTION",   "C_PORT_ENABLE",       "C_PORT_SUSPEND",      "C_PORT_OVER_CURRENT", 
+        "C_PORT_RESET",        "PORT_TEST",           
+};
+
+void check_port_status_(int feature, u32 change_status, u32 hw_change_status)
+{
+        int mask = (1 << feature);
+        if ((change_status & mask) != (hw_change_status & mask))
+                TRACE_MSG4(HCD, "%-20s %2d %6s %6s MISMATCH",  port_feature_name[feature], feature,
+                                change_status & mask ? "SET" : "RESET", hw_change_status & mask ? "SET" : "RESET" );
+}
+
+void check_port_status(struct bus_hcpriv *bus_hcpriv, u16 wIndex, char *msg)
+{
+        struct rh_hcpriv *rh_hcpriv = bus_hcpriv->rh_hcpriv;
+        u32 change_status = ((wIndex -1) < bus_hcpriv->num_ports) ?  bus_hcpriv->rh_hcpriv->port_change_status[wIndex-1] : 0;
+        u32 hw_change_status = hcd_hw_rh_get_port_change_status(bus_hcpriv, wIndex);
+
+        TRACE_MSG4(HCD,"PORT_STATUS[%2d] %08x %08x %s", wIndex, 
+                        change_status, hcd_hw_rh_get_port_change_status(bus_hcpriv, wIndex), msg);
+
+        UNLESS(rh_hcpriv->suspended) {
+                // XXX
+        }
+
+        check_port_status_(PORT_CONNECTION, change_status, hw_change_status); 
+        check_port_status_(PORT_ENABLE, change_status, hw_change_status); 
+        check_port_status_(PORT_RESET, change_status, hw_change_status); 
+        check_port_status_(PORT_POWER, change_status, hw_change_status);
+        check_port_status_(C_PORT_CONNECTION, change_status, hw_change_status); 
+        check_port_status_(C_PORT_ENABLE, change_status, hw_change_status); 
+        check_port_status_(C_PORT_RESET, change_status, hw_change_status); 
+}
+/* ********************************************************************************************** */
+/*
+ * wIndex is port number (1-N)
+ * wValue is feature selector
+ */
+STATIC void hcd_rh_port_feature(struct bus_hcpriv *bus_hcpriv, u16 wValue, u16 wIndex, int set_flag)
+{
+        TRACE_MSG4(HCD,"%s port %d FEATURE %02x %s", (set_flag?"SET":"CLR"),  wIndex, 
+                        wValue, port_feature_name[wValue]);
+        /* Not a valid port number
+         */
+        if (wIndex > bus_hcpriv->num_ports) {
+                TRACE_MSG1(HCD, "invalid port: %d", wIndex);
+                return;
+        }
+        /* Feature selector is wValue
+         * wIndex is 1-origin
+         */
+        TRACE_MSG2(HCD, "port_change_status[%d]: -> %08x", wIndex, bus_hcpriv->rh_hcpriv->port_change_status[wIndex -1]);
+        if (set_flag) {
+                // SET feature
+                switch (wValue) {
+                case PORT_SUSPEND:
+                case PORT_RESET:
+                case PORT_POWER:
+                        bus_hcpriv->rh_hcpriv->port_change_status[wIndex - 1] |= (1 << wValue);
+                        break;
+                }
+        } 
+        else {
+                // CLEAR feature (valid features from USB2.0 11.24.2.2 pg 423).
+                switch (wValue) {
+                case PORT_ENABLE: // Disable port.
+                case PORT_SUSPEND: // Cause a Host initiated resume, or no-op if already active.
+                case PORT_POWER: // Put port in powered-off state.
+                case C_PORT_CONNECTION: // clear the PORT_CONNECTION change bit
+                case C_PORT_RESET: // clear the PORT_RESET change bit
+                case C_PORT_ENABLE: // clear the PORT_ENABLE change bit
+                case C_PORT_SUSPEND: // clear the PORT_SUSPEND change bit
+                case C_PORT_OVER_CURRENT: // clear the PORT_OVERCURRENT change bit
+                        bus_hcpriv->rh_hcpriv->port_change_status[wIndex - 1] &= ~(1 << wValue);
+                        break;
+                }
+        }
+        /* update hardware?
+         */
+
+        RETURN_UNLESS (wIndex == bus_hcpriv->otg_port);
+        RETURN_IF (bus_hcpriv->rh_hcpriv->otg_device_mask & (1 << wIndex));
+
+        hcd_hw_rh_port_feature(bus_hcpriv, wValue, wIndex, set_flag);
+
+        TRACE_MSG4(HCD, "OTG Port wValue: %x wIndex: %d %d set: %d", wValue, wIndex, bus_hcpriv->otg_port, set_flag);
+        if (set_flag) {
+                // SET feature
+                switch (wValue) {
+                case PORT_RESET:
+                        TRACE_MSG0(HCD, "OTG Port - HUB_PORT_ENABLED - PORT_RESET");
+                        //otg_event(bus_hcpriv->hcd->otg, HUB_PORT_ENABLED, HCD, "Hub Port Enable (PORT_RESET)");
+                        otg_event(hcd_instance->otg, BUS_RESET, HCD, "PORT_RESET (SET Feature)");
+                        break;
+                case PORT_SUSPEND:
+                case PORT_POWER:
+                        break;
+                }
+                check_port_status(bus_hcpriv, wIndex, "PORT SET");
+        } 
+        else {
+                // CLEAR feature (valid features from USB2.0 11.24.2.2 pg 423).
+                switch (wValue) {
+                case C_PORT_CONNECTION: // clear the PORT_CONNECTION change bit
+                        TRACE_MSG0(HCD, "OTG Port - HUB_PORT_ENABLED/ - C_PORT_CONNECTION");
+                        break;
+                case C_PORT_ENABLE: // clear the PORT_ENABLE change bit
+                        TRACE_MSG0(HCD, "OTG Port - HUB_PORT_ENABLED/ - C_PORT_ENABLE");
+                        break;
+                case PORT_ENABLE: // Disable port.
+                case PORT_SUSPEND: // Cause a Host initiated resume, or no-op if already active.
+                case PORT_POWER: // Put port in powered-off state.
+                case C_PORT_RESET: // clear the PORT_RESET change bit
+                case C_PORT_SUSPEND: // clear the PORT_SUSPEND change bit
+                case C_PORT_OVER_CURRENT: // clear the PORT_OVERCURRENT change bit
+                        break;
+                }
+                check_port_status(bus_hcpriv, wIndex, "PORT CLR");
+        }
+}
+
+/* ********************************************************************************************** */
+/*
+ * Functions for periodic response to the INT URB.
+ * Note that the USB2.0 spec says (sec 11.12.2 pg 337) that a hub controller
+ * will respond to an INT poll as long as the change bits remain set.  This
+ * means we have to record them and continue responding periodically (hence
+ * the timer) until the class driver clears them.
+ *
+ */
+
+void hcd_rh_urb_complete(struct bus_hcpriv *bus_hcpriv, struct urb *urb)
+{       
+        // Almost not needed (vrh could do this directly), except for tracing.
+        TRACE_MSG1(HCD,"urb %p",urb);
+        
+        /* Call the upper layer completion routine.
+         */
+        if (urb->complete)  
+                urb->complete(urb,NULL);
+                        
+        // XXX should we do this here
+        // usb_put_urb(int_urb);
+}                               
+/* ********************************************************************************************** */
+/*
+ * Some sort of change may have happened on the root hub or one of the ports,
+ * scan for changes in the shadow data, and notify usbcore if there is an
+ * INT URB available to do so with.
+ */
+STATIC void hcd_rh_portstatus_bh(void *data)
+{
+        struct bus_hcpriv *bus_hcpriv = (struct bus_hcpriv *) data;
+        struct rh_hcpriv *rh_hcpriv = bus_hcpriv->rh_hcpriv;
+        u8 change_data;
+        struct urb *int_urb;
+
+        u32 hub_port_change_status;
+        unsigned long flags;
+
+        TRACE_MSG1(HCD, "suspended: %d", rh_hcpriv->suspended);
+        RETURN_IF(rh_hcpriv->suspended);
+
+        //printk(KERN_INFO"%s:\n", __FUNCTION__);
+        
+        local_irq_save(flags);
+        hub_port_change_status = bus_hcpriv->rh_hcpriv->hub_port_change_status;
+        bus_hcpriv->rh_hcpriv->hub_port_change_status = 0;
+        local_irq_restore(flags);
+
+
+        change_data = (u8) hub_port_change_status & 0xff;
+        //change_data ~= ~bus_hcpriv->rh_hcpriv->otg_device_mask;
+
+        RETURN_UNLESS (change_data && (int_urb = rh_hcpriv->int_urb));
+
+        TRACE_MSG1(HCD,"non-zero change_data: %02x", change_data);
+
+        change_data |= 1;       // set hub change bit XXX check
+
+        // Completing the urb will "use it up", so remove the link to it.
+
+        del_timer(&rh_hcpriv->poll_timer);
+        rh_hcpriv->int_urb = NULL;
+
+        *((u8 *)(int_urb->transfer_buffer)) = change_data;
+        int_urb->actual_length = 1;
+
+        /* Completing an INT urb will cause it to be immediately re-submitted, so this will call submit_urb()
+         */
+        hcd_rh_urb_complete(bus_hcpriv, int_urb);
+        usb_put_urb(int_urb);
+}
+/* ********************************************************************************************** */
+STATIC void poll_int_urb(unsigned long data)
+{
+        struct bus_hcpriv *bus_hcpriv = (struct bus_hcpriv *) (void *) data;
+        unsigned long flags;
+
+        RETURN_UNLESS (bus_hcpriv && bus_hcpriv->rh_hcpriv);
+
+        local_irq_save(flags);
+        PREPARE_WORK_ITEM(bus_hcpriv->rh_hcpriv->psc_bh, hcd_rh_portstatus_bh, bus_hcpriv);
+        SCHEDULE_WORK(bus_hcpriv->rh_hcpriv->psc_bh);
+        local_irq_restore(flags);
+}
+/* ********************************************************************************************** */
+STATIC u32 rh_get_port_change_status(struct bus_hcpriv *bus_hcpriv, u16 wIndex)
+{
+        if ((wIndex != bus_hcpriv->otg_port) || !(bus_hcpriv->rh_hcpriv->otg_device_mask)) 
+                return hcd_hw_rh_get_port_change_status(bus_hcpriv, wIndex);
+        
+        TRACE_MSG0(HCD, "USING SHADOW INFORMATION");
+        return bus_hcpriv->rh_hcpriv->port_change_status[wIndex-1];
+}
+
+/*
+ * hcd_rh_submit_urb()
+ * Returns: values as for the generic submit_urb() function, see comp_code_to_status(int cc).
+ */
+int hcd_rh_submit_urb(struct bus_hcpriv *bus_hcpriv, struct urb *urb, int mem_flags)
+{
+        struct rh_hcpriv *rh_hcpriv;
+        unsigned int pipe = urb->pipe;
+        struct usbd_device_request *req;
+        u8 bRequest;
+        u8 bmRequestType;
+        u16 wValue;
+        u16 wIndex;
+        u16 wLength;
+        u8 direction;
+        u8 recipient;
+        u8 type;
+        int rc = 0;
+        int rc_stall = -EPIPE;
+        void *reply = NULL;
+        int rlen = 0;
+        u16 status[2];
+
+        static rh_urb; 
+
+        TRACE_MSG1(HCD,"pipe: %08x", pipe);
+
+
+        //printk(KERN_INFO"%s: rh_urb: %d\n", __FUNCTION__);
+        TRACE_MSG1(HCD, "rh_urb: %d", rh_urb++);
+
+        RETURN_ETIMEDOUT_UNLESS(bus_hcpriv && bus_hcpriv->rh_hcpriv);
+
+        rh_hcpriv = bus_hcpriv->rh_hcpriv;
+
+        if (usb_pipeint(pipe)) {
+                // This must be our interrupt endpoint poll.
+                TRACE_MSG1(HCD,"interrupt polling urb: %08lx",urb);
+                urb->status = 0;
+
+                // Increment the ref count, we're keeping this for a while.
+                usb_get_urb(urb);
+                if (rh_hcpriv->int_urb) {
+                        // Hmm, a new one while we still have the old...Shouldn't happen, and doesn't work if it does :(.
+                        //printk(KERN_INFO "%s: releasing old: %08x\n",__FUNCTION__,(u32)(void*)rh_hcpriv->int_urb);
+                        del_timer(&rh_hcpriv->poll_timer);
+                        usb_put_urb(rh_hcpriv->int_urb);
+                }
+
+                rh_hcpriv->int_urb = urb;
+                /* Schedule a scan for changes in case anything happened while there was no urb.
+                 * This has to be slightly delayed, in case this urb is a response to a completed
+                 * scan - usbcore submits a new int urb before clearing the hub/port change status,
+                 * relying on the int urb polling delay to give it time to do the clearing. 
+                 */
+                //printk(KERN_INFO "%s: scheduling PSC poll\n",__FUNCTION__);
+                //init_poll_timer(bus_hcpriv);
+                init_timer(&rh_hcpriv->poll_timer);
+                rh_hcpriv->poll_timer.function = poll_int_urb;
+                rh_hcpriv->poll_timer.data = (unsigned long) (void *) bus_hcpriv;
+                rh_hcpriv->poll_timer.expires = jiffies + 
+                        ((((rh_hcpriv->int_urb->interval < 10) ? 10 : rh_hcpriv->int_urb->interval) * HZ) / 1000);
+                add_timer(&rh_hcpriv->poll_timer);
+                return rc;
+        }
+
+        TRACE_MSG1(HCD,"req: %08x",(u32)(void*)urb->setup_packet);
+        RETURN_EINVAL_UNLESS((req = (struct usbd_device_request *) urb->setup_packet))
+
+        bRequest = req->bRequest;
+        bmRequestType = req->bmRequestType;
+        wValue = le16_to_cpu(req->wValue);
+        wIndex = le16_to_cpu(req->wIndex);
+        wLength = le16_to_cpu(req->wLength);
+        direction  = bmRequestType & USB_REQ_DIRECTION_MASK;
+        recipient = bmRequestType & USB_REQ_RECIPIENT_MASK;
+        type = bmRequestType & USB_REQ_TYPE_MASK;
+
+        TRACE_MSG8(HCD,"urb: %08lx req: %02x reqType: %02x val=%u ndx=%u len=%u dir: %02x recip: %02x",
+                  urb,bRequest,bmRequestType, wValue,wIndex,wLength,direction,recipient);
+
+        if (USB_REQ_HOST2DEVICE == direction) {
+                /* these do not require a reply */
+                int set_flag = (USB_REQ_SET_FEATURE == bRequest);
+                TRACE_MSG0(HCD,"H->D");
+                //printk(KERN_INFO "%s: H->D\n",__FUNCTION__);
+                urb->actual_length = 0;
+                switch (recipient) {
+                case USB_RECIP_HUB:
+                        switch (bRequest) {
+                        case USB_REQ_CLEAR_FEATURE:
+                        case USB_REQ_SET_FEATURE:
+                                if (USB_REQ_TYPE_CLASS == type) {
+                                        TRACE_MSG2(HCD,"%s hub feature: %04x",(set_flag?"set":"clear"),wValue);
+                                        //printk(KERN_INFO "%s hub feature: %04x\n",(set_flag?"set":"clear"),wValue);
+                                        hcd_rh_hub_feature(bus_hcpriv, wValue, set_flag);
+                                } 
+                                else {
+                                        TRACE_MSG2(HCD,"HUB C/S feature invalid type r: %02x t: %02x",bRequest,bmRequestType);
+                                        rc = rc_stall;
+                                }
+                                break;
+                        case USB_REQ_SET_ADDRESS:
+                                if (USB_REQ_TYPE_STANDARD == type) {
+                                        TRACE_MSG1(HCD,"set address %u",wValue);
+                                        //printk(KERN_INFO "set address %u\n",wValue);
+                                        bus_hcpriv->root_hub_addr = wValue;
+                                } 
+                                else {
+                                        TRACE_MSG2(HCD,"HUB SETADDRESS invalid type r: %02x t: %02x",bRequest,bmRequestType);
+                                        rc = rc_stall;
+                                }
+                                break;
+                        case USB_REQ_SET_CONFIGURATION:
+                                if (USB_REQ_TYPE_STANDARD == type) {
+                                        TRACE_MSG1(HCD,"set configuration: %02x",wValue);
+                                        //printk(KERN_INFO "set configuration: %02x\n",wValue);
+                                        rh_hcpriv->curr_cfg = (u8) (0xFF & wValue);
+                                } 
+                                else {
+                                        TRACE_MSG2(HCD,"HUB SETCONFIG invalid type r: %02x t: %02x",bRequest,bmRequestType);
+                                        rc = rc_stall;
+                                }
+                                break;
+                        case USB_REQ_SET_INTERFACE:
+                                // wIndex is interface number
+                                if (USB_REQ_TYPE_STANDARD == type) {
+                                        TRACE_MSG2(HCD,"set interface: %02x to : %02x",wIndex,wValue);
+                                        //printk(KERN_INFO "set interface: %02x to : %02x\n",wIndex,wValue);
+                                        rh_hcpriv->curr_itf = (u8) (0xFF & wValue);
+                                } 
+                                else {
+                                        TRACE_MSG2(HCD,"HUB SETINTERFACE invalid type r: %02x t: %02x",bRequest,bmRequestType);
+                                        rc = rc_stall;
+                                }
+                                break;
+                        default:
+                                TRACE_MSG2(HCD,"HUB invalid request r: %02x t: %02x",bRequest,bmRequestType);
+                                rc = rc_stall;
+                        }
+                        break;
+                case USB_RECIP_PORT:
+                        switch (bRequest) {
+                        case USB_REQ_CLEAR_FEATURE:
+                        case USB_REQ_SET_FEATURE:
+                                // At this point wIndex == port_num is 1-origin.
+                                if (USB_REQ_TYPE_CLASS == type && wIndex <= bus_hcpriv->num_ports && wIndex > 0) {
+                                        TRACE_MSG3(HCD,"%s port %u FEATURE: %04x",(set_flag?"set":"clear"),wIndex,wValue);
+                                        hcd_rh_port_feature(bus_hcpriv, wValue, wIndex, set_flag);
+                                } 
+                                else {
+                                        TRACE_MSG2(HCD,"PORT C/S FEATURE invalid type r: %02x t: %02x",bRequest,bmRequestType);
+                                        rc = rc_stall;
+                                }
+                                break;
+                        default:
+                                TRACE_MSG2(HCD,"PORT invalid request r: %02x t: %02x",bRequest,bmRequestType);
+                                rc = rc_stall;
+                        }
+                        break;
+                case USB_REQ_RECIPIENT_ENDPOINT:
+                        // Clear stalled endpoint
+                        if (USB_REQ_CLEAR_FEATURE != bRequest || 1 != wValue ||
+                            USB_REQ_TYPE_STANDARD != type) {  // QQSV verify type correct
+                                TRACE_MSG3(HCD,"ENDPOINT invalid request,val,type r: %02x t: %02x val=%u",
+                                               bRequest,bmRequestType,wValue);
+                                rc = rc_stall;
+                        }
+                        // Nothing to really do to "clear" the stalled EP.
+                        TRACE_MSG2(HCD,"clear endpoint ndx: %02x val: %02x",wIndex,wValue);
+                        //printk(KERN_INFO "clear endpoint ndx: %02x val: %02x\n",wIndex,wValue);
+                        break;
+                case USB_REQ_RECIPIENT_INTERFACE:
+                default:
+                        TRACE_MSG2(HCD,"H->D invalid recipient r: %02x t: %02x",bRequest,bmRequestType);
+                        rc = rc_stall;
+                }
+        } 
+        else {
+                /* these do require a reply */
+                TRACE_MSG0(HCD,"H<-D");
+                //printk(KERN_INFO "%s: H<-D\n",__FUNCTION__);
+                switch (recipient) {
+                case USB_RECIP_HUB:
+                        switch (bRequest) {
+                        case USB_REQ_GET_DESCRIPTOR:
+                                if (USB_REQ_TYPE_CLASS == type) {
+                                        // Return the hub class descriptor
+                                        TRACE_MSG0(HCD,"get hub class descriptor");
+                                        //printk(KERN_INFO "get hub class descriptor\n");
+                                        reply = (void *) rh_hcpriv->hd;
+                                        rlen = rh_hcpriv->hd->bDescLength;
+                                } 
+                                else if (USB_REQ_TYPE_STANDARD != type) {
+                                        TRACE_MSG2(HCD,"HUB GETDESCRIPTOR invalid type r: %02x t: %02x",bRequest,bmRequestType);
+                                        rc = rc_stall;
+                                } 
+                                else {
+                                        // Return one of the other descriptors
+                                        switch (wValue >> 8) {
+                                        case USB_DT_DEVICE:
+                                                TRACE_MSG0(HCD,"get device descriptor");
+                                                //printk(KERN_INFO "get device descriptor\n");
+                                                reply = (void *) rh_hcpriv->dd;
+                                                rlen = rh_hcpriv->dd->bLength;
+                                                break;
+                                        case USB_DT_CONFIGURATION:
+                                                TRACE_MSG0(HCD,"get configuration descriptor");
+                                                reply = (void *) rh_hcpriv->cd;
+                                                rlen = USB_DT_CONFIG_SIZE+USB_DT_INTERFACE_SIZE+USB_DT_ENDPOINT_SIZE;
+                                                break;
+                                        case USB_DT_STRING:
+                                                TRACE_MSG1(HCD,"get string descriptor %u",wValue);
+                                                reply = urb->transfer_buffer;
+                                                rlen = hcd_rh_string(bus_hcpriv,(wValue & 0xFF),reply,wLength);
+                                                break;
+                                        default:
+                                                TRACE_MSG1(HCD,"HUB GETDESCRIPTOR invalid wValue: %04x",wValue);
+                                                rc = rc_stall;
+                                        }
+                                }
+                                break;
+                        case USB_REQ_GET_STATUS:
+                                if (USB_REQ_TYPE_CLASS == type) {
+                                        u32 change_status = bus_hcpriv->rh_hcpriv->hub_change_status;
+                                        status[0] = cpu_to_le16(change_status & 0xFFFF);
+                                        status[1] = cpu_to_le16(change_status >> 16);
+                                        reply = (void *) &status[0];
+                                        rlen = 4;
+                                        TRACE_MSG1(HCD,"GET HUB_CHANGE_STATUS: %08x", bus_hcpriv->rh_hcpriv->hub_change_status);
+                                } 
+                                else {
+                                        TRACE_MSG2(HCD,"HUB GETSTATUS invalid type r: %02x t: %02x",bRequest,bmRequestType);
+                                        rc = rc_stall;
+                                }
+                                break;
+                        case USB_REQ_GET_INTERFACE:
+                                // FIXME Verify type and interface number (wIndex)
+                                TRACE_MSG1(HCD,"get interface %u",wIndex);
+                                reply = (void *) &rh_hcpriv->curr_itf;
+                                rlen = 1;
+                                break;
+                        case USB_REQ_GET_CONFIGURATION:
+                                // FIXME Verify type
+                                TRACE_MSG0(HCD,"get configuration");
+                                // Return a single byte value
+                                reply = (void *) &rh_hcpriv->curr_cfg;
+                                rlen = 1;
+                                break;
+                        default:
+                                TRACE_MSG2(HCD,"H<-D HUB invalid request r: %02x t: %02x",bRequest,bmRequestType);
+                                rc = rc_stall;
+                        }
+                        break;
+                case USB_RECIP_PORT:
+                        switch (bRequest) {
+                        case USB_REQ_GET_STATUS:
+                                if (USB_REQ_TYPE_CLASS == type) {
+                                        /* USB2.0 11.24.2.7 pg 426 says first hword of reply are status (Tbl 11-21),
+                                                                        second hword is change (Tbl 11-22). */
+                                        u32 change_status = ((wIndex -1) < bus_hcpriv->num_ports) ?
+                                                bus_hcpriv->rh_hcpriv->port_change_status[wIndex-1] : 0;
+                                        u32 hw_change_status = ((wIndex -1) < bus_hcpriv->num_ports) ?
+                                                rh_get_port_change_status(bus_hcpriv, wIndex) : 0;
+
+                                        check_port_status(bus_hcpriv, wIndex, "HOST POLL");
+
+                                        status[0] = cpu_to_le16(hw_change_status & 0xFFFF);
+                                        status[1] = cpu_to_le16(change_status >> 16);
+                                        reply = (void *) &status[0];
+                                        rlen = 4;
+
+                                        TRACE_MSG3(HCD,"GET PORT_CHANGE_STATUS[%2d] %04x %04x", wIndex, status[0], status[1]);
+                                } 
+                                else {
+                                        TRACE_MSG2(HCD,"PORT GETSTATUS invalid type r: %02x t: %02x",bRequest,bmRequestType);
+                                        rc = rc_stall;
+                                }
+                                break;
+                        default:
+                                TRACE_MSG2(HCD,"H<-D PORT invalid request r: %02x t: %02x",bRequest,bmRequestType);
+                                rc = rc_stall;
+                        }
+                        break;
+                case USB_REQ_RECIPIENT_INTERFACE:
+                case USB_REQ_RECIPIENT_ENDPOINT:
+                default:
+                        TRACE_MSG2(HCD,"H<-D invalid recipient r: %02x t: %02x",bRequest,bmRequestType);
+                        rc = rc_stall; 
+                }
+                if (!rc && reply) {
+                        // There is a reply to copy into place.
+                        if (rlen > wLength) {
+                                // Truncate the reply (used to get part of a descriptor).
+                                rlen = wLength;
+                        }
+                        if (rlen > 0 && reply != urb->transfer_buffer) {
+                                memcpy(urb->transfer_buffer,reply,rlen);
+                        }
+                        urb->actual_length = rlen;
+                }
+        }
+        // All done, "complete" it.
+        urb->status = rc;
+        TRACE_MSG3(HCD,"urb: %08lx completing status=%d len=%d",urb,urb->status,urb->actual_length);
+        hcd_rh_urb_complete(bus_hcpriv,urb);
+        TRACE_MSG2(HCD,"urb: %08lx completed->%d",urb,rc);
+        return rc;
+}
+
+/*
+ * hcd_rh_unlink_urb()
+ */
+int hcd_rh_unlink_urb(struct bus_hcpriv *bus_hcpriv, struct urb *urb)
+{
+        struct rh_hcpriv *rh_hcpriv = bus_hcpriv->rh_hcpriv;
+        RETURN_EINVAL_UNLESS(urb && (urb == rh_hcpriv->int_urb));
+        del_timer(&rh_hcpriv->poll_timer);
+        rh_hcpriv->int_urb = NULL;
+        urb->status = -ENOENT; // Cancelled
+        hcd_rh_urb_complete(bus_hcpriv,urb);
+        usb_put_urb(urb);
+        return 0;
+}
+/* ********************************************************************************************** */
+/*
+ * For the hardware specific root hub component.
+ */
+irqreturn_t hcd_rh_int_hndlr(int irq, void *dev_id, struct pt_regs *regs)
+{
+        struct bus_hcpriv *bus_hcpriv = (struct bus_hcpriv *) dev_id;
+
+        // Assume port status change.
+        //printk(KERN_INFO "%s: scheduling hcd_rh_portstatus_bh\n",__FUNCTION__);
+        
+        PREPARE_WORK_ITEM(bus_hcpriv->rh_hcpriv->psc_bh, hcd_rh_portstatus_bh, bus_hcpriv);
+        SCHEDULE_WORK(bus_hcpriv->rh_hcpriv->psc_bh);
+        return IRQ_HANDLED;
+}
+/* ********************************************************************************************** */
+#if !defined(OTG_C99)
+struct usbd_device_descriptor rh_device_descriptor;
+struct usbd_configuration_descriptor rh_configuration_descriptor;
+struct usbd_interface_descriptor rh_interface_descriptor;
+struct usbd_endpoint_descriptor rh_endpoint_descriptor;
+struct hub_descriptor rh_hub_descriptor;
+
+void rh_global_init(void)
+{
+        ZERO(rh_device_descriptor);
+        rh_device_descriptor.bLength = USB_DT_DEVICE_SIZE;
+        rh_device_descriptor.bDescriptorType = USB_DT_DEVICE;
+        rh_device_descriptor.bcdUSB = __constant_cpu_to_le16(USB_BCD_VERSION);
+        rh_device_descriptor.bDeviceClass = USB_CLASS_HUB;
+        rh_device_descriptor.bDeviceSubClass = 0;
+        rh_device_descriptor.bDeviceProtocol = 0;
+        rh_device_descriptor.bMaxPacketSize0 = 8;
+        rh_device_descriptor.iManufacturer = XHC_RH_STRING_MANUFACTURER;
+        rh_device_descriptor.iProduct = XHC_RH_STRING_PRODUCT;
+        rh_device_descriptor.iSerialNumber = XHC_RH_STRING_SERIAL;
+        rh_device_descriptor.bNumConfigurations = 1;
+
+        ZERO(rh_configuration_descriptor);
+        rh_configuration_descriptor.bLength = USB_DT_CONFIG_SIZE;
+        rh_configuration_descriptor.bDescriptorType = USB_DT_CONFIGURATION;
+        rh_configuration_descriptor.wTotalLength = __constant_cpu_to_le16(USB_DT_CONFIG_SIZE+USB_DT_INTERFACE_SIZE
+                        + USB_DT_ENDPOINT_SIZE);
+        rh_configuration_descriptor.bNumInterfaces = 1;
+        rh_configuration_descriptor.bConfigurationValue = 1;
+        rh_configuration_descriptor.iConfiguration = XHC_RH_STRING_CONFIGURATION;
+
+        ZERO(rh_interface_descriptor);
+        rh_interface_descriptor.bLength = USB_DT_INTERFACE_SIZE;
+        rh_interface_descriptor.bDescriptorType = USB_DT_INTERFACE;
+        rh_interface_descriptor.bInterfaceNumber = 0;
+        rh_interface_descriptor.bAlternateSetting = 0;
+        rh_interface_descriptor.bNumEndpoints = 1;
+        rh_interface_descriptor.bInterfaceClass = USB_CLASS_HUB;
+        rh_interface_descriptor.bInterfaceSubClass = 0;
+        rh_interface_descriptor.bInterfaceProtocol = 0;
+        rh_interface_descriptor.iInterface = XHC_RH_STRING_INTERFACE;
+
+        ZERO(rh_endpoint_descriptor);
+        rh_endpoint_descriptor.bLength = USB_DT_ENDPOINT_SIZE;
+        rh_endpoint_descriptor.bDescriptorType = USB_DT_ENDPOINT;
+        rh_endpoint_descriptor.bEndpointAddress = USB_DIR_IN | 1;  // Fixed EP 1
+        rh_endpoint_descriptor.bmAttributes = INTERRUPT;
+        rh_endpoint_descriptor.wMaxPacketSize = __constant_cpu_to_le16(4);
+        rh_endpoint_descriptor.bInterval = 0xFF;
+
+        ZERO(rh_hub_descriptor);
+        rh_hub_descriptor.bDescLength = USB_DT_HUB_NONVAR_SIZE+2;
+        rh_hub_descriptor.bDescriptorType = USB_DT_HUB;
+};
+
+#else /* !defined(OTG_C99) */
+struct usbd_device_descriptor rh_device_descriptor = {
+        .bLength = USB_DT_DEVICE_SIZE,
+        .bDescriptorType = USB_DT_DEVICE,
+        .bcdUSB = __constant_cpu_to_le16(USB_BCD_VERSION),
+        .bDeviceClass = USB_CLASS_HUB,
+        .bDeviceSubClass = 0,
+        .bDeviceProtocol = 0,
+        .bMaxPacketSize0 = 8,
+        .iManufacturer = XHC_RH_STRING_MANUFACTURER,
+        .iProduct = XHC_RH_STRING_PRODUCT,
+        .iSerialNumber = XHC_RH_STRING_SERIAL,
+        .bNumConfigurations = 1,
+};
+
+struct usbd_configuration_descriptor rh_configuration_descriptor = {
+        .bLength = USB_DT_CONFIG_SIZE,
+        .bDescriptorType = USB_DT_CONFIGURATION,
+        .wTotalLength = __constant_cpu_to_le16(USB_DT_CONFIG_SIZE+USB_DT_INTERFACE_SIZE+USB_DT_ENDPOINT_SIZE),
+        .bNumInterfaces = 1,
+        .bConfigurationValue = 1,
+        .iConfiguration = XHC_RH_STRING_CONFIGURATION,
+};
+
+struct usbd_interface_descriptor rh_interface_descriptor = {
+        .bLength = USB_DT_INTERFACE_SIZE,
+        .bDescriptorType = USB_DT_INTERFACE,
+        .bInterfaceNumber = 0,
+        .bAlternateSetting = 0,
+        .bNumEndpoints = 1,
+        .bInterfaceClass = USB_CLASS_HUB,
+        .bInterfaceSubClass = 0,
+        .bInterfaceProtocol = 0,
+        .iInterface = XHC_RH_STRING_INTERFACE,
+};
+
+struct usbd_endpoint_descriptor rh_endpoint_descriptor = {
+        .bLength = USB_DT_ENDPOINT_SIZE,
+        .bDescriptorType = USB_DT_ENDPOINT,
+        .bEndpointAddress = USB_DIR_IN | 1,  // Fixed EP 1
+        .bmAttributes = INTERRUPT,
+        .wMaxPacketSize = __constant_cpu_to_le16(4),
+        .bInterval = 0xFF,
+};
+
+/* This assumes 1-8 ports.
+ * If ports > 8, then DeviceRemovable and PortPwrCtrlMask need to be sized accordingly. 
+ */
+struct hub_descriptor rh_hub_descriptor = {
+        .bDescLength = USB_DT_HUB_NONVAR_SIZE+2,
+        .bDescriptorType = USB_DT_HUB,
+};
+#endif /* !defined(OTG_C99) */
+
+/* ********************************************************************************************** */
+/*
+ * Copy static descriptors to buffer to allow modification for actual
+ * configuration and provide full configuration descriptor.
+ */
+STATIC void build_descriptors(struct bus_hcpriv *bus_hcpriv)
+{
+        struct rh_hcpriv *rh_hcpriv = bus_hcpriv->rh_hcpriv;
+        void *bp = rh_hcpriv->descriptors;
+
+
+        rh_hcpriv->dd = (struct usbd_device_descriptor *) bp;
+        memcpy(bp, &rh_device_descriptor, sizeof(rh_device_descriptor));
+        rh_hcpriv->cd = (struct usbd_configuration_descriptor *) bp = bp + USB_DT_DEVICE_SIZE;
+        memcpy(bp, &rh_configuration_descriptor, sizeof(rh_configuration_descriptor));
+        rh_hcpriv->id = (struct usbd_interface_descriptor *) bp = bp + USB_DT_CONFIG_SIZE;
+        memcpy(bp, &rh_interface_descriptor, sizeof(rh_interface_descriptor));
+        rh_hcpriv->ed = (struct usbd_endpoint_descriptor *) bp = bp + USB_DT_INTERFACE_SIZE;
+        memcpy(bp, &rh_endpoint_descriptor, sizeof(rh_endpoint_descriptor));
+        rh_hcpriv->hd = (struct hub_descriptor *) bp = bp + USB_DT_ENDPOINT_SIZE;
+        memcpy(bp, &rh_hub_descriptor, sizeof(rh_hub_descriptor));
+
+        rh_hcpriv->dd->idVendor = cpu_to_le16(bus_hcpriv->rh_vendorid);
+        rh_hcpriv->dd->idProduct = cpu_to_le16(bus_hcpriv->rh_productid);
+        rh_hcpriv->dd->bcdDevice = cpu_to_le16(bus_hcpriv->rh_bcddevice);
+        rh_hcpriv->cd->bmAttributes = bus_hcpriv->rh_bmAttributes;
+        rh_hcpriv->cd->bMaxPower = bus_hcpriv->rh_bMaxPower;
+        rh_hcpriv->hd->bNbrPorts = bus_hcpriv->num_ports,
+        rh_hcpriv->hd->wHubCharacteristics = cpu_to_le16(hcd_hw_rh_hub_attributes(bus_hcpriv));
+        rh_hcpriv->hd->bPwrOn2PwrGood = hcd_hw_rh_power_delay(bus_hcpriv);
+        rh_hcpriv->hd->bHubContrCurrent = hcd_hw_rh_hub_contr_current(bus_hcpriv);
+        rh_hcpriv->hd->DeviceRemovable = hcd_hw_rh_DeviceRemovable(bus_hcpriv);
+        rh_hcpriv->hd->PortPwrCtrlMask = hcd_hw_rh_PortPwrCtrlMask(bus_hcpriv);
+}
+
+/* ********************************************************************************************** */
+
+#define PORT_SYNC_MASK  ( (1 << PORT_POWER) | (1 << PORT_SUSPEND) | (1 << PORT_RESET) )
+
+/* puts OTG capable port into a_host or b_host state - attempt to use port */
+void rh_loc_sof_func(struct otg_instance *otg, u8 on)
+{
+        struct bus_hcpriv *bus_hcpriv = (struct bus_hcpriv *)(((struct hcd_instance *)(otg->hcd))->privdata);
+        int i;
+        unsigned long flags;
+
+        RETURN_UNLESS (bus_hcpriv && bus_hcpriv->rh_hcpriv && bus_hcpriv->rh_hcpriv->port_change_status);
+
+        local_irq_save(flags);
+        switch (on) {
+        case SET:
+                //printk(KERN_INFO"%s: SET\n", __FUNCTION__);
+                TRACE_MSG0(HCD, "RH LOC_SOF SET");
+
+                // Stop treating otg capable port as a device
+                bus_hcpriv->rh_hcpriv->otg_device_mask &= ~(0x1 << (bus_hcpriv->otg_port));
+
+                /* Sync the state of the HW to the shadow values, because
+                 * the root hub was registered with usbcore a while back, and
+                 * at the very least it probably tried to turn on the power,
+                 * and until now, the otg_device_mask was preventing that. 
+                 */
+                for (i = 0; i < 16; i++) {
+
+                        if (PORT_SYNC_MASK & (0x1 << i) & bus_hcpriv->rh_hcpriv->port_change_status[bus_hcpriv->otg_port - 1]) {
+                                hcd_hw_rh_port_feature(bus_hcpriv, i, bus_hcpriv->otg_port, TRUE);
+                                check_port_status(bus_hcpriv, bus_hcpriv->otg_port, "OTG EN");
+                        }
+                }
+
+                /* "Connect" on OTG port and simulate a port status change interrupt
+                 */
+                bus_hcpriv->rh_hcpriv->port_change_status[bus_hcpriv->otg_port - 1] |= 0x00010001;
+                TRACE_MSG2(HCD, "OUTPUT: RH LOC_SOF_SET port_change_status[%d]: %08x", 
+                                bus_hcpriv->otg_port, bus_hcpriv->rh_hcpriv->port_change_status[bus_hcpriv->otg_port - 1]);
+                check_port_status(bus_hcpriv, bus_hcpriv->otg_port, "SOF SET");
+                break;
+
+        case RESET:
+                //printk(KERN_INFO"%s: RESET\n", __FUNCTION__);
+                TRACE_MSG0(HCD, "RH LOC_SOF RESET");
+
+                check_port_status(bus_hcpriv, bus_hcpriv->otg_port, "SOF RESET - BEFORE");
+
+                // "Disconnect" on OTG port and simulate a port status change interrupt
+                bus_hcpriv->rh_hcpriv->port_change_status[bus_hcpriv->otg_port - 1] &= ~0x00000001;
+                bus_hcpriv->rh_hcpriv->port_change_status[bus_hcpriv->otg_port - 1] |= 0x00010000;
+                TRACE_MSG2(HCD, "OUTPUT: RH LOC_SOF_RESET port_change_status[%d]: %08x",
+                                bus_hcpriv->otg_port, bus_hcpriv->rh_hcpriv->port_change_status[bus_hcpriv->otg_port - 1]);
+
+
+                // Turn off port power.
+                hcd_hw_rh_port_feature(bus_hcpriv, PORT_POWER, bus_hcpriv->otg_port, FALSE);
+                //hcd_hw_rh_port_feature(bus_hcpriv, PORT_ENABLE, bus_hcpriv->otg_port, FALSE);
+
+                check_port_status(bus_hcpriv, bus_hcpriv->otg_port, "SOF RESET - AFTER");
+
+                // Start treating otg capable port as a device
+                bus_hcpriv->rh_hcpriv->otg_device_mask |= (0x1 << (bus_hcpriv->otg_port));
+                break;
+        }
+        
+        bus_hcpriv->rh_hcpriv->hub_port_change_status |= (1 << bus_hcpriv->otg_port);
+        TRACE_MSG0(HCD, "Schedule hcd_rh_portstatus_bh");
+        PREPARE_WORK_ITEM(bus_hcpriv->rh_hcpriv->psc_bh, hcd_rh_portstatus_bh, bus_hcpriv);
+        SCHEDULE_WORK(bus_hcpriv->rh_hcpriv->psc_bh);
+        local_irq_restore(flags);
+}
+
+/* puts OTG capable port into a_host or b_host state - attempt to use port */
+void rh_loc_suspend_func(struct otg_instance *otg, u8 on)
+{
+        struct bus_hcpriv *bus_hcpriv = (struct bus_hcpriv *)(((struct hcd_instance *)(otg->hcd))->privdata);
+        struct rh_hcpriv *rh_hcpriv = bus_hcpriv->rh_hcpriv;
+        int i;
+        unsigned long flags;
+
+        //printk(KERN_INFO"%s: \n", __FUNCTION__);
+        RETURN_UNLESS (bus_hcpriv && bus_hcpriv->rh_hcpriv && bus_hcpriv->rh_hcpriv->port_change_status);
+
+        local_irq_save(flags);
+        switch (on) {
+        case SET:
+                TRACE_MSG0(HCD, "OUTPUT: RH LOC_SUSPEND SET");
+
+                rh_hcpriv->suspended = 1;
+
+                // suspend
+                hcd_hw_rh_port_feature(bus_hcpriv, PORT_SUSPEND, bus_hcpriv->otg_port, TRUE);
+
+                // "Connect" on OTG port and simulate a port status change interrupt
+                bus_hcpriv->rh_hcpriv->port_change_status[bus_hcpriv->otg_port - 1] |= 0x00040004;
+                TRACE_MSG2(HCD, "OUTPUT: RH LOC_SUSPNED_SET port_change_status[%d]: %08x", 
+                                bus_hcpriv->otg_port, bus_hcpriv->rh_hcpriv->port_change_status[bus_hcpriv->otg_port - 1]);
+                check_port_status(bus_hcpriv, bus_hcpriv->otg_port, "LOC_SUSPEND SET");
+                break;
+
+        case RESET:
+
+                TRACE_MSG0(HCD, "OUTPUT: RH LOC_SUSPEND RESET");
+                check_port_status(bus_hcpriv, bus_hcpriv->otg_port, "LOC_SUSPEND RESET - BEFORE");
+
+                // "Disconnect" on OTG port and simulate a port status change interrupt
+                bus_hcpriv->rh_hcpriv->port_change_status[bus_hcpriv->otg_port - 1] &= ~0x00000004;
+                bus_hcpriv->rh_hcpriv->port_change_status[bus_hcpriv->otg_port - 1] |= 0x00040000;
+                TRACE_MSG2(HCD, "OUTPUT: RH LOC_SUSPEND port_change_status[%d]: %08x",
+                                bus_hcpriv->otg_port, bus_hcpriv->rh_hcpriv->port_change_status[bus_hcpriv->otg_port - 1]);
+
+
+                // suspend
+                hcd_hw_rh_port_feature(bus_hcpriv, PORT_SUSPEND, bus_hcpriv->otg_port, FALSE);
+                check_port_status(bus_hcpriv, bus_hcpriv->otg_port, "SOF RESET - AFTER");
+
+                rh_hcpriv->suspended = 0;
+
+                break;
+        }
+
+        bus_hcpriv->rh_hcpriv->hub_port_change_status |= (1 << bus_hcpriv->otg_port);
+        TRACE_MSG0(HCD, "Schedule hcd_rh_portstatus_bh");
+        PREPARE_WORK_ITEM(bus_hcpriv->rh_hcpriv->psc_bh, hcd_rh_portstatus_bh, bus_hcpriv);
+        SCHEDULE_WORK(bus_hcpriv->rh_hcpriv->psc_bh);
+        local_irq_restore(flags);
+}
+
+void rh_suspend_func(struct otg_instance *otg, u8 flag)
+{
+        /* suspends the OTG capable port */
+        //TRACE_MSG0(HCD, "--");
+        switch (flag) {
+        case SET:
+                //printk(KERN_INFO"%s: SET\n", __FUNCTION__);
+                TRACE_MSG0(HCD, "OUTPUT: RH SUSPEND_SET");
+                break;
+        case RESET:
+                //printk(KERN_INFO"%s: RESET\n", __FUNCTION__);
+                TRACE_MSG0(HCD, "OUTPUT: RH SUSPEND_RESET");
+                break;
+        }
+}
+
+/* ********************************************************************************************* */
+
+int hcd_rh_init(struct bus_hcpriv *bus_hcpriv)
+{
+        struct rh_hcpriv *rh_hcpriv;
+        int wIndex;
+        int port_mask;
+
+        #if !defined(OTG_C99)
+        rh_global_init();
+        #endif /* !defined(OTG_C99) */
+
+        //printk(KERN_INFO"%s: \n", __FUNCTION__);
+        RETURN_ZERO_UNLESS (bus_hcpriv);
+        RETURN_ENOMEM_UNLESS ((rh_hcpriv = bus_hcpriv->rh_hcpriv = 
+                                (struct rh_hcpriv *) kmalloc(sizeof(struct rh_hcpriv),GFP_KERNEL)));
+        memset(rh_hcpriv, 0, sizeof(struct rh_hcpriv));
+
+        //printk(KERN_INFO "%s: rh_hcpriv: %08x\n",__FUNCTION__,(u32)(void*)rh_hcpriv);
+        //init_timer(&rh_hcpriv->poll_timer);
+        // Get the number of ports, and which port is OTG capable.
+        
+        //rh_hcpriv->num_ports = hcd_hw_rh_num_ports(bus_hcpriv);
+
+        // Allocate the shadow port_change_status array.
+        UNLESS ((rh_hcpriv->port_change_status = (u32 *)  kmalloc((bus_hcpriv->num_ports*sizeof(u32)),GFP_KERNEL))) {
+                //printk(KERN_ERR "%s: allocation of root hub shadow port data fails\n",__FUNCTION__);
+                kfree(rh_hcpriv);
+                bus_hcpriv->rh_hcpriv = rh_hcpriv = NULL;
+                return -ENOMEM;
+        }
+        memset(rh_hcpriv->port_change_status,0,(bus_hcpriv->num_ports*sizeof(u32)));
+        UNLESS ((rh_hcpriv->dev = usb_alloc_dev(NULL, bus_hcpriv->usb_bus, 0))) {
+                //printk(KERN_ERR "%s: allocation of root hub dev fails\n",__FUNCTION__);
+                kfree(rh_hcpriv->port_change_status);
+                rh_hcpriv->port_change_status = NULL;
+                kfree(rh_hcpriv);
+                bus_hcpriv->rh_hcpriv = rh_hcpriv = NULL;
+                return -ENOMEM;
+        }
+        rh_hcpriv->dev->speed = USB_SPEED_FULL;  // QQSV --- this belongs in the HW dependent portion...
+
+        // Build up USB descriptors
+        build_descriptors(bus_hcpriv);
+
+        // Leave OTG capable port as disabled as possible until OTG core enables it.
+        rh_hcpriv->otg_device_mask = bus_hcpriv->otg_capable_mask;
+
+        for (wIndex = 1; wIndex <= bus_hcpriv->num_ports; wIndex++) {
+                port_mask = (1 << (wIndex - 1));
+                if (port_mask & bus_hcpriv->otg_capable_mask) 
+                        TRACE_MSG1(HCD,"OTG capable port %d", wIndex);
+                else 
+                        TRACE_MSG1(HCD,"Standard port %d", wIndex);
+        }
+        return 0;
+}
+
+void hcd_rh_exit(struct bus_hcpriv *bus_hcpriv)
+{
+        struct rh_hcpriv *rh_hcpriv;
+        RETURN_UNLESS(bus_hcpriv && (rh_hcpriv = bus_hcpriv->rh_hcpriv));
+
+        (void *) hcd_rh_unlink_urb(bus_hcpriv,rh_hcpriv->int_urb);
+
+        if (rh_hcpriv->dev) {
+                usb_put_dev(rh_hcpriv->dev);
+                rh_hcpriv->dev = NULL;
+        }
+        if (rh_hcpriv->port_change_status) {
+                kfree(rh_hcpriv->port_change_status);
+                rh_hcpriv->port_change_status = NULL;
+        }
+        kfree(rh_hcpriv);
+        bus_hcpriv->rh_hcpriv = rh_hcpriv = NULL;
+}
+
diff -uNr linux/drivers/no-otg/ocd/otg-hcd/hcd.c linux/drivers/otg/ocd/otg-hcd/hcd.c
--- linux/drivers/no-otg/ocd/otg-hcd/hcd.c	1970-01-01 01:00:00.000000000 +0100
+++ linux/drivers/otg/ocd/otg-hcd/hcd.c	2006-09-01 21:41:32.000000000 +0200
@@ -0,0 +1,139 @@
+/*
+ * otg/ocd/otg-hcd/hcd.c - OTG Host Controller Driver
+ *
+ *      Copyright (c) 2004 Belcarra
+ *
+ * By: 
+ *      Stuart Lynne <sl@belcara.com>, 
+ *      Tom Rushworth <tbr@belcara.com>, 
+ *      Bruce Balden <balden@belcara.com>
+ *
+ * Notes
+ *
+ * 1. The usbd-bi layer has be re-implemented to re-factor the UDC layer in a way to
+ * simplify implementation of UDC drivers. As much of the complexity of dealing with the middle
+ * layers and buffer (urb) handling is provided for in the common bi layer. 
+ *
+ * 2. TODO The udc interface will be further modified to allow the UDC to export a block of
+ * function pointers for the common bi layer to use. This will allow the common layer to
+ * implement default operations where the UDC does not provide an function. For example many UDC
+ * drivers do not provide full support for cable detection and usb pullup control. If these
+ * routines are not provided the common layer will supply defaults. This eliminates a reasonably
+ * larger amount of effectively unused code from many of the udc drivers.
+ *
+ */
+/*!
+ * @file otg/ocd/otg-hcd/hcd.c
+ * @brief Root Hub for Generic Host Controller Driver
+ *
+ *
+ * This file initializes the low level hardware drivers.
+ *
+ * This is the linux 2.4 version.
+ *
+ * @ingroup OTGHCD
+ */
+
+#include <otg/otg-compat.h>
+
+
+#include <otg/usbp-chap9.h>
+#include <otg/usbp-bus.h>
+#include <otg/otg-trace.h>
+#include <otg/otg-api.h>
+#include <otg/otg-tcd.h>
+#include <otg/otg-hcd.h>
+#include <otg/otg-pcd.h>
+
+/* ************************************************************************************* */
+
+void hcd_en_func (struct otg_instance *otg, u8 flag)
+{
+        //struct hcd_instance *hcd = otg->hcd;
+        TRACE_MSG0(HCD, "--");
+        switch (flag) {
+        case SET:
+                TRACE_MSG0(HCD, "OUTPUT: HCD_EN_SET");
+                break;
+        case RESET:
+                TRACE_MSG0(HCD, "OUTPUT: HCD_EN_RESET");
+                break;
+        }
+}
+
+void hcd_init_func (struct otg_instance *otg, u8 flag)
+{
+        struct hcd_instance *hcd = otg->hcd;
+
+        TRACE_MSG0(HCD, "--");
+        switch (flag) {
+        case SET:
+                TRACE_MSG0(HCD, "OUTPUT: HCD_INIT_SET");
+                otg_event(hcd->otg, HCD_OK, HCD, "HCD_INIT SET HCD_OK");
+                break;
+        case RESET:
+                TRACE_MSG0(HCD, "OUTPUT: HCD_INIT_RESET");
+                otg_event(hcd->otg, HCD_OK, HCD, "HCD_INIT SET HCD_OK");
+                break;
+        }
+}
+
+/* ************************************************************************************* */
+
+void hcd_loc_sof_func (struct otg_instance *otg, u8 flag)
+{
+        //struct hcd_instance *hcd = otg->hcd;
+        TRACE_MSG0(HCD, "--");
+        switch (flag) {
+        case SET:
+                TRACE_MSG0(HCD, "OUTPUT: HCD_LOC_SOF_SET");
+                break;
+        case RESET:
+                TRACE_MSG0(HCD, "OUTPUT: HCD_LOC_SOF_RESET");
+                break;
+        }
+}
+
+void hcd_suspend_func (struct otg_instance *otg, u8 flag)
+{
+        //struct hcd_instance *hcd = otg->hcd;
+        TRACE_MSG0(HCD, "--");
+        switch (flag) {
+        case SET:
+                TRACE_MSG0(HCD, "OUTPUT: HCD_SUSPEND_EN_SET");
+                break;
+        case RESET:
+                TRACE_MSG0(HCD, "OUTPUT: HCD_SUSPEND_EN_RESET");
+                break;
+        }
+}
+
+void hcd_remote_wakeup_en_func (struct otg_instance *otg, u8 flag)
+{
+        //struct hcd_instance *hcd = otg->hcd;
+        TRACE_MSG0(HCD, "--");
+        switch (flag) {
+        case SET:
+                TRACE_MSG0(HCD, "OUTPUT: HCD_REMOTE_WAKEUP_EN_SET");
+                break;
+        case RESET:
+                TRACE_MSG0(HCD, "OUTPUT: HCD_REMOTE_WAKEUP_EN_RESET");
+                break;
+        }
+}
+
+void hcd_hnp_en_func (struct otg_instance *otg, u8 flag)
+{
+        //struct hcd_instance *hcd = otg->hcd;
+        TRACE_MSG0(HCD, "--");
+        switch (flag) {
+        case SET:
+                TRACE_MSG0(HCD, "OUTPUT: HCD_HNP_EN_SET");
+                break;
+        case RESET:
+                TRACE_MSG0(HCD, "OUTPUT: HCD_HNP_EN_RESET");
+                break;
+        }
+}
+
+
diff -uNr linux/drivers/no-otg/ocd/otg-i2c/i2c-l26.c linux/drivers/otg/ocd/otg-i2c/i2c-l26.c
--- linux/drivers/no-otg/ocd/otg-i2c/i2c-l26.c	1970-01-01 01:00:00.000000000 +0100
+++ linux/drivers/otg/ocd/otg-i2c/i2c-l26.c	2006-09-01 21:41:32.000000000 +0200
@@ -0,0 +1,160 @@
+/*
+ * otg/ocd/otg-i2c/i2c-l26.c -- Linux 2.6 I2C access
+ *
+ *      Copyright (c) 2004 Belcarra
+ *
+ * By: 
+ *      Stuart Lynne <sl@lbelcarra.com>, 
+ *      Tom Rushworth <tbr@belcarra.com>, 
+ *      Bruce Balden <balden@belcarra.com>
+ *
+ */
+/*!
+ * @file otg/ocd/otg-i2c/i2c-l26.c
+ * @brief Linux I2C I/O via generic i2c device.
+ *
+ * @ingroup ISP1301TCD
+ */
+
+#include <otg/otg-compat.h>
+
+#include <asm/irq.h>
+#include <asm/system.h>
+#include <asm/io.h>
+#include <linux/i2c.h>
+
+#include <otg/pcd-include.h>
+#include <linux/pci.h>
+#include <otghw/isp1301-hardware.h>
+#include "isp1301.h"
+
+/* ********************************************************************************************* */
+
+/*
+ * N.B. i2c functions must not be called from interrupt handlers
+ */
+
+static struct file *i2c_file;
+static struct i2c_client *omap_i2c_client;
+static int initstate_i2c;
+static int initstate_region;
+#define MAX_I2C 16
+
+#ifdef CONFIG_OMAP_H2
+#define ADAPTOR_NAME "OMAP I2C"
+#endif /* CONFIG_OMAP_H2 */
+#ifdef CONFIG_ARCH_MAINSTONE
+#define ADAPTOR_NAME "PXA-I2C-Adapter"
+#endif /* CONFIG_ARCH_MAINSTONE */
+
+/*! i2c_configure
+ * Attempt to find and open generic i2c device
+ * @return - non-zero for failure
+ */
+int  i2c_configure(char *name, int addr)
+{
+        char filename[20];
+        int tmp;
+
+        RETURN_ZERO_IF(initstate_i2c);
+
+        /*find the I2C driver we need 
+         */
+        for (tmp = 0; tmp < MAX_I2C; tmp++) {
+
+                sprintf(filename, "/dev/i2c/%d", tmp);
+
+                printk(KERN_INFO"%s: %s\n", __FUNCTION__, filename);
+
+                UNLESS (IS_ERR(i2c_file = filp_open(filename, O_RDWR, 0))) {
+
+                        //printk(KERN_INFO"%s: %s found\n", __FUNCTION__, filename);
+
+                        /*found some driver */
+                        omap_i2c_client = (struct i2c_client *)i2c_file->private_data;
+
+                        printk(KERN_INFO"%s: found %s\n", __FUNCTION__, omap_i2c_client->adapter->name);
+                        if (strlen(omap_i2c_client->adapter->name) >= 8) {
+                                if (!strncmp(omap_i2c_client->adapter->name, name, strlen(name)))
+                                        break;  /*we found our driver! */
+                        }
+                        omap_i2c_client = NULL;
+                        filp_close(i2c_file, NULL);
+                }
+                printk(KERN_INFO"%s: %s %d\n", __FUNCTION__, filename, i2c_file);
+        }
+        if (tmp == MAX_I2C) {                           // Nothing found
+                printk(KERN_ERR"%s: cannot find I2C driver", __FUNCTION__);
+                return -ENODEV;
+        }
+        omap_i2c_client->addr = addr;
+        initstate_i2c = 1;
+        return 0;
+}
+
+/*! i2c_close
+ * Close i2c device fd.
+ */
+void  i2c_close(void)
+{
+        if (initstate_i2c)
+                filp_close(i2c_file, NULL);
+        initstate_i2c = 0;
+}
+
+/*! i2c_readb
+ * Read byte from i2c device
+ * @param subaddr
+ */
+u8 i2c_readb(u8 subaddr)
+{
+        u8 buf = 0;
+        i2c_master_send(omap_i2c_client, &subaddr, 1);
+        i2c_master_recv(omap_i2c_client, &buf, 1);
+        TRACE_MSG2(TCD, "addr: %02x buf:  %02x", subaddr, buf);
+        return buf;
+}
+
+/*! i2c_readw
+ * Read word from i2c device
+ * @param subaddr
+ */
+u16 i2c_readw(u8 subaddr)
+{
+        u16 buf = 0;
+        i2c_master_send(omap_i2c_client, &subaddr, 1);
+        i2c_master_recv(omap_i2c_client, (u8 *)&buf, 2);
+        TRACE_MSG2(TCD, "addr: %02x buf:  %04x", subaddr, buf);
+        return buf;
+}
+
+/*! i2c_readl
+ * Read long from i2c device
+ * @param subaddr
+ */
+u32 i2c_readl(u8 subaddr)
+{
+        u32 buf = 0;
+        i2c_master_send(omap_i2c_client, &subaddr, 1);
+        i2c_master_recv(omap_i2c_client, (u8 *)&buf, 4);
+        TRACE_MSG2(TCD, "addr: %02x buf:  %08x", subaddr, buf);
+        return buf;
+}
+
+
+/*! i2c_writeb
+ * Writw byte to i2c device
+ * @param subaddr
+ * @param buf
+ */
+int  i2c_writeb(u8 subaddr, u8 buf)
+{
+        char tmpbuf[2];
+
+        tmpbuf[0] = subaddr;    /*register number */
+        tmpbuf[1] = buf;        /*register data */
+        i2c_master_send(omap_i2c_client, &tmpbuf[0], 2);
+        
+        return 0;
+}
+
diff -uNr linux/drivers/no-otg/ocd/otg-pcd/pcd-init-l24.c linux/drivers/otg/ocd/otg-pcd/pcd-init-l24.c
--- linux/drivers/no-otg/ocd/otg-pcd/pcd-init-l24.c	1970-01-01 01:00:00.000000000 +0100
+++ linux/drivers/otg/ocd/otg-pcd/pcd-init-l24.c	2006-09-01 21:41:32.000000000 +0200
@@ -0,0 +1,119 @@
+/*
+ * otg/ocd/otg-pcd/pcd-init-l24.c - OTG Peripheral Controller Driver Module Initialization
+ *
+ *      Copyright (c) 2004 Belcarra
+ *
+ * By: 
+ *      Stuart Lynne <sl@belcarra.com>, 
+ *      Tom Rushworth <tbr@belcarra.com>, 
+ *      Bruce Balden <balden@belcarra.com>
+ *
+ */
+/*!
+ * @file otg/ocd/otg-pcd/pcd-init-l24.c
+ * @brief PCD only driver init.
+ *
+ *
+ * PCD Initialization
+ *
+ * This file initializes all of the low level hardware drivers for a PCD.
+ *
+ * The Peripheral Controller Driver (pcd) implements the lowest layer of the
+ * USBD stack to send and receive data from the USB actings as a USB
+ * peripheral.
+ *
+ * Notes
+ *
+ * 1. This is the linux 2.4 version.
+ *
+ * TODO
+ *
+ * 1. hook up serial_number_str to pcd.c for use with bus interface layer.
+ *
+ *
+ * @ingroup OTGPCD
+ */
+
+#include <otg/otg-compat.h>
+#include <otg/otg-module.h>
+
+#include <otg/usbp-chap9.h>
+#include <otg/otg-module.h>
+#include <otg/usbp-bus.h>
+
+#include <otg/otg-trace.h>
+#include <otg/otg-api.h>
+#include <otg/otg-tcd.h>
+#include <otg/otg-hcd.h>
+#include <otg/otg-pcd.h>
+
+#ifdef MODULE
+#if LINUX_VERSION_CODE >= KERNEL_VERSION (2,4,17)
+MODULE_LICENSE ("GPL");
+#endif
+MODULE_PARM (serial_number_str, "s");
+MODULE_PARM_DESC (serial_number_str, "Serial Number");
+MODULE_AUTHOR ("sl@belcarra.com, tbr@belcarra.com");
+MODULE_DESCRIPTION ("USB On-The-Go PCD");
+#endif
+char *serial_number_str;
+
+otg_tag_t PCD;
+struct pcd_instance *pcd_instance;
+
+/* ************************************************************************************* */
+
+/* pcd_modexit - module exit or init failure cleanup
+ *
+ * Specifically for each driver:
+ *
+ * 	call ops.mod_exit
+ * 	reset instance address and  ops table address in state machine to NULL
+ * 	invalidate tag
+ */
+static void pcd_modexit (void)
+{
+        struct otg_instance *otg = ocd_instance->otg;
+        printk(KERN_INFO"%s\n", __FUNCTION__);
+        if (otg)
+                otg_exit(otg);
+        if (pcd_ops.mod_exit) pcd_ops.mod_exit();
+        pcd_instance = otg_set_pcd_ops(NULL);
+        PCD = otg_trace_invalidate_tag(PCD);
+}
+
+/* pcd_modinit - linux module initialization
+ *
+ * This needs to initialize the ocd, pcd and tcd drivers.  
+ *
+ * Specifically for each driver:
+ *
+ * 	obtain tag
+ * 	pass ops table address to state machine and get instance address
+ * 	call ops.mod_init
+ *
+ * Note that we automatically provide a default tcd_init if
+ * none is set.
+ */
+static int pcd_modinit (void)
+{
+        printk(KERN_INFO"%s\n", __FUNCTION__);
+
+        #if !defined(OTG_C99)
+        pcd_global_init();
+        #endif /* !defined(OTG_C99) */
+
+        PCD = otg_trace_obtain_tag();
+
+        UNLESS(pcd_ops.pcd_init_func) pcd_ops.pcd_init_func = pcd_init_func;
+        THROW_UNLESS(pcd_instance = otg_set_pcd_ops(&pcd_ops), error);
+        THROW_IF((pcd_ops.mod_init) ? pcd_ops.mod_init() : 0, error);
+
+        CATCH(error) {
+                pcd_modexit();
+                return -EINVAL;
+        }
+        return 0;
+}
+MOD_EXIT(pcd_modexit);
+MOD_INIT(pcd_modinit);
diff -uNr linux/drivers/no-otg/ocd/otg-pcd/pcd-ocd-init-l24.c linux/drivers/otg/ocd/otg-pcd/pcd-ocd-init-l24.c
--- linux/drivers/no-otg/ocd/otg-pcd/pcd-ocd-init-l24.c	1970-01-01 01:00:00.000000000 +0100
+++ linux/drivers/otg/ocd/otg-pcd/pcd-ocd-init-l24.c	2006-09-01 21:41:32.000000000 +0200
@@ -0,0 +1,141 @@
+/*
+ * otg/pcd/pcd-ocd-init-l24.c - OTG Peripheral and OTG Controller Drivers Module Initialization
+ *
+ *      Copyright (c) 2004 Belcarra
+ *
+ * By: 
+ *      Stuart Lynne <sl@belcarra.com>, 
+ *      Tom Rushworth <tbr@belcarra.com>, 
+ *      Bruce Balden <balden@belcarra.com>
+ *
+ */
+/*!
+ * @file otg/ocd/otg-pcd/pcd-ocd-init-l24.c
+ * @brief PCD only driver init.
+ *
+ *
+ * PCD/OCD Initialization
+ *
+ * This file initializes all of the low level hardware drivers for a combined
+ * PCD/OCD module. It does not initialize the TCD module.
+ *
+ * USB Periphirals using this must implement the following:
+ *
+ *      ocd_ops         operations from ocd driver
+ *      pcd_ops         operations from pcd driver
+ *
+ * The OTG Controller driver (ocd) implements any required OTG timers and if
+ * necessary mediates access to and initializes the OTG and/or USB hardware.
+ *
+ * The Peripheral Controller Driver (pcd) implements the lowest layer of the
+ * USBD stack to send and receive data from the USB actings as a USB
+ * peripheral.
+ *
+ * Notes
+ *
+ * 1. This is the linux 2.4 version.
+ *
+ * TODO
+ *
+ * 1. hook up serial_number_str to pcd.c for use with bus interface layer.
+ *
+ * @ingroup OTGPCD
+ */
+
+#include <otg/otg-compat.h>
+#include <otg/otg-module.h>
+
+#include <otg/usbp-chap9.h>
+#include <otg/usbp-bus.h>
+
+#include <otg/otg-trace.h>
+#include <otg/otg-api.h>
+#include <otg/otg-tcd.h>
+#include <otg/otg-hcd.h>
+#include <otg/otg-pcd.h>
+#include <otg/otg-ocd.h>
+
+#ifdef MODULE
+#if LINUX_VERSION_CODE >= KERNEL_VERSION (2,4,17)
+MODULE_LICENSE ("GPL");
+#endif
+MODULE_PARM (serial_number_str, "s");
+MODULE_PARM_DESC (serial_number_str, "Serial Number");
+MODULE_AUTHOR ("sl@belcarra.com, tbr@belcarra.com");
+MODULE_DESCRIPTION ("USB On-The-Go PCD");
+#endif
+char *serial_number_str;
+
+otg_tag_t PCD;
+struct pcd_instance *pcd_instance;
+otg_tag_t OCD;
+struct ocd_instance *ocd_instance;
+
+
+/* ************************************************************************************* */
+
+/* pcd_ocd_modexit - module exit or init failure cleanup
+ *
+ * Specifically for each driver:
+ *
+ * 	call ops.mod_exit
+ * 	reset instance address and  ops table address in state machine to NULL
+ * 	invalidate tag
+ */
+static void pcd_ocd_modexit (void)
+{
+        struct otg_instance *otg = ocd_instance->otg;
+        printk(KERN_INFO"%s\n", __FUNCTION__);
+        if (otg)
+                otg_exit(otg);
+
+        if (pcd_ops.mod_exit) pcd_ops.mod_exit();
+        pcd_instance = otg_set_pcd_ops(NULL);
+        PCD = otg_trace_invalidate_tag(PCD);
+
+        if (ocd_ops.mod_exit) ocd_ops.mod_exit();
+        ocd_instance = otg_set_ocd_ops(NULL);
+        OCD = otg_trace_invalidate_tag(OCD);
+}
+
+/* pcd_ocd_modinit - linux module initialization
+ *
+ * This needs to initialize the ocd, pcd and tcd drivers.  
+ *
+ * Specifically for each driver:
+ *
+ * 	obtain tag
+ * 	pass ops table address to state machine and get instance address
+ * 	call ops.mod_init
+ *
+ * Note that we automatically provide a default tcd_init if
+ * none is set.
+ */
+static int pcd_ocd_modinit (void)
+{
+        printk(KERN_INFO"%s\n", __FUNCTION__);
+
+        #if !defined(OTG_C99)
+        pcd_global_init();
+        #endif /* !defined(OTG_C99) */
+
+        UNLESS(pcd_ops.pcd_init_func) pcd_ops.pcd_init_func = pcd_init_func;
+        PCD = otg_trace_obtain_tag();
+        THROW_UNLESS(pcd_instance = otg_set_pcd_ops(&pcd_ops), error);
+        THROW_IF((pcd_ops.mod_init) ? pcd_ops.mod_init() : 0, error);
+
+        OCD = otg_trace_obtain_tag();
+        THROW_UNLESS(ocd_instance = otg_set_ocd_ops(&ocd_ops), error);
+        THROW_IF((ocd_ops.mod_init) ? ocd_ops.mod_init() : 0, error);
+
+        CATCH(error) {
+                pcd_ocd_modexit();
+                return -EINVAL;
+        }
+        return 0;
+}
+
+MOD_EXIT (pcd_ocd_modexit);
+MOD_INIT (pcd_ocd_modinit);
+
+
diff -uNr linux/drivers/no-otg/ocd/otg-pcd/pcd.c linux/drivers/otg/ocd/otg-pcd/pcd.c
--- linux/drivers/no-otg/ocd/otg-pcd/pcd.c	1970-01-01 01:00:00.000000000 +0100
+++ linux/drivers/otg/ocd/otg-pcd/pcd.c	2006-09-01 21:41:32.000000000 +0200
@@ -0,0 +1,845 @@
+/*
+ * otg/ocd/otg-pcd/pcd.c - OTG Peripheral Controller Driver
+ *
+ *      Copyright (c) 2004 Belcarra
+ *
+ * By: 
+ *      Stuart Lynne <sl@belcarra.com>, 
+ *      Tom Rushworth <tbr@belcarra.com>, 
+ *      Bruce Balden <balden@belcarra.com>
+ *
+ */
+/*!
+ * @file otg/ocd/otg-pcd/pcd.c
+ * @brief PCD only driver init.
+ * Notes
+ *
+ * 1. The pcd file abstracts much of the UDC complexity as possible and
+ * provides a common implementation that is shared to various extents by the
+ * various pcd drivers.
+ *
+ * @ingroup OTGPCD
+ */
+
+#include <otg/pcd-include.h>
+#include <asm/au1000.h>
+
+
+extern char *serial_number_str;
+
+/* ******************************************************************************************* */
+/*! bus_request_endpoints
+ */
+int bus_request_endpoints(struct usbd_bus_instance *bus, struct usbd_endpoint_map *endpoint_map_array, int endpointsRequested, 
+                struct usbd_endpoint_request *requestedEndpoints)
+{
+        struct pcd_instance *pcd = (struct pcd_instance *)bus->privdata;
+        int i;
+        TRACE_MSG0(PCD, "BUS_REQUEST ENDPOINTS:");
+        if (usbd_pcd_ops.request_endpoints(pcd, endpoint_map_array, endpointsRequested, requestedEndpoints)) {
+                printk(KERN_INFO"%s: failed\n", __FUNCTION__);
+                return -EINVAL;
+        }
+        for (i = 0; i < endpointsRequested; i++) {
+                struct usbd_endpoint_map *endpoint_map = endpoint_map_array + i;
+                 TRACE_MSG4(PCD, "address: %02x physical: %02x request: %02x size: %04x", 
+                                 endpoint_map->bEndpointAddress[0], endpoint_map->physicalEndpoint[0], 
+                                 endpoint_map->bmAttributes[0], endpoint_map->wMaxPacketSize[0]);
+        }
+        return 0;
+}
+
+/*! bus_set_endpoints
+ */
+int bus_set_endpoints(struct usbd_bus_instance *bus, int endpointsRequested, struct usbd_endpoint_map *endpoint_map_array)
+{
+        struct pcd_instance *pcd = (struct pcd_instance *)bus->privdata;
+        TRACE_MSG0(PCD, "SET ENDPOINTS");
+        return usbd_pcd_ops.set_endpoints ? usbd_pcd_ops.set_endpoints(pcd, endpointsRequested, endpoint_map_array) : 0;
+}
+
+/*! bus_set_address
+ */
+int bus_set_address(struct usbd_bus_instance *bus, int address)
+{
+        struct pcd_instance *pcd = (struct pcd_instance *)bus->privdata;
+        if (usbd_pcd_ops.set_address) usbd_pcd_ops.set_address (pcd, address);
+        return 0;
+}
+
+/* ******************************************************************************************* */
+
+/*! pcd_search_endpoint_index - find endpoint map index given endpoint address
+ */
+int pcd_search_endpoint_index(struct usbd_bus_instance *bus, int bEndpointAddress)
+{
+        int i;
+        for (i = 0; i < bus->endpoints; i++)
+                BREAK_IF (bus->endpoint_array[i].bEndpointAddress == bEndpointAddress);
+        return i;
+}
+
+/*! pcd_search_endpoint - find endpoint given endpoint address
+ */
+struct usbd_endpoint_instance * pcd_search_endpoint(struct usbd_bus_instance *bus, int bEndpointAddress)
+{
+        int i = pcd_search_endpoint_index(bus, bEndpointAddress);
+        TRACE_MSG2(PCD, "BUS_SEARCH ENDPOINT:  addr: %02x index: %d", bEndpointAddress, i);
+        RETURN_NULL_UNLESS(i < bus->endpoints);
+        return &(bus->endpoint_array[i]);
+}
+
+/*! bus_endpoint_halted - check if endpoint halted
+ * Used by the USB Device Core to check endpoint halt status.
+ *
+ * Return actual halted status if available or'd with endpoint->feature_setting set value.
+ * Assume not halted if udc driver does not provide an endpoint halted function.
+ */
+int bus_endpoint_halted (struct usbd_bus_instance *bus, int bEndpointAddress)
+{
+        struct pcd_instance *pcd = (struct pcd_instance *)bus->privdata;
+        int endpoint_index = pcd_search_endpoint_index(bus, bEndpointAddress);
+        struct usbd_endpoint_instance *endpoint = pcd_search_endpoint(bus, bEndpointAddress);
+        int halted;
+
+        TRACE_MSG1(PCD, "BUS_ENDPOINT HALTED:  endpoint: %p", (int) endpoint);
+        RETURN_EINVAL_UNLESS(endpoint);
+
+        //halted = usbd_pcd_ops.endpoint_halted ? usbd_pcd_ops.endpoint_halted(pcd, endpoint_index) : 0;
+        halted = usbd_pcd_ops.endpoint_halted ? usbd_pcd_ops.endpoint_halted(pcd, endpoint) : 0;
+        TRACE_MSG1(PCD, "BUS_ENDPOINT HALTED: %08x", bus->endpoint_array[endpoint_index].feature_setting);
+
+        return halted || (endpoint->feature_setting & FEATURE(USB_ENDPOINT_HALT)? 1 : 0);
+}
+
+/*! bus_halt_endpoint - handle set/clear feature requests
+ * Used by the USB Device Core to set/clear endpoint halt status.
+ *
+ * We assume that if the udc driver does not implement anything then 
+ * we should just return zero for ok.
+ */
+int bus_halt_endpoint (struct usbd_bus_instance *bus, int bEndpointAddress, int flag)
+{
+        struct pcd_instance *pcd = (struct pcd_instance *)bus->privdata;
+        int endpoint_index = pcd_search_endpoint_index(bus, bEndpointAddress);
+        struct usbd_endpoint_instance *endpoint = pcd_search_endpoint(bus, bEndpointAddress);
+
+        TRACE_MSG1(PCD, "BUS_ENDPOINT HALT:  endpoint: %p", (int) endpoint);
+        RETURN_EINVAL_UNLESS(endpoint);
+        endpoint->feature_setting = flag;
+        return usbd_pcd_ops.halt_endpoint ? usbd_pcd_ops.halt_endpoint(pcd, endpoint, flag) : 0;
+}
+
+/*! pcd_disable_endpoints - disable udc and all endpoints
+ */
+static void pcd_disable_endpoints (struct usbd_bus_instance *bus)
+{
+        struct pcd_instance *pcd = (struct pcd_instance *)bus->privdata;
+        int i;
+        RETURN_IF (!bus || !bus->endpoint_array);
+        for (i = 1; i < usbd_pcd_ops.max_endpoints; i++) {
+                struct usbd_endpoint_instance *endpoint = (bus->endpoint_array + i);
+                CONTINUE_IF (!endpoint);
+                usbd_flush_endpoint (endpoint);
+        }
+}
+
+
+/*! bus_event_handler - handle generic bus event
+ * Called by usb core layer to inform bus of an event.
+ */
+int bus_event_handler (struct usbd_bus_instance *bus, usbd_device_event_t event, int data)
+{
+        struct pcd_instance *pcd = (struct pcd_instance *)bus->privdata;
+        int epn;
+        //struct usbd_endpoint_map *endpoint_map_array = bus->function_instance->endpoint_map_array;
+
+        //TRACE_MSG1(PCD, "BUS_EVENT %x", event);
+        switch (event) {
+        case DEVICE_UNKNOWN:
+                break;
+
+        case DEVICE_INIT:
+                TRACE_MSG0(PCD, "EVENT INIT");
+                break;
+
+        case DEVICE_CREATE:
+                TRACE_MSG0(PCD, "EVENT CREATE");
+                pcd_disable_endpoints (bus);
+                if (usbd_pcd_ops.start) usbd_pcd_ops.start (pcd);
+                break;
+
+        case DEVICE_HUB_CONFIGURED:
+                TRACE_MSG0(PCD, "EVENT HUB_CONFIGURED");
+                //bi_connect (bus, NULL);
+                break;
+
+        case DEVICE_RESET:
+                TRACE_MSG0(PCD, "EVENT RESET");
+
+                if (usbd_pcd_ops.set_address) usbd_pcd_ops.set_address (pcd, 0);
+
+                if (usbd_pcd_ops.reset_ep) usbd_pcd_ops.reset_ep (pcd, 0);
+
+                pcd_disable_endpoints (bus);
+                otg_event_irq(pcd->otg, BUS_RESET | BUS_SUSPENDED_, PCD, "DEVICE_RESET");
+
+                break;
+
+        case DEVICE_ADDRESS_ASSIGNED:
+                TRACE_MSG1(PCD, "EVENT ADDRESSED: %d", data);
+
+                otg_event_irq(pcd->otg, ADDRESSED, PCD, "ADDRESSED");
+
+                break;
+
+        case DEVICE_CONFIGURED:
+                TRACE_MSG0(PCD, "EVENT CONFIGURED");
+                otg_event_irq(pcd->otg, CONFIGURED, PCD, "CONFIGURED");
+                // iterate across the physical endpoint instance array to enable the endpoints
+                if (usbd_pcd_ops.setup_ep)
+                        for (epn = 1; epn < bus->endpoints; epn++) 
+                                usbd_pcd_ops.setup_ep (pcd, epn, bus->endpoint_array + epn);
+                break;
+
+        case DEVICE_DE_CONFIGURED:
+                TRACE_MSG0(PCD, "EVENT DE-CONFIGURED");
+                break;
+
+        case DEVICE_SET_INTERFACE:
+                TRACE_MSG0(PCD, "EVENT SET INTERFACE");
+                break;
+
+        case DEVICE_SET_FEATURE:
+                TRACE_MSG0(PCD, "EVENT SET FEATURE");
+                break;
+
+        case DEVICE_CLEAR_FEATURE:
+                TRACE_MSG0(PCD, "EVENT CLEAR FEATURE");
+                break;
+
+        case DEVICE_BUS_INACTIVE:
+                TRACE_MSG0(PCD, "EVENT INACTIVE");
+                TRACE_MSG1(PCD, "EVENT INACTIVE: state: %x", bus->device_state);
+                otg_event_irq(pcd->otg, BUS_SUSPENDED, PCD, "DEVICE_BUS_INACTIVE");
+                break;
+
+        case DEVICE_BUS_ACTIVITY:
+                TRACE_MSG0(PCD, "EVENT ACTIVITY");
+                otg_event_irq(pcd->otg, BUS_SUSPENDED_, PCD, "DEVICE_BUS_ACTIVITY");
+                break;
+
+        case DEVICE_POWER_INTERRUPTION:
+                TRACE_MSG0(PCD, "POWER INTERRUPTION");
+                break;
+
+        case DEVICE_HUB_RESET:
+                TRACE_MSG0(PCD, "HUB RESET");
+                break;
+
+        case DEVICE_DESTROY:
+                TRACE_MSG0(PCD, "DEVICE DESTROY");
+                //bi_disconnect (bus, NULL);
+                pcd_disable_endpoints (bus);
+                if (usbd_pcd_ops.stop) usbd_pcd_ops.stop (pcd);
+                break;
+
+        case DEVICE_CLOSE:
+                TRACE_MSG0(PCD, "DEVICE CLOSE");
+                break;
+        default:
+                TRACE_MSG1(PCD, "DEVICE UNKNOWN: %d", event);
+                break;
+        }
+        TRACE_MSG2(PCD, "EVENT bNumInterfaces: %x alternates: %x", bus->bNumInterfaces, bus->alternates);
+        return 0;
+}
+
+
+/*! bus_start_endpoint_in
+ */
+int bus_start_endpoint_in (struct usbd_bus_instance *bus, struct usbd_endpoint_instance *endpoint)
+{
+        struct pcd_instance *pcd = (struct pcd_instance *)bus->privdata;
+        unsigned long flags;
+
+        RETURN_ZERO_IF (!endpoint);
+        ocd_ops.interrupts++;
+
+        local_irq_save (flags);
+
+        // call pcd_start_endpoint_in IFF we didn't previously have a tx urb 
+        if (!endpoint->tx_urb && pcd_tx_next_irq (endpoint)) {
+                usbd_pcd_ops.start_endpoint_in (pcd, endpoint);
+        }
+        local_irq_restore (flags);
+        return 0;
+}
+
+/*! bus_start_endpoint_out
+ */
+int bus_start_endpoint_out (struct usbd_bus_instance *bus, struct usbd_endpoint_instance *endpoint)
+{
+        struct pcd_instance *pcd = (struct pcd_instance *)bus->privdata;
+        unsigned long flags;
+
+        RETURN_ZERO_IF (!endpoint);
+        ocd_ops.interrupts++;
+        local_irq_save (flags);
+        // call pcd_start_endpoint_OUT IFF we didn't previously have a rcv urb 
+        if (!endpoint->rcv_urb && pcd_rcv_next_irq (endpoint)) {
+                usbd_pcd_ops.start_endpoint_out (pcd, endpoint);
+        }
+        local_irq_restore (flags);
+        return 0;
+}
+
+
+/*! bus_cancel_urb_irq - cancel sending an urb
+ * Used by the USB Device Core to cancel an urb.
+ */
+int bus_cancel_urb_irq (struct usbd_urb *urb)
+{
+        struct usbd_bus_instance *bus = urb->bus;
+        struct pcd_instance *pcd = (struct pcd_instance *)bus->privdata;
+        RETURN_EINVAL_IF (!urb);
+        TRACE_MSG1(PCD, "BUS_CANCEL URB: %x", (int)urb);
+        switch (urb->endpoint->bEndpointAddress & USB_ENDPOINT_DIR_MASK) {
+        case USB_DIR_IN:
+                // is this the active urb?
+                if (urb->endpoint->tx_urb == urb) {
+                        urb->endpoint->tx_urb = NULL;
+                        TRACE_MSG1(PCD, "CANCEL IN URB: %02x", urb->endpoint->bEndpointAddress);
+                        if (usbd_pcd_ops.cancel_in_irq) usbd_pcd_ops.cancel_in_irq (pcd, urb);
+                }
+                usbd_urb_finished_irq (urb, SEND_CANCELLED);
+                break;
+
+        case USB_DIR_OUT:
+                // is this the active urb?
+                if (urb->endpoint->rcv_urb == urb) {
+                        urb->endpoint->rcv_urb = NULL;
+                        TRACE_MSG1(PCD, "CANCEL OUT URB: %02x", urb->endpoint->bEndpointAddress);
+                        if (usbd_pcd_ops.cancel_out_irq) usbd_pcd_ops.cancel_out_irq (pcd, urb);
+                }
+                TRACE_MSG0(PCD, "CANCEL RECV URB");
+                usbd_urb_finished_irq (urb, RECV_CANCELLED);
+                break;
+        }
+        urb->endpoint->last = urb->endpoint->sent = 0;
+        return 0;
+}
+
+/*! pcd_rcv_finished_irq 
+ */
+struct usbd_urb * pcd_rcv_finished_irq (struct usbd_endpoint_instance *endpoint, int len, int urb_bad)
+{
+        struct usbd_urb *rcv_urb;
+
+        // if we had an urb then update actual_length, dispatch if neccessary
+        if (likely ( (int) (rcv_urb = endpoint->rcv_urb))) {
+                struct usbd_bus_instance *bus = rcv_urb->bus;
+                struct pcd_instance *pcd = (struct pcd_instance *)bus->privdata;
+
+                //TRACE_MSG5(PCD, "BUS_RCV COMPLETE: finished buffer: %x actual: %d len: %d bad: %d: status: %d",
+                //                rcv_urb->buffer, rcv_urb->actual_length, len, urb_bad, rcv_urb->status);
+
+                if (!urb_bad && !endpoint->rcv_error && (rcv_urb->bus->status == USBD_OK)) {
+
+#if 0
+                        int i;
+                        u8 *cp = rcv_urb->buffer;
+                        for (i = 0; i < rcv_urb->actual_length + len; i+= 8) {
+
+                                TRACE_MSG8(PCD, "RECV  %02x %02x %02x %02x %02x %02x %02x %02x",
+                                                cp[i + 0], cp[i + 1], cp[i + 2], cp[i + 3],
+                                                cp[i + 4], cp[i + 5], cp[i + 6], cp[i + 7]
+                                          );
+                        }
+#endif
+                        // increment the received data size
+                        rcv_urb->actual_length += len;
+
+                        endpoint->rcv_urb = NULL;
+                        rcv_urb->jiffies = jiffies;
+                        rcv_urb->framenum = pcd_ops.framenum ? pcd_ops.framenum () : 0;
+                        //TRACE_MSG1(PCD, "BUS_RCV COMPLETE: framenum: %x", (int) rcv_urb->framenum);
+                        usbd_urb_finished_irq (rcv_urb, RECV_OK);
+                        rcv_urb = NULL;
+                }
+                else {
+                        rcv_urb->actual_length = 0;
+                        //endpoint->rcv_error = 1;
+                }
+        }
+
+        // if we don't have an urb see if we can get one
+        return pcd_rcv_next_irq (endpoint);
+}
+
+/*! pcd_rcv_complete_irq 
+ */
+struct usbd_urb * pcd_rcv_complete_irq (struct usbd_endpoint_instance *endpoint, int len, int urb_bad)
+{
+        struct usbd_urb *rcv_urb;
+
+        // if we had an urb then update actual_length, dispatch if neccessary
+        if (likely ( (int) (rcv_urb = endpoint->rcv_urb))) {
+                struct usbd_bus_instance *bus = rcv_urb->bus;
+                struct pcd_instance *pcd = (struct pcd_instance *)bus->privdata;
+
+                TRACE_MSG4(PCD, "BUS_RCV COMPLETE: actual: %d len: %d bad: %d: status: %d",
+                                rcv_urb->actual_length, len, urb_bad, rcv_urb->status);
+
+                //TRACE_MSG4(PCD, "BUS_RCV COMPLETE: request: %d buffer: %d packet: %d transfer: %d",
+                //                rcv_urb->request_length, rcv_urb->buffer_length,
+                //                endpoint->wMaxPacketSize, endpoint->rcv_transferSize);
+
+                // check the urb is ok, are we adding data less than the packetsize
+                if (!urb_bad && !endpoint->rcv_error && (rcv_urb->bus->status == USBD_OK) && (len <= endpoint->wMaxPacketSize)) {
+
+                        // increment the received data size
+                        rcv_urb->actual_length += len;
+
+                        // if the current received data is short (less than full packetsize) which
+                        // indicates the end of the bulk transfer, we have received the maximum
+                        // transfersize, or if we do not have enough room to receive another packet 
+                        // then pass this data up to the function driver
+
+                        // XXX this needs to be fixed, for example the MSC driver 
+                        // has varying maximum sizes
+
+
+                        if (
+                                        ( (len < endpoint->wMaxPacketSize) ||
+                                          (rcv_urb->actual_length >= endpoint->rcv_transferSize) ||
+                                          (rcv_urb->actual_length >= rcv_urb->request_length) ||
+                                          (rcv_urb->actual_length + endpoint->wMaxPacketSize > rcv_urb->buffer_length)))
+                        {
+#if 0
+                                int i;
+                                u8 *cp = rcv_urb->buffer;
+                                for (i = 0; i < rcv_urb->actual_length; i+= 8)
+                                        TRACE_MSG8(PCD, "RECV  %02x %02x %02x %02x %02x %02x %02x %02x",
+                                                        cp[i + 0], cp[i + 1], cp[i + 2], cp[i + 3],
+                                                        cp[i + 4], cp[i + 5], cp[i + 6], cp[i + 7]
+                                                  );
+#endif
+                                endpoint->rcv_urb = NULL;
+                                rcv_urb->jiffies = jiffies;
+                                rcv_urb->framenum = pcd_ops.framenum ? pcd_ops.framenum () : 0;
+                                TRACE_MSG2(PCD, "BUS_RCV COMPLETE: finished length: %d framenum: %x", 
+                                                rcv_urb->actual_length, (int) rcv_urb->framenum);
+                                usbd_urb_finished_irq (rcv_urb, RECV_OK);
+                                rcv_urb = NULL;
+                        }
+                }
+                else {
+                        rcv_urb->actual_length = 0;
+                        //endpoint->rcv_error = 1;
+                }
+        }
+
+        // if we don't have an urb see if we can get one
+        return pcd_rcv_next_irq (endpoint);
+}
+
+/*! pcd_tx_complete_irq 
+ */
+struct usbd_urb * pcd_tx_complete_irq (struct usbd_endpoint_instance *endpoint, int restart)
+{
+        struct usbd_urb *tx_urb;
+
+        TRACE_MSG2(PCD, "tx_urb: %x restart: %d", endpoint->tx_urb, restart);
+
+        // if we have a tx_urb advance or reset, finish if complete
+        if ( (tx_urb = endpoint->tx_urb)) {
+
+                struct usbd_bus_instance *bus = tx_urb->bus;
+                struct pcd_instance *pcd = (struct pcd_instance *)bus->privdata;
+
+                TRACE_MSG5(PCD, "BUS_TX CURRENT TX_URB: %p actual: %d sent: %d last: %d: flags: %d", 
+                                (int)endpoint->tx_urb, tx_urb->actual_length, endpoint->sent,
+                                endpoint->last, tx_urb->flags);
+
+                if (likely (!restart)) {
+                        int sent = endpoint->last;
+                        endpoint->sent += sent;
+                        endpoint->last -= sent;
+                }
+                else {
+                        TRACE_MSG0(PCD, "RESTARTING");
+                        endpoint->last = 0;
+                }
+
+                //if ( ( (tx_urb->actual_length - endpoint->sent) <= 0) && ! (tx_urb->flags & USBD_URB_SENDZLP) ) {
+                
+                if ( (endpoint->sent >= tx_urb->actual_length) && ! (tx_urb->flags & USBD_URB_SENDZLP) ) {
+                        tx_urb->jiffies = jiffies;
+                        tx_urb->framenum = pcd_ops.framenum ? pcd_ops.framenum () : 0;
+                        TRACE_MSG2(PCD, "BUS_TX COMPLETE: finished tx_urb: %p framenum: %x", 
+                                        (int)tx_urb, (int)tx_urb->framenum);
+                        usbd_urb_finished_irq (tx_urb, SEND_FINISHED_OK);
+                        endpoint->tx_urb = NULL;
+                        endpoint->last = endpoint->sent = 0;
+                }
+        }
+        return pcd_tx_next_irq (endpoint);
+}
+
+/* ******************************************************************************************* */
+
+/*! pcd_disable - Stop using the USB Device Controller
+ * Stop using the USB Device Controller.
+ * Shutdown and free dma channels, de-register the interrupt handler.
+ */
+void pcd_disable (struct usbd_bus_instance *bus)
+{
+        struct pcd_instance *pcd = (struct pcd_instance *)bus->privdata;
+        TRACE_MSG0(PCD, "BUS_UDC DISABLE");
+        if (usbd_pcd_ops.disable_ep) usbd_pcd_ops.disable_ep (pcd, 0);
+        pcd_disable_endpoints (bus);
+
+        if (usbd_pcd_ops.disable) usbd_pcd_ops.disable (pcd);
+}
+
+/* ************************************************************************************* */
+/*! bus_framenum 
+ */
+int bus_framenum (void)
+{
+        return (pcd_ops.framenum) ? pcd_ops.framenum () : 0;
+}
+
+/*! bus_interrupts 
+ */
+u64 bus_interrupts (void)
+{
+        return ocd_ops.interrupts;
+}
+
+/*! bus_ticks 
+ */
+u64 bus_ticks (void)
+{
+        return (ocd_ops.ticks) ? ocd_ops.ticks () : 0;
+}
+
+/*! bus_elapsed 
+ */
+u64 bus_elapsed (u64 *t1, u64 *t2)
+{
+        return (ocd_ops.elapsed) ? (ocd_ops.elapsed (t1, t2)) : 0;
+}
+
+/*! pcd_recv_setup_emulate_irq - emulate a device request
+ */
+int pcd_recv_setup_emulate_irq(struct usbd_bus_instance *bus, u8 bmRequestType, u8 bRequest, u16 wValue, u16 wIndex, u16 wLength)
+{
+        struct pcd_instance *pcd = (struct pcd_instance *)bus->privdata;
+        struct usbd_device_request request;
+
+        TRACE_MSG4(PCD, "bmRequestType: %02x bRequest: %02x wValue: %04x wIndex: %04x", bmRequestType, bRequest, wValue, wIndex);
+        request.bmRequestType = bmRequestType;
+        request.bRequest = bRequest;
+        request.wValue = cpu_to_le16(wValue);
+        request.wIndex = cpu_to_le16(wIndex);
+        request.wLength = cpu_to_le16(wLength);
+        return pcd_recv_setup_irq(pcd, &request);
+}
+
+/*! pcd_check_device_feature - verify that feature is set or clear
+ * Check current feature setting and emulate SETUP Request to set or clear
+ * if required.
+ */
+void pcd_check_device_feature(struct usbd_bus_instance *bus, int feature, int flag)
+{
+        int status = bus->device_feature_settings & (1 << feature);
+        TRACE_MSG2(PCD, "BUS_CHECK FEATURE: feature: %x flag: %x", feature, flag);
+        if (!status && flag)
+                pcd_recv_setup_emulate_irq(bus, USB_REQ_HOST2DEVICE, USB_REQ_SET_FEATURE, feature, 0, 0);
+        else if (status && !flag)
+                pcd_recv_setup_emulate_irq(bus, USB_REQ_HOST2DEVICE, USB_REQ_CLEAR_FEATURE, feature, 0, 0);
+        TRACE_MSG1(PCD, "BUS_CHECK FEATURE: features; %08x", bus->device_feature_settings);
+}
+
+/* ******************************************************************************************* */
+#if !defined(OTG_C99)
+struct usbd_bus_operations bus_ops;
+
+void pcd_global_init(void)
+{
+        ZERO(bus_ops);
+        bus_ops.start_endpoint_in = bus_start_endpoint_in;
+        bus_ops.start_endpoint_out = bus_start_endpoint_out;
+        bus_ops.cancel_urb_irq = bus_cancel_urb_irq;
+        bus_ops.endpoint_halted = bus_endpoint_halted;
+        bus_ops.halt_endpoint = bus_halt_endpoint;
+        bus_ops.set_address = bus_set_address;
+        bus_ops.event_handler = bus_event_handler;
+        bus_ops.request_endpoints = bus_request_endpoints;
+        bus_ops.set_endpoints = bus_set_endpoints;
+        bus_ops.framenum = bus_framenum;
+        bus_ops.ticks = bus_ticks;
+        bus_ops.elapsed = bus_elapsed;
+        bus_ops.interrupts = bus_interrupts;
+        //bus_ops.device_feature = bus_device_feature;
+}
+
+#else /* !defined(OTG_C99) */
+struct usbd_bus_operations bus_ops = {
+        .start_endpoint_in = bus_start_endpoint_in,
+        .start_endpoint_out = bus_start_endpoint_out,
+        .cancel_urb_irq = bus_cancel_urb_irq,
+        .endpoint_halted = bus_endpoint_halted,
+        .halt_endpoint = bus_halt_endpoint,
+        .set_address = bus_set_address,
+        .event_handler = bus_event_handler,
+        .request_endpoints = bus_request_endpoints,
+        .set_endpoints = bus_set_endpoints,
+        .framenum = bus_framenum,
+        .ticks = bus_ticks,
+        .elapsed = bus_elapsed,
+        .interrupts = bus_interrupts,
+        //.device_feature = bus_device_feature,
+};
+#endif /* !defined(OTG_C99) */
+
+
+struct usbd_bus_driver bus_driver = {
+        bops: &bus_ops,
+};
+
+
+
+/* ******************************************************************************************* */
+/* Prevent overlapp of bi administrative functions mainly:
+ *      bus_register_bh
+ *      bus_deregister_bh
+ */ 
+//DECLARE_MUTEX (usbd_bi_sem);     
+struct usbd_bus_instance *usbd_bus;
+
+/*! pcd_startup_events
+ */
+void pcd_startup_events(struct usbd_bus_instance *bus)
+{
+        struct pcd_instance *pcd = (struct pcd_instance *)bus->privdata;
+        //TRACE_MSG0(PCD, "BI_STARTUP");
+        if (usbd_pcd_ops.startup_events) {
+                TRACE_MSG0(PCD, "BI_STARTUP_EVENTS - udc");
+                usbd_pcd_ops.startup_events(pcd);
+        }
+        else {
+                TRACE_MSG0(PCD, "BI_STARTUP_EVENTS - default");
+                usbd_bus_event_handler_irq (bus, DEVICE_INIT, 0);
+                usbd_bus_event_handler_irq (bus, DEVICE_CREATE, 0);
+                usbd_bus_event_handler_irq (bus, DEVICE_HUB_CONFIGURED, 0);
+                usbd_bus_event_handler_irq (bus, DEVICE_RESET, 0);
+        }
+        //TRACE_MSG0(PCD, "BI_STARTUP: FINISHED");
+}
+
+/*! bus_register_bh
+ */
+void bus_register_bh(void *arg)
+{
+        struct pcd_instance *pcd = arg;
+        struct otg_instance *otg = pcd->otg;
+        struct usbd_bus_instance *bus;
+        struct usbd_endpoint_instance *endpoint;
+        unsigned long flags;
+
+        TRACE_MSG1(PCD, "BUS_REGISTER_BH: pcd: %p", (int)arg);
+        RETURN_UNLESS(pcd);
+
+        TRACE_MSG0(PCD, "BUS_REGISTER_BH");
+
+        bus_driver.name = usbd_pcd_ops.name;
+        bus_driver.max_endpoints = usbd_pcd_ops.max_endpoints;
+        bus_driver.maxpacketsize = usbd_pcd_ops.ep0_packetsize;
+        bus_driver.capabilities = usbd_pcd_ops.capabilities;
+        bus_driver.bMaxPower = usbd_pcd_ops.bMaxPower;
+        bus_driver.ports = usbd_pcd_ops.ports;
+        // XXX bus_driver.otg_bmAttributes = tcd_ops.bmAttributes;
+        bus_driver.otg_bmAttributes = usbd_pcd_ops.bmAttributes;
+
+        TRACE_MSG1(PCD, "BUS_REGISTER_BH UDC Capabilities: %x", bus_driver.capabilities);
+        // XXX TRACE_MSG1(PCD, "BUS_REGISTER_BH UDC OTG Attributes: %x\n", tcd_ops.bmAttributes);
+        TRACE_MSG1(PCD, "BUS_REGISTER_BH UDC OTG Attributes: %x\n", usbd_pcd_ops.bmAttributes);
+
+        // register this bus interface driver and create the device driver instance
+        if (! (bus = usbd_register_bus (&bus_driver, usbd_pcd_ops.ep0_packetsize))) {
+                TRACE_MSG0(PCD, "BUS_REGISTER_BH: register failed");
+                printk (KERN_INFO "%s: failed\n", __FUNCTION__);
+                pcd_disable (NULL);
+                otg_event(pcd->otg, enable_otg_ | PCD_OK, PCD, "BUS_REGISTER_BH");
+                return;
+        }
+
+        bus->privdata = (void *) pcd;
+        pcd->bus = bus;
+
+        //if (usbd_pcd_ops.serial_init ? usbd_pcd_ops.serial_init (pcd) : -EINVAL) {
+        //
+        //}
+        
+        if (serial_number_str && strlen(serial_number_str)) {
+                char *sp, *dp;
+                int i;
+                printk(KERN_INFO"%s:\n", __FUNCTION__);
+                printk(KERN_INFO"%s: serial_number_str: %s\n", __FUNCTION__, serial_number_str);
+                TRACE_MSG1(PCD, "prm serial_number_str: %s", serial_number_str);
+                for (sp = serial_number_str, dp = otg->serial_number, i = 0;
+                                *sp && (i < (sizeof(otg->serial_number) - 1)); i++, sp++)
+                        if (isxdigit(*sp)) *dp++ = toupper(*sp);
+        }
+
+
+        TRACE_MSG1(PCD, "otg serial_number_str: %s", pcd->otg->serial_number);
+        TRACE_MSG0(PCD, "BUS_REGISTER_BH: usbd_enable_function_irq");
+
+        if (usbd_enable_function_irq (bus, pcd->otg->function_name, pcd->otg->serial_number)) {
+                TRACE_MSG0(PCD, "BUS_REGISTER_BH: enable function failed");
+                printk (KERN_INFO "%s: failed\n", __FUNCTION__);
+                pcd_disable (NULL);
+                otg_event(pcd->otg, enable_otg_ | PCD_OK, PCD, "BUS_REGISTER_BH");
+                return;
+        }
+
+        // setup endpoint zero
+        endpoint = bus->endpoint_array + 0;
+        endpoint->bEndpointAddress = 0;
+
+        //endpoint->tx_attributes = 0;
+        //
+
+        endpoint->wMaxPacketSize = usbd_pcd_ops.ep0_packetsize;
+        endpoint->rcv_transferSize = 4096;       // XXX should this be higher
+        endpoint->wMaxPacketSize = usbd_pcd_ops.ep0_packetsize;
+        if (usbd_pcd_ops.setup_ep) usbd_pcd_ops.setup_ep (pcd, 0, endpoint);
+
+        TRACE_MSG0(PCD, "BUS_REGISTER_BH: startup");
+
+        // hopefully device enumeration will finish this process
+        // XXX should this move to pcd_en?
+        pcd_startup_events (bus);
+        TRACE_MSG0(PCD, "BUS_REGISTER_BH: FINISHED - sending PCD_OK");
+        //MOD_INC_USE_COUNT;
+        otg_event(pcd->otg, PCD_OK, PCD, "BUS_REGISTER_BH PCD_OK");
+}
+
+/*! bus_deregister_bh
+ */
+void bus_deregister_bh(void *arg)
+{
+        struct pcd_instance *pcd = arg;
+        struct usbd_bus_instance *bus;
+        struct bus_data *data;
+        unsigned long flags;
+
+        //TRACE_MSG1(PCD, "BUS_DEREGISTER_BH: pcd: %x", pcd);
+        if (pcd && (bus = pcd->bus) && (usbd_bus_state_enabled == bus->bus_state)) {
+
+                if (usbd_pcd_ops.disable) usbd_pcd_ops.disable (pcd);
+
+                if (bus->device_state != STATE_ATTACHED) {
+                        usbd_bus_event_handler_irq (bus, DEVICE_RESET, 0);
+                        usbd_bus_event_handler_irq (bus, DEVICE_POWER_INTERRUPTION, 0);
+                        usbd_bus_event_handler_irq (bus, DEVICE_HUB_RESET, 0);
+                }
+                usbd_bus_event_handler_irq (bus, DEVICE_DESTROY, 0);
+                pcd_disable_endpoints (bus);
+                pcd_disable (bus);
+
+                usbd_disable_function (bus);
+                bus->bus_state = usbd_bus_state_disabled;
+
+                //TRACE_MSG1(PCD, "%s: BI_SEM UP", (int)__FUNCTION__);
+                //otg_event(pcd->otg, exit_ok, "BUS_DISABLE EXIT_OK");
+
+                //if (bus->serial_number_str)
+                //        lkfree (pcd->bus->serial_number_str);
+
+                usbd_deregister_bus (bus);
+                pcd->bus = NULL;
+
+                TRACE_MSG0(PCD, "BUS_DEREGISTER_BH: FINISHED - sending PCD_OK");
+                //MOD_DEC_USE_COUNT;
+        }
+        otg_event(pcd->otg, PCD_OK, PCD, "BUS_DEREGISTER_BH PCD_OK");
+}
+
+/* ************************************************************************************* */
+
+
+/*! pcd_en_func - enable
+ *
+ * This is called to enable / disable the PCD and USBD stack.
+ */
+void pcd_en_func (struct otg_instance *otg, u8 flag)
+{
+        struct pcd_instance *pcd = otg->pcd;
+        struct usbd_bus_instance *bus = pcd->bus;
+
+        //TRACE_MSG0(PCD, "--");
+        switch (flag) {
+        case SET:
+                TRACE_MSG0(PCD, "PCD_EN: SET");
+#ifdef CONFIG_OTG_DB1550_J15
+                int ret = au_readl(SYS_PINFUNC);
+                au_writel((ret | 0x8000),SYS_PINFUNC);
+                ret = au_readl(SYS_PINFUNC);
+                TRACE_MSG1(PCD, "Set the OTG for device mode (sys_pfunc = %04x)", ret);
+#endif
+                // check if we can see the UDC and register, then enable the function
+                if (usbd_pcd_ops.enable) usbd_pcd_ops.enable (pcd);
+                //BREAK_IF (pcd_enable_irq (bus));
+                //BREAK_IF (usbd_enable_function_irq (bus, otg->function_name));
+
+                // XXX should this move to here
+                // pcd_startup_events (bus);
+                break;
+
+        case RESET:
+                TRACE_MSG0(PCD, "PCD_EN: RESET");
+                usbd_bus_event_handler_irq (bus, DEVICE_RESET, 0);
+                TRACE_MSG0(PCD, "PCD_EN: DESTROY");
+                usbd_bus_event_handler_irq (bus, DEVICE_DESTROY, 0);
+                TRACE_MSG0(PCD, "PCD_EN: FINISHED");
+                // XXX need to set a flag here
+                break;
+        }
+}
+
+/*! pcd_init_func - per peripheral controller common initialization
+ *
+ * This is called to initialize / de-initialize the PCD and USBD stack.
+ *
+ * We start work items to do this.
+ *
+ */
+void pcd_init_func (struct otg_instance *otg, u8 flag)
+{
+        struct pcd_instance *pcd = otg->pcd;
+        struct usbd_bus_instance *bus = pcd->bus;
+        //struct bus_data *data = NULL;
+
+        //printk(KERN_INFO"%s:\n", __FUNCTION__);
+
+        //TRACE_MSG0(PCD, "--");
+        switch (flag) {
+        case SET:
+                TRACE_MSG0(PCD, "PCD_INIT: SET");
+                PREPARE_WORK_ITEM(pcd->bh, bus_register_bh, pcd);
+                SCHEDULE_WORK(pcd->bh);
+                TRACE_MSG0(PCD, "BUS_REGISTER_SCHEDULE: finished");
+                break;
+
+        case RESET:
+                TRACE_MSG0(PCD, "PCD_INIT: RESET");
+                PREPARE_WORK_ITEM(pcd->bh, bus_deregister_bh, pcd);
+                SCHEDULE_WORK(pcd->bh);
+        }
+}
+
diff -uNr linux/drivers/no-otg/ocd/otg-pcd/tr-init-l24.c linux/drivers/otg/ocd/otg-pcd/tr-init-l24.c
--- linux/drivers/no-otg/ocd/otg-pcd/tr-init-l24.c	1970-01-01 01:00:00.000000000 +0100
+++ linux/drivers/otg/ocd/otg-pcd/tr-init-l24.c	2006-09-01 21:41:32.000000000 +0200
@@ -0,0 +1,234 @@
+/*
+ * otg/ocd/otg-pcd/tr-init-l24.c - Traditional Device Peripheral and OTG Controller Drivers Module Initialization
+ *
+ *      Copyright (c) 2004 Belcarra
+ *
+ * By: 
+ *      Stuart Lynne <sl@belcarra.com>, 
+ *      Tom Rushworth <tbr@belcarra.com>, 
+ *      Bruce Balden <balden@belcarra.com>
+ *
+ */
+/*!
+ * @file otg/ocd/otg-pcd/tr-init-l24.c
+ * @brief Traditional device driver init.
+ *
+ * TR OTG PCD/OCD/TCD Initialization
+ *
+ * This file initializes all of the low level hardware drivers for a traditional device.
+ *
+ * Traditional USB Devices must implement the following:
+ *
+ *      ocd_ops         operations from ocd driver
+ *      pcd_ops         operations from pcd driver
+ *      tcd_ops         operations from tcd driver
+ *
+ * The OTG Controller driver (ocd) implements any required OTG timers and if
+ * necessary mediates access to and initializes the OTG and/or USB hardware.
+ *
+ * The Peripheral Controller Driver (pcd) implements the lowest layer of the
+ * USBD stack to send and receive data from the USB actings as a USB
+ * peripheral.
+ *
+ * The Transceiver Controller Driver (tcd) implements control of the OTG or
+ * USB transceiver. At a minimum for a traditional device this should
+ * implement VBUS sensing (sometimes known as cable detect) and where
+ * possible control over the DP+ pullup resistor.
+ *
+ *
+ * Notes
+ *
+ * 1. This is the linux 2.4 version.
+ *
+ * 2. This will optionally do an auto start with TR_INIT if the OCD_CAPBILITIES_AUTO
+ * flag is set.
+ *
+ * TODO
+ *
+ * 1. hook up serial_number_str to pcd.c for use with bus interface layer.
+ *
+ * @ingroup OTGPCD
+ */
+
+#include <otg/otg-compat.h>
+#include <otg/otg-module.h>
+
+#include <otg/usbp-chap9.h>
+#include <otg/usbp-bus.h>
+#include <otg/otg-trace.h>
+#include <otg/otg-api.h>
+#include <otg/otg-tcd.h>
+#include <otg/otg-hcd.h>
+#include <otg/otg-pcd.h>
+#include <otg/otg-ocd.h>
+
+EMBED_LICENSE();
+
+MOD_PARM (serial_number_str, "s");
+MOD_PARM_DESC (serial_number_str, "Serial Number");
+MOD_AUTHOR ("sl@belcarra.com, tbr@belcarra.com");
+MOD_DESCRIPTION ("Traditional USB Device");
+
+char *serial_number_str;
+
+otg_tag_t OCD;
+struct ocd_instance *ocd_instance;
+
+otg_tag_t PCD;
+struct pcd_instance *pcd_instance;
+
+otg_tag_t TCD;
+struct tcd_instance *tcd_instance;
+
+
+/* ************************************************************************************* */
+
+/* tr_ocd_start_timer
+ * Fake - Set or reset timer to interrupt in number of uS (micro-seconds). 
+ * This is only suitable for MN or TR firmware.
+ */
+int tr_ocd_start_timer(struct otg_instance *otg, int usec)
+{
+        otg_event(otg, TMOUT, OCD, "FAKE TMOUT");
+        return 0;
+}
+
+/* ************************************************************************** */
+/* tr_tcd_en_func - used to enable or disable transciever
+ *
+ */
+void tr_tcd_en_func(struct otg_instance *otg, u8 flag)
+{
+        struct pcd_instance *pcd = (struct pcd_instance *) otg->pcd;
+        struct usbd_bus_instance *bus = pcd->bus;
+
+        //printk(KERN_INFO"%s: start\n", __FUNCTION__);
+        TRACE_MSG0(TCD, "--");
+        TRACE_MSG2(TCD, "pcd: %x bus: %x", pcd, pcd->bus);
+
+        switch (flag) {
+        case PULSE:
+        case SET:
+                TRACE_MSG0(TCD, "TR_TCD_EN SET");
+                /* enable clock interrupt */
+                otg_event_set_irq(tcd_instance->otg, 1, 1, B_SESS_VLD, TCD, "B_SESS_VLD");
+                break;
+        case RESET:
+                TRACE_MSG0(TCD, "TR_TCD_EN RESET");
+                /* disable clock interrupt */
+                break;
+        }
+//        TRACE_MSG2(TCD, "ep0 endpoint: %x bEndpointAddress: %02x BBB", bus->ep0->endpoint_map_array->endpoint,
+ //                       bus->ep0->endpoint_map_array->endpoint->bEndpointAddress);
+}
+
+
+/* ************************************************************************** */
+
+/* tr_modexit - This is used as module exit, and as cleanup if modinit fails.
+ *
+ * Specifically for each driver:
+ *
+ * 	call ops.mod_exit
+ * 	reset instance address and  ops table address in state machine to NULL
+ * 	invalidate tag
+ */
+static void tr_modexit (void)
+{
+        struct otg_instance *otg = ocd_instance->otg;
+        printk(KERN_INFO"%s\n", __FUNCTION__);
+
+        TRACE_MSG1(TCD, "otg: %x", otg);
+        if (otg) {
+                TRACE_MSG0(TCD, "Calling otg_exit()");
+                otg_exit(otg);
+                TRACE_MSG0(TCD, "back from otg_exit()");
+        } 
+        else
+                TRACE_MSG0(TCD, "did not call otg_exit()");
+
+#if 0
+        if ((ocd_ops.capabilities & OCD_CAPABILITIES_TR) && (ocd_ops.capabilities & OCD_CAPABILITIES_AUTO)) {
+                printk(KERN_INFO"%s calling otg_admin\n",__FUNCTION__);
+                otg_admin("exit_all", "Traditional Device Auto TR_INIT");
+        }
+#endif
+
+        if (tcd_ops.mod_exit) tcd_ops.mod_exit();
+        if (ocd_ops.mod_exit) ocd_ops.mod_exit();
+        if (pcd_ops.mod_exit) pcd_ops.mod_exit();
+
+        tcd_instance = otg_set_tcd_ops(NULL);
+        ocd_instance = otg_set_ocd_ops(NULL);
+        pcd_instance = otg_set_pcd_ops(NULL);
+
+        TCD = otg_trace_invalidate_tag(TCD);
+        OCD = otg_trace_invalidate_tag(OCD);
+        PCD = otg_trace_invalidate_tag(PCD);
+        printk(KERN_INFO"%s finished\n", __FUNCTION__);
+}
+
+
+/* tr_modinit - linux module initialization
+ *
+ * This needs to initialize the ocd, pcd and tcd drivers.  
+ *
+ * Specifically for each driver:
+ *
+ * 	obtain tag
+ * 	pass ops table address to state machine and get instance address
+ * 	call ops.mod_init
+ *
+ */
+static int tr_modinit (void)
+{
+        struct otg_instance *otg;
+        printk(KERN_INFO"%s\n", __FUNCTION__);
+
+        #if !defined(OTG_C99)
+        pcd_global_init();
+        #endif /* !defined(OTG_C99) */
+
+        /* For each of ocd, pcd and tcd do:
+         * 
+         *      get tag
+         *      update ops table with default functions where possible
+         *      do otg_set_xxx_ops() as appropriate
+         *      call xxx_ops.mod_init as appropriate
+         */
+        
+        OCD = otg_trace_obtain_tag();
+        PCD = otg_trace_obtain_tag();
+        TCD = otg_trace_obtain_tag();
+
+        UNLESS(ocd_ops.start_timer) ocd_ops.start_timer = tr_ocd_start_timer;
+        UNLESS(pcd_ops.pcd_init_func) pcd_ops.pcd_init_func = pcd_init_func;
+        UNLESS(pcd_ops.pcd_en_func) pcd_ops.pcd_en_func = pcd_en_func;
+        UNLESS(tcd_ops.tcd_en_func) tcd_ops.tcd_en_func = tr_tcd_en_func;
+
+        THROW_IF((ocd_ops.mod_init) ? ocd_ops.mod_init() : 0, error);
+        THROW_IF((pcd_ops.mod_init) ? pcd_ops.mod_init() : 0, error);
+        THROW_IF((tcd_ops.mod_init) ? tcd_ops.mod_init() : 0, error);
+
+        THROW_UNLESS(ocd_instance = otg_set_ocd_ops(&ocd_ops), error);
+        THROW_UNLESS(pcd_instance = otg_set_pcd_ops(&pcd_ops), error);
+        THROW_UNLESS(tcd_instance = otg_set_tcd_ops(&tcd_ops), error);
+
+        THROW_UNLESS(ocd_instance && (otg = ocd_instance->otg), error);
+
+        CATCH(error) {
+                tr_modexit();
+                return -EINVAL;
+        }
+        /* Success!
+         */
+        otg_init(otg);
+#if 0
+        otg_start(otg, ocd_ops.capabilities & OCD_CAPABILITIES_TR);
+#endif
+        return 0;
+}
+
+module_init(tr_modinit);
+MOD_EXIT (tr_modexit);
+
diff -uNr linux/drivers/no-otg/ocd/otg-tcd/tcd-init-l24.c linux/drivers/otg/ocd/otg-tcd/tcd-init-l24.c
--- linux/drivers/no-otg/ocd/otg-tcd/tcd-init-l24.c	1970-01-01 01:00:00.000000000 +0100
+++ linux/drivers/otg/ocd/otg-tcd/tcd-init-l24.c	2006-09-01 21:41:33.000000000 +0200
@@ -0,0 +1,95 @@
+/*
+ * otg/ocd/otg-tcd/tcd-init-l24.c - OTG Transceiver Controller Driver Module Initialization
+ *
+ *      Copyright (c) 2004 Belcarra
+ *
+ * By: 
+ *      Stuart Lynne <sl@belcara.com>, 
+ *      Tom Rushworth <tbr@belcara.com>, 
+ *      Bruce Balden <balden@belcara.com>
+ *
+ */
+/*!
+ * @file otg/ocd/otg-tcd/tcd-init-l24.c
+ * @brief PCD only driver init.
+ *
+ * OTG TCD Initialization
+ *
+ * This file initializes the low level hardware drivers.
+ *
+ * This is the linux 2.4 version.
+ *
+ * @ingroup OTGTCD
+ */
+
+#include <otg/otg-compat.h>
+#include <otg/otg-module.h>
+
+#include <otg/usbp-chap9.h>
+#include <otg/usbp-bus.h>
+#include <otg/otg-trace.h>
+#include <otg/otg-api.h>
+#include <otg/otg-tcd.h>
+#include <otg/otg-hcd.h>
+#include <otg/otg-pcd.h>
+
+
+#ifdef MODULE
+#if LINUX_VERSION_CODE >= KERNEL_VERSION (2,4,17)
+MODULE_LICENSE ("GPL");
+#endif
+MODULE_AUTHOR ("sl@belcarra.com, tbr@belcarra.com");
+MODULE_DESCRIPTION ("USB On-The-Go TCD");
+#endif
+
+otg_tag_t TCD;
+
+extern struct tcd_ops tcd_ops;
+struct tcd_instance *tcd_instance;
+
+//extern void db1550_tcd_global_init(void);
+
+/* ************************************************************************************* */
+
+/* tcd_modexit - This is *only* used for drivers compiled and used as a module.
+ */
+static void tcd_modexit (void)
+{
+        struct otg_instance *otg = tcd_instance->otg;
+        //printk(KERN_INFO"%s\n", __FUNCTION__);
+
+        if (otg)
+                otg_exit(otg);
+
+        if (tcd_ops.mod_exit) tcd_ops.mod_exit();
+        tcd_instance = otg_set_tcd_ops(NULL);
+        otg_trace_invalidate_tag(TCD);
+}
+
+/* otg_modinit - linux module initialization
+ *
+ * This needs to initialize the hcd, pcd and tcd drivers. This includes tcd and possibly hcd
+ * for some architectures.
+ *
+ */
+static int tcd_modinit (void)
+{
+	printk(KERN_INFO"Initilize the tcd structure\n");
+//	db1550_tcd_global_init();
+        TCD = otg_trace_obtain_tag();
+        THROW_UNLESS(tcd_instance = otg_set_tcd_ops(&tcd_ops), error);
+        THROW_IF((tcd_ops.mod_init) ? tcd_ops.mod_init() : 0, error);
+        //printk(KERN_INFO"%s finish\n", __FUNCTION__);
+
+        TRACE_MSG0(TCD, "--");
+
+        CATCH(error) {
+                tcd_modexit();
+                return -EINVAL;
+        }
+        return 0;
+}
+
+module_init (tcd_modinit);
+module_exit (tcd_modexit);
+
diff -uNr linux/drivers/no-otg/ocd/otg-tcd/tcd.c linux/drivers/otg/ocd/otg-tcd/tcd.c
--- linux/drivers/no-otg/ocd/otg-tcd/tcd.c	1970-01-01 01:00:00.000000000 +0100
+++ linux/drivers/otg/ocd/otg-tcd/tcd.c	2006-09-01 21:41:33.000000000 +0200
@@ -0,0 +1,55 @@
+/*
+ * otg/ocd/otg-tcd/tcd.c - OTG Transceiver Controller Driver
+ *
+ *      Copyright (c) 2004 Belcarra
+ *
+ * By: 
+ *      Stuart Lynne <sl@belcara.com>, 
+ *      Tom Rushworth <tbr@belcara.com>, 
+ *      Bruce Balden <balden@belcara.com>
+ *
+ */
+/*!
+ * @file otg/ocd/otg-tcd/tcd.c
+ * @brief TCD Support
+ * Notes
+ *
+ * @ingroup OTGTCD
+ */
+
+#include <otg/otg-compat.h>
+
+#include <otg/usbp-chap9.h>
+#include <otg/usbp-bus.h>
+#include <otg/otg-trace.h>
+#include <otg/otg-api.h>
+#include <otg/otg-tcd.h>
+#include <otg/otg-hcd.h>
+#include <otg/otg-pcd.h>
+
+extern struct tcd_ops tcd_ops;
+
+/* ************************************************************************************* */
+void tcd_cable_event_irq (struct otg_instance *otg)
+{
+        TRACE_MSG1(TCD, "TCD_CABLE_EVENT: vbus: %d", tcd_vbus(otg));
+        otg_event(otg, VBUS_VLD, TCD, "CABLE");
+        //otg_b_sess_vld(otg, 1, tcd_vbus (otg), "Cable Event");
+}
+
+void tcd_cable_event (struct otg_instance *otg)
+{
+        unsigned long flags;
+        local_irq_save (flags);
+        tcd_cable_event_irq (otg);
+        local_irq_restore (flags);
+}
+
+/* ************************************************************************************* */
+int tcd_vbus (struct otg_instance *otg)
+{
+        TRACE_MSG0(TCD, "OCD VBUS");
+        return tcd_ops.vbus ? tcd_ops.vbus (otg) : 1;
+}
+
+
diff -uNr linux/drivers/no-otg/otg/hcd-hw.h linux/drivers/otg/otg/hcd-hw.h
--- linux/drivers/no-otg/otg/hcd-hw.h	1970-01-01 01:00:00.000000000 +0100
+++ linux/drivers/otg/otg/hcd-hw.h	2006-09-01 21:41:33.000000000 +0200
@@ -0,0 +1,101 @@
+/*
+ * otg/hcd/hc_xfer_hw.h - Generic transfer level USBOTG aware Host Controller Driver (HCD)
+ *
+ *      Copyright (c) 2004 Belcarra Technologies
+ *
+ * By:
+ *      Tom Rushworth <tbr@belcara.com>,
+ *      Stuart Lynne <sl@belcara.com>,
+ *      Bruce Balden <balden@belcara.com>
+ *
+ */
+/*!
+ * @file otg/otg/hcd-hw.h
+ * @brief Implements Generic Host Controller Driver
+ *
+ * @ingroup OTGHCD
+ */
+#ifndef HC_XFER_HW_H
+#define HC_XFER_HW_H 1
+
+/*===========================================================================*
+ * Functions provided by the hardware specific component (whatever HW it is).
+ * Each HW specific component provides these functions.
+ *===========================================================================*/
+
+/*===========================================================================*
+ * For the generic transfer aware framework.
+ *===========================================================================*/
+
+/*
+ * hcd_hw_max_active_transfers()
+ * Returns: the (constant) maximum number of concurrently active transfers the HW will handle.
+ */
+extern int hcd_hw_max_active_transfers(struct bus_hcpriv *bus_hcpriv);
+
+/*
+ * hcd_hw_start_transfer()
+ * Returns: tid >= 0  when the transfer starts - this is an index in [0..hcd_hw_max_active_transfers]
+ *          -1        when the transfer is invalid,
+ *          -2        when there are no resources for the transfer currently available.
+ */
+extern int hcd_hw_start_transfer(struct bus_hcpriv *bus_hcpriv,
+                                 int len,       // length of data region
+                                 void *data,    // virtual address of data region
+                                 int toggle,    // toggle value to start the xfer with
+                                 int maxps,     // max packet size of endpoint
+                                 int slow,      // 1 ==> slow speed, 0 ==> full speed  QQSV verify: (((urb->pipe) >> 26) & 1)
+                                 int endpoint,  // endpoint number
+                                 int address,   // USB device address
+                                 int pid,       // {PID_OUT, PID_IN, PID_SETUP}
+                                 int format,    // PIPE_{CONTROL,BULK,INTERRUPT,ISOCHRONOUS}
+                                 u32 other);    // Values depending on format
+
+/*
+ * hcd_hw_unlink_urb()
+ * Called in irq_lock, calls back to xhc_transfer_complete().
+ * Returns: 0     when transfer is unlinked, non-zero otherwise.
+ */
+extern int hcd_hw_unlink_urb(struct bus_hcpriv *bus_hcpriv, int tid); // tid is value returned by hcd_hw_start_transfer()
+
+extern u32 hcd_hw_frame_number(struct bus_hcpriv *bus_hcpriv);
+
+extern void hcd_hw_enable_interrupts(struct bus_hcpriv *bus_hcpriv);
+extern void hcd_hw_disable_interrupts(struct bus_hcpriv *bus_hcpriv);
+
+extern void hcd_hw_get_ops(struct bus_hcpriv *bus_hcpriv);
+
+extern int hcd_hw_init(struct bus_hcpriv *bus_hcpriv);
+
+extern void hcd_hw_exit(struct bus_hcpriv *bus_hcpriv);
+
+/*===========================================================================*
+ * For the virtual root hub.
+ *===========================================================================*/
+
+extern int hcd_hw_rh_num_ports(struct bus_hcpriv *bus_hcpriv);
+extern u8 hcd_hw_rh_otg_capable_mask(struct bus_hcpriv *bus_hcpriv);
+
+extern char *hcd_hw_rh_string(struct bus_hcpriv *bus_hcpriv, int strno);
+extern u16 hcd_hw_rh_idVendor(struct bus_hcpriv *bus_hcpriv);
+extern u16 hcd_hw_rh_idProduct(struct bus_hcpriv *bus_hcpriv);
+extern u16 hcd_hw_rh_bcdDevice(struct bus_hcpriv *bus_hcpriv);
+extern u8 hcd_hw_rh_cfg_bmAttributes(struct bus_hcpriv *bus_hcpriv);
+extern u8 hcd_hw_rh_cfg_MaxPower(struct bus_hcpriv *bus_hcpriv);
+extern u16 hcd_hw_rh_hub_attributes(struct bus_hcpriv *bus_hcpriv);
+extern u8 hcd_hw_rh_power_delay(struct bus_hcpriv *bus_hcpriv);
+extern u8 hcd_hw_rh_hub_contr_current(struct bus_hcpriv *bus_hcpriv);
+extern u8 hcd_hw_rh_DeviceRemovable(struct bus_hcpriv *bus_hcpriv);
+extern u8 hcd_hw_rh_PortPwrCtrlMask(struct bus_hcpriv *bus_hcpriv);
+extern u32 hcd_hw_rh_get_hub_change_status(struct bus_hcpriv *bus_hcpriv);
+extern void hcd_hw_rh_hub_feature(struct bus_hcpriv *bus_hcpriv, int feat_selector, int set_flag);
+extern void hcd_hw_hcd_en_func(struct otg_instance *oi, u8 on);
+/*
+ * Note: in the following functions, portnum is 0-origin.
+ * (I.e., valid range is [0..(hcd_hw_rh_num_ports-1)].)
+ */
+extern u32 hcd_hw_rh_get_port_change_status(struct bus_hcpriv *bus_hcpriv, int portnum);
+extern void hcd_hw_rh_port_feature(struct bus_hcpriv *bus_hcpriv, u16 wValue, u16 wIndex, int set_flag);
+
+
+#endif
diff -uNr linux/drivers/no-otg/otg/hcd-l26.h linux/drivers/otg/otg/hcd-l26.h
--- linux/drivers/no-otg/otg/hcd-l26.h	1970-01-01 01:00:00.000000000 +0100
+++ linux/drivers/otg/otg/hcd-l26.h	2006-09-01 21:41:33.000000000 +0200
@@ -0,0 +1,119 @@
+/*
+ * otg/hcd/hc_xfer.h - Generic transfer level USBOTG aware Host Controller Driver (HCD)
+ *
+ *      Copyright (c) 2004 Belcarra Technologies
+ *
+ * By:
+ *      Stuart Lynne <sl@belcara.com>,
+ *      Tom Rushworth <tbr@belcara.com>,
+ *      Bruce Balden <balden@belcara.com>
+ *
+ */
+/*!
+ * @file otg/otg/hcd-l26.h
+ * @brief Implements Linux version of Generic Host Controller driver
+ *
+ * @ingroup OTGHCD
+ */
+#ifndef HC_XFER_H
+#define HC_XFER_H 1
+
+/*===========================================================================*
+ * Data structures and functions provided by the generic transfer aware
+ * host controller framework.
+ *
+ * The generic framework must be linked with a hardware specific component,
+ * whose functions are specified in hc_xfer_hw.h, and a generic root hub
+ * component, whose functions are specified in hc_xfer_rh.h.
+ *
+ * The resulting module provides a table of operations to the usb core layer,
+ * passed to the core layer by a call to usb_alloc_bus().
+ *===========================================================================*/
+
+#define XHC_RH_DESCRIPTORS_SIZE (USB_DT_DEVICE_SIZE+USB_DT_CONFIG_SIZE+USB_DT_INTERFACE_SIZE+USB_DT_ENDPOINT_SIZE+USB_DT_HUB_NONVAR_SIZE+2)
+
+typedef struct rh_hcpriv {
+        struct urb *int_urb;
+        struct usbd_device_descriptor *dd;
+        struct usbd_configuration_descriptor *cd;
+        struct usbd_interface_descriptor *id;
+        struct usbd_endpoint_descriptor *ed;
+        struct hub_descriptor *hd;
+        struct timer_list poll_timer;
+        //int poll_jiffies;
+        struct usb_device *dev;
+        int dev_registered;
+        u32 hub_change_status;   // For shadowing the HW
+        u32 *port_change_status; // An array of [num_ports] values for shadowing the HW
+        u32 hub_port_change_status;
+        struct WORK_STRUCT psc_bh;
+        u8 curr_cfg;
+        u8 curr_itf;
+        //u8 num_ports;
+        u8 otg_device_mask;   // (1 << port_num) bit set iff port_num is acting as a device, not host, and so not usable
+        u8 descriptors[XHC_RH_DESCRIPTORS_SIZE];
+        int suspended;
+} rh_hcpriv_t;
+
+typedef struct bus_hcpriv {          // XFER level Host Controller Info
+        struct usb_bus *usb_bus;
+        void *hw_hci;              // HW specific information for this HC
+        int root_hub_addr;         // Initially 0, set by the virtual root hub when addressed.
+        struct rh_hcpriv *rh_hcpriv;          // Generic root hub info (see hc_xfer_rh.[hc])
+        int terminating;           // Boolean, TRUE if shutting down.
+        //struct hcd_instance *hcd;  // OTG Controller Info
+        int max_active_urbs;
+        struct urb **active_urbs;
+        int device_registered;     // bus_device status
+        struct device bus_device;      // Linux Driver Model info.
+        //struct hcd_ops otg_ops;    // operations for OTG component.
+        struct usb_driver *usb_driver;
+
+        u8 num_ports;
+        u8 otg_port;
+        u8 otg_capable_mask;
+        u16 rh_vendorid;
+        u16 rh_productid;
+        u16 rh_bcddevice;
+        char *rh_serial;
+        char *rh_product;
+        char *rh_manufacturer;
+        u8 rh_bmAttributes;
+        u8 rh_bMaxPower;
+
+        struct usb_device *roothub_dev;
+        struct usb_device *first_dev;
+
+        int max_active_transfers;
+
+} bus_hcpriv_t;
+
+typedef struct dev_hcpriv {      // XFER level HCI per attached device info
+        struct usb_device *dev;
+        struct list_head queued_urbs_both[2][16];
+        u32 num_urbs_both[2][16];
+        u8 epq_state_both[2][16];
+} dev_hcpriv_t;
+
+#define EPQ_EMPTY         0
+#define EPQ_RUNNING       1
+#define EPQ_WAITING       2
+
+/*===========================================================================*
+ * For the hardware specific component.
+ *===========================================================================*/
+
+extern void hcd_transfer_complete(struct bus_hcpriv *bus_hcpriv, int transfer_id, int format,
+                                  int cc, u32 remaining, int next_toggle);
+
+/*===========================================================================*
+ * For the virtual root hub.
+ *===========================================================================*/
+
+extern void hcd_rh_urb_complete(struct bus_hcpriv *bus_hcpriv, struct urb *urb);
+
+#include "../otg/otg-trace.h"
+extern otg_tag_t xfer_hci_trace_tag;
+
+
+#endif
diff -uNr linux/drivers/no-otg/otg/hcd-rh.h linux/drivers/otg/otg/hcd-rh.h
--- linux/drivers/no-otg/otg/hcd-rh.h	1970-01-01 01:00:00.000000000 +0100
+++ linux/drivers/otg/otg/hcd-rh.h	2006-09-01 21:41:33.000000000 +0200
@@ -0,0 +1,55 @@
+/*
+ * otg/hcd/hc_xfer_rh.h - Generic transfer level USBOTG aware Host Controller Driver (HCD)
+ *
+ *      Copyright (c) 2004 Belcarra Technologies
+ *
+ * By:
+ *      Stuart Lynne <sl@belcara.com>,
+ *      Tom Rushworth <tbr@belcara.com>,
+ *      Bruce Balden <balden@belcara.com>
+ *
+ */
+/*!
+ * @file otg/otg/hcd-rh.h
+ * @brief Implements Generic Root Hub function for Generic Host Controller Driver.
+ *
+ * @ingroup OTGHCD
+ */
+#ifndef HC_XFER_RH_H
+#define HC_XFER_RH_H 1
+
+#define XHC_RH_STRING_CONFIGURATION 1
+#define XHC_RH_STRING_INTERFACE     2
+#define XHC_RH_STRING_SERIAL        3
+#define XHC_RH_STRING_PRODUCT       4
+#define XHC_RH_STRING_MANUFACTURER  5
+
+/*===========================================================================*
+ * Functions provided by the generic virtual root hub.
+ *===========================================================================*/
+
+/*===========================================================================*
+ * For the generic transfer aware framework.
+ *===========================================================================*/
+
+/*
+ * hcd_rh_submit_urb()
+ * Returns: values as for the generic submit_urb() function.
+ */
+extern int hcd_rh_submit_urb(struct bus_hcpriv *bus_hcpriv, struct urb *urb, int mem_flags);
+
+extern int hcd_rh_unlink_urb(struct bus_hcpriv *bus_hcpriv, struct urb *urb);
+
+extern void hcd_rh_get_ops(struct bus_hcpriv *bus_hcpriv);
+
+extern int hcd_rh_init(struct bus_hcpriv *bus_hcpriv);
+
+extern void hcd_rh_exit(struct bus_hcpriv *bus_hcpriv);
+
+/*===========================================================================*
+ * For the hardware specific root hub component.
+ *===========================================================================*/
+
+extern irqreturn_t hcd_rh_int_hndlr(int irq, void *dev_id, struct pt_regs *regs);
+
+#endif
diff -uNr linux/drivers/no-otg/otg/otg-api.h linux/drivers/otg/otg/otg-api.h
--- linux/drivers/no-otg/otg/otg-api.h	1970-01-01 01:00:00.000000000 +0100
+++ linux/drivers/otg/otg/otg-api.h	2006-09-01 21:41:33.000000000 +0200
@@ -0,0 +1,233 @@
+/*
+ * otg/otg-api.h
+ *
+ *      Copyright (c) 2004 Belcarra
+ *
+ * By: 
+ *      Stuart Lynne <sl@belcarra.com>, 
+ *      Tom Rushworth <tbr@belcarra.com>, 
+ *      Bruce Balden <balden@belcarra.com>
+ *
+ */
+/*
+ * Doxygen group definitions - N.B. These much each be in
+ * their own separate comment...
+ */
+/*!
+ * @defgroup onthegogroup On-The-Go
+ */
+/*!
+ * @defgroup functiongroup Function Drivers
+ */
+/*!
+ * @defgroup tcdgroup Transceiver Controller Drivers
+ */
+/*!
+ * @defgroup pcdgroup Peripheral Controller Drivers
+ */
+/*!
+ * @defgroup libgroup Architecture Libraries
+ */
+/*!
+ * @defgroup platformgroup Platform Drivers
+ */
+
+
+/*!
+ * @defgroup OTGCore Core
+ * @ingroup onthegogroup
+ */
+
+/*!
+ * @file otg/otg/otg-api.h
+ * @brief Core Defines for USB OTG Core Layaer
+ *
+ * @ingroup OTGCore
+ */
+
+/*!
+ * @name OTGCORE OTG API Definitions
+ * This contains the OTG API structures and definitions.
+ * @{
+ */
+
+#include <otg/otg-fw.h>
+
+#ifdef CONFIG_OTG_FW_MN
+#include <otg/otg-fw-mn.h>
+#else /* CONFIG_OTG_FW_MN */                     
+#include <otg/otg-fw-df.h>
+#endif /* CONFIG_OTG_FW_MN */                     
+
+
+struct otg_instance;
+typedef void (*otg_output_proc_t) (struct otg_instance *, u8);
+
+extern struct otg_firmware *otg_firmware_loaded;
+extern struct otg_firmware *otg_firmware_orig;
+extern struct otg_firmware *otg_firmware_loading;
+
+
+char * otg_get_state_name(int state);
+
+/*!
+ * @struct otg_instance
+ *
+ * This tracks the current OTG configuration and state
+ */
+struct otg_instance {
+
+        u16                     state;                  /*!< current state      */
+        int                     previous;               /*!< previous state     */
+
+        int                     active;                 /*!< used as semaphore  */
+
+        u32                     current_inputs;         /*!< input settings     */
+        u64                     outputs;                /*!< output settings    */
+
+        u64                     tickcount;              /*!< when we transitioned to current state      */
+        u64                     bconn;                  /*!< when b_conn was set        */
+
+        struct otg_state       *current_outputs;       /*!< output table entry for current state        */
+        struct otg_state       *previous_outputs;      /*!< output table entry for current state        */
+
+        otg_output_proc_t       otg_output_ops[MAX_OUTPUTS]; /*!< array of functions mapped to output numbers */
+
+        int                     (*start_timer) (struct otg_instance *, int); /*!< OCD function to start timer */
+        u64                     (*ticks) (void);        /*!< OCD function to return ticks */
+        u64                     (*elapsed) ( u64 *, u64 *); /*!< OCD function to return elapsed ticks */
+
+        void *                  hcd;                    /*!< pointer to hcd instance */
+        void *                  ocd;                    /*!< pointer to ocd instance */
+        void *                  pcd;                    /*!< pointer to pcd instance */
+        void *                  tcd;                    /*!< pointer to tcd instance */
+
+        char                    function_name[OTGADMIN_MAXSTR]; /*!< current function */
+        char                    serial_number[OTGADMIN_MAXSTR]; /*!< current serial number */
+};
+
+typedef int (*otg_event_t) (struct otg_instance *, int, char *);
+
+struct otg_event_info {
+        char *name;
+        otg_event_t event;
+};
+
+
+/* 1 - used by external functions to pass in administrative commands as simple strings
+ */
+extern int otg_status(int, u32, u32, char *, int);
+extern void otg_message(char *);
+                                        
+/* 2 - used internally by any OTG stack component to single event from interrupt context
+ */
+extern int otg_event_irq(struct otg_instance *, u64, otg_tag_t, char *);
+                                        
+/* 3 - used internally by any OTG stack component to single event from non-interrupt context,
+ */
+extern int otg_event(struct otg_instance *, u64, otg_tag_t, char *);
+
+/* 5 - used by PCD/TCD drivers to signal various OTG Transceiver events
+ */
+int otg_event_set_irq(struct otg_instance *, int, int, u32, otg_tag_t, char *);
+
+
+extern void otg_init (struct otg_instance *otg);
+extern void otg_exit (struct otg_instance *otg);
+
+/* message 
+ */
+#if defined(OTG_LINUX)
+extern int otg_message_init_l24(struct otg_instance *);
+extern void otg_message_exit_l24(void);
+#endif /* defined(OTG_LINUX) */
+
+#if defined(OTG_WINCE)
+extern int otg_message_init_w42(struct otg_instance *);
+extern void otg_message_exit_w42(void);
+#endif /* defined(OTG_WINCE) */
+
+extern int otg_message_init(struct otg_instance *);
+extern void otg_message_exit(void);
+
+extern int otg_write_message_irq(char *buf, int size);
+extern int otg_write_message(char *buf, int size);
+extern int otg_read_message(char *buf, int size);
+extern int otg_data_queued(void);
+extern unsigned int otg_message_block(void);
+extern unsigned int otg_message_poll(struct file *, struct poll_table_struct *);
+
+
+/* trace
+ */
+extern int otg_trace_init (void);
+extern void otg_trace_exit (void);
+#if defined(OTG_LINUX)
+extern int otg_trace_init_l24(void);
+extern void otg_trace_exit_l24(void);
+#endif /* defined(OTG_LINUX) */
+
+#if defined(OTG_WINCE)
+extern int otg_trace_init_w42(void);
+extern void otg_trace_exit_w42(void);
+#endif /* defined(OTG_WINCE) */
+
+extern int otgtrace_init (void);
+extern void otgtrace_exit (void);
+extern int otg_trace_proc_read (char *page, int count, int * pos);
+extern int otg_trace_proc_write (const char *buf, int count, int * pos);
+
+/* usbp
+ */
+extern int usbd_device_init (void);
+extern void usbd_device_exit (void);
+
+
+/*
+ * otgcore/otg-mesg.c
+ */
+
+extern void otg_message(char *buf);
+extern void otg_mesg_get_status_update(struct otg_status_update *status_update);
+extern void otg_mesg_get_firmware_info(struct otg_firmware_info *firmware_info);
+extern int otg_mesg_set_firmware_info(struct otg_firmware_info *firmware_info);
+extern struct otg_instance * mesg_otg_instance;
+
+
+
+/*
+ * otgcore/otg.c
+ */
+extern char * otg_get_state_names(int i);
+
+/*
+ * ops
+ */
+
+extern struct hcd_ops *otg_hcd_ops;
+extern struct ocd_ops *otg_ocd_ops;
+extern struct pcd_ops *otg_pcd_ops;
+extern struct tcd_ops *otg_tcd_ops;
+extern struct usbd_ops *otg_usbd_ops;
+
+#define CORE core_trace_tag
+extern otg_tag_t CORE;
+
+extern struct hcd_instance * otg_set_hcd_ops(struct hcd_ops *);
+extern struct ocd_instance * otg_set_ocd_ops(struct ocd_ops *);
+extern struct pcd_instance * otg_set_pcd_ops(struct pcd_ops *);
+extern struct tcd_instance * otg_set_tcd_ops(struct tcd_ops *);
+extern int otg_set_usbd_ops(struct usbd_ops *);
+
+extern void otg_get_trace_info(otg_trace_t *p);
+extern int otg_tmr_id_gnd(void);
+extern u64 otg_tmr_ticks(void);
+extern u64 otg_tmr_elapsed(u64 *t1, u64 *t2);
+extern u16 otg_tmr_framenum(void);
+extern u32 otg_tmr_interrupts(void);
+
+extern void otg_write_info_message(char *msg);
+
+/* @} */
+
+
diff -uNr linux/drivers/no-otg/otg/otg-compat.h linux/drivers/otg/otg/otg-compat.h
--- linux/drivers/no-otg/otg/otg-compat.h	1970-01-01 01:00:00.000000000 +0100
+++ linux/drivers/otg/otg/otg-compat.h	2006-09-01 21:41:33.000000000 +0200
@@ -0,0 +1,73 @@
+/*
+ * otg/otgcore/otg-compat.h
+ *
+ *      Copyright (c) 2004 Belcarra
+ *
+ */
+/*!
+ * @file otg/otg/otg-compat.h
+ * @brief Common include file to determine and include appropriate OS compatibility file.
+ *
+ *
+ * @ingroup OTGCore
+ */
+#ifndef _OTG_COMPAT_H
+#define _OTG_COMPAT_H 1
+
+#if defined(_WIN32_WCE)
+#define OTG_WINCE       _WIN32_WCE
+#endif /* defined(_WIN32_WCE) */
+
+        /* What operating system are we running under? */
+
+        /* Recursively include enough information to determine which release */
+
+        #if (__GNUC__ >=3)
+        #define GCC3
+        #else
+        #define GCC2
+        #endif
+        #include <linux/config.h>
+        #include <linux/kernel.h>
+        #include <linux/version.h>
+
+#if defined(__GNUC__)
+        #define OTG_LINUX
+        #ifndef CONFIG_OTG_NOC99
+        #define OTG_C99
+        #else
+        #endif
+
+        #if LINUX_VERSION_CODE > KERNEL_VERSION(2,5,2)
+        #define LINUX26
+        #elif LINUX_VERSION_CODE > KERNEL_VERSION(2,4,5)
+        #define LINUX24
+        #else /* LINUX_VERSION_CODE > KERNEL_VERSION(2,4,5) */
+        #define LINUX24
+        #define LINUX_OLD
+        #warning "Early unsupported release of Linux kernel"
+        #endif /* LINUX_VERSION_CODE > KERNEL_VERSION(2,4,5) */
+
+        /* We are running under a supported version of Linux */
+        #include <otg/otg-linux.h>
+
+#elif defined(OTG_WINCE)
+
+        /* We are running under a supported version of WinCE */
+        #include <otg/otg-wince.h>
+	#include <otg/otg-wince-ex.h>
+
+#else /* defined(OTG_WINCE) */
+
+        #error "Operating system not recognized"
+
+#endif /* defined(OTG_WINCE) */
+
+#include <otg/otg-utils.h>
+
+#if !defined(OTG_C99)
+#else /* !defined(OTG_C99) */
+#endif /* !defined(OTG_C99) */
+
+
+#endif /* _OTG_COMPAT_H */
diff -uNr linux/drivers/no-otg/otg/otg-fw-df.h linux/drivers/otg/otg/otg-fw-df.h
--- linux/drivers/no-otg/otg/otg-fw-df.h	1970-01-01 01:00:00.000000000 +0100
+++ linux/drivers/otg/otg/otg-fw-df.h	2006-09-01 21:41:33.000000000 +0200
@@ -0,0 +1,53 @@
+
+/* Generated by otg-states-h.awk
+ *
+ * Do not Edit thie file
+ */
+
+/*!
+* @file otg/otg/otg-fw-df.h
+* @brief OTG Firmware - Firmware for df
+*
+* This file defines the OTG State Machine tests.
+*
+* @ingroup OTGFW
+*/
+
+
+/*!
+* @page OTGFW
+* @section OTGFW - otg-fw-df.h
+* This contains the input, output and timout definitions for the OTG state machine firmware
+*/
+extern char     otg_fw_name_df[];
+extern int      otg_test_max_df;
+extern struct otg_test otg_tests_df[];
+extern struct otg_output otg_outputs_df[];
+
+ /*
+  * Copyright (c) 2004 Belcarra
+  *
+  */
+ /*!
+  * This is the default Firmware. It is included in the
+  * compiled modules and supports the auto Traditional USB
+  * mode. No user inputs are implemented.
+  */
+
+#define invalid_state                   0	 /*  State machine has been initialized. */
+
+#define otg_disabled                    1	 /*  State machine has been initialized. */
+
+#define terminator_state                2	 /*  terminator */
+ /*
+  * This is not an OTG State. It is used internally to mark the end of the
+  * list of states and inputs.
+  */
+
+#define OTG_STATES_DF      3
+
+extern struct otg_state otg_states_df[OTG_STATES_DF + 1];
+
+extern char    *otg_get_state_name_df(int);
+
+extern struct otg_firmware otg_firmware_df;
diff -uNr linux/drivers/no-otg/otg/otg-fw-mn.h linux/drivers/otg/otg/otg-fw-mn.h
--- linux/drivers/no-otg/otg/otg-fw-mn.h	1970-01-01 01:00:00.000000000 +0100
+++ linux/drivers/otg/otg/otg-fw-mn.h	2006-09-01 21:41:33.000000000 +0200
@@ -0,0 +1,201 @@
+
+/* Generated by otg-states-h.awk
+ *
+ * Do not Edit thie file
+ */
+
+/*!
+* @file otg/otg/otg-fw-mn.h
+* @brief OTG Firmware - Firmware for mn
+*
+* This file defines the OTG State Machine tests.
+*
+* @ingroup OTGFW
+*/
+
+
+/*!
+* @page OTGFW
+* @section OTGFW - otg-fw-mn.h
+* This contains the input, output and timout definitions for the OTG state machine firmware
+*/
+extern char     otg_fw_name_mn[];
+extern int      otg_test_max_mn;
+extern struct otg_test otg_tests_mn[];
+extern struct otg_output otg_outputs_mn[];
+
+ /*
+  * Copyright (c) 2004 Belcarra
+  */
+ /*!
+  * This is the initialization set for pcd and tcd.
+  */
+
+#define invalid_state                   0	 /*  Un-initialized state. */
+ /*
+  * This the initial state of the software when first loaded.
+  * It is not possible to return to this state.
+  */
+
+#define otg_disabled                    1	 /*  State Machine ready but disabled. */
+ /*
+  * The USBOTG State Machine has been initialized but is inactive.
+  * This state may have arrived at from either the invalid_state or
+  * from the otg_disable state.
+  */
+
+#define otg_disable_tcd                 2	 /*  State Machine waiting for driver de-initialization. */
+ /*
+  * The State Machine stops the device drivers and waits for them
+  * to signal that they have finished de-initializing.
+  *
+  * This state disables the TCD driver and waits for TCD ok.
+  */
+
+#define otg_disable_pcd                 3	 /*  State Machine waiting for driver de-initialization. */
+ /*
+  * The State Machine stops the device drivers and waits for them
+  * to signal that they have finished de-initializing.
+  *
+  * This state disables the PCD driver and waits for PCD ok.
+  */
+
+#define otg_enable_pcd                  4	 /*  State Machine waiting for driver initialization. */
+ /*
+  * The State Machine starts the device drivers and waits for them
+  * to signal that they have finished initializing.
+  *
+  * This state enables the PCD driver and waits for PCD ok.
+  */
+
+#define otg_enable_tcd                  5	 /*  State Machine waiting for driver initialization. */
+ /*
+  * The State Machine starts the device drivers and waits for them
+  * to signal that they have finished initializing.
+  *
+  * This state enables the PCD driver and waits for PCD ok.
+  */
+ /*
+  * Copyright (c) 2004 Belcarra
+  *
+  */
+ /*!
+  * This is the minimal firmware. It can be included in the
+  * compiled modules and supports the auto Traditional USB
+  * mode. No user inputs are required for normal operation.
+  *
+  * The b_bus_drop input can be optionally used to disconnect and re-connect.
+  *
+  * The enable_otg input can be optionally used to disable and re-enable.
+  * Note that disable/enable will reset b_bus_drop.
+  *
+  */
+ /*!
+  * @name Meta State - otg_init
+  * @{
+  */
+
+#define otg_enabled                     6	 /*  State machine has been initialized. */
+ /*!
+  * The State Machine has successfully started the device drivers and
+  * is waiting for an input event. Typically it will move from here to
+  * an idle state specific to the current conditions (a_idle, b_idle, mn_idle etc.)
+  * based on user request b_bus_drop.
+  *
+  */
+ /* @} */
+ /*!
+  * @name Meta State - b_idle
+  * @{
+  */
+
+#define mn_idle                         7	 /*  In idle mode with bus_drop reset. */
+ /*!
+  * The State Machine is in the idle state for a Traditional USB Device.
+  * It is waiting for Vbus to indicate that it has been plugged into a USB Host.
+  *
+  */
+
+#define mn_bus_drop                     8	 /*  In idle mode with bus_drop set. */
+ /*!
+  * The State Machine is in the idle state for a Traditional USB Device.
+  * The B-Device applications has requested that the bus connection be dropped.
+  * Wait for either enable_otg reset or b_bus_drop reset.
+  */
+ /* @} */
+ /*!
+  * @name Meta State - b_peripheral
+  * @{
+  */
+
+#define mn_peripheral                   9	 /*  Signal a connection and wait for packet traffic. */
+ /*!
+  * The State Machine in the peripheral state for a Traditional USB Device.
+  * The D+ pullup is enabled and we are waiting for a BUS_RESET to
+  * indicate that the USB Host has recognized that a USB Device is attached.
+  */
+
+#define mn_bus_reset                   10	 /*  The bus has been reset. */
+ /*!
+  * The State Machine in the bus_reset state for a Traditional USB Device.
+  * It is waiting to be enumerated and configured by the USB Host.
+  */
+
+#define mn_addressed                   11	 /*  The device has been addressed. */
+ /*!
+  * The State Machine in the configured state for a Traditional USB Device.
+  * This means that there is an active session, there is packet traffic
+  * with this device.
+  */
+
+#define mn_configured                  12	 /*  The device has been configured. */
+ /*!
+  * The State Machine in the configured state for a Traditional USB Device.
+  * This means that there is an active session, there is packet traffic
+  * with this device.
+  */
+
+#define mn_discharge                   13	 /*  Discharging Vbus */
+ /*!
+  * The State Machine in the discharge state for a Traditional USB Device.
+  * The device has been unplugged. The Vbus discharge resistor will be enabled
+  * for the TLDISC_DSCHRG time period.
+  */
+ /* @} */
+ /*!
+  * @name Meta State - b_suspended
+  * @{
+  */
+
+#define mn_suspended                   14	 /*  The bus has been suspended */
+ /*!
+  * The State Machine in the suspend state for a Traditional USB Device.
+  */
+
+#define mn_wakeup_enabled              15	 /*  Suspended with REMOTE WAKEUP enabled. */
+ /*!
+  * The State Machine in the suspend state for a Traditional USB Device,
+  * prior to suspended the USB Host enabled Remote Wakeup by sending a
+  * set REMOTE WAKUP request.
+  */
+
+#define mn_wakeup                      16	 /*  Perform REMOTE WAKEUP procedure. */
+ /*!
+  * The State Machine in the wakeup state for a Traditional USB Device,
+  * The REMOTE WAKEUP procedure will be performed.
+  */
+ /* @} */
+
+#define terminator_state               17	 /*  terminator */
+ /*!
+  * This is not an OTG State. It is used internally to mark the end of the
+  * list of states and inputs.
+  */
+
+#define OTG_STATES_MN     18
+
+extern struct otg_state otg_states_mn[OTG_STATES_MN + 1];
+
+extern char    *otg_get_state_name_mn(int);
+
+extern struct otg_firmware otg_firmware_mn;
diff -uNr linux/drivers/no-otg/otg/otg-fw.h linux/drivers/otg/otg/otg-fw.h
--- linux/drivers/no-otg/otg/otg-fw.h	1970-01-01 01:00:00.000000000 +0100
+++ linux/drivers/otg/otg/otg-fw.h	2006-09-01 21:41:33.000000000 +0200
@@ -0,0 +1,1309 @@
+
+/* Generated by otg-h.awk
+ *
+ * Do not Edit, see otg-state.awk
+ */
+
+
+/*!
+* @defgroup OTGFW Firmware
+* @ingroup onthegogroup
+*/
+
+/*!
+* @file otg/otg/otg-fw.h
+* @brief OTG Firmware - Input, Output and Timeout definitions
+* This file defines the OTG State Machine input, output and timeout constants.
+*
+*
+* @ingroup OTGFW
+*/
+
+/*!
+* @name OTGFW
+* @section OTGFW - otg-fw.h
+*/
+
+/*!
+* @name OTGVERSION OTG Version
+* Version information
+* @{
+*/
+
+#define OTG_VERSION_FW 200502152159L
+
+/*! @} */
+
+/*!
+* @name BASIC
+* These are the provided for application layer compatibility.
+* @{
+*/
+#ifdef OTG_APPLICATION
+typedef unsigned char u8;
+typedef unsigned short u16;
+typedef unsigned long u32;
+typedef unsigned long long u64;
+#include <stdio.h>
+#include <sys/ioctl.h>
+#endif	/* OTG_APPLICATION/ */
+
+/*! @} */
+
+/*!
+* @name OTGSTRUCT OTG Structures
+* Basic OTG Structures
+*/
+#define OTGADMIN_MAXSTR      48
+
+/*!
+ * @struct otg_test
+ * 
+ * This defines the tests that allow the state machine to move
+ * from state to state based on input events.
+ * 
+ *  Goto target: if (                               
+ *              (!test1 || (test1 & inputs)) && 
+ *              (!test2 || (test2 & inputs)) && 
+ * 
+ */
+typedef struct otg_test {
+	u16             test;	/*!< Test number */
+	u16             state;	/*!< State Machine State */
+	u32             target;	/*!< Goto this target if             */
+	u64             test1;	/*!< (!test1 || (test1 & inputs)) && */
+	u64             test2;	/*!< (!test2 || (test2 & inputs)) && */
+	u64             test3;	/*!< (!test3 || (test3 & inputs)) && */
+	/* N.B. An empty test is ignored.    */
+} otg_test_t;			/*!<                                 */
+
+
+/*!
+ * @struct otg_input_name
+ * This structure simply allows for a table of input names that can
+ * searched for by input mask
+ */
+typedef struct otg_input_name {
+	u32             value;	/*!< Bit value */
+	char            name[OTGADMIN_MAXSTR];	/*!< Name of state. */
+} otg_input_name_t;
+
+
+/*!
+ * @struct otg_state
+ * This defines an OTG State. Each state has a name, meta state,
+ * timeout, reset and outputs
+ * 
+ * On entry to the state the reset mask is or'd into the current 
+ * input mask, the settings defined in the output mask are used to.
+ * call the required output functions and then if present the timer
+ * is started with the timeout value (in uSec).
+ * 
+ */
+typedef struct otg_state {
+	u16             state;	/*!< State Machine State */
+	u16             meta;	/*!< Meta Machine State */
+	char            name[OTGADMIN_MAXSTR];	/*!< Name of state. */
+	u32             tmout;	/*!< Start timeout if non-zero */
+	u64             reset;	/*!< Or these inputs on entry to state */
+	u64             outputs;	/*!< Or these outputs on entry to state */
+} otg_state_t;
+
+
+/*!
+ * @struct otg_ioctl_name
+ * This structure allows a table for a table of IOCTL event names that can be
+ * searched for by the ioctl command value. It also stores the actual
+ * input mask to set or reset when according to the ioctl arguement.
+ */
+typedef struct otg_ioctl_name {
+	u32             cmd;	/*!< Ioctl cmd */
+	u32             set;	/*!< Signal to set/reset */
+	char           *name;	/*!< Name of ioctl. */
+} otg_ioctl_name_t;
+
+
+/*!
+ * @struct otg_admin_command
+ * This allows for a table of IOCTL admin commands.
+ */
+typedef struct otg_admin_command {
+	int             n;	/*!< Ioctl cmd */
+	char            string[OTGADMIN_MAXSTR];	/*!< Signal to set/reset */
+} otg_admin_command_t;
+
+
+/*!
+ * @struct otg_status_update
+ * This is used by the OTG administrative programs to pass data to and received
+ * data from the OTG State Machine driver.
+ */
+typedef struct otg_status_update {
+	u16             state;	/*!< current state */
+	u16             meta;	/*!< current meta state */
+	u32             inputs;	/*!< current inputs */
+	u64             outputs;	/*!< current outputs */
+	u32             capabilities;	/*!<  */
+	char            fw_name[OTGADMIN_MAXSTR];	/*!< name of firmware */
+	char            state_name[OTGADMIN_MAXSTR];	/*!< name of current state */
+	char            meta_name[OTGADMIN_MAXSTR];	/*!< name of current meta-state */
+	char            function_name[OTGADMIN_MAXSTR];	/*!< currently selected function */
+} otg_status_update_t;
+
+
+/*!
+ * @struct otg_firmware_info
+ * This is used by the OTG Administrative program to pass firmware information
+ * to the OTG State Machine./
+ */
+typedef struct otg_firmware_info {
+	int             number_of_states;	/*!< number of states */
+	int             number_of_tests;	/*!< number of tests */
+	char            fw_name[OTGADMIN_MAXSTR];	/*!< name of firmware */
+} otg_firmware_info_t;
+
+
+/*!
+ * @struct otg_firmware
+ * This is used by the OTG Administrative program to pass firmware
+ * to the OTG State Machine./
+ */
+typedef struct otg_firmware {
+	int             number_of_states;	/*!< number of states */
+	int             number_of_tests;	/*!< number of tests */
+	char            fw_name[OTGADMIN_MAXSTR];	/*!< name of firmware */
+	struct otg_state *otg_states;	/*!< output information */
+	struct otg_test *otg_tests;	/*!< test information */
+} otg_firmware_t;
+
+/*! @} */
+
+/*!
+* @name OTGTIME Time Macros
+* Basic OTG Time Macros
+*/
+
+#define US(n)                   (n)		 /*!< micro-seconds */
+
+#define MS(n)                   (1000 * US(n))	 /*!< milli-seconds */
+
+#define SEC(n)                  (1000 * MS(n))	 /*!< seconds */
+
+/*! @} */
+
+/*!
+* @name OTGOUTPUTM Output Macros
+* OTG Output Macros
+*/
+
+/* OTG State Outputs
+ *
+ * Each state that we transition to defines new settings for each of the defined
+ * outputs. Each output setting can have four settings:
+ *
+ *      
+ *      
+ *      
+ *      
+ */
+#define NC      ((u64)0x0)			 /*!< NC      no change */
+#define SET     ((u64)0x1)			 /*!< SET     the output should be set (i.e. enabled) */
+#define RESET   ((u64)0x2)			 /*!< RESET   the output should be reset (i.e. disabled) */
+#define PULSE   ((u64)0x3)			 /*!< OTHER   special setting, for example used for pulse vbus */
+#define POWER   ((u64)0x3)			 /*!< POWER   */
+
+#define _MASK(n)                (((u64) 1) << n) /*!< Set nth bit in 64 bit mask */
+#define _NOT(n)                 (((u64) n) << 32)	/*!< Set nth+32 bit in 64 bit mask */
+#define _ncmask(n)              (NC)		 /*!< No output change mask */
+#define _setmask(n)             (SET << (n*2))	 /*!< Output set mask */
+#define _resetmask(n)           (RESET << (n*2)) /*!< Output reset mask */
+#define _pulsemask(n)           (PULSE << (n*2)) /*!< Output pulse mask */
+#define _powermask(n)           (POWER << (n*2)) /*!< Output poser mask */
+
+/*! @} */
+
+/*!
+* @name OTGNAMES OTG Names
+* Basic OTG Name Tables
+*/
+
+extern struct otg_input_name otg_input_names[];
+extern struct otg_ioctl_name otg_ioctl_names[];
+extern struct otg_timeouts otg_timeouts[];
+extern struct otg_firmware *otg_firmware_loaded;
+extern char    *otg_output_names[];
+
+
+/*! @} */
+
+/* Generated by otg-inputs-h.awk
+ *
+ * Do not Edit this file.
+ */
+
+
+ /*!
+  * N.B. The OTG State Machine Documentation uses the syntax A and A/
+  * to indicate an event being true or not true. Because a trailing
+  * slash is not legal in C we use A and A_ to indicate true and not true.
+  */
+
+ /*!
+  * @name OTG Transceiver Inputs
+  * These inputs reflect changes seen in the OTG Transceiver. These
+  * what are available in most common OTG transceivers, for example
+  * the ISP1301 or MAX3353.
+  *
+  * The following are typical Vbus comparators.
+  *
+  * ISP1301 MAX3353E
+  * B-Session end threshold 0.2V-0.8V 0.5V
+  * Session Valid Comparator 0.8V-2.0V 1.4V
+  * B-Session Valid 2.0V-4.0V N/A
+  * A-Device Vbus Valid Comparator 4.4V 4.6V
+  *
+  * @{
+  */
+#define ID_GND                         _MASK( 0) /*!< otg_ok -    ID is grounded */
+#define ID_GND_                        _NOT(ID_GND)
+#define ID_FLOAT                       _MASK( 1) /*!< otg_ok -    ID is floating */
+#define ID_FLOAT_                      _NOT(ID_FLOAT)
+#define DP_HIGH                        _MASK( 2) /*!< otg_ok -    DP is pulled high */
+#define DP_HIGH_                       _NOT(DP_HIGH)
+#define DM_HIGH                        _MASK( 3) /*!< otg_ok -    DM pullup is pulled high */
+#define DM_HIGH_                       _NOT(DM_HIGH)
+#define B_SESS_END                     _MASK( 4) /*!< otg_ok -    Vbus less than B-Session end thresshold */
+#define B_SESS_END_                    _NOT(B_SESS_END)
+#define A_SESS_VLD                     _MASK( 5) /*!< otg_ok -    Vbus greater than Session valid threshold */
+#define A_SESS_VLD_                    _NOT(A_SESS_VLD)
+#define B_SESS_VLD                     _MASK( 6) /*!< otg_ok -    Vbus greater than B-Session Valid threshold */
+#define B_SESS_VLD_                    _NOT(B_SESS_VLD)
+#define VBUS_VLD                       _MASK( 7) /*!< otg_ok -    Vbus greater than A-Device Vbus Valid threshold */
+#define VBUS_VLD_                      _NOT(VBUS_VLD)
+#define SRP_DET                        _MASK( 8) /*!< a_idle -    SRP Detected (Vbus pulsed) */
+#define SRP_DET_                       _NOT(SRP_DET)
+#define SE0_DET                        _MASK( 9) /*!< b_idle -    SE0 Detected (Single Ended Zeros, DM and DP both low) */
+#define SE0_DET_                       _NOT(SE0_DET)
+#define SE1_DET                        _MASK(10) /*!< b_idle -    SE1 Detected (Single Ended Ones, DM and DP both high) */
+#define SE1_DET_                       _NOT(SE1_DET)
+#define BDIS_ACON                      _MASK(10) /*!< a_hnp_wait -    Auto DP pullup high after B-disconnect */
+#define BDIS_ACON_                     _NOT(BDIS_ACON)
+#define CR_INT_DET                     _MASK(10) /*!< ph_audio -    0.4V < DP < 0.6V */
+#define CR_INT_DET_                    _NOT(CR_INT_DET)
+ /* @} */
+ /*!
+  * @name Peripheral and Host driver signals.
+  *
+  *
+  * @{
+  */
+#define HUB_PORT_CONNECT               _MASK(12) /*!< otg_both -    Port Connection */
+#define HUB_PORT_CONNECT_              _NOT(HUB_PORT_CONNECT)
+#define BUS_RESET                      _MASK(13) /*!< otg_both -    Bus has reset. */
+#define BUS_RESET_                     _NOT(BUS_RESET)
+#define ADDRESSED                      _MASK(14) /*!< otg_both -    Device has been addressed (received SET ADDRESS request.) */
+#define ADDRESSED_                     _NOT(ADDRESSED)
+#define CONFIGURED                     _MASK(15) /*!< otg_both -    Device has been configured (received SET CONFIG request.) */
+#define CONFIGURED_                    _NOT(CONFIGURED)
+#define NOT_SUPPORTED                  _MASK(16) /*!< otg_both -    Peripheral not supported (no class driver.) */
+#define NOT_SUPPORTED_                 _NOT(NOT_SUPPORTED)
+#define BUS_SUSPENDED                  _MASK(17) /*!< otg_both -    Bus has been suspended. */
+#define BUS_SUSPENDED_                 _NOT(BUS_SUSPENDED)
+ /* @} */
+
+ /*!
+  * @name Administrative Policy Inputs
+  * These are only valid in the state indicated and
+  * can be enabled or disabled.
+  *
+  * @{
+  */
+#define a_bcon_no_tmout_req            _MASK(18) /*!< otg_host -    Application on A-host wants Ta_wait_bcon timeout disabled (non-OTG mode). */
+#define a_bcon_no_tmout_req_           _NOT(a_bcon_no_tmout_req)
+#define a_hpwr_req                     _MASK(19) /*!< otg_host -    Application on A-host wants external charge pump enabled. */
+#define a_hpwr_req_                    _NOT(a_hpwr_req)
+#define bus_drop                       _MASK(20) /*!< otg_ok -    Application on Device needs to power down bus. */
+#define bus_drop_                      _NOT(bus_drop)
+#define a_bus_drop                     _MASK(20) /*!< otg_ok -    Application on A-Device needs to power down bus. */
+#define a_bus_drop_                    _NOT(a_bus_drop)
+#define b_bus_drop                     _MASK(20) /*!< otg_ok -    Application on B-Device needs to disconnect from bus. */
+#define b_bus_drop_                    _NOT(b_bus_drop)
+#define bus_req                        _MASK(21) /*!< otg_ok -    Application on Device wants to use the bus. */
+#define bus_req_                       _NOT(bus_req)
+#define a_bus_req                      _MASK(21) /*!< otg_ok -    Application on A-Device wants to act as host */
+#define a_bus_req_                     _NOT(a_bus_req)
+#define b_bus_req                      _MASK(21) /*!< otg_ok -    Application on B-Device wants to act as host */
+#define b_bus_req_                     _NOT(b_bus_req)
+#define b_sess_req                     _MASK(21) /*!< otg_ok -    Application on B-Device to perform SRP (alias for b_srp_req.) */
+#define b_sess_req_                    _NOT(b_sess_req)
+#define suspend_req                    _MASK(22) /*!< otg_host -    Application on Device requests bus be suspended (alias for a_bus_req/.) */
+#define suspend_req_                   _NOT(suspend_req)
+#define a_suspend_req                  _MASK(22) /*!< otg_host -    Application on A-host requests bus be suspended (alias for a_bus_req/.) */
+#define a_suspend_req_                 _NOT(a_suspend_req)
+#define b_suspend_req                  _MASK(22) /*!< otg_host -    Application on B-host requests bus be suspended (alias for b_bus_req/.) */
+#define b_suspend_req_                 _NOT(b_suspend_req)
+ /* @} */
+
+ /*!
+  * @name Administrative Action Inputs
+  * These are only valid in state indicated. The
+  * state machine tests must reset in states
+  * prior and after use.
+  * @{
+  */
+#define set_remote_wakeup_cmd          _MASK(24) /*!< a_host -    A-Device will send Remote Wakeup Enable Request */
+#define set_remote_wakeup_cmd_         _NOT(set_remote_wakeup_cmd)
+#define remote_wakeup_cmd              _MASK(24) /*!< tr_configured -    B-Device will perform Remote Wakeup. */
+#define remote_wakeup_cmd_             _NOT(remote_wakeup_cmd)
+#define reset_remote_wakeup_cmd        _MASK(25) /*!< a_host -    A-Device will send Remote Wakeup Disable Request */
+#define reset_remote_wakeup_cmd_       _NOT(reset_remote_wakeup_cmd)
+#define clr_err_cmd                    _MASK(25) /*!< a_vbus_err -    A-Device ill clears Vbus overcurrent error. */
+#define clr_err_cmd_                   _NOT(clr_err_cmd)
+#define b_hnp_cmd                      _MASK(25) /*!< b_configured -    B-Device will attempt HNP */
+#define b_hnp_cmd_                     _NOT(b_hnp_cmd)
+#define ph_int_cmd                     _MASK(25) /*!< ph_audio -    B-Device will request Carkit interrupt */
+#define ph_int_cmd_                    _NOT(ph_int_cmd)
+#define ph_audio_cmd                   _MASK(25) /*!< ph_uart -    Application on B-Device connected to Carkit requests audio mode. */
+#define ph_audio_cmd_                  _NOT(ph_audio_cmd)
+#define cr_int_cmd                     _MASK(25) /*!< cr_aud -    Application on A-Device wants to emulate Carkit */
+#define cr_int_cmd_                    _NOT(cr_int_cmd)
+#define led_on_cmd                     _MASK(26) /*!< ph_uart -    B-Device will enable Carkit LED */
+#define led_on_cmd_                    _NOT(led_on_cmd)
+#define led_off_cmd                    _MASK(27) /*!< ph_uart -    B-Device will disable Carkit LED */
+#define led_off_cmd_                   _NOT(led_off_cmd)
+ /* @} */
+
+ /*!
+  * @name Internal State
+  * Used to track status changes.
+  * @{
+  */
+#define HNP_ENABLED                    _MASK(27) /*!< b_configured -    B-HNP Enable Request sent (a-host) or received (b-peripheral). */
+#define HNP_ENABLED_                   _NOT(HNP_ENABLED)
+#define HNP_CAPABLE                    _MASK(28) /*!< otg_both -    B-host Peripheral may do HNP */
+#define HNP_CAPABLE_                   _NOT(HNP_CAPABLE)
+#define HNP_SUPPORTED                  _MASK(28) /*!< otg_both -    B-host can do HNP (A-Host Received HNP Supported SET FEATURE) */
+#define HNP_SUPPORTED_                 _NOT(HNP_SUPPORTED)
+#define REMOTE_WAKEUP_ENABLED          _MASK(29) /*!< otg_configured -    Remote Wakeup Enable Request received. */
+#define REMOTE_WAKEUP_ENABLED_         _NOT(REMOTE_WAKEUP_ENABLED)
+#define REMOTE_CAPABLE                 _MASK(29) /*!< otg_both -    Peripheral can do remote wakeup */
+#define REMOTE_CAPABLE_                _NOT(REMOTE_CAPABLE)
+ /* @} */
+
+ /*!
+  * @name Global Administration
+  * @{
+  */
+ /* @} */
+
+ /*!
+  * @name Driver Initialization Finished signals
+  * @{
+  */
+#define PCD_OK                         _MASK(30) /*!< otg_driver -    PCD Driver Initialization Finished. */
+#define PCD_OK_                        _NOT(PCD_OK)
+#define TCD_OK                         _MASK(30) /*!< otg_driver -    TCD Driver Initialization Finished. */
+#define TCD_OK_                        _NOT(TCD_OK)
+#define HCD_OK                         _MASK(30) /*!< otg_driver -    HCD Driver Initialization Finished. */
+#define HCD_OK_                        _NOT(HCD_OK)
+#define OCD_OK                         _MASK(30) /*!< otg_driver -    OCD Driver Initialization Finished. */
+#define OCD_OK_                        _NOT(OCD_OK)
+ /* @} */
+
+ /*!
+  * @name Timeout and enable
+  * @{
+  */
+#define TMOUT                          _MASK(30) /*!< otg_all -    Generic Timeout */
+#define TMOUT_                         _NOT(TMOUT)
+#define enable_otg                     _MASK(31) /*!< otg_all -    Move State Machine otg_disabled state. */
+#define enable_otg_                    _NOT(enable_otg)
+#define AUTO                           _MASK(31) /*!< otg_all -    Auto Return (enable_otg only true when active) */
+#define AUTO_                          _NOT(AUTO)
+ /* @} */
+
+ /*! @name Timeouts C.f. OTG Table 5-2 A-Device Timing
+  * @{
+  */
+#define Ta_wait_vrise                  _MASK(30) /*!< a_wait_vrise -     Wait for Vbus Rise */
+#define Ta_wait_vrise_                 _NOT(Ta_wait_vrise)
+#define TA_WAIT_VRISE                  MS(100)
+#define Ta_wait_vrise_200              _MASK(30) /*!< a_wait_vrise -     Wait for Vbus Rise */
+#define Ta_wait_vrise_200_             _NOT(Ta_wait_vrise_200)
+#define TA_WAIT_VRISE_200              MS(200)
+#define Ta_wait_vrise_400              _MASK(30) /*!< a_wait_vrise -     Wait for Vbus Rise */
+#define Ta_wait_vrise_400_             _NOT(Ta_wait_vrise_400)
+#define TA_WAIT_VRISE_400              MS(400)
+#define Ta_wait_vrise_500              _MASK(30) /*!< a_wait_vrise -     Wait for Vbus Rise */
+#define Ta_wait_vrise_500_             _NOT(Ta_wait_vrise_500)
+#define TA_WAIT_VRISE_500              MS(500)
+#define Ta_wait_vrise_800              _MASK(30) /*!< a_wait_vrise -     Wait for Vbus Rise */
+#define Ta_wait_vrise_800_             _NOT(Ta_wait_vrise_800)
+#define TA_WAIT_VRISE_800              MS(800)
+#define Ta_bcon_ldb                    _MASK(30) /*!< a_bcon_ldb -     B-Connect Long Debounce */
+#define Ta_bcon_ldb_                   _NOT(Ta_bcon_ldb)
+#define TA_BCON_LDB                    MS(100)
+#define Ta_wait_bcon                   _MASK(30) /*!< a_wait_bcon -     Wait for 1 second for B-Connect */
+#define Ta_wait_bcon_                  _NOT(Ta_wait_bcon)
+#define TA_WAIT_BCON                   SEC(1)
+#define Ta_wait_bcon_5                 _MASK(30) /*!< a_wait_bcon -     Wait for 5 second for B-Connect */
+#define Ta_wait_bcon_5_                _NOT(Ta_wait_bcon_5)
+#define TA_WAIT_BCON_5                 SEC(5)
+#define Ta_wait_bcon_10                _MASK(30) /*!< a_wait_bcon -     Wait for 10 second for B-Connect */
+#define Ta_wait_bcon_10_               _NOT(Ta_wait_bcon_10)
+#define TA_WAIT_BCON_10                SEC(10)
+#define Ta_aidl_bdis                   _MASK(30) /*!< a_hnp_wait -     A-Idle to B-Disconnect */
+#define Ta_aidl_bdis_                  _NOT(Ta_aidl_bdis)
+#define TA_AIDL_BDIS                   MS(200)
+#define Ta_bdis_acon                   _MASK(30) /*!< a_suspend -     B-disconnect to A-Connect */
+#define Ta_bdis_acon_                  _NOT(Ta_bdis_acon)
+#define TA_BDIS_ACON                   MS(3)
+#define Ta_bidl_adis_min               _MASK(30) /*!< a_peripheral -     B-Idle to A-Disconnect minimum (TODO) */
+#define Ta_bidl_adis_min_              _NOT(Ta_bidl_adis_min)
+#define TA_BIDL_ADIS_MIN               MS(3)
+#define Ta_bcon_sdb                    _MASK(30) /*!< a_bcon_sdb -     B-Connect Short Debounce */
+#define Ta_bcon_sdb_                   _NOT(Ta_bcon_sdb)
+#define TA_BCON_SDB                    US(2)
+#define Ta_bcon_sdb_win                _MASK(30) /*!< a_bcon_win -     B-Connect Short Debounce Window */
+#define Ta_bcon_sdb_win_               _NOT(Ta_bcon_sdb_win)
+#define TA_BCON_SDB_WIN                MS(100)
+ /* @} */
+
+ /*! @name Timeouts C.f. OTG Table 5-3 B-Device Timing
+  * @{
+  */
+#define Tb_se0_srp                     _MASK(30) /*!< b_srp_se0 -     SE0 Time Before SRP */
+#define Tb_se0_srp_                    _NOT(Tb_se0_srp)
+#define TB_SE0_SRP                     MS(2)
+#define Tb_data_pls                    _MASK(30) /*!< b_srp_init -     Data-Line Pulse Time */
+#define Tb_data_pls_                   _NOT(Tb_data_pls)
+#define TB_DATA_PLS                    MS(7)
+#define Tb_data_pls_min                _MASK(30) /*!< a_srp_min -     Data-Line Pulse minimum time */
+#define Tb_data_pls_min_               _NOT(Tb_data_pls_min)
+#define TB_DATA_PLS_MIN                MS(5)
+#define Tb_data_pls_max                _MASK(30) /*!< a_srp_wait -     Data-Line Pulse maximum time */
+#define Tb_data_pls_max_               _NOT(Tb_data_pls_max)
+#define TB_DATA_PLS_MAX                MS(10)
+#define Tb_srp_init                    _MASK(30) /*!< b_srp_init -     SRP Initiate Time (TODO multi-state? Not-needed?) */
+#define Tb_srp_init_                   _NOT(Tb_srp_init)
+#define TB_SRP_INIT                    MS(100)
+#define Tb_srp_fail_min                _MASK(30) /*!< b_srp_wait -     SRP Fail Time minimum (TODO) */
+#define Tb_srp_fail_min_               _NOT(Tb_srp_fail_min)
+#define TB_SRP_FAIL_MIN                SEC(5)
+#define Tb_aidl_bdis_min               _MASK(30) /*!< b_peripheral -     A-idle to B-Disconnect minimum (TODO) */
+#define Tb_aidl_bdis_min_              _NOT(Tb_aidl_bdis_min)
+#define TB_AIDL_BDIS_MIN               MS(5)
+#define Tb_aidl_bdis_max               _MASK(30) /*!< b_peripheral -     A-idle to B-Disconnect maximum (TODO) */
+#define Tb_aidl_bdis_max_              _NOT(Tb_aidl_bdis_max)
+#define TB_AIDL_BDIS_MAX               MS(150)
+#define Tldisc_dschrg                  _MASK(30) /*!< b_dischrg -     Local Disconnect to Data Line Discharge (TODO) */
+#define Tldisc_dschrg_                 _NOT(Tldisc_dschrg)
+#define TLDISC_DSCHRG                  US(25)
+#define Tb_ase0_brst_min               _MASK(30) /*!< b_wait_acon -     A-SE0 to B-Reset minimum */
+#define Tb_ase0_brst_min_              _NOT(Tb_ase0_brst_min)
+#define TB_ASE0_BRST_MIN               US(3125)
+#define Tb_acon_dbnc                   _MASK(30) /*!< b_acon_dbnc -     A-Connect Debounce */
+#define Tb_acon_dbnc_                  _NOT(Tb_acon_dbnc)
+#define TB_ACON_DBNC                   US(2)
+#define Tb_acon_bse0                   _MASK(30) /*!< b_host_se0 -     A-Connect to B-SE0 */
+#define Tb_acon_bse0_                  _NOT(Tb_acon_bse0)
+#define TB_ACON_BSE0                   MS(1)
+#define Tid_ldb                        _MASK(30) /*!< otg_enable -     ID changes debounce */
+#define Tid_ldb_                       _NOT(Tid_ldb)
+#define TID_LDB                        MS(100)
+ /* @} */
+
+ /*! @name Timeouts for Carkit
+  * @{
+  */
+#define Tph_bcon_ldb                   _MASK(30) /*!< ph_init -     B-Connect Long Debounce */
+#define Tph_bcon_ldb_                  _NOT(Tph_bcon_ldb)
+#define TPH_BCON_LDB                   MS(100)
+#define Tph_init_pls                   _MASK(30) /*!< ph_int -     Timeout for Carkit interrupt. */
+#define Tph_init_pls_                  _NOT(Tph_init_pls)
+#define TPH_INIT_PLS                   US(500)
+#define Tcr_uart_rsp                   _MASK(30) /*!< ph_int -     Timeout for Carkit UART */
+#define Tcr_uart_rsp_                  _NOT(Tcr_uart_rsp)
+#define TCR_UART_RSP                   MS(30)
+#define Tcr_aud_det                    _MASK(30) /*!< ph_audio_wait -     Timeout waiting for Carkit Audio ack */
+#define Tcr_aud_det_                   _NOT(Tcr_aud_det)
+#define TCR_AUD_DET                    US(1000)
+#define Tph_led_off                    _MASK(30) /*!< ph_uart -     Timeout for LED off pulse */
+#define Tph_led_off_                   _NOT(Tph_led_off)
+#define TPH_LED_OFF                    US(10)
+#define Tph_led_on                     _MASK(30) /*!< ph_uart -     Timeout for LED on pulse */
+#define Tph_led_on_                    _NOT(Tph_led_on)
+#define TPH_LED_ON                     MS(4)
+#define Tcr_led_off                    _MASK(30) /*!< ph_uart -     Timeout for LED off pulse */
+#define Tcr_led_off_                   _NOT(Tcr_led_off)
+#define TCR_LED_OFF                    MS(4)
+#define Tcr_led_on                     _MASK(30) /*!< ph_uart -     Timeout for LED on pulse */
+#define Tcr_led_on_                    _NOT(Tcr_led_on)
+#define TCR_LED_ON                     MS(8)
+#define Tcr_dm_disc                    _MASK(30) /*!< cr_aud -     Timeout for DM disconnect */
+#define Tcr_dm_disc_                   _NOT(Tcr_dm_disc)
+#define TCR_DM_DISC                    MS(50)
+#define Tph_mono_ack                   _MASK(30) /*!< cr_wait -     Timeout for CR INT */
+#define Tph_mono_ack_                  _NOT(Tph_mono_ack)
+#define TPH_MONO_ACK                   MS(1)
+#define Tph_aud_det                    _MASK(30) /*!< cr_ack -     Timeout for CR ACK */
+#define Tph_aud_det_                   _NOT(Tph_aud_det)
+#define TPH_AUD_DET                    MS(100)
+ /* @} */
+
+ /*! @name Timeouts for timer test
+  * @{
+  */
+#define Tzero                          _MASK(30) /*!< otg_enabled -     Startup */
+#define Tzero_                         _NOT(Tzero)
+#define TZERO                          MS(2)
+#define Tst_one_ms                     _MASK(30) /*!< tm_start -     Timer test */
+#define Tst_one_ms_                    _NOT(Tst_one_ms)
+#define TST_ONE_MS                     MS(1)
+#define Tst_ten_ms                     _MASK(30) /*!< tm_start -     Timer test */
+#define Tst_ten_ms_                    _NOT(Tst_ten_ms)
+#define TST_TEN_MS                     MS(10)
+#define Tst_one_second                 _MASK(30) /*!< tm_start -     Timer test */
+#define Tst_one_second_                _NOT(Tst_one_second)
+#define TST_ONE_SECOND                 SEC(1)
+#define Tst_two_second                 _MASK(30) /*!< tm_start -     Timer test */
+#define Tst_two_second_                _NOT(Tst_two_second)
+#define TST_TWO_SECOND                 SEC(2)
+#define Tst_four_second                _MASK(30) /*!< tm_start -     Timer test */
+#define Tst_four_second_               _NOT(Tst_four_second)
+#define TST_FOUR_SECOND                SEC(4)
+#define Tst_eight_second               _MASK(30) /*!< tm_start -     Timer test */
+#define Tst_eight_second_              _NOT(Tst_eight_second)
+#define TST_EIGHT_SECOND               SEC(8)
+#define Tst_ten_second                 _MASK(30) /*!< tm_start -     Timer test */
+#define Tst_ten_second_                _NOT(Tst_ten_second)
+#define TST_TEN_SECOND                 SEC(10)
+ /* @} */
+
+/* Generated by otg-outputs-h.awk
+ *
+ * Do not Edit this file.
+ */
+
+
+
+ /* State Machine Outputs
+  */
+
+ /*! @name Driver Initialization Outputs
+  * N.B. tcd_en is used for older devices, to check if Vbus
+  * already enabled.
+  * @{
+  */
+
+#define TCD_INIT_OUT                    0	 /*!<  Initiate Transceiver Controller Driver Initialization (or De-initialization.) */
+#define tcd_init_out                   _setmask(TCD_INIT_OUT)
+#define tcd_init_out_                  _resetmask(TCD_INIT_OUT)
+#define tcd_init_out_set               _setmask(TCD_INIT_OUT)
+#define tcd_init_out_reset             _resetmask(TCD_INIT_OUT)
+#define tcd_init_out_power             _powermask(TCD_INIT_OUT)
+
+#define PCD_INIT_OUT                    1	 /*!<  Initiate Peripheral Controller Driver Initialization (or De-initialization.) */
+#define pcd_init_out                   _setmask(PCD_INIT_OUT)
+#define pcd_init_out_                  _resetmask(PCD_INIT_OUT)
+#define pcd_init_out_set               _setmask(PCD_INIT_OUT)
+#define pcd_init_out_reset             _resetmask(PCD_INIT_OUT)
+#define pcd_init_out_power             _powermask(PCD_INIT_OUT)
+
+#define HCD_INIT_OUT                    2	 /*!<  Initiate Host Controller Driver Initialization (or De-initialization). */
+#define hcd_init_out                   _setmask(HCD_INIT_OUT)
+#define hcd_init_out_                  _resetmask(HCD_INIT_OUT)
+#define hcd_init_out_set               _setmask(HCD_INIT_OUT)
+#define hcd_init_out_reset             _resetmask(HCD_INIT_OUT)
+#define hcd_init_out_power             _powermask(HCD_INIT_OUT)
+
+#define OCD_INIT_OUT                    3	 /*!<  Initiate OTG Controller Driver Initialization (or De-initialization). */
+#define ocd_init_out                   _setmask(OCD_INIT_OUT)
+#define ocd_init_out_                  _resetmask(OCD_INIT_OUT)
+#define ocd_init_out_set               _setmask(OCD_INIT_OUT)
+#define ocd_init_out_reset             _resetmask(OCD_INIT_OUT)
+#define ocd_init_out_power             _powermask(OCD_INIT_OUT)
+
+#define TCD_EN_OUT                      4	 /*!<  Enable Transceiver Controller Driver */
+#define tcd_en_out                     _setmask(TCD_EN_OUT)
+#define tcd_en_out_                    _resetmask(TCD_EN_OUT)
+#define tcd_en_out_set                 _setmask(TCD_EN_OUT)
+#define tcd_en_out_reset               _resetmask(TCD_EN_OUT)
+#define tcd_en_out_power               _powermask(TCD_EN_OUT)
+
+#define PCD_EN_OUT                      5	 /*!<  Enable Peripheral Controller Driver */
+#define pcd_en_out                     _setmask(PCD_EN_OUT)
+#define pcd_en_out_                    _resetmask(PCD_EN_OUT)
+#define pcd_en_out_set                 _setmask(PCD_EN_OUT)
+#define pcd_en_out_reset               _resetmask(PCD_EN_OUT)
+#define pcd_en_out_power               _powermask(PCD_EN_OUT)
+
+#define HCD_EN_OUT                      6	 /*!<  Enable Host Controller Driver */
+#define hcd_en_out                     _setmask(HCD_EN_OUT)
+#define hcd_en_out_                    _resetmask(HCD_EN_OUT)
+#define hcd_en_out_set                 _setmask(HCD_EN_OUT)
+#define hcd_en_out_reset               _resetmask(HCD_EN_OUT)
+#define hcd_en_out_power               _powermask(HCD_EN_OUT)
+ /* @) */
+
+ /*! @name Transceiver Controller Driver Outputs
+  * @{
+  */
+
+#define DRV_VBUS_OUT                    7	 /*!<  A-Device will Drive Vbus to 5V through charge pump. */
+#define drv_vbus_out                   _setmask(DRV_VBUS_OUT)
+#define drv_vbus_out_                  _resetmask(DRV_VBUS_OUT)
+#define drv_vbus_out_set               _setmask(DRV_VBUS_OUT)
+#define drv_vbus_out_reset             _resetmask(DRV_VBUS_OUT)
+#define drv_vbus_out_power             _powermask(DRV_VBUS_OUT)
+
+#define CHRG_VBUS_OUT                   8	 /*!<  B-Device will charge Vbus to 3.3V through resistor (SRP.) */
+#define chrg_vbus_out                  _setmask(CHRG_VBUS_OUT)
+#define chrg_vbus_out_                 _resetmask(CHRG_VBUS_OUT)
+#define chrg_vbus_out_set              _setmask(CHRG_VBUS_OUT)
+#define chrg_vbus_out_reset            _resetmask(CHRG_VBUS_OUT)
+#define chrg_vbus_out_power            _powermask(CHRG_VBUS_OUT)
+
+#define DISCHRG_VBUS_OUT                9	 /*!<  B-Device will discharge Vbus (enable dischage resistor.) */
+#define dischrg_vbus_out               _setmask(DISCHRG_VBUS_OUT)
+#define dischrg_vbus_out_              _resetmask(DISCHRG_VBUS_OUT)
+#define dischrg_vbus_out_set           _setmask(DISCHRG_VBUS_OUT)
+#define dischrg_vbus_out_reset         _resetmask(DISCHRG_VBUS_OUT)
+#define dischrg_vbus_out_power         _powermask(DISCHRG_VBUS_OUT)
+
+#define DM_PULLUP_OUT                  10	 /*!<  DM pullup control - aka loc_carkit */
+#define dm_pullup_out                  _setmask(DM_PULLUP_OUT)
+#define dm_pullup_out_                 _resetmask(DM_PULLUP_OUT)
+#define dm_pullup_out_set              _setmask(DM_PULLUP_OUT)
+#define dm_pullup_out_reset            _resetmask(DM_PULLUP_OUT)
+#define dm_pullup_out_power            _powermask(DM_PULLUP_OUT)
+
+#define DM_PULLDOWN_OUT                11	 /*!<  DM pulldown control */
+#define dm_pulldown_out                _setmask(DM_PULLDOWN_OUT)
+#define dm_pulldown_out_               _resetmask(DM_PULLDOWN_OUT)
+#define dm_pulldown_out_set            _setmask(DM_PULLDOWN_OUT)
+#define dm_pulldown_out_reset          _resetmask(DM_PULLDOWN_OUT)
+#define dm_pulldown_out_power          _powermask(DM_PULLDOWN_OUT)
+
+#define DP_PULLUP_OUT                  12	 /*!<  DP pullup control - aka loc_conn */
+#define dp_pullup_out                  _setmask(DP_PULLUP_OUT)
+#define dp_pullup_out_                 _resetmask(DP_PULLUP_OUT)
+#define dp_pullup_out_set              _setmask(DP_PULLUP_OUT)
+#define dp_pullup_out_reset            _resetmask(DP_PULLUP_OUT)
+#define dp_pullup_out_power            _powermask(DP_PULLUP_OUT)
+
+#define DP_PULLDOWN_OUT                13	 /*!<  DP pulldown control */
+#define dp_pulldown_out                _setmask(DP_PULLDOWN_OUT)
+#define dp_pulldown_out_               _resetmask(DP_PULLDOWN_OUT)
+#define dp_pulldown_out_set            _setmask(DP_PULLDOWN_OUT)
+#define dp_pulldown_out_reset          _resetmask(DP_PULLDOWN_OUT)
+#define dp_pulldown_out_power          _powermask(DP_PULLDOWN_OUT)
+
+#define CLR_OVERCURRENT_OUT            14	 /*!<  Clear overcurrent indication */
+#define clr_overcurrent_out            _setmask(CLR_OVERCURRENT_OUT)
+#define clr_overcurrent_out_           _resetmask(CLR_OVERCURRENT_OUT)
+#define clr_overcurrent_out_set        _setmask(CLR_OVERCURRENT_OUT)
+#define clr_overcurrent_out_reset      _resetmask(CLR_OVERCURRENT_OUT)
+#define clr_overcurrent_out_power      _powermask(CLR_OVERCURRENT_OUT)
+
+#define DM_DET_OUT                     15	 /*!<  Enable B-Device D- High detect */
+#define dm_det_out                     _setmask(DM_DET_OUT)
+#define dm_det_out_                    _resetmask(DM_DET_OUT)
+#define dm_det_out_set                 _setmask(DM_DET_OUT)
+#define dm_det_out_reset               _resetmask(DM_DET_OUT)
+#define dm_det_out_power               _powermask(DM_DET_OUT)
+
+#define DP_DET_OUT                     16	 /*!<  Enable B-Device D+ High detect */
+#define dp_det_out                     _setmask(DP_DET_OUT)
+#define dp_det_out_                    _resetmask(DP_DET_OUT)
+#define dp_det_out_set                 _setmask(DP_DET_OUT)
+#define dp_det_out_reset               _resetmask(DP_DET_OUT)
+#define dp_det_out_power               _powermask(DP_DET_OUT)
+
+#define CR_DET_OUT                     17	 /*!<  Enable D+ CR detect */
+#define cr_det_out                     _setmask(CR_DET_OUT)
+#define cr_det_out_                    _resetmask(CR_DET_OUT)
+#define cr_det_out_set                 _setmask(CR_DET_OUT)
+#define cr_det_out_reset               _resetmask(CR_DET_OUT)
+#define cr_det_out_power               _powermask(CR_DET_OUT)
+
+#define CHARGE_PUMP_OUT                18	 /*!<  Enable external charge pump. */
+#define charge_pump_out                _setmask(CHARGE_PUMP_OUT)
+#define charge_pump_out_               _resetmask(CHARGE_PUMP_OUT)
+#define charge_pump_out_set            _setmask(CHARGE_PUMP_OUT)
+#define charge_pump_out_reset          _resetmask(CHARGE_PUMP_OUT)
+#define charge_pump_out_power          _powermask(CHARGE_PUMP_OUT)
+
+#define BDIS_ACON_OUT                  19	 /*!<  Enable auto A-connect after B-disconnect. */
+#define bdis_acon_out                  _setmask(BDIS_ACON_OUT)
+#define bdis_acon_out_                 _resetmask(BDIS_ACON_OUT)
+#define bdis_acon_out_set              _setmask(BDIS_ACON_OUT)
+#define bdis_acon_out_reset            _resetmask(BDIS_ACON_OUT)
+#define bdis_acon_out_power            _powermask(BDIS_ACON_OUT)
+
+#define MX21_VBUS_DRAIN                20	 /*!<  MX21 hack */
+#define mx21_vbus_drain                _setmask(MX21_VBUS_DRAIN)
+#define mx21_vbus_drain_               _resetmask(MX21_VBUS_DRAIN)
+#define mx21_vbus_drain_set            _setmask(MX21_VBUS_DRAIN)
+#define mx21_vbus_drain_reset          _resetmask(MX21_VBUS_DRAIN)
+#define mx21_vbus_drain_power          _powermask(MX21_VBUS_DRAIN)
+
+#define ID_PULLDOWN_OUT                21	 /*!<  Enable the ID to ground pulldown ( (CEA-936 - 5 wire carkit.) */
+#define id_pulldown_out                _setmask(ID_PULLDOWN_OUT)
+#define id_pulldown_out_               _resetmask(ID_PULLDOWN_OUT)
+#define id_pulldown_out_set            _setmask(ID_PULLDOWN_OUT)
+#define id_pulldown_out_reset          _resetmask(ID_PULLDOWN_OUT)
+#define id_pulldown_out_power          _powermask(ID_PULLDOWN_OUT)
+
+#define UART_OUT                       22	 /*!<  Enable Transparent UART mode (CEA-936.) */
+#define uart_out                       _setmask(UART_OUT)
+#define uart_out_                      _resetmask(UART_OUT)
+#define uart_out_set                   _setmask(UART_OUT)
+#define uart_out_reset                 _resetmask(UART_OUT)
+#define uart_out_power                 _powermask(UART_OUT)
+
+#define AUDIO_OUT                      23	 /*!<  Enable Audio mode (CEA-936 CarKit interrupt detector.) */
+#define audio_out                      _setmask(AUDIO_OUT)
+#define audio_out_                     _resetmask(AUDIO_OUT)
+#define audio_out_set                  _setmask(AUDIO_OUT)
+#define audio_out_reset                _resetmask(AUDIO_OUT)
+#define audio_out_power                _powermask(AUDIO_OUT)
+
+#define MONO_OUT                       24	 /*!<  Enable Mono-Audio mode (CEA-936.) */
+#define mono_out                       _setmask(MONO_OUT)
+#define mono_out_                      _resetmask(MONO_OUT)
+#define mono_out_set                   _setmask(MONO_OUT)
+#define mono_out_reset                 _resetmask(MONO_OUT)
+#define mono_out_power                 _powermask(MONO_OUT)
+ /* @) */
+
+ /*! @name Peripheral Controller Driver Outputs
+  * @{
+  */
+
+#define REMOTE_WAKEUP_OUT              25	 /*!<  Peripheral will perform remote wakeup. */
+#define remote_wakeup_out              _setmask(REMOTE_WAKEUP_OUT)
+#define remote_wakeup_out_             _resetmask(REMOTE_WAKEUP_OUT)
+#define remote_wakeup_out_set          _setmask(REMOTE_WAKEUP_OUT)
+#define remote_wakeup_out_reset        _resetmask(REMOTE_WAKEUP_OUT)
+#define remote_wakeup_out_power        _powermask(REMOTE_WAKEUP_OUT)
+ /* @) */
+
+ /*! @name Host Controller Driver Outputs
+  * @{
+  */
+
+#define LOC_SOF_OUT                    26	 /*!<  Host will enable packet traffic. */
+#define loc_sof_out                    _setmask(LOC_SOF_OUT)
+#define loc_sof_out_                   _resetmask(LOC_SOF_OUT)
+#define loc_sof_out_set                _setmask(LOC_SOF_OUT)
+#define loc_sof_out_reset              _resetmask(LOC_SOF_OUT)
+#define loc_sof_out_power              _powermask(LOC_SOF_OUT)
+
+#define LOC_SUSPEND_OUT                27	 /*!<  Host will suspend bus. */
+#define loc_suspend_out                _setmask(LOC_SUSPEND_OUT)
+#define loc_suspend_out_               _resetmask(LOC_SUSPEND_OUT)
+#define loc_suspend_out_set            _setmask(LOC_SUSPEND_OUT)
+#define loc_suspend_out_reset          _resetmask(LOC_SUSPEND_OUT)
+#define loc_suspend_out_power          _powermask(LOC_SUSPEND_OUT)
+
+#define REMOTE_WAKEUP_EN_OUT           28	 /*!<  Host will send remote wakeup enable or disable request. */
+#define remote_wakeup_en_out           _setmask(REMOTE_WAKEUP_EN_OUT)
+#define remote_wakeup_en_out_          _resetmask(REMOTE_WAKEUP_EN_OUT)
+#define remote_wakeup_en_out_set       _setmask(REMOTE_WAKEUP_EN_OUT)
+#define remote_wakeup_en_out_reset     _resetmask(REMOTE_WAKEUP_EN_OUT)
+#define remote_wakeup_en_out_power     _powermask(REMOTE_WAKEUP_EN_OUT)
+
+#define HNP_EN_OUT                     29	 /*!<  Host will send HNP enable request. */
+#define hnp_en_out                     _setmask(HNP_EN_OUT)
+#define hnp_en_out_                    _resetmask(HNP_EN_OUT)
+#define hnp_en_out_set                 _setmask(HNP_EN_OUT)
+#define hnp_en_out_reset               _resetmask(HNP_EN_OUT)
+#define hnp_en_out_power               _powermask(HNP_EN_OUT)
+
+#define HPWR_OUT                       30	 /*!<  Host will enable high power (external charge pump.) */
+#define hpwr_out                       _setmask(HPWR_OUT)
+#define hpwr_out_                      _resetmask(HPWR_OUT)
+#define hpwr_out_set                   _setmask(HPWR_OUT)
+#define hpwr_out_reset                 _resetmask(HPWR_OUT)
+#define hpwr_out_power                 _powermask(HPWR_OUT)
+ /* @) */
+#define MAX_OUTPUTS        31
+
+/* Generated by otg-ioctls-h.awk
+ *
+ * Do not Edit this file,
+ */
+
+
+#if defined(OTG_LINUX)
+
+#define OTGADMIN_MAGIC              'O'
+#define OTGADMIN_VERSION            _IOR(OTGADMIN_MAGIC, 1, u64)
+#define OTGADMIN_STATUS             _IOR(OTGADMIN_MAGIC, 3, struct otg_status_update)
+#define OTGADMIN_SET_FUNCTION       _IOW(OTGADMIN_MAGIC, 4, struct otg_admin_command)
+#define OTGADMIN_GET_FUNCTION       _IOR(OTGADMIN_MAGIC, 4, struct otg_admin_command)
+#define OTGADMIN_SET_INFO           _IOW(OTGADMIN_MAGIC, 5, struct otg_firmware_info)
+#define OTGADMIN_GET_INFO           _IOR(OTGADMIN_MAGIC, 5, struct otg_firmware_info)
+#define OTGADMIN_SET_STATE          _IOW(OTGADMIN_MAGIC, 6, struct otg_state)
+#define OTGADMIN_GET_STATE          _IOR(OTGADMIN_MAGIC, 6, struct otg_state)
+#define OTGADMIN_SET_TEST           _IOW(OTGADMIN_MAGIC, 7, struct otg_test)
+#define OTGADMIN_GET_TEST           _IOR(OTGADMIN_MAGIC, 7, struct otg_test)
+#define OTGADMIN_SET_SERIAL         _IOW(OTGADMIN_MAGIC, 8, struct otg_admin_command)
+#define OTGADMIN_GET_SERIAL         _IOR(OTGADMIN_MAGIC, 8, struct otg_admin_command)
+
+
+
+ /*!
+  * Signal Sent: a_bcon_no_tmout_req
+  * State Valid: otg_host
+  *     Application on A-host wants Ta_wait_bcon timeout disabled (non-OTG mode).
+  */
+#define OTGADMIN_A_BCON_NO_TMOUT_REQ         _IOW(OTGADMIN_MAGIC, 10, int)
+
+ /*!
+  * Signal Sent: a_hpwr_req
+  * State Valid: otg_host
+  *     Application on A-host wants external charge pump enabled.
+  */
+#define OTGADMIN_A_HPWR_REQ                  _IOW(OTGADMIN_MAGIC, 11, int)
+
+ /*!
+  * Signal Sent: bus_drop
+  * State Valid: otg_ok
+  *     Application on Device needs to power down bus.
+  */
+#define OTGADMIN_BUS_DROP                    _IOW(OTGADMIN_MAGIC, 12, int)
+
+ /*!
+  * Signal Sent: a_bus_drop
+  * State Valid: otg_ok
+  *     Application on A-Device needs to power down bus.
+  */
+#define OTGADMIN_A_BUS_DROP                  _IOW(OTGADMIN_MAGIC, 13, int)
+
+ /*!
+  * Signal Sent: b_bus_drop
+  * State Valid: otg_ok
+  *     Application on B-Device needs to disconnect from bus.
+  */
+#define OTGADMIN_B_BUS_DROP                  _IOW(OTGADMIN_MAGIC, 14, int)
+
+ /*!
+  * Signal Sent: bus_req
+  * State Valid: otg_ok
+  *     Application on Device wants to use the bus.
+  */
+#define OTGADMIN_BUS_REQ                     _IOW(OTGADMIN_MAGIC, 15, int)
+
+ /*!
+  * Signal Sent: a_bus_req
+  * State Valid: otg_ok
+  *     Application on A-Device wants to act as host
+  */
+#define OTGADMIN_A_BUS_REQ                   _IOW(OTGADMIN_MAGIC, 16, int)
+
+ /*!
+  * Signal Sent: b_bus_req
+  * State Valid: otg_ok
+  *     Application on B-Device wants to act as host
+  */
+#define OTGADMIN_B_BUS_REQ                   _IOW(OTGADMIN_MAGIC, 17, int)
+
+ /*!
+  * Signal Sent: b_sess_req
+  * State Valid: otg_ok
+  *     Application on B-Device to perform SRP (alias for b_srp_req.)
+  */
+#define OTGADMIN_B_SESS_REQ                  _IOW(OTGADMIN_MAGIC, 18, int)
+
+ /*!
+  * Signal Sent: suspend_req
+  * State Valid: otg_host
+  *     Application on Device requests bus be suspended (alias for a_bus_req/.)
+  */
+#define OTGADMIN_SUSPEND_REQ                 _IOW(OTGADMIN_MAGIC, 19, int)
+
+ /*!
+  * Signal Sent: a_suspend_req
+  * State Valid: otg_host
+  *     Application on A-host requests bus be suspended (alias for a_bus_req/.)
+  */
+#define OTGADMIN_A_SUSPEND_REQ               _IOW(OTGADMIN_MAGIC, 20, int)
+
+ /*!
+  * Signal Sent: b_suspend_req
+  * State Valid: otg_host
+  *     Application on B-host requests bus be suspended (alias for b_bus_req/.)
+  */
+#define OTGADMIN_B_SUSPEND_REQ               _IOW(OTGADMIN_MAGIC, 21, int)
+
+ /*!
+  * Signal Sent: set_remote_wakeup_cmd
+  * State Valid: a_host
+  *     A-Device will send Remote Wakeup Enable Request
+  */
+#define OTGADMIN_SET_REMOTE_WAKEUP_CMD       _IOW(OTGADMIN_MAGIC, 22, int)
+
+ /*!
+  * Signal Sent: remote_wakeup_cmd
+  * State Valid: tr_configured
+  *     B-Device will perform Remote Wakeup.
+  */
+#define OTGADMIN_REMOTE_WAKEUP_CMD           _IOW(OTGADMIN_MAGIC, 23, int)
+
+ /*!
+  * Signal Sent: reset_remote_wakeup_cmd
+  * State Valid: a_host
+  *     A-Device will send Remote Wakeup Disable Request
+  */
+#define OTGADMIN_RESET_REMOTE_WAKEUP_CMD     _IOW(OTGADMIN_MAGIC, 24, int)
+
+ /*!
+  * Signal Sent: clr_err_cmd
+  * State Valid: a_vbus_err
+  *     A-Device ill clears Vbus overcurrent error.
+  */
+#define OTGADMIN_CLR_ERR_CMD                 _IOW(OTGADMIN_MAGIC, 25, int)
+
+ /*!
+  * Signal Sent: b_hnp_cmd
+  * State Valid: b_configured
+  *     B-Device will attempt HNP
+  */
+#define OTGADMIN_B_HNP_CMD                   _IOW(OTGADMIN_MAGIC, 26, int)
+
+ /*!
+  * Signal Sent: ph_int_cmd
+  * State Valid: ph_audio
+  *     B-Device will request Carkit interrupt
+  */
+#define OTGADMIN_PH_INT_CMD                  _IOW(OTGADMIN_MAGIC, 27, int)
+
+ /*!
+  * Signal Sent: ph_audio_cmd
+  * State Valid: ph_uart
+  *     Application on B-Device connected to Carkit requests audio mode.
+  */
+#define OTGADMIN_PH_AUDIO_CMD                _IOW(OTGADMIN_MAGIC, 28, int)
+
+ /*!
+  * Signal Sent: cr_int_cmd
+  * State Valid: cr_aud
+  *     Application on A-Device wants to emulate Carkit
+  */
+#define OTGADMIN_CR_INT_CMD                  _IOW(OTGADMIN_MAGIC, 29, int)
+
+ /*!
+  * Signal Sent: led_on_cmd
+  * State Valid: ph_uart
+  *     B-Device will enable Carkit LED
+  */
+#define OTGADMIN_LED_ON_CMD                  _IOW(OTGADMIN_MAGIC, 30, int)
+
+ /*!
+  * Signal Sent: led_off_cmd
+  * State Valid: ph_uart
+  *     B-Device will disable Carkit LED
+  */
+#define OTGADMIN_LED_OFF_CMD                 _IOW(OTGADMIN_MAGIC, 31, int)
+
+ /*!
+  * Signal Sent: enable_otg
+  * State Valid: otg_all
+  *     Move State Machine otg_disabled state.
+  */
+#define OTGADMIN_ENABLE_OTG                  _IOW(OTGADMIN_MAGIC, 32, int)
+
+#define OTGADMIN_MAXNR 33
+
+
+#endif				/* defined(OTG_LINUX) */
+
+/* Generated by otg-iocontrol-h.awk
+ *
+ * Do not Edit this file,
+ */
+
+
+#if defined(OTG_WINCE)
+
+#define OTGADMIN_BASE$              0x401
+#define _USBLAN_CTL_CODE(_Function, _Method, _Access) \
+        CTL_CODE(OTGADMIN_BASE, _Function, _Method, _Access)
+
+#define OTGADMIN_VERSION            OTGADMIN_CTL_CODE(0x401, METHOD_BUFFERRED, FILE_READ_ACCESS)
+#define OTGADMIN_STATUS             OTGADMIN_CTL_CODE(0x403, METHOD_BUFFERRED, FILE_READ_ACCESS)
+#define OTGADMIN_SET_FUNCTION       OTGADMIN_CTL_CODE(0x404, METHOD_BUFFERRED, FILE_WRITE_ACCESS)
+#define OTGADMIN_GET_FUNCTION       OTGADMIN_CTL_CODE(0x404, METHOD_BUFFERRED, FILE_READ_ACCESS)
+#define OTGADMIN_SET_INFO           OTGADMIN_CTL_CODE(0x405, METHOD_BUFFERRED, FILE_WRITE_ACCESS)
+#define OTGADMIN_GET_INFO           OTGADMIN_CTL_CODE(0x405, METHOD_BUFFERRED, FILE_READ_ACCESS)
+#define OTGADMIN_SET_STATE          OTGADMIN_CTL_CODE(0x406, METHOD_BUFFERRED, FILE_WRITE_ACCESS)
+#define OTGADMIN_GET_STATE          OTGADMIN_CTL_CODE(0x406, METHOD_BUFFERRED, FILE_READ_ACCESS)
+#define OTGADMIN_SET_TEST           OTGADMIN_CTL_CODE(0x407, METHOD_BUFFERRED, FILE_WRITE_ACCESS)
+#define OTGADMIN_GET_TEST           OTGADMIN_CTL_CODE(0x407, METHOD_BUFFERRED, FILE_READ_ACCESS)
+
+
+
+ /*!
+  * Signal Sent: a_bcon_no_tmout_req
+  * State Valid: otg_host
+  *     Application on A-host wants Ta_wait_bcon timeout disabled (non-OTG mode).
+  */
+#define OTGADMIN_A_BCON_NO_TMOUT_REQ         OTGADMIN_CTL_CODE(, 0x40a, METHOD_BUFFERRED, FILE_READ_ACCESS)
+
+ /*!
+  * Signal Sent: a_hpwr_req
+  * State Valid: otg_host
+  *     Application on A-host wants external charge pump enabled.
+  */
+#define OTGADMIN_A_HPWR_REQ                  OTGADMIN_CTL_CODE(, 0x40b, METHOD_BUFFERRED, FILE_READ_ACCESS)
+
+ /*!
+  * Signal Sent: bus_drop
+  * State Valid: otg_ok
+  *     Application on Device needs to power down bus.
+  */
+#define OTGADMIN_BUS_DROP                    OTGADMIN_CTL_CODE(, 0x40c, METHOD_BUFFERRED, FILE_READ_ACCESS)
+
+ /*!
+  * Signal Sent: a_bus_drop
+  * State Valid: otg_ok
+  *     Application on A-Device needs to power down bus.
+  */
+#define OTGADMIN_A_BUS_DROP                  OTGADMIN_CTL_CODE(, 0x40d, METHOD_BUFFERRED, FILE_READ_ACCESS)
+
+ /*!
+  * Signal Sent: b_bus_drop
+  * State Valid: otg_ok
+  *     Application on B-Device needs to disconnect from bus.
+  */
+#define OTGADMIN_B_BUS_DROP                  OTGADMIN_CTL_CODE(, 0x40e, METHOD_BUFFERRED, FILE_READ_ACCESS)
+
+ /*!
+  * Signal Sent: bus_req
+  * State Valid: otg_ok
+  *     Application on Device wants to use the bus.
+  */
+#define OTGADMIN_BUS_REQ                     OTGADMIN_CTL_CODE(, 0x40f, METHOD_BUFFERRED, FILE_READ_ACCESS)
+
+ /*!
+  * Signal Sent: a_bus_req
+  * State Valid: otg_ok
+  *     Application on A-Device wants to act as host
+  */
+#define OTGADMIN_A_BUS_REQ                   OTGADMIN_CTL_CODE(, 0x410, METHOD_BUFFERRED, FILE_READ_ACCESS)
+
+ /*!
+  * Signal Sent: b_bus_req
+  * State Valid: otg_ok
+  *     Application on B-Device wants to act as host
+  */
+#define OTGADMIN_B_BUS_REQ                   OTGADMIN_CTL_CODE(, 0x411, METHOD_BUFFERRED, FILE_READ_ACCESS)
+
+ /*!
+  * Signal Sent: b_sess_req
+  * State Valid: otg_ok
+  *     Application on B-Device to perform SRP (alias for b_srp_req.)
+  */
+#define OTGADMIN_B_SESS_REQ                  OTGADMIN_CTL_CODE(, 0x412, METHOD_BUFFERRED, FILE_READ_ACCESS)
+
+ /*!
+  * Signal Sent: suspend_req
+  * State Valid: otg_host
+  *     Application on Device requests bus be suspended (alias for a_bus_req/.)
+  */
+#define OTGADMIN_SUSPEND_REQ                 OTGADMIN_CTL_CODE(, 0x413, METHOD_BUFFERRED, FILE_READ_ACCESS)
+
+ /*!
+  * Signal Sent: a_suspend_req
+  * State Valid: otg_host
+  *     Application on A-host requests bus be suspended (alias for a_bus_req/.)
+  */
+#define OTGADMIN_A_SUSPEND_REQ               OTGADMIN_CTL_CODE(, 0x414, METHOD_BUFFERRED, FILE_READ_ACCESS)
+
+ /*!
+  * Signal Sent: b_suspend_req
+  * State Valid: otg_host
+  *     Application on B-host requests bus be suspended (alias for b_bus_req/.)
+  */
+#define OTGADMIN_B_SUSPEND_REQ               OTGADMIN_CTL_CODE(, 0x415, METHOD_BUFFERRED, FILE_READ_ACCESS)
+
+ /*!
+  * Signal Sent: set_remote_wakeup_cmd
+  * State Valid: a_host
+  *     A-Device will send Remote Wakeup Enable Request
+  */
+#define OTGADMIN_SET_REMOTE_WAKEUP_CMD       OTGADMIN_CTL_CODE(, 0x416, METHOD_BUFFERRED, FILE_READ_ACCESS)
+
+ /*!
+  * Signal Sent: remote_wakeup_cmd
+  * State Valid: tr_configured
+  *     B-Device will perform Remote Wakeup.
+  */
+#define OTGADMIN_REMOTE_WAKEUP_CMD           OTGADMIN_CTL_CODE(, 0x417, METHOD_BUFFERRED, FILE_READ_ACCESS)
+
+ /*!
+  * Signal Sent: reset_remote_wakeup_cmd
+  * State Valid: a_host
+  *     A-Device will send Remote Wakeup Disable Request
+  */
+#define OTGADMIN_RESET_REMOTE_WAKEUP_CMD     OTGADMIN_CTL_CODE(, 0x418, METHOD_BUFFERRED, FILE_READ_ACCESS)
+
+ /*!
+  * Signal Sent: clr_err_cmd
+  * State Valid: a_vbus_err
+  *     A-Device ill clears Vbus overcurrent error.
+  */
+#define OTGADMIN_CLR_ERR_CMD                 OTGADMIN_CTL_CODE(, 0x419, METHOD_BUFFERRED, FILE_READ_ACCESS)
+
+ /*!
+  * Signal Sent: b_hnp_cmd
+  * State Valid: b_configured
+  *     B-Device will attempt HNP
+  */
+#define OTGADMIN_B_HNP_CMD                   OTGADMIN_CTL_CODE(, 0x41a, METHOD_BUFFERRED, FILE_READ_ACCESS)
+
+ /*!
+  * Signal Sent: ph_int_cmd
+  * State Valid: ph_audio
+  *     B-Device will request Carkit interrupt
+  */
+#define OTGADMIN_PH_INT_CMD                  OTGADMIN_CTL_CODE(, 0x41b, METHOD_BUFFERRED, FILE_READ_ACCESS)
+
+ /*!
+  * Signal Sent: ph_audio_cmd
+  * State Valid: ph_uart
+  *     Application on B-Device connected to Carkit requests audio mode.
+  */
+#define OTGADMIN_PH_AUDIO_CMD                OTGADMIN_CTL_CODE(, 0x41c, METHOD_BUFFERRED, FILE_READ_ACCESS)
+
+ /*!
+  * Signal Sent: cr_int_cmd
+  * State Valid: cr_aud
+  *     Application on A-Device wants to emulate Carkit
+  */
+#define OTGADMIN_CR_INT_CMD                  OTGADMIN_CTL_CODE(, 0x41d, METHOD_BUFFERRED, FILE_READ_ACCESS)
+
+ /*!
+  * Signal Sent: led_on_cmd
+  * State Valid: ph_uart
+  *     B-Device will enable Carkit LED
+  */
+#define OTGADMIN_LED_ON_CMD                  OTGADMIN_CTL_CODE(, 0x41e, METHOD_BUFFERRED, FILE_READ_ACCESS)
+
+ /*!
+  * Signal Sent: led_off_cmd
+  * State Valid: ph_uart
+  *     B-Device will disable Carkit LED
+  */
+#define OTGADMIN_LED_OFF_CMD                 OTGADMIN_CTL_CODE(, 0x41f, METHOD_BUFFERRED, FILE_READ_ACCESS)
+
+ /*!
+  * Signal Sent: enable_otg
+  * State Valid: otg_all
+  *     Move State Machine otg_disabled state.
+  */
+#define OTGADMIN_ENABLE_OTG                  OTGADMIN_CTL_CODE(, 0x420, METHOD_BUFFERRED, FILE_READ_ACCESS)
+
+#define OTGADMIN_MAXNR 33
+
+
+#endif				/* defined(OTG_WINCE) */
+
+/* Generated by otg-metas-h.awk
+ *
+ * Do not Edit thie file
+ */
+
+
+ /*! @name OTG Figure 6-2 Meta States
+  *
+  * These are the Meta States defined in the 2.0 OTG Specification
+  * Figure 6.2 - Dual-Role A-Device.
+  * @{
+  */
+
+#define m_a_idle                          0	 /*  */
+
+#define m_a_wait_vrise                    1	 /*  */
+
+#define m_a_wait_bcon                     2	 /*  */
+
+#define m_a_host                          3	 /*  */
+
+#define m_a_suspend                       4	 /*  */
+
+#define m_a_peripheral                    5	 /*  */
+
+#define m_a_wait_vfall                    6	 /*  */
+
+#define m_a_vbus_err                      7	 /*  */
+ /* @} */
+ /*! @name OTG Figure 6-3 Meta States
+  *
+  * These are the Meta States defined in the 2.0 OTG Specification
+  * Figure 6.3 - Dual-Role B-Device.
+  * @{
+  */
+
+#define m_b_idle                          8	 /*  */
+
+#define m_b_srp_init                      9	 /*  */
+
+#define m_b_peripheral                   10	 /*  */
+
+#define m_b_suspend                      11	 /*  */
+
+#define m_b_wait_acon                    12	 /*  */
+
+#define m_b_host                         13	 /*  */
+
+#define m_b_suspended                    14	 /*  */
+ /* @} */
+ /*! @name Carkit Meta States Figure 7-7
+  *
+  * @{
+  */
+
+#define m_ph_disc                        15	 /*  Equivalent to b_peripheral */
+
+#define m_ph_init                        16	 /*  */
+
+#define m_ph_uart                        17	 /*  */
+
+#define m_ph_aud                         18	 /*  */
+
+#define m_ph_wait                        19	 /*  */
+
+#define m_ph_exit                        20	 /*  */
+
+#define m_cr_init                        21	 /*  */
+
+#define m_cr_uart                        22	 /*  */
+
+#define m_cr_aud                         23	 /*  */
+
+#define m_cr_ack                         24	 /*  */
+
+#define m_cr_wait                        25	 /*  */
+
+#define m_cr_disc                        26	 /*  */
+ /* @} */
+ /*! @name Additional states used locally.
+  *
+  * @{
+  */
+
+#define m_otg_init                       27	 /*  */
+
+#define m_usb_accessory                  28	 /*  */
+
+#define m_usb_factory                    29	 /*  */
+
+#define m_unknown                        30	 /*  */
+ /* @} */
+
+#define OTG_METAS_FW     31
+
+extern char    *otg_meta_names[];
diff -uNr linux/drivers/no-otg/otg/otg-hcd.h linux/drivers/otg/otg/otg-hcd.h
--- linux/drivers/no-otg/otg/otg-hcd.h	1970-01-01 01:00:00.000000000 +0100
+++ linux/drivers/otg/otg/otg-hcd.h	2006-09-01 21:41:33.000000000 +0200
@@ -0,0 +1,85 @@
+/*
+ * otg/otg-hcd.h - OTG Host Controller Driver
+ *
+ *      Copyright (c) 2004 Belcarra
+ *
+ * By: 
+ *      Stuart Lynne <sl@belcarra.com>, 
+ *      Tom Rushworth <tbr@belcarra.com>, 
+ *      Bruce Balden <balden@belcarra.com>
+ *
+ *
+ */
+/*!
+ * @defgroup OTGHCD Host Controller Driver Support
+ * @ingroup onthegogroup
+ */
+/*!
+ * @file otg/otg/otg-hcd.h
+ * @brief Defines common to On-The-Go Host Controller Support
+ *
+ * This file defines the hcd_ops and hcd_instance structures.
+ *
+ * The hcd_ops structure contains all of the output functions that will
+ * be called as required by the OTG event handler when changing states.
+ *
+ * The hcd_instance structure is used to maintain the global data
+ * required by the host controller drivers.
+ *
+ * @ingroup OTGHCD
+ */
+
+/*!
+ * @name HCD Host Controller Driver
+ * @{
+ */
+struct hcd_instance;
+
+/*!
+ * @struct hcd_ops
+ * The pcd_ops structure contains pointers to all of the functions implemented for the
+ * linked in driver. Having them in this structure allows us to easily determine what
+ * functions are available without resorting to ugly compile time macros or ifdefs
+ *
+ * There is only one instance of this, defined in the device specific lower layer.
+ */
+struct hcd_ops {
+
+        /* mandatory */
+        int max_ports;                                  /*!< maximum number of ports available */
+        u32 capabilities;                               /*!< UDC Capabilities - see usbd-bus.h for details */
+        char *name;                                     /*!< name of controller */
+
+        /* Driver Initialization - by degrees
+         */
+        int (*mod_init) (void);                         /*!< HCD Module Initialization */
+        void (*mod_exit) (void);                        /*!< HCD Module Exit */
+
+
+        otg_output_proc_t       hcd_init_func;          /*!< OTG calls to initialize or de-initialize the HCD */
+        otg_output_proc_t       hcd_en_func;            /*!< OTG calls to enable or disable the HCD */
+        otg_output_proc_t       loc_sof_func;           /*!< OTG calls to into a_host or b_host state - attempt to use port */
+        otg_output_proc_t       loc_suspend_func;       /*!< OTG calls to suspend bus */
+        otg_output_proc_t       remote_wakeup_en_func;  /*!< OTG calls to issue SET FEATURE REMOTE WAKEUP */
+        otg_output_proc_t       hnp_en_func;            /*!< OTG calls to issues SET FEATURE B_HNP_ENABLE */
+};
+
+/*!
+ * @struct hcd_instance
+ */
+struct hcd_instance {
+        struct otg_instance *otg;                       /*!< pointer to OTG Instance */
+        void * privdata;                                /*!< pointer to private data for PCD */
+        struct WORK_STRUCT bh;                          /*!< work structure for bottom half handler */
+};
+
+#define HCD hcd_trace_tag
+extern otg_tag_t HCD;
+extern struct hcd_ops hcd_ops;
+extern struct hcd_instance *hcd_instance;
+
+extern void hcd_init_func(struct otg_instance *, u8 );
+extern void hcd_en_func(struct otg_instance *, u8 );
+
+/* @} */
+
diff -uNr linux/drivers/no-otg/otg/otg-linux.h linux/drivers/otg/otg/otg-linux.h
--- linux/drivers/no-otg/otg/otg-linux.h	1970-01-01 01:00:00.000000000 +0100
+++ linux/drivers/otg/otg/otg-linux.h	2006-09-01 21:41:33.000000000 +0200
@@ -0,0 +1,284 @@
+/*
+ * otg/otg/otg-linux.h
+ *
+ *      Copyright (c) 2004 Belcarra
+ *
+ */
+
+/*!
+ * @file otg/otg/otg-linux.h
+ * @brief Linux OS Compatibility defines
+ *
+ * @ingroup OTGCore
+ */
+#ifndef _OTG_LINUX_H
+#define _OTG_LINUX_H 1
+
+
+
+#include <linux/config.h>
+#include <linux/kernel.h>
+#include <linux/version.h>
+#include <linux/init.h>
+#include <linux/ctype.h>
+#include <linux/slab.h>
+#include <asm/types.h>
+#include <asm/uaccess.h>
+#include <asm/semaphore.h>
+#include <asm/atomic.h>
+#include <linux/proc_fs.h>
+#include <linux/interrupt.h>
+#include <linux/random.h>
+#include <linux/sched.h>
+
+/*! @name Compilation Related
+ */
+
+/*! @{ */
+#undef PRAGMAPACK
+#define PACKED __attribute__((packed))
+#define INLINE __inline__
+
+/*! @} */
+
+/*! @name Memory Allocation Primitives
+ *
+ * CKMALLOC()
+ * LSTRDUP()
+ * LKFREE()
+ * LIST_ENTRY()
+ * LIST_FOR_EACH()
+ */
+
+ /*! @{ */
+
+#if defined(LINUX26)
+    #include <linux/gfp.h>
+#define GET_KERNEL_PAGE()  __get_free_page(GFP_KERNEL)
+
+#else /* LINUX26 */
+
+    #include <linux/mm.h>
+    #define GET_KERNEL_PAGE() get_free_page(GFP_KERNEL)
+#endif /* LINUX26 */
+
+
+
+// Common to all supported versions of Linux ??
+
+#define CKMALLOC(n,f) _ckmalloc(__FUNCTION__, __LINE__, n, f)
+#define LSTRDUP(str) _lstrdup(__FUNCTION__, __LINE__, str)
+#define LKFREE(p) _lkfree(__FUNCTION__, __LINE__, p)
+
+#define ckmalloc(n,f) _ckmalloc(__FUNCTION__, __LINE__, n, f)
+#define lstrdup(str) _lstrdup(__FUNCTION__, __LINE__, str)
+#define lkfree(p) _lkfree(__FUNCTION__, __LINE__, p)
+
+#define OTG_MALLOC_TEST
+#undef OTG_MALLOC_DEBUG
+
+#ifdef OTG_MALLOC_TEST
+    extern int otg_mallocs;
+#endif
+
+
+static INLINE void *_ckmalloc (const char *func, int line, int n, int f)
+{
+    void *p;
+    if ((p = kmalloc (n, f)) == NULL) {
+        return NULL;
+    }
+    memset (p, 0, n);
+    #ifdef OTG_MALLOC_TEST
+    ++otg_mallocs;
+    #endif
+    #ifdef OTG_MALLOC_DEBUG
+        printk(KERN_INFO"%s: %p %s %d %d\n", __FUNCTION__, p, func, line, otg_mallocs);
+    #endif
+    return p;
+}
+
+static INLINE char *_lstrdup (const char *func, int line, char *str)
+{
+    int n;
+    char *s;
+    if (str && (n = strlen (str) + 1) && (s = kmalloc (n, GFP_ATOMIC))) {
+#ifdef OTG_MALLOC_TEST
+        ++otg_mallocs;
+#endif
+#ifdef OTG_MALLOC_DEBUG
+        printk(KERN_INFO"%s: %p %s %d %d\n", __FUNCTION__, s, func, line, otg_mallocs);
+#endif
+        return strcpy (s, str);
+    }
+    return NULL;
+}
+
+static INLINE void _lkfree (const char *func, int line, void *p)
+{
+    if (p) {
+#ifdef OTG_MALLOC_TEST
+        --otg_mallocs;
+#endif
+#ifdef OTG_MALLOC_DEBUG
+        printk(KERN_INFO"%s: %p %s %d %d\n", __FUNCTION__, p, func, line, otg_mallocs);
+#endif
+        kfree (p);
+#ifdef MALLOC_TEST
+        if (otg_mallocs < 0) {
+                printk(KERN_INFO"%s: %p %s %d %d otg_mallocs less zero!\n", __FUNCTION__, p, func, line, otg_mallocs);
+        }
+#endif
+#ifdef OTG_MALLOC_DEBUG
+        else {
+            printk(KERN_INFO"%s: %s %d NULL\n", __FUNCTION__, func, line);
+        }
+#endif
+    }
+}
+
+#if 1
+#include <linux/list.h>
+#define LIST_NODE struct list_head
+#define LIST_NODE_INIT(name) struct list_head name = {&name, &name}
+#define LIST_ENTRY(pointer, type, member) list_entry(pointer, type, member)
+#define LIST_FOR_EACH(cursor, head) list_for_each(cursor, head)
+#define LIST_ADD_TAIL(n,h) list_add_tail(n,h)
+#define LIST_DEL(h) list_del(&(h))
+#else
+#include <otg/otg-list.h>
+#endif
+
+/*! @} */
+
+
+/*! @name Atomic Operations
+ *
+ * atomic_post_inc()
+ * atomic_pre_dec()
+ */
+/*! @{ */
+static __inline__ int atomic_post_inc(volatile atomic_t *v)
+{
+        unsigned long flags;
+        int result;
+        local_irq_save(flags);
+        result = (v->counter)++;
+        local_irq_restore(flags);
+        return(result);
+}
+
+static __inline__ int atomic_pre_dec(volatile atomic_t *v)
+{
+        unsigned long flags;
+        int result;
+        local_irq_save(flags);
+        result = --(v->counter);
+        local_irq_restore(flags);
+        return(result);
+}
+/*! @} */
+
+
+
+/*!@name Scheduling Primitives
+ *
+ * WORK_STRUCT
+ * WORK_ITEM
+ *
+ * SCHEDULE_TIMEOUT()\n
+ * SET_WORK_ARG()\n
+ * SCHEDULE_WORK()\n
+ * SCHEDULE_IMMEDIATE_WORK()\n
+ * NO_WORK_DATA()\n
+ * MOD_DEC_USE_COUNT\n
+ * MOD_INC_USE_COUNT\n
+ */
+
+/*! @{ */
+
+static void inline SCHEDULE_TIMEOUT(int seconds){
+        schedule_timeout( seconds * HZ );
+}
+
+
+/* Separate Linux 2.4 and 2.6 versions of scheduling primitives */
+#if defined(LINUX26)
+    #include <linux/workqueue.h>
+
+    #define WORK_STRUCT  work_struct
+    #define WORK_ITEM    work_struct
+    typedef struct WORK_ITEM WORK_ITEM;
+    #if 0
+        #define PREPARE_WORK_ITEM(__item,__routine,__data) INIT_WORK((__item),(__routine),(__data))
+    #else
+        #include <linux/interrupt.h>
+        #define PREPARE_WORK_ITEM(__item,__routine,__data) __prepare_work(&(__item),(__routine),(__data))
+        static inline void __prepare_work(struct work_struct *_work,
+            void (*_routine),
+            void * _data){
+            INIT_LIST_HEAD(&_work->entry);
+            _work->pending = 0;
+            _work->func = _routine;
+            _work->data = _data;
+            init_timer(&_work->timer);
+        }
+    #endif
+    #undef PREPARE_WORK
+    typedef void (* WORK_PROC)(void *);
+
+    #define SET_WORK_ARG(__item, __data) (__item).data = __data
+
+    #define SCHEDULE_WORK(item) schedule_work(&item)
+    #define SCHEDULE_IMMEDIATE_WORK(item) SCHEDULE_WORK((item))
+    #define PENDING_WORK_ITEM(item) (item.pending != 0)
+    #define NO_WORK_DATA(item) (!item.data)
+    #define _MOD_DEC_USE_COUNT   //Not used in 2.6
+    #define _MOD_INC_USE_COUNT   //Not used in 2.6
+
+#else /* LINUX26 */
+
+    #define WORK_STRUCT  tq_struct
+    #define WORK_ITEM    tq_struct
+    typedef struct WORK_ITEM WORK_ITEM;
+    #define PREPARE_WORK_ITEM(item,work_routine,work_data) { item.routine = work_routine; item.data = work_data; }
+    #define SET_WORK_ARG(__item, __data) (__item).data = __data
+    #define NO_WORK_DATA(item) (!(item).data)
+    #define SCHEDULE_WORK(item) schedule_task(&item)
+    #define PENDING_WORK_ITEM(item) (item.sync != 0)
+    #define _MOD_DEC_USE_COUNT   MOD_DEC_USE_COUNT
+    #define _MOD_INC_USE_COUNT   MOD_INC_USE_COUNT
+
+    typedef void (* WORK_PROC)(void *);
+
+    #if !defined(IRQ_HANDLED)
+        // Irq's
+        typedef void irqreturn_t;
+        #define IRQ_NONE
+        #define IRQ_HANDLED
+        #define IRQ_RETVAL(x)
+    #endif
+#endif /* LINUX26 */
+
+/*! @} */
+
+/*!@name Semaphores
+ *
+ * up()
+ * down()
+ */
+/*! @{ */
+#define UP(s) up(s)
+#define DOWN(s) down(s)
+/*! @} */
+
+/*! @name Printk
+ *
+ * PRINTK()
+ */
+/*! @{ */
+#define PRINTK(s) printk(s)
+/*! @} */
+
+
+#endif /* _OTG_LINUX_H */
diff -uNr linux/drivers/no-otg/otg/otg-list.h linux/drivers/otg/otg/otg-list.h
--- linux/drivers/no-otg/otg/otg-list.h	1970-01-01 01:00:00.000000000 +0100
+++ linux/drivers/otg/otg/otg-list.h	2006-09-01 21:41:33.000000000 +0200
@@ -0,0 +1,69 @@
+#ifndef _OTG_LIST_H
+#define _OTG_LIST_H 1
+
+/*
+	This file defines doubly linked list capabilities 
+	needed. Many operating systems have built in capabilities
+	which match and may possibly be borrowed.
+
+
+        Here is a typical implementation
+*/
+
+struct otg_list_node { 
+	struct otg_list_node *previous, *next;
+};
+typedef struct otg_list_node otg_list_node_t, *otg_list_node_ptr;
+
+//A macro to define an list node within a structure
+#define OTG_LIST_HEAD     struct otg_list_node  
+#define OTG_LIST_HEAD_INIT(name) struct otg_list_node name = {&name, &name};
+//Assume that an OTG_LIST_HEAD pointer is embedded as structure
+// Recover numerical offset of member "member" within a pointer to a "type"
+#define __OTG_PTR_OFFSET(type, member) (u32) (   &(((type *) 0)->member) )
+//Recover typed pointer from pointer to interior of type at the given offset
+#define __OTG_PTR_CONTAINER(ptr, type, member) (type *) ((char *) ptr - __OTG_PTR_OFFSET(type,member))
+// member "member" of type "type". Recover a pointer to the beginning of the structure
+
+#define OTG_LIST_ENTRY(pointer, type, member) __OTG_PTR_CONTAINER(pointer, type, member)
+
+//Recover list header embedded in structure which is a member of a list
+#define OTG_LIST_MEMBER(pointer, member) &(ptr->member)
+//Add an entry at the logical end of the list
+
+//#define INLINE inline
+#define OTG_LIST_ADD_TAIL(new_entry, list_head) __otg_list_add_tail(new_entry, list_head)
+static INLINE void __otg_list_add_tail(struct otg_list_node *new_entry, struct otg_list_node *list_head)
+{
+	struct otg_list_node *old_previous = list_head->previous;
+	old_previous->next = new_entry;
+        new_entry->next = list_head;
+        new_entry->previous = old_previous;
+        list_head -> previous = new_entry;
+
+}
+
+//Delete an entry from the surrounding list
+#define OTG_LIST_DEL_ENTRY(del_entry)  __otg_list_del_entry(&(del_entry))
+
+static void INLINE __otg_list_del_entry(struct otg_list_node *del_entry){
+	//Remove from list 
+	del_entry->previous->next = del_entry->next;
+	del_entry->next->previous = del_entry->previous;
+	//Invalidate the entry
+	del_entry->previous = NULL;
+	del_entry->next = NULL;
+}
+
+#define  OTG_LIST_FOR_EACH(cursor, list) for(cursor=(list)->next; cursor != (list); cursor=cursor->next)
+
+#undef LIST_HEAD
+#define LIST_HEAD OTG_LIST_HEAD
+#undef LIST_HEAD_INIT
+#define LIST_HEAD_INIT(name) OTG_LIST_HEAD_INIT(name)
+#define LIST_ENTRY(pointer,type,member) OTG_LIST_ENTRY(pointer,type,member)
+#define LIST_ADD_TAIL(new_entry, list_head) OTG_LIST_ADD_TAIL(new_entry, list_head)
+#define LIST_DEL_ENTRY(del_entry) OTG_LIST_DEL_ENTRY(del_entry)
+#define LIST_FOR_EACH(cursor, list)  OTG_LIST_FOR_EACH(cursor, list)
+
+#endif /*_OTG_LIST_H */
diff -uNr linux/drivers/no-otg/otg/otg-module.h linux/drivers/otg/otg/otg-module.h
--- linux/drivers/no-otg/otg/otg-module.h	1970-01-01 01:00:00.000000000 +0100
+++ linux/drivers/otg/otg/otg-module.h	2006-09-01 21:41:33.000000000 +0200
@@ -0,0 +1,71 @@
+/*
+ * otg/otg/otg-module.h
+ *
+ *      Copyright (c) 2004 Belcarra
+ *
+ */
+/*!
+ * @file otg/otg/otg-module.h
+ * @brief Linux Module OS Compatibility defines
+ *
+ *
+ * @ingroup OTGCore
+ */
+#ifndef _OTG_MODULE_H
+#define _OTG_MODULE_H 1
+
+#if defined(OTG_LINUX)
+    #if !defined(_LINUX_MODULE_H)
+        #include <linux/module.h>
+    #endif /* _LINUX_MODULE_H */
+    
+    #if defined(MODULE)
+
+        #define MOD_EXIT(exit_routine) module_exit(exit_routine)
+        #define MOD_INIT(init_routine) module_init(init_routine)
+        #define MOD_PROC(proc) (proc)
+        #define MOD_AUTHOR(string) MODULE_AUTHOR(string)
+        #define MOD_PARM(param, type) MODULE_PARM(param, type)
+        #define MOD_PARM_DESC(param,desc) MODULE_PARM_DESC(param, desc)
+        #define MOD_DESCRIPTION(description) MODULE_DESCRIPTION(description)
+        #define OTG_EXPORT_SYMBOL(symbol) EXPORT_SYMBOL(symbol)
+        #define OTG_EPILOGUE  1 /* EPILOGUE ROUTINE NEEDED */
+    
+    #else /* defined(MODULE) */
+
+        #define MOD_EXIT(exit_routine) 
+        #define MOD_INIT(init_routine) module_init(init_routine)
+        #define MOD_PROC(proc) NULL
+        #define MOD_AUTHOR(string) 
+        #define MOD_PARM(param, type)
+        #define MOD_PARM_DESC(param,desc)
+        #define MOD_DESCRIPTION(description) 
+        #define OTG_EXPORT_SYMBOL(symbol) //EXPORT_SYMBOL(symbol)
+        #undef EXPORT_SYMBOL
+        #define OTG_EPILOGUE 0  /* EPILOGUE ROUTINE NOT NEEDED */
+
+    #endif /* defined(MODULE) */
+    
+    //#undef MODULE_AUTHOR
+    //#undef MODULE_DESCRIPTION
+    //#undef MODULE_PARM_DESC
+    //#undef MODULE_PARM
+    #if defined(LINUX24) || defined(LINUX26)
+        #include <linux/version.h>
+        #if defined(MODULE) && (LINUX_VERSION_CODE >= KERNEL_VERSION (2,4,17))
+        //"GPL License applies under certain cirumstances; consult your vendor for details"
+            #define EMBED_LICENSE() MODULE_LICENSE ("GPL")
+        #else 
+            #define EMBED_LICENSE()   //Operation not supported for earlier Linux kernels
+        #endif
+
+    #else /* defined(LINUX24) || defined(LINUX26) */
+        #error "Need to define EMBED_LICENSE for the current operating system"
+    #endif /* defined(LINUX24) || defined(LINUX26) */
+    #define EMBED_MODULE_INFO(section,moduleinfo) static char __##section##_module_info[] = moduleinfo "balden@belcarra.com"
+    #define EMBED_USBD_INFO(moduleinfo) EMBED_MODULE_INFO(usbd,moduleinfo)
+    #define GET_MODULE_INFO(section) __##section##_module_info
+#endif /* defined(OTG_LINUX) */
+    
+    
+#endif
diff -uNr linux/drivers/no-otg/otg/otg-ocd.h linux/drivers/otg/otg/otg-ocd.h
--- linux/drivers/no-otg/otg/otg-ocd.h	1970-01-01 01:00:00.000000000 +0100
+++ linux/drivers/otg/otg/otg-ocd.h	2006-09-01 21:41:33.000000000 +0200
@@ -0,0 +1,94 @@
+/*
+ * otg/otg-ocd.h - OTG Controller Driver
+ *
+ *      Copyright (c) 2004 Belcarra
+ *
+ * By: 
+ *      Stuart Lynne <sl@belcarra.com>, 
+ *      Tom Rushworth <tbr@belcarra.com>, 
+ *      Bruce Balden <balden@belcarra.com>
+ *
+ *
+ */
+/*!
+ * @defgroup OTGOCD OTG Controller Driver Support
+ * @ingroup onthegogroup
+ */
+/*!
+ * @file otg/otg/otg-ocd.h
+ * @brief Defines common to On-The-Go OTG Controller Support
+ *
+ * This file defines the ocd_ops and ocd_instance structures.
+ *
+ * The ocd_ops structure contains all of the output functions that will
+ * be called as required by the OTG event handler when changing states.
+ *
+ * The ocd_instance structure is used to maintain the global data
+ * required by the OTG controller drivers.
+ *
+ * @ingroup OTGOCD
+ */
+
+/*!
+ * @name OCD OTG Controller Driver
+ * @{
+ */
+//struct ocd_instance;
+
+struct ocd_instance {
+        struct otg_instance *otg;
+        void * privdata;
+};
+
+
+typedef int (*otg_timer_callback_proc_t) (void *);
+
+#define OCD_CAPABILITIES_DR          1 << 0
+#define OCD_CAPABILITIES_PO          1 << 1
+#define OCD_CAPABILITIES_TR          1 << 2
+#define OCD_CAPABILITIES_HOST        1 << 3
+
+#define OCD_CAPABILITIES_AUTO        1 << 4
+
+
+/*!
+ * @struct ocd_ops
+ * The ocd_ops structure contains pointers to all of the functions implemented for the
+ * linked in driver. Having them in this structure allows us to easily determine what
+ * functions are available without resorting to ugly compile time macros or ifdefs
+ *
+ * There is only one instance of this, defined in the device specific lower layer.
+ */
+struct ocd_ops {
+
+        u32 capabilities;                               /* OCD Capabilities */
+
+        /* Driver Initialization - by degrees
+         */
+        int (*mod_init) (void);                         /*!< OCD Module Initialization */
+        void (*mod_exit) (void);                        /*!< OCD Module Exit */
+
+        otg_output_proc_t       ocd_init_func;          /*!< OTG calls to initialize or de-initialize the OCD */
+
+        int (*start_timer) (struct otg_instance *, int);/*!< called by OTG to start timer */
+        u64 (*ticks) (void);            /*!< called by OTG to  fetch current ticks, typically micro-seconds when available */
+        u64 (*elapsed) ( u64 *, u64 *);                 /*!< called by OTG to get micro-seconds elapsed between two ticks */
+        u32 interrupts;                                 /*!< called by OTG to get number of interrupts */
+};
+
+
+#if 0
+struct ocd_instance {
+        struct otg_instance *otg;
+        void * privdata;
+};
+#endif
+
+#ifndef OTG_APPLICATION
+
+//#define OCD ocd_trace_tag
+extern otg_tag_t OCD;
+extern struct ocd_ops ocd_ops;
+extern struct ocd_instance *ocd_instance;
+#endif
+/* @} */
diff -uNr linux/drivers/no-otg/otg/otg-p2k.h linux/drivers/otg/otg/otg-p2k.h
--- linux/drivers/no-otg/otg/otg-p2k.h	1970-01-01 01:00:00.000000000 +0100
+++ linux/drivers/otg/otg/otg-p2k.h	2006-09-01 21:41:33.000000000 +0200
@@ -0,0 +1,208 @@
+/*
+ * otg/otg/otg-p2k.h
+ *
+ *      Copyright (c) 2004 Belcarra
+ *
+ */
+/*!
+ * @file otg/otg/otg-p2k.h
+ * @brief Linux OS Compatibility defines
+ *
+ * @ingroup OTGCore
+ */
+
+#ifndef _OTG_P2K_H 1
+#define _OTG_P2K_H 1
+
+/*!@name Common Platform 2000 includes
+ */
+/*! * @{ */
+
+/* UNKNOWN SO FAR */
+/*! @} */
+
+/*!@name  Compilation Related
+ */
+
+/*! @{ */
+#define PRAGMAPACK
+#define PACKED 
+#define INLINE __inline
+
+/*! @} */
+
+/*!@name OTG int typedefs
+ */
+/*! * @{ */
+typedef unsigned short u16;
+typedef unsigned char u8;
+typedef unsigned long int u32;
+typedef __int64 u64;
+typedef unsigned char __u8;
+typedef unsigned short __u16;
+typedef unsigned long int __u32;
+typedef __int64 __u64;
+
+/*! @} */
+
+/*!@name  Memory Allocation Primitives
+ *
+ * CKMALLOC()
+ * LSTRDUP()
+ * LKFREE()
+ * LIST_ENTRY()
+ */
+/*! @{ */
+
+#define GFP_KERNEL 0
+#define GFP_ATOMIC 0
+
+#define CKMALLOC(n,f) _ckmalloc(__FILE__, __LINE__, n, f)
+#define LSTRDUP(str) _lstrdup(__FILE__, __LINE__, str)
+#define LKFREE(p) _lkfree(__FILE__, __LINE__, p)
+
+#define OTG_MALLOC_TEST
+#undef OTG_MALLOC_DEBUG
+
+#ifdef OTG_MALLOC_TEST
+    extern int otg_mallocs;
+#endif
+
+static INLINE void *_ckmalloc (const char *func, int line, int n, int f)
+{
+
+	return 0;
+}
+static INLINE char *_lstrdup (const char *func, int line, char *str)
+{
+
+	return 0;
+}
+static INLINE void _lkfree (const char *func, int line, void *p)
+{
+}
+
+// XXX 
+#define LIST_ENTRY(p, t, m) \
+        ((t *)((char *)(p)-(unsigned long)(&((t *)0)->m)))
+
+// XXX
+#define LIST_FOR_EACH(p, h) \
+        for (p = (h)->next, (p->next); p != (h); \
+                p = p->next, (p->next))
+
+#define LIST_ADD_TAIL(n,h) (0)
+#define LIST_ADD_TAIL(n,h) (0)
+#define LIST_DEL(h) (0)
+
+
+/*! @} */
+
+
+/*! @name Atomic Operations
+ *
+ * atomic_post_inc()
+ * atomic_pre_dec()
+ */
+/*! @{ */
+
+/* @} */
+
+
+
+/*! @name Scheduling Primitives
+ *
+ * WORK_STRUCT
+ * WORK_ITEM
+ *
+ * SCHEDULE_TIMEOUT()
+ * SET_WORK_ARG()
+ * SCHEDULE_WORK()
+ * SCHEDULE_IMMEDIATE_WORK()
+ * NO_WORK_DATA()
+ * MOD_DEC_USE_COUNT
+ * MOD_INC_USE_COUNT
+ */
+/*! @{ */
+#define HZ      60
+#define SCHEDULE_TIMEOUT(seconds)
+
+    #define SCHEDULE_WORK(item) (0)
+#define PENDING_WORK_ITEM(item) (0)
+#define PREPARE_WORK_ITEM(__item,__routine,__data) (0)
+
+/*! @} */
+
+
+/*! @name Semaphores
+ *
+ * up()
+ * down()
+ */
+/*! @{ */
+#define UP(s) 
+#define DOWN(s) 
+/*! @} */
+
+
+/*!@name Printk
+ *
+ * PRINTK()
+ */
+/*! @{ */
+//#define PRINTK(s) printk(s)
+//          DEBUGMSG(ZONE_INIT,(_T("OTGCORE - CORE - %s"), _T(msg)));
+
+/*! @} */
+
+/*! @name Little Endian Macros
+ * /
+ *! @{ 
+ */
+
+__inline u16 cpu_to_le16 ( u16 x )
+{
+        return (x<<8) | (x>>8);
+}
+
+__inline u16 le16_to_cpu ( u16 val )
+{
+        return ((((val) >> 8) & 0xff) | (((val) & 0xff) << 8));
+}
+
+__inline u32 le32_to_cpu (u32 val)
+{
+        return (((val) & 0xff000000) >> 24) | (((val) & 0x00ff0000) >>  8) |
+                (((val) & 0x0000ff00) <<  8) | (((val) & 0x000000ff) << 24);
+}
+/*! @} */
+
+/*! @name little endian macros
+ */
+/*! @{ */
+#define local_irq_save(f) f = 0; __asm cli
+#define local_irq_restore(f) __asm sti
+
+/*! @} */
+
+/*! @name Ioctl
+ */
+/*! @{ */
+#define _IOR(m,n,s) ((m)|(n))
+#define _IOW(m,n,s) ((m)|(n))
+#define _IOCSIZE(a) (sizeof(a))
+#define copy_from_user(d,s,n) memcpy(d,s,n)
+#define copy_to_user(d,s,n) memcpy(d,s,n)
+
+/*! @} */
+
+/*! @name ERRNO
+ */
+/*! @{ */
+#define EINVAL  WSAEINVAL
+
+/*! @} */
+
+
+
+#endif /* _OTG_P2K_H */
diff -uNr linux/drivers/no-otg/otg/otg-pcd.h linux/drivers/otg/otg/otg-pcd.h
--- linux/drivers/no-otg/otg/otg-pcd.h	1970-01-01 01:00:00.000000000 +0100
+++ linux/drivers/otg/otg/otg-pcd.h	2006-09-01 21:41:33.000000000 +0200
@@ -0,0 +1,84 @@
+/*
+ * otg/otg-pcd.h - OTG Peripheral Controller Driver
+ *
+ *      Copyright (c) 2004 Belcarra
+ *
+ * By: 
+ *      Stuart Lynne <sl@belcarra.com>, 
+ *      Tom Rushworth <tbr@belcarra.com>, 
+ *      Bruce Balden <balden@belcarra.com>
+ *
+ */
+
+/*!
+ * @defgroup OTGPCD Peripheral Controller Driver Support
+ * @ingroup onthegogroup
+ */
+/*!
+ * @file otg/otg/otg-pcd.h
+ * @brief Defines common to On-The-Go Peripheral Controller Support
+ *
+ * This file defines the pcd_ops and pcd_instance structures.
+ *
+ * The pcd_ops structure contains all of the output functions that will
+ * be called as required by the OTG event handler when changing states.
+ *
+ * The pcd_instance structure is used to maintain the global data
+ * required by the peripheral controller drivers.
+ *
+ * @ingroup OTGPCD
+ */
+
+/*!
+ * @name PCD Peripheral Controller Driver
+ * @{
+ */
+typedef u16 (*framenum_t)(void);
+
+/*!
+ * @struct pcd_ops
+ *
+ * The pcd_ops structure contains pointers to all of the functions implemented for the
+ * linked in driver. Having them in this structure allows us to easily determine what
+ * functions are available without resorting to ugly compile time macros or ifdefs
+ *
+ * There is only one instance of this, defined in the device specific lower layer.
+ */
+struct pcd_ops {
+
+        int (*mod_init) (void);                         /*!< PCD Module Initialization */
+        void (*mod_exit) (void);                        /*!< PCD Module Exit */
+       
+        otg_output_proc_t       pcd_init_func;          /*!< OTG calls to initialize or de-initialize the PCD */
+        otg_output_proc_t       pcd_en_func;            /*!< OTG calls to enable or disable the PCD */
+        otg_output_proc_t       remote_wakeup_func;     /*!< OTG calls to have PCD perform remote wakeup */
+
+        framenum_t		framenum;               /*!< OTG calls to get current USB Frame number */
+};
+
+#define PCD pcd_trace_tag
+extern otg_tag_t PCD;
+extern struct pcd_ops pcd_ops;
+extern struct pcd_instance *pcd_instance;
+
+/*!
+ * @struct pcd_instance
+ */
+struct pcd_instance {
+        struct otg_instance *otg;                       /*!< pointer to OTG Instance */
+        struct usbd_bus_instance *bus;                  /*!< pointer to usb bus instance */
+        void * privdata;                                /*!< pointer to private data for PCD */
+        int pcd_exiting;                                /*!< non-zero if OTG is unloading */
+        struct WORK_STRUCT bh;                          /*!< work structure for bottom half handler */
+        
+};
+
+
+#if !defined(OTG_C99)
+extern void pcd_global_init(void);
+#endif /* !defined(OTG_C99) */
+extern void pcd_init_func(struct otg_instance *, u8 );
+extern void pcd_en_func(struct otg_instance *, u8 );
+
+/* @} */
+
diff -uNr linux/drivers/no-otg/otg/otg-tcd.h linux/drivers/otg/otg/otg-tcd.h
--- linux/drivers/no-otg/otg/otg-tcd.h	1970-01-01 01:00:00.000000000 +0100
+++ linux/drivers/otg/otg/otg-tcd.h	2006-09-01 21:41:33.000000000 +0200
@@ -0,0 +1,114 @@
+/*
+ * otg/otg-tcd.h
+ *
+ *      Copyright (c) 2004 Belcarra
+ *
+ * By: 
+ *      Stuart Lynne <sl@belcarra.com>, 
+ *      Tom Rushworth <tbr@belcarra.com>, 
+ *      Bruce Balden <balden@belcarra.com>
+ *
+ */
+
+/*!
+ * @defgroup OTGTCD Transceiver Controller Driver Support
+ * @ingroup onthegogroup
+ */
+/*!
+ * @file otg/otg/otg-tcd.h
+ * @brief Defines common to On-The-Go Transceiver Controller Support
+ *
+ * This file defines the tcd_ops and tcd_instance structures.
+ *
+ * The tcd_ops structure contains all of the output functions that will
+ * be called as required by the OTG event handler when changing states.
+ *
+ * The tcd_instance structure is used to maintain the global data
+ * required by the transceiver controller drivers.
+ *
+ * @ingroup OTGTCD
+ */
+
+/*!
+ * @name TCD Transceiver Controller Driver
+ * @{
+ */
+
+
+/*!
+ * @struct tcd_ops
+ * The pcd_ops structure contains pointers to all of the functions implemented for the
+ * linked in driver. Having them in this structure allows us to easily determine what
+ * functions are available without resorting to ugly compile time macros or ifdefs
+ *
+ * There is only one instance of this, defined in the device specific lower layer.
+ */
+struct tcd_ops {
+        u64 initial_state;
+        
+        void * privdata;
+
+        /* Driver Initialization - by degrees
+         */
+        int (*mod_init) (void);                         /*!< TCD Module Initialization */
+        void (*mod_exit) (void);                        /*!< TCD Module Exit */
+
+
+        /* 3. called for usbd_vbus() if defined */
+        int (*vbus) (struct otg_instance *);            /* return non-zero if the Vbus valid */
+        int (*id) (struct otg_instance *);              /* return non-zero if the ID valid */
+
+        /* 4. called by otg event to control outputs
+         */
+
+        otg_output_proc_t       tcd_init_func;          /*!< OTG calls to initialize or de-initialize the HCD */
+        otg_output_proc_t       tcd_en_func;            /*!< OTG calls to enable or disable the TCD */
+        otg_output_proc_t       chrg_vbus_func;         /*!< OTG calls to enable or disable charging Vbus */
+        otg_output_proc_t       drv_vbus_func;          /*!< OTG calls to enable or disable Vbus */
+        otg_output_proc_t       dischrg_vbus_func;      /*!< OTG calls to enable or disable discharging Vbus */
+        otg_output_proc_t       dp_pullup_func;         /*!< OTG calls to enable or disable D+ pullup (aka loc_conn) */
+        otg_output_proc_t       dm_pullup_func;         /*!< OTG calls to enable or disable D- pullup (aka loc_carkit) */
+        otg_output_proc_t       dp_pulldown_func;       /*!< OTG calls to enable or disable D+ pulldown (aka loc_conn) */
+        otg_output_proc_t       dm_pulldown_func;       /*!< OTG calls to enable or disable D- pulldown (aka loc_carkit) */
+        otg_output_proc_t       peripheral_host_func;   /*!< OTG calls to enable or disable D- pulldown (aka loc_carkit) */
+        otg_output_proc_t       overcurrent_func;       /*!< OTG calls to clear overcurrent indication */
+        otg_output_proc_t       dm_det_func;            /*!< OTG calls to enable or disable D+ pullup detection */
+        otg_output_proc_t       dp_det_func;            /*!< OTG calls to enable or disable D- pullup detection */
+        otg_output_proc_t       cr_det_func;            /*!< OTG calls to enable or disable D+ CRINT detection */
+        otg_output_proc_t       charge_pump_func;       /*!< OTG calls to enable or disable external charge pump */
+        otg_output_proc_t       bdis_acon_func;         /*!< OTG calls to enable or disable the BDIS ACON feature */
+        otg_output_proc_t       mx21_vbus_drain_func;   /*!< OTG calls to enable or disable the mx21 vbus drain feature */
+        otg_output_proc_t       id_pulldown_func;       /*!< OTG calls to enable or disable ID pulldown to ground */
+        otg_output_proc_t       audio_func;             /*!< OTG calls to enable or disable audio function */
+        otg_output_proc_t       uart_func;              /*!< OTG calls to enable or disable uart function */
+        otg_output_proc_t       mono_func;              /*!< OTG calls to enable or disable mono function */
+};
+
+/*!
+ * @struct hcd_instance
+ */
+struct tcd_instance {
+        struct otg_instance *otg;                       /*!< pointer to OTG Instance */
+        struct WORK_STRUCT bh;                          /*!< work structure for bottom half handler */
+};
+
+
+extern struct trace_ops trace_ops;
+extern struct tcd_ops tcd_ops;
+
+
+extern struct tcd_instance *tcd_instance;
+extern int tcd_vbus (struct otg_instance *otg);
+
+/* pcd_cable_event[_irq] - these can be called in a traditional PCD implementation 
+ * to set b_sess_vld appropriately when vbus is sensed (pcd must implement ocd_ops.vbus)
+ */
+extern void tcd_cable_event_irq (struct otg_instance *);
+extern void tcd_cable_event (struct otg_instance *);
+
+//extern void tcd_init(struct otg_instance *, u8 );
+
+#define TCD tcd_trace_tag
+extern otg_tag_t TCD;
+
+/* @} */
diff -uNr linux/drivers/no-otg/otg/otg-trace.h linux/drivers/otg/otg/otg-trace.h
--- linux/drivers/no-otg/otg/otg-trace.h	1970-01-01 01:00:00.000000000 +0100
+++ linux/drivers/otg/otg/otg-trace.h	2006-09-01 21:41:33.000000000 +0200
@@ -0,0 +1,404 @@
+/*
+ * otg/otgcore/otg-trace.h
+ *
+ *      Copyright (c) 2004 Belcarra
+ *
+ * By: 
+ *      Stuart Lynne <sl@belcarra.com>, 
+ *      Tom Rushworth <tbr@belcarra.com>, 
+ *
+ */
+/*!
+ * @file otg/otg/otg-trace.h
+ * @brief Core Defines for USB OTG Core Layaer
+ *
+ * Fast Trace Utility
+ * This set of definitions and code is meant to provide a _fast_ debugging facility
+ * (much faster than printk) so that time critical code can be debugged by looking
+ * at a trace of events provided by reading a file in the procfs without affecting
+ * the timing of events in the critical code.
+ *
+ * The mechanism used it to allocate a (large) ring buffer of relatively small structures
+ * that include location and high-res timestamp info, and up to 8 bytes of optional
+ * data.  Values are stored and timestamps are taken as the critical code runs, but
+ * data formatting and display are done during the procfs read, when more time is
+ * available :).
+ *
+ * Note that there is usually some machine dependent code involved in getting the
+ * high-res timestamp, and there may be other bits used just to keep the overall
+ * time impact as low as possible.
+ *
+ * Varargs up to 9 arguments are now supported, but you have to supply the number
+ * of args, since examining the format string for the number at trace event time
+ * was deemed too expensive time-wise.
+ * 
+ *
+ * @ingroup OTGCore
+ */
+
+#ifndef OTG_TRACE_H
+#define OTG_TRACE_H 1
+
+//#include <linux/interrupt.h>
+
+
+
+typedef u8 otg_tag_t;  // 1-origin tags (0 ==> invalid/unused trace entry.  Max is 32.
+
+#ifdef PRAGMAPACK
+#pragma pack(push,1)
+#endif /* PRAGMAPACK */
+typedef enum otg_trace_types {
+        otg_trace_msg_invalid_n,
+        otg_trace_msg_va_start_n,
+        otg_trace_msg_va_list_n,
+        otg_trace_setup_n,
+        otg_trace_msg_n,
+        otg_trace_msg32_n,
+        otg_trace_msg16_n,
+        otg_trace_msg8_n
+} PACKED otg_trace_types_t;
+#ifdef PRAGMAPACK
+#pragma pack(pop)
+#endif /* PRAGMAPACK */
+
+typedef struct otg_trace_msg {
+        char    *msg;
+} otg_trace_msg_t;
+
+typedef struct otg_trace_msg32 {
+        char    *msg;
+        u32      val;
+} otg_trace_msg32_t;
+
+typedef struct otg_trace_msg16 {
+        char    *msg;
+        u16     val0;
+        u16     val1;
+} otg_trace_msg16_t;
+
+typedef struct otg_trace_msg8 {
+        char    *msg;
+        u8      val0;
+        u8      val1;
+        u8      val2;
+        u8      val3;
+} otg_trace_msg8_t;
+
+#define OTG_TRACE_MAX_IN_VA                  7
+typedef struct otg_trace_msg_va_list {
+        u32     val[OTG_TRACE_MAX_IN_VA];
+} otg_trace_msg_va_list_t;
+
+typedef struct otg_trace_msg_va_start {
+        const char    *function;
+        u32     interrupts;
+        u64     ticks;
+        u16     framenum;
+        u16     dummy;
+
+        union {
+                otg_trace_msg_t        msg;
+                otg_trace_msg8_t       msg8;
+                otg_trace_msg16_t      msg16;
+                otg_trace_msg32_t      msg32;
+
+#if !defined(BELCARRA_DEBUG)
+                struct usbd_device_request       setup;
+#else
+                unsigned char setup[8];
+#endif /* !defined(BELCARRA_DEBUG) */
+
+        } trace;
+} PACKED otg_trace_msg_va_start_t;
+
+typedef struct trace {
+        otg_trace_types_t        otg_trace_type;
+        otg_tag_t                tag;
+        u8                       va_num_args;
+        u8                       in_interrupt:1;
+        u8                       id_gnd:1;
+        union {
+                otg_trace_msg_va_start_t s;
+                otg_trace_msg_va_list_t l;
+        } va;
+} PACKED otg_trace_t;
+
+#define TRACE_MAX_IS_2N 1
+#define TRACE_MAX  0x00008000
+#define TRACE_MASK 0x00007FFF
+//#define TRACE_MAX  0x000080
+//#define TRACE_MASK 0x00007F
+
+
+#if defined(CONFIG_OTG_TRACE) || defined(CONFIG_OTG_TRACE_MODULE)
+
+#if !defined(OLD_TRACE_API)
+
+// The new API hides otg_trace_get_next(), and adds otg_trace_setup() as part of the core.
+
+#if !defined(BELCARRA_DEBUG)
+extern void otg_trace_setup(otg_tag_t tag, const char *fn, void *setup);
+#define TRACE_SETUP(tag,setup)     otg_trace_setup(tag,__FUNCTION__,setup)
+#endif
+#define TRACE_MSG0(tag, msg)       otg_trace_msg(tag, __FUNCTION__, 0, msg,  0)                                
+#define TRACE_FMSG0(tag, f, msg)   otg_trace_msg(tag, f, 0, msg,  0)                                
+
+#else
+
+extern int otg_trace_first;
+extern int otg_trace_last_read;
+extern int otg_trace_next;
+
+#if 0
+
+extern otg_trace_t *otg_traces;
+
+
+otg_trace_t *otg_trace_get_next(otg_tag_t tag, const char *fn, otg_trace_types_t otg_trace_type, int n);
+
+#if !defined(BELCARRA_DEBUG)
+/*
+ * Trace a setup packet.
+ * The BELCARRA_DEBUG guard is so the rest of these functions can be used
+ * outside of a USB context, such as debugging TTY flow control.
+ */
+static __inline__ void otg_trace_setup(otg_tag_t tag, const char *fn, struct usbd_device_request *setup)
+{
+        if (otg_traces) {
+                otg_trace_t *p = otg_trace_get_next(tag,fn,otg_trace_setup_n,1);
+                memcpy(&p->va.s.trace.setup, setup, sizeof(struct usbd_device_request));
+        }
+}
+#define TRACE_SETUP(tag,setup) otg_trace_setup(tag,__FUNCTION__,setup)
+#endif /* !defined(BELCARRA_DEBUG) */
+
+/*
+ * Trace a single non-parameterized message.
+ */
+static __inline__ void otg_trace_msg_0(otg_tag_t tag, const char *fn, char *msg)
+{
+        if (otg_traces) {
+                otg_trace_t *p = otg_trace_get_next(tag,fn,otg_trace_msg_n,1);
+                p->va.s.trace.msg.msg = msg;
+        }
+}
+#define TRACE_MSG0(tag, msg) otg_trace_msg_0(tag, __FUNCTION__, msg)
+#define TRACE_FMSG0(tag, f, msg) otg_trace_msg_0(tag, f, msg)
+
+/*
+ * Trace a message with a single 32-bit value.
+ */
+static __inline__ void otg_trace_msg_1xU32(otg_tag_t tag, const char *fn, char *fmt, u32 val)
+{
+        if (otg_traces) {
+                otg_trace_t *p = otg_trace_get_next(tag,fn,otg_trace_msg32_n,1);
+                p->va.s.trace.msg32.val = val;
+                p->va.s.trace.msg32.msg = fmt;
+        }
+}
+#define OTRACE_MSG1(tag,fmt,val) otg_trace_msg_1xU32(tag,__FUNCTION__,fmt,(u32)val)
+
+/*
+ * Trace a message with two 16-bit values.
+ */
+static __inline__ void otg_trace_msg_2xU16(otg_tag_t tag, const char *fn, char *fmt, u16 val0, u16 val1)
+{
+        if (otg_traces) {
+                otg_trace_t *p = otg_trace_get_next(tag,fn,otg_trace_msg16_n,1);
+                p->va.s.trace.msg16.val0 = val0;
+                p->va.s.trace.msg16.val1 = val1;
+                p->va.s.trace.msg16.msg = fmt;
+        }
+}
+#define OTRACE_MSG2(tag,fmt,val0,val1) otg_trace_msg_2xU16(tag,__FUNCTION__,fmt,(u16)val0,(u16)val1)
+
+/*
+ * Trace a message with four 8-bit values.
+ */
+static __inline__ void otg_trace_msg_4xU8(otg_tag_t tag, const char *fn, char *fmt, u8 val0, u8 val1, u8 val2, u8 val3)
+{
+        if (otg_traces) {
+                otg_trace_t *p = otg_trace_get_next(tag,fn,otg_trace_msg8_n,1);
+                p->va.s.trace.msg8.val0 = val0;
+                p->va.s.trace.msg8.val1 = val1;
+                p->va.s.trace.msg8.val2 = val2;
+                p->va.s.trace.msg8.val3 = val3;
+                p->va.s.trace.msg8.msg = fmt;
+        }
+}
+#define OTRACE_MSG4(tag,fmt,val0,val1,val2,val3) otg_trace_msg_4xU8(tag,__FUNCTION__,fmt,(u8)val0,(u8)val1,(u8)val2,(u8)val3)
+#endif
+
+#endif
+
+extern void otg_trace_msg(otg_tag_t tag, const char *fn, u8 nargs, char *fmt, ...);
+
+#define eprintf(format, args...)  printk (KERN_INFO format , ## args)
+
+#define TRACE_MSG(tag, nargs, fmt, args...) otg_trace_msg(tag, __FUNCTION__, nargs, fmt, ## args)
+
+#define TRACE_MSG1(tag, fmt, a1) \
+        otg_trace_msg(tag, __FUNCTION__, 1, fmt,  a1)                                
+#define TRACE_MSG2(tag, fmt, a1, a2) \
+        otg_trace_msg(tag, __FUNCTION__, 2, fmt,  a1, a2)                            
+#define TRACE_MSG3(tag, fmt, a1, a2, a3) \
+        otg_trace_msg(tag, __FUNCTION__, 3, fmt,  a1, a2, a3)                        
+#define TRACE_MSG4(tag, fmt, a1, a2, a3, a4) \
+        otg_trace_msg(tag, __FUNCTION__, 4, fmt,  a1, a2, a3, a4)                    
+#define TRACE_MSG5(tag, fmt, a1, a2, a3, a4, a5) \
+        otg_trace_msg(tag, __FUNCTION__, 5, fmt,  a1, a2, a3, a4, a5)                
+#define TRACE_MSG6(tag, fmt, a1, a2, a3, a4, a5, a6) \
+        otg_trace_msg(tag, __FUNCTION__, 6, fmt,  a1, a2, a3, a4, a5, a6)            
+#define TRACE_MSG7(tag, fmt, a1, a2, a3, a4, a5, a6, a7) \
+        otg_trace_msg(tag, __FUNCTION__, 7, fmt,  a1, a2, a3, a4, a5, a6, a7)        
+#define TRACE_MSG8(tag, fmt, a1, a2, a3, a4, a5, a6, a7, a8) \
+        otg_trace_msg(tag, __FUNCTION__, 8, fmt,  a1, a2, a3, a4, a5, a6, a7, a8)    
+#if 0
+#define TRACE_MSG9(tag, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9) \
+        otg_trace_msg(tag, __FUNCTION__, 9, fmt,  a1, a2, a3, a4, a5, a6, a7, a8, a9)
+#endif
+#define TRACE_FMSG1(tag, f, fmt, a1) \
+        otg_trace_msg(tag, f, 1, fmt,  a1)                                
+#define TRACE_FMSG2(tag, f, fmt, a1, a2) \
+        otg_trace_msg(tag, f, 2, fmt,  a1, a2)                            
+#define TRACE_FMSG3(tag, f, fmt, a1, a2, a3) \
+        otg_trace_msg(tag, f, 3, fmt,  a1, a2, a3)                        
+#define TRACE_FMSG4(tag, f, fmt, a1, a2, a3, a4) \
+        otg_trace_msg(tag, f, 4, fmt,  a1, a2, a3, a4)                    
+#define TRACE_FMSG5(tag, f, fmt, a1, a2, a3, a4, a5) \
+        otg_trace_msg(tag, f, 5, fmt,  a1, a2, a3, a4, a5)                
+#define TRACE_FMSG6(tag, f, fmt, a1, a2, a3, a4, a5, a6) \
+        otg_trace_msg(tag, f, 6, fmt,  a1, a2, a3, a4, a5, a6)            
+#define TRACE_FMSG7(tag, f, fmt, a1, a2, a3, a4, a5, a6, a7) \
+        otg_trace_msg(tag, f, 7, fmt,  a1, a2, a3, a4, a5, a6, a7)        
+#define TRACE_FMSG8(tag, f, fmt, a1, a2, a3, a4, a5, a6, a7, a8) \
+        otg_trace_msg(tag, f, 8, fmt,  a1, a2, a3, a4, a5, a6, a7, a8)    
+#if 0
+#define TRACE_FMSG9(tag, f, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9) \
+        otg_trace_msg(tag, f, 9, fmt,  a1, a2, a3, a4, a5, a6, a7, a8, a9)
+#endif
+extern otg_tag_t otg_trace_obtain_tag(void);
+extern otg_tag_t otg_trace_invalidate_tag(otg_tag_t tag);
+
+#elif defined(OTG_WINCE)
+
+#define TRACE_MSG0(tag, msg) \
+          DEBUGMSG(ZONE_INIT,(_T("OTGCORE - CORE - %s"), _T(msg)))
+
+#define TRACE_MSG1(tag, fmt, a1) \
+		  DEBUGMSG(ZONE_INIT,(_T("OTGCORE - CORE - %s %d"), _T(fmt), a1))
+
+#define TRACE_MSG2(tag, fmt, a1, a2) \
+		  DEBUGMSG(ZONE_INIT,(_T("OTGCORE - CORE - %s %d %d"), _T(fmt), a1, a2))
+
+#define TRACE_MSG3(tag, fmt, a1, a2, a3) \
+		  DEBUGMSG(ZONE_INIT,(_T("OTGCORE - CORE - %s %d %d"), _T(fmt), a1, a2, a3));
+
+#define TRACE_MSG4(tag, fmt, a1, a2, a3, a4) \
+		  DEBUGMSG(ZONE_INIT,(_T("OTGCORE - CORE - %s %d %d %d %d"), _T(fmt), a1, a2, a3, a4));
+
+#define TRACE_MSG5(tag, fmt, a1, a2, a3, a4, a5) \
+		  DEBUGMSG(ZONE_INIT,(_T("OTGCORE - CORE - %s %d %d %d %d %d"), _T(fmt), a1, a2, a3, a4, a5));
+
+#define TRACE_MSG6(tag, fmt, a1, a2, a3, a4, a5, a6) \
+		  DEBUGMSG(ZONE_INIT,(_T("OTGCORE - CORE - %s %d %d %d %d %d %d %d"), _T(fmt), a1, a2, a3, a4, a5, a6));
+
+#define TRACE_MSG7(tag, fmt, a1, a2, a3, a4, a5, a6, a7) \
+		  DEBUGMSG(ZONE_INIT,(_T("OTGCORE - CORE - %s %d %d %d %d %d %d %d %d"), _T(fmt), a1, a2, a3, a4, a5, a6, a7));
+
+#define TRACE_MSG8(tag, fmt, a1, a2, a3, a4, a5, a6, a7, a8) \
+		  DEBUGMSG(ZONE_INIT,(_T("OTGCORE - CORE - %s %d %d %d %d %d %d %d %d %d"), _T(fmt), a1, a2, a3, a4, a5, a6, a7, a8));
+
+extern otg_tag_t otg_trace_obtain_tag(void);
+extern otg_tag_t otg_trace_invalidate_tag(otg_tag_t tag);
+
+/*
+#define TRACE_OTG_CURRENT(o) \
+          DEBUGMSG(ZONE_INIT,(_T("OTGCORE - CURRENT - %s (%s) RESET: %08x SET: %08x"),\
+                                otg_state_names[o->state], \
+                                otg_state_names[o->previous], \
+                                (u32)(~o->current_inputs), \
+                                (u32)(o->current_inputs))\
+						);
+
+#define TRACE_OTG_CHANGE(o) \
+          DEBUGMSG(ZONE_INIT,(_T("OTGCORE - CHANGE - %s (%s) RESET: %08x SET: %08x"),\
+                                otg_state_names[o->state], \
+                                otg_state_names[o->previous], \
+                                (u32)(~o->current_inputs), \
+                                (u32)(o->current_inputs))\
+						);
+
+#define TRACE_OTG_NEW(o) \
+          DEBUGMSG(ZONE_INIT,(_T("OTGCORE - NEW - %s (%s) RESET: %08x SET: %08x"),\
+                                otg_state_names[o->state], \
+                                otg_state_names[o->previous], \
+                                (u32)(~o->current_inputs), \
+                                (u32)(o->current_inputs))\
+						);
+
+#define TRACE_OTG_INPUTS(u) \
+          DEBUGMSG(ZONE_INIT,(_T("OTGCORE - INPUTS - RESET: %08x SET: %08x"),\
+                                (u32)(u >> 32), \
+                                (u32)(u & 0xffffffff))\
+						);
+*/
+
+extern otg_tag_t otg_trace_obtain_tag(void);
+extern otg_tag_t otg_trace_invalidate_tag(otg_tag_t tag);
+
+#else /* defined(CONFIG_OTG_TRACE) || defined(CONFIG_OTG_TRACE_MODULE) */
+
+#define TRACE_SETUP(tag,setup)
+
+//#define TRACE_MSG(tag,nargs,fmt,...)
+//#define TRACE_FMSG(tag, nargs,fmt,...)
+
+#define TRACE_MSG0(tag, fmt) 
+#define TRACE_MSG1(tag, fmt, a1) 
+#define TRACE_MSG2(tag, fmt, a1, a2) 
+#define TRACE_MSG3(tag, fmt, a1, a2, a3) 
+#define TRACE_MSG4(tag, fmt, a1, a2, a3, a4) 
+#define TRACE_MSG5(tag, fmt, a1, a2, a3, a4, a5) 
+#define TRACE_MSG6(tag, fmt, a1, a2, a3, a4, a5, a6) 
+#define TRACE_MSG7(tag, fmt, a1, a2, a3, a4, a5, a6, a7) 
+#define TRACE_MSG8(tag, fmt, a1, a2, a3, a4, a5, a6, a7, a8) 
+//#define TRACE_MSG9(tag, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9) 
+
+#define TRACE_FMSG0(tag, f, fmt) 
+#define TRACE_FMSG1(tag, f, fmt, a1) 
+#define TRACE_FMSG2(tag, f, fmt, a1, a2) 
+#define TRACE_FMSG3(tag, f, fmt, a1, a2, a3) 
+#define TRACE_FMSG4(tag, f, fmt, a1, a2, a3, a4) 
+#define TRACE_FMSG5(tag, f, fmt, a1, a2, a3, a4, a5) 
+#define TRACE_FMSG6(tag, f, fmt, a1, a2, a3, a4, a5, a6) 
+#define TRACE_FMSG7(tag, f, fmt, a1, a2, a3, a4, a5, a6, a7) 
+#define TRACE_FMSG8(tag, f, fmt, a1, a2, a3, a4, a5, a6, a7, a8) 
+//#define TRACE_FMSG9(tag, f, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9) 
+
+//static inline otg_tag_t otg_trace_obtain_tag(void)
+//{
+//        return 0;
+//}
+
+//static inline otg_tag_t otg_trace_invalidate_tag(otg_tag_t tag)
+//{
+//        return 0;
+//}
+
+extern otg_tag_t otg_trace_obtain_tag(void);
+extern otg_tag_t otg_trace_invalidate_tag(otg_tag_t tag);
+
+#endif /* defined(CONFIG_OTG_TRACE) || defined(CONFIG_OTG_TRACE_MODULE) */
+
+#define USBD usbd_trace_tag
+extern otg_tag_t USBD;
+
+//#define ACM acm_trace_tag
+//extern otg_tag_t ACM;
+
+//#define NETWORK network_trace_tag
+//extern otg_tag_t NETWORK;
+
+#endif /* OTG_TRACE_H */
diff -uNr linux/drivers/no-otg/otg/otg-utils.h linux/drivers/otg/otg/otg-utils.h
--- linux/drivers/no-otg/otg/otg-utils.h	1970-01-01 01:00:00.000000000 +0100
+++ linux/drivers/otg/otg/otg-utils.h	2006-09-01 21:41:33.000000000 +0200
@@ -0,0 +1,151 @@
+/*
+ * otg/otg/otg-utils.h
+ *
+ *      Basic utilities for OTG package
+ *      Copyright (c) 2004 Belcarra
+ */
+/*!
+ * @file otg/otg/otg-utils.h
+ * @brief Useful macros.
+ *
+ *
+ * @ingroup OTGCore
+ */
+
+
+#if !defined(_OTG_UTILS_H)
+#define _OTG_UTILS_H 1
+
+/*! @name Min, Max and zero
+ * @{
+ */
+#ifndef MIN
+#define MIN(a,b) ((a) < (b) ? (a) : (b))
+#endif
+#ifndef MAX
+#define MAX(a,b) ((a) > (b) ? (a) : (b))
+#endif
+
+#define ZERO(a) memset(&a, 0, sizeof(a))
+
+/* @} */
+
+/*! @name TRUE and FALSE
+ *
+ * @{
+ */
+#if !defined(TRUE)
+#define TRUE 1
+#endif
+
+#if !defined(FALSE)
+#define FALSE 0
+#endif
+/* @} */
+
+
+/*! @name CATCH and THROW
+ *
+ * These macros implement a conceptually clean way to use
+ * C's goto to implement a standard error handler. The typical forms are:
+ *
+ *      THROW(error);                   // continue execution in CATCH statement
+ *      THROW_IF(test, error);          // continue execution in CATCH statement if test true
+ *      THROW_UNLESS(test, error);      // continue execution in CATCH statement if test false
+ *      
+ *      // program execution will continue after the CATCH statement;
+ *      CATCH(error) {
+ *              // handle error here
+ *      }
+ *
+ * @{
+ */
+#define CATCH(x) while(0) x:
+#define THROW(x) goto x
+#define THROW_IF(e, x) if (e)  goto x; 
+#define THROW_UNLESS(e, x) UNLESS (e)  goto x; 
+/* @} */
+
+
+/*! @name UNLESS
+ *
+ * This implements a simple equivalent to negated if (expression).
+ *
+ *      Unless expression then statement else statement.
+ *
+ * @{
+ */
+#define unless(x) if(!(x))
+#define UNLESS(x) if(!(x))
+/* @} */
+
+/*! @name BREAK_ and CONTINUE_
+ *
+ * These implement a conditional break and continue statement.
+ *
+ * @{
+ */
+#define BREAK_UNLESS(x) UNLESS (x)  break; 
+#define BREAK_IF(x) if (x)  break; 
+#define CONTINUE_IF(x) if (x)  continue; 
+#define CONTINUE_UNLESS(x) UNLESS (x)  continue; 
+
+/* @} */
+
+
+/*! @name RETURN_
+ *
+ * These implement conditional return statement.
+ * @{
+ */
+#define RETURN_IF(x) if (x)  return; 
+#define RETURN_ZERO_IF(x) if (x)  return 0; 
+#define RETURN_NULL_IF(x) if (x)  return NULL; 
+#define RETURN_EBUSY_IF(x) if (x)  return -EBUSY; 
+#define RETURN_EFAULT_IF(x) if (x)  return -EFAULT; 
+#define RETURN_EINVAL_IF(x) if (x)  return -EINVAL; 
+#define RETURN_ENOMEM_IF(x) if (x)  return -ENOMEM; 
+#define RETURN_EAGAIN_IF(x) if (x)  return -EAGAIN; 
+#define RETURN_ETIMEDOUT_IF(x) if (x)  return -ETIMEDOUT; 
+#define RETURN_EPIPE_IF(x) if (x)  return -EPIPE; 
+#define RETURN_IRQ_HANDLED_IF(x) if (x)  return IRQ_HANDLED; 
+#define RETURN_TRUE_IF(x) if (x)  return TRUE; 
+#define RETURN_FALSE_IF(x) if (x)  return FALSE; 
+
+#define RETURN_UNLESS(x) UNLESS (x)  return; 
+#define RETURN_NULL_UNLESS(x) UNLESS (x)  return NULL; 
+#define RETURN_ZERO_UNLESS(x) UNLESS (x)  return 0; 
+#define RETURN_EBUSY_UNLESS(x) UNLESS (x)  return -EBUSY; 
+#define RETURN_EFAULT_UNLESS(x) UNLESS (x)  return -EFAULT; 
+#define RETURN_EINVAL_UNLESS(x) UNLESS (x)  return -EINVAL; 
+#define RETURN_ENOMEM_UNLESS(x) UNLESS (x)  return -ENOMEM; 
+#define RETURN_EAGAIN_UNLESS(x) UNLESS (x)  return -EAGAIN; 
+#define RETURN_ETIMEDOUT_UNLESS(x) UNLESS (x)  return -ETIMEDOUT; 
+#define RETURN_EPIPE_UNLESS(x) UNLESS (x)  return -EPIPE; 
+#define RETURN_IRQ_HANDLED_UNLESS(x) UNLESS (x)  return IRQ_HANDLED; 
+#define RETURN_TRUE_UNLESS(x) UNLESS (x)  return TRUE; 
+#define RETURN_FALSE_UNLESS(x) UNLESS (x)  return FALSE; 
+
+/* @} */
+
+
+/*! @name likely and unlikely
+ *
+ * Under linux these can be used to provide a hint to GCC that
+ * the expression being evaluated is probably going to evaluate
+ * to true (likely) or false (unlikely). The intention is that
+ * GCC can then provide optimal code for that.
+ * @{
+ */
+#ifndef likely
+#define likely(x) x
+#endif
+
+#ifndef unlikely 
+#define unlikely(x) x
+#endif
+/* @} */
+
+
+#endif /* _OTG_UTILS_H */
+
diff -uNr linux/drivers/no-otg/otg/pcd-include.h linux/drivers/otg/otg/pcd-include.h
--- linux/drivers/no-otg/otg/pcd-include.h	1970-01-01 01:00:00.000000000 +0100
+++ linux/drivers/otg/otg/pcd-include.h	2006-09-01 21:41:33.000000000 +0200
@@ -0,0 +1,36 @@
+/*
+ * otg/udc.h
+ *
+ *      Copyright (c) 2004 Belcarra
+ *
+ * By: 
+ *      Stuart Lynne <sl@belcarra.com>, 
+ *      Tom Rushworth <tbr@belcarra.com>, 
+ *      Bruce Balden <balden@belcarra.com>
+ *
+ */
+/*!
+ * @file otg/otg/pcd-include.h
+ * @brief Linux OS Precompiled Headers 
+ *
+ * @ingroup OTGCore
+ */
+
+#include <otg/otg-compat.h>
+#include <otg/otg-module.h>
+
+#include <asm/irq.h>
+#include <asm/system.h>
+#include <asm/io.h>
+
+#include <otg/usbp-chap9.h>
+//#include <otg/usbp-func.h>
+#include <otg/usbp-bus.h>
+#include <otg/otg-trace.h>
+#include <otg/otg-api.h>
+#include <otg/otg-tcd.h>
+#include <otg/otg-hcd.h>
+#include <otg/otg-pcd.h>
+#include <otg/otg-ocd.h>
+#include <otg/usbp-pcd.h>
+
diff -uNr linux/drivers/no-otg/otg/usbp-bus.h linux/drivers/otg/otg/usbp-bus.h
--- linux/drivers/no-otg/otg/usbp-bus.h	1970-01-01 01:00:00.000000000 +0100
+++ linux/drivers/otg/otg/usbp-bus.h	2006-09-01 21:41:33.000000000 +0200
@@ -0,0 +1,354 @@
+/*
+ * otg/otg/usbp-bus.h - USB Device Bus Interface Driver Interface
+ *
+ *      Copyright (c) 2004 Belcarra
+ *
+ * By: 
+ *      Stuart Lynne <sl@belcarra.com>, 
+ *      Tom Rushworth <tbr@belcarra.com>, 
+ *      Bruce Balden <balden@belcarra.com>
+ *
+ */
+
+/*!
+ * @file otg/otg/usbp-bus.h
+ * @brief Bus interface Defines for USB Device Core Layaer
+ *
+ * This file contains the USB Peripheral Driver definitions.
+ *
+ * The 
+ *
+ * This is the interface between the bottom of the USB Core and the top of
+ * the Bus Interace Drivers and is comprised of:
+ *
+ *      o public functions exported by the USB Core layer 
+ *
+ *      o structures and functions passed by the Function Driver to the USB
+ *      Core layer
+ *
+ * USB Peripheral Controller Drivers are structured such that the upper edge
+ * implements interfaces to the USB Peripheral Core layer to provide low
+ * level USB services and the lower edge interfaces to the actual USB
+ * Hardware (typically called the USB Device Controller or UDC.)
+ *
+ * The peripheral controller layer primarily deals with endpoints.
+ * There is one control endpoint and one or more data endpoints.
+ *
+ * The control endpoint is special, with received data processed with
+ * the built in EP0 function. Which in turn will pass requests to 
+ * interface functions as appropriate.
+ *
+ * Data endpoints are controlled by interface driver and have a
+ * function callback specified by the interface driver to perform
+ * whatever needs to be done when data is sent or received.
+ *
+ *
+ *
+ * @ingroup USBP
+ */
+
+struct usbd_endpoint_map; 
+struct usbd_endpoint_instance;
+struct usbd_bus_instance;
+struct usbd_urb;
+
+/*!
+ * USB Bus Interface Driver structures
+ *
+ * Driver description:
+ * 
+ *  struct usbd_bus_operations
+ *  struct usbd_bus_driver
+ *
+ */
+
+extern int usbd_maxstrings;
+
+
+/*!
+ * exported to otg
+ */
+struct usbd_ops {
+        int (*admin) (char *);
+        char * (*function_name) (int);
+};
+
+
+
+/*! Operations that the slave layer or function driver can use to interact
+ * with the bus interface driver. 
+ *
+ * The send_urb() function is used by the usb-device endpoint 0 driver and
+ * function drivers to submit data to be sent.
+ *
+ * The cancel_urb() function is used by the usb-device endpoint 0 driver and
+ * function drivers to remove previously queued data to be sent.
+ *
+ * The endpoint_halted() function is used by the ep0 control function to
+ * check if an endpoint is halted.
+ *
+ * The endpoint_feature() function is used by the ep0 control function to
+ * set/reset device features on an endpoint.
+ *
+ * The device_event() function is used by the usb device core to tell the
+ * bus interface driver about various events.
+ */
+struct usbd_bus_operations {
+        //int (*xbus_serial_number) (struct usbd_bus_instance *, char *);
+
+        int (*start_endpoint_in) (struct usbd_bus_instance *, struct usbd_endpoint_instance *);
+        int (*start_endpoint_out) (struct usbd_bus_instance *, struct usbd_endpoint_instance *);
+        int (*cancel_urb_irq) (struct usbd_urb *);
+        //int (*device_feature) (struct usbd_bus_instance *, int, int);
+        int (*endpoint_halted) (struct usbd_bus_instance *, int, int);
+        int (*halt_endpoint) (struct usbd_bus_instance *, int, int, int);
+        int (*set_address) (struct usbd_bus_instance *, int);
+        int (*event_handler) (struct usbd_bus_instance *, usbd_device_event_t, int);
+        int (*request_endpoints) (struct usbd_bus_instance *, struct usbd_endpoint_map *, 
+                        int, struct usbd_endpoint_request *);
+        int (*set_endpoints) (struct usbd_bus_instance *, int , struct usbd_endpoint_map *);
+        int (*framenum) (void); 
+        u64 (*ticks) (void); 
+        u64 (*elapsed) (u64 *, u64 *); 
+        u64 (*interrupts) (void);
+};
+
+/*! Endpoint Map
+ *
+ * An array of these structures is created by the bus interface driver to
+ * show what endpoints have been configured for the function driver.
+ *
+ * This is the logical array of endpoints that are indexed into by the 
+ * function layer using the logical endpoint numbers defined by the function
+ * drivers. It maps these numbers into a real physical endpoint.
+ *
+ * Fields that can be different for Full speed versus High speed are 
+ * represented with an array both values are available.
+ *
+ * For interfaces with multiple alternate settings, each separate
+ * combinantion of interface_num/altsetting/endpoint must be specified.
+ * It is up to the lower layers to determine if / when overlapped 
+ * endpoints can be re-used.
+ *
+ */
+struct usbd_endpoint_map {
+        u8 configuration;
+        u8 interface_num;
+        u8 alternate;
+        u8 bEndpointAddress[2];         // logical endpoint address
+        u16 wMaxPacketSize[2];          // packetsSize for requested endpoint
+        u8 bmAttributes[2];             // requested endpoint type
+        u16 transferSize[2];            // transferSize for bulk transfers
+        u8 physicalEndpoint[2];         // physical endpoint number
+        struct usbd_endpoint_instance    *endpoint;
+};
+
+
+/*! Endpoint configuration
+ *
+ * Per endpoint configuration data. Used to track which actual physical
+ * endpoints.
+ *
+ * The bus layer maintains an array of these to represent all physical
+ * endpoints.
+ *
+ */
+struct usbd_endpoint_instance {
+        int                             bEndpointAddress;       // logical endpoint address 
+        int                             physical_endpoint;      // physical endpoint address - bus interface specific
+        int                             bmAttributes;           // endpoint type
+        u16                             wMaxPacketSize;         // packet size for requested endpoint
+        u32                             feature_setting;        // save set feature information
+
+        // control
+        int                             state;                  // available for use by bus interface driver
+
+        // receive side
+        struct urb_link                 rdy;                    // empty urbs ready to receive
+        struct usbd_urb                 *rcv_urb;               // active urb
+        u32                             rcv_transferSize;       // maximum transfer size from function driver
+        int                             rcv_error;              // current bulk-in has an error
+
+        // transmit side
+        struct urb_link                 tx;                     // urbs ready to transmit
+        struct usbd_urb                 *tx_urb;                // active urb
+        u32                             tx_transferSize;        // maximum transfer size from function driver
+
+        u32                             sent;                   // data already sent
+        u32                             last;                   // data sent in last packet XXX do we need this
+
+        void                            *privdata;
+
+};
+
+/*! @name endpoint zero states
+ * @{
+ */
+#define WAIT_FOR_SETUP          0
+#define DATA_STATE_XMIT         1
+#define DATA_STATE_NEED_ZLP     2
+#define WAIT_FOR_OUT_STATUS     3
+#define DATA_STATE_RECV         4
+#define DATA_STATE_PENDING_XMIT 5
+#define WAIT_FOR_IN_STATUS      6
+/*! @} */
+
+/*! Bus Interface data structure
+ *
+ * Keep track of specific bus interface. 
+ *
+ * This is passed to the usb-device layer when registering. It contains all
+ * required information about each real bus interface found such that the
+ * usb-device layer can create and maintain a usb-device structure.
+ *
+ * Note that bus interface registration is incumbent on finding specific
+ * actual real bus interfaces. There will be a registration for each such
+ * device found.
+ *
+ * The max_tx_endpoints and max_rx_endpoints are the maximum number of
+ * possible endpoints that this bus interface can support. The default
+ * endpoint 0 is not included in these counts.
+ *
+ */
+struct usbd_bus_driver {
+        char                            *name;
+        u8                              max_endpoints;  // maximimum number of rx enpoints
+        u8                              otg_bmAttributes;
+        u8                              maxpacketsize;
+        u8                              HighSpeedCapable;
+        struct usbd_device_description   *device_description;
+        struct usbd_bus_operations       *bops;
+        void                            *privdata;
+        u32                             capabilities;
+        u8                              bMaxPower;
+        u8                              ports;
+};
+
+/*!
+ * Bus state
+ *
+ * enabled or disabled
+ */
+typedef enum usbd_bus_state {
+        usbd_bus_state_unknown,
+        usbd_bus_state_disabled,
+        usbd_bus_state_enabled
+} usbd_bus_state_t;
+
+
+/*!
+ * @name UDC Capabilities
+ * @{
+ */
+#define HAVE_CABLE_IRQ                  0x0001
+#define REMOTE_WAKEUP_SUPPORTED         0x0002
+#define ROOT_HUB                        0x0004
+/*! @} */
+
+/*! Bus Interface configuration structure
+ *
+ * This is allocated for each configured instance of a bus interface driver.
+ *
+ * It contains a pointer to the appropriate bus interface driver.
+ *
+ * The privdata pointer may be used by the bus interface driver to store private
+ * per instance state information.
+ */
+struct usbd_bus_instance {
+
+        struct usbd_bus_driver           *driver;
+
+        int                              endpoints;
+        struct usbd_endpoint_instance    *endpoint_array;        // array of available configured endpoints
+
+        struct urb_link                 finished;               // processed urbs
+
+        //char                            *serial_number_str;
+
+        usbd_device_status_t             status;                 // device status
+        usbd_bus_state_t                 bus_state;
+        usbd_device_state_t              device_state;           // current USB Device state
+        usbd_device_state_t              suspended_state;        // previous USB Device state
+
+        struct usbd_function_instance    *ep0;                   // ep0 configuration
+        struct usbd_function_instance    *function_instance;
+
+
+        u8                              HighSpeedFlag;
+
+        u8                              ConfigurationValue;     // current set configuration (zero is default)
+        u8                              bNumInterfaces;         // number of interfaces in the current configuration
+        u8                              *alternates;            // array[0..interfaces-1] of alternate settings 
+                                                                // for each interface
+
+        u32                             device_feature_settings;// save set feature information
+        u8                              otg_bmAttributes;
+
+        struct WORK_STRUCT              device_bh;              // runs as bottom half, equivalent to interrupt time
+
+        void                            *privdata;              // private data for the bus interface
+        char                            *arg;
+};
+
+
+
+
+/*! bus driver registration
+ *
+ * Called by bus interface drivers to register themselves when loaded
+ * or de-register when unloading.
+ */
+struct usbd_bus_instance *usbd_register_bus (struct usbd_bus_driver *, int);
+void usbd_deregister_bus (struct usbd_bus_instance *);
+
+
+/*! Enable/Disable Function
+ *
+ * Called by a bus interface driver to select and enable a specific function 
+ * driver.
+ */
+int usbd_enable_function_irq (struct usbd_bus_instance *, char *, char *);
+void usbd_disable_function (struct usbd_bus_instance *);
+
+
+/*! 
+ * usbd_configure_device is used by function drivers (usually the control endpoint)
+ * to change the device configuration.
+ *
+ * usbd_device_event is used by bus interface drivers to tell the higher layers that
+ * certain events have taken place.
+ */
+void usbd_bus_event_handler (struct usbd_bus_instance *, usbd_device_event_t, int);
+void usbd_bus_event_handler_irq (struct usbd_bus_instance *, usbd_device_event_t, int);
+
+
+/*!
+ * usbd_device_request - process a received urb
+ * @urb: pointer to an urb structure
+ *
+ * Used by a USB Bus interface driver to pass received data in a URB to the
+ * appropriate USB Function driver.
+ *
+ * This function must return 0 for success and -EINVAL if the request
+ * is to be stalled.
+ *
+ * Not that if the SETUP is Host to Device with a non-zero wLength then there
+ * *MUST* be a valid receive urb queued OR the request must be stalled.
+ */
+int usbd_device_request (struct usbd_bus_instance*, struct usbd_device_request *);
+
+/*!
+ * Device I/O
+ */
+void usbd_urb_finished (struct usbd_urb *, int );
+void usbd_urb_finished_irq (struct usbd_urb *, int );
+struct usbd_urb *usbd_first_urb_detached_irq (urb_link * hd);
+
+/*! flush endpoint
+ */
+void usbd_flush_endpoint_irq (struct usbd_endpoint_instance *);
+void usbd_flush_endpoint (struct usbd_endpoint_instance *);
+int usbd_find_endpoint_index(struct usbd_bus_instance *bus, int bEndpointAddress);
+
+
+
diff -uNr linux/drivers/no-otg/otg/usbp-chap9.h linux/drivers/otg/otg/usbp-chap9.h
--- linux/drivers/no-otg/otg/usbp-chap9.h	1970-01-01 01:00:00.000000000 +0100
+++ linux/drivers/otg/otg/usbp-chap9.h	2006-09-01 21:41:33.000000000 +0200
@@ -0,0 +1,1577 @@
+/*
+ * otg/otg/usbp-chap9.h
+ *
+ *      Copyright (c) 2004 Belcarra
+ * By:
+ *      Stuart Lynne <sl@belcarra.com>,
+ *      Tom Rushworth <tbr@belcarra.com>,
+ *      Bruce Balden <balden@belcarra.com>
+ *
+ */
+/*!
+ * @defgroup USBP USB Peripheral Support
+ * @ingroup onthegogroup
+ */
+
+ 
+/*!
+ * @file otg/otg/usbp-chap9.h
+ * @brief Chapter 9 and other class descriptor structure definitions.
+ *
+ * USB Descriptors are used to build a configuration database for each USB
+ * Function driver.
+ *
+ * @ingroup USBP
+ */
+
+/*!
+ * @name Device and/or Interface Class Codes
+ */
+
+ /*! @{ */
+
+
+#define USB_CLASS_PER_INTERFACE         0       /* for DeviceClass */
+#define USB_CLASS_AUDIO                 1
+#define USB_CLASS_COMM                  2
+#define USB_CLASS_HID                   3
+#define USB_CLASS_PHYSICAL              5
+#define USB_CLASS_PRINTER               7
+#define USB_CLASS_MASS_STORAGE          8
+#define USB_CLASS_HUB                   9
+#define USB_CLASS_DATA                  10
+#define USB_CLASS_APP_SPEC              0xfe
+#define USB_CLASS_VENDOR_SPEC           0xff
+/*! @} */
+
+
+/*! @name USB Device Request Types (bmRequestType)
+ *C.f. USB 2.0 Table 9-2
+*/
+
+/*! @{ */
+
+#define USB_TYPE_STANDARD               (0x00 << 5)
+#define USB_TYPE_CLASS                  (0x01 << 5)
+#define USB_TYPE_VENDOR                 (0x02 << 5)
+#define USB_TYPE_RESERVED               (0x03 << 5)
+
+#define USB_RECIP_DEVICE                0x00
+#define USB_RECIP_INTERFACE             0x01
+#define USB_RECIP_ENDPOINT              0x02
+#define USB_RECIP_OTHER                 0x03
+
+#define USB_RECIP_HUB                   0x00
+#define USB_RECIP_PORT                  0x03
+
+#define USB_DIR_OUT                     0
+#define USB_DIR_IN                      0x80
+#define USB_ENDPOINT_OPT                0x40
+/*! @} */
+
+/*! @name Descriptor types
+ * C.f. USB Table 9-5
+ */
+
+/*! @{ */
+
+#define USB_DT_DEVICE                           0x01
+#define USB_DT_CONFIGURATION                    0x02
+#define USB_DT_STRING                           0x03
+#define USB_DT_INTERFACE                        0x04
+#define USB_DT_ENDPOINT                         0x05
+#define USB_DT_DEVICE_QUALIFIER                 0x06
+#define USB_DT_OTHER_SPEED_CONFIGURATION        0x07
+#define USB_DT_INTERFACE_POWER                  0x08
+#define USB_DT_OTG                              0x09
+
+
+#define USB_DT_HID                      (USB_TYPE_CLASS | 0x01)
+#define USB_DT_REPORT                   (USB_TYPE_CLASS | 0x02)
+#define USB_DT_PHYSICAL                 (USB_TYPE_CLASS | 0x03)
+#define USB_DT_HUB                      (USB_TYPE_CLASS | 0x09)
+/*! @} */
+
+/*! @name Descriptor sizes per descriptor type
+ */
+ /*! @{ */
+
+#define USB_DT_DEVICE_SIZE              18
+#define USB_DT_CONFIG_SIZE              9
+#define USB_DT_INTERFACE_SIZE           9
+#define USB_DT_ENDPOINT_SIZE            7
+#define USB_DT_ENDPOINT_AUDIO_SIZE      9               /* Audio extension */
+#define USB_DT_HUB_NONVAR_SIZE          7
+#define USB_DT_HID_SIZE                 9
+/*! @} */
+
+/*! @name Endpoints
+ */
+/*! @{ */
+#define USB_ENDPOINT_NUMBER_MASK        0x0f            /* in bEndpointAddress */
+#define USB_ENDPOINT_DIR_MASK           0x80
+
+#define USB_ENDPOINT_MASK               0x03            /* in bmAttributes */
+#define USB_ENDPOINT_CONTROL            0x00
+#define USB_ENDPOINT_ISOCHRONOUS        0x01
+#define USB_ENDPOINT_BULK               0x02
+#define USB_ENDPOINT_INTERRUPT          0x03
+/*! @} */
+
+/*!
+ * @name USB Packet IDs (PIDs)
+ */
+/*! @{ */
+#define USB_PID_UNDEF_0                        0xf0
+#define USB_PID_OUT                            0xe1
+#define USB_PID_ACK                            0xd2
+#define USB_PID_DATA0                          0xc3
+#define USB_PID_PING                           0xb4             /* USB 2.0 */
+#define USB_PID_SOF                            0xa5
+#define USB_PID_NYET                           0x96             /* USB 2.0 */
+#define USB_PID_DATA2                          0x87             /* USB 2.0 */
+#define USB_PID_SPLIT                          0x78             /* USB 2.0 */
+#define USB_PID_IN                             0x69
+#define USB_PID_NAK                            0x5a
+#define USB_PID_DATA1                          0x4b
+#define USB_PID_PREAMBLE                       0x3c             /* Token mode */
+#define USB_PID_ERR                            0x3c             /* USB 2.0: handshake mode */
+#define USB_PID_SETUP                          0x2d
+#define USB_PID_STALL                          0x1e
+#define USB_PID_MDATA                          0x0f             /* USB 2.0 */
+/*! @} */
+
+/*! * @name Standard requests
+ */
+
+  /*! @{ */
+
+#define USB_REQ_GET_STATUS              0x00
+#define USB_REQ_CLEAR_FEATURE           0x01
+#define USB_REQ_SET_FEATURE             0x03
+#define USB_REQ_SET_ADDRESS             0x05
+#define USB_REQ_GET_DESCRIPTOR          0x06
+#define USB_REQ_SET_DESCRIPTOR          0x07
+#define USB_REQ_GET_CONFIGURATION       0x08
+#define USB_REQ_SET_CONFIGURATION       0x09
+#define USB_REQ_GET_INTERFACE           0x0A
+#define USB_REQ_SET_INTERFACE           0x0B
+#define USB_REQ_SYNCH_FRAME             0x0C
+/*! @} */
+
+/*!
+ * @name Hub requests
+ */
+ /*! @{ */
+#define USB_REQ_CLEAR_TT_BUFFER         0x08
+#define USB_REQ_RESET_TT                0x09
+#define USB_REQ_GET_TT_STATE            0x0A
+#define USB_REQ_STOP_TT                 0x0B
+/*! @} */
+
+/*!
+ * @name HID requests
+ */
+ /*! @{ */
+#define USB_REQ_GET_REPORT              0x01
+#define USB_REQ_GET_IDLE                0x02
+#define USB_REQ_GET_PROTOCOL            0x03
+#define USB_REQ_SET_REPORT              0x09
+#define USB_REQ_SET_IDLE                0x0A
+#define USB_REQ_SET_PROTOCOL            0x0B
+/*! @} */
+
+
+/*!
+ * @name USB Spec Release number
+ */
+ /*! @{ */
+
+#define USB_BCD_VERSION                 0x0200
+/*! @} */
+
+/*!
+ * @name Audio
+ */
+ /*! @{ */
+
+#define CS_AUDIO_UNDEFINED              0x20
+#define CS_AUDIO_DEVICE                 0x21
+#define CS_AUDIO_CONFIGURATION          0x22
+#define CS_AUDIO_STRING                 0x23
+#define CS_AUDIO_INTERFACE              0x24
+#define CS_AUDIO_ENDPOINT               0x25
+
+#define AUDIO_HEADER                    0x01
+#define AUDIO_INPUT_TERMINAL            0x02
+#define AUDIO_OUTPUT_TERMINAL           0x03
+#define AUDIO_MIXER_UNIT                0x04
+#define AUDIO_SELECTOR_UNIT             0x05
+#define AUDIO_FEATURE_UNIT              0x06
+#define AUDIO_PROCESSING_UNIT           0x07
+#define AUDIO_EXTENSION_UNIT            0x08
+/*! @} */
+
+
+/*!
+ * @name Device Requests      (c.f Table 9-2)
+ */
+ /*! @{ */
+
+#define USB_REQ_DIRECTION_MASK          0x80
+#define USB_REQ_TYPE_MASK               0x60
+#define USB_REQ_RECIPIENT_MASK          0x1f
+
+#define USB_REQ_DEVICE2HOST             0x80
+#define USB_REQ_HOST2DEVICE             0x00
+
+#define USB_REQ_TYPE_STANDARD           0x00
+#define USB_REQ_TYPE_CLASS              0x20
+#define USB_REQ_TYPE_VENDOR             0x40
+
+#define USB_REQ_RECIPIENT_DEVICE        0x00
+#define USB_REQ_RECIPIENT_INTERFACE     0x01
+#define USB_REQ_RECIPIENT_ENDPOINT      0x02
+#define USB_REQ_RECIPIENT_OTHER         0x03
+/*! @} */
+
+/*!
+ * @name Hub Recipients
+ */
+ /*! @{ */
+#define USB_REQ_RECIPIENT_HUB           0x00
+#define USB_REQ_RECIPIENT_PORT          0x03
+/*! @} */
+
+/*!
+ * @name get status bits
+ */
+ /*! @{ */
+
+#define USB_STATUS_SELFPOWERED          0x01
+#define USB_STATUS_REMOTEWAKEUP         0x02
+
+#define USB_STATUS_HALT                 0x01
+/*! @} */
+
+/*!
+ * @name Convert Feature Selector to a Status Flag Setting
+ */
+ /*! @{ */
+#define FEATURE(f)                      (1 << f)
+/*! @} */
+
+/*!
+ * @name standard feature selectors
+ */
+ /*! @{ */
+#define USB_ENDPOINT_HALT               0x00
+#define USB_DEVICE_REMOTE_WAKEUP        0x01
+#define USB_TEST_MODE                   0x02
+/*! @} */
+
+/*!
+ * @name Hub Feature Selectors C.f. Table 11-17
+ */
+ /*! @{ */
+#if !defined(C_HUB_LOCAL_POWER)
+#define C_HUB_LOCAL_POWER  0x0
+#endif
+#if !defined(C_HUB_OVER_CURRENT)
+#define C_HUB_OVER_CURRENT  0x1
+#endif
+
+#define PORT_CONNECTION   0
+#define PORT_ENABLE   1
+#define PORT_SUSPEND   2
+#define PORT_OVER_CURRENT  3
+#define PORT_RESET   4
+
+#define PORT_POWER   8
+#define PORT_LOW_SPEED   9
+#define PORT_HIGH_SPEED   10
+
+#define C_PORT_CONNECTION         16
+#define C_PORT_ENABLE          17
+#define C_PORT_SUSPEND          18
+#define C_PORT_OVER_CURRENT         19
+#define C_PORT_RESET          20
+
+#define PORT_TEST   21
+#define PORT_INDICATOR   22
+
+/*! @} */
+
+#ifdef PRAGMAPACK
+#pragma pack(push,1)
+#endif
+
+
+struct usbd_device_request {
+        u8 bmRequestType;
+        u8 bRequest;
+        u16 wValue;
+        u16 wIndex;
+        u16 wLength;
+} PACKED;
+
+
+
+struct usbd_otg_descriptor {
+        u8 bLength;
+        u8 bDescriptorType;
+        u8 bmAttributes;
+} PACKED;
+
+/*!
+ * @name OTG
+ */
+ /*! @{ */
+#define USB_OTG_HNP_SUPPORTED   0x02
+#define USB_OTG_SRP_SUPPORTED   0x01
+/*! @} */
+
+/*!
+ * @name OTG Feature selectors
+ */
+ /*! @{ */
+#define USB_OTG_B_HNP_ENABLE     0x03
+#define USB_OTG_A_HNP_SUPPORT    0x04
+#define USB_OTG_A_ALT_HNP_ENABLE 0x05
+/*! @} */
+
+/*!
+ * @name Class-Specific Request Codes
+ * C.f. CDC Table 46
+ */
+ /*! @{ */
+
+#define CDC_CLASS_REQUEST_SEND_ENCAPSULATED     0x00
+#define CDC_CLASS_REQUEST_GET_ENCAPSULATED      0x01
+
+#define CDC_CLASS_REQUEST_SET_COMM_FEATURE      0x02
+#define CDC_CLASS_REQUEST_GET_COMM_FEATURE      0x03
+#define CDC_CLASS_REQUEST_CLEAR_COMM_FEATURE    0x04
+
+#define CDC_CLASS_REQUEST_SET_LINE_CODING       0x20
+#define CDC_CLASS_REQUEST_GET_LINE_CODING       0x21
+
+#define CDC_CLASS_REQUEST_SET_CONTROL_LINE_STATE 0x22
+#define CDC_CLASS_REQUEST_SEND_BREAK            0x23
+/*! @} */
+
+/*!
+ * @name Notification codes
+ * c.f. CDC Table 68
+ */
+ /*! @{ */
+
+#define CDC_NOTIFICATION_NETWORK_CONNECTION         0x00
+#define CDC_NOTIFICATION_RESPONSE_AVAILABLE         0x01
+#define CDC_NOTIFICATION_AUX_JACK_HOOK_STATE        0x08
+#define CDC_NOTIFICATION_RING_DETECT                0x09
+#define CDC_NOTIFICATION_SERIAL_STATE               0x20
+#define CDC_NOTIFICATION_CALL_STATE_CHANGE          0x28
+#define CDC_NOTIFICATION_LINE_STATE_CHANGE          0x29
+#define CDC_NOTIFICATION_CONNECTION_SPEED_CHANGE    0x2a
+/*! @} */
+
+struct hub_descriptor {
+ u8 bDescLength;
+ u8 bDescriptorType;
+ u8 bNbrPorts;
+ u16 wHubCharacteristics;
+ u8 bPwrOn2PwrGood;
+ u8 bHubContrCurrent;
+ u8 DeviceRemovable;
+ u8 PortPwrCtrlMask;
+} PACKED;
+
+/*!
+ * @name Hub Descriptor C.f. 11.23.2.1 Table 11-13
+ * N.B. This assumes 1-8 ports, DeviceRemovable and PortPwrCtrlMask
+ * must be sized for the number of if ports > 8.
+ */
+ /*! @{ */
+
+
+#define HUB_GANGED_POWER  0x00
+#define HUB_INDIVIDUAL_POWER  0x01
+#define HUB_COMPOUND_DEVICE  0x04
+#define HUB_GLOBAL_OVERCURRENT  0x00
+#define HUB_INDIVIDUAL_OVERCURRENT 0x08
+#define HUB_NO_OVERCURRENT  0x10
+#define HUB_TT_8   0x00
+#define HUB_TT_16   0x20
+#define HUB_TT_24   0x40
+#define HUB_TT_32   0x60
+#define HUB_INDICATORS_SUPPORTED 0x80
+/*! @} */
+
+/*!
+ * @name HID - Class Descriptors
+ * C.f. 7.1.1
+ */
+ /*! @{ */
+
+#define HID     0x21
+#define HID_REPORT      0x22
+#define HID_PHYSICAL    0x23
+/*! @} */
+
+/*!
+ * name HID Descriptor
+ * C.f. E.8
+ */
+ /*! @{ */
+
+
+struct hid_descriptor {
+        u8 bLength;
+        u8 bDescriptorType;
+        u16 bcdHID;
+        u8 bCountryCode;
+        u8 bNumDescriptors;
+        u8 bReportType;
+        u16 wItemLength;
+} PACKED;
+
+/*! @} */
+
+/*!
+ * name ACM - Line Coding structure
+ * C.f. CDC Table 50
+ */
+ /*! @{ */
+
+
+struct cdc_acm_line_coding {
+        u32 dwDTERate;
+        u8 bCharFormat;
+        u8 bParityType;
+        u8 bDataBits;
+} PACKED;
+
+ /*!@} */
+ 
+/*!
+ * @name ACM - Line State
+ * C.f. CDC Table 51
+ */
+ /*! @{ */
+typedef u16 cdc_acm_bmLineState;
+#define CDC_LINESTATE_D1_RTS            (1 << 1)
+#define CDC_LINESTATE_D0_DTR            (1 << 0)
+/*! @} */
+
+/*!
+ * Serial State - C.f. 6.3.5
+ */
+struct acm_serial_state {
+        u16     uart_state_bitmap;
+};
+
+
+/*! @name ACM - UART State
+ * C.f. Table 69 - UART State Bitmap Values
+ */
+/*! @{ */
+typedef u16 cdc_acm_bmUARTState;
+#define CDC_UARTSTATE_BOVERRUN        (1 << 6)
+#define CDC_UARTSTATE_BPARITY         (1 << 5)
+#define CDC_UARTSTATE_BFRAMING        (1 << 4)
+#define CDC_UARTSTATE_BRINGSIGNAL     (1 << 3)
+#define CDC_UARTSTATE_BBREAK          (1 << 2)
+#define CDC_UARTSTATE_BTXCARRIER_DSR  (1 << 1)
+#define CDC_UARTSTATE_BRXCARRIER_DCD  (1 << 0)
+/*! @} */
+
+/* !USB Notification
+ *
+ */
+
+struct cdc_notification_descriptor {
+        u8 bmRequestType;
+        u8 bNotification;
+        u16 wValue;
+        u16 wIndex;
+        u16 wLength;
+        u8 data[2];
+} PACKED;
+
+
+
+
+/*!
+ * @name Communications Class Types
+ *
+ * c.f. CDC  USB Class Definitions for Communications Devices
+ * c.f. WMCD USB CDC Subclass Specification for Wireless Mobile Communications Devices
+ *
+ * @{
+ */
+
+#define CLASS_BCD_VERSION               0x0110
+/*! @} */
+
+/*! @name c.f. CDC 4.1 Table 14 */
+
+/*! @{ */
+
+#define AUDIO_CLASS                     0x01
+#define COMMUNICATIONS_DEVICE_CLASS     0x02
+/*! @} */
+
+/*! @name c.f. CDC 4.2 Table 15
+ */
+ /*! @{ */
+#define COMMUNICATIONS_INTERFACE_CLASS  0x02
+/*! @} */
+
+/*! @name c.f. CDC 4.3 Table 16
+ */
+ /*! @{ */
+#define COMMUNICATIONS_NO_SUBCLASS      0x00
+#define COMMUNICATIONS_DLCM_SUBCLASS    0x01
+#define COMMUNICATIONS_ACM_SUBCLASS     0x02
+#define COMMUNICATIONS_TCM_SUBCLASS     0x03
+#define COMMUNICATIONS_MCCM_SUBCLASS    0x04
+#define COMMUNICATIONS_CCM_SUBCLASS     0x05
+#define COMMUNICATIONS_ENCM_SUBCLASS    0x06
+#define COMMUNICATIONS_ANCM_SUBCLASS    0x07
+
+#define AUDIO_CONTROL_SUBCLASS          0x01
+#define AUDIO_STREAMING_SUBCLASS        0x02
+/*! @} */
+
+/*! @name c.f. WMCD 5.1
+ */
+ /*! @{ */
+#define COMMUNICATIONS_WHCM_SUBCLASS    0x08
+#define COMMUNICATIONS_DMM_SUBCLASS     0x09
+#define COMMUNICATIONS_MDLM_SUBCLASS    0x0a
+#define COMMUNICATIONS_OBEX_SUBCLASS    0x0b
+/*! @} */
+
+/*! @name c.f. CDC 4.6 Table 18
+ */
+ /*! @{ */
+#define DATA_INTERFACE_CLASS            0x0a
+/*! @} */
+
+/*! @name c.f. CDC 4.7 Table 19
+ */
+ /*! @{ */
+#define COMMUNICATIONS_NO_PROTOCOL      0x00
+/*! @} */
+
+
+/*! @name c.f. CDC 5.2.3 Table 24
+ * c.f. Audio Appendix A.4
+ */
+ /*! @{ */
+#define CS_UNDEFINED                    0x20
+#define CS_DEVICE                       0x21
+#define CS_CONFIG                       0x22
+#define CS_STRING                       0x23
+#define CS_INTERFACE                    0x24
+#define CS_ENDPOINT                     0x25
+/*! @} */
+
+/*!
+ * @name Audio Interface Class - c.f Appendix A.1
+ */
+ /*! @{ */
+#define AUDIO_INTERFACE_CLASS           0x01
+/*! @} */
+
+/*! @name Audio Interface Subclass - c.f Appendix A.2
+ */
+ /*! @{ */
+#define AUDIO_SUBCLASS_UNDEFINED        0x00
+#define AUDIO_AUDIOCONTROL              0x01
+#define AUDIO_AUDIOSTREAMING            0x02
+#define AUDIO_MIDISTREAMING             0x03
+/*! @} */
+
+/*! @name Audio Interface Proctol - c.f. Appendix A.3
+ */
+ /*! @{ */
+#define AUDIO_PR_PROTOCOL_UNDEFINED     0x00
+/*! @} */
+
+/*! @name Audio Class-Specific AC Interface Descriptor Subtypes - c.f. A.5
+ */
+ /*! @{ */
+#define AUDIO_AC_DESCRIPTOR_UNDEFINED   0x00
+#define AUDIO_AC_HEADER                 0x01
+#define AUDIO_AC_INPUT_TERMINAL         0x02
+#define AUDIO_AC_OUTPUT_TERMINAL        0x03
+#define AUDIO_AC_MIXER_UNIT             0x04
+#define AUDIO_AC_SELECTOR_UNIT          0x05
+#define AUDIO_AC_FEATURE_UNIT           0x06
+#define AUDIO_AC_PROCESSING_UNIT        0x07
+#define AUDIO_AC_EXTENSION_UNIT         0x08
+/*! @} */
+
+/*! @name Audio Class-Specific AS Interface Descriptor Subtypes - c.f. A.6
+ */
+ /*! @{ */
+#define AUDIO_AS_DESCRIPTOR_UNDEFINED   0x00
+#define AUDIO_AS_GENERAL                0x01
+#define AUDIO_AS_FORMAT_TYPE            0x02
+#define AUDIO_AS_FORMAT_UNSPECFIC       0x03
+/*! @} */
+
+/*! @name Audio Class Processing Unit Processing Types - c.f. A.7
+ */
+ /*! @{ */
+#define AUDIO_PROCESS_UNDEFINED         0x00
+#define AUDIO_UP_DOWN_MUIX_PROCESS      0x01
+#define AUDIO_DOLBY_PROLOGIC_PROCESS    0x02
+#define AUDIO_3D_STEREO_EXTENDER_PROCESS 0x03
+#define AUDIO_REVERBERATION_PROCESS     0x04
+#define AUDIO_CHORUS_PROCESS            0x05
+#define AUDIO_DYN_RANGE_COMP_PROCESS    0x06
+/*! @} */
+
+/*! @name Audio Class-Specific Endpoint Descriptor Subtypes - c.f. A.8
+ */
+ /*! @{ */
+#define AUDIO_DESCRIPTOR_UNDEFINED      0x00
+#define AUDIO_EP_GENERAL                0x01
+/*! @} */
+
+/*! @name Audio Class-Specific Request Codes - c.f. A.9
+ */
+ /*! @{ */
+#define AUDIO_REQUEST_CODE_UNDEFINED    0x00
+#define AUDIO_SET_CUR                   0x01
+#define AUDIO_GET_CUR                   0x81
+#define AUDIO_SET_MIN                   0x02
+#define AUDIO_GET_MIN                   0x82
+#define AUDIO_SET_MAX                   0x03
+#define AUDIO_GET_MAX                   0x83
+#define AUDIO_SET_RES                   0x04
+#define AUDIO_GET_RES                   0x84
+#define AUDIO_SET_MEM                   0x05
+#define AUDIO_GET_MEM                   0x85
+#define AUDIO_GET_STAT                  0xff
+/*! @} */
+
+/*!
+ * @name bDescriptorSubtypes
+ *
+ * c.f. CDC 5.2.3 Table 25
+ * c.f. WMCD 5.3 Table 5.3
+ */
+ /*! @{ */
+
+#define USB_ST_HEADER                   0x00
+#define USB_ST_CMF                      0x01
+#define USB_ST_ACMF                     0x02
+#define USB_ST_DLMF                     0x03
+#define USB_ST_TRF                      0x04
+#define USB_ST_TCLF                     0x05
+#define USB_ST_UF                       0x06
+#define USB_ST_CSF                      0x07
+#define USB_ST_TOMF                     0x08
+#define USB_ST_USBTF                    0x09
+#define USB_ST_NCT                      0x0a
+#define USB_ST_PUF                      0x0b
+#define USB_ST_EUF                      0x0c
+#define USB_ST_MCMF                     0x0d
+#define USB_ST_CCMF                     0x0e
+#define USB_ST_ENF                      0x0f
+#define USB_ST_ATMNF                    0x10
+
+#define USB_ST_WHCM                     0x11
+#define USB_ST_MDLM                     0x12
+#define USB_ST_MDLMD                    0x13
+#define USB_ST_DMM                      0x14
+#define USB_ST_OBEX                     0x15
+#define USB_ST_CS                       0x16
+#define USB_ST_CSD                      0x17
+#define USB_ST_TCM                      0x18
+/*! @} */
+
+/*!
+ * @name Class Descriptor Description Structures...
+ * c.f. CDC 5.1
+ * c.f. WCMC 6.7.2
+ *
+ * XXX add the other dozen class descriptor description structures....
+ *
+ */
+ /*! @{ */
+struct usbd_header_functional_descriptor {
+        u8 bFunctionLength;
+        u8 bDescriptorType;
+        u8 bDescriptorSubtype;
+        u16 bcdCDC;
+};
+
+struct usbd_call_management_functional_descriptor {
+        u8 bFunctionLength;
+        u8 bDescriptorType;
+        u8 bDescriptorSubtype;
+        u8 bmCapabilities;
+        u8 bDataInterface;
+};
+
+struct usbd_abstract_control_functional_descriptor {
+        u8 bFunctionLength;
+        u8 bDescriptorType;
+        u8 bDescriptorSubtype;
+        u8 bmCapabilities;
+};
+
+struct usbd_union_functional_functor {
+        u8 bFunctionLength;
+        u8 bDescriptorType;
+        u8 bDescriptorSubtype;
+        u8 bMasterInterface;
+        u8 bSlaveInterface[1];
+        //u8        bSlaveInterface[0];                    // XXX FIXME
+};
+
+struct usbd_ethernet_networking_descriptor {
+        u8 bFunctionLength;
+        u8 bDescriptorType;
+        u8 bDescriptorSubtype;
+        char *iMACAddress;
+        u8 bmEthernetStatistics;
+        u16 wMaxSegmentSize;
+        u16 wNumberMCFilters;
+        u8 bNumberPowerFilters;
+};
+
+struct usbd_mobile_direct_line_model_descriptor {
+        u8 bFunctionLength;
+        u8 bDescriptorType;
+        u8 bDescriptorSubtype;
+        u16 bcdVersion;
+        u8 bGUID[16];
+};
+
+struct usbd_mobile_direct_line_model_detail_descriptor {
+        u8 bFunctionLength;
+        u8 bDescriptorType;
+        u8 bDescriptorSubtype;
+        u8 bGuidDescriptorType;
+        u8 bDetailData[2];
+        //u8      bDetailData[0];                         // XXX FIXME
+};
+#if 0
+struct usbd_header_description {
+        u8 bDescriptorSubtype;
+        u16 bcdCDC;
+};
+
+struct usbd_call_management_description {
+        u8 bmCapabilities;
+        u8 bDataInterface;
+};
+
+struct usbd_abstract_control_description {
+        u8 bmCapabilities;
+};
+
+struct usbd_union_function_description {
+        u8 bMasterInterface;
+        u8 bSlaveInterface[1];
+        //u8        bSlaveInterface[0];                    // XXX FIXME
+};
+
+struct usbd_ethernet_networking_description {
+        char *iMACAddress;
+        u8 bmEthernetStatistics;
+        u16 wMaxSegmentSize;
+        u16 wNumberMCFilters;
+        u8 bNumberPowerFilters;
+};
+
+struct usbd_mobile_direct_line_model_description {
+        u16 bcdVersion;
+        u8 bGUID[16];
+};
+
+struct usbd_mobile_direct_line_model_detail_description {
+        u8 bGuidDescriptorType;
+        u8 bDetailData[2];
+        //u8      bDetailData[0];                         // XXX FIXME
+};
+
+struct usbd_class_description {
+        u8 bDescriptorSubtype;
+        u8 elements;
+        union {
+                struct usbd_header_description header;
+                struct usbd_call_management_description call_management;
+                struct usbd_abstract_control_description abstract_control;
+                struct usbd_union_function_description union_function;
+                struct usbd_ethernet_networking_description ethernet_networking;
+                struct usbd_mobile_direct_line_model_description mobile_direct;
+                struct usbd_mobile_direct_line_model_detail_description mobile_direct_detail;
+        } description;
+};
+#endif
+/*! @} */
+
+/*!
+ * @name Endpoint Modifiers
+ * static struct usbd_endpoint_description function_default_A_1[] = {
+ *
+ *     {this_endpoint: 0, attributes: CONTROL,   max_size: 8,  polling_interval: 0 },
+ *     {this_endpoint: 1, attributes: BULK,      max_size: 64, polling_interval: 0, direction: IN},
+ *     {this_endpoint: 2, attributes: BULK,      max_size: 64, polling_interval: 0, direction: OUT},
+ *     {this_endpoint: 3, attributes: INTERRUPT, max_size: 8,  polling_interval: 0},
+ *
+ *
+ */
+ /*! @{ */
+
+#define CONTROL         0x00
+#define ISOCHRONOUS     0x01
+#define BULK            0x02
+#define INTERRUPT       0x03
+/*! @} */
+
+
+/*! @name configuration modifiers
+ */
+
+/* @{ */
+#define BMATTRIBUTE_RESERVED            0x80
+#define BMATTRIBUTE_SELF_POWERED        0x40
+#define BMATTRIBUTE_REMOTE_WAKEUP       0x20
+
+#if 0
+/*
+ * The UUT tester specifically tests for MaxPower to be non-zero (> 0).
+ */
+#if !defined(CONFIG_OTG_MAXPOWER) || (CONFIG_OTG_MAXPOWER == 0)
+        #define BMATTRIBUTE BMATTRIBUTE_RESERVED | BMATTRIBUTE_SELF_POWERED
+        #define BMAXPOWER 1
+#else
+        #define BMATTRIBUTE BMATTRIBUTE_RESERVED
+        #define BMAXPOWER CONFIG_USBD_MAXPOWER
+#endif
+#endif
+/*! @} */
+
+
+
+/*!
+ * @name Standard Usb Descriptor Structures
+ */
+ /*! @{ */
+
+struct usbd_endpoint_descriptor {
+        u8 bLength;
+        u8 bDescriptorType;   // 0x5
+        u8 bEndpointAddress;
+        u8 bmAttributes;
+        u16 wMaxPacketSize;
+        u8 bInterval;
+} PACKED;
+
+struct usbd_interface_descriptor {
+        u8 bLength;
+        u8 bDescriptorType;   // 0x04
+        u8 bInterfaceNumber;
+        u8 bAlternateSetting;
+        u8 bNumEndpoints;
+        u8 bInterfaceClass;
+        u8 bInterfaceSubClass;
+        u8 bInterfaceProtocol;
+        u8 iInterface;
+} PACKED;
+
+struct usbd_configuration_descriptor {
+        u8 bLength;
+        u8 bDescriptorType;   // 0x2
+        u16 wTotalLength;
+        u8 bNumInterfaces;
+        u8 bConfigurationValue;
+        u8 iConfiguration;
+        u8 bmAttributes;
+        u8 bMaxPower;
+} PACKED;
+
+struct usbd_device_descriptor {
+        u8 bLength;
+        u8 bDescriptorType;   // 0x01
+        u16 bcdUSB;
+        u8 bDeviceClass;
+        u8 bDeviceSubClass;
+        u8 bDeviceProtocol;
+        u8 bMaxPacketSize0;
+        u16 idVendor;
+        u16 idProduct;
+        u16 bcdDevice;
+        u8 iManufacturer;
+        u8 iProduct;
+        u8 iSerialNumber;
+        u8 bNumConfigurations;
+} PACKED;
+
+struct usbd_string_descriptor {
+        u8 bLength;
+        u8 bDescriptorType;   // 0x03
+        u16 wData[0];
+} PACKED;
+
+struct usbd_device_qualifier_descriptor {
+        u8 bLength;
+        u8 bDescriptorType;
+        u16 bcdUSB;
+        u8 bDeviceClass;
+        u8 bDeviceSubClass;
+        u8 bDeviceProtocol;
+        u8 bMaxPacketSize0;
+        u8 bNumConfigurations;
+        u8 bReserved;
+} PACKED;
+
+struct usbd_generic_descriptor {
+        u8 bLength;
+        u8 bDescriptorType;
+} PACKED;
+
+struct usbd_generic_class_descriptor {
+        u8 bLength;
+        u8 bDescriptorType;
+        u8 bDescriptorSubtype;
+} PACKED;
+/*! @} */
+
+/*!
+ * @name Communications Class Dscriptor Structures
+ * c.f. CDC 5.2 Table 25c
+  */
+
+/*! @{*/
+
+struct usbd_class_function_descriptor {
+        u8 bFunctionLength;
+        u8 bDescriptorType;
+        u8 bDescriptorSubtype;
+} PACKED;
+
+struct usbd_class_function_descriptor_generic {
+        u8 bFunctionLength;
+        u8 bDescriptorType;
+        u8 bDescriptorSubtype;
+        u8 bmCapabilities;
+} PACKED;
+
+struct usbd_class_header_function_descriptor {
+        u8 bFunctionLength;
+        u8 bDescriptorType;
+        u8 bDescriptorSubtype;        // 0x00
+        u16 bcdCDC;
+} PACKED;
+
+struct usbd_class_call_management_descriptor {
+        u8 bFunctionLength;
+        u8 bDescriptorType;
+        u8 bDescriptorSubtype;        // 0x01
+        u8 bmCapabilities;
+        u8 bDataInterface;
+} PACKED;
+
+struct usbd_class_abstract_control_descriptor {
+        u8 bFunctionLength;
+        u8 bDescriptorType;
+        u8 bDescriptorSubtype;        // 0x02
+        u8 bmCapabilities;
+} PACKED;
+
+struct usbd_class_direct_line_descriptor {
+        u8 bFunctionLength;
+        u8 bDescriptorType;
+        u8 bDescriptorSubtype;        // 0x03
+} PACKED;
+
+struct usbd_class_telephone_ringer_descriptor {
+        u8 bFunctionLength;
+        u8 bDescriptorType;
+        u8 bDescriptorSubtype;        // 0x04
+        u8 bRingerVolSeps;
+        u8 bNumRingerPatterns;
+} PACKED;
+
+struct usbd_class_telephone_call_descriptor {
+        u8 bFunctionLength;
+        u8 bDescriptorType;
+        u8 bDescriptorSubtype;        // 0x05
+        u8 bmCapabilities;
+} PACKED;
+
+struct usbd_class_union_function_descriptor {
+        u8 bFunctionLength;
+        u8 bDescriptorType;
+        u8 bDescriptorSubtype;        // 0x06
+        u8 bMasterInterface;
+        u8 bSlaveInterface0[0];
+} PACKED;
+
+struct usbd_class_country_selection_descriptor {
+        u8 bFunctionLength;
+        u8 bDescriptorType;
+        u8 bDescriptorSubtype;        // 0x07
+        u8 iCountryCodeRelDate;
+        u16 wCountryCode0[0];
+} PACKED;
+
+
+struct usbd_class_telephone_operational_descriptor {
+        u8 bFunctionLength;
+        u8 bDescriptorType;
+        u8 bDescriptorSubtype;        // 0x08
+        u8 bmCapabilities;
+} PACKED;
+
+
+struct usbd_class_usbd_terminal_descriptor {
+        u8 bFunctionLength;
+        u8 bDescriptorType;
+        u8 bDescriptorSubtype;        // 0x09
+        u8 bEntityId;
+        u8 bInterfaceNo;
+        u8 bOutInterfaceNo;
+        u8 bmOptions;
+        u8 bChild0[0];
+} PACKED;
+
+struct usbd_class_network_channel_descriptor {
+        u8 bFunctionLength;
+        u8 bDescriptorType;
+        u8 bDescriptorSubtype;        // 0x0a
+        u8 bEntityId;
+        u8 iName;
+        u8 bChannelIndex;
+        u8 bPhysicalInterface;
+} PACKED;
+
+struct usbd_class_protocol_unit_function_descriptor {
+        u8 bFunctionLength;
+        u8 bDescriptorType;
+        u8 bDescriptorSubtype;        // 0x0b
+        u8 bEntityId;
+        u8 bProtocol;
+        u8 bChild0[0];
+} PACKED;
+
+struct usbd_class_extension_unit_descriptor {
+        u8 bFunctionLength;
+        u8 bDescriptorType;
+        u8 bDescriptorSubtype;        // 0x0c
+        u8 bEntityId;
+        u8 bExtensionCode;
+        u8 iName;
+        u8 bChild0[0];
+} PACKED;
+
+struct usbd_class_multi_channel_descriptor {
+        u8 bFunctionLength;
+        u8 bDescriptorType;
+        u8 bDescriptorSubtype;        // 0x0d
+        u8 bmCapabilities;
+} PACKED;
+
+struct usbd_class_capi_control_descriptor {
+        u8 bFunctionLength;
+        u8 bDescriptorType;
+        u8 bDescriptorSubtype;        // 0x0e
+        u8 bmCapabilities;
+} PACKED;
+
+struct usbd_class_ethernet_networking_descriptor {
+        u8 bFunctionLength;
+        u8 bDescriptorType;
+        u8 bDescriptorSubtype;        // 0x0f
+        u8 iMACAddress;
+        u32 bmEthernetStatistics;
+        u16 wMaxSegmentSize;
+        u16 wNumberMCFilters;
+        u8 bNumberPowerFilters;
+} PACKED;
+
+struct usbd_class_atm_networking_descriptor {
+        u8 bFunctionLength;
+        u8 bDescriptorType;
+        u8 bDescriptorSubtype;        // 0x10
+        u8 iEndSystermIdentifier;
+        u8 bmDataCapabilities;
+        u8 bmATMDeviceStatistics;
+        u16 wType2MaxSegmentSize;
+        u16 wType3MaxSegmentSize;
+        u16 wMaxVC;
+} PACKED;
+
+
+struct usbd_class_mdlm_descriptor {
+        u8 bFunctionLength;
+        u8 bDescriptorType;
+        u8 bDescriptorSubtype;        // 0x12
+        u16 bcdVersion;
+        u8 bGUID[16];
+} PACKED;
+
+/*
+struct usbd_class_mdlmd_descriptor {
+        u8 bFunctionLength;
+        u8 bDescriptorType;
+        u8 bDescriptorSubtype;        // 0x13
+        u8 bGuidDescriptorType;
+        u8 bDetailData[0];
+} PACKED;
+*/
+
+struct usbd_class_blan_descriptor {
+        u8 bFunctionLength;           // 0x7
+        u8 bDescriptorType;
+        u8 bDescriptorSubtype;        // 0x13
+        u8 bGuidDescriptorType;
+        u8 bmNetworkCapabilities;
+        u8 bmDataCapabilities;
+        u8 bPad;
+} PACKED;
+
+struct usbd_class_safe_descriptor {
+        u8 bFunctionLength;           // 0x6
+        u8 bDescriptorType;
+        u8 bDescriptorSubtype;        // 0x13
+        u8 bGuidDescriptorType;
+        u8 bmNetworkCapabilities;
+        u8 bmDataCapabilities;
+} PACKED;
+
+/*@}*/
+
+
+/*!
+ * @name Descriptor Union Structures
+ */
+ /*! @{ */
+
+struct usbd_descriptor {
+        union {
+                struct usbd_generic_descriptor generic_descriptor;
+                struct usbd_generic_class_descriptor generic_class_descriptor;
+                struct usbd_endpoint_descriptor endpoint_descriptor;
+                struct usbd_interface_descriptor interface_descriptor;
+                struct usbd_configuration_descriptor configuration_descriptor;
+                struct usbd_device_descriptor device_descriptor;
+                struct usbd_string_descriptor string_descriptor;
+        } descriptor;
+
+} PACKED;
+
+struct usbd_class_descriptor {
+        union {
+                struct usbd_class_function_descriptor function;
+                struct usbd_class_function_descriptor_generic generic;
+                struct usbd_class_header_function_descriptor header_function;
+                struct usbd_class_call_management_descriptor call_management;
+                struct usbd_class_abstract_control_descriptor abstract_control;
+                struct usbd_class_direct_line_descriptor direct_line;
+                struct usbd_class_telephone_ringer_descriptor telephone_ringer;
+                struct usbd_class_telephone_operational_descriptor telephone_operational;
+                struct usbd_class_telephone_call_descriptor telephone_call;
+                struct usbd_class_union_function_descriptor union_function;
+                struct usbd_class_country_selection_descriptor country_selection;
+                struct usbd_class_usbd_terminal_descriptor usb_terminal;
+                struct usbd_class_network_channel_descriptor network_channel;
+                struct usbd_class_extension_unit_descriptor extension_unit;
+                struct usbd_class_multi_channel_descriptor multi_channel;
+                struct usbd_class_capi_control_descriptor capi_control;
+                struct usbd_class_ethernet_networking_descriptor ethernet_networking;
+                struct usbd_class_atm_networking_descriptor atm_networking;
+                struct usbd_class_mdlm_descriptor mobile_direct;
+                //struct usbd_class_mdlmd_descriptor mobile_direct_detail;
+                struct usbd_class_blan_descriptor mobile_direct_blan_detail;
+                struct usbd_class_safe_descriptor mobile_direct_safe_detail;
+        } descriptor;
+
+} PACKED;
+/*! @} */
+
+/*!
+ * @name Audio Status Word Format - c.f. Table 3.1
+ */
+ /*! @{ */
+
+#define AUDIO_STATUS_INTERRUPT_PENDING                          1<<7
+#define AUDIO_STATUS_MEMORY_CONTENT_CHANGED                     1<<6
+#define AUDIO_STATUS_ORIGINATOR_AUDIO_CONTROL_INTERFACE         0
+#define AUDIO_STATUS_ORIGINATOR_AUDIO_STREAMING_INTERFACE       1
+#define AUDIO_STATUS_ORIGINATOR_AUDIO_STREAMING_ENDPOINT        2
+#define AUDIO_STATUS_ORIGINATOR_AUDIOCONTROL_INTERFACE          0
+
+struct usbd_audio_status_word {
+        u8 bStatusType;
+        u8 bOriginator;
+} PACKED;
+
+struct usbd_audio_ac_interface_header_descriptor {
+        u8 bFunctionLength;
+        u8 bDescriptorType;
+        u8 bDescriptorSubtype;
+        u16 bcdADC;
+        u16 wTotalLength;
+        u8 binCollection;
+        u8 bainterfaceNr[0];
+} PACKED;
+
+struct usbd_audio_input_terminal_descriptor {
+        u8 bFunctionLength;
+        u8 bDescriptorType;
+        u8 bDescriptorSubtype;
+        u8 bTerminalID;
+        u16 wTerminalType;
+        u8 bAssocTerminal;
+        u8 bNrChannels;
+        u16 wChannelConfig;
+        u8 iChannelNames;
+        u8 iTerminal;
+} PACKED;
+
+struct usbd_audio_input_terminal_description {
+        struct usbd_audio_input_terminal_descriptor *audio_input_terminal_descriptor;
+        u16 wTerminalType;
+        u16 wChannelConfig;
+        char * iChannelNames;
+        char * iTerminal;
+};
+
+struct usbd_audio_output_terminal_descriptor {
+        u8 bFunctionLength;
+        u8 bDescriptorType;
+        u8 bDescriptorSubtype;
+        u8 bTerminalID;
+        u16 wTerminalType;
+        u8 bAssocTerminal;
+        u8 bSourceID;
+        u8 iTerminal;
+} PACKED;
+
+struct usbd_audio_output_terminal_description {
+        struct usbd_audio_output_terminal_descriptor *audio_output_terminal_descriptor;
+        u16 wTerminalType;
+        char * iTerminal;
+};
+
+struct usbd_audio_mixer_unit_descriptor_a {
+        u8 bFunctionLength;
+        u8 bDescriptorType;
+        u8 bDescriptorSubtype;
+        u8 bUniteID;
+        u8 bNrinPins;
+} PACKED;
+
+struct usbd_audio_mixer_unit_descriptor_b {
+        u8 baSourceID;
+        u8 bNrChannels;
+        u16 wChannelConfig;
+        u8 iChannelNames;
+        u8 bmControls;
+        u8 iMixer;
+} PACKED;
+
+struct usbd_audio_mixer_unit_description {
+        struct usbd_audio_mixer_unit_descriptor *audio_mixer_unit_descriptor;
+        u16 wChannelConfig;
+        char * iMixer;
+};
+
+
+struct usbd_audio_as_general_interface_descriptor {
+        u8 bLength;
+        u8 bDescriptorType;
+        u8 bDescriptorSubtype;
+        u8 bTerminalLink;
+        u8 bDelay;
+        u16 wFormatTag;
+} PACKED;
+
+struct usbd_audio_as_general_interface_description {
+        struct usbd_audio_as_general_interface_descriptor *audio_as_general_interface_descriptor;
+        u16 wFormatTag;
+};
+
+struct usbd_audio_as_format_type_descriptor {
+        u8 bLength;
+        u8 bDescriptorType;
+        u8 bDescriptorSubtype;
+        u8 bFormatType;
+        u8 bNrChannels;
+        u8 bSubFrameSize;
+        u8 bBitResolution;
+        u8 bSamFreqType;
+        u8 iSamFreq[3];
+} PACKED;
+
+struct usbd_audio_as_format_type_description {
+        struct usbd_audio_as_format_type_descriptor *audio_as_format_type_descriptor;
+        u16 wFormatTag;
+};
+
+
+
+
+struct usbd_audio_descriptor {
+        union {
+                struct usbd_audio_ac_interface_header_descriptor header;
+                struct usbd_audio_input_terminal_descriptor input;
+                struct usbd_audio_output_terminal_descriptor output;
+        } descriptor;
+} PACKED;
+
+
+
+struct usbd_ac_endpoint_descriptor {
+        u8 bLength;
+        u8 bDescriptorType;
+        u8 bEndpointAddress;
+        u8 bmAttributes;
+        u16 wMaxPacketSize;
+        u8 bInterval;
+        u8 bRefresh;
+        u8 bSynchAddress;
+} PACKED;
+
+
+struct usbd_as_iso_endpoint_descriptor {
+        u8 bLength;
+        u8 bDescriptorType;
+        u8 bDescriptorSubtype;
+        u8 bmAttributes;
+        u8 bLockDelayUnits;
+        u16 wLockDelay;
+} PACKED;
+/*! @}*/
+
+
+/*!
+ * @name MCPC
+ */
+ /*! @{ */
+
+/*! struct usbd_interface_association
+ * Used in AB-1 or DL-1 interface bundles.
+ */
+struct usbd_interface_association_descriptor {
+        u8 bLength;
+        u8 bDescriptorType;
+        u8 bFirstInterface;
+        u8 bInterfaceCount;
+        u8 bFunctionClass;
+        u8 bFunctionSubClass;
+        u8 bFunctionProcotol;
+        u8 iFunction;
+} PACKED;
+
+struct usbd_mobile_abstract_control_model_specific_desciptor {
+        u8 bFunctionLength;
+        u8 bDescriptorType;
+        u8 bDescriptorSubType;
+        u8 bType;
+        u8 bMode_1;
+};
+
+struct usbd_mobile_direct_line_model_specific_desciptor {
+        u8 bFunctionLength;
+        u8 bDescriptorType;
+        u8 bDescriptorSubType;
+        u8 bType;
+        u8 bMode_N[2];
+};
+
+
+/*! @} */
+
+/*! 
+ * Device Events 
+ *
+ * These are defined in the USB Spec (c.f USB Spec 2.0 Figure 9-1).
+ *
+ * There are additional events defined to handle some extra actions we need to have handled.
+ *
+ */
+typedef enum usbd_device_event {
+
+        DEVICE_UNKNOWN,         // bi - unknown event
+        DEVICE_INIT,            // bi  - initialize
+        DEVICE_CREATE,          // bi  - 
+        DEVICE_HUB_CONFIGURED,  // bi  - bus has been plugged int
+        DEVICE_RESET,           // bi  - hub has powered our port
+
+        DEVICE_ADDRESS_ASSIGNED,// ep0 - set address setup received
+        DEVICE_CONFIGURED,      // ep0 - set configure setup received
+        DEVICE_SET_INTERFACE,   // ep0 - set interface setup received
+
+        DEVICE_SET_FEATURE,     // ep0 - set feature setup received
+        DEVICE_CLEAR_FEATURE,   // ep0 - clear feature setup received
+
+        DEVICE_DE_CONFIGURED,   // ep0 - set configure setup received for ??
+
+        DEVICE_BUS_INACTIVE,    // bi  - bus in inactive (no SOF packets)
+        DEVICE_BUS_ACTIVITY,    // bi  - bus is active again
+
+        DEVICE_POWER_INTERRUPTION,// bi  - hub has depowered our port
+        DEVICE_HUB_RESET,       // bi  - bus has been unplugged
+        DEVICE_DESTROY,         // bi  - device instance should be destroyed
+        DEVICE_CLOSE,           // bi  - device instance should be destroyed
+
+} usbd_device_event_t;
+
+/*! USB Request Block structure 
+ *
+ * This is used for both sending and receiving data. 
+ *
+ * The callback function is used to let the function driver know when
+ * transmitted data has been sent.
+ *
+ * The callback function is set by the alloc_recv function when an urb is
+ * allocated for receiving data for an endpoint and used to call the
+ * function driver to inform it that data has arrived.
+ *
+ * Note that for OUT urbs the buffer is always allocated to a multiple of
+ * the packetsize that is 1 larger than the requested size. This prevents
+ * overflow if the host unexpectedly sends a full sized packet when we are
+ * expecting a short one (the host is always right..)
+ *
+ * USBD_URB_SENDZLP - set this flag if a ZLP (zero length packet) should be
+ * sent after the data is sent. This is required if sending data that is a
+ * multiple of the packetsize but less than the maximum transfer size for
+ * the protocol in use.  A ZLP is required to ensure that the host
+ * recognizes a short transfer.
+ *
+ */
+
+typedef struct urb_link {
+        struct urb_link *next;
+        struct urb_link *prev;
+} urb_link;
+
+
+/*! URB Status
+ *
+ * This defines the current status of a pending or finshed URB.
+ *
+ */
+typedef enum usbd_urb_status {
+        SEND_IN_QUEUE,
+        SEND_IN_PROGRESS,
+        SEND_FINISHED_OK,
+        SEND_FINISHED_ERROR,
+        SEND_CANCELLED,
+        SEND_STALLED,
+        SEND_DISABLED,
+        RECV_IN_QUEUE,
+        RECV_IN_PROGRESS,
+        RECV_OK,
+        RECV_ERROR,
+        RECV_CANCELLED,
+        RECV_STALLED,
+        RECV_DISABLED
+} usbd_urb_status_t;
+
+#define USBD_URB_SENDZLP        0x01
+
+struct usbd_urb {
+
+        struct usbd_bus_instance        *bus;
+        struct usbd_function_instance   *function_instance;
+        struct usbd_endpoint_instance   *endpoint;
+
+        int                             (*callback) (struct usbd_urb *, int);
+
+        u8                              *buffer;        // data received (OUT) or being sent (IN)
+        u32                             request_length; // maximum data expected for OUT
+        u32                             buffer_length;  // allocated size of buffer
+        u32                             actual_length;  // actual data received (OUT or being sent (IN)
+        u32                             flags;
+        
+        void                            *function_privdata;
+        void                            *bus_privdata;
+        void                            *pcimap;
+
+        struct urb_link                 link;   
+        usbd_urb_status_t               status;         // what is the current status of the urb
+        time_t                          jiffies;        // timestamp for when urb was started or finished
+        u16                             framenum;       // SOF framenum when urb was finished
+};
+
+/*! Endpoint Request
+ *
+ * An array of these structures is initialized by each function driver to specify the endpoints
+ * it requires. 
+ *
+ * The bus interface driver will attempt to fulfill these requests with the actual endpoints it
+ * has available.
+ *
+ * Note that in most cases the bEndpointAddress should be left as zero except for specialized
+ * function drivers that both require a specific value and know ahead of time that the specified
+ * value is legal for the bus interface driver being used.
+ */
+struct usbd_endpoint_request {
+        u8 configuration;               /* configuration endpoint will be in */
+        u8 interface_num;               /* interface endpoint will be in */
+        u8 alternate;                   /* altsetting for this request */
+        u8 bmAttributes;                /* endpoint type AND direction */
+        u16 fs_requestedTransferSize;   /* max full speed transfer size for this endpoint */
+        u16 hs_requestedTransferSize;   /* max high speed transfer size for this endpoint */
+        u8 bEndpointAddress;            /* specific bEndpointAddress function driver requires */
+        u8 physical;                    /* physical endpoint used */
+};
+
+/*!
+ * Device status
+ *
+ * Overall state, we use this to show when we are suspended.
+ * This is required because when resumed we need to go back
+ * to previous state.
+ */
+typedef enum usbd_device_status {
+        USBD_OPENING,                   // we are currently opening
+        USBD_RESETING,                  // we are currently opening
+        USBD_OK,                        // ok to use
+        USBD_SUSPENDED,                 // we are currently suspended
+        USBD_CLOSING,                   // we are currently closing
+        USBD_CLOSED,                    // we are currently closing
+        USBD_UNKNOWN,
+} usbd_device_status_t;
+
+
+/*! 
+ * Device State (c.f USB Spec 2.0 Figure 9-1)
+ *
+ * What state the usb device is in. 
+ *
+ * Note the state does not change if the device is suspended, we simply set a
+ * flag to show that it is suspended.
+ *
+ */
+typedef enum usbd_device_state {
+        STATE_INIT,                     // just initialized
+        STATE_CREATED,                  // just created
+        STATE_ATTACHED,                 // we are attached 
+        STATE_POWERED,                  // we have seen power indication (electrical bus signal)
+        STATE_DEFAULT,                  // we been reset
+        STATE_ADDRESSED,                // we have been addressed (in default configuration)
+        STATE_CONFIGURED,               // we have seen a set configuration device command
+        STATE_SUSPENDED,                // device has been suspended
+        STATE_UNKNOWN,                  // destroyed
+} usbd_device_state_t;
+
+
+
+#ifdef PRAGMAPACK
+#pragma pack(pop)
+#endif
+
+
diff -uNr linux/drivers/no-otg/otg/usbp-func.h linux/drivers/otg/otg/usbp-func.h
--- linux/drivers/no-otg/otg/usbp-func.h	1970-01-01 01:00:00.000000000 +0100
+++ linux/drivers/otg/otg/usbp-func.h	2006-09-01 21:41:33.000000000 +0200
@@ -0,0 +1,428 @@
+/*
+ * otg/otg/usbp-func.h - USB Device Function Driver Interface
+ *
+ *      Copyright (c) 2004 Belcarra
+ *
+ * By: 
+ *      Stuart Lynne <sl@belcarra.com>, 
+ *      Tom Rushworth <tbr@belcarra.com>, 
+ *      Bruce Balden <balden@belcarra.com>
+ *
+ *
+ */
+
+/*!
+ * @file otg/otg/usbp-func.h
+ * @brief Function Driver related structures and definitions.
+ *
+ * This file contains the USB Function Driver and USB Interface Driver 
+ * definitions.
+ *
+ * The USBOTG support allows for implementation of composite devices.
+ *
+ * From USB 2.0, C.f. 5.2.3:
+ *
+ *      A Device that has multiple interfaces controlled independantly of
+ *      each other is referred to as a composite device. A composite device
+ *      has only a single device.
+ *
+ * In this implemenation the function portion of the device is split into
+ * two types of drivers:
+ *
+ *      - USB Interface Driver
+ *      - USB Function Driver
+ *
+ *
+ * USB Interface Driver
+ *
+ * An USB Interface Driver specifies the Interface related descriptors and
+ * implements the data handling processes for each of the data endpoints
+ * associated with the interface (or interfaces) required for the function.
+ * It typically will also implement the upper edge interface to the OS.
+ *
+ * The USB Interface Driver will also handle non-standard device requests
+ * and feature requests that are for the interface or an endpoint associated
+ * with one of the interfaces associated with the driver.
+ *
+ * Each interface implemented by an USB Interface Driver will have a
+ * interface instance that stores a pointer to the parent USB Function
+ * Driver and has an array of pointers to endpoint instances for the data
+ * endpoints associated with the interface.
+ *
+ *
+ * USB Function Driver
+ *
+ * A USB Function Driver specifies the device and configuration descriptors.
+ * A configuration descriptor is assembled from the interface descriptors
+ * from the USB Interface Driver (or drivers) that it is making available to
+ * the USB Host.
+ *
+ * The USB Function Driver handles non-standard device requests and feature
+ * requests for the device.
+ *
+ * Each function driver has a function instance that maintains an array
+ * of pointers to configuration instances and a pointer to the device
+ * descriptor.
+ *
+ * Each configuration instance maintains an array of pointers to the 
+ * interface instances for that configuration and a pointer to the 
+ * complete configuration descriptor.
+ *
+ * @ingroup USBP
+ */
+
+/*!
+ * This file contains the USB Device Core Common definitions. It also contains definitions for
+ * functions that are commonly used by both Function Drivers and Bus Interface Drivers.
+ *
+ * Specifically:
+ *
+ *      o public functions exported by the USB Core layer 
+ *
+ *      o common structures and definitions 
+ *
+ */
+
+
+
+struct usbd_bus_instance;
+struct usbd_endpoint_instance;
+struct usbd_function_instance;
+
+/*!
+ * USB Function Driver types
+ */
+typedef enum usbd_function_types {
+        function_simple,
+        function_interface,
+        function_composite
+} usbd_function_types_t; 
+
+
+/*!
+ * USB Function Driver structures
+ *
+ * Descriptors:
+ *   struct usbd_endpoint_description
+ *   struct usbd_interface_description 
+ *   struct usbd_configuration_description
+ *
+ * Driver description:
+ *   struct usbd_function_driver
+ *   struct usbd_function_operations
+ *
+ */
+
+struct usbd_function_operations {
+
+        int (*function_enable) (struct usbd_function_instance *);
+        void (*function_disable) (struct usbd_function_instance *);
+
+        /*
+         * All of the following may be called from an interrupt context
+         */
+        void (*event_handler) (struct usbd_function_instance *, usbd_device_event_t, int);
+        int (*device_request) (struct usbd_function_instance *, struct usbd_device_request *);
+        int (*set_configuration) (struct usbd_function_instance *, int configuration);
+
+        int (*set_interface) (struct usbd_function_instance *, int interface, int altsetting);
+
+        void * (*get_descriptor)(struct usbd_function_instance *, int descriptor_type, int descriptor_index);
+        int * (*set_descriptor)(struct usbd_function_instance *, int descriptor_type, int descriptor_index, void *);
+
+        void (*endpoint_cleared)(struct usbd_function_instance*, int wIndex, int);
+
+};
+
+
+/*!
+ * function driver definitions
+ */
+struct usbd_alternate_instance {
+        struct usbd_interface_descriptor *interface_descriptor;
+        int                             classes;
+        struct usbd_generic_class_descriptor    **class_list;
+        int                             endpoints;
+        struct usbd_endpoint_descriptor  **endpoint_list;
+        u8                              *endpoint_indexes;
+};
+
+
+/*!
+ * usb device description structures
+ */
+
+struct usbd_alternate_description {
+        struct usbd_interface_descriptor *interface_descriptor;
+        char                            *iInterface;
+
+        // list of CDC class descriptions for this alternate interface
+        u8                              classes;
+        struct usbd_generic_class_descriptor    **class_list;
+
+        // list of endpoint descriptions for this alternate interface
+        u8                              endpoints;
+        struct usbd_endpoint_descriptor  **endpoint_list;
+
+        // list of indexes into endpoint request map for each endpoint descriptor
+        u8                              *endpoint_indexes;
+};
+
+typedef struct usbd_generic_class_descriptor usbd_class_descriptor_t;
+typedef struct usbd_endpoint_descriptor      usbd_endpoint_descriptor_t;
+
+struct usbd_interface_description {
+        // list of alternate interface descriptions for this interface
+        u8                              alternates;
+        struct usbd_alternate_description *alternate_list;
+};
+
+struct usbd_configuration_description {
+        struct usbd_configuration_descriptor *configuration_descriptor;
+        char                            *iConfiguration;
+        // list of interface descriptons for this configuration
+        //u8                              bNumInterfaces;
+        //struct usbd_interface_description *interface_list;
+        int                             configuration_type;
+};
+
+struct usbd_device_description {
+        struct usbd_device_descriptor   *device_descriptor;
+        #ifdef CONFIG_OTG_HIGH_SPEED
+        struct usbd_device_qualifier_descriptor *device_qualifier_descriptor;
+        #endif /* CONFIG_OTG_HIGH_SPEED */
+        struct usbd_otg_descriptor      *otg_descriptor;
+        u8                              *iManufacturer;
+        u8                              *iProduct;
+        u8                              *iSerialNumber;
+        //u8                              endpointsRequested;
+        //struct usbd_endpoint_request    *requestedEndpoints;
+};
+
+
+struct usbd_interfaces_instance {
+        u8                              alternates;
+        struct usbd_alternate_instance  *alternates_instance_array;
+};
+
+struct usbd_configuration_instance {
+        int                             bNumInterfaces;
+        struct usbd_configuration_descriptor *configuration_descriptor;
+        struct usbd_interfaces_instance  *interfaces_instance_array;
+        struct usbd_function_driver     *function_driver;
+};
+
+
+/*! Function Driver data structure
+ *
+ * Function driver and its configuration descriptors. 
+ *
+ * This is passed to the usb-device layer when registering. It contains all
+ * required information about the function driver for the usb-device layer
+ * to use the function drivers configuration data and to configure this
+ * function driver an active configuration.
+ *
+ * Note that each function driver registers itself on a speculative basis.
+ * Whether a function driver is actually configured will depend on the USB
+ * HOST selecting one of the function drivers configurations. 
+ *
+ * This may be done multiple times WRT to either a single bus interface
+ * instance or WRT to multiple bus interface instances. In other words a
+ * multiple configurations may be selected for a specific bus interface. Or
+ * the same configuration may be selected for multiple bus interfaces. 
+ *
+ */
+struct usbd_function_driver {
+        const char                              *name;
+        struct usbd_function_operations         *fops;  // functions 
+
+        // device & configuration descriptions 
+        struct usbd_device_description  *device_description;
+        struct usbd_configuration_description *configuration_description;
+        int                             bNumConfigurations;
+
+        u16                             idVendor;
+        u16                             idProduct;
+        u16                             bcdDevice;
+
+        u8                              bNumInterfaces;
+        struct usbd_interface_description *interface_list;
+
+        u8                              endpointsRequested;
+        struct usbd_endpoint_request    *requestedEndpoints;
+
+        // constructed descriptors
+        struct usbd_device_descriptor   *device_descriptor;
+#ifdef CONFIG_OTG_HIGH_SPEED
+        struct usbd_device_qualifier_descriptor *device_qualifier_descriptor;
+#endif /* CONFIG_OTG_HIGH_SPEED */
+        struct usbd_otg_descriptor       *otg_descriptor;
+        struct usbd_configuration_instance *configuration_instance_array;
+};
+
+/*! Function configuration structure
+ *
+ * This is allocated for each configured instance of a function driver.
+ *
+ * It stores pointers to the usbd_function_driver for the appropriate function,
+ * and pointers to the USB HOST requested usbd_configuration_description and
+ * usbd_interface_description.
+ *
+ * The privdata pointer may be used by the function driver to store private
+ * per instance state information.
+ *
+ * The endpoint map will contain a list of all endpoints for the configuration 
+ * driver, and only related endpoints for an interface driver.
+ *
+ * The interface driver array will be NULL for an interface driver.
+ */
+struct usbd_function_instance {
+        char *                          name;
+        LIST_NODE                       instances;             // linked list (was drivers in usbd_function_driver)
+        struct usbd_bus_instance        *bus;
+        struct usbd_function_driver     *function_driver;
+        void                            *privdata;              // private data for the function
+
+        usbd_function_types_t           function_type;
+
+        int                             configuration;          // configuration driver only
+        int                             altsetting;             // interface driver only
+
+        char                            **interfaces_list;
+
+        int                             interfaces;
+        struct usbd_function_instance   **interfaces_array;
+
+        int                             endpoints;
+        struct usbd_endpoint_request    *requestedEndpoints;
+        struct usbd_endpoint_map        *endpoint_map_array;  
+
+};
+
+
+struct usbd_function_instance;
+
+/*!
+ * @name Function Driver Registration
+ *
+ * Called by function drivers to register themselves when loaded
+ * or de-register when unloading.
+ * @{ 
+ */
+struct usbd_function_instance * usbd_register_function (struct usbd_function_driver *, char *, void *);
+struct usbd_function_instance * usbd_register_interface (struct usbd_function_driver *, char *, void *);
+struct usbd_function_instance * usbd_register_composite (struct usbd_function_driver *function_driver, char*, char **, void *);
+
+void usbd_deregister_function (struct usbd_function_instance *);
+struct usbd_function_instance *usbd_find_function (char *name);
+struct usbd_function_instance *usbd_find_interface(char *name);
+/*! @} */
+
+
+/*!
+ * @name String Descriptor database
+ *
+ * @{
+ */
+
+struct usbd_string_descriptor *usbd_get_string_descriptor (u8);
+u8 usbd_realloc_string (u8, char *);
+u8 usbd_alloc_string (char *);
+void usbd_free_string_descriptor(u8 );
+
+//extern struct usbd_string_descriptor **usb_strings;
+//void usbd_free_descriptor_strings (struct usbd_descriptor *);
+
+/*! @} */
+
+
+
+/*!
+ * @name Device Information
+ * 
+ * @}
+ */
+int usbd_high_speed(struct usbd_function_instance *);
+int usbd_bmaxpower(struct usbd_function_instance *);
+int usbd_endpoint_wMaxPacketSize(struct usbd_function_instance *, int, int);
+int usbd_endpoint_zero_wMaxPacketSize(struct usbd_function_instance *, int);
+int usbd_endpoint_bEndpointAddress(struct usbd_function_instance *, int, int);
+int usbd_get_bMaxPower(struct usbd_function_instance *);
+/*! @} */
+
+
+/*!
+ * @name Device Control
+ * 
+ * @}
+ */
+usbd_device_state_t usbd_get_device_state(struct usbd_function_instance *);
+usbd_device_status_t usbd_get_device_status(struct usbd_function_instance *);
+int usbd_framenum(struct usbd_function_instance *);
+u64 usbd_ticks(struct usbd_function_instance *);
+u64 usbd_elapsed(struct usbd_function_instance *, u64 *, u64 *);
+
+/*! @} */
+
+/*!
+ * @name Endpoint I/O
+ * 
+ * @}
+ */
+
+struct usbd_urb *usbd_alloc_urb (struct usbd_function_instance *, int, int length, int (*callback) (struct usbd_urb *, int));
+struct usbd_urb *usbd_alloc_urb_ep0 (struct usbd_function_instance *, int length, int (*callback) (struct usbd_urb *, int));
+void usbd_free_urb (struct usbd_urb *urb);
+int usbd_start_in_urb (struct usbd_urb *urb);
+int usbd_start_out_urb (struct usbd_urb *);
+int usbd_cancel_urb(struct usbd_urb *);
+int usbd_halt_endpoint (struct usbd_function_instance *function, int endpoint_index);
+int usbd_endpoint_halted (struct usbd_function_instance *function, int endpoint);
+
+
+/*! @} */
+
+
+/*! 
+ * @name endpoint information
+ *
+ * Used by function drivers to get specific endpoint information
+ * @{ 
+ */
+
+int usbd_endpoint_transferSize(struct usbd_function_instance *, int, int);
+int usbd_endpoint_interface(struct usbd_function_instance *, int);
+int usbd_interface_AltSetting(struct usbd_function_instance *, int);
+int usbd_ConfigurationValue(struct usbd_function_instance *);
+void usbd_endpoint_update(struct usbd_function_instance *, int , struct usbd_endpoint_descriptor *, int);
+/*! @} */
+
+//int usbd_remote_wakeup_enabled(struct usbd_function_instance *);
+
+/*! 
+ * @name device information
+ *
+ * Used by function drivers to get device feature information
+ * @{ 
+ */
+int usbd_otg_bmattributes(struct usbd_function_instance *);
+/*! @} */
+
+/*! 
+ * @name usbd_feature_enabled - return non-zero if specified feature has been enabled
+ * @{ 
+ */
+int usbd_feature_enabled(struct usbd_function_instance *function, int f);
+/*! @} */
+
+/* DEPRECATED */
+/*! 
+ * @name Access to function privdata (DEPRECATED)
+ * @{ 
+ */
+void *usbd_function_get_privdata(struct usbd_function_instance *function);
+void usbd_function_set_privdata(struct usbd_function_instance *function, void *privdata);
+void usbd_flush_endpoint_index (struct usbd_function_instance *, int );
+int usbd_old_get_descriptor (struct usbd_function_instance *function, u8 *buffer, int max, int descriptor_type, int index);
+/*! @} */
+
diff -uNr linux/drivers/no-otg/otg/usbp-pcd.h linux/drivers/otg/otg/usbp-pcd.h
--- linux/drivers/no-otg/otg/usbp-pcd.h	1970-01-01 01:00:00.000000000 +0100
+++ linux/drivers/otg/otg/usbp-pcd.h	2006-09-01 21:41:34.000000000 +0200
@@ -0,0 +1,279 @@
+/*
+ * otg/otg/usbp-pcd.h - OTG Peripheral Controller Driver
+ *
+ *      Copyright (c) 2004 Belcarra
+ *
+ * By: 
+ *      Stuart Lynne <sl@belcarra.com>, 
+ *      Tom Rushworth <tbr@belcarra.com>, 
+ *      Bruce Balden <balden@belcarra.com>
+ *
+ */
+
+/*!
+ * @file otg/otg/usbp-pcd.h
+ * @brief Peripheral Controller Driver Related Structures and Definitions.
+ *
+ * @ingroup USBP
+ */
+
+/*!
+ * The pcd_ops structure contains pointers to all of the functions implemented for the
+ * linked in driver. Having them in this structure allows us to easily determine what
+ * functions are available without resorting to ugly compile time macros or ifdefs
+ *
+ * There is only one instance of this, defined in the device specific lower layer.
+ */
+struct usbd_pcd_ops {
+
+        /* mandatory */
+        u8 bmAttributes;
+        int max_endpoints;
+        int ep0_packetsize;
+        u32 capabilities;                               /* UDC Capabilities - see usbd-bus.h for details */
+        u8 bMaxPower;
+        char *name;
+        u8 ports;
+
+
+        /* 3. called by ot_init() if defined */
+        int (* serial_init) (struct pcd_instance *);    /* get device serial number if available */
+
+        /* 4. called from usbd_enable_function_irq() - REQUIRED */
+        int (*request_endpoints)                        /* process endpoint_request list and return endpoint_map */
+                (struct pcd_instance *,
+                 struct usbd_endpoint_map *,            /* showing allocated endpoints with attributes, addresses */
+                 int, struct usbd_endpoint_request*);   /* and other associated information */
+
+        /* 5. called from usbd_enable_function_irq() - REQUIRED */
+        int (*set_endpoints)                            /* setup required endpoints in hardware */
+                (struct pcd_instance *, int , struct usbd_endpoint_map *);      
+
+        /* 6. called by xxx() if defined */
+        void (*enable) (struct pcd_instance *);         /* enable the UDC */
+
+        /* 7. called by xxx() if defined */
+        void (*disable) (struct pcd_instance *);        /* disable the UDC */
+
+        /* 8. called by xxx() if defined */
+        void (*start) (struct pcd_instance *);          /* called for DEVICE_CREATE to start the UDC */
+        void (*stop) (struct pcd_instance *);           /* called for DEVICE_DESTROY to stop the UDC */
+        void (*disable_ep) 
+                (struct pcd_instance *, 
+                 unsigned int ep);                      /* called for DEVICE_DESTROTY to disable endpoint zero */
+
+        /* 9. called by xxx() if defined */
+        void (*set_address) 
+                (struct pcd_instance *,
+                unsigned char address);                 /* called for DEVICE_RESET and DEVICE_ADDRESS_ASSIGNED to */
+
+
+        /* 10. called by xxx() if defined */
+        void (*setup_ep) 
+                (struct pcd_instance *,
+                unsigned int ep,                        /* called for DEVICE_CONFIGURED to setup specified endpoint for use */
+                struct usbd_endpoint_instance *endpoint); 
+
+        /* 11. called by xxx() if defined */
+        void (*reset_ep) 
+                (struct pcd_instance *,
+                unsigned int ep);                       /* called for DEVICE_RESET to reset endpoint zero */
+
+
+        /* 12. called by usbd_send_urb() - REQUIRED */
+        void (*start_endpoint_in)                       /* start an IN urb */
+                (struct pcd_instance *,
+                struct usbd_endpoint_instance *);       
+
+        /* 13. called by usbd_start_recv() - REQUIRED */
+        void (*start_endpoint_out)                      /* start an OUT urb */
+                (struct pcd_instance *,
+                struct usbd_endpoint_instance *);
+
+        /* 14. called by usbd_cancel_urb_irq() */
+        void (*cancel_in_irq) 
+                (struct pcd_instance *,
+                struct usbd_urb *urb);                  /* cancel active urb for IN endpoint */
+        void (*cancel_out_irq) 
+                (struct pcd_instance *,
+                struct usbd_urb *urb);                  /* cancel active urb for OUT endpoint */
+
+
+        /* 15. called from ep0_recv_setup() for GET_STATUS request */
+        int (*endpoint_halted) 
+                (struct pcd_instance *, 
+                 struct usbd_endpoint_instance *);      /* Return non-zero if requested endpoint is halted */
+        int (*halt_endpoint) 
+                (struct pcd_instance *, 
+                 struct usbd_endpoint_instance *,
+                 int);                                  /* Return non-zero if requested endpoint clear fails */
+
+        /* 16 */
+        void (*startup_events) (struct pcd_instance *);   /* perform UDC specific USB events */
+
+	/* 17. root hub operations
+	 */
+	int (*hub_status) (struct pcd_instance *);
+	int (*port_status) (struct pcd_instance *, int);
+	int (*hub_feature) (struct pcd_instance *, int, int);
+	int (*port_feature) (struct pcd_instance *, int, int, int);
+	int (*get_ports_status) (struct pcd_instance *);
+	int (*wait_for_change) (struct pcd_instance *);
+};
+
+extern struct usbd_pcd_ops usbd_pcd_ops;
+
+
+/*! pcd_rcv_next_irq - get the current or next urb to receive into
+ * Called by UDC driver to get current receive urb or next available receive urb.
+ */
+static __inline__ struct usbd_urb * pcd_rcv_next_irq (struct usbd_endpoint_instance *endpoint)
+{
+        if (!endpoint->rcv_urb)
+                if ( (endpoint->rcv_urb = usbd_first_urb_detached_irq (&endpoint->rdy)))
+                        endpoint->rcv_urb->status = RECV_IN_PROGRESS;
+
+        //TRACE_MSG1(PCD, "BUS_RCV_URB: %p", endpoint->rcv_urb);
+        return endpoint->rcv_urb;
+}
+
+
+/*! pcd_rcv_complete_irq - complete a receive operation
+ * Called by UDC driver to show completion of outstanding I/O, this will
+ * update the urb length and if necessary will finish the urb passing it to the
+ * bottom half which will in turn call the function driver callback.
+ */
+struct usbd_urb * pcd_rcv_complete_irq (struct usbd_endpoint_instance *endpoint, int len, int urb_bad);
+struct usbd_urb * pcd_rcv_finished_irq (struct usbd_endpoint_instance *endpoint, int len, int urb_bad);
+
+
+/*! pcd_rcv_cancelled_irq - cancel current receive urb
+ * Called by UDC driver to cancel the current receive urb.
+ */
+static __inline__ void pcd_rcv_cancelled_irq (struct usbd_endpoint_instance *endpoint)
+{
+        struct usbd_urb *rcv_urb;
+
+        TRACE_MSG1(PCD, "BUS_RCV CANCELLED: %p", (int) endpoint->rcv_urb);
+        RETURN_IF (! (rcv_urb = endpoint->rcv_urb));
+        //TRACE_MSG1(PCD, "rcv_urb: %p", (int)endpoint->rcv_urb);
+        usbd_urb_finished_irq (rcv_urb, RECV_CANCELLED);
+        endpoint->sent = endpoint->last = 0;
+        endpoint->rcv_urb = NULL;
+}
+
+
+/*! pcd_tx_next_irq - get the current or next urb to transmit
+ * Called by UDC driver to get current transmit urb or next available transmit urb.
+ */
+static __inline__ struct usbd_urb * pcd_tx_next_irq (struct usbd_endpoint_instance *endpoint)
+{
+        if (!endpoint->tx_urb)
+                if ( (endpoint->tx_urb = usbd_first_urb_detached_irq (&endpoint->tx))) {
+#if 0
+                        int i;
+                        u8 *cp = endpoint->tx_urb->buffer;
+
+                        TRACE_MSG2(PCD, "NEXT TX: length: %d flags: %x", 
+                                        endpoint->tx_urb->actual_length, endpoint->tx_urb->flags);
+
+                        for (i = 0; i < endpoint->tx_urb->actual_length;  i+= 8)
+
+                                TRACE_MSG8(PCD, "SENT  %02x %02x %02x %02x %02x %02x %02x %02x",
+                                                cp[i + 0], cp[i + 1], cp[i + 2], cp[i + 3],
+                                                cp[i + 4], cp[i + 5], cp[i + 6], cp[i + 7]
+                                          );
+#endif
+                        endpoint->tx_urb->status = SEND_IN_PROGRESS;
+                }
+        //TRACE_MSG1(PCD, "BUS_TX NEXT TX_URB: %p", (int)endpoint->tx_urb);
+        return endpoint->tx_urb;
+}
+
+
+/*! pcd_tx_complete_irq - complete a transmit operation
+ * Called by UDC driver to show completion of an outstanding I/O, this will update the urb sent
+ * information and if necessary will finish the urb passing it to the bottom half which will in
+ * turn call the function driver callback.
+ */
+struct usbd_urb * pcd_tx_complete_irq (struct usbd_endpoint_instance *endpoint, int restart);
+
+
+/*! pcd_tx_cancelled_irq - finish pending tx_urb with SEND_CANCELLED
+ * Called by UDC driver to cancel the current transmit urb.
+ */
+static __inline__ void pcd_tx_cancelled_irq (struct usbd_endpoint_instance *endpoint)
+{
+        struct usbd_urb *tx_urb;
+
+        TRACE_MSG1(PCD, "BUS_TX CANCELLED: %p", (int) endpoint->tx_urb);
+        RETURN_IF (! (tx_urb = endpoint->tx_urb));
+        usbd_urb_finished_irq (tx_urb, SEND_CANCELLED);
+        endpoint->sent = endpoint->last = 0;
+        endpoint->tx_urb = NULL;
+}
+
+
+/*! pcd_tx_sendzlp - test if we need to send a ZLP
+ * This has a side-effect of reseting the USBD_URB_SENDZLP flag.
+ */
+static __inline__ int pcd_tx_sendzlp (struct usbd_endpoint_instance *endpoint)
+{
+        struct usbd_urb *tx_urb = endpoint->tx_urb;
+        RETURN_FALSE_IF (!tx_urb || (tx_urb->actual_length < endpoint->sent) || ! (tx_urb->flags & USBD_URB_SENDZLP)); 
+        tx_urb->flags &= ~USBD_URB_SENDZLP;
+        return TRUE;
+}
+
+
+/*! pcd_rcv_complete_irq - complete a receive
+ * Called from rcv interrupt to complete.
+ */
+static __inline__ void pcd_rcv_fast_complete_irq (struct usbd_endpoint_instance *endpoint, struct usbd_urb *rcv_urb)
+{
+        //TRACE_MSG1(PCD, "BUS_RCV FAST COMPLETE: %d", rcv_urb->actual_length);
+        usbd_urb_finished_irq (rcv_urb, RECV_OK);
+} 
+
+/*! pcd_recv_setup - process a device request
+ * Note that we verify if a receive urb has been queued for H2D with non-zero wLength
+ * and return -EINVAL to stall if the upper layers have not properly tested for and 
+ * setup a receive urb in this case.
+ */
+static __inline__ int pcd_recv_setup_irq (struct pcd_instance *pcd, struct usbd_device_request *request)
+{
+        struct usbd_bus_instance *bus = pcd->bus;
+        struct usbd_endpoint_instance *endpoint = bus->endpoint_array + 0;
+        TRACE_SETUP (PCD, request);
+        RETURN_EINVAL_IF (usbd_device_request(bus, request));         // fail if already failed
+
+        //TRACE_MSG2(PCD, "BUS_RECV SETUP: RCV URB: %x TX_URB: %x", (int)endpoint->rcv_urb, (int)endpoint->tx_urb);
+        RETURN_ZERO_IF ( (request->bmRequestType & USB_REQ_DIRECTION_MASK) == USB_REQ_DEVICE2HOST);      
+        RETURN_ZERO_IF (!le16_to_cpu (request->wLength));
+        RETURN_EINVAL_IF (!endpoint->rcv_urb);
+        return 0;
+}
+
+/*! pcd_recv_setup_emulate_irq - emulate a device request
+ * Called by the UDC driver to inject a SETUP request. This is typically used
+ * by drivers for UDC's that do not pass all SETUP requests to us but instead give us
+ * a configuration change interrupt. 
+ */
+int pcd_recv_setup_emulate_irq(struct usbd_bus_instance *, u8, u8, u16, u16, u16);
+
+/*! pcd_ep0_reset_irq - reset ep0 endpoint 
+ */
+static void __inline__ pcd_ep0_reset_endpoint_irq (struct usbd_endpoint_instance *endpoint)
+{
+        TRACE_MSG0(PCD, "BUS_EP0 RESET");
+        pcd_tx_cancelled_irq (endpoint);
+        pcd_rcv_cancelled_irq (endpoint);
+        endpoint->sent = endpoint->last = 0;
+}
+
+/*! pcd_check_device_feature - verify that feature is set or clear
+ * Check current feature setting and emulate SETUP Request to set or clear
+ * if required.
+ */
+void pcd_check_device_feature(struct usbd_bus_instance *, int , int );
+
diff -uNr linux/drivers/no-otg/otgcore/Makefile linux/drivers/otg/otgcore/Makefile
--- linux/drivers/no-otg/otgcore/Makefile	1970-01-01 01:00:00.000000000 +0100
+++ linux/drivers/otg/otgcore/Makefile	2006-09-01 21:41:34.000000000 +0200
@@ -0,0 +1,107 @@
+#
+# Belcarra OTG - On-The-Go 
+#
+# Copyright (c) 2004 Belcarra Technologies Corp
+
+TOPDIR ?= ../../../..
+
+subdir-y 	:= 
+subdir-m 	:=
+subdir-n 	:=
+subdir- 	:=
+
+
+# The target object and module list name.
+
+O_TARGET	:= otgcore_target.o 
+
+# Objects that export symbols.
+
+export-objs	:= core-init-l24.o otg.o otg-trace.o otg-mesg.o usbp-bops.o usbp-fops.o otg-mesg-l24.o
+
+# Multipart objects. (core layer)
+
+list-multi		:= otgtrace.o otgcore.o otgadmin.o 
+
+otgcore-objs		:= core-init-l24.o otg.o \
+			otg-trace.o otg-trace-l24.o \
+			otg-mesg.o otg-mesg-l24.o \
+			otg-fw.o \
+			usbp-fops.o usbp-bops.o 
+
+
+usbprocfs-objs		:= usbp-procfs.o
+
+
+ifeq ("$(CONFIG_OTG_FW_MN)", "y")
+otgcore-objs		+= otg-fw-mn.o
+else
+otgcore-objs		+= otg-fw-df.o
+endif
+
+
+# Optional parts of multipart objects.
+
+# Object file lists.
+
+obj-y		:=
+obj-m		:=
+obj-n		:=
+obj-		:=
+
+# Each configuration option enables a list of files.
+
+obj-$(CONFIG_OTG)		+= otgcore.o 
+obj-$(CONFIG_OTG_PROCFSM)       += usbprocfs.o
+
+
+# Object files in subdirectories
+
+
+# Extract lists of the multi-part drivers.
+# The 'int-*' lists are the intermediate files used to build the multi's.
+
+multi-y		:= $(filter $(list-multi), $(obj-y))
+multi-m		:= $(filter $(list-multi), $(obj-m))
+int-y		:= $(sort $(foreach m, $(multi-y), $($(basename $(m))-objs)))
+int-m		:= $(sort $(foreach m, $(multi-m), $($(basename $(m))-objs)))
+
+# Files that are both resident and modular: remove from modular.
+
+obj-m		:= $(filter-out $(obj-y), $(obj-m))
+int-m		:= $(filter-out $(int-y), $(int-m))
+
+# Translate to Rules.make lists.
+
+O_OBJS		:= $(filter-out $(export-objs), $(obj-y))
+OX_OBJS		:= $(filter     $(export-objs), $(obj-y))
+M_OBJS		:= $(sort $(filter-out $(export-objs), $(obj-m)))
+MX_OBJS		:= $(sort $(filter     $(export-objs), $(obj-m)))
+MI_OBJS		:= $(sort $(filter-out $(export-objs), $(int-m)))
+MIX_OBJS	:= $(sort $(filter     $(export-objs), $(int-m)))
+
+# The global Rules.make.
+
+include $(TOPDIR)/Rules.make
+OTG=$(TOPDIR)/drivers/otg
+#USBDCORE_DIR=$(OTG)/usbdcore
+OTGCORE_DIR=$(OTG)/otgcore
+EXTRA_CFLAGS +=          -Wno-missing-prototypes -Wno-unused -Wno-format -I$(OTG)
+EXTRA_CFLAGS_nostdinc += -Wno-missing-prototypes -Wno-unused -Wno-format -I$(OTG) 
+
+vpath %.c $(OTGCORE_DIR)
+
+# Link rules for multi-part drivers.
+
+otgadmin.o: $(otgadmin-objs)
+	$(LD) -r -o $@ $(otgadmin-objs)
+
+otgcore.o: $(otgcore-objs)
+	$(LD) -r -o $@ $(otgcore-objs)
+
+otgtrace.o: $(otgtrace-objs)
+	$(LD) -r -o $@ $(otgtrace-objs)
+
+usbprocfs.o: $(usbprocfs-objs)
+	$(LD) -r -o $@ $(usbprocfs-objs)
+
diff -uNr linux/drivers/no-otg/otgcore/Makefile-l26 linux/drivers/otg/otgcore/Makefile-l26
--- linux/drivers/no-otg/otgcore/Makefile-l26	1970-01-01 01:00:00.000000000 +0100
+++ linux/drivers/otg/otgcore/Makefile-l26	2006-09-01 21:41:34.000000000 +0200
@@ -0,0 +1,24 @@
+#
+# Belcarra OTG - On-The-Go 
+#
+# Copyright (c) 2004 Belcarra Technologies Corp
+
+OTG=$(TOPDIR)/drivers/otg
+EXTRA_CFLAGS +=          -Wno-missing-prototypes -Wno-unused -Wno-format -I$(OTG) 
+EXTRA_CFLAGS_nostdinc += -Wno-missing-prototypes -Wno-unused -Wno-format -I$(OTG) 
+
+#otgadmin-objs		:= admin-init-l24.o otg-admin.o otg-decode.o 
+#obj-$(CONFIG_OTG)	+= otgadmin.o 
+
+otgcore-objs		:= core-init-l24.o otg.o \
+			otg-trace.o otg-trace-l24.o \
+			otg-mesg.o otg-mesg-l24.o \
+			otg-fw.o \
+			usbp-bops.o \
+			usbp-fops.o \
+
+obj-$(CONFIG_OTG)	+= otgcore.o 
+
+#otgtrace-objs		:= otg-trace.o
+#obj-$(CONFIG_OTG_TRACE)	+= otgtrace.o 
+
diff -uNr linux/drivers/no-otg/otgcore/TODO-USBP.txt linux/drivers/otg/otgcore/TODO-USBP.txt
--- linux/drivers/no-otg/otgcore/TODO-USBP.txt	1970-01-01 01:00:00.000000000 +0100
+++ linux/drivers/otg/otgcore/TODO-USBP.txt	2006-09-01 21:41:34.000000000 +0200
@@ -0,0 +1,51 @@
+USBD Core Architecture ToDo                                     Stuart Lynne
+Belcarra                                        Fri Aug 13 14:37:13 PDT 2004 
+
+
+The following need to be addressed to support ongoing development and support
+for the USB Device core and functions.
+
+
+Specifically:
+        - finish acm, make CDC compliant, support all possible operations
+        - enhance network, add NDIS support, 
+        - wmc - implement wmc
+        - obex
+        - mass storage
+
+
+These require:
+
+        - enhancements to allow building composite functions
+        - common routines for handling CDC class requirements
+
+
+1. interface list
+        
+        - currently we have an endpoint list that allows each endpoint to be
+          mapped to a function
+
+        - we need to do the same for interfaces, allow each interface to be
+          owned by a function, this will allow ep0 to multiplex per
+          interface standard requests to the proper function driver
+
+
+2. common functions
+
+        - a common send_interrupt_urb function
+        
+                int usbd_send_interrupt(struct usbd_function_instance *,
+                        int, void *, int,  int (*callback) (struct usbd_urb *, int));
+                
+
+        - this creates an interrupt urb on the required endpoint, copies the
+          data in, and starts it with the appropriate callback etc.
+
+
+        - notification
+
+                usbd_network_notfication
+                usbd_response_available
+                usbd_serial_state
+
+
diff -uNr linux/drivers/no-otg/otgcore/core-init-l24.c linux/drivers/otg/otgcore/core-init-l24.c
--- linux/drivers/no-otg/otgcore/core-init-l24.c	1970-01-01 01:00:00.000000000 +0100
+++ linux/drivers/otg/otgcore/core-init-l24.c	2006-09-01 21:41:34.000000000 +0200
@@ -0,0 +1,173 @@
+/*
+ * otg/otg/core-init-l24.c - OTG Peripheral Controller Driver Module Initialization
+ *
+ *      Copyright (c) 2004 Belcarra
+ *
+ * By: 
+ *      Stuart Lynne <sl@belcara.com>, 
+ *      Tom Rushworth <tbr@belcara.com>, 
+ *      Bruce Balden <balden@belcara.com>
+ *
+ * This is the linux 2.4 version.
+ *
+ */
+/*!
+ * @file otg/otgcore/core-init-l24.c
+ * @brief OTG Core Linux initialization.
+ *
+ * This file is the starting point for defining the Linux OTG Core 
+ * driver. It references and starts all of the other components that
+ * must be "linked" into the OTGCORE mdoule.
+ *
+ * @ingroup OTGCore
+ */
+
+
+#include <otg/otg-compat.h>
+#include <otg/otg-module.h>
+
+#include <otg/usbp-chap9.h>
+#include <otg/usbp-func.h>
+#include <otg/usbp-bus.h>
+#include <otg/otg-trace.h>
+#include <otg/otg-api.h>
+#include <otg/otg-tcd.h>
+#include <otg/otg-hcd.h>
+#include <otg/otg-pcd.h>
+#include <otg/otg-ocd.h>
+
+EMBED_LICENSE(); // When supported by the OS, embed license information in the binary
+
+
+#ifdef OTG_MALLOC_TEST
+int otg_mallocs;
+#endif /* OTG_MALLOC_TEST */
+
+otg_tag_t CORE;
+
+#ifdef CONFIG_OTG_FW_MN
+struct otg_firmware *otg_firmware_loaded = &otg_firmware_mn;
+struct otg_firmware *otg_firmware_orig = &otg_firmware_mn;
+#else /* CONFIG_OTG_FW_MN */
+struct otg_firmware *otg_firmware_loaded = &otg_firmware_df;
+struct otg_firmware *otg_firmware_orig = &otg_firmware_df;
+#endif /* CONFIG_OTG_FW_MN */
+
+struct otg_firmware *otg_firmware_loading;
+
+
+struct otg_instance otg_instance_private;
+
+struct hcd_instance hcd_instance_private = { otg: &otg_instance_private, };
+struct ocd_instance ocd_instance_private = { otg: &otg_instance_private, };
+struct pcd_instance pcd_instance_private = { otg: &otg_instance_private, };
+struct tcd_instance tcd_instance_private = { otg: &otg_instance_private, };
+
+struct otg_instance otg_instance_private = {
+        hcd: &hcd_instance_private,
+        ocd: &ocd_instance_private,
+        pcd: &pcd_instance_private,
+        tcd: &tcd_instance_private,
+};
+
+struct otg_instance *otg_instance = &otg_instance_private;
+
+char * otg_get_state_name(int state)
+{
+        struct otg_state *otg_state;
+        if (!otg_firmware_loaded || (state >= otg_firmware_loaded->number_of_states))
+                return "UNKNOWN_STATE";
+
+        otg_state = otg_firmware_loaded->otg_states + state;
+        return otg_state->name;
+}
+
+
+/* ************************************************************************************* */
+
+
+/* otg_modinit - linux module initialization
+ */
+static int otg_modinit (void)
+{
+	int message_init = 0;
+	int message_init_l24 = 0;
+	int trace_init = 0;
+	int trace_init_l24 = 0;
+
+
+        printk(KERN_INFO"%s: \n", __FUNCTION__);
+
+        trace_init = otg_trace_init();
+        trace_init_l24 = otg_trace_init_l24();
+
+        CORE = otg_trace_obtain_tag();
+
+        TRACE_MSG0(CORE,"--");
+        otg_instance->current_outputs = otg_firmware_loaded->otg_states;
+        otg_instance->previous_outputs = otg_firmware_loaded->otg_states;
+
+	/* initialize otg-mesg and usbp 
+	 */
+        THROW_IF((message_init = otg_message_init(otg_instance)), error);
+        THROW_IF((message_init_l24 = otg_message_init_l24(otg_instance)), error);
+	THROW_IF(usbd_device_init(), error);
+
+        otg_set_ocd_ops(NULL);
+        otg_set_tcd_ops(NULL);
+        otg_set_hcd_ops(NULL);
+        otg_set_pcd_ops(NULL);
+
+        CATCH(error) {
+                CORE = otg_trace_invalidate_tag(CORE);
+                if (trace_init_l24 )otg_trace_exit_l24();
+                if (trace_init) otg_trace_exit();
+                if (message_init_l24 )otg_message_exit_l24();
+                if (message_init) otg_message_exit();
+                return -EINVAL;
+        }
+        return 0;
+}
+module_init (otg_modinit);
+
+
+#if OTG_EPILOGUE  /* Set nonzero in <otg-module.h> when -DMODULE is in force */
+/* otg_modexit - This is *only* used for drivers compiled and used as a module.
+ */
+static void otg_modexit (void)
+{
+        printk(KERN_INFO"%s: \n", __FUNCTION__);
+
+	usbd_device_exit();
+        otg_message_exit_l24();
+
+        CORE = otg_trace_invalidate_tag(CORE);
+        otg_trace_exit();
+
+        if (otg_firmware_loading) {
+                lkfree(otg_firmware_loading->otg_states);
+                lkfree(otg_firmware_loading->otg_tests);
+                lkfree(otg_firmware_loading);
+        }
+        if (otg_firmware_loaded && (otg_firmware_loaded != otg_firmware_orig)) {
+                lkfree(otg_firmware_loaded->otg_states);
+                lkfree(otg_firmware_loaded->otg_tests);
+                lkfree(otg_firmware_loaded);
+        }
+}
+#endif
+
+#if defined (CONFIG_OTG_USBD_PM)
+int usbd_load(char * arg) { return 0; }
+int usbd_unload(char *arg) { return 0; }
+OTG_EXPORT_SYMBOL(usbd_load);
+OTG_EXPORT_SYMBOL(usbd_unload);
+#endif
+
+MOD_EXIT(otg_modexit);
+OTG_EXPORT_SYMBOL(otg_get_state_name);
+
+#ifdef OTG_MALLOC_TEST
+OTG_EXPORT_SYMBOL(otg_mallocs);
+#endif /* OTG_MALLOC_TEST */
+
diff -uNr linux/drivers/no-otg/otgcore/hub.c linux/drivers/otg/otgcore/hub.c
--- linux/drivers/no-otg/otgcore/hub.c	1970-01-01 01:00:00.000000000 +0100
+++ linux/drivers/otg/otgcore/hub.c	2006-09-01 21:41:34.000000000 +0200
@@ -0,0 +1,607 @@
+/*
+ * otg/otgcore/hub.c
+ *
+ *      Copyright (c) 2004 Belcarra
+ *
+ * By: 
+ *      Stuart Lynne <sl@belcarra.com>, 
+ *      Tom Rushworth <tbr@belcarra.com>, 
+ *      Bruce Balden <balden@belcarra.com>
+ *
+ * This is a root hub function.
+ *
+ */
+/*!
+ * @file otg/otgcore/hub.c
+ * @brief A simple hub function driver.
+ *
+ *
+ * @ingroup USBDCore
+ */
+#include <otg/otg-compat.h>
+#include <linux/config.h>
+#include <linux/kernel.h>
+#include <linux/version.h>
+#include <linux/ctype.h>
+#include <linux/timer.h>
+#include <linux/slab.h>
+#include <linux/errno.h>
+
+#include <otg/usbp-chap9.h>
+#include <otg/usbp-func.h>
+#include <otg/usbp-bus.h>
+
+#ifdef CONFIG_OTG_ROOT_HUB
+/*
+ * Root Hub Configuration
+ *
+ * Endpoint, Class, Interface, Configuration and Device descriptors/descriptions
+ * Data Interface Alternate description(s)
+ * Interface description(s)
+ * Configuration description(s)
+ * Device Description
+ */
+
+#define BULK_INT        0x00
+#define ENDPOINTS       0x01
+
+#if 0
+
+Configuration descriptor [1      ] 09 02 19 00 01 01 02 00 00 
+                                   09 02 19 00 01 01 00 40 00
+
+Interface descriptor     [1:1:0  ] 09 04 00 00 01 09 00 00 03 
+Endpoint descriptor      [1:1:0:1] 07 05 81 03 10 00 ff 
+
+#endif
+
+#if defined(OTG_WINCE)
+
+static struct hub_descriptor rh_hub_descriptor;
+struct usbd_endpoint_descriptor rh_data;
+
+static struct usbd_endpoint_descriptor *rh_default[] = { &rh_data, };
+u8 rh_indexes[] = { BULK_INT, };
+
+static struct usbd_interface_descriptor rh_data_alternate_descriptor;
+static struct usbd_alternate_description rh_data_alternate_descriptions[1];
+};
+
+static struct usbd_interface_description rh_interfaces[1];
+
+struct usbd_configuration_descriptor rh_configuration_descriptor;
+
+struct usbd_configuration_description rh_description[1];
+
+static struct usbd_device_descriptor rh_device_descriptor;
+#ifdef CONFIG_OTG_HIGH_SPEED
+static struct usbd_device_qualifier_descriptor rh_device_qualifier_descriptor;
+#endif /* CONFIG_OTG_HIGH_SPEED */
+
+static struct usbd_endpoint_request rh_endpoint_requests[ENDPOINTS+1] = {
+        { 1, 0, 0, USB_DIR_IN | USB_ENDPOINT_INTERRUPT, 16, 64, },
+        { 0, },
+};
+
+static struct usbd_otg_descriptor rh_otg_descriptor;
+
+struct usbd_device_description rh_device_description;
+
+void hub_global_init(void)
+{
+        ZERO(rh_hub_descriptor);
+        rh_hub_descriptor.bDescLength = 0x09;
+        rh_hub_descriptor.bDescriptorType = USB_DT_HUB;
+        rh_hub_descriptor.bNbrPorts = 0x00;
+        rh_hub_descriptor.wHubCharacteristics = HUB_GANGED_POWER | HUB_GLOBAL_OVERCURRENT;
+        rh_hub_descriptor.bPwrOn2PwrGood = 0x01;
+        rh_hub_descriptor.bHubContrCurrent = 0x01;
+        rh_hub_descriptor.DeviceRemovable = 0x00;
+        rh_hub_descriptor.PortPwrCtrlMask = 0xff;
+
+        ZERO(rh_data);
+        rh_data.bLength: 0x07; 
+        rh_data.bDescriptorType = USB_DT_ENDPOINT; 
+        rh_data.bEndpointAddress = USB_DIR_IN; 
+        rh_data.bmAttributes = INTERRUPT; 
+        rh_data.wMaxPacketSize = __constant_cpu_to_le16(0x4);
+        rh_data.bInterval: 0xff; 
+
+        ZERO(rh_data_alternate_desriptor);
+        rh_data_alternate_descriptor.bLength = 0x09;
+        rh_data_alternate_descriptor.bDescriptorType = USB_DT_INTERFACE;
+        rh_data_alternate_descriptor.bNumEndpoints = 0x01;
+        rh_data_alternate_descriptor.bInterfaceClass = USB_CLASS_HUB;
+
+        ZERO(rh_data_alternate_descriptions);
+        rh_data_alternate_descriptors[0].iInterface =Root hub - Status";
+        rh_data_alternate_descriptors[0].interface_descriptor = &rh_data_alternate_descriptor;
+        rh_data_alternate_descriptors[0].endpoints = sizeof (rh_default) / sizeof(struct usbd_endpoint_descriptor *);
+        rh_data_alternate_descriptors[0].endpoint_list = rh_default;
+        rh_data_alternate_descriptors[0].endpoint_indexes = rh_indexes;
+
+        ZERO(rh_interfaces);
+
+        rh_interfaces[0].alternates = sizeof (rh_data_alternate_descriptions) / sizeof (struct usbd_alternate_description);
+        rh_interfaces[0].alternate_list = rh_data_alternate_descriptions;
+
+
+        ZERO(rh_configuration_descriptor);
+        rh_configuration_descriptor.bLength = 0x09;
+        rh_configuration_descriptor.bDescriptorType = USB_DT_CONFIGURATION;
+        rh_configuration_descriptor.bNumInterfaces = sizeof (rh_interfaces) / sizeof (struct usbd_interface_description);
+        rh_configuration_descriptor.bConfigurationValue = 0x01;
+
+
+        ZERO(rh_description);
+        rh_description[0].configuration_descriptor =  &rh_configuration_descriptor;
+        rh_description[0].iConfiguration = Root Hub";
+        rh_description[0].bNumInterfaces = sizeof (rh_interfaces) / sizeof (struct usbd_interface_description);
+        rh_description[0].interface_list = rh_interfaces;
+
+        ZERO(rh_device_descriptor);
+
+        rh_device_descriptor.bLength = sizeof(struct usbd_device_descriptor);
+        rh_device_descriptor.bDescriptorType = USB_DT_DEVICE;
+        rh_device_descriptor.bcdUSB = __constant_cpu_to_le16(USB_BCD_VERSION);
+        rh_device_descriptor.bDeviceClass = USB_CLASS_HUB;
+
+
+#ifdef CONFIG_OTG_HIGH_SPEED
+        ZERO(rh_device_qualifier_descriptor);
+        rh_device_qualifier_descriptor.bLength = sizeof(struct usbd_device_qualifier_descriptor);
+        rh_device_qualifier_descriptor.bDescriptorType = USB_DT_DEVICE_QUALIFIER;
+        rh_device_qualifier_descriptor.bcdUSB = __constant_cpu_to_le16(USB_BCD_VERSION);
+        rh_device_qualifier_descriptor.bDeviceClass = USB_CLASS_HUB;
+#endif /* CONFIG_OTG_HIGH_SPEED */
+
+        ZERO(rh_otg_descriptor);
+        rh_otg_descriptor.bLength = sizeof(struct usbd_otg_descriptor);
+        rh_otg_descriptor.bDescriptorType = USB_DT_OTG;
+        rh_otg_descriptor.bmAttributes = 0;
+
+
+        ZERO(rh_device_description);
+        rh_device_description.device_descriptor = &rh_device_descriptor;
+        #ifdef CONFIG_OTG_HIGH_SPEED
+        rh_device_description.device_qualifier_descriptor = &rh_device_qualifier_descriptor;
+        #endif /* CONFIG_OTG_HIGH_SPEED */
+        rh_device_description.otg_descriptor = &rh_otg_descriptor;
+        rh_device_description.iManufacturer = "";
+        rh_device_description.iProduct = "root hub";
+        rh_device_description.endpointsRequested = ENDPOINTS;
+        rh_device_description.requestedEndpoints = rh_endpoint_requests;
+
+}
+#else /* defined(OTG_WINCE) */
+
+static struct hub_descriptor rh_hub_descriptor = {
+        bDescLength: 0x09,
+        bDescriptorType: USB_DT_HUB,
+        bNbrPorts: 0x00,
+        wHubCharacteristics: HUB_GANGED_POWER | HUB_GLOBAL_OVERCURRENT,
+        bPwrOn2PwrGood: 0x01,
+        bHubContrCurrent: 0x01,
+        DeviceRemovable: 0x00,
+        PortPwrCtrlMask: 0xff,
+};
+
+struct usbd_endpoint_descriptor rh_data = {
+        bLength: 0x07, 
+        bDescriptorType: USB_DT_ENDPOINT, 
+        bEndpointAddress: USB_DIR_IN, 
+        bmAttributes: INTERRUPT, 
+        wMaxPacketSize: __constant_cpu_to_le16(0x4), 
+        bInterval: 0xff, 
+};
+
+static struct usbd_endpoint_descriptor *rh_default[] = { &rh_data, };
+u8 rh_indexes[] = { BULK_INT, };
+
+static struct usbd_interface_descriptor rh_data_alternate_descriptor = {
+        bLength: 0x09,
+        bDescriptorType: USB_DT_INTERFACE,
+        bNumEndpoints: 0x01,
+        bInterfaceClass: USB_CLASS_HUB,
+}; 
+
+static struct usbd_alternate_description rh_data_alternate_descriptions[] = {
+      { iInterface:"Root hub - Status",
+              interface_descriptor: &rh_data_alternate_descriptor,
+              endpoints:sizeof (rh_default) / sizeof(struct usbd_endpoint_descriptor *),
+              endpoint_list: rh_default,
+              endpoint_indexes: rh_indexes,
+      },
+};
+
+static struct usbd_interface_description rh_interfaces[] = {
+      { alternates:sizeof (rh_data_alternate_descriptions) / sizeof (struct usbd_alternate_description),
+              alternate_list:rh_data_alternate_descriptions,},
+};
+
+
+struct usbd_configuration_descriptor rh_configuration_descriptor = {
+        bLength: 0x09,
+        bDescriptorType: USB_DT_CONFIGURATION,
+        bNumInterfaces: sizeof (rh_interfaces) / sizeof (struct usbd_interface_description),
+        bConfigurationValue: 0x01,
+};
+
+struct usbd_configuration_description rh_description[] = {
+      { configuration_descriptor: &rh_configuration_descriptor,
+              iConfiguration:"Root Hub",
+                bNumInterfaces:sizeof (rh_interfaces) / sizeof (struct usbd_interface_description),
+              interface_list:rh_interfaces,},
+};
+
+static struct usbd_device_descriptor rh_device_descriptor = {
+        bLength: sizeof(struct usbd_device_descriptor),
+        bDescriptorType: USB_DT_DEVICE,
+        bcdUSB: __constant_cpu_to_le16(USB_BCD_VERSION),
+        bDeviceClass: USB_CLASS_HUB,
+};
+
+#ifdef CONFIG_OTG_HIGH_SPEED
+static struct usbd_device_qualifier_descriptor rh_device_qualifier_descriptor = {
+        bLength: sizeof(struct usbd_device_qualifier_descriptor),
+        bDescriptorType: USB_DT_DEVICE_QUALIFIER,
+        bcdUSB: __constant_cpu_to_le16(USB_BCD_VERSION),
+        bDeviceClass: USB_CLASS_HUB,
+};
+#endif /* CONFIG_OTG_HIGH_SPEED */
+
+static struct usbd_endpoint_request rh_endpoint_requests[ENDPOINTS+1] = {
+        { 1, 0, 0, USB_DIR_IN | USB_ENDPOINT_INTERRUPT, 16, 64, },
+        { 0, },
+};
+
+static struct usbd_otg_descriptor rh_otg_descriptor = {
+        bLength : sizeof(struct usbd_otg_descriptor),
+        bDescriptorType: USB_DT_OTG,
+        bmAttributes: 0,
+};
+
+struct usbd_device_description rh_device_description = {
+        device_descriptor: &rh_device_descriptor,
+        #ifdef CONFIG_OTG_HIGH_SPEED
+        device_qualifier_descriptor: &rh_device_qualifier_descriptor,
+        #endif /* CONFIG_OTG_HIGH_SPEED */
+        otg_descriptor: &rh_otg_descriptor,
+        iManufacturer: "",
+        iProduct: "root hub",
+        endpointsRequested: ENDPOINTS,
+        requestedEndpoints: rh_endpoint_requests,
+};
+#endif /* defined(OTG_WINCE) */
+
+/* root hub Private variables ******************************************************************** */
+
+struct rh_private {
+        struct usbd_function_instance *function;
+        int usb_driver_registered;              // non-zero if usb function registered
+        unsigned char connected;                // non-zero if connected to host (configured)
+        u8 ports;
+        u8 hub_features;
+        u32 *port_features;
+        u32 ports_status;
+        struct WORK_STRUCT bh;
+};
+
+struct rh_private rh_private;
+
+/* Transmit Function *************************************************************************** */
+
+void rh_task(void *data)
+{
+
+        //struct rh_private *rh = (struct rh_private *)data;
+        //struct usbd_function_instance *function = rh->function;
+        //struct usbd_bus_instance *bus = function->bus;
+
+        printk(KERN_INFO"%s: start\n", __FUNCTION__);
+#if 0
+        wait_queue_head_t rh_wait_queue;
+        init_waitqueue_head(&rh_wait_queue);
+
+        while (rh->bh.data && rh->connected) {
+                u32 ports_status = cpu_to_le32(bus->driver->bops->get_ports_status(bus));
+
+                printk(KERN_INFO"%s: status: %08x\n", __FUNCTION__, ports_status);
+
+                if (rh->ports_status != ports_status) {
+                        struct usbd_urb *urb = usbd_alloc_urb (function, BULK_INT, 4, NULL);
+                        BREAK_UNLESS(urb);
+                        memcpy(urb->buffer, &ports_status, 4);
+                        urb->actual_length = 4;
+                        if (usbd_send_urb(urb)) {
+                                printk(KERN_INFO"%s: usbd_send_urb failed\n", __FUNCTION__);
+                                usbd_dealloc_urb (urb);
+                        }
+                }
+                rh->ports_status = ports_status;
+                interruptible_sleep_on_timeout(&rh_wait_queue, 10000);
+        }
+#endif
+        printk(KERN_INFO"%s: exiting\n", __FUNCTION__);
+}
+
+/* USB Device Functions ************************************************************************ */
+
+/* rh_event_irq - process a device event
+ */
+void rh_event_irq (struct usbd_function_instance *function, usbd_device_event_t event, int data)
+{
+        struct rh_private *rh = &rh_private;
+
+        switch (event) {
+        case DEVICE_CONFIGURED:
+                rh->connected = 1;
+
+		SET_WORK_ARG(rh->bh, &rh_private);
+//                rh->bh.data = &rh_private;
+		SCHEDULE_WORK(rh_private.bh);
+//                schedule_task(&rh_private.bh);
+                break;
+
+        case DEVICE_RESET:
+        case DEVICE_DE_CONFIGURED:
+                BREAK_UNLESS(rh->connected);
+                rh->connected = 0;
+                rh->bh.data = 0;
+                break;
+        default: 
+                break;
+        }
+}
+
+/* copy_config
+ * @urb: pointer to urb
+ * @data: pointer to configuration data
+ * @length: length of data
+ *
+ * Copy configuration data to urb transfer buffer if there is room for it.
+ */
+static int copy_report (struct usbd_urb *urb, void *data, int size, int max_buf)
+{
+        int available;
+        int length = size;
+
+        //printk(KERN_INFO"%s: data: %p size: %d max: %d\n", __FUNCTION__, data, size, max_buf);
+        RETURN_EINVAL_UNLESS (urb);
+        RETURN_EINVAL_UNLESS (data);
+        RETURN_EINVAL_UNLESS (size);
+
+        RETURN_EINVAL_IF ((available = max_buf - urb->actual_length) <= 0);
+
+        //printk(KERN_INFO"%s: length: %d available: %d\n", __FUNCTION__, length, available);
+
+        length = (length < available) ? length : available;
+        memcpy (urb->buffer + urb->actual_length, data, length);
+        urb->actual_length += length;
+        return 0;
+}
+
+/* rh_recv_setup_irq - called to indicate urb has been received
+ */
+int rh_recv_setup_irq (struct usbd_function_instance *function, struct usbd_device_request *request)
+{
+        struct usbd_bus_instance *bus = function->bus;
+        u8 bRequest = request->bRequest;
+        u8 bmRequestType = request->bmRequestType;
+        u16 wValue = le16_to_cpu(request->wValue);
+        u16 wIndex = le16_to_cpu(request->wIndex);
+        u16 wLength = le16_to_cpu(request->wLength);
+
+        u8 direction  = bmRequestType & USB_REQ_DIRECTION_MASK;
+        u8 recipient = bmRequestType & USB_REQ_RECIPIENT_MASK;
+
+        //printk(KERN_INFO"%s:\n", __FUNCTION__);
+        /* verify that this is a class request
+         */
+        RETURN_EINVAL_UNLESS((request->bmRequestType & USB_REQ_TYPE_MASK) == USB_REQ_TYPE_CLASS);
+
+        //printk(KERN_INFO"%s: valid direction: %x recipient: %x bRequest: %02x\n", 
+        //                __FUNCTION__, direction, recipient, bRequest);
+
+        if (USB_REQ_HOST2DEVICE == direction) {
+
+                //printk(KERN_INFO"%s: H2D\n", __FUNCTION__);
+
+                /* these do not require a reply
+                 */
+                int setclear_flag = USB_REQ_SET_FEATURE == bRequest;
+                switch (recipient) {
+                case USB_RECIP_HUB:
+                        switch (request->bRequest) {
+                        case USB_REQ_CLEAR_FEATURE:
+                        case USB_REQ_SET_FEATURE:
+                                bus->driver->bops->hub_feature(bus, wValue, setclear_flag);
+                        }
+                        return 0;
+                case USB_RECIP_PORT:
+                        switch (request->bRequest) {
+                        case USB_REQ_CLEAR_FEATURE:
+                        case USB_REQ_SET_FEATURE:
+                                bus->driver->bops->port_feature(bus, wValue, wIndex, setclear_flag);
+#if 0
+                        case USB_REQ_CLEAR_TT_BUFFER:
+                        case USB_REQ_RESET_TT:
+                        case USB_REQ_STOP_TT:
+#endif
+                        }
+                        return 0;
+                }
+        }
+        else {
+                //printk(KERN_INFO"%s: D2H\n", __FUNCTION__);
+
+                /* these do require a reply
+                 */
+                struct usbd_urb *urb;
+                int rc = 0;
+                u32 status;
+
+                RETURN_EINVAL_UNLESS(wLength);
+                RETURN_EINVAL_UNLESS((urb = usbd_alloc_urb_ep0(function, wLength, NULL)));
+
+                switch (recipient) {
+                case USB_RECIP_HUB:
+                        //printk(KERN_INFO"%s: HUB\n", __FUNCTION__);
+                        switch (request->bRequest) {
+                        case USB_REQ_GET_DESCRIPTOR:
+                                BREAK_UNLESS (copy_report(urb, (void *)&rh_hub_descriptor, sizeof(rh_hub_descriptor), wLength));
+                                rc = -EINVAL;
+                                break;
+
+                        case USB_REQ_GET_STATUS:
+                                status = cpu_to_le16(bus->driver->bops->hub_status(bus));
+                                urb->actual_length = 4;
+                                memcpy(urb->buffer, &status, 4);
+                                break;
+                        }
+                        break;
+                case USB_RECIP_PORT:
+                        //printk(KERN_INFO"%s: PORT\n", __FUNCTION__);
+                        switch (request->bRequest) {
+                        case USB_REQ_GET_STATUS:
+                                status = cpu_to_le16(bus->driver->bops->port_status(bus, wIndex));
+                                urb->actual_length = 4;
+                                memcpy(urb->buffer, &status, 4);
+                                break;
+#if 0
+                        case USB_REQ_GET_TT_STATE:
+#endif
+                        }
+                        break;
+                default:
+                        rc = -EINVAL; 
+                }
+                UNLESS (rc)
+                        RETURN_ZERO_IF(!usbd_send_urb(urb));
+                /* error */
+                usbd_dealloc_urb(urb);
+        }
+        return -EINVAL;
+}
+
+
+static int rh_function_enable (struct usbd_function_instance *function)
+{
+        struct rh_private *rh = &rh_private;
+        struct usbd_bus_instance *bus = function->bus;
+        int ports;
+
+        printk (KERN_INFO "%s:\n", __FUNCTION__);
+
+        if (rh->port_features)
+                lkfree(rh->port_features);
+
+        rh->ports = ports = bus->driver->ports;
+        rh->port_features = ckmalloc(4 * ports, GFP_KERNEL);
+        rh_private.bh.data = &rh_private;
+        
+        rh_hub_descriptor.bNbrPorts = ports;
+
+        printk (KERN_INFO "%s: ports: %d\n", __FUNCTION__, ports);
+
+        //schedule_task(&rh_private.bh);
+
+        printk (KERN_INFO "%s: finished\n", __FUNCTION__);
+
+        return 0;
+}
+
+static void rh_function_disable (struct usbd_function_instance *function)
+{               
+        struct rh_private *rh = &rh_private;
+        printk (KERN_INFO "%s:\n", __FUNCTION__);
+        if (rh->port_features)
+                lkfree(rh->port_features);
+        printk (KERN_INFO "%s: finished\n", __FUNCTION__);
+}
+
+#if defined(OTG_WINCE)
+
+struct otg_instance otg_instance_private;
+
+void hub_ops_init(void)
+{
+        ZERO(otg_instance_private);
+        otg_instance_private.hcd = &hcd_instance_privatej
+}
+
+#else /* defined(OTG_WINCE) */
+
+static struct usbd_function_operations function_ops = {
+        event_irq: rh_event_irq,
+        recv_setup_irq: rh_recv_setup_irq,
+        function_enable: rh_function_enable,
+        function_disable: rh_function_disable,
+};
+
+static struct usbd_function_driver function_driver = {
+        name:"root hub",
+        fops:&function_ops,
+        device_description:&rh_device_description,
+        bNumConfigurations:sizeof (rh_description) / sizeof (struct usbd_configuration_description),
+        configuration_description:rh_description,
+        idVendor: __constant_cpu_to_le16(0x15ec),
+        idProduct: __constant_cpu_to_le16(0xf010),
+        bcdDevice: __constant_cpu_to_le16(0x000),
+};
+#endif /* defined(OTG_WINCE) */
+
+/* USB Module init/exit ************************************************************************ */
+#if defined(OTG_WINCE)
+
+
+#else /* defined(OTG_WINCE) */
+
+
+/*
+ * rh_modinit - module init
+ */
+int rh_init (void)
+{
+        printk (KERN_INFO "%s:\n", __FUNCTION__);
+
+        THROW_IF (usbd_register_function (&function_driver), error);
+
+        printk (KERN_INFO "%s: registered\n", __FUNCTION__);
+
+        rh_private.usb_driver_registered++;
+
+//        rh_private.bh.sync = 0;
+//        rh_private.bh.routine = rh_task;
+	PREPARE_WORK_ITEM(rh_private.bh,rh_task,NULL);
+
+        CATCH(error) {
+                if (rh_private.usb_driver_registered) {
+                        usbd_deregister_function (&function_driver);
+                        rh_private.usb_driver_registered = 0;
+                }
+                return -EINVAL;
+        }
+        printk (KERN_INFO "%s: finished\n", __FUNCTION__);
+        return 0;
+}
+
+/* rh_modexit - module cleanup
+ */
+void rh_exit (void)
+{
+        //while (rh_private.bh.sync) 
+        while (PENDING_WORK_ITEM(rh_private.bh))
+                schedule_timeout(HZ);
+
+        //rh_private.bh.data = 0;
+        SET_WORK_ARG(rh_private.bh,0);
+        while (PENDING_WORK_ITEM(rh_private.bh))
+                schedule_timeout(HZ);
+        
+        if (rh_private.usb_driver_registered) 
+                usbd_deregister_function (&function_driver);
+}
+#endif /* defined(OTG_WINCE) */
+#endif /* CONFIG_OTG_ROOT_HUB */
+
diff -uNr linux/drivers/no-otg/otgcore/otg-fw-df.c linux/drivers/otg/otgcore/otg-fw-df.c
--- linux/drivers/no-otg/otgcore/otg-fw-df.c	1970-01-01 01:00:00.000000000 +0100
+++ linux/drivers/otg/otgcore/otg-fw-df.c	2006-09-01 21:41:34.000000000 +0200
@@ -0,0 +1,121 @@
+
+/* Generated by otg-tests-c.awk
+ *
+ * Do not Edit, see otg-state.awk
+ */
+
+
+/*!
+* @file otg/otgcore/otg-fw-df.c
+* @brief OTG Firmware - Firmware for df
+*
+* This file defines the OTG State Machine tests.
+*
+*
+* @ingroup OTGFW
+*/
+
+/*!
+* @page OTGFW
+* @section OTGFW - otg-fw-df.c
+* This contains the input, output and timout definitions for the OTG state machine firmware
+*/
+
+
+#ifdef OTG_APPLICATION
+#define NULL 0
+#include "otg-fw.h"
+#include "otg-fw-df.h"
+#else	/* OTG_APPLICATION/ */
+#include <otg/otg-compat.h>
+#include <otg/usbp-chap9.h>
+#include <otg/usbp-func.h>
+#include <otg/usbp-bus.h>
+#include <otg/otg-trace.h>
+//#include <otg/otg-api.h>
+#include <otg/otg-fw.h>
+#include <otg/otg-fw-df.h>
+#endif	/* OTG_APPLICATION */
+
+char            otg_fw_name_df[] = "otg_df";
+
+
+struct otg_test otg_tests_df[] = {
+	/*
+	 * Copyright (c) 2004 Belcarra
+	 *
+	 */
+	/*!
+	 * This is the default Firmware. It is included in the
+	 * compiled modules and supports the auto Traditional USB
+	 * mode. No user inputs are implemented.
+	 */
+	{					 /*   */
+	 0,					 /* .test */
+	 invalid_state,				 /* .state */
+	 otg_disabled,				 /* .target */
+	 enable_otg,				 /* .test1 */
+	 },
+	/*
+	 * This is not an OTG State. It is used internally to mark the end of the
+	 * list of states and inputs.
+	 */
+	{					 /*   */
+	 1,					 /* .test */
+	 terminator_state,			 /* .state */
+	 invalid_state,				 /* .target */
+	 0,					 /* .test1 */
+	 },
+	{2, invalid_state,},
+
+};
+
+#define OTG_TESTS_DF 2
+
+int             otg_test_max_df = 2;
+
+ /* eof */
+
+/* Generated by otg-info-c.awk
+ *
+ * Do not Edit, see otg-state.awk
+ */
+
+struct otg_state otg_states_df[OTG_STATES_DF + 1] = {
+	{					 /* 0 */
+	 invalid_state,				 /* .state */
+	 m_otg_init,				 /* .meta */
+	 "invalid_state",			 /* .name */
+	 0,					 /* .tmout */
+	 0,					 /* .reset */
+	 },
+	{					 /* 1 */
+	 otg_disabled,				 /* .state */
+	 m_otg_init,				 /* .meta */
+	 "otg_disabled",			 /* .name */
+	 0,					 /* .tmout */
+	 0,					 /* .reset */
+	 },
+	{					 /* 2 */
+	 terminator_state,			 /* .state */
+	 m_otg_init,				 /* .meta */
+	 "terminator_state",			 /* .name */
+	 0,					 /* .tmout */
+	 0,					 /* .reset */
+	 /* .outputs */
+	 0,
+	 },
+
+	{0, 0, "", 0, 0,},
+
+};
+
+struct otg_firmware otg_firmware_df = {
+	OTG_STATES_DF,				 /* number of states */
+	OTG_TESTS_DF,				 /* number of tests */
+	"otg-df",				 /* name of firmware */
+	otg_states_df,				 /* struct otg_state * */
+	otg_tests_df,				 /* struct otg_test * */
+};
+
+/* eof */
diff -uNr linux/drivers/no-otg/otgcore/otg-fw-mn.c linux/drivers/otg/otgcore/otg-fw-mn.c
--- linux/drivers/no-otg/otgcore/otg-fw-mn.c	1970-01-01 01:00:00.000000000 +0100
+++ linux/drivers/otg/otgcore/otg-fw-mn.c	2006-09-01 21:41:34.000000000 +0200
@@ -0,0 +1,511 @@
+
+/* Generated by otg-tests-c.awk
+ *
+ * Do not Edit, see otg-state.awk
+ */
+
+
+/*!
+* @file otg/otgcore/otg-fw-mn.c
+* @brief OTG Firmware - Firmware for mn
+*
+* This file defines the OTG State Machine tests.
+*
+*
+* @ingroup OTGFW
+*/
+
+/*!
+* @page OTGFW
+* @section OTGFW - otg-fw-mn.c
+* This contains the input, output and timout definitions for the OTG state machine firmware
+*/
+
+
+#ifdef OTG_APPLICATION
+#define NULL 0
+#include "otg-fw.h"
+#include "otg-fw-mn.h"
+#else	/* OTG_APPLICATION/ */
+#include <otg/otg-compat.h>
+#include <otg/usbp-chap9.h>
+#include <otg/usbp-func.h>
+#include <otg/usbp-bus.h>
+#include <otg/otg-trace.h>
+//#include <otg/otg-api.h>
+#include <otg/otg-fw.h>
+#include <otg/otg-fw-mn.h>
+#endif	/* OTG_APPLICATION */
+
+char            otg_fw_name_mn[] = "otg_mn";
+
+
+struct otg_test otg_tests_mn[] = {
+	/*
+	 * Copyright (c) 2004 Belcarra
+	 */
+	/*!
+	 * This is the initialization set for pcd and tcd.
+	 */
+	/*
+	 * This the initial state of the software when first loaded.
+	 * It is not possible to return to this state.
+	 */
+	{					 /*   Initialize by sending the otg_enable signal. */
+	 0,					 /* .test */
+	 invalid_state,				 /* .state */
+	 otg_disabled,				 /* .target */
+	 enable_otg,				 /* .test1 */
+	 },
+	/*
+	 * The USBOTG State Machine has been initialized but is inactive.
+	 * This state may have arrived at from either the invalid_state or
+	 * from the otg_disable state.
+	 */
+	{					 /*   Initialize by sending the otg_enable signal. */
+	 1,					 /* .test */
+	 otg_disabled,				 /* .state */
+	 otg_enable_pcd,			 /* .target */
+	 enable_otg,				 /* .test1 */
+	 },
+	/*
+	 * The State Machine stops the device drivers and waits for them
+	 * to signal that they have finished de-initializing.
+	 *
+	 * This state disables the TCD driver and waits for TCD ok.
+	 */
+	{					 /*   Wait for ok from de-initializing drivers. */
+	 2,					 /* .test */
+	 otg_disable_tcd,			 /* .state */
+	 otg_disable_pcd,			 /* .target */
+	 TCD_OK,				 /* .test1 */
+	 },
+	/*
+	 * The State Machine stops the device drivers and waits for them
+	 * to signal that they have finished de-initializing.
+	 *
+	 * This state disables the PCD driver and waits for PCD ok.
+	 */
+	{					 /*   Wait for ok from de-initializing drivers. */
+	 3,					 /* .test */
+	 otg_disable_pcd,			 /* .state */
+	 otg_disabled,				 /* .target */
+	 PCD_OK,				 /* .test1 */
+	 },
+	/*
+	 * The State Machine starts the device drivers and waits for them
+	 * to signal that they have finished initializing.
+	 *
+	 * This state enables the PCD driver and waits for PCD ok.
+	 */
+	{					 /*   Wait for ok from initializing drivers. */
+	 4,					 /* .test */
+	 otg_enable_pcd,			 /* .state */
+	 otg_enable_tcd,			 /* .target */
+	 PCD_OK,				 /* .test1 */
+	 },
+	/*
+	 * The State Machine starts the device drivers and waits for them
+	 * to signal that they have finished initializing.
+	 *
+	 * This state enables the PCD driver and waits for PCD ok.
+	 */
+	{					 /*   Wait for ok from initializing drivers. */
+	 5,					 /* .test */
+	 otg_enable_tcd,			 /* .state */
+	 otg_enabled,				 /* .target */
+	 TCD_OK,				 /* .test1 */
+	 },
+	/*
+	 * Copyright (c) 2004 Belcarra
+	 *
+	 */
+	/*!
+	 * This is the minimal firmware. It can be included in the
+	 * compiled modules and supports the auto Traditional USB
+	 * mode. No user inputs are required for normal operation.
+	 *
+	 * The b_bus_drop input can be optionally used to disconnect and re-connect.
+	 *
+	 * The enable_otg input can be optionally used to disable and re-enable.
+	 * Note that disable/enable will reset b_bus_drop.
+	 *
+	 */
+	/*!
+	 * The State Machine has successfully started the device drivers and
+	 * is waiting for an input event. Typically it will move from here to
+	 * an idle state specific to the current conditions (a_idle, b_idle, mn_idle etc.)
+	 * based on user request b_bus_drop.
+	 *
+	 */
+	{					 /*   Check for disable. */
+	 6,					 /* .test */
+	 otg_enabled,				 /* .state */
+	 otg_disable_tcd,			 /* .target */
+	 enable_otg_,				 /* .test1 */
+	 },
+	{					 /*   Move to idle. */
+	 7,					 /* .test */
+	 otg_enabled,				 /* .state */
+	 mn_idle,				 /* .target */
+	 AUTO | AUTO_,				 /* .test1 */
+	 },
+	/*!
+	 * The State Machine is in the idle state for a Traditional USB Device.
+	 * It is waiting for Vbus to indicate that it has been plugged into a USB Host.
+	 *
+	 */
+	{					 /*   Check for disable (must be done for check for bus_drop.) */
+	 8,					 /* .test */
+	 mn_idle,				 /* .state */
+	 otg_disable_tcd,			 /* .target */
+	 enable_otg_,				 /* .test1 */
+	 },
+	{					 /*   Check for b_bus_drop */
+	 9,					 /* .test */
+	 mn_idle,				 /* .state */
+	 mn_bus_drop,				 /* .target */
+	 b_bus_drop,				 /* .test1 */
+	 },
+	{					 /*   move to peripheral mode when SESSION valid. */
+	 10,					 /* .test */
+	 mn_idle,				 /* .state */
+	 mn_peripheral,				 /* .target */
+	 b_bus_drop_,				 /* .test1 */
+	 B_SESS_VLD,				 /* .test2 */
+	 },
+	/*!
+	 * The State Machine is in the idle state for a Traditional USB Device.
+	 * The B-Device applications has requested that the bus connection be dropped.
+	 * Wait for either enable_otg reset or b_bus_drop reset.
+	 */
+	{					 /*   Wait for b_bus_drop/ or enable_otg/ */
+	 11,					 /* .test */
+	 mn_bus_drop,				 /* .state */
+	 mn_idle,				 /* .target */
+	 b_bus_drop_ | enable_otg_,		 /* .test1 */
+	 },
+	/*!
+	 * The State Machine in the peripheral state for a Traditional USB Device.
+	 * The D+ pullup is enabled and we are waiting for a BUS_RESET to
+	 * indicate that the USB Host has recognized that a USB Device is attached.
+	 */
+	{					 /*   Move to idle if we loose any of these inputs. */
+	 12,					 /* .test */
+	 mn_peripheral,				 /* .state */
+	 mn_idle,				 /* .target */
+	 enable_otg_ | B_SESS_VLD_ | b_bus_drop, /* .test1 */
+	 },
+	{					 /*   Move to next state if bus reset is seen. */
+	 13,					 /* .test */
+	 mn_peripheral,				 /* .state */
+	 mn_bus_reset,				 /* .target */
+	 BUS_RESET,				 /* .test1 */
+	 },
+	/*!
+	 * The State Machine in the bus_reset state for a Traditional USB Device.
+	 * It is waiting to be enumerated and configured by the USB Host.
+	 */
+	{					 /*   Move to idle via discharge, if we loose any of these inputs. */
+	 14,					 /* .test */
+	 mn_bus_reset,				 /* .state */
+	 mn_discharge,				 /* .target */
+	 enable_otg_ | B_SESS_VLD_ | b_bus_drop, /* .test1 */
+	 },
+	{					 /*   Progress if we are addressed. */
+	 15,					 /* .test */
+	 mn_bus_reset,				 /* .state */
+	 mn_addressed,				 /* .target */
+	 ADDRESSED,				 /* .test1 */
+	 },
+	/*!
+	 * The State Machine in the configured state for a Traditional USB Device.
+	 * This means that there is an active session, there is packet traffic
+	 * with this device.
+	 */
+	{					 /*   Move to idle via discharge, if we loose any of these inputs. */
+	 16,					 /* .test */
+	 mn_addressed,				 /* .state */
+	 mn_discharge,				 /* .target */
+	 enable_otg_ | B_SESS_VLD_ | b_bus_drop, /* .test1 */
+	 },
+	{					 /*   Progress if we are configured. */
+	 17,					 /* .test */
+	 mn_addressed,				 /* .state */
+	 mn_configured,				 /* .target */
+	 CONFIGURED,				 /* .test1 */
+	 },
+	/*!
+	 * The State Machine in the configured state for a Traditional USB Device.
+	 * This means that there is an active session, there is packet traffic
+	 * with this device.
+	 */
+	{					 /*   */
+	 18,					 /* .test */
+	 mn_configured,				 /* .state */
+	 mn_suspended,				 /* .target */
+	 BUS_SUSPENDED,				 /* .test1 */
+	 },
+	{					 /*   Move to idle via discharge, if we loose any of these inputs. */
+	 19,					 /* .test */
+	 mn_configured,				 /* .state */
+	 mn_discharge,				 /* .target */
+	 enable_otg_ | B_SESS_VLD_ | b_bus_drop, /* .test1 */
+	 },
+	/*!
+	 * The State Machine in the discharge state for a Traditional USB Device.
+	 * The device has been unplugged. The Vbus discharge resistor will be enabled
+	 * for the TLDISC_DSCHRG time period.
+	 */
+	{					 /*   Progress to idle on timeout. */
+	 20,					 /* .test */
+	 mn_discharge,				 /* .state */
+	 mn_idle,				 /* .target */
+	 Tldisc_dschrg,				 /* .test1 */
+	 },
+	/*!
+	 * The State Machine in the suspend state for a Traditional USB Device.
+	 */
+	{					 /*   Move to idle via discharge, if we loose any of these inputs. */
+	 21,					 /* .test */
+	 mn_suspended,				 /* .state */
+	 mn_discharge,				 /* .target */
+	 enable_otg_ | B_SESS_VLD_ | b_bus_drop, /* .test1 */
+	 },
+	{					 /*   Check for a resumed bus. */
+	 22,					 /* .test */
+	 mn_suspended,				 /* .state */
+	 mn_configured,				 /* .target */
+	 BUS_SUSPENDED_,			 /* .test1 */
+	 },
+	{					 /*   Is remote wakeup enabled? */
+	 23,					 /* .test */
+	 mn_suspended,				 /* .state */
+	 mn_wakeup_enabled,			 /* .target */
+	 REMOTE_WAKEUP_ENABLED,			 /* .test1 */
+	 },
+	/*!
+	 * The State Machine in the suspend state for a Traditional USB Device,
+	 * prior to suspended the USB Host enabled Remote Wakeup by sending a
+	 * set REMOTE WAKUP request.
+	 */
+	{					 /*   Move to idle via discharge, if we loose any of these inputs. */
+	 24,					 /* .test */
+	 mn_wakeup_enabled,			 /* .state */
+	 mn_discharge,				 /* .target */
+	 enable_otg_ | b_bus_req_ | B_SESS_VLD_, /* .test1 */
+	 },
+	{					 /*   Check for a resumed bus. */
+	 25,					 /* .test */
+	 mn_wakeup_enabled,			 /* .state */
+	 mn_suspended,				 /* .target */
+	 BUS_SUSPENDED_,			 /* .test1 */
+	 },
+	{					 /*   Remote wakeup requested? */
+	 26,					 /* .test */
+	 mn_wakeup_enabled,			 /* .state */
+	 mn_wakeup,				 /* .target */
+	 remote_wakeup_cmd,			 /* .test1 */
+	 },
+	/*!
+	 * The State Machine in the wakeup state for a Traditional USB Device,
+	 * The REMOTE WAKEUP procedure will be performed.
+	 */
+	{					 /*   Automatic return. */
+	 27,					 /* .test */
+	 mn_wakeup,				 /* .state */
+	 mn_wakeup_enabled,			 /* .target */
+	 AUTO | AUTO_,				 /* .test1 */
+	 },
+	/*!
+	 * This is not an OTG State. It is used internally to mark the end of the
+	 * list of states and inputs.
+	 */
+	{					 /*   */
+	 28,					 /* .test */
+	 terminator_state,			 /* .state */
+	 invalid_state,				 /* .target */
+	 0,					 /* .test1 */
+	 },
+	{29, invalid_state,},
+
+};
+
+#define OTG_TESTS_MN 29
+
+int             otg_test_max_mn = 29;
+
+ /* eof */
+
+/* Generated by otg-info-c.awk
+ *
+ * Do not Edit, see otg-state.awk
+ */
+
+struct otg_state otg_states_mn[OTG_STATES_MN + 1] = {
+	{					 /* 0 */
+	 invalid_state,				 /* .state */
+	 m_otg_init,				 /* .meta */
+	 "invalid_state",			 /* .name */
+	 0,					 /* .tmout */
+	 0,					 /* .reset */
+	 },
+	{					 /* 1 */
+	 otg_disabled,				 /* .state */
+	 m_otg_init,				 /* .meta */
+	 "otg_disabled",			 /* .name */
+	 0,					 /* .tmout */
+	 enable_otg_,				 /* .reset */
+	 /* .outputs */
+	 chrg_vbus_out_ | drv_vbus_out_ | dp_pullup_out_ | loc_sof_out_,
+	 },
+	{					 /* 2 */
+	 otg_disable_tcd,			 /* .state */
+	 m_otg_init,				 /* .meta */
+	 "otg_disable_tcd",			 /* .name */
+	 0,					 /* .tmout */
+	 TCD_OK_,				 /* .reset */
+	 /* .outputs */
+	 tcd_init_out_,
+	 },
+	{					 /* 3 */
+	 otg_disable_pcd,			 /* .state */
+	 m_otg_init,				 /* .meta */
+	 "otg_disable_pcd",			 /* .name */
+	 0,					 /* .tmout */
+	 PCD_OK_,				 /* .reset */
+	 /* .outputs */
+	 pcd_init_out_,
+	 },
+	{					 /* 4 */
+	 otg_enable_pcd,			 /* .state */
+	 m_otg_init,				 /* .meta */
+	 "otg_enable_pcd",			 /* .name */
+	 0,					 /* .tmout */
+	 PCD_OK_,				 /* .reset */
+	 /* .outputs */
+	 chrg_vbus_out_ | drv_vbus_out_ | dp_pullup_out_ | loc_sof_out_ | pcd_init_out,
+	 },
+	{					 /* 5 */
+	 otg_enable_tcd,			 /* .state */
+	 m_otg_init,				 /* .meta */
+	 "otg_enable_tcd",			 /* .name */
+	 0,					 /* .tmout */
+	 TCD_OK_,				 /* .reset */
+	 /* .outputs */
+	 chrg_vbus_out_ | drv_vbus_out_ | dp_pullup_out_ | loc_sof_out_ | tcd_init_out,
+	 },
+	{					 /* 6 */
+	 otg_enabled,				 /* .state */
+	 m_otg_init,				 /* .meta */
+	 "otg_enabled",				 /* .name */
+	 0,					 /* .tmout */
+	 b_bus_drop_,				 /* .reset */
+	 },
+	{					 /* 7 */
+	 mn_idle,				 /* .state */
+	 m_b_idle,				 /* .meta */
+	 "mn_idle",				 /* .name */
+	 0,					 /* .tmout */
+	 0,					 /* .reset */
+	 /* .outputs */
+	 dp_pullup_out_ | pcd_en_out_ | tcd_en_out_power | dischrg_vbus_out_,
+	 },
+	{					 /* 8 */
+	 mn_bus_drop,				 /* .state */
+	 m_b_idle,				 /* .meta */
+	 "mn_bus_drop",				 /* .name */
+	 0,					 /* .tmout */
+	 0,					 /* .reset */
+	 },
+	{					 /* 9 */
+	 mn_peripheral,				 /* .state */
+	 m_b_peripheral,			 /* .meta */
+	 "mn_peripheral",			 /* .name */
+	 0,					 /* .tmout */
+	 BUS_RESET_,				 /* .reset */
+	 /* .outputs */
+	 dp_pullup_out | pcd_en_out | dischrg_vbus_out_,
+	 },
+	{					 /* 10 */
+	 mn_bus_reset,				 /* .state */
+	 m_b_peripheral,			 /* .meta */
+	 "mn_bus_reset",			 /* .name */
+	 0,					 /* .tmout */
+	 BUS_RESET_ | ADDRESSED_,		 /* .reset */
+	 },
+	{					 /* 11 */
+	 mn_addressed,				 /* .state */
+	 m_b_peripheral,			 /* .meta */
+	 "mn_addressed",			 /* .name */
+	 0,					 /* .tmout */
+	 ADDRESSED_ | CONFIGURED_,		 /* .reset */
+	 /* .outputs */
+	 dp_pullup_out | pcd_en_out | dischrg_vbus_out_,
+	 },
+	{					 /* 12 */
+	 mn_configured,				 /* .state */
+	 m_b_peripheral,			 /* .meta */
+	 "mn_configured",			 /* .name */
+	 0,					 /* .tmout */
+	 CONFIGURED_ | BUS_SUSPENDED_,		 /* .reset */
+	 /* .outputs */
+	 dp_pullup_out | pcd_en_out | dischrg_vbus_out_,
+	 },
+	{					 /* 13 */
+	 mn_discharge,				 /* .state */
+	 m_b_peripheral,			 /* .meta */
+	 "mn_discharge",			 /* .name */
+	 TLDISC_DSCHRG,				 /* .tmout */
+	 0,					 /* .reset */
+	 /* .outputs */
+	 dischrg_vbus_out | pcd_en_out_,
+	 },
+	{					 /* 14 */
+	 mn_suspended,				 /* .state */
+	 m_b_suspended,				 /* .meta */
+	 "mn_suspended",			 /* .name */
+	 0,					 /* .tmout */
+	 0,					 /* .reset */
+	 },
+	{					 /* 15 */
+	 mn_wakeup_enabled,			 /* .state */
+	 m_b_suspended,				 /* .meta */
+	 "mn_wakeup_enabled",			 /* .name */
+	 0,					 /* .tmout */
+	 remote_wakeup_cmd_,			 /* .reset */
+	 /* .outputs */
+	 remote_wakeup_out_,
+	 },
+	{					 /* 16 */
+	 mn_wakeup,				 /* .state */
+	 m_b_suspended,				 /* .meta */
+	 "mn_wakeup",				 /* .name */
+	 0,					 /* .tmout */
+	 0,					 /* .reset */
+	 /* .outputs */
+	 remote_wakeup_out,
+	 },
+	{					 /* 17 */
+	 terminator_state,			 /* .state */
+	 m_otg_init,				 /* .meta */
+	 "terminator_state",			 /* .name */
+	 0,					 /* .tmout */
+	 0,					 /* .reset */
+	 /* .outputs */
+	 0,
+	 },
+
+	{0, 0, "", 0, 0,},
+
+};
+
+struct otg_firmware otg_firmware_mn = {
+	OTG_STATES_MN,				 /* number of states */
+	OTG_TESTS_MN,				 /* number of tests */
+	"otg-mn",				 /* name of firmware */
+	otg_states_mn,				 /* struct otg_state * */
+	otg_tests_mn,				 /* struct otg_test * */
+};
+
+/* eof */
diff -uNr linux/drivers/no-otg/otgcore/otg-fw.c linux/drivers/otg/otgcore/otg-fw.c
--- linux/drivers/no-otg/otgcore/otg-fw.c	1970-01-01 01:00:00.000000000 +0100
+++ linux/drivers/otg/otgcore/otg-fw.c	2006-09-01 21:41:34.000000000 +0200
@@ -0,0 +1,251 @@
+
+/* Generated by otg-fw-c.awk
+ *
+ * Do not Edit, see otg-state.awk
+ */
+
+/*!
+* @file otg/otgcore/otg-fw.c
+* @brief OTG Firmware - Input, Output and Timeout definitions
+*
+* This file defines the OTG State Machine input, output and timeout constants.
+*
+*
+* @ingroup OTGFW
+*/
+
+/*!
+* @page OTGFW
+* @section OTGFW - otg-fw.c
+* This contains the input, output and timout definitions for the OTG state machine firmware
+*/
+
+
+#ifdef OTG_APPLICATION
+#include "otg-fw.h"
+#else	/* OTG_APPLICATION/ */
+#include <otg/otg-compat.h>
+#include <otg/usbp-chap9.h>
+#include <otg/usbp-func.h>
+#include <otg/usbp-bus.h>
+#include <otg/otg-trace.h>
+#include <otg/otg-api.h>
+#endif	/* OTG_APPLICATION */
+
+#define OTG_VERSION_FWC 200502152159L
+
+
+#if OTG_VERSION_FWC != OTG_VERSION_FW
+#error OTG_VERSION_FWC != OTG_VERSION_FW
+#endif	/* OTG_VERSION_FWC != OTG_VERSION_FW */
+
+/* Generated by otg-input-name-c.awk
+ *
+ * Do not Edit, see otg-state.awk
+ */
+
+
+/* #include "otg-fw.h" */
+
+#define OTG_VERSION_INPUT_TABLE 200502152159L
+
+
+#if OTG_VERSION_INPUT_TABLE != OTG_VERSION_FW
+#error OTG_VERSION_INPUT_TABLE != OTG_VERSION_FW
+#endif	/* OTG_VERSION_INPUT_TABLE != OTG_VERSION_FW */
+
+
+struct otg_input_name otg_input_names[] = {
+	{ID_GND, "ID_GND",},
+	{ID_FLOAT, "ID_FLOAT",},
+	{DP_HIGH, "DP_HIGH",},
+	{DM_HIGH, "DM_HIGH",},
+	{B_SESS_END, "B_SESS_END",},
+	{A_SESS_VLD, "A_SESS_VLD",},
+	{B_SESS_VLD, "B_SESS_VLD",},
+	{VBUS_VLD, "VBUS_VLD",},
+	{SRP_DET, "SRP_DET",},
+	{SE0_DET, "SE0_DET",},
+	{SE1_DET, "SE1_DET",},
+	{BDIS_ACON, "BDIS_ACON",},
+	{CR_INT_DET, "CR_INT_DET",},
+	{HUB_PORT_CONNECT, "HUB_PORT_CONNECT",},
+	{BUS_RESET, "BUS_RESET",},
+	{ADDRESSED, "ADDRESSED",},
+	{CONFIGURED, "CONFIGURED",},
+	{NOT_SUPPORTED, "NOT_SUPPORTED",},
+	{BUS_SUSPENDED, "BUS_SUSPENDED",},
+	{a_bcon_no_tmout_req, "a_bcon_no_tmout_req",},
+	{a_hpwr_req, "a_hpwr_req",},
+	{bus_drop, "bus_drop",},
+	{a_bus_drop, "a_bus_drop",},
+	{b_bus_drop, "b_bus_drop",},
+	{bus_req, "bus_req",},
+	{a_bus_req, "a_bus_req",},
+	{b_bus_req, "b_bus_req",},
+	{b_sess_req, "b_sess_req",},
+	{suspend_req, "suspend_req",},
+	{a_suspend_req, "a_suspend_req",},
+	{b_suspend_req, "b_suspend_req",},
+	{set_remote_wakeup_cmd, "set_remote_wakeup_cmd",},
+	{remote_wakeup_cmd, "remote_wakeup_cmd",},
+	{reset_remote_wakeup_cmd, "reset_remote_wakeup_cmd",},
+	{clr_err_cmd, "clr_err_cmd",},
+	{b_hnp_cmd, "b_hnp_cmd",},
+	{ph_int_cmd, "ph_int_cmd",},
+	{ph_audio_cmd, "ph_audio_cmd",},
+	{cr_int_cmd, "cr_int_cmd",},
+	{led_on_cmd, "led_on_cmd",},
+	{led_off_cmd, "led_off_cmd",},
+	{HNP_ENABLED, "HNP_ENABLED",},
+	{HNP_CAPABLE, "HNP_CAPABLE",},
+	{HNP_SUPPORTED, "HNP_SUPPORTED",},
+	{REMOTE_WAKEUP_ENABLED, "REMOTE_WAKEUP_ENABLED",},
+	{REMOTE_CAPABLE, "REMOTE_CAPABLE",},
+	{PCD_OK, "PCD_OK",},
+	{TCD_OK, "TCD_OK",},
+	{HCD_OK, "HCD_OK",},
+	{OCD_OK, "OCD_OK",},
+	{TMOUT, "TMOUT",},
+	{enable_otg, "enable_otg",},
+	{AUTO, "AUTO",},
+	{0, "0",},
+
+};
+
+ /* eof */
+
+/* Generated by otg-ioctls-names-c.awk
+ *
+ * Do not Edit, see otg-state.awk
+ */
+#if defined(OTG_LINUX) || defined(OTG_WINCE)
+
+
+/* #include "otg-fw.h" */
+
+#define OTG_VERSION_IOCTL_TABLE 200502152159L
+
+
+#if OTG_VERSION_IOCTL_TABLE != OTG_VERSION_FW
+#error OTG_VERSION_IOCTL_TABLE != OTG_VERSION_FW
+#endif	/* OTG_VERSION_IOCTL_TABLE != OTG_VERSION_FW */
+
+struct otg_ioctl_name otg_ioctl_names[] = {
+	{OTGADMIN_A_BCON_NO_TMOUT_REQ, a_bcon_no_tmout_req, "OTGADMIN_A_BCON_NO_TMOUT_REQ",},
+	{OTGADMIN_A_HPWR_REQ, a_hpwr_req, "OTGADMIN_A_HPWR_REQ",},
+	{OTGADMIN_BUS_DROP, bus_drop, "OTGADMIN_BUS_DROP",},
+	{OTGADMIN_A_BUS_DROP, a_bus_drop, "OTGADMIN_A_BUS_DROP",},
+	{OTGADMIN_B_BUS_DROP, b_bus_drop, "OTGADMIN_B_BUS_DROP",},
+	{OTGADMIN_BUS_REQ, bus_req, "OTGADMIN_BUS_REQ",},
+	{OTGADMIN_A_BUS_REQ, a_bus_req, "OTGADMIN_A_BUS_REQ",},
+	{OTGADMIN_B_BUS_REQ, b_bus_req, "OTGADMIN_B_BUS_REQ",},
+	{OTGADMIN_B_SESS_REQ, b_sess_req, "OTGADMIN_B_SESS_REQ",},
+	{OTGADMIN_SUSPEND_REQ, suspend_req, "OTGADMIN_SUSPEND_REQ",},
+	{OTGADMIN_A_SUSPEND_REQ, a_suspend_req, "OTGADMIN_A_SUSPEND_REQ",},
+	{OTGADMIN_B_SUSPEND_REQ, b_suspend_req, "OTGADMIN_B_SUSPEND_REQ",},
+	{OTGADMIN_SET_REMOTE_WAKEUP_CMD, set_remote_wakeup_cmd, "OTGADMIN_SET_REMOTE_WAKEUP_CMD",},
+	{OTGADMIN_REMOTE_WAKEUP_CMD, remote_wakeup_cmd, "OTGADMIN_REMOTE_WAKEUP_CMD",},
+	{OTGADMIN_RESET_REMOTE_WAKEUP_CMD, reset_remote_wakeup_cmd, "OTGADMIN_RESET_REMOTE_WAKEUP_CMD",},
+	{OTGADMIN_CLR_ERR_CMD, clr_err_cmd, "OTGADMIN_CLR_ERR_CMD",},
+	{OTGADMIN_B_HNP_CMD, b_hnp_cmd, "OTGADMIN_B_HNP_CMD",},
+	{OTGADMIN_PH_INT_CMD, ph_int_cmd, "OTGADMIN_PH_INT_CMD",},
+	{OTGADMIN_PH_AUDIO_CMD, ph_audio_cmd, "OTGADMIN_PH_AUDIO_CMD",},
+	{OTGADMIN_CR_INT_CMD, cr_int_cmd, "OTGADMIN_CR_INT_CMD",},
+	{OTGADMIN_LED_ON_CMD, led_on_cmd, "OTGADMIN_LED_ON_CMD",},
+	{OTGADMIN_LED_OFF_CMD, led_off_cmd, "OTGADMIN_LED_OFF_CMD",},
+	{OTGADMIN_ENABLE_OTG, enable_otg, "OTGADMIN_ENABLE_OTG",},
+	{0, 0, "",},
+
+};
+
+ /* eof */
+#endif				/* defined(OTG_LINUX) || defined(OTG_WINCE) */
+
+/* Generated by otg-output-names-c.awk
+ *
+ * Do not Edit, see otg-state.awk
+ */
+
+char           *otg_output_names[] = {
+	"tcd_init_out",				 /* 0   Initiate Transceiver Controller Driver Initialization (or De-initialization.) */
+	"pcd_init_out",				 /* 1   Initiate Peripheral Controller Driver Initialization (or De-initialization.) */
+	"hcd_init_out",				 /* 2   Initiate Host Controller Driver Initialization (or De-initialization). */
+	"ocd_init_out",				 /* 3   Initiate OTG Controller Driver Initialization (or De-initialization). */
+	"tcd_en_out",				 /* 4   Enable Transceiver Controller Driver */
+	"pcd_en_out",				 /* 5   Enable Peripheral Controller Driver */
+	"hcd_en_out",				 /* 6   Enable Host Controller Driver */
+	"drv_vbus_out",				 /* 7   A-Device will Drive Vbus to 5V through charge pump. */
+	"chrg_vbus_out",			 /* 8   B-Device will charge Vbus to 3.3V through resistor (SRP.) */
+	"dischrg_vbus_out",			 /* 9   B-Device will discharge Vbus (enable dischage resistor.) */
+	"dm_pullup_out",			 /* 10   DM pullup control - aka loc_carkit */
+	"dm_pulldown_out",			 /* 11   DM pulldown control */
+	"dp_pullup_out",			 /* 12   DP pullup control - aka loc_conn */
+	"dp_pulldown_out",			 /* 13   DP pulldown control */
+	"clr_overcurrent_out",			 /* 14   Clear overcurrent indication */
+	"dm_det_out",				 /* 15   Enable B-Device D- High detect */
+	"dp_det_out",				 /* 16   Enable B-Device D+ High detect */
+	"cr_det_out",				 /* 17   Enable D+ CR detect */
+	"charge_pump_out",			 /* 18   Enable external charge pump. */
+	"bdis_acon_out",			 /* 19   Enable auto A-connect after B-disconnect. */
+	"mx21_vbus_drain",			 /* 20   MX21 hack */
+	"id_pulldown_out",			 /* 21   Enable the ID to ground pulldown ( (CEA-936 - 5 wire carkit.) */
+	"uart_out",				 /* 22   Enable Transparent UART mode (CEA-936.) */
+	"audio_out",				 /* 23   Enable Audio mode (CEA-936 CarKit interrupt detector.) */
+	"mono_out",				 /* 24   Enable Mono-Audio mode (CEA-936.) */
+	"remote_wakeup_out",			 /* 25   Peripheral will perform remote wakeup. */
+	"loc_sof_out",				 /* 26   Host will enable packet traffic. */
+	"loc_suspend_out",			 /* 27   Host will suspend bus. */
+	"remote_wakeup_en_out",			 /* 28   Host will send remote wakeup enable or disable request. */
+	"hnp_en_out",				 /* 29   Host will send HNP enable request. */
+	"hpwr_out",				 /* 30   Host will enable high power (external charge pump.) */
+	NULL,
+
+};
+
+ /* eof */
+
+/* Generated by otg-meta-names-c.awk
+ *
+ * Do not Edit, see otg-state.awk
+ */
+
+
+
+char           *otg_meta_names[] = {
+	"a_idle",				 /*  0 -  */
+	"a_wait_vrise",				 /*  1 -  */
+	"a_wait_bcon",				 /*  2 -  */
+	"a_host",				 /*  3 -  */
+	"a_suspend",				 /*  4 -  */
+	"a_peripheral",				 /*  5 -  */
+	"a_wait_vfall",				 /*  6 -  */
+	"a_vbus_err",				 /*  7 -  */
+	"b_idle",				 /*  8 -  */
+	"b_srp_init",				 /*  9 -  */
+	"b_peripheral",				 /* 10 -  */
+	"b_suspend",				 /* 11 -  */
+	"b_wait_acon",				 /* 12 -  */
+	"b_host",				 /* 13 -  */
+	"b_suspended",				 /* 14 -  */
+	"ph_disc",				 /* 15 -  Equivalent to b_peripheral */
+	"ph_init",				 /* 16 -  */
+	"ph_uart",				 /* 17 -  */
+	"ph_aud",				 /* 18 -  */
+	"ph_wait",				 /* 19 -  */
+	"ph_exit",				 /* 20 -  */
+	"cr_init",				 /* 21 -  */
+	"cr_uart",				 /* 22 -  */
+	"cr_aud",				 /* 23 -  */
+	"cr_ack",				 /* 24 -  */
+	"cr_wait",				 /* 25 -  */
+	"cr_disc",				 /* 26 -  */
+	"otg_init",				 /* 27 -  */
+	"usb_accessory",			 /* 28 -  */
+	"usb_factory",				 /* 29 -  */
+	"unknown",				 /* 30 -  */
+
+	"",
+};
+
+/* eof */
diff -uNr linux/drivers/no-otg/otgcore/otg-mesg-l24.c linux/drivers/otg/otgcore/otg-mesg-l24.c
--- linux/drivers/no-otg/otgcore/otg-mesg-l24.c	1970-01-01 01:00:00.000000000 +0100
+++ linux/drivers/otg/otgcore/otg-mesg-l24.c	2006-09-01 21:41:34.000000000 +0200
@@ -0,0 +1,399 @@
+/*
+ * otg/otgcore/otg-mesg-l24.c - OTG state machine Administration
+ *
+ *      Copyright (c) 2004 Belcarra
+ *
+ * By: 
+ *      Stuart Lynne <sl@lbelcarra.com>, 
+ *      Tom Rushworth <tbr@belcarra.com>, 
+ *      Bruce Balden <balden@belcarra.com>
+ *
+ */
+/*!
+ * @file otg/otgcore/otg-mesg-l24.c
+ * @brief OTG Core Linux Message Interface.
+ *
+ * Implement linux char device via /proc/otg-mesg.
+ *
+ * @ingroup OTGCore
+ */
+
+#include <otg/otg-compat.h>
+#include <otg/otg-module.h>
+
+#include <otg/usbp-chap9.h>
+#include <otg/usbp-func.h>
+#include <otg/usbp-bus.h>
+#include <otg/otg-trace.h>
+#include <otg/otg-api.h>
+#include <otg/otg-tcd.h>
+#include <otg/otg-hcd.h>
+#include <otg/otg-pcd.h>
+#include <otg/otg-ocd.h>
+
+#include <linux/poll.h>
+#include <linux/sched.h>
+
+        
+wait_queue_head_t otg_message_queue;
+
+/*!
+ * otg_message() - queue message data
+ * @param buf message to send.
+ */
+void otg_message (char *buf)
+{
+        char time_buf[64];
+        static u64 save_ticks;
+        u64 new_ticks;
+        u64 ticks;
+
+        new_ticks = otg_tmr_ticks();
+
+        otg_write_message(buf, strlen(buf));
+
+        ticks = otg_tmr_elapsed(&new_ticks, &save_ticks); 
+        save_ticks = new_ticks;
+
+        if (ticks < 10000)
+                sprintf(time_buf, " %d uS\n", ticks);
+        else if (ticks < 10000000)
+                //sprintf(time_buf, " %d mS\n", ticks / 1000);
+                sprintf(time_buf, " %d mS\n", ticks >> 10);
+        else 
+                //sprintf(time_buf, " %d S\n", ticks / 1000000);
+                sprintf(time_buf, " %d S\n", ticks >> 20);
+
+        otg_write_message(time_buf, strlen(time_buf));
+
+
+        //TRACE_MSG0(CORE, "wakeup ");
+        //printk(KERN_INFO"%s: wakeup\n", __FUNCTION__);
+        wake_up_interruptible(&otg_message_queue);
+}
+
+/*!
+ * otg_message_block() - implement block
+ */
+unsigned int otg_message_block(void)
+{
+        int count = otg_data_queued();
+        if (count) return count;
+        //TRACE_MSG0(CORE, "sleeping ");
+        //printk(KERN_INFO"%s: sleeping\n", __FUNCTION__);
+        interruptible_sleep_on(&otg_message_queue);
+        return otg_data_queued();
+}
+
+/*!
+ * otg_message_poll() - implement poll
+ * @param filp file pointer
+ * @param wait poll table structure
+ */
+unsigned int otg_message_poll(struct file *filp, struct poll_table_struct *wait)
+{
+        //TRACE_MSG0(CORE, "polling ");
+        poll_wait(filp, &otg_message_queue, wait);
+        return otg_data_queued() ? POLLIN | POLLRDNORM : 0;
+}
+
+/*! 
+ * otg_message_ioctl_internal() - ioctl call
+ * @param cmd ioctl command.
+ * @param arg ioctl arguement.
+ * @return non-zero for error.
+ */
+int otg_message_ioctl_internal(unsigned int cmd, unsigned long arg)
+{
+        int i;
+        int len;
+        int flag;
+        struct otg_admin_command admin_command;
+        struct otg_status_update status_update;
+        struct otg_firmware_info firmware_info;
+        struct otg_state otg_state;
+        struct otg_test otg_test;
+        struct otg_ioctl_name *otg_ioctl_name;
+        static char func_buf[32];
+        char *sp, *dp;
+	char *name;
+
+        switch (cmd) {
+
+        case OTGADMIN_GET_FUNCTION:
+                //TRACE_MSG0(CORE, "OTGADMIN_GET_FUNCTION");
+                RETURN_EINVAL_UNLESS(otg_firmware_loaded);
+                RETURN_EINVAL_IF(copy_from_user(&admin_command, (void *)arg, _IOC_SIZE(cmd)));
+		name = otg_usbd_ops->function_name(admin_command.n);
+                admin_command.string[0] = '\0';
+		if (name)
+			strncat(admin_command.string, name, sizeof(admin_command.string));
+
+                RETURN_EINVAL_IF(copy_to_user((void *)arg, &admin_command, _IOC_SIZE(cmd)));
+                return 0;
+
+        case OTGADMIN_SET_FUNCTION:
+                //TRACE_MSG0(CORE, "OTGADMIN_SET_FUNCTION");
+                RETURN_EINVAL_UNLESS(otg_firmware_loaded);
+                memset(&admin_command, 0x41, sizeof(struct otg_admin_command));
+                RETURN_EINVAL_IF(copy_from_user(&admin_command, (void *)arg, _IOC_SIZE(cmd)));
+                len = sizeof(mesg_otg_instance->function_name);
+                admin_command.string[len] = '\0';
+                //TRACE_MSG1(CORE, "Setting function: \"%s\"", mesg_otg_instance->function_name);
+                strncpy(mesg_otg_instance->function_name, admin_command.string, len);
+                mesg_otg_instance->function_name[len-1] = '\0';
+                return 0;
+
+        case OTGADMIN_GET_SERIAL:
+                //TRACE_MSG0(CORE, "OTGADMIN_GET_SERIAL");
+                RETURN_EINVAL_UNLESS(otg_firmware_loaded);
+                strncpy(admin_command.string, mesg_otg_instance->serial_number, sizeof(admin_command.string));
+                admin_command.string[sizeof(admin_command.string) - 1] = '\0';
+                RETURN_EINVAL_IF(copy_to_user((void *)arg, &admin_command, _IOC_SIZE(cmd)));
+                return 0;
+
+        case OTGADMIN_SET_SERIAL:
+                //TRACE_MSG0(CORE, "OTGADMIN_SET_SERIAL");
+                RETURN_EINVAL_UNLESS(otg_firmware_loaded);
+                RETURN_EINVAL_IF(copy_from_user(&admin_command, (void *)arg, _IOC_SIZE(cmd)));
+                admin_command.string[sizeof(admin_command.string) - 1] = '\0';
+                printk(KERN_INFO"%s: string: %s\n", __FUNCTION__, admin_command.string);
+                for (sp = admin_command.string, dp = mesg_otg_instance->serial_number, i = 0; 
+                                *sp && (i < (sizeof(admin_command.string) - 1)); i++, sp++)
+                        if (isxdigit(*sp)) *dp++ = toupper(*sp);
+                        
+                *sp = '\0';
+                //TRACE_MSG1(CORE, "serial_number: %s", mesg_otg_instance->serial_number);
+                printk(KERN_INFO"%s: serial: %s\n", __FUNCTION__, mesg_otg_instance->serial_number);
+                return 0;
+
+        case OTGADMIN_STATUS:
+                //TRACE_MSG0(CORE, "OTGADMIN_STATUS");
+                RETURN_EINVAL_UNLESS(otg_firmware_loaded);
+                memset(&status_update, 0, sizeof(struct otg_status_update));
+
+                otg_mesg_get_status_update(&status_update);
+
+                RETURN_EINVAL_IF(copy_to_user((void *)arg, &status_update, _IOC_SIZE(cmd)));
+                return 0;
+
+        case OTGADMIN_GET_INFO:
+                //TRACE_MSG0(CORE, "OTGADMIN_GET_INFO");
+                RETURN_EINVAL_UNLESS(otg_firmware_loaded);
+
+                otg_mesg_get_firmware_info(&firmware_info);
+
+                RETURN_EINVAL_IF(copy_to_user((void *)arg, &firmware_info, _IOC_SIZE(cmd)));
+                //TRACE_MSG0(CORE, "OTGADMIN_GET_INFO: finished");
+                return 0;
+
+        case OTGADMIN_SET_INFO:
+                //TRACE_MSG0(CORE, "OTGADMIN_SET_INFO");
+                memset(&firmware_info, 0x41, sizeof(firmware_info));
+                RETURN_EINVAL_IF(copy_from_user(&firmware_info, (void *)arg, _IOC_SIZE(cmd)));
+
+
+                return otg_mesg_set_firmware_info(&firmware_info);
+                return 0;
+
+        case OTGADMIN_GET_STATE:
+        case OTGADMIN_SET_STATE:
+                //TRACE_MSG0(CORE, "OTGADMIN_XXX_STATE");
+                RETURN_EINVAL_IF(copy_from_user(&otg_state, (void *)arg, _IOC_SIZE(cmd)));
+
+                switch (cmd) {
+                case OTGADMIN_GET_STATE:
+                        RETURN_EINVAL_UNLESS(otg_firmware_loaded);
+                        //TRACE_MSG0(CORE, "OTGADMIN_GET_STATE");
+                        RETURN_EINVAL_UNLESS (otg_state.state < otg_firmware_loaded->number_of_states);
+                        memcpy(&otg_state, otg_firmware_loaded->otg_states + otg_state.state, sizeof(otg_state));
+                        RETURN_EINVAL_IF(copy_to_user((void *)arg, &otg_state, _IOC_SIZE(cmd)));
+                        break;
+                case OTGADMIN_SET_STATE:
+                        RETURN_EINVAL_UNLESS(otg_firmware_loading);
+                        //TRACE_MSG0(CORE, "OTGADMIN_SET_STATE");
+                        RETURN_EINVAL_UNLESS (otg_state.state < otg_firmware_loading->number_of_states);
+                        memcpy(otg_firmware_loading->otg_states + otg_state.state, &otg_state, sizeof(otg_state));
+                        break;
+                }
+                return 0;
+
+
+        case OTGADMIN_GET_TEST:
+        case OTGADMIN_SET_TEST:
+                RETURN_EINVAL_IF(copy_from_user(&otg_test, (void *)arg, _IOC_SIZE(cmd)));
+
+                switch (cmd) {
+                case OTGADMIN_GET_TEST:
+                        RETURN_EINVAL_UNLESS(otg_firmware_loaded);
+                        //TRACE_MSG1(CORE, "OTGADMIN_GET_TEST: %d", otg_test.test);
+                        RETURN_EINVAL_UNLESS (otg_test.test < otg_firmware_loaded->number_of_tests);
+                        memcpy(&otg_test, otg_firmware_loaded->otg_tests + otg_test.test, sizeof(otg_test));
+                        RETURN_EINVAL_IF(copy_to_user((void *)arg, &otg_test, _IOC_SIZE(cmd)));
+                        break;
+                case OTGADMIN_SET_TEST:
+                        RETURN_EINVAL_UNLESS(otg_firmware_loading);
+                        //TRACE_MSG1(CORE, "OTGADMIN_SET_TEST : %d", otg_test.test);
+                        RETURN_EINVAL_UNLESS (otg_test.test < otg_firmware_loading->number_of_tests);
+                        memcpy(otg_firmware_loading->otg_tests + otg_test.test, &otg_test, sizeof(otg_test));
+                        break;
+                }
+                return 0;
+
+        default:
+                //TRACE_MSG0(CORE, "OTGADMIN_");
+                RETURN_EINVAL_UNLESS(otg_firmware_loaded);
+                for (otg_ioctl_name = otg_ioctl_names; otg_ioctl_name && otg_ioctl_name->cmd; otg_ioctl_name++) {
+                        //TRACE_MSG4(CORE, "lookup: %04x %04x %08x %08x", 
+                        //                _IOC_NR(cmd), _IOC_NR(otg_ioctl_name->cmd), cmd, otg_ioctl_name->cmd);
+                        BREAK_IF(_IOC_NR(otg_ioctl_name->cmd) == _IOC_NR(cmd));
+                }
+                //TRACE_MSG3(CORE, "checking %d %08x %08x", _IOC_NR(cmd), otg_ioctl_name->cmd, cmd);
+                RETURN_EINVAL_UNLESS(otg_ioctl_name->cmd);
+                __get_user(flag, (int *)arg);
+                //TRACE_MSG3(CORE, "%s %08x flag: %d", otg_ioctl_name->name, otg_ioctl_name->set, flag);
+                otg_event (mesg_otg_instance, flag ? otg_ioctl_name->set : _NOT(otg_ioctl_name->set), CORE, otg_ioctl_name->name);
+                return 0;
+        }
+        return 0;
+}
+
+
+
+/*! 
+ * otg_message_ioctl() - ioctl
+ * @param inode inode structure.
+ * @param filp file.
+ * @param cmd ioctl command.
+ * @param arg ioctl argument.
+ * @return non-zero for error.
+ */
+int otg_message_ioctl(struct inode *inode, struct file *filp, unsigned int cmd, unsigned long arg)
+{
+        int i;
+        int len;
+        int flag;
+        struct otg_admin_command admin_command;
+        struct otg_status_update status_update;
+        struct otg_firmware_info firmware_info;
+        struct otg_state otg_state;
+        struct otg_test otg_test;
+        //struct otg_ioctl_map *map = ioctl_map;
+        struct otg_ioctl_name *otg_ioctl_name;
+
+        static char func_buf[32];
+
+        //TRACE_MSG6(CORE, "cmd: %08x arg: %08x type: %02d nr: %02d dir: %02d size: %02d", 
+        //                cmd, arg, _IOC_TYPE(cmd), _IOC_NR(cmd), _IOC_DIR(cmd), _IOC_SIZE(cmd));
+        
+        RETURN_EINVAL_UNLESS (_IOC_TYPE(cmd) == OTGADMIN_MAGIC);
+        RETURN_EINVAL_UNLESS (_IOC_NR(cmd) <= OTGADMIN_MAXNR);
+
+        RETURN_EFAULT_IF((_IOC_DIR(cmd) == _IOC_READ) && !access_ok(VERIFY_WRITE, (void *)arg, _IOC_SIZE(cmd)));
+        RETURN_EFAULT_IF((_IOC_DIR(cmd) == _IOC_WRITE) && !access_ok(VERIFY_READ, (void *)arg, _IOC_SIZE(cmd)));
+
+        return otg_message_ioctl_internal(cmd, arg);
+}
+
+
+/*! 
+ * otg_message_proc_read() - implement proc file system read.
+ * Standard proc file system read function.
+ * @param file file.
+ * @param buf buffer to fill
+ * @param count size of buffer
+ * @param pos file position
+ * @return number of bytes or negative number for error
+ */
+static ssize_t otg_message_proc_read (struct file *file, char *buf, size_t count, loff_t * pos)
+{
+        unsigned long page;
+        int len = 0, avail;
+        char *bp,*ep;
+        struct list_head *lhd;
+        int state;
+
+        // get a page, max 4095 bytes of data...
+        RETURN_ENOMEM_UNLESS ((page = GET_KERNEL_PAGE()));
+
+        bp = (char *) page;
+
+        len = bp - (char *) page;
+
+        if (( avail = otg_message_block())) {
+
+                len += otg_read_message(bp, 4095);
+
+                if (len > count)
+                        len = -EINVAL;
+                else if ((len > 0) && copy_to_user (buf, (char *) page, len))
+                        len = -EFAULT;
+
+        }
+
+        free_page (page);
+        return len;
+}
+
+/*! otg_message_proc_ioctl() -
+ * @param inode inode structure.
+ * @param filp file.
+ * @param cmd ioctl command.
+ * @param arg ioctl argument.
+ * @return non-zero for error.
+ */
+int otg_message_proc_ioctl(struct inode *inode, struct file *filp, unsigned int cmd, unsigned long arg)
+{
+        return otg_message_ioctl(inode, filp, cmd, arg);
+}
+
+/*!
+ * @param filp file.
+ * @param wait buffer to fill
+ * @return number of bytes or negative number for error
+ */
+unsigned int otg_message_proc_poll(struct file *filp, struct poll_table_struct *wait)
+{
+        return otg_message_poll(filp, wait);
+}
+
+/*!
+ * @var otg_message_proc_switch_functions
+ */
+static struct file_operations otg_message_proc_switch_functions = {
+        ioctl:otg_message_proc_ioctl,
+        poll:otg_message_proc_poll,
+        read:otg_message_proc_read,
+};
+
+
+/*!
+ * otg_message_init_l24() - initialize
+ */
+int otg_message_init_l24(struct otg_instance *otg)
+{
+        struct proc_dir_entry *message = NULL;
+        init_waitqueue_head(&otg_message_queue);
+
+        THROW_IF (!(message = create_proc_entry ("otg_message", 0666, 0)), error);
+        message->proc_fops = &otg_message_proc_switch_functions;
+        CATCH(error) {
+                printk(KERN_ERR"%s: creating /proc/otg_message failed\n", __FUNCTION__);
+                if (message)
+                        remove_proc_entry("otg_message", NULL);
+                return -EINVAL;
+        }
+        return 0;
+}
+
+/*!
+ * otg_message_exit_l24() - exit
+ */
+void otg_message_exit_l24(void)
+{
+        remove_proc_entry("otg_message", NULL);
+}
+
+EXPORT_SYMBOL(otg_message);
+
+
diff -uNr linux/drivers/no-otg/otgcore/otg-mesg.c linux/drivers/otg/otgcore/otg-mesg.c
--- linux/drivers/no-otg/otgcore/otg-mesg.c	1970-01-01 01:00:00.000000000 +0100
+++ linux/drivers/otg/otgcore/otg-mesg.c	2006-09-01 21:41:35.000000000 +0200
@@ -0,0 +1,369 @@
+/*
+ * otg/otgcore/otg-mesg.c - OTG state machine Administration
+ *
+ *      Copyright (c) 2004 Belcarra
+ *
+ * By: 
+ *      Stuart Lynne <sl@lbelcarra.com>, 
+ *      Tom Rushworth <tbr@belcarra.com>, 
+ *      Bruce Balden <balden@belcarra.com>
+ *
+ */
+/*!
+ * @file otg/otgcore/otg-mesg.c
+ * @brief OTG Core Message Facility.
+ *
+ * The OTG Core Message Facility has two functions:
+ *
+ *      1. implement a message queue for messages from the otg state machine to the otg
+ *         management application
+ *
+ *      2. implement an ioctl control mechanism allowing the otg managment application 
+ *         to pass data and commands to the otg state machine
+ *
+ * @ingroup OTGCore
+ */
+
+#include <otg/otg-compat.h>
+#include <otg/otg-module.h>
+
+#include <otg/usbp-chap9.h>
+#include <otg/usbp-func.h>
+#include <otg/usbp-bus.h>
+#include <otg/otg-trace.h>
+#include <otg/otg-api.h>
+#include <otg/otg-tcd.h>
+#include <otg/otg-hcd.h>
+#include <otg/otg-pcd.h>
+#include <otg/otg-ocd.h>
+
+#if defined(OTG_LINUX)
+#include <linux/poll.h>
+#include <linux/sched.h>
+#endif /* defined(OTG_LINUX) */
+
+//#define OTG_MESSAGE_BUF_SIZE    512
+#define OTG_MESSAGE_BUF_SIZE    2048
+
+struct otg_instance * mesg_otg_instance;
+
+char *otg_message_buf;          // buffer
+char *otg_message_head;         // next available empty space
+char *otg_message_tail;         // next available data if not equal to head
+
+/*! 
+ * otg_data_avail() - amount of data in buffer
+ *                                      space                   data
+ *           tp     hp                   20 - (10 - 5) = 15      (10 - 5)
+ *     0......5.data.10......20
+ *
+ *          hp     tp                   (10 - 5) = 5            20 - (10 - 5)
+ *     0.data.5......10.data.20
+ *
+ * @return Bytes available in message ring buffer.
+ */
+int otg_data_avail(void)
+{
+        int avail = (otg_message_head >= otg_message_tail) ? 
+                (otg_message_head - otg_message_tail) :
+                OTG_MESSAGE_BUF_SIZE - (otg_message_tail - otg_message_head) ;
+
+        //TRACE_MSG4(CORE, "buf: %p head: %p tail: %p  data avail: %d", otg_message_buf, otg_message_head, otg_message_tail, avail);
+        return avail;
+}
+
+/*! 
+ * otg_space_avail() - amount of space in buffer
+ *
+ * @return Space available in message ring buffer.
+ */
+int otg_space_avail(void)
+{
+        int avail = (otg_message_head >= otg_message_tail) ? 
+                OTG_MESSAGE_BUF_SIZE - (otg_message_head - otg_message_tail) :
+                (otg_message_tail - otg_message_head) ;
+
+        //TRACE_MSG4(CORE, "buf: %p head: %p tail: %p space avail: %d", otg_message_buf, otg_message_head, otg_message_tail, avail);
+        return avail;
+}
+
+/*! 
+ * otg_get_byte() - return next byte
+ *
+ * @return Next byte in message ring buffer
+ */
+char otg_get_byte(void)
+{
+        char c = *otg_message_tail;
+
+        //TRACE_MSG3(CORE, "buf: %p head: %p tail: %p", otg_message_buf, otg_message_head, otg_message_tail);
+        if ((otg_message_tail - otg_message_buf) == OTG_MESSAGE_BUF_SIZE)
+                otg_message_tail = otg_message_buf;
+        else
+                otg_message_tail++;
+
+        return c;
+}
+
+/*!
+ * otg_put_byte() - put byte into buffer
+ *
+ * Put byte into message ring buffer
+ *
+ * @param byte put into message ring.
+ */
+void otg_put_byte(char byte)
+{
+        *otg_message_head = byte;
+        if ((otg_message_head - otg_message_buf) == OTG_MESSAGE_BUF_SIZE)
+                otg_message_head = otg_message_buf;
+        else
+                otg_message_head++;
+
+}
+
+/*! 
+ * otg_write_message_irq() - queue message data (interrupt version)
+ *
+ * Attempt to write message data into ring buffer, return amount
+ * of data actually written.
+ *
+ * @param buf - pointer to data
+ * @param size - amount of available data
+ * @return number of bytes that where not written.
+ */
+int otg_write_message_irq (char *buf, int size)
+{
+        struct pcd_instance *pcd = NULL;
+        struct usbd_bus_instance *bus = NULL;
+
+        pcd = (struct pcd_instance *)mesg_otg_instance->pcd;
+        if (pcd)
+                bus = pcd->bus;
+
+        if (size >= (OTG_MESSAGE_BUF_SIZE - 2))
+                return size;
+
+        while (otg_space_avail() < (size + 1))
+                otg_get_byte();
+
+
+        /* copy, note that we don't have to test for space as we have
+         * already ensured that there is enough room for all of the data
+         */
+        while (size--) 
+                otg_put_byte(*buf++);
+
+        //otg_put_byte('\n');
+
+        return size;
+}
+
+/*! 
+ * otg_write_message() - queue message data
+ *
+ * Attempt to write message data into ring buffer, return amount
+ * of data actually written.
+ *
+ * @param buf - pointer to data
+ * @param size - amount of available data
+ * @return number of bytes that where not written.
+ */
+int otg_write_message (char *buf, int size)
+{
+	int rc;
+        unsigned long flags;
+        local_irq_save (flags);
+	rc = otg_write_message_irq(buf, size);
+        local_irq_restore (flags);
+        return size;
+}
+
+/*!
+ * otg_read_message() - get queued message data
+ *
+ * @param buf - pointer to data
+ * @param size - amount of available data
+ * @return the number of bytes read.
+ */
+int otg_read_message(char *buf, int size)
+{
+        int count = 0;
+        unsigned long flags;
+	
+        local_irq_save (flags);
+
+        /* Copy bytes to the buffer while there is data available and space
+         * to put it in the buffer
+         */
+        for ( ; otg_data_avail() && size--; count++)
+                BREAK_IF((*buf++ = otg_get_byte()) == '\n');
+
+        local_irq_restore (flags);
+        return count;
+}
+
+/*!
+ * otg_data_queued() - return amount of data currently queued
+ *
+ * @return Amount of data currently queued in mesage ring buffer.
+ */
+int otg_data_queued(void)
+{
+        int count;
+        unsigned long flags;
+        local_irq_save (flags);
+        count = otg_data_avail();
+        local_irq_restore (flags);
+        //printk(KERN_INFO"%s: count: %d\n", __FUNCTION__, count);
+        return count;
+}
+
+
+/*!
+ * otg_message_init() - initialize
+ * @param otg OTG instance
+ */
+int otg_message_init(struct otg_instance *otg)
+{
+        struct proc_dir_entry *message = NULL;
+        otg_message_buf = otg_message_head = otg_message_tail = NULL;
+        THROW_UNLESS((otg_message_buf = CKMALLOC(OTG_MESSAGE_BUF_SIZE + 16, GFP_KERNEL)), error);
+        otg_message_head = otg_message_tail = otg_message_buf ;
+
+        mesg_otg_instance = otg;
+        CATCH(error) {
+                #if defined(OTG_LINUX)
+                printk(KERN_ERR"%s: creating /proc/otg_message failed\n", __FUNCTION__);
+                #endif /* defined(OTG_LINUX) */
+                #if defined(OTG_WINCE)
+                DEBUGMSG(ZONE_INIT, (_T("otg_message_init: creating //proc//otg_message failed\n")) );
+                #endif /* defined(OTG_LINUX) */
+                return -EINVAL;
+        }
+        return 0;
+}
+
+/*!
+ * otg_message_exit() - exit
+ */
+void otg_message_exit(void)
+{
+        mesg_otg_instance = NULL;
+        if (otg_message_buf)
+                LKFREE(otg_message_buf);
+        otg_message_buf = otg_message_head = otg_message_tail = NULL;
+}
+
+
+/* ********************************************************************************************* */
+/* ********************************************************************************************* */
+
+/*!
+ * otg_mesg_get_status_update() - get status update
+ * @param status_update
+ */
+void otg_mesg_get_status_update(struct otg_status_update *status_update)
+{
+
+        TRACE_MSG0(CORE, "--");
+        status_update->state = mesg_otg_instance->state;
+        status_update->meta = mesg_otg_instance->current_outputs->meta;
+        status_update->inputs = mesg_otg_instance->current_inputs;
+        status_update->outputs = mesg_otg_instance->outputs;
+        status_update->capabilities = otg_ocd_ops ? otg_ocd_ops->capabilities : 0;
+
+        strncpy(status_update->fw_name, otg_firmware_loaded->fw_name, OTGADMIN_MAXSTR);
+        strncpy(status_update->state_name, otg_get_state_name(mesg_otg_instance->state), OTGADMIN_MAXSTR);
+
+        if (mesg_otg_instance->function_name)
+                strncpy(status_update->function_name, mesg_otg_instance->function_name, OTGADMIN_MAXSTR);
+
+        TRACE_MSG5(CORE, "state: %02x meta: %02x inputs: %08x outputs: %04x", 
+                        status_update->state, status_update->meta, status_update->inputs, 
+                        status_update->outputs, status_update->capabilities);
+}
+
+
+/*!
+ * otg_mesg_get_firmware_info() - get firmware info
+ * @param firmware_info
+ */
+void otg_mesg_get_firmware_info(struct otg_firmware_info *firmware_info)
+{
+        memset(firmware_info, 0, sizeof(firmware_info));
+        // XXX irq lock
+
+        firmware_info->number_of_states = otg_firmware_loaded->number_of_states;
+        firmware_info->number_of_tests = otg_firmware_loaded->number_of_tests;
+        memcpy(&firmware_info->fw_name, otg_firmware_loaded->fw_name, sizeof(firmware_info->fw_name));
+
+}
+
+/*! 
+ * otg_mesg_set_firmware_info() - set firmware info
+ * @param firmware_info
+ */
+int otg_mesg_set_firmware_info(struct otg_firmware_info *firmware_info)
+{
+
+        if (otg_firmware_loading) {
+
+                if ( (otg_firmware_loading->number_of_states == firmware_info->number_of_states) &&
+                                (otg_firmware_loading->number_of_tests == firmware_info->number_of_tests) 
+                   ) {
+                        TRACE_MSG0(CORE, "OTGADMIN_SET_INFO: loaded");
+                        otg_firmware_loaded = otg_firmware_loading;
+                        otg_firmware_loading = NULL;
+                        return 0;
+                }
+                else {
+                        TRACE_MSG0(CORE, "OTGADMIN_SET_INFO: abandoned");
+                        LKFREE(otg_firmware_loading->otg_states);
+                        LKFREE(otg_firmware_loading->otg_tests);
+                        LKFREE(otg_firmware_loading);
+                        return -EINVAL;
+                }
+        }
+
+        TRACE_MSG2(CORE, "OTGADMIN_SET_INFO: otg_firmware_loaded: %x otg_firmware_orig: %x", 
+                        otg_firmware_loaded, otg_firmware_orig);
+        if (otg_firmware_loaded && (otg_firmware_loaded != otg_firmware_orig)) {
+                LKFREE(otg_firmware_loaded->otg_states);
+                LKFREE(otg_firmware_loaded->otg_tests);
+                LKFREE(otg_firmware_loaded);
+                otg_firmware_loaded = otg_firmware_orig;
+                TRACE_MSG0(CORE, "OTGADMIN_SET_INFO: unloaded");
+        }
+        if (firmware_info->number_of_states && firmware_info->number_of_tests) {
+                int size;
+                otg_firmware_loaded = NULL;
+                TRACE_MSG0(CORE, "OTGADMIN_SET_INFO: setup");
+                THROW_UNLESS((otg_firmware_loading = CKMALLOC(sizeof(struct otg_firmware), GFP_KERNEL)), error);
+                size = sizeof(struct otg_state) * firmware_info->number_of_states;
+                THROW_UNLESS((otg_firmware_loading->otg_states = CKMALLOC(size, GFP_KERNEL)), error);
+                size = sizeof(struct otg_test) * firmware_info->number_of_tests;
+                THROW_UNLESS((otg_firmware_loading->otg_tests = CKMALLOC(size, GFP_KERNEL)), error);
+
+                otg_firmware_loading->number_of_states = firmware_info->number_of_states;
+                otg_firmware_loading->number_of_tests = firmware_info->number_of_tests;
+                memcpy(otg_firmware_loading->fw_name, &firmware_info->fw_name, sizeof(firmware_info->fw_name));
+
+                CATCH(error) {
+                        TRACE_MSG0(CORE, "OTGADMIN_SET_INFO: setup error");
+                        if (otg_firmware_loading) {
+                                if (otg_firmware_loading->otg_states) LKFREE(otg_firmware_loading->otg_states);
+                                if (otg_firmware_loading->otg_tests) LKFREE(otg_firmware_loading->otg_tests);
+                                LKFREE(otg_firmware_loading);
+                                return -EINVAL;
+                        }
+                }
+
+        }
+        return 0;
+}
+
+
+/* ********************************************************************************************* */
+/* ********************************************************************************************* */
+
diff -uNr linux/drivers/no-otg/otgcore/otg-trace-l24.c linux/drivers/otg/otgcore/otg-trace-l24.c
--- linux/drivers/no-otg/otgcore/otg-trace-l24.c	1970-01-01 01:00:00.000000000 +0100
+++ linux/drivers/otg/otgcore/otg-trace-l24.c	2006-09-01 21:41:35.000000000 +0200
@@ -0,0 +1,174 @@
+/*
+ * otg/otgcore/otg-trace-l24.c
+ *
+ *      Copyright (c) 2004 Belcarra
+ *
+ * By: 
+ *      Stuart Lynne <sl@belcarra.com>, 
+ *      Tom Rushworth <tbr@belcarra.com>, 
+ *
+ */
+/*!
+ * @file otg/otgcore/otg-trace-l24.c
+ * @brief OTG Core Linux Debug Trace Facility
+ *
+ * This allows access to the OTG Core Trace via /proc/trace_otg. Each time
+ * this file is opened and read it will contain a snapshot of the most
+ * recent trace messages. 
+ *
+ * Reading the trace resets it, the next read will open a new snapshot.
+ *
+ * N.B. There is no protection from multiple reads.
+ *
+ * N.B. This is for debugging and is not normally enabled for production software.
+ *
+ * @ingroup OTGCore
+ */
+
+#include <otg/otg-compat.h>
+#include <otg/otg-module.h>
+#include <linux/vmalloc.h>
+#include <stdarg.h>
+
+#include <otg/usbp-chap9.h>
+#include <otg/usbp-func.h>
+#include <otg/usbp-bus.h>
+
+#include <otg/otg-trace.h>
+#include <otg/otg-api.h>
+
+#if defined(CONFIG_OTG_TRACE) || defined(CONFIG_OTG_TRACE_MODULE)
+
+DECLARE_MUTEX(otg_trace_sem_l24);
+
+
+/* *    
+ * otg_trace_proc_read_l24 - implement proc file system read.
+ * @file        
+ * @buf         
+ * @count
+ * @pos 
+ *      
+ * Standard proc file system read function.
+ */         
+static ssize_t otg_trace_proc_read_l24 (struct file *file, char *buf, size_t count, loff_t * pos)
+{                                  
+        unsigned long page;
+        int len = 0;
+        int index;
+        int oindex;
+        int rc;
+
+        u64 ticks = 0;
+
+        unsigned char *cp;
+        int skip = 0;
+
+        otg_trace_t px;
+        otg_trace_t ox;
+        otg_trace_t vx;
+        otg_trace_t *o, *p;
+
+        RETURN_ENOMEM_IF (!(page = GET_KERNEL_PAGE()));
+
+        _MOD_INC_USE_COUNT;
+
+        // Lock out tag changes while reading
+        DOWN(&otg_trace_sem_l24);
+        len = otg_trace_proc_read((char *)page, count, (int *)pos);
+
+        if (len > count) 
+                len = -EINVAL;
+        
+        else if (len > 0 && copy_to_user (buf, (char *) page, len)) 
+                len = -EFAULT;
+        
+        free_page (page);
+
+        UP(&otg_trace_sem_l24);
+        free_page (page);
+        _MOD_DEC_USE_COUNT;
+        return len;
+}
+
+#define MAX_TRACE_CMD_LEN  64
+/* *
+ * otg_trace_proc_write_l24 - implement proc file system write.
+ * @file
+ * @buf
+ * @count
+ * @pos
+ *
+ * Proc file system write function.
+ */
+static ssize_t otg_trace_proc_write_l24 (struct file *file, const char *buf, size_t count, loff_t * pos)
+{
+        char command[MAX_TRACE_CMD_LEN+1];
+        size_t l = MIN(count, MAX_TRACE_CMD_LEN);
+
+        RETURN_ZERO_UNLESS(count);
+        RETURN_EINVAL_IF (copy_from_user (command, buf, l));
+
+
+        RETURN_EINVAL_IF(copy_from_user (command, buf, l));
+
+        return otg_trace_proc_write(command, l, (int *)pos);
+#if 0
+        else {
+                n -= l;
+                while (n > 0) {
+                        if (copy_from_user (&c, buf + (count - n), 1)) {
+                                count = -EFAULT;
+                                break;
+                        }
+                        n -= 1;
+                }
+                // Terminate command[]
+                if (l > 0 && command[l-1] == '\n') {
+                        l -= 1;
+                }
+                command[l] = 0;
+        }
+#endif
+}
+
+/* Module init ************************************************************** */
+
+
+
+static struct file_operations otg_trace_proc_operations_functions = {
+        read: otg_trace_proc_read_l24,
+        write: otg_trace_proc_write_l24,
+};
+
+/*! otg_trace_init_l24
+ *
+ * Return non-zero if not successful.
+ */
+int otg_trace_init_l24 (void)
+{
+        struct proc_dir_entry *p;
+
+        // create proc filesystem entries
+        if ((p = create_proc_entry ("trace_otg", 0, 0)) == NULL) 
+                printk(KERN_INFO"%s PROC FS failed\n", "trace_otg");
+        else 
+                p->proc_fops = &otg_trace_proc_operations_functions;
+
+        return 0;
+}
+
+/*! otg_trace_exit_l24 - remove procfs entry, free trace data space.
+ */
+void otg_trace_exit_l24 (void)
+{
+        otg_trace_t *p;
+        unsigned long flags;
+        remove_proc_entry ("trace_otg", NULL);
+}
+
+#else /* defined(CONFIG_OTG_TRACE) || defined(CONFIG_OTG_TRACE_MODULE) */
+int otg_trace_init_l24 (void) {return 0;}
+void otg_trace_exit_l24 (void) {}
+#endif /* defined(CONFIG_OTG_TRACE) || defined(CONFIG_OTG_TRACE_MODULE) */
+
diff -uNr linux/drivers/no-otg/otgcore/otg-trace.c linux/drivers/otg/otgcore/otg-trace.c
--- linux/drivers/no-otg/otgcore/otg-trace.c	1970-01-01 01:00:00.000000000 +0100
+++ linux/drivers/otg/otgcore/otg-trace.c	2006-09-01 21:41:35.000000000 +0200
@@ -0,0 +1,615 @@
+/*
+ * otg/otgcore/otg-trace.c
+ *
+ *      Copyright (c) 2004 Belcarra
+ *
+ * By: 
+ *      Stuart Lynne <sl@belcarra.com>, 
+ *      Tom Rushworth <tbr@belcarra.com>, 
+ *
+ */
+/*!
+ * @file otg/otgcore/otg-trace.c
+ * @brief OTG Core Debug Trace Facility
+ *
+ * This implements the OTG Core Trace Facility. 
+ *
+ * @ingroup OTGCore
+ */
+
+#include <otg/otg-compat.h>
+#include <otg/otg-module.h>
+
+#if defined(OTG_LINUX)
+#include <linux/vmalloc.h>
+#include <stdarg.h>
+#endif /* defined(OTG_LINUX) */
+
+#include <otg/usbp-chap9.h>
+#include <otg/usbp-func.h>
+#include <otg/usbp-bus.h>
+
+#include <otg/otg-trace.h>
+#include <otg/otg-api.h>
+
+#define STATIC static
+
+#if defined(CONFIG_OTG_TRACE) || defined(CONFIG_OTG_TRACE_MODULE)
+
+DECLARE_MUTEX(otg_trace_sem);
+
+static int otg_trace_exiting;
+
+#define OTG_TRACE_NAME "trace_otg"
+
+typedef struct otg_trace_snapshot {
+        int first;
+        int next;
+        int total;
+        otg_trace_t *traces;
+} otg_trace_snapshot;
+
+otg_trace_snapshot otg_snapshot1;
+otg_trace_snapshot otg_snapshot2;
+
+static otg_trace_snapshot *otg_trace_current_traces;
+static otg_trace_snapshot *otg_trace_other_traces;
+
+STATIC otg_trace_snapshot *rel_snapshot(otg_trace_snapshot *tss)
+{
+        RETURN_NULL_UNLESS(tss);
+        if (tss->traces) {
+                vfree(tss->traces);
+                tss->traces = NULL;
+        }
+        //vfree(tss);
+        return NULL;
+}
+
+STATIC void init_snapshot(otg_trace_snapshot *tss)
+{
+        tss->first = tss->next = tss->total = 0;
+        memset(tss->traces,0,sizeof(otg_trace_t) * TRACE_MAX);
+}
+
+STATIC otg_trace_snapshot *get_snapshot(otg_trace_snapshot *tss)
+{
+        //otg_trace_snapshot *tss;
+        unsigned long flags;
+        //UNLESS ((tss = vmalloc(sizeof(otg_trace_snapshot)))) {
+        //        printk(KERN_ERR "Cannot allocate memory for OTG trace snapshot.\n");
+        //        return NULL;
+        //}
+        
+        UNLESS ((tss->traces = vmalloc(sizeof(otg_trace_t) * TRACE_MAX))) {
+                printk(KERN_ERR "Cannot allocate memory for OTG trace log.\n");
+                return rel_snapshot(tss);
+        }
+        init_snapshot(tss);
+        return tss;
+}
+
+/*! otg_trace_get_next
+ */
+STATIC otg_trace_t *otg_trace_get_next(otg_trace_snapshot *tss, otg_tag_t tag, const char *fn, otg_trace_types_t otg_trace_type, int n)
+{
+        otg_trace_t *p;
+
+        // Get the next (n) trace slots.
+
+        p = tss->traces + tss->next;
+        
+        while (n-- > 0) {
+                tss->next = (tss->next + 1) & TRACE_MASK;
+                if (tss->next == tss->first) {
+                        // We have wraparound, bump tss->first
+                        tss->first = (tss->first + 1) & TRACE_MASK;
+                }
+                tss->total++;
+        }
+        // End of next trace slot.
+        
+        otg_get_trace_info(p);
+        p->otg_trace_type = otg_trace_type;
+        p->tag = tag;
+        p->va.s.function = fn;
+
+        return p;
+}
+
+/* otg_trace_next_va
+ */
+STATIC otg_trace_t *otg_trace_next_va(otg_trace_snapshot *tss, otg_trace_t *p)
+{
+        /* Return the next trace slot in a va group.
+           No need to be atomic, just worry about wraparound. */
+        int nxt = (p - tss->traces);
+        nxt = (nxt + 1) & TRACE_MASK;
+        p = tss->traces + nxt;
+        return(p);
+}
+
+/* otg_trace_setup
+ */
+void otg_trace_setup(otg_tag_t tag, const char *fn, void *setup)
+{
+        otg_trace_t *p;
+        otg_trace_snapshot *tss;
+        unsigned long flags;
+        local_irq_save(flags);
+        UNLESS ((tss = otg_trace_current_traces)) {
+                local_irq_restore(flags);
+                return;
+        }
+        // Reserve the trace slots.
+        UNLESS ((p = otg_trace_get_next(tss,tag,fn,otg_trace_setup_n,1))) {
+                local_irq_restore(flags);
+                return;
+        }
+        p->va_num_args = 0;
+        memcpy(&p->va.s.trace.setup, setup, sizeof(p->va.s.trace.setup) /*sizeof(struct usbd_device_request)*/);
+        local_irq_restore(flags);
+}
+
+/* otg_trace_msg
+ */
+void otg_trace_msg(otg_tag_t tag, const char *fn, u8 nargs, char *fmt, ...)
+{
+        int n;
+        otg_trace_t *p;
+        otg_trace_snapshot *tss;
+        unsigned long flags;
+
+        // Figure out how many trace slots we're going to need.
+        n = 1;
+        if (nargs > 1) {
+                n = 2;
+                // Too many arguments, bail.
+                RETURN_IF (nargs > (OTG_TRACE_MAX_IN_VA+1));
+        }
+
+        local_irq_save(flags);
+        UNLESS ((tss = otg_trace_current_traces)) {
+                local_irq_restore(flags);
+                return;
+        }
+        // Reserve the trace slots.
+        UNLESS((p = otg_trace_get_next(tss,tag,fn,otg_trace_msg_va_start_n,n))) {
+                local_irq_restore(flags);
+                return;
+        }
+        p->va.s.trace.msg32.msg = fmt;
+        p->va_num_args = nargs;
+        if (nargs > 0) {
+                va_list ap;
+                va_start(ap,fmt);
+                p->va.s.trace.msg32.val = va_arg(ap,u32);
+                if (--nargs > 0) {
+                        // We need the next trace slot.
+                        int i;
+                        while (nargs >= 8) {
+                                p = otg_trace_next_va(tss,p);
+                                p->otg_trace_type = otg_trace_msg_va_list_n;
+                                for (i = 0; i < 8; i++) p->va.l.val[i] = va_arg(ap,u32);
+                                nargs -= 8;
+                        }
+                        p = otg_trace_next_va(tss,p);
+                        p->otg_trace_type = otg_trace_msg_va_list_n;
+                        for (i = 0; i < nargs; i++) p->va.l.val[i] = va_arg(ap,u32);
+                }
+                va_end(ap);
+        }
+        local_irq_restore(flags);
+}
+
+/* Proc Filesystem *************************************************************************** */
+
+/* int slot_in_data
+ */
+STATIC int slot_in_data(otg_trace_snapshot *tss, int slot)
+{
+        // Return 1 if slot is in the valid data region, 0 otherwise.
+        return (((tss->first < tss->next) && (slot >= tss->first) && (slot < tss->next)) ||
+                ((tss->first > tss->next) && ((slot < tss->next) || (slot >= tss->first))));
+
+}
+
+/*! otg_trace_read_slot
+ *
+ * Place a slot pointer in *ps (and possibly *pv), with the older entry (if any) in *po.
+ * Return: -1 at end of data
+ *          0 if not a valid enty
+ *          1 if entry valid but no older valid entry
+ *          2 if both entry and older are valid.
+ */
+STATIC int otg_trace_read_slot(otg_trace_snapshot *tss, int index, otg_trace_t **ps, otg_trace_t **po)
+{
+        int res;
+        int previous;
+        otg_trace_t *s,*o;
+
+        *ps = *po = NULL;
+        index = (tss->first + index) & TRACE_MASK;
+        // Are we at the end of the data?
+        if (!slot_in_data(tss,index)) {
+                // End of data.
+                return(-1);
+        }
+        /* Nope, there's data to show. */
+        s = tss->traces + index;
+        if (!s->tag ||
+            s->otg_trace_type == otg_trace_msg_invalid_n ||
+            s->otg_trace_type == otg_trace_msg_va_list_n) {
+                /* Ignore this slot, it's not valid, or is part
+                   of a previously processed varargs. */
+                return(0);
+        }
+        *ps = s;
+        res = 1;
+        // Is there a previous event (for "ticks" calculation)?
+        previous = (index - 1) & TRACE_MASK;
+        if (previous != tss->next && tss->total > 1) {
+                // There is a valid previous event.
+                res = 2;
+                o = tss->traces + previous;
+                /* If the previous event was a varargs event, we want
+                   a copy of the first slot, not the last. */
+                while (o->otg_trace_type == otg_trace_msg_va_list_n) {
+                        previous = (previous - 1) & TRACE_MASK;
+                        if (previous == tss->next) {
+                                res = 1;
+                                o = NULL;
+                                break;
+                        }
+                        o = tss->traces + previous;
+                }
+                *po = o;
+        }
+        return(res);
+}
+
+
+/*! otg_trace_proc_read - implement proc file system read.
+ *      
+ * Standard proc file system read function.
+ * @file        
+ * @buf         
+ * @count
+ * @pos 
+ */         
+int otg_trace_proc_read(char *page, int count, int * pos)
+{                                  
+        int len = 0;
+        int index;
+        int oindex;
+        int rc;
+
+        u64 ticks = 0;
+
+        unsigned char *cp;
+        int skip = 0;
+
+        otg_trace_t *o;
+        otg_trace_t *s;
+        otg_trace_snapshot *tss;
+
+        DOWN(&otg_trace_sem);
+
+        /* Grab the current snapshot, and replace it with the other.  This
+         * needs to be atomic WRT otg_trace_msg() calls.
+         */
+        UNLESS (*pos) {
+                unsigned long flags;
+                tss = otg_trace_other_traces;
+                init_snapshot(tss);                                     // clear previous
+                local_irq_save(flags);
+                otg_trace_other_traces = otg_trace_current_traces;      // swap snapshots
+                otg_trace_current_traces = tss;
+                local_irq_restore(flags);
+        } 
+
+        tss = otg_trace_other_traces;
+
+        /* Get the index of the entry to read - this needs to be atomic WRT tag operations. 
+         */
+        s = o = NULL;
+        oindex = index = (*pos)++;
+        do {
+                rc = otg_trace_read_slot(tss,index,&s,&o);
+                switch (rc) {
+                case -1: // End of data.
+                        UP(&otg_trace_sem);
+                        return(0);
+
+                case 0: // Invalid slot, skip it and try the next.
+                        index = (*pos)++;
+                        break;
+
+                case 1: // s valid, o NULL
+                case 2: // s and o valid
+                        break;
+                }
+        } while (rc == 0);
+
+        len = 0;
+
+        UNLESS (oindex) 
+                len += sprintf((char *) page + len, "Tag  Index     Ints Framenum   Ticks\n");
+
+        /* If there is a previous trace event, we want to calculate how many
+         * ticks have elapsed siince it happened.  Unfortunately, determining
+         * if there _is_ a previous event isn't obvious, since we have to watch
+         * out for startup, varargs and wraparound. 
+         */
+        if (o) {
+        
+                ticks = otg_tmr_elapsed(&s->va.s.ticks, &o->va.s.ticks);
+
+                if ((o->va.s.interrupts != s->va.s.interrupts) || (o->tag != s->tag) || (o->in_interrupt != s->in_interrupt)) 
+                        skip++;
+        }
+
+        len += sprintf((char *) page + len, "%s%02x %c %8u %8d ", skip?"\n":"", 
+                        s->tag, s->id_gnd ? 'A' : 'B', index, s->va.s.interrupts);
+
+        len += sprintf((char *) page + len, "%03x  ", s->va.s.framenum);
+
+        if (ticks < 10000)
+                len += sprintf((char *) page + len, "%8duS", ticks);
+        else if (ticks < 10000000)
+                len += sprintf((char *) page + len, "%8dmS", ticks >> 10);
+        else 
+                len += sprintf((char *) page + len, "%8dS ", ticks >> 20);
+
+        len += sprintf((char *) page + len, " %s ", s->in_interrupt ? "--" : "==");
+        len += sprintf((char *) page + len, "%-24s: ",s->va.s.function);
+
+        switch (s->otg_trace_type) {
+        case otg_trace_msg_invalid_n:
+        case otg_trace_msg_va_list_n:
+                len += sprintf((char *) page + len, " --          N/A");
+                break;
+
+        case otg_trace_msg_va_start_n:
+                if (s->va_num_args <= 1) 
+                        len += sprintf((char *) page + len, s->va.s.trace.msg32.msg, s->va.s.trace.msg32.val);
+                
+                else {
+                        otg_trace_t *v = otg_trace_next_va(tss,s);
+                        len += sprintf((char *) page + len, s->va.s.trace.msg32.msg, s->va.s.trace.msg32.val,
+                                       v->va.l.val[0], v->va.l.val[1], v->va.l.val[2], v->va.l.val[3],
+                                       v->va.l.val[4], v->va.l.val[5], v->va.l.val[6]);
+                }
+                break;
+
+        case otg_trace_msg_n:
+                len += sprintf((char *) page + len, s->va.s.trace.msg.msg);
+                break;
+
+        case otg_trace_msg32_n:
+                len += sprintf((char *) page + len, s->va.s.trace.msg32.msg, s->va.s.trace.msg32.val);
+                break;
+
+        case otg_trace_msg16_n:
+                len += sprintf((char *) page + len, s->va.s.trace.msg16.msg, s->va.s.trace.msg16.val0, s->va.s.trace.msg16.val1);
+                break;
+
+        case otg_trace_msg8_n:
+                len += sprintf((char *) page + len, s->va.s.trace.msg8.msg, 
+                               s->va.s.trace.msg8.val0, s->va.s.trace.msg8.val1, s->va.s.trace.msg8.val2, 
+                               s->va.s.trace.msg8.val3);
+                break;
+
+        case otg_trace_setup_n:
+                cp = (unsigned char *)&s->va.s.trace.setup;
+                len += sprintf((char *) page + len, 
+                               " --      request [%02x %02x %02x %02x %02x %02x %02x %02x]", 
+                               cp[0], cp[1], cp[2], cp[3], cp[4], cp[5], cp[6], cp[7]);
+                break;
+        }
+        len += sprintf((char *) page + len, "\n");
+
+        UP(&otg_trace_sem);
+        return len;
+}
+
+/*! otg_trace_proc_write - implement proc file system write.
+ * @file
+ * @buf
+ * @count
+ * @pos
+ *
+ * Proc file system write function.
+ */
+int otg_trace_proc_write (const char *buf, int count, int * pos)
+{
+#define MAX_TRACE_CMD_LEN  64
+        char command[MAX_TRACE_CMD_LEN+1];
+        size_t n = count;
+        size_t l;
+        char c;
+
+        if (n > 0) {
+                l = MIN(n,MAX_TRACE_CMD_LEN);
+                if (copy_from_user (command, buf, l)) 
+                        count = -EFAULT;
+                else {
+                        n -= l;
+                        while (n > 0) {
+                                if (copy_from_user (&c, buf + (count - n), 1)) {
+                                        count = -EFAULT;
+                                        break;
+                                }
+                                n -= 1;
+                        }
+                        // Terminate command[]
+                        if (l > 0 && command[l-1] == '\n') {
+                                l -= 1;
+                        }
+                        command[l] = 0;
+                }
+        }
+
+        if (0 >= count) {
+                printk(KERN_INFO"%s: count <= 0 %d\n", __FUNCTION__, count);
+                return count;
+        }
+
+
+        return count;
+}
+
+static u32 otg_tags_mask = 0x0;
+extern void otg_trace_exit (void);
+
+/*! otg_trace_obtain_tag(void)
+ */
+otg_tag_t otg_trace_obtain_tag(void)
+{
+        /* Return the next unused tag value [1..32] if one is available,
+         * return 0 if none is available. 
+         */
+        otg_tag_t tag = 0;
+        DOWN(&otg_trace_sem);
+        if (otg_tags_mask == 0xffffffff || otg_trace_exiting) {
+                // They're all in use.
+                UP(&otg_trace_sem);
+                return tag;
+        }
+        if (otg_tags_mask != 0x00000000) {
+                // 2nd or later tag, search for an unused one
+                while (otg_tags_mask & (0x1 << tag)) {
+                        tag += 1;
+                }
+        } 
+        
+        // Successful 1st or later tag.
+        otg_tags_mask |= 0x1 << tag;
+        UP(&otg_trace_sem);
+        return tag + 1;
+}
+
+/*! otg_trace_invalidate_tag(otg_tag_t tag)
+ */
+otg_tag_t otg_trace_invalidate_tag(otg_tag_t tag)
+{
+        otg_trace_t *p,*e;
+        otg_trace_snapshot *css,*oss;
+        unsigned long flags;
+
+        // Nothing to do
+        RETURN_ZERO_IF ((0 >= tag) || (tag > 32));
+        
+        // Grab a local copy of the snapshot pointers.
+        local_irq_save(flags);
+        css = otg_trace_current_traces;
+        oss = otg_trace_other_traces;
+        local_irq_restore(flags);
+
+
+        // Run through all the trace messages, and invalidate any with the given tag.
+
+        DOWN(&otg_trace_sem);
+        if (css)
+                for (e = TRACE_MAX + (p = css->traces); p < e; p++) {
+                        if (p->tag == tag) {
+                                p->otg_trace_type = otg_trace_msg_invalid_n;
+                                p->tag = 0;
+                        }
+                }
+        if (oss)
+                for (e = TRACE_MAX + (p = oss->traces); p < e; p++) {
+                        if (p->tag == tag) {
+                                p->otg_trace_type = otg_trace_msg_invalid_n;
+                                p->tag = 0;
+                        }
+                }
+        
+        // Turn off the tag
+        otg_tags_mask &= ~(0x1 << (tag-1));
+        if (otg_tags_mask == 0x00000000) { /* otg_trace_exit (); */ }
+        UP(&otg_trace_sem);
+        return 0;
+}
+
+
+
+/* Module init ************************************************************** */
+
+#define OT otg_trace_init_test
+otg_tag_t OT;
+
+/*! otg_trace_init
+ *
+ * Return non-zero if not successful.
+ */
+int otg_trace_init (void)
+{
+        otg_trace_exiting = 0;
+        otg_trace_current_traces = otg_trace_other_traces = NULL;
+        UNLESS ((otg_trace_current_traces = get_snapshot(&otg_snapshot1)) && 
+                        (otg_trace_other_traces = get_snapshot(&otg_snapshot2))) 
+        {
+                printk(KERN_ERR"%s: malloc failed (2x) %d\n", __FUNCTION__, sizeof(otg_trace_t) * TRACE_MAX);
+                otg_trace_current_traces = rel_snapshot(otg_trace_current_traces);
+                otg_trace_other_traces = rel_snapshot(otg_trace_other_traces);
+                return -ENOMEM;
+        }
+        OT = otg_trace_obtain_tag();
+        RETURN_ENOMEM_IF(!OT);
+        TRACE_MSG0(OT,"--");
+        return 0;
+}
+
+/*! otg_trace_exit - remove procfs entry, free trace data space.
+ */
+void otg_trace_exit (void)
+{
+        otg_trace_t *p;
+        otg_tag_t tag;
+
+        unsigned long flags;
+        otg_trace_snapshot *c,*o;
+
+        otg_trace_invalidate_tag(OT);
+        otg_trace_exiting = 1;
+
+        local_irq_save (flags);
+
+        /* Look for any outstanding tags. */
+        for (tag = 32; otg_tags_mask && tag > 0; tag--) 
+                if (otg_tags_mask & (0x1 << (tag-1))) 
+                        otg_trace_invalidate_tag(tag);
+
+        c = otg_trace_current_traces;
+        o = otg_trace_other_traces;
+        otg_trace_current_traces = otg_trace_other_traces = NULL;
+        local_irq_restore (flags);
+
+        c = rel_snapshot(c);
+        o = rel_snapshot(o);
+}
+
+#else /* defined(CONFIG_OTG_TRACE) || defined(CONFIG_OTG_TRACE_MODULE) */
+int otg_trace_init (void) {return 0;}
+void otg_trace_exit (void) {}
+otg_tag_t otg_trace_obtain_tag(void) { return 0; }
+void otg_trace_setup(otg_tag_t tag, const char *fn, void *setup) { }
+void otg_trace_msg(otg_tag_t tag, const char *fn, u8 nargs, char *fmt, ...) { }
+otg_tag_t otg_trace_invalidate_tag(otg_tag_t tag) { return 0; }
+#endif /* defined(CONFIG_OTG_TRACE) || defined(CONFIG_OTG_TRACE_MODULE) */
+
+OTG_EXPORT_SYMBOL(otg_trace_setup);
+OTG_EXPORT_SYMBOL(otg_trace_msg);
+OTG_EXPORT_SYMBOL(otg_trace_obtain_tag);
+OTG_EXPORT_SYMBOL(otg_trace_invalidate_tag);
+
+#if 0
+// These are only needed when debugging the trace code itself.
+OTG_EXPORT_SYMBOL(otg_trace_get_next);
+OTG_EXPORT_SYMBOL(otg_trace_next_va);
+OTG_EXPORT_SYMBOL(slot_in_data);
+OTG_EXPORT_SYMBOL(otg_trace_read_slot);
+OTG_EXPORT_SYMBOL(otg_trace_proc_read);
+#endif
+
diff -uNr linux/drivers/no-otg/otgcore/otg.c linux/drivers/otg/otgcore/otg.c
--- linux/drivers/no-otg/otgcore/otg.c	1970-01-01 01:00:00.000000000 +0100
+++ linux/drivers/otg/otgcore/otg.c	2006-09-01 21:41:35.000000000 +0200
@@ -0,0 +1,936 @@
+/*
+ * otg/otgcore/otg.c - OTG state machine
+ *
+ *      Copyright (c) 2004 Belcarra
+ *
+ * By: 
+ *      Stuart Lynne <sl@lbelcarra.com>, 
+ *      Tom Rushworth <tbr@belcarra.com>, 
+ *      Bruce Balden <balden@belcarra.com>
+ *
+ */
+/*!
+ * @file otg/otgcore/otg.c
+ * @brief OTG Core State Machine Event Processing
+ *
+ * The OTG State Machine receives "input" information passed to it
+ * from the other drivers (pcd, tcd, hcd and ocd) that describe what
+ * is happening.
+ *
+ * The State Machine uses the inputs to move from state to state. Each
+ * state defines four things:
+ *
+ * 1. Reset - what inputs to set or reset on entry to the state.
+ *
+ * 2. Outputs - what output functions to call to set or reset.
+ *
+ * 3. Timeout - an optional timeout value to set
+ *
+ * 4. Tests - a series of tests that allow the state machine to move to
+ * new states based on current or new inputs.
+ *
+ * @ingroup OTGCore
+ */
+#ifndef OTG_REGRESS
+
+#include <otg/otg-compat.h>
+#include <otg/otg-module.h>
+
+#include <otg/usbp-chap9.h>
+#include <otg/usbp-func.h>
+#include <otg/usbp-bus.h>
+
+#include <otg/otg-trace.h>
+#include <otg/otg-api.h>
+#include <otg/otg-tcd.h>
+#include <otg/otg-hcd.h>
+#include <otg/otg-pcd.h>
+#include <otg/otg-ocd.h>
+
+
+struct hcd_ops *otg_hcd_ops;
+struct ocd_ops *otg_ocd_ops;
+struct pcd_ops *otg_pcd_ops;
+struct tcd_ops *otg_tcd_ops;
+struct usbd_ops *otg_usbd_ops;
+
+extern struct otg_instance otg_instance_private;
+extern struct hcd_instance hcd_instance_private;
+extern struct ocd_instance ocd_instance_private;
+extern struct pcd_instance pcd_instance_private;
+extern struct tcd_instance tcd_instance_private;
+
+#if 0
+/* otg_vbus - return vbus status
+ *
+ * XXX Should we default to set or reset if there is no vbus function
+ */
+int otg_vbus(struct otg_instance *otg)
+{
+        TRACE_MSG2(CORE, "otg_tcd_ops: %p vbus: %p", otg_tcd_ops, otg_tcd_ops ? otg_tcd_ops->vbus : NULL);
+        return (otg_tcd_ops && otg_tcd_ops->vbus) ? otg_tcd_ops->vbus(otg) : 1;
+}
+#endif
+
+#if defined (OTG_WINCE)
+#define TRACE_SM_CURRENT(t, m, o) \
+				
+#else
+#define TRACE_SM_CURRENT(t, m, o) \
+                TRACE_MSG5(t, "SM_CURRENT: RESET: %08x SET: %08x %s (%s) %s",\
+                                (u32)(~o->current_inputs), \
+                                (u32)(o->current_inputs), \
+                                o->current_outputs->name, \
+                                o->previous_outputs->name, \
+                                m \
+                          );
+#endif //OTG_WINCE
+#define TRACE_SM_CHANGE(t, m, o) \
+                TRACE_MSG5(t, "SM_CHANGE:  RESET: %08x SET: %08x %s (%s) %s",\
+                                (u32)(~o->current_inputs), \
+                                (u32)(o->current_inputs), \
+                                o->current_outputs->name, \
+                                o->previous_outputs->name, \
+                                m \
+                          );
+
+#define TRACE_SM_NEW(t, m, o) \
+                TRACE_MSG5(t, "SM_NEW:     RESET: %08x SET: %08x %s (%s) %s",\
+                                (u32)(~o->current_inputs), \
+                                (u32)(o->current_inputs), \
+                                o->current_outputs->name, \
+                                o->previous_outputs->name, \
+                                m \
+                          );
+
+
+#define TRACE_SM_INPUTS(t, m, u, c) \
+                TRACE_MSG4(t, "SM_INPUTS:  RESET: %08x SET: %08x CUR: %08x %s",\
+                                (u32)(u >> 32), \
+                                (u32)(u & 0xffffffff), \
+                                c, \
+                                m \
+                                )
+
+#endif
+
+
+
+DECLARE_MUTEX (otg_sem);
+
+/*!
+ * Output change lookup table - this maps the current output and the desired
+ * output to what should be done. This is just so that we don't redo changes,
+ * if the output does not change we do not want to re-call the output function
+ */
+u8 otg_output_lookup[4][4] = {
+      /*  NC       SET      RESET    PULSE, <--- current/  new     */
+        { NC,      NC,      NC,      PULSE,  },         /* NC      */ 
+        { SET,     NC,      SET,     PULSE,  },         /* SET     */ 
+        { RESET,   RESET,   NC,      PULSE,  },         /* RESET   */ 
+        { PULSE,   PULSE,   PULSE,   PULSE,  },         /* UNKNOWN */ 
+};
+
+
+/*!
+ * otg_new() - check OTG new inputs and select new state to be in
+ *
+ * This is used by the OTG event handler to see if the state should
+ * change based on the current input values.
+ *
+ * @param otg pointer to the otg instance.
+ * @return either the next OTG state or invalid_state if no change.
+ */
+static int otg_new(struct otg_instance *otg)
+{
+        int state = otg->state;
+        struct otg_test *otg_test = otg_firmware_loaded ? otg_firmware_loaded->otg_tests : NULL;
+        u64 input_mask = ((u64)otg->current_inputs) | ((u64)(~otg->current_inputs) << 32);   // get a copy of current inputs
+
+        UNLESS(otg_test) return invalid_state;
+
+        TRACE_MSG3(CORE, "OTG_NEW: %s inputs: %08x %08x", otg_get_state_name(state),
+                        (u32)(input_mask>>32&0xffffffff), (u32)(input_mask&0xffffffff));
+
+        /* iterate across the otg inputs table, for each entry that matches the current
+         * state, test the input_mask to see if a state transition is required.
+         */
+        for (; otg_test->target != invalid_state; otg_test++) {
+
+                u64 test1 = otg_test->test1;
+                u64 test2 = otg_test->test2;
+                u64 test3 = otg_test->test3;
+
+                CONTINUE_UNLESS(otg_test->state == state);     // skip states that don't match
+
+
+                TRACE_MSG7(CORE, "OTG_NEW: %s (%s, %d) test1: %08x %08x test2 %08x %08x",
+                                otg_get_state_name(otg_test->state),
+                                otg_get_state_name(otg_test->target),
+                                otg_test->test,
+                                (u32)(test1>>32&0xffffffff), (u32)(test1&0xffffffff),
+                                (u32)(test2>>32&0xffffffff), (u32)(test2&0xffffffff)
+                                );
+
+                /* the otg inputs table has multiple masks that define between multiple tests. Each
+                 * test is a simple OR to check if specific inputs are present.
+                 */
+                CONTINUE_UNLESS (
+                                (!otg_test->test1 || ((test1 = otg_test->test1 & input_mask))) &&
+                                (!otg_test->test2 || ((test2 = otg_test->test2 & input_mask))) &&
+                                (!otg_test->test3 || ((test3 = otg_test->test3 & input_mask))) /*&&
+                                (!otg_test->test4 || (otg_test->test4 & input_mask)) */ );
+
+                TRACE_MSG7(CORE, "OTG_NEW: GOTO %s test1: %08x %08x test2 %08x %08x test3 %08x %08x",
+                                otg_get_state_name(otg_test->target),
+                                (u32)(test1>>32&0xffffffff), (u32)(test1&0xffffffff),
+                                (u32)(test2>>32&0xffffffff), (u32)(test2&0xffffffff),
+                                (u32)(test3>>32&0xffff), (u32)(test3&0xffff)
+                                );
+
+                return otg_test->target;
+        }
+        return invalid_state;
+}
+
+char mbuf[96];
+
+/*!
+ * otg_write_state_message_irq() -
+ *
+ * Send a message to the otg management application.
+ *
+ * @param msg
+ * @param reset
+ * @param inputs
+ */
+void otg_write_state_message_irq(char *msg, u64 reset, u32 inputs)
+{
+        sprintf(mbuf, "State: %s reset: %08x set: %08x inputs: %08x ", msg, (u32)(reset>>32), (u32)(reset&0xffffffff), inputs);
+        mbuf[95] = '\0';
+        otg_message(mbuf);
+}
+
+/*!
+ * otg_write_output_message_irq() -
+ *
+ * Send a message to the otg management application.
+ *
+ * @param msg
+ * @param val
+ */
+void otg_write_output_message_irq(char *msg, int val)
+{
+        strcpy(mbuf, "Output: ");
+        strncat(mbuf, msg, 64 - strlen(mbuf));
+        if (val == 2) strncat(mbuf, "/", 64 - strlen(mbuf));
+        mbuf[95] = '\0';
+        otg_message(mbuf);
+}
+
+/*!
+ * otg_write_timer_message_irq() -
+ *
+ * Send a message to the otg management application.
+ *
+ * @param msg
+ * @param val
+ */
+void otg_write_timer_message_irq(char *msg, int val)
+{
+        if (val < 10000)
+                sprintf(mbuf, "Timer: %s %d uS %u", msg, val, otg_tmr_ticks());
+        else if (val < 10000000)
+                //sprintf(time_buf, " %d mS\n", ticks / 1000);
+                sprintf(mbuf, "Timer: %s %d mS %u", msg, val >> 10, otg_tmr_ticks());
+        else
+                //sprintf(time_buf, " %d S\n", ticks / 1000000);
+                sprintf(mbuf, "Timer: %s %d S %u", msg, val >> 20, otg_tmr_ticks());
+
+        mbuf[95] = '\0';
+        otg_message(mbuf);
+}
+
+/*!
+ * otg_write_info_message_irq() -
+ *
+ * Send a message to the otg management application.
+ *
+ * @param msg
+ */
+void otg_write_info_message(char *msg)
+{
+        unsigned long flags;
+        local_irq_save (flags);
+        sprintf(mbuf, "Info: %s", msg);
+        mbuf[95] = '\0';
+        otg_message(mbuf);
+        local_irq_restore (flags);
+}
+
+/*!
+ * otg_write_reset_message_irq() -
+ *
+ * Send a message to the otg management application.
+ *
+ * @param msg
+ * @param val
+ */
+void otg_write_reset_message(char *msg, int val)
+{
+        unsigned long flags;
+        local_irq_save (flags);
+        sprintf(mbuf, "State: %s reset: %x", msg, val);
+        mbuf[95] = '\0';
+        otg_message(mbuf);
+        local_irq_restore (flags);
+}
+
+OTG_EXPORT_SYMBOL(otg_write_info_message);
+
+char otg_change_names[4] = {
+        '_', ' ', '/', '#',
+};
+
+/*!
+ * otg_do_event_irq() - process OTG events and determine OTG outputs to reflect new state
+ *
+ * This function is passed a mask with changed input values. This
+ * is passed to the otg_new() function to see if the state should
+ * change. If it changes then the output functions required for
+ * the new state are called.
+ *
+ * @param otg pointer to the otg instance.
+ * @param inputs input mask
+ * @param tag trace tag to use for tracing
+ * @param msg message for tracing
+ * @return non-zero if state changed.
+ */
+static int otg_do_event_irq(struct otg_instance *otg, u64 inputs, otg_tag_t tag, char *msg)
+{
+        u32 current_inputs = otg->current_inputs;
+        int current_state = otg->state;
+        u32 inputs_set = (u32)(inputs & 0xffffffff);           // get changed inputs that SET something
+        u32 inputs_reset = (u32) (inputs >> 32);                // get changed inputs that RESET something
+        int target;
+
+        RETURN_ZERO_UNLESS(otg && inputs);;
+        TRACE_SM_INPUTS(tag, msg, inputs, current_inputs);
+
+        /* Special Overrides - These are special tests that satisfy specific injunctions from the
+         * OTG Specification.
+         */
+        if (inputs_set & inputs_reset) {                // verify that there is no overlap between SET and RESET masks
+                TRACE_MSG1(CORE, "OTG_EVENT: ERROR attempting to set and reset the same input: %08x", inputs_set & inputs_reset);
+                return 0;
+        }
+        /* don't allow bus_req and bus_drop to be set at the same time
+         */
+        if ((inputs_set & (bus_req | bus_drop)) == (bus_req | bus_drop) ) {
+                TRACE_MSG2(CORE, "OTG_EVENT: ERROR attempting to set both bus_req and bus_drop: %08x %08x",
+                                inputs_set, (bus_req | bus_drop));
+                return 0;
+        }
+        /* set bus_drop_ if bus_req, set bus_req_ if bus_drop
+         */
+        if (inputs_set & bus_req) {
+                inputs |= bus_drop_;
+                TRACE_MSG1(CORE, "OTG_EVENT: forcing bus_drop/: %08x", inputs_set);
+        }
+        if (inputs_set & bus_drop) {
+                inputs |= bus_req_;
+                TRACE_MSG1(CORE, "OTG_EVENT: forcing bus_req/: %08x", inputs_set);
+        }
+
+        otg->current_inputs &= ~inputs_reset;
+        otg->current_inputs |= inputs_set;
+        TRACE_MSG3(CORE, "OTG_EVENT: reset: %08x set: %08x inputs: %08x", inputs_reset, inputs_set, otg->current_inputs);
+
+        TRACE_SM_CHANGE(tag, msg, otg);
+
+        RETURN_ZERO_IF(otg->active);
+
+        otg->active++;
+
+        /* Search the state input change table to see if we need to change to a new state.
+         * This may take several iterations.
+         */
+        while (invalid_state != (target = otg_new(otg))) {
+
+                struct otg_state *otg_state;
+                int original_state = otg->state;
+                u64 current_outputs;
+                u64 output_results;
+                u64 new_outputs;
+                int i;
+
+                /* if previous output started timer, then cancel timer before proceeding
+                 */
+                if ((otg_state = otg->current_outputs) && otg_state->tmout && otg->start_timer) {
+                        TRACE_MSG0(CORE, "reseting timer");
+                        otg->start_timer(otg, 0);
+                }
+
+                BREAK_UNLESS(otg_firmware_loaded);
+
+                BREAK_UNLESS(target < otg_firmware_loaded->number_of_states);
+
+                otg_state = otg_firmware_loaded->otg_states + target;
+
+                BREAK_UNLESS(otg_state->name);
+
+                otg->previous = otg->state;
+                otg->state = target;
+                otg->tickcount = otg_tmr_ticks();
+
+
+                otg->previous_outputs= otg->current_outputs;
+                //otg->current_outputs = NULL;
+
+
+                /* A matching input table rule has been found, we are transitioning to a new
+                 * state.  We need to find the new state entry in the output table.
+                 * XXX this could be a table lookup instead of linear search.
+                 */
+
+                current_outputs = otg->outputs;
+                new_outputs = otg_state->outputs;
+                output_results = 0;
+
+                TRACE_SM_NEW(tag, msg, otg);
+
+                /* reset any inputs that the new state want's reset
+                 */
+                otg_do_event_irq(otg, otg_state->reset | TMOUT_, tag, otg_state->name);
+
+                otg_write_state_message_irq(otg_state->name, otg_state->reset, otg->current_inputs);
+
+#if 1
+                switch (target) {                            /* C.f. 6.6.1.12 b_conn reset */
+                case otg_disabled:
+                        otg->current_inputs = 0;
+                        otg->outputs = 0;
+                        break;
+                default:
+                        //otg_event_irq(otg, not(b_conn), "a_host/ & a_suspend/: set b_conn/");
+                        break;
+                }
+#endif
+                TRACE_MSG5(CORE, "OTG_EVENT: CALLING OUTPUT MAX:%d OUTPUTS CURRENT: %08x %08x NEW: %08x %08x",
+                                MAX_OUTPUTS,
+                                (u32)(current_outputs >> 32),
+                                (u32)(current_outputs & 0xffffffff),
+                                (u32)(new_outputs >> 32),
+                                (u32)(new_outputs & 0xffffffff)
+                          );
+
+                /* Iterate across the outputs bitmask, calling the appropriate functions
+                 * to make required changes. The current outputs are saved and we DO NOT
+                 * make calls to change output values until they change again.
+                 */
+                for (i = 0; i < MAX_OUTPUTS; i++) {
+
+                        u8 current_output = (u8)(current_outputs & 0x3);
+                        u8 new_output = (u8)(new_outputs & 0x3);
+                        u8 changed = otg_output_lookup[new_output][current_output];
+
+                        output_results >>= 2;
+
+                        switch (changed) {
+                        case NC:
+                                output_results |= ((current_outputs & 0x3) << (MAX_OUTPUTS * 2));
+                                break;
+                        case SET:
+                        case RESET:
+                                output_results |= (((u64)changed) << (MAX_OUTPUTS * 2));
+                        case PULSE:
+
+
+                                otg_write_output_message_irq(otg_output_names[i], changed);
+
+                                //TRACE_MSG3(CORE, "OTG_EVENT: CHECKING OUTPUT %s %d %s",
+                                //                otg_state->name, i, otg_output_names[i]);
+
+                                BREAK_UNLESS(otg->otg_output_ops[i]); // Check if we have an output routine
+
+                                TRACE_MSG7(CORE, "OTG_EVENT: CALLING OUTPUT %s %d %s%c cur: %02x new: %02x chng: %02x",
+                                                otg_state->name, i, otg_output_names[i],
+                                                otg_change_names[changed&0x3],
+                                                current_output, new_output,
+                                                changed);
+
+                                otg->otg_output_ops[i](otg, changed); // Output changed, call function to do it
+
+                                //TRACE_MSG2(CORE, "OTG_EVENT: OUTPUT FINISED %s %s", otg_state->name, otg_output_names[i]);
+                                break;
+                        }
+
+                        current_outputs >>= 2;
+                        new_outputs >>= 2;
+                }
+
+                output_results >>= 2;
+                otg->outputs = output_results;
+                otg->current_outputs = otg_state;
+
+                //TRACE_MSG2(CORE, "OTG_EVENT: STATE: otg->current_outputs: %08x %08x",
+                //                (u32)(otg->outputs & 0xffffffff), (u32)(otg->outputs >> 32));
+
+                if (((otg->outputs & pcd_en_out_set) == pcd_en_out_set) &&
+                                        ((otg->outputs & hcd_en_out_set) == hcd_en_out_set))
+                {
+                        TRACE_MSG0(CORE, "WARNING PCD_EN and HCD_EN both set");
+                        printk(KERN_INFO "%s: WARNING PCD_EN and HCD_EN both set\n", __FUNCTION__);
+                }
+
+                /* start timer?
+                 */
+                CONTINUE_UNLESS(otg_state->tmout);
+                TRACE_MSG1(CORE, "setting timer: %d", otg_state->tmout);
+                if (otg->start_timer) {
+                        otg_write_timer_message_irq(otg_state->name, otg_state->tmout);
+                        otg->start_timer(otg, otg_state->tmout);
+                }
+                else {
+                        //TRACE_MSG0(CORE, "NO TIMER");
+                        otg_do_event_irq(otg, TMOUT_, CORE, otg_state->name);
+                }
+
+        }
+        //TRACE_MSG0(CORE, "finishing");
+        otg->active = 0;
+
+        if (current_state == otg->state)
+                TRACE_MSG0(CORE, "OTG_EVENT: NOT CHANGED");
+
+        otg->active = 0;
+        return 0;
+}
+
+/*!
+ * otg_write_input_message_irq() -
+
+ * @param inputs input mask
+ * @param tag trace tag to use for tracing
+ * @param msg message for tracing
+ */
+void otg_write_input_message_irq(u64 inputs, otg_tag_t tag, char *msg)
+{
+        u32 reset = (u32)(inputs >> 32);
+        u32 set = (u32) (inputs & 0xffffffff);
+
+        char buf[64];
+        //snprintf(buf, sizeof(buf), "Input: %08x %08x %s", *inputs >> 32, *inputs & 0xffffffff, msg ? msg : "NULL");
+        TRACE_MSG3(tag, "Inputs: %08x %08x %s", set, reset, msg ? msg : "NULL");
+        #ifdef OTG_WINCE
+        sprintf(buf, "Inputs: %08x %08x %s", reset, set, msg ? msg : "NULL");
+        #else /* OTG_WINCE */
+        snprintf(buf, sizeof(buf), "Inputs: %08x %08x %s", reset, set, msg ? msg : "NULL");
+        #endif /* OTG_WINCE */
+        buf[63] = '\0';
+        otg_message(buf);
+}
+
+
+/*!
+ * otg_event_input_message_irq() -
+ * @param otg pointer to the otg instance.
+ * @param inputs input mask
+ * @param tag trace tag to use for tracing
+ * @param msg message for tracing
+ * @return non-zero if state changed.
+ */
+int otg_event_irq(struct otg_instance *otg, u64 inputs, otg_tag_t tag, char *msg)
+{
+        TRACE_SM_INPUTS(tag, msg, inputs, otg->current_inputs);
+        //otg_write_input_message_irq((u32)(inputs >> 32), (u32) (inputs & 0xffffffff), tag, msg);
+        otg_write_input_message_irq(inputs, tag, msg);
+        return otg_do_event_irq(otg, inputs, tag, msg);
+}
+
+/*!
+ * otg_event() -
+ * @param otg pointer to the otg instance.
+ * @param inputs input mask
+ * @param tag trace tag to use for tracing
+ * @param msg message for tracing
+ * @return non-zero if state changed.
+ */
+int otg_event(struct otg_instance *otg, u64 inputs, otg_tag_t tag, char *msg)
+{
+        int rc; 
+        unsigned long flags;
+        RETURN_ZERO_UNLESS(otg);
+        local_irq_save (flags);
+        rc = otg_event_irq(otg, inputs, tag, msg);
+        local_irq_restore (flags);
+        return rc;
+}
+
+
+/*!
+ * otg_event_set_irq() -
+ * @param otg pointer to the otg instance.
+ * @param changed
+ * @param flag
+ * @param input input mask
+ * @param tag trace tag to use for tracing
+ * @param msg message for tracing
+ * @return non-zero if state changed.
+ */
+int otg_event_set_irq(struct otg_instance *otg, int changed, int flag, u32 input, otg_tag_t tag, char *msg)
+{
+        RETURN_ZERO_UNLESS(changed);
+        TRACE_MSG4(tag, "%s: %08x changed: %d flag: %d", msg, input, changed, flag);
+        return otg_event(otg, flag ? input : _NOT(input), tag, msg);
+}
+
+
+/*!
+ * otg_init() - create and initialize otg instance
+ *
+ * Called to initialize the OTG state machine. This will cause the state
+ * to change from the invalid_state to otg_disabled.
+ *
+ * If the device has the OCD_CAPABILITIES_AUTO flag set then
+ * an initial enable_otg event is generated. This will cause
+ * the state to move from otg_disabled to otg_enabled. The
+ * requird drivers will be initialized by calling the appropriate
+ * output functions:
+ *
+ *      - pcd_init_func
+ *      - hcd_init_func
+ *      - tcd_init_func
+ *
+ * @param otg pointer to the otg instance.
+ */
+void otg_init (struct otg_instance *otg)
+{
+        DOWN(&otg_sem);
+        otg->outputs = tcd_init_out_ | pcd_init_out_ | hcd_init_out_;
+
+        /* This will move the state machine into the otg_disabled state
+         */
+        otg_event(otg, enable_otg, CORE, "enable_otg"); // XXX
+
+        if (otg_ocd_ops->capabilities & OCD_CAPABILITIES_AUTO)
+                otg_event(otg, AUTO, CORE, "AUTO"); // XXX
+
+        UP(&otg_sem);
+}
+
+
+/*!
+ * otg_exit()
+ *
+ * This is called by the driver that started the state machine to
+ * cause it to exit. The state will move to otg_disabled.
+ *
+ * The appropriate output functions will be called to disable the
+ * peripheral drivers.
+ *
+ * @param otg pointer to the otg instance.
+ */
+void otg_exit (struct otg_instance *otg)
+{
+        //TRACE_MSG0(CORE, "DOWN OTG_SEM");
+        DOWN(&otg_sem);
+
+        //TRACE_MSG0(CORE, "OTG_EXIT");
+        //otg_event(otg, exit_all | not(a_bus_req) | a_bus_drop | not(b_bus_req), "tcd_otg_exit");
+
+        otg_event(otg, enable_otg_, CORE, "enable_otg_");
+
+        while (otg->state != otg_disabled) {
+                // TRACE_MSG pointless here, module will be gon before we can look at it.
+                #ifdef OTG_WINCE
+                //DEBUGMSG(KERN_ERR"%s: waiting for state machine to disable\n", __FUNCTION__);
+                #else /* OTG_WINCE */
+                printk(KERN_ERR"%s: waiting for state machine to disable\n", __FUNCTION__);
+                #endif /* OTG_WINCE */
+                SCHEDULE_TIMEOUT(10 * HZ);
+        }
+        //TRACE_MSG0(CORE, "UP OTG_SEM");
+        UP(&otg_sem);
+}
+
+
+OTG_EXPORT_SYMBOL(otg_event);
+OTG_EXPORT_SYMBOL(otg_event_irq);
+OTG_EXPORT_SYMBOL(otg_init);
+OTG_EXPORT_SYMBOL(otg_exit);
+OTG_EXPORT_SYMBOL(otg_event_set_irq);
+
+/* ********************************************************************************************* */
+
+/*!
+ * otg_tcd_init_func() -
+ *
+ * This is a default tcd_init_func. It will be used
+ * if no other is provided.
+ *
+ * @param otg pointer to the otg instance.
+ * @param flag set or reset
+ */
+void otg_tcd_init_func (struct otg_instance *otg, u8 flag)
+{
+        otg_event(otg, TCD_OK, CORE, "TCD_OK");
+}
+
+/*!
+ * otg_pcd_init_func() -
+ *
+ * This is a default pcd_init_func. It will be used
+ * if no other is provided.
+ *
+ * @param otg pointer to the otg instance.
+ * @param flag set or reset
+ */
+void otg_pcd_init_func (struct otg_instance *otg, u8 flag)
+{
+        otg_event(otg, PCD_OK, CORE, "PCD_OK");
+}
+
+/*!
+ * otg_hcd_init_func() -
+ *
+ * This is a default hcd_init_func. It will be used
+ * if no other is provided.
+ *
+ * @param otg pointer to the otg instance.
+ * @param flag set or reset
+ */
+void otg_hcd_init_func (struct otg_instance *otg, u8 flag)
+{
+        otg_event(otg, HCD_OK, CORE, "HCD_OK");
+}
+
+/*!
+ * otg_ocd_init_func() -
+ *
+ * This is a default hcd_init_func. It will be used
+ * if no other is provided.
+ *
+ * @param otg pointer to the otg instance.
+ * @param flag set or reset
+ */
+void otg_ocd_init_func (struct otg_instance *otg, u8 flag)
+{
+        otg_event(otg, HCD_OK, CORE, "OCD_OK");
+}
+
+/*!
+ * otg_hcd_set_ops() -
+ * @param hcd_ops - host operations table to use
+ */
+struct hcd_instance * otg_set_hcd_ops(struct hcd_ops *hcd_ops)
+{
+        otg_hcd_ops = hcd_ops;
+        if (hcd_ops) {
+                otg_instance_private.otg_output_ops[HCD_INIT_OUT] = otg_hcd_ops->hcd_init_func;
+                otg_instance_private.otg_output_ops[HCD_EN_OUT] = otg_hcd_ops->hcd_en_func;
+                otg_instance_private.otg_output_ops[LOC_SOF_OUT] = otg_hcd_ops->loc_sof_func;
+                otg_instance_private.otg_output_ops[LOC_SUSPEND_OUT] = otg_hcd_ops->loc_suspend_func;
+                otg_instance_private.otg_output_ops[REMOTE_WAKEUP_EN_OUT] = otg_hcd_ops->remote_wakeup_en_func;
+                otg_instance_private.otg_output_ops[HNP_EN_OUT] = otg_hcd_ops->hnp_en_func;
+        }
+        else
+                otg_instance_private.otg_output_ops[HCD_INIT_OUT] =
+                otg_instance_private.otg_output_ops[HCD_EN_OUT] =
+                otg_instance_private.otg_output_ops[LOC_SOF_OUT] =
+                otg_instance_private.otg_output_ops[LOC_SUSPEND_OUT] =
+                otg_instance_private.otg_output_ops[REMOTE_WAKEUP_EN_OUT] =
+                otg_instance_private.otg_output_ops[HNP_EN_OUT] = NULL;
+
+
+        UNLESS (otg_instance_private.otg_output_ops[HCD_INIT_OUT])
+                otg_instance_private.otg_output_ops[HCD_INIT_OUT] = otg_hcd_init_func;
+
+        return &hcd_instance_private;
+}
+
+/*!
+ * otg_ocd_set_ops() -
+ *
+ * @param ocd_ops - ocd operations table to use
+ */
+struct ocd_instance * otg_set_ocd_ops(struct ocd_ops *ocd_ops)
+{
+        otg_ocd_ops = ocd_ops;
+        if (ocd_ops) {
+                otg_instance_private.otg_output_ops[OCD_INIT_OUT] = otg_ocd_ops->ocd_init_func;
+                otg_instance_private.start_timer = ocd_ops->start_timer;
+        }
+        else {
+                otg_instance_private.otg_output_ops[OCD_INIT_OUT] = NULL;
+                otg_instance_private.start_timer = NULL;
+        }
+
+        UNLESS (otg_instance_private.otg_output_ops[OCD_INIT_OUT])
+                otg_instance_private.otg_output_ops[OCD_INIT_OUT] = otg_ocd_init_func;
+
+        return &ocd_instance_private;
+}
+
+/*!
+ * otg_pcd_set_ops() -
+ * @param pcd_ops - pcd operations table to use
+ */
+struct pcd_instance * otg_set_pcd_ops(struct pcd_ops *pcd_ops)
+{
+        otg_pcd_ops = pcd_ops;
+        if (pcd_ops) {
+                otg_instance_private.otg_output_ops[PCD_INIT_OUT] = otg_pcd_ops->pcd_init_func;
+                otg_instance_private.otg_output_ops[PCD_EN_OUT] = otg_pcd_ops->pcd_en_func;
+                otg_instance_private.otg_output_ops[REMOTE_WAKEUP_OUT] = otg_pcd_ops->remote_wakeup_func;
+        }
+        else
+                otg_instance_private.otg_output_ops[PCD_INIT_OUT] =
+                otg_instance_private.otg_output_ops[PCD_EN_OUT] =
+                otg_instance_private.otg_output_ops[REMOTE_WAKEUP_OUT] = NULL;
+
+        UNLESS (otg_instance_private.otg_output_ops[PCD_INIT_OUT])
+                otg_instance_private.otg_output_ops[PCD_INIT_OUT] = otg_pcd_init_func;
+
+        return &pcd_instance_private;
+}
+
+/*!
+ * otg_tcd_set_ops() -
+ * @param tcd_ops - tcd operations table to use
+ */
+struct tcd_instance * otg_set_tcd_ops(struct tcd_ops *tcd_ops)
+{
+        otg_tcd_ops = tcd_ops;
+        if (tcd_ops) {
+                otg_instance_private.otg_output_ops[TCD_INIT_OUT] = otg_tcd_ops->tcd_init_func;
+                otg_instance_private.otg_output_ops[TCD_EN_OUT] = otg_tcd_ops->tcd_en_func;
+                otg_instance_private.otg_output_ops[CHRG_VBUS_OUT] = otg_tcd_ops->chrg_vbus_func;
+                otg_instance_private.otg_output_ops[DRV_VBUS_OUT] = otg_tcd_ops->drv_vbus_func;
+                otg_instance_private.otg_output_ops[DISCHRG_VBUS_OUT] = otg_tcd_ops->dischrg_vbus_func;
+                otg_instance_private.otg_output_ops[DP_PULLUP_OUT] = otg_tcd_ops->dp_pullup_func;
+                otg_instance_private.otg_output_ops[DM_PULLUP_OUT] = otg_tcd_ops->dm_pullup_func;
+                otg_instance_private.otg_output_ops[DP_PULLDOWN_OUT] = otg_tcd_ops->dp_pulldown_func;
+                otg_instance_private.otg_output_ops[DM_PULLDOWN_OUT] = otg_tcd_ops->dm_pulldown_func;
+                //otg_instance_private.otg_output_ops[PERIPHERAL_HOST] = otg_tcd_ops->peripheral_host_func;
+                otg_instance_private.otg_output_ops[CLR_OVERCURRENT_OUT] = otg_tcd_ops->overcurrent_func;
+                otg_instance_private.otg_output_ops[DM_DET_OUT] = otg_tcd_ops->dm_det_func;
+                otg_instance_private.otg_output_ops[DP_DET_OUT] = otg_tcd_ops->dp_det_func;
+                otg_instance_private.otg_output_ops[CR_DET_OUT] = otg_tcd_ops->cr_det_func;
+                otg_instance_private.otg_output_ops[AUDIO_OUT] = otg_tcd_ops->audio_func;
+                otg_instance_private.otg_output_ops[CHARGE_PUMP_OUT] = otg_tcd_ops->charge_pump_func;
+                otg_instance_private.otg_output_ops[BDIS_ACON_OUT] = otg_tcd_ops->bdis_acon_func;
+                otg_instance_private.otg_output_ops[MX21_VBUS_DRAIN] = otg_tcd_ops->mx21_vbus_drain_func;
+                otg_instance_private.otg_output_ops[ID_PULLDOWN_OUT] = otg_tcd_ops->id_pulldown_func;
+                otg_instance_private.otg_output_ops[UART_OUT] = otg_tcd_ops->uart_func;
+                otg_instance_private.otg_output_ops[MONO_OUT] = otg_tcd_ops->mono_func;
+        }
+        else 
+                otg_instance_private.otg_output_ops[TCD_INIT_OUT] = 
+                otg_instance_private.otg_output_ops[TCD_EN_OUT] = 
+                otg_instance_private.otg_output_ops[CHRG_VBUS_OUT] = 
+                otg_instance_private.otg_output_ops[DRV_VBUS_OUT] = 
+                otg_instance_private.otg_output_ops[DISCHRG_VBUS_OUT] = 
+                otg_instance_private.otg_output_ops[DP_PULLUP_OUT] = 
+                otg_instance_private.otg_output_ops[DM_PULLUP_OUT] = 
+                otg_instance_private.otg_output_ops[DP_PULLDOWN_OUT] = 
+                otg_instance_private.otg_output_ops[DM_PULLDOWN_OUT] = 
+                //otg_instance_private.otg_output_ops[PERIPHERAL_HOST] = 
+                otg_instance_private.otg_output_ops[CLR_OVERCURRENT_OUT] =
+                otg_instance_private.otg_output_ops[DM_DET_OUT] =
+                otg_instance_private.otg_output_ops[DP_DET_OUT] =
+                otg_instance_private.otg_output_ops[CR_DET_OUT] = 
+                otg_instance_private.otg_output_ops[AUDIO_OUT] =
+                otg_instance_private.otg_output_ops[CHARGE_PUMP_OUT] =
+                otg_instance_private.otg_output_ops[BDIS_ACON_OUT] =
+                otg_instance_private.otg_output_ops[MX21_VBUS_DRAIN] = 
+                otg_instance_private.otg_output_ops[ID_PULLDOWN_OUT] =
+                otg_instance_private.otg_output_ops[UART_OUT] =
+                otg_instance_private.otg_output_ops[MONO_OUT] = NULL;
+
+        UNLESS (otg_instance_private.otg_output_ops[TCD_INIT_OUT])
+                otg_instance_private.otg_output_ops[TCD_INIT_OUT] = otg_tcd_init_func;
+
+        return &tcd_instance_private;
+}
+
+/*!
+ * otg_set_usbd_ops() -
+ * @param usbd_ops
+ */
+int otg_set_usbd_ops(struct usbd_ops *usbd_ops)
+{
+        otg_usbd_ops = usbd_ops;
+        return 0;
+}
+
+
+/*!
+ * otg_get_trace_info()
+ */
+void otg_get_trace_info(otg_trace_t *p)
+{
+        p->id_gnd = otg_instance_private.current_inputs & ID_GND ? 1 : 0;
+        p->va.s.ticks = (otg_ocd_ops && otg_ocd_ops->ticks) ? otg_ocd_ops->ticks () : 0;
+        p->va.s.interrupts = (otg_ocd_ops && otg_ocd_ops->interrupts) ? otg_ocd_ops->interrupts : 0;
+        p->va.s.framenum = (otg_pcd_ops && otg_pcd_ops->framenum) ? otg_pcd_ops->framenum() : 0;
+        p->in_interrupt = in_interrupt();
+}
+
+/*!
+ * otg_tmr_ticks() -
+ * @return number of ticks.
+ */
+u64 otg_tmr_ticks(void)
+{
+        return (otg_ocd_ops && otg_ocd_ops->ticks) ? otg_ocd_ops->ticks () : 0;
+}
+
+/*!
+ * otg_tmr_elapsed() -
+ * @param t1
+ * @param t2
+ * @return number of uSecs between t1 and t2 ticks.
+ */
+u64 otg_tmr_elapsed(u64 *t1, u64 *t2)
+{
+	return (otg_ocd_ops && otg_ocd_ops->elapsed) ? otg_ocd_ops->elapsed (t1, t2) : 0;
+}
+
+/*!
+ * otg_tmr_framenum() -
+ * @return the current USB frame number.
+ */
+u16 otg_tmr_framenum(void)
+{
+	return (otg_pcd_ops && otg_pcd_ops->framenum) ? otg_pcd_ops->framenum() : 0;
+}
+
+/*!
+ * otg_tmr_interrupts() -
+ * @return the current number of interrupts.
+ */
+u32 otg_tmr_interrupts(void)
+{
+	return (otg_ocd_ops && otg_ocd_ops->interrupts) ? otg_ocd_ops->interrupts : 0;
+}
+
+
+OTG_EXPORT_SYMBOL(otg_hcd_ops);
+OTG_EXPORT_SYMBOL(otg_ocd_ops);
+OTG_EXPORT_SYMBOL(otg_pcd_ops);
+OTG_EXPORT_SYMBOL(otg_tcd_ops);
+OTG_EXPORT_SYMBOL(otg_set_hcd_ops);
+OTG_EXPORT_SYMBOL(otg_set_ocd_ops);
+OTG_EXPORT_SYMBOL(otg_set_pcd_ops);
+OTG_EXPORT_SYMBOL(otg_set_tcd_ops);
+OTG_EXPORT_SYMBOL(otg_set_usbd_ops);
+OTG_EXPORT_SYMBOL(otg_tmr_ticks);
+OTG_EXPORT_SYMBOL(otg_tmr_elapsed);
+OTG_EXPORT_SYMBOL(otg_write_input_message_irq);
+
+
+#if defined(OTG_WINCE)
+#else /* defined(OTG_WINCE) */
+#endif /* defined(OTG_WINCE) */
+
diff -uNr linux/drivers/no-otg/otgcore/sources linux/drivers/otg/otgcore/sources
--- linux/drivers/no-otg/otgcore/sources	1970-01-01 01:00:00.000000000 +0100
+++ linux/drivers/otg/otgcore/sources	2006-09-01 21:41:34.000000000 +0200
@@ -0,0 +1,41 @@
+!if 0
+Copyright (c) Microsoft Corporation.  All rights reserved.
+!endif
+!if 0
+Use of this source code is subject to the terms of the Microsoft end-user
+license agreement (EULA) under which you licensed this SOFTWARE PRODUCT.
+If you did not accept the terms of the EULA, you are not authorized to use
+this source code. For a copy of the EULA, please see the LICENSE.RTF on your
+install media.
+!endif
+
+SYNCHRONIZE_DRAIN=1
+
+INCLUDES= \
+		$(_PUBLICROOT)\common\ddk\inc; $(_PUBLICROOT)\common\oak\inc; $(_PUBLICROOT)\common\sdk\inc; \
+		$(_WINCEROOT)\public\common\oak\DRIVERS\otg;
+
+
+TARGETNAME=otgcore
+TARGETTYPE=LIBRARY
+
+
+SOURCES=                \
+        core-init-w42.c \
+        otg.c \
+        otg-mesg.c \
+        otg-mesg-w42.c \
+        otg-ops.c \
+        usbp-bops.c \
+        usbp-fops.c \
+        otg-fw.c \
+        otg-fw-df.c \
+        otg-fw-mn.c \
+        usbp.c \
+        otg-trace.c \
+        otg-trace-w42.c \
+        ep0.c \
+
+#        usbp-procfs.c \
+
+
diff -uNr linux/drivers/no-otg/otgcore/usbp-bops.c linux/drivers/otg/otgcore/usbp-bops.c
--- linux/drivers/no-otg/otgcore/usbp-bops.c	1970-01-01 01:00:00.000000000 +0100
+++ linux/drivers/otg/otgcore/usbp-bops.c	2006-09-01 21:41:35.000000000 +0200
@@ -0,0 +1,1857 @@
+/*
+ * otg/otgcore/usbp-bops.c - USB Device Prototype
+ *
+ *      Copyright (c) 2004-2005 Belcarra
+ *
+ * By:
+ *      Stuart Lynne <sl@belcarra.com>,
+ *      Bruce Balden <balden@belcarra.com>
+ *
+ */
+/*!
+ * @file otg/otgcore/usbp-bops.c
+ * @brief Bus Interface related functions.
+ *
+ * This implements the functions used to implement Peripheral Controller Drivers (PCD.)
+ *
+ * @ingroup USBP
+ */
+
+#include <otg/otg-compat.h>
+#include <otg/otg-module.h>
+
+#include <otg/usbp-chap9.h>
+#include <otg/otg-trace.h>
+#include <otg/otg-api.h>
+#include <otg/otg-trace.h>
+#include <otg/usbp-func.h>
+#include <otg/usbp-bus.h>
+#include <otg/otg-pcd.h>
+
+/* ********************************************************************************************* */
+/*!
+ * Note that all of the list functions overlapped operation using the usbd_bus_sem;
+ *
+ *      usbd_device_bh
+ *
+ *      usbd_register_bus
+ *      usbd_deregister_bus
+ *      usbd_enable_function_irq
+ *      usbd_disable_function
+ */
+DECLARE_MUTEX(usbd_bus_sem);
+void usbd_device_bh (void *data);
+
+/* ********************************************************************************************* */
+/*!
+ * Function driver enable/disable
+ *
+ * Called by usbd_enable_function/usbd_disable_function to call the selected
+ * function drivers function_enable or function_disable function.
+ */
+int usbd_strings_init (void);
+void usbd_strings_exit(void);
+
+extern struct usbd_function_instance *usbd_find_function (char *name);
+
+struct usbd_bus_instance *usbd_bus_instance;
+OTG_EXPORT_SYMBOL(usbd_bus_instance);
+
+/* ********************************************************************************************* */
+/* ********************************************************************************************* */
+/*!
+ * Bus Interface Management:
+ *      usbd_register_bus()
+ *      usbd_deregister_bus()
+ */
+
+/*! urb_link_init() - initialize urb linke
+ *
+ * Initialize an urb_link to be a single element list.
+ * If the urb_link is being used as a distinguished list head
+ * the list is empty when the head is the only link in the list.
+ *
+ * @param ul link to urb
+ */
+static INLINE void urb_link_init (urb_link * ul)
+{
+        ul->prev = ul->next = ul;
+}
+
+static struct usbd_function_operations ep0_ops;
+struct usbd_function_driver ep0_driver;
+        
+
+/*! 
+ * usbd_register_bus() - called by a USB BUS INTERFACE driver to register a bus driver
+ *
+ * Used by a USB Bus interface driver to register itself with the usb device layer.
+ *
+ * @param driver
+ * @param ep0_wMaxPacketSize
+ * @return non-zero if error
+ */
+struct usbd_bus_instance *usbd_register_bus (struct usbd_bus_driver *driver, int ep0_wMaxPacketSize)
+{
+        int i;
+        struct usbd_bus_instance *bus = NULL;
+
+        DOWN(&usbd_bus_sem);
+
+        THROW_IF(usbd_bus_instance, error);
+        THROW_IF((bus = CKMALLOC (sizeof (struct usbd_bus_instance), GFP_ATOMIC)) == NULL, error);
+
+        bus->driver = driver;
+        bus->endpoints = bus->driver->max_endpoints;
+        bus->otg_bmAttributes = bus->driver->otg_bmAttributes;
+
+        THROW_IF(!(bus->endpoint_array = CKMALLOC(sizeof (struct usbd_endpoint_instance) * bus->endpoints, GFP_ATOMIC)), error); 
+
+        for (i = 0; i < bus->endpoints; i++) {
+                struct usbd_endpoint_instance *endpoint = bus->endpoint_array + i;
+                endpoint->physical_endpoint = i;
+                urb_link_init (&endpoint->rdy);
+                urb_link_init (&endpoint->tx);
+        }
+
+        urb_link_init (&bus->finished);
+
+        ZERO(ep0_ops);
+        ZERO(ep0_driver);
+        ep0_driver.name = "EP0";
+        ep0_driver.fops = &ep0_ops;
+        
+
+        THROW_IF((bus->ep0 = CKMALLOC (sizeof (struct usbd_function_instance), GFP_ATOMIC)) == NULL, error);
+        bus->ep0->function_driver = &ep0_driver;
+        THROW_IF(!(bus->ep0->endpoint_map_array = CKMALLOC(sizeof(struct usbd_endpoint_map) * 2, GFP_KERNEL)), error);
+        bus->ep0->endpoint_map_array[0].wMaxPacketSize[0] = ep0_wMaxPacketSize;
+        bus->ep0->endpoint_map_array[0].wMaxPacketSize[1] = ep0_wMaxPacketSize;
+
+        bus->device_state = STATE_CREATED;
+        bus->status = USBD_OPENING;
+
+        usbd_bus_instance = bus;
+
+        CATCH(error) {
+                if (bus) {
+                        if (bus->endpoint_array) 
+                                LKFREE(bus->endpoint_array);
+                        LKFREE(bus);
+                }
+                bus->endpoints = 0;
+                #if defined(OTG_LINUX)
+                printk(KERN_INFO"%s: FAILED\n", __FUNCTION__);
+                #endif /* defined(OTG_LINUX) */
+                #if defined(OTG_WINCE)
+                DEBUGMSG(ZONE_INIT, (_T("usbd_register_bus: FAILED\n")));
+                #endif /* defined(OTG_LINUX) */
+                bus =  NULL;
+        }
+
+        UP(&usbd_bus_sem);
+        return bus;
+}
+
+/*! 
+ * usbd_deregister_bus() - called by a USB BUS INTERFACE driver to deregister a bus driver
+ *
+ * Used by a USB Bus interface driver to de-register itself with the usb device
+ * layer.
+ *
+ * @param bus
+ */
+void usbd_deregister_bus (struct usbd_bus_instance *bus)
+{
+        DOWN(&usbd_bus_sem);
+
+        usbd_bus_instance = NULL;
+
+        if (bus->ep0) {
+                if (bus->ep0->endpoint_map_array)
+                        LKFREE(bus->ep0->endpoint_map_array);
+                LKFREE(bus->ep0);
+        }
+
+        LKFREE (bus->arg);
+        LKFREE (bus->endpoint_array);
+        LKFREE (bus);
+        bus->endpoints = 0;
+        UP(&usbd_bus_sem);
+}
+
+OTG_EXPORT_SYMBOL(usbd_register_bus);
+OTG_EXPORT_SYMBOL(usbd_deregister_bus);
+
+/* ********************************************************************************************* */
+/* ********************************************************************************************* */
+/*!
+ * Function Management:
+ *      usbd_enable_function()
+ *      usbd_disable_function()
+ *
+ */
+
+/*!
+ * alloc_function_alternates() - allocate alternate instance array
+ *
+ * Return a pointer to an array of alternate instances. Each instance contains a pointer
+ * to a filled in alternate descriptor and a pointer to the endpoint descriptor array.
+ *
+ * Returning NULL will cause the caller to cleanup all previously allocated memory.
+ * @param bInterfaceNumber
+ * @param interface_description
+ * @param alternates
+ * @param alternate_description_array
+ * @return pointer to alternate instance array
+ */
+static struct usbd_alternate_instance *
+alloc_function_alternates (int bInterfaceNumber,
+                struct usbd_interface_description *interface_description,
+                int alternates, struct usbd_alternate_description *alternate_description_array)
+{
+        int alternate_num;
+        struct usbd_alternate_instance *alternate_instance_array = NULL;
+
+        // allocate array of alternate instances
+        RETURN_NULL_IF(!(alternate_instance_array = (struct usbd_alternate_instance *)
+                                CKMALLOC (sizeof (struct usbd_alternate_instance) * alternates, GFP_KERNEL)));
+
+        // iterate across the alternate descriptions
+        for (alternate_num = 0; alternate_num < alternates; alternate_num++) {
+
+                struct usbd_alternate_description *alternate_description = alternate_description_array + alternate_num;
+                struct usbd_alternate_instance *alternate_instance = alternate_instance_array + alternate_num;
+                struct usbd_interface_descriptor *interface_descriptor;
+
+                interface_descriptor = alternate_description->interface_descriptor;
+                if (interface_descriptor->bInterfaceNumber != bInterfaceNumber) {
+                        //TRACE_MSG2(USBD, "bInterfaceNumber mis-match: %d should be %d",
+                        //                interface_descriptor->bInterfaceNumber, bInterfaceNumber);
+                        LKFREE(alternate_instance_array);
+                        return NULL;
+                }
+
+                interface_descriptor->bNumEndpoints = alternate_description->endpoints;
+                interface_descriptor->iInterface = usbd_alloc_string (alternate_description->iInterface );
+
+                alternate_instance->class_list = alternate_description->class_list;
+                alternate_instance->classes = alternate_description->classes;
+                alternate_instance->endpoint_list = alternate_description->endpoint_list;
+                alternate_instance->endpoint_indexes = alternate_description->endpoint_indexes;
+
+                // save number of alternates, classes and endpoints for this alternate
+                alternate_instance->endpoints = alternate_description->endpoints;
+                alternate_instance->interface_descriptor = interface_descriptor;
+        }
+        return alternate_instance_array;
+}
+
+/*!
+ * alloc_function_interfaces() - allocate interface instance array
+ *
+ * Return a pointer to an array of interface instances. Each instance contains a pointer
+ * to a filled in interface descriptor and a pointer to the endpoint descriptor array.
+ *
+ * Returning NULL will cause the caller to cleanup all previously allocated memory.
+ *
+ * @param bNumInterfaces
+ * @param interface_description_array
+ * @return interface instance
+ */
+static struct usbd_interfaces_instance *
+alloc_function_interfaces (int bNumInterfaces, struct usbd_interface_description *interface_description_array)
+{
+        int interface_num;
+        struct usbd_interfaces_instance *interfaces_instance_array = NULL;
+
+        // allocate array of interface instances
+        RETURN_NULL_IF(!(interfaces_instance_array = (struct usbd_interfaces_instance *)
+                                CKMALLOC (sizeof (struct usbd_interfaces_instance) * bNumInterfaces, GFP_KERNEL)));
+
+        // iterate across the interface descriptions
+        for (interface_num = 0; interface_num < bNumInterfaces; interface_num++) {
+
+                struct usbd_interface_description *interface_description = interface_description_array + interface_num;
+                struct usbd_interfaces_instance *interfaces_instance = interfaces_instance_array + interface_num;
+
+                interfaces_instance->alternates_instance_array =
+                        alloc_function_alternates (interface_num, interface_description,
+                                        interface_description->alternates, interface_description->alternate_list);
+                interfaces_instance->alternates = interface_description->alternates;
+        }
+        return interfaces_instance_array;
+}
+
+
+/*!
+ * alloc_function_configurations() - allocate configuration instance array
+ *
+ * Return a pointer to an array of configuration instances. Each instance contains a pointer
+ * to a filled in configuration descriptor and a pointer to the interface instances array.
+ *
+ * Returning NULL will cause the caller to cleanup all previously allocated memory.
+ *
+ * @param bNumConfigurations
+ * @param configuration_description_array
+ * @param otg_descriptor
+ * @param remote_wakeup_supported
+ * @param bMaxPower
+ * @return configuration instance
+ */
+static struct usbd_configuration_instance *
+alloc_function_configurations (struct usbd_function_driver *function_driver,
+                int remote_wakeup_supported,
+                int bMaxPower)
+{
+
+        int bNumConfigurations = function_driver->bNumConfigurations;
+        struct usbd_configuration_description *configuration_description_array = function_driver->configuration_description;
+        struct usbd_otg_descriptor *otg_descriptor = function_driver->otg_descriptor;
+        int config_num;
+        struct usbd_configuration_instance *configuration_instance_array = NULL;
+
+        //TRACE_MSG2(USBD, "remote: %x bMaxPower: %x", remote_wakeup_supported, bMaxPower);
+
+        // allocate array
+        RETURN_NULL_IF(!(configuration_instance_array = (struct usbd_configuration_instance *)
+                                CKMALLOC (sizeof (struct usbd_configuration_instance) * bNumConfigurations, GFP_KERNEL)));
+        // fill in array
+        for (config_num = 0; config_num < bNumConfigurations; config_num++) {
+                int interface_num;
+                int length;
+
+                struct usbd_configuration_description *configuration_description = configuration_description_array + config_num;
+                struct usbd_configuration_descriptor *configuration_descriptor;
+
+                configuration_descriptor = configuration_description->configuration_descriptor;
+
+                // setup fields in configuration descriptor
+                // XXX c.f. 9.4.7 zero is default, so config MUST BE 1 to n, not 0 to n-1
+                // XXX N.B. the configuration itself is fetched 0 to n-1.
+
+                configuration_descriptor->bConfigurationValue = config_num + 1;
+                configuration_descriptor->wTotalLength = 0;
+                configuration_descriptor->bNumInterfaces = function_driver->bNumInterfaces;
+                configuration_descriptor->iConfiguration = usbd_alloc_string (configuration_description->iConfiguration);
+                configuration_descriptor->bMaxPower = bMaxPower;
+                configuration_descriptor->bmAttributes = BMATTRIBUTE_RESERVED | 
+                        bMaxPower ? 0 : BMATTRIBUTE_SELF_POWERED | 
+                        remote_wakeup_supported ? BMATTRIBUTE_REMOTE_WAKEUP : 0;
+                
+                // save the configuration descriptor in the configuration instance array
+                configuration_instance_array[config_num].configuration_descriptor = configuration_descriptor;
+
+                RETURN_NULL_IF (!(configuration_instance_array[config_num].interfaces_instance_array = 
+                                        alloc_function_interfaces (function_driver->bNumInterfaces, 
+                                                function_driver->interface_list)));
+
+                length = sizeof(struct usbd_configuration_descriptor);
+
+                for (interface_num = 0; interface_num < configuration_descriptor->bNumInterfaces; interface_num++) {
+
+                        int alternate_num;
+                        struct usbd_interfaces_instance *interfaces_instance = 
+                                configuration_instance_array[config_num].interfaces_instance_array + interface_num;
+
+                        TRACE_MSG2(USBD, "len: %02x:%02d configuration", length, length);
+                        for (alternate_num = 0; alternate_num < interfaces_instance->alternates; alternate_num++) {
+                                int class_num;
+                                int endpoint;
+                                struct usbd_alternate_instance *alternate_instance = 
+                                        interfaces_instance->alternates_instance_array + alternate_num;
+
+                                length += sizeof (struct usbd_interface_descriptor);
+
+                                TRACE_MSG2(USBD, "len: %02x:%02d interface", length, length);
+                                for (class_num = 0; class_num < alternate_instance->classes; class_num++) {
+                                        struct usbd_generic_class_descriptor * class_descriptor = 
+                                                *(alternate_instance->class_list + class_num);
+                                        length += class_descriptor->bLength;
+                                        TRACE_MSG2(USBD, "len: %02x:%02d class_num", length, length);
+                                }
+
+                                for (endpoint = 0; endpoint < alternate_instance->endpoints; endpoint++) {
+                                        struct usbd_endpoint_descriptor * endpoint_descriptor = 
+                                                *(alternate_instance->endpoint_list + endpoint);
+                                        length += endpoint_descriptor->bLength;
+                                        TRACE_MSG2(USBD, "len: %02x:%02d endpoint", length, length);
+                                }
+                        }
+                }
+                length += otg_descriptor ? sizeof(struct usbd_otg_descriptor) : 0;
+                configuration_descriptor->wTotalLength = cpu_to_le16 ((u16)length);
+                // XXX only support a single configuration currently
+                configuration_instance_array[config_num].bNumInterfaces = function_driver->bNumInterfaces;
+                TRACE_MSG3(USBD, "configuration_instance_array[%d]: %p wTotalLength: %d", config_num, 
+                                &configuration_instance_array[config_num], length);
+        }
+        return configuration_instance_array;
+}
+
+
+/*!
+ * bus_function_enable() - enable a usbd function driver for use
+ *
+ * This will copy descriptors from the function driver and get them ready
+ * for use.
+ * @param bus
+ * @param function
+ * @param serial_number
+ * @return non-zero if error
+ */
+static int 
+bus_function_enable (struct usbd_bus_instance *bus, struct usbd_function_instance *function, char *serial_number)
+{
+        struct usbd_function_driver *function_driver = function->function_driver;
+        struct usbd_device_description *device_description;
+        struct usbd_device_descriptor *device_descriptor;
+#ifdef CONFIG_OTG_HIGH_SPEED
+        struct usbd_device_qualifier_descriptor *device_qualifier_descriptor;
+#endif /* CONFIG_OTG_HIGH_SPEED */
+        struct usbd_otg_descriptor *otg_descriptor;
+        int rc = 0;
+
+        function->bus = bus;
+
+        RETURN_EINVAL_IF(function_driver->fops->function_enable && function_driver->fops->function_enable (function));
+        RETURN_ZERO_IF(!(device_description = function_driver->device_description));
+        RETURN_ZERO_IF(!(device_descriptor = device_description->device_descriptor));
+
+        device_descriptor->bMaxPacketSize0 = bus->driver->maxpacketsize;
+        device_descriptor->iManufacturer = usbd_alloc_string (device_description->iManufacturer);
+        device_descriptor->iProduct = usbd_alloc_string (device_description->iProduct);
+
+        if (strlen (serial_number))
+                device_descriptor->iSerialNumber = usbd_alloc_string (serial_number);
+        else
+                device_descriptor->iSerialNumber = usbd_alloc_string (device_description->iSerialNumber);
+
+        device_descriptor->bNumConfigurations = function_driver->bNumConfigurations;
+        device_descriptor->idVendor = function_driver->idVendor;
+        device_descriptor->idProduct = function_driver->idProduct;
+        device_descriptor->bcdDevice = function_driver->bcdDevice;
+
+        if ((otg_descriptor = device_description->otg_descriptor)) {
+                otg_descriptor->bmAttributes = usbd_otg_bmattributes(function);
+                function_driver->otg_descriptor = otg_descriptor;
+        }
+
+        /* call alloc_function_configurations() to allocate the configuration descriptor array
+         */
+        RETURN_EINVAL_IF (!(function_driver->configuration_instance_array =
+                                alloc_function_configurations (function_driver, 
+                                        bus->driver->capabilities & REMOTE_WAKEUP_SUPPORTED,
+                                        bus->driver->bMaxPower
+                                        )));
+
+        function_driver->device_descriptor = device_descriptor;
+#ifdef CONFIG_OTG_HIGH_SPEED
+        TRACE_MSG0(USBD, "HIGH SPEED");
+        if ((device_qualifier_descriptor = device_description->device_qualifier_descriptor)) {
+                device_qualifier_descriptor->bNumConfigurations = function_driver->bNumConfigurations;
+                function_driver->device_qualifier_descriptor = device_qualifier_descriptor;
+        }
+#endif /* CONFIG_OTG_HIGH_SPEED */
+        //TRACE_MSG0(USBD, "finished");
+        return rc;
+}
+
+/*!
+ * usbd_function_disable() - disable a usbd function driver
+ * @param function
+ */
+void usbd_function_disable (struct usbd_function_instance *function)
+{
+        int configuration;
+        struct usbd_function_driver *function_driver = function->function_driver;
+        struct usbd_configuration_instance *configuration_instance_array = function_driver->configuration_instance_array;
+
+        if (function->function_driver->fops->function_disable) 
+                function->function_driver->fops->function_disable (function);
+
+        RETURN_IF(!function_driver->configuration_instance_array);
+
+        // iterate across the descriptors list and de-allocate all structures
+        if (function_driver->configuration_instance_array) {
+                for (configuration = 0; configuration < function_driver->bNumConfigurations; configuration++) {
+                        int interface_num;
+                        struct usbd_configuration_instance *configuration_instance = configuration_instance_array + configuration;
+
+                        for (interface_num = 0; interface_num < configuration_instance->bNumInterfaces; interface_num++) {
+
+                                //int alternate_num;
+                                struct usbd_interfaces_instance *interfaces_instance = 
+                                        configuration_instance->interfaces_instance_array + interface_num;
+
+                                LKFREE (interfaces_instance->alternates_instance_array);
+                        }
+                        LKFREE (configuration_instance->interfaces_instance_array);
+                }
+        }
+        LKFREE (configuration_instance_array);
+        function->bus = NULL;
+}
+
+
+/*! 
+ * usbd_enable_function() - called to enable the desired function
+ *
+ * Used by a USB Bus interface driver to create a virtual device.
+ *
+ * @param bus
+ * @param arg
+ * @param serial_number
+ * @return non-zero if error
+ */
+int usbd_enable_function(struct usbd_bus_instance *bus, char *arg, char *serial_number)
+{
+        struct usbd_function_instance *function = NULL;
+        struct usbd_function_driver *function_driver = NULL;
+        LIST_NODE *lhd = NULL;
+        int len = 0;
+        int i;
+        int rc = -EINVAL;
+        int epn, endpointsRequested;
+        int endpoints;
+
+        DOWN(&usbd_bus_sem);
+
+        if (arg && bus->arg) 
+                LKFREE(bus->arg);
+
+        if (arg) {
+                bus->arg = LSTRDUP(arg);
+                len = strlen(arg);
+        }
+
+        /* initialize the strings pool, and zero endpoint array urbs
+         */
+        THROW_IF(usbd_strings_init (), error);
+
+        for (i = 1; i < bus->endpoints; i++) {
+                struct usbd_endpoint_instance *endpoint = bus->endpoint_array + i;
+                endpoint->rcv_urb = endpoint->tx_urb = NULL;
+        }               
+        
+        /* find requested function
+         */
+        if ((function = usbd_find_function(arg))) {
+
+                function_driver = function->function_driver;
+
+                TRACE_MSG1(USBD, "request endpoints: %x", function_driver);
+
+                switch(function->function_type) {
+                case function_simple:
+                        THROW_IF(bus->driver->bops->request_endpoints( bus, function->endpoint_map_array,
+                                                function->function_driver->endpointsRequested,
+                                                function->function_driver->requestedEndpoints), error);
+                        break;
+                case function_interface:
+                        break;
+                case function_composite:
+                        THROW_IF(bus->driver->bops->request_endpoints( bus, function->endpoint_map_array,
+                                                function->endpoints,
+                                                function->requestedEndpoints), error);
+                        break;
+                }
+
+
+                bus_function_enable (bus, function, serial_number);
+        }
+
+        THROW_IF (!(bus->function_instance = function), error);
+
+
+        /* if composite we need to compose the interface list and endpoint map
+         */
+        if(function_composite == function->function_type) {
+                char *name;
+                int i;
+        }
+
+        /* we need to de-compose the endpoint request array into the individual
+         * interface endpoint map array.
+         */
+        for (endpoints = 0, i = 0; i < function->interfaces; i++) {
+                struct usbd_function_instance *interface_function = function->interfaces_array[i];
+                interface_function->endpoint_map_array = function->endpoint_map_array;
+                endpoints += interface_function->endpoints;
+        }
+
+
+        switch(bus->function_instance->function_type) {
+        case function_simple:
+        case function_composite:
+                THROW_IF(bus->driver->bops->set_endpoints( bus, 
+                                        function->function_driver->endpointsRequested,
+                                        function->endpoint_map_array), error);
+                break;
+        case function_interface:
+                break;
+        }
+
+        // device bottom half
+	PREPARE_WORK_ITEM(bus->device_bh, usbd_device_bh, bus);
+        bus->status = USBD_OK;
+        bus->bus_state = usbd_bus_state_enabled;
+        bus->ep0->endpoint_map_array->endpoint = bus->endpoint_array;
+        THROW_IF (bus_function_enable (bus, bus->ep0, NULL), error);
+
+        // iterate across the logical endpoint map to copy appropriate information 
+        // into the physical endpoint instance array
+
+        endpointsRequested = bus->function_instance->function_driver->endpointsRequested;
+
+        for (epn = 0; epn < /*bus->function_instance->*/endpointsRequested; epn++) {
+
+                struct usbd_endpoint_map *endpoint_map = bus->function_instance->endpoint_map_array + epn;
+                int physicalEndpoint = endpoint_map->physicalEndpoint[0];
+                struct usbd_endpoint_instance *endpoint = bus->endpoint_array + physicalEndpoint;
+
+                endpoint_map->endpoint = endpoint;
+                endpoint->bEndpointAddress = endpoint_map->bEndpointAddress[0];
+                endpoint->bmAttributes = endpoint_map->bmAttributes[0];
+
+                switch(endpoint->bEndpointAddress & USB_ENDPOINT_DIR_MASK) {
+                case USB_DIR_IN:
+                        endpoint->tx_transferSize = endpoint_map->transferSize[0];
+                        endpoint->wMaxPacketSize = endpoint_map->wMaxPacketSize[0];
+                        endpoint->last = 0;
+                        endpoint->tx_urb = NULL;
+                        break;
+
+                case USB_DIR_OUT:
+                        endpoint->rcv_transferSize = endpoint_map->transferSize[0];
+                        endpoint->wMaxPacketSize = endpoint_map->wMaxPacketSize[0];
+                        endpoint->rcv_urb = NULL;
+                        break;
+                }
+        }
+
+        /* if composite we need to de-compose the endpoint map into the individual
+         * interface endpoint maps.
+         */
+        if(function_composite == function->function_type) {
+        }
+
+        rc = 0;
+
+        CATCH(error) {
+                usbd_strings_exit();
+        }
+        UP(&usbd_bus_sem);
+        return rc;
+}
+
+int usbd_enable_function_irq (struct usbd_bus_instance *bus, char *arg, char *serial_number)
+{
+        return usbd_enable_function(bus, arg, serial_number);
+}
+
+/*! 
+ * usbd_disable_function() - called to disable the current function
+ *
+ * Used by a USB Bus interface driver to destroy a virtual device.
+ *
+ * @param bus
+ */
+void usbd_disable_function (struct usbd_bus_instance *bus)
+{
+        DOWN(&usbd_bus_sem);
+
+        // prevent any more bottom half scheduling
+        bus->status = USBD_CLOSING;
+        UP(&usbd_bus_sem);
+
+        /* XXX 
+         * if composite ???
+         */
+        switch(bus->function_instance->function_type) {
+        case function_simple:
+        case function_interface:
+                break;
+        case function_composite:
+                break;
+        }
+
+        // wait for pending device bottom half to finish
+//XXX FIXME
+        //while (bus->device_bh.data /*|| device->function_bh.data */) {
+	while (PENDING_WORK_ITEM(bus->device_bh)){
+
+                //printk(KERN_INFO"%s: waiting for usbd_device_bh %ld %p interrupt: %d\n", __FUNCTION__, 
+                //                bus->device_bh.sync, bus->device_bh.data, in_interrupt());
+
+                // This can probably be either, but for consistency's sake...
+#if 1
+		//I.e. run this task immediately and then delay up to 2000 secdons ...
+#ifdef LINUX24
+                queue_task(&bus->device_bh, &tq_immediate);
+                mark_bh (IMMEDIATE_BH);
+#else
+		SCHEDULE_WORK(bus->device_bh);
+#endif
+                // schedule_task(&device->device_bh);
+
+                SCHEDULE_TIMEOUT (2000 * HZ);
+#else
+		//Generic version ...
+		SCHEDULE_IMMEDIATE_WORK(bus->device_bh);
+		SCHEDULE_TIMEOUT(2000); //Seconds
+#endif
+        }
+        DOWN(&usbd_bus_sem);
+
+        // tell the function driver to close
+        usbd_function_disable (bus->ep0);
+        usbd_function_disable (bus->function_instance);
+
+        // free alternates memory
+        if (bus->alternates) {
+                bus->bNumInterfaces = 0;
+                LKFREE(bus->alternates);
+                bus->alternates = NULL;
+        }
+        if (bus->function_instance) {
+#if 0
+                if (bus->function_instance->endpoint_map_array) 
+                        LKFREE(bus->function_instance->endpoint_map_array);
+                LKFREE(bus->function_instance);
+#endif
+                bus->function_instance = NULL;
+        }
+        usbd_strings_exit();
+        bus->status = USBD_CLOSED;
+        UP(&usbd_bus_sem);
+}
+
+OTG_EXPORT_SYMBOL(usbd_enable_function);
+OTG_EXPORT_SYMBOL(usbd_enable_function_irq);
+OTG_EXPORT_SYMBOL(usbd_disable_function);
+
+/* ********************************************************************************************* */
+/* ********************************************************************************************* */
+/*!
+ * Bus Event Handling:
+ *      usbd_bus_event_handler()
+ *
+ */
+static usbd_device_state_t event_states[DEVICE_CLOSE] = {
+        STATE_UNKNOWN, STATE_INIT, STATE_ATTACHED, STATE_POWERED,
+        STATE_DEFAULT, STATE_ADDRESSED, STATE_CONFIGURED, STATE_UNKNOWN,
+        STATE_UNKNOWN, STATE_UNKNOWN, STATE_ADDRESSED, STATE_SUSPENDED,
+        STATE_UNKNOWN, STATE_POWERED, STATE_ATTACHED,
+};
+
+static usbd_device_status_t event_status[DEVICE_CLOSE+1] = {
+        USBD_UNKNOWN,   // DEVICE_UNKNOWN
+        USBD_OPENING,   // DEVICE_INIT
+        USBD_OPENING,   // DEVICE_CREATE
+        USBD_OPENING,   // DEVICE_HUB_CONFIGURED
+        USBD_RESETING,  // DEVICE_RESET
+        USBD_OK,        // DEVICE_ADDRESS_ASSIGNED
+        USBD_OK,        // DEVICE_CONFIGURED
+        USBD_OK,        // DEVICE_SET_INTERFACE
+        USBD_OK,        // DEVICE_SET_FEATURE
+        USBD_OK,        // DEVICE_CLEAR_FEATURE
+        USBD_OK,        // DEVICE_DE_CONFIGURED
+        USBD_SUSPENDED, // DEVICE_BUS_INACTIVE
+        USBD_OK,        // DEVICE_BUS_ACTIVITY
+        USBD_RESETING,  // DEVICE_POWER_INTERRUPTION
+        USBD_RESETING,  // DEVICE_HUB_RESET
+        USBD_CLOSING,   // DEVICE_DESTROY
+        USBD_CLOSED,    // DEVICE_CLOSE
+};
+
+/*!
+ * usbd_bus_event_handler_irq() - called to respond to various usb events
+ *
+ * This is called from an interrupt context.
+ *
+ * Used by a Bus driver to indicate an event.
+ * @param bus
+ * @param event
+ * @param data
+ */
+void usbd_bus_event_handler_irq (struct usbd_bus_instance *bus, usbd_device_event_t event, int data)
+{
+        RETURN_UNLESS(bus);
+
+        /* get the next bus device state by table lookup of event, we need to save the
+         * current state IFF we are suspending so that it can be restored on resume.
+         */
+        switch (event) {
+        case DEVICE_BUS_INACTIVE:
+                bus->suspended_state = bus->device_state;
+                /* FALL THROUGH */
+        default:
+                bus->device_state = event_states[event];
+                //TRACE_MSG0(USBD, "default");
+                break;
+        case DEVICE_UNKNOWN:
+                //TRACE_MSG0(USBD, "UNKNOWN");
+                break;
+        case DEVICE_BUS_ACTIVITY:
+                bus->device_state = bus->suspended_state;
+                //TRACE_MSG0(USBD, "ACTIVITY");
+                break;
+
+        case DEVICE_SET_INTERFACE:
+        case DEVICE_SET_FEATURE:
+        case DEVICE_CLEAR_FEATURE:
+                //TRACE_MSG0(USBD, "SET");
+                break;
+        }
+
+        /* set the bus status by table lookup of event.
+         */
+        switch (event) {
+        case DEVICE_BUS_ACTIVITY:
+        case DEVICE_BUS_INACTIVE:
+                BREAK_IF(USBD_CLOSING != bus->status);
+                /* FALL THROUGH */
+        default:
+                bus->status = event_status[event];
+                //TRACE_MSG0(USBD, "DEFAULT");
+                break;
+        }
+
+        /* we need to reset things on some transitions
+         */
+        switch (bus->device_state) {
+        default:
+                // if we lost configuration then get rid of alternate settings
+                if (bus->alternates) {
+                        LKFREE(bus->alternates);
+                        bus->alternates = NULL;
+                }
+                bus->bNumInterfaces = 0;
+                bus->device_feature_settings = 0; 
+                bus->ConfigurationValue = 0; 
+        case STATE_CONFIGURED:
+                break;
+        case STATE_SUSPENDED:
+                // XXX Checkme - is suspend equivalent to end of session?
+                // C.f. OTG 6.5.1-6.5.3 imply that these must be reset at the end of a session or reset
+                //bus->device_feature_settings &= 
+                //        ~(FEATURE(USB_OTG_A_HNP_ENABLE) | FEATURE(USB_OTG_B_HNP_ENABLE) | FEATURE(USB_OTG_A_ALT_HNP_ENABLE)); 
+                break;
+        }
+        /* Pass the event to the bus interface driver and then the function driver and 
+         * any interface function drivers.
+         */
+        bus->driver->bops->event_handler (bus, event, data);
+        bus->function_instance->function_driver->fops->event_handler(bus->function_instance, event, data);
+        #if 0
+        // XXX pass to interface function drivers
+        #endif
+}
+
+/*! 
+ * usbd_bus_event_handler() - process bus event
+ * @param bus
+ * @param event
+ * @param data
+ */
+void usbd_bus_event_handler (struct usbd_bus_instance *bus, usbd_device_event_t event, int data)
+{
+        unsigned long flags;
+        local_irq_save (flags);
+        usbd_bus_event_handler_irq(bus, event, data);
+        local_irq_restore (flags);
+}
+
+OTG_EXPORT_SYMBOL(usbd_bus_event_handler);
+OTG_EXPORT_SYMBOL(usbd_bus_event_handler_irq);
+
+/* ********************************************************************************************* */
+/* ********************************************************************************************* */
+/*!
+ * Device Request Processing:
+ *	usbd_device_request()
+ *
+ */
+
+/*! 
+ * C.f. 9.4 Standard Requests and table 9.3
+ *
+ * Encode valid requests into a bitmap for each recipient type for 
+ * both directions. This is needed because there are some requests
+ * that appear to be a standard request but are not described in
+ * Chapter nine. For example the mouse driver hid request.
+ *
+ * So we only process the actual set of standard requests that are 
+ * defined in chapter nine and even if it appears to be standard but
+ * is not in chapter nine then we pass it to the function driver.
+ *
+ * @{
+ */
+
+#define STD(x) (1<<(x+1))
+
+/*! @{ */
+/*! h2d_standard_requests and d2h_standard_requests
+ *
+ * These tables list all of the valid Chapter Nine requests. Any request
+ * not listed in these tables will NOT be processed by the EP0 function and 
+ * will instead be passed to the appropriate function driver.
+ */
+u32 h2d_standard_requests[4] = {
+        // 0 - Device
+        STD(USB_REQ_CLEAR_FEATURE) |
+        STD(USB_REQ_SET_FEATURE) |
+        STD(USB_REQ_SET_ADDRESS) |
+        STD(USB_REQ_SET_DESCRIPTOR) |
+        STD(USB_REQ_SET_CONFIGURATION) ,
+        // 1 - Interface
+        STD(USB_REQ_CLEAR_FEATURE) |
+        STD(USB_REQ_SET_FEATURE) |
+        STD(USB_REQ_SET_INTERFACE) ,
+        // 2 - Endpoint
+        STD(USB_REQ_CLEAR_FEATURE) |
+        STD(USB_REQ_SET_FEATURE) ,
+        // 3 - Other
+        0,
+};
+
+u32 d2h_standard_requests[4] = {
+        // 0 - Device
+        STD(USB_REQ_GET_STATUS) |
+        STD(USB_REQ_GET_DESCRIPTOR) | 
+        STD(USB_REQ_GET_CONFIGURATION),
+        // 1 - Interface
+        STD(USB_REQ_GET_STATUS) |
+        STD(USB_REQ_GET_INTERFACE) ,
+        // 2 - Endpoint
+        STD(USB_REQ_GET_STATUS) |
+        STD(USB_REQ_SYNCH_FRAME) ,
+        // 3 - Other
+        0,
+};
+
+/* @} */
+
+
+/*!
+ * devreq_set_configuration() - process a received urb
+ *
+ * Used by a USB Bus interface driver to pass received device request to
+ * the appropriate USB Function driver.
+ *
+ * @param function
+ * @param request
+ * @return non-zero if errror
+ */
+static int devreq_set_configuration(struct usbd_function_instance *function, int wValue)
+{
+        struct usbd_bus_instance *bus = function->bus;
+        struct usbd_function_driver *function_driver = bus->function_instance->function_driver;
+        int bNumConfigurations = function_driver->bNumConfigurations;
+        int bNumInterfaces;
+        u8 ConfigurationValue;
+
+        struct usbd_configuration_instance *configuration_instance;
+        struct usbd_configuration_descriptor *configuration_descriptor;
+
+        // get rid of previous interface and alternates 
+        if (bus->bNumInterfaces && bus->alternates) {
+                bus->bNumInterfaces = 0;
+                LKFREE(bus->alternates);
+                bus->alternates = NULL;
+        }
+
+        // c.f. 9.4.7 - the top half of wValue is reserved
+        //
+        // c.f. 9.4.7 - zero is the default or addressed state, in our case this
+        // is the same is configuration zero, but will be fixed in usbd.c when used.
+
+        ConfigurationValue = wValue & 0x7f;
+
+        RETURN_EINVAL_IF(ConfigurationValue > bNumConfigurations);
+
+        /* save ConfigurationValue for GET_CONFIGURATION request
+         */
+        bus->ConfigurationValue = ConfigurationValue;
+
+        /* A ConfigurationValue of zero is for the default configuration (i.e.) the
+         * first one. A non-zero value needs to be decremented to allow it to be used
+         * as an index.
+         */
+        ConfigurationValue = !ConfigurationValue ? 0 : ConfigurationValue -1;
+
+        configuration_instance = 
+                &function_driver->configuration_instance_array[ConfigurationValue];
+
+        configuration_descriptor = configuration_instance->configuration_descriptor;
+
+        RETURN_EINVAL_IF(!configuration_descriptor);
+
+        bNumInterfaces = configuration_instance->bNumInterfaces;
+
+        /* reset interface and alternate settings
+         */
+        RETURN_EINVAL_IF (!(bus->alternates = (u8 *) CKMALLOC(bNumInterfaces, GFP_ATOMIC)));
+        bus->bNumInterfaces = bNumInterfaces;
+
+        /* Call function driver set_configuration() operation and if available any
+         * interface drivers set_configuration() operations.
+         */
+        if (function_driver->fops->set_configuration) {
+                function_driver->fops->function_enable && function_driver->fops->set_configuration (function, wValue);
+        }
+        /*
+         * XXX
+         */
+
+        usbd_bus_event_handler (bus, DEVICE_CONFIGURED, 0);
+        return 0;
+}
+
+/*!
+ * devreq_set_interface_altsetting() -
+ *
+ * Used by a USB Bus interface driver to pass received device request to
+ * the appropriate USB Function driver.
+ *
+ * @param function
+ * @param request
+ * @return non-zero if errror
+ */
+static int devreq_set_interface_altsetting(struct usbd_function_instance *function, int wIndex, int wValue)
+{
+        struct usbd_bus_instance *bus = function->bus;
+        struct usbd_function_driver *function_driver = function->function_driver;
+
+        RETURN_EINVAL_IF(wIndex > bus->bNumInterfaces);         // wIndex is interface
+        
+        bus->alternates[wIndex] = (u8) wValue;
+
+        /* Call function driver set_interface() operation and if available any
+         * interface drivers set_interface() operations.
+         */
+        if (function_driver->fops->set_interface)
+                function_driver->fops->function_enable && function_driver->fops->set_interface (function, wIndex, wValue);
+        /*
+         * XXX
+         */
+
+        usbd_bus_event_handler (bus, DEVICE_SET_INTERFACE, 0);
+
+        return 0;
+}
+
+/*!
+ * devreq_get_device_feature_settings() - process a received urb
+ *
+ * Used by a USB Bus interface driver to pass received device request to
+ * the appropriate USB Function driver.
+ *
+ * @param function
+ * @param request
+ * @return non-zero if errror
+ */
+int devreq_get_device_feature_settings(struct usbd_function_instance *function)
+{
+        struct usbd_bus_instance *bus = function->bus;
+        return bus->device_feature_settings;
+}
+
+
+/*! devreq_device_feature - handle set/clear feature requests for device
+ * Used by the USB Device Core to set/clear endpoint halt status.
+ *
+ * We assume that if the udc driver does not implement anything then 
+ * we should just return zero for ok.
+ */
+int devreq_device_feature (struct usbd_bus_instance *bus, int features, int flag)
+{
+        struct pcd_instance *pcd = (struct pcd_instance *)bus->privdata;
+        //u32 features = bus->device_feature_settings;
+
+        TRACE_MSG0(USBD, "BUS_DEVICE FEATURE");
+
+        /* if A-Device has enabled hnp 
+         */
+        if (features & FEATURE(USB_OTG_B_HNP_ENABLE) )
+                otg_event(pcd->otg, HNP_ENABLED, USBD, "DEVICE FEATURE - B_HNP_ENABLE");
+
+        /* if A-Device does not support HNP on this port
+         */
+        else if ( features & FEATURE(USB_OTG_A_ALT_HNP_ENABLE) )
+                ; //otg_event(pcd->otg, not(a_hnp_support));
+
+        /* if A-Device does support HNP
+         */
+        else if (features & FEATURE(USB_OTG_A_HNP_SUPPORT))
+                ; //otg_event(pcd->otg, a_hnp_support);
+
+        return 0;
+        //return usbd_pcd_ops.device_feature ? usbd_pcd_ops.device_feature() : 0;
+}
+
+/*! 
+ * copy_config() - copy data into buffer
+ */
+static int copy_config (u8 *cp, void *data, int actual_length, int max_buf)
+{
+        int available = max_buf - actual_length;
+        int length = MIN(*(u8 *)data, available);
+        RETURN_ZERO_UNLESS (data);
+        RETURN_ZERO_UNLESS (length);
+        memcpy (cp, data, length);
+        return length;
+}
+
+/*! 
+ * copy_endpoint() - copy data into buffer
+ */
+static int copy_endpoint (struct usbd_function_instance *function, u8 *cp,
+                struct usbd_endpoint_descriptor *endpoint, int endpoint_index, int actual_length, int max_buf, int hs)
+{
+        int available = max_buf - actual_length;
+        int length = MIN(endpoint->bLength, available);
+        struct usbd_endpoint_descriptor endpoint_copy;
+
+        RETURN_ZERO_IF (!length);
+        memcpy (&endpoint_copy, endpoint, endpoint->bLength);
+        usbd_endpoint_update(function, endpoint_index, &endpoint_copy, hs);
+        memcpy (cp, &endpoint_copy, length);
+        return length;
+}
+
+
+/*! 
+ * usbd_old_get_descriptor() - copy a descriptor into buffer
+ *
+ * Return non-zero for error.
+ */
+int usbd_old_get_descriptor (struct usbd_function_instance *function, u8 *buffer, int max, int descriptor_type, int index)
+{
+        struct usbd_bus_instance *bus = function->bus;
+        struct usbd_function_driver *function_driver = bus->function_instance->function_driver;
+        int actual_length = 0;
+
+        switch (descriptor_type) {
+        case USB_DT_DEVICE:
+                {
+                        struct usbd_device_descriptor *device_descriptor = function_driver->device_descriptor;
+
+                        // copy descriptor for this device
+                        actual_length += copy_config (buffer + actual_length, device_descriptor, actual_length, max);
+
+                        // correct the correct control endpoint 0 max packet size into the descriptor
+                        device_descriptor = (struct usbd_device_descriptor *) buffer;
+                        device_descriptor->bMaxPacketSize0 = bus->driver->maxpacketsize;
+                }
+                break;
+
+        #ifdef CONFIG_OTG_HIGH_SPEED
+        case USB_DT_DEVICE_QUALIFIER:      // c.f. 9.6.2 Device Qualifier
+                {
+                        struct usbd_device_qualifier_descriptor *device_qualifier_descriptor = 
+                                function_driver->device_qualifier_descriptor;
+
+                        // copy descriptor for this device
+                        actual_length += copy_config (buffer + actual_length, device_qualifier_descriptor, actual_length, max);
+
+                }
+                break;
+
+        case USB_DT_OTHER_SPEED_CONFIGURATION:
+        #endif /* CONFIG_OTG_HIGH_SPEED */
+
+        case USB_DT_CONFIGURATION:
+                {
+                        #ifdef CONFIG_OTG_HIGH_SPEED
+                        int hs = bus->HighSpeedFlag ?  descriptor_type == USB_DT_CONFIGURATION:
+                                descriptor_type == USB_DT_OTHER_SPEED_CONFIGURATION;
+                        #else /* CONFIG_OTG_HIGH_SPEED */
+                        int hs = 0;
+                        #endif /* CONFIG_OTG_HIGH_SPEED */
+                        int interface_num;
+
+                        struct usbd_configuration_instance *configuration_instance = 
+                                &function_driver->configuration_instance_array[index];
+
+                        struct usbd_configuration_descriptor *configuration_descriptor = 
+                                configuration_instance->configuration_descriptor;
+
+                        struct usbd_otg_descriptor *otg_descriptor = function_driver->otg_descriptor;
+
+                        RETURN_EINVAL_IF (!configuration_descriptor);
+                        RETURN_EINVAL_IF (index > function_driver->device_descriptor->bNumConfigurations);
+
+                        actual_length += copy_config (buffer + actual_length, configuration_descriptor, actual_length, max);
+
+                        // iterate across all bNumInterfaces for specified configuration
+                        for (interface_num = 0; interface_num < configuration_descriptor->bNumInterfaces; interface_num++) {
+
+                                int alternate_num;
+                                struct usbd_interfaces_instance *interfaces_instance =
+                                        configuration_instance->interfaces_instance_array + interface_num;
+
+                                // iterate across all interface alternates
+                                for (alternate_num = 0; alternate_num < interfaces_instance->alternates; alternate_num++) {
+
+                                        int class_num;
+                                        int endpoint;
+
+                                        struct usbd_alternate_instance *alternate_instance =
+                                                interfaces_instance->alternates_instance_array + alternate_num;
+
+                                        //struct usbd_interface_descriptor *usbd_interface_descriptor;
+
+                                        // copy descriptor for this interface
+                                        actual_length += copy_config (buffer + actual_length, 
+                                                        alternate_instance->interface_descriptor, actual_length, max);
+
+                                        // iterate across all classes for the alternate interface
+                                        for (class_num = 0; class_num < alternate_instance->classes; class_num++) 
+                                                actual_length += copy_config (buffer + actual_length, 
+                                                                *(alternate_instance->class_list + class_num), 
+                                                                actual_length, max);
+
+                                        // iterate across all endpoints for the alternate interface
+                                        //interface_descriptor = alternate_instance->interface_descriptor;
+
+                                        for (endpoint = 0; endpoint < alternate_instance->endpoints ; endpoint++) {
+
+                                                TRACE_MSG2(USBD, "endpoint: %d index: %d",
+                                                                endpoint, alternate_instance->endpoint_indexes[endpoint]);
+
+                                                actual_length += copy_endpoint (
+                                                                bus->function_instance, 
+                                                                buffer + actual_length, 
+                                                                *(( alternate_instance->endpoint_list) + endpoint), 
+                                                                alternate_instance->endpoint_indexes[endpoint],
+                                                                actual_length, 
+                                                                max, 
+                                                                hs);
+                                        }
+                                }
+                        }
+
+                        /* copy the OTG descriptor if required
+                         */
+                        actual_length += copy_config (buffer + actual_length, otg_descriptor, actual_length, max);
+
+                        /* set the configuration attributes and power fields in the configuration descriptor
+                         */
+                        configuration_descriptor = (struct usbd_configuration_descriptor *)buffer;
+
+                        configuration_descriptor->bmAttributes = BMATTRIBUTE_RESERVED;
+
+                        if (bus->device_feature_settings & FEATURE(USB_DEVICE_REMOTE_WAKEUP))
+                                configuration_descriptor->bmAttributes |= BMATTRIBUTE_REMOTE_WAKEUP;
+
+                        if (bus->driver->bMaxPower == 0) {
+                                configuration_descriptor->bmAttributes |= BMATTRIBUTE_SELF_POWERED;
+                                configuration_descriptor->bMaxPower  = 1;
+                        }
+                        else
+                                configuration_descriptor->bMaxPower  = bus->driver->bMaxPower;
+
+                        TRACE_MSG2(USBD, "bmAttributes: %02x bMaxPower: %02x", 
+                                        configuration_descriptor->bmAttributes, configuration_descriptor->bMaxPower);
+                }
+                break;
+
+        case USB_DT_STRING:
+                {
+                        struct usbd_string_descriptor *string_descriptor = NULL;
+                        RETURN_EINVAL_IF (!(string_descriptor = usbd_get_string_descriptor ((u8)index)));
+                        actual_length += copy_config (buffer + actual_length, string_descriptor, actual_length, max);
+                }
+                break;
+        default:
+                return -EINVAL;
+        }
+        return actual_length;
+}
+
+
+
+
+/*!
+ * setclear() - set or reset a bit in a mask
+ * @param features
+ * @param flag
+ * @param value
+ */
+static void setclear(u32 *features, u32 flag, u32 value)
+{
+        if (value) *features |= flag;
+        else *features &= ~flag;
+}
+
+/*! do_standard_device_request - process a device request
+ *
+ * Process a received device request. If not a Chapter nine request pass it
+ * to the other loaded function driver device_request() function.
+ *
+ * Return non-zero to indicate failure.
+ */
+static int do_standard_device_request (struct usbd_function_instance *function, struct usbd_device_request *request)
+{
+        struct usbd_bus_instance *bus = function->bus;
+        u8 bRequest = request->bRequest;
+        u8 bmRequestType = request->bmRequestType;
+        u16 wValue = le16_to_cpu(request->wValue);
+        u16 wIndex = le16_to_cpu(request->wIndex);
+        u16 wLength = le16_to_cpu(request->wLength);
+        u16 endpoint_index;
+
+        usbd_device_state_t device_state = usbd_get_device_state(function);
+
+        switch (device_state) {
+        case STATE_CREATED:
+        case STATE_ATTACHED:
+        case STATE_POWERED:
+                TRACE_MSG1(USBD, "bad device_state: %d", device_state);
+                return -EINVAL;
+
+        case STATE_INIT:
+        case STATE_DEFAULT:
+                switch (bRequest) {
+                case USB_REQ_CLEAR_FEATURE:
+                case USB_REQ_GET_INTERFACE:
+                case USB_REQ_GET_STATUS:
+                case USB_REQ_SET_DESCRIPTOR:
+                case USB_REQ_SET_INTERFACE:
+                case USB_REQ_SYNCH_FRAME:
+                        TRACE_MSG2(USBD, "bad request: %d for this device_state: %d", bRequest, device_state);
+                        return -EINVAL;
+
+                case USB_REQ_GET_CONFIGURATION:
+                case USB_REQ_SET_ADDRESS:
+                case USB_REQ_SET_CONFIGURATION:
+                case USB_REQ_GET_DESCRIPTOR:
+                case USB_REQ_SET_FEATURE:
+                        break;
+                }
+        case STATE_ADDRESSED:
+        case STATE_CONFIGURED:
+        case STATE_SUSPENDED:
+                break;
+        case STATE_UNKNOWN:
+                TRACE_MSG1(USBD, "unknown device_state: %d", device_state);
+                return -EINVAL;
+        }
+
+        /* Handle all requests that return data (direction bit set on bm RequestType).
+         * N.B. no Chapter 9 requests send additional data.
+         */
+        if ((bmRequestType & USB_REQ_DIRECTION_MASK)) {
+                struct usbd_urb *urb;
+                int rc = 0;
+                switch (bRequest) {
+                case USB_REQ_CLEAR_FEATURE:
+                case USB_REQ_SET_ADDRESS:
+                case USB_REQ_SET_CONFIGURATION:
+                case USB_REQ_SET_DESCRIPTOR:
+                case USB_REQ_SET_FEATURE:
+                case USB_REQ_SET_INTERFACE:
+                case USB_REQ_SYNCH_FRAME:
+                        TRACE_MSG0(USBD, "bad direction");
+                        return -EINVAL;
+                }
+
+                RETURN_EINVAL_IF(!wLength);
+
+                /* allocate urb, no callback, urb will be automatically de-allocated
+                 */
+                RETURN_EINVAL_IF(!(urb = usbd_alloc_urb_ep0 (function, wLength, NULL)));
+
+                switch (bRequest) {
+
+                case USB_REQ_GET_STATUS:
+                        urb->actual_length = 2;
+                        urb->buffer[0] = urb->buffer[1] = 0;
+
+                        rc = 0;
+                        switch (bmRequestType & USB_REQ_RECIPIENT_MASK) {
+                        case USB_REQ_RECIPIENT_DEVICE:
+
+                                UNLESS (usbd_get_bMaxPower(function)) urb->buffer[0] |= USB_STATUS_SELFPOWERED;
+                                urb->buffer[0] |= 
+                                        (devreq_get_device_feature_settings(function) & FEATURE(USB_DEVICE_REMOTE_WAKEUP)) ? 
+                                        USB_STATUS_REMOTEWAKEUP : 0;
+                                break;
+                        case USB_REQ_RECIPIENT_ENDPOINT:
+                                RETURN_EINVAL_UNLESS((endpoint_index = usbd_find_endpoint_index(bus, wIndex)) < bus->endpoints);
+                                urb->buffer[0] = usbd_endpoint_halted (function, endpoint_index);
+                                break;
+                        case USB_REQ_RECIPIENT_INTERFACE:
+                                break;
+                        case USB_REQ_RECIPIENT_OTHER:
+                        default:
+                                TRACE_MSG0(USBD, "bad recipient");
+                                rc = -EINVAL;
+                        }
+                        break;
+
+                case USB_REQ_GET_DESCRIPTOR:
+                        rc = usbd_old_get_descriptor (function, urb->buffer, wLength, wValue >> 8, wValue & 0xff);
+                        if (rc != -EINVAL) {
+                                urb->actual_length = rc;
+                                rc = 0;
+                        }
+                        else
+                                TRACE_MSG0(USBD, "bad description");
+                        break;
+
+                case USB_REQ_GET_CONFIGURATION:
+                        TRACE_MSG1(USBD, "GET CONFIGURATION: %d", bus->ConfigurationValue);
+                        urb->actual_length = 1; 
+                        urb->buffer[0] = bus->ConfigurationValue;
+                        break;
+
+                case USB_REQ_GET_INTERFACE:
+                        RETURN_EINVAL_IF(wIndex > bus->bNumInterfaces);
+                        urb->actual_length = 1; 
+                        urb->buffer[0] = bus->alternates[wIndex];
+                        break;
+                default:
+                        TRACE_MSG0(USBD, "bad descriptor type");
+                        rc = 1;
+                }
+
+                if (!(urb->actual_length % usbd_endpoint_zero_wMaxPacketSize(function, usbd_high_speed(function))) && 
+                                (urb->actual_length < wLength)) 
+                        urb->flags |= USBD_URB_SENDZLP;
+                
+                RETURN_ZERO_UNLESS(rc || usbd_start_in_urb(urb));
+                /* only get here if error */
+                usbd_free_urb(urb);
+                TRACE_MSG0(USBD, "get failed");
+                return -EINVAL;
+        }
+        /* Handle the requests that do not return data. 
+         */
+        else {
+                int setclear_flag = USB_REQ_SET_FEATURE == bRequest;
+                switch (bRequest) {
+                case USB_REQ_CLEAR_FEATURE:
+                case USB_REQ_SET_FEATURE:
+                        TRACE_MSG2(USBD, "FEATURE: recipient: %d wValue: %d", 
+                                        bmRequestType & USB_REQ_RECIPIENT_MASK, wValue);
+                        switch (bmRequestType & USB_REQ_RECIPIENT_MASK) {
+                        case USB_REQ_RECIPIENT_DEVICE:
+                                //switch (wValue) {
+                                //        otg_event(pcd->otg, HNP_ENABLED, PCD, "DEVICE FEATURE - B_HNP_ENABLE");
+                                //        break;
+                                //}
+                                switch (wValue) {
+                                case USB_OTG_A_ALT_HNP_ENABLE:                  // C.f. OTG 6.5.3
+                                case USB_OTG_A_HNP_SUPPORT:                     // C.f. OTG 6.5.2
+                                case USB_OTG_B_HNP_ENABLE:                      // C.f. OTG 6.5.1
+                                        RETURN_EINVAL_UNLESS(setclear_flag);    // cleared by reset
+                                case USB_DEVICE_REMOTE_WAKEUP:
+                                        break;
+                                default:
+                                        return -EINVAL;
+                                }
+                                setclear(&bus->device_feature_settings, FEATURE(wValue), setclear_flag);
+                                return devreq_device_feature(bus, FEATURE(wValue), setclear_flag);
+
+                        case USB_REQ_RECIPIENT_ENDPOINT:
+                                RETURN_EINVAL_UNLESS(USB_ENDPOINT_HALT == wValue);
+
+                                setclear(&(bus->endpoint_array[wIndex].feature_setting), 
+                                                FEATURE(wValue), bRequest == USB_REQ_SET_FEATURE);
+
+                                /* wIndex contains bEndpointAddress, also find and verify the endpoint map index as well. 
+                                 * Then clear the endpoint using the bus interface driver halt_endpoint() operation,
+                                 * if that is ok, then inform the function driver using the endpoint_cleared() operation.
+                                 *
+                                 */
+                                RETURN_EINVAL_UNLESS((endpoint_index = usbd_find_endpoint_index(bus, wIndex)) < bus->endpoints);
+                                RETURN_EINVAL_IF(function->bus->driver->bops->halt_endpoint(bus, wIndex,
+                                                        endpoint_index, setclear_flag));
+
+                                if (function->function_driver->fops->endpoint_cleared && !setclear_flag)
+                                        function->function_driver->fops->endpoint_cleared(function, wIndex, endpoint_index );
+
+                                return 0;
+
+                        case USB_REQ_RECIPIENT_INTERFACE:
+                        case USB_REQ_RECIPIENT_OTHER:
+                        default:
+                                TRACE_MSG0(USBD, "bad recipient");
+                                return -EINVAL;
+                        }
+
+                case USB_REQ_SET_ADDRESS:
+                        if (bus->driver->bops->set_address)  bus->driver->bops->set_address (bus, wValue);
+                        usbd_bus_event_handler (bus, DEVICE_ADDRESS_ASSIGNED, wValue);
+                        return 0;
+
+                case USB_REQ_SET_DESCRIPTOR:
+                        TRACE_MSG0(USBD, "set descriptor not supported");
+                        return -EINVAL;
+
+                case USB_REQ_SET_CONFIGURATION:
+                        return devreq_set_configuration(function, wValue);
+
+                case USB_REQ_SET_INTERFACE:
+                        return devreq_set_interface_altsetting(function, wIndex, wValue);
+
+                case USB_REQ_GET_CONFIGURATION:
+                case USB_REQ_GET_DESCRIPTOR:
+                case USB_REQ_GET_INTERFACE:
+                case USB_REQ_GET_STATUS:
+                case USB_REQ_SYNCH_FRAME: 
+                        TRACE_MSG0(USBD, "unkown");
+                        return -EINVAL;
+                }
+        }
+        TRACE_MSG0(USBD, "not possible");
+        return -EINVAL;
+}
+
+
+/*!
+ * usbd_device_request() - process a device request
+ *
+ * Used by a USB Bus interface driver to pass received device request to
+ * the appropriate USB Function driver.
+ *
+ * @param function
+ * @param request
+ * @return non-zero if errror
+ */
+int usbd_device_request(struct usbd_bus_instance *bus, struct usbd_device_request *request)
+{
+        struct usbd_function_instance *function = bus->function_instance;
+        u8 bRequest = request->bRequest;
+        u8 bmRequestType = request->bmRequestType;
+        u16 wValue = le16_to_cpu(request->wValue);
+        u16 wIndex = le16_to_cpu(request->wIndex);
+        u16 wLength = le16_to_cpu(request->wLength);
+
+        TRACE_MSG5(USBD, "bmRequestType:%02x bRequest:%02x wValue:%04x wIndex:%04x wLength:%04x",
+                        bmRequestType, bRequest, wValue, wIndex, wLength);
+
+        /* Handle only USB Standard Requests (c.f. USB Spec table 9-2, D6..5 must be 0),
+         * otherwise call the currently enabled function specific receive setup.
+         */
+        if (!(bmRequestType & USB_REQ_TYPE_MASK) && 
+                                ((bmRequestType & USB_DIR_IN ?  d2h_standard_requests : h2d_standard_requests)
+                                [bmRequestType & 0x3] & STD(bRequest)))
+                return do_standard_device_request(function, request);
+
+        /* Standard Requests not in Chapter nine, Class and Vendor device requests
+         * must be handled by the function driver or interface driver as appropriate.
+         */
+        switch(bmRequestType & USB_REQ_RECIPIENT_MASK) {
+        case USB_REQ_RECIPIENT_INTERFACE:
+        case USB_REQ_RECIPIENT_ENDPOINT:
+                /* XXX
+                 * if function->interfaces[] is valid then we shuffle request off to
+                 * interface driver for interface and endpoint recipients, otherwise
+                 * fall through.
+                 */
+
+                /*
+                 * XXX for now just fall through for all.
+                 */
+                
+        case USB_REQ_RECIPIENT_DEVICE:
+        case USB_REQ_RECIPIENT_OTHER:
+        default:
+                return function->function_driver->fops->device_request(function, request);
+        }
+}
+
+OTG_EXPORT_SYMBOL(usbd_device_request);
+OTG_EXPORT_SYMBOL(usbd_old_get_descriptor);
+
+/* ********************************************************************************************* */
+/* ********************************************************************************************* */
+/*!
+ * Device I/O:
+ *      usbd_urb_finished()
+ *      usbd_first_urb_detached()
+ *      usbd_find_endpoint_address()
+ */
+
+/*!
+ * do_urb_callback() - tell function that an urb has been transmitted.
+ *
+ * Must be called from an interrupt or with interrupts disabled.
+ *
+ * Used by a USB Bus driver to pass a sent urb back to the function
+ * driver via the endpoints done queue.
+ *
+ * If there is no callback or if the URB call back returns non-zero then the
+ * urb must be de-allocated here.
+ *
+ * @param urb
+ * @param rc
+ */
+static void do_urb_callback (struct usbd_urb *urb, int rc)
+{
+        RETURN_UNLESS (urb);
+        if (!urb->callback || urb->callback(urb,rc))
+                usbd_free_urb(urb);
+}
+
+/*!
+ * urb_append_irq() - append a linked list of urbs to another linked list
+ * @param hd link to urb list
+ * @param urb to append to list
+ */
+static INLINE void urb_append_irq (urb_link * hd, struct usbd_urb *urb)
+{
+        urb_link *new;
+        urb_link *pul;
+        RETURN_UNLESS (hd && urb);
+        new = &urb->link;
+        pul = hd->prev;
+        new->prev->next = hd;
+        hd->prev = new->prev;
+        new->prev = pul;
+        pul->next = new;
+}
+
+/*!
+ * urb_append() - irq safe version
+ * @param hd list of urbs
+ * @param urb urb to append
+ */
+void urb_append(urb_link *hd, struct usbd_urb *urb)
+{
+        unsigned long flags;
+        local_irq_save (flags);
+        urb_append_irq(hd, urb);
+        local_irq_restore (flags);
+}
+
+
+/*!
+ * usbd_urb_finished() - tell function that an urb has been finished.
+ *
+ * Used by a USB Bus driver to pass a sent urb back to the function
+ * driver via the endpoints finished queue.
+ * @param urb urb to process
+ * @param rc result code
+ *
+ */
+void usbd_urb_finished(struct usbd_urb *urb, int rc)
+{
+        urb->status = (usbd_urb_status_t)rc;
+        #if defined(OTG_LINUX)
+        urb->jiffies = jiffies;
+        #endif /* defined(OTG_LINUX) */
+        if (USBD_OK != urb->bus->status)
+                do_urb_callback (urb, rc);
+        else {
+                urb_append_irq (&(urb->bus->finished), urb);
+                RETURN_IF (PENDING_WORK_ITEM(urb->bus->device_bh));
+#ifdef LINUX24
+		//FIXME XXX
+                queue_task (&urb->bus->device_bh, &tq_immediate);
+                mark_bh (IMMEDIATE_BH);
+#else
+		SCHEDULE_WORK(urb->bus->device_bh);
+#endif
+        }
+}
+void usbd_urb_finished_irq (struct usbd_urb *urb, int rc)
+{
+        usbd_urb_finished(urb, rc);
+}
+
+/*!
+ * @name STRUCT
+ * @brief
+ * Structure member address manipulation macros.
+ *
+ * These are used by client code (code using the urb_link routines), since
+ * the urb_link structure is embedded in the client data structures.
+ *
+ * Note: a macro offsetof equivalent to member_offset is defined in stddef.h
+ *       but this is kept here for the sake of portability.
+ *
+ * p2surround returns a pointer to the surrounding structure given
+ * type of the surrounding structure, the name memb of the structure
+ * member pointed at by ptr.  For example, if you have:
+ *
+ *      struct foo {
+ *          int x;
+ *          float y;
+ *          char z;
+ *      } thingy;
+ *
+ *      char *cp = &thingy.z;
+ *
+ * then
+ *      &thingy == p2surround(struct foo, z, cp)
+ *
+ * @{
+ */
+
+#define _cv_(ptr)                 ((char*)(void*)(ptr))
+#define member_offset(type,memb)  (_cv_(&(((type*)0)->memb))-(char*)0)
+#define p2surround(type,memb,ptr) ((type*)(void*)(_cv_(ptr)-member_offset(type,memb)))
+
+/*!
+ * @name list
+ * @brief List support functions
+ *
+ * @{
+ */
+
+/*!
+ * urb_link() - return first urb_link in list
+ *
+ * Return the first urb_link in a list with a distinguished
+ * head "hd", or NULL if the list is empty.  This will also
+ * work as a predicate, returning NULL if empty, and non-NULL
+ * otherwise.
+ *
+ * Called from interrupt.
+ *
+ * @return link to urb
+ */
+static INLINE urb_link *first_urb_link_irq (urb_link * hd)
+{
+        urb_link *nx;
+        return (!hd || !(nx = hd->next) || (nx == hd)) ? NULL : nx;
+}
+
+/*!
+ * first_urb_irq() - return first urb in list
+ * Return the first urb in a list with a distinguished
+ * head "hd", or NULL if the list is empty.
+ *
+ * Called from interrupt.
+ *
+ * @param hd linked list of urbs
+ * @return pointer to urb
+ */
+static INLINE struct usbd_urb *first_urb_irq (urb_link * hd)
+{
+        urb_link *nx;
+        return (!(nx = first_urb_link_irq (hd))) ? NULL : p2surround (struct usbd_urb, link, nx);
+}
+
+
+
+/*!
+ * usbd_first_urb_detached_irq() - detach an return first urb
+ * Detach and return the first urb in a list with a distinguished
+ * head "hd", or NULL if the list is empty.
+ *
+ * @param hd linked list of urbs
+ * @return pointer to urb or NULL
+ */
+struct usbd_urb *usbd_first_urb_detached_irq (urb_link * hd)
+{
+        struct usbd_urb *urb;
+        urb_link *ul;
+        RETURN_NULL_IF (!(urb = first_urb_irq (hd)));
+        ul = &urb->link;
+        ul->next->prev = ul->prev;
+        ul->prev->next = ul->next;
+        ul->prev = ul->next = ul;
+        return urb;
+}
+/* @} */
+
+/*!
+ * usbd_first_urb_detached() - detach and return urb
+ *
+ * Detach and return the first urb in a list with a distinguished
+ * head "hd", or NULL if the list is empty.
+ *
+ * @param hd list of urbs
+ * @return pointer to urb or NULL
+ */
+struct usbd_urb *usbd_first_urb_detached (urb_link * hd)
+{
+        struct usbd_urb *urb;
+        unsigned long flags;
+        local_irq_save (flags);
+        urb = usbd_first_urb_detached_irq (hd);
+        local_irq_restore (flags);
+        return urb;
+}
+
+
+/*!
+ * usbd_find_endpoint_index()
+ * @param bus
+ * @param bEndpointAddress
+ * Find the endpoint map index for the specified bEndpointAddress.
+ */
+int usbd_find_endpoint_index(struct usbd_bus_instance *bus, int bEndpointAddress)
+{
+        int i;
+        for (i = 0; i < bus->endpoints; i++)
+                BREAK_IF (bus->endpoint_array[i].bEndpointAddress == bEndpointAddress);
+        return i;
+}
+
+OTG_EXPORT_SYMBOL(usbd_urb_finished);
+OTG_EXPORT_SYMBOL(usbd_urb_finished_irq);
+OTG_EXPORT_SYMBOL(usbd_first_urb_detached);
+OTG_EXPORT_SYMBOL(usbd_first_urb_detached_irq);
+OTG_EXPORT_SYMBOL(usbd_find_endpoint_index);
+
+/* ********************************************************************************************** */
+/* usbb bus bottom half ************************************************************************* */
+
+/*! 
+ * usbd_device_bh() -  Bottom half handler to process sent or received urbs.
+ * @param data
+ */
+void usbd_device_bh (void *data)
+{
+        struct usbd_bus_instance *bus = data;
+        struct usbd_endpoint_instance *endpoint;
+        struct usbd_urb *urb;
+
+        RETURN_IF (!bus || !(endpoint = bus->endpoint_array));
+        DOWN(&usbd_bus_sem);
+
+        for (; (urb = usbd_first_urb_detached (&bus->finished)); do_urb_callback (urb, urb->status));
+
+        #if defined(OTG_LINUX)
+        if (USBD_CLOSING == bus->status)
+                bus->device_bh.data = NULL;
+        #endif /* defined(OTG_LINUX) */
+        UP(&usbd_bus_sem);
+}
+
+/* ********************************************************************************************** */
+/* Module init ********************************************************************************** */
+
+extern char * usbd_function_name(int n);
+struct usbd_ops usbd_ops;
+otg_tag_t USBD;
+
+/*!
+ * usbd_device_init() - initialize
+ */
+int usbd_device_init (void)
+{
+        USBD = otg_trace_obtain_tag();
+        TRACE_MSG0(USBD,"--");
+        usbd_ops.function_name = usbd_function_name;
+        otg_set_usbd_ops(&usbd_ops);
+        return 0;
+}
+
+/*!
+ * usbd_device_exit() - de-initialize
+ */
+void usbd_device_exit (void)
+{
+        otg_set_usbd_ops(NULL);
+        otg_trace_invalidate_tag(USBD);
+}
+
+
diff -uNr linux/drivers/no-otg/otgcore/usbp-fops.c linux/drivers/otg/otgcore/usbp-fops.c
--- linux/drivers/no-otg/otgcore/usbp-fops.c	1970-01-01 01:00:00.000000000 +0100
+++ linux/drivers/otg/otgcore/usbp-fops.c	2006-09-01 21:41:35.000000000 +0200
@@ -0,0 +1,1123 @@
+/*
+ * otg/otgcore/usbp-fops.c - USB Function support
+ *
+ *      Copyright (c) 2004-2005 Belcarra
+ *
+ * By: 
+ *      Stuart Lynne <sl@belcarra.com>, 
+ *      Bruce Balden <balden@belcarra.com>
+ *
+ */
+/*!
+ * @file otg/otgcore/usbp-fops.c
+ * @brief Function driver related functions.
+ *
+ * This implements the functions used to implement USB Function drivers.
+ *
+ * @ingroup USBP
+ */
+
+#include <otg/otg-compat.h>
+#include <otg/otg-module.h>
+
+#include <otg/usbp-chap9.h>
+#include <otg/otg-trace.h>
+#include <otg/usbp-func.h>
+#include <otg/usbp-bus.h>
+
+
+struct usbd_string_descriptor **usb_strings;
+void urb_append(urb_link *hd, struct usbd_urb *urb);
+
+LIST_NODE_INIT(usbd_function_instances);      // list of all registered configuration function modules
+LIST_NODE_INIT(usbd_interface_instances);          // list of all registered interface function modules
+
+/* ********************************************************************************************* */
+/* ********************************************************************************************* */
+/*!
+ * Function Registration:
+ *	usbd_register_function()
+ *	usbd_register_interface()
+ *	usbd_register_composite()
+ *	usbd_deregister_function()
+ *	usbd_find_function()
+ *	usbd_find_interface()
+ *
+ */
+
+/*!
+ * usbd_register_function_internal() - register a usbd function driver
+ *
+ * USBD Function drivers call this to register with the USBD Core layer.
+ * Return NULL on failure.
+ *
+ * @param function_driver
+ * @param privdata
+ * @return function instance
+ *
+ */
+static struct usbd_function_instance *
+usbd_register_function_internal(struct usbd_function_driver *function_driver, char *name, 
+                usbd_function_types_t function_type, char **interface_list, void *privdata)
+{
+        struct usbd_function_instance *function = NULL;
+
+        TRACE_MSG3(USBD, "interface: %s driver: %s endpoints: %d", name, 
+                        function_driver->name, function_driver->endpointsRequested);
+
+        if (!(function = (struct usbd_function_instance *) CKMALLOC (sizeof (struct usbd_function_instance), GFP_KERNEL))) {
+                #if defined(OTG_LINUX)
+                printk(KERN_ERR,"usbd_function_instance allocation failed.\n");
+                #endif /* defined(OTG_LINUX) */
+                #if defined(OTG_WINCE)
+                DEBUGMSG(ZONE_INIT, (_T("usbd_register_function: FAILED\n")));
+                #endif /* defined(OTG_LINUX) */
+                return(NULL);
+        }
+        function->name = LSTRDUP(name);
+        function->function_driver = function_driver;
+        function->privdata = privdata;
+        function->function_type = function_type;
+        function->interfaces_list = interface_list;
+
+        //function->endpointsRequested = function->function_driver->endpointsRequested;
+        //function->requestedEndpoitns = function->function_driver->requestedEndpoints;
+
+        switch(function_type) {
+        case function_simple:
+        case function_interface:
+                break;
+        case function_composite:
+                break;
+        }
+
+        if (!(function->endpoint_map_array = (struct usbd_endpoint_map *)
+                                CKMALLOC (sizeof(struct usbd_endpoint_map) * 
+                                        function_driver->endpointsRequested, GFP_KERNEL))) {
+                #if defined(OTG_LINUX)
+                printk(KERN_ERR,"usbd_endpoint_map allocation failed.\n");
+                #endif /* defined(OTG_LINUX) */
+                #if defined(OTG_WINCE)
+                DEBUGMSG(ZONE_INIT, (_T("usbd_register_function: FAILED\n")));
+                #endif /* defined(OTG_LINUX) */
+                LKFREE(function);
+                return(NULL);
+        }
+
+        switch(function_type) {
+        case function_simple:
+        case function_interface:
+        case function_composite:
+                break;
+        }
+        return function;
+}
+
+/*!
+ * usbd_register_function() - register a usbd function driver
+ *
+ * USBD Function drivers call this to register with the USBD Core layer.
+ * Return NULL on failure.
+ *
+ * @param function_driver
+ * @param privdata
+ * @return function instance
+ *
+ */
+struct usbd_function_instance *
+usbd_register_function (struct usbd_function_driver *function_driver, char * name, void *privdata)
+{
+        struct usbd_function_instance *function = 
+                usbd_register_function_internal(function_driver, name, function_simple, NULL, privdata);
+        TRACE_MSG1(USBD, "%s", name);
+
+        LIST_ADD_TAIL (&function->instances, &usbd_function_instances);
+        return function;
+}
+
+/*!
+ * usbd_register_interface() - register a usbd function driver
+ *
+ * USBD Function drivers call this to register with the USBD Core layer.
+ * Return NULL on failure.
+ *
+ * @param function_driver
+ * @param privdata
+ * @return function instance
+ *
+ */
+struct usbd_function_instance *
+usbd_register_interface(struct usbd_function_driver *function_driver, char * name, void *privdata)
+{
+        struct usbd_function_instance *function = 
+                usbd_register_function_internal(function_driver, name, function_interface, NULL, privdata);
+        TRACE_MSG1(USBD, "%s", name);
+        LIST_ADD_TAIL (&function->instances, &usbd_interface_instances);
+        return function;
+}
+
+/*!
+ * usbd_register_composite() - register a usbd function driver
+ *
+ * USBD Function drivers call this to register with the USBD Core layer.
+ * Return NULL on failure.
+ *
+ * @param function_driver
+ * @param privdata
+ * @return function instance
+ *
+ */
+struct usbd_function_instance *
+usbd_register_composite (struct usbd_function_driver *function_driver, char * name, char **interfaces_list, void *privdata)
+{
+        struct usbd_function_instance *function = NULL;
+        struct usbd_function_instance **interfaces_array = NULL;
+        struct usbd_endpoint_request *requestedEndpoints = NULL;
+        struct usbd_endpoint_map *endpoint_map_array = NULL;
+
+        int i, j, k, l;
+        char *interface_name;
+        int endpoints = 0;
+        int cfg_length = 0;
+
+        TRACE_MSG1(USBD, "%s", name);
+        THROW_UNLESS((function = usbd_register_function_internal(function_driver, 
+                                        name, function_composite, interfaces_list, privdata)), error);
+
+        /* list and count interfaces, then allocate interfaces_array.
+         */
+        for (i = 0; (interface_name = function->interfaces_list[i]); i++) {
+                TRACE_MSG2(USBD, "INTERFACE[%02d] %20s", i, interface_name);
+        }
+        function->interfaces = i;
+        
+        TRACE_MSG1(USBD, "interfaces: %d", function->interfaces);
+
+        THROW_UNLESS((interfaces_array = (struct usbd_function_instance **) 
+                                CKMALLOC (sizeof(struct usbd_function_instance) * function->interfaces, GFP_KERNEL)), error);
+        function->interfaces_array = interfaces_array;
+
+#if 0
+        /* Find all interfaces and compose the interface list, count the endpoints 
+         * and allocte the endpoint request array. Also find and total descriptor sizes.
+         */
+        for (i = 0; i < function->interfaces; i++) {
+                struct usbd_function_instance *interface_function;
+                struct usbd_interface_description *interface_description = interface_function->interface_list;
+                THROW_UNLESS(interface_function = usbd_find_interface(interfaces_list[i]), error);
+
+                TRACE_MSG4(USBD, "interface_function: %x name: %s driver: %s %d", interface_function, 
+                                interface_function->name, 
+                                interface_function->function_driver->name, 
+                                interface_function->function_driver->endpointsRequested);
+
+                interfaces_array[i] = interface_function;
+                endpoints += interface_function->function_driver->endpointsRequested;
+                TRACE_MSG4(USBD, "ENDPOINT [%02d] %20s %02d %02d", 
+                                i, interfaces_list[i], interface_function->function_driver->endpointsRequested, endpoints);
+
+                /* iterate across all interface descriptions */
+                for (j = 0; j < bNumInterfaces; j++) {
+                        struct usbd_alternate_instance *alternate_instance = interface_description[j];
+                        
+                        /* iterate across all alternates for this interface */
+                        for (k = 0; k < interface_description[j].alternates; k++) {
+
+                                
+
+                        }
+                }
+
+        }
+#endif
+        function->endpoints = endpoints;
+        TRACE_MSG2(USBD, "endpoints: %d %d", endpoints, function->endpoints);
+
+
+        /* requeset the endpoints from the bus interface driver
+         */
+        THROW_UNLESS((requestedEndpoints = (struct usbd_endpoint_request *) 
+                                CKMALLOC (sizeof(struct usbd_endpoint_request) * function->endpoints, GFP_KERNEL)), error);
+
+        function->requestedEndpoints = requestedEndpoints;
+#if 0
+        /* we need to compose the endpoint request array
+         */
+        for (endpoints = 0, i = 0; i < function->interfaces; i++) {
+                struct usbd_function_instance *interface_function = interfaces_array[i];
+                struct usbd_interface_description *interface_description = interface_function->interface_list;
+
+                for (j = 0; j < interface_function->endpoints; j++) {
+                        memcpy(&requestedEndpoints[endpoints], &interface_function->requestedEndpoints[j],
+                                        sizeof(struct usbd_endpoint_request));
+                        requestedEndpoints[endpoints].interface_num = i;
+                }
+        }
+#endif
+
+
+        /* Insert into function list
+         */
+        LIST_ADD_TAIL (&function->instances, &usbd_function_instances);
+        return function;
+
+        CATCH(error) {
+                if (requestedEndpoints) LKFREE(requestedEndpoints);
+                if (interfaces_array) LKFREE(interfaces_array);
+                if (function) usbd_deregister_function(function);
+                return NULL;
+        }
+}
+
+/*! 
+ * usbd_deregister_function() - de-register a usbd function driver instance
+ *
+ * USBD Function drivers call this to de-register with the USBD Core layer.
+ * @param function_instance
+ */
+void usbd_deregister_function (struct usbd_function_instance *function_instance)
+{
+        RETURN_UNLESS (function_instance);
+        LIST_DEL (function_instance->instances);
+        if (function_instance->name) LKFREE(function_instance->name);
+        if (function_instance->endpoint_map_array) LKFREE(function_instance->endpoint_map_array);
+        LKFREE(function_instance);
+}
+
+/*!
+ * usbd_find_function_internal() - register a usbd function driver
+ *
+ * USBD Function drivers call this to register with the USBD Core layer.
+ * Return NULL on failure.
+ *
+ * @param name
+ * @return function instance
+ *
+ */
+static struct usbd_function_instance *
+usbd_find_function_internal(LIST_NODE *usbd_function_instances, char *name)
+{
+        int len = name ? strlen(name) : 0;
+        LIST_NODE *lhd = NULL;
+        TRACE_MSG1(USBD, "%s", name);
+        LIST_FOR_EACH (lhd, usbd_function_instances) {
+
+		struct usbd_function_instance *function = LIST_ENTRY (lhd, struct usbd_function_instance, instances);
+                struct usbd_function_driver *function_driver;
+                function_driver = function->function_driver;
+                printk(KERN_ERR"%s: name: %s len: %d function: %s\n", __FUNCTION__, name ? name : "", len, function_driver->name);
+                CONTINUE_IF(len || strncmp(function_driver->name, name, len));
+
+                TRACE_MSG5(USBD, "%s %s %d %s %d", name, function->name, len,
+                                function->function_driver->name,
+                                function->function_driver->endpointsRequested);
+                CONTINUE_IF(len && strncmp(function->name, name, len));
+
+                TRACE_MSG4(USBD, "FOUND %s %x %s %d", name, function, 
+                                function->function_driver->name,
+                                function->function_driver->endpointsRequested);
+
+                return function;
+
+                //struct usbd_function_driver *function_driver = function->function_driver;
+                //printk(KERN_ERR"%s: name: %s len: %d function: %s\n", __FUNCTION__,name?name : "", len, function_driver->name);
+                //CONTINUE_IF(len || strncmp(function_driver->name, name, len));
+        }
+        return NULL;
+}
+
+
+/*!
+ * usbd_find_function() - register a usbd function driver
+ *
+ * USBD Function drivers call this to register with the USBD Core layer.
+ * Return NULL on failure.
+ *
+ * @param name
+ * @return function instance
+ *
+ */
+struct usbd_function_instance *
+usbd_find_function (char *name)
+{
+        return usbd_find_function_internal(&usbd_function_instances, name);
+}
+
+/*!
+ * usbd_default_function() - register a usbd function driver
+ *
+ * USBD Function drivers call this to register with the USBD Core layer.
+ * Return NULL on failure.
+ *
+ * @param name
+ * @return function instance
+ *
+ */
+struct usbd_function_instance *
+usbd_default_function (char *name)
+{
+        return usbd_find_function_internal(&usbd_function_instances, "");
+}
+
+/*!
+ * usbd_find_interface() - register a usbd function driver
+ *
+ * USBD Function drivers call this to register with the USBD Core layer.
+ * Return NULL on failure.
+ *
+ * @param name
+ * @return function instance
+ *
+ */
+struct usbd_function_instance *usbd_find_interface(char *name)
+{
+        TRACE_MSG1(USBD, "%s", name);
+        return usbd_find_function_internal(&usbd_interface_instances, name);
+}
+
+
+/*! 
+ * usbd_function_name() - return name of nth function driver
+ * @param n index to function name
+ */
+char * usbd_function_name(int n)
+{
+        struct usbd_function_instance *function = NULL;
+        LIST_NODE *lhd = NULL;
+
+        LIST_FOR_EACH (lhd, &usbd_function_instances) {
+                struct usbd_function_driver *function_driver;
+                function = LIST_ENTRY (lhd, struct usbd_function_instance, instances);
+                function_driver = function->function_driver;
+
+                TRACE_MSG2(USBD, "name: [%d] %s", n, function_driver->name);
+
+                CONTINUE_IF(n--);
+                return (char *)function_driver->name;
+        };
+        return NULL;
+}
+
+OTG_EXPORT_SYMBOL(usbd_register_function);
+OTG_EXPORT_SYMBOL(usbd_register_interface);
+OTG_EXPORT_SYMBOL(usbd_register_composite);
+OTG_EXPORT_SYMBOL(usbd_deregister_function);
+
+/* ********************************************************************************************* */
+/* ********************************************************************************************* */
+/*!
+ * String Descriptors:
+ *	usbd_alloc_string_descriptor()
+ *	usbd_free_string_descriptor()
+ *	usbd_get_string_descriptor()
+ */
+
+#define LANGID_ENGLISH          "\011"
+#define LANGID_US_ENGLISH       "\004"
+#define LANGIDs  LANGID_US_ENGLISH LANGID_ENGLISH
+
+/*!
+ * usbd_alloc_string_zero() - allocate a string descriptor and return index number
+ *
+ * Find an empty slot in index string array, create a corresponding descriptor
+ * and return the slot number.
+ *
+ * @param str string to allocate
+ * @return the slot number
+ */
+static u8 usbd_alloc_string_zero (char *str)
+{
+        u8 bLength;
+        u16 *wData;
+        struct usbd_string_descriptor *string = NULL;
+
+        RETURN_ZERO_IF(usb_strings[0] != NULL);
+
+        bLength = sizeof (struct usbd_string_descriptor) + strlen (str);
+
+        RETURN_ZERO_IF(!(string = (struct usbd_string_descriptor *) CKMALLOC (bLength, GFP_KERNEL)));
+
+        string->bLength = bLength;                              // set descriptor length
+        string->bDescriptorType = USB_DT_STRING;                // set descriptor type
+
+        for (wData = string->wData; *str; str += 2, wData++)    // copy
+                *wData = (u16) ((str[0] << 8 | str[1]));
+
+        usb_strings[0] = string;                                // save in string index array
+        return 0;
+}
+
+int usbd_maxstrings = 40;
+
+/*!
+ * usbd_strings_init() - initialize usb strings pool
+ */
+int usbd_strings_init (void)
+{
+        //TRACE_MSG0(USBD, "entered");
+        RETURN_ZERO_IF(usb_strings);
+        usbd_maxstrings = MIN(usbd_maxstrings, 254);
+
+        RETURN_EINVAL_IF (!(usb_strings = CKMALLOC (sizeof (struct usbd_string_descriptor *) * usbd_maxstrings, GFP_KERNEL)));
+        if (usbd_alloc_string_zero (LANGIDs) != 0) {
+                LKFREE (usb_strings);
+                return -1;
+        }
+        return 0;
+}
+
+/*!
+ * usbd_strings_exit() - de-initialize usb strings pool
+ */
+void usbd_strings_exit(void)
+{
+        int i;
+        //TRACE_MSG0(USBD, "entered");
+        RETURN_IF (!usb_strings);
+        for (i = 0; i < usbd_maxstrings; i++)
+                usbd_free_string_descriptor((u8)i);
+        LKFREE (usb_strings);
+        usb_strings = NULL;
+}
+
+
+/*! 
+ * usbd_get_string_descriptor() - find and return a string descriptor
+ *
+ * Find an indexed string and return a pointer to a it.
+ * @param index of string
+ * @return pointer to string descriptor or NULL
+ */
+struct usbd_string_descriptor *usbd_get_string_descriptor (u8 index)
+{
+        RETURN_NULL_IF (index >= usbd_maxstrings);
+        return usb_strings[index];
+}
+
+/*! 
+ * usbd_realloc_string() - allocate a string descriptor and return index number
+ *
+ * Find an empty slot in index string array, create a corresponding descriptor
+ * and return the slot number.
+ * @param index
+ * @param str
+ * @return new index
+ */
+u8 usbd_realloc_string (u8 index, char *str)
+{
+        struct usbd_string_descriptor *string;
+        u8 bLength;
+        u16 *wData;
+
+        RETURN_ZERO_IF(!str || !strlen (str));
+        //TRACE_MSG2(USBD, "index: %d str: %s", index, str);
+
+        // XXX should have semaphores...
+
+        if ((index < usbd_maxstrings) && (string = usb_strings[index])) {
+                usb_strings[index] = NULL;
+                LKFREE (string);
+        }
+
+        bLength = sizeof (struct usbd_string_descriptor) + 2 * strlen (str);
+
+        RETURN_ZERO_IF(!(string = (struct usbd_string_descriptor *)CKMALLOC (bLength, GFP_KERNEL)));
+
+        string->bLength = bLength;
+        string->bDescriptorType = USB_DT_STRING;
+
+        for (wData = string->wData; *str;) 
+                *wData++ = (u16) (*str++);
+
+        // store in string index array
+        usb_strings[index] = string;
+        return index;
+}
+
+/*! 
+ * usbd_alloc_string() - allocate a string descriptor and return index number
+ *
+ * Find an empty slot in index string array, create a corresponding descriptor
+ * and return the slot number.
+ * @param str
+ * @return index
+ *
+ * XXX rename to usbd_alloc_string_descriptor
+ */
+u8 usbd_alloc_string (char *str)
+{
+        int i;
+        struct usbd_string_descriptor *string = NULL;
+        u8 bLength;
+        u16 *wData;
+
+        RETURN_ZERO_IF(!str || !strlen (str));
+
+        // find an empty string descriptor slot
+        for (i = 1; i < usbd_maxstrings; i++) {
+
+                CONTINUE_IF (usb_strings[i] != NULL);
+
+                bLength = sizeof (struct usbd_string_descriptor) + 2 * strlen (str);
+
+                RETURN_ZERO_IF(!(string = (struct usbd_string_descriptor *)CKMALLOC (bLength, GFP_KERNEL)));
+
+                string->bLength = bLength;
+                string->bDescriptorType = USB_DT_STRING;
+
+                for (wData = string->wData; *str;) 
+                        *wData++ = (u16) (*str++);
+
+                // store in string index array
+                usb_strings[i] = string;
+                return i;
+        }
+        return 0;
+}
+
+
+/*! 
+ * usbd_free_string_descriptor() - deallocate a string descriptor
+ *
+ * Find and remove an allocated string.
+ * @param index
+ *
+ */
+void usbd_free_string_descriptor (u8 index)
+{
+        struct usbd_string_descriptor *string;
+
+        if ((index < usbd_maxstrings) && (string = usb_strings[index])) {
+                usb_strings[index] = NULL;
+                LKFREE (string);
+        }
+}
+
+OTG_EXPORT_SYMBOL(usbd_realloc_string);
+OTG_EXPORT_SYMBOL(usbd_alloc_string);
+OTG_EXPORT_SYMBOL(usbd_free_string_descriptor);
+OTG_EXPORT_SYMBOL(usbd_get_string_descriptor);
+OTG_EXPORT_SYMBOL(usbd_maxstrings);
+
+
+/* ********************************************************************************************* */
+/* ********************************************************************************************* */
+/*!
+ * Device Information:
+ *	usbd_high_speed()
+ *	usbd_get_bMaxPower()
+ *	usbd_endpoint_wMaxPacketsize()
+ *	usbd_endpoint_wMaxPacketsize_ep0()
+ *	usbd_endpoint_bEndpointAddress()
+ *	
+ */
+
+
+/*!
+ * usbd_high_speed() - return high speed status
+ * @return true if operating at high speed
+ */
+int usbd_high_speed(struct usbd_function_instance *function)
+{
+        // XXX TODO - per function modifications for composite devices
+        return function->bus->HighSpeedFlag ? 1 : 0;
+}
+
+/*!
+ * usbd_get_bMaxPower() - process a received urb
+ *
+ * Used by a USB Bus interface driver to pass received device request to
+ * the appropriate USB Function driver.
+ *
+ * @param function
+ * @param request
+ * @return non-zero if errror
+ */
+int usbd_get_bMaxPower(struct usbd_function_instance *function)
+{
+        struct usbd_bus_instance *bus = function->bus;
+        return bus->driver->bMaxPower;
+}
+
+
+/*!
+ * usbd_endpoint_wMaxPacketSize() - get maximum packet size for endpoint
+ * @param function
+ * @param endpoint_index 
+ * @param hs highspeed flag
+ * @return endpoint size
+ */
+int usbd_endpoint_wMaxPacketSize(struct usbd_function_instance *function, int endpoint_index, int hs)
+{
+        struct usbd_endpoint_map *endpoint_map;
+        RETURN_ZERO_IF(!(endpoint_map = function->endpoint_map_array));
+        return le16_to_cpu(endpoint_map[endpoint_index].wMaxPacketSize[hs]);
+}
+
+/*!
+ * usbd_endpoint_zero_wMaxPacketSize() - get maximum packet size for endpoint zero
+ * @param function
+ * @param hs highspeed flag
+ * @return endpoint size
+ */
+int usbd_endpoint_zero_wMaxPacketSize(struct usbd_function_instance *function, int hs)
+{
+        struct usbd_endpoint_map *endpoint_map;
+        RETURN_ZERO_IF(!(endpoint_map = function->bus->ep0->endpoint_map_array));
+        return le16_to_cpu(endpoint_map[0].wMaxPacketSize[hs]);
+}
+
+/*!
+ * usbd_endpoint_bEndpointAddress() - get endpoint addrsess
+ * @param function
+ * @param endpoint_index
+ * @param hs high speed flag
+ * @return endpoint address
+ */
+int usbd_endpoint_bEndpointAddress(struct usbd_function_instance *function, int endpoint_index, int hs)
+{
+        struct usbd_endpoint_map *endpoint_map;
+        RETURN_ZERO_IF(!(endpoint_map = function->endpoint_map_array));
+        return endpoint_map[endpoint_index].bEndpointAddress[hs];
+}
+
+OTG_EXPORT_SYMBOL(usbd_endpoint_wMaxPacketSize);
+OTG_EXPORT_SYMBOL(usbd_endpoint_zero_wMaxPacketSize);
+OTG_EXPORT_SYMBOL(usbd_endpoint_bEndpointAddress);
+OTG_EXPORT_SYMBOL(usbd_high_speed);
+
+/* ********************************************************************************************* */
+/* ********************************************************************************************* */
+/*!
+ * Device Control:
+ *
+ *	XXX usbd_request_endpoints()
+ *	XXX usbd_configure_endpoints()
+ *
+ *      usbd_get_device_state()
+ *      usbd_get_device_status()
+ *      usbd_framenum()
+ *      usbd_ticks()
+ *      usbd_elapsed()
+ */
+
+/*!
+ * usbd_flush_endpoint_irq() - flush urbs from endpoint
+ *
+ * Iterate across the approrpiate tx or rcv list and cancel any outstanding urbs.
+ *
+ * @param endpoint flush urbs on this endpoint
+ */
+void usbd_flush_endpoint_irq (struct usbd_endpoint_instance *endpoint)
+{
+        struct usbd_urb *urb;
+
+        if (endpoint->bEndpointAddress)
+                TRACE_MSG1(USBD, "bEndpointAddress: %02x", endpoint->bEndpointAddress);
+
+        if ((urb = endpoint->tx_urb))
+                usbd_cancel_urb(urb);
+
+        for (; (urb = usbd_first_urb_detached_irq (&endpoint->tx)); usbd_cancel_urb(urb));
+
+        if ((urb = endpoint->rcv_urb))
+                usbd_cancel_urb(urb);
+
+        for (; (urb = usbd_first_urb_detached_irq (&endpoint->rdy)); usbd_cancel_urb(urb));
+}
+
+/*!
+ * usbd_flush_endpoint() - flush urbs from endpoint
+ *
+ * Iterate across the approrpiate tx or rcv list and cancel any outstanding urbs.
+ *
+ * @param endpoint flush urbs on this endpoint
+ */
+void usbd_flush_endpoint (struct usbd_endpoint_instance *endpoint)
+{
+        unsigned long flags;
+        local_irq_save (flags);
+        usbd_flush_endpoint_irq(endpoint);
+        local_irq_restore (flags);
+}
+
+/*!
+ * usbd_flush_endpoint_address() - flush endpoint
+ * @param function
+ * @param endpoint_index
+ */
+void usbd_flush_endpoint_index (struct usbd_function_instance *function, int endpoint_index)
+{
+        struct usbd_endpoint_map *endpoint_map;
+        struct usbd_endpoint_instance *endpoint;
+
+        RETURN_IF(!function);
+        RETURN_IF(!(endpoint_map = function->endpoint_map_array));
+        RETURN_IF(!(endpoint = endpoint_map[endpoint_index].endpoint));
+        usbd_flush_endpoint (endpoint);
+}
+
+/*!
+ * usbd_get_device_state() - return device state
+ * @param function
+ * @return current device state
+ */
+usbd_device_state_t usbd_get_device_state(struct usbd_function_instance *function)
+{
+        //TRACE_MSG1(USBD, "%d", function->bus->device_state);
+        return (function && function->bus) ? function->bus->device_state : STATE_UNKNOWN;
+}
+
+/*!
+ * usbd_get_device_status() - return device status
+ * @param function
+ * @return current device status
+ */
+usbd_device_status_t usbd_get_device_status(struct usbd_function_instance *function)
+{
+        //TRACE_MSG1(USBD, "%d", function->bus->status);
+        return (function && function->bus) ?  function->bus->status : USBD_UNKNOWN;
+}
+
+/*!
+ * usbd_framenum() - return framenum
+ * @param function
+ * @return current framenum
+ */
+int usbd_framenum(struct usbd_function_instance * function)
+{
+        RETURN_ZERO_UNLESS(function);
+        RETURN_ZERO_UNLESS(function->bus->driver->bops->framenum);
+        return function->bus->driver->bops->framenum ();
+}
+
+/*!
+ * usbd_ticks() - return ticks
+ * @param function
+ * @return current ticks
+ */
+u64 usbd_ticks(struct usbd_function_instance *function)
+{
+        RETURN_ZERO_UNLESS(function);
+        RETURN_ZERO_UNLESS(function->bus->driver->bops->ticks);
+        return function->bus->driver->bops->ticks ();
+}
+
+/*!
+ * usbd_elapsed() - return elapsed
+ * @param function
+ * @param t1
+ * @param t2
+ * @return elapsed uSecs between t1 and t2
+ */
+u64 usbd_elapsed(struct usbd_function_instance *function, u64 *t1, u64 *t2)
+{
+        RETURN_ZERO_UNLESS(function);
+        RETURN_ZERO_UNLESS(function->bus->driver->bops->elapsed);
+        return function->bus->driver->bops->elapsed (t1, t2);
+}
+
+
+//OTG_EXPORT_SYMBOL(usbd_set_endpoint_halt);
+//OTG_EXPORT_SYMBOL(usbd_endpoint_halted);
+OTG_EXPORT_SYMBOL(usbd_get_device_state);
+OTG_EXPORT_SYMBOL(usbd_get_device_status);
+OTG_EXPORT_SYMBOL(usbd_framenum);
+OTG_EXPORT_SYMBOL(usbd_ticks);
+OTG_EXPORT_SYMBOL(usbd_elapsed);
+OTG_EXPORT_SYMBOL(usbd_flush_endpoint);
+OTG_EXPORT_SYMBOL(usbd_flush_endpoint_index);
+
+/* ********************************************************************************************* */
+/* ********************************************************************************************* */
+/*!
+ * Endpoint I/O:
+ *	usbd_alloc_urb()
+ *	usbd_start_in_urb()
+ *	usbd_start_out_urb()
+ *	usbd_cancel_urb()
+ *
+ */
+
+/*!
+ * usbd_alloc_urb() - allocate an URB appropriate for specified endpoint
+ *
+ * Allocate an urb structure. The usb device urb structure is used to
+ * contain all data associated with a transfer, including a setup packet for
+ * control transfers.
+ * 
+ * @param function
+ * @param endpoint_index
+ * @param length
+ * @param callback
+ * @return urb
+ */
+struct usbd_urb *usbd_alloc_urb (struct usbd_function_instance *function, int endpoint_index,
+                int length, int (*callback) (struct usbd_urb *, int))
+{
+        struct usbd_urb *urb = NULL;
+        struct usbd_endpoint_map *endpoint_map;
+        struct usbd_endpoint_instance *endpoint;
+        unsigned long flags;
+
+        RETURN_NULL_IF(!(endpoint_map = function->endpoint_map_array));
+        RETURN_NULL_IF(!(endpoint = endpoint_map[endpoint_index].endpoint));
+
+        local_irq_save(flags);
+        THROW_IF (!(urb = (struct usbd_urb *)CKMALLOC (sizeof (struct usbd_urb), GFP_ATOMIC)), error); 
+        urb->endpoint = endpoint;
+        urb->bus = function->bus;
+        urb->function_instance = function;
+        urb->link.prev = urb->link.next = &urb->link;
+        urb->callback = callback;
+        urb->buffer_length = urb->actual_length = 0;
+
+        if (length) {
+                urb->request_length = length;
+
+                /* For receive we always overallocate to ensure that receiving another
+                 * full sized packet when we are only expecting a short packet will
+                 * not overflow the buffer
+                 */
+                if (!endpoint->bEndpointAddress || endpoint->bEndpointAddress & USB_ENDPOINT_DIR_MASK) {
+                        length = ((length / endpoint->wMaxPacketSize) + 1) * endpoint->wMaxPacketSize;
+                }
+                urb->buffer_length = length;
+
+                #if 0
+                if (urb->endpoint && urb->endpoint->bEndpointAddress && urb->function_instance && 
+                                urb->function_instance->function_driver->fops->alloc_urb_data) 
+                {
+                        THROW_IF(urb->function_instance->function_driver->fops->alloc_urb_data (urb, length), error); 
+                }
+                else 
+                #endif
+                        THROW_IF(!(urb->buffer = (u8 *)CKMALLOC (length, GFP_ATOMIC)), error);
+                
+        }
+
+        CATCH(error) {
+                #if defined(OTG_LINUX)
+                printk(KERN_ERR"%s: dealloc %p\n", __FUNCTION__, urb); 
+                #endif /* defined(OTG_LINUX) */
+                #if defined(OTG_WINCE)
+                DEBUGMSG(ZONE_INIT, (_T("usbd_alloc_urb: FAILED\n")));
+                #endif /* defined(OTG_LINUX) */
+                usbd_free_urb(urb);
+                urb = NULL;
+        }
+        local_irq_restore(flags);
+        return urb;
+}
+
+/*!
+ * usbd_halt_endpoint() - 
+ *
+ * @param function function that owns endpoint
+ * @param endpoint endpoint number
+ * @return non-zero if endpoint is halted.
+ */
+int usbd_halt_endpoint (struct usbd_function_instance *function, int endpoint_index)
+{
+        struct usbd_endpoint_map *endpoint_map;
+        struct usbd_endpoint_instance *endpoint;
+
+        RETURN_ZERO_IF(!(endpoint_map = function->endpoint_map_array));
+        RETURN_ZERO_IF(!(endpoint = endpoint_map[endpoint_index].endpoint));
+
+        return function->bus->driver->bops->halt_endpoint (function->bus, endpoint->bEndpointAddress, endpoint_index, 1);
+}
+
+
+/*!
+ * usbd_alloc_urb_ep0() - allocate an urb for endpoint zero
+ * @param function
+ * @param length
+ * @param callback
+ * @return urb
+ */
+struct usbd_urb *usbd_alloc_urb_ep0 (struct usbd_function_instance *function, int length, 
+                int (*callback) (struct usbd_urb *, int))
+{
+        return usbd_alloc_urb(function->bus->ep0, 0, length, callback);
+}
+
+/*!
+ * usbd_free_urb() - deallocate an URB and associated buffer
+ *
+ * Deallocate an urb structure and associated data.
+ * @param urb
+ */
+void usbd_free_urb (struct usbd_urb *urb)
+{
+        RETURN_IF (!urb);
+        if (urb->buffer) {
+                #if 0
+                if (urb->function_instance && urb->function_instance->function_driver->fops->dealloc_urb_data)
+                        urb->function_instance->function_driver->fops->dealloc_urb_data ((void *) urb->buffer);
+                else
+                #endif
+                        LKFREE ((void *) urb->buffer);
+        }
+        LKFREE (urb);
+}
+
+
+/*!
+ * usbd_start_in_urb() - submit a urb to send
+ *
+ * Used by a USB Function driver to submit data to be sent in an urb to the
+ * appropriate USB Bus driver via the endpoints transmit queue.
+ *
+ * @param urb to send
+ * @return non-zero if error
+ */
+int usbd_start_in_urb (struct usbd_urb *urb)
+{
+        RETURN_EINVAL_IF (urb->endpoint->bEndpointAddress && (USBD_OK != urb->bus->status));
+        if (urb->endpoint->feature_setting & FEATURE(USB_ENDPOINT_HALT)) {
+                usbd_urb_finished(urb, SEND_STALLED);
+                return 0;
+        }
+        urb->status = SEND_IN_QUEUE;
+        #if defined(OTG_LINUX)
+        urb->jiffies = jiffies;
+        #endif /* defined(OTG_LINUX) */
+        urb_append (&(urb->endpoint->tx), urb);
+        return urb->bus->driver->bops->start_endpoint_in(urb->bus, urb->endpoint);
+}
+
+/*!
+ * usbd_start_out_urb() - recycle a received urb
+ *
+ * Used by a USB Function interface driver to recycle an urb.
+ *
+ * @param urb to process
+ * @return non-zero if error
+ */
+int usbd_start_out_urb (struct usbd_urb *urb)
+{
+        RETURN_EINVAL_IF (urb->endpoint->bEndpointAddress && (USBD_OK != urb->bus->status));
+        if (urb->endpoint->feature_setting & FEATURE(USB_ENDPOINT_HALT)) {
+                usbd_urb_finished(urb, RECV_STALLED);
+                return 0;
+        }
+        urb->actual_length = 0;
+        urb->status = RECV_IN_QUEUE;
+        #if defined(OTG_LINUX)
+        urb->jiffies = jiffies;
+        #endif /* defined(OTG_LINUX) */
+        urb_append (&(urb->endpoint->rdy), urb);
+        return urb->bus->driver->bops->start_endpoint_out(urb->bus, urb->endpoint);
+}
+
+/*!
+ * usbd_cancel_urb() - cancel an urb being sent
+ *
+ * @param urb to process
+ * @return non-zero if error
+ */
+int usbd_cancel_urb (struct usbd_urb *urb)
+{
+        //TRACE_MSG0(USBD, "entered");
+        return urb->bus->driver->bops->cancel_urb_irq (urb);
+}
+
+
+/*!
+ * usbd_endpoint_halted() - return halt status
+ *
+ * @param function function that owns endpoint
+ * @param endpoint endpoint number
+ * @return non-zero if endpoint is halted.
+ */
+int usbd_endpoint_halted (struct usbd_function_instance *function, int endpoint_index)
+{
+        struct usbd_endpoint_instance *endpoint;
+        struct usbd_endpoint_map *endpoint_map;
+        //TRACE_MSG0(USBD, "entered");
+        //
+        RETURN_ZERO_UNLESS((endpoint_map = function->endpoint_map_array));
+        RETURN_ZERO_UNLESS((endpoint = endpoint_map[endpoint_index].endpoint));
+        return function->bus->driver->bops->endpoint_halted (function->bus, endpoint->bEndpointAddress, endpoint_index);
+}
+
+
+OTG_EXPORT_SYMBOL(usbd_alloc_urb);
+OTG_EXPORT_SYMBOL(usbd_alloc_urb_ep0);
+OTG_EXPORT_SYMBOL(usbd_free_urb);
+
+OTG_EXPORT_SYMBOL(usbd_start_in_urb);
+OTG_EXPORT_SYMBOL(usbd_start_out_urb);
+OTG_EXPORT_SYMBOL(usbd_cancel_urb);
+OTG_EXPORT_SYMBOL(usbd_halt_endpoint);
+
+/* ********************************************************************************************* */
+
+/*!
+ * usbd_function_get_privdata() - get private data pointer
+ * @param function
+ * @return void * pointer to private data
+ */
+void *usbd_function_get_privdata(struct usbd_function_instance *function)
+{
+        return(function->privdata);
+}
+
+/*!
+ * usbd_function_set_privdata() - set private data structure in function
+ * @param function
+ * @param privdata
+ */
+void usbd_function_set_privdata(struct usbd_function_instance *function, void *privdata)
+{
+        function->privdata = privdata;
+}
+
+
+
+
+/*!
+ * usbd_endpoint_transferSize() - get transferSize for endpoint
+ * @param function
+ * @param endpoint_index
+ * @param hs highspeed flag
+ * @return transfer size
+ */
+int usbd_endpoint_transferSize(struct usbd_function_instance *function, int endpoint_index, int hs)
+{
+        struct usbd_endpoint_map *endpoint_map;
+        RETURN_ZERO_IF(!(endpoint_map = function->endpoint_map_array));
+        return endpoint_map[endpoint_index].transferSize[hs];
+}
+
+/*!
+ * usbd_endpoint_update() - update endpoint address and size
+ * @param function
+ * @param endpoint_index
+ * @param endpoint descriptor
+ * @param hs high speed flag
+ */
+void usbd_endpoint_update(struct usbd_function_instance *function, int endpoint_index, 
+                struct usbd_endpoint_descriptor *endpoint, int hs)
+{
+        endpoint->bEndpointAddress = usbd_endpoint_bEndpointAddress(function, endpoint_index, hs);
+        endpoint->wMaxPacketSize = usbd_endpoint_wMaxPacketSize(function, endpoint_index, hs);
+}
+
+/*!
+ * usbd_otg_bmattributes() - return attributes
+ * @param function
+ * @return endpoint attributes
+ */
+int usbd_otg_bmattributes(struct usbd_function_instance *function)
+{
+        // XXX TODO - per function modifications for composite devices
+        return function->bus->otg_bmAttributes;
+}
+
+OTG_EXPORT_SYMBOL(usbd_function_get_privdata);
+OTG_EXPORT_SYMBOL(usbd_function_set_privdata);
+OTG_EXPORT_SYMBOL(usbd_endpoint_transferSize);
+OTG_EXPORT_SYMBOL(usbd_otg_bmattributes);
+OTG_EXPORT_SYMBOL(usbd_endpoint_update);
diff -uNr linux/drivers/no-otg/otgcore/usbp-procfs.c linux/drivers/otg/otgcore/usbp-procfs.c
--- linux/drivers/no-otg/otgcore/usbp-procfs.c	1970-01-01 01:00:00.000000000 +0100
+++ linux/drivers/otg/otgcore/usbp-procfs.c	2006-09-01 21:41:35.000000000 +0200
@@ -0,0 +1,302 @@
+/*
+ * otg/otgcore/usbp-procfs.c - USB Device Core Layer
+ *
+ *      Copyright (c) 2004 Belcarra
+ *
+ * By: 
+ *      Stuart Lynne <sl@belcarra.com>, 
+ *      Tom Rushworth <tbr@belcarra.com>, 
+ *      Bruce Balden <balden@belcarra.com>
+ *
+ */
+/*!
+ * @file otg/otgcore/usbp-procfs.c
+ * @brief Implements /proc/usbd-functions, which displays descriptors for the current selected function.
+ *
+ *
+ * @ingroup USBP
+ */
+
+#include <otg/otg-compat.h>
+#include <otg/otg-module.h>
+
+#ifdef LINUX24
+EXPORT_NO_SYMBOLS;
+#endif
+
+
+MOD_AUTHOR ("sl@belcarra.com, tbr@belcarra.com");
+MOD_DESCRIPTION ("USB Device Core Support Procfs");
+EMBED_LICENSE();
+
+#include <otg/usbp-chap9.h>
+#include <otg/usbp-func.h>
+#include <otg/usbp-bus.h>
+
+EMBED_USBD_INFO ("usbdprocfs 2.0-beta");
+
+#define MAX_INTERFACES 2
+
+extern struct usbd_bus_instance *usbd_bus_instance;
+
+/* Proc Filesystem *************************************************************************** */
+/* *
+ * dohex
+ *
+ */
+static void dohexdigit (char *cp, unsigned char val)
+{
+        if (val < 0xa) {
+                *cp = val + '0';
+        } else if ((val >= 0x0a) && (val <= 0x0f)) {
+                *cp = val - 0x0a + 'a';
+        }
+}
+
+/* *
+ * dohex
+ *
+ */
+static void dohexval (char *cp, unsigned char val)
+{
+        dohexdigit (cp++, val >> 4);
+        dohexdigit (cp++, val & 0xf);
+}
+
+/* *
+ * dump_descriptor
+ */
+static int dump_descriptor (char *buf, char *sp)
+{
+        int num;
+        int len = 0;
+
+        RETURN_ZERO_UNLESS(sp);
+
+        num = *sp;
+
+        //printk(KERN_INFO"%s: %p %d %d %d\n", __FUNCTION__, buf, *buf, buf[0], num);
+
+        while (sp && num--) {
+                dohexval (buf, *sp++);
+                buf += 2;
+                *buf++ = ' ';
+                len += 3;
+        }
+        len++;
+        *buf = '\n';
+        return len;
+}
+
+/* dump_descriptors
+ */
+static int dump_config_descriptor(char *buf, char *sp)
+{
+        struct usbd_configuration_descriptor *config = (struct usbd_configuration_descriptor *) sp;
+
+        int wTotalLength = le16_to_cpu(config->wTotalLength);
+        int bConfigurationValue = config->bConfigurationValue;
+        int interface_num;
+        int class_num;
+        int endpoint_num;
+        int total;
+
+        interface_num = class_num = endpoint_num = 0;
+
+        for (total = 0; wTotalLength > 0; ) {
+                //printk(KERN_INFO"%s: wTotalLength: %d total: %d bLength: %d type: %d\n",
+                //                __FUNCTION__, wTotalLength, total, sp[0], sp[1]);
+                BREAK_UNLESS(sp[0]);
+                switch (sp[1]) {
+                case USB_DT_CONFIGURATION:
+                        interface_num = class_num = endpoint_num = 0;
+                        total += sprintf(buf + total, "\nConfiguration descriptor [%d      ] ",
+                                        bConfigurationValue);
+                        break;
+                case USB_DT_INTERFACE:
+                        class_num = 0;
+                        total += sprintf(buf + total, "\nInterface descriptor     [%d:%d:%d  ] ",
+                                        bConfigurationValue, ++interface_num, class_num);
+                        break;
+                case USB_DT_ENDPOINT:
+                        class_num = endpoint_num = 0;
+                        total += sprintf(buf + total, "Endpoint descriptor      [%d:%d:%d:%d] ",
+                                        bConfigurationValue, interface_num, class_num, ++endpoint_num);
+                        break;
+                case USB_DT_OTG:
+                        class_num = endpoint_num = 0;
+                        total += sprintf(buf + total, "OTG descriptor           [%d      ] ", bConfigurationValue);
+                        break;
+                default:
+                        endpoint_num = 0;
+                        total += sprintf(buf + total, "Class descriptor         [%d:%d:%d  ] ",
+                                        bConfigurationValue, interface_num, ++class_num);
+                        break;
+                }
+                total += dump_descriptor(buf + total, sp);
+                wTotalLength -= sp[0];
+                sp += sp[0];
+        }
+        total += sprintf(buf + total, "\n");
+        return total;
+}
+
+/*! *
+ * usbd_device_proc_read - implement proc file system read.
+ * @param file
+ * @param buf
+ * @param count
+ * @param pos
+ *
+ * Standard proc file system read function.
+ *
+ * We let upper layers iterate for us, *pos will indicate which device to return
+ * statistics for.
+ */
+static ssize_t usbd_device_proc_read_functions (struct file *file, char *buf, size_t count, loff_t * pos)
+{
+        unsigned long page;
+        int len = 0;
+        int index;
+
+        u8 config_descriptor[512];
+        int config_size;
+
+        //struct list_head *lhd;
+
+        // get a page, max 4095 bytes of data...
+//        if (!(page = GET_FREE_PAGE(GFP_KERNEL))) {
+
+        if (!(page = GET_KERNEL_PAGE())) {
+                return -ENOMEM;
+        }
+
+        len = 0;
+        index = (*pos)++;
+
+        if (index == 0) {
+                len += sprintf ((char *) page + len, "usb-device list\n");
+        }
+
+        //printk(KERN_INFO"%s: index: %d len: %d\n", __FUNCTION__, index, len);
+
+        if (usbd_bus_instance && usbd_bus_instance->function_instance) {
+                int configuration = index;
+                struct usbd_function_instance *function_instance = usbd_bus_instance->function_instance;
+                struct usbd_function_driver *function_driver = function_instance->function_driver;
+                struct usbd_configuration_instance *configuration_instance_array = function_driver->configuration_instance_array;
+                if (configuration_instance_array) {
+
+                        if (index == 0) {
+                                len += sprintf ((char *) page + len, "\nDevice descriptor                  ");
+                                len += dump_descriptor ((char *) page + len, (char *) function_driver->device_descriptor);
+#ifdef CONFIG_OTG_HIGH_SPEED
+                                len += sprintf ((char *) page + len, "\nDevice Qualifier descriptor        ");
+                                len += dump_descriptor ((char *) page + len, 
+                                                (char *) function_driver->device_qualifier_descriptor);
+#endif
+                        }
+                        if (configuration < function_driver->bNumConfigurations) {
+
+                                if ((config_size = usbd_old_get_descriptor(usbd_bus_instance->ep0, config_descriptor, 
+                                                                sizeof(config_descriptor),
+                                                                USB_DT_CONFIGURATION, 0)) > index) {
+                                        len += dump_config_descriptor((char *)page + len, config_descriptor );
+                                }
+#ifdef CONFIG_OTG_HIGH_SPEED
+                                if ((config_size = usbd_old_get_descriptor(usbd_bus_instance->ep0, config_descriptor, 
+                                                                sizeof(config_descriptor),
+                                                                USB_DT_OTHER_SPEED_CONFIGURATION, index)) > 0) {
+                                        len += dump_config_descriptor((char *)page + len, config_descriptor );
+                                }
+
+#endif
+                        }
+                        else if (configuration == function_driver->bNumConfigurations) {
+                                int i;
+                                int k;
+                                struct usbd_string_descriptor *string_descriptor;
+
+                                //len += sprintf ((char *) page + len, "\n\n");
+
+                                if ((string_descriptor = usbd_get_string_descriptor (0)) != NULL) {
+                                        len += sprintf ((char *) page + len, "String                   [%2d]      ", 0);
+
+                                        for (k = 0; k < (string_descriptor->bLength / 2) - 1; k++) {
+                                                len += sprintf ((char *) page + len, "%02x %02x ", 
+                                                                string_descriptor->wData[k] >> 8, 
+                                                                string_descriptor->wData[k] & 0xff);
+                                                len++;
+                                        }
+                                        len += sprintf ((char *) page + len, "\n");
+                                }
+
+                                for (i = 1; i < usbd_maxstrings; i++) {
+
+                                        if ((string_descriptor = usbd_get_string_descriptor (i)) != NULL) {
+
+                                                len += sprintf((char *)page+len, "String                   [%2d:%2d]   ", 
+                                                                i, string_descriptor->bLength);
+
+                                                // bLength = sizeof(struct usbd_string_descriptor) + 2*strlen(str)-2;
+
+                                                for (k = 0; k < (string_descriptor->bLength / 2) - 1; k++) {
+                                                        *(char *) (page + len) = (char) string_descriptor->wData[k];
+                                                        len++;
+                                                }
+                                                len += sprintf ((char *) page + len, "\n");
+                                        }
+                                }
+                                len += sprintf((char *)page + len, "\n--\n"); 
+                        }
+                }
+        }
+
+        //printk(KERN_INFO"%s: len: %d count: %d\n", __FUNCTION__, len, count);
+
+        if (len > count) {
+                //printk(KERN_INFO"%s: len > count\n", __FUNCTION__);
+                //printk(KERN_INFO"%s", page);
+                len = -EINVAL;
+        } 
+        else if ((len > 0) && copy_to_user (buf, (char *) page, len)) {
+                //printk(KERN_INFO"%s: EFAULT\n", __FUNCTION__);
+                len = -EFAULT;
+        }
+        else {
+                //printk(KERN_INFO"%s: OK\n", __FUNCTION__);
+        }
+        free_page (page);
+        return len;
+}
+
+/* Module init ******************************************************************************* */
+#if defined(OTG_WINCE)
+
+#else /* defined(OTG_WINCE) */
+
+static struct file_operations usbd_device_proc_operations_functions = {
+        read:usbd_device_proc_read_functions,
+};
+
+static int usbd_procfs_init (void)
+{
+        struct proc_dir_entry *p;
+        // create proc filesystem entries
+        if ((p = create_proc_entry ("usb-functions", 0, 0)) == NULL)
+                return -ENOMEM;
+        p->proc_fops = &usbd_device_proc_operations_functions;
+        return 0;
+}
+
+static void usbd_procfs_exit (void)
+{
+        // remove proc filesystem entry
+        remove_proc_entry ("usb-functions", NULL);
+}
+
+module_init (usbd_procfs_init);
+module_exit (usbd_procfs_exit);
+
+#endif /* defined(OTG_WINCE) */
+
diff -uNr linux/drivers/no-otg/otghw/atlas-hardware.h linux/drivers/otg/otghw/atlas-hardware.h
--- linux/drivers/no-otg/otghw/atlas-hardware.h	1970-01-01 01:00:00.000000000 +0100
+++ linux/drivers/otg/otghw/atlas-hardware.h	2006-09-01 21:41:35.000000000 +0200
@@ -0,0 +1,59 @@
+/*
+ * otg/include/atlas-hardware.h -- Atlas Transceiver hardware specific defines
+ *
+ *      Copyright (c) 2004 Belcarra
+ *
+ * By: 
+ *      Stuart Lynne <sl@lbelcarra.com>, 
+ *      Tom Rushworth <tbr@belcarra.com>, 
+ *      Bruce Balden <balden@belcarra.com>
+ *
+ */
+/*!
+ * @defgroup ATLAS
+ * @ingroup tcdgroup
+ */
+/*!
+ * @file otghw/atlas-hardware.h
+ * @brief Hardware defines for Freescale USBOTG Hardware
+ *
+ * @ingroup ATLAS
+ */
+
+/* References are to:
+ *
+ *      Atlas DTS 0.7 - 04/02/27
+ */
+
+/*
+ * SPI Register Summary
+ */
+
+#define ATLAS_FSENB             (1 << 0)
+#define ATLAS_USB_SUSPEND       (1 << 1)
+#define ATLAS_USB_PU            (1 << 2)
+#define ATLAS_UDP_PD            (1 << 3)
+#define ATLAS_UDM_PD            (1 << 4)
+#define ATLAS_PULLOVR           (1 << 5)
+#define ATLAS_VUSB_EN           (1 << 6)
+#define ATLAS_USB_PS            (1 << 7)
+#define ATLAS_VBUS_REG_EN       (1 << 8)
+#define ATLAS_VBUS_PD_ENB       (1 << 9)
+#define ATLAS_CURRLIM           (1 << 10)
+#define ATLAS_SE0_CONN          (1 << 11)
+#define ATLAS_DLP_SRP           (1 << 12)
+#define ATLAS_USB_XCVR_EN       (1 << 13)
+#define ATLAS_VBUS_REG_LVL      (1 << 14)
+
+#define ATLAS_DATA_MODE_MASK    (3 << 15)
+#define ATLAS_DATA_MODE_USE     (0 << 15)
+#define ATLAS_DATA_MODE_BSE     (1 << 15)
+#define ATLAS_DATA_MODE_DM      (2 << 15)
+#define ATLAS_DATA_MODE_NU      (3 << 15)
+
+#define ATLAS_RS232ENB          (1 << 17)
+#define ATLAS_ID_INTERRUPT      (1 << 20)
+#define ATLAS_AUDIO_EN          (1 << 21)
+#define ATLAS_STEREO_EN         (1 << 22)
+
+
diff -uNr linux/drivers/no-otg/otghw/bvd-hardware.h linux/drivers/otg/otghw/bvd-hardware.h
--- linux/drivers/no-otg/otghw/bvd-hardware.h	1970-01-01 01:00:00.000000000 +0100
+++ linux/drivers/otg/otghw/bvd-hardware.h	2006-09-01 21:41:35.000000000 +0200
@@ -0,0 +1,601 @@
+/*
+ * otg/otghw/bvd-hardware.h
+ *
+ *      Copyright (c) 2004 Belcarra
+ *
+ *
+ * TODO
+ *
+ * 1. see if using UDCCSR0_ACM/AREN will help
+ *
+ */
+/*!
+ * @file otghw/bvd-hardware.h
+ * @brief Hardware defines for Intel PXA-270 Hardware
+ *
+ * @ingroup BVD
+ */
+
+
+#ifndef UDCOTGICR
+#define UDCOTGICR       __REG(0x40600018) /* UDC On-The-Go interrupt control */
+#endif
+
+#ifndef UDCOTGISR
+#define UDCOTGISR       __REG(0x4060001c) 
+
+#define UDCOTGISR_IRSF  (1 << 24)       /* OTG SET_FEATURE command recvd */
+#define UDCOTGISR_IRXR  (1 << 17)       /* Extra Transciever Interrupt Rising Edge Interrupt Enable */
+#define UDCOTGISR_IRXF  (1 << 16)       /* Extra Transciever Interrupt Falling Edge Interrupt Enable */
+#define UDCOTGISR_IRVV40R (1 << 9)      /* OTG Vbus Valid 4.0V Rising Edge Interrupt Enable */
+#define UDCOTGISR_IRVV40F (1 << 8)      /* OTG Vbus Valid 4.0V Falling Edge Interrupt Enable */
+#define UDCOTGISR_IRVV44R (1 << 7)      /* OTG Vbus Valid 4.4V Rising Edge Interrupt Enable */
+#define UDCOTGISR_IRVV44F (1 << 6)      /* OTG Vbus Valid 4.4V Falling Edge Interrupt Enable */
+#define UDCOTGISR_IRSVR (1 << 5)        /* OTG Session Valid Rising Edge Interrupt Enable */
+#define UDCOTGISR_IRSVF (1 << 4)        /* OTG Session Valid Falling Edge Interrupt Enable */
+#define UDCOTGISR_IRSDR (1 << 3)        /* OTG A-Device SRP Detect Rising Edge Interrupt Enable */
+#define UDCOTGISR_IRSDF (1 << 2)        /* OTG A-Device SRP Detect Falling Edge Interrupt Enable */
+#define UDCOTGISR_IRIDR (1 << 1)        /* OTG ID Change Rising Edge Interrupt Enable */
+#define UDCOTGISR_IRIDF (1 << 0)        /* OTG ID Change Falling Edge */
+ 
+
+#endif
+
+
+#ifndef UP2OCR
+#define UP2OCR          __REG(0x40600020) 
+
+#define UP2OCR_SEOS_OFF         (0x00 << 24)
+#define UP2OCR_SEOS_DSEO        (0x02 << 24)
+#define UP2OCR_SEOS_HSEO        (0x03 << 24)
+#define UP2OCR_SEOS_DEXT        (0x04 << 24)
+#define UP2OCR_SEOS_HEXT        (0x05 << 24)
+#define UP2OCR_SEOS_DINT        (0x06 << 24)
+#define UP2OCR_SEOS_HINT        (0x07 << 24)
+
+#define UP2OCR_HXOE             (0x1 << 17)
+#define UP2OCR_HXS              (0x1 << 16)
+
+#define UP2OCR_IDON             (0x1 << 10)
+#define UP2OCR_EXSUS            (0x1 << 9)
+#define UP2OCR_EXSP             (0x1 << 8)
+#define UP2OCR_DMPUBE           (0x1 << 7)
+#define UP2OCR_DPPUBE           (0x1 << 6)
+#define UP2OCR_DMPUE            (0x1 << 5)
+#define UP2OCR_DPPUE            (0x1 << 4)
+#define UP2OCR_DMPDE            (0x1 << 3)
+#define UP2OCR_DPPDE            (0x1 << 2)
+#define UP2OCR_CPVPE            (0x1 << 1)
+#define UP2OCR_CPVEN            (0x1 << 0)
+
+#endif
+
+#ifndef UP3OCR
+#define UP3OCR          __REG(0x40600024)
+#endif
+
+/*
+ * UDCCSR0 Bit Definitions C.f. Sheet 1 or 3
+ */
+#define UDCCSR0_ACM             (1 << 9)
+#define UDCCSR0_AREN            (1 << 8)
+
+
+/*
+ * Mainstone Miscellanous Write Register (MSCWR2) C.f. 3.2.2.6 (DVK)
+ *
+ * USB_OTG_RST  enable external usb otg transceiver
+ * USB_OTG_SEL  enable usb otg (disables ffuart)
+ * nUSBC_SC     enable usb client detection
+ */
+#ifndef MSCWR2_USB_OTG_SEL
+#define MSCWR2_USB_OTG_SEL MST_MSCWR2_USB_OTG_SEL
+#endif
+#ifndef MSCWR2_USB_OTG_RST
+#define MSCWR2_USB_OTG_RST MST_MSCWR2_USB_OTG_RST
+#endif
+#ifndef MSCRD1_USB_CBL
+#define MSCRD1_USB_CBL MST_MSCRD_USB_CBL
+#endif
+#ifndef MSCWR2_nUSBC_SC
+#define MSCWR2_nUSBC_SC MST_MSCWR2_nUSBC_SC
+#endif
+
+/*
+ * USB_P2_N GPIO configuration - C.f. 24.2
+ *
+ * These should be in
+ */
+#define GPIO34_USB_P2_2   (34 | GPIO_ALT_FN_1_OUT)
+#define GPIO35_USB_P2_1   (35 | GPIO_ALT_FN_2_IN)
+#define GPIO36_USB_P2_4   (36 | GPIO_ALT_FN_1_OUT)
+#define GPIO37_USB_P2_8   (37 | GPIO_ALT_FN_1_OUT)
+#define GPIO38_USB_P2_3   (38 | GPIO_ALT_FN_3_IN)
+#define GPIO39_USB_P2_6   (39 | GPIO_ALT_FN_1_OUT)
+#define GPIO40_USB_P2_5   (40 | GPIO_ALT_FN_3_IN)
+#define GPIO41_USB_P2_7   (41 | GPIO_ALT_FN_2_IN)
+
+
+#if 0
+/* UDC endpiont0 states */
+#define EP0_WAIT_FOR_SETUP          0
+#define EP0_DATA_STATE_XMIT         1
+#define EP0_DATA_STATE_NEED_ZLP     2
+#define EP0_WAIT_FOR_OUT_STATUS     3
+
+/* UDC register definitions */
+#define UDCCR           __REG(0x40600000) /* UDC Control Register */
+#define UDCICR0         __REG(0x40600004) /* UDC Interrupt Control Register0 */
+#define UDCICR1         __REG(0x40600008) /* UDC Interrupt Control Register1 */
+#define UDCISR0         __REG(0x4060000C) /* UDC Interrupt Status Register 0 */
+#define UDCISR1         __REG(0x40600010) /* UDC Interrupt Status Register 1 */
+#define UDCFNR          __REG(0x40600014) /* UDC Frame Number Register */
+
+#define UDCCSR0         __REG(0x40600100) /* UDC Control/Status register - Endpoint 0 */
+#define UDCCSRA         __REG(0x40600104) /* UDC Control/Status register - Endpoint A */
+#define UDCCSRB         __REG(0x40600108) /* UDC Control/Status register - Endpoint B */
+#define UDCCSRC         __REG(0x4060010C) /* UDC Control/Status register - Endpoint C */
+#define UDCCSRD         __REG(0x40600110) /* UDC Control/Status register - Endpoint D */
+#define UDCCSRE         __REG(0x40600114) /* UDC Control/Status register - Endpoint E */
+#define UDCCSRF         __REG(0x40600118) /* UDC Control/Status register - Endpoint F */
+#define UDCCSRG         __REG(0x4060011C) /* UDC Control/Status register - Endpoint G */
+#define UDCCSRH         __REG(0x40600120) /* UDC Control/Status register - Endpoint H */
+#define UDCCSRI         __REG(0x40600124) /* UDC Control/Status register - Endpoint I */
+#define UDCCSRJ         __REG(0x40600128) /* UDC Control/Status register - Endpoint J */
+#define UDCCSRK         __REG(0x4060012C) /* UDC Control/Status register - Endpoint K */
+#define UDCCSRL         __REG(0x40600130) /* UDC Control/Status register - Endpoint L */
+#define UDCCSRM         __REG(0x40600134) /* UDC Control/Status register - Endpoint M */
+#define UDCCSRN         __REG(0x40600138) /* UDC Control/Status register - Endpoint N */
+#define UDCCSRP         __REG(0x4060013C) /* UDC Control/Status register - Endpoint P */
+#define UDCCSRQ         __REG(0x40600140) /* UDC Control/Status register - Endpoint Q */
+#define UDCCSRR         __REG(0x40600144) /* UDC Control/Status register - Endpoint R */
+#define UDCCSRS         __REG(0x40600148) /* UDC Control/Status register - Endpoint S */
+#define UDCCSRT         __REG(0x4060014C) /* UDC Control/Status register - Endpoint T */
+#define UDCCSRU         __REG(0x40600150) /* UDC Control/Status register - Endpoint U */
+#define UDCCSRV         __REG(0x40600154) /* UDC Control/Status register - Endpoint V */
+#define UDCCSRW         __REG(0x40600158) /* UDC Control/Status register - Endpoint W */
+#define UDCCSRX         __REG(0x4060015C) /* UDC Control/Status register - Endpoint X */
+
+#define UDCBCR0         __REG(0x40600200) /* Byte Count Register - EP0 */
+#define UDCBCRA         __REG(0x40600204) /* Byte Count Register - EPA */
+#define UDCBCRB         __REG(0x40600208) /* Byte Count Register - EPB */
+#define UDCBCRC         __REG(0x4060020C) /* Byte Count Register - EPC */
+#define UDCBCRD         __REG(0x40600210) /* Byte Count Register - EPD */
+#define UDCBCRE         __REG(0x40600214) /* Byte Count Register - EPE */
+#define UDCBCRF         __REG(0x40600218) /* Byte Count Register - EPF */
+#define UDCBCRG         __REG(0x4060021C) /* Byte Count Register - EPG */
+#define UDCBCRH         __REG(0x40600220) /* Byte Count Register - EPH */
+#define UDCBCRI         __REG(0x40600224) /* Byte Count Register - EPI */
+#define UDCBCRJ         __REG(0x40600228) /* Byte Count Register - EPJ */
+#define UDCBCRK         __REG(0x4060022C) /* Byte Count Register - EPK */
+#define UDCBCRL         __REG(0x40600230) /* Byte Count Register - EPL */
+#define UDCBCRM         __REG(0x40600234) /* Byte Count Register - EPM */
+#define UDCBCRN         __REG(0x40600238) /* Byte Count Register - EPN */
+#define UDCBCRP         __REG(0x4060023C) /* Byte Count Register - EPP */
+#define UDCBCRQ         __REG(0x40600240) /* Byte Count Register - EPQ */
+#define UDCBCRR         __REG(0x40600244) /* Byte Count Register - EPR */
+#define UDCBCRS         __REG(0x40600248) /* Byte Count Register - EPS */
+#define UDCBCRT         __REG(0x4060024C) /* Byte Count Register - EPT */
+#define UDCBCRU         __REG(0x40600250) /* Byte Count Register - EPU */
+#define UDCBCRV         __REG(0x40600254) /* Byte Count Register - EPV */
+#define UDCBCRW         __REG(0x40600258) /* Byte Count Register - EPW */
+#define UDCBCRX         __REG(0x4060025C) /* Byte Count Register - EPX */
+
+#define UDCDR0          __REG(0x40600300) /* Data Register - EP0 */
+#define UDCDRA          __REG(0x40600304) /* Data Register - EPA */
+#define UDCDRB          __REG(0x40600308) /* Data Register - EPB */
+#define UDCDRC          __REG(0x4060030C) /* Data Register - EPC */
+#define UDCDRD          __REG(0x40600310) /* Data Register - EPD */
+#define UDCDRE          __REG(0x40600314) /* Data Register - EPE */
+#define UDCDRF          __REG(0x40600318) /* Data Register - EPF */
+#define UDCDRG          __REG(0x4060031C) /* Data Register - EPG */
+#define UDCDRH          __REG(0x40600320) /* Data Register - EPH */
+#define UDCDRI          __REG(0x40600324) /* Data Register - EPI */
+#define UDCDRJ          __REG(0x40600328) /* Data Register - EPJ */
+#define UDCDRK          __REG(0x4060032C) /* Data Register - EPK */
+#define UDCDRL          __REG(0x40600330) /* Data Register - EPL */
+#define UDCDRM          __REG(0x40600334) /* Data Register - EPM */
+#define UDCDRN          __REG(0x40600338) /* Data Register - EPN */
+#define UDCDRP          __REG(0x4060033C) /* Data Register - EPP */
+#define UDCDRQ          __REG(0x40600340) /* Data Register - EPQ */
+#define UDCDRR          __REG(0x40600344) /* Data Register - EPR */
+#define UDCDRS          __REG(0x40600348) /* Data Register - EPS */
+#define UDCDRT          __REG(0x4060034C) /* Data Register - EPT */
+#define UDCDRU          __REG(0x40600350) /* Data Register - EPU */
+#define UDCDRV          __REG(0x40600354) /* Data Register - EPV */
+#define UDCDRW          __REG(0x40600358) /* Data Register - EPW */
+#define UDCDRX          __REG(0x4060035C) /* Data Register - EPX */
+
+#define UDCCRA          __REG(0x40600404) /* Configuration register EPA */
+#define UDCCRB          __REG(0x40600408) /* Configuration register EPB */
+#define UDCCRC          __REG(0x4060040C) /* Configuration register EPC */
+#define UDCCRD          __REG(0x40600410) /* Configuration register EPD */
+#define UDCCRE          __REG(0x40600414) /* Configuration register EPE */
+#define UDCCRF          __REG(0x40600418) /* Configuration register EPF */
+#define UDCCRG          __REG(0x4060041C) /* Configuration register EPG */
+#define UDCCRH          __REG(0x40600420) /* Configuration register EPH */
+#define UDCCRI          __REG(0x40600424) /* Configuration register EPI */
+#define UDCCRJ          __REG(0x40600428) /* Configuration register EPJ */
+#define UDCCRK          __REG(0x4060042C) /* Configuration register EPK */
+#define UDCCRL          __REG(0x40600430) /* Configuration register EPL */
+#define UDCCRM          __REG(0x40600434) /* Configuration register EPM */
+#define UDCCRN          __REG(0x40600438) /* Configuration register EPN */
+#define UDCCRP          __REG(0x4060043C) /* Configuration register EPP */
+#define UDCCRQ          __REG(0x40600440) /* Configuration register EPQ */
+#define UDCCRR          __REG(0x40600444) /* Configuration register EPR */
+#define UDCCRS          __REG(0x40600448) /* Configuration register EPS */
+#define UDCCRT          __REG(0x4060044C) /* Configuration register EPT */
+#define UDCCRU          __REG(0x40600450) /* Configuration register EPU */
+#define UDCCRV          __REG(0x40600454) /* Configuration register EPV */
+#define UDCCRW          __REG(0x40600458) /* Configuration register EPW */
+#define UDCCRX          __REG(0x4060045C) /* Configuration register EPX */
+
+/* UDC Control Register (UDCCR) */
+#define UDCCR_DWRE      (1 <<16)        /* Device Remote Wake-up Feature */
+#define UDCCR_ACN       (1 <<11)        /* Active UDC Configuration Number */
+#define UDCCR_AIN       (1 << 8)        /* Active UDC Interface Number */
+#define	UDCCR_AAISN     (1 << 5)        /* Active UDC Alternative Interface Setting Number */
+#define UDCCR_SMAC      (1 << 4)        /* Switch Endpoint Memory to Active Configuration */
+#define UDCCR_EMCE      (1 << 3)        /* Endpoint Meory Configuration Error */
+#define UDCCR_UDR       (1 << 2)        /* UDC Resume */
+#define UDCCR_UDA       (1 << 1)        /* UDC Active */
+#define UDCCR_UDE       (1 << 0)        /* UDC Enable */
+
+#define UDCCR_ACN_MASK       0x3
+#define UDCCR_AIN_MASK       0x7
+#define UDCCR_AAISN_MASK     0x7
+
+#define UDCICR0_IE0      (1<<0)       /* EP0 Interrupt Enable */
+#define UDCICR0_IEA      (1<<2)       /* EPA Interrupt Enable */
+#define UDCICR0_IEB      (1<<4)       /* EPB Interrupt Enable */
+#define UDCICR0_IEC      (1<<6)       /* EPC Interrupt Enable */
+#define UDCICR0_IED      (1<<8)       /* EPD Interrupt Enable */
+#define UDCICR0_IEE      (1<<10)       /* EPE Interrupt Enable */
+#define UDCICR0_IEF      (1<<12)       /* EPF Interrupt Enable */
+#define UDCICR0_IEG      (1<<14)       /* EPG Interrupt Enable */
+#define UDCICR0_IEH      (1<<16)       /* EPH Interrupt Enable */
+#define UDCICR0_IEI      (1<<18)       /* EPI Interrupt Enable */
+#define UDCICR0_IEJ      (1<<20)       /* EPJ Interrupt Enable */
+#define UDCICR0_IEK      (1<<22)       /* EPK Interrupt Enable */
+#define UDCICR0_IEL      (1<<24)       /* EPL Interrupt Enable */
+#define UDCICR0_IEM      (1<<26)       /* EPM Interrupt Enable */
+#define UDCICR0_IEN      (1<<28)       /* EPN Interrupt Enable */
+#define UDCICR0_IEP      (1<<30)       /* EPP Interrupt Enable */
+#define UDCICR1_IEQ      (1<<0)        /* EPQ Interrupt Enable */
+#define UDCICR1_IER      (1<<2)        /* EPR Interrupt Enable */
+#define UDCICR1_IES      (1<<4)        /* EPS Interrupt Enable */
+#define UDCICR1_IET      (1<<6)        /* EPT Interrupt Enable */
+#define UDCICR1_IEU      (1<<8)        /* EPU Interrupt Enable */
+#define UDCICR1_IEV      (1<<10)        /* EPV Interrupt Enable */
+#define UDCICR1_IEW      (1<<12)        /* EPW Interrupt Enable */
+#define UDCICR1_IEX      (1<<14)        /* EPX Interrupt Enable */
+#define UDCICR1_IERS     (1<<27)        /* Reset */
+#define UDCICR1_IESU     (1<<28)        /* Suspend */
+#define UDCICR1_IERU     (1<<29)        /* Resume */
+#define UDCICR1_IESOF    (1<<30)        /* Start Of Frame */
+#define UDCICR1_IECC     (1<<31)        /* Configuration Changed */
+
+#define UDCISR0_IE0      (1<<0)       /* EP0 Interrupt Enable */
+#define UDCISR0_IEA      (1<<2)       /* EPA Interrupt Enable */
+#define UDCISR0_IEB      (1<<4)       /* EPB Interrupt Enable */
+#define UDCISR0_IEC      (1<<6)       /* EPC Interrupt Enable */
+#define UDCISR0_IED      (1<<8)       /* EPD Interrupt Enable */
+#define UDCISR0_IEE      (1<<10)       /* EPE Interrupt Enable */
+#define UDCISR0_IEF      (1<<12)       /* EPF Interrupt Enable */
+#define UDCISR0_IEG      (1<<14)       /* EPG Interrupt Enable */
+#define UDCISR0_IEH      (1<<16)       /* EPH Interrupt Enable */
+#define UDCISR0_IEI      (1<<18)       /* EPI Interrupt Enable */
+#define UDCISR0_IEJ      (1<<20)       /* EPJ Interrupt Enable */
+#define UDCISR0_IEK      (1<<22)       /* EPK Interrupt Enable */
+#define UDCISR0_IEL      (1<<24)       /* EPL Interrupt Enable */
+#define UDCISR0_IEM      (1<<26)       /* EPM Interrupt Enable */
+#define UDCISR0_IEN      (1<<28)       /* EPN Interrupt Enable */
+#define UDCISR0_IEP      (1<<30)       /* EPP Interrupt Enable */
+#define UDCISR1_IEQ      (1<<0)        /* EPQ Interrupt Enable */
+#define UDCISR1_IER      (1<<2)        /* EPR Interrupt Enable */
+#define UDCISR1_IES      (1<<4)        /* EPS Interrupt Enable */
+#define UDCISR1_IET      (1<<6)        /* EPT Interrupt Enable */
+#define UDCISR1_IEU      (1<<8)        /* EPU Interrupt Enable */
+#define UDCISR1_IEV      (1<<10)        /* EPV Interrupt Enable */
+#define UDCISR1_IEW      (1<<12)        /* EPW Interrupt Enable */
+#define UDCISR1_IEX      (1<<14)        /* EPX Interrupt Enable */
+#define UDCISR1_IERS     (1<<27)        /* Reset */
+#define UDCISR1_IESU     (1<<28)        /* Suspend */
+#define UDCISR1_IERU     (1<<29)        /* Resume */
+#define UDCISR1_IESOF    (1<<30)        /* Start Of Frame */
+#define UDCISR1_IECC     (1<<31)        /* Configuration Changed */
+
+#define UDC_INT_FIFOERROR  (0x2)
+#define UDC_INT_PACKETCMP  (0x1)
+
+#define UDC_FNR_MASK     (0x7ff)
+
+/* UDC Endpoint 0 Control Status Register (UDCCS0) */
+#define	UDCCSR0_OPR      (1 << 0)	/* OUT packet ready */
+#define	UDCCSR0_IPR	(1 << 1)	/* IN packet ready */
+#define	UDCCSR0_FTF	(1 << 2)	/* Flush Tx FIFO */
+#define	UDCCSR0_DME	(1 << 3)	/* DMA Enable */
+#define	UDCCSR0_SST	(1 << 4)	/* Sent stall */
+#define	UDCCSR0_FST	(1 << 5)	/* Force stall */
+#define	UDCCSR0_RNE	(1 << 6)	/* Receive FIFO not empty */
+#define	UDCCSR0_SA	(1 << 7)	/* Setup Active */
+
+/* UDC Endpoint A-X */
+#define	UDCCSR_FS	(1 << 0)	/* FIFO service */
+#define	UDCCSR_PC	(1 << 1)	/* Packet complete */
+#define	UDCCSR_TRN	(1 << 2)	/* Tx/Rx NAK */
+#define	UDCCSR_DME	(1 << 3)	/* DMA Enable */
+#define	UDCCSR_SST	(1 << 4)	/* Sent STALL */
+#define	UDCCSR_FST	(1 << 5)	/* Force STALL */
+#define UDCCSR_BNE       (1 << 6)        /* Buffer Not Empty */
+#define UDCCSR_BNF       (1 << 6)        /* Buffer Not Full */
+#define	UDCCSR_SP	(1 << 7)	/* Short packet */
+#define	UDCCSR_FEF	(1 << 8)	/* Flush Endpoint FIFO */
+#define UDCCSR_DPE       (1 << 9)        /* Data Packet Error */
+
+#define UDCCSR_WR_MASK   (UDCCSR_DME|UDCCSR_FST)
+#define UDC_BCR_MASK    (0x3ff)
+
+#define UDCCONR_EE      (1 << 0)        /* Endpoint Enable */
+#define UDCCONR_DE      (1 << 1)        /* Double-buffering Enable */
+#define UDCCONR_MPS     (1 << 2)        /* Maximum Packet Size */
+#define UDCCONR_ED      (1 <<12)        /* USB Endpoint Direction */
+#define UDCCONR_ET      (1 <<13)        /* Endpoint Type */
+#define UDCCONR_EN      (1 <<15)        /* Endpoint Number */
+#define UDCCONR_AISN    (1 <<19)        /* Alternate Interface Number */
+#define UDCCONR_IN      (1 <<22)        /* Interface Number */
+#define UDCCONR_CN      (1 <<25)        /* Configuration Number */
+
+#define UDC_CON_CN_MASK       (0x3)
+#define UDC_CON_IN_MASK       (0x7)
+#define UDC_CON_AISN_MASK     (0x7)
+#define UDC_CON_EN_MASK       (0xf)
+#define UDC_CON_ET_MASK       (0x3)
+#define UDC_CON_MPS_MASK      (0x3ff)
+
+/* DMA Request to Channel Map Register */
+
+/* Endpoint 0 state definitions */
+#define WAIT_FOR_SETUP          0
+#define DATA_STATE_XMIT         1
+#define DATA_STATE_RECV         2
+#define DATA_STATE_PENDING_XMIT 3
+#define DATA_STATE_NEED_ZLP     4
+#define WAIT_FOR_OUT_STATUS     5
+
+#endif
+
+
+#define UDCCR           __REG(0x40600000) /* UDC Control Register */
+#define UDCCR_OEN	(1 << 31)	/* On-the-Go Enable */
+#define UDCCR_AALTHNP	(1 << 30)	/* A-device Alternate Host Negotiation
+					   Protocol Port Support */
+#define UDCCR_AHNP	(1 << 29)	/* A-device Host Negotiation Protocol
+					   Support */
+#define UDCCR_BHNP	(1 << 28)	/* B-device Host Negotiation Protocol
+					   Enable */
+#define UDCCR_DWRE	(1 << 16)	/* Device Remote Wake-up Enable */
+#define UDCCR_ACN	(0x03 << 11)	/* Active UDC configuration Number */
+#define UDCCR_ACN_S	11
+#define UDCCR_AIN	(0x07 << 8)	/* Active UDC interface Number */
+#define UDCCR_AIN_S	8
+#define UDCCR_AAISN	(0x07 << 5)	/* Active UDC Alternate Interface
+					   Setting Number */
+#define UDCCR_AAISN_S	5
+#define UDCCR_SMAC	(1 << 4)	/* Switch Endpoint Memory to Active
+					   Configuration */
+#define UDCCR_EMCE	(1 << 3)	/* Endpoint Memory Configuration
+					   Error */
+#define UDCCR_UDR	(1 << 2)	/* UDC Resume */
+#define UDCCR_UDA	(1 << 1)	/* UDC Active */
+#define UDCCR_UDE	(1 << 0)	/* UDC Enable */
+
+#define UDCICR0         __REG(0x40600004) /* UDC Interrupt Control Register0 */
+#define UDCICR1         __REG(0x40600008) /* UDC Interrupt Control Register1 */
+#define UDCICR_FIFOERR	(1 << 1)	/* FIFO Error interrupt for EP */
+#define UDCICR_PKTCOMPL (1 << 0)	/* Packet Complete interrupt for EP */
+
+#define UDC_INT_FIFOERROR  (0x2)
+#define UDC_INT_PACKETCMP  (0x1)
+
+#define UDCICR_INT(n,intr) (((intr) & 0x03) << (((n) & 0x0F) * 2))
+#define UDCICR1_IECC	(1 << 31)	/* IntEn - Configuration Change */
+#define UDCICR1_IESOF	(1 << 30)	/* IntEn - Start of Frame */
+#define UDCICR1_IERU	(1 << 29)	/* IntEn - Resume */
+#define UDCICR1_IESU	(1 << 28)	/* IntEn - Suspend */
+#define UDCICR1_IERS	(1 << 27)	/* IntEn - Reset */
+
+#define UDCISR0         __REG(0x4060000C) /* UDC Interrupt Status Register 0 */
+#define UDCISR1         __REG(0x40600010) /* UDC Interrupt Status Register 1 */
+#define UDCISR_INT(n,intr) (((intr) & 0x03) << (((n) & 0x0F) * 2))
+#define UDCISR1_IECC	(1 << 31)	/* IntEn - Configuration Change */
+#define UDCISR1_IESOF	(1 << 30)	/* IntEn - Start of Frame */
+#define UDCISR1_IERU	(1 << 29)	/* IntEn - Resume */
+#define UDCISR1_IESU	(1 << 28)	/* IntEn - Suspend */
+#define UDCISR1_IERS	(1 << 27)	/* IntEn - Reset */
+
+
+#define UDCFNR          __REG(0x40600014) /* UDC Frame Number Register */
+#define UDCOTGICR	__REG(0x40600018) /* UDC On-The-Go interrupt control */
+#define UDCOTGICR_IESF	(1 << 24)	/* OTG SET_FEATURE command recvd */
+#define UDCOTGICR_IEXR	(1 << 17)	/* Extra Transciever Interrupt
+					   Rising Edge Interrupt Enable */
+#define UDCOTGICR_IEXF	(1 << 16)	/* Extra Transciever Interrupt
+					   Falling Edge Interrupt Enable */
+#define UDCOTGICR_IEVV40R (1 << 9)	/* OTG Vbus Valid 4.0V Rising Edge
+					   Interrupt Enable */
+#define UDCOTGICR_IEVV40F (1 << 8)	/* OTG Vbus Valid 4.0V Falling Edge
+					   Interrupt Enable */
+#define UDCOTGICR_IEVV44R (1 << 7)	/* OTG Vbus Valid 4.4V Rising Edge
+					   Interrupt Enable */
+#define UDCOTGICR_IEVV44F (1 << 6)	/* OTG Vbus Valid 4.4V Falling Edge
+					   Interrupt Enable */
+#define UDCOTGICR_IESVR	(1 << 5)	/* OTG Session Valid Rising Edge
+					   Interrupt Enable */
+#define UDCOTGICR_IESVF	(1 << 4)	/* OTG Session Valid Falling Edge
+					   Interrupt Enable */
+#define UDCOTGICR_IESDR	(1 << 3)	/* OTG A-Device SRP Detect Rising
+					   Edge Interrupt Enable */
+#define UDCOTGICR_IESDF	(1 << 2)	/* OTG A-Device SRP Detect Falling
+					   Edge Interrupt Enable */
+#define UDCOTGICR_IEIDR	(1 << 1)	/* OTG ID Change Rising Edge
+					   Interrupt Enable */
+#define UDCOTGICR_IEIDF	(1 << 0)	/* OTG ID Change Falling Edge
+					   Interrupt Enable */
+
+#define UDCCSN(x)	__REG2(0x40600100, (x) << 2)
+#define UDCCSR0         __REG(0x40600100) /* UDC Control/Status register - Endpoint 0 */
+#define UDCCSR0_SA	(1 << 7)	/* Setup Active */
+#define UDCCSR0_RNE	(1 << 6)	/* Receive FIFO Not Empty */
+#define UDCCSR0_FST	(1 << 5)	/* Force Stall */
+#define UDCCSR0_SST	(1 << 4)	/* Sent Stall */
+#define UDCCSR0_DME	(1 << 3)	/* DMA Enable */
+#define UDCCSR0_FTF	(1 << 2)	/* Flush Transmit FIFO */
+#define UDCCSR0_IPR	(1 << 1)	/* IN Packet Ready */
+#define UDCCSR0_OPC	(1 << 0)	/* OUT Packet Complete */
+
+#define UDCCSRA         __REG(0x40600104) /* UDC Control/Status register - Endpoint A */
+#define UDCCSRB         __REG(0x40600108) /* UDC Control/Status register - Endpoint B */
+#define UDCCSRC         __REG(0x4060010C) /* UDC Control/Status register - Endpoint C */
+#define UDCCSRD         __REG(0x40600110) /* UDC Control/Status register - Endpoint D */
+#define UDCCSRE         __REG(0x40600114) /* UDC Control/Status register - Endpoint E */
+#define UDCCSRF         __REG(0x40600118) /* UDC Control/Status register - Endpoint F */
+#define UDCCSRG         __REG(0x4060011C) /* UDC Control/Status register - Endpoint G */
+#define UDCCSRH         __REG(0x40600120) /* UDC Control/Status register - Endpoint H */
+#define UDCCSRI         __REG(0x40600124) /* UDC Control/Status register - Endpoint I */
+#define UDCCSRJ         __REG(0x40600128) /* UDC Control/Status register - Endpoint J */
+#define UDCCSRK         __REG(0x4060012C) /* UDC Control/Status register - Endpoint K */
+#define UDCCSRL         __REG(0x40600130) /* UDC Control/Status register - Endpoint L */
+#define UDCCSRM         __REG(0x40600134) /* UDC Control/Status register - Endpoint M */
+#define UDCCSRN         __REG(0x40600138) /* UDC Control/Status register - Endpoint N */
+#define UDCCSRP         __REG(0x4060013C) /* UDC Control/Status register - Endpoint P */
+#define UDCCSRQ         __REG(0x40600140) /* UDC Control/Status register - Endpoint Q */
+#define UDCCSRR         __REG(0x40600144) /* UDC Control/Status register - Endpoint R */
+#define UDCCSRS         __REG(0x40600148) /* UDC Control/Status register - Endpoint S */
+#define UDCCSRT         __REG(0x4060014C) /* UDC Control/Status register - Endpoint T */
+#define UDCCSRU         __REG(0x40600150) /* UDC Control/Status register - Endpoint U */
+#define UDCCSRV         __REG(0x40600154) /* UDC Control/Status register - Endpoint V */
+#define UDCCSRW         __REG(0x40600158) /* UDC Control/Status register - Endpoint W */
+#define UDCCSRX         __REG(0x4060015C) /* UDC Control/Status register - Endpoint X */
+
+#define UDCCSR_DPE	(1 << 9)	/* Data Packet Error */
+#define UDCCSR_FEF	(1 << 8)	/* Flush Endpoint FIFO */
+#define UDCCSR_SP	(1 << 7)	/* Short Packet Control/Status */
+#define UDCCSR_BNE	(1 << 6)	/* Buffer Not Empty (IN endpoints) */
+#define UDCCSR_BNF	(1 << 6)	/* Buffer Not Full (OUT endpoints) */
+#define UDCCSR_FST	(1 << 5)	/* Force STALL */
+#define UDCCSR_SST	(1 << 4)	/* Sent STALL */
+#define UDCCSR_DME	(1 << 3)	/* DMA Enable */
+#define UDCCSR_TRN	(1 << 2)	/* Tx/Rx NAK */
+#define UDCCSR_PC	(1 << 1)	/* Packet Complete */
+#define UDCCSR_FS	(1 << 0)	/* FIFO needs service */
+
+#define UDCBCN(x)	__REG2(0x40600200, (x)<<2)
+#define UDCBCR0         __REG(0x40600200) /* Byte Count Register - EP0 */
+#define UDCBCRA         __REG(0x40600204) /* Byte Count Register - EPA */
+#define UDCBCRB         __REG(0x40600208) /* Byte Count Register - EPB */
+#define UDCBCRC         __REG(0x4060020C) /* Byte Count Register - EPC */
+#define UDCBCRD         __REG(0x40600210) /* Byte Count Register - EPD */
+#define UDCBCRE         __REG(0x40600214) /* Byte Count Register - EPE */
+#define UDCBCRF         __REG(0x40600218) /* Byte Count Register - EPF */
+#define UDCBCRG         __REG(0x4060021C) /* Byte Count Register - EPG */
+#define UDCBCRH         __REG(0x40600220) /* Byte Count Register - EPH */
+#define UDCBCRI         __REG(0x40600224) /* Byte Count Register - EPI */
+#define UDCBCRJ         __REG(0x40600228) /* Byte Count Register - EPJ */
+#define UDCBCRK         __REG(0x4060022C) /* Byte Count Register - EPK */
+#define UDCBCRL         __REG(0x40600230) /* Byte Count Register - EPL */
+#define UDCBCRM         __REG(0x40600234) /* Byte Count Register - EPM */
+#define UDCBCRN         __REG(0x40600238) /* Byte Count Register - EPN */
+#define UDCBCRP         __REG(0x4060023C) /* Byte Count Register - EPP */
+#define UDCBCRQ         __REG(0x40600240) /* Byte Count Register - EPQ */
+#define UDCBCRR         __REG(0x40600244) /* Byte Count Register - EPR */
+#define UDCBCRS         __REG(0x40600248) /* Byte Count Register - EPS */
+#define UDCBCRT         __REG(0x4060024C) /* Byte Count Register - EPT */
+#define UDCBCRU         __REG(0x40600250) /* Byte Count Register - EPU */
+#define UDCBCRV         __REG(0x40600254) /* Byte Count Register - EPV */
+#define UDCBCRW         __REG(0x40600258) /* Byte Count Register - EPW */
+#define UDCBCRX         __REG(0x4060025C) /* Byte Count Register - EPX */
+
+#define UDCDN(x)	__REG2(0x40600300, (x)<<2)
+#define PHYS_UDCDN(x)	(0x40600300 + ((x)<<2))
+#define PUDCDN(x)	(volatile u32 *)(io_p2v(PHYS_UDCDN((x))))
+#define UDCDR0          __REG(0x40600300) /* Data Register - EP0 */
+#define UDCDRA          __REG(0x40600304) /* Data Register - EPA */
+#define UDCDRB          __REG(0x40600308) /* Data Register - EPB */
+#define UDCDRC          __REG(0x4060030C) /* Data Register - EPC */
+#define UDCDRD          __REG(0x40600310) /* Data Register - EPD */
+#define UDCDRE          __REG(0x40600314) /* Data Register - EPE */
+#define UDCDRF          __REG(0x40600318) /* Data Register - EPF */
+#define UDCDRG          __REG(0x4060031C) /* Data Register - EPG */
+#define UDCDRH          __REG(0x40600320) /* Data Register - EPH */
+#define UDCDRI          __REG(0x40600324) /* Data Register - EPI */
+#define UDCDRJ          __REG(0x40600328) /* Data Register - EPJ */
+#define UDCDRK          __REG(0x4060032C) /* Data Register - EPK */
+#define UDCDRL          __REG(0x40600330) /* Data Register - EPL */
+#define UDCDRM          __REG(0x40600334) /* Data Register - EPM */
+#define UDCDRN          __REG(0x40600338) /* Data Register - EPN */
+#define UDCDRP          __REG(0x4060033C) /* Data Register - EPP */
+#define UDCDRQ          __REG(0x40600340) /* Data Register - EPQ */
+#define UDCDRR          __REG(0x40600344) /* Data Register - EPR */
+#define UDCDRS          __REG(0x40600348) /* Data Register - EPS */
+#define UDCDRT          __REG(0x4060034C) /* Data Register - EPT */
+#define UDCDRU          __REG(0x40600350) /* Data Register - EPU */
+#define UDCDRV          __REG(0x40600354) /* Data Register - EPV */
+#define UDCDRW          __REG(0x40600358) /* Data Register - EPW */
+#define UDCDRX          __REG(0x4060035C) /* Data Register - EPX */
+
+#define UDCCN(x)       __REG2(0x40600400, (x)<<2)
+#define UDCCRA          __REG(0x40600404) /* Configuration register EPA */
+#define UDCCRB          __REG(0x40600408) /* Configuration register EPB */
+#define UDCCRC          __REG(0x4060040C) /* Configuration register EPC */
+#define UDCCRD          __REG(0x40600410) /* Configuration register EPD */
+#define UDCCRE          __REG(0x40600414) /* Configuration register EPE */
+#define UDCCRF          __REG(0x40600418) /* Configuration register EPF */
+#define UDCCRG          __REG(0x4060041C) /* Configuration register EPG */
+#define UDCCRH          __REG(0x40600420) /* Configuration register EPH */
+#define UDCCRI          __REG(0x40600424) /* Configuration register EPI */
+#define UDCCRJ          __REG(0x40600428) /* Configuration register EPJ */
+#define UDCCRK          __REG(0x4060042C) /* Configuration register EPK */
+#define UDCCRL          __REG(0x40600430) /* Configuration register EPL */
+#define UDCCRM          __REG(0x40600434) /* Configuration register EPM */
+#define UDCCRN          __REG(0x40600438) /* Configuration register EPN */
+#define UDCCRP          __REG(0x4060043C) /* Configuration register EPP */
+#define UDCCRQ          __REG(0x40600440) /* Configuration register EPQ */
+#define UDCCRR          __REG(0x40600444) /* Configuration register EPR */
+#define UDCCRS          __REG(0x40600448) /* Configuration register EPS */
+#define UDCCRT          __REG(0x4060044C) /* Configuration register EPT */
+#define UDCCRU          __REG(0x40600450) /* Configuration register EPU */
+#define UDCCRV          __REG(0x40600454) /* Configuration register EPV */
+#define UDCCRW          __REG(0x40600458) /* Configuration register EPW */
+#define UDCCRX          __REG(0x4060045C) /* Configuration register EPX */
+
+#define UDCCONR_CN	(0x03 << 25)	/* Configuration Number */
+#define UDCCONR_CN_S	(25)
+#define UDCCONR_IN	(0x07 << 22)	/* Interface Number */
+#define UDCCONR_IN_S	(22)
+#define UDCCONR_AISN	(0x07 << 19)	/* Alternate Interface Number */
+#define UDCCONR_AISN_S	(19)
+#define UDCCONR_EN	(0x0f << 15)	/* Endpoint Number */
+#define UDCCONR_EN_S	(15)
+#define UDCCONR_ET	(0x03 << 13)	/* Endpoint Type: */
+#define UDCCONR_ET_S	(13)
+#define UDCCONR_ET_INT	(0x03 << 13)	/*   Interrupt */
+#define UDCCONR_ET_BULK	(0x02 << 13)	/*   Bulk */
+#define UDCCONR_ET_ISO	(0x01 << 13)	/*   Isochronous */
+#define UDCCONR_ET_NU	(0x00 << 13)	/*   Not used */
+#define UDCCONR_ED	(1 << 12)	/* Endpoint Direction */
+#define UDCCONR_MPS	(0x3ff << 2)	/* Maximum Packet Size */
+#define UDCCONR_MPS_S	(2)
+#define UDCCONR_DE	(1 << 1)	/* Double Buffering Enable */
+#define UDCCONR_EE	(1 << 0)	/* Endpoint Enable */
+
+
+#define UDC_INT_FIFOERROR  (0x2)
+#define UDC_INT_PACKETCMP  (0x1)
+
+#define UDC_FNR_MASK     (0x7ff)
+
+#define UDCCSR_WR_MASK   (UDCCSR_DME|UDCCSR_FST)
+#define UDC_BCR_MASK    (0x3ff)
+
+
diff -uNr linux/drivers/no-otg/otghw/cea-936.h linux/drivers/otg/otghw/cea-936.h
--- linux/drivers/no-otg/otghw/cea-936.h	1970-01-01 01:00:00.000000000 +0100
+++ linux/drivers/otg/otghw/cea-936.h	2006-09-01 21:41:35.000000000 +0200
@@ -0,0 +1,148 @@
+/*
+ * otg/carkit.h
+ *
+ *      Copyright (c) 2004 Belcarra
+ *
+ *
+ * Notes
+ *
+ * 1. Document references are to CEA-936a - Dec 12, 2003
+ *
+ */
+/*!
+ * @defgroup CEA
+ * @ingroup tcdgroup
+ */
+/*!
+ * @file otghw/cea-936.h
+ * @brief Hardware defines for CEA-936 Hardware
+ *
+ * @ingroup CEA
+ */
+
+
+/* 
+ * Command Descriptions - C.f. 10.3
+ */
+#define CARKIT_GET_DEVICE       0x01
+#define CARKIT_GET_CARKIT       0x02
+#define CARKIT_AUDIO_EN         0x03
+#define CARKIT_SET_CRNT         0x04
+#define CARKIT_SET_UART         0x05
+#define CARKIT_CARKIT_MASTER    0x06
+
+/* 
+ * generic get command
+ */
+struct carkit_get {
+        u8      strt;
+        u8      command_type;
+        u8      crc;
+        u8      terminator;
+};
+
+struct carkit_set {
+        u8      strt;
+        u8      command_type;
+        u8      command_value;
+        u8      crc;
+        u8      terminator;
+};
+
+struct carkit_get_carkit_ack {
+        u8      ack;
+        u8      terminator;
+};
+
+/*
+ * Get Device - C.f 10.3.1
+ */
+
+struct carkit_get_device_carkit {
+        u8      ack;
+        u8      device_id;
+        u16     vendor_id;
+        u16     product_id;
+        u8      crc;
+        u8      terminator;
+};
+
+#define CARKIT_DEVICE_ID_GENERIC        0x01
+#define CARKIT_DEVICE_ID_ENHANCED       0x02
+#define CARKIT_DEVICE_ID_PROPRIETARY    0x03
+
+
+/*
+ * Get CarKit - C.f. 10.3.2
+ */
+
+struct carkit_get_carkit_carkit {
+        u8      ack;
+        u16     cr_ftrs;
+        u8      cr_sts;
+        u8      crc;
+        u8      terminator;
+};
+
+#define CARKIT_CR_FTRS_0_STERO          (1 << 0)
+#define CARKIT_CR_FTRS_0_CRNT_ADJ       (1 << 1)
+#define CARKIT_CR_FTRS_0_UART_MAX       (7 << 2)
+#define CARKIT_CR_FTRS_0_UART_MSTR      (1 << 5)
+
+#define CARKIT_CR_FTRS_1_PH_REMOVE_FTR  (1 << 0)
+#define CARKIT_CR_FTRS_1_CAR_OFF_FTR    (1 << 1)
+#define CARKIT_CR_FTRS_1_CRDL_BTN_FTR   (1 << 2)
+
+#define CARKIT_CR_PH_REMOVE_STS         (1 << 0)
+#define CARKIT_CR_CAR_OFF_STS           (1 << 1)
+#define CARKIT_CR_CRDL_BTN_STS          (1 << 2)
+#define CARKIT_CR_PH_RMV_CHG            (1 << 4)
+#define CARKIT_CR_CAR_OFF_CHG           (1 << 5)
+#define CARKIT_CR_CRDL_BTN_CHG          (1 << 6)
+
+
+/*
+ * Set Carkit - C.f. 10.3.3
+ */
+#define CARKIT_CR_CTL_PH_RMV_IEN        (1 << 0)
+#define CARKIT_CR_CAR_OFF_IEN           (1 << 1)
+#define CARKIT_CR_CRDL_BTN_IEN          (1 << 2)
+#define CARKIT_CR_CARKIT_MSTR           (1 << 5)
+#define CARKIT_CR_PULSE_ID              (1 << 6)
+#define CARKIT_CR_ID_INT_EN             (1 << 7)
+
+
+/*
+ * Audio En - C.f. 10.3.4
+ */
+#define CARKIT_AUDIO_MODE_MONO          (1 << 0)
+#define CARKIT_AUDIO_MODE_STEREO        (1 << 1)
+
+/*
+ * Set Crnt - C.f. 10.3.5
+ */
+#define CARKIT_CRNT_LMT                 (0xf)
+
+
+/*
+ * Set UART - C.f. 10.3.6
+ */
+#define CARKIT_UART_SPEED_9600          (1 << 0)
+#define CARKIT_UART_SPEED_19200         (1 << 1)
+#define CARKIT_UART_SPEED_38400         (1 << 3)
+#define CARKIT_UART_SPEED_57600         (1 << 4)
+#define CARKIT_UART_SPEED_115200        (1 << 4)
+
+
+/*
+ * Special chars - C.f. 10.4
+ */
+#define CARKIT_STRT                     0x41
+#define CARKIT_ESC                      0x18
+#define CARKIT_ACK                      0x06
+#define CARKIT_NYET                     0x30
+#define CARKIT_NAK                      0x15
+#define CARKIT_NSUP                     0x1f
+#define CARKIT_CR                       0x0d
+
+
diff -uNr linux/drivers/no-otg/otghw/fsotg-hardware.h linux/drivers/otg/otghw/fsotg-hardware.h
--- linux/drivers/no-otg/otghw/fsotg-hardware.h	1970-01-01 01:00:00.000000000 +0100
+++ linux/drivers/otg/otghw/fsotg-hardware.h	2006-09-01 21:41:35.000000000 +0200
@@ -0,0 +1,1054 @@
+/*
+ * otghw/fsotg-hardware.h
+ *
+ * Copyright (c) 2004 Belcarra
+ *
+ * By: 
+ *      Stuart Lynne <sl@belcarra.com>, 
+ *      Tom Rushworth <tbr@belcarra.com>, 
+ *      Bruce Balden <balden@belcarra.com>
+ *
+ */
+
+/*!
+ * @defgroup FSOTG Freescale USBOTG Support
+ * @ingroup libgroup
+ */
+/*!
+ * @file otghw/fsotg-hardware.h
+ * @brief Hardware defines for Freescale USBOTG Hardware
+ *
+ * This supports the Freescale implementation of the Trans Dimension 
+ * USBOTG.
+ *
+ * This is used on:
+ *
+ *      - i.MX21
+ *      - SCM-A11
+ *      - Argon+
+ *      - Zeus
+ *
+ * @ingroup FSOTG
+ */
+
+#if defined(CONFIG_ARCH_MX2ADS)
+#include <otghw/mx2ads.h>
+#include <otghw/mx2ads-hardware.h>
+#endif /* defined(CONFIG_ARCH_MX2ADS) */
+
+#if defined(CONFIG_ARCH_SCMA11EVB)
+#endif /* defined(CONFIG_ARCH_SCMA11EVB) */
+
+
+
+/*!
+ * @name OTG_SYS_CTRL
+ * C.f. 23.8 USB Control Register
+ */
+ /*! @{ */
+
+//#define OTG_SYS_CTRL                          (OTG_SYS_BASE+0x00)
+
+#define SYS_CTRL_I2C_WU_INT_STAT                (1 << 27)
+#define SYS_CTRL_OTG_WU_INT_STAT                (1 << 26)
+#define SYS_CTRL_HOST_WU_INT_STAT               (1 << 25)
+#define SYS_CTRL_FNT_WU_INT_STAT                (1 << 24)
+
+#define SYS_CTRL_I2C_WU_INT_EN                  (1 << 19)
+#define SYS_CTRL_OTG_WU_INT_EN                  (1 << 18)
+#define SYS_CTRL_HOST_WU_INT_EN                 (1 << 17)
+#define SYS_CTRL_FNT_WU_INT_EN                  (1 << 16)
+
+#define SYS_CTRL_OTG_BYP_VAL                    (0x3 << 11)
+#define SYS_CTRL_HOST1_BYP_VAL                  (0x3 << 9)
+
+#define SYS_CTRL_OTG_PWR_MASK                   (1 << 6)
+#define SYS_CTRL_HOST1_PWR_MASK                 (1 << 6)
+#define SYS_CTRL_HOST2_PWR_MASK                 (1 << 4)
+#define SYS_CTRL_USB_BYP                        (1 << 2)
+#define SYS_CTRL_HOST1_TXEN_OE                  (1 << 1)
+/*! @} */
+
+/*! 
+ * @name C.f. 23.9 USBOTG Module Registers
+ */
+ /*! @{ */
+
+#define OTG_CORE_HWMODE                         (OTG_CORE_BASE+0x00)            //  32bit core hardware mode reg
+
+#define XCVR_D_D                                0x00
+#define XCVR_SE_D                               0x01
+#define XCVR_D_SE                               0x02
+#define XCVR_SE_SE                              0x03
+
+#define MODULE_ANASDBEN                         (1 << 14)
+#define MODULE_OTGXCVR                          (0x3 << 6)
+#define MODULE_HOSTXCVR                         (0x3 << 4)
+#define MODULE_CRECFG                           (0x3)
+#define MODULE_CRECFG_HHNP                      (0x0)
+#define MODULE_CRECFG_HOST                      (0x1)
+#define MODULE_CRECFG_FUNC                      (0x2)
+#define MODULE_CRECFG_SHNP                      (0x3)
+
+#define OTG_CORE_CINT_STAT                      (OTG_CORE_BASE+0x04)            //  32bit core int status reg
+
+#define MODULE_FCINTDSPEN                       (1 << 6)
+
+#define MODULE_ASHNPINT                         (1 << 5)
+#define MODULE_ASFCINT                          (1 << 4)
+#define MODULE_ASHCINT                          (1 << 3)
+#define MODULE_HNPINT                           (1 << 2)
+#define MODULE_FCINT                            (1 << 1)
+#define MODULE_HCINT                            (1)
+
+#define OTG_CORE_CINT_STEN                      (OTG_CORE_BASE+0x08)            //  32bit core int enable reg
+
+#define MODULE_ASHNPINT_EN                      (1 << 5)
+#define MODULE_ASFCINT_EN                       (1 << 4)
+#define MODULE_ASHCINT_EN                       (1 << 3)
+#define MODULE_HNPINT_EN                        (1 << 2)
+#define MODULE_FCINT_EN                         (1 << 1)
+#define MODULE_HCINT_EN                         (1)
+
+#define OTG_CORE_CLK_CTRL                       (OTG_CORE_BASE+0x0C)            //  32bit core clock control reg
+
+#define MODULE_FUNC_CLK                         (1 << 2)
+#define MODULE_HOST_CLK                         (1 << 1)
+#define MODULE_MAIN_CLK                         (1)
+
+#define OTG_CORE_RST_CTRL                       (OTG_CORE_BASE+0x10)            // 32bit core reset control reg
+
+#define MODULE_RSTI2C                           (1 << 15)
+#define MODULE_RSTCTRL                          (1 << 5)
+#define MODULE_RSTFC                            (1 << 4)
+#define MODULE_RSTFSIE                          (1 << 3)
+#define MODULE_RSTRH                            (1 << 2)
+#define MODULE_RSTHSIE                          (1 << 1)
+#define MODULE_RSTHC                            (1)
+
+#define OTG_CORE_FRM_INTVL                      (OTG_CORE_BASE+0x14)            //  32bit core frame interval reg
+
+#define MODULE_RESET_FRAME                      (1 << 15)
+
+#define OTG_CORE_FRM_REMAIN                     (OTG_CORE_BASE+0x18)            //  32bit core frame remaining reg
+
+#define OTG_CORE_HNP_CSTAT                      (OTG_CORE_BASE+0x1C)            //  32bit core HNP current state reg
+
+#define MODULE_HNPDAT                           (1 << 30)
+#define MODULE_VBUSBSE                          (1 << 29)
+#define MODULE_VBUSABSV                         (1 << 28)
+#define MODULE_VBUSGTAVV                        (1 << 27)
+
+#define MODULE_ARMTHNPE                         (1 << 25)
+#define MODULE_BHNPEN                           (1 << 24)
+
+#define MODULE_SLAVE                            (1 << 22)
+#define MODULE_MASTER                           (1 << 21)
+#define MODULE_BGEN                             (1 << 20)
+#define MODULE_CMPEN                            (1 << 19)
+#define MODULE_ISBDEV                           (1 << 18)
+#define MODULE_ISADEV                           (1 << 17)
+
+#define MODULE_SWVBUSPUL                        (1 << 15)
+
+#define MODULE_SWAUTORST                        (1 << 12)
+#define MODULE_SWPUDP                           (1 << 11)
+
+#define MODULE_SWPDDM                           (1 << 9)
+#define MODULE_HNPSTATE                         (0x1f << 8)
+#define MODULE_CLRERROR                         (1 << 3)
+#define MODULE_ADROPBUS                         (1 << 2)
+#define MODULE_ABBUSREQ                         (1 << 1)
+
+#define HNP_A_IDLE                              (0x10 << 8)
+#define HNP_A_MASTER                            (0x11 << 8)
+#define HNP_A_SLAVE                             (0x12 << 8)
+#define HNP_A_WAIT_VPULSE                       (0x13 << 8)
+
+#define HNP_A_WAIT_DPULSE                       (0x14 << 8)
+#define HNP_A_WAIT_CONN_A                       (0x15 << 8)
+#define HNP_A_WAIT_CONN_B                       (0x16 << 8)
+#define HNP_A_WAIT_VRISE                        (0x17 << 8)
+#define HNP_A_SUSPEND                           (0x18 << 8)
+#define HNP_A_WAIT_VFALL                        (0x19 << 8)
+#define HNP_A_VBUS_ERR                          (0x1a << 8)
+#define HNP_CONN_DEBOUNCE                       (0x1b << 8)
+#define HNP_A_WAIT_ABREQ                        (0x1c << 8)
+#define HNP_B_IDLE                              (0x00 << 8)
+#define HNP_B_MASTER                            (0x01 << 8)
+#define HNP_B_SLAVE                             (0x02 << 8)
+#define HNP_B_SRP_INIT_V                        (0x03 << 8)
+#define HNP_B_SRP_INIT_D                        (0x04 << 8)
+#define HNP_B_PRE_WAIT_CONN_AB_SHORT_DB         (0x05 << 8)
+#define HNP_B_WAIT_CONN_A                       (0x06 << 8)
+#define HNP_B_PRE_SLAVE                         (0x0a << 8)
+
+
+#define OTG_CORE_HNP_TIMER1                     (OTG_CORE_BASE+0x20)            //  32bit core HNP timer 1 reg
+#define OTG_CORE_HNP_TIMER2                     (OTG_CORE_BASE+0x24)            //  32bit core HNP timer 2 reg
+#define OTG_CORE_HNP_T3PCR                      (OTG_CORE_BASE+0x28)            //  32bit core HNP timer 3 pulse ctrl
+
+#define HNP_DATAPULSE                           (1 << 5)
+#define HNP_VBUSPULSE                           (1 << 4)
+
+#define OTG_CORE_HINT_STAT                      (OTG_CORE_BASE+0x2C)            //  32bit core HNP int status reg
+
+#define HNP_I2COTGINT                           (1 << 15)
+#define HNP_AWAITBTO                            (1 << 8)                        // Hardware HNP Only
+#define HNP_AIDLEBDTO                           (1 << 7)                        // Hardware HNP Only
+#define HNP_SRPSUCFAIL                          (1 << 6)                        // Hardware HNP Only
+#define HNP_SRPINT                              (1 << 5)
+#define HNP_VBUSERROR                           (1 << 4)                        // Hardware HNP Only
+#define HNP_ABSEVAILD                           (1 << 3)
+#define HNP_ABUSVALID                           (1 << 2)
+#define HNP_MASSLVCHG                           (1 << 1)                        // Hardware HNP Only
+#define HNP_IDCHANGE                            (1)
+
+#define OTG_CORE_HINT_STEN                      (OTG_CORE_BASE+0x30)            //  32bit core HNP int enable reg
+
+#define HNP_I2COTGINT_EN                        (1 << 15)
+#define HNP_AWAITBTO_EN                         (1 << 8)
+#define HNP_AIDLEBDTO_EN                        (1 << 7)
+#define HNP_SRPSUCFAIL_EN                       (1 << 6)
+#define HNP_SRPINT_EN                           (1 << 5)
+#define HNP_VBUSERROR_EN                        (1 << 4)
+#define HNP_ABSEVAILD_EN                        (1 << 3)
+#define HNP_ABUSVALID_EN                        (1 << 2)
+#define HNP_MASSLVCHG_EN                        (1 << 1)
+#define HNP_IDCHANGE_EN                         (1)
+
+
+#define OTG_CORE_CPUEPSEL_STAT                  (OTG_CORE_BASE+0x34)
+#define OTG_CORE_INTERRUPT_STEN                 (OTG_CORE_BASE+0x3c)
+/*! @} */
+
+
+/*!
+ * @name C.f. 23.11.11 Host Registers
+ */
+ /*! @{ */
+
+
+#define OTG_HOST_CONTROL                        (OTG_HOST_BASE+0x00)     //  32bit host controller config reg
+
+#define HOST_CONTROL_HCRESET                    (1 << 31)
+#define HOST_CONTROL_RMTWUEN                    (1 << 4)
+
+#define HOST_CONTROL_HCUSBSTE_RESET             (0 << 2)
+#define HOST_CONTROL_HCUSBSTE_RESUME            (1 << 2)
+#define HOST_CONTROL_HCUSBSTE_OPERATIONAL       (2 << 2)
+#define HOST_CONTROL_HCUSBSTE_SUSPEND           (3 << 2)
+
+#define HOST_CONTROL_CTLBLKSR_11                (0)
+#define HOST_CONTROL_CTLBLKSR_21                (1)
+#define HOST_CONTROL_CTLBLKSR_41                (2)
+#define HOST_CONTROL_CTLBLKSR_81                (3)
+
+
+#define OTG_HOST_SINT_STAT                      (OTG_HOST_BASE+0x08)     //  32bit host system int status reg
+
+#define HOST_PSCINT                             (1 << 6)                // Port Status Change 
+#define HOST_FMOFINT                            (1 << 5)                // Frame Number Overflow
+#define HOST_HERRINT                            (1 << 4)                // Host Error
+#define HOST_RESDETINT                          (1 << 3)                // Resume Detected
+#define HOST_SOFINT                             (1 << 2)                // Start of Frame
+#define HOST_DONEINT                            (1 << 1)                // Done Register
+#define HOST_SORINT                             (1)                     // Schedule Overrun
+
+#define OTG_HOST_SINT_STEN                      (OTG_HOST_BASE+0x0C)     //  32bit host system int enable reg
+
+#define HOST_PSCINT_EN                          (1 << 6)                // Port Status Change 
+#define HOST_FMOFINT_EN                         (1 << 5)                // Frame Number Overflow
+#define HOST_HERRINT_EN                         (1 << 4)                // Host Error
+#define HOST_RESDETINT_EN                       (1 << 3)                // Resume Detected
+#define HOST_SOFINT_EN                          (1 << 2)                // Start of Frame
+#define HOST_DONEINT_EN                         (1 << 1)                // Done Register
+#define HOST_SORINT_EN                          (1)                     // Schedule Overrun
+
+#define OTG_HOST_XINT_STAT                      (OTG_HOST_BASE+0x18)     //  32bit host X buf int status reg
+#define OTG_HOST_YINT_STAT                      (OTG_HOST_BASE+0x1C)     //  32bit host Y buf int status reg
+
+#define OTG_HOST_XYINT_STEN                     (OTG_HOST_BASE+0x20)     //  32bit host XY buf int enable reg
+#define OTG_HOST_XFILL_STAT                     (OTG_HOST_BASE+0x28)     //  32bit host X filled status reg
+#define OTG_HOST_YFILL_STAT                     (OTG_HOST_BASE+0x2C)     //  32bit host Y filled status reg
+
+#define OTG_HOST_ETD_EN                         (OTG_HOST_BASE+0x40)     //  32bit host ETD enables reg
+#define OTG_HOST_DIR_ROUTE                      (OTG_HOST_BASE+0x48)     //  32bit host direct routing reg
+#define OTG_HOST_IINT                           (OTG_HOST_BASE+0x4C)     //  32bit host immediate interrupt reg
+
+#define OTG_HOST_EP_DSTAT                       (OTG_HOST_BASE+0x50)     //  32bit host endpoints done status
+#define OTG_HOST_ETD_DONE                       (OTG_HOST_BASE+0x54)     //  32bit host ETD done reg
+
+#define OTG_HOST_FRM_NUM                        (OTG_HOST_BASE+0x60)     //  32bit host frame number reg
+#define OTG_HOST_LSP_THRESH                     (OTG_HOST_BASE+0x64)     //  32bit host low speed threshold reg
+
+#define OTG_HOST_ROOTHUB_DESCA                  (OTG_HOST_BASE+0x68)     //  32bit host root hub descriptor A
+#define OTG_HOST_ROOTHUB_DESCB                  (OTG_HOST_BASE+0x6C)     //  32bit host root hub descriptor B
+/* @} */
+
+/*!
+ * @name C.f. 23.11.29
+ */
+/*! @{ */
+#define OTG_HOST_ROOTHUB_STATUS                 (OTG_HOST_BASE+0x70)     //  32bit host root hub status reg
+
+#define ROOTHUB_STATUS_CLRMTWUE                 (1 << 31)               // Clear Remote Wakeup Enable
+#define ROOTHUB_STATUS_OVRCURCHG                (1 << 17)               // Over Current Indicator Change
+#define ROOTHUB_STATUS_LOCPWRSC                 (1 << 16)               // Local Power Status Change
+#define ROOTHUB_STATUS_DEVCONWUE                (1 << 15)               // Device Connect Wakeup Enable
+#define ROOTHUB_STATUS_OVRCURI                  (1 << 1)                // Over Current Indicator
+#define ROOTHUB_STATUS_LOCPWRS                  (1)                     // Local Power Status
+/*! @} */
+
+
+/*!
+ * @name C.f. 23.11.30
+ */
+/*! @{ */
+#define OTG_HOST_PORT_STATUS_1                  (OTG_HOST_BASE+0x74)     //  32bit host port 1 status bits
+#define OTG_HOST_PORT_STATUS_2                  (OTG_HOST_BASE+0x78)     //  32bit host port 2 status bits
+#define OTG_HOST_PORT_STATUS_3                  (OTG_HOST_BASE+0x7c)     //  32bit host port 3 status bits
+
+#define PORT_STATUS_PRTRSTSC                    (1 << 20)               // Port Reset Status Change
+#define PORT_STATUS_OVRCURIC                    (1 << 19)               // Port Over Current Indicator Change
+#define PORT_STATUS_PRTSTATSC                   (1 << 18)               // Port Suspend Status Change
+#define PORT_STATUS_PRTENBLSC                   (1 << 17)               // Port Enable Status Change
+#define PORT_STATUS_CONNECTSC                   (1 << 16)               // Connect Status Change
+
+#define PORT_STATUS_LSDEVCON                    (1 << 9)                // Low Speed Device Attached
+#define PORT_STATUS_PRTPWRST                    (1 << 8)                // Port Power Status
+
+#define PORT_STATUS_PRTRSTST                    (1 << 4)                // Port Reset Status
+#define PORT_STATUS_PRTOVRCURI                  (1 << 3)                // Port Over Current Indicator
+#define PORT_STATUS_PRTSUSPST                   (1 << 2)                // Port Suspend Status
+#define PORT_STATUS_PRTENABST                   (1 << 1)                // Port Enable Status
+#define PORT_STATUS_CURCONST                    (1)                     // Current Connect Status
+
+
+/* flags are the same for the interrupt, control and bulk transfer descriptors */
+// R1: sec 23.11.10.2,3 pg 23-44,47
+#define ETD_GET_COMPCODE(flags)   ((flags >> 12) & 0xf)
+// R1: sec 23.11.10 Table 23-23 pg 23-40
+#define ETD_CC_NOERROR            0x0
+#define ETD_CC_CRCERR             0x1
+#define ETD_CC_BITSTUFFERR        0x2
+#define ETD_CC_DATATOGERR         0x3
+#define ETD_CC_STALL              0x4
+#define ETD_CC_DEVNOTRESPONDING   0x5
+#define ETD_CC_PIDFAILURE         0x6
+// Reserved                       0x7
+#define ETD_CC_DATAOVERRUN        0x8
+#define ETD_CC_DATAUNDERRUN       0x9
+#define ETD_CC_ACK                0xA
+#define ETD_CC_NAK                0xB
+#define ETD_CC_BUFFEROVERRUN      0xC
+#define ETD_CC_BUFFERUNDERRUN     0xD
+#define ETD_CC_SCHEDOVERRUN       0xE
+#define ETD_CC_NOTACCESSED        0xF
+
+// R1: sec 23.11.10.2,3 pg 23-44,47 (contd)
+#define ETD_GET_ERRORCNT(flags)   (((flags) >> 8) & 0xf)
+#define ETD_GET_DATATOGL(flags)   (((flags) >> 6) & 0x3)
+#define ETD_SET_DATATOGL(v)       (((v) & 0x3) << 6)
+#define ETD_GET_DELAYINT(flags)   (((flags) >> 3) & 0x7)
+#define ETD_SET_DELAYINT(v)       (((v) & 0x7) << 3)
+#define ETD_GET_BUFROUND(flags)   (((flags) >> 2) & 0x1)
+#define ETD_SET_BUFROUND(v)       (((v) & 0x1) << 2)
+#define ETD_GET_DIRPID(flags)     ((flags) & 0x3)
+#define ETD_SET_DIRPID(v)         ((v) & 0x3)
+#define ETD_FLIP_DIRPID(flags)    ((flags) ^ 0x3) /* xor with 0x3 flips 2<->1 (IN<->OUT) */
+#define ETD_DIRPID_SETUP          0x0  // OUT
+#define ETD_DIRPID_OUT            0x1
+#define ETD_DIRPID_IN             0x2
+#define ETD_DIRPID_RESERVED       0x3
+
+// For non-isoc td.w3.val -
+#define ETD_GET_BUFSIZE(w3)       (((w3) >> 21) & 0xfff)
+#define ETD_SET_BUFSIZE(v)        (((v) & 0xfff) << 21)
+#define ETD_GET_TOTBYECNT(w3)     ((w3) & 0xfffff)
+#define ETD_SET_TOTBYECNT(v)      ((v) & 0xfffff)
+
+/* isoc flags shares the COMPCODE and DELAYINT bitfields with the other TDs,
+   but the remaining bits are different. */
+#define ETD_GET_AUTOISO(flags)    (((flags) >> 11) & 0x1)
+#define ETD_SET_AUTOISO(v)        (((v) & 0x1) << 11)
+#define ETD_GET_FRAMECNT(flags)   (((flags) >> 8) & 0x1)
+#define ETD_SET_FRAMECNT(v)       (((v) & 0x1) << 8)
+// For isoc td.w3.val -
+/* pkt1 and pkt0 use the COMPCODE bitfield, and add a PKTLEN field. */
+#define ETD_GET_PKTLEN(pkt)       ((pkt) & 0x3ff)
+#define ETD_SET_PKTLEN(v)         ((v) & 0x3ff)
+
+
+// ETD endpoint descriptor (etd->epd) bitfield access
+// R1: sec 23.11.10.1 pg 23-41 (word0)
+#define ETD_GET_SNDNAK(epd)    (((epd) >> 30) & 0x1)
+#define ETD_SET_SNDNAK(v)      (((v) & 0x1) << 30)
+#define ETD_GET_TOGCRY(epd)    (((epd) >> 28) & 0x1)
+#define ETD_SET_TOGCRY(v)      (((v) & 0x1) << 28)
+#define ETD_GET_HALTED(epd)    (((epd) >> 27) & 0x1)
+#define ETD_SET_HALTED(v)      (((v) & 0x1) << 27)
+#define ETD_GET_MAXPKTSIZ(epd) (((epd) >> 16) & 0x3ff)
+#define ETD_SET_MAXPKTSIZ(v)   (((v) & 0x3ff) << 16)
+#define ETD_GET_FORMAT(epd)    (((epd) >> 14) & 0x3)
+#define ETD_SET_FORMAT(v)      (((v) & 0x3) << 14)
+#define ETD_FORMAT_CONTROL     0x0
+#define ETD_FORMAT_ISOC        0x1
+#define ETD_FORMAT_BULK        0x2
+#define ETD_FORMAT_INTERRUPT   0x3
+#define ETD_GET_SPEED(epd)     (((epd) >> 13) & 0x1)
+#define ETD_SET_SPEED(v)       (((v) & 0x1) << 13)
+#define ETD_SPEED_FULL         0x0
+#define ETD_SPEED_LOW          0x1
+#define ETD_GET_DIRECT(epd)    (((epd) >> 11) & 0x3)
+#define ETD_SET_DIRECT(v)      (((v) & 0x3) << 11)
+/* xor with 3 flips 2<->1 */
+#define ETD_FLIP_DIRECT(epd)   ((epd) ^ (0x3 << 11))
+#define ETD_DIRECT_TD00        0x0
+#define ETD_DIRECT_OUT         0x1
+#define ETD_DIRECT_IN          0x2
+#define ETD_DIRECT_TD11        0x3
+#define ETD_GET_ENDPNT(epd)    (((epd) >> 7) & 0xf)
+#define ETD_SET_ENDPNT(v)      (((v) & 0xf) << 7)
+#define ETD_GET_ADDRESS(epd)   ((epd) & 0x7f)
+#define ETD_SET_ADDRESS(v)     ((v) & 0x7f)
+
+// etd_urb_state[] values
+#define ETD_URB_COMPLETED       0
+#define ETD_URB_SETUP_STATUS    1
+#define ETD_URB_SETUP_DATA      2
+#define ETD_URB_SETUP_START     3
+#define ETD_URB_BULK_START      4
+#define ETD_URB_BULKWZLP        5
+#define ETD_URB_BULKWZLP_START  6
+#define ETD_URB_INTERRUPT_START 7
+#define ETD_URB_ISOC_START      8
+/*! @} */
+
+
+/*!
+ * @name C.f. 23.12.8 Function Registers
+ */
+/*! @{ */
+
+#define OTG_FUNC_CMD_STAT                       (OTG_FUNC_BASE+0x00)            //  32bit func command status reg
+
+#define COMMAND_SOFTRESET                       (1 << 7)
+#define COMMAND_BADISOAP                        (1 << 3)
+#define COMMAND_SUPDET                          (1 << 2)
+#define COMMAND_RSMINPROG                       (1 << 1)
+#define COMMAND_RESETDET                        (1)
+
+
+#define OTG_FUNC_DEV_ADDR                       (OTG_FUNC_BASE+0x04)            //  32bit func device address reg
+
+#define OTG_FUNC_SINT_STAT                      (OTG_FUNC_BASE+0x08)            //  32bit func system int status reg
+
+#define SYSTEM_DONEREGINTDS                     (1 << 5)
+#define SYSTEM_SOFDETINT                        (1 << 4)
+#define SYSTEM_DONEREGINT                       (1 << 3)
+#define SYSTEM_SUSPDETINT                       (1 << 2)
+#define SYSTEM_RSMFININT                        (1 << 1)
+#define SYSTEM_RESETINT                         (1)
+
+#define OTG_FUNC_SINT_STEN                      (OTG_FUNC_BASE+0x0C)            //  32bit func system int enable reg
+
+#define SYSTEM_DONEREGINTDS_EN                  (1 << 5)
+#define SYSTEM_SOFDETINT_EN                     (1 << 4)
+#define SYSTEM_DONEREGINT_EN                    (1 << 3)
+#define SYSTEM_SUSPDETINT_EN                    (1 << 2)
+#define SYSTEM_RSMFININT_EN                     (1 << 1)
+#define SYSTEM_RESETINT_EN                      (1)
+
+
+#define OTG_FUNC_XINT_STAT                      (OTG_FUNC_BASE+0x10)            //  32bit func X buf int status reg
+#define OTG_FUNC_YINT_STAT                      (OTG_FUNC_BASE+0x14)            //  32bit func Y buf int status reg
+#define OTG_FUNC_XYINT_STEN                     (OTG_FUNC_BASE+0x18)            //  32bit func XY buf int enable reg
+#define OTG_FUNC_XFILL_STAT                     (OTG_FUNC_BASE+0x1C)            //  32bit func X filled status reg
+
+#define OTG_FUNC_YFILL_STAT                     (OTG_FUNC_BASE+0x20)            //  32bit func Y filled status reg
+#define OTG_FUNC_EP_EN                          (OTG_FUNC_BASE+0x24)            //  32bit func endpoints enable reg
+#define OTG_FUNC_EP_RDY                         (OTG_FUNC_BASE+0x28)            //  32bit func endpoints ready reg
+#define OTG_FUNC_IINT                           (OTG_FUNC_BASE+0x2C)            //  32bit func immediate interrupt reg
+
+#define OTG_FUNC_EP_DSTAT                       (OTG_FUNC_BASE+0x30)            //  32bit func endpoints done status
+#define OTG_FUNC_EP_DEN                         (OTG_FUNC_BASE+0x34)            //  32bit func endpoints done enable
+#define OTG_FUNC_EP_TOGGLE                      (OTG_FUNC_BASE+0x38)            //  32bit func endpoints toggle bits
+#define OTG_FUNC_FRM_NUM                        (OTG_FUNC_BASE+0x3C)            //  32bit func frame number reg
+
+
+#define EP0_STALL                               (1 << 31)
+#define EP0_SETUP                               (1 << 30)
+#define EP0_OVERRUN                             (1 << 29)
+#define EP0_AUTOISO                             (1 << 27)
+
+
+/* generic endpoint register manipulations
+ */
+#define EP_OUT                                  0x1
+#define EP_IN                                   0x2
+#define EP_BOTH                                 0x3
+
+#define ETD_MASK(n)                             (1 << n)
+#define ep_num_both(n)                          (EP_BOTH << n)
+#define ep_num_dir(n, dir)                      ((dir ? EP_IN : EP_OUT) << (n*2))
+#define ep_num_out(n)                           ep_num_dir(n, USB_DIR_OUT)
+#define ep_num_in(n)                            ep_num_dir(n, USB_DIR_IN)
+
+
+/* ep descriptor access
+ */
+static __inline__ u32 etd_word(int n, int word)
+{
+        u32 offset = n * 16;
+        offset += word * 4;
+        return OTG_ETD_BASE + offset;
+}
+
+/* ep descriptor access
+ */
+static __inline__ u32 ep_word(int n, int dir, int word)
+{
+        u32 offset = n * 2;
+        offset += dir ? 1 : 0;
+        offset *= 16;
+        offset += word * 4;
+        return OTG_EP_BASE + offset;
+}
+
+/* endpoint data buffer access
+ * 
+ * This is a simplistic allocator, will do until we want to support ISO or host mode.
+ *
+ * This works because we are assuming a maximum of 16 allocate endpoints, and no
+ * overlapped endpoints (both in and out are allocated).
+ */
+
+static volatile __inline__ u16 data_x_buf(int n, int dir)
+{ 
+        return 0x40 * (n * 4 + 2 * (dir ? 1 : 0));
+}
+static volatile __inline__ u16 data_y_buf(int n, int dir)
+{ 
+        return 0x40 * (n * 4 + 2 * (dir ? 1 : 0) + 1);
+}
+
+static volatile __inline__ u8 * data_x_address(int n, int dir)
+{ 
+        return (volatile u8 *) IO_ADDRESS(OTG_DATA_BASE + data_x_buf(n, dir));
+}
+static volatile __inline__ u8 * data_y_address(int n, int dir)
+{ 
+        return (volatile u8 *) IO_ADDRESS(OTG_DATA_BASE + data_y_buf(n, dir));
+}
+/*! @} */
+
+/*!
+ * @name C.f. 23.13.3 DMA Registers
+ */
+/*! @{ */
+#define OTG_DMA_REV_NUM                         (OTG_DMA_BASE+0x000)     //  32bit dma revision number reg
+#define OTG_DMA_DINT_STAT                       (OTG_DMA_BASE+0x004)     //  32bit dma int status reg
+#define OTG_DMA_DINT_STEN                       (OTG_DMA_BASE+0x008)     //  32bit dma int enable reg
+#define OTG_DMA_ETD_ERR                         (OTG_DMA_BASE+0x00C)     //  32bit dma ETD error status reg
+#define OTG_DMA_EP_ERR                          (OTG_DMA_BASE+0x010)     //  32bit dma EP error status reg
+#define OTG_DMA_ETD_EN                          (OTG_DMA_BASE+0x020)     //  32bit dma ETD DMA enable reg
+#define OTG_DMA_EP_EN                           (OTG_DMA_BASE+0x024)     //  32bit dma EP DMA enable reg
+#define OTG_DMA_ETD_ENXREQ                      (OTG_DMA_BASE+0x028)     //  32bit dma ETD DMA enable Xtrig req
+#define OTG_DMA_EP_ENXREQ                       (OTG_DMA_BASE+0x02C)     //  32bit dma EP DMA enable Ytrig req
+#define OTG_DMA_ETD_ENXYREQ                     (OTG_DMA_BASE+0x030)     //  32bit dma ETD DMA enble XYtrig req
+#define OTG_DMA_EP_ENXYREQ                      (OTG_DMA_BASE+0x034)     //  32bit dma EP DMA enable XYtrig req
+#define OTG_DMA_ETD_BURST4                      (OTG_DMA_BASE+0x038)     //  32bit dma ETD DMA enble burst4 reg
+#define OTG_DMA_EP_BURST4                       (OTG_DMA_BASE+0x03C)     //  32bit dma EP DMA enable burst4 reg
+#define OTG_DMA_MISC_CTRL                       (OTG_DMA_BASE+0x040)     //  32bit dma EP misc control reg
+#define OTG_DMA_ETD_CH_CLR                      (OTG_DMA_BASE+0x044)     //  32bit dma ETD clear channel reg
+#define OTG_DMA_EP_CH_CLR                       (OTG_DMA_BASE+0x048)     //  32bit dma EP clear channel reg
+                        
+
+#define OTG_DMA_ETD_CH_CLR                      (OTG_DMA_BASE+0x044)     //  32bit dma ETD clear channel reg
+#define OTG_DMA_ETD_ERR                         (OTG_DMA_BASE+0x00C)     //  32bit dma ETD error status reg
+#define OTG_DMA_ETD_EN                          (OTG_DMA_BASE+0x020)     //  32bit dma ETD DMA enable reg
+#define OTG_DMA_ETD_ENXREQ                      (OTG_DMA_BASE+0x028)     //  32bit dma ETD DMA enable Xtrig req
+#define OTG_DMA_ETD_ENXYREQ                     (OTG_DMA_BASE+0x030)     //  32bit dma ETD DMA enble XYtrig req
+#define OTG_DMA_ETD_BURST4                      (OTG_DMA_BASE+0x038)     //  32bit dma ETD DMA enble burst4 reg
+
+
+
+#define OTG_DMA_EP_CH_CLR                       (OTG_DMA_BASE+0x048)     //  32bit dma EP clear channel reg
+#define OTG_DMA_EP_ERR                          (OTG_DMA_BASE+0x010)     //  32bit dma EP error status reg
+#define OTG_DMA_EP_EN                           (OTG_DMA_BASE+0x024)     //  32bit dma EP DMA enable reg
+#define OTG_DMA_EP_ENXREQ                       (OTG_DMA_BASE+0x02C)     //  32bit dma EP DMA enable Ytrig req
+#define OTG_DMA_EP_ENXYREQ                      (OTG_DMA_BASE+0x034)     //  32bit dma EP DMA enable XYtrig req
+#define OTG_DMA_EP_BURST4                       (OTG_DMA_BASE+0x03C)     //  32bit dma EP DMA enable burst4 reg
+
+
+#define dma_num_dir(n, dir) (n * 2 + (dir ? 1 : 0))
+#define dma_num_out(n) dma_num_dir(n, USB_DIR_OUT)
+#define dma_num_in(n) dma_num_dir(n, USB_DIR_IN)
+
+#define OTG_DMA_ETD_MSA(x)                      (OTG_DMA_BASE+0x100+x*4)
+#define OTG_DMA_EPN_MSA(x)                      (OTG_DMA_BASE+0x180+x*4)
+#define OTG_DMA_ETDN_BPTR(x)                    (OTG_DMA_BASE+0x280+x*4)
+#define OTG_DMA_EPN_BPTR(x)                     (OTG_DMA_BASE+0x284+x*4)
+
+/*! @} */
+
+/* ********************************************************************************************** */
+/*! 
+ * Warning: the MX21 memory mapped region appears to require 32-bit access only.
+ * As of this writing, only the ETD region has been tested, but it does show the
+ * following behavior:
+ *
+ * Step 1:   *(volatile u32 *)(void*)0xe40243f8  == 00000000
+ * Step 2:   *(volatile u16 *)(void*)0xe40243f8 = 0x0084;
+ * Step 3:   *(volatile u32 *)(void*)0xe40243f8  == 00840084
+ * Step 4:   *(volatile u8 *)(void*)(0xe40243f8+3) = 0x01;
+ * Step 5:   *(volatile u32 *)(void*)0xe40243f8  == 01010101
+ *
+ * Any access to less than 32 bits is replicated as many times as required
+ * to make 32 bits, and the surrounding 32-bit region is changed.
+ *
+ * Just to add insult to injury, gcc 3.2.3 wants to store constants
+ * that are small enough using byte instructions.  E.g.
+ *
+ *      mm->host.System_Interrupt_Enables = (SIEN_PSCIEN | SIEN_FMOFIEN | SIEN_HERRIEN | SIEN_RESDETIEN | 
+ *                                         SIEN_DONEIEN | SIEN_SORIEN);
+ *
+ * becomes:
+ *
+ *      ldrb    r2, [r3, #140]
+ *      mov     r2, #123
+ *      strb    r2, [r3, #140]
+ *      ldrb    r2, [r3, #141]
+ *      strb    r1, [r3, #141]
+ *      ldrb    r2, [r3, #142]
+ *      strb    r1, [r3, #142]
+ *      ldrb    r2, [r3, #143]
+ *      strb    r1, [r3, #143]
+ *
+ * which, given the 32-bit access restrictions above,
+ * is not going to work very well.  Changing the original assignment to:
+ *
+ *      *&(mm->host.System_Interrupt_Enables) = (SIEN_PSCIEN | SIEN_FMOFIEN | SIEN_HERRIEN | SIEN_RESDETIEN | 
+ *                                           SIEN_DONEIEN | SIEN_SORIEN);
+ *
+ * which should be identical, from a language definition point of view,
+ * produces:
+ *
+ *      mov     r2, #123
+ *      str     r2, [r3, #140]
+ *
+ */
+
+
+/*!
+ * @name TransferDescriptors
+ *
+ * Note that the MX21 needs to run in little-endian mode for OTG, but
+ * since the memory can only be accessed in 32-bit quantities, anything
+ * smaller will be built in regular memory, then transfered one word at
+ * a time to the memory-mapped registers, and that word access will flip
+ * the order of bytes.  u16 quantities survive this flipping because they
+ * get "pre-flipped" in regular memory.
+ * 
+ *
+ * An Endpoint Transfer Descriptor (ETD) consists of 4x32bit words.
+ * The first word is the Endpoint Descriptor, the last three are
+ * the transfer descriptor.  Transfer descriptors come in 3 formats,
+ * CONTROL/BULK, INTERRUPT, and ISOCRONOUS.  Becasue of the memory
+ * access restrictions to less than 32 bits described above, only
+ * the words are mapped directly,  The smaller fields are assembled
+ * into 32-bit words before being placed into the live ETD. 
+ *
+ */
+ /*! @{ */
+
+/*!
+ * @struct transfer_descriptor_w1 
+ * All three types of TD have the same format for w1.
+ */
+typedef struct transfer_descriptor_w1 {
+#if defined(LITTLE_ENDIAN)
+        u16 x;
+        u16 y;
+#else
+        u16 y;
+        u16 x;
+#endif
+} __attribute__ ((packed)) volatile transfer_descriptor_w1;
+
+/*!
+ * @struct control_bulk_transfer_descriptor_w2 
+ * There are 3 different formats for w2.
+ * C.f. R1: sec 23.11.10.2 pg 23-43
+ */
+typedef struct control_bulk_transfer_descriptor_w2 {
+#if defined(LITTLE_ENDIAN)
+        u8 rtrydelay;
+        u8 reserved;
+        u16 flags;
+#else
+        u16 flags;
+        u8 reserved;
+        u8 rtrydelay;
+#endif
+} __attribute__ ((packed)) volatile control_bulk_transfer_descriptor_w2;
+
+/*!
+ * @struct interrupt_transfer_descriptor_w2 
+ * C.f. R1: sec 23.11.10.3 pg 23-46
+ */
+typedef struct interrupt_transfer_descriptor_w2 {
+#if defined(LITTLE_ENDIAN)
+        u8 polinterv;
+        u8 relpolpos;
+        u16 flags;
+#else
+        u16 flags;
+        u8 relpolpos;
+        u8 polinterv;
+#endif
+} __attribute__ ((packed)) volatile interrupt_transfer_descriptor_w2;
+
+/*!
+ * @struct isoc_transfer_descriptor_w2 
+ * C.f. R1: sec 23.11.10.4 pg 23-48
+ */
+typedef struct isoc_transfer_descriptor_w2 {
+#if defined(LITTLE_ENDIAN)
+        u16 startfrm;
+        u16 flags;
+#else
+        u16 flags;
+        u16 startfrm;
+#endif
+} __attribute__ ((packed)) volatile isoc_transfer_descriptor_w2;
+
+
+
+/*!
+ * @struct isoc_transfer_descriptor_w3 
+ * There are 2 different formats for w3.
+ * All but isoc use a packet size and 20-bit bytecount, accessed through w3.val.
+ */
+typedef struct isoc_transfer_descriptor_w3 {
+#if defined(LITTLE_ENDIAN)
+        u16 pkt0;
+        u16 pkt1;
+#else
+        u16 pkt1;
+        u16 pkt0;
+#endif
+} __attribute__ ((packed)) volatile isoc_transfer_descriptor_w3;
+
+/*!
+ * @name transfer_descriptor
+ */
+/*! @{ */ 
+typedef struct transfer_descriptor {
+        union {
+                u32 val;
+                transfer_descriptor_w1 bufsrtad;
+        } volatile w1;
+        union {
+                u32 val;
+                control_bulk_transfer_descriptor_w2 cb;
+                interrupt_transfer_descriptor_w2 intr;
+                isoc_transfer_descriptor_w2 isoc;
+        } volatile w2;
+        union {
+                u32 val;
+                isoc_transfer_descriptor_w3 isoc;
+        } volatile w3;
+} __attribute__ ((packed)) volatile transfer_descriptor;
+
+/*! @} */
+
+/*!
+ * @name endpoint_transfer_descriptor 
+ * C.f. R1: sec 23.11.10 pg 23-41
+ */
+ /*! @{ */
+typedef struct endpoint_transfer_descriptor {
+        u32 epd;
+        transfer_descriptor td;
+} __attribute__ ((packed)) volatile endpoint_transfer_descriptor;  // ETD
+
+/*! @} */
+
+
+/*!
+ * @name DataBuffs
+ *
+ * There is no predetermined size for data buffers, so
+ * this structure was chosen as an arbitrary, but generally
+ * adequate size. 
+ */
+/*! @{ */
+
+/*!
+ * @struct fs_data_buff
+ */
+typedef struct fs_data_buff {
+        u32 data[DATA_BUFF_SIZE/sizeof(u32)]; // Cannot safely write less than 32-bit quantities
+} volatile fs_data_buff;
+
+
+/*! 
+ * @struct fs_hcpriv
+ */
+typedef struct fs_hcpriv {
+        struct bus_hcpriv *bus_hcpriv;
+        u32 sof_count;
+        u32 free_etds;
+        endpoint_transfer_descriptor sdp_etd[NUM_ETDS]; // Setup data phase save area
+        void *sdp_data[NUM_ETDS];
+        u8 etd_urb_state[NUM_ETDS];
+        u16 free_buffs;
+        u16 buff_list[NUM_DATA_BUFFS]; 
+} fs_hcpriv;
+
+
+/*!
+ * get_data_buff() - get data buff
+ */
+static __inline__ fs_data_buff *get_data_buff(fs_hcpriv *fs_hcpriv)
+{
+        // Get the next available free data_buff, return NULL if none available.
+        unsigned long flags;
+        fs_data_buff *db;
+        u16 ndx;
+        local_irq_save(flags);
+        if (0xffff != (ndx = fs_hcpriv->free_buffs)) {
+                fs_hcpriv->free_buffs = fs_hcpriv->buff_list[ndx];
+                db = ndx + (fs_data_buff *)OTG_DATA_BASE;
+        }
+        else 
+                db = NULL;
+        local_irq_restore(flags);
+        return(db);
+}
+
+/*!
+ * rel_data_buff() - release data buff
+ */
+static __inline__ fs_data_buff *rel_data_buff(fs_hcpriv *fs_hcpriv, fs_data_buff *db)
+{
+        // Release db to the pool of available data_buffs.
+        unsigned long flags;
+        u16 ndx;
+        if (NULL != db) {
+                local_irq_save(flags);
+                ndx = db - (fs_data_buff *)OTG_DATA_BASE;
+                fs_hcpriv->buff_list[ndx] = fs_hcpriv->free_buffs;
+                fs_hcpriv->free_buffs = ndx;
+#if 0
+                db->next = fs_hcpriv->free_buffs;
+                fs_hcpriv->free_buffs = db;
+#endif
+                local_irq_restore(flags);
+        }
+        return(NULL);
+}
+
+/*!
+ * data_buff_boff() - get data buffer offset given address
+ */
+static __inline__ u16 data_buff_boff(fs_data_buff *db)
+{
+        return((u16)(((void *) db) - ((void *) OTG_DATA_BASE)));
+}
+
+/*!
+ * data_buff_addr() - get data buffer address given offset
+ */
+static __inline__ fs_data_buff *data_buff_addr(u16 boff)
+{
+        // Return the address of the fs_data_buffer that is boff bytes from the start of data buffer memory.
+        return(boff + ((void *) OTG_DATA_BASE));
+}
+/*! @} */
+
+/* ********************************************************************************************** */
+
+/*!
+ * @name FSUSBOTGIO
+ * @brief Freescale USBOTG I/O support.
+ */
+ /*! @{ */
+
+/*!
+ * fs_rb() - read a byte
+ * @param port
+ * @return byte read
+ */ 
+static u8 __inline__ fs_rb(u32 port)
+{
+        return *(volatile u8 *) (IO_ADDRESS(port));
+}
+
+/*! 
+ * fs_rl() - read a long
+ * @param port
+ * @return word read
+ */
+static u32  __inline__ fs_rl(u32 port)
+{       
+        return *(volatile u32 *) (IO_ADDRESS(port));
+}       
+        
+/*! 
+ * fs_wb() - write a byte
+ * @param port
+ * @param val
+ */
+static void __inline__ fs_wb(u32 port, u8 val)
+{               
+        *(volatile u8 *)(IO_ADDRESS(port)) = val;
+}       
+
+/*!
+ * fs_orb() - or a byte
+ * @param port
+ * @param val
+ */     
+static void __inline__ fs_orb(u32 port, u8 val)
+{       
+        u8 set =  fs_rb(port) | val;                                   
+        *(volatile u8 *)(IO_ADDRESS(port)) = set;                   
+}       
+
+/*!
+ * fs_andb() - and a byte
+ * @param port
+ * @param val
+ */     
+static void __inline__ fs_andb(u32 port, u8 val)
+{       
+        u8 set =  fs_rb(port) & val;                                   
+        *(volatile u8 *)(IO_ADDRESS(port)) = set;                   
+}       
+
+/*!
+ * fs_wl() - write a long
+ * @param port
+ * @param val
+ */
+static void __inline__ fs_wl(u32 port, u32 val)
+{
+        u32 set;
+        *(volatile u32 *)(IO_ADDRESS(port)) = val;
+        set = fs_rl(port);
+}       
+
+/*!
+ * fs_orl() - or a long
+ * @param port
+ * @param val
+ */
+static void __inline__ fs_orl(u32 port, u32 val)
+{
+        u32 set = fs_rl(port);
+        *(volatile u32 *)(IO_ADDRESS(port)) = (set | val);
+}
+
+/*! 
+ * fs_andl() - and a long
+ * @param port
+ * @param val
+ */
+static void __inline__ fs_andl(u32 port, u32 val)
+{
+        u32 set = fs_rl(port);
+        *(volatile u32 *)(IO_ADDRESS(port)) = (set & val);
+}
+
+/*!
+ * fs_host_port_stat() - get host start port address for ports 1-3
+ * @param n
+ * @returns port address
+ */
+static u32 inline fs_host_port_stat(int n)
+{
+        switch (n) {
+        default:
+        case 1:
+                return OTG_HOST_PORT_STATUS_1;
+        case 2:
+                return OTG_HOST_PORT_STATUS_2;
+        case 3:
+                return OTG_HOST_PORT_STATUS_3;
+        }
+}
+
+
+/*!
+ * fs_wl_set() - set a word and verify
+ * @param tag
+ * @param port
+ * @param val
+ */
+static void inline fs_wl_set(otg_tag_t tag, u32 port, u32 val)
+{
+        u32 set;
+        *(volatile u32 *)(IO_ADDRESS(port)) = val;
+        set = fs_rl(port);
+        if ((set & val) != val) {
+                TRACE_MSG1(tag, "SET FAILED: %08x", set);
+        }
+}
+
+/*!
+ * fs_wl_clr() - clr a word and verify
+ * @param tag
+ * @param port
+ * @param clr
+ */
+static void inline fs_wl_clr(otg_tag_t tag, u32 port, u32 clr)
+{
+        u32 set;
+        *(volatile u32 *)(IO_ADDRESS(port)) = clr;
+        set = fs_rl(port);
+        if (set & clr) {
+                TRACE_MSG1(tag, "CLEAR FAILED 1: %08x", set);
+                *(volatile u32 *)(IO_ADDRESS(port)) = clr;
+                set = fs_rl(port);
+                if (set & clr)
+                        TRACE_MSG1(tag, "CLEAR FAILED 2: %08x", set);
+        }
+}
+
+
+/*!
+ * fs_memcpy32() - emulage memcpy using long copy
+ * @param dp destination pointer
+ * @param sp source pointer
+ * @param words number of 32bit words to copy
+ *
+ */
+static void inline fs_memcpy32(u32 *dp, u32 *sp, volatile int words)
+{
+        while (words--) *dp++ = *sp++;
+}
+/*!
+ * fs_memcpy() - emulage memcpy using byte copy
+ * @param dp destination pointer
+ * @param sp source pointer
+ * @param bytes number of 8bit bytes to copy
+ */
+static void inline fs_memcpy(u8 *dp, u8 *sp, volatile int bytes)
+{
+        while (bytes--) *dp++ = *sp++;
+}
+/*!
+ * fs_clear_words() - clear memory
+ * @param addr address to clear from
+ * @param words number of 32bit words to clear.
+ */
+static void inline fs_clear_words(volatile u32 *addr, int words)
+{
+        while (words--) *addr++ = 0;
+}
+
+/*! @} */
+
diff -uNr linux/drivers/no-otg/otghw/isp1301-hardware.h linux/drivers/otg/otghw/isp1301-hardware.h
--- linux/drivers/no-otg/otghw/isp1301-hardware.h	1970-01-01 01:00:00.000000000 +0100
+++ linux/drivers/otg/otghw/isp1301-hardware.h	2006-09-01 21:41:35.000000000 +0200
@@ -0,0 +1,218 @@
+/*
+ * otg/otghw/isp1301-hardware.h -- ISP1301 hardware specific defines
+ *
+ *      Copyright (c) 2004 Belcarra
+ *
+ * By: 
+ *      Stuart Lynne <sl@lbelcarra.com>, 
+ *      Tom Rushworth <tbr@belcarra.com>, 
+ *      Bruce Balden <balden@belcarra.com>
+ *
+ */
+/*!
+ * @file otg/otghw/isp1301-hardware.h
+ * @brief Public Structures And Defines For ISP1301 Hardware.
+ *
+ * The Phillips ISP1301 is an OTG Transceiver. There a additional
+ * compatible parts such as the Maxim MAX3301E
+ *
+ * @ingroup ISP1301TCD
+ */
+
+
+/*!
+ * @name ADR/PSW - C.f. 8.9.1
+ *
+ * The i2c address is either 0x2c or 0x2d depending on the level of the
+ * ADR/PSW pin after device reset. Note that this pin can be programmed as
+ * an output via the Mode Control 2 register, bit PSW_OE to enable an
+ * @{
+ */
+
+#define ISP1301_I2C_ADDR_LOW                    0x2c
+#define ISP1301_I2C_ADDR_HIGH                   0x2d
+/*! @} */
+
+/*!
+ * @name ISP1301 Registers C.f. 11.1
+ * @{
+ */
+
+#define ISP1301_VENDOR_ID                       0x00
+#define ISP1301_PRODUCT_ID                      0x02
+#define ISP1301_VERSION_ID                      0x14
+
+#define ISP1301_VENDOR_ID_LOW                   0x00
+#define ISP1301_PRODUCT_ID_LOW                  0x02
+#define ISP1301_VERSION_ID_LOW                  0x14
+#define ISP1301_VENDOR_ID_HIGH                  0x01
+#define ISP1301_PRODUCT_ID_HIGH                 0x03
+#define ISP1301_VERSION_ID_HIGH                 0x15
+
+/* these are all single byte registers
+ */
+#define ISP1301_MODE_CONTROL_1                  0x04
+#define ISP1301_MODE_CONTROL_1_SET              0x04
+#define ISP1301_MODE_CONTROL_1_CLR              0x05
+
+#define ISP1301_MODE_CONTROL_2                  0x12
+#define ISP1301_MODE_CONTROL_2_SET              0x12
+#define ISP1301_MODE_CONTROL_2_CLR              0x13
+
+#define ISP1301_OTG_CONTROL                     0x06
+#define ISP1301_OTG_CONTROL_SET                 0x06
+#define ISP1301_OTG_CONTROL_CLR                 0x07
+
+#define ISP1301_OTG_STATUS                      0x10
+
+#define ISP1301_INTERRUPT_SOURCE                0x08
+
+#define ISP1301_INTERRUPT_LATCH                 0x0a
+#define ISP1301_INTERRUPT_LATCH_SET             0x0a
+#define ISP1301_INTERRUPT_LATCH_CLR             0x0b
+
+#define ISP1301_INTERRUPT_ENABLE_LOW            0x0c
+#define ISP1301_INTERRUPT_ENABLE_LOW_SET        0x0c
+#define ISP1301_INTERRUPT_ENABLE_LOW_CLR        0x0d
+
+#define ISP1301_INTERRUPT_ENABLE_HIGH           0x0e
+#define ISP1301_INTERRUPT_ENABLE_HIGH_SET       0x0e
+#define ISP1301_INTERRUPT_ENABLE_HIGH_CLR       0x0f
+/*! @} */
+
+
+/*!
+ * @name Mode Control 1 register - C.f. Table 17 and Table 18
+ * @{
+ */
+#define ISP1301_UART_EN                         (1 << 6)
+#define ISP1301_OE_INT_EN                       (1 << 5)
+#define ISP1301_BDIS_ACON_EN                    (1 << 4)
+#define ISP1301_TRANSP_EN                       (1 << 3)
+#define ISP1301_DAT_SE0                         (1 << 2)
+#define ISP1301_SUSPEND_REG                     (1 << 1)
+#define ISP1301_SPEED_REG                       (1 << 0)
+/*! @} */
+
+
+/*!
+ * @name Mode Control 2 register - C.f. Table 19 and Table 20
+ * @{
+ */
+#define ISP1301_EN2V7                           (1 << 7)
+#define ISP1301_PSW_OE                          (1 << 6)
+#define ISP1301_AUDIO_EN                        (1 << 5)
+#define ISP1301_TRANSP_BDIR                     (3 << 3)
+#define ISP1301_BI_DI                           (1 << 2)
+#define ISP1301_SPD_SUSP_CTRL                   (1 << 1)
+#define ISP1301_GLOBAL_PWR_ON                   (1 << 0)
+/*! @} */
+
+
+/*!
+ * @name OTG Control register - C.f. Table 21 and Table 22
+ * @{
+ */
+#define ISP1301_VBUS_CHRG                       (1 << 7)
+#define ISP1301_VBUS_DISCHRG                    (1 << 6)
+#define ISP1301_VBUS_DRV                        (1 << 5)
+#define ISP1301_ID_PULLDOWN                     (1 << 4)
+#define ISP1301_DM_PULLDOWN                     (1 << 3)
+#define ISP1301_DP_PULLDOWN                     (1 << 2)
+#define ISP1301_DM_PULLUP                       (1 << 1)
+#define ISP1301_DP_PULLUP                       (1 << 0)
+
+#define ISP1301_VBUS_RESET (ISP1301_VBUS_CHRG | ISP1301_VBUS_DISCHRG | ISP1301_VBUS_DRV)
+
+/*! @} */
+
+
+/*!
+ * @name OTG Status register - C.f. Table 23 and Table 24
+ * @{
+ */
+#define ISP1301_B_SESS_VLD                      (1 << 7)
+#define ISP1301_B_SESS_END                      (1 << 6)
+/*! @} */
+
+
+/*!
+ * @name Interrupt Source Register
+ * Interrupt Source register - C.f. Table 25 and Table 26
+ * Interrupt Latch register - C.f. Table 27 and Table 28
+ * Interrupt Enable Low register - C.f. Table 29 and Table 30
+ * Interrupt Enable High register - C.f. Table 31 and Table 32
+ * @{
+ */
+#define ISP1301_CR_INT                          (1 << 7)
+#define ISP1301_BDIS_ACON                       (1 << 6)
+#define ISP1301_ID_FLOAT                        (1 << 5)
+#define ISP1301_DM_HI                           (1 << 4)
+#define ISP1301_ID_GND                          (1 << 3)
+#define ISP1301_DP_HI                           (1 << 2)
+#define ISP1301_SESS_VLD                        (1 << 1)
+#define ISP1301_VBUS_VLD                        (1 << 0)
+
+//#define ISP1301_SE1                             (ISP1301_DM_HI | ISP1301_DP_HI)
+
+/*! @} */
+
+/*!
+ * @name Maxim MAX3301E
+ *
+ * This part is compatible with the ISP1301 with minor differences:
+ *
+ * Mode Control 1 register is called Control Register 1. It is almost
+ * identical except for:
+ *
+ *      bit     isp1301         max3301e
+ *      3       transp_en       not used
+ * @{
+ */
+#define MAX3301E_CONTROL_REGISTER_1             0x04
+#define MAX3301E_CONTROL_REGISTER_1_SET         0x04
+#define MAX3301E_CONTROL_REGISTER_1_CLR         0x05
+/*! @} */
+
+/*!
+ * @name Mode Control 2
+ * Mode Control 2 is called Special Function Register 1. Mostly the same
+ * except for:
+ *
+ *      bit     isp1301         max3301e
+ *      7       en2v7           gp_en
+ *      6       psw_oe          sess_end
+ *      5       audio_en        int_source
+ * @{
+ */
+#define MAX3301E_SPECIAL_FUNCTION_1             0x12
+#define MAX3301E_SPECIAL_FUNCTION_1_SET         0x12
+#define MAX3301E_SPECIAL_FUNCTION_1_CLR         0x13
+#define MAX3301E_GP_EN                          (1 << 7)
+#define MAX3301E_SESS_END                       (1 << 6)
+#define MAX3301E_INT_SOURCE                     (1 << 5)
+/*! @} */
+
+/*!
+ * @name OTG Status
+ * The OTG Status is not present, but B_SESS_END seems to be available
+ * in the Mode Control 2 register bit 6.
+ *
+ * An additional register is available for MAX3301E specific features.
+ *
+ * Special Function Register 2 
+ * @{
+ */
+
+#define MAX3301E_SPECIAL_FUNCTION_2             0x16
+#define MAX3301E_SPECIAL_FUNCTION_2_SET         0x16
+#define MAX3301E_SPECIAL_FUNCTION_2_CLR         0x17
+#define MAX3301E_SDWN                           (1 << 0)
+#define MAX3301E_IRQ_MODE                       (1 << 1)
+#define MAX3301E_XCVR_INPUT_DISC                (1 << 2)
+#define MAX3301E_REQ_SET                        (1 << 3)
+/*! @} */
+
+
+
+
diff -uNr linux/drivers/no-otg/otghw/isp1301.h linux/drivers/otg/otghw/isp1301.h
--- linux/drivers/no-otg/otghw/isp1301.h	1970-01-01 01:00:00.000000000 +0100
+++ linux/drivers/otg/otghw/isp1301.h	2006-09-01 21:41:35.000000000 +0200
@@ -0,0 +1,158 @@
+/*
+ * otg/otghw/isp1301/isp1301.h -- USB Transceiver Controller driver 
+ *
+ *      Copyright (c) 2004 Belcarra
+ *
+ * By: 
+ *      Stuart Lynne <sl@lbelcarra.com>, 
+ *      Tom Rushworth <tbr@belcarra.com>, 
+ *      Bruce Balden <balden@belcarra.com>
+ *
+ */
+/*!
+ * @defgroup ISP1301TCD Phillips ISP1301 
+ * @ingroup tcdgroup
+ */
+/*!
+ * @file otg/otghw/isp1301.h
+ * @brief Private structures and defines for ISP1301 Transciever Controller Driver.
+ *
+ * This file contains the private defines and structures for the ISP1301 Transceiver 
+ * Driver. 
+ *
+ * @ingroup ISP1301TCD
+ */
+
+/*!
+ * @name ISP1301 ISP1301 configuration
+ * @{
+ */
+
+#ifdef CONFIG_OMAP_H2
+#define ISP1301_GPIO    	(2)
+#define	TCD_ISP1301_I2C_ADDR 	ISP1301_I2C_ADDR_HIGH;
+#endif
+
+#define ISP1301_NAME    "isp1301"
+
+#define ISP1301_LOC_CONN 0x01
+
+#define ISP1301_INT_SRC         0xeb
+
+/*!
+ * @enum otg_transceiver 
+ * Define ISP1301 compatible transceivers
+ */
+typedef enum otg_transceiver {
+        unknown_transceiver,    /*!< unknown */
+        isp1301,                /*!< Phillips ISP1301 */
+        max3301e                /*!< Maxim MAX3301e */
+} otg_transceiver_t;
+
+/*!
+ * @struct otg_transceiver_map 
+ * How to determine transceiver model and manufacturer
+ */
+struct otg_transceiver_map {
+        otg_transceiver_t transceiver_type;     /*!< transceiver type */
+        u16 vendor;                             /*!< vendor number */
+        u16 product;                            /*!< product number */
+        u16 revision;                           /*!< revision number */
+        char *name;                             /*!< name */
+};
+
+/*! 
+ * @struct isp1301_private
+ * Information required for operation
+ */
+struct isp1301_private {
+        WORK_STRUCT bh;                         /*!< work structure for bottom half handler */
+        u16 vendor;                             /*!< vendor number found */
+        u16 product;                            /*!< product number found */
+        u16 revision;                           /*!< revision number found */
+        u32 flags;                              /*!< flags */
+        u8  int_src;                            /*!< current interrupt source */
+        struct otg_transceiver_map *transceiver_map; /*! pointer to transceiver map for this transceiver */
+};
+
+/*! 
+ * @enum tx_mode
+ * This defines the various ways that the ISP1301 can be
+ * wired into the host USB. 
+ */
+typedef enum tx_mode {
+        dat_se0_bidirectional,          /*!< 3-Wire */
+        vp_vm_bidirectional,            /*!< 4-Wire */
+        dat_se0_unidirectional          /*!< 6-Wire */
+} tx_mode_t;
+
+/*! 
+ * @enum spd_ctrl
+ * This defines how the speed and suspend pins are controlled
+ * for this host.
+ */
+typedef enum spd_ctrl {
+        spd_susp_pins,                  /*!< controlled by SPEED and SUSPEND pins */
+        spd_susp_reg,                   /*!< controled by SPEED_REG and SUSPEND_REG in Mode Control 1 register */
+} spd_ctrl_t;
+
+
+extern struct tcd_ops tcd_ops;
+extern struct isp1301_private isp1301_private;
+extern struct tcd_instance *tcd_instance;
+
+/* isp1301.c */
+extern int isp1301_mod_init(WORK_PROC );
+extern int isp1301_mod_init__(void );
+extern void isp1301_mod_exit(void);
+extern void isp1301_tcd_en(struct otg_instance *, u8 );
+extern void isp1301_chrg_vbus(struct otg_instance *, u8 );
+extern void isp1301_drv_vbus(struct otg_instance *, u8 );
+extern void isp1301_dischrg_vbus(struct otg_instance *, u8 );
+extern void isp1301_dp_pullup_func(struct otg_instance *, u8 );
+extern void isp1301_dm_pullup_func(struct otg_instance *, u8 );
+extern void isp1301_dp_pulldown_func(struct otg_instance *, u8 );
+extern void isp1301_dm_pulldown_func(struct otg_instance *, u8 );
+extern void isp1301_peripheral_host_func(struct otg_instance *, u8 );
+extern void isp1301_dm_det_func(struct otg_instance *, u8 );
+extern void isp1301_dp_det_func(struct otg_instance *, u8 );
+extern void isp1301_cr_det_func(struct otg_instance *, u8 );
+extern void isp1301_bdis_acon_func(struct otg_instance *, u8 );
+extern void isp1301_mx21_vbus_drain_func(struct otg_instance *, u8 );
+extern void isp1301_id_pulldown_func(struct otg_instance *, u8 );
+extern void isp1301_audio_func(struct otg_instance *, u8 );
+extern void isp1301_uart_func(struct otg_instance *, u8 );
+extern void isp1301_mono_func(struct otg_instance *, u8 );
+
+extern void isp1301_otg_timer(struct otg_instance *, u8 );
+extern void isp1301_bh(void *);
+extern int isp1301_id (struct otg_instance *);
+extern int isp1301_vbus (struct otg_instance *);
+extern void  isp1301_configure(tx_mode_t, spd_ctrl_t );
+
+extern void isp1301_bh_wakeup(int);
+
+/* thread */
+extern int isp1301_thread_init (void (bh_proc)(void *));
+extern void isp1301_thread_exit (wait_queue_head_t *);
+extern void isp1301_thread_wakeup(int enabled, int first);
+
+
+
+/* i2c-xxx.c */
+int  i2c_configure(char *name, int addr);
+extern void  i2c_close(void);
+extern u8 i2c_readb(u8 );
+extern u16 i2c_readw(u8 );
+extern u32 i2c_readl(u8 );
+extern void i2c_writeb(u8 , u8 );
+
+
+/* ********************************************************************************************* */
+#define TRACE_I2CB(t,r) TRACE_MSG3(t, "%-40s[%02x] %02x", #r, r, i2c_readb(r))
+#define TRACE_I2CW(t,r) TRACE_MSG3(t, "%-40s[%02x] %04x", #r, r, i2c_readw(r))
+#define TRACE_GPIO(t,b,r) TRACE_MSG2(t, "%-40s %04x", #r, readw(b + r))
+#define TRACE_REGL(t,r) TRACE_MSG2(t, "%-40s %08x", #r, readl(r))
+
+/* @} */
+
diff -uNr linux/drivers/no-otg/otghw/max3353e-hardware.h linux/drivers/otg/otghw/max3353e-hardware.h
--- linux/drivers/no-otg/otghw/max3353e-hardware.h	1970-01-01 01:00:00.000000000 +0100
+++ linux/drivers/otg/otghw/max3353e-hardware.h	2006-09-01 21:41:35.000000000 +0200
@@ -0,0 +1,141 @@
+/*
+ * otg/otg/max3353e-hardware.h -- MAX3353E hardware specific defines
+ *
+ *      Copyright (c) 2004 Belcarra
+ *
+ * By: 
+ *      Stuart Lynne <sl@lbelcarra.com>, 
+ *      Tom Rushworth <tbr@belcarra.com>, 
+ *      Bruce Balden <balden@belcarra.com>
+ *
+ */
+/*!
+ * @file otg/otg/max3353e-hardware.h
+ * @brief Public structures and defines for MAX3353E Hardware.
+ *
+ * The Maxim MAX3353E is an OTG Charge Pump with controllable
+ * pullup resistors.
+ *
+ * @ingroup MAX3353ETCD
+ */
+
+
+/*!
+ * @name ADR/PSW - C.f. Slave Address
+ *
+ * The high/low address is selected with the ADD pin.
+ *
+ * @{
+ */
+
+#define MAX3353E_I2C_ADDR_LOW                    0x16
+#define MAX3353E_I2C_ADDR_HIGH                   0x2C
+
+/*! @} */
+
+
+
+/*!
+ * @name MAX3353E Registers C.f. Table 2 - Register Address Map
+ * @{
+ */
+
+#define MAX3353E_MANUFACTURER_REGISTER_0        0x00
+#define MAX3353E_MANUFACTURER_REGISTER_1        0x01
+#define MAX3353E_MANUFACTURER_REGISTER_2        0x02
+#define MAX3353E_MANUFACTURER_REGISTER_3        0x03
+
+#define MAX3353E_PRODUCT_ID_REGISTER_0          0x04
+#define MAX3353E_PRODUCT_ID_REGISTER_1          0x05
+#define MAX3353E_PRODUCT_ID_REGISTER_2          0x06
+#define MAX3353E_PRODUCT_ID_REGISTER_3          0x07
+
+#define MAX3353E_CONTROL_REGISTER_1             0x10
+#define MAX3353E_CONTROL_REGISTER_2             0x11
+
+#define MAX3353E_STATUS_REGISTER                0x13
+
+#define MAX3353E_INTERRUPT_MASK                 0x14
+#define MAX3353E_INTERRUPT_EDGE                 0x15
+#define MAX3353E_INTERRUPT_LATCH                0x16
+
+/*! @} */
+
+
+/*!
+ * @name Control Register 1
+ * @{
+ */
+#define MAX3353E_DM_PULLDOWN                    (1 << 7)
+#define MAX3353E_DP_PULLDOWN                    (1 << 6)
+#define MAX3353E_DM_PULLUP                      (1 << 5)
+#define MAX3353E_DP_PULLUP                      (1 << 4)
+
+#define MAX3353E_BDISC_ACONN                    (1 << 2)
+#define MAX3353E_IRQ_MODE                       (1 << 1)
+
+/*! @} */
+
+/*!
+ * @name Control Register 2
+ * @{
+ */
+#define MAX3353E_VBUS_DISCHG                    (1 << 4)
+#define MAX3353E_VBUS_DRV                       (1 << 3)
+#define MAX3353E_VBUS_CHG2                      (1 << 2)
+#define MAX3353E_VBUS_CHG1                      (1 << 1)
+#define MAX3353E_DSWIN                          (1 << 0)
+
+/*! @} */
+
+/*!
+ * @name Status Register
+ * @{
+ */
+#define MAX3353E_B_HNP                          (1 << 6)
+#define MAX3353E_A_HNP                          (1 << 5)
+#define MAX3353E_ID_FLOAT                       (1 << 4)
+#define MAX3353E_ID_GND                         (1 << 3)
+#define MAX3353E_SESSION_END                    (1 << 2)
+#define MAX3353E_SESSION_VALID_EN               (1 << 1)
+#define MAX3353E_VBUS_VALID                     (1 << 0)
+
+/*! @} */
+
+/*!
+ * @name Interrupt Mask
+ * @{
+ */
+#define MAX3353E_A_HNP_EN                       (1 << 5)
+#define MAX3353E_ID_FLOAT_EN                    (1 << 4)
+#define MAX3353E_ID_GND_EN                      (1 << 3)
+#define MAX3353E_SESSION_END_EN                 (1 << 2)
+#define MAX3353E_SESSION_VALID_EN               (1 << 1)
+#define MAX3353E_VBUS_VALID_EN                  (1 << 0)
+
+/*! @} */
+
+/*!
+ * @name Interrupt Edge
+ * @{
+ */
+#define MAX3353E_SESSION_VALID_ED               (1 << 1)
+#define MAX3353E_VBUS_VALID_ED                  (1 << 0)
+
+/*! @} */
+
+/*!
+ * @name Interrupt Latch
+ * @{
+ */
+#define MAX3353E_A_HNP_RQ                       (1 << 7)
+#define MAX3353E_ID_FLOAT_RQ                    (1 << 6)
+#define MAX3353E_ID_GND_RQ                      (1 << 5)
+#define MAX3353E_SESSION_END_RQ                 (1 << 4)
+#define MAX3353E_SESSION_VALID_RN               (1 << 3)
+#define MAX3353E_VBUS_VALID_RN                  (1 << 2)
+#define MAX3353E_SESSION_VALID_RP               (1 << 1)
+#define MAX3353E_VBUS_VALID_RP                  (1 << 0)
+
+/*! @} */
+
diff -uNr linux/drivers/no-otg/otghw/mx2ads-hardware.h linux/drivers/otg/otghw/mx2ads-hardware.h
--- linux/drivers/no-otg/otghw/mx2ads-hardware.h	1970-01-01 01:00:00.000000000 +0100
+++ linux/drivers/otg/otghw/mx2ads-hardware.h	2006-09-01 21:41:35.000000000 +0200
@@ -0,0 +1,111 @@
+/*
+ * otghw/mx2ads-hardware.h
+ *
+ * Copyright (c) 2004 Belcarra
+ *
+ * By: 
+ *      Stuart Lynne <sl@belcarra.com>, 
+ *      Tom Rushworth <tbr@belcarra.com>, 
+ *      Bruce Balden <balden@belcarra.com>
+ *
+ */
+/*! 
+ * @file otghw/mx2ads-hardware.h
+ * @brief MX2ADS Hardware constants
+ *
+ * @ingroup MX2ADS
+ */
+
+
+/*!
+ * @name MX2ETDS
+ * R1 = i.MX21 Application Processor Reference Manual Rev 0.6 2003/11/01 TO 1.0
+ *      (aka "IMX21RM_TO1.pdf")
+ * R2 = "USBOTG_L3_Specification_v.C1.0.pdf"
+ * @{
+ */
+
+#define NUM_ETDS                32
+#define DATA_BUFF_SIZE          64
+#define DATA_BUFFER_TOTAL       4096
+#define NUM_DATA_BUFFS          (4096/DATA_BUFF_SIZE)
+//#define LITTLE_ENDIAN           1
+
+/* @} */
+
+
+/*!
+ * @name MX2Interrupts
+ * @{
+ */
+#define OTG_USBWKUP                 53
+#define OTG_USBDMA                  54
+#define OTG_USBHOST                 55
+#define OTG_USBFUNC                 56
+#define OTG_USBHNP                  57
+#define OTG_USBCTRL                 58
+/* @} */
+
+/*!
+ * @name AHBMemoryMap
+ * C.F. Table 23-5 AHB Memory Map
+ * @{
+ */
+#ifndef OTG_BASE_ADDR
+#define OTG_BASE_ADDR                           0x10024000      
+
+#define OTG_CORE_BASE                           (OTG_BASE_ADDR+0x000)   //  base location for core
+#define OTG_FUNC_BASE                           (OTG_BASE_ADDR+0x040)   //  base location for function
+#define OTG_HOST_BASE                           (OTG_BASE_ADDR+0x080)   //  base location for host
+#define OTG_I2C_BASE                            (OTG_BASE_ADDR+0x100)   //  base location for I2C
+
+#define OTG_ETD_BASE                            (OTG_BASE_ADDR+0x200)   //  base location for etd memory
+#define OTG_EP_BASE                             (OTG_BASE_ADDR+0x400)   //  base location for ep memory
+#define OTG_SYS_BASE                            (OTG_BASE_ADDR+0x600)   //  base location for system
+
+#define OTG_DMA_BASE                            (OTG_BASE_ADDR+0x800)   //  base location for dma
+
+#define OTG_DATA_BASE                           (OTG_BASE_ADDR+0x1000)  //  base location for data memory
+
+#define OTG_SYS_CTRL                            (OTG_SYS_BASE+0x000)    //  base location for system
+#endif
+/* @} */
+
+/*!
+ * @name I2CTransceiver
+ * C.f. 23.14.10 I2C OTG Transceiver Controller Registers
+ * These are the I2C controller and access registers.
+ *
+ * N.B. I2C_ERROR is not documented.
+ * @{
+ */
+
+
+#define I2C_BUSY                                (1 << 7)
+#define I2C_ERROR                               (1 << 2)
+#define I2C_HWSMODE                             (1 << 1)
+#define I2C_I2COE                               (1 << 0)
+
+#define I2C_SCLK_TO_SCL_DIVISION                (OTG_I2C_BASE+0x1E)
+
+#define I2C_INTERRUPT_AND_CONTROL               (OTG_I2C_BASE+0x1F)
+
+#define I2C_STATUS_MASK                         (0x7)
+
+#define I2C_NOACK_EN                            (1 << 6)
+#define I2C_RWREADY_EN                          (1 << 5)
+#define I2C_OTGXCVRINT_EN                       (1 << 4)
+
+#define I2C_NOACK                               (1 << 2)
+#define I2C_RWREADY                             (1 << 1)
+#define I2C_OTGXCVRINT                          (1 << 0)
+
+#define MX2_OTG_XCVR_DEVAD                      OTG_I2C_BASE+0x18
+#define MX2_SEQ_OP_REG                          OTG_I2C_BASE+0x19
+#define MX2_SEQ_RD_STARTAD                      OTG_I2C_BASE+0x1a
+#define MX2_I2C_OP_CTRL_REG                     OTG_I2C_BASE+0x1b
+#define MX2_SCLK_TO_SCL_HPER                    OTG_I2C_BASE+0x1e
+#define MX2_I2C_INTERRUPT_AND_CTRL              OTG_I2C_BASE+0x1f
+
+/* @} */
+
diff -uNr linux/drivers/no-otg/otghw/mx2ads.h linux/drivers/otg/otghw/mx2ads.h
--- linux/drivers/no-otg/otghw/mx2ads.h	1970-01-01 01:00:00.000000000 +0100
+++ linux/drivers/otg/otghw/mx2ads.h	2006-09-01 21:41:35.000000000 +0200
@@ -0,0 +1,46 @@
+/*
+ * otghw/mx2ads.h
+ *
+ *      Copyright (c) 2004 Belcarra
+ *
+ * By: 
+ *      Stuart Lynne <sl@belcarra.com>, 
+ *      Tom Rushworth <tbr@belcarra.com>, 
+ *      Bruce Balden <balden@belcarra.com>
+ */
+ 
+/*!
+ * @defgroup MX2ADS Freescale MX2ADS
+ * @ingroup platformgroup 
+ */
+/*! 
+ * @file otghw/mx2ads.h
+ * @brief MX2ADS Platform constants
+ *
+ * Notes
+ *
+ * 1. Interrupts - we seem to get all interrupts on the ctrl interrupt.
+ *
+ * 2. Clearing - it appears that clearing the status bits sometimes fails, This
+ * has been observed in SINT_STAT and XFILL_STAT.
+ *
+ * 3. Double buffer - without DMA it does not appear to be possible to use
+ * both X and Y buffers for OUT transfers because we can not apparantely guarantee
+ * the order that the X and Y buffers will be used. If we get an interrupt with
+ * both X and Y active we cannot determine which order to use them. As long as
+ * we get each interrupt fast enough this is not a problem as we can then just
+ * use whichever is active.
+ *
+ * 4. Remote Wakeup - cannot set RSMINPROG bit
+ *
+ * @ingroup MX2ADS
+ */
+
+
+#define UDC_NAME                "MX21"
+#define UDC_MAX_ENDPOINTS       32
+#define EP0_PACKETSIZE          64
+
+
+extern otg_tag_t MX2;
+
diff -uNr linux/drivers/no-otg/otghw/td243-hardware.h linux/drivers/otg/otghw/td243-hardware.h
--- linux/drivers/no-otg/otghw/td243-hardware.h	1970-01-01 01:00:00.000000000 +0100
+++ linux/drivers/otg/otghw/td243-hardware.h	2006-09-01 21:41:35.000000000 +0200
@@ -0,0 +1,709 @@
+/*
+ * otg/pcd/mx2/td243-hardware.h
+ *
+ * Copyright (c) 2004 Belcarra
+ *
+ * By: 
+ *      Stuart Lynne <sl@belcarra.com>, 
+ *      Tom Rushworth <tbr@belcarra.com>, 
+ *      Bruce Balden <balden@belcarra.com>
+ *
+ */
+/*!
+ * @defgroup TD243 Transdimension USBOTG 
+ * @ingroup libgroup
+ */
+/*!
+ * @file /otg/otghw/td243-hardware.h
+ * @brief Hardware defines for Transimension USBOTG Hardware
+ *
+ * @ingroup TD243
+ */
+
+
+/*
+ * C.f. 23.8 USB Control Register
+ */
+
+#define SYS_CTRL_I2C_WU_INT_STAT                (1 << 27)
+#define SYS_CTRL_OTG_WU_INT_STAT                (1 << 26)
+#define SYS_CTRL_HOST_WU_INT_STAT               (1 << 25)
+#define SYS_CTRL_FNT_WU_INT_STAT                (1 << 24)
+
+#define SYS_CTRL_I2C_WU_INT_EN                  (1 << 19)
+#define SYS_CTRL_OTG_WU_INT_EN                  (1 << 18)
+#define SYS_CTRL_HOST_WU_INT_EN                 (1 << 17)
+#define SYS_CTRL_FNT_WU_INT_EN                  (1 << 16)
+
+#define SYS_CTRL_OTG_BYP_VAL                    (0x3 << 11)
+#define SYS_CTRL_HOST1_BYP_VAL                  (0x3 << 9)
+
+        
+
+#define SYS_CTRL_OTG_PWR_MASK                   (1 << 6)
+#define SYS_CTRL_HOST1_PWR_MASK                 (1 << 6)
+#define SYS_CTRL_HOST2_PWR_MASK                 (1 << 4)
+#define SYS_CTRL_USB_BYP                        (1 << 2)
+#define SYS_CTRL_HOST1_TXEN_OE                  (1 << 1)
+
+
+/*
+ * C.f. 23.9 USBOTG Module Registers
+ */
+
+#define OTG_CORE_HWMODE                         (OTG_CORE_BASE+0x00)            //  32bit core hardware mode reg
+
+#define XCVR_D_D                                0x00
+#define XCVR_SE_D                               0x01
+#define XCVR_D_SE                               0x02
+#define XCVR_SE_SE                              0x03
+
+#define MODULE_ANASDBEN                         (1 << 14)
+#define MODULE_OTGXCVR                          (0x3 << 6)
+#define MODULE_HOSTXCVR                         (0x3 << 4)
+#define MODULE_CRECFG                           (0x3)
+#define MODULE_CRECFG_HHNP                      (0x0)
+#define MODULE_CRECFG_HOST                      (0x1)
+#define MODULE_CRECFG_FUNC                      (0x2)
+#define MODULE_CRECFG_SHNP                      (0x3)
+
+#define OTG_CORE_CINT_STAT                      (OTG_CORE_BASE+0x04)            //  32bit core int status reg
+
+#define MODULE_FCINTDSPEN                       (1 << 6)
+
+#define MODULE_ASHNPINT                         (1 << 5)
+#define MODULE_ASFCINT                          (1 << 4)
+#define MODULE_ASHCINT                          (1 << 3)
+#define MODULE_HNPINT                           (1 << 2)
+#define MODULE_FCINT                            (1 << 1)
+#define MODULE_HCINT                            (1)
+
+#define OTG_CORE_CINT_STEN                      (OTG_CORE_BASE+0x08)            //  32bit core int enable reg
+
+#define MODULE_ASHNPINT_EN                      (1 << 5)
+#define MODULE_ASFCINT_EN                       (1 << 4)
+#define MODULE_ASHCINT_EN                       (1 << 3)
+#define MODULE_HNPINT_EN                        (1 << 2)
+#define MODULE_FCINT_EN                         (1 << 1)
+#define MODULE_HCINT_EN                         (1)
+
+#define OTG_CORE_CLK_CTRL                       (OTG_CORE_BASE+0x0C)            //  32bit core clock control reg
+
+#define MODULE_FUNC_CLK                         (1 << 2)
+#define MODULE_HOST_CLK                         (1 << 1)
+#define MODULE_MAIN_CLK                         (1)
+
+#define OTG_CORE_RST_CTRL                       (OTG_CORE_BASE+0x10)            // 32bit core reset control reg
+
+#define MODULE_RSTI2C                           (1 << 15)
+#define MODULE_RSTCTRL                          (1 << 5)
+#define MODULE_RSTFC                            (1 << 4)
+#define MODULE_RSTFSIE                          (1 << 3)
+#define MODULE_RSTRH                            (1 << 2)
+#define MODULE_RSTHSIE                          (1 << 1)
+#define MODULE_RSTHC                            (1)
+
+#define OTG_CORE_FRM_INTVL                      (OTG_CORE_BASE+0x14)            //  32bit core frame interval reg
+
+#define MODULE_RESET_FRAME                      (1 << 15)
+
+#define OTG_CORE_FRM_REMAIN                     (OTG_CORE_BASE+0x18)            //  32bit core frame remaining reg
+
+#define OTG_CORE_HNP_CSTAT                      (OTG_CORE_BASE+0x1C)            //  32bit core HNP current state reg
+
+#define MODULE_HNPDAT                           (1 << 30)
+#define MODULE_VBUSBSE                          (1 << 29)
+#define MODULE_VBUSABSV                         (1 << 28)
+#define MODULE_VBUSGTAVV                        (1 << 27)
+
+#define MODULE_ARMTHNPE                         (1 << 25)
+#define MODULE_BHNPEN                           (1 << 24)
+
+#define MODULE_SLAVE                            (1 << 22)
+#define MODULE_MASTER                           (1 << 21)
+#define MODULE_BGEN                             (1 << 20)
+#define MODULE_CMPEN                            (1 << 19)
+#define MODULE_ISBDEV                           (1 << 18)
+#define MODULE_ISADEV                           (1 << 17)
+
+#define MODULE_SWVBUSPUL                        (1 << 15)
+
+#define MODULE_SWAUTORST                        (1 << 12)
+#define MODULE_SWPUDP                           (1 << 11)
+
+#define MODULE_SWPDDM                           (1 << 9)
+#define MODULE_HNPSTATE                         (0x1f << 8)
+#define MODULE_CLRERROR                         (1 << 3)
+#define MODULE_ADROPBUS                         (1 << 2)
+#define MODULE_ABBUSREQ                         (1 << 1)
+
+#define HNP_A_IDLE                              (0x10 << 8)
+#define HNP_A_MASTER                            (0x11 << 8)
+#define HNP_A_SLAVE                             (0x12 << 8)
+#define HNP_A_WAIT_VPULSE                       (0x13 << 8)
+
+#define HNP_A_WAIT_DPULSE                       (0x14 << 8)
+#define HNP_A_WAIT_CONN_A                       (0x15 << 8)
+#define HNP_A_WAIT_CONN_B                       (0x16 << 8)
+#define HNP_A_WAIT_VRISE                        (0x17 << 8)
+#define HNP_A_SUSPEND                           (0x18 << 8)
+#define HNP_A_WAIT_VFALL                        (0x19 << 8)
+#define HNP_A_VBUS_ERR                          (0x1a << 8)
+#define HNP_CONN_DEBOUNCE                       (0x1b << 8)
+#define HNP_A_WAIT_ABREQ                        (0x1c << 8)
+#define HNP_B_IDLE                              (0x00 << 8)
+#define HNP_B_MASTER                            (0x01 << 8)
+#define HNP_B_SLAVE                             (0x02 << 8)
+#define HNP_B_SRP_INIT_V                        (0x03 << 8)
+#define HNP_B_SRP_INIT_D                        (0x04 << 8)
+#define HNP_B_PRE_WAIT_CONN_AB_SHORT_DB         (0x05 << 8)
+#define HNP_B_WAIT_CONN_A                       (0x06 << 8)
+#define HNP_B_PRE_SLAVE                         (0x0a << 8)
+
+
+#define OTG_CORE_HNP_TIMER1                     (OTG_CORE_BASE+0x20)            //  32bit core HNP timer 1 reg
+#define OTG_CORE_HNP_TIMER2                     (OTG_CORE_BASE+0x24)            //  32bit core HNP timer 2 reg
+#define OTG_CORE_HNP_T3PCR                      (OTG_CORE_BASE+0x28)            //  32bit core HNP timer 3 pulse ctrl
+
+#define HNP_DATAPULSE                           (1 << 5)
+#define HNP_VBUSPULSE                           (1 << 4)
+
+#define OTG_CORE_HINT_STAT                      (OTG_CORE_BASE+0x2C)            //  32bit core HNP int status reg
+
+#define HNP_I2COTGINT                           (1 << 15)
+#define HNP_AWAITBTO                            (1 << 8)                        // Hardware HNP Only
+#define HNP_AIDLEBDTO                           (1 << 7)                        // Hardware HNP Only
+#define HNP_SRPSUCFAIL                          (1 << 6)                        // Hardware HNP Only
+#define HNP_SRPINT                              (1 << 5)
+#define HNP_VBUSERROR                           (1 << 4)                        // Hardware HNP Only
+#define HNP_ABSEVAILD                           (1 << 3)
+#define HNP_ABUSVALID                           (1 << 2)
+#define HNP_MASSLVCHG                           (1 << 1)                        // Hardware HNP Only
+#define HNP_IDCHANGE                            (1)
+
+#define OTG_CORE_HINT_STEN                      (OTG_CORE_BASE+0x30)            //  32bit core HNP int enable reg
+
+#define HNP_I2COTGINT_EN                        (1 << 15)
+#define HNP_AWAITBTO_EN                         (1 << 8)
+#define HNP_AIDLEBDTO_EN                        (1 << 7)
+#define HNP_SRPSUCFAIL_EN                       (1 << 6)
+#define HNP_SRPINT_EN                           (1 << 5)
+#define HNP_VBUSERROR_EN                        (1 << 4)
+#define HNP_ABSEVAILD_EN                        (1 << 3)
+#define HNP_ABUSVALID_EN                        (1 << 2)
+#define HNP_MASSLVCHG_EN                        (1 << 1)
+#define HNP_IDCHANGE_EN                         (1)
+
+
+#define OTG_CORE_CPUEPSEL_STAT                  (OTG_CORE_BASE+0x34)
+#define OTG_CORE_INTERRUPT_STEN                 (OTG_CORE_BASE+0x3c)
+
+
+/*
+ * C.f. 23.11.11 Host Registers
+ */
+
+
+#define OTG_HOST_CONTROL                        (OTG_HOST_BASE+0x00)     //  32bit host controller config reg
+
+#define HOST_CONTROL_HCRESET                    (1 << 31)
+#define HOST_CONTROL_RMTWUEN                    (1 << 4)
+
+#define HOST_CONTROL_HCUSBSTE_RESET             (0 << 2)
+#define HOST_CONTROL_HCUSBSTE_RESUME            (1 << 2)
+#define HOST_CONTROL_HCUSBSTE_OPERATIONAL       (2 << 2)
+#define HOST_CONTROL_HCUSBSTE_SUSPEND           (3 << 2)
+
+#define HOST_CONTROL_CTLBLKSR_11                (0)
+#define HOST_CONTROL_CTLBLKSR_21                (1)
+#define HOST_CONTROL_CTLBLKSR_41                (2)
+#define HOST_CONTROL_CTLBLKSR_81                (3)
+
+
+#define OTG_HOST_SINT_STAT                      (OTG_HOST_BASE+0x08)     //  32bit host system int status reg
+
+#define HOST_PSCINT                             (1 << 6)                // Port Status Change 
+#define HOST_FMOFINT                            (1 << 5)                // Frame Number Overflow
+#define HOST_HERRINT                            (1 << 4)                // Host Error
+#define HOST_RESDETINT                          (1 << 3)                // Resume Detected
+#define HOST_SOFINT                             (1 << 2)                // Start of Frame
+#define HOST_DONEINT                            (1 << 1)                // Done Register
+#define HOST_SORINT                             (1)                     // Schedule Overrun
+
+#define OTG_HOST_SINT_STEN                      (OTG_HOST_BASE+0x0C)     //  32bit host system int enable reg
+
+#define HOST_PSCINT_EN                          (1 << 6)                // Port Status Change 
+#define HOST_FMOFINT_EN                         (1 << 5)                // Frame Number Overflow
+#define HOST_HERRINT_EN                         (1 << 4)                // Host Error
+#define HOST_RESDETINT_EN                       (1 << 3)                // Resume Detected
+#define HOST_SOFINT_EN                          (1 << 2)                // Start of Frame
+#define HOST_DONEINT_EN                         (1 << 1)                // Done Register
+#define HOST_SORINT_EN                          (1)                     // Schedule Overrun
+
+#define OTG_HOST_XINT_STAT                      (OTG_HOST_BASE+0x18)     //  32bit host X buf int status reg
+#define OTG_HOST_YINT_STAT                      (OTG_HOST_BASE+0x1C)     //  32bit host Y buf int status reg
+
+#define OTG_HOST_XYINT_STEN                     (OTG_HOST_BASE+0x20)     //  32bit host XY buf int enable reg
+#define OTG_HOST_XFILL_STAT                     (OTG_HOST_BASE+0x28)     //  32bit host X filled status reg
+#define OTG_HOST_YFILL_STAT                     (OTG_HOST_BASE+0x2C)     //  32bit host Y filled status reg
+
+#define OTG_HOST_ETD_EN                         (OTG_HOST_BASE+0x40)     //  32bit host ETD enables reg
+#define OTG_HOST_DIR_ROUTE                      (OTG_HOST_BASE+0x48)     //  32bit host direct routing reg
+#define OTG_HOST_IINT                           (OTG_HOST_BASE+0x4C)     //  32bit host immediate interrupt reg
+
+#define OTG_HOST_EP_DSTAT                       (OTG_HOST_BASE+0x50)     //  32bit host endpoints done status
+#define OTG_HOST_ETD_DONE                       (OTG_HOST_BASE+0x54)     //  32bit host ETD done reg
+
+#define OTG_HOST_FRM_NUM                        (OTG_HOST_BASE+0x60)     //  32bit host frame number reg
+#define OTG_HOST_LSP_THRESH                     (OTG_HOST_BASE+0x64)     //  32bit host low speed threshold reg
+
+#define OTG_HOST_ROOTHUB_DESCA                  (OTG_HOST_BASE+0x68)     //  32bit host root hub descriptor A
+#define OTG_HOST_ROOTHUB_DESCB                  (OTG_HOST_BASE+0x6C)     //  32bit host root hub descriptor B
+
+/*
+ * C.f. 23.11.29
+ */
+#define OTG_HOST_ROOTHUB_STATUS                 (OTG_HOST_BASE+0x70)     //  32bit host root hub status reg
+
+#define ROOTHUB_STATUS_CLRMTWUE                 (1 << 31)               // Clear Remote Wakeup Enable
+#define ROOTHUB_STATUS_OVRCURCHG                (1 << 17)               // Over Current Indicator Change
+#define ROOTHUB_STATUS_LOCPWRSC                 (1 << 16)               // Local Power Status Change
+#define ROOTHUB_STATUS_DEVCONWUE                (1 << 15)               // Device Connect Wakeup Enable
+#define ROOTHUB_STATUS_OVRCURI                  (1 << 1)                // Over Current Indicator
+#define ROOTHUB_STATUS_LOCPWRS                  (1)                     // Local Power Status
+
+
+/*
+ * C.f. 23.11.30
+ */
+#define OTG_HOST_PORT_STATUS_1                  (OTG_HOST_BASE+0x74)     //  32bit host port 1 status bits
+#define OTG_HOST_PORT_STATUS_2                  (OTG_HOST_BASE+0x78)     //  32bit host port 2 status bits
+#define OTG_HOST_PORT_STATUS_3                  (OTG_HOST_BASE+0x7c)     //  32bit host port 3 status bits
+
+#define PORT_STATUS_PRTRSTSC                    (1 << 20)               // Port Reset Status Change
+#define PORT_STATUS_OVRCURIC                    (1 << 19)               // Port Over Current Indicator Change
+#define PORT_STATUS_PRTSTATSC                   (1 << 18)               // Port Suspend Status Change
+#define PORT_STATUS_PRTENBLSC                   (1 << 17)               // Port Enable Status Change
+#define PORT_STATUS_CONNECTSC                   (1 << 16)               // Connect Status Change
+
+#define PORT_STATUS_LSDEVCON                    (1 << 9)                // Low Speed Device Attached
+#define PORT_STATUS_PRTPWRST                    (1 << 8)                // Port Power Status
+
+#define PORT_STATUS_PRTRSTST                    (1 << 4)                // Port Reset Status
+#define PORT_STATUS_PRTOVRCURI                  (1 << 3)                // Port Over Current Indicator
+#define PORT_STATUS_PRTSUSPST                   (1 << 2)                // Port Suspend Status
+#define PORT_STATUS_PRTENABST                   (1 << 1)                // Port Enable Status
+#define PORT_STATUS_CURCONST                    (1)                     // Current Connect Status
+
+
+/* flags are the same for the interrupt, control and bulk transfer descriptors */
+// R1: sec 23.11.10.2,3 pg 23-44,47
+#define ETD_GET_COMPCODE(flags)   ((flags >> 12) & 0xf)
+// R1: sec 23.11.10 Table 23-23 pg 23-40
+#define ETD_CC_NOERROR            0x0
+#define ETD_CC_CRCERR             0x1
+#define ETD_CC_BITSTUFFERR        0x2
+#define ETD_CC_DATATOGERR         0x3
+#define ETD_CC_STALL              0x4
+#define ETD_CC_DEVNOTRESPONDING   0x5
+#define ETD_CC_PIDFAILURE         0x6
+// Reserved                       0x7
+#define ETD_CC_DATAOVERRUN        0x8
+#define ETD_CC_DATAUNDERRUN       0x9
+#define ETD_CC_ACK                0xA
+#define ETD_CC_NAK                0xB
+#define ETD_CC_BUFFEROVERRUN      0xC
+#define ETD_CC_BUFFERUNDERRUN     0xD
+#define ETD_CC_SCHEDOVERRUN       0xE
+#define ETD_CC_NOTACCESSED        0xF
+
+// R1: sec 23.11.10.2,3 pg 23-44,47 (contd)
+#define ETD_GET_ERRORCNT(flags)   (((flags) >> 8) & 0xf)
+#define ETD_GET_DATATOGL(flags)   (((flags) >> 6) & 0x3)
+#define ETD_SET_DATATOGL(v)       (((v) & 0x3) << 6)
+#define ETD_GET_DELAYINT(flags)   (((flags) >> 3) & 0x7)
+#define ETD_SET_DELAYINT(v)       (((v) & 0x7) << 3)
+#define ETD_GET_BUFROUND(flags)   (((flags) >> 2) & 0x1)
+#define ETD_SET_BUFROUND(v)       (((v) & 0x1) << 2)
+#define ETD_GET_DIRPID(flags)     ((flags) & 0x3)
+#define ETD_SET_DIRPID(v)         ((v) & 0x3)
+#define ETD_FLIP_DIRPID(flags)    ((flags) ^ 0x3) /* xor with 0x3 flips 2<->1 (IN<->OUT) */
+#define ETD_DIRPID_SETUP          0x0  // OUT
+#define ETD_DIRPID_OUT            0x1
+#define ETD_DIRPID_IN             0x2
+#define ETD_DIRPID_RESERVED       0x3
+
+// For non-isoc td.w3.val -
+#define ETD_GET_BUFSIZE(w3)       (((w3) >> 21) & 0xfff)
+#define ETD_SET_BUFSIZE(v)        (((v) & 0xfff) << 21)
+#define ETD_GET_TOTBYECNT(w3)     ((w3) & 0xfffff)
+#define ETD_SET_TOTBYECNT(v)      ((v) & 0xfffff)
+
+/* isoc flags shares the COMPCODE and DELAYINT bitfields with the other TDs,
+   but the remaining bits are different. */
+#define ETD_GET_AUTOISO(flags)    (((flags) >> 11) & 0x1)
+#define ETD_SET_AUTOISO(v)        (((v) & 0x1) << 11)
+#define ETD_GET_FRAMECNT(flags)   (((flags) >> 8) & 0x1)
+#define ETD_SET_FRAMECNT(v)       (((v) & 0x1) << 8)
+// For isoc td.w3.val -
+/* pkt1 and pkt0 use the COMPCODE bitfield, and add a PKTLEN field. */
+#define ETD_GET_PKTLEN(pkt)       ((pkt) & 0x3ff)
+#define ETD_SET_PKTLEN(v)         ((v) & 0x3ff)
+
+
+// ETD endpoint descriptor (etd->epd) bitfield access
+// R1: sec 23.11.10.1 pg 23-41 (word0)
+#define ETD_GET_SNDNAK(epd)    (((epd) >> 30) & 0x1)
+#define ETD_SET_SNDNAK(v)      (((v) & 0x1) << 30)
+#define ETD_GET_TOGCRY(epd)    (((epd) >> 28) & 0x1)
+#define ETD_SET_TOGCRY(v)      (((v) & 0x1) << 28)
+#define ETD_GET_HALTED(epd)    (((epd) >> 27) & 0x1)
+#define ETD_SET_HALTED(v)      (((v) & 0x1) << 27)
+#define ETD_GET_MAXPKTSIZ(epd) (((epd) >> 16) & 0x3ff)
+#define ETD_SET_MAXPKTSIZ(v)   (((v) & 0x3ff) << 16)
+#define ETD_GET_FORMAT(epd)    (((epd) >> 14) & 0x3)
+#define ETD_SET_FORMAT(v)      (((v) & 0x3) << 14)
+#define ETD_FORMAT_CONTROL     0x0
+#define ETD_FORMAT_ISOC        0x1
+#define ETD_FORMAT_BULK        0x2
+#define ETD_FORMAT_INTERRUPT   0x3
+#define ETD_GET_SPEED(epd)     (((epd) >> 13) & 0x1)
+#define ETD_SET_SPEED(v)       (((v) & 0x1) << 13)
+#define ETD_SPEED_FULL         0x0
+#define ETD_SPEED_LOW          0x1
+#define ETD_GET_DIRECT(epd)    (((epd) >> 11) & 0x3)
+#define ETD_SET_DIRECT(v)      (((v) & 0x3) << 11)
+/* xor with 3 flips 2<->1 */
+#define ETD_FLIP_DIRECT(epd)   ((epd) ^ (0x3 << 11))
+#define ETD_DIRECT_TD00        0x0
+#define ETD_DIRECT_OUT         0x1
+#define ETD_DIRECT_IN          0x2
+#define ETD_DIRECT_TD11        0x3
+#define ETD_GET_ENDPNT(epd)    (((epd) >> 7) & 0xf)
+#define ETD_SET_ENDPNT(v)      (((v) & 0xf) << 7)
+#define ETD_GET_ADDRESS(epd)   ((epd) & 0x7f)
+#define ETD_SET_ADDRESS(v)     ((v) & 0x7f)
+
+// etd_urb_state[] values
+#define ETD_URB_COMPLETED       0
+#define ETD_URB_SETUP_STATUS    1
+#define ETD_URB_SETUP_DATA      2
+#define ETD_URB_SETUP_START     3
+#define ETD_URB_BULK_START      4
+#define ETD_URB_BULKWZLP        5
+#define ETD_URB_BULKWZLP_START  6
+#define ETD_URB_INTERRUPT_START 7
+#define ETD_URB_ISOC_START      8
+
+
+/*
+ * C.f. 23.12.8 Function Registers
+ */
+
+#define OTG_FUNC_CMD_STAT                       (OTG_FUNC_BASE+0x00)            //  32bit func command status reg
+
+#define COMMAND_SOFTRESET                       (1 << 7)
+#define COMMAND_BADISOAP                        (1 << 3)
+#define COMMAND_SUPDET                          (1 << 2)
+#define COMMAND_RSMINPROG                       (1 << 1)
+#define COMMAND_RESETDET                        (1)
+
+
+#define OTG_FUNC_DEV_ADDR                       (OTG_FUNC_BASE+0x04)            //  32bit func device address reg
+
+#define OTG_FUNC_SINT_STAT                      (OTG_FUNC_BASE+0x08)            //  32bit func system int status reg
+
+#define SYSTEM_DONEREGINTDS                     (1 << 5)
+#define SYSTEM_SOFDETINT                        (1 << 4)
+#define SYSTEM_DONEREGINT                       (1 << 3)
+#define SYSTEM_SUSPDETINT                       (1 << 2)
+#define SYSTEM_RSMFININT                        (1 << 1)
+#define SYSTEM_RESETINT                         (1)
+
+#define OTG_FUNC_SINT_STEN                      (OTG_FUNC_BASE+0x0C)            //  32bit func system int enable reg
+
+#define SYSTEM_DONEREGINTDS_EN                  (1 << 5)
+#define SYSTEM_SOFDETINT_EN                     (1 << 4)
+#define SYSTEM_DONEREGINT_EN                    (1 << 3)
+#define SYSTEM_SUSPDETINT_EN                    (1 << 2)
+#define SYSTEM_RSMFININT_EN                     (1 << 1)
+#define SYSTEM_RESETINT_EN                      (1)
+
+
+#define OTG_FUNC_XINT_STAT                      (OTG_FUNC_BASE+0x10)            //  32bit func X buf int status reg
+#define OTG_FUNC_YINT_STAT                      (OTG_FUNC_BASE+0x14)            //  32bit func Y buf int status reg
+#define OTG_FUNC_XYINT_STEN                     (OTG_FUNC_BASE+0x18)            //  32bit func XY buf int enable reg
+#define OTG_FUNC_XFILL_STAT                     (OTG_FUNC_BASE+0x1C)            //  32bit func X filled status reg
+
+#define OTG_FUNC_YFILL_STAT                     (OTG_FUNC_BASE+0x20)            //  32bit func Y filled status reg
+#define OTG_FUNC_EP_EN                          (OTG_FUNC_BASE+0x24)            //  32bit func endpoints enable reg
+#define OTG_FUNC_EP_RDY                         (OTG_FUNC_BASE+0x28)            //  32bit func endpoints ready reg
+#define OTG_FUNC_IINT                           (OTG_FUNC_BASE+0x2C)            //  32bit func immediate interrupt reg
+
+#define OTG_FUNC_EP_DSTAT                       (OTG_FUNC_BASE+0x30)            //  32bit func endpoints done status
+#define OTG_FUNC_EP_DEN                         (OTG_FUNC_BASE+0x34)            //  32bit func endpoints done enable
+#define OTG_FUNC_EP_TOGGLE                      (OTG_FUNC_BASE+0x38)            //  32bit func endpoints toggle bits
+#define OTG_FUNC_FRM_NUM                        (OTG_FUNC_BASE+0x3C)            //  32bit func frame number reg
+
+
+#define EP0_STALL                               (1 << 31)
+#define EP0_SETUP                               (1 << 30)
+#define EP0_OVERRUN                             (1 << 29)
+#define EP0_AUTOISO                             (1 << 27)
+
+
+/* generic endpoint register manipulations
+ */
+#define EP_OUT                                  0x1
+#define EP_IN                                   0x2
+#define EP_BOTH                                 0x3
+
+#define ETD_MASK(n)                             (1 << n)
+#define ep_num_both(n)                          (EP_BOTH << n)
+#define ep_num_dir(n, dir)                      ((dir ? EP_IN : EP_OUT) << (n*2))
+#define ep_num_out(n)                           ep_num_dir(n, USB_DIR_OUT)
+#define ep_num_in(n)                            ep_num_dir(n, USB_DIR_IN)
+
+
+/* ep descriptor access
+ */
+static __inline__ u32 etd_word(int n, int word)
+{
+        u32 offset = n * 16;
+        offset += word * 4;
+        return OTG_ETD_BASE + offset;
+}
+
+/* ep descriptor access
+ */
+static __inline__ u32 ep_word(int n, int dir, int word)
+{
+        u32 offset = n * 2;
+        offset += dir ? 1 : 0;
+        offset *= 16;
+        offset += word * 4;
+        return OTG_EP_BASE + offset;
+}
+
+/* endpoint data buffer access
+ * 
+ * This is a simplistic allocator, will do until we want to support ISO or host mode.
+ *
+ * This works because we are assuming a maximum of 16 allocate endpoints, and no
+ * overlapped endpoints (both in and out are allocated).
+ */
+
+static volatile __inline__ u16 data_x_buf(int n, int dir)
+{ 
+        return 0x40 * (n * 4 + 2 * (dir ? 1 : 0));
+}
+static volatile __inline__ u16 data_y_buf(int n, int dir)
+{ 
+        return 0x40 * (n * 4 + 2 * (dir ? 1 : 0) + 1);
+}
+
+static volatile __inline__ u8 * data_x_address(int n, int dir)
+{ 
+        return (volatile u8 *) IO_ADDRESS(OTG_DATA_BASE + data_x_buf(n, dir));
+}
+static volatile __inline__ u8 * data_y_address(int n, int dir)
+{ 
+        return (volatile u8 *) IO_ADDRESS(OTG_DATA_BASE + data_y_buf(n, dir));
+}
+
+/*
+ * C.f. 23.13.3 DMA Registers
+ */
+#define OTG_DMA_REV_NUM                         (OTG_DMA_BASE+0x000)     //  32bit dma revision number reg
+#define OTG_DMA_DINT_STAT                       (OTG_DMA_BASE+0x004)     //  32bit dma int status reg
+#define OTG_DMA_DINT_STEN                       (OTG_DMA_BASE+0x008)     //  32bit dma int enable reg
+#define OTG_DMA_ETD_ERR                         (OTG_DMA_BASE+0x00C)     //  32bit dma ETD error status reg
+#define OTG_DMA_EP_ERR                          (OTG_DMA_BASE+0x010)     //  32bit dma EP error status reg
+#define OTG_DMA_ETD_EN                          (OTG_DMA_BASE+0x020)     //  32bit dma ETD DMA enable reg
+#define OTG_DMA_EP_EN                           (OTG_DMA_BASE+0x024)     //  32bit dma EP DMA enable reg
+#define OTG_DMA_ETD_ENXREQ                      (OTG_DMA_BASE+0x028)     //  32bit dma ETD DMA enable Xtrig req
+#define OTG_DMA_EP_ENXREQ                       (OTG_DMA_BASE+0x02C)     //  32bit dma EP DMA enable Ytrig req
+#define OTG_DMA_ETD_ENXYREQ                     (OTG_DMA_BASE+0x030)     //  32bit dma ETD DMA enble XYtrig req
+#define OTG_DMA_EP_ENXYREQ                      (OTG_DMA_BASE+0x034)     //  32bit dma EP DMA enable XYtrig req
+#define OTG_DMA_ETD_BURST4                      (OTG_DMA_BASE+0x038)     //  32bit dma ETD DMA enble burst4 reg
+#define OTG_DMA_EP_BURST4                       (OTG_DMA_BASE+0x03C)     //  32bit dma EP DMA enable burst4 reg
+#define OTG_DMA_MISC_CTRL                       (OTG_DMA_BASE+0x040)     //  32bit dma EP misc control reg
+#define OTG_DMA_ETD_CH_CLR                      (OTG_DMA_BASE+0x044)     //  32bit dma ETD clear channel reg
+#define OTG_DMA_EP_CH_CLR                       (OTG_DMA_BASE+0x048)     //  32bit dma EP clear channel reg
+                        
+
+#define OTG_DMA_ETD_CH_CLR                      (OTG_DMA_BASE+0x044)     //  32bit dma ETD clear channel reg
+#define OTG_DMA_ETD_ERR                         (OTG_DMA_BASE+0x00C)     //  32bit dma ETD error status reg
+#define OTG_DMA_ETD_EN                          (OTG_DMA_BASE+0x020)     //  32bit dma ETD DMA enable reg
+#define OTG_DMA_ETD_ENXREQ                      (OTG_DMA_BASE+0x028)     //  32bit dma ETD DMA enable Xtrig req
+#define OTG_DMA_ETD_ENXYREQ                     (OTG_DMA_BASE+0x030)     //  32bit dma ETD DMA enble XYtrig req
+#define OTG_DMA_ETD_BURST4                      (OTG_DMA_BASE+0x038)     //  32bit dma ETD DMA enble burst4 reg
+
+
+
+#define OTG_DMA_EP_CH_CLR                       (OTG_DMA_BASE+0x048)     //  32bit dma EP clear channel reg
+#define OTG_DMA_EP_ERR                          (OTG_DMA_BASE+0x010)     //  32bit dma EP error status reg
+#define OTG_DMA_EP_EN                           (OTG_DMA_BASE+0x024)     //  32bit dma EP DMA enable reg
+#define OTG_DMA_EP_ENXREQ                       (OTG_DMA_BASE+0x02C)     //  32bit dma EP DMA enable Ytrig req
+#define OTG_DMA_EP_ENXYREQ                      (OTG_DMA_BASE+0x034)     //  32bit dma EP DMA enable XYtrig req
+#define OTG_DMA_EP_BURST4                       (OTG_DMA_BASE+0x03C)     //  32bit dma EP DMA enable burst4 reg
+
+
+#define dma_num_dir(n, dir) (n * 2 + (dir ? 1 : 0))
+#define dma_num_out(n) dma_num_dir(n, USB_DIR_OUT)
+#define dma_num_in(n) dma_num_dir(n, USB_DIR_IN)
+
+#define OTG_DMA_ETD_MSA(x)                      (OTG_DMA_BASE+0x100+x*4)
+#define OTG_DMA_EPN_MSA(x)                      (OTG_DMA_BASE+0x180+x*4)
+#define OTG_DMA_ETDN_BPTR(x)                    (OTG_DMA_BASE+0x280+x*4)
+#define OTG_DMA_EPN_BPTR(x)                     (OTG_DMA_BASE+0x284+x*4)
+
+
+/*
+ * C.f. 23.14.10 I2C OTG Transceiver Controller Registers
+ * These are the ISP1301 shadow registers.
+ *
+ * See isp1301-hardware.h for register bit defines.
+ */
+#if 1
+#define MX2_VENDOR_ID_LOW                       OTG_I2C_BASE+0x00
+#define MX2_VENDOR_ID_HIGH                      OTG_I2C_BASE+0x01
+#define MX2_PRODUCT_ID_LOW                      OTG_I2C_BASE+0x02
+#define MX2_PRODUCT_ID_HIGH                     OTG_I2C_BASE+0x03
+
+#define MX2_MODE_CONTROL_1_SET                  OTG_I2C_BASE+0x04
+#define MX2_MODE_CONTROL_1_CLR                  OTG_I2C_BASE+0x05
+#define MX2_OTG_CONTROL_SET                     OTG_I2C_BASE+0x06
+#define MX2_OTG_CONTROL_CLR                     OTG_I2C_BASE+0x07
+
+#define MX2_INTERRUPT_SOURCE                    OTG_I2C_BASE+0x08
+
+#define MX2_INT_LAT_REG_SET                     OTG_I2C_BASE+0x0a
+#define MX2_INT_LAT_REG_CLR                     OTG_I2C_BASE+0x0b
+
+#define MX2_INT_FALSE_REG_SET                   OTG_I2C_BASE+0x0c
+#define MX2_INT_FALSE_REG_CLR                   OTG_I2C_BASE+0x0d
+#define MX2_INT_TRUE_REG_SET                    OTG_I2C_BASE+0x0e
+#define MX2_INT_TRUE_REG_CLR                    OTG_I2C_BASE+0x0f
+
+#define MX2_OTG_STATUS_REG                      OTG_I2C_BASE+0x10
+
+#define MX2_MODE_CONTROL_2_SET                  OTG_I2C_BASE+0x12
+#define MX2_MODE_CONTROL_2_CLR                  OTG_I2C_BASE+0x13
+
+#define MX2_BCD_DEV_LOW                         OTG_I2C_BASE+0x14
+#define MX2_BCD_DEV_HIGH                        OTG_I2C_BASE+0x15
+#endif
+
+/*
+ * C.f. 23.14.10 I2C OTG Transceiver Controller Registers
+ * These are the I2C controller and access registers.
+ *
+ * N.B. I2C_ERROR is not documented.
+ */
+
+
+#define I2C_BUSY                                (1 << 7)
+#define I2C_ERROR                               (1 << 2)
+#define I2C_HWSMODE                             (1 << 1)
+#define I2C_I2COE                               (1 << 0)
+
+#define I2C_SCLK_TO_SCL_DIVISION                (OTG_I2C_BASE+0x1E)
+
+#define I2C_INTERRUPT_AND_CONTROL               (OTG_I2C_BASE+0x1F)
+
+#define I2C_STATUS_MASK                         (0x7)
+
+#define I2C_NOACK_EN                            (1 << 6)
+#define I2C_RWREADY_EN                          (1 << 5)
+#define I2C_OTGXCVRINT_EN                       (1 << 4)
+
+#define I2C_NOACK                               (1 << 2)
+#define I2C_RWREADY                             (1 << 1)
+#define I2C_OTGXCVRINT                          (1 << 0)
+
+#define MX2_OTG_XCVR_DEVAD                      OTG_I2C_BASE+0x18
+#define MX2_SEQ_OP_REG                          OTG_I2C_BASE+0x19
+#define MX2_SEQ_RD_STARTAD                      OTG_I2C_BASE+0x1a
+#define MX2_I2C_OP_CTRL_REG                     OTG_I2C_BASE+0x1b
+#define MX2_SCLK_TO_SCL_HPER                    OTG_I2C_BASE+0x1e
+#define MX2_I2C_INTERRUPT_AND_CTRL              OTG_I2C_BASE+0x1f
+
+
+/* ********************************************************************************************** */
+
+/*! mx2_rb
+ */ 
+static u8 __inline__ mx2_rb(u32 port)
+{
+        return *(volatile u8 *) (IO_ADDRESS(port));
+}
+
+/*! mx2_rl
+ */
+static u32  __inline__ mx2_rl(u32 port)
+{       
+        return *(volatile u32 *) (IO_ADDRESS(port));
+}       
+        
+/*! mx2_wb
+ */
+static void __inline__ mx2_wb(u32 port, u8 val)
+{               
+        *(volatile u8 *)(IO_ADDRESS(port)) = val;
+}       
+
+/*! mx2_orb 
+ */     
+static void __inline__ mx2_orb(u32 port, u8 val)
+{       
+        u8 set =  mx2_rb(port) | val;                                   
+        *(volatile u8 *)(IO_ADDRESS(port)) = set;                   
+}       
+
+/*! mx2_andb 
+ */     
+static void __inline__ mx2_andb(u32 port, u8 val)
+{       
+        u8 set =  mx2_rb(port) & val;                                   
+        *(volatile u8 *)(IO_ADDRESS(port)) = set;                   
+}       
+
+/*! mx2_wl
+ */
+static void __inline__ mx2_wl(u32 port, u32 val) 
+{
+        u32 set;
+        *(volatile u32 *)(IO_ADDRESS(port)) = val;
+        set = mx2_rl(port);
+}       
+
+/*! mx2_orl
+ */
+static void __inline__ mx2_orl(u32 port, u32 val)
+{
+        u32 set = mx2_rl(port);
+        *(volatile u32 *)(IO_ADDRESS(port)) = (set | val);
+}
+
+/*! mx2_andl
+ */
+static void __inline__ mx2_andl(u32 port, u32 val)
+{
+        u32 set = mx2_rl(port);
+        *(volatile u32 *)(IO_ADDRESS(port)) = (set & val);
+}
+
+static u32 inline mx2_host_port_stat(int n)
+{
+        switch (n) {
+        default:
+        case 1:
+                return OTG_HOST_PORT_STATUS_1;
+        case 2:
+                return OTG_HOST_PORT_STATUS_2;
+        case 3:
+                return OTG_HOST_PORT_STATUS_3;
+        }
+}
+