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
|
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2008 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
/*
* DESCRIPTION
*
* ttymux - Multiplexer driver for multiplexing termio compliant streams onto
* a single upper stream.
*
* ADD2FRONT macro can be used to specify the order in which a console
* device is put in the queue of multiplexed physical serial devices,
* during the association and disassociation of a console interface.
* When this macro is defined, the device is placed in front of the queue,
* otherwise by default it is placed at the end.
* Console I/O happens to each of the physical devices in the order of
* their position in this queue.
*/
#include <sys/types.h>
#include <sys/file.h>
#include <sys/stream.h>
#include <sys/strsubr.h>
#include <sys/strlog.h>
#include <sys/strsun.h>
#include <sys/modctl.h>
#include <sys/debug.h>
#include <sys/kbio.h>
#include <sys/devops.h>
#include <sys/errno.h>
#include <sys/stat.h>
#include <sys/kmem.h>
#include <sys/ddi.h>
#include <sys/consdev.h>
#include <sys/tty.h>
#include <sys/ptyvar.h>
#include <sys/termio.h>
#include <sys/fcntl.h>
#include <sys/mkdev.h>
#include <sys/ser_sync.h>
#include <sys/esunddi.h>
#include <sys/policy.h>
#include <sys/ttymux.h>
#include "ttymux_impl.h"
/*
* Extern declarations
*/
extern mblk_t *mkiocb(uint_t);
extern int nulldev();
extern uintptr_t space_fetch(char *key);
extern int sm_ioctl_cmd(sm_uqi_t *, mblk_t *);
extern int ttymux_abort_ioctl(mblk_t *);
extern int ttymux_device_fini(sm_lqi_t *);
extern int ttymux_device_init(sm_lqi_t *);
/*
* Exported interfaces
*/
int sm_disassociate(int, sm_lqi_t *, ulong_t);
int sm_associate(int, sm_lqi_t *, ulong_t, uint_t, char *);
/*
* Variables defined here and visible only internally
*/
sm_ss_t *sm_ssp = 0;
static int sm_instance = 0;
static int smctlunit;
static uint_t sm_default_trflag = 0;
uint_t sm_max_units = 6;
uint_t sm_minor_cnt = 0;
static uint_t sm_refuse_opens = 0;
/*
* Local definitions.
*/
/* force these flags to be unset on console devices */
static ulong_t sm_cmask = (ulong_t)(CRTSXOFF|CRTSCTS);
/*
* SECTION
* Implementation Section:
*/
void
sm_debug(char *msg, ...)
{
va_list args;
char buf[256];
int sz;
va_start(args, msg);
sz = vsnprintf(buf, sizeof (buf), msg, args);
va_end(args);
if (sz < 0)
(void) strlog(ddi_driver_major(sm_ssp->sm_dip), sm_instance, 1,
SL_TRACE, "vsnprintf parse error\n");
else if (sz > sizeof (buf)) {
char *b;
size_t len = sz + 1;
b = kmem_alloc(len, KM_SLEEP);
va_start(args, msg);
sz = vsnprintf(b, len, msg, args);
va_end(args);
if (sz > 0)
(void) strlog(ddi_driver_major(sm_ssp->sm_dip),
sm_instance, 1, SL_TRACE, b);
kmem_free(b, len);
} else {
(void) strlog(ddi_driver_major(sm_ssp->sm_dip), sm_instance,
1, SL_TRACE, buf);
}
}
void
sm_log(char *msg, ...)
{
va_list args;
char buf[128];
int sz;
va_start(args, msg);
sz = vsnprintf(buf, sizeof (buf), msg, args);
va_end(args);
if (sz < 0)
(void) strlog(ddi_driver_major(sm_ssp->sm_dip), sm_instance, 1,
SL_TRACE, "vsnprintf parse error\n");
else if (sz > sizeof (buf)) {
char *b;
size_t len = sz + 1;
b = kmem_alloc(len, KM_SLEEP);
va_start(args, msg);
sz = vsnprintf(b, len, msg, args);
va_end(args);
if (sz > 0)
(void) strlog(ddi_driver_major(sm_ssp->sm_dip),
sm_instance, 1, SL_NOTE, b);
kmem_free(b, len);
} else {
(void) strlog(ddi_driver_major(sm_ssp->sm_dip), sm_instance,
1, SL_NOTE, buf);
}
}
/*
* Should only be called if the caller can guarantee that the vnode
* and/or the stream won't disappear while finding the dip.
* This routine is only called during an I_PLINK request so it's safe.
* The routine obtains the dev_t for a linked se stream.
*/
static void
sm_setdip(queue_t *q, sm_lqi_t *lqi)
{
lqi->sm_dev = q && STREAM(q) ? STREAM(q)->sd_vnode->v_rdev : NODEV;
}
/*
* Called from driver close, state change reports and I_PUNLINK ioctl.
* A lower stream has been unlinked - clean up the state associated with it.
*/
void
sm_lqifree(sm_lqi_t *lqi)
{
int mu_owned;
sm_lqi_t **pplqi;
ASSERT(mutex_owned(lqi->sm_umutex));
ASSERT(SM_RQ(lqi) != 0);
/*
* Clear all state associated with this lower queue except
* the identity of the queues themselves and the link id which
* can only be cleared by issuing a streams I_PUNLINK ioctl.
*
* The association of a lower queue is a two step process:
* 1. initialise the lower q data structure on I_PLINK
* 2. associate an upper q with the lower q on SM_CMD_ASSOCIATE.
*
* If step 2 has ocurred then
* remove this lower queue info from the logical unit.
*/
if (lqi->sm_uqi) {
sm_dbg('Y', ("lqifree unit %d, ", lqi->sm_uqi->sm_lunit));
if ((mu_owned = mutex_owned(lqi->sm_uqi->sm_umutex)) == 0)
LOCK_UNIT(lqi->sm_uqi);
pplqi = &lqi->sm_uqi->sm_lqs;
while (*pplqi != lqi) {
ASSERT(*pplqi);
pplqi = &((*pplqi)->sm_nlqi);
}
*pplqi = lqi->sm_nlqi;
lqi->sm_uqi->sm_nlqs--;
if (mu_owned == 0)
UNLOCK_UNIT(lqi->sm_uqi);
lqi->sm_uqi = 0;
}
}
/*
* Given a q return the associated lower queue data structure or NULL.
* Return the data locked.
*/
static sm_lqi_t *
get_lqi_byq(queue_t *q)
{
int i;
sm_lqi_t *lqi, *flqi = 0;
for (i = 0; i < MAX_LQS; i++) {
lqi = &sm_ssp->sm_lqs[i];
LOCK_UNIT(lqi);
if (flqi == 0 && lqi->sm_linkid == 0) /* assumes muxids != 0 */
flqi = lqi;
else if (SM_RQ(lqi) == q || SM_WQ(lqi) == q) {
if (flqi)
UNLOCK_UNIT(flqi);
return (lqi);
}
else
UNLOCK_UNIT(lqi);
}
return (flqi);
}
/*
* Given a streams link identifier return the associated lower queue data
* structure or NULL.
*/
sm_lqi_t *
get_lqi_byid(int linkid)
{
int i;
sm_lqi_t *lqi;
if (linkid == 0)
return (NULL);
for (i = 0; i < MAX_LQS; i++) {
lqi = &sm_ssp->sm_lqs[i];
if (lqi->sm_linkid == linkid)
return (lqi);
}
return (NULL);
}
/*
* Given a dev_t for a lower stream return the associated lower queue data
* structure or NULL.
*/
sm_lqi_t *
get_lqi_bydevt(dev_t dev)
{
int i;
sm_lqi_t *lqi;
if (dev == NODEV)
return (NULL);
for (i = 0; i < MAX_LQS; i++) {
lqi = &sm_ssp->sm_lqs[i];
if (lqi->sm_dev == dev)
return (lqi);
}
return (NULL);
}
/*
* Determine whether the input flag is set on at least
* howmany queues.
*/
static int
sm_is_flag_set(sm_uqi_t *uqi, uint_t flag, uint_t howmany)
{
sm_lqi_t *lqi;
if (howmany == 0)
return (0);
for (lqi = uqi->sm_lqs; lqi; lqi = lqi->sm_nlqi) {
if (lqi->sm_flags & flag)
if (--howmany == 0)
return (1);
}
return (0);
}
/*
* How many usable queues are associated with a given upper stream
*/
static int
sm_uwq_error(sm_uqi_t *uqi)
{
return (sm_is_flag_set(uqi, (WERROR_MODE|HANGUP_MODE), uqi->sm_nlqs));
}
/*
* How many of the queues associated with a given upper stream
* - do not - have the given flags set.
*/
static int
sm_q_count(sm_uqi_t *uqi, uint_t flag)
{
sm_lqi_t *lqi;
int count = 0;
for (lqi = uqi->sm_lqs; lqi; lqi = lqi->sm_nlqi) {
if ((lqi->sm_flags & flag) == 0)
count++;
}
return (count);
}
/*
* How many of the queues associated with a given upper stream
* - do not - have the given flags set.
*/
static int
sm_qs_without(sm_uqi_t *uqi, uint_t flag, uint_t ioflag)
{
sm_lqi_t *lqi;
int count = 0;
for (lqi = uqi->sm_lqs; lqi; lqi = lqi->sm_nlqi) {
if ((lqi->sm_flags & flag) == 0 &&
(lqi->sm_ioflag & ioflag) == 0)
count++;
}
return (count);
}
/*
* How many usable queues are associated with a given upper stream
*/
static int
sm_good_qs(sm_uqi_t *uqi)
{
return (sm_q_count(uqi, (WERROR_MODE|HANGUP_MODE)));
}
static int
sm_cnt_oqs(sm_uqi_t *uqi)
{
return (sm_qs_without(uqi, (WERROR_MODE|HANGUP_MODE),
(uint_t)FOROUTPUT));
}
/*
* Send an ioctl downstream and remember that it was sent so that
* its response can be caught on the way back up.
*/
static void
sm_issue_ioctl(void *arg)
{
sm_lqi_t *lqi = arg;
uint_t cmdflag = 0;
queue_t *q = SM_WQ(lqi);
int iocmd, size;
LOCK_UNIT(lqi);
lqi->sm_bid = 0;
if ((lqi->sm_flags & (WERROR_MODE|HANGUP_MODE)) == 0 &&
(lqi->sm_flags & (WANT_CDSTAT|WANT_TCSET))) {
mblk_t *pioc;
if (lqi->sm_flags & WANT_TCSET) {
lqi->sm_flags &= ~WANT_TCSET;
iocmd = TCSETS;
cmdflag = WANT_TCSET;
} else if (lqi->sm_flags & WANT_SC) {
lqi->sm_flags &= ~WANT_SC;
iocmd = TIOCGSOFTCAR;
cmdflag = WANT_SC;
} else if (lqi->sm_flags & WANT_CD) {
lqi->sm_flags &= ~WANT_CD;
iocmd = TIOCMGET;
} else if (lqi->sm_flags & WANT_CL) {
lqi->sm_flags &= ~WANT_CL;
iocmd = TCGETS;
cmdflag = WANT_CL;
} else {
UNLOCK_UNIT(lqi);
return;
}
if (pioc = mkiocb(iocmd)) {
if (cmdflag == WANT_TCSET) {
pioc->b_cont =
sm_allocb(sizeof (struct termios),
BPRI_MED);
if (pioc->b_cont == 0) {
freemsg(pioc);
pioc = 0;
} else {
struct termios *tc = (struct termios *)
pioc->b_cont->b_wptr;
bzero((caddr_t)tc,
sizeof (struct termios));
tc->c_cflag = lqi->sm_ttycommon->
t_cflag;
pioc->b_cont->b_rptr =
pioc->b_cont->b_wptr;
pioc->b_cont->b_wptr +=
sizeof (struct termios);
}
size = sizeof (struct iocblk) +
sizeof (struct termios);
}
else
size = sizeof (struct iocblk);
}
else
size = sizeof (struct iocblk);
if (pioc != 0) {
lqi->sm_piocid = ((struct iocblk *)pioc->b_rptr)->
ioc_id;
lqi->sm_flags |= SM_IOCPENDING;
/* lqi->sm_flags |= cmdflag; */
UNLOCK_UNIT(lqi);
(void) putq(q, pioc);
} else {
UNLOCK_UNIT(lqi);
lqi->sm_bid = qbufcall(WR(q), size, BPRI_MED,
sm_issue_ioctl, lqi);
}
}
else
UNLOCK_UNIT(lqi);
}
/*
* Associate one of the drivers minor nodes with a serial device.
*/
int
sm_associate(int unit, sm_lqi_t *plqi, ulong_t tag, uint_t ioflag, char *dp)
{
sm_uqi_t *uqi;
int rval = 0;
sm_dbg('Y', ("sm_associate(%d, %d, %d): ",
(plqi) ? plqi->sm_linkid : 0, unit, ioflag));
/*
* Check the data is valid.
* Associate a lower queue with a logical unit.
*/
if (unit < 0 || unit >= NLUNITS || plqi == 0 ||
(uqi = get_uqi(sm_ssp, unit)) == 0) {
sm_dbg('@', (" invalid: lqi=0x%p lui=0x%p:", plqi, uqi));
rval = EINVAL;
} else {
if ((ioflag & FORIO) == 0)
ioflag = FORIO;
LOCK_UNIT(plqi);
if (plqi->sm_uqi) {
if (plqi->sm_uqi->sm_lunit == unit) {
if ((ioflag & (uint_t)FORIO) != 0)
plqi->sm_ioflag =
(ioflag & (uint_t)FORIO);
rval = 0;
} else {
sm_dbg('@', ("already associated with unit %d:",
plqi->sm_uqi->sm_lunit));
rval = EINVAL;
}
} else {
LOCK_UNIT(uqi);
if ((ioflag & (uint_t)FORIO) != 0)
plqi->sm_ioflag = (ioflag & (uint_t)FORIO);
plqi->sm_ttycommon->t_cflag = uqi->sm_ttycommon->
t_cflag;
plqi->sm_ttycommon->t_flags = uqi->sm_ttycommon->
t_flags;
plqi->sm_uqi = uqi;
plqi->sm_mbits = 0;
plqi->sm_tag = tag;
if (*dp == '/')
(void) strncpy(plqi->sm_path, dp, MAXPATHLEN);
else
*(plqi->sm_path) = '\0';
plqi->sm_flags |= WANT_TCSET;
#ifdef ADD2FRONT
plqi->sm_nlqi = uqi->sm_lqs;
uqi->sm_lqs = plqi;
#else
plqi->sm_nlqi = 0;
if (uqi->sm_lqs) {
sm_lqi_t *lq;
for (lq = uqi->sm_lqs; lq->sm_nlqi;
lq = lq->sm_nlqi) {
}
lq->sm_nlqi = plqi;
} else
uqi->sm_lqs = plqi;
#endif
uqi->sm_nlqs++;
(void) ttymux_device_init(plqi);
UNLOCK_UNIT(uqi);
rval = 0;
/*
* Everything looks good so it's now ok to enable lower
* queue processing.
* Note the lower queue should be enabled as soon as
* I_PLINK returns (used in sm_get_ttymodes etc).
* Schedule ioctls to obtain the terminal settings.
*/
if ((uqi->sm_flags & FULLY_OPEN) || uqi->sm_waitq)
plqi->sm_uqflags |= SM_UQVALID;
qenable(SM_RQ(plqi));
if (plqi->sm_flags & (WANT_CDSTAT|WANT_TCSET)) {
/*
* Bypass the lower half of the driver (hence
* no qwriter) and apply the current termio
* settings on the lower stream.
*/
UNLOCK_UNIT(plqi);
if (plqi->sm_bid) {
qunbufcall(SM_WQ(plqi), plqi->sm_bid);
plqi->sm_bid = 0;
}
/*
* Only set cflags on the lower q if we know
* the settings on any other lower queue.
*/
sm_issue_ioctl(plqi);
LOCK_UNIT(plqi);
}
}
UNLOCK_UNIT(plqi);
}
sm_dbg('Y', ("sm_associate: rval=%d.\n", rval));
return (rval);
}
/*
* Break an association between one of the driver's minor nodes and
* a serial device.
*/
int
sm_disassociate(int unit, sm_lqi_t *plqi, ulong_t tag)
{
sm_uqi_t *uqi;
int rval = 0;
sm_dbg('Y', ("sm_disassociate: link %d, unit %d: ",
(plqi) ? plqi->sm_linkid : 0, unit));
/*
* Check the data is valid.
* Disassociate a lower queue with a logical unit.
*/
if (unit < 0 || unit >= NLUNITS || plqi == 0 ||
(uqi = get_uqi(sm_ssp, unit)) == 0) {
sm_dbg('@', ("invalid: lqi=0x%p lui=0x%p", plqi, uqi));
rval = EINVAL;
} else {
LOCK_UNIT(plqi);
if (plqi->sm_uqi == NULL) {
sm_dbg('@', ("unit not associated"));
rval = EINVAL;
} else if (plqi->sm_uqi->sm_lunit != unit) {
sm_dbg('@', ("unit and linkid not related",
plqi->sm_uqi->sm_lunit));
rval = EINVAL;
} else if (plqi->sm_tag != tag) {
sm_dbg('@',
("Invalid tag for TTYMUX_DISASSOC ioctl\n"));
rval = EPERM;
} else {
sm_dbg('Y', ("disassociating "));
(void) ttymux_device_fini(plqi);
/*
* Indicate that carrier status is no
* longer required and that the upper
* queue should not be used by plqi
*/
plqi->sm_flags &= ~(WANT_CDSTAT|WANT_TCSET);
plqi->sm_uqflags &= ~(SM_UQVALID|SM_OBPCNDEV);
plqi->sm_ioflag = 0u;
sm_lqifree(plqi);
rval = 0;
}
UNLOCK_UNIT(plqi);
}
sm_dbg('Y', (" rval=%d.\n", rval));
return (rval);
}
/*
* Streams helper routines;
*/
/*
* Schedule a qbufcall for an upper queue.
* Must be called within the perimiter of the parameter q.
* fn must reenable the q.
* Called:
* whenever a message must be placed on multiple queues and allocb fails;
*/
static void
sm_sched_uqcb(queue_t *q, int memreq, int pri, void (*fn)())
{
sm_uqi_t *uqi = q->q_ptr;
if (uqi->sm_ttybid != 0)
qunbufcall(q, uqi->sm_ttybid);
noenable(q);
uqi->sm_ttybid = qbufcall(q, memreq, pri, fn, uqi);
}
/*
* qbufcall routine to restart the queues when memory is available.
*/
static void
sm_reenable_q(sm_uqi_t *uqi)
{
queue_t *wq = SM_WQ(uqi);
if ((uqi->sm_flags & SM_STOPPED) == 0) {
enableok(wq);
qenable(wq);
}
}
/*
* Place a message on the write queue of each stream associated with
* the given upper stream.
*/
static void
sm_senddown(sm_uqi_t *uqi)
{
sm_lqi_t *lqi;
for (lqi = uqi->sm_lqs; lqi != 0; lqi = lqi->sm_nlqi) {
if (lqi->sm_mp != 0) {
putnext(SM_WQ(lqi), lqi->sm_mp);
lqi->sm_mp = 0;
}
}
}
/*
* For each lower device that should receive a write message duplicate
* the message block.
*/
static int
sm_dupmsg(sm_uqi_t *uqi, mblk_t *mp)
{
sm_lqi_t *lqi;
mblk_t *origmp = mp;
for (lqi = uqi->sm_lqs; lqi != 0; lqi = lqi->sm_nlqi) {
lqi->sm_mp = 0;
if (lqi->sm_flags & WERROR_MODE) {
continue;
}
if ((lqi->sm_ioflag & (uint_t)FOROUTPUT) == 0) {
if (DB_TYPE(mp) == M_DATA)
continue;
}
if (lqi->sm_nlqi == 0) {
lqi->sm_mp = mp;
origmp = NULL;
} else if ((lqi->sm_mp = sm_copymsg(mp)) == 0) {
sm_lqi_t *flqi;
for (flqi = uqi->sm_lqs; flqi != lqi;
flqi = flqi->sm_nlqi) {
if (lqi->sm_mp) {
/* must have been sm_copymsg */
sm_freemsg(lqi->sm_mp);
lqi->sm_mp = 0;
}
}
return (sm_cnt_oqs(uqi) * msgdsize(mp));
}
}
if (origmp != NULL)
freemsg(origmp);
return (0);
}
/*
* Return 1 if all associated lower devices have room for another message
* otherwise return 0.
*/
static int
sm_cansenddown(sm_uqi_t *uqi)
{
register sm_lqi_t *lqi;
if (uqi->sm_lqs == 0)
return (0);
for (lqi = uqi->sm_lqs; lqi != 0; lqi = lqi->sm_nlqi) {
if ((lqi->sm_flags & WERROR_MODE) == 0 &&
canputnext(SM_WQ(lqi)) == 0)
return (0);
}
return (1);
}
/*
* Put a message down all associated lower queues.
* Return 1 if the q function was called.
*/
static int
sm_putqs(queue_t *q, mblk_t *mp, int (*qfn)())
{
register sm_uqi_t *uqi = (sm_uqi_t *)q->q_ptr;
register int memreq;
int pri = (DB_TYPE(mp) < QPCTL) ? BPRI_MED : BPRI_HI;
int rval = 0;
if (uqi->sm_lqs == 0 || (uqi->sm_flags & WERROR_MODE)) {
sm_dbg('Q', ("sm_putqs: freeing (0x%p 0x%p).\n", uqi->sm_lqs,
uqi->sm_flags));
freemsg(mp);
} else if (pri != BPRI_HI && sm_cansenddown(uqi) == 0) {
/* a lower q is flow controlled */
(void) qfn(q, mp);
rval = 1;
} else if ((memreq = sm_dupmsg(uqi, mp)) == 0) {
sm_senddown(uqi);
} else {
sm_log("sm_putqs: msg 0x%x - can't alloc %d bytes (pri %d).\n",
DB_TYPE(mp), memreq, pri);
sm_sched_uqcb(q, memreq, pri, sm_reenable_q);
(void) qfn(q, mp);
rval = 1;
}
return (rval);
}
/*
* Service a streams link and unlink requests.
*/
static void
sm_link_req(queue_t *wq, mblk_t *mp)
{
struct linkblk *linkp;
int rval;
int cmd;
sm_lqi_t *plqi;
ASSERT(DB_TYPE(mp) == M_IOCTL);
cmd = ((struct iocblk *)mp->b_rptr)->ioc_cmd;
switch (cmd) {
case I_LINK:
case I_PLINK:
sm_dbg('G', ("sm_link_req: M_IOCTL %x (I_PLINK).\n", cmd));
linkp = (struct linkblk *)mp->b_cont->b_rptr;
/*
* 1. Sanity check the link block.
* 2. Validate that the queue is not already linked
* (and resources available).
* 3. Validate that the lower queue is not associated with
* a logical unit.
* 4. Remember that this lower queue is linked to the driver.
*/
if ((linkp == NULL) || (MBLKL(mp) < sizeof (*linkp)) ||
linkp->l_qbot == NULL) {
sm_dbg('I', ("sm_link_req: invalid link block.\n"));
rval = EINVAL;
} else if ((plqi = get_lqi_byq(linkp->l_qbot)) == 0) {
sm_dbg('I', ("sm_link_req: out of resources.\n"));
rval = EBUSY; /* out of resources */
} else if (plqi->sm_uqi) {
UNLOCK_UNIT(plqi); /* was aquired by get_lqi_byq */
sm_dbg('I', ("sm_link_req: already associated.\n"));
rval = EBUSY; /* already linked */
} else {
SM_WQ(plqi) = linkp->l_qbot;
SM_RQ(plqi) = OTHERQ(linkp->l_qbot);
linkp->l_qbot->q_ptr =
OTHERQ(linkp->l_qbot)->q_ptr = plqi;
plqi->sm_linkid = linkp->l_index;
UNLOCK_UNIT(plqi); /* was aquired by get_lqi_byq */
sm_dbg('H', ("sm_link_req: linkid = %d.\n",
linkp->l_index));
sm_setdip(linkp->l_qbot, plqi);
plqi->sm_ttycommon->t_flags = 0;
plqi->sm_ttycommon->t_cflag = 0;
plqi->sm_mbits = 0;
(void) ttymux_device_init(plqi);
rval = 0;
}
break;
case I_UNLINK:
case I_PUNLINK:
sm_dbg('G', ("sm_link_req: M_IOCTL (I_PUNLINK).\n"));
linkp = (struct linkblk *)mp->b_cont->b_rptr;
if ((linkp == NULL) ||
(MBLKL(mp) < sizeof (*linkp)) ||
linkp->l_qbot == NULL) {
rval = EINVAL;
} else if ((plqi = get_lqi_byid(linkp->l_index)) == 0) {
rval = EINVAL;
} else {
sm_uqi_t *uqi;
int werrmode;
/*
* Mark the lower q as invalid.
*/
sm_dbg('G', ("I_PUNLINK: freeing link %d\n",
linkp->l_index));
if (plqi->sm_bid) {
qunbufcall(SM_RQ(plqi), plqi->sm_bid);
plqi->sm_bid = 0;
}
if (plqi->sm_ttybid) {
qunbufcall(SM_RQ(plqi), plqi->sm_ttybid);
plqi->sm_ttybid = 0;
}
uqi = plqi->sm_uqi;
(void) ttymux_device_fini(plqi);
if (uqi)
(void) sm_disassociate(uqi->sm_lunit,
plqi, plqi->sm_tag);
LOCK_UNIT(plqi);
plqi->sm_piocid = 0;
werrmode = (plqi->sm_flags & (WERROR_MODE|HANGUP_MODE))
? 1 : 0;
plqi->sm_mbits = 0;
plqi->sm_flags = 0;
ttycommon_close(plqi->sm_ttycommon);
/* SM_RQ(plqi) = SM_WQ(plqi) = 0; */
plqi->sm_ttycommon->t_flags = 0;
plqi->sm_ttycommon->t_cflag = 0;
plqi->sm_ttycommon->t_iflag = 0;
plqi->sm_linkid = 0;
plqi->sm_dev = NODEV;
plqi->sm_hadkadbchar = 0;
plqi->sm_nachar = sm_ssp->sm_abs;
UNLOCK_UNIT(plqi);
if (uqi &&
werrmode &&
(uqi->sm_flags & FULLY_OPEN) &&
sm_uwq_error(uqi) &&
putnextctl(SM_RQ(uqi), M_HANGUP) == 0) {
sm_log("sm_link_req: putnextctl(M_HANGUP)"
" failed.\n");
}
rval = 0;
}
break;
default:
rval = EINVAL;
}
if (rval != 0)
miocnak(wq, mp, 0, rval);
else
miocack(wq, mp, 0, 0);
}
static int
sm_getiocinfo(mblk_t *mp, struct sm_iocinfo *info)
{
switch (DB_TYPE(mp)) {
case M_COPYOUT:
info->sm_id = ((struct copyreq *)mp->b_rptr)->cq_id;
info->sm_cmd = ((struct copyreq *)mp->b_rptr)->cq_cmd;
info->sm_data = (((struct copyreq *)mp->b_rptr)->cq_size &&
mp->b_cont) ? (void *)mp->b_cont->b_rptr : 0;
break;
case M_COPYIN:
info->sm_id = ((struct copyresp *)mp->b_rptr)->cp_id;
info->sm_cmd = ((struct copyresp *)mp->b_rptr)->cp_cmd;
info->sm_data = 0;
break;
case M_IOCACK:
info->sm_id = ((struct iocblk *)mp->b_rptr)->ioc_id;
info->sm_cmd = ((struct iocblk *)mp->b_rptr)->ioc_cmd;
/* the se driver has bug so we cannot use ioc_count */
info->sm_data = (((struct iocblk *)mp->b_rptr)->
ioc_error == 0 && mp->b_cont) ?
(void *)mp->b_cont->b_rptr : 0;
break;
case M_IOCNAK:
info->sm_id = ((struct iocblk *)mp->b_rptr)->ioc_id;
info->sm_cmd = ((struct iocblk *)mp->b_rptr)->ioc_cmd;
info->sm_data = 0;
break;
case M_IOCDATA:
info->sm_id = ((struct copyresp *)mp->b_rptr)->cp_id;
info->sm_cmd = ((struct copyresp *)mp->b_rptr)->cp_cmd;
info->sm_data = (((struct copyresp *)mp->b_rptr)->
cp_rval == 0 && mp->b_cont) ?
(void *)mp->b_cont->b_rptr : 0;
break;
case M_IOCTL:
info->sm_id = ((struct iocblk *)mp->b_rptr)->ioc_id;
info->sm_cmd = ((struct iocblk *)mp->b_rptr)->ioc_cmd;
info->sm_data = 0;
break;
default:
return (EINVAL);
}
return (0);
}
/*
* Record the termio settings that have been set on the upper stream
*/
static int
sm_update_ttyinfo(mblk_t *mp, sm_uqi_t *uqi)
{
int err;
struct sm_iocinfo info;
if ((err = sm_getiocinfo(mp, &info)) != 0)
return (err);
switch (info.sm_cmd) {
case TIOCSPPS:
case TIOCGPPS:
case TIOCGPPSEV:
return (ENOTSUP);
case TIOCGWINSZ:
case TIOCSWINSZ:
break;
case TCSBRK:
case TIOCSBRK:
case TIOCCBRK:
break;
case TCSETSF:
uqi->sm_flags |= FLUSHR_PEND;
sm_dbg('I', ("TCSETSF: FLUSH is pending\n"));
/*FALLTHROUGH*/
case TCSETSW:
case TCSETS:
case TCGETS:
if (info.sm_data != 0) {
((struct termios *)info.sm_data)->c_cflag &=
(tcflag_t)(~uqi->sm_cmask);
uqi->sm_ttycommon->t_cflag =
((struct termios *)info.sm_data)->c_cflag;
}
break;
case TCSETAF:
sm_dbg('I', ("TCSETAF: FLUSH is pending\n"));
uqi->sm_flags |= FLUSHR_PEND;
/*FALLTHROUGH*/
case TCSETAW:
case TCSETA:
case TCGETA:
if (info.sm_data != 0) {
((struct termio *)info.sm_data)->c_cflag &=
(tcflag_t)(~uqi->sm_cmask);
uqi->sm_ttycommon->t_cflag =
(tcflag_t)((struct termio *)info.sm_data)->c_cflag;
}
break;
case TIOCSSOFTCAR:
case TIOCGSOFTCAR:
if (info.sm_data != 0) {
if (*(int *)info.sm_data == 1)
uqi->sm_ttycommon->t_flags |= TS_SOFTCAR;
else
uqi->sm_ttycommon->t_flags &= ~TS_SOFTCAR;
}
break;
case TIOCMSET:
case TIOCMGET:
if (info.sm_data != 0)
uqi->sm_mbits = *(int *)info.sm_data;
break;
case TIOCMBIS:
if (info.sm_data != 0)
uqi->sm_mbits |= *(int *)info.sm_data;
break;
case TIOCMBIC:
if (info.sm_data != 0)
uqi->sm_mbits &= ~(*(int *)info.sm_data);
break;
default:
return (EINVAL);
/* NOTREACHED */
} /* end switch cmd */
if ((uqi->sm_mbits & TIOCM_CD) ||
(uqi->sm_ttycommon->t_flags & TS_SOFTCAR) ||
(uqi->sm_ttycommon->t_cflag & CLOCAL))
uqi->sm_flags |= SM_CARON;
else
uqi->sm_flags &= ~SM_CARON;
return (0);
}
/*
* SECTION
* STREAM's interface to the OS.
* Routines directly callable from the OS.
*/
/*
* Processes high priority messages comming from modules above the
* multiplexor.
* Return 1 if the queue was disabled.
*/
static int
sm_hp_uwput(queue_t *wq, mblk_t *mp)
{
sm_uqi_t *uqi = (sm_uqi_t *)(wq->q_ptr);
int rval = 0;
sm_lqi_t *plqi;
int msgtype = DB_TYPE(mp);
switch (msgtype) {
case M_FLUSH:
/*
* How to flush the bottom half:
* putctl1(SM_WQ(plqi), *mp->b_rptr)
* will work on the bottom half but if FLUSHR is set
* when is the right time to flush the upper read queue.
*
* Could set uqi->sm_flags & WANT_FLUSH but then what happens
* if FLUSHR is set and the driver sends up a FLUSHR
* before it handles the current FLUSHR request
* (if only there was an id for the message that could
* be matched when it returns back from the drivers.
*
* Thus I'm going by the book - the bottom half acts like
* a stream head and turns around FLUSHW back down to
* the driver (see lrput). The upper half acts like a
* driver and turns around FLUSHR:
*/
sm_dbg('I', ("sm_hp_uwput: FLUSH request 0x%x\n", *mp->b_rptr));
/* flush the upper write queue */
if (*mp->b_rptr & FLUSHW)
flushq(wq, FLUSHDATA);
/*
* flush each associated lower write queue
* and pass down the driver (ignore the FLUSHR and deal with
* it when it comes back up the read side.
*/
for (plqi = uqi->sm_lqs; plqi != 0; plqi = plqi->sm_nlqi) {
if ((plqi->sm_flags & WERROR_MODE) == 0 &&
SM_WQ(plqi)) {
sm_dbg('I', ("flush lq 0x%p\n", SM_WQ(plqi)));
if (*mp->b_rptr & FLUSHW)
flushq(SM_WQ(plqi), FLUSHDATA);
(void) putnextctl1(SM_WQ(plqi), M_FLUSH,
*mp->b_rptr);
}
}
break;
case M_STARTI:
for (plqi = uqi->sm_lqs; plqi != 0; plqi = plqi->sm_nlqi) {
plqi->sm_flags &= ~SM_ISTOPPED;
if ((plqi->sm_flags & WERROR_MODE) == 0)
(void) putnextctl(SM_WQ(plqi), msgtype);
}
break;
case M_STOPI:
for (plqi = uqi->sm_lqs; plqi != 0; plqi = plqi->sm_nlqi) {
plqi->sm_flags |= SM_ISTOPPED;
if ((plqi->sm_flags & WERROR_MODE) == 0)
(void) putnextctl(SM_WQ(plqi), msgtype);
}
break;
case M_STOP: /* must never be queued */
uqi->sm_flags |= SM_STOPPED;
noenable(wq);
for (plqi = uqi->sm_lqs; plqi != 0; plqi = plqi->sm_nlqi)
if ((plqi->sm_flags & WERROR_MODE) == 0)
(void) putnextctl(SM_WQ(plqi), msgtype);
rval = 1;
break;
case M_START: /* never be queued */
uqi->sm_flags &= ~SM_STOPPED;
enableok(wq);
qenable(wq);
for (plqi = uqi->sm_lqs; plqi != 0; plqi = plqi->sm_nlqi)
if ((plqi->sm_flags & WERROR_MODE) == 0)
(void) putnextctl(SM_WQ(plqi), msgtype);
break;
case M_PCSIG:
case M_COPYOUT:
case M_COPYIN:
case M_IOCACK:
case M_IOCNAK:
/* Wrong direction for message */
break;
case M_READ:
break;
case M_PCPROTO:
case M_PCRSE:
default:
sm_dbg('I', ("sm_hp_uwput: default case %d.\n", msgtype));
break;
} /* end switch on high pri message type */
freemsg(mp);
return (rval);
}
static int
sm_default_uwioctl(queue_t *wq, mblk_t *mp, int (*qfn)())
{
int err;
struct iocblk *iobp;
sm_uqi_t *uqi;
uqi = (sm_uqi_t *)(wq->q_ptr);
iobp = (struct iocblk *)mp->b_rptr;
switch (iobp->ioc_cmd) {
case TIOCEXCL:
case TIOCNXCL:
case TIOCSTI:
/*
* The three ioctl types we support do not require any
* additional allocation and should not return a pending
* ioctl state. For this reason it is safe for us to ignore
* the return value from ttycommon_ioctl().
* Additionally, we translate any error response from
* ttycommon_ioctl() into EINVAL.
*/
(void) ttycommon_ioctl(uqi->sm_ttycommon, wq, mp, &err);
if (err < 0)
miocnak(wq, mp, 0, EINVAL);
else
miocack(wq, mp, 0, 0);
return (0);
default:
break;
}
if ((err = sm_update_ttyinfo(mp, uqi)) != 0) {
miocnak(wq, mp, 0, err);
return (0);
}
/*
* If uqi->sm_siocdata.sm_iocid just overwrite it since the stream
* head will have timed it out
*/
uqi->sm_siocdata.sm_iocid = iobp->ioc_id;
uqi->sm_siocdata.sm_acked = 0;
uqi->sm_siocdata.sm_nacks = sm_good_qs(uqi);
uqi->sm_siocdata.sm_acnt = 0;
uqi->sm_siocdata.sm_policy = uqi->sm_policy;
uqi->sm_siocdata.sm_flags = 0;
sm_dbg('Z', (" want %d acks for id %d.\n",
uqi->sm_siocdata.sm_nacks, iobp->ioc_id));
return (sm_putqs(wq, mp, qfn));
}
/*
*
* sm_uwput - put function for an upper STREAM write.
*/
static int
sm_uwput(queue_t *wq, mblk_t *mp)
{
sm_uqi_t *uqi;
uchar_t msgtype;
int cmd;
struct iocblk *iobp;
uqi = (sm_uqi_t *)(wq->q_ptr);
msgtype = DB_TYPE(mp);
ASSERT(uqi != 0 && sm_ssp != 0);
if (msgtype >= QPCTL && msgtype != M_IOCDATA) {
(void) sm_hp_uwput(wq, mp);
return (0);
}
switch (DB_TYPE(mp)) {
case M_DATA:
case M_DELAY:
case M_BREAK:
default:
(void) sm_putqs(wq, mp, putq);
break;
case M_CTL:
if (((struct iocblk *)mp->b_rptr)->ioc_cmd == MC_CANONQUERY) {
(void) putnextctl1(OTHERQ(wq), M_CTL, MC_NOCANON);
}
freemsg(mp);
break;
case M_IOCDATA: /* not handled as high pri because may need to putbq */
sm_dbg('M', ("sm_uwput(M_IOCDATA)\n"));
/*FALLTHROUGH*/
case M_IOCTL:
cmd = (msgtype == M_IOCDATA) ?
((struct copyresp *)mp->b_rptr)->cp_cmd :
((struct iocblk *)mp->b_rptr)->ioc_cmd;
iobp = (struct iocblk *)mp->b_rptr;
iobp->ioc_rval = 0;
sm_dbg('M', ("sm_uwput(M_IOCTL:%d)\n", cmd));
switch (cmd) {
case CONSGETABORTENABLE:
iobp->ioc_error = ttymux_abort_ioctl(mp);
DB_TYPE(mp) = iobp->ioc_error ? M_IOCNAK : M_IOCACK;
qreply(wq, mp);
break;
case CONSSETABORTENABLE:
iobp->ioc_error =
secpolicy_sys_config(iobp->ioc_cr, B_FALSE) != 0 ?
EPERM : ttymux_abort_ioctl(mp);
DB_TYPE(mp) = iobp->ioc_error ? M_IOCNAK : M_IOCACK;
qreply(wq, mp);
break;
case TTYMUX_SETABORT:
if (secpolicy_sys_config(iobp->ioc_cr, B_FALSE) != 0) {
iobp->ioc_error = EPERM;
DB_TYPE(mp) = M_IOCNAK;
qreply(wq, mp);
break;
}
/*FALLTHROUGH*/
case TTYMUX_GETABORT:
case TTYMUX_GETABORTSTR:
case TTYMUX_ASSOC:
case TTYMUX_DISASSOC:
case TTYMUX_SETCTL:
case TTYMUX_GETLINK:
case TTYMUX_CONSDEV:
case TTYMUX_GETCTL:
case TTYMUX_LIST:
(void) sm_ioctl_cmd(uqi, mp);
qreply(wq, mp);
break;
case I_LINK:
case I_PLINK:
case I_UNLINK:
case I_PUNLINK:
qwriter(wq, mp, sm_link_req, PERIM_OUTER);
break;
case TCSETSW:
case TCSETSF:
case TCSETAW:
case TCSETAF:
case TCSBRK:
if (wq->q_first) {
sm_dbg('A', ("sm_uwput: TCSET-> on srv q.\n"));
/* keep message order intact */
(void) putq(wq, mp);
break;
}
/*FALLTHROUGH*/
default:
(void) sm_default_uwioctl(wq, mp, putq);
break;
}
break; /* M_IOCTL */
} /* end switch on message type */
return (0);
}
/*
* sm_uwsrv - service function for an upper STREAM write.
* 'sm_uwsrv' takes a q parameter. The q parameter specifies the queue
* which is to be serviced. This function reads the messages which are on
* this service queue and passes them to the appropriate lower driver queue.
*/
static int
sm_uwsrv(queue_t *q)
{
mblk_t *mp;
sm_uqi_t *uqi = (sm_uqi_t *)(q->q_ptr);
int msgtype;
ASSERT(q == SM_WQ(uqi));
/*
* Empty the queue unless explicitly stopped.
*/
while (mp = getq(q)) {
msgtype = DB_TYPE(mp);
if (msgtype >= QPCTL && msgtype != M_IOCDATA)
if (sm_hp_uwput(q, mp)) {
sm_dbg('T', ("sm_uwsrv: flowcontrolled.\n"));
break; /* indicates that the is disabled */
}
else
continue;
if (uqi->sm_flags & SM_STOPPED) {
(void) putbq(q, mp);
sm_dbg('T', ("sm_uwsrv: SM_STOPPED.\n"));
break;
}
/*
* Read any ttycommon data that may
* change (TS_SOFTCAR, CREAD, etc.).
*/
switch (DB_TYPE(mp)) {
case M_IOCTL:
case M_IOCDATA:
if (sm_default_uwioctl(q, mp, putbq))
return (0);
break;
default:
if (sm_putqs(q, mp, putbq))
return (0);
}
}
return (0);
}
/*
* Lower write side service routine used for backenabling upstream
* flow control.
*/
static int
sm_lwsrv(queue_t *q)
{
sm_lqi_t *lqi = (sm_lqi_t *)q->q_ptr;
queue_t *uwq;
LOCK_UNIT(lqi);
if (lqi->sm_uqflags & SM_UQVALID) {
/*
* It's safe to lock uqi since lwsrv runs asynchronously
* with the upper write routines so this cannot be an
* upper half thread. While holding the lqi lock and
* if SM_UQVALID is set we are guaranteed that
* lqi->sm_uqi will be valid.
*/
sm_dbg('I', ("sm_lwsrv: re-enabling upper queue.\n"));
uwq = SM_WQ(lqi->sm_uqi);
UNLOCK_UNIT(lqi);
qenable(uwq);
} else {
UNLOCK_UNIT(lqi);
}
return (0);
}
/*
* Upper read queue ioctl response handler for messages
* passed from the lower half of the driver.
*/
static int
sm_uriocack(queue_t *rq, mblk_t *mp)
{
sm_uqi_t *uqi = (sm_uqi_t *)rq->q_ptr;
int err, flag;
sm_iocdata_t *iodp;
struct sm_iocinfo info;
if ((err = sm_getiocinfo(mp, &info)) != 0) {
sm_dbg('I', ("Unknown ioctl response\n"));
return (err);
}
if (info.sm_id == uqi->sm_piocdata.sm_iocid) {
iodp = &uqi->sm_piocdata;
} else if (info.sm_id == uqi->sm_siocdata.sm_iocid) {
iodp = &uqi->sm_siocdata;
} else {
sm_log("Unexpected ioctl response\n");
sm_dbg('I', ("Unexpected ioctl response (id %d)\n",
info.sm_id));
/*
* If the response is sent up it will result in
* duplicate ioctl responses. The ioctl has probably been
* timed out by the stream head so dispose of the response
* (since it has arrived too late.
*/
goto out;
}
flag = SM_COPYIN;
switch (DB_TYPE(mp)) {
case M_COPYOUT:
flag = SM_COPYOUT;
/*FALLTHRU*/
case M_COPYIN:
if (iodp->sm_flags & flag)
goto out;
iodp->sm_flags |= flag;
break;
case M_IOCACK:
iodp->sm_ackcnt += 1;
iodp->sm_acnt += 1;
if (iodp->sm_policy == FIRSTACK) {
if (iodp->sm_acnt == iodp->sm_nacks)
iodp->sm_iocid = 0;
if (iodp->sm_acnt == 1)
iodp->sm_acked = 1;
else
goto out;
} else {
if (iodp->sm_acnt == iodp->sm_nacks) {
iodp->sm_iocid = 0;
iodp->sm_acked = 1;
} else
goto out;
}
break;
case M_IOCNAK:
iodp->sm_nakcnt += 1;
iodp->sm_acnt += 1;
if (iodp->sm_acnt == iodp->sm_nacks) {
iodp->sm_iocid = 0;
if (iodp->sm_acked == 0) {
iodp->sm_acked = 1;
break;
}
}
goto out;
default:
goto out;
}
/*
* Merge the tty settings each of the associated lower streams.
*/
if (info.sm_data)
(void) sm_update_ttyinfo(mp, uqi);
if (iodp == &uqi->sm_piocdata) {
if (iodp->sm_iocid == 0) {
uqi->sm_flags &= ~SM_IOCPENDING;
}
} else {
sm_dbg('I', ("sm_uriocack: forwarding response for %d.\n",
info.sm_id));
putnext(rq, mp);
return (0);
}
out:
sm_dbg('I', ("sm_uriocack: freeing response for %d.\n", info.sm_id));
freemsg(mp);
return (0);
}
/*
* Transfer a message from the lower read side of the multiplexer onto
* the associated upper stream.
*/
static int
sm_ursendup(queue_t *q, mblk_t *mp)
{
sm_uqi_t *uqi = (sm_uqi_t *)q->q_ptr;
if (!canputnext(q) && DB_TYPE(mp) < QPCTL) {
sm_dbg('I', ("sm_ursendup: flow controlled.\n"));
return (1);
}
switch (DB_TYPE(mp)) {
case M_COPYIN:
case M_COPYOUT:
case M_IOCACK:
case M_IOCNAK:
(void) sm_uriocack(q, mp);
break;
case M_HANGUP:
if (sm_uwq_error(uqi)) {
/* there are no usable lower q's */
uqi->sm_flags &= ~SM_CARON;
putnext(q, mp);
} else {
/* there are still usable q's - don't send up */
freemsg(mp);
}
break;
case M_ERROR:
if (sm_uwq_error(uqi)) {
/* there are no usable lower q's */
uqi->sm_flags &= ~SM_CARON;
putnext(q, mp);
} else if (*mp->b_rptr == NOERROR) {
/* the error has cleared */
uqi->sm_flags &= ~ERROR_MODE;
putnext(q, mp);
} else {
/* there are still usable q's - don't send up */
freemsg(mp);
}
break;
case M_FLUSH:
flushq(q, FLUSHDATA);
putnext(q, mp); /* time to use FLUSHR_PEND flag */
break;
case M_CTL:
/* wrong direction - must have come from sm_close */
uqi->sm_flags |= SM_CLOSE;
sm_dbg('I', ("sm_ursrv: had SM_CLOSE.\n"));
freemsg(mp);
break;
case M_UNHANGUP:
/* just pass them all up - they're harmless */
uqi->sm_flags |= SM_CARON;
/* FALLTHROUGH */
default:
putnext(q, mp);
break;
}
return (0);
}
/*
* sm_urput - put function for a lower STREAM read.
*/
static int
sm_urput(queue_t *q, mblk_t *mp)
{
if (sm_ursendup(q, mp) != 0)
(void) putq(q, mp);
return (0);
}
/*
* Upper read side service routine.
* Read side needs to be fast so only check for duplicate M_IOCTL acks.
*/
static int
sm_ursrv(queue_t *q)
{
sm_uqi_t *uqi = (sm_uqi_t *)q->q_ptr;
mblk_t *mp;
int flags = uqi->sm_flags;
while ((mp = getq(q))) {
if (sm_ursendup(q, mp) != 0) {
sm_dbg('I', ("sm_ursrv: flow controlled.\n"));
(void) putbq(q, mp);
uqi->sm_flags |= WANT_RENB;
break;
}
}
/*
* If the q service was called because it was no longer
* flow controled then enable each of the driver queues.
*/
if ((flags & WANT_RENB) && !(uqi->sm_flags & WANT_RENB)) {
sm_lqi_t *lqi;
queue_t *drq; /* read q of linked driver */
uqi->sm_flags &= ~WANT_RENB;
for (lqi = uqi->sm_lqs; lqi != 0; lqi = lqi->sm_nlqi) {
drq = SM_RQ(lqi)->q_next;
if (drq && drq->q_first != 0)
qenable(drq);
}
}
return (0);
}
/*
* Check a message sent from a linked device for abort requests and
* for flow control.
*/
static int
sm_lrmsg_check(queue_t *q, mblk_t *mp)
{
sm_lqi_t *lqi = (sm_lqi_t *)q->q_ptr;
switch (DB_TYPE(mp)) {
case M_DATA:
LOCK_UNIT(lqi);
/*
* check for abort - only allow abort on I/O consoles
* known to OBP -
* fix it when we do polled io
*/
if ((lqi->sm_ioflag & (uint_t)FORINPUT) == 0) {
freemsg(mp);
UNLOCK_UNIT(lqi);
return (1);
}
if ((lqi->sm_uqflags & SM_OBPCNDEV) &&
lqi->sm_ctrla_abort_on &&
abort_enable == KIOCABORTALTERNATE) {
uchar_t *rxc;
boolean_t aborted = B_FALSE;
for (rxc = mp->b_rptr;
rxc != mp->b_wptr;
rxc++)
if (*rxc == *lqi->sm_nachar) {
lqi->sm_nachar++;
if (*lqi->sm_nachar == '\0') {
abort_sequence_enter(
(char *)NULL);
lqi->sm_nachar = sm_ssp->sm_abs;
aborted = B_TRUE;
}
} else
lqi->sm_nachar = (*rxc == *sm_ssp->
sm_abs) ?
sm_ssp->
sm_abs + 1 :
sm_ssp->sm_abs;
if (aborted) {
freemsg(mp);
UNLOCK_UNIT(lqi);
return (1);
}
}
UNLOCK_UNIT(lqi);
break;
case M_BREAK: /* we'll eventually see this as a flush */
LOCK_UNIT(lqi);
/*
* Only allow abort on OBP devices. When polled I/O is
* supported allow abort on any console device.
* Parity errors are reported upstream as breaks so
* ensure that there is no data in the message before
* deciding whether to abort.
*/
if ((lqi->sm_uqflags & SM_OBPCNDEV) && /* console stream */
(mp->b_wptr - mp->b_rptr == 0 &&
msgdsize(mp) == 0)) { /* not due to parity */
if (lqi->sm_break_abort_on &&
abort_enable != KIOCABORTALTERNATE)
abort_sequence_enter((char *)NULL);
freemsg(mp);
UNLOCK_UNIT(lqi);
return (1);
} else {
UNLOCK_UNIT(lqi);
}
break;
default:
break;
}
if (DB_TYPE(mp) >= QPCTL)
return (0);
LOCK_UNIT(lqi); /* lock out the upper half */
if ((lqi->sm_uqflags & SM_UQVALID) && SM_RQ(lqi->sm_uqi)) {
UNLOCK_UNIT(lqi);
if (!canput(SM_RQ(lqi->sm_uqi))) {
sm_dbg('I', ("sm_lrmsg_check: flow controlled.\n"));
(void) putq(q, mp);
return (1);
}
} else {
UNLOCK_UNIT(lqi);
}
return (0);
}
/*
* sm_sendup - deliver a message to the upper read side of the multiplexer
*/
static int
sm_sendup(queue_t *q, mblk_t *mp)
{
sm_lqi_t *lqi = (sm_lqi_t *)q->q_ptr;
if (sm_ssp == NULL) {
freemsg(mp);
return (0);
}
/*
* Check for CD status change messages from driver.
* (Remark: this is an se driver thread running at soft interupt
* priority and the waiters are in user context).
*/
switch (DB_TYPE(mp)) {
case M_DATA:
case M_BREAK: /* we'll eventually see this as a flush */
break;
/* high priority messages */
case M_IOCACK:
case M_IOCNAK:
if ((lqi->sm_flags & SM_IOCPENDING) && lqi->sm_piocid ==
((struct iocblk *)mp->b_rptr)->ioc_id) {
freemsg(mp);
lqi->sm_flags &= ~SM_IOCPENDING;
sm_issue_ioctl(lqi);
return (0);
}
break;
case M_UNHANGUP:
/*
* If the driver can send an M_UNHANGUP it must be able to
* accept messages from above (ie clear WERROR_MODE if set).
*/
sm_dbg('E', ("lrput: M_UNHANGUP\n"));
lqi->sm_mbits |= TIOCM_CD;
lqi->sm_flags &= ~(WERROR_MODE|HANGUP_MODE);
break;
case M_HANGUP:
sm_dbg('E', ("lrput: MHANGUP\n"));
lqi->sm_mbits &= ~TIOCM_CD;
lqi->sm_flags |= (WERROR_MODE|HANGUP_MODE);
break;
case M_ERROR:
sm_dbg('E', ("lrput: MERROR\n"));
/*
* Tell the driver to flush rd/wr queue if its read/write error.
* if its a read/write error flush rq/wq (type in first bytes).
*/
if ((mp->b_wptr - mp->b_rptr) == 2) {
uchar_t rw = 0;
if (*mp->b_rptr == NOERROR) {
/* not in error anymore */
lqi->sm_flags &= ~ERROR_MODE;
lqi->sm_flags |= WANT_CD;
} else {
if (*mp->b_rptr != 0) {
/* read error */
rw |= FLUSHR;
lqi->sm_flags |= RERROR_MODE;
}
mp->b_rptr++;
if (*mp->b_rptr != 0) {
/* write error */
rw |= FLUSHW;
lqi->sm_flags |= WERROR_MODE;
}
mp->b_rptr--;
/* has next driver done qprocsoff */
if (rw && OTHERQ(q)->q_next != NULL) {
(void) putnextctl1(OTHERQ(q), M_FLUSH,
rw);
}
}
} else if (*mp->b_rptr != 0 && OTHERQ(q)->q_next != NULL) {
sm_dbg('E', ("lrput: old style MERROR (?)\n"));
lqi->sm_flags |= (RERROR_MODE | WERROR_MODE);
(void) putnextctl1(OTHERQ(q), M_FLUSH, FLUSHRW);
}
break;
case M_PCSIG:
case M_SIG:
break;
case M_COPYOUT:
case M_COPYIN:
break;
case M_FLUSH:
/* flush the read queue and pass on up */
flushq(q, FLUSHDATA);
break;
default:
break;
}
LOCK_UNIT(lqi); /* lock out the upper half */
if (lqi->sm_uqflags & SM_UQVALID && SM_RQ(lqi->sm_uqi)) {
UNLOCK_UNIT(lqi);
(void) putq(SM_RQ(lqi->sm_uqi), mp);
return (0);
} else {
sm_dbg('I', ("sm_sendup: uq not valid\n"));
freemsg(mp);
}
UNLOCK_UNIT(lqi);
return (0);
}
/*
* sm_lrput - put function for a lower STREAM read.
*/
static int
sm_lrput(queue_t *q, mblk_t *mp)
{
if (sm_lrmsg_check(q, mp) == 0)
(void) sm_sendup(q, mp);
return (0);
}
/*
* sm_lrsrv - service function for the lower read STREAM.
*/
static int
sm_lrsrv(queue_t *q)
{
mblk_t *mp;
sm_dbg('I', ("sm_lrsrv: not controlled.\n"));
while (mp = getq(q))
(void) sm_sendup(q, mp);
return (0);
}
/*
* Check whether a thread is allowed to open the requested device.
*/
static int
sm_ok_to_open(sm_uqi_t *uqi, int protocol, cred_t *credp, int *abort_waiters)
{
int rval = 0;
int proto;
*abort_waiters = 0;
switch (protocol) {
case ASYNC_DEVICE: /* Standard async protocol */
if ((uqi->sm_protocol == NULL_PROTOCOL) ||
(uqi->sm_protocol == ASYN_PROTOCOL)) {
/*
* Lock out other incompatible protocol requests.
*/
proto = ASYN_PROTOCOL;
rval = 0;
} else
rval = EBUSY;
break;
case OUTLINE: /* Outdial protocol */
if ((uqi->sm_protocol == NULL_PROTOCOL) ||
(uqi->sm_protocol == OUTD_PROTOCOL)) {
proto = OUTD_PROTOCOL;
rval = 0;
} else if (uqi->sm_protocol == ASYN_PROTOCOL) {
/*
* check for dialout request on a line that is already
* open for dial in:
* kick off any thread that is waiting to fully open
*/
if (uqi->sm_flags & FULLY_OPEN)
rval = EBUSY;
else {
proto = OUTD_PROTOCOL;
*abort_waiters = 1;
}
} else
rval = EBUSY;
break;
default:
rval = ENOTSUP;
}
if (rval == 0 &&
(uqi->sm_ttycommon->t_flags & TS_XCLUDE) &&
secpolicy_excl_open(credp) != 0) {
if (uqi->sm_flags & FULLY_OPEN) {
rval = EBUSY; /* exclusive device already open */
} else {
/* NB TS_XCLUDE cant be set during open so NOTREACHED */
/* force any waiters to yield TS_XCLUDE */
*abort_waiters = 1;
}
}
if (rval == 0)
uqi->sm_protocol = proto;
sm_dbg('A', ("ok_to_open (0x%p, %d) proto=%d rval %d (wabort=%d)",
uqi, protocol, uqi->sm_protocol, rval, *abort_waiters));
return (rval);
}
/* wait for memory to become available whilst performing a qwait */
/*ARGSUSED*/
static void dummy_callback(void *arg)
{}
/* ARGSUSED */
static int
sm_dump_msg(queue_t *q, mblk_t *mp)
{
freemsg(mp);
return (0);
}
/*
* Wait for a message to arrive - must be called with exclusive
* access at the outer perimiter.
*/
static int
sm_qwait_sig(sm_uqi_t *uqi, queue_t *q)
{
int err;
sm_dbg('C', ("sm_qwait_sig: waiting.\n"));
uqi->sm_waitq = q;
uqi->sm_nwaiters++; /* required by the close routine */
err = qwait_sig(q);
if (--uqi->sm_nwaiters == 0)
uqi->sm_waitq = 0;
if (err == 0)
err = EINTR;
else if (q->q_ptr == 0) /* can happen if there are multiple waiters */
err = -1;
else if (uqi->sm_flags & SM_CLOSE) {
uqi->sm_flags &= ~SM_CLOSE;
err = 1; /* a different protocol has closed its stream */
}
else
err = 0; /* was worth waiting for */
sm_dbg('C', ("sm_qwait_sig: rval %d\n", err));
return (err);
}
/*
* Defer the opening of one the drivers devices until the state of each
* associated lower stream is known.
*/
static int
sm_defer_open(sm_uqi_t *uqi, queue_t *q)
{
uint_t cmdflags = WANT_CDSTAT;
int err, nqs;
while ((nqs = sm_good_qs(uqi)) == 0) {
sm_dbg('C', ("sm_defer_open: no good qs\n"));
if (err = sm_qwait_sig(uqi, q))
return (err);
}
while ((uqi->sm_flags & SM_CARON) == 0) {
int iocmd;
mblk_t *pioc;
sm_dbg('C', ("sm_defer_open: flags 0x%x cmdflags 0x%x\n",
uqi->sm_flags, cmdflags));
if (cmdflags == 0) {
if (err = sm_qwait_sig(uqi, q))
return (err);
continue; /* waiting for an M_UNHANGUP */
} else if (cmdflags & WANT_SC) {
cmdflags &= ~WANT_SC;
iocmd = TIOCGSOFTCAR;
} else if (cmdflags & WANT_CD) {
cmdflags &= ~WANT_CD;
iocmd = TIOCMGET;
} else if (cmdflags & WANT_CL) {
cmdflags &= ~WANT_CL;
iocmd = TCGETS;
}
if (uqi->sm_piocdata.sm_iocid == 0) {
while ((pioc = mkiocb(iocmd)) == 0) {
bufcall_id_t id =
qbufcall(q, sizeof (struct iocblk),
BPRI_MED, dummy_callback, 0);
if (err = sm_qwait_sig(uqi, q)) {
/* wait for the bufcall */
qunbufcall(q, id);
return (err);
}
qunbufcall(q, id);
}
uqi->sm_flags |= SM_IOCPENDING;
uqi->sm_piocdata.sm_iocid =
((struct iocblk *)pioc->b_rptr)->ioc_id;
uqi->sm_piocdata.sm_acked = 0;
uqi->sm_piocdata.sm_nacks = nqs;
uqi->sm_piocdata.sm_acnt = 0;
uqi->sm_piocdata.sm_ackcnt = uqi->
sm_piocdata.sm_nakcnt = 0;
uqi->sm_piocdata.sm_policy = uqi->sm_policy;
uqi->sm_piocdata.sm_flags = SM_INTERNALIOC;
if (sm_putqs(WR(q), pioc, sm_dump_msg) != 0) {
uqi->sm_piocdata.sm_iocid = 0;
sm_log("sm_defer_open: bad putqs\n");
return (-1);
}
}
sm_dbg('C', ("sm_defer_open: flags 0x%x\n", uqi->sm_flags));
while ((uqi->sm_flags & SM_CARON) == 0 &&
(uqi->sm_flags & SM_IOCPENDING) != 0)
if (err = sm_qwait_sig(uqi, q))
return (err);
sm_dbg('C', ("defer_open: uq flags 0x%x.\n", uqi->sm_flags));
}
sm_dbg('C', ("defer_open: return 0.\n"));
return (0);
}
static int
sm_open(queue_t *rq, dev_t *devp, int flag, int sflag, cred_t *credp)
{
int ftstat;
int unit;
int protocol;
sm_uqi_t *uqi;
int abort_waiters;
if (sm_ssp == NULL)
return (ENXIO);
/*
* sflag = 0 => streams device.
*/
if (sflag != 0 || DEV_TO_UNIT(*devp) >= NLUNITS) {
sm_dbg('C', ("open: sflag=%d or bad dev_t.\n", sflag));
return (ENXIO);
}
unit = DEV_TO_UNIT(*devp);
protocol = DEV_TO_PROTOBITS(*devp);
uqi = get_uqi(sm_ssp, unit);
sm_dbg('C', ("open(0x%p, %d, 0x%x) :- unit=%d, proto=%d, uqi=0x%p\n",
rq, *devp, flag, unit, protocol, uqi));
if (uqi == 0)
return (ENXIO);
if (sm_refuse_opens && unit > smctlunit && uqi->sm_nlqs == 0)
return (ENXIO);
if (uqi->sm_flags & EXCL_OPEN && (flag & FEXCL)) {
return (EBUSY); /* device in use */
}
if ((flag & FEXCL)) {
if (secpolicy_excl_open(credp) != 0)
return (EPERM);
if ((uqi->sm_flags & FULLY_OPEN) || uqi->sm_nwaiters > 0)
return (EBUSY); /* device in use */
uqi->sm_flags |= EXCL_OPEN;
}
if (uqi->sm_protocol == NULL_PROTOCOL) {
struct termios *termiosp;
int len;
if (ddi_getlongprop(DDI_DEV_T_ANY, ddi_root_node(),
DDI_PROP_NOTPROM, "ttymodes", (caddr_t)&termiosp, &len)
== DDI_PROP_SUCCESS &&
(len == sizeof (struct termios))) {
sm_dbg('C', ("open: c_cflag=0x%x\n",
termiosp->c_cflag));
uqi->sm_ttycommon->t_iflag = termiosp->c_iflag;
uqi->sm_ttycommon->t_cflag = termiosp->c_cflag;
uqi->sm_ttycommon->t_stopc = termiosp->c_cc[VSTOP];
uqi->sm_ttycommon->t_startc = termiosp->c_cc[VSTART];
/*
* IGNBRK,BRKINT,INPCK,IXON,IXANY,IXOFF - drivers
* PARMRK,IGNPAR,ISTRIP - how to report parity
* INLCR,IGNCR,ICRNL,IUCLC - ldterm (sophisticated I/O)
* IXON, IXANY, IXOFF - flow control input
* CBAUD,CSIZE,CS5-8,CSTOPB,PARENB,PARODD,HUPCL,
* RCV1EN,XMT1EN,LOBLK,XCLUDE,CRTSXOFF,CRTSCTS,
* CIBAUD,PAREXT,CBAUDEXT,CIBAUDEXT,CREAD,CLOCAL
*/
kmem_free(termiosp, len);
}
else
bzero((caddr_t)uqi->sm_ttycommon,
sizeof (uqi->sm_ttycommon));
if (*devp == rconsdev) {
uqi->sm_cmask = sm_cmask;
uqi->sm_ttycommon->t_flags |= TS_SOFTCAR;
} else {
uqi->sm_ttycommon->t_flags &= ~TS_SOFTCAR;
}
/*
* Clear the default CLOCAL and TS_SOFTCAR flags since
* they must correspond to the settings on the real devices.
*/
uqi->sm_ttycommon->t_cflag &= ~(uqi->sm_cmask|CLOCAL);
uqi->sm_mbits = 0;
uqi->sm_policy = FIRSTACK;
if (unit == 0 && sm_ssp->sm_ms == 0)
sm_ssp->sm_ms = (sm_mux_state_t *)
space_fetch(TTYMUXPTR);
if (sm_ssp->sm_ms) {
if (sm_ssp->sm_ms->sm_cons_stdin.sm_dev == *devp ||
sm_ssp->sm_ms->sm_cons_stdout.sm_dev == *devp)
sm_ssp->sm_lconsole = uqi;
}
}
/*
* Does this thread need to wait?
*/
sm_dbg('C', ("sm_open: %d %d 0x%p 0x%x\n",
!(flag & (FNDELAY|FNONBLOCK)), !(protocol == OUTLINE), uqi->sm_lqs,
uqi->sm_flags));
tryopen:
abort_waiters = 0;
if (ftstat = sm_ok_to_open(uqi, protocol, credp, &abort_waiters)) {
sm_dbg('C', ("open failed stat=%d.\n", ftstat));
if ((uqi->sm_flags & FULLY_OPEN) == 0 && uqi->sm_nwaiters == 0)
uqi->sm_protocol = NULL_PROTOCOL;
if (flag & FEXCL)
uqi->sm_flags &= ~EXCL_OPEN;
return (ftstat);
}
if (abort_waiters) {
uqi->sm_dev = *devp;
/* different device wants to use the unit */
SM_RQ(uqi) = rq;
SM_WQ(uqi) = WR(rq);
}
if (rq->q_ptr == 0) {
sm_lqi_t *lqi;
uqi->sm_dev = *devp;
rq->q_ptr = WR(rq)->q_ptr = uqi;
SM_RQ(uqi) = rq;
SM_WQ(uqi) = WR(rq);
qprocson(rq);
for (lqi = uqi->sm_lqs; lqi != 0; lqi = lqi->sm_nlqi) {
LOCK_UNIT(lqi);
lqi->sm_uqflags |= SM_UQVALID;
UNLOCK_UNIT(lqi);
}
sm_dbg('C', ("sm_open: SM_UQVALID set on lqs.\n"));
}
if (*devp != rconsdev && BLOCKING(uqi, protocol, flag)) {
uqi->sm_flags |= WANT_CDSTAT;
do {
/*
* Wait for notifications of changes in the CLOCAL
* and TS_SOFTCAR flags and a TIOCM_CD flag of a
* TIOCMGET request (come in on the write side queue).
*/
if ((ftstat = sm_defer_open(uqi, rq)) != EINTR) {
if (ftstat) {
goto tryopen;
} else {
continue;
}
}
if (uqi->sm_nwaiters == 0) { /* clean up */
/*
* only opens on an asynchronous
* protocols reach here so checking
* nwaiters == 0 is sufficient to
* ensure that no other thread
* is waiting on this logical unit
*/
if ((uqi->sm_flags & FULLY_OPEN) == 0) {
sm_lqi_t *lqi;
uqi->sm_dev = NODEV;
sm_dbg('C', ("sm_open FULLY_OPEN=0\n"));
for (lqi = uqi->sm_lqs; lqi != 0;
lqi = lqi->sm_nlqi) {
LOCK_UNIT(lqi);
lqi->sm_uqflags &= ~SM_UQVALID;
UNLOCK_UNIT(lqi);
}
qprocsoff(rq);
rq->q_ptr = WR(rq)->q_ptr = 0;
SM_RQ(uqi) = 0;
SM_WQ(uqi) = 0;
}
}
if ((uqi->sm_flags & FULLY_OPEN) == 0 &&
uqi->sm_nwaiters == 0)
uqi->sm_protocol = NULL_PROTOCOL;
if (flag & FEXCL)
uqi->sm_flags &= ~EXCL_OPEN;
sm_dbg('C', ("sm_open: done (ret %d).\n", ftstat));
return (ftstat);
} while (BLOCKING(uqi, protocol, flag));
}
uqi->sm_flags |= FULLY_OPEN;
sm_dbg('C', ("sm_open done (ret %d).\n", ftstat));
return (ftstat);
}
/*
* Multiplexer device close routine.
*/
/*ARGSUSED*/
static int
sm_close(queue_t *rq, int flag, cred_t *credp)
{
sm_uqi_t *uqi = (sm_uqi_t *)rq->q_ptr;
sm_lqi_t *lqi;
if (sm_ssp == NULL)
return (ENXIO);
if (uqi == NULL) {
sm_dbg('C', ("close: WARN:- q 0x%p already closed.\n", rq));
return (ENXIO);
}
sm_dbg('C', ("close: uqi=0x%p unit=%d q=0x%p)\n", uqi, uqi->sm_lunit,
rq));
if (SM_RQ(uqi) != rq)
sm_dbg('C', ("sm_close: rq != current uqi queue\n"));
if (uqi->sm_ttybid) {
qunbufcall(SM_RQ(uqi), uqi->sm_ttybid);
uqi->sm_ttybid = 0;
}
/*
* Tell all the linked queues that the upper queue has gone
* Note close will never get called on a stream while there is a
* thread blocked trying to open the same stream.
* If there is a blocked open on a different stream but on
* the same logical unit it will reset the lower queue flags.
*/
for (lqi = uqi->sm_lqs; lqi != 0; lqi = lqi->sm_nlqi) {
LOCK_UNIT(lqi);
lqi->sm_uqflags &= ~SM_UQVALID;
UNLOCK_UNIT(lqi);
}
/*
* Turn off the STREAMs queue processing for this queue.
*/
qprocsoff(rq);
/*
* Similarly we will never get here if there is thread trying to
* open ths stream.
*/
LOCK_UNIT(uqi);
if (uqi->sm_waitq == 0)
uqi->sm_flags = (uqi->sm_flags & SM_OBPCNDEV) ? SM_OBPCNDEV :
0U;
uqi->sm_dev = NODEV;
uqi->sm_protocol = NULL_PROTOCOL;
ttycommon_close(uqi->sm_ttycommon);
/* it just frees any pending ioctl */
uqi->sm_ttycommon->t_cflag = 0;
uqi->sm_ttycommon->t_flags = 0;
/*
* Reset the queue pointers to NULL.
* If a thread is qwaiting in the open routine it will recheck
* the q_ptr.
*/
rq->q_ptr = NULL;
WR(rq)->q_ptr = NULL;
UNLOCK_UNIT(uqi);
if (sm_ssp->sm_lconsole == uqi) {
/* this will never be the outdial device closing */
sm_ssp->sm_lconsole = 0;
}
/*
* If there is another thread waiting for this close then unblock
* the thread by putting a message on its read queue.
*/
if (uqi->sm_waitq) {
sm_dbg('C', ("close(0x%p): doing putctl on 0x%p\n",
rq, uqi->sm_waitq));
if (rq == uqi->sm_waitq)
sm_log("close: waitq and closeq are same q\n");
(void) putctl(uqi->sm_waitq, M_CTL);
}
uqi->sm_flags &= ~(EXCL_OPEN | FULLY_OPEN);
sm_dbg('C', ("close: returning ok.\n"));
return (0);
}
/*
* Initialise the software abort sequence for use when one of the
* driver's nodes provides the system console.
*/
static void
sm_set_abort()
{
char ds[3] = { '\r', '~', CNTRL('b') };
char as[SM_MAX_ABSLEN];
int len = SM_MAX_ABSLEN;
if (ddi_prop_op(DDI_DEV_T_ANY, sm_ssp->sm_dip, PROP_LEN_AND_VAL_BUF, 0,
"abort-str", as, &len) != DDI_PROP_SUCCESS ||
(len = strlen(as)) < SM_MIN_ABSLEN) {
(void) strcpy(as, ds);
len = strlen(as);
} else {
char *s;
int i;
for (s = as, i = 0; i < len-1; i++, s++) {
if (as[i] == '^' && as[i+1] >= 'a' && as[i+1] <= 'z') {
*s = as[i+1] - 'a' + 1;
i++;
} else {
*s = as[i];
}
}
*s++ = as[i];
*s = '\0';
len = strlen(as);
}
if (len < SM_MIN_ABSLEN)
(void) strcpy(sm_ssp->sm_abs, ds);
else
(void) strcpy(sm_ssp->sm_abs, as);
}
/*
*
* sm_attach - initialisation routine per driver instance.
*/
static int
sm_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
{
int unit;
char name[32];
sm_uqi_t *uqi;
sm_lqi_t *lqip;
/*
* Is this an attach?
*/
if (cmd != DDI_ATTACH) {
return (DDI_FAILURE);
}
/*
* Validate the instance number (sm is a single instance driver).
*/
if (sm_ssp) { /* only one instance allowed */
return (DDI_FAILURE);
}
sm_instance = ddi_get_instance(dip);
/*
* Create the default minor node which will become the console.
* (create it with three different names).:
* con which appears in the /dev filesystem;
* input which matches the prom /multiplexer:input node;
* output which matches the prom /multiplexer:input node
* Create a minor node for control operations.
*/
if (ddi_create_minor_node(dip, "con", S_IFCHR, 0,
DDI_PSEUDO, 0) != DDI_SUCCESS ||
ddi_create_minor_node(dip, "input", S_IFCHR, 0,
DDI_PSEUDO, 0) != DDI_SUCCESS ||
ddi_create_minor_node(dip, "output", S_IFCHR, 0,
DDI_PSEUDO, 0) != DDI_SUCCESS ||
ddi_create_minor_node(dip, "ctl", S_IFCHR, 1,
DDI_PSEUDO, 0) != DDI_SUCCESS) {
cmn_err(CE_WARN, "sm_attach: create minors failed.\n");
ddi_remove_minor_node(dip, NULL);
return (DDI_FAILURE);
}
smctlunit = 1;
/*
* Allocate private state for this instance.
*/
sm_ssp = (sm_ss_t *)kmem_zalloc(sizeof (sm_ss_t), KM_SLEEP);
/*
* Initialise per instance data.
*/
sm_ssp->sm_dip = dip;
/*
* Get required debug level.
*/
sm_ssp->sm_trflag = ddi_prop_get_int(DDI_DEV_T_ANY, dip,
DDI_PROP_DONTPASS, "sm-trlv", sm_default_trflag);
sm_max_units = ddi_prop_get_int(DDI_DEV_T_ANY, dip,
DDI_PROP_DONTPASS, "sm-max-units", sm_max_units);
sm_minor_cnt = ddi_prop_get_int(DDI_DEV_T_ANY, dip,
DDI_PROP_DONTPASS, "sm-minor-cnt", 0);
sm_refuse_opens = ddi_prop_get_int(DDI_DEV_T_ANY, dip,
DDI_PROP_DONTPASS, "sm-refuse-opens", sm_refuse_opens);
sm_ssp->sm_ctrla_abort_on = ddi_prop_get_int(DDI_DEV_T_ANY, dip,
DDI_PROP_DONTPASS, "sm-ctrla-abort-on", 1);
sm_ssp->sm_break_abort_on = ddi_prop_get_int(DDI_DEV_T_ANY, dip,
DDI_PROP_DONTPASS, "sm-break-abort-on", 0);
sm_set_abort();
sm_ssp->sm_lqs = (sm_lqi_t *)kmem_zalloc(sizeof (sm_lqi_t) * MAX_LQS,
KM_SLEEP);
sm_ssp->sm_uqs = (sm_uqi_t *)kmem_zalloc(sizeof (sm_uqi_t) * NLUNITS,
KM_SLEEP);
for (unit = 2; unit < NLUNITS && unit < sm_minor_cnt + 2; unit++) {
if (snprintf(name, sizeof (name), "sm%c", 'a' + unit-2) >
sizeof (name)) {
cmn_err(CE_WARN,
"sm_attach: create device for unit %d failed.\n",
unit);
} else if (ddi_create_minor_node(dip, name, S_IFCHR,
unit, DDI_NT_SERIAL, NULL) != DDI_SUCCESS) {
ddi_remove_minor_node(dip, NULL);
return (DDI_FAILURE);
}
if (snprintf(name, sizeof (name), "sm%c,cu", 'a' + unit-2) >
sizeof (name)) {
cmn_err(CE_WARN,
"sm_attach: create cu device for unit %d failed.\n",
unit);
continue;
} else if (ddi_create_minor_node(dip, name, S_IFCHR,
unit|OUTLINE, DDI_NT_SERIAL_DO, NULL) != DDI_SUCCESS) {
ddi_remove_minor_node(dip, NULL);
return (DDI_FAILURE);
}
}
for (unit = 0; unit < NLUNITS; unit++) {
uqi = get_uqi(sm_ssp, unit);
uqi->sm_lqs = 0;
uqi->sm_dev = NODEV;
uqi->sm_nlqs = 0;
uqi->sm_lunit = unit;
uqi->sm_protocol = NULL_PROTOCOL;
mutex_init(uqi->sm_umutex, NULL, MUTEX_DRIVER, NULL);
cv_init(uqi->sm_ucv, NULL, CV_DRIVER, NULL);
mutex_init(&uqi->sm_ttycommon->t_excl, NULL,
MUTEX_DRIVER, NULL);
}
for (unit = 0; unit < MAX_LQS; unit++) {
lqip = get_lqi(sm_ssp, unit);
lqip->sm_unit = unit;
lqip->sm_hadkadbchar = 0;
lqip->sm_nachar = sm_ssp->sm_abs;
lqip->sm_ioflag = FORIO;
lqip->sm_ctrla_abort_on = sm_ssp->sm_ctrla_abort_on;
lqip->sm_break_abort_on = sm_ssp->sm_break_abort_on;
mutex_init(lqip->sm_umutex, NULL, MUTEX_DRIVER, NULL);
cv_init(lqip->sm_ucv, NULL, CV_DRIVER, NULL);
mutex_init(&lqip->sm_ttycommon->t_excl, NULL,
MUTEX_DRIVER, NULL);
}
return (DDI_SUCCESS);
}
/*
*
* sm_detach - detach routine per driver instance.
*/
static int
sm_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
{
sm_uqi_t *lu;
sm_lqi_t *pu;
int unit;
/*
* Is this a detach request for instance 0 (single instance driver).
*/
if (cmd != DDI_DETACH)
return (DDI_FAILURE);
if (sm_ssp == NULL)
return (DDI_FAILURE);
sm_dbg('V', ("detach ..."));
/*
* Check that all the upper and lower queues are closed.
*/
for (unit = 0; unit < NLUNITS; unit++) {
lu = &sm_ssp->sm_uqs[unit];
if (lu && lu->sm_protocol != NULL_PROTOCOL) {
sm_dbg('V', ("detach: upper unit still open.\n"));
return (DDI_FAILURE);
}
}
for (unit = 0; unit < MAX_LQS; unit++) {
pu = &sm_ssp->sm_lqs[unit];
if (pu && pu->sm_linkid != 0) {
sm_dbg('V', ("detach: lower unit still linked (%d)\n",
pu->sm_linkid));
return (DDI_FAILURE);
}
}
for (unit = 0; unit < NLUNITS; unit++) {
lu = &sm_ssp->sm_uqs[unit];
mutex_destroy(lu->sm_umutex);
cv_destroy(lu->sm_ucv);
mutex_destroy(&lu->sm_ttycommon->t_excl);
}
for (unit = 0; unit < MAX_LQS; unit++) {
pu = &sm_ssp->sm_lqs[unit];
mutex_destroy(pu->sm_umutex);
cv_destroy(pu->sm_ucv);
mutex_destroy(&pu->sm_ttycommon->t_excl);
}
/*
* Tidy up per instance state.
*/
kmem_free(sm_ssp->sm_lqs, sizeof (sm_lqi_t) * MAX_LQS);
kmem_free(sm_ssp->sm_uqs, sizeof (sm_uqi_t) * NLUNITS);
kmem_free(sm_ssp, sizeof (sm_ss_t));
sm_ssp = 0;
/*
* Remove all of the devices created in attach.
*/
ddi_remove_minor_node(dip, NULL);
return (DDI_SUCCESS);
}
/*
* SECTION
* Driver interface to the OS.
*/
/*
* The driver is responsible for managing the mapping between the file system
* device types (major/minor pairs) and the corresponding instance of the driver
* or device information pointer (dip).
* sm_info - return the instance or dip corresponding to the dev_t.
*/
/*ARGSUSED*/
static int
sm_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
{
int res = DDI_SUCCESS;
switch (infocmd) {
case DDI_INFO_DEVT2DEVINFO:
if (sm_ssp == NULL)
res = DDI_FAILURE;
else
*result = (void *)sm_ssp->sm_dip;
break;
case DDI_INFO_DEVT2INSTANCE:
*result = (void*)0; /* single instance driver */
break;
default:
res = DDI_FAILURE;
break;
}
return (res);
}
/*
* End of driver implementation
*/
/*
* Loadable module interface to the kernel
*/
/*
* Firstly the Streams specific interface
*/
/*
* Solaris driver/STREAM initialisation structures.
*/
static struct module_info uinfo =
{
SM_MOD_ID,
TTYMUX_DRVNAME,
0, /* min packet size */
INFPSZ, /* max packet size */
2048, /* high water mark */
256, /* low water mark */
};
/*
* Use zero water marks becuase the lower queues are used only for flow control.
*/
static struct module_info linfo =
{
SM_MOD_ID,
TTYMUX_DRVNAME,
0, /* min packet size */
INFPSZ, /* max packet size */
0, /* high water mark */
0 /* low water mark */
};
/*
* Solaris upper read STREAM initialisation structure.
*/
static struct qinit urinit =
{
sm_urput, /* put */
sm_ursrv, /* service */
sm_open, /* open */
sm_close, /* close */
NULL, /* admin */
&uinfo, /* module info */
NULL /* stats */
};
/*
* Solaris upper write STREAM initialisation structure.
*/
static struct qinit uwinit =
{
sm_uwput,
sm_uwsrv,
NULL,
NULL,
NULL,
&uinfo,
NULL
};
/*
* Solaris lower read STREAM initialisation structure.
*/
static struct qinit lrinit =
{
sm_lrput,
sm_lrsrv,
NULL,
NULL, NULL,
&linfo,
NULL
};
/*
* Solaris lower write STREAM initialisation structure.
*/
static struct qinit lwinit =
{
putq,
sm_lwsrv,
NULL,
NULL,
NULL,
&linfo,
NULL
};
/*
* Multiplexing STREAM structure.
*/
struct streamtab sm_streamtab =
{
&urinit,
&uwinit,
&lrinit,
&lwinit
};
/*
* Driver operations structure (struct cb_ops) and
* driver dynamic loading functions (struct dev_ops).
*/
/*
* Fold the Stream interface to the kernel into the driver interface
* to the OS.
*/
DDI_DEFINE_STREAM_OPS(sm_ops, \
nulldev, nulldev, \
sm_attach, sm_detach, nodev, \
sm_info, (D_NEW | D_MTQPAIR|D_MTOUTPERIM|D_MTOCEXCL | D_MP),
&sm_streamtab, ddi_quiesce_not_supported);
/*
* Driver module information.
*/
extern struct mod_ops mod_driverops;
static struct modldrv modldrv =
{
&mod_driverops,
"serial mux driver",
&sm_ops
};
static struct modlinkage modlinkage =
{
MODREV_1,
&modldrv,
NULL
};
/*
* Define the body of our interface to the OS.
*/
/*
* '_init' is called by Solaris to initialise any driver
* specific state and to install the driver.
*/
int
_init(void)
{
return (mod_install(&modlinkage));
}
/*
* _info - return this drivers interface to the kernel.
*/
int
_info(struct modinfo *modinfop)
{
return (mod_info(&modlinkage, modinfop));
}
/*
* _fini - the OS is finished with the services provided by the driver.
* remove ourself and then remove any footprint that remains.
*/
int
_fini(void)
{
return (mod_remove(&modlinkage));
}
|