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
|
/*
* Cronyx-Sigma adapter driver for FreeBSD.
* Supports PPP/HDLC and Cisco/HDLC protocol in synchronous mode,
* and asyncronous channels with full modem control.
* Keepalive protocol implemented in both Cisco and PPP modes.
*
* Copyright (C) 1994-2002 Cronyx Engineering.
* Author: Serge Vakulenko, <vak@cronyx.ru>
*
* Copyright (C) 1999-2004 Cronyx Engineering.
* Rewritten on DDK, ported to NETGRAPH, rewritten for FreeBSD 3.x-5.x by
* Kurakin Roman, <rik@cronyx.ru>
*
* This software is distributed with NO WARRANTIES, not even the implied
* warranties for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*
* Authors grant any other persons or organisations a permission to use,
* modify and redistribute this software in source and binary forms,
* as long as this message is kept with the software, all derivative
* works or modified versions.
*
* Cronyx Id: if_cx.c,v 1.1.2.23 2004/02/26 17:56:40 rik Exp $
*/
#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");
#include <sys/param.h>
#if __FreeBSD_version >= 500000
# define NCX 1
#else
# include "cx.h"
#endif
#if NCX > 0
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/module.h>
#include <sys/proc.h>
#include <sys/mbuf.h>
#include <sys/sockio.h>
#include <sys/malloc.h>
#include <sys/socket.h>
#include <sys/conf.h>
#include <sys/errno.h>
#include <sys/tty.h>
#if __FreeBSD_version >= 400000
# include <sys/bus.h>
# include <machine/bus.h>
# include <sys/rman.h>
# include <isa/isavar.h>
#endif
#include <sys/fcntl.h>
#include <sys/interrupt.h>
#include <vm/vm.h>
#include <vm/pmap.h>
#include <net/if.h>
#include <machine/cpufunc.h>
#include <machine/cserial.h>
#include <machine/clock.h>
#if __FreeBSD_version < 500000
#include <machine/ipl.h>
#include <i386/isa/isa_device.h>
#endif
#if __FreeBSD_version >= 400000
# include <machine/resource.h>
# if __FreeBSD_version <= 501000
# include <i386/isa/intr_machdep.h>
# endif
#endif
#if __FreeBSD_version >= 400000
# include <dev/cx/machdep.h>
# include <dev/cx/cxddk.h>
# include <dev/cx/cronyxfw.h>
#else
# include <i386/isa/cronyx/machdep.h>
# include <i386/isa/cronyx/cxddk.h>
# include <i386/isa/cronyx/cronyxfw.h>
#endif
#include "opt_ng_cronyx.h"
#ifdef NETGRAPH_CRONYX
# include "opt_netgraph.h"
# include <netgraph/ng_message.h>
# include <netgraph/netgraph.h>
# if __FreeBSD_version >= 400000
# include <dev/cx/ng_cx.h>
# else
# include <netgraph/ng_cx.h>
# endif
#else
# include <net/if_types.h>
# if __FreeBSD_version < 500000
# include "sppp.h"
# if NSPPP <= 0
# error The device cx requires sppp or netgraph.
# endif
# endif
# include <net/if_sppp.h>
# define PP_CISCO IFF_LINK2
#if __FreeBSD_version < 400000
# include <bpfilter.h>
# if NBPFILTER > 0
# include <net/bpf.h>
# endif
#else
# if __FreeBSD_version < 500000
# include <bpf.h>
# endif
# include <net/bpf.h>
# define NBPFILTER NBPF
#endif
#endif
#if __FreeBSD_version < 502113
#define ttyld_modem(foo, bar) ((*linesw[(foo)->t_line].l_modem)((foo), (bar)))
#define ttyld_rint(foo, bar) ((*linesw[(foo)->t_line].l_rint)((bar), (foo)))
#define ttyld_start(foo) ((*linesw[(foo)->t_line].l_start)((foo)))
#define ttyld_open(foo, bar) ((*linesw[(foo)->t_line].l_open) ((bar), (foo)))
#define ttyld_close(foo, bar) ((*linesw[(foo)->t_line].l_close) ((foo), (bar)))
#define ttyld_read(foo, bar, barf) ((*linesw[(foo)->t_line].l_read) ((foo), (bar), (barf)))
#define ttyld_write(foo, bar, barf) ((*linesw[(foo)->t_line].l_write) ((foo), (bar), (barf)))
#endif
/* If we don't have Cronyx's sppp version, we don't have fr support via sppp */
#ifndef PP_FR
#define PP_FR 0
#endif
#define CX_DEBUG(d,s) ({if (d->chan->debug) {\
printf ("%s: ", d->name); printf s;}})
#define CX_DEBUG2(d,s) ({if (d->chan->debug>1) {\
printf ("%s: ", d->name); printf s;}})
#define UNIT(d) (minor(d) & 0x3f)
#define IF_CUNIT(d) (minor(d) & 0x40)
#define UNIT_CTL 0x3f
#define CALLOUT(d) (minor(d) & 0x80)
#define CDEV_MAJOR 42
typedef struct _async_q {
int beg;
int end;
#define BF_SZ 14400
int buf[BF_SZ+1];
} async_q;
#define AQ_GSZ(q) ((BF_SZ + (q)->end - (q)->beg)%BF_SZ)
#define AQ_PUSH(q,c) {*((q)->buf + (q)->end) = c;\
(q)->end = ((q)->end + 1)%BF_SZ;}
#define AQ_POP(q,c) {c = *((q)->buf + (q)->beg);\
(q)->beg = ((q)->beg + 1)%BF_SZ;}
#if __FreeBSD_version >= 400000
static void cx_identify __P((driver_t *, device_t));
static int cx_probe __P((device_t));
static int cx_attach __P((device_t));
static int cx_detach __P((device_t));
static device_method_t cx_isa_methods [] = {
DEVMETHOD(device_identify, cx_identify),
DEVMETHOD(device_probe, cx_probe),
DEVMETHOD(device_attach, cx_attach),
DEVMETHOD(device_detach, cx_detach),
{0, 0}
};
typedef struct _bdrv_t {
cx_board_t *board;
struct resource *base_res;
struct resource *drq_res;
struct resource *irq_res;
int base_rid;
int drq_rid;
int irq_rid;
void *intrhand;
} bdrv_t;
static driver_t cx_isa_driver = {
"cx",
cx_isa_methods,
sizeof (bdrv_t),
};
static devclass_t cx_devclass;
#endif
typedef struct _drv_t {
char name [8];
cx_chan_t *chan;
cx_board_t *board;
cx_buf_t buf;
struct tty *tty;
struct callout_handle dcd_timeout_handle;
unsigned dtrwait;
unsigned dtroff;
unsigned callout;
unsigned lock;
int open_dev;
int cd;
int running;
struct callout_handle dtr_timeout_handle;
#ifdef NETGRAPH
char nodename [NG_NODELEN+1];
hook_p hook;
hook_p debug_hook;
node_p node;
struct ifqueue lo_queue;
struct ifqueue hi_queue;
short timeout;
struct callout_handle timeout_handle;
#else
struct sppp pp;
#endif
#if __FreeBSD_version >= 400000
dev_t devt[3];
#endif
async_q aqueue;
#define CX_READ 1
#define CX_WRITE 2
int intr_action;
short atimeout;
} drv_t;
extern long csigma_fw_len;
extern const char *csigma_fw_version;
extern const char *csigma_fw_date;
extern const char *csigma_fw_copyright;
extern const cr_dat_tst_t csigma_fw_tvec[];
extern const u_char csigma_fw_data[];
static void cx_oproc (struct tty *tp);
static int cx_param (struct tty *tp, struct termios *t);
static void cx_stop (struct tty *tp, int flag);
static void cx_dtrwakeup (void *a);
static void cx_receive (cx_chan_t *c, char *data, int len);
static void cx_transmit (cx_chan_t *c, void *attachment, int len);
static void cx_error (cx_chan_t *c, int data);
static void cx_modem (cx_chan_t *c);
static void cx_up (drv_t *d);
static void cx_start (drv_t *d);
#if __FreeBSD_version < 502113
static void ttyldoptim(struct tty *tp);
#endif
#if __FreeBSD_version < 500000
static swihand_t cx_softintr;
#else
static void cx_softintr (void *);
static void *cx_fast_ih;
#endif
static void cx_down (drv_t *d);
static void cx_watchdog (drv_t *d);
static void cx_carrier (void *arg);
#ifdef NETGRAPH
extern struct ng_type typestruct;
#else
static void cx_ifstart (struct ifnet *ifp);
static void cx_tlf (struct sppp *sp);
static void cx_tls (struct sppp *sp);
static void cx_ifwatchdog (struct ifnet *ifp);
static int cx_sioctl (struct ifnet *ifp, u_long cmd, caddr_t data);
static void cx_initialize (void *softc);
#endif
static cx_board_t *adapter [NCX];
static drv_t *channel [NCX*NCHAN];
static struct callout_handle led_timo [NCX];
static struct callout_handle timeout_handle;
#if __FreeBSD_version >= 400000
extern struct cdevsw cx_cdevsw;
#endif
static int MY_SOFT_INTR;
/*
* Print the mbuf chain, for debug purposes only.
*/
static void printmbuf (struct mbuf *m)
{
printf ("mbuf:");
for (; m; m=m->m_next) {
if (m->m_flags & M_PKTHDR)
printf (" HDR %d:", m->m_pkthdr.len);
if (m->m_flags & M_EXT)
printf (" EXT:");
printf (" %d", m->m_len);
}
printf ("\n");
}
/*
* Make an mbuf from data.
*/
static struct mbuf *makembuf (void *buf, u_int len)
{
struct mbuf *m, *o, *p;
MGETHDR (m, M_DONTWAIT, MT_DATA);
if (! m)
return 0;
if (len >= MINCLSIZE)
MCLGET (m, M_DONTWAIT);
m->m_pkthdr.len = len;
m->m_len = 0;
p = m;
while (len) {
u_int n = M_TRAILINGSPACE (p);
if (n > len)
n = len;
if (! n) {
/* Allocate new mbuf. */
o = p;
MGET (p, M_DONTWAIT, MT_DATA);
if (! p) {
m_freem (m);
return 0;
}
if (len >= MINCLSIZE)
MCLGET (p, M_DONTWAIT);
p->m_len = 0;
o->m_next = p;
n = M_TRAILINGSPACE (p);
if (n > len)
n = len;
}
bcopy (buf, mtod (p, caddr_t) + p->m_len, n);
p->m_len += n;
buf = n + (char*) buf;
len -= n;
}
return m;
}
/*
* Recover after lost transmit interrupts.
*/
static void cx_timeout (void *arg)
{
drv_t *d;
int s, i;
for (i=0; i<NCX*NCHAN; ++i) {
d = channel[i];
if (! d)
continue;
s = splhigh ();
if (d->atimeout == 1 && d->tty && d->tty->t_state & TS_BUSY) {
d->tty->t_state &= ~TS_BUSY;
if (d->tty->t_dev) {
d->intr_action |= CX_WRITE;
MY_SOFT_INTR = 1;
#if __FreeBSD_version >= 500000
swi_sched (cx_fast_ih, 0);
#else
setsofttty ();
#endif
}
CX_DEBUG (d, ("cx_timeout\n"));
}
if (d->atimeout)
d->atimeout--;
splx (s);
}
timeout_handle = timeout (cx_timeout, 0, hz*5);
}
static void cx_led_off (void *arg)
{
cx_board_t *b = arg;
int s = splhigh ();
cx_led (b, 0);
led_timo[b->num].callout = 0;
splx (s);
}
/*
* Activate interupt handler from DDK.
*/
#if __FreeBSD_version >= 400000
static void cx_intr (void *arg)
{
bdrv_t *bd = arg;
cx_board_t *b = bd->board;
#else
static void cx_intr (int bnum)
{
cx_board_t *b = adapter [bnum];
#endif
int s = splhigh ();
/* Turn LED on. */
cx_led (b, 1);
cx_int_handler (b);
/* Turn LED off 50 msec later. */
if (! led_timo[b->num].callout)
led_timo[b->num] = timeout (cx_led_off, b, hz/20);
splx (s);
}
static int probe_irq (cx_board_t *b, int irq)
{
int mask, busy, cnt;
/* Clear pending irq, if any. */
cx_probe_irq (b, -irq);
DELAY (100);
for (cnt=0; cnt<5; ++cnt) {
/* Get the mask of pending irqs, assuming they are busy.
* Activate the adapter on given irq. */
busy = cx_probe_irq (b, irq);
DELAY (100);
/* Get the mask of active irqs.
* Deactivate our irq. */
mask = cx_probe_irq (b, -irq);
DELAY (100);
if ((mask & ~busy) == 1 << irq) {
cx_probe_irq (b, 0);
/* printf ("cx%d: irq %d ok, mask=0x%04x, busy=0x%04x\n",
b->num, irq, mask, busy); */
return 1;
}
}
/* printf ("cx%d: irq %d not functional, mask=0x%04x, busy=0x%04x\n",
b->num, irq, mask, busy); */
cx_probe_irq (b, 0);
return 0;
}
static short porttab [] = {
0x200, 0x220, 0x240, 0x260, 0x280, 0x2a0, 0x2c0, 0x2e0,
0x300, 0x320, 0x340, 0x360, 0x380, 0x3a0, 0x3c0, 0x3e0, 0
};
static char dmatab [] = { 7, 6, 5, 0 };
static char irqtab [] = { 5, 10, 11, 7, 3, 15, 12, 0 };
#if __FreeBSD_version >= 400000
static int cx_is_free_res (device_t dev, int rid, int type, u_long start,
u_long end, u_long count)
{
struct resource *res;
if (!(res = bus_alloc_resource (dev, type, &rid, start, end, count,
RF_ALLOCATED)))
return 0;
bus_release_resource (dev, type, rid, res);
return 1;
}
static void cx_identify (driver_t *driver, device_t dev)
{
u_long iobase, rescount;
int devcount;
device_t *devices;
device_t child;
devclass_t my_devclass;
int i, k;
if ((my_devclass = devclass_find ("cx")) == NULL)
return;
devclass_get_devices (my_devclass, &devices, &devcount);
if (devcount == 0) {
/* We should find all devices by our self. We could alter other
* devices, but we don't have a choise
*/
for (i = 0; (iobase = porttab [i]) != 0; i++) {
if (!cx_is_free_res (dev, 1, SYS_RES_IOPORT,
iobase, iobase + NPORT, NPORT))
continue;
if (cx_probe_board (iobase, -1, -1) == 0)
continue;
devcount++;
child = BUS_ADD_CHILD (dev, ISA_ORDER_SPECULATIVE, "cx",
-1);
if (child == NULL)
return;
device_set_desc_copy (child, "Cronyx Sigma");
device_set_driver (child, driver);
bus_set_resource (child, SYS_RES_IOPORT, 0,
iobase, NPORT);
if (devcount >= NCX)
break;
}
} else {
static short porttab [] = {
0x200, 0x220, 0x240, 0x260, 0x280, 0x2a0, 0x2c0, 0x2e0,
0x300, 0x320, 0x340, 0x360, 0x380, 0x3a0, 0x3c0, 0x3e0, 0
};
/* Lets check user choise.
*/
for (k = 0; k < devcount; k++) {
if (bus_get_resource (devices[k], SYS_RES_IOPORT, 0,
&iobase, &rescount) != 0)
continue;
for (i = 0; porttab [i] != 0; i++) {
if (porttab [i] != iobase)
continue;
if (!cx_is_free_res (devices[k], 1, SYS_RES_IOPORT,
iobase, iobase + NPORT, NPORT))
continue;
if (cx_probe_board (iobase, -1, -1) == 0)
continue;
porttab [i] = -1;
device_set_desc_copy (devices[k], "Cronyx Sigma");
break;
}
if (porttab [i] == 0) {
device_delete_child (
device_get_parent (devices[k]),
devices [k]);
devices[k] = 0;
continue;
}
}
for (k = 0; k < devcount; k++) {
if (devices[k] == 0)
continue;
if (bus_get_resource (devices[k], SYS_RES_IOPORT, 0,
&iobase, &rescount) == 0)
continue;
for (i = 0; (iobase = porttab [i]) != 0; i++) {
if (porttab [i] == -1) {
continue;
}
if (!cx_is_free_res (devices[k], 1, SYS_RES_IOPORT,
iobase, iobase + NPORT, NPORT))
continue;
if (cx_probe_board (iobase, -1, -1) == 0)
continue;
bus_set_resource (devices[k], SYS_RES_IOPORT, 0,
iobase, NPORT);
porttab [i] = -1;
device_set_desc_copy (devices[k], "Cronyx Sigma");
break;
}
if (porttab [i] == 0) {
device_delete_child (
device_get_parent (devices[k]),
devices [k]);
}
}
free (devices, M_TEMP);
}
return;
}
static int cx_probe (device_t dev)
{
int unit = device_get_unit (dev);
int i;
u_long iobase, rescount;
if (!device_get_desc (dev) ||
strcmp (device_get_desc (dev), "Cronyx Sigma"))
return ENXIO;
if (bus_get_resource (dev, SYS_RES_IOPORT, 0, &iobase, &rescount) != 0) {
printf ("cx%d: Couldn't get IOPORT\n", unit);
return ENXIO;
}
if (!cx_is_free_res (dev, 1, SYS_RES_IOPORT,
iobase, iobase + NPORT, NPORT)) {
printf ("cx%d: Resource IOPORT isn't free %lx\n", unit, iobase);
return ENXIO;
}
for (i = 0; porttab [i] != 0; i++) {
if (porttab [i] == iobase) {
porttab [i] = -1;
break;
}
}
if (porttab [i] == 0) {
return ENXIO;
}
if (!cx_probe_board (iobase, -1, -1)) {
printf ("cx%d: probing for Sigma at %lx faild\n", unit, iobase);
return ENXIO;
}
return 0;
}
#else /* __FreeBSD_version < 400000 */
static int cx_probe (struct isa_device *id)
{
cx_board_t *b;
int i;
#ifndef NETGRAPH
if (! sppp_attach) {
printf ("cx%d: no synchronous PPP driver configured\n",
id->id_unit);
return 0;
}
#endif
if (id->id_iobase < 0) {
/* Autodetect the adapter. */
for (i=0; ; i++) {
if (! porttab[i]) {
id->id_iobase = -1;
return 0;
}
id->id_iobase = porttab[i];
if (id->id_unit > 0 && adapter[0] && adapter[0]->port == id->id_iobase)
continue;
if (id->id_unit > 1 && adapter[1] && adapter[1]->port == id->id_iobase)
continue;
if (! haveseen_isadev (id, CC_IOADDR | CC_QUIET) &&
cx_probe_board (id->id_iobase, -1, -1))
break;
}
} else if (! cx_probe_board (id->id_iobase, -1, -1))
return 0;
if (id->id_drq < 0) {
/* Find available 16-bit DRQ. */
for (i=0; ; ++i) {
if (! dmatab[i]) {
printf ("cx%d: no available drq found\n",
id->id_unit);
id->id_drq = -1;
return 0;
}
id->id_drq = dmatab[i];
if (! haveseen_isadev (id, CC_DRQ | CC_QUIET)
&& !isa_dma_acquire (id->id_drq))
break;
}
}
b = malloc (sizeof (cx_board_t), M_DEVBUF, M_WAITOK);
if (!b) {
printf ("cx:%d: Couldn't allocate memory\n", id->id_unit);
return (ENXIO);
}
adapter[id->id_unit] = b;
bzero (b, sizeof(cx_board_t));
if (! cx_open_board (b, id->id_unit, id->id_iobase,
id->id_irq ? ffs (id->id_irq) - 1 : -1, id->id_drq)) {
printf ("cx%d: cannot initialize adapter\n", id->id_unit);
isa_dma_release (id->id_drq);
adapter[id->id_unit] = 0;
free (b, M_DEVBUF);
return 0;
}
if (id->id_irq) {
if (! probe_irq (b, ffs (id->id_irq) - 1))
printf ("cx%d: irq %d not functional\n",
id->id_unit, ffs (id->id_irq) - 1);
} else {
/* Find available IRQ. */
for (i=0; ; ++i) {
if (! irqtab[i]) {
printf ("cx%d: no available irq found\n",
id->id_unit);
id->id_irq = -1;
isa_dma_release (id->id_drq);
adapter[id->id_unit] = 0;
free (b, M_DEVBUF);
return 0;
}
id->id_irq = 1 << irqtab[i];
if (haveseen_isadev (id, CC_IRQ | CC_QUIET))
continue;
#ifdef KLD_MODULE
if (register_intr (irqtab[i], 0, 0, (inthand2_t*)
cx_intr, &net_imask, id->id_unit) != 0)
continue;
unregister_intr (irqtab[i], (inthand2_t*) cx_intr);
#endif
if (probe_irq (b, irqtab[i]))
break;
}
}
cx_init (b, b->num, b->port, ffs (id->id_irq) - 1, b->dma);
cx_setup_board (b, 0, 0, 0);
return 1;
}
#endif /* __FreeBSD_version < 400000 */
/*
* The adapter is present, initialize the driver structures.
*/
#if __FreeBSD_version < 400000
static int cx_attach (struct isa_device *id)
{
#else
static int cx_attach (device_t dev)
{
bdrv_t *bd = device_get_softc (dev);
u_long iobase, drq, irq, rescount;
int unit = device_get_unit (dev);
int i;
int s;
#endif
cx_board_t *b;
cx_chan_t *c;
drv_t *d;
#if __FreeBSD_version >= 400000
KASSERT ((bd != NULL), ("cx%d: NULL device softc\n", unit));
bus_get_resource (dev, SYS_RES_IOPORT, 0, &iobase, &rescount);
bd->base_rid = 0;
bd->base_res = bus_alloc_resource (dev, SYS_RES_IOPORT, &bd->base_rid,
iobase, iobase + NPORT, NPORT, RF_ACTIVE);
if (! bd->base_res) {
printf ("cx%d: cannot allocate base address\n", unit);
return ENXIO;
}
if (bus_get_resource (dev, SYS_RES_DRQ, 0, &drq, &rescount) != 0) {
for (i = 0; (drq = dmatab [i]) != 0; i++) {
if (!cx_is_free_res (dev, 1, SYS_RES_DRQ,
drq, drq + 1, 1))
continue;
bus_set_resource (dev, SYS_RES_DRQ, 0, drq, 1);
break;
}
if (dmatab[i] == 0) {
bus_release_resource (dev, SYS_RES_IOPORT, bd->base_rid,
bd->base_res);
printf ("cx%d: Couldn't get DRQ\n", unit);
return ENXIO;
}
}
bd->drq_rid = 0;
bd->drq_res = bus_alloc_resource (dev, SYS_RES_DRQ, &bd->drq_rid,
drq, drq + 1, 1, RF_ACTIVE);
if (! bd->drq_res) {
printf ("cx%d: cannot allocate drq\n", unit);
bus_release_resource (dev, SYS_RES_IOPORT, bd->base_rid,
bd->base_res);
return ENXIO;
}
if (bus_get_resource (dev, SYS_RES_IRQ, 0, &irq, &rescount) != 0) {
for (i = 0; (irq = irqtab [i]) != 0; i++) {
if (!cx_is_free_res (dev, 1, SYS_RES_IRQ,
irq, irq + 1, 1))
continue;
bus_set_resource (dev, SYS_RES_IRQ, 0, irq, 1);
break;
}
if (irqtab[i] == 0) {
bus_release_resource (dev, SYS_RES_DRQ, bd->drq_rid,
bd->drq_res);
bus_release_resource (dev, SYS_RES_IOPORT, bd->base_rid,
bd->base_res);
printf ("cx%d: Couldn't get IRQ\n", unit);
return ENXIO;
}
}
bd->irq_rid = 0;
bd->irq_res = bus_alloc_resource (dev, SYS_RES_IRQ, &bd->irq_rid,
irq, irq + 1, 1, RF_ACTIVE);
if (! bd->irq_res) {
printf ("cx%d: Couldn't allocate irq\n", unit);
bus_release_resource (dev, SYS_RES_DRQ, bd->drq_rid,
bd->drq_res);
bus_release_resource (dev, SYS_RES_IOPORT, bd->base_rid,
bd->base_res);
return ENXIO;
}
b = malloc (sizeof (cx_board_t), M_DEVBUF, M_WAITOK);
if (!b) {
printf ("cx:%d: Couldn't allocate memory\n", unit);
return (ENXIO);
}
adapter[unit] = b;
bzero (b, sizeof(cx_board_t));
if (! cx_open_board (b, unit, iobase, irq, drq)) {
printf ("cx%d: error loading firmware\n", unit);
free (b, M_DEVBUF);
bus_release_resource (dev, SYS_RES_IRQ, bd->irq_rid,
bd->irq_res);
bus_release_resource (dev, SYS_RES_DRQ, bd->drq_rid,
bd->drq_res);
bus_release_resource (dev, SYS_RES_IOPORT, bd->base_rid,
bd->base_res);
return ENXIO;
}
bd->board = b;
if (! probe_irq (b, irq)) {
printf ("cx%d: irq %ld not functional\n", unit, irq);
bd->board = 0;
adapter [unit] = 0;
free (b, M_DEVBUF);
bus_release_resource (dev, SYS_RES_IRQ, bd->irq_rid,
bd->irq_res);
bus_release_resource (dev, SYS_RES_DRQ, bd->drq_rid,
bd->drq_res);
bus_release_resource (dev, SYS_RES_IOPORT, bd->base_rid,
bd->base_res);
return ENXIO;
}
s = splhigh ();
if (bus_setup_intr (dev, bd->irq_res, INTR_TYPE_NET, cx_intr, bd,
&bd->intrhand)) {
printf ("cx%d: Can't setup irq %ld\n", unit, irq);
bd->board = 0;
adapter [unit] = 0;
free (b, M_DEVBUF);
bus_release_resource (dev, SYS_RES_IRQ, bd->irq_rid,
bd->irq_res);
bus_release_resource (dev, SYS_RES_DRQ, bd->drq_rid,
bd->drq_res);
bus_release_resource (dev, SYS_RES_IOPORT, bd->base_rid,
bd->base_res);
splx (s);
return ENXIO;
}
cx_init (b, b->num, b->port, irq, drq);
cx_setup_board (b, 0, 0, 0);
#else /* __FreeBSD_version >= 400000 */
b = adapter[id->id_unit];
#endif /* __FreeBSD_version >= 400000 */
printf ("cx%d: <Cronyx-Sigma-%s>\n", b->num, b->name);
#if __FreeBSD_version < 400000
id->id_ointr = cx_intr;
#endif
for (c=b->chan; c<b->chan+NCHAN; ++c) {
#if __FreeBSD_version >= 400000
char *dnmt="tty %x";
char *dnmc="cua %x";
#endif
if (c->type == T_NONE)
continue;
d = contigmalloc (sizeof(drv_t), M_DEVBUF, M_WAITOK,
0x100000, 0x1000000, 16, 0);
channel [b->num*NCHAN + c->num] = d;
bzero (d, sizeof(drv_t));
sprintf (d->name, "cx%d.%d", b->num, c->num);
d->board = b;
d->chan = c;
d->dtrwait = 3 * hz; /* Default DTR off timeout is 3 seconds. */
d->open_dev = 0;
c->sys = d;
switch (c->type) {
case T_SYNC_RS232:
case T_SYNC_V35:
case T_SYNC_RS449:
case T_UNIV:
case T_UNIV_RS232:
case T_UNIV_RS449:
case T_UNIV_V35:
#ifdef NETGRAPH
if (ng_make_node_common (&typestruct, &d->node) != 0) {
printf ("%s: cannot make common node\n", d->name);
channel [b->num*NCHAN + c->num] = 0;
c->sys = 0;
#if __FreeBSD_version < 400000
free (d, M_DEVBUF);
#else
contigfree (d, sizeof (*d), M_DEVBUF);
#endif
continue;
}
#if __FreeBSD_version >= 500000
NG_NODE_SET_PRIVATE (d->node, d);
#else
d->node->private = d;
#endif
sprintf (d->nodename, "%s%d", NG_CX_NODE_TYPE,
c->board->num*NCHAN + c->num);
if (ng_name_node (d->node, d->nodename)) {
printf ("%s: cannot name node\n", d->nodename);
#if __FreeBSD_version >= 500000
NG_NODE_UNREF (d->node);
#else
ng_rmnode (d->node);
ng_unref (d->node);
#endif
channel [b->num*NCHAN + c->num] = 0;
c->sys = 0;
#if __FreeBSD_version < 400000
free (d, M_DEVBUF);
#else
contigfree (d, sizeof (*d), M_DEVBUF);
#endif
continue;
}
d->lo_queue.ifq_maxlen = IFQ_MAXLEN;
d->hi_queue.ifq_maxlen = IFQ_MAXLEN;
#if __FreeBSD_version >= 500000
mtx_init (&d->lo_queue.ifq_mtx, "cx_queue_lo", NULL, MTX_DEF);
mtx_init (&d->hi_queue.ifq_mtx, "cx_queue_hi", NULL, MTX_DEF);
#endif
#else /*NETGRAPH*/
d->pp.pp_if.if_softc = d;
#if __FreeBSD_version > 501000
if_initname (&d->pp.pp_if, "cx", b->num * NCHAN + c->num);
#else
d->pp.pp_if.if_unit = b->num * NCHAN + c->num;
d->pp.pp_if.if_name = "cx";
#endif
d->pp.pp_if.if_mtu = PP_MTU;
d->pp.pp_if.if_flags = IFF_POINTOPOINT | IFF_MULTICAST;
d->pp.pp_if.if_ioctl = cx_sioctl;
d->pp.pp_if.if_start = cx_ifstart;
d->pp.pp_if.if_watchdog = cx_ifwatchdog;
d->pp.pp_if.if_init = cx_initialize;
sppp_attach (&d->pp.pp_if);
if_attach (&d->pp.pp_if);
d->pp.pp_tlf = cx_tlf;
d->pp.pp_tls = cx_tls;
#if __FreeBSD_version >= 400000 || NBPFILTER > 0
/* If BPF is in the kernel, call the attach for it.
* Size of PPP header is 4 bytes. */
bpfattach (&d->pp.pp_if, DLT_PPP, 4);
#endif
#endif /*NETGRAPH*/
}
cx_start_chan (c, &d->buf, vtophys (&d->buf));
cx_register_receive (c, &cx_receive);
cx_register_transmit (c, &cx_transmit);
cx_register_error (c, &cx_error);
cx_register_modem (c, &cx_modem);
#if __FreeBSD_version >= 400000
dnmt[3] = 'x'+b->num;
dnmc[3] = 'x'+b->num;
d->devt[0] = make_dev (&cx_cdevsw, b->num*NCHAN + c->num, UID_ROOT, GID_WHEEL, 0644, dnmt, b->num*NCHAN + c->num);
d->devt[1] = make_dev (&cx_cdevsw, b->num*NCHAN + c->num + 64, UID_ROOT, GID_WHEEL, 0600, "cx%d", b->num*NCHAN + c->num);
d->devt[2] = make_dev (&cx_cdevsw, b->num*NCHAN + c->num + 128, UID_ROOT, GID_WHEEL, 0660, dnmc, b->num*NCHAN + c->num);
}
splx (s);
return 0;
#else /* __FreeBSD_version < 400000 */
}
return 1;
#endif
}
#if __FreeBSD_version >= 400000
static int cx_detach (device_t dev)
{
bdrv_t *bd = device_get_softc (dev);
cx_board_t *b = bd->board;
cx_chan_t *c;
int s = splhigh ();
/* Check if the device is busy (open). */
for (c = b->chan; c < b->chan + NCHAN; ++c) {
drv_t *d = (drv_t*) c->sys;
if (!d || d->chan->type == T_NONE)
continue;
if (d->lock) {
splx (s);
return EBUSY;
}
if (c->mode == M_ASYNC && d->tty && (d->tty->t_state & TS_ISOPEN) &&
(d->open_dev|0x2)) {
splx (s);
return EBUSY;
}
if (d->running) {
splx (s);
return EBUSY;
}
}
/* Deactivate the timeout routine. And soft interrupt*/
if (led_timo[b->num].callout)
untimeout (cx_led_off, b, led_timo[b->num]);
for (c = b->chan; c < b->chan + NCHAN; ++c) {
drv_t *d = c->sys;
if (!d || d->chan->type == T_NONE)
continue;
if (d->dtr_timeout_handle.callout)
untimeout (cx_dtrwakeup, d, d->dtr_timeout_handle);
if (d->dcd_timeout_handle.callout)
untimeout (cx_carrier, c, d->dcd_timeout_handle);
}
bus_teardown_intr (dev, bd->irq_res, bd->intrhand);
bus_deactivate_resource (dev, SYS_RES_IRQ, bd->irq_rid, bd->irq_res);
bus_release_resource (dev, SYS_RES_IRQ, bd->irq_rid, bd->irq_res);
bus_deactivate_resource (dev, SYS_RES_DRQ, bd->drq_rid, bd->drq_res);
bus_release_resource (dev, SYS_RES_DRQ, bd->drq_rid, bd->drq_res);
bus_deactivate_resource (dev, SYS_RES_IOPORT, bd->base_rid, bd->irq_res);
bus_release_resource (dev, SYS_RES_IOPORT, bd->base_rid, bd->base_res);
cx_close_board (b);
/* Detach the interfaces, free buffer memory. */
for (c = b->chan; c < b->chan + NCHAN; ++c) {
drv_t *d = (drv_t*) c->sys;
if (!d || d->chan->type == T_NONE)
continue;
#if __FreeBSD_version >= 502113
if (d->tty) {
ttyrel (d->tty);
d->tty = NULL;
}
#endif
#ifdef NETGRAPH
#if __FreeBSD_version >= 500000
if (d->node) {
ng_rmnode_self (d->node);
NG_NODE_UNREF (d->node);
d->node = NULL;
}
mtx_destroy (&d->lo_queue.ifq_mtx);
mtx_destroy (&d->hi_queue.ifq_mtx);
#else
ng_rmnode (d->node);
d->node = NULL;
#endif
#else
#if __FreeBSD_version >= 410000 && NBPFILTER > 0
/* Detach from the packet filter list of interfaces. */
bpfdetach (&d->pp.pp_if);
#endif
/* Detach from the sync PPP list. */
sppp_detach (&d->pp.pp_if);
if_detach (&d->pp.pp_if);
#endif
destroy_dev (d->devt[0]);
destroy_dev (d->devt[1]);
destroy_dev (d->devt[2]);
}
cx_led_off (b);
if (led_timo[b->num].callout)
untimeout (cx_led_off, b, led_timo[b->num]);
splx (s);
s = splhigh ();
for (c = b->chan; c < b->chan + NCHAN; ++c) {
drv_t *d = (drv_t*) c->sys;
if (!d || d->chan->type == T_NONE)
continue;
/* Deallocate buffers. */
contigfree (d, sizeof (*d), M_DEVBUF);
}
bd->board = 0;
adapter [b->num] = 0;
free (b, M_DEVBUF);
splx (s);
return 0;
}
#endif
#ifndef NETGRAPH
static void cx_ifstart (struct ifnet *ifp)
{
drv_t *d = ifp->if_softc;
cx_start (d);
}
static void cx_ifwatchdog (struct ifnet *ifp)
{
drv_t *d = ifp->if_softc;
cx_watchdog (d);
}
static void cx_tlf (struct sppp *sp)
{
drv_t *d = sp->pp_if.if_softc;
CX_DEBUG (d, ("cx_tlf\n"));
/* cx_set_dtr (d->chan, 0);*/
/* cx_set_rts (d->chan, 0);*/
sp->pp_down (sp);
}
static void cx_tls (struct sppp *sp)
{
drv_t *d = sp->pp_if.if_softc;
CX_DEBUG (d, ("cx_tls\n"));
sp->pp_up (sp);
}
/*
* Initialization of interface.
* It seems to be never called by upper level.
*/
static void cx_initialize (void *softc)
{
drv_t *d = softc;
CX_DEBUG (d, ("cx_initialize\n"));
}
/*
* Process an ioctl request.
*/
static int cx_sioctl (struct ifnet *ifp, u_long cmd, caddr_t data)
{
drv_t *d = ifp->if_softc;
int error, s, was_up, should_be_up;
/* No socket ioctls while the channel is in async mode. */
if (d->chan->type == T_NONE || d->chan->mode == M_ASYNC)
return EBUSY;
/* Socket ioctls on slave subchannels are not allowed. */
was_up = (ifp->if_flags & IFF_RUNNING) != 0;
error = sppp_ioctl (ifp, cmd, data);
if (error)
return error;
if (! (ifp->if_flags & IFF_DEBUG))
d->chan->debug = 0;
else if (! d->chan->debug)
d->chan->debug = 1;
switch (cmd) {
default: CX_DEBUG2 (d, ("ioctl 0x%lx\n", cmd)); return 0;
case SIOCADDMULTI: CX_DEBUG2 (d, ("SIOCADDMULTI\n")); return 0;
case SIOCDELMULTI: CX_DEBUG2 (d, ("SIOCDELMULTI\n")); return 0;
case SIOCSIFFLAGS: CX_DEBUG2 (d, ("SIOCSIFFLAGS\n")); break;
case SIOCSIFADDR: CX_DEBUG2 (d, ("SIOCSIFADDR\n")); break;
}
/* We get here only in case of SIFFLAGS or SIFADDR. */
s = splhigh ();
should_be_up = (ifp->if_flags & IFF_RUNNING) != 0;
if (!was_up && should_be_up) {
/* Interface goes up -- start it. */
cx_up (d);
cx_start (d);
} else if (was_up && !should_be_up) {
/* Interface is going down -- stop it. */
/* if ((d->pp.pp_flags & PP_FR) || (ifp->if_flags & PP_CISCO))*/
cx_down (d);
}
splx (s);
return 0;
}
#endif /*NETGRAPH*/
/*
* Stop the interface. Called on splimp().
*/
static void cx_down (drv_t *d)
{
int s = splhigh ();
CX_DEBUG (d, ("cx_down\n"));
cx_set_dtr (d->chan, 0);
cx_set_rts (d->chan, 0);
d->running = 0;
splx (s);
}
/*
* Start the interface. Called on splimp().
*/
static void cx_up (drv_t *d)
{
int s = splhigh ();
CX_DEBUG (d, ("cx_up\n"));
cx_set_dtr (d->chan, 1);
cx_set_rts (d->chan, 1);
d->running = 1;
splx (s);
}
/*
* Start output on the (slave) interface. Get another datagram to send
* off of the interface queue, and copy it to the interface
* before starting the output.
*/
static void cx_send (drv_t *d)
{
struct mbuf *m;
u_short len;
CX_DEBUG2 (d, ("cx_send\n"));
/* No output if the interface is down. */
if (! d->running)
return;
/* No output if the modem is off. */
if (! cx_get_dsr (d->chan) && ! cx_get_loop(d->chan))
return;
if (cx_buf_free (d->chan)) {
/* Get the packet to send. */
#ifdef NETGRAPH
IF_DEQUEUE (&d->hi_queue, m);
if (! m)
IF_DEQUEUE (&d->lo_queue, m);
#else
m = sppp_dequeue (&d->pp.pp_if);
#endif
if (! m)
return;
#if (__FreeBSD_version >= 400000 || NBPFILTER > 0) && !defined (NETGRAPH)
if (d->pp.pp_if.if_bpf)
#if __FreeBSD_version >= 500000
BPF_MTAP (&d->pp.pp_if, m);
#else
bpf_mtap (&d->pp.pp_if, m);
#endif
#endif
len = m->m_pkthdr.len;
if (! m->m_next)
cx_send_packet (d->chan, (u_char*)mtod (m, caddr_t),
len, 0);
else {
u_char buf [DMABUFSZ];
m_copydata (m, 0, len, buf);
cx_send_packet (d->chan, buf, len, 0);
}
m_freem (m);
/* Set up transmit timeout, 10 seconds. */
#ifdef NETGRAPH
d->timeout = 10;
#else
d->pp.pp_if.if_timer = 10;
#endif
}
#ifndef NETGRAPH
d->pp.pp_if.if_flags |= IFF_OACTIVE;
#endif
}
/*
* Start output on the interface.
* Always called on splimp().
*/
static void cx_start (drv_t *d)
{
int s = splhigh ();
if (d->running) {
if (! d->chan->dtr)
cx_set_dtr (d->chan, 1);
if (! d->chan->rts)
cx_set_rts (d->chan, 1);
cx_send (d);
}
splx (s);
}
/*
* Handle transmit timeouts.
* Recover after lost transmit interrupts.
* Always called on splimp().
*/
static void cx_watchdog (drv_t *d)
{
int s = splhigh ();
CX_DEBUG (d, ("device timeout\n"));
if (d->running) {
cx_setup_chan (d->chan);
cx_start_chan (d->chan, 0, 0);
cx_set_dtr (d->chan, 1);
cx_set_rts (d->chan, 1);
cx_start (d);
}
splx (s);
}
/*
* Transmit callback function.
*/
static void cx_transmit (cx_chan_t *c, void *attachment, int len)
{
drv_t *d = c->sys;
if (!d)
return;
if (c->mode == M_ASYNC && d->tty) {
d->tty->t_state &= ~(TS_BUSY | TS_FLUSH);
d->atimeout = 0;
if (d->tty->t_dev) {
d->intr_action |= CX_WRITE;
MY_SOFT_INTR = 1;
#if __FreeBSD_version >= 500000
swi_sched (cx_fast_ih, 0);
#else
setsofttty ();
#endif
}
return;
}
#ifdef NETGRAPH
d->timeout = 0;
#else
++d->pp.pp_if.if_opackets;
d->pp.pp_if.if_flags &= ~IFF_OACTIVE;
d->pp.pp_if.if_timer = 0;
#endif
cx_start (d);
}
/*
* Process the received packet.
*/
static void cx_receive (cx_chan_t *c, char *data, int len)
{
drv_t *d = c->sys;
struct mbuf *m;
char *cc = data;
#if __FreeBSD_version >= 500000 && defined NETGRAPH
int error;
#endif
if (!d)
return;
if (c->mode == M_ASYNC && d->tty) {
if (d->tty->t_state & TS_ISOPEN) {
async_q *q = &d->aqueue;
int size = BF_SZ - 1 - AQ_GSZ (q);
if (len <= 0 && !size)
return;
if (len > size) {
c->ierrs++;
cx_error (c, CX_OVERRUN);
len = size - 1;
}
while (len--) {
AQ_PUSH (q, *(unsigned char *)cc);
cc++;
}
d->intr_action |= CX_READ;
MY_SOFT_INTR = 1;
#if __FreeBSD_version >= 500000
swi_sched (cx_fast_ih, 0);
#else
setsofttty ();
#endif
}
return;
}
if (! d->running)
return;
m = makembuf (data, len);
if (! m) {
CX_DEBUG (d, ("no memory for packet\n"));
#ifndef NETGRAPH
++d->pp.pp_if.if_iqdrops;
#endif
return;
}
if (c->debug > 1)
printmbuf (m);
#ifdef NETGRAPH
m->m_pkthdr.rcvif = 0;
#if __FreeBSD_version >= 500000
NG_SEND_DATA_ONLY (error, d->hook, m);
#else
ng_queue_data (d->hook, m, 0);
#endif
#else
++d->pp.pp_if.if_ipackets;
m->m_pkthdr.rcvif = &d->pp.pp_if;
#if __FreeBSD_version >= 400000 || NBPFILTER > 0
/* Check if there's a BPF listener on this interface.
* If so, hand off the raw packet to bpf. */
if (d->pp.pp_if.if_bpf)
#if __FreeBSD_version >= 500000
BPF_TAP (&d->pp.pp_if, data, len);
#else
bpf_tap (&d->pp.pp_if, data, len);
#endif
#endif
sppp_input (&d->pp.pp_if, m);
#endif
}
#if __FreeBSD_version < 502113
#define CONDITION(t,tp) (!(t->c_iflag & (ICRNL | IGNCR | IMAXBEL | INLCR | ISTRIP | IXON))\
&& (!(tp->t_iflag & BRKINT) || (tp->t_iflag & IGNBRK))\
&& (!(tp->t_iflag & PARMRK)\
|| (tp->t_iflag & (IGNPAR | IGNBRK)) == (IGNPAR | IGNBRK))\
&& !(t->c_lflag & (ECHO | ICANON | IEXTEN | ISIG | PENDIN))\
&& linesw[tp->t_line].l_rint == ttyinput)
#else
#define CONDITION(t,tp) (!(t->c_iflag & (ICRNL | IGNCR | IMAXBEL | INLCR | ISTRIP | IXON))\
&& (!(tp->t_iflag & BRKINT) || (tp->t_iflag & IGNBRK))\
&& (!(tp->t_iflag & PARMRK)\
|| (tp->t_iflag & (IGNPAR | IGNBRK)) == (IGNPAR | IGNBRK))\
&& !(t->c_lflag & (ECHO | ICANON | IEXTEN | ISIG | PENDIN))\
&& linesw[tp->t_line]->l_rint == ttyinput)
#endif
/*
* Error callback function.
*/
static void cx_error (cx_chan_t *c, int data)
{
drv_t *d = c->sys;
async_q *q;
if (!d)
return;
q = &(d->aqueue);
switch (data) {
case CX_FRAME:
CX_DEBUG (d, ("frame error\n"));
if (c->mode == M_ASYNC && d->tty && (d->tty->t_state & TS_ISOPEN)
&& (AQ_GSZ (q) < BF_SZ - 1)
&& (!CONDITION((&d->tty->t_termios), (d->tty))
|| !(d->tty->t_iflag & (IGNPAR | PARMRK)))) {
AQ_PUSH (q, TTY_FE);
d->intr_action |= CX_READ;
MY_SOFT_INTR = 1;
#if __FreeBSD_version >= 500000
swi_sched (cx_fast_ih, 0);
#else
setsofttty ();
#endif
}
#ifndef NETGRAPH
else
++d->pp.pp_if.if_ierrors;
#endif
break;
case CX_CRC:
CX_DEBUG (d, ("crc error\n"));
if (c->mode == M_ASYNC && d->tty && (d->tty->t_state & TS_ISOPEN)
&& (AQ_GSZ (q) < BF_SZ - 1)
&& (!CONDITION((&d->tty->t_termios), (d->tty))
|| !(d->tty->t_iflag & INPCK)
|| !(d->tty->t_iflag & (IGNPAR | PARMRK)))) {
AQ_PUSH (q, TTY_PE);
d->intr_action |= CX_READ;
MY_SOFT_INTR = 1;
#if __FreeBSD_version >= 500000
swi_sched (cx_fast_ih, 0);
#else
setsofttty ();
#endif
}
#ifndef NETGRAPH
else
++d->pp.pp_if.if_ierrors;
#endif
break;
case CX_OVERRUN:
CX_DEBUG (d, ("overrun error\n"));
#ifdef TTY_OE
if (c->mode == M_ASYNC && d->tty && (d->tty->t_state & TS_ISOPEN)
&& (AQ_GSZ (q) < BF_SZ - 1)
&& (!CONDITION((&d->tty->t_termios), (d->tty)))) {
AQ_PUSH (q, TTY_OE);
d->intr_action |= CX_READ;
MY_SOFT_INTR = 1;
#if __FreeBSD_version >= 500000
swi_sched (cx_fast_ih, 0);
#else
setsofttty ();
#endif
}
#endif
#ifndef NETGRAPH
else {
++d->pp.pp_if.if_collisions;
++d->pp.pp_if.if_ierrors;
}
#endif
break;
case CX_OVERFLOW:
CX_DEBUG (d, ("overflow error\n"));
#ifndef NETGRAPH
if (c->mode != M_ASYNC)
++d->pp.pp_if.if_ierrors;
#endif
break;
case CX_UNDERRUN:
CX_DEBUG (d, ("underrun error\n"));
if (c->mode != M_ASYNC) {
#ifdef NETGRAPH
d->timeout = 0;
#else
++d->pp.pp_if.if_oerrors;
d->pp.pp_if.if_flags &= ~IFF_OACTIVE;
d->pp.pp_if.if_timer = 0;
cx_start (d);
#endif
}
break;
case CX_BREAK:
CX_DEBUG (d, ("break error\n"));
if (c->mode == M_ASYNC && d->tty && (d->tty->t_state & TS_ISOPEN)
&& (AQ_GSZ (q) < BF_SZ - 1)
&& (!CONDITION((&d->tty->t_termios), (d->tty))
|| !(d->tty->t_iflag & (IGNBRK | BRKINT | PARMRK)))) {
AQ_PUSH (q, TTY_BI);
d->intr_action |= CX_READ;
MY_SOFT_INTR = 1;
#if __FreeBSD_version >= 500000
swi_sched (cx_fast_ih, 0);
#else
setsofttty ();
#endif
}
#ifndef NETGRAPH
else
++d->pp.pp_if.if_ierrors;
#endif
break;
default:
CX_DEBUG (d, ("error #%d\n", data));
}
}
#if __FreeBSD_version < 500000
static int cx_open (dev_t dev, int flag, int mode, struct proc *p)
#else
static int cx_open (dev_t dev, int flag, int mode, struct thread *td)
#endif
{
int unit = UNIT (dev);
drv_t *d;
int error;
if (unit >= NCX*NCHAN || ! (d = channel[unit]))
return ENXIO;
CX_DEBUG2 (d, ("cx_open unit=%d, flag=0x%x, mode=0x%x\n",
unit, flag, mode));
if (d->chan->mode != M_ASYNC || IF_CUNIT(dev)) {
d->open_dev |= 0x1;
return 0;
}
if (!d->tty) {
d->tty = ttymalloc (d->tty);
d->tty->t_oproc = cx_oproc;
d->tty->t_param = cx_param;
#if __FreeBSD_version >= 400000
d->tty->t_stop = cx_stop;
#endif
}
#if __FreeBSD_version >= 400000
dev->si_tty = d->tty;
#endif
d->tty->t_dev = dev;
again:
if (d->dtroff) {
error = tsleep (&d->dtrwait, TTIPRI | PCATCH, "cxdtr", 0);
if (error)
return error;
goto again;
}
if ((d->tty->t_state & TS_ISOPEN) && (d->tty->t_state & TS_XCLUDE) &&
#if __FreeBSD_version >= 500000
suser (td))
#else
p->p_ucred->cr_uid != 0)
#endif
return EBUSY;
if (d->tty->t_state & TS_ISOPEN) {
/*
* Cannot open /dev/cua if /dev/tty already opened.
*/
if (CALLOUT (dev) && ! d->callout)
return EBUSY;
/*
* Opening /dev/tty when /dev/cua is already opened.
* Wait for close, then try again.
*/
if (! CALLOUT (dev) && d->callout) {
if (flag & O_NONBLOCK)
return EBUSY;
error = tsleep (d, TTIPRI | PCATCH, "cxbi", 0);
if (error)
return error;
goto again;
}
} else if (d->lock && ! CALLOUT (dev) && (flag & O_NONBLOCK))
/*
* We try to open /dev/tty in non-blocking mode
* while somebody is already waiting for carrier on it.
*/
return EBUSY;
else {
ttychars (d->tty);
if (d->tty->t_ispeed == 0) {
d->tty->t_iflag = 0;
d->tty->t_oflag = 0;
d->tty->t_lflag = 0;
d->tty->t_cflag = CREAD | CS8 | HUPCL;
d->tty->t_ispeed = d->chan->rxbaud;
d->tty->t_ospeed = d->chan->txbaud;
}
if (CALLOUT (dev))
d->tty->t_cflag |= CLOCAL;
else
d->tty->t_cflag &= ~CLOCAL;
cx_param (d->tty, &d->tty->t_termios);
ttsetwater (d->tty);
}
splhigh ();
if (! (d->tty->t_state & TS_ISOPEN)) {
cx_start_chan (d->chan, 0, 0);
cx_set_dtr (d->chan, 1);
cx_set_rts (d->chan, 1);
d->cd = cx_get_cd (d->chan);
if (CALLOUT (dev) || cx_get_cd (d->chan))
ttyld_modem(d->tty, 1);
}
if (! (flag & O_NONBLOCK) && ! (d->tty->t_cflag & CLOCAL) &&
! (d->tty->t_state & TS_CARR_ON)) {
/* Lock the channel against cxconfig while we are
* waiting for carrier. */
d->lock++;
error = tsleep (&d->tty->t_rawq, TTIPRI | PCATCH, "cxdcd", 0);
/* Unlock the channel. */
d->lock--;
spl0 ();
if (error)
goto failed;
goto again;
}
error = ttyld_open (d->tty, dev);
ttyldoptim (d->tty);
spl0 ();
if (error) {
failed: if (! (d->tty->t_state & TS_ISOPEN)) {
splhigh ();
cx_set_dtr (d->chan, 0);
cx_set_rts (d->chan, 0);
if (d->dtrwait) {
d->dtr_timeout_handle =
timeout (cx_dtrwakeup, d, d->dtrwait);
d->dtroff = 1;
}
spl0 ();
}
return error;
}
if (d->tty->t_state & TS_ISOPEN)
d->callout = CALLOUT (dev) ? 1 : 0;
d->open_dev |= 0x2;
CX_DEBUG2 (d, ("cx_open done\n"));
return 0;
}
#if __FreeBSD_version < 500000
static int cx_close (dev_t dev, int flag, int mode, struct proc *p)
#else
static int cx_close (dev_t dev, int flag, int mode, struct thread *td)
#endif
{
drv_t *d = channel [UNIT (dev)];
int s;
CX_DEBUG2 (d, ("cx_close\n"));
if ((!(d->open_dev&0x2)) || IF_CUNIT(dev)){
d->open_dev &= ~0x1;
return 0;
}
s = splhigh ();
ttyld_close(d->tty, flag);
ttyldoptim (d->tty);
/* Disable receiver.
* Transmitter continues sending the queued data. */
cx_enable_receive (d->chan, 0);
/* Clear DTR and RTS. */
if ((d->tty->t_cflag & HUPCL) || ! (d->tty->t_state & TS_ISOPEN)) {
cx_set_dtr (d->chan, 0);
cx_set_rts (d->chan, 0);
if (d->dtrwait) {
d->dtr_timeout_handle =
timeout (cx_dtrwakeup, d, d->dtrwait);
d->dtroff = 1;
}
}
ttyclose (d->tty);
splx (s);
d->callout = 0;
/*
* Wake up bidirectional opens.
* Since we may be opened twice we couldn't call ttyrel() here.
* So just keep d->tty for future use. It would be freed by
* ttyrel() at cx_detach().
*/
wakeup (d);
d->open_dev &= ~0x2;
return 0;
}
static int cx_read (dev_t dev, struct uio *uio, int flag)
{
drv_t *d = channel [UNIT (dev)];
if (d) CX_DEBUG2 (d, ("cx_read\n"));
if (!d || d->chan->mode != M_ASYNC || IF_CUNIT(dev) || !d->tty)
return EBADF;
return ttyld_read (d->tty, uio, flag);
}
static int cx_write (dev_t dev, struct uio *uio, int flag)
{
drv_t *d = channel [UNIT (dev)];
if (d) CX_DEBUG2 (d, ("cx_write\n"));
if (!d || d->chan->mode != M_ASYNC || IF_CUNIT(dev) || !d->tty)
return EBADF;
return ttyld_write (d->tty, uio, flag);
}
static int cx_modem_status (drv_t *d)
{
int status = 0, s = splhigh ();
/* Already opened by someone or network interface is up? */
if ((d->chan->mode == M_ASYNC && d->tty && (d->tty->t_state & TS_ISOPEN) &&
(d->open_dev|0x2)) || (d->chan->mode != M_ASYNC && d->running))
status = TIOCM_LE; /* always enabled while open */
if (cx_get_dsr (d->chan)) status |= TIOCM_DSR;
if (cx_get_cd (d->chan)) status |= TIOCM_CD;
if (cx_get_cts (d->chan)) status |= TIOCM_CTS;
if (d->chan->dtr) status |= TIOCM_DTR;
if (d->chan->rts) status |= TIOCM_RTS;
splx (s);
return status;
}
#if __FreeBSD_version < 500000
static int cx_ioctl (dev_t dev, u_long cmd, caddr_t data, int flag, struct proc *p)
#else
static int cx_ioctl (dev_t dev, u_long cmd, caddr_t data, int flag, struct thread *td)
#endif
{
drv_t *d = channel [UNIT (dev)];
cx_chan_t *c;
struct serial_statistics *st;
int error, s;
char mask[16];
if (!d || !(c = d->chan))
return EINVAL;
switch (cmd) {
case SERIAL_GETREGISTERED:
CX_DEBUG2 (d, ("ioctl: getregistered\n"));
bzero (mask, sizeof(mask));
for (s=0; s<NCX*NCHAN; ++s)
if (channel [s])
mask [s/8] |= 1 << (s & 7);
bcopy (mask, data, sizeof (mask));
return 0;
case SERIAL_GETPORT:
CX_DEBUG2 (d, ("ioctl: getport\n"));
s = splhigh ();
*(int *)data = cx_get_port (c);
splx (s);
if (*(int *)data<0)
return (EINVAL);
else
return 0;
case SERIAL_SETPORT:
CX_DEBUG2 (d, ("ioctl: setproto\n"));
/* Only for superuser! */
#if __FreeBSD_version < 400000
error = suser (p->p_ucred, &p->p_acflag);
#elif __FreeBSD_version < 500000
error = suser (p);
#else /* __FreeBSD_version >= 500000 */
error = suser (td);
#endif /* __FreeBSD_version >= 500000 */
if (error)
return error;
s = splhigh ();
cx_set_port (c, *(int *)data);
splx (s);
return 0;
#ifndef NETGRAPH
case SERIAL_GETPROTO:
CX_DEBUG2 (d, ("ioctl: getproto\n"));
s = splhigh ();
strcpy ((char*)data, (c->mode == M_ASYNC) ? "async" :
(d->pp.pp_flags & PP_FR) ? "fr" :
(d->pp.pp_if.if_flags & PP_CISCO) ? "cisco" : "ppp");
splx (s);
return 0;
case SERIAL_SETPROTO:
CX_DEBUG2 (d, ("ioctl: setproto\n"));
/* Only for superuser! */
#if __FreeBSD_version < 400000
error = suser (p->p_ucred, &p->p_acflag);
#elif __FreeBSD_version < 500000
error = suser (p);
#else /* __FreeBSD_version >= 500000 */
error = suser (td);
#endif /* __FreeBSD_version >= 500000 */
if (error)
return error;
if (c->mode == M_ASYNC)
return EBUSY;
if (d->pp.pp_if.if_flags & IFF_RUNNING)
return EBUSY;
if (! strcmp ("cisco", (char*)data)) {
d->pp.pp_flags &= ~(PP_FR);
d->pp.pp_flags |= PP_KEEPALIVE;
d->pp.pp_if.if_flags |= PP_CISCO;
} else if (! strcmp ("fr", (char*)data)) {
d->pp.pp_if.if_flags &= ~(PP_CISCO);
d->pp.pp_flags |= PP_FR | PP_KEEPALIVE;
} else if (! strcmp ("ppp", (char*)data)) {
d->pp.pp_flags &= ~(PP_FR | PP_KEEPALIVE);
d->pp.pp_if.if_flags &= ~(PP_CISCO);
} else
return EINVAL;
return 0;
case SERIAL_GETKEEPALIVE:
CX_DEBUG2 (d, ("ioctl: getkeepalive\n"));
if ((d->pp.pp_flags & PP_FR) ||
(d->pp.pp_if.if_flags & PP_CISCO) ||
(c->mode == M_ASYNC))
return EINVAL;
s = splhigh ();
*(int*)data = (d->pp.pp_flags & PP_KEEPALIVE) ? 1 : 0;
splx (s);
return 0;
case SERIAL_SETKEEPALIVE:
CX_DEBUG2 (d, ("ioctl: setkeepalive\n"));
/* Only for superuser! */
#if __FreeBSD_version < 400000
error = suser (p->p_ucred, &p->p_acflag);
#elif __FreeBSD_version < 500000
error = suser (p);
#else /* __FreeBSD_version >= 500000 */
error = suser (td);
#endif /* __FreeBSD_version >= 500000 */
if (error)
return error;
if ((d->pp.pp_flags & PP_FR) ||
(d->pp.pp_if.if_flags & PP_CISCO))
return EINVAL;
s = splhigh ();
if (*(int*)data)
d->pp.pp_flags |= PP_KEEPALIVE;
else
d->pp.pp_flags &= ~PP_KEEPALIVE;
splx (s);
return 0;
#endif /*NETGRAPH*/
case SERIAL_GETMODE:
CX_DEBUG2 (d, ("ioctl: getmode\n"));
s = splhigh ();
*(int*)data = (c->mode == M_ASYNC) ?
SERIAL_ASYNC : SERIAL_HDLC;
splx (s);
return 0;
case SERIAL_SETMODE:
CX_DEBUG2 (d, ("ioctl: setmode\n"));
/* Only for superuser! */
#if __FreeBSD_version < 400000
error = suser (p->p_ucred, &p->p_acflag);
#elif __FreeBSD_version < 500000
error = suser (p);
#else /* __FreeBSD_version >= 500000 */
error = suser (td);
#endif /* __FreeBSD_version >= 500000 */
if (error)
return error;
/* Somebody is waiting for carrier? */
if (d->lock)
return EBUSY;
/* /dev/ttyXX is already opened by someone? */
if (c->mode == M_ASYNC && d->tty && (d->tty->t_state & TS_ISOPEN) &&
(d->open_dev|0x2))
return EBUSY;
/* Network interface is up?
* Cannot change to async mode. */
if (c->mode != M_ASYNC && d->running &&
(*(int*)data == SERIAL_ASYNC))
return EBUSY;
s = splhigh ();
if (c->mode == M_HDLC && *(int*)data == SERIAL_ASYNC) {
cx_set_mode (c, M_ASYNC);
cx_enable_receive (c, 0);
cx_enable_transmit (c, 0);
} else if (c->mode == M_ASYNC && *(int*)data == SERIAL_HDLC) {
cx_set_mode (c, M_HDLC);
cx_enable_receive (c, 1);
cx_enable_transmit (c, 1);
}
splx (s);
return 0;
case SERIAL_GETSTAT:
CX_DEBUG2 (d, ("ioctl: getestat\n"));
st = (struct serial_statistics*) data;
s = splhigh ();
st->rintr = c->rintr;
st->tintr = c->tintr;
st->mintr = c->mintr;
st->ibytes = c->ibytes;
st->ipkts = c->ipkts;
st->ierrs = c->ierrs;
st->obytes = c->obytes;
st->opkts = c->opkts;
st->oerrs = c->oerrs;
splx (s);
return 0;
case SERIAL_CLRSTAT:
CX_DEBUG2 (d, ("ioctl: clrstat\n"));
/* Only for superuser! */
#if __FreeBSD_version < 400000
error = suser (p->p_ucred, &p->p_acflag);
#elif __FreeBSD_version < 500000
error = suser (p);
#else /* __FreeBSD_version >= 500000 */
error = suser (td);
#endif /* __FreeBSD_version >= 500000 */
if (error)
return error;
s = splhigh ();
c->rintr = 0;
c->tintr = 0;
c->mintr = 0;
c->ibytes = 0;
c->ipkts = 0;
c->ierrs = 0;
c->obytes = 0;
c->opkts = 0;
c->oerrs = 0;
splx (s);
return 0;
case SERIAL_GETBAUD:
CX_DEBUG2 (d, ("ioctl: getbaud\n"));
if (c->mode == M_ASYNC)
return EINVAL;
s = splhigh ();
*(long*)data = cx_get_baud(c);
splx (s);
return 0;
case SERIAL_SETBAUD:
CX_DEBUG2 (d, ("ioctl: setbaud\n"));
/* Only for superuser! */
#if __FreeBSD_version < 400000
error = suser (p->p_ucred, &p->p_acflag);
#elif __FreeBSD_version < 500000
error = suser (p);
#else /* __FreeBSD_version >= 500000 */
error = suser (td);
#endif /* __FreeBSD_version >= 500000 */
if (error)
return error;
if (c->mode == M_ASYNC)
return EINVAL;
s = splhigh ();
cx_set_baud (c, *(long*)data);
splx (s);
return 0;
case SERIAL_GETLOOP:
CX_DEBUG2 (d, ("ioctl: getloop\n"));
if (c->mode == M_ASYNC)
return EINVAL;
s = splhigh ();
*(int*)data = cx_get_loop (c);
splx (s);
return 0;
case SERIAL_SETLOOP:
CX_DEBUG2 (d, ("ioctl: setloop\n"));
/* Only for superuser! */
#if __FreeBSD_version < 400000
error = suser (p->p_ucred, &p->p_acflag);
#elif __FreeBSD_version < 500000
error = suser (p);
#else /* __FreeBSD_version >= 500000 */
error = suser (td);
#endif /* __FreeBSD_version >= 500000 */
if (error)
return error;
if (c->mode == M_ASYNC)
return EINVAL;
s = splhigh ();
cx_set_loop (c, *(int*)data);
splx (s);
return 0;
case SERIAL_GETDPLL:
CX_DEBUG2 (d, ("ioctl: getdpll\n"));
if (c->mode == M_ASYNC)
return EINVAL;
s = splhigh ();
*(int*)data = cx_get_dpll (c);
splx (s);
return 0;
case SERIAL_SETDPLL:
CX_DEBUG2 (d, ("ioctl: setdpll\n"));
/* Only for superuser! */
#if __FreeBSD_version < 400000
error = suser (p->p_ucred, &p->p_acflag);
#elif __FreeBSD_version < 500000
error = suser (p);
#else /* __FreeBSD_version >= 500000 */
error = suser (td);
#endif /* __FreeBSD_version >= 500000 */
if (error)
return error;
if (c->mode == M_ASYNC)
return EINVAL;
s = splhigh ();
cx_set_dpll (c, *(int*)data);
splx (s);
return 0;
case SERIAL_GETNRZI:
CX_DEBUG2 (d, ("ioctl: getnrzi\n"));
if (c->mode == M_ASYNC)
return EINVAL;
s = splhigh ();
*(int*)data = cx_get_nrzi (c);
splx (s);
return 0;
case SERIAL_SETNRZI:
CX_DEBUG2 (d, ("ioctl: setnrzi\n"));
/* Only for superuser! */
#if __FreeBSD_version < 400000
error = suser (p->p_ucred, &p->p_acflag);
#elif __FreeBSD_version < 500000
error = suser (p);
#else /* __FreeBSD_version >= 500000 */
error = suser (td);
#endif /* __FreeBSD_version >= 500000 */
if (error)
return error;
if (c->mode == M_ASYNC)
return EINVAL;
s = splhigh ();
cx_set_nrzi (c, *(int*)data);
splx (s);
return 0;
case SERIAL_GETDEBUG:
CX_DEBUG2 (d, ("ioctl: getdebug\n"));
s = splhigh ();
*(int*)data = c->debug;
splx (s);
return 0;
case SERIAL_SETDEBUG:
CX_DEBUG2 (d, ("ioctl: setdebug\n"));
/* Only for superuser! */
#if __FreeBSD_version < 400000
error = suser (p->p_ucred, &p->p_acflag);
#elif __FreeBSD_version < 500000
error = suser (p);
#else /* __FreeBSD_version >= 500000 */
error = suser (td);
#endif /* __FreeBSD_version >= 500000 */
if (error)
return error;
s = splhigh ();
c->debug = *(int*)data;
splx (s);
#ifndef NETGRAPH
if (d->chan->debug)
d->pp.pp_if.if_flags |= IFF_DEBUG;
else
d->pp.pp_if.if_flags &= (~IFF_DEBUG);
#endif
return 0;
}
if (c->mode == M_ASYNC && !IF_CUNIT(dev) && d->tty) {
#if __FreeBSD_version >= 502113
error = ttyioctl (dev, cmd, data, flag, td);
ttyldoptim (d->tty);
if (error != ENOTTY) {
if (error)
CX_DEBUG2 (d, ("ttioctl: 0x%lx, error %d\n", cmd, error));
return error;
}
#else
#if __FreeBSD_version >= 500000
error = (*linesw[d->tty->t_line].l_ioctl) (d->tty, cmd, data, flag, td);
#else
error = (*linesw[d->tty->t_line].l_ioctl) (d->tty, cmd, data, flag, p);
#endif
ttyldoptim (d->tty);
if (error != ENOIOCTL) {
if (error)
CX_DEBUG2 (d, ("l_ioctl: 0x%lx, error %d\n", cmd, error));
return error;
}
error = ttioctl (d->tty, cmd, data, flag);
ttyldoptim (d->tty);
if (error != ENOIOCTL) {
if (error)
CX_DEBUG2 (d, ("ttioctl: 0x%lx, error %d\n", cmd, error));
return error;
}
#endif
}
switch (cmd) {
case TIOCSBRK: /* Start sending line break */
CX_DEBUG2 (d, ("ioctl: tiocsbrk\n"));
s = splhigh ();
cx_send_break (c, 500);
splx (s);
return 0;
case TIOCCBRK: /* Stop sending line break */
CX_DEBUG2 (d, ("ioctl: tioccbrk\n"));
return 0;
case TIOCSDTR: /* Set DTR */
CX_DEBUG2 (d, ("ioctl: tiocsdtr\n"));
s = splhigh ();
cx_set_dtr (c, 1);
splx (s);
return 0;
case TIOCCDTR: /* Clear DTR */
CX_DEBUG2 (d, ("ioctl: tioccdtr\n"));
s = splhigh ();
cx_set_dtr (c, 0);
splx (s);
return 0;
case TIOCMSET: /* Set DTR/RTS */
CX_DEBUG2 (d, ("ioctl: tiocmset\n"));
s = splhigh ();
cx_set_dtr (c, (*(int*)data & TIOCM_DTR) ? 1 : 0);
cx_set_rts (c, (*(int*)data & TIOCM_RTS) ? 1 : 0);
splx (s);
return 0;
case TIOCMBIS: /* Add DTR/RTS */
CX_DEBUG2 (d, ("ioctl: tiocmbis\n"));
s = splhigh ();
if (*(int*)data & TIOCM_DTR) cx_set_dtr (c, 1);
if (*(int*)data & TIOCM_RTS) cx_set_rts (c, 1);
splx (s);
return 0;
case TIOCMBIC: /* Clear DTR/RTS */
CX_DEBUG2 (d, ("ioctl: tiocmbic\n"));
s = splhigh ();
if (*(int*)data & TIOCM_DTR) cx_set_dtr (c, 0);
if (*(int*)data & TIOCM_RTS) cx_set_rts (c, 0);
splx (s);
return 0;
case TIOCMGET: /* Get modem status */
CX_DEBUG2 (d, ("ioctl: tiocmget\n"));
*(int*)data = cx_modem_status (d);
return 0;
#ifdef TIOCMSDTRWAIT
case TIOCMSDTRWAIT:
CX_DEBUG2 (d, ("ioctl: tiocmsdtrwait\n"));
/* Only for superuser! */
#if __FreeBSD_version < 400000
error = suser (p->p_ucred, &p->p_acflag);
#elif __FreeBSD_version < 500000
error = suser (p);
#else /* __FreeBSD_version >= 500000 */
error = suser (td);
#endif /* __FreeBSD_version >= 500000 */
if (error)
return error;
s = splhigh ();
d->dtrwait = *(int*)data * hz / 100;
splx (s);
return 0;
#endif
#ifdef TIOCMGDTRWAIT
case TIOCMGDTRWAIT:
CX_DEBUG2 (d, ("ioctl: tiocmgdtrwait\n"));
s = splhigh ();
*(int*)data = d->dtrwait * 100 / hz;
splx (s);
return 0;
#endif
}
CX_DEBUG2 (d, ("ioctl: 0x%lx\n", cmd));
return ENOTTY;
}
/*
* Wake up opens() waiting for DTR ready.
*/
static void cx_dtrwakeup (void *arg)
{
drv_t *d = arg;
d->dtroff = 0;
wakeup (&d->dtrwait);
}
#if __FreeBSD_version < 502113
static void
ttyldoptim(tp)
struct tty *tp;
{
struct termios *t;
t = &tp->t_termios;
if (CONDITION(t,tp))
tp->t_state |= TS_CAN_BYPASS_L_RINT;
else
tp->t_state &= ~TS_CAN_BYPASS_L_RINT;
}
#endif
#if __FreeBSD_version >= 500000
void cx_softintr (void *unused)
#else
void cx_softintr ()
#endif
{
drv_t *d;
async_q *q;
int i, s, ic, k;
while (MY_SOFT_INTR) {
MY_SOFT_INTR = 0;
for (i=0; i<NCX*NCHAN; ++i) {
d = channel [i];
if (!d || !d->chan || d->chan->type == T_NONE
|| d->chan->mode != M_ASYNC || !d->tty
|| !d->tty->t_dev)
continue;
s = splhigh ();
if (d->intr_action & CX_READ) {
q = &(d->aqueue);
if (d->tty->t_state & TS_CAN_BYPASS_L_RINT) {
k = AQ_GSZ(q);
if (d->tty->t_rawq.c_cc + k >
d->tty->t_ihiwat
&& (d->tty->t_cflag & CRTS_IFLOW
|| d->tty->t_iflag & IXOFF)
&& !(d->tty->t_state & TS_TBLOCK))
ttyblock(d->tty);
d->tty->t_rawcc += k;
while (k>0) {
k--;
AQ_POP (q, ic);
splx (s);
putc (ic, &d->tty->t_rawq);
s = splhigh ();
}
ttwakeup(d->tty);
if (d->tty->t_state & TS_TTSTOP
&& (d->tty->t_iflag & IXANY
|| d->tty->t_cc[VSTART] ==
d->tty->t_cc[VSTOP])) {
d->tty->t_state &= ~TS_TTSTOP;
d->tty->t_lflag &= ~FLUSHO;
d->intr_action |= CX_WRITE;
}
} else {
while (q->end != q->beg) {
AQ_POP (q, ic);
splx (s);
ttyld_rint (d->tty, ic);
s = splhigh ();
}
}
d->intr_action &= ~CX_READ;
}
splx (s);
s = splhigh ();
if (d->intr_action & CX_WRITE) {
if (d->tty->t_line)
ttyld_start (d->tty);
else
cx_oproc (d->tty);
d->intr_action &= ~CX_WRITE;
}
splx (s);
}
}
}
/*
* Fill transmitter buffer with data.
*/
static void cx_oproc (struct tty *tp)
{
int s = splhigh (), k;
drv_t *d = channel [UNIT (tp->t_dev)];
static u_char buf[DMABUFSZ];
u_char *p;
u_short len = 0, sublen = 0;
if (!d) {
splx (s);
return;
}
CX_DEBUG2 (d, ("cx_oproc\n"));
if (tp->t_cflag & CRTSCTS && (tp->t_state & TS_TBLOCK) && d->chan->rts)
cx_set_rts (d->chan, 0);
else if (tp->t_cflag & CRTSCTS && ! (tp->t_state & TS_TBLOCK) && ! d->chan->rts)
cx_set_rts (d->chan, 1);
if (! (tp->t_state & (TS_TIMEOUT | TS_TTSTOP))) {
/* Start transmitter. */
cx_enable_transmit (d->chan, 1);
/* Is it busy? */
if (! cx_buf_free (d->chan)) {
tp->t_state |= TS_BUSY;
splx (s);
return;
}
if (tp->t_iflag & IXOFF) {
p = (buf + (DMABUFSZ/2));
sublen = q_to_b (&tp->t_outq, p, (DMABUFSZ/2));
k = sublen;
while (k--) {
/* Send XON/XOFF out of band. */
if (*p == tp->t_cc[VSTOP]) {
cx_xflow_ctl (d->chan, 0);
p++;
continue;
}
if (*p == tp->t_cc[VSTART]) {
cx_xflow_ctl (d->chan, 1);
p++;
continue;
}
buf[len] = *p;
len++;
p++;
}
} else {
p = buf;
len = q_to_b (&tp->t_outq, p, (DMABUFSZ/2));
}
if (len) {
cx_send_packet (d->chan, buf, len, 0);
tp->t_state |= TS_BUSY;
d->atimeout = 10;
CX_DEBUG2 (d, ("out %d bytes\n", len));
}
}
ttwwakeup (tp);
splx (s);
}
static int cx_param (struct tty *tp, struct termios *t)
{
drv_t *d = channel [UNIT (tp->t_dev)];
int s, bits, parity;
if (!d)
return EINVAL;
s = splhigh ();
if (t->c_ospeed == 0) {
/* Clear DTR and RTS. */
cx_set_dtr (d->chan, 0);
splx (s);
CX_DEBUG2 (d, ("cx_param (hangup)\n"));
return 0;
}
CX_DEBUG2 (d, ("cx_param\n"));
/* Check requested parameters. */
if (t->c_ospeed < 300 || t->c_ospeed > 256*1024) {
splx (s);
return EINVAL;
}
if (t->c_ispeed && (t->c_ispeed < 300 || t->c_ispeed > 256*1024)) {
splx (s);
return EINVAL;
}
/* And copy them to tty and channel structures. */
tp->t_ispeed = t->c_ispeed = tp->t_ospeed = t->c_ospeed;
tp->t_cflag = t->c_cflag;
/* Set character length and parity mode. */
switch (t->c_cflag & CSIZE) {
default:
case CS8: bits = 8; break;
case CS7: bits = 7; break;
case CS6: bits = 6; break;
case CS5: bits = 5; break;
}
parity = ((t->c_cflag & PARENB) ? 1 : 0) *
(1 + ((t->c_cflag & PARODD) ? 0 : 1));
/* Set current channel number. */
if (! d->chan->dtr)
cx_set_dtr (d->chan, 1);
ttyldoptim (tp);
cx_set_async_param (d->chan, t->c_ospeed, bits, parity, (t->c_cflag & CSTOPB),
!(t->c_cflag & PARENB), (t->c_cflag & CRTSCTS),
(t->c_iflag & IXON), (t->c_iflag & IXANY),
t->c_cc[VSTART], t->c_cc[VSTOP]);
splx (s);
return 0;
}
#if __FreeBSD_version < 400000
static struct tty *cx_devtotty (dev_t dev)
{
int unit = UNIT (dev);
if (unit == UNIT_CTL || unit >= NCX*NCHAN || ! channel[unit])
return 0;
return channel[unit]->tty;
}
#endif
/*
* Stop output on a line
*/
static void cx_stop (struct tty *tp, int flag)
{
drv_t *d = channel [UNIT (tp->t_dev)];
int s;
if (!d)
return;
s = splhigh ();
if (tp->t_state & TS_BUSY) {
/* Stop transmitter */
CX_DEBUG2 (d, ("cx_stop\n"));
cx_transmitter_ctl (d->chan, 0);
}
splx (s);
}
/*
* Process the (delayed) carrier signal setup.
*/
static void cx_carrier (void *arg)
{
drv_t *d = arg;
cx_chan_t *c = d->chan;
int s, cd;
s = splhigh ();
cd = cx_get_cd (c);
if (d->cd != cd) {
if (cd) {
CX_DEBUG (d, ("carrier on\n"));
d->cd = 1;
splx (s);
if (d->tty)
ttyld_modem(d->tty, 1);
} else {
CX_DEBUG (d, ("carrier loss\n"));
d->cd = 0;
splx (s);
if (d->tty)
ttyld_modem(d->tty, 0);
}
}
}
/*
* Modem signal callback function.
*/
static void cx_modem (cx_chan_t *c)
{
drv_t *d = c->sys;
if (!d || c->mode != M_ASYNC)
return;
/* Handle carrier detect/loss. */
untimeout (cx_carrier, c, d->dcd_timeout_handle);
/* Carrier changed - delay processing DCD for a while
* to give both sides some time to initialize. */
d->dcd_timeout_handle = timeout (cx_carrier, d, hz/2);
}
#if __FreeBSD_version < 400000
struct isa_driver cxdriver = { cx_probe, cx_attach, "cx" };
static struct cdevsw cx_cdevsw = {
cx_open, cx_close, cx_read, cx_write,
cx_ioctl, cx_stop, noreset, cx_devtotty,
ttpoll, nommap, NULL, "cx",
NULL, -1,
};
#elif __FreeBSD_version < 500000
static struct cdevsw cx_cdevsw = {
cx_open, cx_close, cx_read, cx_write,
cx_ioctl, ttypoll, nommap, nostrategy,
"cx", CDEV_MAJOR, nodump, nopsize,
D_TTY, -1
};
#elif __FreeBSD_version == 500000
static struct cdevsw cx_cdevsw = {
cx_open, cx_close, cx_read, cx_write,
cx_ioctl, ttypoll, nommap, nostrategy,
"cx", CDEV_MAJOR, nodump, nopsize,
D_TTY,
};
#elif __FreeBSD_version <= 501000
static struct cdevsw cx_cdevsw = {
.d_open = cx_open,
.d_close = cx_close,
.d_read = cx_read,
.d_write = cx_write,
.d_ioctl = cx_ioctl,
.d_poll = ttypoll,
.d_mmap = nommap,
.d_strategy = nostrategy,
.d_name = "cx",
.d_maj = CDEV_MAJOR,
.d_dump = nodump,
.d_flags = D_TTY,
};
#elif __FreeBSD_version < 502103
static struct cdevsw cx_cdevsw = {
.d_open = cx_open,
.d_close = cx_close,
.d_read = cx_read,
.d_write = cx_write,
.d_ioctl = cx_ioctl,
.d_poll = ttypoll,
.d_name = "cx",
.d_maj = CDEV_MAJOR,
.d_flags = D_TTY,
};
#else /* __FreeBSD_version >= 502103 */
static struct cdevsw cx_cdevsw = {
.d_version = D_VERSION,
.d_open = cx_open,
.d_close = cx_close,
.d_read = cx_read,
.d_write = cx_write,
.d_ioctl = cx_ioctl,
.d_name = "cx",
.d_maj = CDEV_MAJOR,
.d_flags = D_TTY | D_NEEDGIANT,
};
#endif
#ifdef NETGRAPH
#if __FreeBSD_version >= 500000
static int ng_cx_constructor (node_p node)
{
drv_t *d = NG_NODE_PRIVATE (node);
#else
static int ng_cx_constructor (node_p *node)
{
drv_t *d = (*node)->private;
#endif
CX_DEBUG (d, ("Constructor\n"));
return EINVAL;
}
static int ng_cx_newhook (node_p node, hook_p hook, const char *name)
{
int s;
#if __FreeBSD_version >= 500000
drv_t *d = NG_NODE_PRIVATE (node);
#else
drv_t *d = node->private;
#endif
if (d->chan->mode == M_ASYNC)
return EINVAL;
/* Attach debug hook */
if (strcmp (name, NG_CX_HOOK_DEBUG) == 0) {
#if __FreeBSD_version >= 500000
NG_HOOK_SET_PRIVATE (hook, NULL);
#else
hook->private = 0;
#endif
d->debug_hook = hook;
return 0;
}
/* Check for raw hook */
if (strcmp (name, NG_CX_HOOK_RAW) != 0)
return EINVAL;
#if __FreeBSD_version >= 500000
NG_HOOK_SET_PRIVATE (hook, d);
#else
hook->private = d;
#endif
d->hook = hook;
s = splhigh ();
cx_up (d);
splx (s);
return 0;
}
static int print_modems (char *s, cx_chan_t *c, int need_header)
{
int status = cx_modem_status (c->sys);
int length = 0;
if (need_header)
length += sprintf (s + length, " LE DTR DSR RTS CTS CD\n");
length += sprintf (s + length, "%4s %4s %4s %4s %4s %4s\n",
status & TIOCM_LE ? "On" : "-",
status & TIOCM_DTR ? "On" : "-",
status & TIOCM_DSR ? "On" : "-",
status & TIOCM_RTS ? "On" : "-",
status & TIOCM_CTS ? "On" : "-",
status & TIOCM_CD ? "On" : "-");
return length;
}
static int print_stats (char *s, cx_chan_t *c, int need_header)
{
int length = 0;
if (need_header)
length += sprintf (s + length, " Rintr Tintr Mintr Ibytes Ipkts Ierrs Obytes Opkts Oerrs\n");
length += sprintf (s + length, "%7ld %7ld %7ld %8ld %7ld %7ld %8ld %7ld %7ld\n",
c->rintr, c->tintr, c->mintr, c->ibytes, c->ipkts,
c->ierrs, c->obytes, c->opkts, c->oerrs);
return length;
}
static int print_chan (char *s, cx_chan_t *c)
{
drv_t *d = c->sys;
int length = 0;
length += sprintf (s + length, "cx%d", c->board->num * NCHAN + c->num);
if (d->chan->debug)
length += sprintf (s + length, " debug=%d", d->chan->debug);
if (cx_get_baud (c))
length += sprintf (s + length, " %ld", cx_get_baud (c));
else
length += sprintf (s + length, " extclock");
if (c->mode == M_HDLC) {
length += sprintf (s + length, " dpll=%s", cx_get_dpll (c) ? "on" : "off");
length += sprintf (s + length, " nrzi=%s", cx_get_nrzi (c) ? "on" : "off");
}
length += sprintf (s + length, " loop=%s", cx_get_loop (c) ? "on\n" : "off\n");
return length;
}
#if __FreeBSD_version >= 500000
static int ng_cx_rcvmsg (node_p node, item_p item, hook_p lasthook)
{
drv_t *d = NG_NODE_PRIVATE (node);
struct ng_mesg *msg;
#else
static int ng_cx_rcvmsg (node_p node, struct ng_mesg *msg,
const char *retaddr, struct ng_mesg **rptr)
{
drv_t *d = node->private;
#endif
struct ng_mesg *resp = NULL;
int error = 0;
if (!d)
return EINVAL;
CX_DEBUG (d, ("Rcvmsg\n"));
#if __FreeBSD_version >= 500000
NGI_GET_MSG (item, msg);
#endif
switch (msg->header.typecookie) {
default:
error = EINVAL;
break;
case NGM_CX_COOKIE:
printf ("Don't forget to implement\n");
error = EINVAL;
break;
case NGM_GENERIC_COOKIE:
switch (msg->header.cmd) {
default:
error = EINVAL;
break;
case NGM_TEXT_STATUS: {
char *s;
int l = 0;
int dl = sizeof (struct ng_mesg) + 730;
#if __FreeBSD_version >= 500000
NG_MKRESPONSE (resp, msg, dl, M_NOWAIT);
if (! resp) {
error = ENOMEM;
break;
}
#else
MALLOC (resp, struct ng_mesg *, dl,
M_NETGRAPH, M_NOWAIT);
if (! resp) {
error = ENOMEM;
break;
}
#endif
bzero (resp, dl);
s = (resp)->data;
l += print_chan (s + l, d->chan);
l += print_stats (s + l, d->chan, 1);
l += print_modems (s + l, d->chan, 1);
#if __FreeBSD_version < 500000
(resp)->header.version = NG_VERSION;
(resp)->header.arglen = strlen (s) + 1;
(resp)->header.token = msg->header.token;
(resp)->header.typecookie = NGM_CX_COOKIE;
(resp)->header.cmd = msg->header.cmd;
#endif
strncpy ((resp)->header.cmdstr, "status", NG_CMDSTRLEN);
}
break;
}
break;
}
#if __FreeBSD_version >= 500000
NG_RESPOND_MSG (error, node, item, resp);
NG_FREE_MSG (msg);
#else
*rptr = resp;
FREE (msg, M_NETGRAPH);
#endif
return error;
}
#if __FreeBSD_version >= 500000
static int ng_cx_rcvdata (hook_p hook, item_p item)
{
drv_t *d = NG_NODE_PRIVATE (NG_HOOK_NODE(hook));
struct mbuf *m;
meta_p meta;
#else
static int ng_cx_rcvdata (hook_p hook, struct mbuf *m, meta_p meta)
{
drv_t *d = hook->node->private;
#endif
struct ifqueue *q;
int s;
#if __FreeBSD_version >= 500000
NGI_GET_M (item, m);
NGI_GET_META (item, meta);
NG_FREE_ITEM (item);
if (! NG_HOOK_PRIVATE (hook) || ! d) {
NG_FREE_M (m);
NG_FREE_META (meta);
#else
if (! hook->private || ! d) {
NG_FREE_DATA (m,meta);
#endif
return ENETDOWN;
}
q = (meta && meta->priority > 0) ? &d->hi_queue : &d->lo_queue;
s = splhigh ();
#if __FreeBSD_version >= 500000
IF_LOCK (q);
if (_IF_QFULL (q)) {
_IF_DROP (q);
IF_UNLOCK (q);
splx (s);
NG_FREE_M (m);
NG_FREE_META (meta);
return ENOBUFS;
}
_IF_ENQUEUE (q, m);
IF_UNLOCK (q);
#else
if (IF_QFULL (q)) {
IF_DROP (q);
splx (s);
NG_FREE_DATA (m, meta);
return ENOBUFS;
}
IF_ENQUEUE (q, m);
#endif
cx_start (d);
splx (s);
return 0;
}
static int ng_cx_rmnode (node_p node)
{
#if __FreeBSD_version >= 500000
drv_t *d = NG_NODE_PRIVATE (node);
CX_DEBUG (d, ("Rmnode\n"));
if (d && d->running) {
int s = splhigh ();
cx_down (d);
splx (s);
}
#ifdef KLD_MODULE
if (node->nd_flags & NG_REALLY_DIE) {
NG_NODE_SET_PRIVATE (node, NULL);
NG_NODE_UNREF (node);
}
node->nd_flags &= ~NG_INVALID;
#endif
#else /* __FreeBSD_version < 500000 */
drv_t *d = node->private;
int s;
s = splhigh ();
cx_down (d);
splx (s);
node->flags |= NG_INVALID;
ng_cutlinks (node);
#ifdef KLD_MODULE
ng_unname (node);
ng_unref (node);
#else
node->flags &= ~NG_INVALID;
#endif
#endif
return 0;
}
static void ng_cx_watchdog (void *arg)
{
drv_t *d = arg;
if (d->timeout == 1)
cx_watchdog (d);
if (d->timeout)
d->timeout--;
d->timeout_handle = timeout (ng_cx_watchdog, d, hz);
}
static int ng_cx_connect (hook_p hook)
{
#if __FreeBSD_version >= 500000
drv_t *d = NG_NODE_PRIVATE (NG_HOOK_NODE (hook));
#else
drv_t *d = hook->node->private;
#endif
d->timeout_handle = timeout (ng_cx_watchdog, d, hz);
return 0;
}
static int ng_cx_disconnect (hook_p hook)
{
#if __FreeBSD_version >= 500000
drv_t *d = NG_NODE_PRIVATE (NG_HOOK_NODE (hook));
#else
drv_t *d = hook->node->private;
#endif
int s;
s = splhigh ();
#if __FreeBSD_version >= 500000
if (NG_HOOK_PRIVATE (hook))
#else
if (hook->private)
#endif
cx_down (d);
splx (s);
untimeout (ng_cx_watchdog, d, d->timeout_handle);
return 0;
}
#endif /*NETGRAPH*/
#ifdef KLD_MODULE
#if __FreeBSD_version < 400000
/*
* Function called when loading the driver.
*/
static int cx_load (void)
{
int i;
for (i=0;i<NCX; ++i) {
struct isa_device id = {-1, &cxdriver, -1, 0, -1, 0, 0, (inthand2_t *)cx_intr, i, 0, 0, 0, 0 ,0 ,1 ,0 ,0};
disable_intr();
if (!cx_probe (&id)) {
enable_intr();
break;
}
cx_attach (&id);
register_intr ((adapter [i])->irq, 0, 0, (inthand2_t*) cx_intr,
&net_imask, id.id_unit);
enable_intr();
}
if (!i) {
/* Deactivate the timeout routine. And soft interrupt*/
untimeout (cx_timeout, 0, timeout_handle);
unregister_swi (SWI_TTY, cx_softintr);
return ENXIO;
}
return 0;
}
/*
* Function called when unloading the driver.
*/
static int cx_unload (void)
{
int i, s;
/* Check if the device is busy (open). */
for (i=0; i<NCX*NCHAN; ++i) {
drv_t *d = channel[i];
cx_chan_t *c;
if (!d || (c=d->chan)->type == T_NONE)
continue;
if (d->lock)
return EBUSY;
if (c->mode == M_ASYNC && d->tty && (d->tty->t_state & TS_ISOPEN) &&
(d->open_dev|0x2))
return EBUSY;
if (d->running)
return EBUSY;
}
s = splhigh ();
/* Deactivate the timeout routine. And soft interrupt*/
for (i=0; i<NCX; ++i) {
cx_board_t *b = adapter [i];
if (!b || ! b->port)
continue;
untimeout (cx_timeout, 0, timeout_handle);
unregister_swi (SWI_TTY, cx_softintr);
break;
}
for (i=0; i<NCX*NCHAN; ++i) {
drv_t *d = channel[i];
cx_chan_t *c;
if (!d || (c=d->chan)->type == T_NONE)
continue;
if (d->dtr_timeout_handle.callout)
untimeout (cx_dtrwakeup, d, d->dtr_timeout_handle);
if (d->dcd_timeout_handle.callout)
untimeout (cx_carrier, c, d->dcd_timeout_handle);
}
/* Close all active boards. */
for (i=0; i<NCX; ++i) {
cx_board_t *b = adapter [i];
if (!b || ! b->port)
continue;
cx_close_board (b);
}
for (i=0; i<NCX; ++i) {
cx_board_t *b = adapter [i];
if (!b || ! b->port)
continue;
if (led_timo[i].callout)
untimeout (cx_led_off, b, led_timo[i]);
}
/* OK to unload the driver, unregister the interrupt first. */
for (i=0; i<NCX; ++i) {
cx_board_t *b = adapter [i];
if (!b || ! b->port)
continue;
/* Disable the interrupt request. */
disable_intr();
unregister_intr (b->irq, (inthand2_t *)cx_intr);
isa_dma_release (b->dma);
enable_intr();
}
splx (s);
s = splhigh ();
/* Detach the interfaces, free buffer memory. */
for (i=0; i<NCX*NCHAN; ++i) {
drv_t *d = channel[i];
cx_chan_t *c;
if (!d || (c=d->chan)->type == T_NONE)
continue;
#ifndef NETGRAPH
#if NBPFILTER > 0
/* Detach from the packet filter list of interfaces. */
{
struct bpf_if *q, **b = &bpf_iflist;
while ((q = *b)) {
if (q->bif_ifp == d->pp.pp_if) {
*b = q->bif_next;
free (q, M_DEVBUF);
}
b = &(q->bif_next);
}
}
#endif /* NBPFILTER */
/* Detach from the sync PPP list. */
sppp_detach (&d->pp.pp_if);
/* Detach from the system list of interfaces. */
{
struct ifaddr *ifa;
TAILQ_FOREACH (ifa, &d->pp.pp_if.if_addrhead, ifa_link) {
TAILQ_REMOVE (&d->pp.pp_if.if_addrhead, ifa, ifa_link);
free (ifa, M_IFADDR);
}
TAILQ_REMOVE (&ifnet, &d->pp.pp_if, if_link);
}
#endif /* !NETGRAPH */
/* Deallocate buffers. */
/* free (d, M_DEVBUF);*/
}
for (i=0; i<NCX; ++i) {
cx_board_t *b = adapter [i];
if (!b)
continue;
adapter [b->num] = 0;
free (b, M_DEVBUF);
}
splx (s);
return 0;
}
#define devsw(a) cdevsw[major((a))]
#endif /* __FreeBSD_version < 400000 */
#endif /* KLD_MODULE */
#if __FreeBSD_version < 400000
#ifdef KLD_MODULE
static int cx_modevent (module_t mod, int type, void *unused)
{
dev_t dev;
int result;
static int load_count = 0;
dev = makedev (CDEV_MAJOR, 0);
switch (type) {
case MOD_LOAD:
if (devsw(dev))
return (ENXIO);
load_count ++;
cdevsw_add (&dev, &cx_cdevsw, NULL);
timeout_handle = timeout (cx_timeout, 0, hz*5);
/* Software interrupt. */
register_swi (SWI_TTY, cx_softintr);
result = cx_load ();
return result;
case MOD_UNLOAD:
result = cx_unload ();
if (result)
return result;
if (devsw(dev)&&!(load_count-1)) {
cdevsw_add (&dev, NULL, NULL);
}
load_count --;
return result;
case MOD_SHUTDOWN:
break;
}
return 0;
}
#endif /* KLD_MODULE */
#else /* __FreeBSD_version >= 400000 */
static int cx_modevent (module_t mod, int type, void *unused)
{
dev_t dev;
static int load_count = 0;
struct cdevsw *cdsw;
#if __FreeBSD_version >= 502103
dev = udev2dev (makeudev(CDEV_MAJOR, 0));
#else
dev = makedev (CDEV_MAJOR, 0);
#endif
switch (type) {
case MOD_LOAD:
if (dev != NODEV &&
(cdsw = devsw (dev)) &&
cdsw->d_maj == CDEV_MAJOR) {
printf ("Sigma driver is already in system\n");
return (EEXIST);
}
#if __FreeBSD_version >= 500000 && defined NETGRAPH
if (ng_newtype (&typestruct))
printf ("Failed to register ng_cx\n");
#endif
++load_count;
#if __FreeBSD_version <= 500000
cdevsw_add (&cx_cdevsw);
#endif
timeout_handle = timeout (cx_timeout, 0, hz*5);
/* Software interrupt. */
#if __FreeBSD_version < 500000
register_swi (SWI_TTY, cx_softintr);
#else
swi_add(&tty_ithd, "tty:cx", cx_softintr, NULL, SWI_TTY, 0,
&cx_fast_ih);
#endif
break;
case MOD_UNLOAD:
if (load_count == 1) {
printf ("Removing device entry for Sigma\n");
#if __FreeBSD_version <= 500000
cdevsw_remove (&cx_cdevsw);
#endif
#if __FreeBSD_version >= 500000 && defined NETGRAPH
ng_rmtype (&typestruct);
#endif
}
if (timeout_handle.callout)
untimeout (cx_timeout, 0, timeout_handle);
#if __FreeBSD_version >= 500000
ithread_remove_handler (cx_fast_ih);
#else
unregister_swi (SWI_TTY, cx_softintr);
#endif
--load_count;
break;
case MOD_SHUTDOWN:
break;
}
return 0;
}
#endif /* __FreeBSD_version >= 400000 */
#ifdef NETGRAPH
static struct ng_type typestruct = {
.version = NG_ABI_VERSION,
.name = NG_CX_NODE_TYPE,
.constructor = ng_cx_constructor,
.rcvmsg = ng_cx_rcvmsg,
.shutdown = ng_cx_rmnode,
.newhook = ng_cx_newhook,
.connect = ng_cx_connect,
.rcvdata = ng_cx_rcvdata,
.disconnect = ng_cx_disconnect
};
#if __FreeBSD_version < 400000
NETGRAPH_INIT_ORDERED (cx, &typestruct, SI_SUB_DRIVERS,\
SI_ORDER_MIDDLE + CDEV_MAJOR);
#endif
#endif /*NETGRAPH*/
#if __FreeBSD_version >= 500000
#ifdef NETGRAPH
MODULE_DEPEND (ng_cx, netgraph, NG_ABI_VERSION, NG_ABI_VERSION, NG_ABI_VERSION);
#else
MODULE_DEPEND (isa_cx, sppp, 1, 1, 1);
#endif
#ifdef KLD_MODULE
DRIVER_MODULE (cxmod, isa, cx_isa_driver, cx_devclass, cx_modevent, NULL);
#else
DRIVER_MODULE (cx, isa, cx_isa_driver, cx_devclass, cx_modevent, NULL);
#endif
#elif __FreeBSD_version >= 400000
#ifdef NETGRAPH
DRIVER_MODULE(cx, isa, cx_isa_driver, cx_devclass, ng_mod_event, &typestruct);
#else
DRIVER_MODULE(cx, isa, cx_isa_driver, cx_devclass, cx_modevent, 0);
#endif
#else /* __FreeBSD_version < 400000 */
#ifdef KLD_MODULE
#ifndef NETGRAPH
static moduledata_t cxmod = { "cx", cx_modevent, NULL};
DECLARE_MODULE (cx, cxmod, SI_SUB_DRIVERS, SI_ORDER_MIDDLE + CDEV_MAJOR);
#endif
#else /* KLD_MODULE */
/*
* Now for some driver initialisation.
* Occurs ONCE during boot (very early).
* This is if we are NOT a loadable module.
*/
static void cx_drvinit (void *unused)
{
#if __FreeBSD_version < 400000
dev_t dev;
dev = makedev (CDEV_MAJOR, 0);
cdevsw_add (&dev, &cx_cdevsw, NULL);
#else
cdevsw_add (&cx_cdevsw);
#endif
/* Activate the timeout routine. */
timeout_handle = timeout (cx_timeout, 0, hz*5);
/* Software interrupt. */
register_swi (SWI_TTY, cx_softintr);
#ifdef NETGRAPH
#if 0
/* Register our node type in netgraph */
if (ng_newtype (&typestruct))
printf ("Failed to register ng_cx\n");
#endif
#endif
}
SYSINIT (cxdev, SI_SUB_DRIVERS, SI_ORDER_MIDDLE+CDEV_MAJOR, cx_drvinit, 0)
#endif /* KLD_MODULE */
#endif /* __FreeBSD_version < 400000 */
#endif /* NCX */
|