summaryrefslogtreecommitdiff
path: root/usr/src/uts/common/io/conskbd.c
blob: feaf659d3358d4973f66f50eb5f7726318a4663a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
/*
 * 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 2009 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */


/*
 * Console kbd multiplexor driver for Sun.
 * The console "zs" port is linked under us, with the "kbd" module pushed
 * on top of it.
 * Minor device 0 is what programs normally use.
 * Minor device 1 is used to feed predigested keystrokes to the "workstation
 * console" driver, which it is linked beneath.
 *
 *
 *     This module can support multiple keyboards to be used simultaneously.
 * and enable users to use at a time multiple keyboards connected to the
 * same system. All the keyboards are linked under conskbd, and act as a
 * keyboard with replicated keys.
 *
 *     The DIN keyboards of SUN, for exmple , type 3/4/5,  are supported via
 * a two-level architecure. The lower one is one of serialport drivers, such
 * as zs, se, and the upper is  "kb" STREAMS module. Currenly, the serialport
 * drivers don't support polled I/O interfaces, we couldn't group the keyboard
 * of this kind under conskbd. So we do as the follows:
 *
 *         A new ioctl CONSSETKBDTYPE interface between conskbd and lower
 *     keyboard drivers is added. When conskbd receives I_LINK or I_PLINK
 *     ioctl, it will send a CONSSETKBDTYPE ioctl to the driver which is
 *     requesting to be linked under conskbd. If the lower driver does't
 *     recognize this ioctl, the virtual keyboard will be disabled so that
 *     only one keyboard instance could be linked under conskbd.
 */
#define	KEYMAP_SIZE_VARIABLE

#include <sys/types.h>
#include <sys/param.h>
#include <sys/stropts.h>
#include <sys/stream.h>
#include <sys/strsubr.h>
#include <sys/strsun.h>
#include <sys/conf.h>
#include <sys/stat.h>
#include <sys/errno.h>
#include <sys/modctl.h>
#include <sys/kbio.h>
#include <sys/ddi.h>
#include <sys/sunddi.h>
#include <sys/consdev.h>
#include <sys/note.h>
#include <sys/kmem.h>
#include <sys/kstat.h>
#include <sys/policy.h>
#include <sys/kbd.h>
#include <sys/kbtrans.h>
#include <sys/promif.h>
#include <sys/vuid_event.h>
#include <sys/conskbd.h>
#include <sys/beep.h>

extern struct keyboard *kbtrans_usbkb_maptab_init(void);
extern void kbtrans_usbkb_maptab_fini(struct keyboard **);
extern int ddi_create_internal_pathname(dev_info_t *, char *, int, minor_t);

/*
 * Module linkage routines for the kernel
 */
static int conskbd_attach(dev_info_t *, ddi_attach_cmd_t);
static int conskbd_detach(dev_info_t *, ddi_detach_cmd_t);
static int conskbd_info(dev_info_t *, ddi_info_cmd_t, void *, void **);

/*
 * STREAMS queue processing procedures
 */
static int	conskbduwsrv(queue_t *);
static int	conskbdlwserv(queue_t *);
static int	conskbdlrput(queue_t *, mblk_t *);
static int	conskbdclose(queue_t *, int, cred_t *);
static int	conskbdopen(queue_t *, dev_t *, int, int, cred_t *);


/* STREAMS driver id and limit value struct */
static struct module_info conskbdm_info = {
	0,		/* mi_idnum */
	"conskbd",	/* mi_idname */
	0,		/* mi_minpsz */
	1024,		/* mi_maxpsz */
	2048,		/* mi_hiwat */
	128		/* mi_lowat */
};

/*
 * STREAMS queue processing procedure structures
 */
/* upper read queue processing procedure structures */
static struct qinit conskbdurinit = {
	NULL,			/* qi_putp */
	(int (*)())NULL,	/* qi_srvp */
	conskbdopen,		/* qi_qopen */
	conskbdclose,		/* qi_qclose */
	(int (*)())NULL,	/* qi_qadmin */
	&conskbdm_info,		/* qi_minfo */
	NULL			/* qi_mstat */
};

/* upper write queue processing procedures structuresi */
static struct qinit conskbduwinit = {
	putq,		/* qi_putp */
	conskbduwsrv,	/* qi_srvp */
	conskbdopen,			/* qi_qopen */
	conskbdclose,			/* qi_qclose */
	(int (*)())NULL,		/* qi_qadmin */
	&conskbdm_info,			/* qi_minfo */
	NULL				/* qi_mstat */
};

/* lower read queue processing procedures structures */
static struct qinit conskbdlrinit = {
	conskbdlrput,	/* qi_putp */
	(int (*)())NULL,		/* qi_srvp */
	(int (*)())NULL,		/* qi_qopen */
	(int (*)())NULL,		/* qi_qclose */
	(int (*)())NULL,		/* qi_qadmin */
	&conskbdm_info,			/* qi_minfo */
	NULL				/* qi_mstat */
};

/* lower write processing procedures structures */
static struct qinit conskbdlwinit = {
	putq,				/* qi_putp */
	conskbdlwserv,	/* qi_srvp */
	(int (*)())NULL,		/* qi_qopen */
	(int (*)())NULL,		/* qi_qclose */
	(int (*)())NULL,		/* qi_qadmin */
	&conskbdm_info,			/* qi_minfo */
	NULL				/* qi_mstat */
};

/* STREAMS entity declaration structure */
static struct streamtab conskbd_str_info = {
	&conskbdurinit,		/* st_rdinit */
	&conskbduwinit,		/* st_wrinit */
	&conskbdlrinit,		/* st_muxrinit */
	&conskbdlwinit,		/* st_muxwinit */
};


/* Entry points structure */
static struct cb_ops cb_conskbd_ops = {
	nulldev,		/* cb_open */
	nulldev,		/* cb_close */
	nodev,			/* cb_strategy */
	nodev,			/* cb_print */
	nodev,			/* cb_dump */
	nodev,			/* cb_read */
	nodev,			/* cb_write */
	nodev,			/* cb_ioctl */
	nodev,			/* cb_devmap */
	nodev,			/* cb_mmap */
	nodev,			/* cb_segmap */
	nochpoll,		/* cb_chpoll */
	ddi_prop_op,		/* cb_prop_op */
	&conskbd_str_info,	/* cb_stream */
	D_MP | D_MTOUTPERIM | D_MTOCEXCL	/* cb_flag */
};


/*
 * Device operations structure
 */
static struct dev_ops conskbd_ops = {
	DEVO_REV,		/* devo_rev */
	0,			/* devo_refcnt */
	conskbd_info,		/* devo_getinfo */
	nulldev,		/* devo_identify */
	nulldev,		/* devo_probe */
	conskbd_attach,		/* devo_attach */
	conskbd_detach,		/* devo_detach */
	nodev,			/* devo_reset */
	&(cb_conskbd_ops),	/* devo_cb_ops */
	(struct bus_ops *)NULL,	/* devo_bus_ops */
	NULL,			/* devo_power */
	ddi_quiesce_not_needed,		/* quiesce */
};

/*
 * Module linkage information for the kernel.
 */
static struct modldrv modldrv = {
	&mod_driverops, /* Type of module.  This one is a pseudo driver */
	"conskbd multiplexer driver",
	&conskbd_ops,	/* driver ops */
};

/*
 * Module linkage structure
 */
static struct modlinkage modlinkage = {
	MODREV_1,	/* ml_rev */
	&modldrv,	/* ml_linkage */
	NULL		/* NULL terminates the list */
};

/*
 * Debug printing
 */
#ifndef DPRINTF
#ifdef DEBUG
void	conskbd_dprintf(const char *fmt, ...);
#define	DPRINTF(l, m, args) \
	(((l) >= conskbd_errlevel) && ((m) & conskbd_errmask) ?	\
		conskbd_dprintf args :				\
		(void) 0)

/*
 * Severity levels for printing
 */
#define	PRINT_L0	0	/* print every message */
#define	PRINT_L1	1	/* debug */
#define	PRINT_L2	2	/* quiet */

/*
 * Masks
 */
#define	PRINT_MASK_ALL		0xFFFFFFFFU
uint_t	conskbd_errmask = PRINT_MASK_ALL;
uint_t	conskbd_errlevel = PRINT_L2;

#else
#define	DPRINTF(l, m, args)	/* NOTHING */
#endif
#endif

/*
 * Module global data are protected by outer perimeter. Modifying
 * these global data is executed in outer perimeter exclusively.
 * Except in conskbdopen() and conskbdclose(), which are entered
 * exclusively (Refer to D_MTOCEXCL flag), all changes for the
 * global variables are protected by qwriter().
 */
static	queue_t	*conskbd_regqueue; /* regular keyboard queue above us */
static	queue_t	*conskbd_consqueue; /* console queue above us */


static dev_info_t *conskbd_dip;		/* private copy of devinfo pointer */
static long	conskbd_idle_stamp;	/* seconds tstamp of latest keystroke */
static struct keyboard *conskbd_keyindex;

/*
 * Normally, kstats of type KSTAT_TYPE_NAMED have multiple elements.  In
 * this case we use this type for a single element because the ioctl code
 * for it knows how to handle mixed kernel/user data models.  Also, it
 * will be easier to add new statistics later.
 */
static struct {
	kstat_named_t idle_sec;		/* seconds since last keystroke */
} conskbd_kstat = {
	{ "idle_sec", KSTAT_DATA_LONG, }
};

/*
 * Local routines prototypes
 */
static int conskbd_kstat_update(kstat_t *, int);

static void conskbd_ioctl(queue_t *, mblk_t *);
static void conskbd_ioc_plink(queue_t *, mblk_t *);
static void conskbd_ioc_punlink(queue_t *, mblk_t *);
static void conskbd_legacy_kbd_ioctl(queue_t *, mblk_t *);
static void conskbd_virtual_kbd_ioctl(queue_t *, mblk_t *);
static mblk_t *conskbd_alloc_firm_event(ushort_t, int);

static conskbd_pending_msg_t *conskbd_mux_find_msg(mblk_t *);
static void conskbd_mux_enqueue_msg(conskbd_pending_msg_t *);
static void conskbd_mux_dequeue_msg(conskbd_pending_msg_t *);
static void conskbd_link_lowque_virt(queue_t *, mblk_t *);
static void conskbd_link_lowque_legacy(queue_t *, mblk_t *);

static void conskbd_handle_downstream_msg(queue_t *, mblk_t *);
static void conskbd_kioctype_complete(conskbd_lower_queue_t *, mblk_t *);
static void conskbd_kioctrans_complete(conskbd_lower_queue_t *, mblk_t *);
static void conskbd_kioclayout_complete(conskbd_lower_queue_t *, mblk_t *);
static void conskbd_kiocsled_complete(conskbd_lower_queue_t *, mblk_t *);
static void conskbd_mux_upstream_msg(conskbd_lower_queue_t *, mblk_t *);
static void conskbd_legacy_upstream_msg(conskbd_lower_queue_t *, mblk_t *);
static void conskbd_lqs_ack_complete(conskbd_lower_queue_t *, mblk_t *);

static void conskbd_polledio_enter(cons_polledio_arg_t);
static void conskbd_polledio_exit(cons_polledio_arg_t);
static int  conskbd_polledio_ischar(cons_polledio_arg_t);
static int  conskbd_polledio_getchar(cons_polledio_arg_t);
static void conskbd_polledio_setled(struct kbtrans_hardware *, int);

static void conskbd_streams_setled(struct kbtrans_hardware *, int);
static boolean_t conskbd_override_kbtrans(queue_t *, mblk_t *);
static boolean_t
conskbd_polled_keycheck(struct kbtrans_hardware *,
    kbtrans_key_t *, enum keystate *);

/*
 * Callbacks needed by kbtrans
 */
static struct kbtrans_callbacks conskbd_callbacks = {
	conskbd_streams_setled,
	conskbd_polledio_setled,
	conskbd_polled_keycheck,
};

/*
 * Single private "global" lock for the few rare conditions
 * we want single-threaded.
 */
static	kmutex_t	conskbd_msgq_lock;
static	conskbd_pending_msg_t	*conskbd_msg_queue;

/*
 * The software state structure of virtual keyboard.
 * Currently, only one virtual keyboard is supported.
 */
static conskbd_state_t	conskbd = { 0 };

/* This variable backs up the layout state for non-self-ID keyboards */
static int kbd_layout_bak = 0;

/*
 * _init()
 *
 * Description:
 *      Driver initialization, called when driver is first loaded.
 *      This is how access is initially given to all the static structures.
 *
 * Arguments:
 *      None
 *
 * Returns:
 *      ddi_soft_state_init() status, see ddi_soft_state_init(9f), or
 *      mod_install() status, see mod_install(9f)
 */
int
_init(void)
{
	int	error;

	error = mod_install(&modlinkage);
	if (error != 0) {
		return (error);
	}

	conskbd_keyindex = kbtrans_usbkb_maptab_init();

	mutex_init(&conskbd_msgq_lock, NULL, MUTEX_DRIVER, NULL);

	return (error);

}	/* _init() */

/*
 * _fini()
 *
 * Description:
 *      Module de-initialization, called when the driver is to be unloaded.
 *
 * Arguments:
 *      None
 *
 * Returns:
 *      mod_remove() status, see mod_remove(9f)
 */
int
_fini(void)
{
	int	error;

	error = mod_remove(&modlinkage);
	if (error != 0)
		return (error);
	mutex_destroy(&conskbd_msgq_lock);
	kbtrans_usbkb_maptab_fini(&conskbd_keyindex);

	return (0);

}	/* _fini() */

/*
 * _info()
 *
 * Description:
 *      Module information, returns information about the driver.
 *
 * Arguments:
 *      modinfo         *modinfop       Pointer to the opaque modinfo structure
 *
 * Returns:
 *      mod_info() status, see mod_info(9f)
 */
int
_info(struct modinfo *modinfop)
{
	return (mod_info(&modlinkage, modinfop));

}	/* _info() */


/*
 * conskbd_attach()
 *
 * Description:
 *	This routine creates two device nodes. One is the "kbd" node, which
 * is used by user application programs(such as Xserver).The other is the
 * "conskbd" node, which is an internal node. consconfig_dacf module will
 * open this internal node, and link the conskbd under the wc (workstaion
 * console).
 *
 * Arguments:
 *      dev_info_t      *dip    Pointer to the device's dev_info struct
 *      ddi_attach_cmd_t cmd    Attach command
 *
 * Returns:
 *      DDI_SUCCESS             The driver was initialized properly
 *      DDI_FAILURE             The driver couldn't be initialized properly
 */
/*ARGSUSED*/
static int
conskbd_attach(dev_info_t *devi, ddi_attach_cmd_t cmd)
{
	kstat_t	*ksp;

	switch (cmd) {
	case DDI_ATTACH:
		break;

	default:
		return (DDI_FAILURE);

	}
	if ((ddi_create_minor_node(devi, "kbd", S_IFCHR,
	    0, DDI_PSEUDO, 0) == DDI_FAILURE) ||
	    (ddi_create_internal_pathname(devi, "conskbd", S_IFCHR,
	    1) == DDI_FAILURE)) {
		ddi_remove_minor_node(devi, NULL);
		return (DDI_FAILURE);
	}
	conskbd_dip = devi;

	ksp = kstat_create("conskbd", 0, "activity", "misc", KSTAT_TYPE_NAMED,
	    sizeof (conskbd_kstat) / sizeof (kstat_named_t),
	    KSTAT_FLAG_VIRTUAL);
	if (ksp) {
		ksp->ks_data = (void *) &conskbd_kstat;
		ksp->ks_update = conskbd_kstat_update;
		kstat_install(ksp);
		conskbd_idle_stamp = gethrestime_sec();	/* initial value */
	}

	conskbd.conskbd_layout = -1;	/* invalid layout */
	conskbd.conskbd_led_state = -1;
	conskbd.conskbd_bypassed = B_FALSE;

	return (DDI_SUCCESS);

}	/* conskbd_attach() */

/*
 * conskbd_detach()
 *
 * Description:
 *      Detach an instance of the conskbd driver. In fact, the driver can not
 * be detached.
 *
 * Arguments:
 *      dev_info_t              *dip    Pointer to the device's dev_info struct
 *      ddi_detach_cmd_t        cmd     Detach command
 *
 * Returns:
 *      DDI_SUCCESS     The driver was detached
 *      DDI_FAILURE     The driver couldn't be detached
 */
/*ARGSUSED*/
static int
conskbd_detach(dev_info_t *devi, ddi_detach_cmd_t cmd)
{
	return (DDI_FAILURE);

}	/* conskbd_detach() */

/* ARGSUSED */
static int
conskbd_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg,
    void **result)
{
	register int error;

	switch (infocmd) {
	case DDI_INFO_DEVT2DEVINFO:
		if (conskbd_dip == NULL) {
			error = DDI_FAILURE;
		} else {
			*result = (void *) conskbd_dip;
			error = DDI_SUCCESS;
		}
		break;
	case DDI_INFO_DEVT2INSTANCE:
		*result = (void *)0;
		error = DDI_SUCCESS;
		break;
	default:
		error = DDI_FAILURE;
	}
	return (error);

}	/* conskbd_info() */

/*ARGSUSED*/
static int
conskbdopen(queue_t *q, dev_t *devp, int flag, int sflag, cred_t *crp)
{
	dev_t	unit;
	int	err;

	unit = getminor(*devp);

	if (unit == 0) {
		/*
		 * Opening "/dev/kbd".
		 */
		conskbd_regqueue = q;
		qprocson(q);
		return (0);
	} else if (unit != 1) {
		/* we don't do that under Bozo's Big Tent */
		return (ENODEV);
	}

	/*
	 * Check if already initialized
	 */
	if (conskbd_consqueue != NULL)
		return (0);

	/*
	 * Opening the device to be linked under the console.
	 */
	conskbd_consqueue = q;

	if (secpolicy_console(crp) != 0)
		return (EPERM);

	/*
	 * initialize kbtrans module for conskbd
	 */
	err = kbtrans_streams_init(q, sflag, (struct kbtrans_hardware *)
	    &conskbd, &conskbd_callbacks, &conskbd.conskbd_kbtrans, 0, 0);
	if (err != 0)
		return (err);
	kbtrans_streams_set_keyboard(conskbd.conskbd_kbtrans, KB_USB,
	    conskbd_keyindex);

	conskbd.conskbd_polledio.cons_polledio_version = CONSPOLLEDIO_V1;
	conskbd.conskbd_polledio.cons_polledio_argument =
	    (cons_polledio_arg_t)&conskbd;
	conskbd.conskbd_polledio.cons_polledio_putchar = NULL;
	conskbd.conskbd_polledio.cons_polledio_getchar =
	    (int (*)(cons_polledio_arg_t)) conskbd_polledio_getchar;
	conskbd.conskbd_polledio.cons_polledio_ischar =
	    (boolean_t (*)(cons_polledio_arg_t))conskbd_polledio_ischar;
	conskbd.conskbd_polledio.cons_polledio_enter = conskbd_polledio_enter;
	conskbd.conskbd_polledio.cons_polledio_exit = conskbd_polledio_exit;
	qprocson(q);

	return (0);

}	/* conskbdopen() */


/*ARGSUSED*/
static int
conskbdclose(queue_t *q, int flag, cred_t *crp)
{
	if (q == conskbd_regqueue) {

		conskbd_pending_msg_t	*pmsg, *prev, *next;
		mblk_t		*mp;

		/* switch the input stream back to conskbd_consqueue */
		conskbd.conskbd_directio = B_FALSE;

		kbtrans_streams_untimeout(conskbd.conskbd_kbtrans);
		kbtrans_streams_set_queue(conskbd.conskbd_kbtrans,
		    conskbd_consqueue);
		qprocsoff(q);
		conskbd_regqueue = NULL;

		/*
		 * If there are any pending ioctls which conskbd hasn't
		 * responded to yet, remove them from conskbd_msg_queue.
		 * Otherwise, we might send the response to a nonexistent
		 * closed queue. Refer to: conskbd_mux_upstream_msg().
		 */
		for (prev = NULL, pmsg = conskbd_msg_queue; pmsg != NULL;
		    pmsg = next) {
			next = pmsg->kpm_next;
			if (pmsg->kpm_upper_queue == WR(q)) {
				if (prev == NULL)
					conskbd_msg_queue = next;
				else
					prev->kpm_next = next;

				while (pmsg->kpm_resp_list != NULL) {
					mp = pmsg->kpm_resp_list;
					pmsg->kpm_resp_list = mp->b_next;
					mp->b_next = mp->b_prev = NULL;
					freemsg(mp);
				}
				mutex_destroy(&pmsg->kpm_lock);
				kmem_free(pmsg, sizeof (*pmsg));
			} else {
				prev = pmsg;
			}
		}
	} else if (q == conskbd_consqueue) {
		/*
		 * Well, this is probably a mistake, but we will permit you
		 * to close the path to the console if you really insist.
		 */
		qprocsoff(q);
		conskbd_consqueue = NULL;
	}

	return (0);

}	/* conskbdclose() */

/*
 * Service procedure for upper write queue.
 *	To make sure the order of messages, we don't process any
 * message in qi_putq() routine of upper write queue, instead the
 * qi_putq() routine, which is a standard putq() routine, puts all
 * messages into a queue, and lets the following service procedure
 * deal with all messages.
 *	This routine is invoked when ioctl commands are send down
 * by a consumer of the keyboard device, eg, when the keyboard
 * consumer tries to determine the keyboard layout type, or sets
 * the led states.
 */
static int
conskbduwsrv(queue_t *q)
{
	mblk_t	*mp;
	queue_t	*oldq;
	enum kbtrans_message_response ret;
	struct copyresp *csp;
	struct freq_request *frqp;
	int error;

	while ((mp = getq(q)) != NULL) {

		/*
		 * if the virtual keyboard is supported
		 */
		if (conskbd.conskbd_bypassed == B_FALSE) {

			if (conskbd_override_kbtrans(q, mp) == B_TRUE)
				continue;
			/*
			 * The conskbd driver is a psaudo driver. It has two
			 * devcice nodes, one is used by kernel, and the other
			 * is used by end-users. There are two STREAMS queues
			 * corresponding to the two device nodes, console queue
			 * and regular queue.
			 * In conskbd_override_kbtrans() routine, when receives
			 * KIOCSDIRECT ioctl, we need change the direction of
			 * keyboard input messages, and direct the input stream
			 * from keyboard into right queue. It causes this queue
			 * to be switched between regular queue and console
			 * queue. And here, in this routine, the in-parameter
			 * "q" can be any one of the two. Moreover, this module
			 * is executed in multithreaded environment, even if the
			 * q is switched to regular queue, it is possible that
			 * the in-parameter is still the console queue, and we
			 * need to return response to right queue.
			 * The response is sent to upstream by the kbtrans
			 * module. so we need to save the old queue, and wait
			 * kbtrans to proces message and to send response out,
			 * and then switch back to old queue.
			 */
			oldq = kbtrans_streams_get_queue(
			    conskbd.conskbd_kbtrans);
			kbtrans_streams_set_queue(
			    conskbd.conskbd_kbtrans, RD(q));
			ret = kbtrans_streams_message(
			    conskbd.conskbd_kbtrans, mp);
			kbtrans_streams_set_queue(
			    conskbd.conskbd_kbtrans, oldq);

			switch (ret) {
				case KBTRANS_MESSAGE_HANDLED:
					continue;
				case KBTRANS_MESSAGE_NOT_HANDLED:
					break;
			}
		}

		switch (mp->b_datap->db_type) {

		case M_IOCTL:
			conskbd_ioctl(q, mp);
			break;

		case M_FLUSH:
			if (*mp->b_rptr & FLUSHW) {
				flushq(q, FLUSHDATA);
			}
			/*
			 * here, if flush read queue, some key-up messages
			 * may be lost so that upper module or applications
			 * treat corresponding keys as being held down for
			 * ever.
			 */
			freemsg(mp);
			break;

		case M_DATA:
			/*
			 * virtual keyboard doesn't support this interface.
			 * only when it is disabled, we pass the message
			 * down to lower queue.
			 */
			if ((conskbd.conskbd_bypassed) &&
			    (conskbd.conskbd_lqueue_nums > 0)) {
				if (putq(conskbd.conskbd_lqueue_list->
				    lqs_queue, mp) != 1)
					freemsg(mp);
			} else {
				freemsg(mp);
			}
			break;

		case M_IOCDATA:
			/*
			 * Only deal with copyresp to KIOCSETFREQ
			 * transparent ioctl now
			 */
			csp = (struct copyresp *)mp->b_rptr;
			if (csp->cp_rval) {
				miocnak(q, mp, 0, EINVAL);
				break;
			}

			error = 0;
			switch (csp->cp_cmd) {
			case KIOCSETFREQ:
				frqp = (struct freq_request *)mp->
				    b_cont->b_rptr;

				switch (frqp->type) {
				case CONSOLE_BEEP:
					error = beeper_freq(BEEP_CONSOLE,
					    (int)frqp->freq);
						break;

				case KBD_BEEP:
					error = beeper_freq(BEEP_TYPE4,
					    (int)frqp->freq);
						break;

				default:
					error = 1;
				} /* frqp->type */

				break;

			default:
				error = 1;
			} /* csp->cp_cmd */

			if (error == 0)
				miocack(q, mp, 0, 0);
			else
				miocnak(q, mp, 0, EINVAL);

			break;

		default:
			/*
			 * Pass an error message up.
			 */
			mp->b_datap->db_type = M_ERROR;
			if (mp->b_cont) {
				freemsg(mp->b_cont);
				mp->b_cont = NULL;
			}
			mp->b_rptr = mp->b_datap->db_base;
			mp->b_wptr = mp->b_rptr + sizeof (char);
			*mp->b_rptr = EINVAL;
			qreply(q, mp);
		}
	}	/* end of while */

	return (0);
}	/* conskbduwsrv() */

static void
conskbd_ioctl(queue_t *q, mblk_t *mp)
{
	struct	iocblk			*iocp;
	int	error = 0;

	iocp = (struct iocblk *)mp->b_rptr;

	switch (iocp->ioc_cmd) {

	case I_LINK:
	case I_PLINK:
		if (conskbd.conskbd_bypassed == B_TRUE) {
		/*
		 * A legacy keyboard can NOT be connected to conskbd together
		 * with other keyboards. So when a legacy keyboard is already
		 * linked under conkbd, we just reject all others.
		 */
			miocnak(q, mp, 0, EAGAIN);
			break;
		}
		qwriter(q, mp, conskbd_ioc_plink, PERIM_OUTER);
		break;

	case I_UNLINK:
	case I_PUNLINK:
		qwriter(q, mp, conskbd_ioc_punlink, PERIM_OUTER);
		break;

	case KIOCSKABORTEN:
		/*
		 * Check if privileged
		 */
		if ((error = secpolicy_sys_config(iocp->ioc_cr, B_FALSE))) {
			miocnak(q, mp, 0, error);
			return;
		}

		error = miocpullup(mp, sizeof (int));
		if (error != 0) {
			miocnak(q, mp, 0, error);
			return;
		}

		abort_enable = *(int *)mp->b_cont->b_rptr;
		miocack(q, mp, 0, 0);
		break;

	case KIOCSETFREQ:
		if (iocp->ioc_count != TRANSPARENT) {
			/*
			 * We don't support non-transparent ioctls,
			 * i.e. I_STR ioctls
			 */
			miocnak(q, mp, 0, EINVAL);
		} else {
			/* Transparent ioctl */
			mcopyin(mp, NULL, sizeof (struct freq_request), NULL);
			qreply(q, mp);
		}
		break;

	default:
		if (conskbd.conskbd_bypassed == B_TRUE) {
			conskbd_legacy_kbd_ioctl(q, mp);
		} else {
			conskbd_virtual_kbd_ioctl(q, mp);
		}
	}

}	/* conskbd_ioctl() */


static void
conskbd_virtual_kbd_ioctl(queue_t *q, mblk_t *mp)
{
	struct iocblk		*iocp;
	mblk_t			*datap;
	int			cmd;
	int			error = 0;

	iocp = (struct iocblk *)mp->b_rptr;

	switch (iocp->ioc_cmd) {
	case KIOCLAYOUT:
		if ((datap = allocb(sizeof (int), BPRI_HI)) == NULL) {
			miocnak(q, mp, 0, ENOMEM);
			break;
		}

		if (conskbd.conskbd_layout == -1)
			*(int *)datap->b_wptr = KBTRANS_USBKB_DEFAULT_LAYOUT;
		else
			*(int *)datap->b_wptr = conskbd.conskbd_layout;

		datap->b_wptr += sizeof (int);
		if (mp->b_cont)
			freemsg(mp->b_cont);
		mp->b_cont = datap;
		miocack(q, mp, sizeof (int), 0);
		break;

	case KIOCSLAYOUT:
		if (iocp->ioc_count != TRANSPARENT) {
			miocnak(q, mp, 0, EINVAL);
			break;
		}
		kbd_layout_bak = conskbd.conskbd_layout;
		conskbd.conskbd_layout = *(intptr_t *)(mp->b_cont->b_rptr);
		if (conskbd.conskbd_layout != kbd_layout_bak) {

			/* notify the upper of the change event */
			if ((datap = conskbd_alloc_firm_event(
			    KEYBOARD_LAYOUT_CHANGE,
			    conskbd.conskbd_layout)) != NULL) {
				if (conskbd.conskbd_directio) {
					putnext(conskbd_regqueue, datap);
				} else {
					freemsg(datap);
				}
			}
		}
		miocack(q, mp, 0, 0);
		break;

	case CONSOPENPOLLEDIO:
		error = miocpullup(mp, sizeof (struct cons_polledio *));
		if (error != 0) {
			miocnak(q, mp, 0, error);
			break;
		}
		if (conskbd.conskbd_lqueue_list == NULL) {
			miocnak(q, mp, 0, EINVAL);
			break;
		}
		conskbd_handle_downstream_msg(q, mp);
		break;

	case CONSCLOSEPOLLEDIO:
		if (conskbd.conskbd_lqueue_list == NULL) {
			miocnak(q, mp, 0, EINVAL);
			break;
		}
		conskbd_handle_downstream_msg(q, mp);
		break;

	case CONSSETABORTENABLE:
		/*
		 * To enable combined STOP-A(or F1-A) to trap into kmdb,
		 * the lower physical keyboard drivers are always told not
		 * to parse abort sequence(refer to consconfig_dacf module).
		 * Instead, lower drivers always send all keydown & keyup
		 * messages up to conskbd, so that when key STOP(or F1) is
		 * pressed on one keyboard and key A is pressed on another
		 * keyboard, the system could trap into kmdb.
		 *
		 * When we by kbtrans_streams_message() invoked kbtrans to
		 * handle ioctls in conskbduwsrv() routine, kbtrans module
		 * already handle the message though it returned to us a
		 * KBTRANS_MESSAGE_NOT_HANDLED. For virtual keyboard, no
		 * special initialization or un-initialization is needed.
		 * So we just return ACK to upper module.
		 */
		miocack(q, mp, 0, 0);
		break;

	case KIOCCMD:
	case KIOCMKTONE:
		if (conskbd.conskbd_lqueue_list == NULL ||
		    mp->b_cont == NULL) {
			miocnak(q, mp, 0, EINVAL);
			break;
		}
		cmd = *(int *)mp->b_cont->b_rptr;
		if (cmd == KBD_CMD_GETLAYOUT) {
			freemsg(mp->b_cont);
			datap = allocb(sizeof (int), BPRI_HI);
			if (datap == NULL) {
				miocnak(q, mp, 0, ENOMEM);
				return;
			}
			if (conskbd.conskbd_layout == -1)
				*(int *)datap->b_wptr =
				    KBTRANS_USBKB_DEFAULT_LAYOUT;
			else
				*(int *)datap->b_wptr = conskbd.conskbd_layout;

			mp->b_cont = datap;
			miocack(q, mp, sizeof (int), 0);
			return;
		}
		conskbd_handle_downstream_msg(q, mp);
		break;

	default:
		miocnak(q, mp, 0, EINVAL);
		break;
	}

}	/* conskbd_virtual_kbd_ioctl() */

static void
conskbd_legacy_kbd_ioctl(queue_t *q, mblk_t *mp)
{
	conskbd_lower_queue_t	*lq;
	struct	iocblk		*iocp;
	int	error = 0;

	iocp = (struct iocblk *)mp->b_rptr;

	ASSERT(conskbd.conskbd_lqueue_nums == 1);
	switch (iocp->ioc_cmd) {

	case KIOCGDIRECT: {
		mblk_t *datap;

		if ((datap = allocb(sizeof (int), BPRI_MED)) == NULL) {
			miocnak(q, mp, 0, ENOMEM);
			break;
		}

		*(int *)datap->b_wptr = conskbd.conskbd_directio;
		datap->b_wptr += sizeof (int);
		if (mp->b_cont != NULL) {
			freemsg(mp->b_cont);
			mp->b_cont = NULL;
		}
		mp->b_cont = datap;
		miocack(q, mp, sizeof (int), 0);
		break;
	}

	case KIOCSDIRECT:
		error = miocpullup(mp, sizeof (int));
		if (error != 0) {
			miocnak(q, mp, 0, error);
			break;
		}
		conskbd.conskbd_directio = *(int *)mp->b_cont->b_rptr;

		/*
		 * Pass this through, if there's something to pass
		 * it through to, so the system keyboard can reset
		 * itself.
		 */
		if (conskbd.conskbd_lqueue_nums > 0) {
			lq = conskbd.conskbd_lqueue_list;
			ASSERT(lq && lq->lqs_next == NULL);
			if (putq(lq->lqs_queue, mp) != 1) {
				miocnak(q, mp, 0, ENOMEM);
				return;
			}
			break;
		}

		miocack(q, mp, 0, 0);
		break;

	default:
		/*
		 * Pass this through, if there's something to pass it
		 * through to; otherwise, reject it.
		 */
		if (conskbd.conskbd_lqueue_nums > 0) {
			lq = conskbd.conskbd_lqueue_list;
			ASSERT(lq && lq->lqs_next == NULL);
			if (putq(lq->lqs_queue, mp) != 1) {
				miocnak(q, mp, 0, ENOMEM);
				return;
			}
			break;
		}

		/* nobody below us; reject it */
		miocnak(q, mp, 0, EINVAL);
		break;
	}

}	/* conskbd_legacy_kbd_ioctl() */


/*
 * Service procedure for lower write queue.
 * Puts things on the queue below us, if it lets us.
 */
static int
conskbdlwserv(queue_t *q)
{
	register mblk_t *mp;

	while (canput(q->q_next) && (mp = getq(q)) != NULL)
		putnext(q, mp);

	return (0);
}	/* conskbdlwserv() */

/*
 * Put procedure for lower read queue.
 * Pass everything up to minor device 0 if "directio" set, otherwise to minor
 * device 1.
 */
static int
conskbdlrput(queue_t *q, mblk_t *mp)
{
	conskbd_lower_queue_t	*lqs;
	struct iocblk	*iocp;
	Firm_event	*fe;

	DPRINTF(PRINT_L1, PRINT_MASK_ALL, ("conskbdlrput\n"));

	switch (mp->b_datap->db_type) {

	case M_FLUSH:
		if (*mp->b_rptr == FLUSHR) {
			flushq(q, FLUSHDATA);	/* XXX doesn't flush M_DELAY */
			*mp->b_rptr &= ~FLUSHR;	/* it has been flushed */
		}
		if (*mp->b_rptr == FLUSHW) {
			flushq(WR(q), FLUSHDATA);
			qreply(q, mp);	/* give the read queues a crack at it */
		} else
			freemsg(mp);
		break;

	case M_DATA:
		if (conskbd.conskbd_bypassed == B_FALSE) {

			fe = (Firm_event *)mp->b_rptr;

			/*
			 * This is a workaround.
			 *
			 * According to HID specification, there are the
			 * following keycode mapping between PS2 and USB,
			 *
			 *	PS2 AT-101 keycode(29)  --->    USB(49)
			 *	PS2 AT-102 keycode(42)  --->    USB(50)
			 *
			 * However, the two keys, AT-101(29) and AT-102(42),
			 * have the same scancode,0x2B, in PS2 scancode SET1
			 * which we are using. The Kb8042 driver always
			 * recognizes the two keys as PS2(29) so that we could
			 * not know which is being pressed or released when we
			 * receive scancode 0x2B. Fortunately, the two keys can
			 * not co-exist in a specific layout. In other words,
			 * in the table of keycode-to-symbol mapping, either
			 * entry 49 or 50 is a hole. So, if we're processing a
			 * keycode 49, we look at the entry for 49.  If it's
			 * HOLE, remap the key to 50; If we're processing a 50,
			 * look at the entry for 50.  If it's HOLE, we remap
			 * the key to 49.
			 */
			if (fe->id == 49 || fe->id == 50) {
				if (conskbd_keyindex->k_normal[50] == HOLE)
					fe->id = 49;
				else
					fe->id = 50;
			}

			/*
			 * Remember key state of each key of lower physical
			 * keyboard. When a keyboard is umplumbed from conskbd,
			 * we will check all key states. By then,  we will fake
			 * a KEY_RELEASED message for each key in KEY_PRESSED
			 * state. Otherwise, upper module will treat these keys
			 * as held-down for ever.
			 */
			iocp = (struct iocblk *)mp->b_rptr;
			lqs = (conskbd_lower_queue_t *)q->q_ptr;
			if (fe->value)
				lqs->lqs_key_state[fe->id] = KEY_PRESSED;
			else
				lqs->lqs_key_state[fe->id] = KEY_RELEASED;

			kbtrans_streams_key(conskbd.conskbd_kbtrans,
			    fe->id, fe->value ? KEY_PRESSED : KEY_RELEASED);
			freemsg(mp);
		} else {
			if (conskbd.conskbd_directio)
				putnext(conskbd_regqueue, mp);
			else if (conskbd_consqueue != NULL)
				putnext(conskbd_consqueue, mp);
			else
				freemsg(mp);
		}
		conskbd_idle_stamp = gethrestime_sec();
		break;

	case M_IOCACK:
	case M_IOCNAK:
		iocp = (struct iocblk *)mp->b_rptr;
		lqs = (conskbd_lower_queue_t *)q->q_ptr;

		DPRINTF(PRINT_L1, PRINT_MASK_ALL, ("conskbdlrput: "
		    "ACK/NAK - cmd 0x%x\n", iocp->ioc_cmd));

		conskbd_lqs_ack_complete(lqs, mp);
		break;

	case M_ERROR:
	case M_HANGUP:
	default:
		freemsg(mp);	/* anything useful here? */
		break;
	}

	return (0);
}	/* conskbdlrput() */


/* ARGSUSED */
static int
conskbd_kstat_update(kstat_t *ksp, int rw)
{
	if (rw == KSTAT_WRITE)
		return (EACCES);

	conskbd_kstat.idle_sec.value.l = gethrestime_sec() - conskbd_idle_stamp;

	return (0);

}	/* conskbd_kstat_update() */

/*
 * STREAMS architecuture provides guarantee that the ID of each
 * message, iocblk.ioc_id, in a stream is unique. The following
 * routine performes the task: When receive request from upstream,
 * it saves the request in a global link list, clones the request,
 * and then sends a copy of the request to each of lower queues
 * which are plumbed into conskbd. And then, when receives responses
 * from lower queues in conskbdlrput() routine, we can know the
 * request matching received responses by searching the global linked
 * list to find the request which has the same message ID of the
 * response. Then, when all lower queues response this request, we
 * give a response to upstreams based the following policy:
 * If any one of lower queues acks our reuqest, then we return ack
 * to upstreams; only if all lower queues nak our request, we return
 * nak to upstreams. If all responses are nak, the error number of
 * the first response is sent to upstream.
 */
static void
conskbd_handle_downstream_msg(queue_t *q, mblk_t *mp)
{
	conskbd_pending_msg_t	*msg;
	conskbd_lower_queue_t	*lqs;
	struct iocblk	*iocp;
	mblk_t		*clonemp;
	int		retry;

	if (conskbd.conskbd_lqueue_nums == 0) {
		miocnak(q, mp, 0, EINVAL);
		return;
	}

	msg = (conskbd_pending_msg_t *)
	    kmem_zalloc(sizeof (conskbd_pending_msg_t), KM_SLEEP);
	mutex_init(&msg->kpm_lock, NULL, MUTEX_DRIVER, NULL);
	lqs = conskbd.conskbd_lqueue_list;
	iocp = (struct iocblk *)mp->b_rptr;

	ASSERT(iocp->ioc_cmd == CONSOPENPOLLEDIO ||
	    iocp->ioc_cmd == CONSCLOSEPOLLEDIO ||
	    iocp->ioc_cmd == KIOCCMD ||
	    iocp->ioc_cmd == KIOCMKTONE);

	msg->kpm_upper_queue = q;
	msg->kpm_req_msg = mp;
	msg->kpm_req_id = iocp->ioc_id;
	msg->kpm_req_cmd = iocp->ioc_cmd;
	msg->kpm_req_nums = conskbd.conskbd_lqueue_nums;
	conskbd_mux_enqueue_msg(msg);

	for (retry = 0, lqs = conskbd.conskbd_lqueue_list; lqs; ) {

		/*
		 * if a lower physical keyboard is not in polled I/O
		 * mode, we couldn't send CONSCLOSEPOLLEDIO to it,
		 * otherwise, system will panic.
		 */
		if (iocp->ioc_cmd == CONSCLOSEPOLLEDIO &&
		    lqs->lqs_polledio == NULL) {
			lqs = lqs->lqs_next;
			msg->kpm_req_nums --;
			retry = 0;
			continue;
		}

		clonemp = copymsg(mp);
		if (clonemp != NULL) {
			if (putq(lqs->lqs_queue, clonemp) == 1) {
				lqs = lqs->lqs_next;
				retry = 0;
				continue;
			}

			/*
			 * failed to invoke putq(), retry.
			 */
			freemsg(clonemp);
		}

		/*
		 * During testing it was observed that occasionally
		 * copymsg() would fail during boot. The reason for
		 * these failures is unknown. Since we really want
		 * to successfully plumb up all the attached keyboards
		 * during boot we do a best effort here by retrying
		 * the copymsg() call in the hopes that it will
		 * succeeded upon subsequent invocations.
		 *
		 * If all the calls to copymsg() fails, it will cause
		 * the corresponding keyboard to be unavailable, or
		 * or behave weirdly,
		 *
		 * 1) for CONSOPENPOLLEDIO
		 *	if copymsg()fails, the corresponding keyboard
		 *	is not available in polled I/O mode once
		 *	entering kmdb;
		 * 2) for CONSCLOSEPOLLEDIO
		 *	if copymsg() fails, the corresponding keyboard
		 *	is not available in normal mode once returning
		 *	from kmdb;
		 * 3) for KIOCCMD
		 *	3.1) for KBD_CMD_NOBELL
		 *		there's no beep in USB and PS2 keyboard,
		 *		this ioctl actually disables the beep on
		 *		system mainboard. Note that all the cloned
		 *		messages sent down to lower queues do the
		 *		same job for system mainboard. Therefore,
		 *		even if we fail to send this ioctl to most
		 *		of lower queues, the beep still would be
		 *		disabled. So, no trouble exists here.
		 *	3.2) for others
		 *		nothing;
		 *
		 * However, all cases could be resume next time when the
		 * same request comes again.
		 */
		if (retry ++ >= 5) {
			dev_t	devt;
			char	path[MAXPATHLEN + 1];

			devt = lqs->lqs_queue->q_stream->sd_vnode->v_rdev;
			switch (iocp->ioc_cmd) {
			case CONSOPENPOLLEDIO:
				if (ddi_dev_pathname(devt, S_IFCHR,
				    path) == DDI_SUCCESS)
					cmn_err(CE_WARN, "conskbd: "
					    "keyboard is not available"
					    " for system debugging: %s",
					    path);
				break;

			case CONSCLOSEPOLLEDIO:
				if (ddi_dev_pathname(devt, S_IFCHR,
				    path) == DDI_SUCCESS)
					cmn_err(CE_WARN, "conskbd: "
					    "keyboard is not available:"
					    " %s", path);
				break;

			default:
				break;
			}
			msg->kpm_req_nums --;
			lqs = lqs->lqs_next;
			retry = 0;
		}
	}

	if (msg->kpm_req_nums == 0) {
		conskbd_mux_dequeue_msg(msg);
		kmem_free(msg, sizeof (*msg));
		miocnak(q, mp, 0, ENOMEM);
	}

}	/* conskbd_handle_downstream_msg() */


static void
conskbd_ioc_plink(queue_t *q, mblk_t *mp)
{
	mblk_t		*req;
	queue_t		*lowque;
	struct linkblk		*linkp;
	conskbd_lower_queue_t	*lqs;

	lqs = kmem_zalloc(sizeof (*lqs), KM_SLEEP);
	ASSERT(lqs->lqs_state == LQS_UNINITIALIZED);

	linkp = (struct linkblk *)mp->b_cont->b_rptr;
	lowque = linkp->l_qbot;

	lqs->lqs_queue = lowque;
	lqs->lqs_pending_plink = mp;
	lqs->lqs_pending_queue = q;

	req = mkiocb(CONSSETKBDTYPE);
	if (req == NULL) {
		miocnak(q, mp, 0, ENOMEM);
		kmem_free(lqs, sizeof (*lqs));
		return;
	}

	req->b_cont = allocb(sizeof (int), BPRI_MED);
	if (req->b_cont == NULL) {
		freemsg(req);
		miocnak(q, mp, 0, ENOMEM);
		kmem_free(lqs, sizeof (*lqs));
		return;
	}

	lowque->q_ptr = lqs;
	OTHERQ(lowque)->q_ptr = lqs;
	*(int *)req->b_cont->b_wptr = KB_USB;
	req->b_cont->b_wptr += sizeof (int);

	lqs->lqs_state = LQS_KIOCTYPE_ACK_PENDING;

	if (putq(lowque, req) != 1) {
		freemsg(req);
		miocnak(lqs->lqs_pending_queue,
		    lqs->lqs_pending_plink, 0, ENOMEM);
		lowque->q_ptr = NULL;
		OTHERQ(lowque)->q_ptr = NULL;
		kmem_free(lqs, sizeof (*lqs));
	}

}	/* conskbd_ioc_plink() */


static void
conskbd_ioc_punlink(queue_t *q, mblk_t *mp)
{
	int			index;
	struct linkblk		*linkp;
	conskbd_lower_queue_t	*lqs;
	conskbd_lower_queue_t	*prev;

	linkp = (struct linkblk *)mp->b_cont->b_rptr;
	prev = conskbd.conskbd_lqueue_list;
	for (lqs = prev; lqs; lqs = lqs->lqs_next) {
		if (lqs->lqs_queue == linkp->l_qbot) {
			if (prev == lqs)
				conskbd.conskbd_lqueue_list =
				    lqs->lqs_next;
			else
				prev->lqs_next = lqs->lqs_next;

			lqs->lqs_queue->q_ptr =  NULL;
			OTHERQ(lqs->lqs_queue)->q_ptr = NULL;
			conskbd.conskbd_lqueue_nums --;
			if (conskbd.conskbd_lqueue_nums == 0) {
				kbd_layout_bak = conskbd.conskbd_layout;
				conskbd.conskbd_layout = -1;
			}

			for (index = 0; index < KBTRANS_KEYNUMS_MAX; index ++) {
				if (lqs->lqs_key_state[index] == KEY_PRESSED)
					kbtrans_streams_key(
					    conskbd.conskbd_kbtrans,
					    index,
					    KEY_RELEASED);
			}

			kmem_free(lqs, sizeof (*lqs));
			miocack(q, mp, 0, 0);
			return;
		}
		prev = lqs;
	}
	miocnak(q, mp, 0, EINVAL);

}	/* conskbd_ioc_punlink() */

/*
 * Every physical keyboard has a corresponding STREAMS queue. We call this
 * queue lower queue. Every lower queue has a state, refer to conskbd.h file
 * about "enum conskbd_lqs_state".
 * The following routine is used to handle response messages from lower queue.
 * When receiving ack/nak message from lower queue(s), the routine determines
 * the passage for it according to the current state of this lower queue.
 */
static void
conskbd_lqs_ack_complete(conskbd_lower_queue_t *lqs, mblk_t *mp)
{
	switch (lqs->lqs_state) {

	/* S6: working in virtual keyboard mode, multi-keyboards are usable */
	case LQS_INITIALIZED:
		conskbd_mux_upstream_msg(lqs, mp);
		break;

	/* S5: working in legacy mode, only one keyboard is usable */
	case LQS_INITIALIZED_LEGACY:
		conskbd_legacy_upstream_msg(lqs, mp);
		break;

	/* S4: wait lower queue to acknowledge KIOCSLED/KIOCGLED  message */
	case LQS_KIOCSLED_ACK_PENDING:
		conskbd_kiocsled_complete(lqs, mp);
		break;

	/* S3: wait lower queue to acknowledge KIOCLAYOUT  message */
	case LQS_KIOCLAYOUT_ACK_PENDING:
		conskbd_kioclayout_complete(lqs, mp);
		break;

	/* S2: wait lower queue to acknowledge KIOCTRANS  message */
	case LQS_KIOCTRANS_ACK_PENDING:
		conskbd_kioctrans_complete(lqs, mp);
		break;

	/* S1: wait lower queue to acknowledge KIOCTYPE  message */
	case LQS_KIOCTYPE_ACK_PENDING:
		conskbd_kioctype_complete(lqs, mp);
		break;

	/* if reaching here, there must be a error */
	default:
		freemsg(mp);
		cmn_err(CE_WARN, "conskbd: lqs_ack_complete() state error");
		break;
	}

}	/* conskbd_lqs_ack_complete() */


static void
conskbd_kioctype_complete(conskbd_lower_queue_t *lqs, mblk_t *mp)
{
	struct iocblk	*iocp;
	mblk_t		*req;
	queue_t		*lowerque;
	int		err = ENOMEM;

	ASSERT(lqs->lqs_pending_plink);
	ASSERT(lqs->lqs_state == LQS_KIOCTYPE_ACK_PENDING);

	lowerque = lqs->lqs_queue;

	switch (mp->b_datap->db_type) {
	case M_IOCACK:
		req = mkiocb(KIOCTRANS);
		if (req == NULL) {
			goto err_exit;
		}

		req->b_cont = allocb(sizeof (int), BPRI_MED);
		if (req->b_cont == NULL) {
			freemsg(req);
			goto err_exit;
		}

		/* Set the translate mode to TR_UNTRANS_EVENT */
		*(int *)req->b_cont->b_wptr = TR_UNTRANS_EVENT;
		req->b_cont->b_wptr += sizeof (int);

		/* Ready to handle the response to KIOCTRANS */
		lqs->lqs_state = LQS_KIOCTRANS_ACK_PENDING;

		if (putq(lowerque, req) != 1) {
			freemsg(req);
			goto err_exit;
		}
		freemsg(mp);
		return;

	case M_IOCNAK:
		/*
		 * The lower keyboard driver can't mimic USB keyboard,
		 * that's say, the physical keyboard is an old one, such
		 * as TYPE 3/4/5 one. In this case, the virtual keyboard
		 * is disabled, and the data from lower keyboard driver
		 * will bypass the conskbd module.
		 */

		/*
		 * if there is any other keyborad already linked under the
		 * conskbd, we reject the current one.
		 */
		if (conskbd.conskbd_lqueue_nums > 0) {
			iocp = (struct iocblk *)mp->b_rptr;
			err = iocp->ioc_error;
			goto err_exit;
		}

		/*
		 * link this keyboard under conskbd.
		 */
		qwriter(lowerque, mp, conskbd_link_lowque_legacy, PERIM_OUTER);
		return;
	}

err_exit:
	miocnak(lqs->lqs_pending_queue, lqs->lqs_pending_plink, 0, err);
	lowerque->q_ptr = NULL;
	OTHERQ(lowerque)->q_ptr = NULL;
	kmem_free(lqs, sizeof (*lqs));
	freemsg(mp);

}	/* conskbd_kioctype_complete() */

static void
conskbd_kioctrans_complete(conskbd_lower_queue_t *lqs, mblk_t *mp)
{
	struct iocblk	*iocp;
	mblk_t		*req;
	queue_t		*lowerque;
	int		err = ENOMEM;

	ASSERT(lqs->lqs_pending_plink != NULL);
	ASSERT(lqs->lqs_state == LQS_KIOCTRANS_ACK_PENDING);

	lowerque = lqs->lqs_queue;

	switch (mp->b_datap->db_type) {
	case M_IOCACK:
		req = mkiocb(KIOCLAYOUT);
		if (req == NULL) {
			goto err_exit;
		}

		req->b_cont = allocb(sizeof (int), BPRI_MED);
		if (req->b_cont == NULL) {
			freemsg(req);
			goto err_exit;
		}

		/* waiting for response to KIOCLAYOUT */
		lqs->lqs_state = LQS_KIOCLAYOUT_ACK_PENDING;
		if (putq(lqs->lqs_queue, req) != 1) {
			freemsg(req);
			goto err_exit;
		}
		freemsg(mp);
		return;

	case M_IOCNAK:
		iocp = (struct iocblk *)mp->b_rptr;
		err = iocp->ioc_error;
		goto err_exit;
	}

err_exit:
	miocnak(lqs->lqs_pending_queue, lqs->lqs_pending_plink, 0, err);
	lowerque->q_ptr = NULL;
	OTHERQ(lowerque)->q_ptr = NULL;
	kmem_free(lqs, sizeof (*lqs));
	freemsg(mp);

}	/* conskbd_kioctrans_complete() */

/*
 * Allocate a firm event
 */
static mblk_t *
conskbd_alloc_firm_event(ushort_t id, int value)
{
	mblk_t	*mb;
	Firm_event *fe;

	if ((mb = allocb(sizeof (Firm_event), BPRI_HI)) != NULL) {
		fe = (Firm_event *)mb->b_wptr;
		fe->id = id;
		fe->pair_type = FE_PAIR_NONE;
		fe->pair = '\0';
		fe->value = value;
		mb->b_wptr += sizeof (Firm_event);
	}

	return (mb);
}

static void
conskbd_kioclayout_complete(conskbd_lower_queue_t *lqs, mblk_t *mp)
{
	mblk_t		*req;
	int		layout;
	boolean_t	fail;

	ASSERT(lqs->lqs_pending_plink != NULL);
	ASSERT(lqs->lqs_state == LQS_KIOCLAYOUT_ACK_PENDING);

	switch (mp->b_datap->db_type) {
	case M_IOCACK:
		if (miocpullup(mp, sizeof (int)) == 0) {
			layout = *(int *)mp->b_cont->b_rptr;
			/*
			 * We just accept the layout of the first keyboard
			 * requesting to be linked under conskbd. If current
			 * keyboard is the first one, and if we get right
			 * layout from it, we set conskbd's layout
			 */
			if (layout != -1 && conskbd.conskbd_layout == -1) {
				if (layout == 0) {
					conskbd.conskbd_layout = kbd_layout_bak;
				} else {
					conskbd.conskbd_layout = layout;
					if (layout == kbd_layout_bak) {
						break;
					}
					if ((req = conskbd_alloc_firm_event(
					    KEYBOARD_LAYOUT_CHANGE,
					    layout)) != NULL) {
						if (conskbd.conskbd_directio) {
							putnext(
							    conskbd_regqueue,
							    req);
						} else if (conskbd_consqueue
						    != NULL) {
							putnext(
							    conskbd_consqueue,
							    req);
						} else {
							freemsg(req);
						}
					}
				}
			}
		}
		break;


	/* if fail, leave conskbd's layout as it is */
	case M_IOCNAK:
		break;
	}

	fail = B_TRUE;

	if (conskbd.conskbd_led_state == -1)
		req = mkiocb(KIOCGLED);
	else
		req = mkiocb(KIOCSLED);

	if (req) {
		req->b_cont = allocb(sizeof (uchar_t), BPRI_MED);
		if (req->b_cont) {
			if (conskbd.conskbd_led_state != -1) {
				*(uchar_t *)req->b_cont->b_wptr =
				    conskbd.conskbd_led_state;
				req->b_cont->b_wptr += sizeof (uchar_t);
			}

			/* waiting for response to KIOCSLED */
			lqs->lqs_state = LQS_KIOCSLED_ACK_PENDING;
			if (putq(lqs->lqs_queue, req) == 1) {
				fail = B_FALSE;
			} else {
				freemsg(req);
			}

		} else {
			freemsg(req);
		}
	}

	if (fail) {
		/*
		 * If fail to allocate KIOCSLED/KIOCGLED message or put
		 * the message into lower queue, we immediately link
		 * current keyboard under conskbd. Thus, even if fails
		 * to set/get LED, this keyboard could be available.
		 */
		qwriter(lqs->lqs_queue,
		    mp, conskbd_link_lowque_virt, PERIM_OUTER);
	} else {
		freemsg(mp);
	}

}	/* conskbd_kioclayout_complete() */


static void
conskbd_kiocsled_complete(conskbd_lower_queue_t *lqs, mblk_t *mp)
{
	int	led_state;

	ASSERT(lqs->lqs_pending_plink != NULL);
	ASSERT(lqs->lqs_state == LQS_KIOCSLED_ACK_PENDING);

	if (conskbd.conskbd_led_state == -1) {
		switch (mp->b_datap->db_type) {
		case M_IOCACK:
			if (miocpullup(mp, sizeof (uchar_t)) == 0) {
				led_state = *(uchar_t *)mp->b_cont->b_rptr;
				conskbd.conskbd_led_state = led_state;
				kbtrans_streams_setled(conskbd.conskbd_kbtrans,
				    led_state);
			}
			break;

		/* if fail, leave conskbd's led_state as it is */
		case M_IOCNAK:
			break;
		}
	}

	/*
	 * Basically, failure of setting/getting LED is not a fatal
	 * error, so we will plumb the lower queue into conskbd whether
	 * setting/getting LED succeeds or fails.
	 */
	qwriter(lqs->lqs_queue, mp, conskbd_link_lowque_virt, PERIM_OUTER);

}	/* conskbd_kiocsled_complete() */


static void
conskbd_mux_upstream_msg(conskbd_lower_queue_t *lqs, mblk_t *mp)
{
	conskbd_pending_msg_t	*msg;
	struct iocblk		*iocp;
	int			error;
	dev_t			devt;
	char			path[MAXPATHLEN + 1];

	ASSERT(lqs->lqs_state == LQS_INITIALIZED);
	msg = conskbd_mux_find_msg(mp);

	if (!msg) {
		/*
		 * Here we discard the response if:
		 *
		 *   1. It's an KIOCSLED request; see conskbd_streams_setled().
		 *   2. The application has already closed the upper stream;
		 *		see conskbdclose()
		 */
		freemsg(mp);
		return;
	}

	/*
	 * We use the b_next field of mblk_t structure to link all
	 * response coming from lower queues into a linkage list,
	 * and make use of the b_prev field to save a pointer to
	 * the lower queue from which the current response message
	 * comes.
	 */
	ASSERT(mp->b_next == NULL && mp->b_prev == NULL);
	mutex_enter(&msg->kpm_lock);
	mp->b_next = msg->kpm_resp_list;
	mp->b_prev = (mblk_t *)lqs;
	msg->kpm_resp_list = mp;
	msg->kpm_resp_nums ++;

	if (msg->kpm_resp_nums < msg->kpm_req_nums) {
		mutex_exit(&msg->kpm_lock);
		return;
	}

	ASSERT(msg->kpm_resp_nums == msg->kpm_req_nums);
	ASSERT(mp == msg->kpm_resp_list);

	mutex_exit(&msg->kpm_lock);

	conskbd_mux_dequeue_msg(msg);


	/*
	 * Here, we have the policy that, if any one lower queue ACK
	 * our reuqest, then we return ACK to upstreams; only if all
	 * lower queues NAK our request, we return NAK to upstreams.
	 * if all responses are nak, the errno of the  first response
	 * is sent to upstreams
	 */
	ASSERT(mp->b_rptr);
	error = ((struct iocblk *)mp->b_rptr)->ioc_error;

	switch (msg->kpm_req_cmd) {
	case CONSOPENPOLLEDIO:
		/*
		 * Here, we can safely ignore the NAK message. If any one lower
		 * queue returns NAK, the pointer to the corresponding polledio
		 * structure will remain null, that's say lqs->lqs_polledio =
		 * null. When we need to invoke polled I/O interface, we will
		 * check if the pointer is null.
		 */
		for (mp = msg->kpm_resp_list; mp; ) {
			cons_polledio_t		*polledio;

			msg->kpm_resp_list = mp->b_next;
			lqs = (conskbd_lower_queue_t *)mp->b_prev;
			devt = lqs->lqs_queue->q_stream->sd_vnode->v_rdev;
			if (mp->b_datap->db_type == M_IOCACK) {
				polledio = *(struct cons_polledio **)
				    mp->b_cont->b_rptr;
				if (polledio->cons_polledio_version ==
				    CONSPOLLEDIO_V1) {
					lqs->lqs_polledio = polledio;
					error = 0;
				} else {
					/*
					 * USB and PS2 keyboard drivers should
					 * use the same cons_polledio structure
					 * as conskbd.
					 */
					if (ddi_dev_pathname(devt, S_IFCHR,
					    path) == DDI_SUCCESS) {
						cmn_err(CE_WARN, "keyboard "
						    "driver does not support "
						    "system debugging: %s",
						    path);
					}
					error = EINVAL;
				}
			} else {
				if (ddi_dev_pathname(devt, S_IFCHR, path) ==
				    DDI_SUCCESS) {
					cmn_err(CE_WARN, "conskbd: keyboard is"
					    " not available for system"
					    " debugging:  %s", path);
				}
			}
			mp->b_next = NULL;
			mp->b_prev = NULL;
			freemsg(mp);
			mp = msg->kpm_resp_list;
		}

		mp = msg->kpm_req_msg;
		if (error == 0) {
			*(struct cons_polledio **)mp->b_cont->b_rptr =
			    &conskbd.conskbd_polledio;
		}
		break;

	case CONSCLOSEPOLLEDIO:
		for (mp = msg->kpm_resp_list; mp; ) {
			msg->kpm_resp_list = mp->b_next;
			lqs = (conskbd_lower_queue_t *)mp->b_prev;
			if (mp->b_datap->db_type == M_IOCACK) {
				lqs->lqs_polledio = NULL;
				error = 0;
			} else {
				devt =
				    lqs->lqs_queue->q_stream->sd_vnode->v_rdev;

				if (ddi_dev_pathname(devt, S_IFCHR, path) ==
				    DDI_SUCCESS) {
					cmn_err(CE_WARN, "conskbd: keyboard is"
					    " not available: %s", path);
				}
			}

			mp->b_next = NULL;
			mp->b_prev = NULL;
			freemsg(mp);
			mp = msg->kpm_resp_list;
		}
		break;

	case KIOCCMD:
	case KIOCMKTONE:
		for (mp = msg->kpm_resp_list; mp; ) {
			msg->kpm_resp_list = mp->b_next;

			if (mp->b_datap->db_type == M_IOCACK)
				error = 0;
			mp->b_next = NULL;
			mp->b_prev = NULL;
			freemsg(mp);
			mp = msg->kpm_resp_list;
		}
		break;

	default:  /* it is impossible to reach here */
		cmn_err(CE_WARN, "conskbd: unexpected ioctl reply");
	}

	mp = msg->kpm_req_msg;
	if (error == 0) {
		mp->b_datap->db_type = M_IOCACK;
	} else {
		mp->b_datap->db_type = M_IOCNAK;
	}
	iocp = (struct iocblk *)mp->b_rptr;
	iocp->ioc_error = error;
	qreply(msg->kpm_upper_queue, mp);
	mutex_destroy(&msg->kpm_lock);
	kmem_free(msg, sizeof (*msg));

}	/* conskbd_mux_upstream_msg() */

static void
conskbd_link_lowque_legacy(queue_t *lowque, mblk_t *mp)
{
	conskbd_lower_queue_t *lqs;

	freemsg(mp);

	/*
	 * Bypass the virutal keyboard for old hardware,
	 * Now, only current legacy keyboard can be linked
	 * under conskbd
	 */
	conskbd.conskbd_bypassed = B_TRUE;

	/*
	 * Link the lower queue under conskbd
	 */
	lqs = (conskbd_lower_queue_t *)lowque->q_ptr;
	lqs->lqs_state = LQS_INITIALIZED_LEGACY;
	lqs->lqs_next = conskbd.conskbd_lqueue_list;
	conskbd.conskbd_lqueue_list = lqs;
	conskbd.conskbd_lqueue_nums++;

	mioc2ack(lqs->lqs_pending_plink, NULL, 0, 0);
	qreply(lqs->lqs_pending_queue, lqs->lqs_pending_plink);

}	/* conskbd_link_lowque_legacy() */

static void
conskbd_link_lowque_virt(queue_t *lowque, mblk_t *mp)
{
	int		index;
	conskbd_lower_queue_t *lqs;

	freemsg(mp);

	lqs = (conskbd_lower_queue_t *)lowque->q_ptr;

	ASSERT(lqs->lqs_queue == lowque);
	ASSERT(lqs->lqs_pending_plink != NULL);

	/*
	 * Now, link the lower queue under conskbd
	 */
	for (index = 0; index < KBTRANS_KEYNUMS_MAX; index ++) {
		lqs->lqs_key_state[index] = KEY_RELEASED;
	}
	lqs->lqs_next = conskbd.conskbd_lqueue_list;
	lqs->lqs_state = LQS_INITIALIZED;
	conskbd.conskbd_lqueue_nums++;
	conskbd.conskbd_lqueue_list = lqs;
	mioc2ack(lqs->lqs_pending_plink, NULL, 0, 0);
	qreply(lqs->lqs_pending_queue, lqs->lqs_pending_plink);

}	/* conskbd_link_lowque_virt() */

/*ARGSUSED*/
static void
conskbd_legacy_upstream_msg(conskbd_lower_queue_t *lqs, mblk_t *mp)
{
	struct iocblk	*iocp;

	ASSERT(lqs && lqs->lqs_state == LQS_INITIALIZED_LEGACY);

	/*
	 * We assume that all of the ioctls are headed to the
	 * conskbd_regqueue if it is open.  We are intercepting a few ioctls
	 * that we know belong to conskbd_consqueue, and sending them there.
	 * Any other, new ioctls that have to be routed to conskbd_consqueue
	 * should be added to this list.
	 */
	iocp = (struct iocblk *)mp->b_rptr;

	if ((iocp->ioc_cmd == CONSOPENPOLLEDIO) ||
	    (iocp->ioc_cmd == CONSCLOSEPOLLEDIO)) {

		DPRINTF(PRINT_L1, PRINT_MASK_ALL,
		    ("conskbd_legacy_upstream_msg: "
		    "CONSOPEN/CLOSEPOLLEDIO ACK/NAK\n"));
		putnext(conskbd_consqueue, mp);

	} else if (conskbd_regqueue != NULL) {
		DPRINTF(PRINT_L1, PRINT_MASK_ALL,
		    ("conskbd_legacy_upstream_msg: conskbd_regqueue != NULL"));

		putnext(conskbd_regqueue, mp);

	} else if (conskbd_consqueue != NULL) {
		DPRINTF(PRINT_L1, PRINT_MASK_ALL,
		    ("conskbd_legacy_upstream_msg: conskbd_consqueue != NULL"));
		putnext(conskbd_consqueue, mp);
	} else {
		/* if reached here, it must be a error */
		cmn_err(CE_WARN,
		    "kb:  no destination for IOCACK/IOCNAK!");
		freemsg(mp);
	}

}	/* conskbd_legacy_upstream_msg() */

/*
 * This routine is a callback routine for kbtrans module to set LED.
 * Kbtrans will invoke it in two cases:
 *
 * 1) application initiated request
 *	A KIOCSLED ioctl is sent by an application. The ioctl will be
 *	be prcoessed by queue service procedure conskbduwsrv(), which
 *	in turn calls kbtrans to process the ioctl. Then kbtrans invokes
 *	conskbd_streams_setled() to set LED, after that,  kbtrans will
 *	return an ACK message to upper module.
 *
 * 2) Kbtrans initiated the request
 *	When conskbd works in TR_ASCII translation mode, if anyone of
 *	CapsLock, NumberLock and Compose keys is pressed, kbtrans need
 *	to set LED. In this case, there is no ioctl from upper module.
 *	There is no requirement to send response to somebody.
 *
 * In first case, kbtrans will send response to upper module; and in the
 * second, we don't need to send response. So conskbd_streams_setled()
 * has no return value.
 */
static void
conskbd_streams_setled(struct kbtrans_hardware *hw, int led_state)
{
	conskbd_state_t  *conskbdp = (conskbd_state_t *)hw;
	conskbd_lower_queue_t *lqs;
	mblk_t		*req;

	ASSERT(&conskbd == conskbdp);

	if (led_state == -1)
		return;

	conskbdp->conskbd_led_state = led_state;

	/*
	 * Basically, failing to set LED is not a fatal error, we just skip
	 * it if this happens.
	 */
	for (lqs = conskbdp->conskbd_lqueue_list; lqs; lqs = lqs->lqs_next) {
		req = mkiocb(KIOCSLED);

		if (!req) {
			continue;
		}

		req->b_cont = allocb(sizeof (uchar_t), BPRI_MED);
		if (!req->b_cont) {
			freemsg(req);
			continue;
		}
		*(uchar_t *)req->b_cont->b_wptr = led_state;
		req->b_cont->b_wptr += sizeof (uchar_t);
		if (putq(lqs->lqs_queue, req) != 1)
			freemsg(req);
	}

}	/* conskbd_streams_setled() */

static void
conskbd_polledio_setled(struct kbtrans_hardware *hw, int led_state)
{
	conskbd_state_t  *conskbdp = (conskbd_state_t *)hw;
	struct cons_polledio		*cb;
	conskbd_lower_queue_t	*lqs;

	for (lqs = conskbdp->conskbd_lqueue_list; lqs; lqs = lqs->lqs_next) {
		cb = lqs->lqs_polledio;
		if ((cb != NULL) && (cb->cons_polledio_setled != NULL)) {
			cb->cons_polledio_setled(cb->cons_polledio_argument,
			    led_state);
		}
	}

}	/* conskbd_polledio_setled() */

static boolean_t
conskbd_polled_keycheck(struct kbtrans_hardware *hw,
    kbtrans_key_t *keycode, enum keystate *state)
{
	conskbd_state_t  *conskbdp = (conskbd_state_t *)hw;
	struct cons_polledio	*cb;
	conskbd_lower_queue_t	*lqs;
	boolean_t	ret = B_FALSE;

	for (ret = B_FALSE, lqs = conskbdp->conskbd_lqueue_list; lqs != NULL;
	    lqs = lqs->lqs_next) {
		cb = lqs->lqs_polledio;
		if ((cb != NULL) &&
		    (cb->cons_polledio_keycheck != NULL)) {
			ret = cb->cons_polledio_keycheck(
			    cb->cons_polledio_argument, keycode, state);
		}

		/* Get a char from lower queue(hardware) ? */
		if (ret == B_TRUE) {

			/* A legacy keyboard ? */
			if (conskbd.conskbd_bypassed == B_TRUE)
				break;

			/*
			 * This is the PS2 scancode 0x2B -> USB(49) /
			 * USB(50) keycode mapping workaround, for
			 * polled mode.
			 *
			 * There are two possible USB keycode mappings
			 * for PS2 scancode 0x2B and this workaround
			 * makes sure that we use the USB keycode that
			 * does not end up being mapped to a HOLE key
			 * using the current keyboard translation
			 * tables.
			 *
			 * See conskbdlrput() for a detailed
			 * explanation of the problem.
			 */
			if (*keycode == 49 || *keycode == 50) {
				if (conskbd_keyindex->k_normal[50] == HOLE)
					*keycode = 49;
				else
					*keycode = 50;
			}

			break;
		}
	}

	return (ret);

}	/* conskbd_polled_keycheck() */

static boolean_t
conskbd_override_kbtrans(queue_t *q, mblk_t *mp)
{
	struct iocblk		*iocp;
	int		directio;
	int		error;

	if (mp->b_datap->db_type != M_IOCTL)
		return (B_FALSE);

	iocp = (struct iocblk *)mp->b_rptr;

	switch (iocp->ioc_cmd) {
	case KIOCGDIRECT: {
		/*
		 * Don't let the kbtrans-based code see this; it will
		 * respond incorrectly.
		 */
		register mblk_t *datap;

		if ((datap = allocb((int)sizeof (int), BPRI_MED)) == NULL) {
			miocnak(q, mp, 0, ENOMEM);
			return (B_TRUE);
		}

		*(int *)datap->b_wptr = conskbd.conskbd_directio;
		datap->b_wptr += sizeof (int);
		if (mp->b_cont) {
			freemsg(mp->b_cont);
			mp->b_cont = NULL;
		}
		mp->b_cont = datap;
		miocack(q, mp, sizeof (int), 0);
		return (B_TRUE);
	}

	case KIOCSDIRECT:
		/*
		 * Peek at this, set our variables, and then let the kbtrans
		 * based code see it and respond to it.
		 */
		error = miocpullup(mp, sizeof (int));
		if (error != 0) {
			return (B_FALSE);
		}

		directio = *(int *)mp->b_cont->b_rptr;
		if (directio != 0 && directio != 1) {
			miocnak(q, mp, 0, EINVAL);
			return (B_TRUE);
		}
		conskbd.conskbd_directio = directio;

		if (conskbd.conskbd_directio) {
			kbtrans_streams_set_queue(
			    conskbd.conskbd_kbtrans, conskbd_regqueue);
		} else {
			kbtrans_streams_set_queue(
			    conskbd.conskbd_kbtrans, conskbd_consqueue);
		}

		/*
		 * Let the kbtrans-based code see this and respond to it.
		 */
		return (B_FALSE);

	default:
		return (B_FALSE);
	}

}	/* conskbd_override_kbtrans() */


static void
conskbd_polledio_enter(cons_polledio_arg_t arg)
{
	conskbd_state_t		*conskbdp;
	struct cons_polledio		*cb;
	conskbd_lower_queue_t	*lqs;

	conskbdp = (conskbd_state_t *)arg;
	for (lqs = conskbdp->conskbd_lqueue_list; lqs; lqs = lqs->lqs_next) {
		cb = lqs->lqs_polledio;
		if ((cb != NULL) && (cb->cons_polledio_enter != NULL)) {
			cb->cons_polledio_enter(cb->cons_polledio_argument);
		}
	}

}	/* conskbd_polledio_enter() */

static void
conskbd_polledio_exit(cons_polledio_arg_t arg)
{
	conskbd_state_t		*conskbdp;
	struct cons_polledio		*cb;
	conskbd_lower_queue_t	*lqs;

	conskbdp = (conskbd_state_t *)arg;
	for (lqs = conskbdp->conskbd_lqueue_list; lqs; lqs = lqs->lqs_next) {
		cb = lqs->lqs_polledio;
		if ((cb != NULL) && (cb->cons_polledio_exit != NULL)) {
			cb->cons_polledio_exit(cb->cons_polledio_argument);
		}
	}

}	/* conskbd_polledio_exit() */

static int
conskbd_polledio_getchar(cons_polledio_arg_t arg)
{
	conskbd_state_t  *conskbdp;

	conskbdp = (conskbd_state_t *)arg;

	return (kbtrans_getchar(conskbdp->conskbd_kbtrans));

}	/* conskbd_polledio_getchar() */

static int
conskbd_polledio_ischar(cons_polledio_arg_t arg)
{
	conskbd_state_t  *conskbdp;

	conskbdp = (conskbd_state_t *)arg;

	return (kbtrans_ischar(conskbdp->conskbd_kbtrans));

}	/* conskbd_polledio_ischar() */


static void
conskbd_mux_enqueue_msg(conskbd_pending_msg_t *msg)
{
	mutex_enter(&conskbd_msgq_lock);
	msg->kpm_next = conskbd_msg_queue;
	conskbd_msg_queue = msg;
	mutex_exit(&conskbd_msgq_lock);

}	/* conskbd_mux_enqueue_msg() */

/*
 * the messages in conskbd_msg_queue we just enqueue
 */
static conskbd_pending_msg_t *
conskbd_mux_find_msg(mblk_t *mp)
{
	conskbd_pending_msg_t	*msg;
	struct iocblk		*iocp;
	uint_t	id;

	mutex_enter(&conskbd_msgq_lock);
	msg = conskbd_msg_queue;

	iocp = (struct iocblk *)mp->b_rptr;
	ASSERT(iocp);
	id = iocp->ioc_id;
	while (msg && msg->kpm_req_id != id) {
		msg = msg->kpm_next;
	}
	mutex_exit(&conskbd_msgq_lock);

	return (msg);

}	/* conskbd_mux_find_msg() */


static void
conskbd_mux_dequeue_msg(conskbd_pending_msg_t *msg)
{
	conskbd_pending_msg_t *prev;
	conskbd_pending_msg_t *p;

	mutex_enter(&conskbd_msgq_lock);
	prev = conskbd_msg_queue;

	for (p = prev; p && p != msg; p = p->kpm_next)
		prev = p;

	ASSERT(p && p == msg);

	if (prev == p) {
		conskbd_msg_queue = msg->kpm_next;
	} else {
		prev->kpm_next = p->kpm_next;
	}
	p->kpm_next = NULL;
	mutex_exit(&conskbd_msgq_lock);

}	/* conskbd_mux_dequeue_msg() */

#ifdef DEBUG
/*ARGSUSED*/
void
conskbd_dprintf(const char *fmt, ...)
{
	char buf[256];
	va_list ap;

	va_start(ap, fmt);
	(void) vsprintf(buf, fmt, ap);
	va_end(ap);

	cmn_err(CE_CONT, "conskbd: %s", buf);

}	/* conskbd_dprintf() */
#endif