aboutsummaryrefslogtreecommitdiff
path: root/drivers/usb/serial/io_ti.c
blob: 6c96fdaec36e3a4de5007494073904b5116f7ff5 (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
/*
 * Edgeport USB Serial Converter driver
 *
 * Copyright (C) 2000-2002 Inside Out Networks, All rights reserved.
 * Copyright (C) 2001-2002 Greg Kroah-Hartman <greg@kroah.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.
 *
 * Supports the following devices:
 *	EP/1 EP/2 EP/4 EP/21 EP/22 EP/221 EP/42 EP/421 WATCHPORT
 *
 * For questions or problems with this driver, contact Inside Out
 * Networks technical support, or Peter Berger <pberger@brimson.com>,
 * or Al Borchers <alborchers@steinerpoint.com>.
 *
 * Version history:
 *
 *	July 11, 2002 	Removed 4 port device structure since all TI UMP 
 *			chips have only 2 ports 
 *			David Iacovelli (davidi@ionetworks.com)
 *
 */

#include <linux/config.h>
#include <linux/kernel.h>
#include <linux/jiffies.h>
#include <linux/errno.h>
#include <linux/init.h>
#include <linux/slab.h>
#include <linux/tty.h>
#include <linux/tty_driver.h>
#include <linux/tty_flip.h>
#include <linux/module.h>
#include <linux/spinlock.h>
#include <linux/serial.h>
#include <linux/ioctl.h>
#include <asm/uaccess.h>
#include <asm/semaphore.h>
#include <linux/usb.h>

#include "usb-serial.h"
#include "io_16654.h"
#include "io_usbvend.h"
#include "io_ti.h"

/*
 * Version Information
 */
#define DRIVER_VERSION "v0.7"
#define DRIVER_AUTHOR "Greg Kroah-Hartman <greg@kroah.com> and David Iacovelli"
#define DRIVER_DESC "Edgeport USB Serial Driver"


/* firmware image code */
#define IMAGE_VERSION_NAME	PagableOperationalCodeImageVersion
#define IMAGE_ARRAY_NAME	PagableOperationalCodeImage
#define IMAGE_SIZE		PagableOperationalCodeSize
#include "io_fw_down3.h"	/* Define array OperationalCodeImage[] */

#define EPROM_PAGE_SIZE		64


struct edgeport_uart_buf_desc {
	__u32 count;		// Number of bytes currently in buffer
};

/* different hardware types */
#define HARDWARE_TYPE_930	0
#define HARDWARE_TYPE_TIUMP	1

// IOCTL_PRIVATE_TI_GET_MODE Definitions
#define	TI_MODE_CONFIGURING	0   // Device has not entered start device 
#define	TI_MODE_BOOT		1   // Staying in boot mode
#define TI_MODE_DOWNLOAD	2   // Made it to download mode
#define TI_MODE_TRANSITIONING	3   // Currently in boot mode but transitioning to download mode

/* read urb state */
#define EDGE_READ_URB_RUNNING	0
#define EDGE_READ_URB_STOPPING	1
#define EDGE_READ_URB_STOPPED	2

#define EDGE_LOW_LATENCY	1
#define EDGE_CLOSING_WAIT	4000	/* in .01 sec */

#define EDGE_OUT_BUF_SIZE	1024


/* Product information read from the Edgeport */
struct product_info
{
	int	TiMode;			// Current TI Mode
	__u8	hardware_type;		// Type of hardware
} __attribute__((packed));

/* circular buffer */
struct edge_buf {
	unsigned int	buf_size;
	char		*buf_buf;
	char		*buf_get;
	char		*buf_put;
};

struct edgeport_port {
	__u16 uart_base;
	__u16 dma_address;
	__u8 shadow_msr;
	__u8 shadow_mcr;
	__u8 shadow_lsr;
	__u8 lsr_mask;
	__u32 ump_read_timeout;		/* Number of miliseconds the UMP will
					   wait without data before completing
					   a read short */
	int baud_rate;
	int close_pending;
	int lsr_event;
	struct edgeport_uart_buf_desc tx;
	struct async_icount	icount;
	wait_queue_head_t	delta_msr_wait;	/* for handling sleeping while
						   waiting for msr change to
						   happen */
	struct edgeport_serial	*edge_serial;
	struct usb_serial_port	*port;
	__u8 bUartMode;		/* Port type, 0: RS232, etc. */ 
	spinlock_t ep_lock;
	int ep_read_urb_state;
	int ep_write_urb_in_use;
	struct edge_buf *ep_out_buf;
};

struct edgeport_serial {
	struct product_info product_info;
	u8 TI_I2C_Type;			// Type of I2C in UMP
	u8 TiReadI2C;			// Set to TRUE if we have read the I2c in Boot Mode
	struct semaphore es_sem;
	int num_ports_open;
	struct usb_serial *serial;
};


/* Devices that this driver supports */
static struct usb_device_id edgeport_1port_id_table [] = {
	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_1) },
	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_TI3410_EDGEPORT_1) },
	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_TI3410_EDGEPORT_1I) },
	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_PROXIMITY) },
	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_MOTION) },
	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_MOISTURE) },
	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_TEMPERATURE) },
	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_HUMIDITY) },
	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_POWER) },
	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_LIGHT) },
	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_RADIATION) },
	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_DISTANCE) },
	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_ACCELERATION) },
	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_PROX_DIST) },
	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_PLUS_PWR_HP4CD) },
	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_PLUS_PWR_PCI) },
	{ }
};

static struct usb_device_id edgeport_2port_id_table [] = {
	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_2) },
	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_2C) },
	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_2I) },
	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_421) },
	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_21) },
	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_42) },
	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_4) },
	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_4I) },
	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_22I) },
	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_221C) },
	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_22C) },
	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_21C) },
// The 4-port shows up as two 2-port devices
	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_4S) },
	{ }
};

/* Devices that this driver supports */
static struct usb_device_id id_table_combined [] = {
	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_1) },
	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_TI3410_EDGEPORT_1) },
	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_TI3410_EDGEPORT_1I) },
	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_PROXIMITY) },
	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_MOTION) },
	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_MOISTURE) },
	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_TEMPERATURE) },
	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_HUMIDITY) },
	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_POWER) },
	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_LIGHT) },
	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_RADIATION) },
	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_DISTANCE) },
	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_ACCELERATION) },
	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_PROX_DIST) },
	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_PLUS_PWR_HP4CD) },
	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_PLUS_PWR_PCI) },
	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_2) },
	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_2C) },
	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_2I) },
	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_421) },
	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_21) },
	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_42) },
	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_4) },
	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_4I) },
	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_22I) },
	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_221C) },
	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_22C) },
	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_21C) },
	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_4S) },
	{ }
};

MODULE_DEVICE_TABLE (usb, id_table_combined);

static struct usb_driver io_driver = {
	.owner =	THIS_MODULE,
	.name =		"io_ti",
	.probe =	usb_serial_probe,
	.disconnect =	usb_serial_disconnect,
	.id_table =	id_table_combined,
};


static struct EDGE_FIRMWARE_VERSION_INFO OperationalCodeImageVersion;

static int debug;

static int TIStayInBootMode = 0;
static int low_latency = EDGE_LOW_LATENCY;
static int closing_wait = EDGE_CLOSING_WAIT;
static int ignore_cpu_rev = 0;


static void edge_tty_recv(struct device *dev, struct tty_struct *tty, unsigned char *data, int length);

static void stop_read(struct edgeport_port *edge_port);
static int restart_read(struct edgeport_port *edge_port);

static void edge_set_termios (struct usb_serial_port *port, struct termios *old_termios);
static void edge_send(struct usb_serial_port *port);

/* circular buffer */
static struct edge_buf *edge_buf_alloc(unsigned int size);
static void edge_buf_free(struct edge_buf *eb);
static void edge_buf_clear(struct edge_buf *eb);
static unsigned int edge_buf_data_avail(struct edge_buf *eb);
static unsigned int edge_buf_space_avail(struct edge_buf *eb);
static unsigned int edge_buf_put(struct edge_buf *eb, const char *buf,
	unsigned int count);
static unsigned int edge_buf_get(struct edge_buf *eb, char *buf,
	unsigned int count);


static int TIReadVendorRequestSync (struct usb_device *dev,
				__u8		request,
				__u16		value,
				__u16		index,
				u8 		*data,
				int		size)
{
	int status;

	status = usb_control_msg (dev,
				usb_rcvctrlpipe(dev, 0),
				request,
				(USB_TYPE_VENDOR | 
				 USB_RECIP_DEVICE | 
				 USB_DIR_IN),
				value,
				index,
				data,
				size,
				1000);
	if (status < 0)
		return status;
	if (status != size) {
		dbg ("%s - wanted to write %d, but only wrote %d",
		     __FUNCTION__, size, status);
		return -ECOMM;
	}
	return 0;
}

static int TISendVendorRequestSync (struct usb_device *dev,
				__u8		request,
				__u16		value,
				__u16		index,
				u8 		*data,
				int		size)
{
	int status;

	status = usb_control_msg (dev,
				usb_sndctrlpipe(dev, 0),
				request,
				(USB_TYPE_VENDOR | 
				 USB_RECIP_DEVICE | 
				 USB_DIR_OUT),
				value,
				index,
				data,
				size,
				1000);
	if (status < 0)
		return status;
	if (status != size) {
		dbg ("%s - wanted to write %d, but only wrote %d",
		     __FUNCTION__, size, status);
		return -ECOMM;
	}
	return 0;
}

static int TIWriteCommandSync (struct usb_device *dev, __u8 command,
				__u8 moduleid, __u16 value, u8 *data,
				int size)
{
	return TISendVendorRequestSync (dev,
					  command,	  		// Request
					  value,			// wValue 
					  moduleid,			// wIndex
					  data,				// TransferBuffer
					  size);			// TransferBufferLength

}

/* clear tx/rx buffers and fifo in TI UMP */
static int TIPurgeDataSync (struct usb_serial_port *port, __u16 mask)
{
	int port_number = port->number - port->serial->minor;

	dbg ("%s - port %d, mask %x", __FUNCTION__, port_number, mask);

	return TIWriteCommandSync (port->serial->dev,
					UMPC_PURGE_PORT,
					(__u8)(UMPM_UART1_PORT + port_number),
					mask,
					NULL,
					0);
}

/**
 * TIReadDownloadMemory - Read edgeport memory from TI chip
 * @dev: usb device pointer
 * @start_address: Device CPU address at which to read
 * @length: Length of above data
 * @address_type: Can read both XDATA and I2C
 * @buffer: pointer to input data buffer
 */
static int TIReadDownloadMemory(struct usb_device *dev, int start_address,
				int length, __u8 address_type, __u8 *buffer)
{
	int status = 0;
	__u8 read_length;
	__be16 be_start_address;
	
	dbg ("%s - @ %x for %d", __FUNCTION__, start_address, length);

	/* Read in blocks of 64 bytes
	 * (TI firmware can't handle more than 64 byte reads)
	 */
	while (length) {
		if (length > 64)
			read_length= 64;
		else
			read_length = (__u8)length;

		if (read_length > 1) {
			dbg ("%s - @ %x for %d", __FUNCTION__, 
			     start_address, read_length);
		}
		be_start_address = cpu_to_be16 (start_address);
		status = TIReadVendorRequestSync (dev,
						  UMPC_MEMORY_READ,	// Request
						  (__u16)address_type,	// wValue (Address type)
						  (__force __u16)be_start_address,	// wIndex (Address to read)
						  buffer,		// TransferBuffer
						  read_length);	// TransferBufferLength

		if (status) {
			dbg ("%s - ERROR %x", __FUNCTION__, status);
			return status;
		}

		if (read_length > 1) {
			usb_serial_debug_data(debug, &dev->dev, __FUNCTION__,
					      read_length, buffer);
		}

		/* Update pointers/length */
		start_address += read_length;
		buffer += read_length;
		length -= read_length;
	}
	
	return status;
}

static int TIReadRam (struct usb_device *dev, int start_address, int length, __u8 *buffer)
{
	return TIReadDownloadMemory (dev,
				     start_address,
				     length,
				     DTK_ADDR_SPACE_XDATA,
				     buffer);
}

/* Read edgeport memory to a given block */
static int TIReadBootMemory (struct edgeport_serial *serial, int start_address, int length, __u8 * buffer)
{
	int status = 0;
	int i;

	for (i=0; i< length; i++) {
		status = TIReadVendorRequestSync (serial->serial->dev,
					UMPC_MEMORY_READ,		// Request
					serial->TI_I2C_Type,		// wValue (Address type)
					(__u16)(start_address+i),	// wIndex
					&buffer[i],			// TransferBuffer
					0x01);				// TransferBufferLength
		if (status) {
			dbg ("%s - ERROR %x", __FUNCTION__, status);
			return status;
		}
	}

	dbg ("%s - start_address = %x, length = %d", __FUNCTION__, start_address, length);
	usb_serial_debug_data(debug, &serial->serial->dev->dev, __FUNCTION__, length, buffer);

	serial->TiReadI2C = 1;

	return status;
}

/* Write given block to TI EPROM memory */
static int TIWriteBootMemory (struct edgeport_serial *serial, int start_address, int length, __u8 *buffer)
{
	int status = 0;
	int i;
	__u8 temp;

	/* Must do a read before write */
	if (!serial->TiReadI2C) {
		status = TIReadBootMemory(serial, 0, 1, &temp);
		if (status)
			return status;
	}

	for (i=0; i < length; ++i) {
		status = TISendVendorRequestSync (serial->serial->dev,
						UMPC_MEMORY_WRITE,		// Request
						buffer[i],			// wValue
						(__u16)(i+start_address),	// wIndex
						NULL,				// TransferBuffer
						0);				// TransferBufferLength
		if (status)
			return status;
	}

  	dbg ("%s - start_sddr = %x, length = %d", __FUNCTION__, start_address, length);
	usb_serial_debug_data(debug, &serial->serial->dev->dev, __FUNCTION__, length, buffer);

	return status;
}


/* Write edgeport I2C memory to TI chip	*/
static int TIWriteDownloadI2C (struct edgeport_serial *serial, int start_address, int length, __u8 address_type, __u8 *buffer)
{
	int status = 0;
	int write_length;
	__be16 be_start_address;

	/* We can only send a maximum of 1 aligned byte page at a time */
	
	/* calulate the number of bytes left in the first page */
	write_length = EPROM_PAGE_SIZE - (start_address & (EPROM_PAGE_SIZE - 1));

	if (write_length > length)
		write_length = length;

	dbg ("%s - BytesInFirstPage Addr = %x, length = %d", __FUNCTION__, start_address, write_length);
	usb_serial_debug_data(debug, &serial->serial->dev->dev, __FUNCTION__, write_length, buffer);

	/* Write first page */
	be_start_address = cpu_to_be16 (start_address);
	status = TISendVendorRequestSync (serial->serial->dev,
					UMPC_MEMORY_WRITE,	// Request
					(__u16)address_type,	// wValue
					(__force __u16)be_start_address,	// wIndex
					buffer,			// TransferBuffer
					write_length);
	if (status) {
		dbg ("%s - ERROR %d", __FUNCTION__, status);
		return status;
	}

	length		-= write_length;
	start_address	+= write_length;
	buffer		+= write_length;

	/* We should be aligned now -- can write max page size bytes at a time */
	while (length) {
		if (length > EPROM_PAGE_SIZE)
			write_length = EPROM_PAGE_SIZE;
		else
			write_length = length;

		dbg ("%s - Page Write Addr = %x, length = %d", __FUNCTION__, start_address, write_length);
		usb_serial_debug_data(debug, &serial->serial->dev->dev, __FUNCTION__, write_length, buffer);

		/* Write next page */
		be_start_address = cpu_to_be16 (start_address);
		status = TISendVendorRequestSync (serial->serial->dev,
						UMPC_MEMORY_WRITE,	// Request
						(__u16)address_type,	// wValue
						(__force __u16)be_start_address,	// wIndex
						buffer,	  		// TransferBuffer
						write_length);		// TransferBufferLength
		if (status) {
			dev_err (&serial->serial->dev->dev, "%s - ERROR %d\n", __FUNCTION__, status);
			return status;
		}
		
		length		-= write_length;
		start_address	+= write_length;
		buffer		+= write_length;
	}
	return status;
}

/* Examine the UMP DMA registers and LSR
 * 
 * Check the MSBit of the X and Y DMA byte count registers.
 * A zero in this bit indicates that the TX DMA buffers are empty
 * then check the TX Empty bit in the UART.
 */
static int TIIsTxActive (struct edgeport_port *port)
{
	int status;
	struct out_endpoint_desc_block *oedb;
	__u8 *lsr;
	int bytes_left = 0;

	oedb = kmalloc (sizeof (* oedb), GFP_KERNEL);
	if (!oedb) {
		dev_err (&port->port->dev, "%s - out of memory\n", __FUNCTION__);
		return -ENOMEM;
	}

	lsr = kmalloc (1, GFP_KERNEL);	/* Sigh, that's right, just one byte,
					   as not all platforms can do DMA
					   from stack */
	if (!lsr) {
		kfree(oedb);
		return -ENOMEM;
	}
	/* Read the DMA Count Registers */
	status = TIReadRam (port->port->serial->dev,
			    port->dma_address,
			    sizeof( *oedb),
			    (void *)oedb);

	if (status)
		goto exit_is_tx_active;

	dbg ("%s - XByteCount    0x%X", __FUNCTION__, oedb->XByteCount);

	/* and the LSR */
	status = TIReadRam (port->port->serial->dev, 
			    port->uart_base + UMPMEM_OFFS_UART_LSR,
			    1,
			    lsr);

	if (status)
		goto exit_is_tx_active;
	dbg ("%s - LSR = 0x%X", __FUNCTION__, *lsr);
	
	/* If either buffer has data or we are transmitting then return TRUE */
	if ((oedb->XByteCount & 0x80 ) != 0 )
		bytes_left += 64;

	if ((*lsr & UMP_UART_LSR_TX_MASK ) == 0 )
		bytes_left += 1;

	/* We return Not Active if we get any kind of error */
exit_is_tx_active:
	dbg ("%s - return %d", __FUNCTION__, bytes_left );

	kfree(lsr);
	kfree(oedb);
	return bytes_left;
}

static void TIChasePort(struct edgeport_port *port, unsigned long timeout, int flush)
{
	int baud_rate;
	struct tty_struct *tty = port->port->tty;
	wait_queue_t wait;
	unsigned long flags;

	if (!timeout)
		timeout = (HZ*EDGE_CLOSING_WAIT)/100;

	/* wait for data to drain from the buffer */
	spin_lock_irqsave(&port->ep_lock, flags);
	init_waitqueue_entry(&wait, current);
	add_wait_queue(&tty->write_wait, &wait);
	for (;;) {
		set_current_state(TASK_INTERRUPTIBLE);
		if (edge_buf_data_avail(port->ep_out_buf) == 0
		|| timeout == 0 || signal_pending(current)
		|| !usb_get_intfdata(port->port->serial->interface))  /* disconnect */
			break;
		spin_unlock_irqrestore(&port->ep_lock, flags);
		timeout = schedule_timeout(timeout);
		spin_lock_irqsave(&port->ep_lock, flags);
	}
	set_current_state(TASK_RUNNING);
	remove_wait_queue(&tty->write_wait, &wait);
	if (flush)
		edge_buf_clear(port->ep_out_buf);
	spin_unlock_irqrestore(&port->ep_lock, flags);

	/* wait for data to drain from the device */
	timeout += jiffies;
	while ((long)(jiffies - timeout) < 0 && !signal_pending(current)
	&& usb_get_intfdata(port->port->serial->interface)) {  /* not disconnected */
		if (!TIIsTxActive(port))
			break;
		msleep(10);
	}

	/* disconnected */
	if (!usb_get_intfdata(port->port->serial->interface))
		return;

	/* wait one more character time, based on baud rate */
	/* (TIIsTxActive doesn't seem to wait for the last byte) */
	if ((baud_rate=port->baud_rate) == 0)
		baud_rate = 50;
	msleep(max(1,(10000+baud_rate-1)/baud_rate));
}

static int TIChooseConfiguration (struct usb_device *dev)
{
	// There may be multiple configurations on this device, in which case
	// we would need to read and parse all of them to find out which one
	// we want. However, we just support one config at this point,
	// configuration # 1, which is Config Descriptor 0.

	dbg ("%s - Number of Interfaces = %d", __FUNCTION__, dev->config->desc.bNumInterfaces);
	dbg ("%s - MAX Power            = %d", __FUNCTION__, dev->config->desc.bMaxPower*2);

	if (dev->config->desc.bNumInterfaces != 1) {
		dev_err (&dev->dev, "%s - bNumInterfaces is not 1, ERROR!\n", __FUNCTION__);
		return -ENODEV;
	}

	return 0;
}

static int TIReadRom (struct edgeport_serial *serial, int start_address, int length, __u8 *buffer)
{
	int status;

	if (serial->product_info.TiMode == TI_MODE_DOWNLOAD) {
		status = TIReadDownloadMemory (serial->serial->dev,
					       start_address,
					       length,
					       serial->TI_I2C_Type,
					       buffer);
	} else {
		status = TIReadBootMemory (serial,
					   start_address,
					   length,
					   buffer);
	}

	return status;
}

static int TIWriteRom (struct edgeport_serial *serial, int start_address, int length, __u8 *buffer)
{
	if (serial->product_info.TiMode == TI_MODE_BOOT)
		return TIWriteBootMemory (serial,
					  start_address,
					  length,
					  buffer);

	if (serial->product_info.TiMode == TI_MODE_DOWNLOAD)
		return TIWriteDownloadI2C (serial,
					   start_address,
					   length,
					   serial->TI_I2C_Type,
					   buffer);

	return -EINVAL;
}



/* Read a descriptor header from I2C based on type */
static int TIGetDescriptorAddress (struct edgeport_serial *serial, int desc_type, struct ti_i2c_desc *rom_desc)
{
	int start_address;
	int status;

	/* Search for requested descriptor in I2C */
	start_address = 2;
	do {
		status = TIReadRom (serial,
				   start_address,
				   sizeof(struct ti_i2c_desc),
				   (__u8 *)rom_desc );
		if (status)
			return 0;

		if (rom_desc->Type == desc_type)
			return start_address;

		start_address = start_address + sizeof(struct ti_i2c_desc) +  rom_desc->Size;

	} while ((start_address < TI_MAX_I2C_SIZE) && rom_desc->Type);
	
	return 0;
}

/* Validate descriptor checksum */
static int ValidChecksum(struct ti_i2c_desc *rom_desc, __u8 *buffer)
{
	__u16 i;
	__u8 cs = 0;

	for (i=0; i < rom_desc->Size; i++) {
		cs = (__u8)(cs + buffer[i]);
	}
	if (cs != rom_desc->CheckSum) {
		dbg ("%s - Mismatch %x - %x", __FUNCTION__, rom_desc->CheckSum, cs);
		return -EINVAL;
	}
	return 0;
}

/* Make sure that the I2C image is good */
static int TiValidateI2cImage (struct edgeport_serial *serial)
{
	struct device *dev = &serial->serial->dev->dev;
	int status = 0;
	struct ti_i2c_desc *rom_desc;
	int start_address = 2;
	__u8 *buffer;
	__u16 ttype;

	rom_desc = kmalloc (sizeof (*rom_desc), GFP_KERNEL);
	if (!rom_desc) {
		dev_err (dev, "%s - out of memory\n", __FUNCTION__);
		return -ENOMEM;
	}
	buffer = kmalloc (TI_MAX_I2C_SIZE, GFP_KERNEL);
	if (!buffer) {
		dev_err (dev, "%s - out of memory when allocating buffer\n", __FUNCTION__);
		kfree (rom_desc);
		return -ENOMEM;
	}

	// Read the first byte (Signature0) must be 0x52 or 0x10
	status = TIReadRom (serial, 0, 1, buffer);
	if (status)
		goto ExitTiValidateI2cImage; 

	if (*buffer != UMP5152 && *buffer != UMP3410) {
		dev_err (dev, "%s - invalid buffer signature\n", __FUNCTION__);
		status = -ENODEV;
		goto ExitTiValidateI2cImage;
	}

	do {
		// Validate the I2C
		status = TIReadRom (serial,
				start_address,
				sizeof(struct ti_i2c_desc),
				(__u8 *)rom_desc);
		if (status)
			break;

		if ((start_address + sizeof(struct ti_i2c_desc) + rom_desc->Size) > TI_MAX_I2C_SIZE) {
			status = -ENODEV;
			dbg ("%s - structure too big, erroring out.", __FUNCTION__);
			break;
		}

		dbg ("%s Type = 0x%x", __FUNCTION__, rom_desc->Type);

		// Skip type 2 record
		ttype = rom_desc->Type & 0x0f;
		if ( ttype != I2C_DESC_TYPE_FIRMWARE_BASIC
			&& ttype != I2C_DESC_TYPE_FIRMWARE_AUTO ) {
			// Read the descriptor data
			status = TIReadRom(serial,
						start_address+sizeof(struct ti_i2c_desc),
						rom_desc->Size,
						buffer);
			if (status)
				break;

			status = ValidChecksum(rom_desc, buffer);
			if (status)
				break;
		}
		start_address = start_address + sizeof(struct ti_i2c_desc) + rom_desc->Size;

	} while ((rom_desc->Type != I2C_DESC_TYPE_ION) && (start_address < TI_MAX_I2C_SIZE));

	if ((rom_desc->Type != I2C_DESC_TYPE_ION) || (start_address > TI_MAX_I2C_SIZE))
		status = -ENODEV;

ExitTiValidateI2cImage:	
	kfree (buffer);
	kfree (rom_desc);
	return status;
}

static int TIReadManufDescriptor (struct edgeport_serial *serial, __u8 *buffer)
{
	int status;
	int start_address;
	struct ti_i2c_desc *rom_desc;
	struct edge_ti_manuf_descriptor *desc;

	rom_desc = kmalloc (sizeof (*rom_desc), GFP_KERNEL);
	if (!rom_desc) {
		dev_err (&serial->serial->dev->dev, "%s - out of memory\n", __FUNCTION__);
		return -ENOMEM;
	}
	start_address = TIGetDescriptorAddress (serial, I2C_DESC_TYPE_ION, rom_desc);

	if (!start_address) {
		dbg ("%s - Edge Descriptor not found in I2C", __FUNCTION__);
		status = -ENODEV;
		goto exit;
	}

	// Read the descriptor data
	status = TIReadRom (serial,
				start_address+sizeof(struct ti_i2c_desc),
				rom_desc->Size,
				buffer);
	if (status)
		goto exit;
	
	status = ValidChecksum(rom_desc, buffer);
	
	desc = (struct edge_ti_manuf_descriptor *)buffer;
	dbg ( "%s - IonConfig      0x%x", __FUNCTION__, desc->IonConfig 	);
	dbg ( "%s - Version          %d", __FUNCTION__, desc->Version	  	);
	dbg ( "%s - Cpu/Board      0x%x", __FUNCTION__, desc->CpuRev_BoardRev	);
	dbg ( "%s - NumPorts         %d", __FUNCTION__, desc->NumPorts  	);	
	dbg ( "%s - NumVirtualPorts  %d", __FUNCTION__, desc->NumVirtualPorts	);	
	dbg ( "%s - TotalPorts       %d", __FUNCTION__, desc->TotalPorts  	);	

exit:
	kfree (rom_desc);
	return status;
}

/* Build firmware header used for firmware update */
static int BuildI2CFirmwareHeader (__u8 *header, struct device *dev)
{
	__u8 *buffer;
	int buffer_size;
	int i;
	__u8 cs = 0;
	struct ti_i2c_desc *i2c_header;
	struct ti_i2c_image_header *img_header;
	struct ti_i2c_firmware_rec *firmware_rec;

	// In order to update the I2C firmware we must change the type 2 record to type 0xF2.
	// This will force the UMP to come up in Boot Mode.  Then while in boot mode, the driver 
	// will download the latest firmware (padded to 15.5k) into the UMP ram. 
	// And finally when the device comes back up in download mode the driver will cause 
	// the new firmware to be copied from the UMP Ram to I2C and the firmware will update
	// the record type from 0xf2 to 0x02.
	
	// Allocate a 15.5k buffer + 2 bytes for version number (Firmware Record)
	buffer_size = (((1024 * 16) - 512 )+ sizeof(struct ti_i2c_firmware_rec));

	buffer = kmalloc (buffer_size, GFP_KERNEL);
	if (!buffer) {
		dev_err (dev, "%s - out of memory\n", __FUNCTION__);
		return -ENOMEM;
	}
	
	// Set entire image of 0xffs
	memset (buffer, 0xff, buffer_size);

	// Copy version number into firmware record
	firmware_rec = (struct ti_i2c_firmware_rec *)buffer;

	firmware_rec->Ver_Major	= OperationalCodeImageVersion.MajorVersion;
	firmware_rec->Ver_Minor	= OperationalCodeImageVersion.MinorVersion;

	// Pointer to fw_down memory image
	img_header = (struct ti_i2c_image_header *)&PagableOperationalCodeImage[0];

	memcpy (buffer + sizeof(struct ti_i2c_firmware_rec),
		&PagableOperationalCodeImage[sizeof(struct ti_i2c_image_header)],
		le16_to_cpu(img_header->Length));

	for (i=0; i < buffer_size; i++) {
		cs = (__u8)(cs + buffer[i]);
	}

	kfree (buffer);

	// Build new header
	i2c_header =  (struct ti_i2c_desc *)header;
	firmware_rec =  (struct ti_i2c_firmware_rec*)i2c_header->Data;
	
	i2c_header->Type	= I2C_DESC_TYPE_FIRMWARE_BLANK;
	i2c_header->Size	= (__u16)buffer_size;
	i2c_header->CheckSum	= cs;
	firmware_rec->Ver_Major	= OperationalCodeImageVersion.MajorVersion;
	firmware_rec->Ver_Minor	= OperationalCodeImageVersion.MinorVersion;

	return 0;
}

/* Try to figure out what type of I2c we have */
static int TIGetI2cTypeInBootMode (struct edgeport_serial *serial)
{
	int status;
	__u8 data;
		
	// Try to read type 2
	status = TIReadVendorRequestSync (serial->serial->dev,
					UMPC_MEMORY_READ,		// Request
					DTK_ADDR_SPACE_I2C_TYPE_II,	// wValue (Address type)
					0,		 		// wIndex
					&data,				// TransferBuffer
					0x01);				// TransferBufferLength
	if (status)
		dbg ("%s - read 2 status error = %d", __FUNCTION__, status);
	else
		dbg ("%s - read 2 data = 0x%x", __FUNCTION__, data);
	if ((!status) && (data == UMP5152 || data == UMP3410)) {
		dbg ("%s - ROM_TYPE_II", __FUNCTION__);
		serial->TI_I2C_Type = DTK_ADDR_SPACE_I2C_TYPE_II;
		return 0;
	}

	// Try to read type 3
	status = TIReadVendorRequestSync (serial->serial->dev,
					UMPC_MEMORY_READ,		// Request
					DTK_ADDR_SPACE_I2C_TYPE_III,	// wValue (Address type)
					0,				// wIndex
					&data,				// TransferBuffer
					0x01);				// TransferBufferLength
	if (status)
		dbg ("%s - read 3 status error = %d", __FUNCTION__, status);
	else
		dbg ("%s - read 2 data = 0x%x", __FUNCTION__, data);
	if ((!status) && (data == UMP5152 || data == UMP3410)) {
		dbg ("%s - ROM_TYPE_III", __FUNCTION__);
		serial->TI_I2C_Type = DTK_ADDR_SPACE_I2C_TYPE_III;
		return 0;
	}

	dbg ("%s - Unknown", __FUNCTION__);
	serial->TI_I2C_Type = DTK_ADDR_SPACE_I2C_TYPE_II;
	return -ENODEV;
}

static int TISendBulkTransferSync (struct usb_serial *serial, void *buffer, int length, int *num_sent)
{
	int status;

	status = usb_bulk_msg (serial->dev,
				usb_sndbulkpipe(serial->dev,
						serial->port[0]->bulk_out_endpointAddress),
				buffer,
				length,
				num_sent,
				1000);
	return status;
}

/* Download given firmware image to the device (IN BOOT MODE) */
static int TIDownloadCodeImage (struct edgeport_serial *serial, __u8 *image, int image_length)
{
	int status = 0;
	int pos;
	int transfer;
	int done;

	// Transfer firmware image
	for (pos = 0; pos < image_length; ) {
		// Read the next buffer from file
		transfer = image_length - pos;
		if (transfer > EDGE_FW_BULK_MAX_PACKET_SIZE)
			transfer = EDGE_FW_BULK_MAX_PACKET_SIZE;

		// Transfer data
		status = TISendBulkTransferSync (serial->serial, &image[pos], transfer, &done);
		if (status)
			break;
		// Advance buffer pointer
		pos += done;
	}

	return status;
}

// FIXME!!!
static int TIConfigureBootDevice (struct usb_device *dev)
{
	return 0;
}

/**
 * DownloadTIFirmware - Download run-time operating firmware to the TI5052
 * 
 * This routine downloads the main operating code into the TI5052, using the
 * boot code already burned into E2PROM or ROM.
 */
static int TIDownloadFirmware (struct edgeport_serial *serial)
{
	struct device *dev = &serial->serial->dev->dev;
	int status = 0;
	int start_address;
	struct edge_ti_manuf_descriptor *ti_manuf_desc;
	struct usb_interface_descriptor *interface;
	int download_cur_ver;
	int download_new_ver;

	/* This routine is entered by both the BOOT mode and the Download mode
	 * We can determine which code is running by the reading the config
	 * descriptor and if we have only one bulk pipe it is in boot mode
	 */
	serial->product_info.hardware_type = HARDWARE_TYPE_TIUMP;

	/* Default to type 2 i2c */
	serial->TI_I2C_Type = DTK_ADDR_SPACE_I2C_TYPE_II;

	status = TIChooseConfiguration (serial->serial->dev);
	if (status)
		return status;

	interface = &serial->serial->interface->cur_altsetting->desc;
	if (!interface) {
		dev_err (dev, "%s - no interface set, error!\n", __FUNCTION__);
		return -ENODEV;
	}

	// Setup initial mode -- the default mode 0 is TI_MODE_CONFIGURING
	// if we have more than one endpoint we are definitely in download mode
	if (interface->bNumEndpoints > 1)
		serial->product_info.TiMode = TI_MODE_DOWNLOAD;
	else
		// Otherwise we will remain in configuring mode
		serial->product_info.TiMode = TI_MODE_CONFIGURING;

	// Save Download Version Number
	OperationalCodeImageVersion.MajorVersion = PagableOperationalCodeImageVersion.MajorVersion;
	OperationalCodeImageVersion.MinorVersion = PagableOperationalCodeImageVersion.MinorVersion;
	OperationalCodeImageVersion.BuildNumber	 = PagableOperationalCodeImageVersion.BuildNumber;

	/********************************************************************/
	/* Download Mode */
	/********************************************************************/
	if (serial->product_info.TiMode == TI_MODE_DOWNLOAD) {
		struct ti_i2c_desc *rom_desc;

		dbg ("%s - <<<<<<<<<<<<<<<RUNNING IN DOWNLOAD MODE>>>>>>>>>>", __FUNCTION__);

		status = TiValidateI2cImage (serial);
		if (status) {
			dbg ("%s - <<<<<<<<<<<<<<<DOWNLOAD MODE -- BAD I2C >>>>>>>>>>",
			     __FUNCTION__);
			return status;
		}
		
		/* Validate Hardware version number
		 * Read Manufacturing Descriptor from TI Based Edgeport
		 */
		ti_manuf_desc = kmalloc (sizeof (*ti_manuf_desc), GFP_KERNEL);
		if (!ti_manuf_desc) {
			dev_err (dev, "%s - out of memory.\n", __FUNCTION__);
			return -ENOMEM;
		}
		status = TIReadManufDescriptor (serial, (__u8 *)ti_manuf_desc);
		if (status) {
			kfree (ti_manuf_desc);
			return status;
		}

		// Check version number of ION descriptor
		if (!ignore_cpu_rev && TI_GET_CPU_REVISION(ti_manuf_desc->CpuRev_BoardRev) < 2) {
			dbg ( "%s - Wrong CPU Rev %d (Must be 2)", __FUNCTION__, 
			     TI_GET_CPU_REVISION(ti_manuf_desc->CpuRev_BoardRev));
			kfree (ti_manuf_desc);
		   	return -EINVAL;
		}

		rom_desc = kmalloc (sizeof (*rom_desc), GFP_KERNEL);
		if (!rom_desc) {
			dev_err (dev, "%s - out of memory.\n", __FUNCTION__);
			kfree (ti_manuf_desc);
			return -ENOMEM;
		}

		// Search for type 2 record (firmware record)
		if ((start_address = TIGetDescriptorAddress (serial, I2C_DESC_TYPE_FIRMWARE_BASIC, rom_desc)) != 0) {
			struct ti_i2c_firmware_rec *firmware_version;
			__u8 record;

			dbg ("%s - Found Type FIRMWARE (Type 2) record", __FUNCTION__);

			firmware_version = kmalloc (sizeof (*firmware_version), GFP_KERNEL);
			if (!firmware_version) {
				dev_err (dev, "%s - out of memory.\n", __FUNCTION__);
				kfree (rom_desc);
				kfree (ti_manuf_desc);
				return -ENOMEM;
			}

			// Validate version number				
			// Read the descriptor data
			status = TIReadRom (serial,
					start_address+sizeof(struct ti_i2c_desc),
					sizeof(struct ti_i2c_firmware_rec),
					(__u8 *)firmware_version);
			if (status) {
				kfree (firmware_version);
				kfree (rom_desc);
				kfree (ti_manuf_desc);
				return status;
			}

			// Check version number of download with current version in I2c
			download_cur_ver = (firmware_version->Ver_Major << 8) + 
					   (firmware_version->Ver_Minor);
			download_new_ver = (OperationalCodeImageVersion.MajorVersion << 8) +
					   (OperationalCodeImageVersion.MinorVersion);

			dbg ("%s - >>>Firmware Versions Device %d.%d  Driver %d.%d",
			     __FUNCTION__,
			     firmware_version->Ver_Major,
			     firmware_version->Ver_Minor,
			     OperationalCodeImageVersion.MajorVersion,
			     OperationalCodeImageVersion.MinorVersion);

			// Check if we have an old version in the I2C and update if necessary
			if (download_cur_ver != download_new_ver) {
				dbg ("%s - Update I2C Download from %d.%d to %d.%d",
				     __FUNCTION__,
				     firmware_version->Ver_Major,
				     firmware_version->Ver_Minor,
				     OperationalCodeImageVersion.MajorVersion,
				     OperationalCodeImageVersion.MinorVersion);

				// In order to update the I2C firmware we must change the type 2 record to type 0xF2.
				// This will force the UMP to come up in Boot Mode.  Then while in boot mode, the driver 
				// will download the latest firmware (padded to 15.5k) into the UMP ram. 
				// And finally when the device comes back up in download mode the driver will cause 
				// the new firmware to be copied from the UMP Ram to I2C and the firmware will update
				// the record type from 0xf2 to 0x02.

				record = I2C_DESC_TYPE_FIRMWARE_BLANK;

				// Change the I2C Firmware record type to 0xf2 to trigger an update
				status = TIWriteRom (serial,
							start_address,
							sizeof(record),
							&record);
				if (status) {
					kfree (firmware_version);
					kfree (rom_desc);
					kfree (ti_manuf_desc);
					return status;
				}

				// verify the write -- must do this in order for write to 
				// complete before we do the hardware reset
				status = TIReadRom (serial,
							start_address,
							sizeof(record),
							&record);

				if (status) {
					kfree (firmware_version);
					kfree (rom_desc);
					kfree (ti_manuf_desc);
					return status;
				}

				if (record != I2C_DESC_TYPE_FIRMWARE_BLANK) {
					dev_err (dev, "%s - error resetting device\n", __FUNCTION__);
					kfree (firmware_version);
					kfree (rom_desc);
					kfree (ti_manuf_desc);
					return -ENODEV;
				}

				dbg ("%s - HARDWARE RESET", __FUNCTION__);

				// Reset UMP -- Back to BOOT MODE
				status = TISendVendorRequestSync (serial->serial->dev,
								UMPC_HARDWARE_RESET,	// Request
								0,			// wValue
								0,			// wIndex
								NULL,			// TransferBuffer
								0);			// TransferBufferLength

				dbg ( "%s - HARDWARE RESET return %d", __FUNCTION__, status);

				/* return an error on purpose. */
				kfree (firmware_version);
				kfree (rom_desc);
				kfree (ti_manuf_desc);
				return -ENODEV;
			}
			kfree (firmware_version);
		}
		// Search for type 0xF2 record (firmware blank record)
		else if ((start_address = TIGetDescriptorAddress (serial, I2C_DESC_TYPE_FIRMWARE_BLANK, rom_desc)) != 0) {
			#define HEADER_SIZE	(sizeof(struct ti_i2c_desc) + sizeof(struct ti_i2c_firmware_rec))
			__u8 *header;
			__u8 *vheader;

			header  = kmalloc (HEADER_SIZE, GFP_KERNEL);
			if (!header) {
				dev_err (dev, "%s - out of memory.\n", __FUNCTION__);
				kfree (rom_desc);
				kfree (ti_manuf_desc);
				return -ENOMEM;
			}
				
			vheader = kmalloc (HEADER_SIZE, GFP_KERNEL);
			if (!vheader) {
				dev_err (dev, "%s - out of memory.\n", __FUNCTION__);
				kfree (header);
				kfree (rom_desc);
				kfree (ti_manuf_desc);
				return -ENOMEM;
			}
			
			dbg ("%s - Found Type BLANK FIRMWARE (Type F2) record", __FUNCTION__);

			// In order to update the I2C firmware we must change the type 2 record to type 0xF2.
			// This will force the UMP to come up in Boot Mode.  Then while in boot mode, the driver 
			// will download the latest firmware (padded to 15.5k) into the UMP ram. 
			// And finally when the device comes back up in download mode the driver will cause 
			// the new firmware to be copied from the UMP Ram to I2C and the firmware will update
			// the record type from 0xf2 to 0x02.
			status = BuildI2CFirmwareHeader(header, dev);
			if (status) {
				kfree (vheader);
				kfree (header);
				kfree (rom_desc);
				kfree (ti_manuf_desc);
				return status;
			}

			// Update I2C with type 0xf2 record with correct size and checksum
			status = TIWriteRom (serial,
						start_address,
						HEADER_SIZE,
						header);
			if (status) {
				kfree (vheader);
				kfree (header);
				kfree (rom_desc);
				kfree (ti_manuf_desc);
				return status;
			}

			// verify the write -- must do this in order for write to 
			// complete before we do the hardware reset
			status = TIReadRom (serial,
						start_address,
						HEADER_SIZE,
						vheader);

			if (status) {
				dbg ("%s - can't read header back", __FUNCTION__);
				kfree (vheader);
				kfree (header);
				kfree (rom_desc);
				kfree (ti_manuf_desc);
				return status;
			}
			if (memcmp(vheader, header, HEADER_SIZE)) {
				dbg ("%s - write download record failed", __FUNCTION__);
				kfree (vheader);
				kfree (header);
				kfree (rom_desc);
				kfree (ti_manuf_desc);
				return status;
			}

			kfree (vheader);
			kfree (header);

			dbg ("%s - Start firmware update", __FUNCTION__);

			// Tell firmware to copy download image into I2C 
			status = TISendVendorRequestSync (serial->serial->dev,
						UMPC_COPY_DNLD_TO_I2C,	// Request
						0,			// wValue 
						0,			// wIndex
						NULL,			// TransferBuffer
						0);			// TransferBufferLength

		  	dbg ("%s - Update complete 0x%x", __FUNCTION__, status);
			if (status) {
				dev_err (dev, "%s - UMPC_COPY_DNLD_TO_I2C failed\n", __FUNCTION__);
				kfree (rom_desc);
				kfree (ti_manuf_desc);
				return status;
			}
		}

		// The device is running the download code
		kfree (rom_desc);
		kfree (ti_manuf_desc);
		return 0;
	}

	/********************************************************************/
	/* Boot Mode */
	/********************************************************************/
	dbg ("%s - <<<<<<<<<<<<<<<RUNNING IN BOOT MODE>>>>>>>>>>>>>>>",
	     __FUNCTION__);

	// Configure the TI device so we can use the BULK pipes for download
	status = TIConfigureBootDevice (serial->serial->dev);
	if (status)
		return status;

	if (le16_to_cpu(serial->serial->dev->descriptor.idVendor) != USB_VENDOR_ID_ION) {
		dbg ("%s - VID = 0x%x", __FUNCTION__,
		     le16_to_cpu(serial->serial->dev->descriptor.idVendor));
		serial->TI_I2C_Type = DTK_ADDR_SPACE_I2C_TYPE_II;
		goto StayInBootMode;
	}

	// We have an ION device (I2c Must be programmed)
	// Determine I2C image type
	if (TIGetI2cTypeInBootMode(serial)) {
		goto StayInBootMode;
	}

	// Registry variable set?
	if (TIStayInBootMode) {
		dbg ("%s - TIStayInBootMode", __FUNCTION__);
		goto StayInBootMode;
	}

	// Check for ION Vendor ID and that the I2C is valid
	if (!TiValidateI2cImage(serial)) {
		struct ti_i2c_image_header *header;
		int i;
		__u8 cs = 0;
		__u8 *buffer;
		int buffer_size;

		/* Validate Hardware version number
		 * Read Manufacturing Descriptor from TI Based Edgeport
		 */
		ti_manuf_desc = kmalloc (sizeof (*ti_manuf_desc), GFP_KERNEL);
		if (!ti_manuf_desc) {
			dev_err (dev, "%s - out of memory.\n", __FUNCTION__);
			return -ENOMEM;
		}
		status = TIReadManufDescriptor (serial, (__u8 *)ti_manuf_desc);
		if (status) {
			kfree (ti_manuf_desc);
			goto StayInBootMode;
		}

		// Check for version 2
		if (!ignore_cpu_rev && TI_GET_CPU_REVISION(ti_manuf_desc->CpuRev_BoardRev) < 2) {
			dbg ("%s - Wrong CPU Rev %d (Must be 2)", __FUNCTION__,
			     TI_GET_CPU_REVISION(ti_manuf_desc->CpuRev_BoardRev));
			kfree (ti_manuf_desc);
			goto StayInBootMode;
		}

		kfree (ti_manuf_desc);

		// In order to update the I2C firmware we must change the type 2 record to type 0xF2.
		// This will force the UMP to come up in Boot Mode.  Then while in boot mode, the driver 
		// will download the latest firmware (padded to 15.5k) into the UMP ram. 
		// And finally when the device comes back up in download mode the driver will cause 
		// the new firmware to be copied from the UMP Ram to I2C and the firmware will update
		// the record type from 0xf2 to 0x02.
		
		/*
		 * Do we really have to copy the whole firmware image,
		 * or could we do this in place!
		 */

		// Allocate a 15.5k buffer + 3 byte header
		buffer_size = (((1024 * 16) - 512) + sizeof(struct ti_i2c_image_header));
		buffer = kmalloc (buffer_size, GFP_KERNEL);
		if (!buffer) {
			dev_err (dev, "%s - out of memory\n", __FUNCTION__);
			return -ENOMEM;
		}
		
		// Initialize the buffer to 0xff (pad the buffer)
		memset (buffer, 0xff, buffer_size);

		memcpy (buffer, &PagableOperationalCodeImage[0], PagableOperationalCodeSize);

		for(i = sizeof(struct ti_i2c_image_header); i < buffer_size; i++) {
			cs = (__u8)(cs + buffer[i]);
		}
		
		header = (struct ti_i2c_image_header *)buffer;
		
		// update length and checksum after padding
		header->Length 	 = cpu_to_le16((__u16)(buffer_size - sizeof(struct ti_i2c_image_header)));
		header->CheckSum = cs;

		// Download the operational code 
		dbg ("%s - Downloading operational code image (TI UMP)", __FUNCTION__);
		status = TIDownloadCodeImage (serial, buffer, buffer_size);

		kfree (buffer);

		if (status) {
	  		dbg ("%s - Error downloading operational code image", __FUNCTION__);
			return status;
		}

		// Device will reboot
		serial->product_info.TiMode = TI_MODE_TRANSITIONING;

  		dbg ("%s - Download successful -- Device rebooting...", __FUNCTION__);

		/* return an error on purpose */
		return -ENODEV;
	}

StayInBootMode:
	// Eprom is invalid or blank stay in boot mode
	dbg ("%s - <<<<<<<<<<<<<<<STAYING IN BOOT MODE>>>>>>>>>>>>", __FUNCTION__);
	serial->product_info.TiMode = TI_MODE_BOOT;

	return 0;
}


static int TISetDtr (struct edgeport_port *port)
{
	int port_number = port->port->number - port->port->serial->minor;

	dbg ("%s", __FUNCTION__);
	port->shadow_mcr |= MCR_DTR;

	return TIWriteCommandSync (port->port->serial->dev,
				UMPC_SET_CLR_DTR,
				(__u8)(UMPM_UART1_PORT + port_number),
				1,	/* set */
				NULL,
				0);
}

static int TIClearDtr (struct edgeport_port *port)
{
	int port_number = port->port->number - port->port->serial->minor;

	dbg ("%s", __FUNCTION__);
	port->shadow_mcr &= ~MCR_DTR;

	return TIWriteCommandSync (port->port->serial->dev,
				UMPC_SET_CLR_DTR,
				(__u8)(UMPM_UART1_PORT + port_number),
				0,	/* clear */
				NULL,
				0);
}

static int TISetRts (struct edgeport_port *port)
{
	int port_number = port->port->number - port->port->serial->minor;

	dbg ("%s", __FUNCTION__);
	port->shadow_mcr |= MCR_RTS;

	return TIWriteCommandSync (port->port->serial->dev,
				UMPC_SET_CLR_RTS,
				(__u8)(UMPM_UART1_PORT + port_number),
				1,	/* set */
				NULL,
				0);
}

static int TIClearRts (struct edgeport_port *port)
{
	int port_number = port->port->number - port->port->serial->minor;

	dbg ("%s", __FUNCTION__);
	port->shadow_mcr &= ~MCR_RTS;

	return TIWriteCommandSync (port->port->serial->dev,
				UMPC_SET_CLR_RTS,
				(__u8)(UMPM_UART1_PORT + port_number),
				0,	/* clear */
				NULL,
				0);
}

static int TISetLoopBack (struct edgeport_port *port)
{
	int port_number = port->port->number - port->port->serial->minor;

	dbg ("%s", __FUNCTION__);

	return TIWriteCommandSync (port->port->serial->dev,
				UMPC_SET_CLR_LOOPBACK,
				(__u8)(UMPM_UART1_PORT + port_number),
				1,	/* set */
				NULL,
				0);
}

static int TIClearLoopBack (struct edgeport_port *port)
{
	int port_number = port->port->number - port->port->serial->minor;

	dbg ("%s", __FUNCTION__);

	return TIWriteCommandSync (port->port->serial->dev,
				UMPC_SET_CLR_LOOPBACK,
				(__u8)(UMPM_UART1_PORT + port_number),
				0,	/* clear */
				NULL,
				0);
}

static int TISetBreak (struct edgeport_port *port)
{
	int port_number = port->port->number - port->port->serial->minor;

	dbg ("%s", __FUNCTION__);

	return TIWriteCommandSync (port->port->serial->dev,
				UMPC_SET_CLR_BREAK,
				(__u8)(UMPM_UART1_PORT + port_number),
				1,	/* set */
				NULL,
				0);
}

static int TIClearBreak (struct edgeport_port *port)
{
	int port_number = port->port->number - port->port->serial->minor;

	dbg ("%s", __FUNCTION__);

	return TIWriteCommandSync (port->port->serial->dev,
				UMPC_SET_CLR_BREAK,
				(__u8)(UMPM_UART1_PORT + port_number),
				0,	/* clear */
				NULL,
				0);
}

static int TIRestoreMCR (struct edgeport_port *port, __u8 mcr)
{
	int status = 0;

	dbg ("%s - %x", __FUNCTION__, mcr);

	if (mcr & MCR_DTR)
		status = TISetDtr (port);
	else
		status = TIClearDtr (port);

	if (status)
		return status;

	if (mcr & MCR_RTS)
		status = TISetRts (port);
	else
		status = TIClearRts (port);

	if (status)
		return status;

	if (mcr & MCR_LOOPBACK)
		status = TISetLoopBack (port);
	else
		status = TIClearLoopBack (port);

	return status;
}



/* Convert TI LSR to standard UART flags */
static __u8 MapLineStatus (__u8 ti_lsr)
{
	__u8 lsr = 0;

#define MAP_FLAG(flagUmp, flagUart)    \
	if (ti_lsr & flagUmp) \
		lsr |= flagUart;

	MAP_FLAG(UMP_UART_LSR_OV_MASK, LSR_OVER_ERR)	/* overrun */
	MAP_FLAG(UMP_UART_LSR_PE_MASK, LSR_PAR_ERR)	/* parity error */
	MAP_FLAG(UMP_UART_LSR_FE_MASK, LSR_FRM_ERR)	/* framing error */
	MAP_FLAG(UMP_UART_LSR_BR_MASK, LSR_BREAK)	/* break detected */
	MAP_FLAG(UMP_UART_LSR_RX_MASK, LSR_RX_AVAIL)	/* receive data available */
	MAP_FLAG(UMP_UART_LSR_TX_MASK, LSR_TX_EMPTY)	/* transmit holding register empty */

#undef MAP_FLAG

	return lsr;
}

static void handle_new_msr (struct edgeport_port *edge_port, __u8 msr)
{
	struct async_icount *icount;
	struct tty_struct *tty;

	dbg ("%s - %02x", __FUNCTION__, msr);

	if (msr & (EDGEPORT_MSR_DELTA_CTS | EDGEPORT_MSR_DELTA_DSR | EDGEPORT_MSR_DELTA_RI | EDGEPORT_MSR_DELTA_CD)) {
		icount = &edge_port->icount;

		/* update input line counters */
		if (msr & EDGEPORT_MSR_DELTA_CTS)
			icount->cts++;
		if (msr & EDGEPORT_MSR_DELTA_DSR)
			icount->dsr++;
		if (msr & EDGEPORT_MSR_DELTA_CD)
			icount->dcd++;
		if (msr & EDGEPORT_MSR_DELTA_RI)
			icount->rng++;
		wake_up_interruptible (&edge_port->delta_msr_wait);
	}

	/* Save the new modem status */
	edge_port->shadow_msr = msr & 0xf0;

	tty = edge_port->port->tty;
	/* handle CTS flow control */
	if (tty && C_CRTSCTS(tty)) {
		if (msr & EDGEPORT_MSR_CTS) {
			tty->hw_stopped = 0;
			tty_wakeup(tty);
		} else {
			tty->hw_stopped = 1;
		}
	}

	return;
}

static void handle_new_lsr (struct edgeport_port *edge_port, int lsr_data, __u8 lsr, __u8 data)
{
	struct async_icount *icount;
	__u8 new_lsr = (__u8)(lsr & (__u8)(LSR_OVER_ERR | LSR_PAR_ERR | LSR_FRM_ERR | LSR_BREAK));

	dbg ("%s - %02x", __FUNCTION__, new_lsr);

	edge_port->shadow_lsr = lsr;

	if (new_lsr & LSR_BREAK) {
		/*
		 * Parity and Framing errors only count if they
		 * occur exclusive of a break being received.
		 */
		new_lsr &= (__u8)(LSR_OVER_ERR | LSR_BREAK);
	}

	/* Place LSR data byte into Rx buffer */
	if (lsr_data && edge_port->port->tty)
		edge_tty_recv(&edge_port->port->dev, edge_port->port->tty, &data, 1);

	/* update input line counters */
	icount = &edge_port->icount;
	if (new_lsr & LSR_BREAK)
		icount->brk++;
	if (new_lsr & LSR_OVER_ERR)
		icount->overrun++;
	if (new_lsr & LSR_PAR_ERR)
		icount->parity++;
	if (new_lsr & LSR_FRM_ERR)
		icount->frame++;
}


static void edge_interrupt_callback (struct urb *urb, struct pt_regs *regs)
{
	struct edgeport_serial *edge_serial = (struct edgeport_serial *)urb->context;
	struct usb_serial_port *port;
	struct edgeport_port *edge_port;
	unsigned char *data = urb->transfer_buffer;
	int length = urb->actual_length;
	int port_number;
	int function;
	int status;
	__u8 lsr;
	__u8 msr;

	dbg("%s", __FUNCTION__);

	switch (urb->status) {
	case 0:
		/* success */
		break;
	case -ECONNRESET:
	case -ENOENT:
	case -ESHUTDOWN:
		/* this urb is terminated, clean up */
		dbg("%s - urb shutting down with status: %d", __FUNCTION__, urb->status);
		return;
	default:
		dev_err(&urb->dev->dev, "%s - nonzero urb status received: %d\n", __FUNCTION__, urb->status);
		goto exit;
	}

	if (!length) {
		dbg ("%s - no data in urb", __FUNCTION__);
		goto exit;
	}
		
	usb_serial_debug_data(debug, &edge_serial->serial->dev->dev, __FUNCTION__, length, data);
		
	if (length != 2) {
		dbg ("%s - expecting packet of size 2, got %d", __FUNCTION__, length);
		goto exit;
	}

	port_number = TIUMP_GET_PORT_FROM_CODE (data[0]);
	function    = TIUMP_GET_FUNC_FROM_CODE (data[0]);
	dbg ("%s - port_number %d, function %d, info 0x%x",
	     __FUNCTION__, port_number, function, data[1]);
	port = edge_serial->serial->port[port_number];
	edge_port = usb_get_serial_port_data(port);
	if (!edge_port) {
		dbg ("%s - edge_port not found", __FUNCTION__);
		return;
	}
	switch (function) {
	case TIUMP_INTERRUPT_CODE_LSR:
		lsr = MapLineStatus(data[1]);
		if (lsr & UMP_UART_LSR_DATA_MASK) {
			/* Save the LSR event for bulk read completion routine */
			dbg ("%s - LSR Event Port %u LSR Status = %02x",
			     __FUNCTION__, port_number, lsr);
			edge_port->lsr_event = 1;
			edge_port->lsr_mask = lsr;
		} else {
			dbg ("%s - ===== Port %d LSR Status = %02x ======",
			     __FUNCTION__, port_number, lsr);
			handle_new_lsr (edge_port, 0, lsr, 0);
		}
		break;

	case TIUMP_INTERRUPT_CODE_MSR:	// MSR
		/* Copy MSR from UMP */
		msr = data[1];
		dbg ("%s - ===== Port %u MSR Status = %02x ======\n",
		     __FUNCTION__, port_number, msr);
		handle_new_msr (edge_port, msr);
		break;

	default:
		dev_err (&urb->dev->dev, "%s - Unknown Interrupt code from UMP %x\n",
			 __FUNCTION__, data[1]);
		break;
		
	}

exit:
	status = usb_submit_urb (urb, GFP_ATOMIC);
	if (status)
		dev_err (&urb->dev->dev, "%s - usb_submit_urb failed with result %d\n",
			 __FUNCTION__, status);
}

static void edge_bulk_in_callback (struct urb *urb, struct pt_regs *regs)
{
	struct edgeport_port *edge_port = (struct edgeport_port *)urb->context;
	unsigned char *data = urb->transfer_buffer;
	struct tty_struct *tty;
	int status = 0;
	int port_number;

	dbg("%s", __FUNCTION__);

	switch (urb->status) {
	case 0:
		/* success */
		break;
	case -ECONNRESET:
	case -ENOENT:
	case -ESHUTDOWN:
		/* this urb is terminated, clean up */
		dbg("%s - urb shutting down with status: %d", __FUNCTION__, urb->status);
		return;
	default:
		dev_err (&urb->dev->dev,"%s - nonzero read bulk status received: %d\n",
		     __FUNCTION__, urb->status );
	}

	if (urb->status == -EPIPE)
		goto exit;

	if (urb->status) {
		dev_err(&urb->dev->dev,"%s - stopping read!\n", __FUNCTION__);
		return;
	}

	port_number = edge_port->port->number - edge_port->port->serial->minor;

	if (edge_port->lsr_event) {
		edge_port->lsr_event = 0;
		dbg ("%s ===== Port %u LSR Status = %02x, Data = %02x ======",
		     __FUNCTION__, port_number, edge_port->lsr_mask, *data);
		handle_new_lsr (edge_port, 1, edge_port->lsr_mask, *data);
		/* Adjust buffer length/pointer */
		--urb->actual_length;
		++data;
	}

	tty = edge_port->port->tty;
	if (tty && urb->actual_length) {
		usb_serial_debug_data(debug, &edge_port->port->dev, __FUNCTION__, urb->actual_length, data);

		if (edge_port->close_pending) {
			dbg ("%s - close is pending, dropping data on the floor.", __FUNCTION__);
		} else {
			edge_tty_recv(&edge_port->port->dev, tty, data, urb->actual_length);
		}
		edge_port->icount.rx += urb->actual_length;
	}

exit:
	/* continue read unless stopped */
	spin_lock(&edge_port->ep_lock);
	if (edge_port->ep_read_urb_state == EDGE_READ_URB_RUNNING) {
		urb->dev = edge_port->port->serial->dev;
		status = usb_submit_urb(urb, GFP_ATOMIC);
	} else if (edge_port->ep_read_urb_state == EDGE_READ_URB_STOPPING) {
		edge_port->ep_read_urb_state = EDGE_READ_URB_STOPPED;
	}
	spin_unlock(&edge_port->ep_lock);
	if (status)
		dev_err (&urb->dev->dev, "%s - usb_submit_urb failed with result %d\n",
			 __FUNCTION__, status);
}

static void edge_tty_recv(struct device *dev, struct tty_struct *tty, unsigned char *data, int length)
{
	int cnt;

	do {
		if (tty->flip.count >= TTY_FLIPBUF_SIZE) {
			tty_flip_buffer_push(tty);
			if (tty->flip.count >= TTY_FLIPBUF_SIZE) {
				dev_err(dev, "%s - dropping data, %d bytes lost\n",
					__FUNCTION__, length);
				return;
			}
		}
		cnt = min(length, TTY_FLIPBUF_SIZE - tty->flip.count);
		memcpy(tty->flip.char_buf_ptr, data, cnt);
		memset(tty->flip.flag_buf_ptr, 0, cnt);
		tty->flip.char_buf_ptr += cnt;
		tty->flip.flag_buf_ptr += cnt;
		tty->flip.count += cnt;
		data += cnt;
		length -= cnt;
	} while (length > 0);

	tty_flip_buffer_push(tty);
}

static void edge_bulk_out_callback (struct urb *urb, struct pt_regs *regs)
{
	struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
	struct edgeport_port *edge_port = usb_get_serial_port_data(port);

	dbg ("%s - port %d", __FUNCTION__, port->number);

	edge_port->ep_write_urb_in_use = 0;

	switch (urb->status) {
	case 0:
		/* success */
		break;
	case -ECONNRESET:
	case -ENOENT:
	case -ESHUTDOWN:
		/* this urb is terminated, clean up */
		dbg("%s - urb shutting down with status: %d", __FUNCTION__, urb->status);
		return;
	default:
		dev_err (&urb->dev->dev,"%s - nonzero write bulk status received: %d\n",
		     __FUNCTION__, urb->status);
	}

	/* send any buffered data */
	edge_send(port);
}

static int edge_open (struct usb_serial_port *port, struct file * filp)
{
	struct edgeport_port *edge_port = usb_get_serial_port_data(port);
	struct edgeport_serial *edge_serial;
	struct usb_device *dev;
	struct urb *urb;
	int port_number;
	int status;
	u16 open_settings;
	u8 transaction_timeout;

	dbg("%s - port %d", __FUNCTION__, port->number);

	if (edge_port == NULL)
		return -ENODEV;

	if (port->tty)
		port->tty->low_latency = low_latency;

	port_number = port->number - port->serial->minor;
	switch (port_number) {
		case 0:
			edge_port->uart_base = UMPMEM_BASE_UART1;
			edge_port->dma_address = UMPD_OEDB1_ADDRESS;
			break;
		case 1:
			edge_port->uart_base = UMPMEM_BASE_UART2;
			edge_port->dma_address = UMPD_OEDB2_ADDRESS;
			break;
		default:
			dev_err (&port->dev, "Unknown port number!!!\n");
			return -ENODEV;
	}

	dbg ("%s - port_number = %d, uart_base = %04x, dma_address = %04x",
	     __FUNCTION__, port_number, edge_port->uart_base, edge_port->dma_address);

	dev = port->serial->dev;

	memset (&(edge_port->icount), 0x00, sizeof(edge_port->icount));
	init_waitqueue_head (&edge_port->delta_msr_wait);

	/* turn off loopback */
	status = TIClearLoopBack (edge_port);
	if (status) {
		dev_err(&port->dev,"%s - cannot send clear loopback command, %d\n",
			__FUNCTION__, status);
		return status;
	}
	
	/* set up the port settings */
	edge_set_termios (port, NULL);

	/* open up the port */

	/* milliseconds to timeout for DMA transfer */
	transaction_timeout = 2;

	edge_port->ump_read_timeout = max (20, ((transaction_timeout * 3) / 2) );

	// milliseconds to timeout for DMA transfer
	open_settings = (u8)(UMP_DMA_MODE_CONTINOUS | 
			     UMP_PIPE_TRANS_TIMEOUT_ENA | 
			     (transaction_timeout << 2));

	dbg ("%s - Sending UMPC_OPEN_PORT", __FUNCTION__);

	/* Tell TI to open and start the port */
	status = TIWriteCommandSync (dev,
					UMPC_OPEN_PORT,
					(u8)(UMPM_UART1_PORT + port_number),
					open_settings,
					NULL,
					0);
	if (status) {
		dev_err(&port->dev,"%s - cannot send open command, %d\n", __FUNCTION__, status);
		return status;
	}

	/* Start the DMA? */
	status = TIWriteCommandSync (dev,
					UMPC_START_PORT,
					(u8)(UMPM_UART1_PORT + port_number),
					0,
					NULL,
					0);
	if (status) {
		dev_err(&port->dev,"%s - cannot send start DMA command, %d\n", __FUNCTION__, status);
		return status;
	}

	/* Clear TX and RX buffers in UMP */
	status = TIPurgeDataSync (port, UMP_PORT_DIR_OUT | UMP_PORT_DIR_IN);
	if (status) {
		dev_err(&port->dev,"%s - cannot send clear buffers command, %d\n", __FUNCTION__, status);
		return status;
	}

	/* Read Initial MSR */
	status = TIReadVendorRequestSync (dev,
					UMPC_READ_MSR,	// Request
					0,		// wValue
					(__u16)(UMPM_UART1_PORT + port_number),	// wIndex (Address)
					&edge_port->shadow_msr,			// TransferBuffer
					1);					// TransferBufferLength
	if (status) {
 		dev_err(&port->dev,"%s - cannot send read MSR command, %d\n", __FUNCTION__, status);
		return status;
	}

	dbg ("ShadowMSR 0x%X", edge_port->shadow_msr);
 
	/* Set Initial MCR */
	edge_port->shadow_mcr = MCR_RTS | MCR_DTR;
	dbg ("ShadowMCR 0x%X", edge_port->shadow_mcr);

	edge_serial = edge_port->edge_serial;
	if (down_interruptible(&edge_serial->es_sem))
		return -ERESTARTSYS;
	if (edge_serial->num_ports_open == 0) {
		/* we are the first port to be opened, let's post the interrupt urb */
		urb = edge_serial->serial->port[0]->interrupt_in_urb;
		if (!urb) {
			dev_err (&port->dev, "%s - no interrupt urb present, exiting\n", __FUNCTION__);
			status = -EINVAL;
			goto up_es_sem;
		}
		urb->complete = edge_interrupt_callback;
		urb->context = edge_serial;
		urb->dev = dev;
		status = usb_submit_urb (urb, GFP_KERNEL);
		if (status) {
			dev_err (&port->dev, "%s - usb_submit_urb failed with value %d\n", __FUNCTION__, status);
			goto up_es_sem;
		}
	}

	/*
	 * reset the data toggle on the bulk endpoints to work around bug in
	 * host controllers where things get out of sync some times
	 */
	usb_clear_halt (dev, port->write_urb->pipe);
	usb_clear_halt (dev, port->read_urb->pipe);

	/* start up our bulk read urb */
	urb = port->read_urb;
	if (!urb) {
		dev_err (&port->dev, "%s - no read urb present, exiting\n", __FUNCTION__);
		status = -EINVAL;
		goto unlink_int_urb;
	}
	edge_port->ep_read_urb_state = EDGE_READ_URB_RUNNING;
	urb->complete = edge_bulk_in_callback;
	urb->context = edge_port;
	urb->dev = dev;
	status = usb_submit_urb (urb, GFP_KERNEL);
	if (status) {
		dev_err (&port->dev, "%s - read bulk usb_submit_urb failed with value %d\n", __FUNCTION__, status);
		goto unlink_int_urb;
	}

	++edge_serial->num_ports_open;

	dbg("%s - exited", __FUNCTION__);

	goto up_es_sem;

unlink_int_urb:
	if (edge_port->edge_serial->num_ports_open == 0)
		usb_kill_urb(port->serial->port[0]->interrupt_in_urb);
up_es_sem:
	up(&edge_serial->es_sem);
	return status;
}

static void edge_close (struct usb_serial_port *port, struct file *filp)
{
	struct edgeport_serial *edge_serial;
	struct edgeport_port *edge_port;
	int port_number;
	int status;

	dbg("%s - port %d", __FUNCTION__, port->number);
			 
	edge_serial = usb_get_serial_data(port->serial);
	edge_port = usb_get_serial_port_data(port);
	if ((edge_serial == NULL) || (edge_port == NULL))
		return;
	
	/* The bulkreadcompletion routine will check 
	 * this flag and dump add read data */
	edge_port->close_pending = 1;

	/* chase the port close and flush */
	TIChasePort (edge_port, (HZ*closing_wait)/100, 1);

	usb_kill_urb(port->read_urb);
	usb_kill_urb(port->write_urb);
	edge_port->ep_write_urb_in_use = 0;

	/* assuming we can still talk to the device,
	 * send a close port command to it */
	dbg("%s - send umpc_close_port", __FUNCTION__);
	port_number = port->number - port->serial->minor;
	status = TIWriteCommandSync (port->serial->dev,
				     UMPC_CLOSE_PORT,
				     (__u8)(UMPM_UART1_PORT + port_number),
				     0,
				     NULL,
				     0);
	down(&edge_serial->es_sem);
	--edge_port->edge_serial->num_ports_open;
	if (edge_port->edge_serial->num_ports_open <= 0) {
		/* last port is now closed, let's shut down our interrupt urb */
		usb_kill_urb(port->serial->port[0]->interrupt_in_urb);
		edge_port->edge_serial->num_ports_open = 0;
	}
	up(&edge_serial->es_sem);
	edge_port->close_pending = 0;

	dbg("%s - exited", __FUNCTION__);
}

static int edge_write (struct usb_serial_port *port, const unsigned char *data, int count)
{
	struct edgeport_port *edge_port = usb_get_serial_port_data(port);
	unsigned long flags;

	dbg("%s - port %d", __FUNCTION__, port->number);

	if (count == 0) {
		dbg("%s - write request of 0 bytes", __FUNCTION__);
		return 0;
	}

	if (edge_port == NULL)
		return -ENODEV;
	if (edge_port->close_pending == 1)
		return -ENODEV;

	spin_lock_irqsave(&edge_port->ep_lock, flags);
	count = edge_buf_put(edge_port->ep_out_buf, data, count);
	spin_unlock_irqrestore(&edge_port->ep_lock, flags);

	edge_send(port);

	return count;
}

static void edge_send(struct usb_serial_port *port)
{
	int count, result;
	struct edgeport_port *edge_port = usb_get_serial_port_data(port);
	struct tty_struct *tty = port->tty;
	unsigned long flags;


	dbg("%s - port %d", __FUNCTION__, port->number);

	spin_lock_irqsave(&edge_port->ep_lock, flags);

	if (edge_port->ep_write_urb_in_use) {
		spin_unlock_irqrestore(&edge_port->ep_lock, flags);
		return;
	}

	count = edge_buf_get(edge_port->ep_out_buf,
				port->write_urb->transfer_buffer,
				port->bulk_out_size);

	if (count == 0) {
		spin_unlock_irqrestore(&edge_port->ep_lock, flags);
		return;
	}

	edge_port->ep_write_urb_in_use = 1;

	spin_unlock_irqrestore(&edge_port->ep_lock, flags);

	usb_serial_debug_data(debug, &port->dev, __FUNCTION__, count, port->write_urb->transfer_buffer);

	/* set up our urb */
	usb_fill_bulk_urb (port->write_urb, port->serial->dev,
			   usb_sndbulkpipe (port->serial->dev,
					    port->bulk_out_endpointAddress),
			   port->write_urb->transfer_buffer, count,
			   edge_bulk_out_callback,
			   port);

	/* send the data out the bulk port */
	result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
	if (result) {
		dev_err(&port->dev, "%s - failed submitting write urb, error %d\n", __FUNCTION__, result);
		edge_port->ep_write_urb_in_use = 0;
		// TODO: reschedule edge_send
	} else {
		edge_port->icount.tx += count;
	}

	/* wakeup any process waiting for writes to complete */
	/* there is now more room in the buffer for new writes */
	if (tty) {
		/* let the tty driver wakeup if it has a special write_wakeup function */
		tty_wakeup(tty);
	}
}

static int edge_write_room (struct usb_serial_port *port)
{
	struct edgeport_port *edge_port = usb_get_serial_port_data(port);
	int room = 0;
	unsigned long flags;

	dbg("%s - port %d", __FUNCTION__, port->number);

	if (edge_port == NULL)
		return -ENODEV;
	if (edge_port->close_pending == 1)
		return -ENODEV;

	spin_lock_irqsave(&edge_port->ep_lock, flags);
	room = edge_buf_space_avail(edge_port->ep_out_buf);
	spin_unlock_irqrestore(&edge_port->ep_lock, flags);

	dbg("%s - returns %d", __FUNCTION__, room);
	return room;
}

static int edge_chars_in_buffer (struct usb_serial_port *port)
{
	struct edgeport_port *edge_port = usb_get_serial_port_data(port);
	int chars = 0;
	unsigned long flags;

	dbg("%s - port %d", __FUNCTION__, port->number);

	if (edge_port == NULL)
		return -ENODEV;
	if (edge_port->close_pending == 1)
		return -ENODEV;

	spin_lock_irqsave(&edge_port->ep_lock, flags);
	chars = edge_buf_data_avail(edge_port->ep_out_buf);
	spin_unlock_irqrestore(&edge_port->ep_lock, flags);

	dbg ("%s - returns %d", __FUNCTION__, chars);
	return chars;
}

static void edge_throttle (struct usb_serial_port *port)
{
	struct edgeport_port *edge_port = usb_get_serial_port_data(port);
	struct tty_struct *tty;
	int status;

	dbg("%s - port %d", __FUNCTION__, port->number);

	if (edge_port == NULL)
		return;

	tty = port->tty;
	if (!tty) {
		dbg ("%s - no tty available", __FUNCTION__);
		return;
	}

	/* if we are implementing XON/XOFF, send the stop character */
	if (I_IXOFF(tty)) {
		unsigned char stop_char = STOP_CHAR(tty);
		status = edge_write (port, &stop_char, 1);
		if (status <= 0) {
			dev_err(&port->dev, "%s - failed to write stop character, %d\n", __FUNCTION__, status);
		}
	}

	/* if we are implementing RTS/CTS, stop reads */
	/* and the Edgeport will clear the RTS line */
	if (C_CRTSCTS(tty))
		stop_read(edge_port);

}

static void edge_unthrottle (struct usb_serial_port *port)
{
	struct edgeport_port *edge_port = usb_get_serial_port_data(port);
	struct tty_struct *tty;
	int status;

	dbg("%s - port %d", __FUNCTION__, port->number);

	if (edge_port == NULL)
		return;

	tty = port->tty;
	if (!tty) {
		dbg ("%s - no tty available", __FUNCTION__);
		return;
	}

	/* if we are implementing XON/XOFF, send the start character */
	if (I_IXOFF(tty)) {
		unsigned char start_char = START_CHAR(tty);
		status = edge_write (port, &start_char, 1);
		if (status <= 0) {
			dev_err(&port->dev, "%s - failed to write start character, %d\n", __FUNCTION__, status);
		}
	}

	/* if we are implementing RTS/CTS, restart reads */
	/* are the Edgeport will assert the RTS line */
	if (C_CRTSCTS(tty)) {
		status = restart_read(edge_port);
		if (status)
			dev_err(&port->dev, "%s - read bulk usb_submit_urb failed with value %d\n", __FUNCTION__, status);
	}

}

static void stop_read(struct edgeport_port *edge_port)
{
	unsigned long flags;

	spin_lock_irqsave(&edge_port->ep_lock, flags);

	if (edge_port->ep_read_urb_state == EDGE_READ_URB_RUNNING)
		edge_port->ep_read_urb_state = EDGE_READ_URB_STOPPING;
	edge_port->shadow_mcr &= ~MCR_RTS;

	spin_unlock_irqrestore(&edge_port->ep_lock, flags);
}

static int restart_read(struct edgeport_port *edge_port)
{
	struct urb *urb;
	int status = 0;
	unsigned long flags;

	spin_lock_irqsave(&edge_port->ep_lock, flags);

	if (edge_port->ep_read_urb_state == EDGE_READ_URB_STOPPED) {
		urb = edge_port->port->read_urb;
		urb->complete = edge_bulk_in_callback;
		urb->context = edge_port;
		urb->dev = edge_port->port->serial->dev;
		status = usb_submit_urb(urb, GFP_KERNEL);
	}
	edge_port->ep_read_urb_state = EDGE_READ_URB_RUNNING;
	edge_port->shadow_mcr |= MCR_RTS;

	spin_unlock_irqrestore(&edge_port->ep_lock, flags);

	return status;
}

static void change_port_settings (struct edgeport_port *edge_port, struct termios *old_termios)
{
	struct ump_uart_config *config;
	struct tty_struct *tty;
	int baud;
	unsigned cflag;
	int status;
	int port_number = edge_port->port->number - edge_port->port->serial->minor;

	dbg("%s - port %d", __FUNCTION__, edge_port->port->number);

	tty = edge_port->port->tty;
	if ((!tty) ||
	    (!tty->termios)) {
		dbg("%s - no tty structures", __FUNCTION__);
		return;
	}

	config = kmalloc (sizeof (*config), GFP_KERNEL);
	if (!config) {
		dev_err (&edge_port->port->dev, "%s - out of memory\n", __FUNCTION__);
		return;
	}

	cflag = tty->termios->c_cflag;

	config->wFlags = 0;

	/* These flags must be set */
	config->wFlags |= UMP_MASK_UART_FLAGS_RECEIVE_MS_INT;
	config->wFlags |= UMP_MASK_UART_FLAGS_AUTO_START_ON_ERR;
	config->bUartMode = (__u8)(edge_port->bUartMode);

	switch (cflag & CSIZE) {
		case CS5:
			    config->bDataBits = UMP_UART_CHAR5BITS;
			    dbg ("%s - data bits = 5", __FUNCTION__);
			    break;
		case CS6:
			    config->bDataBits = UMP_UART_CHAR6BITS;
			    dbg ("%s - data bits = 6", __FUNCTION__);
			    break;
		case CS7:
			    config->bDataBits = UMP_UART_CHAR7BITS;
			    dbg ("%s - data bits = 7", __FUNCTION__);
			    break;
		default:
		case CS8:
			    config->bDataBits = UMP_UART_CHAR8BITS;
			    dbg ("%s - data bits = 8", __FUNCTION__);
			    break;
	}

	if (cflag & PARENB) {
		if (cflag & PARODD) {
			config->wFlags |= UMP_MASK_UART_FLAGS_PARITY;
			config->bParity = UMP_UART_ODDPARITY;
			dbg("%s - parity = odd", __FUNCTION__);
		} else {
			config->wFlags |= UMP_MASK_UART_FLAGS_PARITY;
			config->bParity = UMP_UART_EVENPARITY;
			dbg("%s - parity = even", __FUNCTION__);
		}
	} else {
		config->bParity = UMP_UART_NOPARITY; 	
		dbg("%s - parity = none", __FUNCTION__);
	}

	if (cflag & CSTOPB) {
		config->bStopBits = UMP_UART_STOPBIT2;
		dbg("%s - stop bits = 2", __FUNCTION__);
	} else {
		config->bStopBits = UMP_UART_STOPBIT1;
		dbg("%s - stop bits = 1", __FUNCTION__);
	}

	/* figure out the flow control settings */
	if (cflag & CRTSCTS) {
		config->wFlags |= UMP_MASK_UART_FLAGS_OUT_X_CTS_FLOW;
		config->wFlags |= UMP_MASK_UART_FLAGS_RTS_FLOW;
		dbg("%s - RTS/CTS is enabled", __FUNCTION__);
	} else {
		dbg("%s - RTS/CTS is disabled", __FUNCTION__);
		tty->hw_stopped = 0;
		restart_read(edge_port);
	}

	/* if we are implementing XON/XOFF, set the start and stop character in the device */
	if (I_IXOFF(tty) || I_IXON(tty)) {
		config->cXon  = START_CHAR(tty);
		config->cXoff = STOP_CHAR(tty);

		/* if we are implementing INBOUND XON/XOFF */
		if (I_IXOFF(tty)) {
			config->wFlags |= UMP_MASK_UART_FLAGS_IN_X;
			dbg ("%s - INBOUND XON/XOFF is enabled, XON = %2x, XOFF = %2x",
			     __FUNCTION__, config->cXon, config->cXoff);
		} else {
			dbg ("%s - INBOUND XON/XOFF is disabled", __FUNCTION__);
		}

		/* if we are implementing OUTBOUND XON/XOFF */
		if (I_IXON(tty)) {
			config->wFlags |= UMP_MASK_UART_FLAGS_OUT_X;
			dbg ("%s - OUTBOUND XON/XOFF is enabled, XON = %2x, XOFF = %2x",
			     __FUNCTION__, config->cXon, config->cXoff);
		} else {
			dbg ("%s - OUTBOUND XON/XOFF is disabled", __FUNCTION__);
		}
	}

	/* Round the baud rate */
	baud = tty_get_baud_rate(tty);
	if (!baud) {
		/* pick a default, any default... */
		baud = 9600;
	}
	edge_port->baud_rate = baud;
	config->wBaudRate = (__u16)((461550L + baud/2) / baud);

	dbg ("%s - baud rate = %d, wBaudRate = %d", __FUNCTION__, baud, config->wBaudRate);

	dbg ("wBaudRate:   %d", (int)(461550L / config->wBaudRate));
	dbg ("wFlags:    0x%x", config->wFlags);
	dbg ("bDataBits:   %d", config->bDataBits);
	dbg ("bParity:     %d", config->bParity);
	dbg ("bStopBits:   %d", config->bStopBits);
	dbg ("cXon:        %d", config->cXon);
	dbg ("cXoff:       %d", config->cXoff);
	dbg ("bUartMode:   %d", config->bUartMode);

	/* move the word values into big endian mode */
	cpu_to_be16s (&config->wFlags);
	cpu_to_be16s (&config->wBaudRate);

	status = TIWriteCommandSync (edge_port->port->serial->dev,
				UMPC_SET_CONFIG,
				(__u8)(UMPM_UART1_PORT + port_number),
				0,
				(__u8 *)config,
				sizeof(*config));
	if (status) {
		dbg ("%s - error %d when trying to write config to device",
		     __FUNCTION__, status);
	}

	kfree (config);
	
	return;
}

static void edge_set_termios (struct usb_serial_port *port, struct termios *old_termios)
{
	struct edgeport_port *edge_port = usb_get_serial_port_data(port);
	struct tty_struct *tty = port->tty;
	unsigned int cflag;

	if (!port->tty || !port->tty->termios) {
		dbg ("%s - no tty or termios", __FUNCTION__);
		return;
	}

	cflag = tty->termios->c_cflag;
	/* check that they really want us to change something */
	if (old_termios) {
		if (cflag == old_termios->c_cflag &&
		    tty->termios->c_iflag == old_termios->c_iflag) {
			dbg ("%s - nothing to change", __FUNCTION__);
			return;
		}
	}

	dbg("%s - clfag %08x iflag %08x", __FUNCTION__, 
	    tty->termios->c_cflag, tty->termios->c_iflag);
	if (old_termios) {
		dbg("%s - old clfag %08x old iflag %08x", __FUNCTION__,
		    old_termios->c_cflag, old_termios->c_iflag);
	}

	dbg("%s - port %d", __FUNCTION__, port->number);

	if (edge_port == NULL)
		return;

	/* change the port settings to the new ones specified */
	change_port_settings (edge_port, old_termios);

	return;
}

static int edge_tiocmset (struct usb_serial_port *port, struct file *file, unsigned int set, unsigned int clear)
{
	struct edgeport_port *edge_port = usb_get_serial_port_data(port);
	unsigned int mcr;

	dbg("%s - port %d", __FUNCTION__, port->number);

	mcr = edge_port->shadow_mcr;
	if (set & TIOCM_RTS)
		mcr |= MCR_RTS;
	if (set & TIOCM_DTR)
		mcr |= MCR_DTR;
	if (set & TIOCM_LOOP)
		mcr |= MCR_LOOPBACK;

	if (clear & TIOCM_RTS)
		mcr &= ~MCR_RTS;
	if (clear & TIOCM_DTR)
		mcr &= ~MCR_DTR;
	if (clear & TIOCM_LOOP)
		mcr &= ~MCR_LOOPBACK;

	edge_port->shadow_mcr = mcr;

	TIRestoreMCR (edge_port, mcr);

	return 0;
}

static int edge_tiocmget(struct usb_serial_port *port, struct file *file)
{
	struct edgeport_port *edge_port = usb_get_serial_port_data(port);
	unsigned int result = 0;
	unsigned int msr;
	unsigned int mcr;

	dbg("%s - port %d", __FUNCTION__, port->number);

	msr = edge_port->shadow_msr;
	mcr = edge_port->shadow_mcr;
	result = ((mcr & MCR_DTR)	? TIOCM_DTR: 0)	  /* 0x002 */
		  | ((mcr & MCR_RTS)	? TIOCM_RTS: 0)   /* 0x004 */
		  | ((msr & EDGEPORT_MSR_CTS)	? TIOCM_CTS: 0)   /* 0x020 */
		  | ((msr & EDGEPORT_MSR_CD)	? TIOCM_CAR: 0)   /* 0x040 */
		  | ((msr & EDGEPORT_MSR_RI)	? TIOCM_RI:  0)   /* 0x080 */
		  | ((msr & EDGEPORT_MSR_DSR)	? TIOCM_DSR: 0);  /* 0x100 */


	dbg("%s -- %x", __FUNCTION__, result);

	return result;
}

static int get_serial_info (struct edgeport_port *edge_port, struct serial_struct __user *retinfo)
{
	struct serial_struct tmp;

	if (!retinfo)
		return -EFAULT;

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

	tmp.type		= PORT_16550A;
	tmp.line		= edge_port->port->serial->minor;
	tmp.port		= edge_port->port->number;
	tmp.irq			= 0;
	tmp.flags		= ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
	tmp.xmit_fifo_size	= edge_port->port->bulk_out_size;
	tmp.baud_base		= 9600;
	tmp.close_delay		= 5*HZ;
	tmp.closing_wait	= closing_wait;
//	tmp.custom_divisor	= state->custom_divisor;
//	tmp.hub6		= state->hub6;
//	tmp.io_type		= state->io_type;


	if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
		return -EFAULT;
	return 0;
}

static int edge_ioctl (struct usb_serial_port *port, struct file *file, unsigned int cmd, unsigned long arg)
{
	struct edgeport_port *edge_port = usb_get_serial_port_data(port);
	struct async_icount cnow;
	struct async_icount cprev;

	dbg("%s - port %d, cmd = 0x%x", __FUNCTION__, port->number, cmd);

	switch (cmd) {
		case TIOCINQ:
			dbg("%s - (%d) TIOCINQ", __FUNCTION__, port->number);
//			return get_number_bytes_avail(edge_port, (unsigned int *) arg);
			break;

		case TIOCSERGETLSR:
			dbg("%s - (%d) TIOCSERGETLSR", __FUNCTION__, port->number);
//			return get_lsr_info(edge_port, (unsigned int *) arg);
			break;

		case TIOCGSERIAL:
			dbg("%s - (%d) TIOCGSERIAL", __FUNCTION__, port->number);
			return get_serial_info(edge_port, (struct serial_struct __user *) arg);
			break;

		case TIOCSSERIAL:
			dbg("%s - (%d) TIOCSSERIAL", __FUNCTION__, port->number);
			break;

		case TIOCMIWAIT:
			dbg("%s - (%d) TIOCMIWAIT", __FUNCTION__, port->number);
			cprev = edge_port->icount;
			while (1) {
				interruptible_sleep_on(&edge_port->delta_msr_wait);
				/* see if a signal did it */
				if (signal_pending(current))
					return -ERESTARTSYS;
				cnow = edge_port->icount;
				if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
				    cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
					return -EIO; /* no change => error */
				if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
				    ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
				    ((arg & TIOCM_CD)  && (cnow.dcd != cprev.dcd)) ||
				    ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts)) ) {
					return 0;
				}
				cprev = cnow;
			}
			/* not reached */
			break;

		case TIOCGICOUNT:
			dbg ("%s - (%d) TIOCGICOUNT RX=%d, TX=%d", __FUNCTION__,
			     port->number, edge_port->icount.rx, edge_port->icount.tx);
			if (copy_to_user((void __user *)arg, &edge_port->icount, sizeof(edge_port->icount)))
				return -EFAULT;
			return 0;
	}

	return -ENOIOCTLCMD;
}

static void edge_break (struct usb_serial_port *port, int break_state)
{
	struct edgeport_port *edge_port = usb_get_serial_port_data(port);
	int status;

	dbg ("%s - state = %d", __FUNCTION__, break_state);

	/* chase the port close */
	TIChasePort (edge_port, 0, 0);

	if (break_state == -1) {
		status = TISetBreak (edge_port);
	} else {
		status = TIClearBreak (edge_port);
	}
	if (status) {
		dbg ("%s - error %d sending break set/clear command.",
		     __FUNCTION__, status);
	}
}

static int edge_startup (struct usb_serial *serial)
{
	struct edgeport_serial *edge_serial;
	struct edgeport_port *edge_port;
	struct usb_device *dev;
	int status;
	int i;

	dev = serial->dev;

	/* create our private serial structure */
	edge_serial = kmalloc (sizeof(struct edgeport_serial), GFP_KERNEL);
	if (edge_serial == NULL) {
		dev_err(&serial->dev->dev, "%s - Out of memory\n", __FUNCTION__);
		return -ENOMEM;
	}
	memset (edge_serial, 0, sizeof(struct edgeport_serial));
	sema_init(&edge_serial->es_sem, 1);
	edge_serial->serial = serial;
	usb_set_serial_data(serial, edge_serial);

	status = TIDownloadFirmware (edge_serial);
	if (status) {
		kfree (edge_serial);
		return status;
	}

	/* set up our port private structures */
	for (i = 0; i < serial->num_ports; ++i) {
		edge_port = kmalloc (sizeof(struct edgeport_port), GFP_KERNEL);
		if (edge_port == NULL) {
			dev_err(&serial->dev->dev, "%s - Out of memory\n", __FUNCTION__);
			goto cleanup;
		}
		memset (edge_port, 0, sizeof(struct edgeport_port));
		spin_lock_init(&edge_port->ep_lock);
		edge_port->ep_out_buf = edge_buf_alloc(EDGE_OUT_BUF_SIZE);
		if (edge_port->ep_out_buf == NULL) {
			dev_err(&serial->dev->dev, "%s - Out of memory\n", __FUNCTION__);
			kfree(edge_port);
			goto cleanup;
		}
		edge_port->port = serial->port[i];
		edge_port->edge_serial = edge_serial;
		usb_set_serial_port_data(serial->port[i], edge_port);
		edge_port->bUartMode = 0;	/* Default is RS232 */
	}
	
	return 0;

cleanup:
	for (--i; i>=0; --i) {
		edge_port = usb_get_serial_port_data(serial->port[i]);
		edge_buf_free(edge_port->ep_out_buf);
		kfree(edge_port);
		usb_set_serial_port_data(serial->port[i], NULL);
	}
	kfree (edge_serial);
	usb_set_serial_data(serial, NULL);
	return -ENOMEM;
}

static void edge_shutdown (struct usb_serial *serial)
{
	int i;
	struct edgeport_port *edge_port;

	dbg ("%s", __FUNCTION__);

	for (i=0; i < serial->num_ports; ++i) {
		edge_port = usb_get_serial_port_data(serial->port[i]);
		if (edge_port) {
			edge_buf_free(edge_port->ep_out_buf);
			kfree(edge_port);
		}
		usb_set_serial_port_data(serial->port[i], NULL);
	}
	kfree (usb_get_serial_data(serial));
	usb_set_serial_data(serial, NULL);
}


/* Circular Buffer */

/*
 * edge_buf_alloc
 *
 * Allocate a circular buffer and all associated memory.
 */

static struct edge_buf *edge_buf_alloc(unsigned int size)
{
	struct edge_buf *eb;


	if (size == 0)
		return NULL;

	eb = (struct edge_buf *)kmalloc(sizeof(struct edge_buf), GFP_KERNEL);
	if (eb == NULL)
		return NULL;

	eb->buf_buf = kmalloc(size, GFP_KERNEL);
	if (eb->buf_buf == NULL) {
		kfree(eb);
		return NULL;
	}

	eb->buf_size = size;
	eb->buf_get = eb->buf_put = eb->buf_buf;

	return eb;
}


/*
 * edge_buf_free
 *
 * Free the buffer and all associated memory.
 */

void edge_buf_free(struct edge_buf *eb)
{
	if (eb != NULL) {
		if (eb->buf_buf != NULL)
			kfree(eb->buf_buf);
		kfree(eb);
	}
}


/*
 * edge_buf_clear
 *
 * Clear out all data in the circular buffer.
 */

static void edge_buf_clear(struct edge_buf *eb)
{
        if (eb != NULL)
                eb->buf_get = eb->buf_put;
                /* equivalent to a get of all data available */
}


/*
 * edge_buf_data_avail
 *
 * Return the number of bytes of data available in the circular
 * buffer.
 */

static unsigned int edge_buf_data_avail(struct edge_buf *eb)
{
	if (eb != NULL)
		return ((eb->buf_size + eb->buf_put - eb->buf_get) % eb->buf_size);
	else
		return 0;
}


/*
 * edge_buf_space_avail
 *
 * Return the number of bytes of space available in the circular
 * buffer.
 */

static unsigned int edge_buf_space_avail(struct edge_buf *eb)
{
	if (eb != NULL)
		return ((eb->buf_size + eb->buf_get - eb->buf_put - 1) % eb->buf_size);
	else
		return 0;
}


/*
 * edge_buf_put
 *
 * Copy data data from a user buffer and put it into the circular buffer.
 * Restrict to the amount of space available.
 *
 * Return the number of bytes copied.
 */

static unsigned int edge_buf_put(struct edge_buf *eb, const char *buf,
	unsigned int count)
{
	unsigned int len;


	if (eb == NULL)
		return 0;

	len  = edge_buf_space_avail(eb);
	if (count > len)
		count = len;

	if (count == 0)
		return 0;

	len = eb->buf_buf + eb->buf_size - eb->buf_put;
	if (count > len) {
		memcpy(eb->buf_put, buf, len);
		memcpy(eb->buf_buf, buf+len, count - len);
		eb->buf_put = eb->buf_buf + count - len;
	} else {
		memcpy(eb->buf_put, buf, count);
		if (count < len)
			eb->buf_put += count;
		else /* count == len */
			eb->buf_put = eb->buf_buf;
	}

	return count;
}


/*
 * edge_buf_get
 *
 * Get data from the circular buffer and copy to the given buffer.
 * Restrict to the amount of data available.
 *
 * Return the number of bytes copied.
 */

static unsigned int edge_buf_get(struct edge_buf *eb, char *buf,
	unsigned int count)
{
	unsigned int len;


	if (eb == NULL)
		return 0;

	len = edge_buf_data_avail(eb);
	if (count > len)
		count = len;

	if (count == 0)
		return 0;

	len = eb->buf_buf + eb->buf_size - eb->buf_get;
	if (count > len) {
		memcpy(buf, eb->buf_get, len);
		memcpy(buf+len, eb->buf_buf, count - len);
		eb->buf_get = eb->buf_buf + count - len;
	} else {
		memcpy(buf, eb->buf_get, count);
		if (count < len)
			eb->buf_get += count;
		else /* count == len */
			eb->buf_get = eb->buf_buf;
	}

	return count;
}


static struct usb_serial_device_type edgeport_1port_device = {
	.owner			= THIS_MODULE,
	.name			= "Edgeport TI 1 port adapter",
	.short_name		= "edgeport_ti_1",
	.id_table		= edgeport_1port_id_table,
	.num_interrupt_in	= 1,
	.num_bulk_in		= 1,
	.num_bulk_out		= 1,
	.num_ports		= 1,
	.open			= edge_open,
	.close			= edge_close,
	.throttle		= edge_throttle,
	.unthrottle		= edge_unthrottle,
	.attach			= edge_startup,
	.shutdown		= edge_shutdown,
	.ioctl			= edge_ioctl,
	.set_termios		= edge_set_termios,
	.tiocmget		= edge_tiocmget,
	.tiocmset		= edge_tiocmset,
	.write			= edge_write,
	.write_room		= edge_write_room,
	.chars_in_buffer	= edge_chars_in_buffer,
	.break_ctl		= edge_break,
	.read_int_callback	= edge_interrupt_callback,
	.read_bulk_callback	= edge_bulk_in_callback,
	.write_bulk_callback	= edge_bulk_out_callback,
};

static struct usb_serial_device_type edgeport_2port_device = {
	.owner			= THIS_MODULE,
	.name			= "Edgeport TI 2 port adapter",
	.short_name		= "edgeport_ti_2",
	.id_table		= edgeport_2port_id_table,
	.num_interrupt_in	= 1,
	.num_bulk_in		= 2,
	.num_bulk_out		= 2,
	.num_ports		= 2,
	.open			= edge_open,
	.close			= edge_close,
	.throttle		= edge_throttle,
	.unthrottle		= edge_unthrottle,
	.attach			= edge_startup,
	.shutdown		= edge_shutdown,
	.ioctl			= edge_ioctl,
	.set_termios		= edge_set_termios,
	.tiocmget		= edge_tiocmget,
	.tiocmset		= edge_tiocmset,
	.write			= edge_write,
	.write_room		= edge_write_room,
	.chars_in_buffer	= edge_chars_in_buffer,
	.break_ctl		= edge_break,
	.read_int_callback	= edge_interrupt_callback,
	.read_bulk_callback	= edge_bulk_in_callback,
	.write_bulk_callback	= edge_bulk_out_callback,
};


static int __init edgeport_init(void)
{
	int retval;
	retval = usb_serial_register(&edgeport_1port_device);
	if (retval)
		goto failed_1port_device_register;
	retval = usb_serial_register(&edgeport_2port_device);
	if (retval)
		goto failed_2port_device_register;
	retval = usb_register(&io_driver);
	if (retval) 
		goto failed_usb_register;
	info(DRIVER_DESC " " DRIVER_VERSION);
	return 0;
failed_usb_register:
	usb_serial_deregister(&edgeport_2port_device);
failed_2port_device_register:
	usb_serial_deregister(&edgeport_1port_device);
failed_1port_device_register:
	return retval;
}

static void __exit edgeport_exit (void)
{
	usb_deregister (&io_driver);
	usb_serial_deregister (&edgeport_1port_device);
	usb_serial_deregister (&edgeport_2port_device);
}

module_init(edgeport_init);
module_exit(edgeport_exit);

/* Module information */
MODULE_AUTHOR(DRIVER_AUTHOR);
MODULE_DESCRIPTION(DRIVER_DESC);
MODULE_LICENSE("GPL");

module_param(debug, bool, S_IRUGO | S_IWUSR);
MODULE_PARM_DESC(debug, "Debug enabled or not");

module_param(low_latency, bool, S_IRUGO | S_IWUSR);
MODULE_PARM_DESC(low_latency, "Low latency enabled or not");

module_param(closing_wait, int, S_IRUGO | S_IWUSR);
MODULE_PARM_DESC(closing_wait, "Maximum wait for data to drain, in .01 secs");

module_param(ignore_cpu_rev, bool, S_IRUGO | S_IWUSR);
MODULE_PARM_DESC(ignore_cpu_rev, "Ignore the cpu revision when connecting to a device");