summaryrefslogblamecommitdiffstats
path: root/private/mvdm/vdmredir/vrdlc5c.c
blob: d45fec96c3d0f858ac70bc63c4ebec3159ca64d7 (plain) (tree)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893




















































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































                                                                                                                      
/*++

Copyright (c) 1991  Microsoft Corporation
Copyright (c) 1991  Nokia Data Systems

Module Name:

    vrdlc5c.c

Abstract:

    This module handles DLC INT 5Ch calls from a VDM

    Contents:
        VrDlc5cHandler
        (ValidateDosAddress)
        (AutoOpenAdapter)
        (ProcessImmediateCommand)
        (MapDosCommandsToNt)
        CompleteCcbProcessing
        (InitializeAdapterSupport)
        (SaveExceptions)
        (RestoreExceptions)
        (CopyDosBuffersToDescriptorArray)
        (BufferCreate)
        (SetExceptionFlags)
        LlcCommand
        (OpenAdapter)
        (CloseAdapter)
        (OpenDirectStation)
        (CloseDirectStation)
        BufferFree
        (VrDlcInit)
        VrVdmWindowInit
        (GetAdapterType)
        (LoadDlcDll)
        TerminateDlcEmulation
        InitializeDlcWorkerThread
        VrDlcWorkerThread
        DlcCallWorker

Author:

    Antti Saarenheimo (o-anttis) 26-DEC-1991

Revision History:

    16-Jul-1992 Richard L Firth (rfirth)
        Rewrote large parts - separated functions into categories (complete
        in DLL, complete in driver, complete asynchronously); allocate NT
        CCBs for commands which complete asynchronously; fixed asynchronous
        processing; added extra debugging; condensed various per-adapter data
        structures into Adapters data structure; made processing closer to IBM
        LAN Tech. Ref. specification

--*/

#include <nt.h>
#include <ntrtl.h>      // ASSERT, DbgPrint
#include <nturtl.h>
#include <windows.h>
#include <softpc.h>     // x86 virtual machine definitions
#include <vrdlctab.h>
#include <vdmredir.h>
#include <smbgtpt.h>
#include <dlcapi.h>     // Official DLC API definition
#include <ntdddlc.h>    // IOCTL commands
#include <dlcio.h>      // Internal IOCTL API interface structures
#include <vrdefld.h>    // VDM_LOAD_INFO
#include "vrdlc.h"
#include "vrdebug.h"
#include "vrdlcdbg.h"

//
// defines
//

//
// for each DLC command, a flags byte in DlcFunctionalCharacteristics uses these
// bits to indicate properties of the command processing
//

#define POINTERS_IN_TABLE   0x01    // pointers in parameter table
#define OUTPUT_PARMS        0x02    // parameters returned from DLC
#define SECONDARY_TABLE     0x04    // parameter table has pointers to secondary table(s)
#define IMMEDIATE_COMMAND   0x20    // command executes without call to DLC DLL
#define SYNCHRONOUS_COMMAND 0x40    // command executes in workstation
#define UNSUPPORTED_COMMAND 0x80    // command is not supported in DOS DLC

//
// macros
//

//
// IS_IMMEDIATE_COMMAND - the following commands are those which complete
// 'immediately' - i.e. without having to submit the CCB to AcsLan or NtAcsLan.
// Immediate commands may read and write the parameter table though
//

#define IS_IMMEDIATE_COMMAND(c) (((c) == LLC_BUFFER_FREE)            || \
                                 ((c) == LLC_BUFFER_GET)             || \
                                 ((c) == LLC_DIR_INTERRUPT)          || \
                                 ((c) == LLC_DIR_MODIFY_OPEN_PARMS)  || \
                                 ((c) == LLC_DIR_RESTORE_OPEN_PARMS) || \
                                 ((c) == LLC_DIR_SET_USER_APPENDAGE)    \
                                 )

//
// private prototypes
//

LLC_STATUS
ValidateDosAddress(
    IN DOS_ADDRESS Address,
    IN WORD Size,
    IN LLC_STATUS ErrorCode
    );

LLC_STATUS
AutoOpenAdapter(
    IN UCHAR AdapterNumber
    );

LLC_STATUS
ProcessImmediateCommand(
    IN UCHAR AdapterNumber,
    IN UCHAR Command,
    IN LLC_DOS_PARMS UNALIGNED * pParms
    );

LLC_STATUS
MapDosCommandsToNt(
    IN PLLC_CCB pDosCcb,
    IN DOS_ADDRESS dpOriginalCcbAddress,
    OUT LLC_DOS_CCB UNALIGNED * pOutputCcb
    );

LLC_STATUS
InitializeAdapterSupport(
    IN UCHAR AdapterNumber,
    IN DOS_DLC_DIRECT_PARMS UNALIGNED * pDirectParms OPTIONAL
    );

VOID
SaveExceptions(
    IN UCHAR AdapterNumber,
    IN LPDWORD pulExceptionFlags
    );

LPDWORD
RestoreExceptions(
    IN UCHAR AdapterNumber
    );

LLC_STATUS
CopyDosBuffersToDescriptorArray(
    IN OUT PLLC_TRANSMIT_DESCRIPTOR pDescriptors,
    IN PLLC_XMIT_BUFFER pDlcBufferQueue,
    IN OUT LPDWORD pIndex
    );

LLC_STATUS
BufferCreate(
    IN UCHAR AdapterNumber,
    IN PVOID pVirtualMemoryBuffer,
    IN DWORD ulVirtualMemorySize,
    IN DWORD ulMinFreeSizeThreshold,
    OUT HANDLE* phBufferPoolHandle
    );

LLC_STATUS
SetExceptionFlags(
    IN UCHAR AdapterNumber,
    IN DWORD ulAdapterCheckFlag,
    IN DWORD ulNetworkStatusFlag,
    IN DWORD ulPcErrorFlag,
    IN DWORD ulSystemActionFlag
    );

LLC_STATUS
OpenAdapter(
    IN UCHAR AdapterNumber
    );

VOID
CloseAdapter(
    IN UCHAR AdapterNumber
    );

LLC_STATUS
OpenDirectStation(
    IN UCHAR AdapterNumber
    );

VOID
CloseDirectStation(
    IN UCHAR AdapterNumber
    );

LLC_STATUS
VrDlcInit(
    VOID
    );

ADAPTER_TYPE
GetAdapterType(
    IN UCHAR AdapterNumber
    );

BOOLEAN
LoadDlcDll(
    VOID
    );

BOOLEAN
InitializeDlcWorkerThread(
    VOID
    );

VOID
VrDlcWorkerThread(
    IN LPVOID Parameters
    );

LLC_STATUS
DlcCallWorker(
    PLLC_CCB pInputCcb,
    PLLC_CCB pOriginalCcb,
    PLLC_CCB pOutputCcb
    );

//
// public data
//

//
// lpVdmWindow is the flat 32-bit address of the VDM_REDIR_DOS_WINDOW structure
// in DOS memory (in the redir TSR)
//

LPVDM_REDIR_DOS_WINDOW lpVdmWindow = 0;

//
// dpVdmWindow is the DOS address (ssssoooo, s=segment, o=offset) of the
// VDM_REDIR_DOS_WINDOW structure in DOS memory (in the redir TSR)
//

DOS_ADDRESS dpVdmWindow = 0;

DWORD OpenedAdapters = 0;

//
// Adapters - for each adapter supported by DOS emulation (maximum 2 adapters -
// primary & secondary) there is a structure which maintains adapter specific
// information - like whether the adapter has been opened, etc.
//

DOS_ADAPTER Adapters[DOS_DLC_MAX_ADAPTERS];

//
// all functions in DLCAPI.DLL are now called indirected through function pointer
// create these stupid typedefs to avoid compiler warnings
//

typedef ACSLAN_STATUS (*ACSLAN_FUNC_PTR)(IN OUT PLLC_CCB, OUT PLLC_CCB*);
ACSLAN_FUNC_PTR lpAcsLan;

typedef LLC_STATUS (*DLC_CALL_DRIVER_FUNC_PTR)(IN UINT, IN UINT, IN PVOID, IN UINT, OUT PVOID, IN UINT);
DLC_CALL_DRIVER_FUNC_PTR lpDlcCallDriver;

typedef LLC_STATUS (*NTACSLAN_FUNC_PTR)(IN PLLC_CCB, IN PVOID, OUT PLLC_CCB, IN HANDLE OPTIONAL);
NTACSLAN_FUNC_PTR lpNtAcsLan;

//
// private data
//

static LLC_EXTENDED_ADAPTER_PARMS DefaultExtendedParms = {
    NULL,                       // hBufferPool
    NULL,                       // pSecurityDescriptor
    LLC_ETHERNET_TYPE_DEFAULT   // LlcEthernetType
};

//
// DlcFunctionCharacteristics - for each DOS DLC command, tells us the size of
// the parameter table to copy and whether there are pointers in the parameter
// table. Segmented 16-bit pointers in the parameter table must be converted to
// flat 32-bit pointers. The Flags byte tells us - at a glance - the following:
//
//  - if this command is supported a) in DOS DLC, b) in our implementation
//  - if this command has parameters
//  - if there are (DOS) pointers in the parameter table
//  - if this command receives output parameters (ie written to parameter table)
//  - if the parameter table has secondary parameter tables (DIR.OPEN.ADAPTER)
//  - if this command is synchronous (ie does not return 0xFF)
//

struct {
    BYTE    ParamSize;  // no parameter tables >255 bytes long
    BYTE    Flags;
} DlcFunctionCharacteristics[] = {
    {0, IMMEDIATE_COMMAND},                         // 0x00, DIR.INTERRUPT
    {
        sizeof(LLC_DIR_MODIFY_OPEN_PARMS),
        IMMEDIATE_COMMAND
    },                                              // 0x01, DIR.MODIFY.OPEN.PARMS
    {0, IMMEDIATE_COMMAND},                         // 0x02, DIR.RESTORE.OPEN.PARMS
    {
        sizeof(LLC_DIR_OPEN_ADAPTER_PARMS),
        SYNCHRONOUS_COMMAND
        | SECONDARY_TABLE
        | OUTPUT_PARMS
        | POINTERS_IN_TABLE
    },                                              // 0x03, DIR.OPEN.ADAPTER
    {0, 0x00},                                      // 0x04, DIR.CLOSE.ADAPTER
    {0, UNSUPPORTED_COMMAND},                       // 0x05, ?
    {0, SYNCHRONOUS_COMMAND},                       // 0x06, DIR.SET.GROUP.ADDRESS
    {0, SYNCHRONOUS_COMMAND},                       // 0x07, DIR.SET.FUNCTIONAL.ADDRESS
    {0, SYNCHRONOUS_COMMAND},                       // 0x08, READ.LOG
    {0, UNSUPPORTED_COMMAND},                       // 0x09, NT: TRANSMIT.FRAME
    {
        sizeof(LLC_TRANSMIT_PARMS),
        POINTERS_IN_TABLE
    },                                              // 0x0a, TRANSMIT.DIR.FRAME
    {
        sizeof(LLC_TRANSMIT_PARMS),
        POINTERS_IN_TABLE
    },                                              // 0x0b, TRANSMIT.I.FRAME
    {0, UNSUPPORTED_COMMAND},                       // 0x0c, ?
    {sizeof(LLC_TRANSMIT_PARMS), POINTERS_IN_TABLE},// 0x0d, TRANSMIT.UI.FRAME
    {sizeof(LLC_TRANSMIT_PARMS), POINTERS_IN_TABLE},// 0x0e, TRANSMIT.XID.CMD
    {sizeof(LLC_TRANSMIT_PARMS), POINTERS_IN_TABLE},// 0x0f, TRANSMIT.XID.RESP.FINAL
    {sizeof(LLC_TRANSMIT_PARMS), POINTERS_IN_TABLE},// 0x10, TRANSMIT.XID.RESP.NOT.FINAL
    {sizeof(LLC_TRANSMIT_PARMS), POINTERS_IN_TABLE},// 0x11, TRANSMIT.TEST.CMD
    {0, UNSUPPORTED_COMMAND},                       // 0x12, ?
    {0, UNSUPPORTED_COMMAND},                       // 0x13, ?
    {0, 0x00},                                      // 0x14, DLC.RESET
    {
        sizeof(LLC_DLC_OPEN_SAP_PARMS),
        SYNCHRONOUS_COMMAND
        | OUTPUT_PARMS
        | POINTERS_IN_TABLE
    },                                              // 0x15, DLC.OPEN.SAP
    {0, 0x00},                                      // 0x16, DLC.CLOSE.SAP
    {0, SYNCHRONOUS_COMMAND},                       // 0x17, DLC_REALLOCATE
    {0, UNSUPPORTED_COMMAND},                       // 0x18, ?
    {
        sizeof(LLC_DLC_OPEN_STATION_PARMS),
        SYNCHRONOUS_COMMAND
        | OUTPUT_PARMS
        | POINTERS_IN_TABLE
    },                                              // 0x19, DLC.OPEN.STATION
    {0, 0x00},                                      // 0x1a, DLC.CLOSE.STATION
    {
        sizeof(LLC_DLC_CONNECT_PARMS),
        POINTERS_IN_TABLE
    },                                              // 0x1b, DLC.CONNECT.STATION
    {
        sizeof(LLC_DLC_MODIFY_PARMS),
        SYNCHRONOUS_COMMAND
        | POINTERS_IN_TABLE
    },                                              // 0x1c, DLC.MODIFY
    {0, SYNCHRONOUS_COMMAND},                       // 0x1d, DLC.FLOW.CONTROL
    {
        sizeof(LLC_DLC_STATISTICS_PARMS),
        SYNCHRONOUS_COMMAND
        | OUTPUT_PARMS
        | POINTERS_IN_TABLE
    },                                              // 0x1e, DLC.STATISTICS
    {0, UNSUPPORTED_COMMAND},                       // 0x1f, ?
    {
        sizeof(LLC_DOS_DIR_INITIALIZE_PARMS),
        SYNCHRONOUS_COMMAND
        | OUTPUT_PARMS
    },                                              // 0x20, DIR.INITIALIZE
    {
        sizeof(DOS_DIR_STATUS_PARMS) - 2,
        SYNCHRONOUS_COMMAND
        | OUTPUT_PARMS
        | POINTERS_IN_TABLE
    },                                              // 0x21, DIR.STATUS
    {0, 0x00},                                      // 0x22, DIR.TIMER.SET
    {0, SYNCHRONOUS_COMMAND},                       // 0x23, DIR.TIMER.CANCEL
    {0, UNSUPPORTED_COMMAND},                       // 0x24, PDT.TRACE.ON / DLC_TRACE_INITIALIZE
    {0, UNSUPPORTED_COMMAND},                       // 0x25, PDT.TRACE.OFF
    {
        sizeof(LLC_BUFFER_GET_PARMS),
        IMMEDIATE_COMMAND
        | OUTPUT_PARMS
    },                                              // 0x26, BUFFER.GET
    {
        sizeof(LLC_BUFFER_FREE_PARMS),
        IMMEDIATE_COMMAND
        | POINTERS_IN_TABLE
    },                                              // 0x27, BUFFER.FREE
    {sizeof(LLC_DOS_RECEIVE_PARMS), OUTPUT_PARMS},  // 0x28, RECEIVE
    {0, SYNCHRONOUS_COMMAND},                       // 0x29, RECEIVE.CANCEL
    {
        sizeof(LLC_DOS_RECEIVE_MODIFY_PARMS),
        SYNCHRONOUS_COMMAND
        | OUTPUT_PARMS
    },                                              // 0x2a, RECEIVE.MODIFY
    {0, UNSUPPORTED_COMMAND},                       // 0x2b, DIR.DEFINE.MIF.ENVIRONMENT
    {0, SYNCHRONOUS_COMMAND},                       // 0x2c, DLC.TIMER.CANCEL.GROUP
    {
        sizeof(LLC_DIR_SET_EFLAG_PARMS),
        IMMEDIATE_COMMAND
    }                                               // 0x2d, DIR.SET.USER.APPENDAGE
};

//
// routines
//


VOID
VrDlc5cHandler(
    VOID
    )

/*++

Routine Description:

    Receives control from the INT 5Ch BOP provided by the DOS redir TSR. The
    DLC calls can be subdivided into the following categories:

        * complete within this translation layer
        * complete synchronously in a call to AcsLan
        * complete asynchronously after calling AcsLan

    The latter type complete when a READ (which we submit when the adapter is
    opened) completes. Control transfers to an ISR in the DOS redir TSR via
    the EventHandlerThread (in vrdlcpst.c)

    The calls can be further subdivided:

        * calls which return parameters in the parameter table
        * calls which do not return parameters in the parameter table

    For the former type of call, we have to copy the parameter table from
    DOS memory and copy the returned parameters back to DOS memory

    With the exception of a few DLC commands, we assume that the parameter
    tables are exactly the same size between DOS and NT, even if the don't
    contain exactly the same information

Arguments:

    None.

Return Value:

    None, LLC_STATUS is return in AL register.

--*/

{
    LLC_CCB ccb;    // should be NT CCB for NtAcsLan
    LLC_PARMS parms;
    LLC_DOS_CCB UNALIGNED * pOutputCcb;
    LLC_DOS_PARMS UNALIGNED * pDosParms;
    DOS_ADDRESS dpOriginalCcbAddress;
    BOOLEAN parmsCopied;
    WORD paramSize;
    LLC_STATUS status;
    UCHAR command;
    UCHAR adapter;
    BYTE functionFlags;

    static BOOLEAN IsDlcDllLoaded = FALSE;

    IF_DEBUG(DLC) {
        DPUT("VrDlc5cHandler entered\n");
    }

    //
    // DLCAPI.DLL is now dynamically loaded
    //

    if (!IsDlcDllLoaded) {
        if (!LoadDlcDll()) {
            setAL(LLC_STATUS_COMMAND_CANCELLED_FAILURE);
            return;
        } else {
            IsDlcDllLoaded = TRUE;
        }
    }

    //
    // dpOriginalCcbAddress is the segmented 16-bit address stored as a DWORD
    // eg. a CCB1 address of 1234:abcd gets stored as 0x1234abcd. This will
    // be used in asynchronous command completion to get back the address of
    // the original DOS CCB
    //

    dpOriginalCcbAddress = (DOS_ADDRESS)MAKE_DWORD(getES(), getBX());

    //
    // pOutputCcb is the flat 32-bit address of the DOS CCB. We can use this
    // to read and write byte fields only (unaligned)
    //

    pOutputCcb = POINTER_FROM_WORDS(getES(), getBX());
    pOutputCcb->uchDlcStatus = (UCHAR)LLC_STATUS_PENDING;

    //
    // zero the CCB_POINTER (pNext) field. CCB1 cannot have chained CCBs on
    // input: this is just for returning (cancelled) pending CCBs. If we don't
    // zero it & the app leaves garbage there, then NtAcsLan can think it is
    // a pointer to a chain of CCBs (CCB2 can be chained), which is bogus
    //

    WRITE_DWORD(&pOutputCcb->pNext, 0);

    IF_DEBUG(CRITICAL) {
        CRITDUMP(("INPUT CCB @%04x:%04x command=%02x\n", getES(), getBX(), pOutputCcb->uchDlcCommand));
    }

    IF_DEBUG(DOS_CCB_IN) {

        //
        // dump the input CCB1 - gives us an opportunity to check out what
        // the DOS app is sending us, even if its garbage
        //

        DUMPCCB(pOutputCcb,
                TRUE,                           // DumpAll
                TRUE,                           // CcbIsInput
                TRUE,                           // IsDos
                HIWORD(dpOriginalCcbAddress),   // segment
                LOWORD(dpOriginalCcbAddress)    // offset
                );
    }

    //
    // first check that the adapter is 0 or 1 - DOS only supports 2 adapters -
    // and check that the request code in the CCB is not off the end of our
    // table. Unsupported requests will be filtered out in the BIG switch
    // statement below
    //

    adapter = pOutputCcb->uchAdapterNumber;
    command = pOutputCcb->uchDlcCommand;

    if (adapter >= DOS_DLC_MAX_ADAPTERS) {

        //
        // adapter is not 0 or 1 - return 0x1D
        //

        status = LLC_STATUS_INVALID_ADAPTER;
        pOutputCcb->uchDlcStatus = (UCHAR)status;
    } else if (command > LAST_ELEMENT(DlcFunctionCharacteristics)) {

        //
        // command is off end of supported list - return 0x01
        //

        status = LLC_STATUS_INVALID_COMMAND;
        pOutputCcb->uchDlcStatus = (UCHAR)status;
    } else {

        //
        // the command is in range. Get the parameter table size and flags from
        // the function characteristics array
        //

        functionFlags = DlcFunctionCharacteristics[command].Flags;
        paramSize = DlcFunctionCharacteristics[command].ParamSize;

        //
        // if we don't support this command, return an error
        //

        status = LLC_STATUS_SUCCESS;
        if (functionFlags & UNSUPPORTED_COMMAND) {
            status = LLC_STATUS_INVALID_COMMAND;
            pOutputCcb->uchDlcStatus = LLC_STATUS_INVALID_COMMAND;
        } else {

            //
            // command is supported. If it has a parameter table, check that
            // the address is in range for 0x1B error check
            //

            if (paramSize) {
                status = ValidateDosAddress((DOS_ADDRESS)(pOutputCcb->u.pParms),
                                            paramSize,
                                            LLC_STATUS_INVALID_PARAMETER_TABLE
                                            );
            }

            //
            // we allow the adapter to be opened as a consequence of another
            // request since DOS apps could assume that the adapter has already
            // been opened (by NetBIOS). If the command is DIR.OPEN.ADAPTER or
            // DIR.CLOSE.ADAPTER then let it go through
            //

            if (status == LLC_STATUS_SUCCESS
            && !Adapters[adapter].IsOpen
            && !(command == LLC_DIR_OPEN_ADAPTER || command == LLC_DIR_CLOSE_ADAPTER)) {
                status = AutoOpenAdapter(adapter);
            } else {
                status = LLC_STATUS_SUCCESS;
            }
        }

        //
        // if we have a valid command, an ok-looking parameter table pointer
        // and an open adapter (or a command which will open or close it) then
        // process the command
        //

        if (status == LLC_STATUS_SUCCESS) {

            //
            // get a 32-bit pointer to the DOS parameter table. This may be
            // an unaligned address!
            //

            pDosParms = READ_FAR_POINTER(&pOutputCcb->u.pParms);

            //
            // the CCB commands are subdivided into those which do not need the
            // CCB to be mapped from DOS memory to NT memory and which complete
            // 'immediately' in this DLL, and those which must be mapped from
            // DOS to NT and which may complete synchronously or asynchronously
            //

            if (functionFlags & IMMEDIATE_COMMAND) {

                IF_DEBUG(DLC) {
                    DPUT("VrDlc5cHandler: request is IMMEDIATE command\n");
                }

                status = ProcessImmediateCommand(adapter, command, pDosParms);

                //
                // the following is safe - it is a single byte write
                //

                pOutputCcb->uchDlcStatus = status;

                //
                // the 'immediate' case is now complete, and control can be
                // returned to the VDM
                //

            } else {

                //
                // the CCB is not one which can be completed immediately. We
                // have to copy (and align) the DOS CCB (and the parameter
                // table, if there is one) into 32-bit address space.
                // Note that since we are going to call AcsLan or NtAcsLan
                // with this CCB then we supply the correct CCB format - 2,
                // not 1 as it was previously. However, handing in a CCB1
                // didn't *seem* to cause any problems (yet)
                //

                RtlCopyMemory(&ccb, pOutputCcb, sizeof(*pOutputCcb));

                //
                // zero the unused fields
                //

                ccb.hCompletionEvent = 0;
                ccb.uchReserved2 = 0;
                ccb.uchReadFlag = 0;
                ccb.usReserved3 = 0;

                parmsCopied = FALSE;
                if (paramSize) {

                    //
                    // if the parameter table contains (segmented) pointers
                    // (which we need to convert to flat-32 bit pointers) OR
                    // the parameter table is not DWORD aligned, copy the whole
                    // parameter table to 32-bit memory . If we need to modify
                    // pointers, do it in the specific case in the switch
                    // statement in MapDosCommandsToNt
                    //
                    // Note: DIR.OPEN.ADAPTER is a special case because its
                    // parameter table just points to 4 other parameter tables.
                    // We take care of this in MapDosCommandsToNt and
                    // CompleteCcbProcessing
                    //

                    if ((functionFlags & POINTERS_IN_TABLE)) {
                        RtlCopyMemory(&parms, pDosParms, paramSize);
                        ccb.u.pParameterTable = &parms;
                        parmsCopied = TRUE;
                    } else {

                        //
                        // didn't need to copy parameter table - leave it in
                        // DOS memory. It is safe to read & write this table
                        //

                        ccb.u.pParameterTable = (PLLC_PARMS)pDosParms;
                    }
                }

                //
                // submit the synchronous/asynchronous CCB for processing
                //

                status = MapDosCommandsToNt(&ccb, dpOriginalCcbAddress, pOutputCcb);
                if (status == STATUS_PENDING) {
                    status = LLC_STATUS_PENDING;
                }

                IF_DEBUG(CRITICAL) {
                    CRITDUMP(("CCB submitted: returns %02x\n", status));
                }

                IF_DEBUG(DLC) {
                    DPUT2("VrDlc5cHandler: MapDosCommandsToNt returns %#x (%d)\n", status, status);
                }

                //
                // if status is not LLC_STATUS_PENDING then the CCB completed
                // synchronously. We can complete the processing here
                //

                if (status != LLC_STATUS_PENDING) {
                    if ((functionFlags & OUTPUT_PARMS) && parmsCopied) {

                        //
                        // if there are no pointers in the parameter table then
                        // we can simply copy the 32-bit parameters to 16-bit
                        // memory. If there are pointers, then they will be in
                        // an incorrect format for DOS. We must update these
                        // parameter tables individually
                        //

                        if (!(functionFlags & POINTERS_IN_TABLE)) {
                            RtlCopyMemory(pDosParms, &parms, paramSize);
                        } else {
                            CompleteCcbProcessing(status, pOutputCcb, &parms);
                        }
                    }

                    //
                    // set the CCB status. It will be marked as PENDING if
                    // LLC_STATUS_PENDING returned from MapDosCommandsToNt
                    //

                    pOutputCcb->uchDlcStatus = (UCHAR)status;
                }
            }
        } else {
            pOutputCcb->uchDlcStatus = (UCHAR)status;
        }
    }

    //
    // return the DLC status in AL
    //

    setAL((UCHAR)status);

#if DBG
    IF_DEBUG(DOS_CCB_OUT) {

        DPUT2("VrDlc5cHandler: returning AL=%02x CCB.RETCODE=%02x\n",
                status,
                pOutputCcb->uchDlcStatus
                );

        //
        // dump the CCB being returned to the DOS app
        //

        DumpCcb(pOutputCcb,
                TRUE,                           // DumpAll
                FALSE,                          // CcbIsInput
                TRUE,                           // IsDos
                HIWORD(dpOriginalCcbAddress),   // segment
                LOWORD(dpOriginalCcbAddress)    // offset
                );
    }

    //
    // make sure (in debug version) that the error code being returned is valid
    // for this particular command. Doesn't necessarily mean the return code is
    // semantically correct, just that it belongs to the set of allowed DLC
    // return codes for the DLC command being processed
    //

    IF_DEBUG(DLC) {
        if (!IsCcbErrorCodeAllowable(pOutputCcb->uchDlcCommand, pOutputCcb->uchDlcStatus)) {
            DPUT2("Returning bad error code: Command=%02x, Retcode=%02x\n",
                    pOutputCcb->uchDlcCommand,
                    pOutputCcb->uchDlcStatus
                    );
            DEBUG_BREAK();
        }
    }
#endif
}


LLC_STATUS
ValidateDosAddress(
    IN DOS_ADDRESS Address,
    IN WORD Size,
    IN LLC_STATUS ErrorCode
    )

/*++

Routine Description:

    IBM DLC performs some checking of pointers - if the address points into
    the IVT or is close enough to the end of a segment that the address would
    wrap then we return an error

    This is a moronic test, but we do it for compatibility (just in case an
    app tests for the specific error code). There are a million other addresses
    in DOS memory that need to be checked. The tests in this routine will only
    protect against scribbling over the interrupt vectors, but would allow e.g.
    scribbling over DOS's code segment

Arguments:

    Address     - DOS address to check (ssssoooo, s=segment, o=offset)
    Size        - word size of structure at Address
    ErrorCode   - which error code to return. This function called to validate
                  A) the parameter table pointer, in which case the error code
                  to return is LLC_STATUS_INVALID_PARAMETER_TABLE (0x1B) or
                  B) pointers within the parameter table, in which case the
                  error to return is LLC_STATUS_INVALID_POINTER_IN_CCB (0x1C)

Return Value:

    LLC_STATUS

--*/

{
    //
    // convert segment:offset into 20-bit real-mode linear address
    //

    DWORD linearAddress = HIWORD(Address) * 16 + LOWORD(Address);

    //
    // the Interrupt Vector Table (IVT) in real-mode occupies addresses 0
    // through 400h
    //

    if ((linearAddress < 0x400L) || (((DWORD)LOWORD(Address) + Size) < (DWORD)LOWORD(Address))) {
        return ErrorCode;
    }
    return LLC_STATUS_SUCCESS;
}


LLC_STATUS
AutoOpenAdapter(
    IN UCHAR AdapterNumber
    )

/*++

Routine Description:

    Opens the adapter as a consequence of a request other than DIR.OPEN.ADAPTER

Arguments:

    AdapterNumber - which adapter to open

Return Value:

    LLC_STATUS
        Success - LLC_STATUS_SUCCESS
        Failure -

--*/

{
    LLC_STATUS status;

    //
    // Any DLC command except DIR.OPEN.ADAPTER or DIR.INITIALIZE automatically
    // opens the adapter. There are three reasons to do this:
    //
    //  1. DIR.STATUS command can be issued before DIR.OPEN.ADAPTER in DOS.
    //     In Windows/Nt this is not possible. Therefore, DIR.STATUS should
    //     open the adapter
    //
    //  2. An application may assume that the adapter is always opened
    //     by NetBIOS and that it can't open the adapter itself if it
    //     has already been opened
    //
    //  3. A DOS DLC application may initialize (= hw reset) a closed
    //     adapter before the open and that takes 5 - 10 seconds.
    //

    IF_DEBUG(DLC) {
        DPUT1("AutoOpenAdapter: automatically opening adapter %d\n", AdapterNumber);
    }

    status = OpenAdapter(AdapterNumber);
    if (status == LLC_STATUS_SUCCESS) {

        //
        // initialize the buffer pool for the direct station on this
        // adapter. If this succeeds, open the direct station. If that
        // succeeds, preset the ADAPTER_PARMS and DLC_PARMS structures
        // in the DOS_ADAPTER with default values
        //

        status = InitializeAdapterSupport(AdapterNumber, NULL);
        if (status == LLC_STATUS_SUCCESS) {
            status = OpenDirectStation(AdapterNumber);
            if (status == LLC_STATUS_SUCCESS) {

            }
        }

        if (status != LLC_STATUS_SUCCESS) {

            IF_DEBUG(DLC) {
                DPUT("AutoOpenAdapter: InitializeAdapterSupport failed\n");
            }

        }
    } else {

        IF_DEBUG(DLC) {
            DPUT("AutoOpenAdapter: auto open adapter failed\n");
        }

    }

    return status;
}


LLC_STATUS
ProcessImmediateCommand(
    IN UCHAR AdapterNumber,
    IN UCHAR Command,
    IN LLC_DOS_PARMS UNALIGNED * pParms
    )

/*++

Routine Description:

    Processes CCB requests which complete 'immediately'. An immediate completion
    is one where the CCB does not have to be submitted to the DLC driver. There
    may be other calls to the driver as a consequence of the immediate command,
    but the CCB itself is not submitted. Immediate command completion requires
    the parameter table only. We may return parameters into the DOS parameter
    table

Arguments:

    AdapterNumber   - which adapter to process command for
    Command         - command to process
    pParms          - pointer to parameter table (in DOS memory)

Return Value:

    LLC_STATUS
        Completion status of the 'immediate' command

--*/

{
    LLC_STATUS status;
    WORD cBuffersLeft;
    WORD stationId;
    DPLLC_DOS_BUFFER buffer;

    switch (Command) {
    case LLC_BUFFER_FREE:

        IF_DEBUG(DLC) {
            DPUT("LLC_BUFFER_FREE\n");
        }

        //
        // if the FIRST_BUFFER field is 0:0 then this request returns success
        //

        buffer = (DPLLC_DOS_BUFFER)READ_DWORD(&pParms->BufferFree.pFirstBuffer);
        if (!buffer) {
            status = LLC_STATUS_SUCCESS;
            break;
        }

        //
        // Windows/Nt doesn't need station id for buffer pool operation =>
        // thus the field is reserved
        //

        stationId = READ_WORD(&pParms->BufferFree.usReserved1);
        status = FreeBuffers(GET_POOL_INDEX(AdapterNumber, stationId),
                             buffer,
                             &cBuffersLeft
                             );

        IF_DEBUG(CRITICAL) {
            CRITDUMP(("LLC_BUFFER_FREE: %d\n", status));
        }

        if (status == LLC_STATUS_SUCCESS) {

            //
            // write the number of buffers left to the parameter table using
            // WRITE_WORD macro, since the table may not be aligned on a WORD
            // boundary
            //

            WRITE_WORD(&pParms->BufferFree.cBuffersLeft, cBuffersLeft);

            //
            // p3-4 of the IBM LAN Tech. Ref. states that the FIRST_BUFFER
            // field will be set to zero when the request is completed
            //

            WRITE_DWORD(&pParms->BufferFree.pFirstBuffer, 0);

            //
            // note that a successful BUFFER.FREE has been executed for this
            // adapter
            //

            Adapters[AdapterNumber].BufferFree = TRUE;

            //
            // perform half of the local-busy reset processing. This only has
            // an effect if the link is in emulated local-busy(buffer) state.
            // This is required because we need 2 events to get us out of local
            // busy buffer state - a BUFFER.FREE and a DLC.FLOW.CONTROL command
            // Apps don't always issue these in the correct sequence
            //

            ResetEmulatedLocalBusyState(AdapterNumber, stationId, LLC_BUFFER_FREE);

            //
            // this here because Extra! for Windows gets its state machine in a
            // knot if we go buffer busy too quickly after a flow control
            //

            if (AllBuffersInPool(GET_POOL_INDEX(AdapterNumber, stationId))) {
                ResetEmulatedLocalBusyState(AdapterNumber, stationId, LLC_DLC_FLOW_CONTROL);
            }
        }
        break;

    case LLC_BUFFER_GET:

        IF_DEBUG(DLC) {
            DPUT("LLC_BUFFER_GET\n");
        }

        status = GetBuffers(
                    GET_POOL_INDEX(AdapterNumber, READ_WORD(&pParms->BufferGet.usReserved1)),
                    READ_WORD(&pParms->BufferGet.cBuffersToGet),
                    &buffer,
                    &cBuffersLeft,
                    FALSE,
                    NULL
                    );

        //
        // if GetBuffers fails, buffer is returned as 0
        //

        WRITE_WORD(&pParms->BufferGet.cBuffersLeft, cBuffersLeft);
        WRITE_DWORD(&pParms->BufferGet.pFirstBuffer, buffer);
        break;

    case LLC_DIR_INTERRUPT:

        IF_DEBUG(DLC) {
            DPUT("LLC_DIR_INTERRUPT\n");
        }

        //
        // We may consider, that the adapter is always initialized!
        // I hope, that the apps doesn't use an appendage routine
        // in DIR_INTERRUPT, because it would be very difficult to
        // call from here.
        //

        status = LLC_STATUS_SUCCESS;
        break;

    case LLC_DIR_MODIFY_OPEN_PARMS:

        IF_DEBUG(DLC) {
            DPUT("LLC_DIR_MODIFY_OPEN_PARMS\n");
        }

        //
        // this command is rejected if a BUFFER.FREE has been issued for this
        // adapter or there is a RECEIVE active at the direct interface
        //

        if (Adapters[AdapterNumber].BufferFree || Adapters[AdapterNumber].DirectReceive) {

            //
            // BUGBUG - this can't be correct error code. Check what is
            //          returned by IBM DOS DLC stack
            //

            status = LLC_STATUS_INVALID_COMMAND;
        } else if (Adapters[AdapterNumber].WaitingRestore) {

            //
            // BUGBUG - this can't be correct error code. Check what is
            //          returned by IBM DOS DLC stack
            //

            status = LLC_STATUS_INVALID_COMMAND;
        } else {

            //
            // Create a buffer pool, if there are no buffers in
            // the current one, set the exception flags (or dos appendage
            // routines), if the operation was success
            //

            status = CreateBufferPool(
                        (DWORD)GET_POOL_INDEX(AdapterNumber, 0),
                        (DOS_ADDRESS)READ_DWORD(&pParms->DirModifyOpenParms.dpPoolAddress),
                        READ_WORD(&pParms->DirModifyOpenParms.cPoolBlocks),
                        READ_WORD(&pParms->DirModifyOpenParms.usBufferSize)
                        );

            if (status == LLC_STATUS_SUCCESS) {

                //
                // SaveExceptions uses RtlCopyMemory, so its okay to pass in a possibly
                // unaligned pointer to the exception handlers
                //

                SaveExceptions(AdapterNumber,
                    (LPDWORD)&pParms->DirModifyOpenParms.dpAdapterCheckExit
                    );

                //
                // set the exception handlers as the exception flags in the
                // DLC driver
                //

                status = SetExceptionFlags(
                            AdapterNumber,
                            READ_DWORD(&pParms->DirModifyOpenParms.dpAdapterCheckExit),
                            READ_DWORD(&pParms->DirModifyOpenParms.dpNetworkStatusExit),
                            READ_DWORD(&pParms->DirModifyOpenParms.dpPcErrorExit),
                            0
                            );
            }

            //
            // mark this adapter as waiting for a DIR.RESTORE.OPEN.PARMS before
            // we can process the next DIR.MODIFY.OPEN.PARMS
            //

            if (status == LLC_STATUS_SUCCESS) {
                Adapters[AdapterNumber].WaitingRestore = TRUE;
            }
        }
        break;

    case LLC_DIR_RESTORE_OPEN_PARMS:

        IF_DEBUG(DLC) {
            DPUT("LLC_DIR_RESTORE_OPEN_PARMS\n");
        }

        //
        // if a DIR.MODIFY.OPEN.PARMS has not been successfully processed for
        // this adapter then return an error
        //

        if (!Adapters[AdapterNumber].WaitingRestore) {
            status = LLC_STATUS_INVALID_OPTION;
        } else {

            //
            // Delete the current buffer pool and restore the previous exception
            // handlers
            //

            DeleteBufferPool(GET_POOL_INDEX(AdapterNumber, 0));
            pParms = (PLLC_DOS_PARMS)RestoreExceptions(AdapterNumber);
            status = SetExceptionFlags(
                        AdapterNumber,
                        READ_DWORD(&pParms->DirSetExceptionFlags.ulAdapterCheckFlag),
                        READ_DWORD(&pParms->DirSetExceptionFlags.ulNetworkStatusFlag),
                        READ_DWORD(&pParms->DirSetExceptionFlags.ulPcErrorFlag),
                        0
                        );

            //
            // if the restore succeeded, mark this adapter as able to accept
            // the next DIR.MODIFY.OPEN.PARMS request
            //

            if (status == LLC_STATUS_SUCCESS) {
                Adapters[AdapterNumber].WaitingRestore = FALSE;
            }
        }
        break;

    case LLC_DIR_SET_USER_APPENDAGE:

        IF_DEBUG(DLC) {
            DPUT("LLC_DIR_SET_USER_APPENDAGE\n");
        }

        if (pParms == NULL) {
            pParms = (PLLC_DOS_PARMS)RestoreExceptions(AdapterNumber);
        } else {
            SaveExceptions(AdapterNumber, (LPDWORD)&pParms->DirSetUserAppendage);
        }
        status = SetExceptionFlags(
                    AdapterNumber,
                    READ_DWORD(&pParms->DirSetUserAppendage.dpAdapterCheckExit),
                    READ_DWORD(&pParms->DirSetUserAppendage.dpNetworkStatusExit),
                    READ_DWORD(&pParms->DirSetUserAppendage.dpPcErrorExit),
                    0
                    );
        break;

#if DBG
    default:
        DPUT1("ProcessImmediateCommand: Error: Command is NOT immediate (%x)\n", Command);
        DbgBreakPoint();
#endif

    }

    return status;
}


LLC_STATUS
MapDosCommandsToNt(
    IN PLLC_CCB pDosCcb,
    IN DOS_ADDRESS dpOriginalCcbAddress,
    OUT LLC_DOS_CCB UNALIGNED * pOutputCcb
    )

/*++

Routine Description:

    This function handles DOS CCBs which must be submitted to the DLC driver.
    The CCBs may complete synchronously - i.e. the DLC driver returns a
    success or failure indication, or they complete asyncronously - i.e. the
    DLC driver returns a pending status.

    This function processes CCBs which may have parameter tables. The parameter
    tables will have been mapped to 32-bit memory unless they are already aligned
    on a DWORD boundary

Architecture
------------

    There is a major difference in the adapter initialization between DOS and
    NT (or OS/2) DLC applications.  A DOS DLC application could assume that
    the adapter is always open (opened by NetBIOS).  It might directly check
    the type of adapter with DIR.STATUS command, open SAP stations and setup
    a link session to a host. Usually a DOS app uses DIR.INTERRUPT to check if
    the adapter is already initialized.  There are also commands to redefine
    new parameters for the direct interface and restore the old ones when the
    application completes.  Only one application may be simultaneously using
    the direct station or a SAP.

    In Windows/NT each DLC application is a process having its own separate
    virtual image of the DLC interface. They must first make a logical open for
    the adapter to access DLC services and close the adapter when they are
    terminating. Process exit automatically closes any open DLC objects.

    A Windows/NT MVDM process does not allocate any DLC resources until it
    issues the first DLC command, which opens the adapter and initializes its
    buffer pool.


DLC Commands Different In Windows/NT And DOS
--------------------------------------------

    BUFFER.FREE, BUFFER.GET
     -  separate buffer pools ...

    DIR.DEFINE.MIF.ENVIRONMENT
     -  not supported, a special api for
        IBM Netbios running on DLC.

    DIR.INITIALIZE
     -  We will always return OK status from DIR.INITIALIZE: the app should not
        call this very often. We don't need to care about the exception handlers
        set in DIR.INITIALIZE, because they are never used. DOS DLC and OS/2 DLC
        states can be mapped thus:

            DOS DLC             OS/2 DLC
            ----------------------------
            uninitialized       Closed
            Initialized         Closed
            Open                Open

        DIR.OPEN.ADAPTER defines new exception handlers, thus the values
        set by DIR.INITIALIZE are valid only when the adapter is closed.
        Therefore, nothing untoward can happen if we just ignore them

    DIR.INTERRUPT
     -  This command opens the adapter, if it has not yet been opened
        and returns the successful status.

    DIR.MODIFY.OPEN.PARMS
     -  Defines buffers pool for the direct station, if it was not defined
        in DIR.OPEN.ADAPTER, and defines DLC exception handlers.

    DIR.OPEN.ADAPTER
     -  Can be executed only immediately after DIR.CLOSE.ADAPTER
        and DIR.INITIALIZE. We must support the special DOS Direct Open Parms.

    DIR.OPEN.DIRECT, DIR.CLOSE.DIRECT
     -  Not supported for DOS

    DIR.SET.USER.APPENDAGE == DIR.SET.EXCEPTION.FLAGS (- system action flag)
     -  This is just one of those many functions to set the exception handlers
        for DOS DLC (you may set them when adapter is opened, you may set
        them when adapter is closed, you may restore the old values and
        set the new values if the buffer pool was uninitialized or if they
        were restored ... (I become crazy)

    DIR.STATUS
     -  (We must fill MicroCodeLevel with a sensible string and set
        AdapterConfigAddress to point a some constant code in DOS
        DLC handler) Not yet implemented!!!
     -  DOS DLC stub code should hook the timer tick interrupt and
        update the timer counter.
     -  We must also set the correct adapter data rate in AdapterConfig
        (this should be done by the DLC driver!).
     -  We must convert the Nt (and OS/2) adapter type to a DOS type
        (ethernet have a different value in IBM DOS and OS/2 DLC
        implementations)

    PDT.TRACE.ON, PDT.TRACE.OFF
     -  Not Supported

    RECEIVE.MODIFY
     -  This function is not supported in the first implementation,
        Richard may have to do this later, if a DOS DLC application
        uses the function.

    RECEIVE
     -  DOS uses shorter RECEIVE parameter table, than NT (or OS/2).
        Thus we cannot directly use DOS CCBs.  We also need the pointer
        of the original receive CCB and the DOS receive appendage is called.
        On the other hand, the only original CCB can be linked to the
        other CCBs (using the original DOS pointer).
        Solution:
        The Input CCB and its parameter table are always allocated from the
        stack. Output CCB is the original DOS CCB mapped to 32-bit address space.
        The receive flag in the input CCB parameter table is the output CCB
        pointer.  The actual receive data appendage can be read from
        the output CCB. DOS DLC driver completes and links the receive CCB
        correctly, because we use the original receive CCB as an output buffer
        and DOS CCB address.
        This method would not work if we had to receive any parameters
        to the parameter table (actually we could get it to work by
        calling directly DLC driver with a correct parameter table address
        in the CCB structure of the input parameter block. The actual
        input parameters would be modified (receive data appendage)).


Adapter Exception Handlers
--------------------------

    The exception handler setting and resetting is very confusing in DOS DLC,
    but the main idea seems to be this: a temporary DOS DLC application must
    restore the exception handlers set by a TSR, because otherwise the next
    network exception would call the random memory.  On the other hand, a newer
    TSR may always overwrite the previous exception handlers (because it never
    restores the old values).  We may assume, that any DOS DLC application
    process resets its exception handlers and restores the original addresses.
    Solution: we have two tables, both initially reset: any set operation
    copies first table 1 to table 2 and saves the new values back to table 1.
    A restore operation copies table 2 back to table 1 and sets its values to DLC.
    We don't make any difference between set/reset by DIR.SET.USER.APPENDAGE or
    doing the same operation with DIR.MODIFY.OPEN.PARMS and DIR.RESTORE.OPEN.PARMS.
    We don't try to save the buffer pool definitions, because it is unnecessary.


DLC Status
----------

    A DOS DLC status indication returns a pointer to a link specific DLC
    status table.  DOS DLC application may keep this pointer and use it
    later for example to find the remote node address and SAP (not very likely).
    On the other hand, the link may expect the status table to be stable
    until another DOS task (eg. from timer tick) has responded to it.
    If we used only one status table for all links, a new overwrite the old
    status, because it has been handled by DOS.
    For example, losing a local buffer busy indication would hang up the link
    station permamently. Thus we cannot use just one link status table.
    Allocating 20 bytes for each 2 * 255 link station would take 10 kB
    DOS memory.  Limiting the number of available link stations would
    be also too painful to implement and manage by installation program.
    Thus we will implement a compromise:
        1. We allocate 5 permanent DLC status tables for the first link stations
           on both adapters.
        2. All other link stations (on both adapters) share the last
           5 status tables

    => We need to allocate only 300 bytes DOS memory for the DLC status tables.
    This will not work, if a DOS application assumes having permanent pointers
    to status tables and uses more than 5 DLC sessions for an adapter or
    if an application has again over 5 link stations on an adapter and
    it gets very rapidily more than 5 DLC status indications (it may lose
    the first DLC indications).

    Actually this should work quite well, because DLC applications should
    save (by copying) the DLC status in the DLC status appendage routine,
    because a new DLC status indication for the same adapter could overwrite
    the previous status.

Arguments:

    pDosCcb             - aligned DOS DLC Command control block (NT CCB)
    dpOriginalCcbAddress- the original
    pOutputCcb          - the original DLC Command control block

Return Value:

    LLC_STATUS

--*/

{
    UCHAR adapter = pDosCcb->uchAdapterNumber;
    UCHAR command = pDosCcb->uchDlcCommand;
    DWORD InputBufferSize;
    UCHAR FrameType;
    DWORD cElement;
    DWORD i;
    PLLC_CCB pNewCcb;
    LLC_STATUS Status;
    NT_DLC_PARMS NtDlcParms;
    LLC_DOS_PARMS UNALIGNED * pParms = (PLLC_DOS_PARMS)pDosCcb->u.pParameterTable;
    PDOS_DLC_DIRECT_PARMS pDirectParms;
    PLLC_PARMS pNtParms;

    //
    // adapterOpenParms and dlcParms are used to take the place of the DOS
    // ADAPTER_PARMS and DLC_PARMS structures in DIR.OPEN.ADAPTER
    //

    LLC_ADAPTER_OPEN_PARMS adapterParms;
    LLC_DLC_PARMS dlcParms;
    DWORD groupAddress;
    DWORD functionalAddress;

    IF_DEBUG(DLC) {
        DPUT("MapDosCommandsToNt\n");
    }

    //
    // check that the command code in the CCB is a valid CCB1 command - this
    // will error if its one of the disallowed OS/2 (CCB2) commands or an
    // unsupported DOS (CCB1) command (eg PDT.TRACE.ON)
    //

    CHECK_CCB_COMMAND(pDosCcb);

    //
    // preset the CCB to PENDING
    //

    pOutputCcb->uchDlcStatus = (UCHAR)LLC_STATUS_PENDING;

    //
    // This large switch statement will convert individual DOS format parameter
    // tables to NT format. Functions that can be handled entirely in VdmRedir
    // return early, else we need to make a call into DlcApi (AcsLan or NtAcsLan)
    //
    // We must convert all 16:16 DOS pointers to flat 32-bit pointers.
    // We must copy all changed data structures (that includes pointers)
    // to stack, because we don't want to change them back, when
    // the command completes.  All transmit commands are changed to
    // the new generic transmit.
    //

    switch (command) {
    default:

        IF_DEBUG(DLC) {
            DPUT("*** Shouldn't be here - this command should be caught already ***\n");
        }

        return LLC_STATUS_INVALID_COMMAND;

    //
    // *** everything below here has been sanctioned ***
    //

    case LLC_DIR_CLOSE_ADAPTER:

        IF_DEBUG(DLC) {
            DPUT("LLC_DIR_CLOSE_ADAPTER\n");
        }

        //
        // no parameter table
        //

        break;

    case LLC_DIR_INITIALIZE:

        IF_DEBUG(DLC) {
            DPUT("LLC_DIR_INITIALIZE\n");
        }

        break;

    case LLC_DIR_OPEN_ADAPTER:

        IF_DEBUG(DLC) {
            DPUT("LLC_DIR_OPEN_ADAPTER\n");
        }

        //
        // copy the adapter parms and DLC parms to 32-bit memory. If there is no
        // adapter parms or direct parms pointer then return an error
        //

        if (!(pParms->DirOpenAdapter.pAdapterParms && pParms->DirOpenAdapter.pExtendedParms)) {
            return LLC_STATUS_PARAMETER_MISSING;
        }

        //
        // copy the DOS ADAPTER_PARMS table to an NT ADAPTER_PARMS table. The
        // NT table is larger, so zero the uncopied part
        //

        RtlCopyMemory(&adapterParms,
                      DOS_PTR_TO_FLAT(pParms->DirOpenAdapter.pAdapterParms),
                      sizeof(ADAPTER_PARMS)
                      );
        RtlZeroMemory(&adapterParms.usReserved3[0],
                      sizeof(LLC_ADAPTER_OPEN_PARMS) - (DWORD)&((PLLC_ADAPTER_OPEN_PARMS)0)->usReserved3
                      );
        pParms->DirOpenAdapter.pAdapterParms = &adapterParms;

        //
        // make a note of the group and functional addresses. We have to set
        // these later if they're not 0x00000000. We have to save these, because
        // the addresses in the table will get blasted when DIR.OPEN.ADAPTER
        // (in DLCAPI.DLL/DLC.SYS) writes the current (0) values to the table
        //

        groupAddress = *(UNALIGNED DWORD *)adapterParms.auchGroupAddress;
        functionalAddress = *(UNALIGNED DWORD *)adapterParms.auchFunctionalAddress;

        //
        // the DLC_PARMS table doesn't HAVE to be supplied - if we just want to
        // use the direct station, we don't need these parameters. However, if
        // they were supplied, copy them to 32-bit memory. The tables are the
        // same size in DOS and NT
        //

        if (pParms->DirOpenAdapter.pDlcParms) {
            RtlCopyMemory(&dlcParms,
                          DOS_PTR_TO_FLAT(pParms->DirOpenAdapter.pDlcParms),
                          sizeof(dlcParms)
                          );
            pParms->DirOpenAdapter.pDlcParms = &dlcParms;
        }

        //
        // set the default values for ADAPTER_PARMS. Not sure about these
        //
        //  usReserved1 == NUMBER_RCV_BUFFERS
        //  usReserved2 == RCV_BUFFER_LEN
        //  usMaxFrameSize == DHB_BUFFER_LENGTH
        //  usReserved3[0] == DATA_HOLD_BUFFERS
        //

        if (pParms->DirOpenAdapter.pAdapterParms->usReserved1 < 2) {
            pParms->DirOpenAdapter.pAdapterParms->usReserved1 = DD_NUMBER_RCV_BUFFERS;
        }
        if (pParms->DirOpenAdapter.pAdapterParms->usReserved2 == 0) {
            pParms->DirOpenAdapter.pAdapterParms->usReserved2 = DD_RCV_BUFFER_LENGTH;
        }
        if (pParms->DirOpenAdapter.pAdapterParms->usMaxFrameSize == 0) {
            pParms->DirOpenAdapter.pAdapterParms->usReserved1 = DD_DHB_BUFFER_LENGTH;
        }
        if (*(PBYTE)&pParms->DirOpenAdapter.pAdapterParms->usReserved3 == 0) {
            pParms->DirOpenAdapter.pAdapterParms->usReserved1 = DD_DATA_HOLD_BUFFERS;
        }

        //
        // if we have DLC_PARMS then set the defaults, else reset the flag
        //

        if (pParms->DirOpenAdapter.pDlcParms) {
            if (pParms->DirOpenAdapter.pDlcParms->uchDlcMaxSaps == 0) {
                pParms->DirOpenAdapter.pDlcParms->uchDlcMaxSaps = DD_DLC_MAX_SAP;
            }
            if (pParms->DirOpenAdapter.pDlcParms->uchDlcMaxStations == 0) {
                pParms->DirOpenAdapter.pDlcParms->uchDlcMaxStations = DD_DLC_MAX_STATIONS;
            }
            if (pParms->DirOpenAdapter.pDlcParms->uchDlcMaxGroupSaps == 0) {
                pParms->DirOpenAdapter.pDlcParms->uchDlcMaxGroupSaps = DD_DLC_MAX_GSAP;
            }
            if (pParms->DirOpenAdapter.pDlcParms->uchT1_TickOne == 0) {
                pParms->DirOpenAdapter.pDlcParms->uchT1_TickOne = DD_DLC_T1_TICK_ONE;
            }
            if (pParms->DirOpenAdapter.pDlcParms->uchT2_TickOne == 0) {
                pParms->DirOpenAdapter.pDlcParms->uchT2_TickOne = DD_DLC_T2_TICK_ONE;
            }
            if (pParms->DirOpenAdapter.pDlcParms->uchTi_TickOne == 0) {
                pParms->DirOpenAdapter.pDlcParms->uchTi_TickOne = DD_DLC_Ti_TICK_ONE;
            }
            if (pParms->DirOpenAdapter.pDlcParms->uchT1_TickTwo == 0) {
                pParms->DirOpenAdapter.pDlcParms->uchT1_TickTwo = DD_DLC_T1_TICK_TWO;
            }
            if (pParms->DirOpenAdapter.pDlcParms->uchT2_TickTwo == 0) {
                pParms->DirOpenAdapter.pDlcParms->uchT2_TickTwo = DD_DLC_T2_TICK_TWO;
            }
            if (pParms->DirOpenAdapter.pDlcParms->uchTi_TickTwo == 0) {
                pParms->DirOpenAdapter.pDlcParms->uchTi_TickTwo = DD_DLC_Ti_TICK_TWO;
            }
            Adapters[adapter].DlcSpecified = TRUE;
        } else {
            Adapters[adapter].DlcSpecified = FALSE;
        }

        //
        // replace DIRECT_PARMS with the EXTENDED_ADAPTER_PARMS
        //

        pDirectParms = (PDOS_DLC_DIRECT_PARMS)
                       READ_FAR_POINTER(&pParms->DirOpenAdapter.pExtendedParms);
        pParms->DirOpenAdapter.pExtendedParms = &DefaultExtendedParms;
        break;

    case LLC_DIR_READ_LOG:

        IF_DEBUG(DLC) {
            DPUT("LLC_DIR_READ_LOG\n");
        }

        pParms->DirReadLog.pLogBuffer = DOS_PTR_TO_FLAT(pParms->DirReadLog.pLogBuffer);
        break;

    case LLC_DIR_SET_FUNCTIONAL_ADDRESS:

        IF_DEBUG(DLC) {
            DPUT("LLC_DIR_SET_FUNCTIONAL_ADDRESS\n");
        }

        //
        // no parameter table
        //

        break;

    case LLC_DIR_SET_GROUP_ADDRESS:

        IF_DEBUG(DLC) {
            DPUT("LLC_DIR_SET_GROUP_ADDRESS\n");
        }

        //
        // no parameter table
        //

        break;

    case LLC_DIR_STATUS:

        IF_DEBUG(DLC) {
            DPUT("LLC_DIR_STATUS\n");
        }

        break;

    case LLC_DIR_TIMER_CANCEL:

        IF_DEBUG(DLC) {
            DPUT("LLC_DIR_TIMER_CANCEL\n");
        }

        //
        // no parameter table
        //

        break;

    case LLC_DIR_TIMER_CANCEL_GROUP:

        IF_DEBUG(DLC) {
            DPUT("LLC_DIR_TIMER_CANCEL_GROUP\n");
        }

        //
        // no parameter table
        //

        break;

    case LLC_DIR_TIMER_SET:

        IF_DEBUG(DLC) {
            DPUT("LLC_DIR_TIMER_SET\n");
        }

        //
        // no parameter table
        //

        //
        // Debug code is too slow - commands keep timing out. Multiply the
        // timer value by 2 to give us a chance!
        //

        IF_DEBUG(DOUBLE_TICKS) {
            pDosCcb->u.dir.usParameter0 *= 2;
        }

        break;

    case LLC_DLC_CLOSE_SAP:

        IF_DEBUG(DLC) {
            DPUT("LLC_DLC_CLOSE_SAP\n");
        }

        //
        // no parameter table
        //

        break;

    case LLC_DLC_CLOSE_STATION:

        IF_DEBUG(DLC) {
            DPUT("LLC_DLC_CLOSE_STATION\n");
        }

        //
        // no parameter table
        //

        break;

    case LLC_DLC_CONNECT_STATION:

        IF_DEBUG(DLC) {
            DPUT("LLC_DLC_CONNECT_STATION\n");
        }

        pParms->DlcConnectStation.pRoutingInfo = DOS_PTR_TO_FLAT(pParms->DlcConnectStation.pRoutingInfo);
        break;

    case LLC_DLC_FLOW_CONTROL:

        IF_DEBUG(DLC) {
            DPUT("LLC_DLC_FLOW_CONTROL\n");
        }

        //
        // if we are resetting the local-busy(buffer) state then we clear the
        // emulated state. When all deferred I-Frames are indicated to the app
        // the real local-busy(buffer) state will be reset in the driver
        //
        // If we don't think the indicated link station is in emulated local
        // busy(buffer) state then let the driver handle the request. If we
        // reset the emulated state then return success
        //

        if ((LOBYTE(pDosCcb->u.dlc.usParameter) & LLC_RESET_LOCAL_BUSY_BUFFER) == LLC_RESET_LOCAL_BUSY_BUFFER) {
            if (ResetEmulatedLocalBusyState(adapter, pDosCcb->u.dlc.usStationId, LLC_DLC_FLOW_CONTROL)) {
                return LLC_STATUS_SUCCESS;
            } else {

                IF_DEBUG(DLC) {
                    DPUT2("MapDosCommandsToNt: ERROR: Adapter %d StationId %04x not in local-busy(buffer) state\n",
                          adapter,
                          pDosCcb->u.dlc.usStationId
                          );
                }

                return LLC_STATUS_SUCCESS;
            }
        }

        //
        // let AcsLan/driver handle any other type of flow control request
        //

        break;

    case LLC_DLC_MODIFY:

        IF_DEBUG(DLC) {
            DPUT("LLC_DLC_MODIFY\n");
        }

        pParms->DlcModify.pGroupList = DOS_PTR_TO_FLAT(pParms->DlcModify.pGroupList);
        break;

    case LLC_DLC_OPEN_SAP:

        IF_DEBUG(DLC) {
            DPUT("LLC_DLC_OPEN_SAP\n");
        }

        //
        // convert segmented group list pointer to flat-32
        //

        pParms->DlcOpenSap.pGroupList = DOS_PTR_TO_FLAT(pParms->DlcOpenSap.pGroupList);

        //
        // Initialize the DOS DLC buffer pool for the SAP station. If this fails
        // return error immediately else call the NT driver to create the SAP
        // proper. If that fails, then the buffer pool created here will be
        // destroyed
        //

        Status = CreateBufferPool(
                    POOL_INDEX_FROM_SAP(pParms->DosDlcOpenSap.uchSapValue, adapter),
                    pParms->DosDlcOpenSap.dpPoolAddress,
                    pParms->DosDlcOpenSap.cPoolBlocks,
                    pParms->DosDlcOpenSap.usBufferSize
                    );
        if (Status != LLC_STATUS_SUCCESS) {

            IF_DEBUG(DLC) {
                DPUT1("MapDosCommandsToNt: Couldn't create buffer pool for DLC.OPEN.SAP (%x)\n", Status);
            }

            return Status;
        }

        //
        // trim the timer parameters to the range expected by the DLC driver
        //

        if (pParms->DlcOpenSap.uchT1 > 10) {
            pParms->DlcOpenSap.uchT1 = 10;
        }
        if (pParms->DlcOpenSap.uchT2 > 10) {
            pParms->DlcOpenSap.uchT2 = 10;
        }
        if (pParms->DlcOpenSap.uchTi > 10) {
            pParms->DlcOpenSap.uchTi = 10;
        }
        break;

    case LLC_DLC_OPEN_STATION:

        IF_DEBUG(DLC) {
            DPUT("LLC_DLC_OPEN_STATION\n");
        }

        pParms->DlcOpenStation.pRemoteNodeAddress = DOS_PTR_TO_FLAT(pParms->DlcOpenStation.pRemoteNodeAddress);
        break;

    case LLC_DLC_REALLOCATE_STATIONS:

        IF_DEBUG(DLC) {
            DPUT("LLC_DLC_REALLOCATE_STATIONS\n");
        }

        break;

    case LLC_DLC_RESET:

        IF_DEBUG(DLC) {
            DPUT("LLC_DLC_RESET\n");
        }

        //
        // no parameter table
        //

        break;

    case LLC_DLC_STATISTICS:

        IF_DEBUG(DLC) {
            DPUT("LLC_DLC_STATISTICS\n");
        }

        pParms->DlcStatistics.pLogBuf = DOS_PTR_TO_FLAT(pParms->DlcStatistics.pLogBuf);
        break;


    //
    // RECEIVE processing
    //

    case LLC_RECEIVE:

        IF_DEBUG(DLC) {
            DPUT("LLC_RECEIVE\n");
        }

        //
        // we have to replace the DOS RECEIVE with an NT RECEIVE for 2 reasons:
        // (i) NT assumes an NT RECEIVE parameter table which is longer than
        // the DOS RECEIVE parameter table: if we send the DOS pointers through
        // NT may write RECEIVE parameter information where we don't want it;
        // (ii) NT will complete the RECEIVE with NT buffers which we need to
        // convert to DOS buffers anyway in the event completion processing
        //
        // NOTE: we no longer chain receive frames on the SAP since this doesn't
        // really improve performance because we generate the same number of VDM
        // interrupts if we don't chain frames, and it just serves to complicate
        // completion event processing
        //

        pNewCcb = (PLLC_CCB)LocalAlloc(LMEM_FIXED,
                                       sizeof(LLC_CCB)
                                       + sizeof(LLC_DOS_RECEIVE_PARMS_EX)
                                       );
        if (pNewCcb == NULL) {
            return LLC_STATUS_NO_MEMORY;
        } else {

            IF_DEBUG(DLC) {
                DPUT1("VrDlc5cHandler: allocated Extended RECEIVE+parms @ %08x\n", pNewCcb);
            }

        }
        RtlCopyMemory(pNewCcb, pDosCcb, sizeof(LLC_DOS_CCB));
        RtlCopyMemory((PVOID)(pNewCcb + 1), (PVOID)pParms, sizeof(LLC_DOS_RECEIVE_PARMS));
        pNewCcb->hCompletionEvent = NULL;
        pNewCcb->uchReserved2 = 0;
        pNewCcb->uchReadFlag = 0;
        pNewCcb->usReserved3 = 0;
        pDosCcb = pNewCcb;
        pNtParms = (PLLC_PARMS)(pNewCcb + 1);
        pDosCcb->u.pParameterTable = pNtParms;
        ((PLLC_DOS_RECEIVE_PARMS_EX)pNtParms)->dpOriginalCcbAddress = dpOriginalCcbAddress;
        ((PLLC_DOS_RECEIVE_PARMS_EX)pNtParms)->dpCompletionFlag = 0;
        dpOriginalCcbAddress = (DOS_ADDRESS)pOutputCcb = (DOS_ADDRESS)pDosCcb;

        //
        // point the notification flag at the extended RECEIVE CCB. This is how
        // we get back to the extended RECEIVE CCB when a READ completes with a
        // receive event. From this CCB pointer, we can find our way to the
        // extended parameter table and from there the original DOS CCB address
        // and from there the original DOS RECEIVE parameter table
        //

        pNtParms->Receive.ulReceiveFlag = (DWORD)dpOriginalCcbAddress;

        //
        // uchRcvReadOption of 0x00 means DO NOT chain received frames. DOS DLC
        // cannot handle more than 1 frame at a time
        //

        pNtParms->Receive.uchRcvReadOption = 0;

        //
        // indicate, using LLC_DOS_SPECIAL_COMMAND as the completion flags, that
        // this RECEIVE CCB & parameter table were allocated on behalf of the
        // VDM, in this emulator, and should be freed when the command completes.
        // This also indicates that the parameter table is the extended receive
        // parameter table and as such contains the address of the original DOS
        // CCB which we must complete with the same information which completes
        // the NT RECEIVE we are about to submit
        //

        pNewCcb->ulCompletionFlag = LLC_DOS_SPECIAL_COMMAND;

#if DBG

        //
        // clear out the first-buffer field to stop the debug code displaying
        // a ton of garbage if the field is left uninitialized
        //

        WRITE_DWORD(&pOutputCcb->u.pParms->DosReceive.pFirstBuffer, 0);
        pNtParms->Receive.pFirstBuffer = NULL;
#endif

        break;

    case LLC_RECEIVE_CANCEL:

        IF_DEBUG(DLC) {
            DPUT("LLC_RECEIVE_CANCEL\n");
        }

        break;

    case LLC_RECEIVE_MODIFY:

        IF_DEBUG(DLC) {
            DPUT("LLC_RECEIVE_MODIFY\n");
        }

        break;


    //
    // TRANSMIT processing. All transmit commands (7 flavours) are collapsed
    // into the new TRANSMIT command
    //

    case LLC_TRANSMIT_DIR_FRAME:

        IF_DEBUG(DLC) {
            DPUT("LLC_TRANSMIT_DIR_FRAME\n");
        }

        FrameType = LLC_DIRECT_TRANSMIT;
        goto TransmitHandling;

    case LLC_TRANSMIT_I_FRAME:

        IF_DEBUG(DLC) {
            DPUT("LLC_TRANSMIT_I_FRAME\n");
        }

        FrameType = LLC_I_FRAME;
        goto TransmitHandling;

    case LLC_TRANSMIT_TEST_CMD:

        IF_DEBUG(DLC) {
            DPUT("LLC_TRANSMIT_TEST_CMD\n");
        }

        FrameType = LLC_TEST_COMMAND_POLL;
        goto TransmitHandling;

    case LLC_TRANSMIT_UI_FRAME:

        IF_DEBUG(DLC) {
            DPUT("LLC_TRANSMIT_UI_FRAME\n");
        }

        FrameType = LLC_UI_FRAME;
        goto TransmitHandling;

    case LLC_TRANSMIT_XID_CMD:

        IF_DEBUG(DLC) {
            DPUT("LLC_TRANSMIT_XID_CMD\n");
        }

        FrameType = LLC_XID_COMMAND_POLL;
        goto TransmitHandling;

    case LLC_TRANSMIT_XID_RESP_FINAL:

        IF_DEBUG(DLC) {
            DPUT("LLC_TRANSMIT_XID_RESP_FINAL\n");
        }

        FrameType = LLC_XID_RESPONSE_FINAL;
        goto TransmitHandling;

    case LLC_TRANSMIT_XID_RESP_NOT_FINAL:

        IF_DEBUG(DLC) {
            DPUT("LLC_TRANSMIT_XID_RESP_NOT_FINAL\n");
        }

        FrameType = LLC_XID_RESPONSE_NOT_FINAL;

TransmitHandling:

        //
        // Copy the DOS CCB to the input buffer, save the original DOS address
        // of the CCB and fix the parameter table address (to a flat address)
        // Copy the link list headers to the descriptor array and build NT CCB
        //

        WRITE_DWORD(&pOutputCcb->pNext, dpOriginalCcbAddress);
        RtlCopyMemory((PCHAR)&NtDlcParms.Async.Ccb, (PCHAR)pOutputCcb, sizeof(NT_DLC_CCB));
        NtDlcParms.Async.Ccb.u.pParameterTable = DOS_PTR_TO_FLAT(NtDlcParms.Async.Ccb.u.pParameterTable);
        NtDlcParms.Async.Parms.Transmit.StationId = pParms->Transmit.usStationId;
        NtDlcParms.Async.Parms.Transmit.RemoteSap = pParms->Transmit.uchRemoteSap;
        NtDlcParms.Async.Parms.Transmit.XmitReadOption = LLC_CHAIN_XMIT_COMMANDS_ON_SAP;
        NtDlcParms.Async.Parms.Transmit.FrameType = FrameType;

        cElement = 0;

        if (pParms->Transmit.pXmitQueue1) {
            Status = CopyDosBuffersToDescriptorArray(
                        NtDlcParms.Async.Parms.Transmit.XmitBuffer,
                        (PLLC_XMIT_BUFFER)pParms->Transmit.pXmitQueue1,
                        &cElement
                        );
            if (Status != LLC_STATUS_SUCCESS) {
                return Status;
            }
        }

        if (pParms->Transmit.pXmitQueue2) {
            Status = CopyDosBuffersToDescriptorArray(
                        NtDlcParms.Async.Parms.Transmit.XmitBuffer,
                        (PLLC_XMIT_BUFFER)pParms->Transmit.pXmitQueue2,
                        &cElement
                        );
            if (Status != LLC_STATUS_SUCCESS) {
                return Status;
            }
        }

        if (pParms->Transmit.cbBuffer1) {
            if (cElement == MAX_TRANSMIT_SEGMENTS) {
                return LLC_STATUS_TRANSMIT_ERROR;
            }

            NtDlcParms.Async.Parms.Transmit.XmitBuffer[cElement].pBuffer = DOS_PTR_TO_FLAT(pParms->Transmit.pBuffer1);
            NtDlcParms.Async.Parms.Transmit.XmitBuffer[cElement].cbBuffer = pParms->Transmit.cbBuffer1;
            NtDlcParms.Async.Parms.Transmit.XmitBuffer[cElement].boolFreeBuffer = FALSE;
            NtDlcParms.Async.Parms.Transmit.XmitBuffer[cElement].eSegmentType = LLC_NEXT_DATA_SEGMENT;
            cElement++;
        }

        if (pParms->Transmit.cbBuffer2) {
            if (cElement == MAX_TRANSMIT_SEGMENTS) {
                return LLC_STATUS_TRANSMIT_ERROR;
            }

            NtDlcParms.Async.Parms.Transmit.XmitBuffer[cElement].cbBuffer = pParms->Transmit.cbBuffer2;
            NtDlcParms.Async.Parms.Transmit.XmitBuffer[cElement].pBuffer = DOS_PTR_TO_FLAT(pParms->Transmit.pBuffer2);
            NtDlcParms.Async.Parms.Transmit.XmitBuffer[cElement].boolFreeBuffer = FALSE;
            NtDlcParms.Async.Parms.Transmit.XmitBuffer[cElement].eSegmentType = LLC_NEXT_DATA_SEGMENT;
            cElement++;
        }

        NtDlcParms.Async.Parms.Transmit.XmitBuffer[0].eSegmentType = LLC_FIRST_DATA_SEGMENT;
        NtDlcParms.Async.Parms.Transmit.XmitBufferCount = cElement;

        InputBufferSize = sizeof(LLC_TRANSMIT_DESCRIPTOR) * cElement
                        + sizeof(NT_DLC_TRANSMIT_PARMS)
                        + sizeof(NT_DLC_CCB)
                        - sizeof(LLC_TRANSMIT_DESCRIPTOR);

        //
        // We don't need return FrameCopied status, when sending
        // I-frames.  We save an extra memory locking, when we use
        // TRANSMIT2 with the I- frames.
        //

        return lpDlcCallDriver((DWORD)adapter,
                               //(FrameType == LLC_I_FRAME)
                               // ? IOCTL_DLC_TRANSMIT2
                               // : IOCTL_DLC_TRANSMIT,
                               IOCTL_DLC_TRANSMIT,
                               &NtDlcParms,
                               InputBufferSize,
                               pOutputCcb,
                               sizeof(NT_DLC_CCB_OUTPUT)
                               );
    }

    //
    // Call the common DLC API entry point for DOS and Windows/Nt
    //

    Status = DlcCallWorker((PLLC_CCB)pDosCcb,    // aligned input CCB pointer
                           (PLLC_CCB)dpOriginalCcbAddress,
                           (PLLC_CCB)pOutputCcb  // possibly unaligned output CCB pointer
                           );

    IF_DEBUG(DLC) {
        DPUT2("MapDosCommandsToNt: NtAcsLan returns %#x (%d)\n", Status, Status);
    }

    switch (pDosCcb->uchDlcCommand) {
    case LLC_DIR_CLOSE_ADAPTER:
    case LLC_DIR_INITIALIZE:
        OpenedAdapters--;

        //
        // NtAcsLan converts DIR.INITIALIZE to DIR.CLOSE.ADAPTER. The former
        // completes "in the workstation", whereas the latter completes
        // asynchronously. Interpret LLC_STATUS_PENDING as LLC_STATUS_SUCCESS
        // in this case, otherwise we may not fully uninitialize the adapter
        //

        if (Status == LLC_STATUS_SUCCESS || Status == LLC_STATUS_PENDING) {

            //
            // We may free all virtual memory in NT buffer pool
            //

            Adapters[adapter].IsOpen = FALSE;
            LocalFree(Adapters[adapter].BufferPool);

            IF_DEBUG(DLC_ALLOC) {
                DPUT1("FREE: freed block @ %x\n", Adapters[adapter].BufferPool);
            }

            //
            // Delete all DOS buffer pools
            //

            for (i = 0; i <= 0xfe00; i += 0x0200) {
                DeleteBufferPool(GET_POOL_INDEX(adapter, i));
            }

            //
            // closing the adapter also closed the direct station
            //

            Adapters[adapter].DirectStationOpen = FALSE;

            //
            // clear the stored ADAPTER_PARMS and DLC_PARMS
            //

            RtlZeroMemory(&Adapters[adapter].AdapterParms, sizeof(ADAPTER_PARMS));
            RtlZeroMemory(&Adapters[adapter].DlcParms, sizeof(DLC_PARMS));

            if (pDosCcb->uchDlcCommand == LLC_DIR_INITIALIZE) {
                Status = LLC_STATUS_SUCCESS;
            }
        }
        break;

    case LLC_DIR_OPEN_ADAPTER:
        if (Status != LLC_STATUS_SUCCESS) {
            break;
        }

        //
        // Initialize the adapter support software
        //

        Status = InitializeAdapterSupport(adapter, pDirectParms);

        //
        // if we allocated the direct station buffer ok then perform the
        // rest of the open request - open the direct station, add any
        // group or functional addresses specified and set the ADAPTER_PARMS
        // and DLC_PARMS default values in the DOS_ADAPTER structure
        //

        if (Status == LLC_STATUS_SUCCESS) {

            //
            // open the direct station
            //

            Status = OpenDirectStation(adapter);
            if (Status == LLC_STATUS_SUCCESS) {

                //
                // add the group address
                //

                if (groupAddress) {
                    Status = LlcCommand(adapter,
                                        LLC_DIR_SET_GROUP_ADDRESS,
                                        groupAddress
                                        );
                } else IF_DEBUG(DLC) {
                    DPUT1("Error: couldn't set group address: %02x\n", Status);
                }

                if (Status == LLC_STATUS_SUCCESS) {

                    //
                    // add the functional address
                    //

                    if (functionalAddress) {
                        Status = LlcCommand(adapter,
                                            LLC_DIR_SET_FUNCTIONAL_ADDRESS,
                                            functionalAddress
                                            );
                    }
                } else IF_DEBUG(DLC) {
                    DPUT1("Error: couldn't set functional address: %02x\n", Status);
                }

            } else IF_DEBUG(DLC) {
                DPUT1("Error: could open Direct Station: %02x\n", Status);
            }
        }

        //
        // copy the returned default information to the adapter structure if
        // we successfully managed to open the direct station and add the
        // group and functional addresses (if specified)
        //

        if (Status == LLC_STATUS_SUCCESS) {
            RtlCopyMemory(&Adapters[adapter].AdapterParms,
                          pParms->DirOpenAdapter.pAdapterParms,
                          sizeof(ADAPTER_PARMS)
                          );
            if (pParms->DirOpenAdapter.pDlcParms) {
                RtlCopyMemory(&Adapters[adapter].DlcParms,
                              pParms->DirOpenAdapter.pDlcParms,
                              sizeof(DLC_PARMS)
                              );
            }
        } else {

            //
            // yoiks! something failed - close the direct station (if its
            // open, close the adapter and fail the request)
            //

            if (Adapters[adapter].DirectStationOpen) {
                CloseDirectStation(adapter);
            }
            CloseAdapter(adapter);
        }
        break;

    case LLC_DLC_CLOSE_SAP:
    case LLC_DLC_CLOSE_STATION:

        //
        // Delete the buffer pools of the closed or closing station.
        // It does not matter, if the close operation is still pending,
        // because the pending operations should always succeed
        //

        if (Status == LLC_STATUS_SUCCESS || Status == LLC_STATUS_PENDING) {

            DeleteBufferPool(GET_POOL_INDEX(adapter, pDosCcb->u.dlc.usStationId));

            //
            // DLC.SYS returns a pointer to the NT RECEIVE CCB for this SAP.
            // Change the pointer to the DOS RECEIVE CCB
            //

            if (Status == LLC_STATUS_SUCCESS || !pDosCcb->ulCompletionFlag) {

                PLLC_CCB pNtReceive;
                PLLC_DOS_RECEIVE_PARMS_EX pNtReceiveParms;

                pNtReceive = (PLLC_CCB)READ_DWORD(&pOutputCcb->pNext);
                if (pNtReceive) {
                    pNtReceiveParms = (PLLC_DOS_RECEIVE_PARMS_EX)(pNtReceive->u.pParameterTable);
                    WRITE_FAR_POINTER(&pOutputCcb->pNext, pNtReceiveParms->dpOriginalCcbAddress);

                    //
                    // free the NT RECEIVE CCB we allocated (see LLC_RECEIVE above)
                    //

                    ASSERT(pNtReceive->ulCompletionFlag == LLC_DOS_SPECIAL_COMMAND);
                    LocalFree((HLOCAL)pNtReceive);

                    IF_DEBUG(DLC) {
                        DPUT1("VrDlc5cHandler: freed Extended RECEIVE+parms @ %08x\n", pNtReceive);
                    }

                }
            }
        }
        break;

    case LLC_DLC_OPEN_SAP:

        //
        // delete the buffer pool, if the open SAP command failed
        //

        if (Status != LLC_STATUS_SUCCESS) {
            DeleteBufferPool(GET_POOL_INDEX(adapter, pParms->DlcOpenSap.usStationId));
        } else {

            //
            // record the DLC status change appendage for this SAP
            //

            Adapters[ adapter ].DlcStatusChangeAppendage

                [ SAP_ID(pParms->DlcOpenSap.usStationId) ]

                    = pParms->DlcOpenSap.DlcStatusFlags;

            //
            // and user value
            //

            Adapters[ adapter ].UserStatusValue

                [ SAP_ID(pParms->DlcOpenSap.usStationId) ]

                    = pParms->DlcOpenSap.usUserStatValue;
        }
        break;

    case LLC_DLC_RESET:

        //
        // Delete the reset sap buffer pool,
        // or all sap buffer pools.  We don't need to care about
        // the possible error codes, because this can fail only
        // if the given sap station does not exist any more =>
        // it does not matter, if we reset it again.
        //

        if (pDosCcb->u.dlc.usStationId != 0) {
            DeleteBufferPool(GET_POOL_INDEX(adapter, pDosCcb->u.dlc.usStationId));
        } else {

            int sapNumber;

            //
            // Close all SAP stations (0x0200 - 0xfe00). SAP number goes up in
            // increments of 2 since bit 0 is ignored (ie SAP 2 == SAP 3 etc)
            //

            for (sapNumber = 2; sapNumber <= 0xfe; sapNumber += 2) {
                DeleteBufferPool(POOL_INDEX_FROM_SAP(sapNumber, adapter));
            }
        }
        break;
    }
    return Status;
}


VOID
CompleteCcbProcessing(
    IN LLC_STATUS Status,
    IN LLC_DOS_CCB UNALIGNED * pCcb,
    IN PLLC_PARMS pNtParms
    )

/*++

Routine Description:

    Performs any CCB completion processing. Processing can be called either
    when the CCB completes synchronously, or asynchronously. Processing is
    typically to fill in parts of the DOS CCB or parameter table

Arguments:

    Status  - of the request
    pCcb    - pointer to DOS CCB to complete (flat 32-bit pointer to DOS memory)
    pNtParms- pointer to NT parameter table

Return Value:

    None.

--*/

{
    LLC_DOS_PARMS UNALIGNED * pDosParms = READ_FAR_POINTER(&pCcb->u.pParms);
    BYTE adapter = pCcb->uchAdapterNumber;

    IF_DEBUG(DLC) {
        DPUT("CompleteCcbProcessing\n");
    }

    switch (pCcb->uchDlcCommand) {
    case LLC_DIR_OPEN_ADAPTER:

        //
        // this command is unique in that it has a parameter table which points
        // to up to 4 other parameter tables. The following values are output:
        //
        //  ADAPTER_PARMS
        //      OPEN_ERROR_CODE
        //      NODE_ADDRESS
        //
        //  DIRECT_PARMS
        //      WORK_LEN_ACT
        //
        //  DLC_PARMS
        //      None for CCB1
        //
        //  NCB_PARMS
        //      Not accessed
        //

        //
        // we only copy the info if the command succeeded (we may have garbage
        // table pointers otherwise). It is also OK to copy the information if
        // the adapter is already open and the caller requested that we pass
        // the default information back
        //

        if (Status == LLC_STATUS_SUCCESS || Status == LLC_STATUS_ADAPTER_OPEN) {

            PLLC_DOS_DIR_OPEN_ADAPTER_PARMS pOpenAdapterParms = (PLLC_DOS_DIR_OPEN_ADAPTER_PARMS)pDosParms;
            PADAPTER_PARMS pAdapterParms = READ_FAR_POINTER(&pOpenAdapterParms->pAdapterParms);
            PDIRECT_PARMS pDirectParms = READ_FAR_POINTER(&pOpenAdapterParms->pDirectParms);
            PDLC_PARMS pDlcParms = READ_FAR_POINTER(&pOpenAdapterParms->pDlcParms);

            //
            // if we got an error and the caller didn't request the original
            // open parameters, then skip out
            //

            if (Status == LLC_STATUS_ADAPTER_OPEN && !(pAdapterParms->OpenOptions & 0x200)) {
                break;
            }

            WRITE_WORD(&pAdapterParms->OpenErrorCode, pNtParms->DirOpenAdapter.pAdapterParms->usOpenErrorCode);
            RtlCopyMemory(&pAdapterParms->NodeAddress,
                          pNtParms->DirOpenAdapter.pAdapterParms->auchNodeAddress,
                          sizeof(pAdapterParms->NodeAddress)
                          );

            //
            // direct parms are not returned from NT DLC, so we just copy the
            // requested work area size to the actual
            //

            WRITE_WORD(&pDirectParms->AdapterWorkAreaActual,
                        READ_WORD(&pDirectParms->AdapterWorkAreaRequested)
                        );

            //
            // copy the entire DLC_PARMS structure from the DOS_ADAPTER structure
            //

            if (pDlcParms) {
                RtlCopyMemory(pDlcParms, &Adapters[adapter].DlcParms, sizeof(*pDlcParms));
            }
        }
        break;

    case LLC_DIR_STATUS:

        //
        // copy the common areas from the 32-bit parameter table to 16-bit table
        // This copies up to the adapter parameters address
        //

        RtlCopyMemory(pDosParms, pNtParms, (DWORD)&((PDOS_DIR_STATUS_PARMS)0)->dpAdapterParmsAddr);

        //
        // fill in the other fields as best we can
        //

        RtlZeroMemory(pDosParms->DosDirStatus.auchMicroCodeLevel,
                      sizeof(pDosParms->DosDirStatus.auchMicroCodeLevel)
                      );
        WRITE_DWORD(&pDosParms->DosDirStatus.dpAdapterMacAddr, 0);
        WRITE_DWORD(&pDosParms->DosDirStatus.dpTimerTick, 0);
        WRITE_WORD(&pDosParms->DosDirStatus.usLastNetworkStatus,
                    Adapters[adapter].LastNetworkStatusChange
                    );

        //
        // If the app has requested we return the extended parameter table, then
        // fill it in with reasonable values if we can. There is one table per
        // adapter, statically allocated in the real-mode redir TSR
        //

        if (pDosParms->DosDirStatus.uchAdapterConfig & 0x20) {

            //
            // Ethernet type uses different bit in DOS and Nt (or OS/2)
            //

            lpVdmWindow->aExtendedStatus[adapter].cbSize = sizeof(EXTENDED_STATUS_PARMS);

            //
            // if adapter type as reported by NtAcsLan is Ethernet (0x100), set
            // adapter type in extended status table to Ethernet (0x10), else
            // record whatever NtAcsLan gave us
            //

            if (pNtParms->DirStatus.usAdapterType & 0x100) {
                WRITE_WORD(&lpVdmWindow->aExtendedStatus[adapter].wAdapterType,
                           0x0010
                           );
                lpVdmWindow->aExtendedStatus[adapter].cbPageFrameSize = 0;
                WRITE_WORD(&lpVdmWindow->aExtendedStatus[adapter].wCurrentFrameSize,
                           0
                           );
                WRITE_WORD(&lpVdmWindow->aExtendedStatus[adapter].wMaxFrameSize,
                           0
                           );
            } else {
                WRITE_WORD(&lpVdmWindow->aExtendedStatus[adapter].wAdapterType,
                           pNtParms->DirStatus.usAdapterType
                           );

                //
                // set the TR page frame size (KBytes)
                //

                lpVdmWindow->aExtendedStatus[adapter].cbPageFrameSize = 16;

                //
                // set the current and maximum DHB sizes for TR cards
                //

                WRITE_WORD(&lpVdmWindow->aExtendedStatus[adapter].wCurrentFrameSize,
                           (WORD)pNtParms->DirStatus.ulMaxFrameLength
                           );
                WRITE_WORD(&lpVdmWindow->aExtendedStatus[adapter].wMaxFrameSize,
                           (WORD)pNtParms->DirStatus.ulMaxFrameLength
                           );
            }

            //
            // record the address of the extended parameter table in the
            // DIR.STATUS parameter table
            //

            WRITE_DWORD(&pDosParms->DosDirStatus.dpExtendedParms,
                        NEW_DOS_ADDRESS(dpVdmWindow, &lpVdmWindow->aExtendedStatus[adapter])
                        );
        } else {

            //
            // no extended parameters requested
            //

            WRITE_DWORD(&pDosParms->DosDirStatus.dpExtendedParms, 0);
        }

        //
        // return the tick counter. We don't currently update the tick counter
        //

        WRITE_DWORD(&pDosParms->DosDirStatus.dpTimerTick,
                    NEW_DOS_ADDRESS(dpVdmWindow, &lpVdmWindow->dwDlcTimerTick)
                    );

        //
        // always return a pointer to the extended adapter parameter table we
        // now keep in DOS memory. We currently always zero this table. It
        // would normally be maintained by the adapter (MAC) software
        //

        WRITE_DWORD(&pDosParms->DosDirStatus.dpAdapterParmsAddr,
                    NEW_DOS_ADDRESS(dpVdmWindow, &lpVdmWindow->AdapterStatusParms[adapter])
                    );
        RtlZeroMemory(&lpVdmWindow->AdapterStatusParms[adapter],
                      sizeof(lpVdmWindow->AdapterStatusParms[adapter])
                      );
        break;

    case LLC_DLC_OPEN_SAP:

        //
        // STATION_ID is only output value
        //

        WRITE_WORD(&pDosParms->DlcOpenSap.usStationId, pNtParms->DlcOpenSap.usStationId);
        break;

    case LLC_DLC_OPEN_STATION:

        //
        // LINK_STATION_ID is only output value
        //

        WRITE_WORD(&pDosParms->DlcOpenStation.usLinkStationId, pNtParms->DlcOpenStation.usLinkStationId);
        break;

    case LLC_DLC_STATISTICS:
        break;
    }
}


LLC_STATUS
InitializeAdapterSupport(
    IN UCHAR AdapterNumber,
    IN DOS_DLC_DIRECT_PARMS UNALIGNED * pDirectParms OPTIONAL
    )

/*++

Routine Description:

    The function initializes the buffer pool for the new adapter opened by
    DOS DLC

Arguments:

    AdapterNumber   - which adapter to initialize the buffer pool for
    pDirectParms    - Direct station parameter table, not used in NT, but
                      optional in DOS

Return Value:

    LLC_STATUS
        LLC_NO_RESOURCES

--*/

{
    LLC_STATUS Status;
    HANDLE hBufferPool;

    IF_DEBUG(DLC) {
        DPUT("InitializeAdapterSupport\n");
    }

    //
    // Check if the global DLL initialization has already been done. This is not
    // done in global DLL init because there is no reason to start an extra
    // thread if DLC is not used. If this succeeds then the asynchronous event
    // handler thread will be waiting on a list of 2 events - one for each
    // adapter. We need to submit a READ CCB for this adapter
    //

    Status = VrDlcInit();
    if (Status != LLC_STATUS_SUCCESS) {
        return Status;
    } else if (InitiateRead(AdapterNumber, &Status) == NULL) {
        return Status;
    }

    OpenedAdapters++;

    //
    // mark the adapter as being opened and get the media type/class
    //

    Adapters[AdapterNumber].IsOpen = TRUE;
    Adapters[AdapterNumber].AdapterType = GetAdapterType(AdapterNumber);

    //
    // Create the DLC buffer pool for the new adapter. DLC driver will
    // deallocate the buffer pool in the DIR.CLOSE.ADAPTER or when
    // the MVDM process makes a process exit
    //

    Adapters[AdapterNumber].BufferPool = (PVOID)LocalAlloc(LMEM_FIXED, DOS_DLC_BUFFER_POOL_SIZE);
    if (Adapters[AdapterNumber].BufferPool == NULL) {
        Status = LLC_STATUS_NO_MEMORY;
        goto ErrorHandler;
    }

    Status = BufferCreate(AdapterNumber,
                          Adapters[AdapterNumber].BufferPool,
                          DOS_DLC_BUFFER_POOL_SIZE,
                          DOS_DLC_MIN_FREE_THRESHOLD,
                          &hBufferPool
                          );
    if (Status != LLC_STATUS_SUCCESS) {
        goto ErrorHandler;
    }

    if (ARGUMENT_PRESENT(pDirectParms)) {

        //
        // create a buffer pool for the direct station (SAP 0). This allows
        // us to receive MAC and non-MAC frames sent to the direct station
        // without having to purposefully allocate a buffer
        //

        Status = CreateBufferPool(GET_POOL_INDEX(AdapterNumber, 0),
                                  pDirectParms->dpPoolAddress,
                                  pDirectParms->cPoolBlocks,
                                  pDirectParms->usBufferSize
                                  );
        if (Status != LLC_STATUS_SUCCESS) {
            goto ErrorHandler;
        }

        SaveExceptions(AdapterNumber,
                       (LPDWORD)&pDirectParms->dpAdapterCheckExit
                       );
        Status = SetExceptionFlags(AdapterNumber,
                                   (DWORD)pDirectParms->dpAdapterCheckExit,
                                   (DWORD)pDirectParms->dpNetworkStatusExit,
                                   (DWORD)pDirectParms->dpPcErrorExit,
                                   0
                                   );
        if (Status != LLC_STATUS_SUCCESS) {
            goto ErrorHandler;
        }
    }

    IF_DEBUG(DLC) {
        DPUT("InitializeAdapterSupport: returning success\n");
    }

    return LLC_STATUS_SUCCESS;

ErrorHandler:

    //
    // The open failed. We must close the adapter, but we don't care about the
    // result. This must succeed
    //

    if (Adapters[AdapterNumber].BufferPool != NULL) {
        LocalFree(Adapters[AdapterNumber].BufferPool);

        IF_DEBUG(DLC_ALLOC) {
            DPUT1("FREE: freed block @ %x\n", Adapters[AdapterNumber].BufferPool);
        }

    }

    CloseAdapter(AdapterNumber);
    Adapters[AdapterNumber].IsOpen = FALSE;

    //
    // this is probably not the right error code to return under these
    // circumstances, but we'll keep it until something better comes along
    //

    IF_DEBUG(DLC) {
        DPUT("InitializeAdapterSupport: returning FAILURE\n");
    }

    return LLC_STATUS_ADAPTER_NOT_INITIALIZED;
}


VOID
SaveExceptions(
    IN UCHAR AdapterNumber,
    IN LPDWORD pulExceptionFlags
    )

/*++

Routine Description:

    Procedure saves the current exception handlers
    and copies new current values on the old ones.

Arguments:

    IN UCHAR AdapterNumber - current adapter
    IN LPDWORD pulExceptionFlags - 3 dos exception handlers in the arrays

Return Value:

    None

--*/

{
    IF_DEBUG(DLC) {
        DPUT("SaveExceptions\n");
    }

    RtlCopyMemory(Adapters[AdapterNumber].PreviousExceptionHandlers,
                  Adapters[AdapterNumber].CurrentExceptionHandlers,
                  sizeof(Adapters[AdapterNumber].PreviousExceptionHandlers)
                  );
    RtlCopyMemory(Adapters[AdapterNumber].CurrentExceptionHandlers,
                  pulExceptionFlags,
                  sizeof(Adapters[AdapterNumber].CurrentExceptionHandlers)
                  );
}


LPDWORD
RestoreExceptions(
    IN UCHAR AdapterNumber
    )

/*++

Routine Description:

    Procedure restores the previous exception handlers
    and returns the their address.

Arguments:

    IN UCHAR AdapterNumber - current adapter

Return Value:

    None

--*/

{
    IF_DEBUG(DLC) {
        DPUT("RestoreExceptions\n");
    }

    RtlCopyMemory(Adapters[AdapterNumber].CurrentExceptionHandlers,
                  Adapters[AdapterNumber].PreviousExceptionHandlers,
                  sizeof(Adapters[AdapterNumber].CurrentExceptionHandlers)
                  );
    return Adapters[AdapterNumber].CurrentExceptionHandlers;
}


LLC_STATUS
CopyDosBuffersToDescriptorArray(
    IN OUT PLLC_TRANSMIT_DESCRIPTOR pDescriptors,
    IN PLLC_XMIT_BUFFER pDlcBufferQueue,
    IN OUT LPDWORD pIndex
    )

/*++

Routine Description:

    The routine copies DOS transmit buffers to a Nt Transmit
    descriptor array.  All DOS pointers must be mapped to the flat
    32-bit address space.  Any data in the parameter table may be
    unaligned.

Arguments:

    pDescriptors    - current descriptor array
    pDlcBufferQueue - DOS transmit buffer queue
    pIndex          - current index in the descriptor array

Return Value:

    LLC_STATUS

--*/

{
    PLLC_XMIT_BUFFER pBuffer;
    DWORD Index = *pIndex;
    DWORD i = 0;
    DWORD DlcStatus = LLC_STATUS_SUCCESS;
    WORD cbBuffer;

    IF_DEBUG(DLC) {
        DPUT("CopyDosBuffersToDescriptorArray\n");
    }

    while (pDlcBufferQueue) {
        pBuffer = (PLLC_XMIT_BUFFER)DOS_PTR_TO_FLAT(pDlcBufferQueue);

        //
        // Check the overflow of the internal xmit buffer in stack and
        // the loop counter, that prevents the forever loop of zero length
        // transmit buffer (the buffer chain might be circular)
        //

        if (Index >= MAX_TRANSMIT_SEGMENTS || i > 60000) {
            DlcStatus = LLC_STATUS_TRANSMIT_ERROR;
            break;
        }

        if ((cbBuffer = READ_WORD(&pBuffer->cbBuffer)) != 0) {
            pDescriptors[Index].pBuffer = (PUCHAR)(pBuffer->auchData)
                                        + READ_WORD(&pBuffer->cbUserData);
            pDescriptors[Index].cbBuffer = cbBuffer;
            pDescriptors[Index].eSegmentType = LLC_NEXT_DATA_SEGMENT;
            pDescriptors[Index].boolFreeBuffer = FALSE;

            Index++;
        }
        i++;
        pDlcBufferQueue = (PLLC_XMIT_BUFFER)READ_DWORD(&pBuffer->pNext);
    }
    *pIndex = Index;
    return DlcStatus;
}


LLC_STATUS
BufferCreate(
    IN UCHAR AdapterNumber,
    IN PVOID pVirtualMemoryBuffer,
    IN DWORD ulVirtualMemorySize,
    IN DWORD ulMinFreeSizeThreshold,
    OUT HANDLE* phBufferPoolHandle
    )

/*++

Routine Description:

    Function creates a Windows/Nt DLC buffer pool.

    THIS COMMAND COMPLETES SYNCHRONOUSLY

Arguments:

    AdapterNumber           -
    pVirtualMemoryBuffer    - pointer to a virtual memory
    ulVirtualMemorySize     - size of all available buffer pool space
    ulMinFreeSizeThreshold  - locks more pages when this is exceeded
    phBufferPoolHandle      -

Return Value:

    LLC_STATUS

--*/

{
    LLC_CCB ccb;
    LLC_BUFFER_CREATE_PARMS BufferCreate;
    LLC_STATUS status;

    IF_DEBUG(DLC) {
        DPUT("BufferCreate\n");
    }

    InitializeCcb(&ccb, AdapterNumber, LLC_BUFFER_CREATE, &BufferCreate);

    BufferCreate.pBuffer = pVirtualMemoryBuffer;
    BufferCreate.cbBufferSize = ulVirtualMemorySize;
    BufferCreate.cbMinimumSizeThreshold = ulMinFreeSizeThreshold;

    status = lpAcsLan(&ccb, NULL);
    *phBufferPoolHandle = BufferCreate.hBufferPool;

    IF_DEBUG(DLC) {
        DPUT2("BufferCreate: returning %#x (%d)\n", status, status);
    }

    return DLC_ERROR_STATUS(status, ccb.uchDlcStatus);
}


LLC_STATUS
SetExceptionFlags(
    IN UCHAR AdapterNumber,
    IN DWORD ulAdapterCheckFlag,
    IN DWORD ulNetworkStatusFlag,
    IN DWORD ulPcErrorFlag,
    IN DWORD ulSystemActionFlag
    )

/*++

Routine Description:

    Sets the new appendage addresses

    THIS COMMAND COMPLETES SYNCHRONOUSLY

Arguments:

    AdapterNumber       -
    ulAdapterCheckFlag  -
    ulNetworkStatusFlag -
    ulPcErrorFlag       -
    ulSystemActionFlag  -

Return Value:

    LLC_STATUS

--*/

{
    LLC_CCB ccb;
    LLC_STATUS status;
    LLC_DIR_SET_EFLAG_PARMS DirSetFlags;

    IF_DEBUG(DLC) {
        DPUT("SetExceptionFlags\n");
    }

    InitializeCcb(&ccb, AdapterNumber, LLC_DIR_SET_EXCEPTION_FLAGS, &DirSetFlags);

    DirSetFlags.ulAdapterCheckFlag = ulAdapterCheckFlag;
    DirSetFlags.ulNetworkStatusFlag = ulNetworkStatusFlag;
    DirSetFlags.ulPcErrorFlag = ulPcErrorFlag;
    DirSetFlags.ulSystemActionFlag = ulSystemActionFlag;

    status = lpAcsLan(&ccb, NULL);
    return DLC_ERROR_STATUS(status, ccb.uchDlcStatus);
}


LLC_STATUS
LlcCommand(
    IN UCHAR AdapterNumber,
    IN UCHAR Command,
    IN DWORD Parameter
    )

/*++

Routine Description:

    Calls the ACSLAN DLL to perform a DLC request which takes no parameter
    table, but which takes parameters in byte, word or dword form in the CCB

    COMMANDS USING THIS ROUTINE MUST COMPLETE SYNCHRONOUSLY

Arguments:

    AdapterNumber   - which adapter to perform command for
    Command         - which DLC command to perform. Currently, commands are:
                        DIR.SET.GROUP.ADDRESS
                        DIR.SET.FUNCTIONAL.ADDRESS
                        DLC.FLOW.CONTROL
                        RECEIVE.CANCEL
    Parameter       - the associated command

Return Value:

    DWORD

--*/

{
    LLC_CCB ccb;
    LLC_STATUS status;

    IF_DEBUG(DLC) {
        DPUT3("LlcCommand(%d, %02x, %08x)\n", AdapterNumber, Command, Parameter);
    }

    InitializeCcb2(&ccb, AdapterNumber, Command);
    ccb.u.ulParameter = Parameter;

    status = lpAcsLan(&ccb, NULL);
    return DLC_ERROR_STATUS(status, ccb.uchDlcStatus);
}


LLC_STATUS
OpenAdapter(
    IN UCHAR AdapterNumber
    )

/*++

Routine Description:

    Opens a DLC adapter context for a Windows/Nt VDM

    THIS COMMAND COMPLETES SYNCHRONOUSLY

Arguments:

    AdapterNumber   - which adapter to open

Return Value:

    LLC_STATUS

--*/

{
    LLC_CCB Ccb;
    LLC_DIR_OPEN_ADAPTER_PARMS DirOpenAdapter;
    LLC_ADAPTER_OPEN_PARMS AdapterParms;
    LLC_EXTENDED_ADAPTER_PARMS ExtendedParms;
    LLC_DLC_PARMS DlcParms;
    LLC_STATUS status;

    IF_DEBUG(DLC) {
        DPUT1("OpenAdapter(AdapterNumber=%d)\n", AdapterNumber);
    }

    InitializeCcb(&Ccb, AdapterNumber, LLC_DIR_OPEN_ADAPTER, &DirOpenAdapter);

    DirOpenAdapter.pAdapterParms = &AdapterParms;
    DirOpenAdapter.pExtendedParms = &ExtendedParms;
    DirOpenAdapter.pDlcParms = &DlcParms;

    ExtendedParms.hBufferPool = NULL;
    ExtendedParms.pSecurityDescriptor = NULL;
    ExtendedParms.LlcEthernetType = LLC_ETHERNET_TYPE_DEFAULT;

    RtlZeroMemory(&AdapterParms, sizeof(AdapterParms));
    RtlZeroMemory(&DlcParms, sizeof(DlcParms));

    status = lpAcsLan(&Ccb, NULL);

    if (status == LLC_STATUS_SUCCESS) {

        //
        // get the adapter media type/class
        //

        Adapters[AdapterNumber].AdapterType = GetAdapterType(AdapterNumber);

        //
        // mark the adapter structure as open
        //

        Adapters[AdapterNumber].IsOpen = TRUE;

        //
        // fill in the DOS ADAPTER_PARMS and DLC_PARMS structures with any
        // returned values
        //

        RtlCopyMemory(&Adapters[AdapterNumber].AdapterParms,
                      &AdapterParms,
                      sizeof(ADAPTER_PARMS)
                      );
        RtlCopyMemory(&Adapters[AdapterNumber].DlcParms,
                      &DlcParms,
                      sizeof(DLC_PARMS)
                      );
        Adapters[AdapterNumber].DlcSpecified = TRUE;
    }

    IF_DEBUG(DLC) {
        DPUT2("OpenAdapter: returning %d (%x)\n", status, status);
    }

    return DLC_ERROR_STATUS(status, Ccb.uchDlcStatus);
}


VOID
CloseAdapter(
    IN UCHAR AdapterNumber
    )

/*++

Routine Description:

    Closes this adapter. Uses a CCB in the DOS_ADAPTER structure specifically
    for this purpose

    THIS COMMAND COMPLETES ** ASYNCHRONOUSLY **

Arguments:

    AdapterNumber   - adapter to close

Return Value:

    None.

--*/

{
    InitializeCcb2(&Adapters[AdapterNumber].AdapterCloseCcb, AdapterNumber, LLC_DIR_CLOSE_ADAPTER);
    Adapters[AdapterNumber].AdapterCloseCcb.ulCompletionFlag = VRDLC_COMMAND_COMPLETION;

#if DBG

    ASSERT(lpAcsLan(&Adapters[AdapterNumber].AdapterCloseCcb, NULL) == LLC_STATUS_SUCCESS);

#else

    lpAcsLan(&Adapters[AdapterNumber].AdapterCloseCcb, NULL);

#endif

    //
    // mark the adapter structure as being closed
    //

    Adapters[AdapterNumber].IsOpen = FALSE;
}


LLC_STATUS
OpenDirectStation(
    IN UCHAR AdapterNumber
    )

/*++

Routine Description:

    Opens the direct station for this adapter

    THIS COMMAND COMPLETES SYNCHRONOUSLY

Arguments:

    AdapterNumber   - which adapter to open direct station for

Return Value:

    LLC_STATUS

--*/

{
    LLC_CCB ccb;
    LLC_DIR_OPEN_DIRECT_PARMS DirOpenDirect;
    LLC_STATUS status;

    IF_DEBUG(DLC) {
        DPUT1("OpenDirectStation(%d)\n", AdapterNumber);
    }

    InitializeCcb(&ccb, AdapterNumber, LLC_DIR_OPEN_DIRECT, &DirOpenDirect);

    DirOpenDirect.usOpenOptions = 0;
    DirOpenDirect.usEthernetType = 0;

    status = lpAcsLan(&ccb, NULL);
    if (status == LLC_STATUS_SUCCESS) {

        //
        // mark this DOS_ADAPTER as having the direct station open
        //

        Adapters[AdapterNumber].DirectStationOpen = TRUE;
    }

    status = DLC_ERROR_STATUS(status, ccb.uchDlcStatus);

    IF_DEBUG(DLC) {
        DPUT2("OpenDirectStation: returning %d (%x)\n", status, status);
    }

    return status;
}


VOID
CloseDirectStation(
    IN UCHAR AdapterNumber
    )

/*++

Routine Description:

    Closes the direct station for this adapter. Uses a CCB in the DOS_ADAPTER
    structure specifically for this purpose

    THIS COMMAND COMPLETES ** ASYNCHRONOUSLY **

Arguments:

    AdapterNumber   - adapter to close the direct station for

Return Value:

    None.

--*/

{
    InitializeCcb2(&Adapters[AdapterNumber].DirectCloseCcb, AdapterNumber, LLC_DIR_CLOSE_DIRECT);
    Adapters[AdapterNumber].DirectCloseCcb.ulCompletionFlag = VRDLC_COMMAND_COMPLETION;

#if DBG

    ASSERT(lpAcsLan(&Adapters[AdapterNumber].DirectCloseCcb, NULL) == LLC_STATUS_SUCCESS);

#else

    lpAcsLan(&Adapters[AdapterNumber].DirectCloseCcb, NULL);

#endif

    //
    // mark the adapter structure as no longer having the direct station open
    //

    Adapters[AdapterNumber].DirectStationOpen = FALSE;
}


LLC_STATUS
BufferFree(
    IN UCHAR AdapterNumber,
    IN PVOID pFirstBuffer,
    OUT LPWORD pusBuffersLeft
    )

/*++

Routine Description:

    Frees a SAP buffer pool in the NT DLC driver

    THIS COMMAND COMPLETES SYNCHRONOUSLY

Arguments:

    AdapterNumber   -
    pFirstBuffer    -
    pusBuffersLeft  -

Return Value:

    LLC_STATUS

--*/

{
    LLC_CCB ccb;
    LLC_BUFFER_FREE_PARMS parms;
    LLC_STATUS status;

    IF_DEBUG(DLC) {
        DPUT1("BufferFree(%x)\n", pFirstBuffer);
    }

    InitializeCcb(&ccb, AdapterNumber, LLC_BUFFER_FREE, &parms);

    parms.pFirstBuffer = pFirstBuffer;

    status = lpAcsLan(&ccb, NULL);
    *pusBuffersLeft = parms.cBuffersLeft;

    return DLC_ERROR_STATUS(status, ccb.uchDlcStatus);
}


LLC_STATUS
VrDlcInit(
    VOID
    )

/*++

Routine Description:

    perform one-shot initialization:

        * clear Adapters structures

        * initialize array of buffer pool structures and initialize the buffer
          pool critical section (InitializeBufferPools in vrdlcbuf.c)

        * create all events and threads for asynchronous command completion
          processing (InitializeEventHandler in vrdlcpst.c)

        * initialize critical sections for each adapter's local-busy(buffer)
          state information

        * set the DLC initialized flag

Arguments:

    None.

Return Value:

    LLC_STATUS
        Success - LLC_STATUS_SUCCESS
                    DLC support already initialized or initialization completed
                    successfully

        Failure - LLC_STATUS_NO_MEMORY
                    failed to create the asynchronous event thread or an event
                    object

--*/

{
    static BOOLEAN VrDlcInitialized = FALSE;
    LLC_STATUS Status = LLC_STATUS_SUCCESS;

    if (!VrDlcInitialized) {

        //
        // ensure that the DOS_ADAPTER structures begin life in a known state
        //

        RtlZeroMemory(Adapters, sizeof(Adapters));

        //
        // clear out the buffer pool structures and initialize the buffer
        // pool critical section
        //

        InitializeBufferPools();

        //
        // crreate the event handler thread and the worker thread
        //

        if (!(InitializeEventHandler() && InitializeDlcWorkerThread())) {
            Status = LLC_STATUS_NO_MEMORY;
        } else {

            //
            // initialize each adapter's local-busy state critical section
            // and set the first & last indicies to -1, meaning no index
            //

            int i;

            for (i = 0; i < ARRAY_ELEMENTS(Adapters); ++i) {
                InitializeCriticalSection(&Adapters[i].EventQueueCritSec);
                InitializeCriticalSection(&Adapters[i].LocalBusyCritSec);
                Adapters[i].FirstIndex = Adapters[i].LastIndex = NO_LINKS_BUSY;
            }
            VrDlcInitialized = TRUE;
        }
    }
    return Status;
}


VOID
VrVdmWindowInit(
    VOID
    )

/*++

Routine Description:

    This routine saves the address of a VDM memory window, that is used
    in the communication betwen VDM TSR and its virtual device driver.
    This is called from a DOS TSR module.

Arguments:

    ES:BX in the VDM context are set to point to a memory window in TSR.

Return Value:

    None

--*/

{
    IF_DEBUG(DLC) {
        DPUT("VrVdmWindowInit\n");
    }

    //
    // Initialize the VDM memory window addresses
    //

    dpVdmWindow = MAKE_DWORD(getES(), getBX());
    lpVdmWindow = (LPVDM_REDIR_DOS_WINDOW)DOS_PTR_TO_FLAT(dpVdmWindow);

    IF_DEBUG(DLC) {
        DPUT2("VrVdmWindowsInit: dpVdmWindow=%08x lpVdmWindow=%08x\n", dpVdmWindow, lpVdmWindow);
    }

    //
    // have to return success to VDM redir TSR
    //

    setCF(0);
}


ADAPTER_TYPE
GetAdapterType(
    IN UCHAR AdapterNumber
    )

/*++

Routine Description:

    Determines what type of adapter AdapterNumber designates

    THE DIR.STATUS COMMAND COMPLETES SYNCHRONOUSLY

Arguments:

    AdapterNumber   - number of adapter to get type of

Return Value:

    ADAPTER_TYPE
        TokenRing, Ethernet, PcNetwork, or UnknownAdapter
--*/

{
    LLC_CCB ccb;
    LLC_DIR_STATUS_PARMS parms;
    LLC_STATUS status;

    IF_DEBUG(DLC) {
        DPUT("GetAdapterType\n");
    }

    InitializeCcb(&ccb, AdapterNumber, LLC_DIR_STATUS, &parms);

    status = lpAcsLan(&ccb, NULL);

    if (status == LLC_STATUS_SUCCESS) {
        switch (parms.usAdapterType) {
        case 0x0001:    // Token Ring Network PC Adapter
        case 0x0002:    // Token Ring Network PC Adapter II
        case 0x0004:    // Token Ring Network Adapter/A
        case 0x0008:    // Token Ring Network PC Adapter II
        case 0x0020:    // Token Ring Network 16/4 Adapter
        case 0x0040:    // Token Ring Network 16/4 Adapter/A
        case 0x0080:    // Token Ring Network Adapter/A
            return TokenRing;

        case 0x0100:    //Ethernet Adapter
            return Ethernet;

        case 0x4000:    // PC Network Adapter
        case 0x8000:    // PC Network Adapter/A
            return PcNetwork;
        }
    }
    return UnknownAdapter;
}


BOOLEAN
LoadDlcDll(
    VOID
    )

/*++

Routine Description:

    Dynamically loads DLCAPI.DLL & fixes-up entry points

Arguments:

    None.

Return Value:

    BOOLEAN
        TRUE if success else FALSE

--*/

{
    HANDLE hLibrary;
    LPWORD lpVdmPointer;

    if ((hLibrary = LoadLibrary("DLCAPI")) == NULL) {

        IF_DEBUG(DLC) {
            DPUT1("LoadDlcDll: Error: cannot load DLCAPI.DLL: %d\n", GetLastError());
        }

        return FALSE;
    }
    if ((lpAcsLan = (ACSLAN_FUNC_PTR)GetProcAddress(hLibrary, "AcsLan")) == NULL) {

        IF_DEBUG(DLC) {
            DPUT1("LoadDlcDll: Error: cannot GetProcAddress(AcsLan): %d\n", GetLastError());
        }

        return FALSE;
    }
    if ((lpDlcCallDriver = (DLC_CALL_DRIVER_FUNC_PTR)GetProcAddress(hLibrary, "DlcCallDriver")) == NULL) {

        IF_DEBUG(DLC) {
            DPUT1("LoadDlcDll: Error: cannot GetProcAddress(DlcCallDriver): %d\n", GetLastError());
        }

        return FALSE;
    }
    if ((lpNtAcsLan = (NTACSLAN_FUNC_PTR)GetProcAddress(hLibrary, "NtAcsLan")) == NULL) {

        IF_DEBUG(DLC) {
            DPUT1("LoadDlcDll: Error: cannot GetProcAddress(NtAcsLan): %d\n", GetLastError());
        }

        return FALSE;
    }

    IF_DEBUG(DLC) {
        DPUT("LoadDlcDll: DLCAPI.DLL loaded Ok\n");
    }

    //
    // Initialize the VDM memory window addresses from our well-known address
    // in the VDM Redir. Do this here because we no longer initialize 32-bit
    // support at the point where we load the 16-bit REDIR
    //

    lpVdmPointer = POINTER_FROM_WORDS(getCS(), (DWORD)&((VDM_LOAD_INFO*)0)->DlcWindowAddr);
    dpVdmWindow = MAKE_DWORD(GET_SEGMENT(lpVdmPointer), GET_OFFSET(lpVdmPointer));
    lpVdmWindow = (LPVDM_REDIR_DOS_WINDOW)DOS_PTR_TO_FLAT(dpVdmWindow);

    IF_DEBUG(DLC) {
        DPUT4("LoadDlcDll: lpVdmPointer=%x dpVdmWindow = %04x:%04x lpVdmWindow=%x\n",
              lpVdmPointer,
              HIWORD(dpVdmWindow),
              LOWORD(dpVdmWindow),
              lpVdmWindow
              );
    }

    return TRUE;
}


VOID
TerminateDlcEmulation(
    VOID
    )

/*++

Routine Description:

    Closes any open adapters. Any pending commands are terminated

Arguments:

    None.

Return Value:

    None.

--*/

{
    DWORD i;

    IF_DEBUG(DLC) {
        DPUT("TerminateDlcEmulation\n");
    }

    IF_DEBUG(CRITICAL) {
        DPUT("TerminateDlcEmulation\n");
    }

    for (i = 0; i < ARRAY_ELEMENTS(Adapters); ++i) {
        if (Adapters[i].IsOpen) {
            CloseAdapter((BYTE)i);
        }
    }
}

HANDLE DlcWorkerEvent;
HANDLE DlcWorkerCompletionEvent;
HANDLE DlcWorkerThreadHandle;

struct {
    PLLC_CCB Input;
    PLLC_CCB Original;
    PLLC_CCB Output;
    LLC_STATUS Status;
} DlcWorkerThreadParms;


BOOLEAN
InitializeDlcWorkerThread(
    VOID
    )

/*++

Routine Description:

    Creates events which control VrDlcWorkerThread and starts the worker thread

Arguments:

    None.

Return Value:

    BOOLEAN
        TRUE    - worker thread was successfully created
        FALSE   - couldn't start worker thread for some reason

--*/

{
    DWORD threadId;

    //
    // create 2 auto-reset events
    //

    DlcWorkerEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
    if (DlcWorkerEvent == NULL) {
        return FALSE;
    }
    DlcWorkerCompletionEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
    if (DlcWorkerEvent == NULL) {
        CloseHandle(DlcWorkerEvent);
        return FALSE;
    }

    //
    // kick off the one-and-only worker thread
    //

    DlcWorkerThreadHandle = CreateThread(NULL,
                                         0,
                                         (LPTHREAD_START_ROUTINE)VrDlcWorkerThread,
                                         NULL,
                                         0,
                                         &threadId
                                         );
    if (DlcWorkerThreadHandle == NULL) {
        CloseHandle(DlcWorkerEvent);
        CloseHandle(DlcWorkerCompletionEvent);
        return FALSE;
    }
    return TRUE;
}


VOID
VrDlcWorkerThread(
    IN LPVOID Parameters
    )

/*++

Routine Description:

    Submits requests to NtAcsLan on behalf of DOS thread. This exists because of
    a problem with 16-bit Windows apps that use DLC (like Extra!). Eg:

        1. start Extra! session. Extra submits RECEIVE command
        2. connect to mainframe
        3. start second Extra! session
        4. connect second instance to mainframe
        5. kill first Extra! session

    On a DOS machine, the RECEIVE is submitted for the entire process, so when
    the first Extra! session is killed, the RECEIVE is still active.

    However, on NT, each session is represented by a separate thread in NTVDM.
    So when the first session is killed, any outstanding IRPs are cancelled,
    including the RECEIVE. The second instance of Extra! doesn't know that the
    RECEIVE has been cancelled, and never receives any more data

Arguments:

    Parameters  - unused pointer to parameter block

Return Value:

    None.

--*/

{
    DWORD object;

    UNREFERENCED_PARAMETER(Parameters);

    while (TRUE) {
        object = WaitForSingleObject(DlcWorkerEvent, INFINITE);
        if (object == WAIT_OBJECT_0) {
            DlcWorkerThreadParms.Status = lpNtAcsLan(DlcWorkerThreadParms.Input,
                                                     DlcWorkerThreadParms.Original,
                                                     DlcWorkerThreadParms.Output,
                                                     NULL
                                                     );
            SetEvent(DlcWorkerCompletionEvent);
        }
    }
}


LLC_STATUS
DlcCallWorker(
    PLLC_CCB pInputCcb,
    PLLC_CCB pOriginalCcb,
    PLLC_CCB pOutputCcb
    )

/*++

Routine Description:

    Queues (depth is one) a request to the DLC worker thread and waits for the
    worker thread to complete the request

Arguments:

    pInputCcb       - pointer to input CCB. Mapped to 32-bit aligned memory
    pOriginalCcb    - address of original CCB. Can be non-aligned DOS address
    pOutputCcb      - pointer to output CCB. Can be non-aligned DOS address

Return Value:

    LLC_STATUS

--*/

{
    DlcWorkerThreadParms.Input = pInputCcb;
    DlcWorkerThreadParms.Original = pOriginalCcb;
    DlcWorkerThreadParms.Output = pOutputCcb;
    SetEvent(DlcWorkerEvent);
    WaitForSingleObject(DlcWorkerCompletionEvent, INFINITE);
    return DlcWorkerThreadParms.Status;
}